'1', 'fblike' => '2', 'twitter' => '3', 'beforepost' => '1', 'afterpost' => '0', 'beforepage' => '1', 'afterpage' => '0', 'beforearchive' => '0', 'afterarchive' => '0' ); // defined buttons var $arrKnownButtons = array('fblike', 'googleplus', 'twitter'); /** * Constructor */ function __construct() { register_activation_hook( __FILE__, array(&$this, 'plugin_install') ); register_deactivation_hook( __FILE__, array(&$this, 'plugin_uninstall') ); /** * Action hooks */ add_action( 'create_ssb', array(&$this, 'direct_display'), 10 , 1); /** * basic init */ add_action( 'init', array(&$this, 'plugin_init') ); // get settings $currentSettings = $this->get_settings(); // social JS + CSS data add_action( 'wp_footer', array(&$this, 'include_social_js') ); if(!isset($currentSettings['override_css'])) { add_action( 'wp_head', array(&$this, 'include_css') ); } /** * Filter hooks */ add_filter( 'the_content', array(&$this, 'insert_buttons') ); add_filter( 'the_excerpt', array(&$this, 'insert_buttons') ); } function plugin_init() { load_plugin_textdomain( 'simplesocialbuttons', '', dirname( plugin_basename( __FILE__ ) ).'/lang' ); } /** * Both avoids time-wasting https calls AND provides better SSL-protection if the current server is accessed using HTTPS */ public function get_current_http( $echo = true ) { $return = 'http' . (strtolower(@$_SERVER['HTTPS']) == 'on' ? 's' : '') . '://'; if($echo != false) { echo $return; return; } return $return; } function include_social_js($force_include = false) { $lang = get_bloginfo('language'); $lang_g = strtolower(substr($lang, 0, 2)); $lang_fb = str_replace('-', '_', $lang); /** * Disable loading of social network JS if disabled for specific post type * * NOTE: Conditional tags seem to work only AFTER the page has loaded, thus the code has been added here instead of at the plugin init * @author Fabian Wolf * @link http://usability-idealist.de/ * @date Di 20. Dez 17:50:01 CET 2011 */ if($this->where_to_insert() != false || $force_include == true) { ?> check_old_settings(); /** * @see http://codex.wordpress.org/Function_Reference/add_option * @param string $name Name of the option to be added. Use underscores to separate words, and do not use uppercase - this is going to be placed into the database. * @param mixed $value Value for this option name. Limited to 2^32 bytes of data. * @param string $deprecated Deprecated in WordPress Version 2.3. * @param string $autoload Should this option be automatically loaded by the function wp_load_alloptions() (puts options into object cache on each page load)? Valid values: yes or no. Default: yes */ add_option( $this->pluginPrefix . 'settings', $defaultSettings, '', 'yes' ); add_option( $this->pluginPrefix . 'version', $this->pluginVersion, '', 'yes' ); // for backward-compatiblity checks } /** * Backward compatiblity for newer versions */ function check_old_settings() { $return = $this->pluginDefaultSettings; $oldSettings = get_option( $this->pluginPrefix . 'settings', array() ); if( !empty($oldSettings) && is_array($oldSettings) != false) { $return = wp_parse_args( $oldSettings, $this->pluginDefaultSettings ); } return $return; } /** * Plugin unistall and database clean up */ function plugin_uninstall() { if( !defined( 'ABSPATH') && !defined('WP_UNINSTALL_PLUGIN') ) { exit(); } delete_option( $this->pluginPrefix . 'settings' ); delete_option( $this->pluginPrefix . 'version' ); } /** * Get settings from database */ public function get_settings() { $return = get_option($this->pluginPrefix . 'settings' ); if(empty($return) != false) { $return = $this->pluginDefaultSettings; } return $return; } /** * Update settings */ function update_settings( $newSettings = array() ) { $return = false; // compile settings $currentSettings = $this->get_settings(); /** * Compile settings array * @see http://codex.wordpress.org/Function_Reference/wp_parse_args * @param mixed $args * @param mixed $defaults */ $updatedSettings = wp_parse_args( $newSettings, $currentSettings ); if($currentSettings != $updatedSettings ) { $return = update_option( $this->pluginPrefix . 'settings', $newSettings ); } return $return; } /** * Returns true on pages where buttons should be shown */ function where_to_insert() { $return = false; // get settings from database $settings = $this->get_settings(); extract( $settings, EXTR_PREFIX_ALL, 'ssb' ); // display on single post? if (is_single()) { if (array_shift(get_post_meta(get_the_ID(), $this->showCustomMetaKey)) == 'true' || (($ssb_beforepost || $ssb_afterpost) && array_shift(get_post_meta(get_the_ID(), $this->hideCustomMetaKey)) != 'true')) { $return = true; } } // display on single page? if (is_page()) { if (array_shift(get_post_meta(get_the_ID(), $this->showCustomMetaKey)) == 'true' || (($ssb_beforepage || $ssb_afterpage) && array_shift(get_post_meta(get_the_ID(), $this->hideCustomMetaKey)) != 'true')) { $return = true; } } // display on frontpage? if((is_front_page() || is_home()) && $ssb_showfront) { $return = true; } // display on category archive? if(is_category() && $ssb_showcategory) { $return = true; } // display on date archive? if(is_date() && $ssb_showarchive) { $return = true; } // display on tag archive? if(is_tag() && $ssb_showtag) { $return = true; } return $return; } /** * Insert the buttons to the content */ function insert_buttons($content) { // Insert or not? if(!$this->where_to_insert() ) { return $content; } // get settings from database $settings = $this->get_settings(); extract( $settings, EXTR_PREFIX_ALL, 'ssb' ); $via = $settings['twitter_username']; // creating order $order = array(); foreach ($this->arrKnownButtons as $button_name) { $order[$button_name] = $settings[$button_name]; } $ssb_buttonscode = $this->generate_buttons_code($order, $via); if(is_single()) { if($ssb_beforepost) { $content = $ssb_buttonscode.$content; } if($ssb_afterpost) { $content = $content.$ssb_buttonscode; } } else if(is_page()) { if($ssb_beforepage || array_shift(get_post_meta(get_the_ID(), $this->showCustomMetaKey)) == 'true') { $content = $ssb_buttonscode.$content; } if($ssb_afterpage) { $content = $content.$ssb_buttonscode; } } else { if($ssb_beforearchive) { $content = $ssb_buttonscode.$content; } if($ssb_afterarchive) { $content = $content.$ssb_buttonscode; } } return $content; } function direct_display($order = null, $via = null) { // Return false if hide SSB for this page/post is disabled if (is_single() && array_shift(get_post_meta(get_the_ID(), $this->hideCustomMetaKey)) == 'true') return false; // Display buttons and scripts $buttons_code = $this->generate_buttons_code($order, $via); echo $buttons_code; $this->include_social_js(true); } /** * Generate buttons html code with specified order * * @param mixed $order - order of social buttons */ function generate_buttons_code($order = null, $via = null) { foreach ($this->arrKnownButtons as $button_name) { $defaultOrder[$button_name] = $this->pluginDefaultSettings[$button_name]; } $order = wp_parse_args($order, $defaultOrder); // define empty buttons code to use $ssb_buttonscode = ''; // get post permalink and title $permalink = get_permalink(); $title = get_the_title(); if (!empty($via)) { $via = ' data-via="'.$via.'" '; } $hashtags = get_post_meta(get_the_ID(), 'hashtags', true); if (!empty($hashtags)) { $hashtags = ' data-hashtags="'.$hashtags.'" '; } //Sorting the buttons foreach($this->arrKnownButtons as $button_name) { if(!empty($order[$button_name]) && (int)$order[$button_name] != 0) { $arrButtons[$button_name] = $order[$button_name]; } } @asort($arrButtons); $arrButtonsCode = array(); foreach($arrButtons as $button_name => $button_sort) { switch($button_name) { case 'googleplus': $arrButtonsCode[] = '
'; break; case 'fblike': $arrButtonsCode[] = '
'; break; case 'twitter': $arrButtonsCode[] = '
'; break; } } if(count($arrButtonsCode) > 0) { $ssb_buttonscode = '
'; $ssb_buttonscode .= implode("\n", $arrButtonsCode); $ssb_buttonscode .= '
'; } return $ssb_buttonscode; } } // end class /** * Admin class * * Gets only initiated if this plugin is called inside the admin section ;) */ class SimpleSocialButtonsPR_Admin extends SimpleSocialButtonsPR { function __construct() { parent::__construct(); add_action('admin_menu', array(&$this, 'admin_actions') ); add_action('add_meta_boxes', array(&$this, 'ssb_meta_box')); add_action('save_post', array(&$this, 'ssb_save_meta'), 10, 2); add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 ); } public function admin_actions() { if (current_user_can('administrator')) add_options_page('Simple Social Buttons ', 'Simple Social Buttons ', 1, 'simple-social-buttons', array(&$this, 'admin_page') ); } public function admin_page() { global $wpdb; include dirname( __FILE__ ).'/ssb-admin.php'; } public function plugin_action_links($links, $file) { static $this_plugin; if (!$this_plugin) { $this_plugin = plugin_basename(__FILE__); } if ($file == $this_plugin) { $settings_link = ''.__('Settings', 'simplesocialbuttons').''; array_unshift($links, $settings_link); } return $links; } /** * Register meta box to hide/show SSB plugin on single post or page */ public function ssb_meta_box() { $postId = $_GET['post']; $postType = get_post_type($postId); if ($postType != 'page' && $postType != 'post') return false; $currentSsbHide = get_post_custom_values($this->hideCustomMetaKey, $postId); $currentSsbShow = get_post_custom_values($this->showCustomMetaKey, $postId); $currentSettings = $this->get_settings(); if ($currentSsbHide[0] == 'true') { $hideChecked = true; } else { $hideChecked = false; } if ($currentSsbShow[0] == 'true') { $showChecked = true; } else { $showChecked = false; } // Rendering meta box if (!function_exists('add_meta_box')) include('includes/template.php'); add_meta_box('ssb_meta_box', __('SSB Settings', 'simplesocialbuttons'), array(&$this, 'render_ssb_meta_box'), $postType, 'side', 'default', array('type' => $postType, 'hidechecked' => $hideChecked, 'showchecked' => $showChecked)); } /** * Showing custom meta field */ public function render_ssb_meta_box($post, $metabox) { wp_nonce_field( plugin_basename( __FILE__ ), 'ssb_noncename' ); ?>
hideCustomMetaKey])) ? $_POST[$this->hideCustomMetaKey] : 'false'; update_post_meta($postId, $this->hideCustomMetaKey, $hideNewValue); $showNewValue = (isset($_POST[$this->showCustomMetaKey])) ? $_POST[$this->showCustomMetaKey] : 'false'; update_post_meta($postId, $this->showCustomMetaKey, $showNewValue); } } // end SimpleSocialButtonsPR_Admin if(is_admin() ) { $_ssb_pr = new SimpleSocialButtonsPR_Admin(); } else { $_ssb_pr = new SimpleSocialButtonsPR(); } /** * Function to insert Simple Social Buttons directly in template. * * @param mixed $order - order of the buttons in array or string (parsed by wp_parse_args()) * * @example 1 - use in template with default order * get_ssb(); * * @example 2 - use in template with specified order * get_ssb('googleplus=3&fblike=2&twitter=1'); * * @example 3 - hiding button by setting order to 0. By using code below googleplus button won't be displayed * get_ssb('googleplus=0&fblike=1&twitter=2'); * * */ function get_ssb($order = null) { do_action('create_ssb', $order); } ?>