← All changes
|
_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php
+171
-271
13.3.3
→
16.3-a.1
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 { |
| @@ -402,15 +405,17 @@ | ||
| 402 | 405 | $module['activated'] = false; |
| 403 | 406 | } |
| 404 | 407 | |
| 405 | 408 | $i18n = jetpack_get_module_i18n( $request['slug'] ); |
| 406 | - if ( isset( $module['name'] ) ) { | |
| 407 | - $module['name'] = $i18n['name']; | |
| 409 | + if ( $i18n ) { | |
| 410 | + if ( isset( $module['name'] ) ) { | |
| 411 | + $module['name'] = $i18n['name']; | |
| 412 | + } | |
| 413 | + if ( isset( $module['description'] ) ) { | |
| 414 | + $module['description'] = $i18n['description']; | |
| 415 | + $module['short_description'] = $i18n['description']; | |
| 416 | + } | |
| 408 | 417 | } |
| 409 | - if ( isset( $module['description'] ) ) { | |
| 410 | - $module['description'] = $i18n['description']; | |
| 411 | - $module['short_description'] = $i18n['description']; | |
| 412 | - } | |
| 413 | 418 | if ( isset( $module['module_tags'] ) ) { |
| 414 | 419 | $module['module_tags'] = array_map( 'jetpack_get_module_i18n_tag', $module['module_tags'] ); |
| 415 | 420 | } |
| 416 | 421 | |
| @@ -447,9 +452,11 @@ | ||
| 447 | 452 | $response[ $module ] = Jetpack::is_module_active( $module ); |
| 448 | 453 | } |
| 449 | 454 | } |
| 450 | 455 | |
| 451 | - $settings = Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list( 'settings' ); | |
| 456 | + $settings = Jetpack_Core_Json_Api_Endpoints::filter_options_for_response( | |
| 457 | + Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list( 'settings' ) | |
| 458 | + ); | |
| 452 | 459 | |
| 453 | 460 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 454 | 461 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 455 | 462 | } |
| @@ -485,24 +492,8 @@ | ||
| 485 | 492 | $response[ $setting ] = class_exists( 'Akismet' ) ? Akismet::get_api_key() : ''; |
| 486 | 493 | } |
| 487 | 494 | break; |
| 488 | 495 | |
| 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 | 496 | case 'search_auto_config': |
| 506 | 497 | // Only writable. |
| 507 | 498 | $response[ $setting ] = 1; |
| 508 | 499 | break; |
| @@ -507,9 +498,9 @@ | ||
| 507 | 498 | $response[ $setting ] = 1; |
| 508 | 499 | break; |
| 509 | 500 | |
| 510 | 501 | default: |
| 511 | - $default = isset( $settings[ $setting ]['default'] ) ? $settings[ $setting ]['default'] : false; | |
| 502 | + $default = $settings[ $setting ]['default'] ?? false; | |
| 512 | 503 | $response[ $setting ] = Jetpack_Core_Json_Api_Endpoints::cast_value( get_option( $setting, $default ), $settings[ $setting ] ); |
| 513 | 504 | break; |
| 514 | 505 | } |
| 515 | 506 | } |
| @@ -515,25 +506,23 @@ | ||
| 515 | 506 | } |
| 516 | 507 | |
| 517 | 508 | $response['akismet'] = is_plugin_active( 'akismet/akismet.php' ); |
| 518 | 509 | |
| 510 | + require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php'; | |
| 511 | + if ( class_exists( 'Jetpack_Memberships' ) ) { | |
| 512 | + $response['newsletter_has_active_plan'] = count( Jetpack_Memberships::get_all_newsletter_plan_ids( false ) ) > 0; | |
| 513 | + } | |
| 514 | + | |
| 515 | + // Make sure we are returning a consistent type | |
| 516 | + if ( ! class_exists( 'Jetpack_Newsletter_Category_Helper' ) ) { | |
| 517 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-newsletter-category-helper.php'; | |
| 518 | + } | |
| 519 | + $response['wpcom_newsletter_categories'] = Jetpack_Newsletter_Category_Helper::get_category_ids(); | |
| 520 | + | |
| 519 | 521 | return rest_ensure_response( $response ); |
| 520 | 522 | } |
| 521 | 523 | |
| 522 | 524 | /** |
| 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 | 525 | * If it's a valid Jetpack module and configuration parameters have been sent, update it. |
| 537 | 526 | * |
| 538 | 527 | * @since 4.3.0 |
| 539 | 528 | * |
| @@ -542,9 +531,9 @@ | ||
| 542 | 531 | * |
| 543 | 532 | * @type string $slug Module slug. |
| 544 | 533 | * } |
| 545 | 534 | * |
| 546 | - * @return bool|WP_Error True if module was updated. Otherwise, a WP_Error instance with the corresponding error. | |
| 535 | + * @return bool|WP_REST_Response|WP_Error True or a WP_REST_Response if module was updated. Otherwise, a WP_Error instance with the corresponding error. | |
| 547 | 536 | */ |
| 548 | 537 | public function update_data( $request ) { |
| 549 | 538 | |
| 550 | 539 | // If it's null, we're trying to update many module options from different modules. |
| @@ -564,9 +553,9 @@ | ||
| 564 | 553 | } |
| 565 | 554 | |
| 566 | 555 | /* |
| 567 | 556 | * Get parameters to update the module. |
| 568 | - * We can not simply use $request->get_params() because when we registered this route, | |
| 557 | + * We cannot simply use $request->get_params() because when we registered this route, | |
| 569 | 558 | * we are adding the entire output of Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list() |
| 570 | 559 | * to the current request object's params. We are interested in body of the actual request. |
| 571 | 560 | * This may be JSON: |
| 572 | 561 | */ |
| @@ -651,13 +640,22 @@ | ||
| 651 | 640 | if ( ! $updated ) { |
| 652 | 641 | $not_updated[ $option ] = $error; |
| 653 | 642 | } |
| 654 | 643 | |
| 644 | + if ( $updated ) { | |
| 645 | + // Return the module state. | |
| 646 | + $response[ $option ] = $value; | |
| 647 | + } | |
| 648 | + | |
| 655 | 649 | // Remove module from list so we don't go through it again. |
| 656 | 650 | unset( $params[ $option ] ); |
| 657 | 651 | } |
| 658 | 652 | } |
| 659 | 653 | |
| 654 | + if ( ! class_exists( 'Jetpack_Newsletter_Category_Helper' ) ) { | |
| 655 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-newsletter-category-helper.php'; | |
| 656 | + } | |
| 657 | + | |
| 660 | 658 | foreach ( $params as $option => $value ) { |
| 661 | 659 | |
| 662 | 660 | // Used if there was an error. Can be overwritten with specific error messages. |
| 663 | 661 | $error = ''; |
| @@ -667,8 +665,14 @@ | ||
| 667 | 665 | |
| 668 | 666 | // Get option attributes, including the group it belongs to. |
| 669 | 667 | $option_attrs = $options[ $option ]; |
| 670 | 668 | |
| 669 | + // Everything outside the Post by Email group requires the admin capability. | |
| 670 | + if ( 'post-by-email' !== $option_attrs['jp_group'] && ! current_user_can( 'jetpack_configure_modules' ) ) { | |
| 671 | + $not_updated[ $option ] = REST_Connector::get_user_permissions_error_msg(); | |
| 672 | + continue; | |
| 673 | + } | |
| 674 | + | |
| 671 | 675 | // If this is a module option and the related module isn't active for any reason, continue with the next one. |
| 672 | 676 | if ( 'settings' !== $option_attrs['jp_group'] ) { |
| 673 | 677 | if ( ! Jetpack::is_module( $option_attrs['jp_group'] ) ) { |
| 674 | 678 | $not_updated[ $option ] = esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ); |
| @@ -719,8 +723,13 @@ | ||
| 719 | 723 | $updated = get_option( 'WPLANG' ) === $language ? true : update_option( 'WPLANG', $language ); |
| 720 | 724 | break; |
| 721 | 725 | |
| 722 | 726 | case 'monitor_receive_notifications': |
| 727 | + if ( ! class_exists( 'Jetpack_Monitor' ) ) { | |
| 728 | + $updated = false; | |
| 729 | + break; | |
| 730 | + } | |
| 731 | + | |
| 723 | 732 | $monitor = new Jetpack_Monitor(); |
| 724 | 733 | |
| 725 | 734 | // If we got true as response, consider it done. |
| 726 | 735 | $updated = true === $monitor->update_option_receive_jetpack_monitor_notification( $value ); |
| @@ -726,8 +735,13 @@ | ||
| 726 | 735 | $updated = true === $monitor->update_option_receive_jetpack_monitor_notification( $value ); |
| 727 | 736 | break; |
| 728 | 737 | |
| 729 | 738 | case 'post_by_email_address': |
| 739 | + if ( ! class_exists( 'Jetpack_Post_By_Email' ) ) { | |
| 740 | + $updated = false; | |
| 741 | + break; | |
| 742 | + } | |
| 743 | + | |
| 730 | 744 | $result = Jetpack_Post_By_Email::init()->process_api_request( $value ); |
| 731 | 745 | |
| 732 | 746 | // If we got an email address (create or regenerate) or 1 (delete), consider it done. |
| 733 | 747 | if ( is_string( $result ) && preg_match( '/[a-z0-9]+@post.wordpress.com/', $result ) ) { |
| @@ -748,9 +762,9 @@ | ||
| 748 | 762 | $result = false; |
| 749 | 763 | } |
| 750 | 764 | |
| 751 | 765 | // If we got one of Protect keys, consider it done. |
| 752 | - if ( preg_match( '/[a-z0-9]{40,}/i', $result ) ) { | |
| 766 | + if ( is_string( $result ) && preg_match( '/[a-z0-9]{40,}/i', $result ) ) { | |
| 753 | 767 | $response[ $option ] = $result; |
| 754 | 768 | $updated = true; |
| 755 | 769 | } |
| 756 | 770 | break; |
| @@ -814,8 +828,17 @@ | ||
| 814 | 828 | ? update_option( 'verification_services_codes', $grouped_options ) |
| 815 | 829 | : true; |
| 816 | 830 | break; |
| 817 | 831 | |
| 832 | + case Jetpack_SEO_Utils::FRONT_PAGE_META_OPTION: | |
| 833 | + Jetpack_SEO_Utils::update_front_page_meta_description( $value ); | |
| 834 | + $response[ $option ] = Jetpack_SEO_Utils::get_front_page_meta_description(); | |
| 835 | + // The helper returns an empty string for a successful clear or | |
| 836 | + // same-value write, so use its authoritative getter for the response | |
| 837 | + // and treat every valid request reaching this switch as handled. | |
| 838 | + $updated = true; | |
| 839 | + break; | |
| 840 | + | |
| 818 | 841 | case 'sharing_services': |
| 819 | 842 | if ( ! class_exists( 'Sharing_Service' ) && ! include_once JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) { |
| 820 | 843 | break; |
| 821 | 844 | } |
| @@ -963,32 +986,37 @@ | ||
| 963 | 986 | ? update_option( $option, (bool) $value ) |
| 964 | 987 | : true; |
| 965 | 988 | break; |
| 966 | 989 | |
| 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 | - } | |
| 990 | + case 'jetpack_subscriptions_reply_to': | |
| 991 | + // If option value was the same, consider it done. | |
| 992 | + require_once JETPACK__PLUGIN_DIR . 'modules/subscriptions/class-settings.php'; | |
| 993 | + $sub_value = Automattic\Jetpack\Modules\Subscriptions\Settings::is_valid_reply_to( $value ) | |
| 994 | + ? $value | |
| 995 | + : Automattic\Jetpack\Modules\Subscriptions\Settings::$default_reply_to; | |
| 996 | + | |
| 997 | + $updated = (string) get_option( $option ) !== (string) $sub_value ? update_option( $option, $sub_value ) : true; | |
| 981 | 998 | break; |
| 999 | + case 'jetpack_subscriptions_from_name': | |
| 1000 | + // If option value was the same, consider it done. | |
| 1001 | + $sub_value = sanitize_text_field( $value ); | |
| 1002 | + $updated = (string) get_option( $option ) !== (string) $sub_value ? update_option( $option, $sub_value ) : true; | |
| 1003 | + break; | |
| 982 | 1004 | |
| 983 | 1005 | case 'stb_enabled': |
| 984 | 1006 | case 'stc_enabled': |
| 985 | 1007 | case 'sm_enabled': |
| 1008 | + case 'jetpack_subscribe_overlay_enabled': | |
| 1009 | + case 'jetpack_subscribe_floating_button_enabled': | |
| 986 | 1010 | case 'wpcom_newsletter_categories_enabled': |
| 987 | 1011 | case 'wpcom_featured_image_in_email': |
| 1012 | + case 'jetpack_gravatar_in_email': | |
| 1013 | + case 'jetpack_author_in_email': | |
| 1014 | + case 'jetpack_post_date_in_email': | |
| 988 | 1015 | case 'wpcom_subscription_emails_use_excerpt': |
| 989 | 1016 | case 'jetpack_subscriptions_subscribe_post_end_enabled': |
| 990 | 1017 | case 'jetpack_subscriptions_login_navigation_enabled': |
| 1018 | + case 'jetpack_subscriptions_subscribe_navigation_enabled': | |
| 991 | 1019 | // Convert the false value to 0. This allows the option to be updated if it doesn't exist yet. |
| 992 | 1020 | $sub_value = $value ? $value : 0; |
| 993 | 1021 | $updated = (string) get_option( $option ) !== (string) $sub_value ? update_option( $option, $sub_value ) : true; |
| 994 | 1022 | break; |
| @@ -1001,9 +1029,9 @@ | ||
| 1001 | 1029 | if ( ! is_array( $value ) ) { |
| 1002 | 1030 | break; |
| 1003 | 1031 | } |
| 1004 | 1032 | |
| 1005 | - $allowed_keys = array( 'invitation', 'comment_follow', 'welcome' ); | |
| 1033 | + $allowed_keys = array( 'invitation', 'comment_follow', 'welcome', 'subscribe_modal_heading', 'free_tier_description', 'hide_free_tier' ); | |
| 1006 | 1034 | $filtered_value = array_filter( |
| 1007 | 1035 | $value, |
| 1008 | 1036 | function ( $key ) use ( $allowed_keys ) { |
| 1009 | 1037 | return in_array( $key, $allowed_keys, true ); |
| @@ -1014,8 +1042,17 @@ | ||
| 1014 | 1042 | if ( empty( $filtered_value ) ) { |
| 1015 | 1043 | break; |
| 1016 | 1044 | } |
| 1017 | 1045 | |
| 1046 | + // `hide_free_tier` is a boolean flag, so pull it out before the HTML | |
| 1047 | + // sanitization below (which expects strings). Sanitize it with | |
| 1048 | + // rest_sanitize_boolean() so stringy booleans (e.g. "false", "0") | |
| 1049 | + // are interpreted correctly rather than being treated as truthy by a | |
| 1050 | + // plain `! empty()`. | |
| 1051 | + $has_hide_free_tier = array_key_exists( 'hide_free_tier', $filtered_value ); | |
| 1052 | + $hide_free_tier = $has_hide_free_tier && rest_sanitize_boolean( $filtered_value['hide_free_tier'] ); | |
| 1053 | + unset( $filtered_value['hide_free_tier'] ); | |
| 1054 | + | |
| 1018 | 1055 | array_walk_recursive( |
| 1019 | 1056 | $filtered_value, |
| 1020 | 1057 | function ( &$value ) { |
| 1021 | 1058 | $value = wp_kses( |
| @@ -1020,9 +1057,15 @@ | ||
| 1020 | 1057 | function ( &$value ) { |
| 1021 | 1058 | $value = wp_kses( |
| 1022 | 1059 | $value, |
| 1023 | 1060 | array( |
| 1024 | - 'a' => array( | |
| 1061 | + 'ul' => array(), | |
| 1062 | + 'li' => array(), | |
| 1063 | + 'p' => array(), | |
| 1064 | + 'strong' => array(), | |
| 1065 | + 'ol' => array(), | |
| 1066 | + 'em' => array(), | |
| 1067 | + 'a' => array( | |
| 1025 | 1068 | 'href' => array(), |
| 1026 | 1069 | ), |
| 1027 | 1070 | ) |
| 1028 | 1071 | ); |
| @@ -1028,19 +1071,72 @@ | ||
| 1028 | 1071 | ); |
| 1029 | 1072 | } |
| 1030 | 1073 | ); |
| 1031 | 1074 | |
| 1075 | + // Normalize whitespace-only `subscribe_modal_heading` input to empty so | |
| 1076 | + // the modal template's `empty()` fallback fires. PHP's `empty()` treats | |
| 1077 | + // `" "` as non-empty, which would otherwise render a blank heading. | |
| 1078 | + if ( isset( $filtered_value['subscribe_modal_heading'] ) ) { | |
| 1079 | + $filtered_value['subscribe_modal_heading'] = trim( $filtered_value['subscribe_modal_heading'] ); | |
| 1080 | + } | |
| 1081 | + | |
| 1082 | + // The free tier description is stored as plain markdown source, so strip | |
| 1083 | + // all HTML and cap its length to match the paid-tier description field. | |
| 1084 | + // WordPress core guarantees mb_substr() (polyfilled in wp-includes/compat.php | |
| 1085 | + // when the mbstring extension is unavailable), so it's safe to use directly. | |
| 1086 | + // A JSON payload could supply a non-scalar (array/object) for this field, | |
| 1087 | + // which would fatal in wp_kses()/mb_substr() on PHP 8+, so drop invalid values. | |
| 1088 | + if ( isset( $filtered_value['free_tier_description'] ) ) { | |
| 1089 | + if ( is_scalar( $filtered_value['free_tier_description'] ) ) { | |
| 1090 | + $filtered_value['free_tier_description'] = mb_substr( wp_kses( (string) $filtered_value['free_tier_description'], array() ), 0, 500 ); | |
| 1091 | + } else { | |
| 1092 | + unset( $filtered_value['free_tier_description'] ); | |
| 1093 | + } | |
| 1094 | + } | |
| 1095 | + | |
| 1096 | + if ( $has_hide_free_tier ) { | |
| 1097 | + $filtered_value['hide_free_tier'] = $hide_free_tier; | |
| 1098 | + } | |
| 1099 | + | |
| 1032 | 1100 | $old_subscription_options = get_option( 'subscription_options' ); |
| 1033 | 1101 | if ( ! is_array( $old_subscription_options ) ) { |
| 1034 | 1102 | $old_subscription_options = array(); |
| 1035 | 1103 | } |
| 1036 | 1104 | $new_subscription_options = array_merge( $old_subscription_options, $filtered_value ); |
| 1105 | + $updated = true; | |
| 1037 | 1106 | |
| 1038 | - if ( update_option( $option, $new_subscription_options ) ) { | |
| 1039 | - $updated[ $option ] = true; | |
| 1107 | + if ( serialize( $old_subscription_options ) === serialize( $new_subscription_options ) ) { // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize | |
| 1108 | + break; // This prevents the option update to fail when the values are the same. | |
| 1040 | 1109 | } |
| 1110 | + | |
| 1111 | + if ( ! update_option( $option, $new_subscription_options ) ) { | |
| 1112 | + $updated = false; | |
| 1113 | + $error = esc_html__( 'Subscription Options failed to process.', 'jetpack' ); | |
| 1114 | + } | |
| 1041 | 1115 | break; |
| 1042 | 1116 | |
| 1117 | + case Jetpack_Newsletter_Category_Helper::NEWSLETTER_CATEGORIES_OPTION: | |
| 1118 | + if ( ! is_array( $value ) || empty( $value ) ) { | |
| 1119 | + $updated = true; | |
| 1120 | + break; | |
| 1121 | + } | |
| 1122 | + | |
| 1123 | + // If we are already current, do nothing | |
| 1124 | + $current_value = Jetpack_Newsletter_Category_Helper::get_category_ids(); | |
| 1125 | + if ( $value === $current_value ) { | |
| 1126 | + $updated = true; | |
| 1127 | + break; | |
| 1128 | + } | |
| 1129 | + | |
| 1130 | + if ( Jetpack_Newsletter_Category_Helper::save_category_ids( $value ) ) { | |
| 1131 | + $updated = true; | |
| 1132 | + } else { | |
| 1133 | + $updated = false; | |
| 1134 | + $error = esc_html__( 'Newsletter category did not update.', 'jetpack' ); | |
| 1135 | + } | |
| 1136 | + | |
| 1137 | + break; | |
| 1138 | + | |
| 1043 | 1139 | default: |
| 1044 | 1140 | // Boolean values are stored as 1 or 0. |
| 1045 | 1141 | if ( isset( $options[ $option ]['type'] ) && 'boolean' === $options[ $option ]['type'] ) { |
| 1046 | 1142 | $value = (int) $value; |
| @@ -1046,9 +1142,9 @@ | ||
| 1046 | 1142 | $value = (int) $value; |
| 1047 | 1143 | } |
| 1048 | 1144 | |
| 1049 | 1145 | // If option value was the same as it's current value, or it's default, consider it done. |
| 1050 | - $default = isset( $options[ $option ]['default'] ) ? $options[ $option ]['default'] : false; | |
| 1146 | + $default = $options[ $option ]['default'] ?? false; | |
| 1051 | 1147 | $updated = get_option( $option, $default ) != $value // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual -- ensure we support scalars or strings saved by update_option. |
| 1052 | 1148 | ? update_option( $option, $value ) |
| 1053 | 1149 | : true; |
| 1054 | 1150 | break; |
| @@ -1102,218 +1198,30 @@ | ||
| 1102 | 1198 | * Perform tasks in the site based on onboarding choices. |
| 1103 | 1199 | * |
| 1104 | 1200 | * @since 5.4.0 |
| 1105 | 1201 | * |
| 1202 | + * @deprecated since 13.9 | |
| 1203 | + * | |
| 1106 | 1204 | * @param array $data Onboarding choices made by user. |
| 1107 | 1205 | * |
| 1108 | 1206 | * @return string Result of onboarding processing and, if there is one, an error message. |
| 1109 | 1207 | */ |
| 1110 | - private function process_onboarding( $data ) { | |
| 1111 | - if ( isset( $data['end'] ) && $data['end'] ) { | |
| 1112 | - return Jetpack::invalidate_onboarding_token() | |
| 1113 | - ? '' | |
| 1114 | - : esc_html__( "The onboarding token couldn't be deleted.", 'jetpack' ); | |
| 1115 | - } | |
| 1116 | - | |
| 1117 | - $error = array(); | |
| 1118 | - | |
| 1119 | - if ( ! empty( $data['siteTitle'] ) ) { | |
| 1120 | - // If option value was the same, consider it done. | |
| 1121 | - if ( ! ( | |
| 1122 | - update_option( 'blogname', $data['siteTitle'] ) | |
| 1123 | - || get_option( 'blogname' ) === $data['siteTitle'] | |
| 1124 | - ) ) { | |
| 1125 | - $error[] = 'siteTitle'; | |
| 1126 | - } | |
| 1127 | - } | |
| 1128 | - | |
| 1129 | - if ( isset( $data['siteDescription'] ) ) { | |
| 1130 | - // If option value was the same, consider it done. | |
| 1131 | - if ( ! ( | |
| 1132 | - update_option( 'blogdescription', $data['siteDescription'] ) | |
| 1133 | - || get_option( 'blogdescription' ) === $data['siteDescription'] | |
| 1134 | - ) ) { | |
| 1135 | - $error[] = 'siteDescription'; | |
| 1136 | - } | |
| 1137 | - } | |
| 1138 | - | |
| 1139 | - $site_title = get_option( 'blogname' ); | |
| 1140 | - $author = get_current_user_id() || 1; | |
| 1141 | - | |
| 1142 | - if ( ! empty( $data['siteType'] ) ) { | |
| 1143 | - if ( ! ( | |
| 1144 | - update_option( 'jpo_site_type', $data['siteType'] ) | |
| 1145 | - || get_option( 'jpo_site_type' ) === $data['siteType'] | |
| 1146 | - ) ) { | |
| 1147 | - $error[] = 'siteType'; | |
| 1148 | - } | |
| 1149 | - } | |
| 1150 | - | |
| 1151 | - if ( isset( $data['homepageFormat'] ) ) { | |
| 1152 | - /* | |
| 1153 | - * If $data['homepageFormat'] is 'posts', | |
| 1154 | - * we have nothing to do since it's WordPress' default | |
| 1155 | - * if it exists, just update | |
| 1156 | - */ | |
| 1157 | - $homepage_format = get_option( 'jpo_homepage_format' ); | |
| 1158 | - if ( ! $homepage_format || $homepage_format !== $data['homepageFormat'] ) { | |
| 1159 | - if ( 'page' === $data['homepageFormat'] ) { | |
| 1160 | - if ( ! ( | |
| 1161 | - update_option( 'show_on_front', 'page' ) | |
| 1162 | - || get_option( 'show_on_front' ) === 'page' | |
| 1163 | - ) ) { | |
| 1164 | - $error[] = 'homepageFormat'; | |
| 1165 | - } | |
| 1166 | - | |
| 1167 | - $home = wp_insert_post( | |
| 1168 | - array( | |
| 1169 | - 'post_type' => 'page', | |
| 1170 | - /* translators: this references the home page of a site, also called front page. */ | |
| 1171 | - 'post_title' => esc_html_x( 'Home Page', 'The home page of a website.', 'jetpack' ), | |
| 1172 | - 'post_content' => sprintf( | |
| 1173 | - /* Translators: placeholder is the site title. */ | |
| 1174 | - esc_html__( 'Welcome to %s.', 'jetpack' ), | |
| 1175 | - $site_title | |
| 1176 | - ), | |
| 1177 | - 'post_status' => 'publish', | |
| 1178 | - 'post_author' => $author, | |
| 1179 | - ) | |
| 1180 | - ); | |
| 1181 | - if ( 0 === $home ) { | |
| 1182 | - $error[] = 'home insert: 0'; | |
| 1183 | - } elseif ( is_wp_error( $home ) ) { | |
| 1184 | - $error[] = 'home creation: ' . $home->get_error_message(); | |
| 1185 | - } | |
| 1186 | - if ( ! ( | |
| 1187 | - update_option( 'page_on_front', $home ) | |
| 1188 | - || get_option( 'page_on_front' ) === $home | |
| 1189 | - ) ) { | |
| 1190 | - | |
| 1191 | - $error[] = 'home set'; | |
| 1192 | - } | |
| 1193 | - | |
| 1194 | - $blog = wp_insert_post( | |
| 1195 | - array( | |
| 1196 | - 'post_type' => 'page', | |
| 1197 | - /* translators: this references the page where blog posts are listed. */ | |
| 1198 | - 'post_title' => esc_html_x( 'Blog', 'The blog of a website.', 'jetpack' ), | |
| 1199 | - 'post_content' => sprintf( | |
| 1200 | - /* Translators: placeholder is the site title. */ | |
| 1201 | - esc_html__( 'These are the latest posts in %s.', 'jetpack' ), | |
| 1202 | - $site_title | |
| 1203 | - ), | |
| 1204 | - 'post_status' => 'publish', | |
| 1205 | - 'post_author' => $author, | |
| 1206 | - ) | |
| 1207 | - ); | |
| 1208 | - if ( 0 === $blog ) { | |
| 1209 | - $error[] = 'blog insert: 0'; | |
| 1210 | - } elseif ( is_wp_error( $blog ) ) { | |
| 1211 | - $error[] = 'blog creation: ' . $blog->get_error_message(); | |
| 1212 | - } | |
| 1213 | - if ( ! ( | |
| 1214 | - update_option( 'page_for_posts', $blog ) | |
| 1215 | - || get_option( 'page_for_posts' ) === $blog | |
| 1216 | - ) ) { | |
| 1217 | - $error[] = 'blog set'; | |
| 1218 | - } | |
| 1219 | - } else { | |
| 1220 | - $front_page = get_option( 'page_on_front' ); | |
| 1221 | - $posts_page = get_option( 'page_for_posts' ); | |
| 1222 | - if ( $posts_page && get_post( $posts_page ) ) { | |
| 1223 | - wp_delete_post( $posts_page ); | |
| 1224 | - } | |
| 1225 | - if ( $front_page && get_post( $front_page ) ) { | |
| 1226 | - wp_delete_post( $front_page ); | |
| 1227 | - } | |
| 1228 | - update_option( 'show_on_front', 'posts' ); | |
| 1229 | - } | |
| 1230 | - } | |
| 1231 | - update_option( 'jpo_homepage_format', $data['homepageFormat'] ); | |
| 1232 | - } | |
| 1233 | - | |
| 1234 | - // Setup contact page and add a form and/or business info. | |
| 1235 | - $contact_page = ''; | |
| 1236 | - if ( ! empty( $data['addContactForm'] ) && ! get_option( 'jpo_contact_page' ) ) { | |
| 1237 | - $contact_form_module_active = Jetpack::is_module_active( 'contact-form' ); | |
| 1238 | - if ( ! $contact_form_module_active ) { | |
| 1239 | - $contact_form_module_active = Jetpack::activate_module( 'contact-form', false, false ); | |
| 1240 | - } | |
| 1241 | - | |
| 1242 | - if ( $contact_form_module_active ) { | |
| 1243 | - $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]'; | |
| 1244 | - } else { | |
| 1245 | - $error[] = 'contact-form activate'; | |
| 1246 | - } | |
| 1247 | - } | |
| 1248 | - | |
| 1249 | - if ( isset( $data['businessPersonal'] ) && 'business' === $data['businessPersonal'] ) { | |
| 1250 | - $contact_page .= "\n" . implode( "\n", $data['businessInfo'] ); | |
| 1251 | - } | |
| 1252 | - | |
| 1253 | - if ( ! empty( $contact_page ) ) { | |
| 1254 | - $form = wp_insert_post( | |
| 1255 | - array( | |
| 1256 | - 'post_type' => 'page', | |
| 1257 | - /* translators: this references a page with contact details and possibly a form. */ | |
| 1258 | - 'post_title' => esc_html_x( 'Contact us', 'Contact page for your website.', 'jetpack' ), | |
| 1259 | - 'post_content' => esc_html__( 'Send us a message!', 'jetpack' ) . "\n" . $contact_page, | |
| 1260 | - 'post_status' => 'publish', | |
| 1261 | - 'post_author' => $author, | |
| 1262 | - ) | |
| 1263 | - ); | |
| 1264 | - if ( 0 === $form ) { | |
| 1265 | - $error[] = 'form insert: 0'; | |
| 1266 | - } elseif ( is_wp_error( $form ) ) { | |
| 1267 | - $error[] = 'form creation: ' . $form->get_error_message(); | |
| 1268 | - } else { | |
| 1269 | - update_option( 'jpo_contact_page', $form ); | |
| 1270 | - } | |
| 1271 | - } | |
| 1272 | - | |
| 1273 | - if ( isset( $data['businessAddress'] ) ) { | |
| 1274 | - $handled_business_address = self::handle_business_address( $data['businessAddress'] ); | |
| 1275 | - if ( is_wp_error( $handled_business_address ) ) { | |
| 1276 | - $error[] = 'BusinessAddress'; | |
| 1277 | - } | |
| 1278 | - } | |
| 1279 | - | |
| 1280 | - if ( ! empty( $data['installWooCommerce'] ) ) { | |
| 1281 | - $wc_install_result = Plugins_Installer::install_and_activate_plugin( 'woocommerce' ); | |
| 1282 | - delete_transient( '_wc_activation_redirect' ); // Redirecting to WC setup would kill our users' flow. | |
| 1283 | - if ( is_wp_error( $wc_install_result ) ) { | |
| 1284 | - $error[] = 'woocommerce installation'; | |
| 1285 | - } | |
| 1286 | - } | |
| 1287 | - | |
| 1288 | - if ( ! empty( $data['stats'] ) ) { | |
| 1289 | - if ( Jetpack::is_connection_ready() ) { | |
| 1290 | - $stats_module_active = Jetpack::is_module_active( 'stats' ); | |
| 1291 | - if ( ! $stats_module_active ) { | |
| 1292 | - $stats_module_active = Jetpack::activate_module( 'stats', false, false ); | |
| 1293 | - } | |
| 1294 | - | |
| 1295 | - if ( ! $stats_module_active ) { | |
| 1296 | - $error[] = 'stats activate'; | |
| 1297 | - } | |
| 1298 | - } else { | |
| 1299 | - $error[] = 'stats not connected'; | |
| 1300 | - } | |
| 1301 | - } | |
| 1302 | - | |
| 1303 | - return empty( $error ) | |
| 1304 | - ? '' | |
| 1305 | - : implode( ', ', $error ); | |
| 1208 | + private function process_onboarding( $data ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 1209 | + _deprecated_function( __METHOD__, '13.9' ); | |
| 1210 | + return ''; | |
| 1306 | 1211 | } |
| 1307 | 1212 | |
| 1308 | 1213 | /** |
| 1309 | 1214 | * Add or update Business Address widget. |
| 1310 | 1215 | * |
| 1216 | + * @deprecated since 13.9 | |
| 1217 | + * | |
| 1311 | 1218 | * @param array $address Array of business address fields. |
| 1312 | 1219 | * |
| 1313 | 1220 | * @return WP_Error|true True if the data was saved correctly. |
| 1314 | 1221 | */ |
| 1315 | 1222 | private static function handle_business_address( $address ) { |
| 1223 | + _deprecated_function( __METHOD__, '13.9' ); | |
| 1316 | 1224 | $first_sidebar = Jetpack_Widgets::get_first_sidebar(); |
| 1317 | 1225 | |
| 1318 | 1226 | $widgets_module_active = Jetpack::is_module_active( 'widgets' ); |
| 1319 | 1227 | if ( ! $widgets_module_active ) { |
| @@ -1397,13 +1305,8 @@ | ||
| 1397 | 1305 | * |
| 1398 | 1306 | * @return bool |
| 1399 | 1307 | */ |
| 1400 | 1308 | public function can_request( $request ) { |
| 1401 | - $req_params = $request->get_params(); | |
| 1402 | - if ( ! empty( $req_params['onboarding']['token'] ) && isset( $req_params['rest_route'] ) ) { | |
| 1403 | - return Jetpack::validate_onboarding_token_action( $req_params['onboarding']['token'], $req_params['rest_route'] ); | |
| 1404 | - } | |
| 1405 | - | |
| 1406 | 1309 | if ( 'GET' === $request->get_method() ) { |
| 1407 | 1310 | return current_user_can( 'jetpack_admin_page' ); |
| 1408 | 1311 | } else { |
| 1409 | 1312 | $module = Jetpack_Core_Json_Api_Endpoints::get_module_requested(); |
| @@ -1412,13 +1315,13 @@ | ||
| 1412 | 1315 | if ( ! is_array( $params ) ) { |
| 1413 | 1316 | $params = $request->get_body_params(); |
| 1414 | 1317 | } |
| 1415 | 1318 | $options = Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list( $params ); |
| 1416 | - foreach ( $options as $option => $definition ) { | |
| 1417 | - if ( in_array( $options[ $option ]['jp_group'], array( 'post-by-email' ), true ) ) { | |
| 1418 | - $module = $options[ $option ]['jp_group']; | |
| 1419 | - break; | |
| 1420 | - } | |
| 1319 | + | |
| 1320 | + // The Post by Email gate applies only when the request contains nothing else. | |
| 1321 | + $groups = array_values( array_unique( array_column( $options, 'jp_group' ) ) ); | |
| 1322 | + if ( array( 'post-by-email' ) === $groups ) { | |
| 1323 | + $module = 'post-by-email'; | |
| 1421 | 1324 | } |
| 1422 | 1325 | } |
| 1423 | 1326 | // User is trying to create, regenerate or delete its PbE. |
| 1424 | 1327 | if ( 'post-by-email' === $module ) { |
| @@ -1434,9 +1337,8 @@ | ||
| 1434 | 1337 | * |
| 1435 | 1338 | * phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound |
| 1436 | 1339 | */ |
| 1437 | 1340 | class Jetpack_Core_API_Module_Data_Endpoint { |
| 1438 | - // phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound | |
| 1439 | 1341 | |
| 1440 | 1342 | /** |
| 1441 | 1343 | * Process request and return different data based on the module we are interested in. |
| 1442 | 1344 | * |
| @@ -1636,11 +1538,9 @@ | ||
| 1636 | 1538 | array( |
| 1637 | 1539 | 'general' => $initial_stats, |
| 1638 | 1540 | |
| 1639 | 1541 | // Build data for 'day' as if it was $wpcom_stats ->get_visits( array( 'unit' => 'day, 'quantity' => 30). |
| 1640 | - 'day' => isset( $initial_stats->visits ) | |
| 1641 | - ? $initial_stats->visits | |
| 1642 | - : array(), | |
| 1542 | + 'day' => $initial_stats->visits ?? array(), | |
| 1643 | 1543 | ) |
| 1644 | 1544 | ); |
| 1645 | 1545 | case 'week': |
| 1646 | 1546 | return rest_ensure_response( |
| @@ -1781,9 +1681,9 @@ | ||
| 1781 | 1681 | $last_service = $copy_services[ $last ]; |
| 1782 | 1682 | unset( $copy_services[ $last ] ); |
| 1783 | 1683 | $message = esc_html( |
| 1784 | 1684 | sprintf( |
| 1785 | - /* translators: %1$s is a comma separated list of services, and %2$s is a single service name like Google, Bing, Pinterest, etc. */ | |
| 1685 | + /* translators: %1$s is a comma-separated list of services, and %2$s is a single service name like Google, Bing, Pinterest, etc. */ | |
| 1786 | 1686 | __( 'Your site is verified with %1$s and %2$s.', 'jetpack' ), |
| 1787 | 1687 | implode( ', ', $copy_services ), |
| 1788 | 1688 | $last_service |
| 1789 | 1689 | ) |