/
ShareHelpers.pm
341 lines (249 loc) · 10.1 KB
/
ShareHelpers.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package Mojolicious::Plugin::ShareHelpers;
use strict;
use warnings;
use Mojo::ByteStream 'b';
use Mojo::Base 'Mojolicious::Plugin';
our $VERSION = '0.6';
our $APP; # for app instance
has url => sub { +{
'twitter' => 'https://twitter.com/share',
'facebook' => 'https://facebook.com/sharer.php',
'vkontakte' => 'https://vk.com/share.php',
'mymailru' => 'https://connect.mail.ru/share',
'google+' => 'https://plus.google.com',
} };
sub register {
my($self, $app) = @_;
$APP = $app;
$app->helper( share_url => sub { $self->share_url ( @_ ) } );
$app->helper( share_button => sub { $self->share_button ( @_ ) } );
$app->helper( share_meta => sub { $self->share_meta ( @_ ) } );
$app->helper( is_share_agent => sub { $self->is_share_agent( @_ ) } );
}
sub share_url {
my($self, $c) = (shift, shift);
my $type = shift;
return '' unless $self->_check_type( $type );
my %args = @_;
my $param;
if ($type eq 'twitter') {
$param->{$_} = $args{$_} for qw(url via text related count lang counturl);
}
elsif ($type eq 'facebook') {
$param->{u} = $args{url };
$param->{t} = $args{text};
}
elsif ($type eq 'vkontakte') {
$param->{url} = $args{url};
}
elsif ($type eq 'mymailru') {
$param->{share_url} = $args{url};
}
elsif ($type eq 'google+') {
$APP->log->error("Google Plus doen't have share URL, use share_button");
return '';
}
my @p = grep { $param->{$_} } sort keys %$param;
return join '?', $self->url->{ $type },
@p ? join '&', map { $_ . '=' . b( $param->{$_} )->url_escape } @p : ()
;
}
sub share_button {
my($self, $c) = (shift, shift);
my $type = shift;
return '' unless $self->_check_type( $type );
my %args = @_;
my $button;
if ($type eq 'twitter') {
if ($args{iframe}) {
my $url = $c->share_url( $type, @_ );
my($param) = $url =~ /.*\?(.*)/;
$button =
qq(<iframe allowtransparency="true" frameborder="0" scrolling="no" style="width:130px; height:50px;" ) .
qq(src="https://platform.twitter.com/widgets/tweet_button.html?$param"></iframe>)
;
}
else {
my $attr; push @$attr, qq(data-$_="$args{$_}")
for grep { $args{$_} } qw(url via text related count lang counturl);
my $param = join ' ', @$attr;
$button =
qq(<a href="https://twitter.com/share" class="twitter-share-button" $param>Tweet</a>) .
qq(<script type="text/javascript" src="https://platform.twitter.com/widgets.js"></script>)
;
}
}
elsif ($type eq 'facebook') {
if ($args{fb}) {
my $attr = { type => $args{type}, href => $args{url}, class => $args{class} };
my $param = join ' ', map { qq($_="$attr->{$_}") } grep { $attr->{$_} } sort keys %$attr;
$button =
qq(<fb:share-button $param></fb:share-button>)
;
}
else {
my $attr = { type => $args{type}, share_url => $args{url} };
my $param = join ' ', map { qq($_="$attr->{$_}") } grep { $attr->{$_} } sort keys %$attr;
$button =
qq(<a name="fb_share" $param>$args{title}</a>) .
qq(<script src="https://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>)
;
}
}
elsif ($type eq 'vkontakte') {
my $url = $args{url} ? qq({url: "$args{url}"}) : 'false';
my $attr = { type => $args{type}, text => $args{title} };
my $param = join ', ', map { qq($_: "$attr->{$_}") } grep { $attr->{$_} } sort keys %$attr;
$button =
qq(<script type="text/javascript" src="https://vk.com/js/api/share.js?146" charset="windows-1251"></script>) .
qq(<script type="text/javascript">document.write(VK.Share.button($url, {$param}));</script>)
;
}
elsif ($type eq 'mymailru') {
use utf8;
my $url = $c->share_url( $type, @_ );
$args{type } ||= '';
$args{title} ||= 'В Мой Мир';
$button =
qq(<script src="https://cdn.connect.mail.ru/js/share/2/share.js" type="text/javascript"></script>) .
qq(<a class="mrc__share" type="$args{type}" href="$url">$args{title}</a>)
;
}
elsif ($type eq 'google+') {
my $attr = { size => $args{size}, href => $args{url}, count => $args{count}, callback => $args{callback} };
my $param = join ' ', 'class="g-plusone"', map { qq(data-$_="$attr->{$_}") } grep { $attr->{$_} } sort keys %$attr;
my $script = join ', ', map { qq($_: "$args{$_}") } grep { $args{$_} } qw(lang parsetags);
$button =
(
$args{noscript}
? ''
: qq(<script type="text/javascript" src="https://apis.google.com/js/plusone.js">) . ( $script ? "{$script}" : '' ) . qq(</script>\n)
) .
qq(<div $param></div>)
;
}
return $button;
}
sub share_meta {
my($self, $c) = (shift, shift);
my %args = @_;
$_ = b($_)->xml_escape->to_string for grep {$_} @args{qw(title description)};
return join "\n",
@_ ? qq(<meta name="medium" content="mult"/>) : '',
$args{og} ? (
$args{fb_app_id} ? qq(<meta property="fb:app_id" content="$args{fb_app_id}"/>) : (),
qq(<meta property="og:site_name" content="$args{site_name}" />),
qq(<meta property="og:type" content="website" />),
map { $args{$_} ? qq(<meta property="og:$_" content="$args{$_}"/>) : () }
qw(image title description)
) : (),
$args{title} ? qq(<meta name="title" content="$args{title}"/>) : (),
$args{description} ? qq(<meta name="description" content="$args{description}"/>) : (),
$args{image} ? qq(<link rel="image_src" href="$args{image}" />) : (),
$args{url} ? (
qq(<link rel="target_url" href="$args{url}"/>),
qq(<link rel="canonical" href="$args{url}"/>),
) : (),
;
}
sub is_share_agent {
my($self, $c) = (shift, shift);
my $ua = $c->req->headers->user_agent;
my $range = $c->req->headers->header('Range');
my $enc = $c->req->headers->header('Accept-Encoding');
my $agent =
$ua =~ /facebookexternalhit/ && $range && $enc eq 'gzip' ? 'facebook' :
$ua =~ /Mozilla/ && $range && $enc =~ /gzip/ ? 'vkontakte' : # XXX: add cp1251
''
;
$APP->log->debug(qq(Found the share agent "$agent")) if $agent;
return $agent;
}
sub _check_type {
my $self = shift;
my $type = shift || '';
if (!$type) {
$APP->log->debug('Missed the share type');
return;
}
elsif (! exists $self->url->{ $type }) {
my $types = join ', ', sort keys %{$self->url};
$APP->log->debug(qq(Bad share type "$type", support types of share: $types));
return;
}
else {
return $type;
}
}
1;
__END__
=encoding UTF-8
=head1 NAME
Mojolicious::Plugin::ShareHelpers - A Mojolicious Plugin for generate share urls, buttons and meta for Twitter, Facebook, VKontakte, MyMailRU and Google Plus
=head1 SYNOPSIS
# Mojolicious
$self->plugin('share_helpers');
# Mojolicious::Lite
plugin 'share_helpers';
# share urls:
<a href="<%== share_url 'twitter', url => $url, text => $text, via => 'sharifulin' %>">Share to Twitter</a>
<a href="<%== share_url 'facebook', url => $url, text => $text %>">Share to Facebook</a>
<a href="<%== share_url 'vkontakte', url => $url %>">Share to ВКонтакте</a>
<a href="<%== share_url 'mymailru', url => $url %>">Share to Мой Мир</a>
# share buttons:
%== share_button 'twitter', url => 'http://mojolicio.us', text => 'Viva la revolution!', via => 'sharifulin';
%== share_button 'facebook', url => 'http://mojolicio.us', type => 'button_count', title => 'Share it';
%== share_button 'vkontakte', url => 'http://mojolicio.us', type => 'round', title => 'Save';
%== share_button 'mymailru', url => 'http://mojolicio.us', type => 'button_count', title => 'Share to Мой Мир';
# google plus button +1:
%== share_button 'google+', lang => 'ru'
%== share_button 'google+', noscript => 1, size => 'tall', url => 'http://mojolicio.us'
# generate meta for share
%== share_meta title => 'Mojolicious', description => 'Viva la revolition!', url => 'http://mojolicio.us', image => 'http://mojolicious.org/webinabox.png'
%== share_meta title => 'Mojolicious', description => 'Viva la revolition!', url => 'http://mojolicio.us', image => 'http://mojolicious.org/webinabox.png', og => 1, fb_app_id => 1234567890, site_name => 'Site Name'
# check share agent, it may returns string such as 'facebook' or 'twitter' or 'vkontakte' or empty string
%= is_share_agent
=head1 DESCRIPTION
L<Mojolicous::Plugin::ShareHelpers> is a plugin for generate share url, share button and share meta (Twitter, Facebook, VKontakte).
Plugin adds a C<share_url>, C<share_button>, C<share_meta> and C<is_share_agent> helpers to L<Mojolicious>.
=head1 SHARE API
=over 5
=item * Twitter Share L<http://dev.twitter.com/pages/tweet_button>
=item * Facebook Share L<http://developers.facebook.com/docs/share>
=item * VK Share L<http://vk.com/pages.php?act=share>
=item * MyMailRU Share L<http://api.mail.ru/sites/plugins/share/extended/>
=item * Google Plus L<http://code.google.com/intl/ru-RU/apis/+1button/>
=back
=head1 METHODS
L<Mojolicious::Plugin::ShareHelpers> inherits all methods from
L<Mojolicious::Plugin> and implements the following new ones.
=head2 C<register>
$plugin->register;
Register plugin hooks in L<Mojolicious> application.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
=head1 AUTHOR
Anatoly Sharifulin <sharifulin@gmail.com>
=head1 BUGS
Please report any bugs or feature requests to C<bug-mojolicious-plugin-sharehelpers at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.htMail?Queue=Mojolicious-plugin-sharehelpers>. We will be notified, and then you'll
automatically be notified of progress on your bug as we make changes.
=over 5
=item * Github
L<http://github.com/sharifulin/Mojolicious-plugin-sharehelpers/tree/master>
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.htMail?Dist=Mojolicious-plugin-sharehelpers>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Mojolicious-plugin-sharehelpers>
=item * CPANTS: CPAN Testing Service
L<http://cpants.perl.org/dist/overview/Mojolicious-plugin-sharehelpers>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Mojolicious-plugin-sharehelpers>
=item * Search CPAN
L<http://search.cpan.org/dist/Mojolicious-plugin-sharehelpers>
=back
=head1 COPYRIGHT & LICENSE
Copyright (C) 2010-2013 by Anatoly Sharifulin.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut