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