View | Details | Raw Unified | Return to bug 29672
Collapse All | Expand All

(-)a/Koha/Plugins.pm (-13 / +48 lines)
Lines 30-35 use Try::Tiny; Link Here
30
use C4::Context;
30
use C4::Context;
31
use C4::Output;
31
use C4::Output;
32
32
33
use Koha::Cache::Memory::Lite;
33
use Koha::Exceptions::Plugin;
34
use Koha::Exceptions::Plugin;
34
use Koha::Plugins::Methods;
35
use Koha::Plugins::Methods;
35
36
Lines 74-98 updated by preceding plugins, provided that these plugins support that. Link Here
74
sub call {
75
sub call {
75
    my ($class, $method, @args) = @_;
76
    my ($class, $method, @args) = @_;
76
77
78
    return unless C4::Context->config('enable_plugins');
79
77
    my @responses;
80
    my @responses;
78
    if (C4::Context->config('enable_plugins')) {
81
    my @plugins = $class->get_enabled_plugins();
79
        my @plugins = $class->new({ enable_plugins => 1 })->GetPlugins({ method => $method });
82
    @plugins = grep { $_->can($method) } @plugins;
80
        @plugins = grep { $_->can($method) } @plugins;
83
81
        # TODO: Remove warn when after_hold_create is removed from the codebase
84
    # TODO: Remove warn when after_hold_create is removed from the codebase
82
        warn "after_hold_create is deprecated and will be removed soon. Contact the following plugin's authors: " . join( ', ', map {$_->{metadata}->{name}} @plugins)
85
    warn "after_hold_create is deprecated and will be removed soon. Contact the following plugin's authors: " . join( ', ', map {$_->{metadata}->{name}} @plugins)
83
            if $method eq 'after_hold_create' and @plugins;
86
        if $method eq 'after_hold_create' and @plugins;
84
        foreach my $plugin (@plugins) {
87
85
            my $response = eval { $plugin->$method(@args) };
88
    foreach my $plugin (@plugins) {
86
            if ($@) {
89
        my $response = eval { $plugin->$method(@args) };
87
                warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name}, $@);
90
        if ($@) {
91
            warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name}, $@);
92
            next;
93
        }
94
95
        push @responses, $response;
96
    }
97
98
    return @responses;
99
}
100
101
sub get_enabled_plugins {
102
    my ($class) = @_;
103
104
    return unless C4::Context->config('enable_plugins');
105
106
    my $cache_key = 'enabled_plugins';
107
    my $enabled_plugins = Koha::Cache::Memory::Lite->get_from_cache($cache_key);
108
    unless ($enabled_plugins) {
109
        $enabled_plugins = [];
110
        my $rs = Koha::Database->schema->resultset('PluginData');
111
        $rs = $rs->search({ plugin_key => '__ENABLED__', plugin_value => 1 });
112
        my @plugin_classes = $rs->get_column('plugin_class')->all();
113
        foreach my $plugin_class (@plugin_classes) {
114
            unless (can_load(modules => { $plugin_class => undef }, nocache => 1)) {
115
                warn "Failed to load $plugin_class: $Module::Load::Conditional::ERROR";
88
                next;
116
                next;
89
            }
117
            }
90
118
91
            push @responses, $response;
119
            my $plugin = eval { $plugin_class->new() };
92
        }
120
            if ($@ || !$plugin) {
121
                warn "Failed to instantiate plugin $plugin_class: $@";
122
                next;
123
            }
93
124
125
            push @$enabled_plugins, $plugin;
126
        }
127
        Koha::Cache::Memory::Lite->set_in_cache($cache_key, $enabled_plugins);
94
    }
128
    }
95
    return @responses;
129
130
    return @$enabled_plugins;
96
}
131
}
97
132
98
=head2 GetPlugins
133
=head2 GetPlugins
(-)a/t/db_dependent/Koha/Plugins/Plugins.t (-1 / +1 lines)
Lines 55-60 subtest 'call() tests' => sub { Link Here
55
    $schema->storage->txn_begin;
55
    $schema->storage->txn_begin;
56
    # Temporarily remove any installed plugins data
56
    # Temporarily remove any installed plugins data
57
    Koha::Plugins::Methods->delete;
57
    Koha::Plugins::Methods->delete;
58
    $schema->resultset('PluginData')->delete();
58
59
59
    t::lib::Mocks::mock_config('enable_plugins', 1);
60
    t::lib::Mocks::mock_config('enable_plugins', 1);
60
    my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
61
    my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
61
- 

Return to bug 29672