source: extensions/SocialButtons/main.inc.php @ 25473

Last change on this file since 25473 was 25473, checked in by mistic100, 10 years ago

factorize

File size: 5.5 KB
Line 
1<?php 
2/*
3Plugin Name: Social Buttons
4Version: auto
5Description: Sharing functions for Facebook, Twitter, Google+ and Tumblr
6Plugin URI: auto
7Author: Mistic
8Author URI: http://www.strangeplanet.fr
9*/
10
11defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
12
13global $prefixeTable;
14
15// +-----------------------------------------------------------------------+
16// | Define plugin constants                                               |
17// +-----------------------------------------------------------------------+
18defined('SOCIALBUTT_ID') or define('SOCIALBUTT_ID', basename(dirname(__FILE__)));
19define('SOCIALBUTT_PATH' ,   PHPWG_PLUGINS_PATH . SOCIALBUTT_ID . '/');
20define('SOCIALBUTT_ADMIN',   get_root_url() . 'admin.php?page=plugin-' . SOCIALBUTT_ID);
21define('SOCIALBUTT_VERSION', 'auto');
22
23
24// +-----------------------------------------------------------------------+
25// | Add event handlers                                                    |
26// +-----------------------------------------------------------------------+
27// init the plugin
28add_event_handler('init', 'socialbutt_init');
29
30if (defined('IN_ADMIN'))
31{
32  add_event_handler('get_admin_plugin_menu_links', 'socialbutt_admin_plugin_menu_links');
33 
34  function socialbutt_admin_plugin_menu_links($menu) 
35  {
36    array_push($menu, array(
37      'NAME' => 'Social Buttons',
38      'URL' => SOCIALBUTT_ADMIN,
39    ));
40    return $menu;
41  }
42}
43else
44{
45  add_event_handler('loc_end_picture', 'socialbutt_add_button');
46  add_event_handler('loc_end_index', 'socialbutt_add_button');
47}
48
49
50/**
51 * plugin initialization
52 */
53function socialbutt_init()
54{
55  global $conf, $pwg_loaded_plugins;
56 
57  // apply upgrade if needed
58  if (
59    SOCIALBUTT_VERSION == 'auto' or
60    $pwg_loaded_plugins[SOCIALBUTT_ID]['version'] == 'auto' or
61    version_compare($pwg_loaded_plugins[SOCIALBUTT_ID]['version'], SOCIALBUTT_VERSION, '<')
62  )
63  {
64    // call install function
65    include_once(SOCIALBUTT_PATH . 'include/install.inc.php');
66    socialbutt_install();
67   
68    // update plugin version in database
69    if ( $pwg_loaded_plugins[SOCIALBUTT_ID]['version'] != 'auto' and SOCIALBUTT_VERSION != 'auto' )
70    {
71      $query = '
72UPDATE '. PLUGINS_TABLE .'
73SET version = "'. SOCIALBUTT_VERSION .'"
74WHERE id = "'. SOCIALBUTT_ID .'"';
75      pwg_query($query);
76     
77      $pwg_loaded_plugins[SOCIALBUTT_ID]['version'] = SOCIALBUTT_VERSION;
78     
79      if (defined('IN_ADMIN'))
80      {
81        $_SESSION['page_infos'][] = 'Social Buttons updated to version '. SOCIALBUTT_VERSION;
82      }
83    }
84  }
85 
86  // prepare plugin configuration
87  $conf['SocialButtons'] = unserialize($conf['SocialButtons']);
88}
89
90
91/**
92 * add buttons
93 */
94function socialbutt_add_button()
95{
96  global $conf, $template, $picture;
97 
98  set_make_full_url();
99  $basename = script_basename();
100  $root_url = get_absolute_root_url();
101 
102  if ($basename == 'picture')
103  {
104    // global $picture;
105   
106    // if ($picture['current']['level'] > 0) return;
107   
108    $share_url = duplicate_picture_url();
109  }
110  else if ($basename == 'index' and $conf['SocialButtons']['on_index'])
111  {
112    $conf['SocialButtons']['position'] = 'index';
113    $share_url = duplicate_index_url(array(), array('start'));
114  }
115  else
116  {
117    return;
118  }
119 
120 
121  $tpl_vars = array(
122    'share_url' => $share_url,
123    'basename' => $basename,
124    'position' => $conf['SocialButtons']['position'],
125    'light' => $conf['SocialButtons']['light'],
126    'copyright' => '(from <a href="'.$share_url.'">'.$conf['gallery_title'].'</a>)',
127    );
128 
129  if ($basename == 'picture')
130  {
131    if ($conf['SocialButtons']['img_size'] == 'Original')
132    {
133      $tpl_vars['source'] = $picture['current']['src_image']->get_url();
134    }
135    else
136    {
137      $tpl_vars['source'] = DerivativeImage::url($conf['SocialButtons']['img_size'], $picture['current']['src_image']);
138    }
139  }
140 
141 
142  $buttons = array();
143  $services = array('google', 'twitter', 'facebook', 'tumblr', 'pinterest', 'reddit', 'linkedin');
144 
145  foreach ($services as $service)
146  {
147    if ($conf['SocialButtons'][$service]['enabled'])
148    {
149      include_once(SOCIALBUTT_PATH . 'include/'. $service .'.inc.php');
150      call_user_func_array('socialbutt_'.$service, array($basename, $root_url, &$tpl_vars, &$buttons));
151    }
152  }
153 
154  unset_make_full_url();
155 
156  if (empty($buttons))
157  {
158    return;
159  }
160 
161 
162  $template->assign(array(
163    'SOCIALBUTT' => $tpl_vars,
164    'SOCIALBUTT_PATH' => SOCIALBUTT_PATH,
165    ));
166 
167  // parse buttons
168  foreach ($buttons as &$button)
169  {
170    $button = $template->parse($button, true);
171  }
172  unset($button);
173 
174  switch ($conf['SocialButtons']['position'])
175  {
176    case 'index':
177      foreach ($buttons as $button)
178      {
179        $template->add_index_button($button, 100);
180      }
181      break;
182    case 'toolbar':
183      foreach ($buttons as $button)
184      {
185        $template->add_picture_button($button, 100);
186      }
187      break;
188    default;
189      define('SOCIALBUTT_POSITION', $conf['SocialButtons']['position']);
190      $template->assign('SOCIALBUTT_BUTTONS', $buttons);
191      $template->set_prefilter('picture', 'socialbutt_add_button_prefilter');
192  }
193}
194
195function socialbutt_add_button_prefilter($content)
196{
197  switch (SOCIALBUTT_POSITION)
198  {
199    case 'top':
200      $search = '<div id="theImage">';
201      $add = '<div id="socialButtons">{foreach from=$SOCIALBUTT_BUTTONS item=BUTTON}{$BUTTON} {/foreach}</div>';
202      break;
203     
204    case 'bottom':
205      $search = '{$ELEMENT_CONTENT}';
206      $add = '<div id="socialButtons">{foreach from=$SOCIALBUTT_BUTTONS item=BUTTON}{$BUTTON} {/foreach}</div>';
207      break;
208  }
209
210  return str_replace($search, $search.$add, $content);
211}
212
213?>
Note: See TracBrowser for help on using the repository browser.