Jetpack_Likes

The WordPress Core Jetpack Likes class.

Defined (1)

The class is defined in the following location(s).

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