plugins/coolplugin/
plugins/coolplugin/lib/
plugins/coolplugin/tmpl/
mt-static/plugins/coolplugin/
$mt/lib/MT/Plugin.pm
and other plugins.name: The name of your plugin. It is recommended that such a short string fit. id: PluginNameWordName author_link: Link to plugin author author_name: plugin author name description: <__ trans phrase = "Description of the plugin, appears if you click on the plugin's name in the plugin menu"> version: Version of Plugin schema_version: Version of the Schema of the Data Plugin plugin_link: Link to the page where you can merge the plugin doc_link: Link to the page where the description of the plugin blog_config_template: name_configuration_configuration.tpl l10n_class: Plugin :: ClassInternationalization icon: Picture Plugin. {gif / png / jpg / itd} settings: How to configure: default: "default value" scope: blog global setup: default: "value more" init: $ Plugin :: Module :: Initialization_Function callbacks: SomeHack: $ Plugin :: Module :: Hak_function tags: function: Tagname: $ Plugin :: Module :: tag_function block: Block Tag Name: $ Plugin :: Module :: block_ tag_function object_types: an object: field: type # this field will be added to the object in the database field: meta type # this field will be stored in the metadata table and will not require updating the database structure applications: # about this later, while you forgot
icon:
was found in the MT code itself, and not in the documentation. package OurPlugin::Plugin use base 'MT::Plugin'; sub cb_init { my $p = shift; return bless $p, 'OurPlugin::Plugin'; }
init: $OurPlugin::Plugin::cb_init
sub save_config { my ($plugin, $args, $scope) = @_; my @errors; # if (@errors) { return $plugin->error("<br />\n".join("<br />\n", @errors)); } return $plugin->SUPER::save_config($args, $scope); } ## end sub save_config
sub load_config { my ($plugin, $args, $scope) = @_; $plugin->SUPER::load_config($args, $scope); # - , $plugin->error() if($plugin->errstr) { # Set $args->{error} to display error banner right before plugin settings $args->{error} = $plugin->errstr; } } ## end sub load_config
<mt:if name="error"> <mtapp:statusmsg id="generic-error" class="error"> <mt:var name="error"> </mtapp:statusmsg> </mt:if>
load_config
comes to the rescue load_config
to set to $args->{...}
necessary variables, without which it would not be possible to normally issue any prompts to the user. <mt:If tag="Version" lt="5"> <script type="text/javascript"> // prevent double-load of jQuery, save if jQ already loaded its state if(window.jQuery) { window.__PLUG_jQ = window.jQuery; window.__PLUGB = window.$; } </script> <script type="text/javascript" src="<$mt:StaticWebPath$>jquery/jquery.js"></script> <script type="text/javascript"> // Restore there jQ state, if it was loaded before if(window.__PLUG_jQ) { window.jQuery = window.__PLUG_jQ; window.$ = window.__PLUGB; window.__PLUG_jQ = undefined; window.__PLUGB = undefined; } </script> </mt:If>
<$mt:$>
. sub tag_somemytag { my ($ctx, $args, $cond) = @_; my $blog_id = $ctx->stash('blog_id'); my $some_param = MT->component('OurPlugin')->get_config_value('some_param', 'blog:' . $blog_id); # my $config = MT->component('OurPlugin')->get_config_hash('blog:' . $blog_id); # .... } ## end sub tag_somemytag
sub tag_somemytag { my ($ctx, $args, $cond) = @_; my $text = "<$mt:SomeOtherTag$>"; .... my $t = MT::Template->new; $t->text($t); return $t->build($ctx) or $ctx->error($t->errstr); } ## end sub tag_somemytag
$ctx->error("error message");
, but in addition there are also “sharpened” error methods, for example $ctx->_no_entry_error();
. sub tag_myloop { my($ctx, $args, $cond) = @_; my $builder = $ctx->stash('builder'); # . # , - my $tokens = $ctx->stash('tokens'); # , my $res = ''; my $vars = $ctx->{__stash}{vars} ||= {}; for my $i (0..$#looparray) { my $item = $looparray[$i]; # , local $vars->{__item__} = $item; # local $vars->{__first__} = $i == 0; local $vars->{__last__} = ($i==($#looparray-1)); local $vars->{__odd__} = ($i % 2) == 0; local $vars->{__even__} = ($i % 2) == 1; local $vars->{__counter__} = $i+1; defined(my $out = $builder->build($ctx, $tokens, $cond)) or return $ctx->error($builder->errstr); $res .= $out; } return $res; }
MT::App::CMS::template_param.
. And if you need to change the output of the page, then template_output is used. These hooks are extremely useful if you want to create your own page in the admin panel (about which, if there is demand, in the next post). When and if you need to find out what kind of hooks are there, feel free to grep the MT code for run_callbacks
, and analyze the POD of the corresponding module.callbacks: MT :: Entry :: pre_save: $ OurPlugin :: Plugin :: cb_entry_pre_save # cms_pre_save.entry: $ OurPlugin :: Plugin :: cb_cms_pre_save_entry # api_pre_save.entry: $ OurPlugin :: Plugin :: cb_api_pre_save_entry
MT::Entry::pre_save
- is called from the ORM handler immediately before being saved to the database.cms_pre_save.entry
- called before saving a record when editing through the admin areaapi_pre_save.entry
- called before saving a record when editing via RPC or ATOM sub cb_entry_pre_save { my ($cb, $entry, $original) = @_; my $txt = $entry->text; if ($txt ne $original->text) { my $newtxt = $txt; # regexp from twitter-blackbird-pie plugin $newtxt =~ s/([^a-zA-Z0-9_]|^)([@\xef\xbc\xa0]+)([a-zA-Z0-9_]{1,20})(\/[a-zA-Z][a-zA-Z0-9\x80-\xff-]{0,79})?/$1@<a href="http://twitter.com/intent/user?screen_name=$3" class="twitter-action">$3</a>/ug; $entry->text($newtxt) if ($newtxt ne $txt); } return 1; } ## end sub cb_entry_pre_save
applitcations: comments: methods: record_some_info: $ OurPlugin :: Plugin :: ajax_record_some_info
sub ajax_record_some_info { my ($app) = @_; my $blog = $app->blog; my $result = "{'error': 'No information supplied'}"; my $user_name = $app->param('user_name'); my $info = $app->param('info'); if ($user_name && $info) { $result = do_store_info($user_name, $info); # } $app->send_http_header(""); $app->print($result); return $app->{no_print_body} = 1; } ## end sub ajax_record_some_info
var u = mtGetUser(); if(u && !u.is_anonymous) { jQuery.post({ url: '<mt:CGIPath encode_js='1'><mt:CommentScript encode_js='1'>', '__mode': 'record_some_info', 'user_name': u.name, 'info': 'he he' });
<script>COMMENTS_URL = '<mt:CGIPath encode_js='1'><mt:CommentScript encode_js='1'>';</script>
Source: https://habr.com/ru/post/119939/
All Articles