Skip to content

Commit

Permalink
Social | Deprecate jetpack/v4 connection endpoints (#41911)
Browse files Browse the repository at this point in the history
* Social | Deprecate jetpack/v4 connection endpoints

* Fix unit tests

* Clean up Proxy_Requests usage

* Convert doing it wrong to a one liner for version replacement

* Add _doing_it_wrong to next-version checks

* Pass version from param

* Revert "Add _doing_it_wrong to next-version checks"

This reverts commit 97412d8bd4f197bda745754bb1f33ce6bc13fcd0.

* Use both the plugin versions

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/13518539324

Upstream-Ref: Automattic/jetpack@2fc7f43
  • Loading branch information
manzoorwanijk authored and matticbot committed Feb 25, 2025
1 parent 9410132 commit 7890321
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 230 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"automattic/jetpack-autoloader": "^5.0.2",
"automattic/jetpack-composer-plugin": "^4.0.1",
"automattic/jetpack-config": "^3.0.1",
"automattic/jetpack-publicize": "^0.61.0",
"automattic/jetpack-publicize": "^0.61.1-alpha",
"automattic/jetpack-connection": "^6.6.0",
"automattic/jetpack-my-jetpack": "^5.5.0",
"automattic/jetpack-sync": "^4.8.2",
Expand Down
8 changes: 8 additions & 0 deletions jetpack_vendor/automattic/jetpack-publicize/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.61.1-alpha] - unreleased

This is an alpha version! The changes listed here are not final.

### Deprecated
- Deprecated jetpack/v4 connection endpoints

