Skip to content

Commit 1b3714c

Browse files
authored
Adds a local handling of the Blaze advise campaign endpoint (#46623)
* Adds a local handling of the Blaze advise campaign endpoint
1 parent 08a799c commit 1b3714c

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Improve post promotion speed by handling the Blaze “advise campaign” endpoint locally, reducing the initial sync delay.

projects/packages/blaze/src/class-dashboard-rest-controller.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ public function register_rest_routes() {
216216
'permission_callback' => array( $this, 'can_user_view_dsp_callback' ),
217217
)
218218
);
219+
register_rest_route(
220+
static::$namespace,
221+
sprintf( '/sites/%d/wordads/dsp/api/v1/templates/advise/campaign/(?P<urn>[a-zA-Z0-9-_:]*)(\?.*)?', $site_id ),
222+
array(
223+
'methods' => WP_REST_Server::READABLE,
224+
'callback' => array( $this, 'get_dsp_templates_advise_campaign' ),
225+
'permission_callback' => array( $this, 'can_user_view_dsp_callback' ),
226+
)
227+
);
219228

220229
register_rest_route(
221230
static::$namespace,
@@ -227,6 +236,26 @@ public function register_rest_routes() {
227236
)
228237
);
229238

239+
// WordAds DSP API Advise routes
240+
register_rest_route(
241+
static::$namespace,
242+
sprintf( '/sites/%d/wordads/dsp/api/v1/advise/campaign/(?P<urn>[a-zA-Z0-9-_:]*)(\?.*)?', $site_id ),
243+
array(
244+
'methods' => WP_REST_Server::READABLE,
245+
'callback' => array( $this, 'get_dsp_advise_campaign' ),
246+
'permission_callback' => array( $this, 'can_user_view_dsp_callback' ),
247+
)
248+
);
249+
register_rest_route(
250+
static::$namespace,
251+
sprintf( '/sites/%d/wordads/dsp/api/v1/advise(?P<sub_path>[a-zA-Z0-9-_\/:]*)(\?.*)?', $site_id ),
252+
array(
253+
'methods' => WP_REST_Server::READABLE,
254+
'callback' => array( $this, 'get_dsp_advise' ),
255+
'permission_callback' => array( $this, 'can_user_view_dsp_callback' ),
256+
)
257+
);
258+
230259
// WordAds DSP API Subscriptions routes
231260
register_rest_route(
232261
static::$namespace,
@@ -723,6 +752,73 @@ public function get_dsp_templates_article_local( $urn, $req ) {
723752
);
724753
}
725754

755+
/**
756+
* Redirect GET requests to the WordAds DSP Templates Advise Campaign endpoint for the site.
757+
*
758+
* @param WP_REST_Request $req The request object.
759+
* @return array|WP_Error
760+
*/
761+
public function get_dsp_templates_advise_campaign( $req ) {
762+
$urn = $req->get_param( 'urn' ) ?? '';
763+
764+
$sync_ready = $this->are_posts_ready();
765+
766+
$response = $sync_ready ?
767+
$this->get_dsp_generic( 'v1/templates/advise/campaign/' . $urn, $req ) :
768+
$this->get_dsp_advise_campaign_local( $urn );
769+
770+
if ( ! is_wp_error( $response ) && is_array( $response ) ) {
771+
$response['sync_ready'] = $sync_ready;
772+
}
773+
774+
return $response;
775+
}
776+
777+
/**
778+
* Get the advise campaign information to be used in the Blaze create campaign flow.
779+
*
780+
* If Jetpack Sync still is running, this endpoint will read local DB data and provide additional information to the WPCOM endpoint.
781+
*
782+
* @param string $urn The request urn.
783+
* @return array|WP_Error
784+
*/
785+
public function get_dsp_advise_campaign_local( $urn ) {
786+
$parsed_urn = $this->get_data_from_urn( $urn );
787+
$site_id = $this->get_site_id();
788+
789+
if ( is_wp_error( $site_id ) ) {
790+
return array();
791+
}
792+
793+
if ( ! $parsed_urn['site_id'] || $parsed_urn['site_id'] !== $site_id ) {
794+
return $this->get_forbidden_error();
795+
}
796+
797+
$post = get_post( $parsed_urn['post_id'] );
798+
if ( ! $post ) {
799+
return new WP_Error( 'post_not_found', esc_html__( 'Post not found', 'jetpack-blaze' ), array( 'status' => 404 ) );
800+
}
801+
802+
$rendered_content = apply_filters( 'the_content', $post->post_content );
803+
804+
$body = array(
805+
'wp_post' => array(
806+
'ID' => $post->ID,
807+
'title' => $post->post_title,
808+
'URL' => get_permalink( $post ),
809+
'type' => $post->post_type,
810+
'content' => $rendered_content,
811+
),
812+
);
813+
814+
return $this->request_as_user(
815+
sprintf( '/sites/%d/wordads/dsp/api/v1/advise/campaign/%s', $site_id, $urn ),
816+
'v2',
817+
array( 'method' => 'POST' ),
818+
$body
819+
);
820+
}
821+
726822
/**
727823
* Redirect GET requests to the WordAds DSP Templates endpoint for the site.
728824
*
@@ -733,6 +829,38 @@ public function get_dsp_templates( $req ) {
733829
return $this->get_dsp_generic( 'v1/templates', $req );
734830
}
735831

832+
/**
833+
* Redirect GET requests to the WordAds DSP Advise Campaign endpoint for the site.
834+
*
835+
* @param WP_REST_Request $req The request object.
836+
* @return array|WP_Error
837+
*/
838+
public function get_dsp_advise_campaign( $req ) {
839+
$urn = $req->get_param( 'urn' ) ?? '';
840+
841+
$sync_ready = $this->are_posts_ready();
842+
843+
$response = $sync_ready ?
844+
$this->get_dsp_generic( 'v1/advise/campaign/' . $urn, $req ) :
845+
$this->get_dsp_advise_campaign_local( $urn );
846+
847+
if ( ! is_wp_error( $response ) && is_array( $response ) ) {
848+
$response['sync_ready'] = $sync_ready;
849+
}
850+
851+
return $response;
852+
}
853+
854+
/**
855+
* Redirect GET requests to the WordAds DSP Advise endpoint for the site.
856+
*
857+
* @param WP_REST_Request $req The request object.
858+
* @return array|WP_Error
859+
*/
860+
public function get_dsp_advise( $req ) {
861+
return $this->get_dsp_generic( 'v1/advise', $req );
862+
}
863+
736864
/**
737865
* Redirect GET requests to WordAds DSP Subscriptions endpoint for the site.
738866
*

0 commit comments

Comments
 (0)