Merged in feature/117-dev-dev01 (pull request #8)
auto-patch 117-dev-dev01-2023-12-15T16_09_06 * auto-patch 117-dev-dev01-2023-12-15T16_09_06
This commit is contained in:
@@ -82,7 +82,7 @@ class OnboardingFreeExtensions extends WC_REST_Data_Controller {
|
||||
/**
|
||||
* Allows removing Jetpack suggestions from WooCommerce Admin when false.
|
||||
*
|
||||
* In this instance it is removed from the list of extensions suggested in the Onboarding Profiler. This list is first retrieved from the WooCommerce.com API, then if a plugin with the 'jetpack' slug is found, it is removed.
|
||||
* In this instance it is removed from the list of extensions suggested in the Onboarding Profiler. This list is first retrieved from the Woo.com API, then if a plugin with the 'jetpack' slug is found, it is removed.
|
||||
*
|
||||
* @since 7.8
|
||||
*/
|
||||
|
||||
@@ -424,14 +424,14 @@ class OnboardingProfile extends \WC_REST_Data_Controller {
|
||||
),
|
||||
'wccom_connected' => array(
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Whether or not the store was connected to WooCommerce.com during the extension flow.', 'woocommerce' ),
|
||||
'description' => __( 'Whether or not the store was connected to Woo.com during the extension flow.', 'woocommerce' ),
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
),
|
||||
'is_agree_marketing' => array(
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Whether or not this store agreed to receiving marketing contents from WooCommerce.com.', 'woocommerce' ),
|
||||
'description' => __( 'Whether or not this store agreed to receiving marketing contents from Woo.com.', 'woocommerce' ),
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
|
||||
@@ -249,8 +249,6 @@ class OnboardingThemes extends \WC_REST_Data_Controller {
|
||||
);
|
||||
}
|
||||
|
||||
$current_theme_slug = wp_get_theme()->get_stylesheet();
|
||||
|
||||
// To be implemented: 1. Fetch themes from the marketplace API. 2. Convert prices to the requested currency.
|
||||
// These are Dotcom themes.
|
||||
$themes = array(
|
||||
@@ -260,7 +258,6 @@ class OnboardingThemes extends \WC_REST_Data_Controller {
|
||||
'color_palettes' => array(),
|
||||
'total_palettes' => 0,
|
||||
'slug' => 'tsubaki',
|
||||
'is_active' => 'tsubaki' === $current_theme_slug,
|
||||
'thumbnail_url' => 'https://i0.wp.com/s2.wp.com/wp-content/themes/premium/tsubaki/screenshot.png',
|
||||
'link_url' => 'https://wordpress.com/theme/tsubaki/',
|
||||
),
|
||||
@@ -270,7 +267,6 @@ class OnboardingThemes extends \WC_REST_Data_Controller {
|
||||
'color_palettes' => array(),
|
||||
'total_palettes' => 0,
|
||||
'slug' => 'tazza',
|
||||
'is_active' => 'tazza' === $current_theme_slug,
|
||||
'thumbnail_url' => 'https://i0.wp.com/s2.wp.com/wp-content/themes/premium/tazza/screenshot.png',
|
||||
'link_url' => 'https://wordpress.com/theme/tazza/',
|
||||
'total_palettes' => 0,
|
||||
@@ -302,7 +298,6 @@ class OnboardingThemes extends \WC_REST_Data_Controller {
|
||||
),
|
||||
'total_palettes' => 5,
|
||||
'slug' => 'amulet',
|
||||
'is_active' => 'amulet' === $current_theme_slug,
|
||||
'thumbnail_url' => 'https://i0.wp.com/s2.wp.com/wp-content/themes/premium/amulet/screenshot.png',
|
||||
'link_url' => 'https://wordpress.com/theme/amulet/',
|
||||
),
|
||||
@@ -333,7 +328,6 @@ class OnboardingThemes extends \WC_REST_Data_Controller {
|
||||
),
|
||||
'total_palettes' => 11,
|
||||
'slug' => 'zaino',
|
||||
'is_active' => 'zaino' === $current_theme_slug,
|
||||
'thumbnail_url' => 'https://i0.wp.com/s2.wp.com/wp-content/themes/premium/zaino/screenshot.png',
|
||||
'link_url' => 'https://wordpress.com/theme/zaino/',
|
||||
),
|
||||
@@ -374,12 +368,27 @@ class OnboardingThemes extends \WC_REST_Data_Controller {
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
return apply_filters(
|
||||
$filtered_response = apply_filters(
|
||||
'__experimental_woocommerce_rest_get_recommended_themes',
|
||||
$response,
|
||||
$industry,
|
||||
$currency
|
||||
);
|
||||
|
||||
/**
|
||||
* Loop through themes checking to see if any are currently active
|
||||
*/
|
||||
$active_theme = get_stylesheet();
|
||||
|
||||
foreach ( $filtered_response['themes'] as &$theme ) {
|
||||
if ( $theme['slug'] === $active_theme ) {
|
||||
$theme['is_active'] = true;
|
||||
} else {
|
||||
$theme['is_active'] = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $filtered_response;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -218,6 +218,7 @@ class Options extends \WC_REST_Data_Controller {
|
||||
'woocommerce_customize_store_ai_suggestions',
|
||||
'woocommerce_admin_customize_store_completed',
|
||||
'woocommerce_admin_customize_store_completed_theme_id',
|
||||
'woocommerce_admin_customize_store_survey_completed',
|
||||
// WC Test helper options.
|
||||
'wc-admin-test-helper-rest-api-filters',
|
||||
'wc_admin_helper_feature_values',
|
||||
|
||||
@@ -420,12 +420,12 @@ class Plugins extends \WC_REST_Data_Controller {
|
||||
/**
|
||||
* Kicks off the WCCOM Connect process.
|
||||
*
|
||||
* @return WP_Error|array Connection URL for WooCommerce.com
|
||||
* @return WP_Error|array Connection URL for Woo.com
|
||||
*/
|
||||
public function request_wccom_connect() {
|
||||
include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-api.php';
|
||||
if ( ! class_exists( 'WC_Helper_API' ) ) {
|
||||
return new \WP_Error( 'woocommerce_rest_helper_not_active', __( 'There was an error loading the WooCommerce.com Helper API.', 'woocommerce' ), 404 );
|
||||
return new \WP_Error( 'woocommerce_rest_helper_not_active', __( 'There was an error loading the Woo.com Helper API.', 'woocommerce' ), 404 );
|
||||
}
|
||||
|
||||
$redirect_uri = wc_admin_url( '&task=connect&wccom-connected=1' );
|
||||
@@ -442,12 +442,12 @@ class Plugins extends \WC_REST_Data_Controller {
|
||||
|
||||
$code = wp_remote_retrieve_response_code( $request );
|
||||
if ( 200 !== $code ) {
|
||||
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to WooCommerce.com. Please try again.', 'woocommerce' ), 500 );
|
||||
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to Woo.com. Please try again.', 'woocommerce' ), 500 );
|
||||
}
|
||||
|
||||
$secret = json_decode( wp_remote_retrieve_body( $request ) );
|
||||
if ( empty( $secret ) ) {
|
||||
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to WooCommerce.com. Please try again.', 'woocommerce' ), 500 );
|
||||
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to Woo.com. Please try again.', 'woocommerce' ), 500 );
|
||||
}
|
||||
|
||||
do_action( 'woocommerce_helper_connect_start' );
|
||||
@@ -477,7 +477,7 @@ class Plugins extends \WC_REST_Data_Controller {
|
||||
}
|
||||
|
||||
/**
|
||||
* Finishes connecting to WooCommerce.com.
|
||||
* Finishes connecting to Woo.com.
|
||||
*
|
||||
* @param object $rest_request Request details.
|
||||
* @return WP_Error|array Contains success status.
|
||||
@@ -488,7 +488,7 @@ class Plugins extends \WC_REST_Data_Controller {
|
||||
include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-updater.php';
|
||||
include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-options.php';
|
||||
if ( ! class_exists( 'WC_Helper_API' ) ) {
|
||||
return new \WP_Error( 'woocommerce_rest_helper_not_active', __( 'There was an error loading the WooCommerce.com Helper API.', 'woocommerce' ), 404 );
|
||||
return new \WP_Error( 'woocommerce_rest_helper_not_active', __( 'There was an error loading the Woo.com Helper API.', 'woocommerce' ), 404 );
|
||||
}
|
||||
|
||||
// Obtain an access token.
|
||||
@@ -504,12 +504,12 @@ class Plugins extends \WC_REST_Data_Controller {
|
||||
|
||||
$code = wp_remote_retrieve_response_code( $request );
|
||||
if ( 200 !== $code ) {
|
||||
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to WooCommerce.com. Please try again.', 'woocommerce' ), 500 );
|
||||
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to Woo.com. Please try again.', 'woocommerce' ), 500 );
|
||||
}
|
||||
|
||||
$access_token = json_decode( wp_remote_retrieve_body( $request ), true );
|
||||
if ( ! $access_token ) {
|
||||
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to WooCommerce.com. Please try again.', 'woocommerce' ), 500 );
|
||||
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to Woo.com. Please try again.', 'woocommerce' ), 500 );
|
||||
}
|
||||
|
||||
\WC_Helper_Options::update(
|
||||
@@ -525,7 +525,7 @@ class Plugins extends \WC_REST_Data_Controller {
|
||||
|
||||
if ( ! \WC_Helper::_flush_authentication_cache() ) {
|
||||
\WC_Helper_Options::update( 'auth', array() );
|
||||
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to WooCommerce.com. Please try again.', 'woocommerce' ), 500 );
|
||||
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to Woo.com. Please try again.', 'woocommerce' ), 500 );
|
||||
}
|
||||
|
||||
delete_transient( '_woocommerce_helper_subscriptions' );
|
||||
|
||||
@@ -42,6 +42,47 @@ final class ProductsLowInStock extends \WC_REST_Products_Controller {
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
)
|
||||
);
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'products/count-low-in-stock',
|
||||
array(
|
||||
'args' => array(),
|
||||
array(
|
||||
'methods' => \WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_low_in_stock_count' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_low_in_stock_count_params(),
|
||||
),
|
||||
'schema' => array( $this, 'get_low_in_stock_count_schema' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return # of low in stock count.
|
||||
*
|
||||
* @param WP_REST_Request $request request object.
|
||||
*
|
||||
* @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response
|
||||
*/
|
||||
public function get_low_in_stock_count( $request ) {
|
||||
global $wpdb;
|
||||
$status = $request->get_param( 'status' );
|
||||
$low_stock_threshold = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
|
||||
|
||||
$sidewide_stock_threshold_only = $this->is_using_sitewide_stock_threshold_only();
|
||||
$count_query_string = $this->get_count_query( $sidewide_stock_threshold_only );
|
||||
$count_query_results = $wpdb->get_results(
|
||||
// phpcs:ignore -- not sure why phpcs complains about this line when prepare() is used here.
|
||||
$wpdb->prepare( $count_query_string, $status, $low_stock_threshold ),
|
||||
);
|
||||
|
||||
$total_results = (int) $count_query_results[0]->total;
|
||||
$response = rest_ensure_response( array( 'total' => $total_results ) );
|
||||
$response->header( 'X-WP-Total', $total_results );
|
||||
$response->header( 'X-WP-TotalPages', 0 );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,7 +194,9 @@ final class ProductsLowInStock extends \WC_REST_Products_Controller {
|
||||
$offset = ( $page - 1 ) * $per_page;
|
||||
$low_stock_threshold = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
|
||||
|
||||
$query_string = $this->get_query( $this->is_using_sitewide_stock_threshold_only() );
|
||||
$sidewide_stock_threshold_only = $this->is_using_sitewide_stock_threshold_only();
|
||||
|
||||
$query_string = $this->get_query( $sidewide_stock_threshold_only );
|
||||
|
||||
$query_results = $wpdb->get_results(
|
||||
// phpcs:ignore -- not sure why phpcs complains about this line when prepare() is used here.
|
||||
@@ -161,7 +204,13 @@ final class ProductsLowInStock extends \WC_REST_Products_Controller {
|
||||
OBJECT_K
|
||||
);
|
||||
|
||||
$total_results = $wpdb->get_var( 'SELECT FOUND_ROWS()' );
|
||||
$count_query_string = $this->get_count_query( $sidewide_stock_threshold_only );
|
||||
$count_query_results = $wpdb->get_results(
|
||||
// phpcs:ignore -- not sure why phpcs complains about this line when prepare() is used here.
|
||||
$wpdb->prepare( $count_query_string, $status, $low_stock_threshold ),
|
||||
);
|
||||
|
||||
$total_results = $count_query_results[0]->total;
|
||||
|
||||
return array(
|
||||
'results' => $query_results,
|
||||
@@ -213,19 +262,22 @@ final class ProductsLowInStock extends \WC_REST_Products_Controller {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a query.
|
||||
* Return a query string for low in stock products.
|
||||
* The query string incldues the following replacement strings:
|
||||
* - :selects
|
||||
* - :postmeta_join
|
||||
* - :postmeta_wheres
|
||||
* - :orderAndLimit
|
||||
*
|
||||
* @param bool $siteside_only generates a query for sitewide low stock threshold only query.
|
||||
* @param array $replacements of replacement strings.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_query( $siteside_only = false ) {
|
||||
private function get_base_query( $replacements = array() ) {
|
||||
global $wpdb;
|
||||
$query = "
|
||||
SELECT
|
||||
SQL_CALC_FOUND_ROWS wp_posts.*,
|
||||
:postmeta_select
|
||||
wc_product_meta_lookup.stock_quantity
|
||||
:selects
|
||||
FROM
|
||||
{$wpdb->wc_product_meta_lookup} wc_product_meta_lookup
|
||||
LEFT JOIN {$wpdb->posts} wp_posts ON wp_posts.ID = wc_product_meta_lookup.product_id
|
||||
@@ -236,21 +288,26 @@ final class ProductsLowInStock extends \WC_REST_Products_Controller {
|
||||
AND wc_product_meta_lookup.stock_quantity IS NOT NULL
|
||||
AND wc_product_meta_lookup.stock_status IN('instock', 'outofstock')
|
||||
:postmeta_wheres
|
||||
order by wc_product_meta_lookup.product_id DESC
|
||||
limit %d, %d
|
||||
:orderAndLimit
|
||||
";
|
||||
|
||||
$postmeta = array(
|
||||
'select' => '',
|
||||
'join' => '',
|
||||
'wheres' => 'AND wc_product_meta_lookup.stock_quantity <= %d',
|
||||
);
|
||||
return strtr( $query, $replacements );
|
||||
}
|
||||
|
||||
if ( ! $siteside_only ) {
|
||||
$postmeta['select'] = 'meta.meta_value AS low_stock_amount,';
|
||||
$postmeta['join'] = "LEFT JOIN {$wpdb->postmeta} AS meta ON wp_posts.ID = meta.post_id
|
||||
AND meta.meta_key = '_low_stock_amount'";
|
||||
$postmeta['wheres'] = "AND (
|
||||
/**
|
||||
* Add sitewide stock query string to base query string.
|
||||
*
|
||||
* @param string $query Base query string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function add_sitewide_stock_query_str( $query ) {
|
||||
global $wpdb;
|
||||
$postmeta = array(
|
||||
'select' => 'meta.meta_value AS low_stock_amount,',
|
||||
'join' => "LEFT JOIN {$wpdb->postmeta} AS meta ON wp_posts.ID = meta.post_id
|
||||
AND meta.meta_key = '_low_stock_amount'",
|
||||
'wheres' => "AND (
|
||||
(
|
||||
meta.meta_value > ''
|
||||
AND wc_product_meta_lookup.stock_quantity <= CAST(
|
||||
@@ -264,8 +321,8 @@ final class ProductsLowInStock extends \WC_REST_Products_Controller {
|
||||
)
|
||||
AND wc_product_meta_lookup.stock_quantity <= %d
|
||||
)
|
||||
)";
|
||||
}
|
||||
)",
|
||||
);
|
||||
|
||||
return strtr(
|
||||
$query,
|
||||
@@ -277,6 +334,64 @@ final class ProductsLowInStock extends \WC_REST_Products_Controller {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a query.
|
||||
*
|
||||
* @param bool $sitewide_only generates a query for sitewide low stock threshold only query.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_query( $sitewide_only = false ) {
|
||||
$query = $this->get_base_query(
|
||||
array(
|
||||
':selects' => 'wp_posts.*, :postmeta_select wc_product_meta_lookup.stock_quantity',
|
||||
':orderAndLimit' => 'order by wc_product_meta_lookup.product_id DESC limit %d, %d',
|
||||
)
|
||||
);
|
||||
|
||||
if ( ! $sitewide_only ) {
|
||||
return $this->add_sitewide_stock_query_str( $query );
|
||||
}
|
||||
|
||||
return strtr(
|
||||
$query,
|
||||
array(
|
||||
':postmeta_select' => '',
|
||||
':postmeta_join' => '',
|
||||
':postmeta_wheres' => 'AND wc_product_meta_lookup.stock_quantity <= %d',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a count query.
|
||||
*
|
||||
* @param bool $sitewide_only generates a query for sitewide low stock threshold only query.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_count_query( $sitewide_only = false ) {
|
||||
$query = $this->get_base_query(
|
||||
array(
|
||||
':selects' => 'count(*) as total',
|
||||
':orderAndLimit' => '',
|
||||
)
|
||||
);
|
||||
|
||||
if ( ! $sitewide_only ) {
|
||||
return $this->add_sitewide_stock_query_str( $query );
|
||||
}
|
||||
|
||||
return strtr(
|
||||
$query,
|
||||
array(
|
||||
':postmeta_select' => '',
|
||||
':postmeta_join' => '',
|
||||
':postmeta_wheres' => 'AND wc_product_meta_lookup.stock_quantity <= %d',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections of attachments.
|
||||
*
|
||||
@@ -316,4 +431,44 @@ final class ProductsLowInStock extends \WC_REST_Products_Controller {
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections for /count-low-in-stock endpoint.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_low_in_stock_count_params() {
|
||||
$params = array();
|
||||
$params['context'] = $this->get_context_param();
|
||||
$params['context']['default'] = 'view';
|
||||
$params['status'] = array(
|
||||
'default' => 'publish',
|
||||
'description' => __( 'Limit result set to products assigned a specific status.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'enum' => array_merge( array_keys( get_post_statuses() ), array( 'future' ) ),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the schema for /count-low-in-stock response.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_low_in_stock_count_schema() {
|
||||
return array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'Count Low in Stock Items',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'total' => 'integer',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user