Google +1 for Sharedaddy

Google published on 01 June the possibility to add Google +1 to your page, which is a great service to add to the existing sharing services.

Im using Sharedaddy, and I will not install another extra plugin to support Google +1 and the Sharedaddy option to add a custom service is not prepared for dynamic services like this one. That’s because I looked to integrate the service into Sharedaddy avoiding to change to much the plugin.

Sharedaddy is Object Oriented, so its easy to create a new Sevice Class which extends Sharing_Advanced_Source. But to load your class, you need to change some files.

  • sharing-sources.php

    To make it dynamic, I created an extra file called sharing-sources-custom.php and load this file from the original Sharedaddy sharing-sources.php:

    // Custom Sharing services
    require_once('sharing-sources-custom.php');
    
  • sharing-services.php

    This file has an array with the current services and the class name for this service, here we must change directly the function to add our new class.

    private function get_all_services() {
    // Default services
    $services = array(
    ...
    'googleplusone' => 'Share_GooglePlusOne'
    );
    

This two changes are enought to get your new service working, if you want you can also add images and edit admin-sharing.css to have preview in your admin panel, but the Service will be working without this.

Here is the Code for the Share_GooglePlusOne class:


 * @copyright Copyright (c) 2011, Marco Neumann
 * Licensed under the GNU GPLv3. 
 *
 */
class Share_GooglePlusOne extends Sharing_Advanced_Source
{
    /**
     * Button Size
     *
     * @var String
     */
    private $_size = 'small';

    /**
     * Button Language
     *
     * @var String
     */
    private $_language = 'en-US';

    /**
     * Button with count?
     *
     * @var String
     */
    private $_count = 'true';

    // Configuration extracted from http://code.google.com/apis/+1button/#configuration
    /**
     * Available sizes
     *
     * @var Array
     */
    private $_optionsSize = array(
        'small'    => 'Small Size',
        'medium'   => 'Medium Size',
        'standard' => 'Standard Size',
        'tall'     => 'Tall Size'
    );

    /**
     * Available languages
     *
     * @var Array
     */
    private $_optionsLanguage = array(
        'ar'     => 'Arabic',
        'bg'     => 'Bulgarian',
        'ca'     => 'Catalan',
        'zh-CN'  => 'Chinese (Simplified)',
        'zh-TW'  => 'Chinese (Traditional)',
        'hr'     => 'Croatian',
        'cs'     => 'Czech',
        'da'     => 'Danish',
        'nl'     => 'Dutch',
        'en-GB'  => 'English (UK)',
        'en-US'  => 'English (US)',
        'et'     => 'Estonian',
        'fil'    => 'Filipino',
        'fi'     => 'Finnish',
        'fr'     => 'French',
        'de'     => 'German',
        'el'     => 'Greek',
        'iw'     => 'Hebrew',
        'hi'     => 'Hindi',
        'hu'     => 'Hungarian',
        'id'     => 'Indonesian',
        'it'     => 'Italian',
        'ja'     => 'Japanese',
        'ko'     => 'Korean',
        'lv'     => 'Latvian',
        'lt'     => 'Lithuanian',
        'ms'     => 'Malay',
        'no'     => 'Norwegian',
        'fa'     => 'Persian',
        'pl'     => 'Polish',
        'pt-BR'  => 'Portuguese (Brazil)',
        'pt-PT'  => 'Portuguese (Portugal)',
        'ro'     => 'Romanian',
        'ru'     => 'Russian',
        'sr'     => 'Serbian',
        'sk'     => 'Slovak',
        'sl'     => 'Slovenian',
        'es'     => 'Spanish',
        'es-419' => 'Spanish (Latin America)',
        'sv'     => 'Swedish',
        'th'     => 'Thai',
        'tr'     => 'Turkish',
        'uk'     => 'Ukrainian',
        'vi'     => 'Vietnamese'
    );

    /**
     * Constructor
     *
     * @param Integer $id
     * @param Array $settings
     */
    public function __construct($id, array $settings)
    {
        parent::__construct($id, $settings);
        
        if (isset($settings['size'])) {
            $this->_size = $settings['size'];
        }
        if (isset($settings['language'])) {
            $this->_language = $settings['language'];
        }
        if (isset($settings['count'])) {
            $this->_count = $settings['count'];
        }
    }

    /**
     * Get Service Name
     *
     * @return String
     */
    public function get_name()
    {
        return __('Google +1', 'sharedaddy');
    }

    /**
     * Displays preview
     *
     * @return Share_GooglePlusOne
     */
    public function display_preview()
    {
?>
    
button_style == 'text' || $this->button_style == 'icon-text') { echo $this->get_name(); } else { echo ' '; } ?>
ID) && $post->ID > 0) { $options = array( 'lang' => $this->_language ); echo ''; } return $this; } /** * Shows icon * * @return String */ public function get_display($post) { return ''; } /** * Displays available Service options * * @return Share_GooglePlusOne */ public function display_options() { ?>
_optionsSize[$data['size']])) { $this->_size = $data['size']; } if (isset($data['language']) && isset($this->_optionsLanguage[$data['language']])) { $this->_language = $data['language']; } $this->_count = ($data['count'])?'true':'false'; return $this; } /** * Returns current options * * @var Array */ public function get_options() { return array( 'size' => $this->_size, 'language' => $this->_language, 'count' => $this->_count ); } }

Here you can download this file were you will find a patch for the actual sharedaddy version and the images for the admin panel. You only need to apply patch on wp-content/plugins/sharedaddy/ and copy the images to wp-content/plugins/sharedaddy/images/

25 Comments to “Google +1 for Sharedaddy”