source: extensions/ShareThis/main.inc.php @ 31054

Last change on this file since 31054 was 31054, checked in by SergeD, 9 years ago

version 1.0.1 - see change log for details

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1<?php
2/*
3Plugin Name: ShareThis
4Version: 1.0.1
5Description: Add "Share This" functionality to your site
6//Plugin URI: http://piwigo.org/ext/extension_view.php?eid=543
7Author: Serguei Dosyukov
8Author URI: http://blog.dragonsoft.us
9*/
10
11defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
12
13// +-----------------------------------------------------------------------+
14// | Define plugin constants                                               |
15// +-----------------------------------------------------------------------+
16
17define('SHARETHIS_VERSION', '1.0.1');
18define('SHARETHIS_ID',      basename(dirname(__FILE__)));
19define('SHARETHIS_PATH' ,   PHPWG_PLUGINS_PATH . SHARETHIS_ID . '/');
20define('SHARETHIS_ADMIN',   get_root_url() . 'admin.php?page=plugin-' . SHARETHIS_ID);
21define('SHARETHIS_PUBLIC',  get_absolute_root_url() . make_index_url(array('section' => 'sharethis')) . '/');
22define('SHARETHIS_DIR',     PHPWG_ROOT_PATH . PWG_LOCAL_DIR . 'sharethis/');
23if (!defined('GDTHEME_PATH')):
24  define('GDTHEME_PATH' ,   PHPWG_THEMES_PATH . 'greydragon/');
25endif;
26
27// +-----------------------------------------------------------------------+
28// | Add event handlers                                                    |
29// +-----------------------------------------------------------------------+
30// init the plugin
31add_event_handler('init', 'sharethis_init');
32
33/*
34 * this is the common way to define event functions: create a new function for each event you want to handle
35 */
36if (defined('IN_ADMIN')):
37  // file containing all admin handlers functions
38  $admin_file = SHARETHIS_PATH . 'include/admin_events.inc.php';
39
40  // admin plugins menu link
41  add_event_handler('get_admin_plugin_menu_links', 'sharethis_admin_plugin_menu_links', EVENT_HANDLER_PRIORITY_NEUTRAL, $admin_file);
42endif;
43
44function sharethis_init() {
45  global $conf;
46
47  // prepare plugin configuration
48  $conf['sharethis'] = safe_unserialize($conf['sharethis']);
49  if ($conf['sharethis']['facebook'] || $conf['sharethis']['pinterest'] || $conf['sharethis']['twitter'] || $conf['sharethis']['googleplus']):
50    add_event_handler('loc_begin_picture', 'sharethis_picture_handler');
51  endif;
52}
53
54function sharethis_picture_handler()
55{
56  global $template, $conf, $current;
57
58  if ($template->get_themeconf("name") == "greydragon"):
59    add_event_handler('gd_get_metadata', 'sharethis_get_tab_metadata');
60  else:
61    $template->set_prefilter('picture', 'sharethis_append_content');
62  endif;
63  load_language('plugin.lang', SHARETHIS_PATH);
64
65  $protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === FALSE ? 'http' : 'https';
66  $host     = $_SERVER['HTTP_HOST'];
67  $script   = $_SERVER['SCRIPT_NAME'];
68  $params   = $_SERVER['QUERY_STRING'];
69  $curUrl   = $protocol . '://' . $host . $script . '?' . $params;
70
71  $template->assign(
72    array(
73      'sharethis_facebook'   => $conf['sharethis']['facebook'],
74      'sharethis_pinterest'  => $conf['sharethis']['pinterest'],
75      'sharethis_twitter'    => $conf['sharethis']['twitter'],
76      'sharethis_googleplus' => $conf['sharethis']['googleplus'],
77      'SHARETHIS_PATH'       => SHARETHIS_PATH,
78      'CUR_PAGE'             => $curUrl
79    ));
80
81}
82
83function sharethis_get_tab_metadata($metadata) {
84
85  // pinterest &media=<logo>
86
87  $url   = '{$CUR_PAGE|escape:\'url\'}';
88  $image = '{$U_HOME}/{$current.selected_derivative->get_url()|escape:\'url\'}';
89  $title = '{$PAGE_TITLE|escape:\'url\'}';
90  $content = '{$CONTENT_DESCRIPTION|urlencode}';
91
92  $metadata[] = array(
93    "id"          => "sharethis",
94    "icon_class"  => "fa fa-share-alt",
95    "title"       => "{'Share This'|@translate}",
96    "content"     => '<span>{\'Share This:\'|@translate}</span>'
97                   . '{if $sharethis_facebook}'
98                      . '{html_head}<meta property="og:title" content="">{"\n"}'
99                       . '<meta property="og:type" content="article">{"\n"}'
100                       . '<meta property="og:url" content="{$CUR_PAGE}">{"\n"}'
101                       . '<meta property="og:image" content="{$U_HOME}/{$current.selected_derivative->get_url()}">{"\n"}'
102                       . '{/html_head}'
103                       . '<a href="http://www.facebook.com/sharer/sharer.php?p[title]=' . $title . '&amp;p[url]=' . $url . '&amp;p[images][0]=' . $image . '&amp;p[summary]=' . $content . '" onclick="window.open(this.href, \'\', \'toolbar=0,status=0,width=700,height=500\'); return false;" title="{\'Share on Facebook\'|@translate}" target="_blank"><i class="fa fa-facebook-square"></i> <span>{\'Facebook\'|@translate}</span></a>{/if}'
104
105
106                   . '{if $sharethis_pinterest}<a href="http://pinterest.com/pin/create/button/?url=' . $url . '" title="{\'Share on Pinterest\'|@translate}" target="_blank"><i class="fa fa-pinterest"></i> <span>{\'Pinterest\'|@translate}</span></a>{/if}'
107                   . '{if $sharethis_twitter}<a href="https://twitter.com/intent/tweet?text=' . $title . '&url=' . $url .'" title="{\'Share on Twitter\'|@translate}" target="_blank"><i class="fa fa-twitter"></i> <span>{\'Twitter\'|@translate}</span></a>{/if}'
108                   . '{if $sharethis_googleplus}<a href="https://plus.google.com/share?url=' . $url . '" title="{\'Share on Google+\'|@translate}" target="_blank"><i class="fa fa-google-plus-square"></i> <span>{\'Google+\'|@translate}</span></a>{/if}',
109    "combine"     => '{combine_css id=\'fa\' path=$SHARETHIS_PATH|cat:"/css/font-awesome.min.css"}'
110                   . '{combine_css id=\'sharethis\' path=$SHARETHIS_PATH|cat:"/css/styles.css"}',
111    "no_overlay"  => TRUE
112  );
113
114  return $metadata;
115}
116
117function sharethis_append_content($tpl_source, &$smarty){
118
119  $metadata = array();
120  $metadata = sharethis_get_tab_metadata($metadata);
121
122  $pattern = '#<div id=\"imageTitle\".*>#';
123
124  if (!preg_match($pattern, $tpl_source)):
125    $pattern = '#<div id=\"imageInfos\".*>#';
126    $replacement = '$0
127      <dl class="imageInfoTable" id="static-sharethis" >
128      ' . $metadata[0]["content"] . '
129      </dl>
130      ' . $metadata[0]["combine"] . '
131         
132    ';
133  else:
134    $replacement = '
135      <div id="static-sharethis">
136      ' . $metadata[0]["content"] . '
137      </div>
138      ' . $metadata[0]["combine"] . '
139      $0   
140    ';
141  endif;
142
143  return preg_replace($pattern, $replacement, $tpl_source, 1);
144}
Note: See TracBrowser for help on using the repository browser.