Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Social: Update social/settings route registration #41596

Merged
merged 7 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Only register social/settings endpoint if Jetpack version does not have it
Original file line number Diff line number Diff line change
Expand Up @@ -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::should_use_jetpack_module_endpoint() ? 'settings' : 'social/settings',
);

$specific_paths = array();
Expand Down
13 changes: 12 additions & 1 deletion projects/packages/publicize/src/class-publicize-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +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." ) );
gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* 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
gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved
*
* @return bool
*/
public static function should_use_jetpack_module_endpoint() {
return class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( (string) JETPACK__VERSION, '14.3', '>=' ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Only register social/settings endpoint if Jetpack version does not have it
31 changes: 19 additions & 12 deletions projects/plugins/social/src/class-rest-settings-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,30 +29,36 @@ class REST_Settings_Controller extends WP_REST_Controller {
public function register_rest_routes() {
register_rest_route(
'jetpack/v4',
'/social/settings',
'/social/review-dismiss',
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' ),
'callback' => array( $this, 'update_review_dismissed' ),
'permission_callback' => array( $this, 'require_publish_posts_permission_callback' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
)
);

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/review-dismiss',
'/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_review_dismissed' ),
'permission_callback' => array( $this, 'require_publish_posts_permission_callback' ),
'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 ),
),
)
Expand Down
Loading