privacy-share-buttons.php 9.59 KB
Newer Older
lucha's avatar
lucha committed
1 2
<?php
/*
lucha's avatar
lucha committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  Plugin Name: Privacy Share Buttons
  Plugin URI: https://github.com/controesempio/Privacy-Share-Buttons
  Description: Enables the well-known "Share this" buttons for different social networks, but with respect toward's your user privacy and data.
  Version: 0.3
  Author: lucha <lucha@paranoici.org>
  Author URI: https://github.com/controesempio/
  Tags: privacy, social, twitter, facebook, googleplus
  License: GPL2

  Copyright (C) 2012 lucha <lucha@paranoici.org>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License, version 2, as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
lucha's avatar
lucha committed
26 27 28
*/

class ShareButton {
lucha's avatar
lucha committed
29 30
    function ShareButton() {
	$this->__construct(func_get_args());
lucha's avatar
lucha committed
31 32
	}

lucha's avatar
lucha committed
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
    function __construct() {		
	$this->url = plugins_url(basename(dirname(__FILE__)));
	$this->css = 'css/jquery.socialshareprivacy.min.css';
	$this->js = $this->url .'/js/jquery.socialshareprivacy.min.js';
	$this->jquery_cookie = $this->url .'/js/jquery.cookie.min.js';

	$this->global_options = array(
	    'path_prefix' => $this->url . '/',
	    'css_path' => $this->css,
	    );
	$this->_all_services = array('buffer',
	    'disqus','mail',
	    'facebook', 'fbshare',
	    'gplus','pinterest',
	    'twitter');
	$this->_services_options = array(
	    'buffer' => array('username' => 'via'),
	    'delicious' => array('username' => 'shortname'),
	    'flattr' => array('username' => 'uid'),
	    'twitter' => array('username' => 'via'),
	    );
	$this->settings = new ShareButtonSettings($this->_all_services, $this->_services_options);
	$this->settings = $this->settings->settings;
	$this->content_class = 'privacy_share_buttons_post';
	add_action( 'init', array( &$this, 'init' ) );
lucha's avatar
lucha committed
58 59
	}

lucha's avatar
lucha committed
60 61 62 63 64 65
    function init() {
	$activate = false;
	foreach ($this->_all_services as $service)
	    if (array_key_exists($service.'-status',$this->settings) and $this->settings[$service.'-status']){
		$activate = true;
		break;
lucha's avatar
lucha committed
66
		}
lucha's avatar
lucha committed
67 68 69 70
	if (!$activate)
	    return;

	add_action('wp_enqueue_scripts',array(&$this, 'enqueue_scripts'));
lucha's avatar
lucha committed
71
		
lucha's avatar
lucha committed
72 73
	add_shortcode('share_buttons',array(&$this,'short_code'));
	add_filter('the_content', array(&$this, 'filter_content'), 8);		
lucha's avatar
lucha committed
74 75
	}
	
lucha's avatar
lucha committed
76 77 78 79
    function enqueue_scripts() {
	wp_register_script('jquery-cookie',$this->jquery_cookie,array('jquery'),false,true);
	wp_enqueue_script('social-share-privacy',$this->js,array('jquery','jquery-cookie'),false,true);
	wp_enqueue_script('ssp', $this->url . '/js/ssp-onload.js',null,false,true);
lucha's avatar
lucha committed
80 81
	}
	
lucha's avatar
lucha committed
82 83 84 85
    function short_code($atts) {
	if ($this->settings['position'] == 'manual')
	    return $this->generate_html();
	return '';
lucha's avatar
lucha committed
86 87
	}
	
lucha's avatar
lucha committed
88 89 90 91 92
    function filter_content($content){
	if (array_key_exists('index',$this->settings) and !$this->settings['index'] and is_home())
	    return $content;
	if (array_key_exists('pages',$this->settings) and !$this->settings['pages'] and is_page())
	    return $content;
lucha's avatar
lucha committed
93
			
lucha's avatar
lucha committed
94 95
	$newcontent = $this->generate_html();
	$position = $this->settings['position'];
lucha's avatar
lucha committed
96

lucha's avatar
lucha committed
97 98 99 100 101
	if ($position == 'before'){		
	    return $newcontent . $content;
	    } else if ($position == 'after') {
	    return $content . $newcontent;
	    }
lucha's avatar
lucha committed
102

lucha's avatar
lucha committed
103
	return $content;
lucha's avatar
lucha committed
104 105
	}
	
lucha's avatar
lucha committed
106 107 108
    function generate_html() {
	global $post;
	return "<div class='share' data-options='" . $this->jsconf() . "'></div>";
lucha's avatar
lucha committed
109 110
	}
	
lucha's avatar
lucha committed
111 112 113 114 115 116 117 118
    function jsconf(){
	$conf = $this->global_options;
	foreach ($this->_all_services as $service)
	    if (array_key_exists($service.'-status',$this->settings) and ($this->settings[$service.'-status'] == 1)){
		$conf['services'][$service]['status'] = true;
		if (array_key_exists($service,$this->_services_options))
		    foreach ($this->_services_options[$service] as $option => $name)
			$conf['services'][$service][$name] = $this->settings[$service.'-'.$option];
lucha's avatar
lucha committed
119
		}
lucha's avatar
lucha committed
120 121 122 123
	    else
		$conf['services'][$service]['status'] = false;
	  
	return json_encode($conf);
lucha's avatar
lucha committed
124
	}
lucha's avatar
lucha committed
125
    }
lucha's avatar
lucha committed
126 127

class ShareButtonSettings {
lucha's avatar
lucha committed
128 129
    function ShareButtonSettings() {
	$this->__construct(func_get_args());
lucha's avatar
lucha committed
130 131
	}

lucha's avatar
lucha committed
132 133 134 135 136 137 138 139 140
    function __construct($services, $options) {
	$this->services = $services;
	$this->services_options = $options;
	$this->settings_section = 'share-button-settings';
	$this->display_section = $this->settings_section . '-display';
	$this->services_section = $this->settings_section . '-services';
	$this->settings = get_option($this->settings_section);
	add_action('admin_init', array( &$this, 'admin_init'));
	add_action('admin_menu', array( &$this, 'admin_menu'));
lucha's avatar
lucha committed
141 142 143

	}

lucha's avatar
lucha committed
144 145 146 147 148 149 150 151
    function admin_menu() {
	add_options_page(
	    __('Privacy Share Buttons','privacy-share-buttons'),
	    __('Privacy Share Buttons','privacy-share-buttons'),
	    'manage_options',
	    $this->settings_section,
	    array( &$this, 'submenu_page')
	    );	     
lucha's avatar
lucha committed
152 153
	}

lucha's avatar
lucha committed
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
    function admin_init() {
	register_setting($this->settings_section, $this->settings_section, array(&$this, 'settings_validate'));

	add_settings_section($this->settings_section,
	    __('Enable Social Share Buttons','privacy-share-buttons'), array(&$this, 'settings_section'), $this->settings_section);

	foreach ($this->services as $service){
	    $this->add_default_fields($service);
	    
	    if (array_key_exists($service,$this->services_options))
		if (array_key_exists('username',$this->services_options[$service]))
		    $this->username_settings($service);
	    }

	add_settings_section($this->display_section,
	    __('Display settings','privacy-share-buttons'),
	    array(&$this, 'display_section'), 
	    $this->settings_section);

	add_settings_field($this->display_section."[index]",
	    __('Display on the Index page','privacy-share-buttons'),
	    array(&$this, 'checkbox'),
	    $this->settings_section, $this->display_section,
	    array('id' => 'index'));


	add_settings_field($this->display_section."[pages]",
	    __('Display on Pages','privacy-share-buttons'),
	    array(&$this, 'checkbox'),
	    $this->settings_section, $this->display_section,
	    array('id' => 'pages'));

	add_settings_field($this->display_section."[position]",
	    __('Position','privacy-share-buttons'),
	    array(&$this, 'position_field'),
	    $this->settings_section, $this->display_section);
lucha's avatar
lucha committed
190 191
	}

lucha's avatar
lucha committed
192 193
    function add_default_fields($serviceid) {
	$display_id = "{$serviceid}-status";
lucha's avatar
lucha committed
194
	
lucha's avatar
lucha committed
195 196 197 198
	$info['id'] = $display_id;
	$info['label'] = __('Enable','privacy-share-buttons');
	$info['name'] = $serviceid;
	add_settings_field($this->settings_section."[$display_id]" , $info['name'], array(&$this, 'checkbox'), $this->settings_section, $this->settings_section, $info);
lucha's avatar
lucha committed
199 200

	} 
lucha's avatar
lucha committed
201 202 203 204 205 206 207 208 209 210 211 212 213
    
    function username_settings($service) {
	$userfield = $this->services_section."[$service-username]";
	add_settings_field($userfield, sprintf(__("%s username",'privacy-share-buttons'),$service),
	    array(&$this, 'username_field'), $this->settings_section, $this->settings_section,$service.'-username');
	}
	
    function username_field($id) {
	$field = $this->settings_section."[$id]";
	$value = '';
	if (array_key_exists($id,$this->settings))
	    $value = $this->settings[$id];
	echo "@<input type='text' name='{$field}' value='{$value}' size='20' />";
lucha's avatar
lucha committed
214 215
	}

lucha's avatar
lucha committed
216 217
    function display_section() {
	_e("By default buttons will be showed on single Posts",'privacy-share-buttons');
lucha's avatar
lucha committed
218 219
	}

lucha's avatar
lucha committed
220 221
    function settings_section() {
	_e("Do not enable too many of them. They will look ugly all together.");
lucha's avatar
lucha committed
222 223
	}

lucha's avatar
lucha committed
224 225 226 227 228 229 230
    function checkbox($info) {
	$id = $info['id'];
	$field =  $this->settings_section."[$id]";
	$value = '0';
	if (array_key_exists($id, $this->settings))
	    $value = $this->settings[$id];
	$checked = checked( '1', $value, false);
lucha's avatar
lucha committed
231

lucha's avatar
lucha committed
232 233 234 235
	echo "<input type='checkbox' name='{$field}' value='1' $checked />";
	echo "<label for='{$field}'>";
	echo __('Enable','privacy-share-buttons');
	echo "</label>";
lucha's avatar
lucha committed
236 237 238

	}

lucha's avatar
lucha committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    function position_field() {
	$field = "{$this->settings_section}[position]";
	$value = $this->settings['position'];
	echo "<div><select name='{$field}'>";

	$options = array(
	    'after' => __("After the post",'privacy-share-buttons'),
	    'before' => __("Before the post",'privacy-share-buttons'),
	    'manual' => __("Manual (shortcode)",'privacy-share-buttons')
	    );
	foreach ($options as $key => $label) {
	    $selected = selected($value, $key, false);
	    echo "<option value='{$key}' $selected>$label</option>";
	    }
	echo "</select></div>";
	echo "<div> <p>"._e('If you choose "Manual (Shortcode)", you can use the shortcode <strong>[share_buttons]</strong> inside your articles','privacy-share-buttons')."</p></div>";
lucha's avatar
lucha committed
255 256
	}

lucha's avatar
lucha committed
257 258
    function settings_validate($input) {
	return $input;
lucha's avatar
lucha committed
259 260
	}

lucha's avatar
lucha committed
261 262 263 264 265 266 267 268 269 270
    function submenu_page() {
?>
	<div class="wrap">
	    <div id="icon-themes" class="icon32"><br></div>
	    <h2>Privacy Share Button Settings</h2>
<?php
	    if ( !empty( $_POST['action'] ) && 'update' == $_POST['action'] ) {
		update_option( $this->settings_section, $_POST[$this->settings_section] );
		$this->settings = get_option( $this->settings_section );
		echo '<div id="message" class="updated fade"><p><strong>'.__('Settings saved.','privacy-share-buttons').'</strong></p></div>';
lucha's avatar
lucha committed
271
		}
lucha's avatar
lucha committed
272 273 274 275 276 277 278 279 280 281 282 283
?>

	<form method="post" action="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; ?>">
<?php settings_fields( $this->settings_section ); ?>
<?php do_settings_sections( $this->settings_section ); ?>
	     <p class="submit">
	     <input type="submit" class="button-primary" value="Save Changes" />
	     </p>
	     </form>
<?php
	     }
    }
lucha's avatar
lucha committed
284

lucha's avatar
lucha committed
285
new ShareButton();