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

Last change on this file since 24758 was 24758, checked in by mistic100, 11 years ago

add reddit button

File size: 5.9 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;
97 
98  $basename = script_basename();
99  set_make_full_url();
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    'position' => $conf['SocialButtons']['position'],
124    'light' => $conf['SocialButtons']['light'],
125    'copyright' => '(from <a href="'.$share_url.'">'.$conf['gallery_title'].'</a>)',
126    );
127  $buttons = array();
128 
129 
130  if ($conf['SocialButtons']['google']['enabled'])
131  {
132    include_once(SOCIALBUTT_PATH . 'include/google.inc.php');
133    socialbutt_google($basename, $root_url, $tpl_vars, $buttons);
134  }
135  if ($conf['SocialButtons']['twitter']['enabled'])
136  {
137    include_once(SOCIALBUTT_PATH . 'include/twitter.inc.php');
138    socialbutt_twitter($basename, $root_url, $tpl_vars, $buttons);
139  }
140  if ($conf['SocialButtons']['facebook']['enabled'])
141  {
142    include_once(SOCIALBUTT_PATH . 'include/facebook.inc.php');
143    socialbutt_facebook($basename, $root_url, $tpl_vars, $buttons);
144  }
145  if ($conf['SocialButtons']['tumblr']['enabled'])
146  {
147    include_once(SOCIALBUTT_PATH . 'include/tumblr.inc.php');
148    socialbutt_tumblr($basename, $root_url, $tpl_vars, $buttons);
149  }
150  if ($conf['SocialButtons']['pinterest']['enabled'] and $basename=='picture')
151  {
152    include_once(SOCIALBUTT_PATH . 'include/pinterest.inc.php');
153    socialbutt_pinterest($basename, $root_url, $tpl_vars, $buttons);
154  }
155  if ($conf['SocialButtons']['reddit']['enabled'])
156  {
157    include_once(SOCIALBUTT_PATH . 'include/reddit.inc.php');
158    socialbutt_reddit($basename, $root_url, $tpl_vars, $buttons);
159  }
160 
161  unset_make_full_url();
162 
163  if (empty($buttons))
164  {
165    return;
166  }
167 
168 
169  $template->assign(array(
170    'SOCIALBUTT' => $tpl_vars,
171    'SOCIALBUTT_PATH' => SOCIALBUTT_PATH,
172    ));
173 
174  // parse buttons
175  foreach ($buttons as &$button)
176  {
177    $button = $template->parse($button, true);
178  }
179  unset($button);
180 
181  switch ($conf['SocialButtons']['position'])
182  {
183    case 'index':
184      foreach ($buttons as $button)
185      {
186        $template->add_index_button($button, 100);
187      }
188      break;
189    case 'toolbar':
190      foreach ($buttons as $button)
191      {
192        $template->add_picture_button($button, 100);
193      }
194      break;
195    default;
196      define('SOCIALBUTT_POSITION', $conf['SocialButtons']['position']);
197      $template->assign('SOCIALBUTT_BUTTONS', $buttons);
198      $template->set_prefilter('picture', 'socialbutt_add_button_prefilter');
199  }
200}
201
202function socialbutt_add_button_prefilter($content)
203{
204  switch (SOCIALBUTT_POSITION)
205  {
206    case 'top':
207      $search = '<div id="theImage">';
208      $add = '<div id="socialButtons">{foreach from=$SOCIALBUTT_BUTTONS item=BUTTON}{$BUTTON} {/foreach}</div>';
209      break;
210     
211    case 'bottom':
212      $search = '{$ELEMENT_CONTENT}';
213      $add = '<div id="socialButtons">{foreach from=$SOCIALBUTT_BUTTONS item=BUTTON}{$BUTTON} {/foreach}</div>';
214      break;
215  }
216
217  return str_replace($search, $search.$add, $content);
218}
219
220?>
Note: See TracBrowser for help on using the repository browser.