@@ -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