/modules/likes.php

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