| 1 | # Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd. |
|---|
| 2 | # This program is distributed under the terms of the |
|---|
| 3 | # GNU General Public License, version 2. |
|---|
| 4 | # |
|---|
| 5 | # $Id$ |
|---|
| 6 | |
|---|
| 7 | package MT::Plugin; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use base qw( MT::Component ); |
|---|
| 11 | |
|---|
| 12 | sub init { |
|---|
| 13 | my $plugin = shift; |
|---|
| 14 | $plugin->{__settings} = {}; |
|---|
| 15 | if (exists $plugin->{app_action_links} |
|---|
| 16 | || exists $plugin->{app_itemset_actions} |
|---|
| 17 | || exists $plugin->{upgrade_functions} |
|---|
| 18 | || exists $plugin->{app_methods} |
|---|
| 19 | || exists $plugin->{junk_filters} |
|---|
| 20 | || exists $plugin->{object_classes}) { |
|---|
| 21 | # Found in MT::Compat::v3 |
|---|
| 22 | $plugin->legacy_init(); |
|---|
| 23 | } |
|---|
| 24 | $plugin->SUPER::init(@_) or return; |
|---|
| 25 | return $plugin; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | sub id { |
|---|
| 29 | my $plugin = shift; |
|---|
| 30 | my $id = $plugin->SUPER::id(@_); |
|---|
| 31 | return $id || $plugin->{plugin_sig}; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | sub key { &MT::Component::_getset(shift, 'key', @_) } |
|---|
| 35 | sub author_name { &MT::Component::_getset_translate(shift, 'author_name', @_) } |
|---|
| 36 | sub author_link { &MT::Component::_getset(shift, 'author_link', @_) } |
|---|
| 37 | sub plugin_link { &MT::Component::_getset(shift, 'plugin_link', @_) } |
|---|
| 38 | sub config_link { &MT::Component::_getset(shift, 'config_link', @_) } |
|---|
| 39 | sub doc_link { &MT::Component::_getset(shift, 'doc_link', @_) } |
|---|
| 40 | sub description { &MT::Component::_getset_translate(shift, 'description', @_) } |
|---|
| 41 | sub settings { |
|---|
| 42 | my $plugin = shift; |
|---|
| 43 | my $s = &MT::Component::_getset($plugin, 'settings', @_); |
|---|
| 44 | unless (ref($s) eq 'MT::PluginSettings') { |
|---|
| 45 | $s = MT::PluginSettings->new($s); |
|---|
| 46 | &MT::Component::_getset($plugin, 'settings', $s); |
|---|
| 47 | } |
|---|
| 48 | return $s; |
|---|
| 49 | } |
|---|
| 50 | sub icon { &MT::Component::_getset(shift, 'icon', @_) } |
|---|
| 51 | |
|---|
| 52 | # Plugin-specific: configuration settings and data |
|---|
| 53 | sub load_config { |
|---|
| 54 | my $plugin = shift; |
|---|
| 55 | my ($param, $scope) = @_; |
|---|
| 56 | my $setting_obj = $plugin->get_config_obj($scope); |
|---|
| 57 | my $settings = $setting_obj->data; |
|---|
| 58 | %$param = %$settings; |
|---|
| 59 | foreach my $key (keys %$settings) { |
|---|
| 60 | next unless defined $key; |
|---|
| 61 | my $value = $settings->{$key}; |
|---|
| 62 | next if !defined $value or $value =~ m/\s/ or length($value) > 100; |
|---|
| 63 | $param->{$key.'_'.$value} = 1; |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | sub save_config { |
|---|
| 68 | my $plugin = shift; |
|---|
| 69 | my ($param, $scope) = @_; |
|---|
| 70 | my $pdata = $plugin->get_config_obj($scope); |
|---|
| 71 | $scope =~ s/:.*//; |
|---|
| 72 | my @vars = $plugin->config_vars($scope); |
|---|
| 73 | my $data = $pdata->data() || {}; |
|---|
| 74 | foreach (@vars) { |
|---|
| 75 | $data->{$_} = exists $param->{$_} ? $param->{$_} : undef; |
|---|
| 76 | } |
|---|
| 77 | $pdata->data($data); |
|---|
| 78 | MT->request('plugin_config.'.$plugin->id, undef); |
|---|
| 79 | $pdata->save() or die $pdata->errstr; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | sub reset_config { |
|---|
| 83 | my $plugin = shift; |
|---|
| 84 | my ($scope) = @_; |
|---|
| 85 | my $obj = $plugin->get_config_obj($scope); |
|---|
| 86 | MT->request('plugin_config.'.$plugin->id, undef); |
|---|
| 87 | $obj->remove if $obj->id; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | sub config_template { |
|---|
| 91 | my $plugin = shift; |
|---|
| 92 | my ($param, $scope) = @_; |
|---|
| 93 | if ($scope) { |
|---|
| 94 | $scope =~ s/:.*//; |
|---|
| 95 | } else { |
|---|
| 96 | $scope = 'system'; |
|---|
| 97 | } |
|---|
| 98 | my $r = $plugin->registry; |
|---|
| 99 | if (my $tmpl = $r->{"${scope}_config_template"} || |
|---|
| 100 | $r->{"config_template"} || |
|---|
| 101 | $plugin->{"${scope}_config_template"} || |
|---|
| 102 | $plugin->{'config_template'}) { |
|---|
| 103 | return $tmpl->($plugin, @_) if ref $tmpl eq 'CODE'; |
|---|
| 104 | if ($tmpl =~ /\s/) { |
|---|
| 105 | return $tmpl; |
|---|
| 106 | } else { # no spaces in $tmpl; must be a filename... |
|---|
| 107 | return $plugin->load_tmpl($tmpl); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | return undef; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | sub config_vars { |
|---|
| 114 | my $plugin = shift; |
|---|
| 115 | my ($scope) = @_; |
|---|
| 116 | $scope ||= 'system'; |
|---|
| 117 | my @settings; |
|---|
| 118 | if (my $s = $plugin->settings) { |
|---|
| 119 | foreach my $setting (@$s) { |
|---|
| 120 | my ($name, $param) = @$setting; |
|---|
| 121 | next if $scope && $param->{scope} && $param->{scope} ne $scope; |
|---|
| 122 | push @settings, $name; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | @settings; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | sub set_config_value { |
|---|
| 129 | my $plugin = shift; |
|---|
| 130 | my ($vars, $scope); |
|---|
| 131 | if (ref $_[0] eq 'HASH') { |
|---|
| 132 | ($vars, $scope) = @_; |
|---|
| 133 | } else { |
|---|
| 134 | my ($variable, $value); |
|---|
| 135 | ($variable, $value, $scope) = @_; |
|---|
| 136 | $vars = { $variable => $value }; |
|---|
| 137 | } |
|---|
| 138 | my $pdata_obj = $plugin->get_config_obj($scope); |
|---|
| 139 | my $configuration = $pdata_obj->data() || {}; |
|---|
| 140 | $configuration->{$_} = $vars->{$_} for keys %$vars; |
|---|
| 141 | $pdata_obj->data($configuration); |
|---|
| 142 | $pdata_obj->save(); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | sub get_config_obj { |
|---|
| 146 | my $plugin = shift; |
|---|
| 147 | my ($scope_id) = @_; |
|---|
| 148 | my $key; |
|---|
| 149 | my $scope = $scope_id; |
|---|
| 150 | if ($scope && $scope ne 'system') { |
|---|
| 151 | $scope =~ s/:.*//; # strip off id, leave identifier |
|---|
| 152 | $key = 'configuration:'.$scope_id; |
|---|
| 153 | } else { |
|---|
| 154 | $scope_id = 'system'; |
|---|
| 155 | $scope = 'system'; |
|---|
| 156 | $key = 'configuration'; |
|---|
| 157 | } |
|---|
| 158 | my $cfg = MT->request('plugin_config.'.$plugin->id); |
|---|
| 159 | unless ($cfg) { |
|---|
| 160 | $cfg = {}; |
|---|
| 161 | MT->request('plugin_config.'.$plugin->id, $cfg); |
|---|
| 162 | } |
|---|
| 163 | return $cfg->{$scope_id} if $cfg->{$scope_id}; |
|---|
| 164 | require MT::PluginData; |
|---|
| 165 | # calling "name" directly here to avoid localization |
|---|
| 166 | my $pdata_obj = MT::PluginData->load({plugin => $plugin->key || $plugin->{name}, |
|---|
| 167 | key => $key}); |
|---|
| 168 | if (!$pdata_obj) { |
|---|
| 169 | $pdata_obj = MT::PluginData->new(); |
|---|
| 170 | $pdata_obj->plugin($plugin->key || $plugin->{name}); |
|---|
| 171 | $pdata_obj->key($key); |
|---|
| 172 | } |
|---|
| 173 | $cfg->{$scope_id} = $pdata_obj; |
|---|
| 174 | my $data = $pdata_obj->data() || {}; |
|---|
| 175 | $plugin->apply_default_settings($data, $scope_id); |
|---|
| 176 | $pdata_obj->data($data); |
|---|
| 177 | $pdata_obj; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | sub apply_default_settings { |
|---|
| 181 | my $plugin = shift; |
|---|
| 182 | my ($data, $scope_id) = @_; |
|---|
| 183 | my $scope = $scope_id; |
|---|
| 184 | if ($scope =~ m/:/) { |
|---|
| 185 | $scope =~ s/:.*//; |
|---|
| 186 | } else { |
|---|
| 187 | $scope_id = 'system'; |
|---|
| 188 | } |
|---|
| 189 | my $defaults; |
|---|
| 190 | my $s = $plugin->settings; |
|---|
| 191 | if ($s && ($defaults = $s->defaults($scope))) { |
|---|
| 192 | foreach (keys %$defaults) { |
|---|
| 193 | $data->{$_} = $defaults->{$_} if !exists $data->{$_}; |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | sub get_config_hash { |
|---|
| 199 | my $plugin = shift; |
|---|
| 200 | $plugin->get_config_obj(@_)->data() || {}; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | sub get_config_value { |
|---|
| 204 | my $plugin = shift; |
|---|
| 205 | my ($var, $scope) = @_; |
|---|
| 206 | my $config = $plugin->get_config_hash($scope); |
|---|
| 207 | return exists $config->{$_[0]} ? $config->{$_[0]} : undef; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | package MT::PluginSettings; |
|---|
| 212 | |
|---|
| 213 | sub new { |
|---|
| 214 | my $pkg = shift; |
|---|
| 215 | my ($self) = @_; |
|---|
| 216 | $self ||= []; |
|---|
| 217 | if (ref $self eq 'HASH') { |
|---|
| 218 | # convert a hash-style setting structure into what PluginSettings |
|---|
| 219 | # expects |
|---|
| 220 | my $settings = []; |
|---|
| 221 | foreach my $key (keys %$self) { |
|---|
| 222 | if (defined $self->{$key}) { |
|---|
| 223 | push @$settings, [ $key, $self->{$key} ]; |
|---|
| 224 | } |
|---|
| 225 | else { |
|---|
| 226 | push @$settings, [ $key ]; |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | @$settings = sort { ( $a->[1] ? $a->[1]->{order} || 0 : 0 ) |
|---|
| 230 | <=> ( $b->[1] ? $b->[1]->{order} || 0 : 0 ) } @$settings; |
|---|
| 231 | $self = $settings; |
|---|
| 232 | } |
|---|
| 233 | # Lowercase all settings keys |
|---|
| 234 | foreach my $setting (@$self) { |
|---|
| 235 | my ($name, $param) = @$setting; |
|---|
| 236 | $param->{lc $_} = $param->{$_} for keys %$param; |
|---|
| 237 | } |
|---|
| 238 | bless $self, $pkg; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | sub defaults { |
|---|
| 242 | my $settings = shift; |
|---|
| 243 | my ($scope) = @_; |
|---|
| 244 | my $defaults = {}; |
|---|
| 245 | foreach my $setting (@$settings) { |
|---|
| 246 | my ($name, $param) = @$setting; |
|---|
| 247 | next unless exists $param->{default}; |
|---|
| 248 | next if $scope && $param->{scope} && $param->{scope} ne $scope; |
|---|
| 249 | $defaults->{$name} = $param->{default}; |
|---|
| 250 | } |
|---|
| 251 | $defaults; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | 1; |
|---|
| 255 | __END__ |
|---|
| 256 | |
|---|
| 257 | =head1 NAME |
|---|
| 258 | |
|---|
| 259 | MT::Plugin - Movable Type class that describes a plugin |
|---|
| 260 | |
|---|
| 261 | =head1 SYNOPSIS |
|---|
| 262 | |
|---|
| 263 | package MyPlugin; |
|---|
| 264 | |
|---|
| 265 | use base 'MT::Plugin'; |
|---|
| 266 | use vars qw($VERSION); |
|---|
| 267 | $VERSION = 1.12; |
|---|
| 268 | |
|---|
| 269 | my $plugin = new MyPlugin({ |
|---|
| 270 | name => 'My Plugin', |
|---|
| 271 | version => $VERSION, |
|---|
| 272 | author_name => 'Conan the Barbaraian', |
|---|
| 273 | author_link => 'http://example.com/', |
|---|
| 274 | plugin_link => 'http://example.com/mt-plugins/example/', |
|---|
| 275 | description => 'Frobnazticates all Diffyhorns', |
|---|
| 276 | config_link => 'myplugin.cgi', |
|---|
| 277 | settings => new MT::PluginSettings([ |
|---|
| 278 | ['option1', { Default => 'default_setting' }], |
|---|
| 279 | ['option2', { Default => 'system_default', Scope => 'system' }], |
|---|
| 280 | ['option2', { Scope => 'blog' }], |
|---|
| 281 | ]), |
|---|
| 282 | config_template => \&config_tmpl |
|---|
| 283 | }); |
|---|
| 284 | MT->add_plugin($plugin); |
|---|
| 285 | |
|---|
| 286 | # Alternatively, instantiating MT::Plugin itself |
|---|
| 287 | |
|---|
| 288 | my $plugin = new MT::Plugin({ |
|---|
| 289 | name => "Example Plugin", |
|---|
| 290 | version => 1.12, |
|---|
| 291 | author_name => "Conan the Barbarian", |
|---|
| 292 | author_link => "http://example.com/", |
|---|
| 293 | plugin_link => "http://example.com/mt-plugins/example/", |
|---|
| 294 | description => "Frobnazticates all Diffyhorns", |
|---|
| 295 | config_link => 'myplugin.cgi', |
|---|
| 296 | doc_link => <documentation URL>, |
|---|
| 297 | settings => new MT::PluginSettings([ |
|---|
| 298 | ['option1', { Default => 'default_setting' }], |
|---|
| 299 | ['option2', { Default => 'system_default', Scope => 'system' }], |
|---|
| 300 | ['option2', { Scope => 'blog' }], |
|---|
| 301 | ]), |
|---|
| 302 | config_template => \&config_tmpl |
|---|
| 303 | }); |
|---|
| 304 | MT->add_plugin($plugin); |
|---|
| 305 | |
|---|
| 306 | =head1 DESCRIPTION |
|---|
| 307 | |
|---|
| 308 | An I<MT::Plugin> object holds data about a plugin which is used to help |
|---|
| 309 | users understand what the plugin does and let them configure the |
|---|
| 310 | plugin. |
|---|
| 311 | |
|---|
| 312 | Normally, a plugin will construct an I<MT::Plugin> object and pass it |
|---|
| 313 | to the C<add_plugin> method of the I<MT> class: |
|---|
| 314 | |
|---|
| 315 | MT->add_plugin($plugin); |
|---|
| 316 | |
|---|
| 317 | This will help populate additional information about the plugin on |
|---|
| 318 | the plugin listing in the MT system overview. |
|---|
| 319 | |
|---|
| 320 | When adding callbacks, you will use the plugin object as well; this |
|---|
| 321 | object is used to help the user identify errors that arise in |
|---|
| 322 | executing the callback. For example, to add a callback which is |
|---|
| 323 | executed before the I<MT::Foo> object is saved to the database, you might |
|---|
| 324 | make a call like this: |
|---|
| 325 | |
|---|
| 326 | MT::Foo->add_callback("pre_save", 10, $plugin, \&callback_function); |
|---|
| 327 | |
|---|
| 328 | This call will tell I<MT::Foo> to call the function |
|---|
| 329 | C<callback_function> just before executing any C<save> operation. The |
|---|
| 330 | number '10' is signalling the priority, which controls the order in |
|---|
| 331 | which various plugins are called. Lower number callbacks are called |
|---|
| 332 | first. |
|---|
| 333 | |
|---|
| 334 | =head1 ARGUMENTS |
|---|
| 335 | |
|---|
| 336 | =over 4 |
|---|
| 337 | |
|---|
| 338 | =item * key |
|---|
| 339 | |
|---|
| 340 | The key is an optional, but recommended element of the plugin. This |
|---|
| 341 | value is used to uniquely identify the plugin and should never change |
|---|
| 342 | from one version to the next. The key is used when available in storing |
|---|
| 343 | plugin configuration data using the C<MT::PluginData> package. |
|---|
| 344 | |
|---|
| 345 | =item * name (required) |
|---|
| 346 | |
|---|
| 347 | A human-readable string identifying the plugin. This will be displayed |
|---|
| 348 | in the plugin's slug on the MT front page. |
|---|
| 349 | |
|---|
| 350 | =item * version |
|---|
| 351 | |
|---|
| 352 | The version number for the release of the plugin. Will be displayed |
|---|
| 353 | next to the plugin's name wherever listed. This information is not |
|---|
| 354 | required, but recommended. |
|---|
| 355 | |
|---|
| 356 | =item * schema_version |
|---|
| 357 | |
|---|
| 358 | If your plugin declares a list of object classes, the schema_version |
|---|
| 359 | is used to determine whether your classes require installation or |
|---|
| 360 | upgrade. MT will store your plugin's schema_version in the C<MT::Config> |
|---|
| 361 | table for future reference. |
|---|
| 362 | |
|---|
| 363 | =item * description |
|---|
| 364 | |
|---|
| 365 | A longer string giving a brief description of what the plugin does. |
|---|
| 366 | |
|---|
| 367 | =item * doc_link |
|---|
| 368 | |
|---|
| 369 | A URL pointing to some documentation for the plugin. This can be a |
|---|
| 370 | relative path, in which case it identifies documentation within the |
|---|
| 371 | plugin's distribution, or it can be an absolute URL, pointing at |
|---|
| 372 | off-site documentation. |
|---|
| 373 | |
|---|
| 374 | =item * config_link |
|---|
| 375 | |
|---|
| 376 | The relative path of a CGI script or some other configuration |
|---|
| 377 | interface for the plugin. This is relative to the "plugin |
|---|
| 378 | envelope"--that is, the directory underneath C<mt/plugins> where all |
|---|
| 379 | your plugin files live. |
|---|
| 380 | |
|---|
| 381 | =item * author_name |
|---|
| 382 | |
|---|
| 383 | The name of the individual or company that created the plugin. |
|---|
| 384 | |
|---|
| 385 | =item * author_link |
|---|
| 386 | |
|---|
| 387 | A URL pointing to the home page of the individual or company that |
|---|
| 388 | created the plugin. |
|---|
| 389 | |
|---|
| 390 | =item * plugin_link |
|---|
| 391 | |
|---|
| 392 | A URL pointing to the home page for the plugin itself. |
|---|
| 393 | |
|---|
| 394 | =item * config_template |
|---|
| 395 | |
|---|
| 396 | Defines a C<MT::Template> file by name to use for plugin configuration. |
|---|
| 397 | This value may also be a code reference, which MT would call |
|---|
| 398 | passing three arguments: the plugin object instance, a hashref of |
|---|
| 399 | C<MT::Template> parameter data and the scope value (either "system" |
|---|
| 400 | for system-wide configuration or "blog:N" where N is the active blog id). |
|---|
| 401 | |
|---|
| 402 | =item * system_config_template |
|---|
| 403 | |
|---|
| 404 | Defines a C<MT::Template> file by name to use for system-wide |
|---|
| 405 | plugin configuration. If not defined, MT will fall back to the config_template |
|---|
| 406 | setting. This value may also be a code reference, which MT would call |
|---|
| 407 | passing three arguments: the plugin object instance, a hashref of |
|---|
| 408 | C<MT::Template> parameter data and the scope value (always "system" in |
|---|
| 409 | this case). |
|---|
| 410 | |
|---|
| 411 | =item * blog_config_template |
|---|
| 412 | |
|---|
| 413 | Defines a C<MT::Template> file by name to use for weblog-specific |
|---|
| 414 | plugin configuration. If not defined, MT will fall back to the config_template |
|---|
| 415 | setting. This value may also be a code reference, which MT would call |
|---|
| 416 | passing three arguments: the plugin object instance, a hashref of |
|---|
| 417 | C<MT::Template> parameter data and the scope value (for weblogs, this |
|---|
| 418 | would be "blog:N", where N is the active blog id). |
|---|
| 419 | |
|---|
| 420 | =item * settings |
|---|
| 421 | |
|---|
| 422 | Identifies the plugin's configuration settings. |
|---|
| 423 | |
|---|
| 424 | =item * app_methods |
|---|
| 425 | |
|---|
| 426 | Used to register custom mode handlers for one or more application classes. |
|---|
| 427 | This parameter accepts a hashref of package names mapping to a hashref of |
|---|
| 428 | modes and their handlers. For example: |
|---|
| 429 | |
|---|
| 430 | app_methods => { |
|---|
| 431 | 'MT::MyPackage' => { |
|---|
| 432 | 'mode1' => \&handler1, |
|---|
| 433 | 'mode2' => \&handler2 |
|---|
| 434 | } |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | =item * callbacks |
|---|
| 438 | |
|---|
| 439 | Used to register object or application callbacks with the callback |
|---|
| 440 | system. This can be used in lieu of MT->add_callback. Example: |
|---|
| 441 | |
|---|
| 442 | callbacks => { |
|---|
| 443 | 'callback_name' => \&callback_handler, |
|---|
| 444 | 'another_callback' => { |
|---|
| 445 | priority => 1, |
|---|
| 446 | code => \&another_handler |
|---|
| 447 | } |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | =item * junk_filters |
|---|
| 451 | |
|---|
| 452 | You can register one or more junk detection filters using this |
|---|
| 453 | key. The format is: |
|---|
| 454 | |
|---|
| 455 | junk_filters => { |
|---|
| 456 | 'MyJunkFilter' => \&junk_filter_handler |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | This is an alternative to using C<MT-E<gt>register_junk_filter>. |
|---|
| 460 | |
|---|
| 461 | =item * init_app |
|---|
| 462 | |
|---|
| 463 | It's possible for a plugin to define code that is to be executed upon |
|---|
| 464 | app initialization with this parameter. It may be a simple coderef which |
|---|
| 465 | will be invoked for all MT::App instances: |
|---|
| 466 | |
|---|
| 467 | init_app => \&init_app_routine |
|---|
| 468 | |
|---|
| 469 | Or it can be a hashref that maps MT::App package names to a coderef. |
|---|
| 470 | |
|---|
| 471 | init_app => { |
|---|
| 472 | 'MT::App::CMS' => \&init_cms_app_routine, |
|---|
| 473 | 'MT::App::Comments' => \&init_comments_app_routine, |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | =item * init_request |
|---|
| 477 | |
|---|
| 478 | A plugin can use this parameter to define a routine to execute upon |
|---|
| 479 | the start of each HTTP request to an application. Like 'init_app', this |
|---|
| 480 | parameter can either be a coderef or a hashref for app-specific routines. |
|---|
| 481 | |
|---|
| 482 | =item * template_tags |
|---|
| 483 | |
|---|
| 484 | This parameter is used to declare custom tag handlers for Movable Type. |
|---|
| 485 | It is similar in function to C<MT::Template::Context-E<gt>add_tag>. |
|---|
| 486 | The parameter is given as a hashref, in this format: |
|---|
| 487 | |
|---|
| 488 | template_tags => { |
|---|
| 489 | 'TagName' => \&tag_handler |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | You may register one or more template tags in this way. |
|---|
| 493 | |
|---|
| 494 | =item * container_tags |
|---|
| 495 | |
|---|
| 496 | This parameter is used to declare custom container tags for Movable Type. |
|---|
| 497 | It is similar in function to C<MT::Template::Context-E<gt>add_container_tag>. |
|---|
| 498 | You may register one or more container tags in this way. |
|---|
| 499 | |
|---|
| 500 | container_tags => { |
|---|
| 501 | 'ContainerTag' => \&container_tag_handler |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | =item * conditional_tags |
|---|
| 505 | |
|---|
| 506 | This parameter is used to declare custom conditional tags for Movable Type. |
|---|
| 507 | This is similar in function to C<MT::Template::Context-E<gt>add_conditional_tag>. |
|---|
| 508 | You may register one or more conditional tags in this way. |
|---|
| 509 | |
|---|
| 510 | conditional_tags => { |
|---|
| 511 | 'IfCondition' => \&conditional_tag_handler |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | =item * global_filters |
|---|
| 515 | |
|---|
| 516 | This parameter is used to declare global tag attributes. It is similar in |
|---|
| 517 | function to C<MT::Template::Context-E<gt>add_global_filter>. You may |
|---|
| 518 | register 1 or more global filters in this way. |
|---|
| 519 | |
|---|
| 520 | global_filters => { |
|---|
| 521 | 'attribute_name' => \&attribute_handler |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | =item * text_filters |
|---|
| 525 | |
|---|
| 526 | Text formatting filters can be declared with this parameter. It is similar |
|---|
| 527 | in function to C<MT-E<gt>add_text_filter>. You may register 1 or more |
|---|
| 528 | filters in this way. |
|---|
| 529 | |
|---|
| 530 | text_filters => { |
|---|
| 531 | 'format_name' => { label => "My Text Format", code => \&formatter } |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | =item * tasks |
|---|
| 535 | |
|---|
| 536 | System tasks that are executed through the C<MT::TaskMgr> can be registered |
|---|
| 537 | with this parameter. The format for this parameter is as follows: |
|---|
| 538 | |
|---|
| 539 | tasks => { |
|---|
| 540 | 'task_id' => \&task_code, |
|---|
| 541 | 'weekly_task' => { |
|---|
| 542 | name => "My Weekly Task", |
|---|
| 543 | frequency => 24 * 60 * 60 * 7, # run every 7 days |
|---|
| 544 | code => \&weekly_task_code |
|---|
| 545 | } |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | The task identifier used for the key of each task registered should be |
|---|
| 549 | globally unique. You should use some unique prefix or suffix that only |
|---|
| 550 | your plugin will use. |
|---|
| 551 | |
|---|
| 552 | Refer to L<MT::TaskMgr> for more details on the MT task subsystem. |
|---|
| 553 | |
|---|
| 554 | =item * object_classes |
|---|
| 555 | |
|---|
| 556 | Declaration of a list of MT::Object descendant classes that you want |
|---|
| 557 | the MT upgrade process to maintain. |
|---|
| 558 | |
|---|
| 559 | object_classes => [ 'MyPlugin::Foo', 'MyPlugin::Bar' ] |
|---|
| 560 | |
|---|
| 561 | Define this in conjunction with a C<schema_version> to have MT maintain |
|---|
| 562 | the schema for your object packages. |
|---|
| 563 | |
|---|
| 564 | =item * upgrade_functions |
|---|
| 565 | |
|---|
| 566 | This defines a list of custom upgrade operations that you may require MT |
|---|
| 567 | to use to upgrade your schema from one version to another. For most upgrades |
|---|
| 568 | (simple column additions or size changes), this is unnecessary, but if you |
|---|
| 569 | require any kind of data manipulation or conversion, you will need to |
|---|
| 570 | register an upgrade function to handle it. Please refer to the L<MT::Upgrade> |
|---|
| 571 | package for how to declare an upgrade function. Here is an example: |
|---|
| 572 | |
|---|
| 573 | upgrade_functions => { |
|---|
| 574 | 'my_plugin_fix_field_a' => { |
|---|
| 575 | version_limit => 1.1, # runs for schema_version < 1.1 |
|---|
| 576 | code => \&plugin_field_a_fixer |
|---|
| 577 | } |
|---|
| 578 | } |
|---|
| 579 | |
|---|
| 580 | =item * icon |
|---|
| 581 | |
|---|
| 582 | Set the icon filename (not including the plugin's static web path). |
|---|
| 583 | |
|---|
| 584 | =item * log_classes |
|---|
| 585 | |
|---|
| 586 | Set the name of the plugin's log classes to use. For example: |
|---|
| 587 | |
|---|
| 588 | MT->add_plugin({ |
|---|
| 589 | name => "My custom plugin", |
|---|
| 590 | log_classes => { customlog => "MyCustomLog" } |
|---|
| 591 | }); |
|---|
| 592 | |
|---|
| 593 | =back |
|---|
| 594 | |
|---|
| 595 | =head1 METHODS |
|---|
| 596 | |
|---|
| 597 | Each of the above arguments to the constructor is also a 'getter' |
|---|
| 598 | method that returns the corresponding value. C<MT::Plugin> also offers |
|---|
| 599 | the following methods: |
|---|
| 600 | |
|---|
| 601 | =head2 MT::Plugin->new |
|---|
| 602 | |
|---|
| 603 | Return a new I<MT::Plugin> instance. This method calls the I<init> method. |
|---|
| 604 | |
|---|
| 605 | =head2 $plugin->init |
|---|
| 606 | |
|---|
| 607 | This construction helper method registers plugin I<callbacks>, |
|---|
| 608 | I<junk_filters>, I<text_filters>, I<log_classes>, I<template_tags>, |
|---|
| 609 | I<conditional_tags>, I<global_filters>. |
|---|
| 610 | |
|---|
| 611 | =head2 $plugin->init_app |
|---|
| 612 | |
|---|
| 613 | For subclassed MT::Plugins that declare this method, it is invoked when |
|---|
| 614 | the application starts up. |
|---|
| 615 | |
|---|
| 616 | =head2 $plugin->init_request |
|---|
| 617 | |
|---|
| 618 | For subclassed MT::Plugins that declare this method, it is invoked when |
|---|
| 619 | the application begins handling a new request. |
|---|
| 620 | |
|---|
| 621 | =head2 $plugin->init_tasks |
|---|
| 622 | |
|---|
| 623 | For subclassed MT::Plugins that declare this method, it is invoked when |
|---|
| 624 | the application begins handling a new task. |
|---|
| 625 | |
|---|
| 626 | =head2 $plugin->envelope |
|---|
| 627 | |
|---|
| 628 | Returns the path to the plugin, relative to the MT directory. This is |
|---|
| 629 | determined automatically when the plugin is loaded. |
|---|
| 630 | |
|---|
| 631 | =head2 $plugin->set_config_value($key, $value[, $scope]) |
|---|
| 632 | |
|---|
| 633 | See the I<get_config_value> description below. |
|---|
| 634 | |
|---|
| 635 | =head2 $plugin->get_config_value($key[, $scope]) |
|---|
| 636 | |
|---|
| 637 | These routines facilitate easy storage of simple configuration |
|---|
| 638 | options. They make use of the PluginData table in the database to |
|---|
| 639 | store a set of key-value pairs for each plugin. Call them as follows: |
|---|
| 640 | |
|---|
| 641 | $plugin->set_config_value($key, $value); |
|---|
| 642 | $value = $plugin->get_config_value($key); |
|---|
| 643 | |
|---|
| 644 | The C<name> field of the plugin object is used as the namespace for |
|---|
| 645 | the keys. Thus it would not be wise for one plugin to use the |
|---|
| 646 | same C<name> as a different plugin. |
|---|
| 647 | |
|---|
| 648 | =head2 $plugin->get_config_obj([$scope]) |
|---|
| 649 | |
|---|
| 650 | Retrieves the MT::PluginData object associated with this plugin |
|---|
| 651 | and the scope identified (which defaults to 'system' if unspecified). |
|---|
| 652 | |
|---|
| 653 | =head2 $plugin->apply_default_settings($data[, $scope]) |
|---|
| 654 | |
|---|
| 655 | Applies the default plugin settings to the given data. |
|---|
| 656 | |
|---|
| 657 | =head2 $plugin->get_config_hash([$scope]) |
|---|
| 658 | |
|---|
| 659 | Retrieves the configuration data associated with this plugin |
|---|
| 660 | and returns it a a Perl hash reference. If the scope parameter is not given, |
|---|
| 661 | the 'system' scope is assumed. |
|---|
| 662 | |
|---|
| 663 | =head2 $plugin->config_template($params[, $scope]) |
|---|
| 664 | |
|---|
| 665 | Called to retrieve a MT::Template object which will be output as the |
|---|
| 666 | configuration form for this plugin. Optionally a scope may be specified |
|---|
| 667 | (defaults to 'system'). |
|---|
| 668 | |
|---|
| 669 | my $system_tmpl = $plugin->config_template($params, 'system'); |
|---|
| 670 | my $system_tmpl = $plugin->config_template($params); |
|---|
| 671 | my $blog_tmpl = $plugin->config_template($params, 'blog:1'); |
|---|
| 672 | |
|---|
| 673 | =head2 $plugin->config_vars([$scope]) |
|---|
| 674 | |
|---|
| 675 | Returns an array of configuration setting names for the requested scope. |
|---|
| 676 | |
|---|
| 677 | =head2 $plugin->save_config($param[, $scope]) |
|---|
| 678 | |
|---|
| 679 | Handles saving configuration data from the plugin configuration form. |
|---|
| 680 | |
|---|
| 681 | my $param = { 'option1' => 'x' }; |
|---|
| 682 | $plugin->save_config($param); # saves system configuration data |
|---|
| 683 | $plugin->save_config($param, 'system'); # saves system configuration data |
|---|
| 684 | $plugin->save_config($param, 'blog:1'); # saves blog configuration data |
|---|
| 685 | |
|---|
| 686 | =head2 $plugin->load_config($param[, $scope]) |
|---|
| 687 | |
|---|
| 688 | This method returns the configuration data associated with a plugin (and |
|---|
| 689 | an optional scope) in the given I<param> hash reference. |
|---|
| 690 | |
|---|
| 691 | =head2 $plugin->reset_config($scope) |
|---|
| 692 | |
|---|
| 693 | This method drops the configuration data associated with this plugin |
|---|
| 694 | given the scope identified and reverts to th MT defaults. |
|---|
| 695 | |
|---|
| 696 | Handles loading configuration data from the plugindata table. |
|---|
| 697 | |
|---|
| 698 | =head2 $plugin->load_tmpl($file[, ...]) |
|---|
| 699 | |
|---|
| 700 | Used to load a MT::Template object relative to the plugin's directory. |
|---|
| 701 | It will scan both the plugin's directory and a directory named 'tmpl' |
|---|
| 702 | underneath it. It will passthrough parameters that follow the $file |
|---|
| 703 | parameter into the MT::Template constructor. |
|---|
| 704 | |
|---|
| 705 | =head2 MT::Plugin->select([$class]) |
|---|
| 706 | |
|---|
| 707 | Return the list of plugins available for the calling class or of the given |
|---|
| 708 | class name argument. |
|---|
| 709 | |
|---|
| 710 | =head2 $plugin->needs_upgrade() |
|---|
| 711 | |
|---|
| 712 | This method compares any previously stored schema version for the |
|---|
| 713 | plugin to the current plugin schema version number to determine if an |
|---|
| 714 | upgrade is in order. |
|---|
| 715 | |
|---|
| 716 | =head2 $plugin->translate($phrase [, @args]) |
|---|
| 717 | |
|---|
| 718 | This method translate the C<$phrase> to the currently selected language |
|---|
| 719 | using the localization module for the plugin. The C<@args> parameters |
|---|
| 720 | are passed through if given. |
|---|
| 721 | |
|---|
| 722 | =head2 $plugin->translate_templatized($text) |
|---|
| 723 | |
|---|
| 724 | This method calls the plugin's I<translate> method on any E<lt>MT_TRANSE<gt> |
|---|
| 725 | tags inside C<$text>. |
|---|
| 726 | |
|---|
| 727 | =head2 $plugin->l10n_filter($text) |
|---|
| 728 | |
|---|
| 729 | This method is an alias for the I<translate_templatized> method. |
|---|
| 730 | |
|---|
| 731 | =head2 $plugin->l10n_class() |
|---|
| 732 | |
|---|
| 733 | This method returns the I<l10n_class> attribute of the plugin or |
|---|
| 734 | I<MT::L10N> if not defined. |
|---|
| 735 | |
|---|
| 736 | =head2 $plugin->register_importer() |
|---|
| 737 | |
|---|
| 738 | This method gives plugins access to registry of importers. |
|---|
| 739 | Detailed information can be found in I<MT::Import> document. |
|---|
| 740 | |
|---|
| 741 | =head2 Additional Accessor Methods |
|---|
| 742 | |
|---|
| 743 | The following are plugin attributes with accompanying get/set methods. |
|---|
| 744 | |
|---|
| 745 | =over 4 |
|---|
| 746 | |
|---|
| 747 | =item * key |
|---|
| 748 | |
|---|
| 749 | =item * name |
|---|
| 750 | |
|---|
| 751 | =item * author_name |
|---|
| 752 | |
|---|
| 753 | =item * author_link |
|---|
| 754 | |
|---|
| 755 | =item * plugin_link |
|---|
| 756 | |
|---|
| 757 | =item * version |
|---|
| 758 | |
|---|
| 759 | =item * schema_version |
|---|
| 760 | |
|---|
| 761 | =item * config_link |
|---|
| 762 | |
|---|
| 763 | =item * doc_link |
|---|
| 764 | |
|---|
| 765 | =item * description |
|---|
| 766 | |
|---|
| 767 | =item * envelope |
|---|
| 768 | |
|---|
| 769 | =item * settings |
|---|
| 770 | |
|---|
| 771 | =item * icon |
|---|
| 772 | |
|---|
| 773 | =item * callbacks |
|---|
| 774 | |
|---|
| 775 | =item * upgrade_functions |
|---|
| 776 | |
|---|
| 777 | =item * object_classes |
|---|
| 778 | |
|---|
| 779 | =item * junk_filters |
|---|
| 780 | |
|---|
| 781 | =item * text_filters |
|---|
| 782 | |
|---|
| 783 | =item * template_tags |
|---|
| 784 | |
|---|
| 785 | =item * conditional_tags |
|---|
| 786 | |
|---|
| 787 | =item * log_classes |
|---|
| 788 | |
|---|
| 789 | =item * container_tags |
|---|
| 790 | |
|---|
| 791 | =item * global_filters |
|---|
| 792 | |
|---|
| 793 | =item * app_methods |
|---|
| 794 | |
|---|
| 795 | =item * app_action_links |
|---|
| 796 | |
|---|
| 797 | =item * app_itemset_actions |
|---|
| 798 | |
|---|
| 799 | =item * tasks |
|---|
| 800 | |
|---|
| 801 | =back |
|---|
| 802 | |
|---|
| 803 | =head1 LOCALIZATION |
|---|
| 804 | |
|---|
| 805 | Proper localization of a plugin requires a bit of structure and discipline. |
|---|
| 806 | First of all, your plugin should declare a 'l10n_class' element upon |
|---|
| 807 | registration. This defines the base L10N package to use for any translation |
|---|
| 808 | done by the plugin: |
|---|
| 809 | |
|---|
| 810 | # file l10nplugin.pl, in MT_DIR/plugins/l10nplugin |
|---|
| 811 | |
|---|
| 812 | my $plugin = new MT::Plugin({ |
|---|
| 813 | name => "My Localized Plugin", |
|---|
| 814 | l10n_class => "l10nplugin::L10N", |
|---|
| 815 | }); |
|---|
| 816 | |
|---|
| 817 | Then, you should have a file like this in C<MT_DIR/plugins/l10nplugin/lib/l10nplugin>: |
|---|
| 818 | |
|---|
| 819 | # file L10N.pm, in MT_DIR/plugins/l10nplugin/lib/l10nplugin |
|---|
| 820 | |
|---|
| 821 | package l10nplugin::L10N; |
|---|
| 822 | use base 'MT::Plugin::L10N'; |
|---|
| 823 | 1; |
|---|
| 824 | |
|---|
| 825 | And then your actual localization modules in C<MT_DIR/plugins/l10nplugin/lib/l10nplugin/L10N>: |
|---|
| 826 | |
|---|
| 827 | # file en_us.pm, in MT_DIR/plugins/l10nplugin/lib/l10nplugin/L10N |
|---|
| 828 | |
|---|
| 829 | package l10n::L10N::en_us; |
|---|
| 830 | |
|---|
| 831 | use base 'l10nplugin::L10N'; |
|---|
| 832 | |
|---|
| 833 | 1; |
|---|
| 834 | |
|---|
| 835 | Here's a French module, to localize the plugin name: |
|---|
| 836 | |
|---|
| 837 | # file fr.pm, in MT_DIR/plugins/l10nplugin/lib/l10nplugin/L10N |
|---|
| 838 | |
|---|
| 839 | package l10nplugin::L10N::fr; |
|---|
| 840 | |
|---|
| 841 | use base 'l10nplugin::L10N::en_us'; |
|---|
| 842 | |
|---|
| 843 | our %Lexicon = ( |
|---|
| 844 | "My Localized Plugin" => "Mon Plugin Localise", |
|---|
| 845 | ); |
|---|
| 846 | |
|---|
| 847 | 1; |
|---|
| 848 | |
|---|
| 849 | And, the following methods of the plugin object are I<automatically> |
|---|
| 850 | translated for you, so you don't need to invoke translate on them |
|---|
| 851 | yourself: |
|---|
| 852 | |
|---|
| 853 | =over 4 |
|---|
| 854 | |
|---|
| 855 | =item * name |
|---|
| 856 | |
|---|
| 857 | =item * author_name |
|---|
| 858 | |
|---|
| 859 | =item * description |
|---|
| 860 | |
|---|
| 861 | =back |
|---|
| 862 | |
|---|
| 863 | To translate individual strings of text from within your plugin code |
|---|
| 864 | is done like this: |
|---|
| 865 | |
|---|
| 866 | my $rebuild_str = $plugin->translate("Publish"); |
|---|
| 867 | |
|---|
| 868 | Note that if your plugin does not translate a phrase, it may be translated |
|---|
| 869 | by the MT translation matrix if the phrase is found there. So common |
|---|
| 870 | MT phrases like "Weblog", "User", etc., are already handled and you should |
|---|
| 871 | not have to duplicate the translation of such terms in your plugin's |
|---|
| 872 | localization modules. |
|---|
| 873 | |
|---|
| 874 | To translate a template that has embedded E<lt>MT_TRANSE<gt> tags in |
|---|
| 875 | them, use the C<translate_templatized> method of the plugin object. |
|---|
| 876 | |
|---|
| 877 | $tmpl = $plugin->load_tmpl("my_template.tmpl"); |
|---|
| 878 | $tmpl->param(\%param); |
|---|
| 879 | my $html = $plugin->translate_templatized($tmpl->output()); |
|---|
| 880 | |
|---|
| 881 | =head1 MT::PluginSettings |
|---|
| 882 | |
|---|
| 883 | The MT::PluginSettings package is also declared with this module. It |
|---|
| 884 | is used to define a group of settings and their defaults for the plugin. |
|---|
| 885 | These settings are processed whenever configuration data is requested |
|---|
| 886 | from the plugin. |
|---|
| 887 | |
|---|
| 888 | Example: |
|---|
| 889 | |
|---|
| 890 | $plugin->{settings} = new MT::PluginSettings([ |
|---|
| 891 | ['option1', { Default => 'default_setting' }], |
|---|
| 892 | ['option2', { Default => 'system_default', Scope => 'system' }], |
|---|
| 893 | ['option2', { Scope => 'blog' }], |
|---|
| 894 | ]); |
|---|
| 895 | |
|---|
| 896 | Settings can be assigned a default value and an applicable 'scope'. |
|---|
| 897 | Currently, recognized scopes are "system" and "blog". |
|---|
| 898 | |
|---|
| 899 | =head1 AUTHOR & COPYRIGHT |
|---|
| 900 | |
|---|
| 901 | Please see L<MT/AUTHOR & COPYRIGHT>. |
|---|
| 902 | |
|---|
| 903 | =cut |
|---|