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:

<?php
/**
 * File for Custom Services
 *
 * @category WordPress
 * @package  Sharedaddy
 */

/**
 * Class for Google+1
 *
 * Implementeation of Google+1 Service for Sharedaddy
 *
 * @category WordPress
 * @package  Sharedaddy
 * @author Marco Neumann <webcoder_at_binware_dot_org>
 * @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()
    {
?>
    <div class="option option-smart-<?php echo $this->_count; ?>">
        <?php
            if ($this->button_style == 'text' || $this->button_style == 'icon-text') {
                echo $this->get_name();
            } else {
               echo '&nbsp;';
            }
        ?>
    </div>
<?php

        return $this;
    }    

    /**
     * Loads Service required footer code
     *
     * @return Share_GooglePlusOne
     */
    public function display_footer()
    {
        // Load JS for Google +1
        global $post;

        if (isset($post->ID) && $post->ID > 0) {
            $options = array(
                'lang' => $this->_language
            );

            echo '<script type="text/javascript">';
            if (!empty($options)) {
                echo 'window.___gcfg = ' . json_encode($options) .';';
            }
            echo '(function() {
                  var po = document.createElement(\'script\'); po.type = \'text/javascript\'; po.async = true;
                  po.src = \'https://apis.google.com/js/plusone.js\';
                  var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(po, s);
                  })();';
            echo '</script>';
        }

        return $this;
    }

    /**
     * Shows icon
     *
     * @return String
     */
    public function get_display($post)
    {
        return '<g:plusone size="' . $this->_size . '" count="' . $this->_count . '" href="' . get_permalink() . '"></g:plusone>';
    }

    /**
     * Displays available Service options
     *
     * @return Share_GooglePlusOne
     */
    public function display_options()
    {
?>
<div class="input">
    <label>
        <select name="size">
            <?php foreach ($this->_optionsSize as $value=>$name): ?>
                <option value="<?php echo $value; ?>"<?php if ($this->_size == $value): ?> selected="selected"<?php endif; ?>><?php _e($name, 'sharedaddy'); ?>
            <?php endforeach; ?>
        </select>
    </label>
    <label>
        <select name="language">
            <?php foreach ($this->_optionsLanguage as $value=>$name): ?>
                <option value="<?php echo $value; ?>"<?php if ($this->_language == $value): ?> selected="selected"<?php endif; ?>><?php _e($name, 'sharedaddy'); ?>
            <?php endforeach; ?>
        </select>
    </label>
    <label>
        <input type="checkbox"<?php if ($this->_count == 'true') { echo ' checked="checked"'; } ?> name="count" /> Show Count
    </label>
</div>
<?php

        return $this;
    }

    /**
     * Updates submitted options
     *
     * @return Share_GooglePlusOne
     */
    public function update_options(array $data)
    {
        if (isset($data['size']) && isset($this->_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”

  1. Matthew Gore 6 June 2011 at 19:37 #

    Thanks; this looks like it could be a good modification! I’m about to give it a try and see if it works for me :) I appreciate the organized way that you arranged this, and without the use of an additional plugin.

    - Matthew

  2. Jimmyjames 8 June 2011 at 22:51 #

    Trying the “patch” method. Patch virgin. Downloaded files, extracted… check, check. Uploaded images to sharedaddy image folder, check. Uploaded patch file to /wp-content/plugins/jetpack/modules/sharedaddy, check…and nothing. I’m assuming I need to do something else to activate this patch, or because I’m using sharing via jetpack the path is wrong? Thanks!

    • P0L0 10 June 2011 at 07:47 #

      Hi

      the patch was made with the standalone sharedaddy plugin, but if you patch from /wp-content/plugins/jetpack/modules it should also work, the patch uses the path “sharedaddy/*”, if you are using somo *nix system it should work with patch -p0 or patch -p1 if you patch from “/wp-content/plugins/jetpack/modules/sharedaddy”

  3. Dave 11 June 2011 at 17:01 #

    Guess I’m a noob at this too. I moved the files to the proper directory but how do I actually execute the patch?

    • P0L0 13 June 2011 at 01:34 #

      Hi Dave

      I just uploaded the complete Sharedaddy 0.2.12 plugin with the Google +1 patch included, so that you don’t need to patch, only copy all files over your actual sharedaddy installation.

  4. Cladrite 20 June 2011 at 06:54 #

    Hi Polo.
    I had Sharedaddy working fine, but wanted to add Google +1. So I downloaded your version 02.12 and replaced the sharedaddy directory in plugins/jetpack/modules with the directory I downloaded from you, but nothing seems changed. The sharing options available at the bottom of my posts remain unchanged.

    Any thoughts or suggestions?

    • P0L0 20 June 2011 at 11:04 #

      Have you enabled Google +1 at Admin Panel!? Google +1 is not enabled by default and you must enable it.

      In the admin panel you should see that Google +1 is available with different options.

  5. steadwell 20 June 2011 at 20:51 #

    Tried the self-administered fix, it didn’t take. Tried the patch, no good for my set-up. Finally, uploaded the entire sharedaddy package into the jetpack/modules folder and it worked like a charm!

  6. [...] Newmann extended Automattic’s Sharedaddy plugin for WordPress, to add support for the Google +1 [...]

  7. huyz 30 June 2011 at 16:28 #

    Hey Marco, great work.
    I had problems with alignment and it wasn’t quite consistent with the other services, though, so I made some modifications:
    http://huyz.us/2011/google-1-for-sharedaddy-revised/

  8. bman (The Underfold) 11 July 2011 at 05:06 #

    This is great! Thanks!

  9. herbatnic 11 July 2011 at 12:53 #

    Nice job! I use it on my three WP-blogs :)

  10. [...] ładne rozwiązanie znalazłem na stronie POLO. Tłumaczy on w jaki sposób działa Sharedaddy, w jaki sposób się do niego podpiąć, aby [...]

    • MAsterKzaM 12 July 2011 at 19:52 #

      My friend is using this on his three WP-blogs, I am on my two — realy nice job!
      Regards & Respect!:)& Thanks!

  11. Lee Carlon 17 July 2011 at 07:54 #

    Thanks – works like a dream.

  12. Geoff P 29 July 2011 at 16:34 #

    Brilliant! P0L0 and Huyz take a bow, guys!

  13. RichardStep 31 July 2011 at 21:05 #

    I just had to say MEGA-THANKS for making my life a little bit more headache free on this fine Sunday. I had been beating it across the keyboard over this for a little while. Brain cells now intact. Thank you!

  14. nicksnotions 1 August 2011 at 05:48 #

    Great instructions! I simply edited the sources and services php and admin css files, dragged the three icons into the images folder, and added the custom php file to my sharedaddy directory and viola!

  15. Christopher Price 13 August 2011 at 02:06 #

    Google posted that they recently updated the Google +1 button to improve performance. Any chance we could see an updated version with this incorporated?

    Doesn’t look like Sharedaddy will be adding Google +1 any time soon, haven’t seen any official comment about it.

    • P0L0 13 August 2011 at 23:32 #

      Hi Christopher,

      I just uploaded Version 1.2 which includes Asynchronous Javascript Loading. I submitted my Code and patches to Sharedaddy but I didn’t got response… ;(

  16. PawnStar 19 August 2011 at 23:34 #

    How would you change the icon it uses to a square one that is the same size as all the other default sharedaddy buttons?

    • P0L0 26 August 2011 at 14:48 #

      I don’t think you can change the icons, you get the icons directly from Google.

  17. 1Earth 2 September 2011 at 04:53 #

    Awesome mod.

    Your “Sharedaddy 0.2.12 with Google +1 – v.1.2″ works perfectly for me; just implement it on a client’s site.

    Thanks for your effort!

  18. petkodimoff 1 October 2011 at 10:30 #

    Thanks – super

  19. Jay 15 December 2011 at 08:01 #

    I’ve created some straightforward instructions on how to add 300+ Specific Sharing Services to Sharedaddy and Jetpack. Let me know if you find it useful!

    Instructions:

    http://blog.shareaholic.com/2011/04/how-to-adding-specific-sharing-services-to-wordpress-com-sharedaddy-or-jetpack/


Leave a Reply