PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
← All changes | _inc/lib/core-api/class.jetpack-core-api-module-endpoints.php +269 -292 12.0.3 → 16.3-a.7 View file →
@@ -5,9 +5,9 @@
5 5 * @package automattic/jetpack
6 6 */
7 7
8 8 use Automattic\Jetpack\Connection\REST_Connector;
9 -use Automattic\Jetpack\Plugins_Installer;
9 +use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
10 10 use Automattic\Jetpack\Stats\WPCOM_Stats;
11 11 use Automattic\Jetpack\Stats_Admin\Main as Stats_Admin_Main;
12 12 use Automattic\Jetpack\Status;
13 13 use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection;
@@ -12,13 +12,16 @@
12 12 use Automattic\Jetpack\Status;
13 13 use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection;
14 14 use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection_Shared_Functions;
15 15
16 +if ( ! defined( 'ABSPATH' ) ) {
17 + exit( 0 );
18 +}
19 +
16 20 /**
17 21 * This is the base class for every Core API endpoint Jetpack uses.
18 22 */
19 -class Jetpack_Core_API_Module_Toggle_Endpoint
20 - extends Jetpack_Core_API_XMLRPC_Consumer_Endpoint {
23 +class Jetpack_Core_API_Module_Toggle_Endpoint extends Jetpack_Core_API_XMLRPC_Consumer_Endpoint {
21 24
22 25 /**
23 26 * Check if the module requires the site to be publicly accessible from WPCOM.
24 27 * If the site meets this requirement, the module is activated. Otherwise an error is returned.
@@ -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' ),
@@ -195,11 +217,11 @@
195 217 * @return bool|Array|WP_Error a resulting value or object, or an error.
196 218 */
197 219 public function process( $request ) {
198 220 if ( 'GET' === $request->get_method() ) {
199 - return $this->get_modules( $request );
221 + return $this->get_modules();
200 222 } else {
201 - return $this->activate_modules( $request );
223 + return static::activate_modules( $request );
202 224 }
203 225 }
204 226
205 227 /**
@@ -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 }
@@ -283,9 +305,9 @@
283 305 $activated_last = array_pop( $activated );
284 306 $activated_text = $activated_count > 1 ? sprintf(
285 307 /* Translators: first variable is a list followed by the last item, which is the second variable. Example: dog, cat and bird. */
286 308 __( '%1$s and %2$s', 'jetpack' ),
287 - join( ', ', $activated ),
309 + implode( ', ', $activated ),
288 310 $activated_last
289 311 ) : $activated_last;
290 312
291 313 $error = sprintf(
@@ -300,9 +322,9 @@
300 322 $failed_last = array_pop( $failed );
301 323 $failed_text = $failed_count > 1 ? sprintf(
302 324 /* Translators: first variable is a list followed by the last item, which is the second variable. Example: dog, cat and bird. */
303 325 __( '%1$s and %2$s', 'jetpack' ),
304 - join( ', ', $failed ),
326 + implode( ', ', $failed ),
305 327 $failed_last
306 328 ) : $failed_last;
307 329
308 330 $error = sprintf(
@@ -402,14 +424,19 @@
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'];
437 + if ( isset( $module['module_tags'] ) ) {
438 + $module['module_tags'] = array_map( 'jetpack_get_module_i18n_tag', $module['module_tags'] );
412 439 }
413 440
414 441 return Jetpack_Core_Json_Api_Endpoints::prepare_modules_for_response( $module );
415 442 }
@@ -444,14 +471,18 @@
444 471 $response[ $module ] = Jetpack::is_module_active( $module );
445 472 }
446 473 }
447 474
448 - $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 + );
449 478
450 479 if ( ! function_exists( 'is_plugin_active' ) ) {
451 480 require_once ABSPATH . 'wp-admin/includes/plugin.php';
452 481 }
453 482
483 + $response['categories'] = get_categories( array( 'get' => 'all' ) );
484 +
454 485 foreach ( $settings as $setting => $properties ) {
455 486 switch ( $setting ) {
456 487 case 'lang_id':
457 488 if ( ! current_user_can( 'install_languages' ) ) {
@@ -480,24 +511,8 @@
480 511 $response[ $setting ] = class_exists( 'Akismet' ) ? Akismet::get_api_key() : '';
481 512 }
482 513 break;
483 514
484 - case 'onboarding':
485 - $business_address = get_option( 'jpo_business_address' );
486 - $business_address = is_array( $business_address ) ? array_map( array( $this, 'decode_special_characters' ), $business_address ) : $business_address;
487 -
488 - $response[ $setting ] = array(
489 - 'siteTitle' => $this->decode_special_characters( get_option( 'blogname' ) ),
490 - 'siteDescription' => $this->decode_special_characters( get_option( 'blogdescription' ) ),
491 - 'siteType' => get_option( 'jpo_site_type' ),
492 - 'homepageFormat' => get_option( 'jpo_homepage_format' ),
493 - 'addContactForm' => (int) get_option( 'jpo_contact_page' ),
494 - 'businessAddress' => $business_address,
495 - 'installWooCommerce' => is_plugin_active( 'woocommerce/woocommerce.php' ),
496 - 'stats' => Jetpack::is_connection_ready() && Jetpack::is_module_active( 'stats' ),
497 - );
498 - break;
499 -
500 515 case 'search_auto_config':
501 516 // Only writable.
502 517 $response[ $setting ] = 1;
503 518 break;
@@ -502,9 +517,10 @@
502 517 $response[ $setting ] = 1;
503 518 break;
504 519
505 520 default:
506 - $response[ $setting ] = Jetpack_Core_Json_Api_Endpoints::cast_value( get_option( $setting ), $settings[ $setting ] );
521 + $default = $settings[ $setting ]['default'] ?? false;
522 + $response[ $setting ] = Jetpack_Core_Json_Api_Endpoints::cast_value( get_option( $setting, $default ), $settings[ $setting ] );
507 523 break;
508 524 }
509 525 }
510 526
@@ -509,25 +525,23 @@
509 525 }
510 526
511 527 $response['akismet'] = is_plugin_active( 'akismet/akismet.php' );
512 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 +
513 540 return rest_ensure_response( $response );
514 541 }
515 542
516 543 /**
517 - * Decode the special HTML characters in a certain value.
518 - *
519 - * @since 5.8
520 - *
521 - * @param string $value Value to decode.
522 - *
523 - * @return string Value with decoded HTML characters.
524 - */
525 - private function decode_special_characters( $value ) {
526 - return (string) htmlspecialchars_decode( $value, ENT_QUOTES );
527 - }
528 -
529 - /**
530 544 * If it's a valid Jetpack module and configuration parameters have been sent, update it.
531 545 *
532 546 * @since 4.3.0
533 547 *
@@ -536,9 +550,9 @@
536 550 *
537 551 * @type string $slug Module slug.
538 552 * }
539 553 *
540 - * @return bool|WP_Error True if module was updated. Otherwise, a WP_Error instance with the corresponding error.
554 + * @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.
541 555 */
542 556 public function update_data( $request ) {
543 557
544 558 // If it's null, we're trying to update many module options from different modules.
@@ -558,9 +572,9 @@
558 572 }
559 573
560 574 /*
561 575 * Get parameters to update the module.
562 - * 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,
563 577 * we are adding the entire output of Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list()
564 578 * to the current request object's params. We are interested in body of the actual request.
565 579 * This may be JSON:
566 580 */
@@ -645,13 +659,22 @@
645 659 if ( ! $updated ) {
646 660 $not_updated[ $option ] = $error;
647 661 }
648 662
663 + if ( $updated ) {
664 + // Return the module state.
665 + $response[ $option ] = $value;
666 + }
667 +
649 668 // Remove module from list so we don't go through it again.
650 669 unset( $params[ $option ] );
651 670 }
652 671 }
653 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 +
654 677 foreach ( $params as $option => $value ) {
655 678
656 679 // Used if there was an error. Can be overwritten with specific error messages.
657 680 $error = '';
@@ -661,8 +684,14 @@
661 684
662 685 // Get option attributes, including the group it belongs to.
663 686 $option_attrs = $options[ $option ];
664 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 +
665 694 // If this is a module option and the related module isn't active for any reason, continue with the next one.
666 695 if ( 'settings' !== $option_attrs['jp_group'] ) {
667 696 if ( ! Jetpack::is_module( $option_attrs['jp_group'] ) ) {
668 697 $not_updated[ $option ] = esc_html__( 'The requested Jetpack module was not found.', 'jetpack' );
@@ -713,8 +742,13 @@
713 742 $updated = get_option( 'WPLANG' ) === $language ? true : update_option( 'WPLANG', $language );
714 743 break;
715 744
716 745 case 'monitor_receive_notifications':
746 + if ( ! class_exists( 'Jetpack_Monitor' ) ) {
747 + $updated = false;
748 + break;
749 + }
750 +
717 751 $monitor = new Jetpack_Monitor();
718 752
719 753 // If we got true as response, consider it done.
720 754 $updated = true === $monitor->update_option_receive_jetpack_monitor_notification( $value );
@@ -720,8 +754,13 @@
720 754 $updated = true === $monitor->update_option_receive_jetpack_monitor_notification( $value );
721 755 break;
722 756
723 757 case 'post_by_email_address':
758 + if ( ! class_exists( 'Jetpack_Post_By_Email' ) ) {
759 + $updated = false;
760 + break;
761 + }
762 +
724 763 $result = Jetpack_Post_By_Email::init()->process_api_request( $value );
725 764
726 765 // If we got an email address (create or regenerate) or 1 (delete), consider it done.
727 766 if ( is_string( $result ) && preg_match( '/[a-z0-9][email protected]/', $result ) ) {
@@ -742,9 +781,9 @@
742 781 $result = false;
743 782 }
744 783
745 784 // If we got one of Protect keys, consider it done.
746 - if ( preg_match( '/[a-z0-9]{40,}/i', $result ) ) {
785 + if ( is_string( $result ) && preg_match( '/[a-z0-9]{40,}/i', $result ) ) {
747 786 $response[ $option ] = $result;
748 787 $updated = true;
749 788 }
750 789 break;
@@ -795,15 +834,16 @@
795 834 case 'facebook':
796 835 $grouped_options_current = (array) get_option( 'verification_services_codes' );
797 836 $grouped_options = $grouped_options_current;
798 837
799 - // Extracts the content attribute from the HTML meta tag if needed.
800 - if ( preg_match( '#.*<meta name="(?:[^"]+)" content="([^"]+)" />.*#i', $value, $matches ) ) {
801 - $grouped_options[ $option ] = $matches[1];
802 - } else {
803 - $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;
804 842 }
805 843
844 + $grouped_options[ $option ] = $validated_code;
845 +
806 846 // If option value was the same, consider it done.
807 847 $updated = $grouped_options_current !== $grouped_options
808 848 ? update_option( 'verification_services_codes', $grouped_options )
809 849 : true;
@@ -808,8 +848,17 @@
808 848 ? update_option( 'verification_services_codes', $grouped_options )
809 849 : true;
810 850 break;
811 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 +
812 861 case 'sharing_services':
813 862 if ( ! class_exists( 'Sharing_Service' ) && ! include_once JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) {
814 863 break;
815 864 }
@@ -957,26 +1006,37 @@
957 1006 ? update_option( $option, (bool) $value )
958 1007 : true;
959 1008 break;
960 1009
961 - case 'onboarding':
962 - require_once JETPACK__PLUGIN_DIR . '_inc/lib/widgets.php';
963 - // Break apart and set Jetpack onboarding options.
964 - $result = $this->process_onboarding( (array) $value );
965 - if ( empty( $result ) ) {
966 - $updated = true;
967 - } else {
968 - $error = sprintf(
969 - /* Translators: placeholder is a list of error codes. */
970 - esc_html__( 'Onboarding failed to process: %s', 'jetpack' ),
971 - $result
972 - );
973 - $updated = false;
974 - }
1010 + case 'jetpack_subscriptions_reply_to':
1011 + // If option value was the same, consider it done.
1012 + require_once JETPACK__PLUGIN_DIR . 'modules/subscriptions/class-settings.php';
1013 + $sub_value = Automattic\Jetpack\Modules\Subscriptions\Settings::is_valid_reply_to( $value )
1014 + ? $value
1015 + : Automattic\Jetpack\Modules\Subscriptions\Settings::$default_reply_to;
1016 +
1017 + $updated = (string) get_option( $option ) !== (string) $sub_value ? update_option( $option, $sub_value ) : true;
975 1018 break;
1019 + case 'jetpack_subscriptions_from_name':
1020 + // If option value was the same, consider it done.
1021 + $sub_value = sanitize_text_field( $value );
1022 + $updated = (string) get_option( $option ) !== (string) $sub_value ? update_option( $option, $sub_value ) : true;
1023 + break;
976 1024
977 1025 case 'stb_enabled':
978 1026 case 'stc_enabled':
1027 + case 'sm_enabled':
1028 + case 'jetpack_subscribe_overlay_enabled':
1029 + case 'jetpack_subscribe_floating_button_enabled':
1030 + case 'wpcom_newsletter_categories_enabled':
1031 + case 'wpcom_featured_image_in_email':
1032 + case 'jetpack_gravatar_in_email':
1033 + case 'jetpack_author_in_email':
1034 + case 'jetpack_post_date_in_email':
1035 + case 'wpcom_subscription_emails_use_excerpt':
1036 + case 'jetpack_subscriptions_subscribe_post_end_enabled':
1037 + case 'jetpack_subscriptions_login_navigation_enabled':
1038 + case 'jetpack_subscriptions_subscribe_navigation_enabled':
979 1039 // Convert the false value to 0. This allows the option to be updated if it doesn't exist yet.
980 1040 $sub_value = $value ? $value : 0;
981 1041 $updated = (string) get_option( $option ) !== (string) $sub_value ? update_option( $option, $sub_value ) : true;
982 1042 break;
@@ -984,11 +1044,128 @@
984 1044 case 'jetpack_blocks_disabled':
985 1045 $updated = (bool) get_option( $option ) !== (bool) $value ? update_option( $option, (bool) $value ) : true;
986 1046 break;
987 1047
1048 + case 'subscription_options':
1049 + if ( ! is_array( $value ) ) {
1050 + break;
1051 + }
1052 +
1053 + $allowed_keys = array( 'invitation', 'comment_follow', 'welcome', 'subscribe_modal_heading', 'free_tier_description', 'hide_free_tier' );
1054 + $filtered_value = array_filter(
1055 + $value,
1056 + function ( $key ) use ( $allowed_keys ) {
1057 + return in_array( $key, $allowed_keys, true );
1058 + },
1059 + ARRAY_FILTER_USE_KEY
1060 + );
1061 +
1062 + if ( empty( $filtered_value ) ) {
1063 + break;
1064 + }
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 +
1075 + array_walk_recursive(
1076 + $filtered_value,
1077 + function ( &$value ) {
1078 + $value = wp_kses(
1079 + $value,
1080 + array(
1081 + 'ul' => array(),
1082 + 'li' => array(),
1083 + 'p' => array(),
1084 + 'strong' => array(),
1085 + 'ol' => array(),
1086 + 'em' => array(),
1087 + 'a' => array(
1088 + 'href' => array(),
1089 + ),
1090 + )
1091 + );
1092 + }
1093 + );
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 +
1120 + $old_subscription_options = get_option( 'subscription_options' );
1121 + if ( ! is_array( $old_subscription_options ) ) {
1122 + $old_subscription_options = array();
1123 + }
1124 + $new_subscription_options = array_merge( $old_subscription_options, $filtered_value );
1125 + $updated = true;
1126 +
1127 + if ( serialize( $old_subscription_options ) === serialize( $new_subscription_options ) ) { // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
1128 + break; // This prevents the option update to fail when the values are the same.
1129 + }
1130 +
1131 + if ( ! update_option( $option, $new_subscription_options ) ) {
1132 + $updated = false;
1133 + $error = esc_html__( 'Subscription Options failed to process.', 'jetpack' );
1134 + }
1135 + break;
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 +
988 1159 default:
989 - // If option value was the same, consider it done.
990 - $updated = get_option( $option ) != $value // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual -- ensure we support scalars or strings saved by update_option.
1160 + // Boolean values are stored as 1 or 0.
1161 + if ( isset( $options[ $option ]['type'] ) && 'boolean' === $options[ $option ]['type'] ) {
1162 + $value = (int) $value;
1163 + }
1164 +
1165 + // If option value was the same as it's current value, or it's default, consider it done.
1166 + $default = $options[ $option ]['default'] ?? false;
1167 + $updated = get_option( $option, $default ) != $value // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual -- ensure we support scalars or strings saved by update_option.
991 1168 ? update_option( $option, $value )
992 1169 : true;
993 1170 break;
994 1171 }
@@ -1009,9 +1186,9 @@
1009 1186 if ( $invalid_count > 0 ) {
1010 1187 $error = sprintf(
1011 1188 /* Translators: the plural variable is a comma-separated list. Example: dog, cat, bird. */
1012 1189 _n( 'Invalid option: %s.', 'Invalid options: %s.', $invalid_count, 'jetpack' ),
1013 - join( ', ', $invalid )
1190 + implode( ', ', $invalid )
1014 1191 );
1015 1192 }
1016 1193 if ( $not_updated_count > 0 ) {
1017 1194 $not_updated_messages = array();
@@ -1028,9 +1205,9 @@
1028 1205 if ( ! empty( $error ) ) {
1029 1206 $error .= ' ';
1030 1207 }
1031 1208 if ( ! empty( $not_updated_messages ) ) {
1032 - $error .= ' ' . join( '. ', $not_updated_messages );
1209 + $error .= ' ' . implode( '. ', $not_updated_messages );
1033 1210 }
1034 1211 }
1035 1212 // There was an error because some options were updated but others were invalid or failed to update.
1036 1213 return new WP_Error( 'some_updated', esc_html( $error ), array( 'status' => 400 ) );
@@ -1041,218 +1218,30 @@
1041 1218 * Perform tasks in the site based on onboarding choices.
1042 1219 *
1043 1220 * @since 5.4.0
1044 1221 *
1222 + * @deprecated since 13.9
1223 + *
1045 1224 * @param array $data Onboarding choices made by user.
1046 1225 *
1047 1226 * @return string Result of onboarding processing and, if there is one, an error message.
1048 1227 */
1049 - private function process_onboarding( $data ) {
1050 - if ( isset( $data['end'] ) && $data['end'] ) {
1051 - return Jetpack::invalidate_onboarding_token()
1052 - ? ''
1053 - : esc_html__( "The onboarding token couldn't be deleted.", 'jetpack' );
1054 - }
1055 -
1056 - $error = array();
1057 -
1058 - if ( ! empty( $data['siteTitle'] ) ) {
1059 - // If option value was the same, consider it done.
1060 - if ( ! (
1061 - update_option( 'blogname', $data['siteTitle'] )
1062 - || get_option( 'blogname' ) === $data['siteTitle']
1063 - ) ) {
1064 - $error[] = 'siteTitle';
1065 - }
1066 - }
1067 -
1068 - if ( isset( $data['siteDescription'] ) ) {
1069 - // If option value was the same, consider it done.
1070 - if ( ! (
1071 - update_option( 'blogdescription', $data['siteDescription'] )
1072 - || get_option( 'blogdescription' ) === $data['siteDescription']
1073 - ) ) {
1074 - $error[] = 'siteDescription';
1075 - }
1076 - }
1077 -
1078 - $site_title = get_option( 'blogname' );
1079 - $author = get_current_user_id() || 1;
1080 -
1081 - if ( ! empty( $data['siteType'] ) ) {
1082 - if ( ! (
1083 - update_option( 'jpo_site_type', $data['siteType'] )
1084 - || get_option( 'jpo_site_type' ) === $data['siteType']
1085 - ) ) {
1086 - $error[] = 'siteType';
1087 - }
1088 - }
1089 -
1090 - if ( isset( $data['homepageFormat'] ) ) {
1091 - /*
1092 - * If $data['homepageFormat'] is 'posts',
1093 - * we have nothing to do since it's WordPress' default
1094 - * if it exists, just update
1095 - */
1096 - $homepage_format = get_option( 'jpo_homepage_format' );
1097 - if ( ! $homepage_format || $homepage_format !== $data['homepageFormat'] ) {
1098 - if ( 'page' === $data['homepageFormat'] ) {
1099 - if ( ! (
1100 - update_option( 'show_on_front', 'page' )
1101 - || get_option( 'show_on_front' ) === 'page'
1102 - ) ) {
1103 - $error[] = 'homepageFormat';
1104 - }
1105 -
1106 - $home = wp_insert_post(
1107 - array(
1108 - 'post_type' => 'page',
1109 - /* translators: this references the home page of a site, also called front page. */
1110 - 'post_title' => esc_html_x( 'Home Page', 'The home page of a website.', 'jetpack' ),
1111 - 'post_content' => sprintf(
1112 - /* Translators: placeholder is the site title. */
1113 - esc_html__( 'Welcome to %s.', 'jetpack' ),
1114 - $site_title
1115 - ),
1116 - 'post_status' => 'publish',
1117 - 'post_author' => $author,
1118 - )
1119 - );
1120 - if ( 0 === $home ) {
1121 - $error[] = 'home insert: 0';
1122 - } elseif ( is_wp_error( $home ) ) {
1123 - $error[] = 'home creation: ' . $home->get_error_message();
1124 - }
1125 - if ( ! (
1126 - update_option( 'page_on_front', $home )
1127 - || get_option( 'page_on_front' ) === $home
1128 - ) ) {
1129 -
1130 - $error[] = 'home set';
1131 - }
1132 -
1133 - $blog = wp_insert_post(
1134 - array(
1135 - 'post_type' => 'page',
1136 - /* translators: this references the page where blog posts are listed. */
1137 - 'post_title' => esc_html_x( 'Blog', 'The blog of a website.', 'jetpack' ),
1138 - 'post_content' => sprintf(
1139 - /* Translators: placeholder is the site title. */
1140 - esc_html__( 'These are the latest posts in %s.', 'jetpack' ),
1141 - $site_title
1142 - ),
1143 - 'post_status' => 'publish',
1144 - 'post_author' => $author,
1145 - )
1146 - );
1147 - if ( 0 === $blog ) {
1148 - $error[] = 'blog insert: 0';
1149 - } elseif ( is_wp_error( $blog ) ) {
1150 - $error[] = 'blog creation: ' . $blog->get_error_message();
1151 - }
1152 - if ( ! (
1153 - update_option( 'page_for_posts', $blog )
1154 - || get_option( 'page_for_posts' ) === $blog
1155 - ) ) {
1156 - $error[] = 'blog set';
1157 - }
1158 - } else {
1159 - $front_page = get_option( 'page_on_front' );
1160 - $posts_page = get_option( 'page_for_posts' );
1161 - if ( $posts_page && get_post( $posts_page ) ) {
1162 - wp_delete_post( $posts_page );
1163 - }
1164 - if ( $front_page && get_post( $front_page ) ) {
1165 - wp_delete_post( $front_page );
1166 - }
1167 - update_option( 'show_on_front', 'posts' );
1168 - }
1169 - }
1170 - update_option( 'jpo_homepage_format', $data['homepageFormat'] );
1171 - }
1172 -
1173 - // Setup contact page and add a form and/or business info.
1174 - $contact_page = '';
1175 - if ( ! empty( $data['addContactForm'] ) && ! get_option( 'jpo_contact_page' ) ) {
1176 - $contact_form_module_active = Jetpack::is_module_active( 'contact-form' );
1177 - if ( ! $contact_form_module_active ) {
1178 - $contact_form_module_active = Jetpack::activate_module( 'contact-form', false, false );
1179 - }
1180 -
1181 - if ( $contact_form_module_active ) {
1182 - $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]';
1183 - } else {
1184 - $error[] = 'contact-form activate';
1185 - }
1186 - }
1187 -
1188 - if ( isset( $data['businessPersonal'] ) && 'business' === $data['businessPersonal'] ) {
1189 - $contact_page .= "\n" . join( "\n", $data['businessInfo'] );
1190 - }
1191 -
1192 - if ( ! empty( $contact_page ) ) {
1193 - $form = wp_insert_post(
1194 - array(
1195 - 'post_type' => 'page',
1196 - /* translators: this references a page with contact details and possibly a form. */
1197 - 'post_title' => esc_html_x( 'Contact us', 'Contact page for your website.', 'jetpack' ),
1198 - 'post_content' => esc_html__( 'Send us a message!', 'jetpack' ) . "\n" . $contact_page,
1199 - 'post_status' => 'publish',
1200 - 'post_author' => $author,
1201 - )
1202 - );
1203 - if ( 0 === $form ) {
1204 - $error[] = 'form insert: 0';
1205 - } elseif ( is_wp_error( $form ) ) {
1206 - $error[] = 'form creation: ' . $form->get_error_message();
1207 - } else {
1208 - update_option( 'jpo_contact_page', $form );
1209 - }
1210 - }
1211 -
1212 - if ( isset( $data['businessAddress'] ) ) {
1213 - $handled_business_address = self::handle_business_address( $data['businessAddress'] );
1214 - if ( is_wp_error( $handled_business_address ) ) {
1215 - $error[] = 'BusinessAddress';
1216 - }
1217 - }
1218 -
1219 - if ( ! empty( $data['installWooCommerce'] ) ) {
1220 - $wc_install_result = Plugins_Installer::install_and_activate_plugin( 'woocommerce' );
1221 - delete_transient( '_wc_activation_redirect' ); // Redirecting to WC setup would kill our users' flow.
1222 - if ( is_wp_error( $wc_install_result ) ) {
1223 - $error[] = 'woocommerce installation';
1224 - }
1225 - }
1226 -
1227 - if ( ! empty( $data['stats'] ) ) {
1228 - if ( Jetpack::is_connection_ready() ) {
1229 - $stats_module_active = Jetpack::is_module_active( 'stats' );
1230 - if ( ! $stats_module_active ) {
1231 - $stats_module_active = Jetpack::activate_module( 'stats', false, false );
1232 - }
1233 -
1234 - if ( ! $stats_module_active ) {
1235 - $error[] = 'stats activate';
1236 - }
1237 - } else {
1238 - $error[] = 'stats not connected';
1239 - }
1240 - }
1241 -
1242 - return empty( $error )
1243 - ? ''
1244 - : join( ', ', $error );
1228 + private function process_onboarding( $data ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1229 + _deprecated_function( __METHOD__, '13.9' );
1230 + return '';
1245 1231 }
1246 1232
1247 1233 /**
1248 1234 * Add or update Business Address widget.
1249 1235 *
1236 + * @deprecated since 13.9
1237 + *
1250 1238 * @param array $address Array of business address fields.
1251 1239 *
1252 1240 * @return WP_Error|true True if the data was saved correctly.
1253 1241 */
1254 1242 private static function handle_business_address( $address ) {
1243 + _deprecated_function( __METHOD__, '13.9' );
1255 1244 $first_sidebar = Jetpack_Widgets::get_first_sidebar();
1256 1245
1257 1246 $widgets_module_active = Jetpack::is_module_active( 'widgets' );
1258 1247 if ( ! $widgets_module_active ) {
@@ -1319,9 +1308,9 @@
1319 1308 if ( ! isset( $sidebars_widgets[ $sidebar ] ) ) {
1320 1309 return false;
1321 1310 }
1322 1311 foreach ( $sidebars_widgets[ $sidebar ] as $widget ) {
1323 - if ( strpos( $widget, 'widget_contact_info' ) !== false ) {
1312 + if ( str_contains( $widget, 'widget_contact_info' ) ) {
1324 1313 return true;
1325 1314 }
1326 1315 }
1327 1316 return false;
@@ -1336,13 +1325,8 @@
1336 1325 *
1337 1326 * @return bool
1338 1327 */
1339 1328 public function can_request( $request ) {
1340 - $req_params = $request->get_params();
1341 - if ( ! empty( $req_params['onboarding']['token'] ) && isset( $req_params['rest_route'] ) ) {
1342 - return Jetpack::validate_onboarding_token_action( $req_params['onboarding']['token'], $req_params['rest_route'] );
1343 - }
1344 -
1345 1329 if ( 'GET' === $request->get_method() ) {
1346 1330 return current_user_can( 'jetpack_admin_page' );
1347 1331 } else {
1348 1332 $module = Jetpack_Core_Json_Api_Endpoints::get_module_requested();
@@ -1351,13 +1335,13 @@
1351 1335 if ( ! is_array( $params ) ) {
1352 1336 $params = $request->get_body_params();
1353 1337 }
1354 1338 $options = Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list( $params );
1355 - foreach ( $options as $option => $definition ) {
1356 - if ( in_array( $options[ $option ]['jp_group'], array( 'post-by-email' ), true ) ) {
1357 - $module = $options[ $option ]['jp_group'];
1358 - break;
1359 - }
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';
1360 1344 }
1361 1345 }
1362 1346 // User is trying to create, regenerate or delete its PbE.
1363 1347 if ( 'post-by-email' === $module ) {
@@ -1373,9 +1357,8 @@
1373 1357 *
1374 1358 * phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
1375 1359 */
1376 1360 class Jetpack_Core_API_Module_Data_Endpoint {
1377 - // phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
1378 1361
1379 1362 /**
1380 1363 * Process request and return different data based on the module we are interested in.
1381 1364 *
@@ -1429,9 +1412,9 @@
1429 1412 * @return mixed|WP_Error Number of blocked attempts if protection is enabled. Otherwise, a WP_Error instance with the corresponding error.
1430 1413 */
1431 1414 public function get_protect_data() {
1432 1415 if ( Jetpack::is_module_active( 'protect' ) ) {
1433 - return get_site_option( 'jetpack_protect_blocked_attempts' );
1416 + return (int) get_site_option( 'jetpack_protect_blocked_attempts', 0 );
1434 1417 }
1435 1418
1436 1419 return new WP_Error(
1437 1420 'not_active',
@@ -1564,32 +1547,26 @@
1564 1547 ) {
1565 1548 $range = 'day';
1566 1549 }
1567 1550
1568 - if ( ! function_exists( 'convert_stats_array_to_object' ) ) {
1569 - require_once JETPACK__PLUGIN_DIR . 'modules/stats.php';
1570 - }
1571 -
1572 1551 $wpcom_stats = new WPCOM_Stats();
1573 1552 switch ( $range ) {
1574 1553
1575 1554 // This is always called first on page load.
1576 1555 case 'day':
1577 - $initial_stats = convert_stats_array_to_object( $wpcom_stats->get_stats() );
1556 + $initial_stats = $wpcom_stats->convert_stats_array_to_object( $wpcom_stats->get_stats() );
1578 1557 return rest_ensure_response(
1579 1558 array(
1580 1559 'general' => $initial_stats,
1581 1560
1582 1561 // Build data for 'day' as if it was $wpcom_stats ->get_visits( array( 'unit' => 'day, 'quantity' => 30).
1583 - 'day' => isset( $initial_stats->visits )
1584 - ? $initial_stats->visits
1585 - : array(),
1562 + 'day' => $initial_stats->visits ?? array(),
1586 1563 )
1587 1564 );
1588 1565 case 'week':
1589 1566 return rest_ensure_response(
1590 1567 array(
1591 - 'week' => convert_stats_array_to_object(
1568 + 'week' => $wpcom_stats->convert_stats_array_to_object(
1592 1569 $wpcom_stats->get_visits(
1593 1570 array(
1594 1571 'unit' => 'week',
1595 1572 'quantity' => 14,
@@ -1600,9 +1577,9 @@
1600 1577 );
1601 1578 case 'month':
1602 1579 return rest_ensure_response(
1603 1580 array(
1604 - 'month' => convert_stats_array_to_object(
1581 + 'month' => $wpcom_stats->convert_stats_array_to_object(
1605 1582 $wpcom_stats->get_visits(
1606 1583 array(
1607 1584 'unit' => 'month',
1608 1585 'quantity' => 12,
@@ -1724,11 +1701,11 @@
1724 1701 $last_service = $copy_services[ $last ];
1725 1702 unset( $copy_services[ $last ] );
1726 1703 $message = esc_html(
1727 1704 sprintf(
1728 - /* 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. */
1729 1706 __( 'Your site is verified with %1$s and %2$s.', 'jetpack' ),
1730 - join( ', ', $copy_services ),
1707 + implode( ', ', $copy_services ),
1731 1708 $last_service
1732 1709 )
1733 1710 );
1734 1711 }