From 224154e3992a41a18cfc2d4434f30f66365e3e56 Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Thu, 6 Feb 2025 13:28:29 +0100 Subject: [PATCH 1/7] Only register social/settings endpoint if Jetpack still not has it --- .../src/class-publicize-script-data.php | 2 +- .../publicize/src/class-publicize-utils.php | 9 +++++ .../src/class-rest-settings-controller.php | 39 +++++++++++-------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/projects/packages/publicize/src/class-publicize-script-data.php b/projects/packages/publicize/src/class-publicize-script-data.php index abbba41c296e5..39750aaf6cfe9 100644 --- a/projects/packages/publicize/src/class-publicize-script-data.php +++ b/projects/packages/publicize/src/class-publicize-script-data.php @@ -306,7 +306,7 @@ public static function get_api_paths() { $commom_paths = array( 'refreshConnections' => '/wpcom/v2/publicize/connections?test_connections=1', // The complete path will be like `/jetpack/v4/social/settings`. - 'socialToggleBase' => class_exists( 'Jetpack' ) ? 'settings' : 'social/settings', + 'socialToggleBase' => Utils::has_new_module_endpoint() ? 'settings' : 'social/settings', ); $specific_paths = array(); diff --git a/projects/packages/publicize/src/class-publicize-utils.php b/projects/packages/publicize/src/class-publicize-utils.php index ef31f3c8ee24d..9dbd9f48fdc64 100644 --- a/projects/packages/publicize/src/class-publicize-utils.php +++ b/projects/packages/publicize/src/class-publicize-utils.php @@ -109,4 +109,13 @@ public static function assert_is_wpcom( $method ) { throw new \Exception( esc_html( "Method $method can only be called on WordPress.com." ) ); } } + + /** + * Check if the new module endpoint is available in the used Jetpack version. + * + * @return bool + */ + public static function has_new_module_endpoint() { + return class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( JETPACK__VERSION, '13.4', '>=' ) ); + } } diff --git a/projects/plugins/social/src/class-rest-settings-controller.php b/projects/plugins/social/src/class-rest-settings-controller.php index afd4db3c2eb09..94bd28d407e5e 100644 --- a/projects/plugins/social/src/class-rest-settings-controller.php +++ b/projects/plugins/social/src/class-rest-settings-controller.php @@ -9,6 +9,7 @@ namespace Automattic\Jetpack\Social; use Automattic\Jetpack\Modules; +use Automattic\Jetpack\Publicize\Publicize_Utils; use Jetpack_Social; use WP_Error; use WP_REST_Controller; @@ -26,24 +27,28 @@ class REST_Settings_Controller extends WP_REST_Controller { * @static */ public function register_rest_routes() { - register_rest_route( - 'jetpack/v4', - '/social/settings', - array( + // If the site has an older version of Jetpack we still need to register the route. + if ( ! Publicize_Utils::has_new_module_endpoint() ) { + register_rest_route( + 'jetpack/v4', + '/social/settings', array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_item' ), - 'permission_callback' => array( $this, 'require_admin_privilege_callback' ), - 'args' => $this->get_endpoint_args_for_item_schema(), - ), - array( - 'methods' => WP_REST_Server::EDITABLE, - 'callback' => array( $this, 'update_item' ), - 'permission_callback' => array( $this, 'require_admin_privilege_callback' ), - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), - ), - ) - ); + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_item' ), + 'permission_callback' => array( $this, 'require_admin_privilege_callback' ), + 'args' => $this->get_endpoint_args_for_item_schema(), + ), + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array( $this, 'update_item' ), + 'permission_callback' => array( $this, 'require_admin_privilege_callback' ), + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), + ), + ) + ); + } + register_rest_route( 'jetpack/v4', '/social/review-dismiss', From 10c4eb063fde8cdb1e3f37917d920e081cd3f194 Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Thu, 6 Feb 2025 13:29:11 +0100 Subject: [PATCH 2/7] changelog --- .../changelog/update-social-settings-route-registration | 4 ++++ .../changelog/update-social-settings-route-registration | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 projects/packages/publicize/changelog/update-social-settings-route-registration create mode 100644 projects/plugins/social/changelog/update-social-settings-route-registration diff --git a/projects/packages/publicize/changelog/update-social-settings-route-registration b/projects/packages/publicize/changelog/update-social-settings-route-registration new file mode 100644 index 0000000000000..3359660e24d14 --- /dev/null +++ b/projects/packages/publicize/changelog/update-social-settings-route-registration @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Only register social/settings endpoint if Jetpack version does not have it diff --git a/projects/plugins/social/changelog/update-social-settings-route-registration b/projects/plugins/social/changelog/update-social-settings-route-registration new file mode 100644 index 0000000000000..3359660e24d14 --- /dev/null +++ b/projects/plugins/social/changelog/update-social-settings-route-registration @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Only register social/settings endpoint if Jetpack version does not have it From bea08ac87f8ff37003cd5b0d18b4e8054e250f1f Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Thu, 6 Feb 2025 15:24:15 +0100 Subject: [PATCH 3/7] Use good version --- projects/packages/publicize/src/class-publicize-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/publicize/src/class-publicize-utils.php b/projects/packages/publicize/src/class-publicize-utils.php index 9dbd9f48fdc64..d4ca84d76a23d 100644 --- a/projects/packages/publicize/src/class-publicize-utils.php +++ b/projects/packages/publicize/src/class-publicize-utils.php @@ -116,6 +116,6 @@ public static function assert_is_wpcom( $method ) { * @return bool */ public static function has_new_module_endpoint() { - return class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( JETPACK__VERSION, '13.4', '>=' ) ); + return class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( JETPACK__VERSION, '14.3', '>=' ) ); } } From 78974f555d18be16920e72ccbf0013ec0dfaf3dc Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Thu, 6 Feb 2025 15:41:37 +0100 Subject: [PATCH 4/7] Fix Phan complainig --- projects/packages/publicize/src/class-publicize-utils.php | 1 + 1 file changed, 1 insertion(+) diff --git a/projects/packages/publicize/src/class-publicize-utils.php b/projects/packages/publicize/src/class-publicize-utils.php index d4ca84d76a23d..2ceb8aca5c4fe 100644 --- a/projects/packages/publicize/src/class-publicize-utils.php +++ b/projects/packages/publicize/src/class-publicize-utils.php @@ -116,6 +116,7 @@ public static function assert_is_wpcom( $method ) { * @return bool */ public static function has_new_module_endpoint() { + // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal - Phan thinks JETPACK__VERSION is not a string (it is). return class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( JETPACK__VERSION, '14.3', '>=' ) ); } } From 0df6110b7ea489edb470674729544b19086a9b9f Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Fri, 7 Feb 2025 15:42:51 +0100 Subject: [PATCH 5/7] Address comments --- .../src/class-publicize-script-data.php | 2 +- .../publicize/src/class-publicize-utils.php | 5 +- .../src/class-rest-settings-controller.php | 46 ++++++++++--------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/projects/packages/publicize/src/class-publicize-script-data.php b/projects/packages/publicize/src/class-publicize-script-data.php index 39750aaf6cfe9..2ae4dc2c8768a 100644 --- a/projects/packages/publicize/src/class-publicize-script-data.php +++ b/projects/packages/publicize/src/class-publicize-script-data.php @@ -306,7 +306,7 @@ public static function get_api_paths() { $commom_paths = array( 'refreshConnections' => '/wpcom/v2/publicize/connections?test_connections=1', // The complete path will be like `/jetpack/v4/social/settings`. - 'socialToggleBase' => Utils::has_new_module_endpoint() ? 'settings' : 'social/settings', + 'socialToggleBase' => Utils::should_use_jetpack_module_endpoint() ? 'settings' : 'social/settings', ); $specific_paths = array(); diff --git a/projects/packages/publicize/src/class-publicize-utils.php b/projects/packages/publicize/src/class-publicize-utils.php index 2ceb8aca5c4fe..fefc782f2c51f 100644 --- a/projects/packages/publicize/src/class-publicize-utils.php +++ b/projects/packages/publicize/src/class-publicize-utils.php @@ -112,11 +112,12 @@ public static function assert_is_wpcom( $method ) { /** * Check if the new module endpoint is available in the used Jetpack version. + * More: https://github.com/Automattic/jetpack-reach/issues/794 * * @return bool */ - public static function has_new_module_endpoint() { + public static function should_use_jetpack_module_endpoint() { // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal - Phan thinks JETPACK__VERSION is not a string (it is). - return class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( JETPACK__VERSION, '14.3', '>=' ) ); + return class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( (string) JETPACK__VERSION, '14.3', '>=' ) ); } } diff --git a/projects/plugins/social/src/class-rest-settings-controller.php b/projects/plugins/social/src/class-rest-settings-controller.php index 94bd28d407e5e..ba858acfa1c55 100644 --- a/projects/plugins/social/src/class-rest-settings-controller.php +++ b/projects/plugins/social/src/class-rest-settings-controller.php @@ -27,28 +27,6 @@ class REST_Settings_Controller extends WP_REST_Controller { * @static */ public function register_rest_routes() { - // If the site has an older version of Jetpack we still need to register the route. - if ( ! Publicize_Utils::has_new_module_endpoint() ) { - register_rest_route( - 'jetpack/v4', - '/social/settings', - array( - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_item' ), - 'permission_callback' => array( $this, 'require_admin_privilege_callback' ), - 'args' => $this->get_endpoint_args_for_item_schema(), - ), - array( - 'methods' => WP_REST_Server::EDITABLE, - 'callback' => array( $this, 'update_item' ), - 'permission_callback' => array( $this, 'require_admin_privilege_callback' ), - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), - ), - ) - ); - } - register_rest_route( 'jetpack/v4', '/social/review-dismiss', @@ -61,6 +39,30 @@ public function register_rest_routes() { ), ) ); + + if ( Publicize_Utils::should_use_jetpack_module_endpoint() ) { + return; + } + + // If the site has an older version of Jetpack we still need to register the route. + register_rest_route( + 'jetpack/v4', + '/social/settings', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_item' ), + 'permission_callback' => array( $this, 'require_admin_privilege_callback' ), + 'args' => $this->get_endpoint_args_for_item_schema(), + ), + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array( $this, 'update_item' ), + 'permission_callback' => array( $this, 'require_admin_privilege_callback' ), + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), + ), + ) + ); } /** From dea469bb19bff7bb2551b98fa9b45fc3a42cbedb Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Mon, 10 Feb 2025 09:55:20 +0100 Subject: [PATCH 6/7] Extend comments even more --- projects/packages/publicize/src/class-publicize-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/packages/publicize/src/class-publicize-utils.php b/projects/packages/publicize/src/class-publicize-utils.php index fefc782f2c51f..6612ecde81590 100644 --- a/projects/packages/publicize/src/class-publicize-utils.php +++ b/projects/packages/publicize/src/class-publicize-utils.php @@ -106,18 +106,18 @@ public static function is_wpcom() { */ public static function assert_is_wpcom( $method ) { if ( ! self::is_wpcom() ) { - throw new \Exception( esc_html( "Method $method can only be called on WordPress.com." ) ); + throw new \Exception( esc_html( "Method $method can only b`e called on WordPress.com." ) ); } } /** * Check if the new module endpoint is available in the used Jetpack version. + * We need the module status in response that's why we do the version check https://github.com/Automattic/jetpack/pull/41461/files#diff-f8e5ef1115599de750b64143dd1901554254eddd95ab4371b6b6b3b2a5914224R638-R642. * More: https://github.com/Automattic/jetpack-reach/issues/794 * * @return bool */ public static function should_use_jetpack_module_endpoint() { - // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal - Phan thinks JETPACK__VERSION is not a string (it is). return class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( (string) JETPACK__VERSION, '14.3', '>=' ) ); } } From c7c32de255715f4f27e8cdbc47ccaa8adfa626f7 Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Mon, 10 Feb 2025 12:14:18 +0100 Subject: [PATCH 7/7] Fix linking --- projects/packages/publicize/src/class-publicize-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/packages/publicize/src/class-publicize-utils.php b/projects/packages/publicize/src/class-publicize-utils.php index 6612ecde81590..b181681d05172 100644 --- a/projects/packages/publicize/src/class-publicize-utils.php +++ b/projects/packages/publicize/src/class-publicize-utils.php @@ -106,14 +106,14 @@ public static function is_wpcom() { */ public static function assert_is_wpcom( $method ) { if ( ! self::is_wpcom() ) { - throw new \Exception( esc_html( "Method $method can only b`e called on WordPress.com." ) ); + throw new \Exception( esc_html( "Method $method can only be called on WordPress.com." ) ); } } /** * Check if the new module endpoint is available in the used Jetpack version. * We need the module status in response that's why we do the version check https://github.com/Automattic/jetpack/pull/41461/files#diff-f8e5ef1115599de750b64143dd1901554254eddd95ab4371b6b6b3b2a5914224R638-R642. - * More: https://github.com/Automattic/jetpack-reach/issues/794 + * More: https://github.com/Automattic/jetpack/pull/41596. * * @return bool */