## [0.61.0] - 2025-02-24
### Added
- Social: Update the admin page for use by Editors and Authors. [#41859]
Expand Down Expand Up @@ -877,6 +884,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated package dependencies.
- Update package.json metadata.

[0.61.1-alpha]: https://github.com/Automattic/jetpack-publicize/compare/v0.61.0...v0.61.1-alpha
[0.61.0]: https://github.com/Automattic/jetpack-publicize/compare/v0.60.1...v0.61.0
[0.60.1]: https://github.com/Automattic/jetpack-publicize/compare/v0.60.0...v0.60.1
[0.60.0]: https://github.com/Automattic/jetpack-publicize/compare/v0.59.1...v0.60.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static function fetch_and_cache_connections() {
public static function fetch_site_connections() {
$proxy = new Proxy_Requests( 'publicize/connections' );

$request = new WP_REST_Request( 'GET', '/wpcom/v2/publicize/connections' );
$request = new WP_REST_Request( 'GET' );

return $proxy->proxy_request_to_wpcom_as_blog( $request );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,36 @@ public static function assert_is_wpcom( $method ) {
public static function should_use_jetpack_module_endpoint() {
return class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( (string) JETPACK__VERSION, '14.3', '>=' ) );
}

/**
* Log a warning that a deprecated endpoint was called.
*
* @param string $function_name The function name.
* @param string $version The version in which the endpoint was deprecated.
* @param string $deprecated_endpoint The deprecated endpoint.
* @param string $alternative_endpoint The alternative endpoint.
*/
public static function endpoint_deprecated_warning( $function_name, $version, $deprecated_endpoint, $alternative_endpoint = '' ) {

$messages = array(
sprintf(
/* translators: %s: REST API endpoint. */
esc_html__( '%1$s endpoint has been deprecated.', 'jetpack-publicize-pkg' ),
'"' . $deprecated_endpoint . '"'
),
);

if ( ! empty( $alternative_endpoint ) ) {
$messages[] = sprintf(
/* translators: %s: alternative endpoint. */
esc_html__( 'Please use %s endpoint instead.', 'jetpack-publicize-pkg' ),
'"' . $alternative_endpoint . '"'
);
}

$messages[] = esc_html__( 'Please update all the Jetpack plugins to the latest version.', 'jetpack-publicize-pkg' );

// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- We have done it above.
_doing_it_wrong( esc_html( $function_name ), implode( ' ', $messages ), $version );
}
}
248 changes: 109 additions & 139 deletions jetpack_vendor/automattic/jetpack-publicize/src/class-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Rest_Authentication;
use Automattic\Jetpack\Publicize\REST_API\Proxy_Requests;
use Jetpack_Options;
use WP_Error;
use WP_REST_Request;
Expand Down Expand Up @@ -319,37 +320,131 @@ public function get_jetpack_social_connections_update_schema() {
* Gets the current Publicize connections, with the resolt of testing them, for the site.
*
* GET `jetpack/v4/publicize/connection-test-results`
*
* @deprecated 0.61.1-alpha
*/
public function get_publicize_connection_test_results() {
$blog_id = $this->get_blog_id();
$path = sprintf( '/sites/%d/publicize/connection-test-results', absint( $blog_id ) );
$response = Client::wpcom_json_api_request_as_user( $path, '2', array(), null, 'wpcom' );
return rest_ensure_response( $this->make_proper_response( $response ) );

Publicize_Utils::endpoint_deprecated_warning(
__METHOD__,
'jetpack-14.4, jetpack-social-6.2.0',
'jetpack/v4/publicize/connection-test-results',
'wpcom/v2/publicize/connections?test_connections=1'
);

$proxy = new Proxy_Requests( 'publicize/connections' );

$request = new WP_REST_Request( 'GET' );

$request->set_param( 'test_connections', '1' );

return rest_ensure_response( $proxy->proxy_request_to_wpcom_as_user( $request ) );
}

/**
* Gets the current Publicize connections for the site.
*
* GET `jetpack/v4/publicize/connections`
*
* @deprecated 0.61.1-alpha
*
* @param WP_REST_Request $request The request object, which includes the parameters.
*/
public function get_publicize_connections( $request ) {
$run_test_results = $request->get_param( 'test_connections' );
$clear_cache = $request->get_param( 'clear_cache' );

$args = array();
Publicize_Utils::endpoint_deprecated_warning(
__METHOD__,
'jetpack-14.4, jetpack-social-6.2.0',
'jetpack/v4/publicize/connections',
'wpcom/v2/publicize/connections?test_connections=1'
);

if ( ! empty( $run_test_results ) ) {
$args['test_connections'] = true;
}
if ( $request->get_param( 'test_connections' ) ) {

if ( ! empty( $clear_cache ) ) {
$args['clear_cache'] = true;
$proxy = new Proxy_Requests( 'publicize/connections' );

return rest_ensure_response( $proxy->proxy_request_to_wpcom_as_user( $request ) );
}

global $publicize;
return rest_ensure_response( $publicize->get_all_connections_for_user( $args ) );
return rest_ensure_response( Connections::get_all_for_user() );
}

/**
* Create a publicize connection
*
* @deprecated 0.61.1-alpha
*
* @param WP_REST_Request $request The request object, which includes the parameters.
* @return WP_REST_Response|WP_Error True if the request was successful, or a WP_Error otherwise.
*/
public function create_publicize_connection( $request ) {

Publicize_Utils::endpoint_deprecated_warning(
__METHOD__,
'jetpack-14.4, jetpack-social-6.2.0',
'jetpack/v4/social/connections',
'wpcom/v2/publicize/connections'
);

$proxy = new Proxy_Requests( 'publicize/connections' );

return rest_ensure_response(
$proxy->proxy_request_to_wpcom_as_user( $request, '', array( 'timeout' => 120 ) )
);
}

/**
* Calls the WPCOM endpoint to update the publicize connection.
*
* POST jetpack/v4/social/connections/{connection_id}
*
* @deprecated 0.61.1-alpha
*
* @param WP_REST_Request $request The request object, which includes the parameters.
*/
public function update_publicize_connection( $request ) {

Publicize_Utils::endpoint_deprecated_warning(
__METHOD__,
'jetpack-14.4, jetpack-social-6.2.0',
'jetpack/v4/social/connections/:connection_id',
'wpcom/v2/publicize/connections/:connection_id'
);

$proxy = new Proxy_Requests( 'publicize/connections' );

$path = $request->get_param( 'connection_id' );

return rest_ensure_response(
$proxy->proxy_request_to_wpcom_as_user( $request, $path, array( 'timeout' => 120 ) )
);
}

/**
* Calls the WPCOM endpoint to delete the publicize connection.
*
* DELETE jetpack/v4/social/connections/{connection_id}
*
* @deprecated 0.61.1-alpha
*
* @param WP_REST_Request $request The request object, which includes the parameters.
*/
public function delete_publicize_connection( $request ) {

Publicize_Utils::endpoint_deprecated_warning(
__METHOD__,
'jetpack-14.4, jetpack-social-6.2.0',
'jetpack/v4/social/connections/:connection_id',
'wpcom/v2/publicize/connections/:connection_id'
);

$proxy = new Proxy_Requests( 'publicize/connections' );

$path = $request->get_param( 'connection_id' );

return rest_ensure_response(
$proxy->proxy_request_to_wpcom_as_user( $request, $path, array( 'timeout' => 120 ) )
);
}

/**
Expand Down Expand Up @@ -453,131 +548,6 @@ protected function get_blog_id() {
return $this->is_wpcom ? get_current_blog_id() : Jetpack_Options::get_option( 'id' );
}

/**
* Calls the WPCOM endpoint to update the publicize connection.
*
* POST jetpack/v4/social/connections/{connection_id}
*
* @param WP_REST_Request $request The request object, which includes the parameters.
*/
public function update_publicize_connection( $request ) {
$external_user_id = $request->get_param( 'external_user_ID' );
$shared = $request->get_param( 'shared' );
$blog_id = $this->get_blog_id();
$connection_id = $request->get_param( 'connection_id' );

$path = sprintf(
'/sites/%d/jetpack-social-connections/%d',
$blog_id,
$connection_id
);

$body = array();

if ( ! empty( $external_user_id ) ) {
$body['external_user_ID'] = $external_user_id;
}

if ( $shared || ( false === $shared ) ) {
$body['shared'] = $shared;
}

$response = Client::wpcom_json_api_request_as_user(
$path,
'2',
array(
'method' => 'POST',
'timeout' => 120,
),
$body,
'wpcom'
);

$response = $this->make_proper_response( $response );

if ( is_wp_error( $response ) ) {
return $response;
}

global $publicize;
return rest_ensure_response( $publicize->get_connection_for_user( (int) $connection_id ) );
}

/**
* Calls the WPCOM endpoint to delete the publicize connection.
*
* DELETE jetpack/v4/social/connections/{connection_id}
*
* @param WP_REST_Request $request The request object, which includes the parameters.
*/
public function delete_publicize_connection( $request ) {
$connection_id = $request->get_param( 'connection_id' );
$blog_id = $this->get_blog_id();

$path = sprintf(
'/sites/%d/jetpack-social-connections/%d',
$blog_id,
$connection_id
);

$response = Client::wpcom_json_api_request_as_user( $path, '2', array( 'method' => 'DELETE' ), null, 'wpcom' );
return rest_ensure_response( $this->make_proper_response( $response ) );
}

/**
* Create a publicize connection
*
* @param WP_REST_Request $request The request object, which includes the parameters.
* @return WP_REST_Response|WP_Error True if the request was successful, or a WP_Error otherwise.
*/
public function create_publicize_connection( $request ) {
$keyring_connection_id = $request->get_param( 'keyring_connection_ID' );
$shared = $request->get_param( 'shared' );
$external_user_id = $request->get_param( 'external_user_ID' );
$blog_id = $this->get_blog_id();

$path = sprintf(
'/sites/%d/jetpack-social-connections/new',
$blog_id
);

$body = array(
'keyring_connection_ID' => $keyring_connection_id,
'shared' => $shared,
);

if ( ! empty( $external_user_id ) ) {
$body['external_user_ID'] = $external_user_id;
}

$response = Client::wpcom_json_api_request_as_user(
$path,
'2',
array(
'method' => 'POST',
'timeout' => 120,
),
$body,
'wpcom'
);

$response = $this->make_proper_response( $response );

if ( is_wp_error( $response ) ) {
return $response;
}

if ( isset( $response['ID'] ) ) {
global $publicize;
return rest_ensure_response( $publicize->get_connection_for_user( (int) $response['ID'] ) );
}

return new WP_Error(
'could_not_create_connection',
__( 'Something went wrong while creating a connection.', 'jetpack-publicize-pkg' )
);
}

/**
* Update the post with information about shares.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function ( $service ) {
public static function fetch_and_cache_services() {
$proxy = new Proxy_Requests( 'external-services' );

$request = new WP_REST_Request( 'GET', '/wpcom/v2/external-services' );
$request = new WP_REST_Request( 'GET' );

$request->set_param( 'type', 'publicize' );

Expand Down
2 changes: 1 addition & 1 deletion jetpack_vendor/i18n-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
),
'jetpack-publicize-pkg' => array(
'path' => 'jetpack_vendor/automattic/jetpack-publicize',
'ver' => '0.61.0',
'ver' => '0.61.1-alpha1740477695',
),
'jetpack-sync' => array(
'path' => 'jetpack_vendor/automattic/jetpack-sync',
Expand Down
Loading

0 comments on commit 7890321

Please sign in to comment.