Skip to content

Commit

Permalink
Improve filtering of fields
Browse files Browse the repository at this point in the history
  • Loading branch information
manzoorwanijk committed Dec 11, 2024
1 parent a54af87 commit 5bb2606
Showing 1 changed file with 47 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function get_item_schema() {
*
* @return array
*/
public static function get_all_connections( $run_tests = false ) {
protected static function get_all_connections( $run_tests = false ) {
/**
* Publicize instance.
*
Expand Down Expand Up @@ -175,48 +175,65 @@ public static function get_all_connections( $run_tests = false ) {
}

/**
* Get list of connected Publicize connections.
* Get a list of publicize connections.
*
* @param WP_REST_Request $request Full details about the request.
* @param bool $is_wpcom Whether we are on WPCOM.
* @param bool $run_tests Whether to run tests on the connections.
*
* @return WP_REST_Response suitable for 1-page collection
* @return array
*/
public function get_items( $request ) {
if ( $this->is_wpcom ) {
$items = array();
public static function get_connections( $is_wpcom, $run_tests = false ) {
if ( $is_wpcom ) {
return self::get_all_connections( $run_tests );
}

$run_tests = $request->get_param( 'test_connections' );
$site_id = Manager::get_site_id( true );
if ( ! $site_id ) {
return array();
}

$connections = self::get_all_connections( $run_tests );
$path = add_query_arg(
array( 'test_connections' => $run_tests ),
sprintf( '/sites/%d/publicize/connections', $site_id )
);

foreach ( $connections as $item ) {
$items[] = $this->prepare_item_for_response( $item, $request );
}
$response = Client::wpcom_json_api_request_as_user( $path, 'v3', array( 'method' => 'GET' ) );

return rest_ensure_response( $items );
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
// TODO log error.
return array();
}

} else {
$site_id = Manager::get_site_id( true );
if ( ! $site_id ) {
return rest_ensure_response( array() );
}
$body = wp_remote_retrieve_body( $response );

$path = add_query_arg(
$request->get_query_params(),
sprintf( '/sites/%d/' . $this->rest_base, $site_id )
);
$items = json_decode( $body, true );

$response = Client::wpcom_json_api_request_as_user( $path, 'v3', array( 'method' => 'GET' ) );
return $items ? $items : array();
}

if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
// TODO log error.
return rest_ensure_response( array() );
}
/**
* Get list of connected Publicize connections.
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_REST_Response suitable for 1-page collection
*/
public function get_items( $request ) {
$items = array();

return rest_ensure_response(
json_decode( wp_remote_retrieve_body( $response ), true )
);
$run_tests = $request->get_param( 'test_connections' );

foreach ( self::get_connections( $this->is_wpcom, $run_tests ) as $item ) {
$data = $this->prepare_item_for_response( $item, $request );

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

$response = rest_ensure_response( $items );
$response->header( 'X-WP-Total', count( $items ) );
$response->header( 'X-WP-TotalPages', 1 );

return $response;
}

/**
Expand Down

0 comments on commit 5bb2606

Please sign in to comment.