Skip to content

Commit

Permalink
Fixes in foreach loop
Browse files Browse the repository at this point in the history
- created a function to reuse the logic
- added the same functionality in get_dsp_blaze_posts
  • Loading branch information
therocket-gr committed Jan 17, 2024
1 parent 0f9c7b0 commit 77fe7f2
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions projects/packages/blaze/src/class-dashboard-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,24 +320,21 @@ public function get_blaze_posts( $req ) {
unset( $req['sub_path'] );
}

$res = $this->request_as_user(
$response = $this->request_as_user(
sprintf( '/sites/%d/blaze/posts%s', $site_id, $this->build_subpath_with_query_strings( $req->get_params() ) ),
'v2',
array( 'method' => 'GET' )
);

if ( ! function_exists( 'wc_get_product' ) || ! function_exists( 'wc_price' ) ) {
return $res;
if ( is_wp_error( $response ) ) {
return $response;
}

foreach ( $res['posts'] as $key => $post ) {
$product = wc_get_product( $post['ID'] );
if ( $product !== false ) {
$res['posts'][ $key ]['price'] = wp_strip_all_tags( wc_price( $product->price ) );
}
if ( isset( $response['posts'] ) && count( $response['posts'] ) > 0 ) {
$response['posts'] = $this->add_prices_in_posts( $response['posts'] );
}

return $res;
return $response;
}

/**
Expand Down Expand Up @@ -381,7 +378,17 @@ public function get_dsp_blaze_posts( $req ) {
unset( $req['sub_path'] );
}

return $this->get_dsp_generic( sprintf( 'v1/wpcom/sites/%d/blaze/posts', $site_id ), $req );
$response = $this->get_dsp_generic( sprintf( 'v1/wpcom/sites/%d/blaze/posts', $site_id ), $req );

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

if ( isset( $response['results'] ) && count( $response['results'] ) > 0 ) {
$response['results'] = $this->add_prices_in_posts( $response['results'] );
}

return $response;
}

/**
Expand Down Expand Up @@ -652,6 +659,30 @@ public function edit_dsp_generic( $path, $req, $args = array() ) {
);
}

/**
* Will check the posts for prices and add them to the posts array
*
* @param WP_REST_Request $posts The posts object.
* @return array|WP_Error
*/
protected function add_prices_in_posts( $posts ) {

if ( ! function_exists( 'wc_get_product' ) || ! function_exists( 'wc_price' ) ) {
return $posts;
}

foreach ( $posts as $key => $post ) {
$product = wc_get_product( $post['ID'] );
if ( $product !== false ) {
$posts[ $key ]['price'] = html_entity_decode( wp_strip_all_tags( wc_price( $product->get_price() ) ) );

Check failure on line 677 in projects/packages/blaze/src/class-dashboard-rest-controller.php

View workflow job for this annotation

GitHub Actions / dev branch for PHP 8.0

The default value of the $flags parameter for html_entity_decode() was changed from ENT_COMPAT to ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 in PHP 8.1. For cross-version compatibility, the $flags parameter should be explicitly set. (PHPCompatibility.ParameterValues.NewHTMLEntitiesFlagsDefault.NotSet)
} else {
$posts[ $key ]['price'] = '';
}
}

return $posts;
}

/**
* Queries the WordPress.com REST API with a user token.
*
Expand Down

0 comments on commit 77fe7f2

Please sign in to comment.