Skip to content

Commit 17c350e

Browse files
manzoorwanijkmatticbot
authored andcommitted
Social | Add site context for publicize endpoints (#40914)
* Allow requests as blog in base controller * Add filters for connections controller * Add changelog * Update baseline.php * Rename the 'include' param to 'scope' for clarity * Return shared connections by default * Remove scope parameter in favour of request context * Only pass test_connections to WPCOM * Update baseline.php Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/12685921332 Upstream-Ref: Automattic/jetpack@27b255e
1 parent af52a01 commit 17c350e

File tree

4 files changed

+74
-15
lines changed

4 files changed

+74
-15
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.57.2-alpha] - unreleased
9+
10+
This is an alpha version! The changes listed here are not final.
11+
12+
### Added
13+
- Publicize: Allow filtering of connections in publicize rest endpoint
14+
815
## [0.57.1] - 2024-12-30
916
### Fixed
1017
- Social: Ensure that broken connection notices work fine on WoA sites. [#40732]
@@ -796,6 +803,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
796803
- Updated package dependencies.
797804
- Update package.json metadata.
798805

806+
[0.57.2-alpha]: https://github.com/Automattic/jetpack-publicize/compare/v0.57.1...v0.57.2-alpha
799807
[0.57.1]: https://github.com/Automattic/jetpack-publicize/compare/v0.57.0...v0.57.1
800808
[0.57.0]: https://github.com/Automattic/jetpack-publicize/compare/v0.56.5...v0.57.0
801809
[0.56.5]: https://github.com/Automattic/jetpack-publicize/compare/v0.56.4...v0.56.5

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@automattic/jetpack-publicize",
4-
"version": "0.57.1",
4+
"version": "0.57.2-alpha",
55
"description": "Publicize makes it easy to share your site’s posts on several social media networks automatically when you publish a new post.",
66
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/publicize/#readme",
77
"bugs": {

src/rest-api/class-base-controller.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
*/
1919
abstract class Base_Controller extends WP_REST_Controller {
2020

21+
/**
22+
* Whether to allow requests as blog.
23+
*
24+
* @var bool
25+
*/
26+
protected $allow_requests_as_blog = false;
27+
2128
/**
2229
* Constructor.
2330
*/
@@ -34,6 +41,22 @@ public static function is_wpcom() {
3441
return ( new Host() )->is_wpcom_simple();
3542
}
3643

44+
/**
45+
* Check if the request is authorized for the blog.
46+
*
47+
* @return bool
48+
*/
49+
protected static function is_authorized_blog_request() {
50+
if ( self::is_wpcom() && is_jetpack_site( get_current_blog_id() ) ) {
51+
52+
$jp_auth_endpoint = new \WPCOM_REST_API_V2_Endpoint_Jetpack_Auth();
53+
54+
return $jp_auth_endpoint->is_jetpack_authorized_for_site() === true;
55+
}
56+
57+
return false;
58+
}
59+
3760
/**
3861
* Filters out data based on ?_fields= request parameter
3962
*
@@ -59,9 +82,11 @@ public function prepare_item_for_response( $item, $request ) {
5982
/**
6083
* Verify that user can access Publicize data
6184
*
85+
* @param WP_REST_Request $request Full details about the request.
6286
* @return true|WP_Error
6387
*/
64-
public function get_items_permission_check() {
88+
public function get_items_permissions_check( $request ) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
89+
6590
global $publicize;
6691

6792
if ( ! $publicize ) {
@@ -72,6 +97,10 @@ public function get_items_permission_check() {
7297
);
7398
}
7499

100+
if ( $this->allow_requests_as_blog && self::is_authorized_blog_request() ) {
101+
return true;
102+
}
103+
75104
if ( $publicize->current_user_can_access_publicize_data() ) {
76105
return true;
77106
}

src/rest-api/class-connections-controller.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public function __construct() {
2727
$this->namespace = 'wpcom/v2';
2828
$this->rest_base = 'publicize/connections';
2929

30+
$this->allow_requests_as_blog = true;
31+
3032
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
3133
}
3234

@@ -41,7 +43,7 @@ public function register_routes() {
4143
array(
4244
'methods' => WP_REST_Server::READABLE,
4345
'callback' => array( $this, 'get_items' ),
44-
'permission_callback' => array( $this, 'get_items_permission_check' ),
46+
'permission_callback' => array( $this, 'get_items_permissions_check' ),
4547
'args' => array(
4648
'test_connections' => array(
4749
'type' => 'boolean',
@@ -165,23 +167,32 @@ public function get_item_schema() {
165167
/**
166168
* Get all connections. Meant to be called directly only on WPCOM.
167169
*
168-
* @param bool $run_tests Whether to run tests on the connections.
170+
* @param array $args Arguments
171+
* - 'test_connections': bool Whether to run connection tests.
172+
* - 'scope': enum('site', 'user') Which connections to include.
169173
*
170174
* @return array
171175
*/
172-
protected static function get_all_connections( $run_tests = false ) {
176+
protected static function get_all_connections( $args = array() ) {
173177
/**
174178
* Publicize instance.
175-
*
176-
* @var \Automattic\Jetpack\Publicize\Publicize $publicize
177179
*/
178180
global $publicize;
179181

180182
$items = array();
181183

184+
$run_tests = $args['test_connections'] ?? false;
185+
182186
$test_results = $run_tests ? self::get_connections_test_status() : array();
183187

184-
foreach ( (array) $publicize->get_services( 'connected' ) as $service_name => $connections ) {
188+
// If a (Jetpack) blog request, return all the connections for that site.
189+
if ( self::is_authorized_blog_request() ) {
190+
$service_connections = $publicize->get_all_connections_for_blog_id( get_current_blog_id() );
191+
} else {
192+
$service_connections = (array) $publicize->get_services( 'connected' );
193+
}
194+
195+
foreach ( $service_connections as $service_name => $connections ) {
185196
foreach ( $connections as $connection ) {
186197

187198
$connection_id = $publicize->get_connection_id( $connection );
@@ -219,13 +230,15 @@ protected static function get_all_connections( $run_tests = false ) {
219230
/**
220231
* Get a list of publicize connections.
221232
*
222-
* @param bool $run_tests Whether to run tests on the connections.
233+
* @param array $args Arguments.
234+
*
235+
* @see Automattic\Jetpack\Publicize\REST_API\Connections_Controller::get_all_connections()
223236
*
224237
* @return array
225238
*/
226-
public static function get_connections( $run_tests = false ) {
239+
public static function get_connections( $args = array() ) {
227240
if ( self::is_wpcom() ) {
228-
return self::get_all_connections( $run_tests );
241+
return self::get_all_connections( $args );
229242
}
230243

231244
$site_id = Manager::get_site_id( true );
@@ -234,11 +247,17 @@ public static function get_connections( $run_tests = false ) {
234247
}
235248

236249
$path = add_query_arg(
237-
array( 'test_connections' => $run_tests ),
250+
array(
251+
'test_connections' => $args['test_connections'] ?? false,
252+
),
238253
sprintf( '/sites/%d/publicize/connections', $site_id )
239254
);
240255

241-
$response = Client::wpcom_json_api_request_as_user( $path, 'v2', array( 'method' => 'GET' ) );
256+
$blog_or_user = ( $args['scope'] ?? '' ) === 'site' ? 'blog' : 'user';
257+
258+
$callback = array( Client::class, "wpcom_json_api_request_as_{$blog_or_user}" );
259+
260+
$response = call_user_func( $callback, $path, 'v2', array( 'method' => 'GET' ), null, 'wpcom' );
242261

243262
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
244263
// TODO log error.
@@ -262,9 +281,12 @@ public static function get_connections( $run_tests = false ) {
262281
public function get_items( $request ) {
263282
$items = array();
264283

265-
$run_tests = $request->get_param( 'test_connections' );
284+
// On Jetpack, we don't want to pass the 'scope' param to get_connections().
285+
$args = array(
286+
'test_connections' => $request->get_param( 'test_connections' ),
287+
);
266288

267-
foreach ( self::get_connections( $run_tests ) as $item ) {
289+
foreach ( self::get_connections( $args ) as $item ) {
268290
$data = $this->prepare_item_for_response( $item, $request );
269291

270292
$items[] = $this->prepare_response_for_collection( $data );

0 commit comments

Comments
 (0)