Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lh 617 html head callbacks #144

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/ConfigAssistant.pack/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ key: ConfigAssistant
author_link: http://openmelody.org/
author_name: "Byrne Reese, Open Melody Software Group"
description: This plugin provides a simple YAML based framework for creating plugin and theme configuration options.
version: 2.1.9
version: 2.1.10
static_version: 4
schema_version: 2

Expand Down
67 changes: 31 additions & 36 deletions addons/ConfigAssistant.pack/lib/ConfigAssistant/Plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use warnings;
use Carp qw( croak );
use MT::Util
qw( relative_date offset_time offset_time_list epoch2ts
ts2epoch format_ts encode_html decode_html dirify );
ts2epoch format_ts encode_html decode_html dirify );
use ConfigAssistant::Util
qw( find_theme_plugin find_template_def find_option_def
find_option_plugin process_file_upload );
find_option_plugin process_file_upload
plugin_static_web_path plugin_static_file_path );
use JSON;

# use MT::Log::Log4perl qw( l4mtdump ); use Log::Log4perl qw( :resurrect );
our $logger;

Expand All @@ -21,32 +21,29 @@ sub tag_plugin_static_web_path {
if ( !$obj ) {
return
$ctx->error(
MT->translate(
"The plugin you specified '[_2]' in '[_1]' "
. "could not be found.",
$ctx->stash('tag'),
$sig
)
MT->translate(
"The plugin you specified '[_2]' in '[_1]' "
. "could not be found.",
$ctx->stash('tag'),
$sig
)
);
}
elsif ( $obj->registry('static_version') ) {
my $url = MT->instance->static_path;
$url .= '/' unless $url =~ m!/$!;
$url .= 'support/plugins/' . $obj->id . '/';
return $url;
return plugin_static_web_path($obj);
}
else {

# TODO - perhaps this should default to: mt-static/plugins/$sig?
return
$ctx->error(
MT->translate(
"The plugin you specified '[_2]' in '[_1]' has not"
. "registered a static directory. Please use "
. "<mt:StaticWebPath> instead.",
$ctx->stash('tag'),
$sig
)
MT->translate(
"The plugin you specified '[_2]' in '[_1]' has not"
. "registered a static directory. Please use "
. "<mt:StaticWebPath> instead.",
$ctx->stash('tag'),
$sig
)
);
}
} ## end sub tag_plugin_static_web_path
Expand All @@ -58,28 +55,26 @@ sub tag_plugin_static_file_path {
if ( !$obj ) {
return
$ctx->error(
MT->translate(
"The plugin you specified '[_2]' in '[_1]' "
. "could not be found.",
$ctx->stash('tag'),
$sig
)
MT->translate(
"The plugin you specified '[_2]' in '[_1]' "
. "could not be found.",
$ctx->stash('tag'),
$sig
)
);
}
elsif ( $obj->registry('static_version') ) {
return
File::Spec->catdir( MT->instance->static_file_path,
'support', 'plugins', $obj->id );
return plugin_static_file_path($obj);
}
else {
return
$ctx->error(
MT->translate(
"The plugin you specified in '[_1]' has not "
. "registered a static directory. Please use "
. "<mt:StaticFilePath> instead.",
$_[0]->stash('tag')
)
MT->translate(
"The plugin you specified in '[_1]' has not "
. "registered a static directory. Please use "
. "<mt:StaticFilePath> instead.",
$_[0]->stash('tag')
)
);
}
} ## end sub tag_plugin_static_file_path
Expand Down Expand Up @@ -1011,7 +1006,7 @@ sub _hdlr_field_category_list {
my ( $ctx, $args, $cond ) = @_;
my $field = $ctx->stash('field') or return _no_field($ctx);
my $value = _get_field_value($ctx);
my @ids = ref($value) eq 'ARRAY' ? @$value : ($value);
my @ids = ref($value) eq 'ARRAY' ? @$value : ($value);
my $class = $ctx->stash('obj_class');

my @categories = MT->model($class)->load( { id => \@ids } );
Expand Down
23 changes: 20 additions & 3 deletions addons/ConfigAssistant.pack/lib/ConfigAssistant/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package ConfigAssistant::Util;
use strict;
use warnings;
use base 'Exporter';
our @EXPORT_OK = qw( find_theme_plugin find_template_def find_option_def
find_option_plugin process_file_upload
ERROR SUCCESS OVERWRITE NO_UPLOAD );
our @EXPORT_OK
= qw( find_theme_plugin find_template_def find_option_def
find_option_plugin process_file_upload
plugin_static_web_path plugin_static_file_path
ERROR SUCCESS OVERWRITE NO_UPLOAD );

use MT::Util qw( encode_url );

Expand All @@ -14,6 +16,21 @@ sub SUCCESS () {1}
sub OVERWRITE () {2}
sub NO_UPLOAD () {3}

sub plugin_static_web_path {
my ($plugin) = @_;
my $url = MT->instance->static_path;
$url .= '/' unless $url =~ m!/$!;
$url .= 'support/plugins/' . $plugin->id . '/';
return $url;
}

sub plugin_static_file_path {
my ($plugin) = @_;
return File::Spec->catdir( MT->instance->static_file_path,
'support', 'plugins', $plugin->id );

}

sub process_file_upload {
my $app = shift;
my ( $param_name, $scope, $extra_path, %upload_param ) = @_;
Expand Down
97 changes: 96 additions & 1 deletion lib/MT/Template/ContextHandlers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use MT::Util qw( start_end_day start_end_week
format_ts offset_time_list first_n_words dirify get_entry
encode_html encode_js remove_html wday_from_ts days_in
spam_protect encode_php encode_url decode_html encode_xml
decode_xml relative_date asset_cleanup );
decode_xml relative_date asset_cleanup caturl trim );
use MT::Request;
use Time::Local qw( timegm timelocal );
use MT::Promise qw( delay );
Expand Down Expand Up @@ -258,6 +258,8 @@ sub core_tags {
'App:ListFilters' => \&_hdlr_app_list_filters,
'App:ActionBar' => \&_hdlr_app_action_bar,
'App:Link' => \&_hdlr_app_link,
'App:HTMLHead' => \&_hdlr_app_html_head,

Var => \&_hdlr_get_var,
CGIPath => \&_hdlr_cgi_path,
AdminCGIPath => \&_hdlr_admin_cgi_path,
Expand Down Expand Up @@ -2391,6 +2393,99 @@ EOT

###########################################################################

=head2 App:HTMLHead

This outputs any elements registered via the html_head registry keys.

B<Attributes:>

=over 4

=item * type (optional)

Provides a way for the tag to output one or more specific types of elements,
e.g. all of the `<script>` tags or all of the registered stylesheets.
Acceptable values are: "scripts," "links" and "stylesheets." The default
value is: "scripts,links,stylesheets."

=item * indent (optional)

The number of spaces to indent elements added to the <html> <head> element.

=back

=cut

sub _is_absolute {
my ($url) = @_;
return 1 if ( $url =~ /^(https?|ftp|file):/ ||
$url =~ /^\// ||
$url !~ /\// );
}

sub _hdlr_app_html_head {
my ( $ctx, $args, $cond ) = @_;
my $app = MT->instance;
my $types = $args->{type} || 'stylesheets,scripts,links';
my $indent = $args->{indent} || 0;
my $out = '';
for my $sig ( keys %MT::Plugins ) {
my $plugin = $MT::Plugins{$sig};
my $obj = $MT::Plugins{$sig}{object};
my $r = $obj->{registry};
next unless $r->{html_head};
foreach my $type (split(',',$types)) {
$type = trim($type);
next unless $r->{html_head}->{$type};
foreach my $f (@{$r->{html_head}->{$type}}) {
if ( my $modes_str = $f->{modes} ) {
my $mode = $app->mode();
next unless ($modes_str =~ /\b$mode\b/);
}
if ( my $cond = $f->{condition} ) {
if ( !ref($cond) ) {
$cond = $f->{condition} = $app->handler_to_coderef($cond);
}
next unless $cond->($app);
}
if ($type eq 'stylesheets') {
my $url = $f->{href};
unless (_is_absolute($url)) {
$url = caturl(ConfigAssistant::Util::plugin_static_web_path($obj),$url);
$url .= '?v='.$r->{version} if $r->{version} > 0;
}
my $link = (' ' x $indent).'<link rel="stylesheet" type="text/css" href="'.$url.'" />'."\n";
$out .= $link;
} elsif ($type eq 'links') {
my $url = $f->{href};
$url = caturl(ConfigAssistant::Util::plugin_static_web_path($obj),$url)
unless (_is_absolute($url));
my $link = (' ' x $indent).'<link rel="'.$f->{rel}.'" ';
$link .= 'type='.$f->{type}.'" ' if ($f->{type});
$link .= 'href="'.$url.'" />'."\n";
$out .= $link;
} elsif ($type eq 'scripts') {
if ($f->{src}) {
my $url = $f->{src};
unless (_is_absolute($url)) {
$url = caturl(ConfigAssistant::Util::plugin_static_web_path($obj),$url);
$url .= '?v='.$r->{version} if $r->{version} > 0;
}
my $link = (' ' x $indent).'<script type="text/javascript" src="'.$url.'"></script>'."\n";
$out .= $link;
} elsif ($f->{content}) {
my $link .= (' ' x $indent).'<script type="text/javascript">'."\n".$f->{content}."\n".(' ' x $indent)."</script>\n";
$out .= $link;
}
}
}
}
}
return $out;
} ## end sub _hdlr_app_html_head

###########################################################################

=head2 App:SettingGroup

An application template tag used to wrap a number of L<App:Setting> tags.
Expand Down
3 changes: 3 additions & 0 deletions tmpl/cms/include/header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
<mt:if name="feed_url">
<link type="application/atom+xml" rel="alternate" <mt:if name="feed_name">title="<mt:var name="feed_name" escape="html">" </mt:if>href="<mt:var name="feed_url" escape="html">" />
</mt:if>
<mtapp:htmlhead type="links" indent="2">
<mt:unless name="optimize_ui">
<link rel="stylesheet" href="<$mt:var name="static_uri"$>styles.css?v=<mt:var name="mt_version_id" escape="url">" type="text/css" />
<mt:else>
<link rel="stylesheet" href="<$mt:var name="static_uri"$>css/main.css?v=<mt:var name="mt_version_id" escape="url">" type="text/css" />
</mt:unless>
<link rel="stylesheet" href="<$mt:var name="static_uri"$>melody/styles.css?v=<mt:var name="mt_version_id" escape="url">" type="text/css" />
<mtapp:htmlhead type="stylesheets" indent="2">
<!--[if IE]>
<link rel="stylesheet" href="<$mt:var name="static_uri"$>css/hacks/ie.css?v=<mt:var name="mt_version_id" escape="url">" type="text/css" />
<![endif]-->
Expand Down Expand Up @@ -62,6 +64,7 @@
<mt:if name="local_lang_id" ne="en-us">
<script type="text/javascript" src="<$mt:var name="static_uri"$>mt_<$mt:var name="local_lang_id"$>.js?v=<mt:var name="mt_version_id" escape="URL">" charset="utf-8"></script>
</mt:if>
<mtapp:htmlhead type="scripts" indent="2">
<$mt:var name="js_include"$>
</head>

Expand Down