Geen omschrijving

jetpack-likes-settings.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. <?php
  2. use Automattic\Jetpack\Sync\Settings;
  3. class Jetpack_Likes_Settings {
  4. function __construct() {
  5. $this->in_jetpack = ! ( defined( 'IS_WPCOM' ) && IS_WPCOM );
  6. }
  7. /**
  8. * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares"
  9. */
  10. public function add_likes_to_sharing_meta_box_title() {
  11. return __( 'Likes and Shares', 'jetpack' );
  12. }
  13. /**
  14. * Adds a metabox to the post screen if the sharing one doesn't currently exist.
  15. */
  16. public function add_meta_box() {
  17. if (
  18. /**
  19. * Allow disabling of the Likes metabox on the post editor screen.
  20. *
  21. * @module likes
  22. *
  23. * @since 2.2.0
  24. *
  25. * @param bool false Should the Likes metabox be disabled? Default to false.
  26. */
  27. apply_filters( 'post_flair_disable', false )
  28. ) {
  29. return;
  30. }
  31. $post_types = get_post_types( array( 'public' => true ) );
  32. /**
  33. * Filters the Likes metabox title.
  34. *
  35. * @module likes
  36. *
  37. * @since 2.2.0
  38. *
  39. * @param string Likes metabox title. Default to "Likes".
  40. */
  41. $title = apply_filters( 'likes_meta_box_title', __( 'Likes', 'jetpack' ) );
  42. foreach( $post_types as $post_type ) {
  43. add_meta_box( 'likes_meta', $title, array( $this, 'meta_box_content' ), $post_type, 'side', 'default', array( '__back_compat_meta_box' => true ) );
  44. }
  45. }
  46. /**
  47. * Shows the likes option in the post screen metabox.
  48. */
  49. public function meta_box_content( $post ) {
  50. $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID();
  51. $checked = true;
  52. $disabled = ! $this->is_enabled_sitewide();
  53. $switched_status = get_post_meta( $post_id, 'switch_like_status', true );
  54. if ( $disabled && empty( $switched_status ) || ! $disabled && $switched_status === '0' ) {
  55. $checked = false;
  56. }
  57. /**
  58. * Fires before the Likes meta box content in the post editor.
  59. *
  60. * @module likes
  61. *
  62. * @since 2.2.0
  63. *
  64. * @param WP_Post|array|null $post Post data.
  65. */
  66. do_action( 'start_likes_meta_box_content', $post );
  67. ?>
  68. <p>
  69. <label for="wpl_enable_post_likes">
  70. <input type="checkbox" name="wpl_enable_post_likes" id="wpl_enable_post_likes" value="1" <?php checked( $checked ); ?>>
  71. <?php esc_html_e( 'Show likes.', 'jetpack' ); ?>
  72. </label>
  73. <input type="hidden" name="wpl_like_status_hidden" value="1" />
  74. </p> <?php
  75. /**
  76. * Fires after the Likes meta box content in the post editor.
  77. *
  78. * @module likes
  79. *
  80. * @since 2.2.0
  81. *
  82. * @param WP_Post|array|null $post Post data.
  83. */
  84. do_action( 'end_likes_meta_box_content', $post );
  85. }
  86. /**
  87. * Returns the current state of the "WordPress.com Likes are" option.
  88. * @return boolean true if enabled sitewide, false if not
  89. */
  90. public function is_enabled_sitewide() {
  91. /**
  92. * Filters whether Likes are enabled by default on all posts.
  93. * true if enabled sitewide, false if not.
  94. *
  95. * @module likes
  96. *
  97. * @since 2.2.0
  98. *
  99. * @param bool $option Are Likes enabled sitewide.
  100. */
  101. return (bool) apply_filters( 'wpl_is_enabled_sitewide', ! Jetpack_Options::get_option_and_ensure_autoload( 'disabled_likes', 0 ) );
  102. }
  103. public function meta_box_save( $post_id ) {
  104. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
  105. return $post_id;
  106. }
  107. if ( empty( $_POST['wpl_like_status_hidden'] ) ) {
  108. return $post_id;
  109. }
  110. // Record sharing disable. Only needs to be done for WPCOM
  111. if ( ! $this->in_jetpack ) {
  112. if ( isset( $_POST['post_type'] ) && in_array( $_POST['post_type'], get_post_types( array( 'public' => true ) ) ) ) {
  113. if ( ! isset( $_POST['wpl_enable_post_sharing'] ) ) {
  114. update_post_meta( $post_id, 'sharing_disabled', 1 );
  115. } else {
  116. delete_post_meta( $post_id, 'sharing_disabled' );
  117. }
  118. }
  119. }
  120. if ( 'post' == $_POST['post_type'] ) {
  121. if ( !current_user_can( 'edit_post', $post_id ) ) {
  122. return $post_id;
  123. }
  124. }
  125. // Record a change in like status for this post - only if it contradicts the
  126. // site like setting. If it doesn't contradict, then we delete the new individual status.
  127. if ( ! $this->is_enabled_sitewide() && ! empty( $_POST['wpl_enable_post_likes'] ) ) {
  128. // Likes turned on for individual posts. User wants to add the button to a single post
  129. update_post_meta( $post_id, 'switch_like_status', 1 );
  130. } else if ( $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) {
  131. // Likes turned on for all posts. User wants to remove the button from a single post
  132. update_post_meta( $post_id, 'switch_like_status', 0 );
  133. } else if (
  134. ( ! $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) ||
  135. ( $this->is_enabled_sitewide() && ! empty( $_POST['wpl_enable_post_likes'] ) )
  136. ) {
  137. // User wants to update the likes button status for an individual post, but the new status
  138. // is the same as if they're asking for the default behavior according to the current Likes setting.
  139. // So we delete the meta.
  140. delete_post_meta( $post_id, 'switch_like_status' );
  141. }
  142. return $post_id;
  143. }
  144. /**
  145. * WordPress.com: Metabox option for sharing (sharedaddy will handle this on the JP blog)
  146. */
  147. public function sharing_meta_box_content( $post ) {
  148. $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID();
  149. $disabled = get_post_meta( $post_id, 'sharing_disabled', true ); ?>
  150. <p>
  151. <label for="wpl_enable_post_sharing">
  152. <input type="checkbox" name="wpl_enable_post_sharing" id="wpl_enable_post_sharing" value="1" <?php checked( ! $disabled ); ?>>
  153. <?php _e( 'Show sharing buttons.', 'jetpack' ); ?>
  154. </label>
  155. <input type="hidden" name="wpl_sharing_status_hidden" value="1" />
  156. </p> <?php
  157. }
  158. /**
  159. * Adds the 'sharing' menu to the settings menu.
  160. * Only ran if sharedaddy and publicize are not already active.
  161. */
  162. function sharing_menu() {
  163. add_submenu_page( 'options-general.php', esc_html__( 'Sharing Settings', 'jetpack' ), esc_html__( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( $this, 'sharing_page' ) );
  164. }
  165. /**
  166. * Provides a sharing page with the sharing_global_options hook
  167. * so we can display the setting.
  168. * Only ran if sharedaddy and publicize are not already active.
  169. */
  170. function sharing_page() {
  171. $this->updated_message(); ?>
  172. <div class="wrap">
  173. <div class="icon32" id="icon-options-general"><br /></div>
  174. <h1><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h1>
  175. <?php
  176. /** This action is documented in modules/sharedaddy/sharing.php */
  177. do_action( 'pre_admin_screen_sharing' );
  178. ?>
  179. <?php $this->sharing_block(); ?>
  180. </div> <?php
  181. }
  182. /**
  183. * Returns the settings have been saved message.
  184. */
  185. function updated_message() {
  186. if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' ){
  187. echo '<div class="updated"><p>' . esc_html__( 'Settings have been saved', 'jetpack' ) . '</p></div>';
  188. }
  189. }
  190. /**
  191. * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts
  192. */
  193. function sharing_block() { ?>
  194. <h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2>
  195. <form method="post" action="">
  196. <table class="form-table">
  197. <tbody>
  198. <?php
  199. /** This action is documented in modules/sharedaddy/sharing.php */
  200. do_action( 'sharing_global_options' );
  201. ?>
  202. </tbody>
  203. </table>
  204. <p class="submit">
  205. <input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" />
  206. </p>
  207. <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" />
  208. </form> <?php
  209. }
  210. /**
  211. * Are likes enabled for this post?
  212. *
  213. * @param int $post_id
  214. * @return bool
  215. */
  216. function is_post_likeable( $post_id = 0 ) {
  217. $post = get_post( $post_id );
  218. if ( ! $post || is_wp_error( $post ) ) {
  219. return false;
  220. }
  221. $sitewide_likes_enabled = (bool) $this->is_enabled_sitewide();
  222. $post_likes_switched = get_post_meta( $post->ID, 'switch_like_status', true );
  223. // on WPCOM, we need to look at post edit date so we don't break old posts
  224. // if post edit date predates this code, stick with the former (buggy) behavior
  225. // see: p7DVsv-64H-p2
  226. $last_modified_time = strtotime( $post->post_modified_gmt );
  227. $behavior_was_changed_at = strtotime( "2019-02-22 00:40:42" );
  228. if ( $this->in_jetpack || $last_modified_time > $behavior_was_changed_at ) {
  229. // the new and improved behavior on Jetpack and recent WPCOM posts:
  230. // $post_likes_switched is empty to follow site setting,
  231. // 0 if we want likes disabled, 1 if we want likes enabled
  232. return $post_likes_switched || ( $sitewide_likes_enabled && $post_likes_switched !== '0' );
  233. }
  234. // implicit else (old behavior): $post_likes_switched simply inverts the global setting
  235. return ( (bool) $post_likes_switched ) xor $sitewide_likes_enabled;
  236. }
  237. /**
  238. * Are likes visible in this context?
  239. *
  240. * Some of this code was taken and modified from sharing_display() to ensure
  241. * similar logic and filters apply here, too.
  242. */
  243. function is_likes_visible() {
  244. if ( Settings::is_syncing() ) {
  245. return false;
  246. }
  247. global $wp_current_filter; // Used to apply 'sharing_show' filter
  248. $post = get_post();
  249. // Never show on feeds or previews
  250. if ( is_feed() || is_preview() ) {
  251. $enabled = false;
  252. // Not a feed or preview, so what is it?
  253. } else {
  254. if ( in_the_loop() ) {
  255. // If in the loop, check if the current post is likeable
  256. $enabled = $this->is_post_likeable();
  257. } else {
  258. // Otherwise, check and see if likes are enabled sitewide
  259. $enabled = $this->is_enabled_sitewide();
  260. }
  261. if ( post_password_required() )
  262. $enabled = false;
  263. if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
  264. $enabled = false;
  265. }
  266. // Sharing Setting Overrides ****************************************
  267. // Single post including custom post types
  268. if ( is_single() ) {
  269. if ( ! $this->is_single_post_enabled( ( $post instanceof WP_Post ) ? $post->post_type : 'post' ) ) {
  270. $enabled = false;
  271. }
  272. // Single page
  273. } elseif ( is_page() && ! is_front_page() ) {
  274. if ( ! $this->is_single_page_enabled() ) {
  275. $enabled = false;
  276. }
  277. // Attachment
  278. } elseif ( is_attachment() ) {
  279. if ( ! $this->is_attachment_enabled() ) {
  280. $enabled = false;
  281. }
  282. // All other loops
  283. } elseif ( ! $this->is_index_enabled() ) {
  284. $enabled = false;
  285. }
  286. }
  287. if ( $post instanceof WP_Post ) {
  288. // Check that the post is a public, published post.
  289. if ( 'attachment' == $post->post_type ) {
  290. $post_status = get_post_status( $post->post_parent );
  291. } else {
  292. $post_status = $post->post_status;
  293. }
  294. if ( 'publish' != $post_status ) {
  295. $enabled = false;
  296. }
  297. }
  298. // Run through the sharing filters
  299. /** This filter is documented in modules/sharedaddy/sharing-service.php */
  300. $enabled = apply_filters( 'sharing_show', $enabled, $post );
  301. /**
  302. * Filters whether the Likes should be visible or not.
  303. * Allows overwriting the options set in Settings > Sharing.
  304. *
  305. * @module likes
  306. *
  307. * @since 2.2.0
  308. *
  309. * @param bool $enabled Should the Likes be visible?
  310. */
  311. return (bool) apply_filters( 'wpl_is_likes_visible', $enabled );
  312. }
  313. /**
  314. * Are Post Likes enabled on single posts?
  315. *
  316. * @param String $post_type custom post type identifier
  317. * @return bool
  318. */
  319. function is_single_post_enabled( $post_type = 'post' ) {
  320. $options = $this->get_options();
  321. return (bool) apply_filters(
  322. /**
  323. * Filters whether Likes should be enabled on single posts.
  324. *
  325. * The dynamic part of the filter, {$post_type}, allows you to specific the post type where Likes should be enabled.
  326. *
  327. * @module likes
  328. *
  329. * @since 2.2.0
  330. *
  331. * @param bool $enabled Are Post Likes enabled on single posts?
  332. */
  333. "wpl_is_single_{$post_type}_disabled",
  334. (bool) in_array( $post_type, $options['show'] )
  335. );
  336. }
  337. /**
  338. * Get the 'disabled_likes' option from the DB of the current blog.
  339. *
  340. * @return array
  341. */
  342. function get_options() {
  343. $setting = array();
  344. $setting['disabled'] = get_option( 'disabled_likes' );
  345. $sharing = get_option( 'sharing-options' );
  346. // Default visibility settings
  347. if ( ! isset( $sharing['global']['show'] ) ) {
  348. $sharing['global']['show'] = array( 'post', 'page' );
  349. // Scalar check
  350. } elseif ( is_scalar( $sharing['global']['show'] ) ) {
  351. switch ( $sharing['global']['show'] ) {
  352. case 'posts' :
  353. $sharing['global']['show'] = array( 'post', 'page' );
  354. break;
  355. case 'index' :
  356. $sharing['global']['show'] = array( 'index' );
  357. break;
  358. case 'posts-index' :
  359. $sharing['global']['show'] = array( 'post', 'page', 'index' );
  360. break;
  361. }
  362. }
  363. // Ensure it's always an array (even if not previously empty or scalar)
  364. $setting['show'] = ! empty( $sharing['global']['show'] ) ? (array) $sharing['global']['show'] : array();
  365. /**
  366. * Filters where the Likes are displayed.
  367. *
  368. * @module likes
  369. *
  370. * @since 2.2.0
  371. *
  372. * @param array $setting Array of Likes display settings.
  373. */
  374. return apply_filters( 'wpl_get_options', $setting );
  375. }
  376. /**
  377. * Are Post Likes enabled on archive/front/search pages?
  378. *
  379. * @return bool
  380. */
  381. function is_index_enabled() {
  382. $options = $this->get_options();
  383. /**
  384. * Filters whether Likes should be enabled on archive/front/search pages.
  385. *
  386. * @module likes
  387. *
  388. * @since 2.2.0
  389. *
  390. * @param bool $enabled Are Post Likes enabled on archive/front/search pages?
  391. */
  392. return (bool) apply_filters( 'wpl_is_index_disabled', (bool) in_array( 'index', $options['show'] ) );
  393. }
  394. /**
  395. * Are Post Likes enabled on single pages?
  396. *
  397. * @return bool
  398. */
  399. function is_single_page_enabled() {
  400. $options = $this->get_options();
  401. /**
  402. * Filters whether Likes should be enabled on single pages.
  403. *
  404. * @module likes
  405. *
  406. * @since 2.2.0
  407. *
  408. * @param bool $enabled Are Post Likes enabled on single pages?
  409. */
  410. return (bool) apply_filters( 'wpl_is_single_page_disabled', (bool) in_array( 'page', $options['show'] ) );
  411. }
  412. /**
  413. * Are Media Likes enabled on single pages?
  414. *
  415. * @return bool
  416. */
  417. function is_attachment_enabled() {
  418. $options = $this->get_options();
  419. /**
  420. * Filters whether Likes should be enabled on attachment pages.
  421. *
  422. * @module likes
  423. *
  424. * @since 2.2.0
  425. *
  426. * @param bool $enabled Are Post Likes enabled on attachment pages?
  427. */
  428. return (bool) apply_filters( 'wpl_is_attachment_disabled', (bool) in_array( 'attachment', $options['show'] ) );
  429. }
  430. /**
  431. * The actual options block to be inserted into the sharing page.
  432. */
  433. function admin_settings_init() {
  434. ?>
  435. <tr>
  436. <th scope="row">
  437. <label><?php esc_html_e( 'WordPress.com Likes are', 'jetpack' ); ?></label>
  438. </th>
  439. <td>
  440. <div>
  441. <label>
  442. <input type="radio" class="code" name="wpl_default" value="on" <?php checked( $this->is_enabled_sitewide(), true ); ?> />
  443. <?php esc_html_e( 'On for all posts', 'jetpack' ); ?>
  444. </label>
  445. </div>
  446. <div>
  447. <label>
  448. <input type="radio" class="code" name="wpl_default" value="off" <?php checked( $this->is_enabled_sitewide(), false ); ?> />
  449. <?php esc_html_e( 'Turned on per post', 'jetpack' ); ?>
  450. </label>
  451. <div>
  452. </td>
  453. </tr>
  454. <?php if ( ! $this->in_jetpack ) : ?>
  455. <tr>
  456. <th scope="row">
  457. <label><?php esc_html_e( 'WordPress.com Reblog Button', 'jetpack' ); ?></label>
  458. </th>
  459. <td>
  460. <div>
  461. <label>
  462. <input type="radio" class="code" name="jetpack_reblogs_enabled" value="on" <?php checked( $this->reblogs_enabled_sitewide(), true ); ?> />
  463. <?php esc_html_e( 'Show the Reblog button on posts', 'jetpack' ); ?>
  464. </label>
  465. </div>
  466. <div>
  467. <label>
  468. <input type="radio" class="code" name="jetpack_reblogs_enabled" value="off" <?php checked( $this->reblogs_enabled_sitewide(), false ); ?> />
  469. <?php esc_html_e( 'Don\'t show the Reblog button on posts', 'jetpack' ); ?>
  470. </label>
  471. </div>
  472. </td>
  473. </tr>
  474. <!-- WPCOM only: Comment Likes -->
  475. <?php if ( ! $this->in_jetpack ) : ?>
  476. <tr>
  477. <th scope="row">
  478. <label><?php esc_html_e( 'Comment Likes are', 'jetpack' ); ?></label>
  479. </th>
  480. <td>
  481. <div>
  482. <label>
  483. <input type="checkbox" class="code" name="jetpack_comment_likes_enabled" value="1" <?php checked( $this->is_comments_enabled(), true ); ?> />
  484. <?php esc_html_e( 'On for all comments', 'jetpack' ); ?>
  485. </label>
  486. </div>
  487. </td>
  488. </tr>
  489. <?php endif; ?>
  490. <?php endif; ?>
  491. </tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?>
  492. <?php
  493. }
  494. /**
  495. * Returns the current state of the "WordPress.com Reblogs are" option.
  496. * @return boolean true if enabled sitewide, false if not
  497. */
  498. function reblogs_enabled_sitewide() {
  499. /**
  500. * Filters whether Reblogs are enabled by default on all posts.
  501. * true if enabled sitewide, false if not.
  502. *
  503. * @module likes
  504. *
  505. * @since 3.0.0
  506. *
  507. * @param bool $option Are Reblogs enabled sitewide.
  508. */
  509. return (bool) apply_filters( 'wpl_reblogging_enabled_sitewide', ! get_option( 'disabled_reblogs' ) );
  510. }
  511. /**
  512. * Used for WPCOM ONLY. Comment likes are in their own module in Jetpack.
  513. * Returns if comment likes are enabled. Defaults to 'off'
  514. * @return boolean true if we should show comment likes, false if not
  515. */
  516. function is_comments_enabled() {
  517. /**
  518. * Filters whether Comment Likes are enabled.
  519. * true if enabled, false if not.
  520. *
  521. * @module comment-likes
  522. *
  523. * @since 2.2.0
  524. *
  525. * @param bool $option Are Comment Likes enabled sitewide.
  526. */
  527. return (bool) apply_filters( 'jetpack_comment_likes_enabled', get_option( 'jetpack_comment_likes_enabled', false ) );
  528. }
  529. /**
  530. * Saves the setting in the database, bumps a stat on WordPress.com
  531. */
  532. function admin_settings_callback() {
  533. // We're looking for these, and doing a dance to set some stats and save
  534. // them together in array option.
  535. $new_state = ! empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on';
  536. $db_state = $this->is_enabled_sitewide();
  537. $reblogs_new_state = ! empty( $_POST['jetpack_reblogs_enabled'] ) ? $_POST['jetpack_reblogs_enabled'] : 'on';
  538. $reblogs_db_state = $this->reblogs_enabled_sitewide();
  539. /** Default State *********************************************************/
  540. // Checked (enabled)
  541. switch( $new_state ) {
  542. case 'off' :
  543. if ( true == $db_state && ! $this->in_jetpack ) {
  544. $g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=disabled_likes' );
  545. }
  546. update_option( 'disabled_likes', 1 );
  547. break;
  548. case 'on' :
  549. default:
  550. if ( false == $db_state && ! $this->in_jetpack ) {
  551. $g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=reenabled_likes' );
  552. }
  553. delete_option( 'disabled_likes' );
  554. break;
  555. }
  556. switch( $reblogs_new_state ) {
  557. case 'off' :
  558. if ( true == $reblogs_db_state && ! $this->in_jetpack ) {
  559. $g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_reblogs=disabled_reblogs' );
  560. }
  561. update_option( 'disabled_reblogs', 1 );
  562. break;
  563. case 'on' :
  564. default:
  565. if ( false == $reblogs_db_state && ! $this->in_jetpack ) {
  566. $g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_reblogs=reenabled_reblogs' );
  567. }
  568. delete_option( 'disabled_reblogs' );
  569. break;
  570. }
  571. // WPCOM only: Comment Likes
  572. if ( ! $this->in_jetpack ) {
  573. $new_comments_state = ! empty( $_POST['jetpack_comment_likes_enabled'] ) ? $_POST['jetpack_comment_likes_enabled'] : false;
  574. switch( (bool) $new_comments_state ) {
  575. case true:
  576. update_option( 'jetpack_comment_likes_enabled', 1 );
  577. break;
  578. case false:
  579. default:
  580. update_option( 'jetpack_comment_likes_enabled', 0 );
  581. break;
  582. }
  583. }
  584. }
  585. /**
  586. * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled.
  587. */
  588. function process_update_requests_if_sharedaddy_not_loaded() {
  589. if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) {
  590. if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) {
  591. /** This action is documented in modules/sharedaddy/sharing.php */
  592. do_action( 'sharing_admin_update' );
  593. wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
  594. die();
  595. }
  596. }
  597. }
  598. /**
  599. * 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.
  600. */
  601. function admin_settings_showbuttonon_init() {
  602. /** This action is documented in modules/sharedaddy/sharing.php */
  603. echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' );
  604. ?>
  605. <th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th>
  606. <td>
  607. <?php
  608. $br = false;
  609. $shows = array_values( get_post_types( array( 'public' => true ) ) );
  610. array_unshift( $shows, 'index' );
  611. $global = $this->get_options();
  612. foreach ( $shows as $show ) :
  613. if ( 'index' == $show ) {
  614. $label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
  615. } else {
  616. $post_type_object = get_post_type_object( $show );
  617. $label = $post_type_object->labels->name;
  618. }
  619. ?>
  620. <?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>
  621. <?php $br = true; endforeach; ?>
  622. </td>
  623. <?php
  624. /** This action is documented in modules/sharedaddy/sharing.php */
  625. echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' );
  626. ?>
  627. <?php
  628. }
  629. /**
  630. * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option.
  631. */
  632. function admin_settings_showbuttonon_callback() {
  633. $options = get_option( 'sharing-options' );
  634. if ( !is_array( $options ) )
  635. $options = array();
  636. $shows = array_values( get_post_types( array( 'public' => true ) ) );
  637. $shows[] = 'index';
  638. $data = $_POST;
  639. if ( isset( $data['show'] ) ) {
  640. if ( is_scalar( $data['show'] ) ) {
  641. switch ( $data['show'] ) {
  642. case 'posts' :
  643. $data['show'] = array( 'post', 'page' );
  644. break;
  645. case 'index' :
  646. $data['show'] = array( 'index' );
  647. break;
  648. case 'posts-index' :
  649. $data['show'] = array( 'post', 'page', 'index' );
  650. break;
  651. }
  652. }
  653. if ( $data['show'] = array_intersect( $data['show'], $shows ) ) {
  654. $options['global']['show'] = $data['show'];
  655. }
  656. } else {
  657. $options['global']['show'] = array();
  658. }
  659. update_option( 'sharing-options', $options );
  660. }
  661. }