← All changes
|
_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php
+172
-278
13.6.2
→
16.3-a.7
View file →
| @@ -6,9 +6,8 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | use Automattic\Jetpack\Connection\REST_Connector; |
| 9 | 9 | use Automattic\Jetpack\Current_Plan as Jetpack_Plan; |
| 10 | -use Automattic\Jetpack\Plugins_Installer; | |
| 11 | 10 | use Automattic\Jetpack\Stats\WPCOM_Stats; |
| 12 | 11 | use Automattic\Jetpack\Stats_Admin\Main as Stats_Admin_Main; |
| 13 | 12 | use Automattic\Jetpack\Status; |
| 14 | 13 | use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection; |
| @@ -13,8 +12,12 @@ | ||
| 13 | 12 | use Automattic\Jetpack\Status; |
| 14 | 13 | use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection; |
| 15 | 14 | use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection_Shared_Functions; |
| 16 | 15 | |
| 16 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 17 | + exit( 0 ); | |
| 18 | +} | |
| 19 | + | |
| 17 | 20 | /** |
| 18 | 21 | * This is the base class for every Core API endpoint Jetpack uses. |
| 19 | 22 | */ |
| 20 | 23 | class Jetpack_Core_API_Module_Toggle_Endpoint extends Jetpack_Core_API_XMLRPC_Consumer_Endpoint { |
| @@ -88,8 +91,16 @@ | ||
| 88 | 91 | ); |
| 89 | 92 | } |
| 90 | 93 | |
| 91 | 94 | if ( Jetpack::activate_module( $module_slug, false, false ) ) { |
| 95 | + if ( ! Jetpack::is_module_active( $module_slug ) ) { | |
| 96 | + return new WP_Error( | |
| 97 | + 'module_forced', | |
| 98 | + esc_html__( 'The requested Jetpack module is disabled by your host or site administrator, so it stays off.', 'jetpack' ), | |
| 99 | + array( 'status' => 409 ) | |
| 100 | + ); | |
| 101 | + } | |
| 102 | + | |
| 92 | 103 | return rest_ensure_response( |
| 93 | 104 | array( |
| 94 | 105 | 'code' => 'success', |
| 95 | 106 | 'message' => esc_html__( 'The requested Jetpack module was activated.', 'jetpack' ), |
| @@ -149,9 +160,20 @@ | ||
| 149 | 160 | array( 'status' => 409 ) |
| 150 | 161 | ); |
| 151 | 162 | } |
| 152 | 163 | |
| 153 | - if ( Jetpack::deactivate_module( $module_slug ) ) { | |
| 164 | + $deactivated = Jetpack::deactivate_module( $module_slug ); | |
| 165 | + | |
| 166 | + // A module that was never saved as active leaves nothing to change, so check the outcome. | |
| 167 | + if ( Jetpack::is_module_active( $module_slug ) ) { | |
| 168 | + return new WP_Error( | |
| 169 | + 'module_forced', | |
| 170 | + esc_html__( 'The requested Jetpack module is enabled by your host or site administrator, so it stays on.', 'jetpack' ), | |
| 171 | + array( 'status' => 409 ) | |
| 172 | + ); | |
| 173 | + } | |
| 174 | + | |
| 175 | + if ( $deactivated ) { | |
| 154 | 176 | return rest_ensure_response( |
| 155 | 177 | array( |
| 156 | 178 | 'code' => 'success', |
| 157 | 179 | 'message' => esc_html__( 'The requested Jetpack module was deactivated.', 'jetpack' ), |
| @@ -259,9 +281,9 @@ | ||
| 259 | 281 | $activated = array(); |
| 260 | 282 | $failed = array(); |
| 261 | 283 | |
| 262 | 284 | foreach ( $request['modules'] as $module ) { |
| 263 | - if ( Jetpack::activate_module( $module, false, false ) ) { | |
| 285 | + if ( Jetpack::activate_module( $module, false, false ) && Jetpack::is_module_active( $module ) ) { | |
| 264 | 286 | $activated[] = $module; |
| 265 | 287 | } else { |
| 266 | 288 | $failed[] = $module; |
| 267 | 289 | } |
| @@ -402,15 +424,17 @@ | ||
| 402 | 424 | $module['activated'] = false; |
| 403 | 425 | } |
| 404 | 426 | |
| 405 | 427 | $i18n = jetpack_get_module_i18n( $request['slug'] ); |
| 406 | - if ( isset( $module['name'] ) ) { | |
| 407 | - $module['name'] = $i18n['name']; | |
| 428 | + if ( $i18n ) { | |
| 429 | + if ( isset( $module['name'] ) ) { | |
| 430 | + $module['name'] = $i18n['name']; | |
| 431 | + } | |
| 432 | + if ( isset( $module['description'] ) ) { | |
| 433 | + $module['description'] = $i18n['description']; | |
| 434 | + $module['short_description'] = $i18n['description']; | |
| 435 | + } | |
| 408 | 436 | } |
| 409 | - if ( isset( $module['description'] ) ) { | |
| 410 | - $module['description'] = $i18n['description']; | |
| 411 | - $module['short_description'] = $i18n['description']; | |
| 412 | - } | |
| 413 | 437 | if ( isset( $module['module_tags'] ) ) { |
| 414 | 438 | $module['module_tags'] = array_map( 'jetpack_get_module_i18n_tag', $module['module_tags'] ); |
| 415 | 439 | } |
| 416 | 440 | |
| @@ -447,9 +471,11 @@ | ||
| 447 | 471 | $response[ $module ] = Jetpack::is_module_active( $module ); |
| 448 | 472 | } |
| 449 | 473 | } |
| 450 | 474 | |
| 451 | - $settings = Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list( 'settings' ); | |
| 475 | + $settings = Jetpack_Core_Json_Api_Endpoints::filter_options_for_response( | |
| 476 | + Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list( 'settings' ) | |
| 477 | + ); | |
| 452 | 478 | |
| 453 | 479 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 454 | 480 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 455 | 481 | } |
| @@ -485,24 +511,8 @@ | ||
| 485 | 511 | $response[ $setting ] = class_exists( 'Akismet' ) ? Akismet::get_api_key() : ''; |
| 486 | 512 | } |
| 487 | 513 | break; |
| 488 | 514 | |
| 489 | - case 'onboarding': | |
| 490 | - $business_address = get_option( 'jpo_business_address' ); | |
| 491 | - $business_address = is_array( $business_address ) ? array_map( array( $this, 'decode_special_characters' ), $business_address ) : $business_address; | |
| 492 | - | |
| 493 | - $response[ $setting ] = array( | |
| 494 | - 'siteTitle' => $this->decode_special_characters( get_option( 'blogname' ) ), | |
| 495 | - 'siteDescription' => $this->decode_special_characters( get_option( 'blogdescription' ) ), | |
| 496 | - 'siteType' => get_option( 'jpo_site_type' ), | |
| 497 | - 'homepageFormat' => get_option( 'jpo_homepage_format' ), | |
| 498 | - 'addContactForm' => (int) get_option( 'jpo_contact_page' ), | |
| 499 | - 'businessAddress' => $business_address, | |
| 500 | - 'installWooCommerce' => is_plugin_active( 'woocommerce/woocommerce.php' ), | |
| 501 | - 'stats' => Jetpack::is_connection_ready() && Jetpack::is_module_active( 'stats' ), | |
| 502 | - ); | |
| 503 | - break; | |
| 504 | - | |
| 505 | 515 | case 'search_auto_config': |
| 506 | 516 | // Only writable. |
| 507 | 517 | $response[ $setting ] = 1; |
| 508 | 518 | break; |
| @@ -507,9 +517,9 @@ | ||
| 507 | 517 | $response[ $setting ] = 1; |
| 508 | 518 | break; |
| 509 | 519 | |
| 510 | 520 | default: |
| 511 | - $default = isset( $settings[ $setting ]['default'] ) ? $settings[ $setting ]['default'] : false; | |
| 521 | + $default = $settings[ $setting ]['default'] ?? false; | |
| 512 | 522 | $response[ $setting ] = Jetpack_Core_Json_Api_Endpoints::cast_value( get_option( $setting, $default ), $settings[ $setting ] ); |
| 513 | 523 | break; |
| 514 | 524 | } |
| 515 | 525 | } |
| @@ -515,25 +525,23 @@ | ||
| 515 | 525 | } |
| 516 | 526 | |
| 517 | 527 | $response['akismet'] = is_plugin_active( 'akismet/akismet.php' ); |
| 518 | 528 | |
| 529 | + require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php'; | |
| 530 | + if ( class_exists( 'Jetpack_Memberships' ) ) { | |
| 531 | + $response['newsletter_has_active_plan'] = count( Jetpack_Memberships::get_all_newsletter_plan_ids( false ) ) > 0; | |
| 532 | + } | |
| 533 | + | |
| 534 | + // Make sure we are returning a consistent type | |
| 535 | + if ( ! class_exists( 'Jetpack_Newsletter_Category_Helper' ) ) { | |
| 536 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-newsletter-category-helper.php'; | |
| 537 | + } | |
| 538 | + $response['wpcom_newsletter_categories'] = Jetpack_Newsletter_Category_Helper::get_category_ids(); | |
| 539 | + | |
| 519 | 540 | return rest_ensure_response( $response ); |
| 520 | 541 | } |
| 521 | 542 | |
| 522 | 543 | /** |
| 523 | - * Decode the special HTML characters in a certain value. | |
| 524 | - * | |
| 525 | - * @since 5.8 | |
| 526 | - * | |
| 527 | - * @param string $value Value to decode. | |
| 528 | - * | |
| 529 | - * @return string Value with decoded HTML characters. | |
| 530 | - */ | |
| 531 | - private function decode_special_characters( $value ) { | |
| 532 | - return (string) htmlspecialchars_decode( $value, ENT_QUOTES ); | |
| 533 | - } | |
| 534 | - | |
| 535 | - /** | |
| 536 | 544 | * If it's a valid Jetpack module and configuration parameters have been sent, update it. |
| 537 | 545 | * |
| 538 | 546 | * @since 4.3.0 |
| 539 | 547 | * |
| @@ -564,9 +572,9 @@ | ||
| 564 | 572 | } |
| 565 | 573 | |
| 566 | 574 | /* |
| 567 | 575 | * Get parameters to update the module. |
| 568 | - * We can not simply use $request->get_params() because when we registered this route, | |
| 576 | + * We cannot simply use $request->get_params() because when we registered this route, | |
| 569 | 577 | * we are adding the entire output of Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list() |
| 570 | 578 | * to the current request object's params. We are interested in body of the actual request. |
| 571 | 579 | * This may be JSON: |
| 572 | 580 | */ |
| @@ -651,13 +659,22 @@ | ||
| 651 | 659 | if ( ! $updated ) { |
| 652 | 660 | $not_updated[ $option ] = $error; |
| 653 | 661 | } |
| 654 | 662 | |
| 663 | + if ( $updated ) { | |
| 664 | + // Return the module state. | |
| 665 | + $response[ $option ] = $value; | |
| 666 | + } | |
| 667 | + | |
| 655 | 668 | // Remove module from list so we don't go through it again. |
| 656 | 669 | unset( $params[ $option ] ); |
| 657 | 670 | } |
| 658 | 671 | } |
| 659 | 672 | |
| 673 | + if ( ! class_exists( 'Jetpack_Newsletter_Category_Helper' ) ) { | |
| 674 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-newsletter-category-helper.php'; | |
| 675 | + } | |
| 676 | + | |
| 660 | 677 | foreach ( $params as $option => $value ) { |
| 661 | 678 | |
| 662 | 679 | // Used if there was an error. Can be overwritten with specific error messages. |
| 663 | 680 | $error = ''; |
| @@ -667,8 +684,14 @@ | ||
| 667 | 684 | |
| 668 | 685 | // Get option attributes, including the group it belongs to. |
| 669 | 686 | $option_attrs = $options[ $option ]; |
| 670 | 687 | |
| 688 | + // Everything outside the Post by Email group requires the admin capability. | |
| 689 | + if ( 'post-by-email' !== $option_attrs['jp_group'] && ! current_user_can( 'jetpack_configure_modules' ) ) { | |
| 690 | + $not_updated[ $option ] = REST_Connector::get_user_permissions_error_msg(); | |
| 691 | + continue; | |
| 692 | + } | |
| 693 | + | |
| 671 | 694 | // If this is a module option and the related module isn't active for any reason, continue with the next one. |
| 672 | 695 | if ( 'settings' !== $option_attrs['jp_group'] ) { |
| 673 | 696 | if ( ! Jetpack::is_module( $option_attrs['jp_group'] ) ) { |
| 674 | 697 | $not_updated[ $option ] = esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ); |
| @@ -719,8 +742,13 @@ | ||
| 719 | 742 | $updated = get_option( 'WPLANG' ) === $language ? true : update_option( 'WPLANG', $language ); |
| 720 | 743 | break; |
| 721 | 744 | |
| 722 | 745 | case 'monitor_receive_notifications': |
| 746 | + if ( ! class_exists( 'Jetpack_Monitor' ) ) { | |
| 747 | + $updated = false; | |
| 748 | + break; | |
| 749 | + } | |
| 750 | + | |
| 723 | 751 | $monitor = new Jetpack_Monitor(); |
| 724 | 752 | |
| 725 | 753 | // If we got true as response, consider it done. |
| 726 | 754 | $updated = true === $monitor->update_option_receive_jetpack_monitor_notification( $value ); |
| @@ -726,8 +754,13 @@ | ||
| 726 | 754 | $updated = true === $monitor->update_option_receive_jetpack_monitor_notification( $value ); |
| 727 | 755 | break; |
| 728 | 756 | |
| 729 | 757 | case 'post_by_email_address': |
| 758 | + if ( ! class_exists( 'Jetpack_Post_By_Email' ) ) { | |
| 759 | + $updated = false; | |
| 760 | + break; | |
| 761 | + } | |
| 762 | + | |
| 730 | 763 | $result = Jetpack_Post_By_Email::init()->process_api_request( $value ); |
| 731 | 764 | |
| 732 | 765 | // If we got an email address (create or regenerate) or 1 (delete), consider it done. |
| 733 | 766 | if ( is_string( $result ) && preg_match( '/[a-z0-9][email protected]/', $result ) ) { |
| @@ -748,9 +781,9 @@ | ||
| 748 | 781 | $result = false; |
| 749 | 782 | } |
| 750 | 783 | |
| 751 | 784 | // If we got one of Protect keys, consider it done. |
| 752 | - if ( preg_match( '/[a-z0-9]{40,}/i', $result ) ) { | |
| 785 | + if ( is_string( $result ) && preg_match( '/[a-z0-9]{40,}/i', $result ) ) { | |
| 753 | 786 | $response[ $option ] = $result; |
| 754 | 787 | $updated = true; |
| 755 | 788 | } |
| 756 | 789 | break; |
| @@ -801,15 +834,16 @@ | ||
| 801 | 834 | case 'facebook': |
| 802 | 835 | $grouped_options_current = (array) get_option( 'verification_services_codes' ); |
| 803 | 836 | $grouped_options = $grouped_options_current; |
| 804 | 837 | |
| 805 | - // Extracts the content attribute from the HTML meta tag if needed. | |
| 806 | - if ( preg_match( '#.*<meta name="(?:[^"]+)" content="([^"]+)" />.*#i', $value, $matches ) ) { | |
| 807 | - $grouped_options[ $option ] = $matches[1]; | |
| 808 | - } else { | |
| 809 | - $grouped_options[ $option ] = $value; | |
| 838 | + $validated_code = jetpack_verification_validate_code( $value ); | |
| 839 | + if ( false === $validated_code ) { | |
| 840 | + $error = esc_html__( 'The site verification code is invalid.', 'jetpack' ); | |
| 841 | + break; | |
| 810 | 842 | } |
| 811 | 843 | |
| 844 | + $grouped_options[ $option ] = $validated_code; | |
| 845 | + | |
| 812 | 846 | // If option value was the same, consider it done. |
| 813 | 847 | $updated = $grouped_options_current !== $grouped_options |
| 814 | 848 | ? update_option( 'verification_services_codes', $grouped_options ) |
| 815 | 849 | : true; |
| @@ -814,8 +848,17 @@ | ||
| 814 | 848 | ? update_option( 'verification_services_codes', $grouped_options ) |
| 815 | 849 | : true; |
| 816 | 850 | break; |
| 817 | 851 | |
| 852 | + case Jetpack_SEO_Utils::FRONT_PAGE_META_OPTION: | |
| 853 | + Jetpack_SEO_Utils::update_front_page_meta_description( $value ); | |
| 854 | + $response[ $option ] = Jetpack_SEO_Utils::get_front_page_meta_description(); | |
| 855 | + // The helper returns an empty string for a successful clear or | |
| 856 | + // same-value write, so use its authoritative getter for the response | |
| 857 | + // and treat every valid request reaching this switch as handled. | |
| 858 | + $updated = true; | |
| 859 | + break; | |
| 860 | + | |
| 818 | 861 | case 'sharing_services': |
| 819 | 862 | if ( ! class_exists( 'Sharing_Service' ) && ! include_once JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) { |
| 820 | 863 | break; |
| 821 | 864 | } |
| @@ -963,30 +1006,14 @@ | ||
| 963 | 1006 | ? update_option( $option, (bool) $value ) |
| 964 | 1007 | : true; |
| 965 | 1008 | break; |
| 966 | 1009 | |
| 967 | - case 'onboarding': | |
| 968 | - require_once JETPACK__PLUGIN_DIR . '_inc/lib/widgets.php'; | |
| 969 | - // Break apart and set Jetpack onboarding options. | |
| 970 | - $result = $this->process_onboarding( (array) $value ); | |
| 971 | - if ( empty( $result ) ) { | |
| 972 | - $updated = true; | |
| 973 | - } else { | |
| 974 | - $error = sprintf( | |
| 975 | - /* Translators: placeholder is a list of error codes. */ | |
| 976 | - esc_html__( 'Onboarding failed to process: %s', 'jetpack' ), | |
| 977 | - $result | |
| 978 | - ); | |
| 979 | - $updated = false; | |
| 980 | - } | |
| 981 | - break; | |
| 982 | - | |
| 983 | 1010 | case 'jetpack_subscriptions_reply_to': |
| 984 | 1011 | // If option value was the same, consider it done. |
| 985 | 1012 | require_once JETPACK__PLUGIN_DIR . 'modules/subscriptions/class-settings.php'; |
| 986 | 1013 | $sub_value = Automattic\Jetpack\Modules\Subscriptions\Settings::is_valid_reply_to( $value ) |
| 987 | 1014 | ? $value |
| 988 | - : Automattic\Jetpack\Modules\Subscriptions\Settings::get_default_reply_to(); | |
| 1015 | + : Automattic\Jetpack\Modules\Subscriptions\Settings::$default_reply_to; | |
| 989 | 1016 | |
| 990 | 1017 | $updated = (string) get_option( $option ) !== (string) $sub_value ? update_option( $option, $sub_value ) : true; |
| 991 | 1018 | break; |
| 992 | 1019 | case 'jetpack_subscriptions_from_name': |
| @@ -998,8 +1025,9 @@ | ||
| 998 | 1025 | case 'stb_enabled': |
| 999 | 1026 | case 'stc_enabled': |
| 1000 | 1027 | case 'sm_enabled': |
| 1001 | 1028 | case 'jetpack_subscribe_overlay_enabled': |
| 1029 | + case 'jetpack_subscribe_floating_button_enabled': | |
| 1002 | 1030 | case 'wpcom_newsletter_categories_enabled': |
| 1003 | 1031 | case 'wpcom_featured_image_in_email': |
| 1004 | 1032 | case 'jetpack_gravatar_in_email': |
| 1005 | 1033 | case 'jetpack_author_in_email': |
| @@ -1021,9 +1049,9 @@ | ||
| 1021 | 1049 | if ( ! is_array( $value ) ) { |
| 1022 | 1050 | break; |
| 1023 | 1051 | } |
| 1024 | 1052 | |
| 1025 | - $allowed_keys = array( 'invitation', 'comment_follow', 'welcome' ); | |
| 1053 | + $allowed_keys = array( 'invitation', 'comment_follow', 'welcome', 'subscribe_modal_heading', 'free_tier_description', 'hide_free_tier' ); | |
| 1026 | 1054 | $filtered_value = array_filter( |
| 1027 | 1055 | $value, |
| 1028 | 1056 | function ( $key ) use ( $allowed_keys ) { |
| 1029 | 1057 | return in_array( $key, $allowed_keys, true ); |
| @@ -1034,8 +1062,17 @@ | ||
| 1034 | 1062 | if ( empty( $filtered_value ) ) { |
| 1035 | 1063 | break; |
| 1036 | 1064 | } |
| 1037 | 1065 | |
| 1066 | + // `hide_free_tier` is a boolean flag, so pull it out before the HTML | |
| 1067 | + // sanitization below (which expects strings). Sanitize it with | |
| 1068 | + // rest_sanitize_boolean() so stringy booleans (e.g. "false", "0") | |
| 1069 | + // are interpreted correctly rather than being treated as truthy by a | |
| 1070 | + // plain `! empty()`. | |
| 1071 | + $has_hide_free_tier = array_key_exists( 'hide_free_tier', $filtered_value ); | |
| 1072 | + $hide_free_tier = $has_hide_free_tier && rest_sanitize_boolean( $filtered_value['hide_free_tier'] ); | |
| 1073 | + unset( $filtered_value['hide_free_tier'] ); | |
| 1074 | + | |
| 1038 | 1075 | array_walk_recursive( |
| 1039 | 1076 | $filtered_value, |
| 1040 | 1077 | function ( &$value ) { |
| 1041 | 1078 | $value = wp_kses( |
| @@ -1040,9 +1077,15 @@ | ||
| 1040 | 1077 | function ( &$value ) { |
| 1041 | 1078 | $value = wp_kses( |
| 1042 | 1079 | $value, |
| 1043 | 1080 | array( |
| 1044 | - 'a' => array( | |
| 1081 | + 'ul' => array(), | |
| 1082 | + 'li' => array(), | |
| 1083 | + 'p' => array(), | |
| 1084 | + 'strong' => array(), | |
| 1085 | + 'ol' => array(), | |
| 1086 | + 'em' => array(), | |
| 1087 | + 'a' => array( | |
| 1045 | 1088 | 'href' => array(), |
| 1046 | 1089 | ), |
| 1047 | 1090 | ) |
| 1048 | 1091 | ); |
| @@ -1048,8 +1091,33 @@ | ||
| 1048 | 1091 | ); |
| 1049 | 1092 | } |
| 1050 | 1093 | ); |
| 1051 | 1094 | |
| 1095 | + // Normalize whitespace-only `subscribe_modal_heading` input to empty so | |
| 1096 | + // the modal template's `empty()` fallback fires. PHP's `empty()` treats | |
| 1097 | + // `" "` as non-empty, which would otherwise render a blank heading. | |
| 1098 | + if ( isset( $filtered_value['subscribe_modal_heading'] ) ) { | |
| 1099 | + $filtered_value['subscribe_modal_heading'] = trim( $filtered_value['subscribe_modal_heading'] ); | |
| 1100 | + } | |
| 1101 | + | |
| 1102 | + // The free tier description is stored as plain markdown source, so strip | |
| 1103 | + // all HTML and cap its length to match the paid-tier description field. | |
| 1104 | + // WordPress core guarantees mb_substr() (polyfilled in wp-includes/compat.php | |
| 1105 | + // when the mbstring extension is unavailable), so it's safe to use directly. | |
| 1106 | + // A JSON payload could supply a non-scalar (array/object) for this field, | |
| 1107 | + // which would fatal in wp_kses()/mb_substr() on PHP 8+, so drop invalid values. | |
| 1108 | + if ( isset( $filtered_value['free_tier_description'] ) ) { | |
| 1109 | + if ( is_scalar( $filtered_value['free_tier_description'] ) ) { | |
| 1110 | + $filtered_value['free_tier_description'] = mb_substr( wp_kses( (string) $filtered_value['free_tier_description'], array() ), 0, 500 ); | |
| 1111 | + } else { | |
| 1112 | + unset( $filtered_value['free_tier_description'] ); | |
| 1113 | + } | |
| 1114 | + } | |
| 1115 | + | |
| 1116 | + if ( $has_hide_free_tier ) { | |
| 1117 | + $filtered_value['hide_free_tier'] = $hide_free_tier; | |
| 1118 | + } | |
| 1119 | + | |
| 1052 | 1120 | $old_subscription_options = get_option( 'subscription_options' ); |
| 1053 | 1121 | if ( ! is_array( $old_subscription_options ) ) { |
| 1054 | 1122 | $old_subscription_options = array(); |
| 1055 | 1123 | } |
| @@ -1065,8 +1133,30 @@ | ||
| 1065 | 1133 | $error = esc_html__( 'Subscription Options failed to process.', 'jetpack' ); |
| 1066 | 1134 | } |
| 1067 | 1135 | break; |
| 1068 | 1136 | |
| 1137 | + case Jetpack_Newsletter_Category_Helper::NEWSLETTER_CATEGORIES_OPTION: | |
| 1138 | + if ( ! is_array( $value ) || empty( $value ) ) { | |
| 1139 | + $updated = true; | |
| 1140 | + break; | |
| 1141 | + } | |
| 1142 | + | |
| 1143 | + // If we are already current, do nothing | |
| 1144 | + $current_value = Jetpack_Newsletter_Category_Helper::get_category_ids(); | |
| 1145 | + if ( $value === $current_value ) { | |
| 1146 | + $updated = true; | |
| 1147 | + break; | |
| 1148 | + } | |
| 1149 | + | |
| 1150 | + if ( Jetpack_Newsletter_Category_Helper::save_category_ids( $value ) ) { | |
| 1151 | + $updated = true; | |
| 1152 | + } else { | |
| 1153 | + $updated = false; | |
| 1154 | + $error = esc_html__( 'Newsletter category did not update.', 'jetpack' ); | |
| 1155 | + } | |
| 1156 | + | |
| 1157 | + break; | |
| 1158 | + | |
| 1069 | 1159 | default: |
| 1070 | 1160 | // Boolean values are stored as 1 or 0. |
| 1071 | 1161 | if ( isset( $options[ $option ]['type'] ) && 'boolean' === $options[ $option ]['type'] ) { |
| 1072 | 1162 | $value = (int) $value; |
| @@ -1072,9 +1162,9 @@ | ||
| 1072 | 1162 | $value = (int) $value; |
| 1073 | 1163 | } |
| 1074 | 1164 | |
| 1075 | 1165 | // If option value was the same as it's current value, or it's default, consider it done. |
| 1076 | - $default = isset( $options[ $option ]['default'] ) ? $options[ $option ]['default'] : false; | |
| 1166 | + $default = $options[ $option ]['default'] ?? false; | |
| 1077 | 1167 | $updated = get_option( $option, $default ) != $value // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual -- ensure we support scalars or strings saved by update_option. |
| 1078 | 1168 | ? update_option( $option, $value ) |
| 1079 | 1169 | : true; |
| 1080 | 1170 | break; |
| @@ -1128,218 +1218,30 @@ | ||
| 1128 | 1218 | * Perform tasks in the site based on onboarding choices. |
| 1129 | 1219 | * |
| 1130 | 1220 | * @since 5.4.0 |
| 1131 | 1221 | * |
| 1222 | + * @deprecated since 13.9 | |
| 1223 | + * | |
| 1132 | 1224 | * @param array $data Onboarding choices made by user. |
| 1133 | 1225 | * |
| 1134 | 1226 | * @return string Result of onboarding processing and, if there is one, an error message. |
| 1135 | 1227 | */ |
| 1136 | - private function process_onboarding( $data ) { | |
| 1137 | - if ( isset( $data['end'] ) && $data['end'] ) { | |
| 1138 | - return Jetpack::invalidate_onboarding_token() | |
| 1139 | - ? '' | |
| 1140 | - : esc_html__( "The onboarding token couldn't be deleted.", 'jetpack' ); | |
| 1141 | - } | |
| 1142 | - | |
| 1143 | - $error = array(); | |
| 1144 | - | |
| 1145 | - if ( ! empty( $data['siteTitle'] ) ) { | |
| 1146 | - // If option value was the same, consider it done. | |
| 1147 | - if ( ! ( | |
| 1148 | - update_option( 'blogname', $data['siteTitle'] ) | |
| 1149 | - || get_option( 'blogname' ) === $data['siteTitle'] | |
| 1150 | - ) ) { | |
| 1151 | - $error[] = 'siteTitle'; | |
| 1152 | - } | |
| 1153 | - } | |
| 1154 | - | |
| 1155 | - if ( isset( $data['siteDescription'] ) ) { | |
| 1156 | - // If option value was the same, consider it done. | |
| 1157 | - if ( ! ( | |
| 1158 | - update_option( 'blogdescription', $data['siteDescription'] ) | |
| 1159 | - || get_option( 'blogdescription' ) === $data['siteDescription'] | |
| 1160 | - ) ) { | |
| 1161 | - $error[] = 'siteDescription'; | |
| 1162 | - } | |
| 1163 | - } | |
| 1164 | - | |
| 1165 | - $site_title = get_option( 'blogname' ); | |
| 1166 | - $author = get_current_user_id() || 1; | |
| 1167 | - | |
| 1168 | - if ( ! empty( $data['siteType'] ) ) { | |
| 1169 | - if ( ! ( | |
| 1170 | - update_option( 'jpo_site_type', $data['siteType'] ) | |
| 1171 | - || get_option( 'jpo_site_type' ) === $data['siteType'] | |
| 1172 | - ) ) { | |
| 1173 | - $error[] = 'siteType'; | |
| 1174 | - } | |
| 1175 | - } | |
| 1176 | - | |
| 1177 | - if ( isset( $data['homepageFormat'] ) ) { | |
| 1178 | - /* | |
| 1179 | - * If $data['homepageFormat'] is 'posts', | |
| 1180 | - * we have nothing to do since it's WordPress' default | |
| 1181 | - * if it exists, just update | |
| 1182 | - */ | |
| 1183 | - $homepage_format = get_option( 'jpo_homepage_format' ); | |
| 1184 | - if ( ! $homepage_format || $homepage_format !== $data['homepageFormat'] ) { | |
| 1185 | - if ( 'page' === $data['homepageFormat'] ) { | |
| 1186 | - if ( ! ( | |
| 1187 | - update_option( 'show_on_front', 'page' ) | |
| 1188 | - || get_option( 'show_on_front' ) === 'page' | |
| 1189 | - ) ) { | |
| 1190 | - $error[] = 'homepageFormat'; | |
| 1191 | - } | |
| 1192 | - | |
| 1193 | - $home = wp_insert_post( | |
| 1194 | - array( | |
| 1195 | - 'post_type' => 'page', | |
| 1196 | - /* translators: this references the home page of a site, also called front page. */ | |
| 1197 | - 'post_title' => esc_html_x( 'Home Page', 'The home page of a website.', 'jetpack' ), | |
| 1198 | - 'post_content' => sprintf( | |
| 1199 | - /* Translators: placeholder is the site title. */ | |
| 1200 | - esc_html__( 'Welcome to %s.', 'jetpack' ), | |
| 1201 | - $site_title | |
| 1202 | - ), | |
| 1203 | - 'post_status' => 'publish', | |
| 1204 | - 'post_author' => $author, | |
| 1205 | - ) | |
| 1206 | - ); | |
| 1207 | - if ( 0 === $home ) { | |
| 1208 | - $error[] = 'home insert: 0'; | |
| 1209 | - } elseif ( is_wp_error( $home ) ) { | |
| 1210 | - $error[] = 'home creation: ' . $home->get_error_message(); | |
| 1211 | - } | |
| 1212 | - if ( ! ( | |
| 1213 | - update_option( 'page_on_front', $home ) | |
| 1214 | - || get_option( 'page_on_front' ) === $home | |
| 1215 | - ) ) { | |
| 1216 | - | |
| 1217 | - $error[] = 'home set'; | |
| 1218 | - } | |
| 1219 | - | |
| 1220 | - $blog = wp_insert_post( | |
| 1221 | - array( | |
| 1222 | - 'post_type' => 'page', | |
| 1223 | - /* translators: this references the page where blog posts are listed. */ | |
| 1224 | - 'post_title' => esc_html_x( 'Blog', 'The blog of a website.', 'jetpack' ), | |
| 1225 | - 'post_content' => sprintf( | |
| 1226 | - /* Translators: placeholder is the site title. */ | |
| 1227 | - esc_html__( 'These are the latest posts in %s.', 'jetpack' ), | |
| 1228 | - $site_title | |
| 1229 | - ), | |
| 1230 | - 'post_status' => 'publish', | |
| 1231 | - 'post_author' => $author, | |
| 1232 | - ) | |
| 1233 | - ); | |
| 1234 | - if ( 0 === $blog ) { | |
| 1235 | - $error[] = 'blog insert: 0'; | |
| 1236 | - } elseif ( is_wp_error( $blog ) ) { | |
| 1237 | - $error[] = 'blog creation: ' . $blog->get_error_message(); | |
| 1238 | - } | |
| 1239 | - if ( ! ( | |
| 1240 | - update_option( 'page_for_posts', $blog ) | |
| 1241 | - || get_option( 'page_for_posts' ) === $blog | |
| 1242 | - ) ) { | |
| 1243 | - $error[] = 'blog set'; | |
| 1244 | - } | |
| 1245 | - } else { | |
| 1246 | - $front_page = get_option( 'page_on_front' ); | |
| 1247 | - $posts_page = get_option( 'page_for_posts' ); | |
| 1248 | - if ( $posts_page && get_post( $posts_page ) ) { | |
| 1249 | - wp_delete_post( $posts_page ); | |
| 1250 | - } | |
| 1251 | - if ( $front_page && get_post( $front_page ) ) { | |
| 1252 | - wp_delete_post( $front_page ); | |
| 1253 | - } | |
| 1254 | - update_option( 'show_on_front', 'posts' ); | |
| 1255 | - } | |
| 1256 | - } | |
| 1257 | - update_option( 'jpo_homepage_format', $data['homepageFormat'] ); | |
| 1258 | - } | |
| 1259 | - | |
| 1260 | - // Setup contact page and add a form and/or business info. | |
| 1261 | - $contact_page = ''; | |
| 1262 | - if ( ! empty( $data['addContactForm'] ) && ! get_option( 'jpo_contact_page' ) ) { | |
| 1263 | - $contact_form_module_active = Jetpack::is_module_active( 'contact-form' ); | |
| 1264 | - if ( ! $contact_form_module_active ) { | |
| 1265 | - $contact_form_module_active = Jetpack::activate_module( 'contact-form', false, false ); | |
| 1266 | - } | |
| 1267 | - | |
| 1268 | - if ( $contact_form_module_active ) { | |
| 1269 | - $contact_page = '[contact-form][contact-field label="' . esc_html__( 'Name', 'jetpack' ) . '" type="name" required="true" /][contact-field label="' . esc_html__( 'Email', 'jetpack' ) . '" type="email" required="true" /][contact-field label="' . esc_html__( 'Website', 'jetpack' ) . '" type="url" /][contact-field label="' . esc_html__( 'Message', 'jetpack' ) . '" type="textarea" /][/contact-form]'; | |
| 1270 | - } else { | |
| 1271 | - $error[] = 'contact-form activate'; | |
| 1272 | - } | |
| 1273 | - } | |
| 1274 | - | |
| 1275 | - if ( isset( $data['businessPersonal'] ) && 'business' === $data['businessPersonal'] ) { | |
| 1276 | - $contact_page .= "\n" . implode( "\n", $data['businessInfo'] ); | |
| 1277 | - } | |
| 1278 | - | |
| 1279 | - if ( ! empty( $contact_page ) ) { | |
| 1280 | - $form = wp_insert_post( | |
| 1281 | - array( | |
| 1282 | - 'post_type' => 'page', | |
| 1283 | - /* translators: this references a page with contact details and possibly a form. */ | |
| 1284 | - 'post_title' => esc_html_x( 'Contact us', 'Contact page for your website.', 'jetpack' ), | |
| 1285 | - 'post_content' => esc_html__( 'Send us a message!', 'jetpack' ) . "\n" . $contact_page, | |
| 1286 | - 'post_status' => 'publish', | |
| 1287 | - 'post_author' => $author, | |
| 1288 | - ) | |
| 1289 | - ); | |
| 1290 | - if ( 0 === $form ) { | |
| 1291 | - $error[] = 'form insert: 0'; | |
| 1292 | - } elseif ( is_wp_error( $form ) ) { | |
| 1293 | - $error[] = 'form creation: ' . $form->get_error_message(); | |
| 1294 | - } else { | |
| 1295 | - update_option( 'jpo_contact_page', $form ); | |
| 1296 | - } | |
| 1297 | - } | |
| 1298 | - | |
| 1299 | - if ( isset( $data['businessAddress'] ) ) { | |
| 1300 | - $handled_business_address = self::handle_business_address( $data['businessAddress'] ); | |
| 1301 | - if ( is_wp_error( $handled_business_address ) ) { | |
| 1302 | - $error[] = 'BusinessAddress'; | |
| 1303 | - } | |
| 1304 | - } | |
| 1305 | - | |
| 1306 | - if ( ! empty( $data['installWooCommerce'] ) ) { | |
| 1307 | - $wc_install_result = Plugins_Installer::install_and_activate_plugin( 'woocommerce' ); | |
| 1308 | - delete_transient( '_wc_activation_redirect' ); // Redirecting to WC setup would kill our users' flow. | |
| 1309 | - if ( is_wp_error( $wc_install_result ) ) { | |
| 1310 | - $error[] = 'woocommerce installation'; | |
| 1311 | - } | |
| 1312 | - } | |
| 1313 | - | |
| 1314 | - if ( ! empty( $data['stats'] ) ) { | |
| 1315 | - if ( Jetpack::is_connection_ready() ) { | |
| 1316 | - $stats_module_active = Jetpack::is_module_active( 'stats' ); | |
| 1317 | - if ( ! $stats_module_active ) { | |
| 1318 | - $stats_module_active = Jetpack::activate_module( 'stats', false, false ); | |
| 1319 | - } | |
| 1320 | - | |
| 1321 | - if ( ! $stats_module_active ) { | |
| 1322 | - $error[] = 'stats activate'; | |
| 1323 | - } | |
| 1324 | - } else { | |
| 1325 | - $error[] = 'stats not connected'; | |
| 1326 | - } | |
| 1327 | - } | |
| 1328 | - | |
| 1329 | - return empty( $error ) | |
| 1330 | - ? '' | |
| 1331 | - : implode( ', ', $error ); | |
| 1228 | + private function process_onboarding( $data ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 1229 | + _deprecated_function( __METHOD__, '13.9' ); | |
| 1230 | + return ''; | |
| 1332 | 1231 | } |
| 1333 | 1232 | |
| 1334 | 1233 | /** |
| 1335 | 1234 | * Add or update Business Address widget. |
| 1336 | 1235 | * |
| 1236 | + * @deprecated since 13.9 | |
| 1237 | + * | |
| 1337 | 1238 | * @param array $address Array of business address fields. |
| 1338 | 1239 | * |
| 1339 | 1240 | * @return WP_Error|true True if the data was saved correctly. |
| 1340 | 1241 | */ |
| 1341 | 1242 | private static function handle_business_address( $address ) { |
| 1243 | + _deprecated_function( __METHOD__, '13.9' ); | |
| 1342 | 1244 | $first_sidebar = Jetpack_Widgets::get_first_sidebar(); |
| 1343 | 1245 | |
| 1344 | 1246 | $widgets_module_active = Jetpack::is_module_active( 'widgets' ); |
| 1345 | 1247 | if ( ! $widgets_module_active ) { |
| @@ -1423,13 +1325,8 @@ | ||
| 1423 | 1325 | * |
| 1424 | 1326 | * @return bool |
| 1425 | 1327 | */ |
| 1426 | 1328 | public function can_request( $request ) { |
| 1427 | - $req_params = $request->get_params(); | |
| 1428 | - if ( ! empty( $req_params['onboarding']['token'] ) && isset( $req_params['rest_route'] ) ) { | |
| 1429 | - return Jetpack::validate_onboarding_token_action( $req_params['onboarding']['token'], $req_params['rest_route'] ); | |
| 1430 | - } | |
| 1431 | - | |
| 1432 | 1329 | if ( 'GET' === $request->get_method() ) { |
| 1433 | 1330 | return current_user_can( 'jetpack_admin_page' ); |
| 1434 | 1331 | } else { |
| 1435 | 1332 | $module = Jetpack_Core_Json_Api_Endpoints::get_module_requested(); |
| @@ -1438,13 +1335,13 @@ | ||
| 1438 | 1335 | if ( ! is_array( $params ) ) { |
| 1439 | 1336 | $params = $request->get_body_params(); |
| 1440 | 1337 | } |
| 1441 | 1338 | $options = Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list( $params ); |
| 1442 | - foreach ( $options as $option => $definition ) { | |
| 1443 | - if ( in_array( $options[ $option ]['jp_group'], array( 'post-by-email' ), true ) ) { | |
| 1444 | - $module = $options[ $option ]['jp_group']; | |
| 1445 | - break; | |
| 1446 | - } | |
| 1339 | + | |
| 1340 | + // The Post by Email gate applies only when the request contains nothing else. | |
| 1341 | + $groups = array_values( array_unique( array_column( $options, 'jp_group' ) ) ); | |
| 1342 | + if ( array( 'post-by-email' ) === $groups ) { | |
| 1343 | + $module = 'post-by-email'; | |
| 1447 | 1344 | } |
| 1448 | 1345 | } |
| 1449 | 1346 | // User is trying to create, regenerate or delete its PbE. |
| 1450 | 1347 | if ( 'post-by-email' === $module ) { |
| @@ -1460,9 +1357,8 @@ | ||
| 1460 | 1357 | * |
| 1461 | 1358 | * phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound |
| 1462 | 1359 | */ |
| 1463 | 1360 | class Jetpack_Core_API_Module_Data_Endpoint { |
| 1464 | - // phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound | |
| 1465 | 1361 | |
| 1466 | 1362 | /** |
| 1467 | 1363 | * Process request and return different data based on the module we are interested in. |
| 1468 | 1364 | * |
| @@ -1662,11 +1558,9 @@ | ||
| 1662 | 1558 | array( |
| 1663 | 1559 | 'general' => $initial_stats, |
| 1664 | 1560 | |
| 1665 | 1561 | // Build data for 'day' as if it was $wpcom_stats ->get_visits( array( 'unit' => 'day, 'quantity' => 30). |
| 1666 | - 'day' => isset( $initial_stats->visits ) | |
| 1667 | - ? $initial_stats->visits | |
| 1668 | - : array(), | |
| 1562 | + 'day' => $initial_stats->visits ?? array(), | |
| 1669 | 1563 | ) |
| 1670 | 1564 | ); |
| 1671 | 1565 | case 'week': |
| 1672 | 1566 | return rest_ensure_response( |
| @@ -1807,9 +1701,9 @@ | ||
| 1807 | 1701 | $last_service = $copy_services[ $last ]; |
| 1808 | 1702 | unset( $copy_services[ $last ] ); |
| 1809 | 1703 | $message = esc_html( |
| 1810 | 1704 | sprintf( |
| 1811 | - /* translators: %1$s is a comma separated list of services, and %2$s is a single service name like Google, Bing, Pinterest, etc. */ | |
| 1705 | + /* translators: %1$s is a comma-separated list of services, and %2$s is a single service name like Google, Bing, Pinterest, etc. */ | |
| 1812 | 1706 | __( 'Your site is verified with %1$s and %2$s.', 'jetpack' ), |
| 1813 | 1707 | implode( ', ', $copy_services ), |
| 1814 | 1708 | $last_service |
| 1815 | 1709 | ) |