likes.php
| 1 | <?php |
| 2 | /** |
| 3 | * Module Name: Likes |
| 4 | * Module Description: Likes are a way for people to show their appreciation for content you have written. It’s also a way for you to show the world how popular your content has become. |
| 5 | * First Introduced: 2.2 |
| 6 | * Sort Order: 4 |
| 7 | * Requires Connection: Yes |
| 8 | * Auto Activate: No |
| 9 | * Module Tags: Social |
| 10 | */ |
| 11 | class Jetpack_Likes { |
| 12 | var $version = '20131104'; |
| 13 | |
| 14 | public static function init() { |
| 15 | static $instance = NULL; |
| 16 | |
| 17 | if ( ! $instance ) { |
| 18 | $instance = new Jetpack_Likes; |
| 19 | } |
| 20 | |
| 21 | return $instance; |
| 22 | } |
| 23 | |
| 24 | function __construct() { |
| 25 | $this->in_jetpack = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? false : true; |
| 26 | |
| 27 | add_action( 'init', array( &$this, 'action_init' ) ); |
| 28 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
| 29 | |
| 30 | if ( $this->in_jetpack ) { |
| 31 | add_action( 'jetpack_activate_module_likes', array( $this, 'module_toggle' ) ); |
| 32 | add_action( 'jetpack_deactivate_module_likes', array( $this, 'module_toggle' ) ); |
| 33 | |
| 34 | Jetpack::enable_module_configurable( __FILE__ ); |
| 35 | Jetpack::module_configuration_load( __FILE__, array( $this, 'configuration_redirect' ) ); |
| 36 | |
| 37 | add_action('admin_print_scripts-settings_page_sharing', array( &$this, 'load_jp_css' ) ); |
| 38 | add_filter( 'sharing_show_buttons_on_row_start', array( $this, 'configuration_target_area' ) ); |
| 39 | |
| 40 | $active = Jetpack::get_active_modules(); |
| 41 | |
| 42 | if ( ! in_array( 'sharedaddy', $active ) && ! in_array( 'publicize', $active ) ) { |
| 43 | add_action( 'admin_menu', array( $this, 'sharing_menu' ) ); // we don't have a sharing page yet |
| 44 | } |
| 45 | |
| 46 | if ( in_array( 'publicize', $active ) && ! in_array( 'sharedaddy', $active ) ) { |
| 47 | add_action( 'pre_admin_screen_sharing', array( $this, 'sharing_block' ), 20 ); // we have a sharing page but not the global options area |
| 48 | add_action( 'pre_admin_screen_sharing', array( $this, 'updated_message' ), -10 ); |
| 49 | } |
| 50 | |
| 51 | if( ! in_array( 'sharedaddy', $active ) ) { |
| 52 | add_action( 'admin_init', array( $this, 'process_update_requests_if_sharedaddy_not_loaded' ) ); |
| 53 | add_action( 'sharing_global_options', array( $this, 'admin_settings_showbuttonon_init' ), 19 ); |
| 54 | add_action( 'sharing_admin_update', array( $this, 'admin_settings_showbuttonon_callback' ), 19 ); |
| 55 | add_action( 'admin_init', array( $this, 'add_meta_box' ) ); |
| 56 | } else { |
| 57 | add_filter( 'sharing_meta_box_title', array( $this, 'add_likes_to_sharing_meta_box_title' ) ); |
| 58 | add_action( 'start_sharing_meta_box_content', array( $this, 'meta_box_content' ) ); |
| 59 | } |
| 60 | |
| 61 | Jetpack_Sync::sync_options( __FILE__, 'social_notifications_like' ); |
| 62 | |
| 63 | } else { // wpcom |
| 64 | add_action( 'admin_init', array( $this, 'add_meta_box' ) ); |
| 65 | add_action( 'end_likes_meta_box_content', array( $this, 'sharing_meta_box_content' ) ); |
| 66 | add_filter( 'likes_meta_box_title', array( $this, 'add_likes_to_sharing_meta_box_title' ) ); |
| 67 | } |
| 68 | |
| 69 | add_action( 'admin_init', array( $this, 'admin_discussion_likes_settings_init' ) ); // Likes notifications |
| 70 | |
| 71 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_likes' ), 60 ); |
| 72 | |
| 73 | add_action( 'save_post', array( $this, 'meta_box_save' ) ); |
| 74 | add_action( 'sharing_global_options', array( $this, 'admin_settings_init' ), 20 ); |
| 75 | add_action( 'sharing_admin_update', array( $this, 'admin_settings_callback' ), 20 ); |
| 76 | } |
| 77 | |
| 78 | function module_toggle() { |
| 79 | $jetpack = Jetpack::init(); |
| 80 | $jetpack->sync->register( 'noop' ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Redirects to the likes section of the sharing page. |
| 85 | */ |
| 86 | function configuration_redirect() { |
| 87 | wp_safe_redirect( admin_url( 'options-general.php?page=sharing#likes' ) ); |
| 88 | die(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Loads Jetpack's CSS on the sharing page so we can use .jetpack-targetable |
| 93 | */ |
| 94 | function load_jp_css() { |
| 95 | Jetpack::init()->admin_styles(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box |
| 100 | * @param string $html row heading for the sharedaddy "which page" setting |
| 101 | * @return string html with the jetpack-targetable class and likes id. tbody gets closed after the like settings |
| 102 | */ |
| 103 | function configuration_target_area( $html = '' ) { |
| 104 | $html = "<tbody id='likes' class='jetpack-targetable'>" . $html; |
| 105 | return $html; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares" |
| 110 | * @param string $title The current title of the metabox, not needed/used. |
| 111 | */ |
| 112 | function add_likes_to_sharing_meta_box_title( $title ) { |
| 113 | return __( 'Likes and Shares', 'jetpack' ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Adds a metabox to the post screen if the sharing one doesn't currently exist. |
| 118 | */ |
| 119 | function add_meta_box() { |
| 120 | if ( apply_filters( 'post_flair_disable', false ) ) |
| 121 | return; |
| 122 | |
| 123 | $post_types = get_post_types( array( 'public' => true ) ); |
| 124 | $title = apply_filters( 'likes_meta_box_title', __( 'Likes', 'jetpack' ) ); |
| 125 | foreach( $post_types as $post_type ) { |
| 126 | add_meta_box( 'likes_meta', $title, array( $this, 'meta_box_content' ), $post_type, 'advanced', 'high' ); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | function meta_box_save( $post_id ) { |
| 131 | if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) |
| 132 | return $post_id; |
| 133 | |
| 134 | // Record sharing disable. Only needs to be done for WPCOM |
| 135 | if ( ! $this->in_jetpack ) { |
| 136 | if ( isset( $_POST['post_type'] ) && ( 'post' == $_POST['post_type'] || 'page' == $_POST['post_type'] ) ) { |
| 137 | if ( isset( $_POST['wpl_sharing_status_hidden'] ) && !isset( $_POST['wpl_enable_post_sharing'] ) ) { |
| 138 | update_post_meta( $post_id, 'sharing_disabled', 1 ); |
| 139 | } else { |
| 140 | delete_post_meta( $post_id, 'sharing_disabled' ); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if ( empty( $_POST['wpl_like_status_hidden'] ) ) |
| 146 | return $post_id; |
| 147 | |
| 148 | if ( 'post' == $_POST['post_type'] ) { |
| 149 | if ( !current_user_can( 'edit_post', $post_id ) ) { |
| 150 | return $post_id; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Record a change in like status for this post - only if it contradicts the |
| 155 | // site like setting. |
| 156 | if ( ( $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) || ( ! $this->is_enabled_sitewide() && !empty( $_POST['wpl_enable_post_likes'] ) ) ) { |
| 157 | update_post_meta( $post_id, 'switch_like_status', 1 ); |
| 158 | //$g_gif = file_get_contents( 'http://stats.wordpress.com/g.gif?v=wpcom-no-pv&x_likes=switched_post_like_status' ); @todo stat |
| 159 | } else { |
| 160 | delete_post_meta( $post_id, 'switch_like_status' ); |
| 161 | } |
| 162 | |
| 163 | return $post_id; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Shows the likes option in the post screen metabox. |
| 168 | */ |
| 169 | function meta_box_content( $post ) { |
| 170 | $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID(); |
| 171 | $checked = true; |
| 172 | $disabled = ! $this->is_enabled_sitewide(); |
| 173 | $switched_status = get_post_meta( $post_id, 'switch_like_status', true ); |
| 174 | |
| 175 | if ( $disabled && empty( $switched_status ) || false == $disabled && !empty( $switched_status ) ) |
| 176 | $checked = false; |
| 177 | |
| 178 | do_action( 'start_likes_meta_box_content', $post ); |
| 179 | ?> |
| 180 | |
| 181 | <p> |
| 182 | <label for="wpl_enable_post_likes"> |
| 183 | <input type="checkbox" name="wpl_enable_post_likes" id="wpl_enable_post_likes" value="1" <?php checked( $checked ); ?>> |
| 184 | <?php esc_html_e( 'Show likes.', 'jetpack' ); ?> |
| 185 | </label> |
| 186 | <input type="hidden" name="wpl_like_status_hidden" value="1" /> |
| 187 | </p> <?php |
| 188 | do_action( 'end_likes_meta_box_content', $post ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * WordPress.com: Metabox option for sharing (sharedaddy will handle this on the JP blog) |
| 193 | */ |
| 194 | function sharing_meta_box_content( $post ) { |
| 195 | $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID(); |
| 196 | $disabled = get_post_meta( $post_id, 'sharing_disabled', true ); ?> |
| 197 | <p> |
| 198 | <label for="wpl_enable_post_sharing"> |
| 199 | <input type="checkbox" name="wpl_enable_post_sharing" id="wpl_enable_post_sharing" value="1" <?php checked( !$disabled ); ?>> |
| 200 | <?php _e( 'Show sharing buttons.', 'jetpack' ); ?> |
| 201 | </label> |
| 202 | <input type="hidden" name="wpl_sharing_status_hidden" value="1" /> |
| 203 | </p> <?php |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page) |
| 208 | */ |
| 209 | |
| 210 | function admin_discussion_likes_settings_init() { |
| 211 | // Add a temporary section, until we can move the setting out of there and with the rest of the email notification settings |
| 212 | add_settings_section( 'likes-notifications', __( 'Likes Notifications', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_section' ), 'discussion' ); |
| 213 | add_settings_field( 'social-notifications', __( 'Email me whenever', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_field' ), 'discussion', 'likes-notifications' ); |
| 214 | // Register the setting |
| 215 | register_setting( 'discussion', 'social_notifications_like', array( $this, 'admin_discussion_likes_settings_validate' ) ); |
| 216 | } |
| 217 | |
| 218 | function admin_discussion_likes_settings_section() { |
| 219 | // Atypical usage here. We emit jquery to move likes notification checkbox to be with the rest of the email notification settings |
| 220 | ?> |
| 221 | <script type="text/javascript"> |
| 222 | jQuery( function( $ ) { |
| 223 | var table = $( '#social_notifications_like' ).parents( 'table:first' ), |
| 224 | header = table.prevAll( 'h3:first' ), |
| 225 | newParent = $( '#moderation_notify' ).parent( 'label' ).parent(); |
| 226 | |
| 227 | if ( !table.size() || !header.size() || !newParent.size() ) { |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | newParent.append( '<br/>' ).append( table.end().parent( 'label' ).siblings().andSelf() ); |
| 232 | header.remove(); |
| 233 | table.remove(); |
| 234 | } ); |
| 235 | </script> |
| 236 | <?php |
| 237 | } |
| 238 | |
| 239 | function admin_likes_get_option( $option ) { |
| 240 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 241 | $option_setting = get_blog_option( get_current_blog_id(), $option ); |
| 242 | } else { |
| 243 | $option_setting = get_option( $option ); |
| 244 | } |
| 245 | |
| 246 | return intval( 'on' == $option_setting ); |
| 247 | } |
| 248 | |
| 249 | function admin_discussion_likes_settings_field() { |
| 250 | $like = $this->admin_likes_get_option( 'social_notifications_like' ); |
| 251 | ?> |
| 252 | <label><input type="checkbox" id="social_notifications_like" name="social_notifications_like" value="1" <?php checked( $like ); ?> /> <?php esc_html_e( 'Someone likes one of my posts', 'jetpack' ); ?></label> |
| 253 | <?php |
| 254 | } |
| 255 | |
| 256 | function admin_discussion_likes_settings_validate( $input ) { |
| 257 | // If it's not set (was unchecked during form submission) or was set to off (during option update), return 'off'. |
| 258 | if ( !$input || 'off' == $input ) |
| 259 | return 'off'; |
| 260 | |
| 261 | // Otherwise, return 'on'. |
| 262 | return 'on'; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * The actual options block to be inserted into the sharing page. |
| 267 | */ |
| 268 | function admin_settings_init() { ?> |
| 269 | <tr> |
| 270 | <th scope="row"> |
| 271 | <label><?php esc_html_e( 'WordPress.com Likes are', 'jetpack' ); ?></label> |
| 272 | </th> |
| 273 | <td> |
| 274 | <div> |
| 275 | <label> |
| 276 | <input type="radio" class="code" name="wpl_default" value="on" <?php checked( $this->is_enabled_sitewide(), true ); ?> /> |
| 277 | <?php esc_html_e( 'On for all posts', 'jetpack' ); ?> |
| 278 | </label> |
| 279 | </div> |
| 280 | <div> |
| 281 | <label> |
| 282 | <input type="radio" class="code" name="wpl_default" value="off" <?php checked( $this->is_enabled_sitewide(), false ); ?> /> |
| 283 | <?php esc_html_e( 'Turned on per post', 'jetpack' ); ?> |
| 284 | </label> |
| 285 | <div> |
| 286 | </td> |
| 287 | </tr> <?php /* |
| 288 | <tr> |
| 289 | <th scope="row"> |
| 290 | <label><?php esc_html_e( 'Comment Likes', 'jetpack' ); ?></label> |
| 291 | </th> |
| 292 | <td> |
| 293 | <div> |
| 294 | <label> |
| 295 | <input type="checkbox" class="code" name="jetpack_comment_likes_enabled" value="1" <?php checked( $this->is_comments_enabled(), true ); ?> /> |
| 296 | <?php esc_html_e( 'Allow people to like comments', 'jetpack' ); ?> |
| 297 | </label> |
| 298 | </div> |
| 299 | </td> |
| 300 | </tr> */ ?> |
| 301 | </tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?> |
| 302 | <?php } |
| 303 | |
| 304 | /** |
| 305 | * If sharedaddy is not loaded, we don't have the "Show buttons on" yet, so we need to add that since it affects likes too. |
| 306 | */ |
| 307 | function admin_settings_showbuttonon_init() { ?> |
| 308 | <?php echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' ); ?> |
| 309 | <th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th> |
| 310 | <td> |
| 311 | <?php |
| 312 | $br = false; |
| 313 | $shows = array_values( get_post_types( array( 'public' => true ) ) ); |
| 314 | array_unshift( $shows, 'index' ); |
| 315 | $global = $this->get_options(); |
| 316 | foreach ( $shows as $show ) : |
| 317 | if ( 'index' == $show ) { |
| 318 | $label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' ); |
| 319 | } else { |
| 320 | $post_type_object = get_post_type_object( $show ); |
| 321 | $label = $post_type_object->labels->name; |
| 322 | } |
| 323 | ?> |
| 324 | <?php if ( $br ) echo '<br />'; ?><label><input type="checkbox"<?php checked( in_array( $show, $global['show'] ) ); ?> name="show[]" value="<?php echo esc_attr( $show ); ?>" /> <?php echo esc_html( $label ); ?></label> |
| 325 | <?php $br = true; endforeach; ?> |
| 326 | </td> |
| 327 | <?php echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' ); ?> |
| 328 | <?php } |
| 329 | |
| 330 | |
| 331 | /** |
| 332 | * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option. |
| 333 | */ |
| 334 | function admin_settings_showbuttonon_callback() { |
| 335 | $options = get_option( 'sharing-options' ); |
| 336 | if ( !is_array( $options ) ) |
| 337 | $options = array(); |
| 338 | |
| 339 | $shows = array_values( get_post_types( array( 'public' => true ) ) ); |
| 340 | $shows[] = 'index'; |
| 341 | $data = $_POST; |
| 342 | |
| 343 | if ( isset( $data['show'] ) ) { |
| 344 | if ( is_scalar( $data['show'] ) ) { |
| 345 | switch ( $data['show'] ) { |
| 346 | case 'posts' : |
| 347 | $data['show'] = array( 'post', 'page' ); |
| 348 | break; |
| 349 | case 'index' : |
| 350 | $data['show'] = array( 'index' ); |
| 351 | break; |
| 352 | case 'posts-index' : |
| 353 | $data['show'] = array( 'post', 'page', 'index' ); |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if ( $data['show'] = array_intersect( $data['show'], $shows ) ) { |
| 359 | $options['global']['show'] = $data['show']; |
| 360 | } |
| 361 | } else { |
| 362 | $options['global']['show'] = array(); |
| 363 | } |
| 364 | |
| 365 | update_option( 'sharing-options', $options ); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled. |
| 370 | */ |
| 371 | function process_update_requests_if_sharedaddy_not_loaded() { |
| 372 | if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) { |
| 373 | if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) { |
| 374 | do_action( 'sharing_admin_update' ); |
| 375 | wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) ); |
| 376 | die(); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Saves the setting in the database, bumps a stat on WordPress.com |
| 383 | */ |
| 384 | function admin_settings_callback() { |
| 385 | // We're looking for these, and doing a dance to set some stats and save |
| 386 | // them together in array option. |
| 387 | $new_state = !empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on'; |
| 388 | $db_state = $this->is_enabled_sitewide(); |
| 389 | |
| 390 | /** Default State *********************************************************/ |
| 391 | |
| 392 | // Checked (enabled) |
| 393 | switch( $new_state ) { |
| 394 | case 'off' : |
| 395 | if ( true == $db_state && ! $this->in_jetpack ) { |
| 396 | $g_gif = file_get_contents( 'http://stats.wordpress.com/g.gif?v=wpcom-no-pv&x_likes=disabled_likes' ); |
| 397 | } |
| 398 | update_option( 'disabled_likes', 1 ); |
| 399 | break; |
| 400 | case 'on' : |
| 401 | default: |
| 402 | if ( false == $db_state && ! $this->in_jetpack ) { |
| 403 | $g_gif = file_get_contents( 'http://stats.wordpress.com/g.gif?v=wpcom-no-pv&x_likes=reenabled_likes' ); |
| 404 | } |
| 405 | delete_option( 'disabled_likes' ); |
| 406 | break; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | // comment setting |
| 411 | $new_comments_state = !empty( $_POST['jetpack_comment_likes_enabled'] ) ? $_POST['jetpack_comment_likes_enabled'] : false; |
| 412 | switch( (bool) $new_comments_state ) { |
| 413 | case true: |
| 414 | update_option( 'jetpack_comment_likes_enabled', 1 ); |
| 415 | break; |
| 416 | case false: |
| 417 | default: |
| 418 | update_option( 'jetpack_comment_likes_enabled', 0 ); |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Adds the 'sharing' menu to the settings menu. |
| 425 | * Only ran if sharedaddy and publicize are not already active. |
| 426 | */ |
| 427 | function sharing_menu() { |
| 428 | add_submenu_page( 'options-general.php', esc_html__( 'Sharing Settings', 'jetpack' ), esc_html__( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( $this, 'sharing_page' ) ); |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Provides a sharing page with the sharing_global_options hook |
| 433 | * so we can display the setting. |
| 434 | * Only ran if sharedaddy and publicize are not already active. |
| 435 | */ |
| 436 | function sharing_page() { |
| 437 | $this->updated_message(); ?> |
| 438 | <div class="wrap"> |
| 439 | <div class="icon32" id="icon-options-general"><br /></div> |
| 440 | <h2><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h2> |
| 441 | <?php do_action( 'pre_admin_screen_sharing' ) ?> |
| 442 | <?php $this->sharing_block(); ?> |
| 443 | </div> <?php |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Returns the settings have been saved message. |
| 448 | */ |
| 449 | function updated_message() { |
| 450 | if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' ) |
| 451 | echo '<div class="updated"><p>' . esc_html__( 'Settings have been saved', 'jetpack' ) . '</p></div>'; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts |
| 456 | */ |
| 457 | function sharing_block() { ?> |
| 458 | <h3><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h3> |
| 459 | <form method="post" action=""> |
| 460 | <table class="form-table"> |
| 461 | <tbody> |
| 462 | <?php do_action( 'sharing_global_options' ); ?> |
| 463 | </tbody> |
| 464 | </table> |
| 465 | |
| 466 | <p class="submit"> |
| 467 | <input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" /> |
| 468 | </p> |
| 469 | |
| 470 | <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" /> |
| 471 | </form> <?php |
| 472 | } |
| 473 | |
| 474 | function admin_init() { |
| 475 | add_filter( 'manage_posts_columns', array( $this, 'add_like_count_column' ) ); |
| 476 | add_filter( 'manage_pages_columns', array( $this, 'add_like_count_column' ) ); |
| 477 | add_action( 'manage_posts_custom_column', array( $this, 'likes_edit_column' ), 10, 2 ); |
| 478 | add_action( 'manage_pages_custom_column', array( $this, 'likes_edit_column' ), 10, 2 ); |
| 479 | add_action( 'admin_print_styles-edit.php', array( $this, 'load_admin_css' ) ); |
| 480 | add_action( "admin_print_scripts-edit.php", array( $this, 'enqueue_admin_scripts' ) ); |
| 481 | } |
| 482 | |
| 483 | function action_init() { |
| 484 | if ( is_admin() ) |
| 485 | return; |
| 486 | |
| 487 | if ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) || |
| 488 | ( defined( 'APP_REQUEST' ) && APP_REQUEST ) || |
| 489 | ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) || |
| 490 | ( defined( 'COOKIE_AUTH_REQUEST' ) && COOKIE_AUTH_REQUEST ) || |
| 491 | ( defined( 'JABBER_SERVER' ) && JABBER_SERVER ) ) |
| 492 | return; |
| 493 | |
| 494 | // Comment Likes widget has been disabled, pending performance improvements. |
| 495 | // add_filter( 'comment_text', array( &$this, 'comment_likes' ), 10, 2 ); |
| 496 | |
| 497 | if ( $this->in_jetpack ) { |
| 498 | add_filter( 'the_content', array( &$this, 'post_likes' ), 30, 1 ); |
| 499 | wp_enqueue_script( 'postmessage', plugins_url( '_inc/postmessage.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
| 500 | wp_enqueue_script( 'jquery_inview', plugins_url( '_inc/jquery.inview.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
| 501 | wp_enqueue_script( 'jetpack_resize', plugins_url( '_inc/jquery.jetpack-resize.js' , dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
| 502 | wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION ); |
| 503 | |
| 504 | } else { |
| 505 | add_filter( 'post_flair', array( &$this, 'post_likes' ), 30, 1 ); |
| 506 | add_filter( 'post_flair_block_css', array( $this, 'post_flair_service_enabled_like' ) ); |
| 507 | |
| 508 | wp_enqueue_script( 'postmessage', '/wp-content/js/postmessage.js', array( 'jquery' ), JETPACK__VERSION, false ); |
| 509 | wp_enqueue_script( 'jquery_inview', '/wp-content/js/jquery/jquery.inview.js', array( 'jquery' ), JETPACK__VERSION, false ); |
| 510 | wp_enqueue_script( 'jetpack_resize', '/wp-content/js/jquery/jquery.jetpack-resize.js', array( 'jquery' ), JETPACK__VERSION, false ); |
| 511 | wp_enqueue_style( 'jetpack_likes', plugins_url( 'jetpack-likes.css', __FILE__ ), array(), JETPACK__VERSION ); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Load the CSS needed for the wp-admin area. |
| 517 | */ |
| 518 | function load_admin_css() { |
| 519 | include( ABSPATH . WPINC . '/version.php' ); |
| 520 | ?> |
| 521 | <style type="text/css"> |
| 522 | .fixed .column-likes { width: 5em; padding-top: 8px; text-align: center !important; } |
| 523 | .fixed .column-stats { width: 5em; } |
| 524 | .fixed .column-likes .post-com-count { background-image: none; } |
| 525 | .fixed .column-likes .comment-count { background-color: #888; } |
| 526 | .fixed .column-likes .comment-count:hover { background-color: #D54E21; } |
| 527 | <?php if ( version_compare( $wp_version, '3.8-alpha', '>=' ) ) : ?> |
| 528 | .fixed .column-likes .post-com-count::after { border: none !important; } |
| 529 | .fixed .column-likes .comment-count { background-color: #bbb; } |
| 530 | .fixed .column-likes .comment-count:hover { background-color: #2ea2cc; } |
| 531 | .fixed .column-likes .vers img { display: none; } |
| 532 | .fixed .column-likes .vers:before { |
| 533 | font: normal 20px/1 dashicons; |
| 534 | content: '\f155'; |
| 535 | speak: none; |
| 536 | -webkit-font-smoothing: antialiased; |
| 537 | -moz-osx-font-smoothing: grayscale; |
| 538 | } |
| 539 | <?php endif; ?> |
| 540 | </style> |
| 541 | <?php |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Load the JS required for loading the like counts. |
| 546 | */ |
| 547 | function enqueue_admin_scripts() { |
| 548 | if ( empty( $_GET['post_type'] ) || 'post' == $_GET['post_type'] || 'page' == $_GET['post_type'] ) { |
| 549 | if ( $this->in_jetpack ) { |
| 550 | wp_enqueue_script( 'likes-post-count', plugins_url( 'modules/likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION ); |
| 551 | wp_enqueue_script( 'likes-post-count-jetpack', plugins_url( 'modules/likes/post-count-jetpack.js', dirname( __FILE__ ) ), array( 'likes-post-count' ), JETPACK__VERSION ); |
| 552 | } else { |
| 553 | wp_enqueue_script( 'jquery.wpcom-proxy-request', "/wp-content/js/jquery/jquery.wpcom-proxy-request.js", array('jquery'), NULL, true ); |
| 554 | wp_enqueue_script( 'likes-post-count', plugins_url( 'likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION ); |
| 555 | wp_enqueue_script( 'likes-post-count-wpcom', plugins_url( 'likes/post-count-wpcom.js', dirname( __FILE__ ) ), array( 'likes-post-count', 'jquery.wpcom-proxy-request' ), JETPACK__VERSION ); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Add "Likes" column data to the post edit table in wp-admin. |
| 562 | * |
| 563 | * @param string $column_name |
| 564 | * @param int $post_id |
| 565 | */ |
| 566 | function likes_edit_column( $column_name, $post_id ) { |
| 567 | if ( 'likes' == $column_name ) { |
| 568 | |
| 569 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 570 | $blog_id = get_current_blog_id(); |
| 571 | } else { |
| 572 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 573 | } |
| 574 | |
| 575 | $permalink = get_permalink( get_the_ID() ); ?> |
| 576 | <a title="" data-post-id="<?php echo (int) $post_id; ?>" class="post-com-count post-like-count" id="post-like-count-<?php echo (int) $post_id; ?>" data-blog-id="<?php echo (int) $blog_id; ?>" href="<?php echo esc_url( $permalink ); ?>#like-<?php echo (int) $post_id; ?>"> |
| 577 | <span class="comment-count">0</span> |
| 578 | </a> |
| 579 | <?php |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Add a "Likes" column header to the post edit table in wp-admin. |
| 585 | * |
| 586 | * @param array $columns |
| 587 | * @return array |
| 588 | */ |
| 589 | function add_like_count_column( $columns ) { |
| 590 | $date = $columns['date']; |
| 591 | unset( $columns['date'] ); |
| 592 | |
| 593 | $columns['likes'] = '<span class="vers"><img title="' . esc_attr__( 'Likes', 'jetpack' ) . '" alt="' . esc_attr__( 'Likes', 'jetpack' ) . '" src="//s0.wordpress.com/i/like-grey-icon.png" /></span>'; |
| 594 | $columns['date'] = $date; |
| 595 | |
| 596 | return $columns; |
| 597 | } |
| 598 | |
| 599 | function post_likes( $content ) { |
| 600 | global $post; |
| 601 | |
| 602 | if ( ! $this->is_likes_visible() ) |
| 603 | return $content; |
| 604 | |
| 605 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 606 | $blog_id = get_current_blog_id(); |
| 607 | $bloginfo = get_blog_details( (int) $blog_id ); |
| 608 | $domain = $bloginfo->domain; |
| 609 | } else { |
| 610 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 611 | $url = home_url(); |
| 612 | $url_parts = parse_url( $url ); |
| 613 | $domain = $url_parts['host']; |
| 614 | } |
| 615 | |
| 616 | add_filter( 'wp_footer', array( $this, 'likes_master' ) ); |
| 617 | |
| 618 | /** |
| 619 | * if the same post appears more then once on a page the page goes crazy |
| 620 | * we need a slightly more unique id / name for the widget wrapper. |
| 621 | */ |
| 622 | $uniqid = uniqid(); |
| 623 | |
| 624 | $src = sprintf( '//widgets.wp.com/likes/#blog_id=%1$d&post_id=%2$d&origin=%3$s&obj_id=%1$d-%2$d-%4$s', $blog_id, $post->ID, $domain, $uniqid ); |
| 625 | $name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post->ID, $uniqid ); |
| 626 | $wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post->ID, $uniqid ); |
| 627 | |
| 628 | $html = "<div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper' data-src='$src' data-name='$name'><h3 class='sd-title'>" . esc_html__( 'Like this:', 'jetpack' ) . '</h3>'; |
| 629 | $html .= "<div class='likes-widget-placeholder post-likes-widget-placeholder' style='height:55px'><span class='button'><span>" . esc_html__( 'Like', 'jetpack' ) . '</span></span> <span class="loading">' . esc_html__( 'Loading...', 'jetpack' ) . '</span></div>'; |
| 630 | $html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>"; |
| 631 | $html .= '</div>'; |
| 632 | |
| 633 | return $content . $html; |
| 634 | } |
| 635 | |
| 636 | function comment_likes( $content, $comment = null ) { |
| 637 | if ( empty( $comment ) ) |
| 638 | return $content; |
| 639 | |
| 640 | if ( ! $this->is_comments_enabled() ) |
| 641 | return $content; |
| 642 | |
| 643 | $protocol = 'http'; |
| 644 | if ( is_ssl() ) |
| 645 | $protocol = 'https'; |
| 646 | |
| 647 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 648 | $blog_id = get_current_blog_id(); |
| 649 | $bloginfo = get_blog_details( (int) $blog_id ); |
| 650 | $domain = $bloginfo->domain; |
| 651 | } else { |
| 652 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 653 | $url = home_url(); |
| 654 | $url_parts = parse_url( $url ); |
| 655 | $domain = $url_parts['host']; |
| 656 | } |
| 657 | |
| 658 | add_filter( 'wp_footer', array( $this, 'likes_master' ) ); |
| 659 | |
| 660 | $src = sprintf( '%1$s://widgets.wp.com/likes/#blog_id=%2$d&comment_id=%3$d&origin=%1$s://%4$s', $protocol, $blog_id, $comment->comment_ID, $domain ); |
| 661 | $name = sprintf( 'like-comment-frame-%1$d-%2$d', $blog_id, $comment->comment_ID ); |
| 662 | $wrapper = sprintf( 'like-comment-wrapper-%1$d-%2$d', $blog_id, $comment->comment_ID ); |
| 663 | |
| 664 | $html = "<div><div class='jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper'>"; |
| 665 | $html .= "<iframe class='comment-likes-widget jetpack-likes-widget' name='$name' height='16px' width='100%' data='$src'></iframe>"; |
| 666 | $html .= '</div></div>'; |
| 667 | return $content . $html; |
| 668 | } |
| 669 | |
| 670 | function post_flair_service_enabled_like( $classes ) { |
| 671 | $classes[] = 'sd-like-enabled'; |
| 672 | return $classes; |
| 673 | } |
| 674 | |
| 675 | function admin_bar_likes() { |
| 676 | global $wp_admin_bar, $post; |
| 677 | |
| 678 | if ( ! $this->is_admin_bar_button_visible() ) { |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | $protocol = 'http'; |
| 683 | if ( is_ssl() ) |
| 684 | $protocol = 'https'; |
| 685 | |
| 686 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 687 | $blog_id = get_current_blog_id(); |
| 688 | $bloginfo = get_blog_details( (int) $blog_id ); |
| 689 | $domain = $bloginfo->domain; |
| 690 | } else { |
| 691 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 692 | $url = home_url(); |
| 693 | $url_parts = parse_url( $url ); |
| 694 | $domain = $url_parts['host']; |
| 695 | } |
| 696 | |
| 697 | add_filter( 'wp_footer', array( $this, 'likes_master' ) ); |
| 698 | |
| 699 | $src = sprintf( '%1$s://widgets.wp.com/likes/#blog_id=%2$d&post_id=%3$d&origin=%1$s://%4$s', $protocol, $blog_id, $post->ID, $domain ); |
| 700 | |
| 701 | $html = "<iframe class='admin-bar-likes-widget jetpack-likes-widget' scrolling='no' frameBorder='0' name='admin-bar-likes-widget' src='$src'></iframe>"; |
| 702 | |
| 703 | $node = array( |
| 704 | 'id' => 'admin-bar-likes-widget', |
| 705 | 'meta' => array( |
| 706 | 'html' => $html |
| 707 | ) |
| 708 | ); |
| 709 | |
| 710 | $wp_admin_bar->add_node( $node ); |
| 711 | } |
| 712 | |
| 713 | function likes_master() { |
| 714 | $protocol = 'http'; |
| 715 | if ( is_ssl() ) |
| 716 | $protocol = 'https'; |
| 717 | |
| 718 | if ( version_compare( $GLOBALS['wp_version'], '3.8-alpha', '>=' ) ) { |
| 719 | add_filter( 'mp6_enabled', '__return_true', 97 ); |
| 720 | } |
| 721 | |
| 722 | $locale = ( '' == get_locale() || 'en' == get_locale() ) ? '' : '&lang=' . strtolower( substr( get_locale(), 0, 2 ) ); |
| 723 | $src = sprintf( '%1$s://widgets.wp.com/likes/master.html?ver=%2$s#ver=%2$s%3$s&mp6=%4$d', $protocol, $this->version, $locale, apply_filters( 'mp6_enabled', 0 ) ); |
| 724 | |
| 725 | // Tidy up after ourselves. |
| 726 | if ( version_compare( $GLOBALS['wp_version'], '3.8-alpha', '>=' ) ) { |
| 727 | remove_filter( 'mp6_enabled', '__return_true', 97 ); |
| 728 | } |
| 729 | |
| 730 | $likersText = wp_kses( __( '<span>%d</span> bloggers like this:', 'jetpack' ), array( 'span' => array() ) ); |
| 731 | ?> |
| 732 | <iframe src='<?php echo $src; ?>' scrolling='no' id='likes-master' name='likes-master' style='display:none;'></iframe> |
| 733 | <div id='likes-other-gravatars'><div class="likes-text"><?php echo $likersText; ?></div><ul class="wpl-avatars sd-like-gravatars"></ul></div> |
| 734 | <script type="text/javascript"> |
| 735 | //<![CDATA[ |
| 736 | var jetpackLikesWidgetQueue = []; |
| 737 | var jetpackLikesWidgetBatch = []; |
| 738 | var jetpackLikesMasterReady = false; |
| 739 | |
| 740 | function JetpackLikespostMessage( message, target ) { |
| 741 | if ( "string" === typeof message ){ |
| 742 | try{ |
| 743 | message = JSON.parse( message ); |
| 744 | } |
| 745 | catch(e) { |
| 746 | return; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | pm( { |
| 751 | target: target, |
| 752 | type: 'likesMessage', |
| 753 | data: message, |
| 754 | origin: '*' |
| 755 | } ); |
| 756 | } |
| 757 | |
| 758 | function JetpackLikesBatchHandler() { |
| 759 | var requests = []; |
| 760 | jQuery( 'div.jetpack-likes-widget-unloaded' ).each( function( i ) { |
| 761 | if ( jetpackLikesWidgetBatch.indexOf( this.id ) > -1 ) |
| 762 | return; |
| 763 | jetpackLikesWidgetBatch.push( this.id ); |
| 764 | var regex = /like-(post|comment)-wrapper-(\d+)-(\d+)-(\w+)/; |
| 765 | var match = regex.exec( this.id ); |
| 766 | if ( ! match || match.length != 5 ) |
| 767 | return; |
| 768 | |
| 769 | var info = { |
| 770 | blog_id: match[2], |
| 771 | width: this.width |
| 772 | }; |
| 773 | |
| 774 | if ( 'post' == match[1] ) { |
| 775 | info.post_id = match[3]; |
| 776 | } else if ( 'comment' == match[1] ) { |
| 777 | info.comment_id = match[3]; |
| 778 | } |
| 779 | |
| 780 | info.obj_id = match[4]; |
| 781 | |
| 782 | requests.push( info ); |
| 783 | }); |
| 784 | |
| 785 | if ( requests.length > 0 ) { |
| 786 | JetpackLikespostMessage( { event: 'initialBatch', requests: requests }, window.frames['likes-master'] ); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | function JetpackLikesMessageListener( event ) { |
| 791 | if ( "undefined" == typeof event.event ) |
| 792 | return; |
| 793 | |
| 794 | if ( 'masterReady' == event.event ) { |
| 795 | jQuery( document ).ready( function() { |
| 796 | jetpackLikesMasterReady = true; |
| 797 | |
| 798 | var stylesData = { |
| 799 | event: 'injectStyles' |
| 800 | }; |
| 801 | |
| 802 | if ( jQuery( 'iframe.admin-bar-likes-widget' ).length > 0 ) { |
| 803 | JetpackLikespostMessage( { event: 'adminBarEnabled' }, window.frames[ 'likes-master' ] ); |
| 804 | |
| 805 | stylesData.adminBarStyles = { |
| 806 | background: jQuery( '#wpadminbar .quicklinks li#wp-admin-bar-wpl-like > a' ).css( 'background' ) |
| 807 | }; |
| 808 | } |
| 809 | |
| 810 | if ( !window.addEventListener ) |
| 811 | jQuery( '#wp-admin-bar-admin-bar-likes-widget' ).hide(); |
| 812 | |
| 813 | stylesData.textStyles = { |
| 814 | color: jQuery( '.sd-text-color').css( 'color' ), |
| 815 | fontFamily: jQuery( '.sd-text-color' ).css( 'font-family' ), |
| 816 | fontSize: jQuery( '.sd-text-color' ).css( 'font-size' ), |
| 817 | direction: jQuery( '.sd-text-color' ).css( 'direction' ), |
| 818 | fontWeight: jQuery( '.sd-text-color' ).css( 'font-weight' ), |
| 819 | fontStyle: jQuery( '.sd-text-color' ).css( 'font-style' ), |
| 820 | textDecoration: jQuery( '.sd-text-color' ).css('text-decoration') |
| 821 | }; |
| 822 | |
| 823 | stylesData.linkStyles = { |
| 824 | color: jQuery( '.sd-link-color' ).css('color'), |
| 825 | fontFamily: jQuery( '.sd-link-color' ).css('font-family'), |
| 826 | fontSize: jQuery( '.sd-link-color' ).css('font-size'), |
| 827 | textDecoration: jQuery( '.sd-link-color' ).css('text-decoration'), |
| 828 | fontWeight: jQuery( '.sd-link-color' ).css( 'font-weight' ), |
| 829 | fontStyle: jQuery( '.sd-link-color' ).css( 'font-style' ) |
| 830 | }; |
| 831 | |
| 832 | JetpackLikespostMessage( stylesData, window.frames[ 'likes-master' ] ); |
| 833 | |
| 834 | JetpackLikesBatchHandler(); |
| 835 | |
| 836 | jQuery( document ).on( 'inview', 'div.jetpack-likes-widget-unloaded', function() { |
| 837 | jetpackLikesWidgetQueue.push( this.id ); |
| 838 | }); |
| 839 | }); |
| 840 | } |
| 841 | |
| 842 | if ( 'showLikeWidget' == event.event ) { |
| 843 | jQuery( '#' + event.id + ' .post-likes-widget-placeholder' ).fadeOut( 'fast', function() { |
| 844 | jQuery( '#' + event.id + ' .post-likes-widget' ).fadeIn( 'fast', function() { |
| 845 | JetpackLikespostMessage( { event: 'likeWidgetDisplayed', blog_id: event.blog_id, post_id: event.post_id, obj_id: event.obj_id }, window.frames['likes-master'] ); |
| 846 | }); |
| 847 | }); |
| 848 | } |
| 849 | |
| 850 | if ( 'showOtherGravatars' == event.event ) { |
| 851 | var $container = jQuery( '#likes-other-gravatars' ); |
| 852 | var $list = $container.find( 'ul' ); |
| 853 | |
| 854 | $container.hide(); |
| 855 | $list.html( '' ); |
| 856 | |
| 857 | $container.find( '.likes-text span' ).text( event.total ); |
| 858 | |
| 859 | jQuery.each( event.likers, function( i, liker ) { |
| 860 | $list.append( '<li class="' + liker.css_class + '"><a href="' + liker.profile_URL + '" class="wpl-liker" rel="nofollow" target="_parent"><img src="' + liker.avatar_URL + '" alt="' + liker.name + '" width="30" height="30" style="padding-right: 3px;" /></a></li>'); |
| 861 | } ); |
| 862 | |
| 863 | var offset = jQuery( "[name='" + event.parent + "']" ).offset(); |
| 864 | |
| 865 | $container.css( 'left', offset.left + event.position.left - 10 + 'px' ); |
| 866 | $container.css( 'top', offset.top + event.position.top - 33 + 'px' ); |
| 867 | |
| 868 | var rowLength = Math.floor( event.width / 37 ); |
| 869 | var height = ( Math.ceil( event.likers.length / rowLength ) * 37 ) + 13; |
| 870 | if ( height > 204 ) { |
| 871 | height = 204; |
| 872 | } |
| 873 | |
| 874 | $container.css( 'height', height + 'px' ); |
| 875 | $container.css( 'width', rowLength * 37 - 7 + 'px' ); |
| 876 | |
| 877 | $list.css( 'width', rowLength * 37 + 'px' ); |
| 878 | |
| 879 | $container.fadeIn( 'slow' ); |
| 880 | |
| 881 | var scrollbarWidth = $list[0].offsetWidth - $list[0].clientWidth; |
| 882 | if ( scrollbarWidth > 0 ) { |
| 883 | $container.width( $container.width() + scrollbarWidth ); |
| 884 | $list.width( $list.width() + scrollbarWidth ); |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | pm.bind( 'likesMessage', function(e) { JetpackLikesMessageListener(e); } ); |
| 890 | |
| 891 | jQuery( document ).click( function( e ) { |
| 892 | var $container = jQuery( '#likes-other-gravatars' ); |
| 893 | |
| 894 | if ( $container.has( e.target ).length === 0 ) { |
| 895 | $container.fadeOut( 'slow' ); |
| 896 | } |
| 897 | }); |
| 898 | |
| 899 | function JetpackLikesWidgetQueueHandler() { |
| 900 | var wrapperID; |
| 901 | if ( ! jetpackLikesMasterReady ) { |
| 902 | setTimeout( JetpackLikesWidgetQueueHandler, 500 ); |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | if ( jetpackLikesWidgetQueue.length > 0 ) { |
| 907 | // We may have a widget that needs creating now |
| 908 | var found = false; |
| 909 | while( jetpackLikesWidgetQueue.length > 0 ) { |
| 910 | // Grab the first member of the queue that isn't already loading. |
| 911 | wrapperID = jetpackLikesWidgetQueue.splice( 0, 1 )[0]; |
| 912 | if ( jQuery( '#' + wrapperID ).hasClass( 'jetpack-likes-widget-unloaded' ) ) { |
| 913 | found = true; |
| 914 | break; |
| 915 | } |
| 916 | } |
| 917 | if ( ! found ) { |
| 918 | setTimeout( JetpackLikesWidgetQueueHandler, 500 ); |
| 919 | return; |
| 920 | } |
| 921 | } else if ( jQuery( 'div.jetpack-likes-widget-unloaded' ).length > 0 ) { |
| 922 | // Grab any unloaded widgets for a batch request |
| 923 | JetpackLikesBatchHandler(); |
| 924 | |
| 925 | // Get the next unloaded widget |
| 926 | wrapperID = jQuery( 'div.jetpack-likes-widget-unloaded' ).first()[0].id; |
| 927 | if ( ! wrapperID ) { |
| 928 | // Everything is currently loaded |
| 929 | setTimeout( JetpackLikesWidgetQueueHandler, 500 ); |
| 930 | return; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | if ( 'undefined' === typeof wrapperID ) { |
| 935 | setTimeout( JetpackLikesWidgetQueueHandler, 500 ); |
| 936 | return; |
| 937 | } |
| 938 | |
| 939 | var $wrapper = jQuery( '#' + wrapperID ); |
| 940 | $wrapper.find( 'iframe' ).remove(); |
| 941 | |
| 942 | if ( $wrapper.hasClass( 'slim-likes-widget' ) ) { |
| 943 | $wrapper.find( '.post-likes-widget-placeholder' ).after( "<iframe class='post-likes-widget jetpack-likes-widget' name='" + $wrapper.data( 'name' ) + "' height='22px' width='68px' frameBorder='0' scrolling='no' src='" + $wrapper.data( 'src' ) + "'></iframe>" ); |
| 944 | } else { |
| 945 | $wrapper.find( '.post-likes-widget-placeholder' ).after( "<iframe class='post-likes-widget jetpack-likes-widget' name='" + $wrapper.data( 'name' ) + "' height='55px' width='100%' frameBorder='0' src='" + $wrapper.data( 'src' ) + "'></iframe>" ); |
| 946 | } |
| 947 | |
| 948 | $wrapper.removeClass( 'jetpack-likes-widget-unloaded' ).addClass( 'jetpack-likes-widget-loading' ); |
| 949 | |
| 950 | $wrapper.find( 'iframe' ).load( function( e ) { |
| 951 | var $iframe = jQuery( e.target ); |
| 952 | $wrapper.removeClass( 'jetpack-likes-widget-loading' ).addClass( 'jetpack-likes-widget-loaded' ); |
| 953 | |
| 954 | JetpackLikespostMessage( { event: 'loadLikeWidget', name: $iframe.attr( 'name' ), width: $iframe.width() }, window.frames[ 'likes-master' ] ); |
| 955 | |
| 956 | if ( $wrapper.hasClass( 'slim-likes-widget' ) ) { |
| 957 | $wrapper.find( 'iframe' ).Jetpack( 'resizeable' ); |
| 958 | } |
| 959 | }); |
| 960 | setTimeout( JetpackLikesWidgetQueueHandler, 250 ); |
| 961 | } |
| 962 | JetpackLikesWidgetQueueHandler(); |
| 963 | //]]> |
| 964 | </script> |
| 965 | <?php |
| 966 | } |
| 967 | |
| 968 | /** |
| 969 | * Get the 'disabled_likes' option from the DB of the current blog. |
| 970 | * |
| 971 | * @return array |
| 972 | */ |
| 973 | function get_options() { |
| 974 | $setting = array(); |
| 975 | $setting['disabled'] = get_option( 'disabled_likes' ); |
| 976 | $sharing = get_option( 'sharing-options' ); |
| 977 | |
| 978 | // Default visibility settings |
| 979 | if ( ! isset( $sharing['global']['show'] ) ) { |
| 980 | $sharing['global']['show'] = array( 'post', 'page' ); |
| 981 | |
| 982 | // Scalar check |
| 983 | } elseif ( is_scalar( $sharing['global']['show'] ) ) { |
| 984 | switch ( $sharing['global']['show'] ) { |
| 985 | case 'posts' : |
| 986 | $sharing['global']['show'] = array( 'post', 'page' ); |
| 987 | break; |
| 988 | case 'index' : |
| 989 | $sharing['global']['show'] = array( 'index' ); |
| 990 | break; |
| 991 | case 'posts-index' : |
| 992 | $sharing['global']['show'] = array( 'post', 'page', 'index' ); |
| 993 | break; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | // Ensure it's always an array (even if not previously empty or scalar) |
| 998 | $setting['show'] = !empty( $sharing['global']['show'] ) ? (array) $sharing['global']['show'] : array(); |
| 999 | |
| 1000 | return apply_filters( 'wpl_get_options', $setting ); |
| 1001 | } |
| 1002 | |
| 1003 | /** _is_ functions ************************************************************/ |
| 1004 | |
| 1005 | /** |
| 1006 | * Are likes visible in this context? |
| 1007 | * |
| 1008 | * Some of this code was taken and modified from sharing_display() to ensure |
| 1009 | * similar logic and filters apply here, too. |
| 1010 | */ |
| 1011 | function is_likes_visible() { |
| 1012 | |
| 1013 | global $wp_current_filter; // Used to check 'get_the_excerpt' filter |
| 1014 | global $post; // Used to apply 'sharing_show' filter |
| 1015 | |
| 1016 | // Never show on feeds or previews |
| 1017 | if ( is_feed() || is_preview() || is_comments_popup() ) { |
| 1018 | $enabled = false; |
| 1019 | |
| 1020 | // Not a feed or preview, so what is it? |
| 1021 | } else { |
| 1022 | |
| 1023 | if ( in_the_loop() ) { |
| 1024 | // If in the loop, check if the current post is likeable |
| 1025 | $enabled = $this->is_post_likeable(); |
| 1026 | } else { |
| 1027 | // Otherwise, check and see if likes are enabled sitewide |
| 1028 | $enabled = $this->is_enabled_sitewide(); |
| 1029 | } |
| 1030 | |
| 1031 | if ( post_password_required() ) |
| 1032 | $enabled = false; |
| 1033 | |
| 1034 | /** Other Checks ******************************************************/ |
| 1035 | |
| 1036 | // Do not show on excerpts |
| 1037 | if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) { |
| 1038 | $enabled = false; |
| 1039 | |
| 1040 | // Sharing Setting Overrides **************************************** |
| 1041 | } else { |
| 1042 | // Single post |
| 1043 | if ( is_singular( 'post' ) ) { |
| 1044 | if ( ! $this->is_single_post_enabled() ) { |
| 1045 | $enabled = false; |
| 1046 | } |
| 1047 | |
| 1048 | // Single page |
| 1049 | } elseif ( is_page() ) { |
| 1050 | if ( ! $this->is_single_page_enabled() ) { |
| 1051 | $enabled = false; |
| 1052 | } |
| 1053 | |
| 1054 | // Attachment |
| 1055 | } elseif ( is_attachment() ) { |
| 1056 | if ( ! $this->is_attachment_enabled() ) { |
| 1057 | $enabled = false; |
| 1058 | } |
| 1059 | |
| 1060 | // All other loops |
| 1061 | } elseif ( ! $this->is_index_enabled() ) { |
| 1062 | $enabled = false; |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | // Check that the post is a public, published post. |
| 1068 | if ( 'publish' != $post->post_status ) { |
| 1069 | $enabled = false; |
| 1070 | } |
| 1071 | |
| 1072 | // Run through the sharing filters |
| 1073 | $enabled = apply_filters( 'sharing_show', $enabled, $post ); |
| 1074 | |
| 1075 | return (bool) apply_filters( 'wpl_is_likes_visible', $enabled ); |
| 1076 | } |
| 1077 | |
| 1078 | /** |
| 1079 | * Returns the current state of the "WordPress.com Likes are" option. |
| 1080 | * @return boolean true if enabled sitewide, false if not |
| 1081 | */ |
| 1082 | function is_enabled_sitewide() { |
| 1083 | return (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) ); |
| 1084 | } |
| 1085 | |
| 1086 | /** |
| 1087 | * Returns if comment likes are enabled. Defaults to 'on' |
| 1088 | * @todo decide what the default should be |
| 1089 | * @return boolean true if we should show comment likes, false if not |
| 1090 | */ |
| 1091 | function is_comments_enabled() { |
| 1092 | return (bool) apply_filters( 'jetpack_comment_likes_enabled', get_option( 'jetpack_comment_likes_enabled', true ) ); |
| 1093 | } |
| 1094 | |
| 1095 | function is_admin_bar_button_visible() { |
| 1096 | global $wp_admin_bar; |
| 1097 | |
| 1098 | if ( ! is_object( $wp_admin_bar ) ) |
| 1099 | return false; |
| 1100 | |
| 1101 | if ( ( ! is_singular( 'post' ) && ! is_attachment() && ! is_page() ) ) |
| 1102 | return false; |
| 1103 | |
| 1104 | if ( ! $this->is_likes_visible() ) |
| 1105 | return false; |
| 1106 | |
| 1107 | if ( ! $this->is_post_likeable() ) |
| 1108 | return false; |
| 1109 | |
| 1110 | return (bool) apply_filters( 'jetpack_admin_bar_likes_enabled', true ); |
| 1111 | } |
| 1112 | |
| 1113 | /** |
| 1114 | * Are likes enabled for this post? |
| 1115 | * |
| 1116 | * @param int $post_id |
| 1117 | * @retun bool |
| 1118 | */ |
| 1119 | function is_post_likeable( $post_id = 0 ) { |
| 1120 | $post = get_post( $post_id ); |
| 1121 | if ( !$post || is_wp_error( $post ) ) { |
| 1122 | return false; |
| 1123 | } |
| 1124 | |
| 1125 | $sitewide_likes_enabled = (bool) Jetpack_Likes::is_enabled_sitewide(); |
| 1126 | $post_likes_switched = (bool) get_post_meta( $post->ID, 'switch_like_status', true ); |
| 1127 | |
| 1128 | $post_likes_enabled = $sitewide_likes_enabled; |
| 1129 | if ( $post_likes_switched ) { |
| 1130 | $post_likes_enabled = ! $post_likes_enabled; |
| 1131 | } |
| 1132 | |
| 1133 | return $post_likes_enabled; |
| 1134 | } |
| 1135 | |
| 1136 | /** |
| 1137 | * Are Post Likes enabled on archive/front/search pages? |
| 1138 | * |
| 1139 | * @return bool |
| 1140 | */ |
| 1141 | function is_index_enabled() { |
| 1142 | $options = $this->get_options(); |
| 1143 | return (bool) apply_filters( 'wpl_is_index_disabled', (bool) in_array( 'index', $options['show'] ) ); |
| 1144 | } |
| 1145 | |
| 1146 | /** |
| 1147 | * Are Post Likes enabled on single posts? |
| 1148 | * |
| 1149 | * @return bool |
| 1150 | */ |
| 1151 | function is_single_post_enabled() { |
| 1152 | $options = $this->get_options(); |
| 1153 | return (bool) apply_filters( 'wpl_is_single_post_disabled', (bool) in_array( 'post', $options['show'] ) ); |
| 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * Are Post Likes enabled on single pages? |
| 1158 | * |
| 1159 | * @return bool |
| 1160 | */ |
| 1161 | function is_single_page_enabled() { |
| 1162 | $options = $this->get_options(); |
| 1163 | return (bool) apply_filters( 'wpl_is_single_page_disabled', (bool) in_array( 'page', $options['show'] ) ); |
| 1164 | } |
| 1165 | |
| 1166 | /** |
| 1167 | * Are Media Likes enabled on single pages? |
| 1168 | * |
| 1169 | * @return bool |
| 1170 | */ |
| 1171 | function is_attachment_enabled() { |
| 1172 | $options = $this->get_options(); |
| 1173 | return (bool) apply_filters( 'wpl_is_attachment_disabled', (bool) in_array( 'attachment', $options['show'] ) ); |
| 1174 | } |
| 1175 | |
| 1176 | } |
| 1177 | |
| 1178 | Jetpack_Likes::init(); |
| 1179 |