From 77fe7f2154449922c930af52d5cfd1c86d289d43 Mon Sep 17 00:00:00 2001 From: Vassilis Dimitrakis Date: Wed, 17 Jan 2024 16:59:37 +0200 Subject: [PATCH] Fixes in foreach loop - created a function to reuse the logic - added the same functionality in get_dsp_blaze_posts --- .../src/class-dashboard-rest-controller.php | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/projects/packages/blaze/src/class-dashboard-rest-controller.php b/projects/packages/blaze/src/class-dashboard-rest-controller.php index bc70ddefd5a17..8c7c61ba41847 100644 --- a/projects/packages/blaze/src/class-dashboard-rest-controller.php +++ b/projects/packages/blaze/src/class-dashboard-rest-controller.php @@ -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; } /** @@ -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; } /** @@ -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() ) ) ); + } else { + $posts[ $key ]['price'] = ''; + } + } + + return $posts; + } + /** * Queries the WordPress.com REST API with a user token. *