From 0df6110b7ea489edb470674729544b19086a9b9f Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Fri, 7 Feb 2025 15:42:51 +0100 Subject: [PATCH] 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 ), + ), + ) + ); } /**