| @@ -10,8 +10,12 @@ | ||
| 10 | 10 | use Automattic\Jetpack\Assets; |
| 11 | 11 | use Automattic\Jetpack\Redirect; |
| 12 | 12 | use Automattic\Jetpack\Status; |
| 13 | 13 | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 17 | + | |
| 14 | 18 | if ( ! defined( 'WP_SHARING_PLUGIN_URL' ) ) { |
| 15 | 19 | define( 'WP_SHARING_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); |
| 16 | 20 | define( 'WP_SHARING_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
| 17 | 21 | } |
| @@ -27,9 +31,13 @@ | ||
| 27 | 31 | */ |
| 28 | 32 | public function __construct() { |
| 29 | 33 | require_once WP_SHARING_PLUGIN_DIR . 'sharing-service.php'; |
| 30 | 34 | |
| 31 | - add_action( 'admin_init', array( $this, 'admin_init' ) ); | |
| 35 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonces are handled in process_requests. | |
| 36 | + if ( isset( $_GET['page'] ) && ( $_GET['page'] === 'sharing.php' || $_GET['page'] === 'sharing' ) ) { | |
| 37 | + add_action( 'admin_init', array( $this, 'admin_init' ) ); | |
| 38 | + } | |
| 39 | + | |
| 32 | 40 | add_action( 'admin_menu', array( $this, 'subscription_menu' ) ); |
| 33 | 41 | |
| 34 | 42 | // Insert our CSS and JS |
| 35 | 43 | add_action( 'load-settings_page_sharing', array( $this, 'sharing_head' ) ); |
| @@ -89,11 +97,9 @@ | ||
| 89 | 97 | * |
| 90 | 98 | * @return void |
| 91 | 99 | */ |
| 92 | 100 | public function admin_init() { |
| 93 | - if ( isset( $_GET['page'] ) && ( $_GET['page'] === 'sharing.php' || $_GET['page'] === 'sharing' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonces are handled in process_requests. | |
| 94 | - $this->process_requests(); | |
| 95 | - } | |
| 101 | + $this->process_requests(); | |
| 96 | 102 | } |
| 97 | 103 | |
| 98 | 104 | /** |
| 99 | 105 | * Save changes to sharing settings. |
| @@ -116,28 +122,24 @@ | ||
| 116 | 122 | */ |
| 117 | 123 | do_action( 'sharing_admin_update' ); |
| 118 | 124 | |
| 119 | 125 | wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) ); |
| 120 | - die(); | |
| 126 | + die( 0 ); | |
| 121 | 127 | } |
| 122 | 128 | } |
| 123 | 129 | |
| 124 | 130 | /** |
| 125 | - * Register Sharing settings menu page in offline mode or when wp-admin interface is enabled. | |
| 131 | + * Register Sharing settings menu page in Settings > Sharing. | |
| 126 | 132 | */ |
| 127 | 133 | public function subscription_menu() { |
| 128 | - $wpcom_is_wp_admin_interface = get_option( 'wpcom_admin_interface' ) === 'wp-admin'; | |
| 129 | - | |
| 130 | - if ( ( new Status() )->is_offline_mode() || $wpcom_is_wp_admin_interface ) { | |
| 131 | - add_submenu_page( | |
| 132 | - 'options-general.php', | |
| 133 | - __( 'Sharing Settings', 'jetpack' ), | |
| 134 | - __( 'Sharing', 'jetpack' ), | |
| 135 | - 'manage_options', | |
| 136 | - 'sharing', | |
| 137 | - array( $this, 'wrapper_admin_page' ) | |
| 138 | - ); | |
| 139 | - } | |
| 134 | + add_submenu_page( | |
| 135 | + 'options-general.php', | |
| 136 | + __( 'Sharing Settings', 'jetpack' ), | |
| 137 | + __( 'Sharing', 'jetpack' ), | |
| 138 | + 'manage_options', | |
| 139 | + 'sharing', | |
| 140 | + array( $this, 'wrapper_admin_page' ) | |
| 141 | + ); | |
| 140 | 142 | } |
| 141 | 143 | |
| 142 | 144 | /** |
| 143 | 145 | * Save changes to sharing services via AJAX. |
| @@ -156,9 +158,9 @@ | ||
| 156 | 158 | $sharer->set_blog_services( |
| 157 | 159 | explode( ',', sanitize_text_field( wp_unslash( $_POST['visible'] ) ) ), |
| 158 | 160 | explode( ',', sanitize_text_field( wp_unslash( $_POST['hidden'] ) ) ) |
| 159 | 161 | ); |
| 160 | - die(); | |
| 162 | + die( 0 ); | |
| 161 | 163 | } |
| 162 | 164 | } |
| 163 | 165 | |
| 164 | 166 | /** |
| @@ -186,9 +188,9 @@ | ||
| 186 | 188 | echo '<!--->'; |
| 187 | 189 | $service->button_style = 'icon-text'; |
| 188 | 190 | $this->output_preview( $service ); |
| 189 | 191 | |
| 190 | - die(); | |
| 192 | + die( 0 ); | |
| 191 | 193 | } |
| 192 | 194 | } |
| 193 | 195 | |
| 194 | 196 | // Fail |
| @@ -240,9 +242,9 @@ | ||
| 240 | 242 | $this->output_service( $service->get_id(), $service, true ); |
| 241 | 243 | echo '<!--->'; |
| 242 | 244 | $service->button_style = 'icon-text'; |
| 243 | 245 | $this->output_preview( $service ); |
| 244 | - die(); | |
| 246 | + die( 0 ); | |
| 245 | 247 | } |
| 246 | 248 | } |
| 247 | 249 | |
| 248 | 250 | /** |
| @@ -361,12 +363,10 @@ | ||
| 361 | 363 | do_action( 'pre_admin_screen_sharing' ); |
| 362 | 364 | ?> |
| 363 | 365 | |
| 364 | 366 | <?php |
| 365 | - $block_availability = Jetpack_Gutenberg::get_cached_availability(); | |
| 366 | - $is_block_available = (bool) isset( $block_availability['sharing-buttons'] ) && $block_availability['sharing-buttons']['available']; | |
| 367 | - $is_block_theme = wp_is_block_theme(); | |
| 368 | - $show_block_message = $is_block_available && $is_block_theme; | |
| 367 | + $is_simple_site = defined( 'IS_WPCOM' ) && IS_WPCOM; | |
| 368 | + $show_block_message = $this->should_use_site_editor() && ! $is_simple_site; | |
| 369 | 369 | |
| 370 | 370 | // We either show old services config or the sharing block message. |
| 371 | 371 | if ( current_user_can( 'manage_options' ) ) : |
| 372 | 372 | $show_block_message ? $this->sharing_block_display() : $this->services_config_display(); |
| @@ -374,9 +374,9 @@ | ||
| 374 | 374 | ?> |
| 375 | 375 | </div> |
| 376 | 376 | |
| 377 | 377 | <script type="text/javascript"> |
| 378 | - var sharing_loading_icon = '<?php echo esc_js( admin_url( '/images/loading.gif' ) ); ?>'; | |
| 378 | + var sharing_loading_icon = <?php echo wp_json_encode( admin_url( '/images/loading.gif' ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?>; | |
| 379 | 379 | <?php |
| 380 | 380 | // phpcs:disable WordPress.Security.NonceVerification.Recommended -- we handle the nonce on the PHP side. |
| 381 | 381 | if ( |
| 382 | 382 | isset( $_GET['create_new_service'] ) && isset( $_GET['name'] ) && isset( $_GET['url'] ) && isset( $_GET['icon'] ) |
| @@ -384,11 +384,11 @@ | ||
| 384 | 384 | ) : |
| 385 | 385 | ?> |
| 386 | 386 | jQuery(document).ready(function() { |
| 387 | 387 | // Prefill new service box and then open it |
| 388 | - jQuery( '#new_sharing_name' ).val( '<?php echo esc_js( sanitize_text_field( wp_unslash( $_GET['name'] ) ) ); ?>' ); | |
| 389 | - jQuery( '#new_sharing_url' ).val( '<?php echo esc_js( sanitize_text_field( wp_unslash( $_GET['url'] ) ) ); ?>' ); | |
| 390 | - jQuery( '#new_sharing_icon' ).val( '<?php echo esc_js( sanitize_text_field( wp_unslash( $_GET['icon'] ) ) ); ?>' ); | |
| 388 | + jQuery( '#new_sharing_name' ).val( <?php echo wp_json_encode( sanitize_text_field( wp_unslash( $_GET['name'] ) ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?> ); | |
| 389 | + jQuery( '#new_sharing_url' ).val( <?php echo wp_json_encode( sanitize_text_field( wp_unslash( $_GET['url'] ) ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?> ); | |
| 390 | + jQuery( '#new_sharing_icon' ).val( <?php echo wp_json_encode( sanitize_text_field( wp_unslash( $_GET['icon'] ) ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?> ); | |
| 391 | 391 | jQuery( '#add-a-new-service' ).click(); |
| 392 | 392 | }); |
| 393 | 393 | <?php endif; ?> |
| 394 | 394 | </script> |
| @@ -396,8 +396,20 @@ | ||
| 396 | 396 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | 399 | /** |
| 400 | + * Check if we should encourage to use the site editor instead of the legacy sharing settings. | |
| 401 | + * | |
| 402 | + * @return boolean | |
| 403 | + */ | |
| 404 | + public function should_use_site_editor() { | |
| 405 | + $block_availability = Jetpack_Gutenberg::get_cached_availability(); | |
| 406 | + $is_block_available = isset( $block_availability['sharing-buttons'] ) && $block_availability['sharing-buttons']['available']; | |
| 407 | + $is_block_theme = wp_is_block_theme(); | |
| 408 | + return $is_block_available && $is_block_theme; | |
| 409 | + } | |
| 410 | + | |
| 411 | + /** | |
| 400 | 412 | * Display services admin UI for settings. |
| 401 | 413 | * |
| 402 | 414 | * @return void |
| 403 | 415 | */ |
| @@ -415,8 +427,22 @@ | ||
| 415 | 427 | <div class="share_manage_options"> |
| 416 | 428 | <h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2> |
| 417 | 429 | <p><?php esc_html_e( 'Add sharing buttons to your blog and allow your visitors to share posts with their friends.', 'jetpack' ); ?></p> |
| 418 | 430 | |
| 431 | + <?php | |
| 432 | + $is_simple_site = defined( 'IS_WPCOM' ) && IS_WPCOM; | |
| 433 | + if ( $this->should_use_site_editor() && $is_simple_site ) : | |
| 434 | + $this->site_editor_prompt_display(); | |
| 435 | + ?> | |
| 436 | + <div class="notice notice-info inline"> | |
| 437 | + <p> | |
| 438 | + <?php esc_html_e( 'You are using a block-based theme. We recommend that you disable the legacy sharing features below and add a sharing button block to your theme’s template instead.', 'jetpack' ); ?> | |
| 439 | + </p> | |
| 440 | + </div> | |
| 441 | + <?php | |
| 442 | + endif; | |
| 443 | + ?> | |
| 444 | + | |
| 419 | 445 | <div id="services-config"> |
| 420 | 446 | <table id="available-services"> |
| 421 | 447 | <tr> |
| 422 | 448 | <td class="description"> |
| @@ -448,9 +474,9 @@ | ||
| 448 | 474 | <tr> |
| 449 | 475 | <td class="description"> |
| 450 | 476 | <h3> |
| 451 | 477 | <?php esc_html_e( 'Enabled Services', 'jetpack' ); ?> |
| 452 | - <img src="<?php echo esc_url( admin_url( 'images/loading.gif' ) ); ?>" width="16" height="16" alt="loading" style="vertical-align: middle; display: none" /> | |
| 478 | + <span class="spinner" style="vertical-align: middle"></span> | |
| 453 | 479 | </h3> |
| 454 | 480 | <p><?php esc_html_e( 'Services dragged here will appear individually.', 'jetpack' ); ?></p> |
| 455 | 481 | </td> |
| 456 | 482 | <td class="services" id="share-drop-target"> |
| @@ -671,9 +697,9 @@ | ||
| 671 | 697 | <tr valign="top" width="100"> |
| 672 | 698 | <th scope="row"></th> |
| 673 | 699 | <td> |
| 674 | 700 | <input type="submit" class="button-primary" value="<?php esc_attr_e( 'Create Share Button', 'jetpack' ); ?>" /> |
| 675 | - <img src="<?php echo esc_url( admin_url( 'images/loading.gif' ) ); ?>" width="16" height="16" alt="loading" style="vertical-align: middle; display: none" /> | |
| 701 | + <span class="spinner" style="vertical-align: middle"></span> | |
| 676 | 702 | </td> |
| 677 | 703 | </tr> |
| 678 | 704 | |
| 679 | 705 | <?php |
| @@ -740,16 +766,9 @@ | ||
| 740 | 766 | <h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2> |
| 741 | 767 | <div class="sharing-block-message__items-wrapper"> |
| 742 | 768 | <div> |
| 743 | 769 | <p><?php esc_html_e( 'Add sharing buttons to your blog and allow your visitors to share posts with their friends.', 'jetpack' ); ?></p> |
| 744 | - <div class="sharing-block-message__buttons-wrapper"> | |
| 745 | - <a href="<?php echo esc_url( admin_url( 'site-editor.php?path=%2Fwp_template' ) ); ?>" class="button button-primary"> | |
| 746 | - <?php esc_html_e( 'Go to the site editor', 'jetpack' ); ?> | |
| 747 | - </a> | |
| 748 | - <a href="<?php echo esc_url( Redirect::get_url( 'jetpack-support-sharing-block' ) ); ?>" class="button" target="_blank" rel="noopener noreferrer"> | |
| 749 | - <?php esc_html_e( 'Learn how to add Sharing Buttons', 'jetpack' ); ?> | |
| 750 | - </a> | |
| 751 | - </div> | |
| 770 | + <?php $this->site_editor_prompt_display(); ?> | |
| 752 | 771 | </div> |
| 753 | 772 | <div> |
| 754 | 773 | <p><?php esc_html_e( 'Sharing Buttons example:', 'jetpack' ); ?></p> |
| 755 | 774 | <div class="sharedaddy sd-sharing-enabled"> |
| @@ -782,8 +801,36 @@ | ||
| 782 | 801 | <br class="clearing" /> |
| 783 | 802 | </div> |
| 784 | 803 | <?php |
| 785 | 804 | } |
| 805 | + | |
| 806 | + /** | |
| 807 | + * Display the "Go to the site editor" prompt. | |
| 808 | + * | |
| 809 | + * @return void | |
| 810 | + */ | |
| 811 | + public function site_editor_prompt_display() { | |
| 812 | + $host = new Status\Host(); | |
| 813 | + | |
| 814 | + $wpcom_link = 'https://wordpress.com/support/wordpress-editor/blocks/sharing-buttons-block/'; | |
| 815 | + | |
| 816 | + if ( function_exists( 'localized_wpcom_url' ) ) { | |
| 817 | + $wpcom_link = localized_wpcom_url( $wpcom_link ); | |
| 818 | + } | |
| 819 | + | |
| 820 | + $link = $host->is_wpcom_platform() ? $wpcom_link : Redirect::get_url( 'jetpack-support-sharing-block' ); | |
| 821 | + | |
| 822 | + ?> | |
| 823 | + <div class="sharing-block-message__buttons-wrapper"> | |
| 824 | + <a href="<?php echo esc_url( admin_url( 'site-editor.php?path=%2Fwp_template' ) ); ?>" class="button button-primary"> | |
| 825 | + <?php esc_html_e( 'Go to the site editor', 'jetpack' ); ?> | |
| 826 | + </a> | |
| 827 | + <a data-target="wpcom-help-center" href="<?php echo esc_url( $link ); ?>" class="button" target="_blank" rel="noopener noreferrer"> | |
| 828 | + <?php esc_html_e( 'Learn how to add Sharing Buttons', 'jetpack' ); ?> | |
| 829 | + </a> | |
| 830 | + </div> | |
| 831 | + <?php | |
| 832 | + } | |
| 786 | 833 | } |
| 787 | 834 | |
| 788 | 835 | /** |
| 789 | 836 | * Callback to get the value for the jetpack_sharing_enabled field. |
| @@ -801,9 +848,9 @@ | ||
| 801 | 848 | return false; |
| 802 | 849 | } |
| 803 | 850 | |
| 804 | 851 | // if sharing IS disabled on this post, enabled=false, so negate the meta |
| 805 | - return (bool) ! get_post_meta( $post['id'], 'sharing_disabled', true ); | |
| 852 | + return ! get_post_meta( $post['id'], 'sharing_disabled', true ); | |
| 806 | 853 | } |
| 807 | 854 | |
| 808 | 855 | /** |
| 809 | 856 | * Callback to set sharing_disabled post_meta when the |