Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vdimitrakis/add price in posts endpoint #35066

Merged
merged 20 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Added price in blaze/posts endpoint
72 changes: 70 additions & 2 deletions projects/packages/blaze/src/class-dashboard-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,21 @@ public function get_blaze_posts( $req ) {
unset( $req['sub_path'] );
}

return $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 ( is_wp_error( $response ) ) {
return $response;
}

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

return $response;
}

/**
Expand Down Expand Up @@ -379,7 +389,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 @@ -709,6 +729,54 @@ 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_get_price_decimal_separator' ) ||
! function_exists( 'wc_get_price_thousand_separator' ) ||
! function_exists( 'wc_get_price_decimals' ) ||
! function_exists( 'get_woocommerce_price_format' ) ||
! function_exists( 'get_woocommerce_currency_symbol' )
) {
return $posts;
}

foreach ( $posts as $key => $item ) {
if ( ! isset( $item['ID'] ) ) {
$posts[ $key ]['price'] = '';
continue;
}
$product = wc_get_product( $item['ID'] );
if ( $product ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may have to be more defensive here, just like in #35566, to avoid fatals in some scenarios.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeherve check out this comment by Panos

#35066 (comment)

i think we should be ok

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid that's not going to be enough. In some scenarios, Woo extensions may change what's returned there. That's something we ran into a few times in the past, and why we ended up checking for $product instanceof WC_Product in the PR I linked to earlier. Also see #11301 for another example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok fixed, added the extra condition

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being extra defensive doesn't hurt :)


$price = $product->get_price();
$decimal_separator = wc_get_price_decimal_separator();
$thousand_separator = wc_get_price_thousand_separator();
$decimals = wc_get_price_decimals();
$price_format = get_woocommerce_price_format();
$currency_symbol = get_woocommerce_currency_symbol();

// Convert to float to avoid issues on PHP 8.
$price = (float) $price;
$negative = $price < 0;
$price = $negative ? $price * -1 : $price;
$price = number_format( $price, $decimals, $decimal_separator, $thousand_separator );
$formatted_price = sprintf( $price_format, $currency_symbol, $price );

$posts[ $key ]['price'] = html_entity_decode( $formatted_price, ENT_COMPAT );
} else {
$posts[ $key ]['price'] = '';
}
}
return $posts;
}

/**
* Queries the WordPress.com REST API with a user token.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Added price in blaze/posts endpoint
Loading