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:

    1
    2
    
    // 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.

    1
    2
    3
    4
    5
    6
    
    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:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?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()
    {
        echo "<div class=\"option option-smart-" . $this->_count ."\">";
  
        if ($this->button_style == 'text' || $this->button_style == 'icon-text') {
                echo $this->get_name();
            } else {
                echo '&nbsp;';
            }    
        echo "</div>";

        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 '';
        }

        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()
    {
        echo "<div class=\"input\">";
        echo "<label>";
        echo "<select name=\"size\">";
        
        foreach ($this->_optionsSize as $value=>$name) {
            echo "<option value=\"" . $value . "\"";
            if ($this->_size == $value) {
                echo " selected=\"selected\"";
            }
            echo ">";
            _e($name, 'sharedaddy');
        }
        
        echo "</select>";
        echo "</label>";
  
        echo "<label>";
        echo "<select name=\"language\">";
      
        foreach ($this->_optionsLanguage as $value=>$name) {
            echo "<option value=\"" . $value . "\"";
            if ($this->_language == $value) {
                echo " selected=\"selected\"";
            }
            echo ">";
            _e($name, 'sharedaddy');
        }
        echo "</select>";
        echo "</label>";
      
        echo "<label>";
        echo "<input type=\"checkbox\"";
        if ($this->_count == 'true') {
            echo ' checked="checked"';
        }
        echo " name=\"count\" /> Show Count";
        echo "</label>";
        echo "</div>";

        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/


ChiliProject 1.4.0 + Ruby Enterprise + Passenger + Apache2
Eclipse/ZendStudio Custom Username for Code Templates
comments powered by Disqus