File: /var/www/vhosts/uyarreklam.com.tr/httpdocs/Admin.tar
API/Coupons.php 0000644 00000004232 15153746747 0007340 0 ustar 00 <?php
/**
* REST API Coupons Controller
*
* Handles requests to /coupons/*
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Coupons controller.
*
* @internal
* @extends WC_REST_Coupons_Controller
*/
class Coupons extends \WC_REST_Coupons_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['search'] = array(
'description' => __( 'Limit results to coupons with codes matching a given string.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Add coupon code searching to the WC API.
*
* @param WP_REST_Request $request Request data.
* @return array
*/
protected function prepare_objects_query( $request ) {
$args = parent::prepare_objects_query( $request );
if ( ! empty( $request['search'] ) ) {
$args['search'] = $request['search'];
$args['s'] = false;
}
return $args;
}
/**
* Get a collection of posts and add the code search option to WP_Query.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
add_filter( 'posts_where', array( __CLASS__, 'add_wp_query_search_code_filter' ), 10, 2 );
$response = parent::get_items( $request );
remove_filter( 'posts_where', array( __CLASS__, 'add_wp_query_search_code_filter' ), 10 );
return $response;
}
/**
* Add code searching to the WP Query
*
* @internal
* @param string $where Where clause used to search posts.
* @param object $wp_query WP_Query object.
* @return string
*/
public static function add_wp_query_search_code_filter( $where, $wp_query ) {
global $wpdb;
$search = $wp_query->get( 'search' );
if ( $search ) {
$code_like = '%' . $wpdb->esc_like( $search ) . '%';
$where .= $wpdb->prepare( "AND {$wpdb->posts}.post_title LIKE %s", $code_like );
}
return $where;
}
}
API/CustomAttributeTraits.php 0000644 00000006634 15153746747 0012247 0 ustar 00 <?php
/**
* Traits for handling custom product attributes and their terms.
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* CustomAttributeTraits class.
*
* @internal
*/
trait CustomAttributeTraits {
/**
* Get a single attribute by its slug.
*
* @internal
* @param string $slug The attribute slug.
* @return WP_Error|object The matching attribute object or WP_Error if not found.
*/
public function get_custom_attribute_by_slug( $slug ) {
$matching_attributes = $this->get_custom_attributes( array( 'slug' => $slug ) );
if ( empty( $matching_attributes ) ) {
return new \WP_Error(
'woocommerce_rest_product_attribute_not_found',
__( 'No product attribute with that slug was found.', 'woocommerce' ),
array( 'status' => 404 )
);
}
foreach ( $matching_attributes as $attribute_key => $attribute_value ) {
return array( $attribute_key => $attribute_value );
}
}
/**
* Query custom attributes by name or slug.
*
* @param string $args Search arguments, either name or slug.
* @return array Matching attributes, formatted for response.
*/
protected function get_custom_attributes( $args ) {
global $wpdb;
$args = wp_parse_args(
$args,
array(
'name' => '',
'slug' => '',
)
);
if ( empty( $args['name'] ) && empty( $args['slug'] ) ) {
return array();
}
$mode = $args['name'] ? 'name' : 'slug';
if ( 'name' === $mode ) {
$name = $args['name'];
// Get as close as we can to matching the name property of custom attributes using SQL.
$like = '%"name";s:%:"%' . $wpdb->esc_like( $name ) . '%"%';
} else {
$slug = sanitize_title_for_query( $args['slug'] );
// Get as close as we can to matching the slug property of custom attributes using SQL.
$like = '%s:' . strlen( $slug ) . ':"' . $slug . '";a:6:{%';
}
// Find all serialized product attributes with names like the search string.
$query_results = $wpdb->get_results(
$wpdb->prepare(
"SELECT meta_value
FROM {$wpdb->postmeta}
WHERE meta_key = '_product_attributes'
AND meta_value LIKE %s
LIMIT 100",
$like
),
ARRAY_A
);
$custom_attributes = array();
foreach ( $query_results as $raw_product_attributes ) {
$meta_attributes = maybe_unserialize( $raw_product_attributes['meta_value'] );
if ( empty( $meta_attributes ) || ! is_array( $meta_attributes ) ) {
continue;
}
foreach ( $meta_attributes as $meta_attribute_key => $meta_attribute_value ) {
$meta_value = array_merge(
array(
'name' => '',
'is_taxonomy' => 0,
),
(array) $meta_attribute_value
);
// Skip non-custom attributes.
if ( ! empty( $meta_value['is_taxonomy'] ) ) {
continue;
}
// Skip custom attributes that didn't match the query.
// (There can be any number of attributes in the meta value).
if ( ( 'name' === $mode ) && ( false === stripos( $meta_value['name'], $name ) ) ) {
continue;
}
if ( ( 'slug' === $mode ) && ( $meta_attribute_key !== $slug ) ) {
continue;
}
// Combine all values when there are multiple matching custom attributes.
if ( isset( $custom_attributes[ $meta_attribute_key ] ) ) {
$custom_attributes[ $meta_attribute_key ]['value'] .= ' ' . WC_DELIMITER . ' ' . $meta_value['value'];
} else {
$custom_attributes[ $meta_attribute_key ] = $meta_attribute_value;
}
}
}
return $custom_attributes;
}
}
API/Customers.php 0000644 00000004163 15153746747 0007701 0 ustar 00 <?php
/**
* REST API Customers Controller
*
* Handles requests to /customers/*
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Customers controller.
*
* @internal
* @extends \Automattic\WooCommerce\Admin\API\Reports\Customers\Controller
*/
class Customers extends \Automattic\WooCommerce\Admin\API\Reports\Customers\Controller {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'customers';
/**
* Register the routes for customers.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d-]+)',
array(
'args' => array(
'id' => array(
'description' => __( 'Unique ID for the resource.', 'woocommerce' ),
'type' => 'integer',
),
),
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = parent::prepare_reports_query( $request );
$args['customers'] = $request['include'];
return $args;
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['include'] = $params['customers'];
unset( $params['customers'] );
return $params;
}
}
API/Data.php 0000644 00000001653 15153746747 0006567 0 ustar 00 <?php
/**
* REST API Data Controller
*
* Handles requests to /data
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Data controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class Data extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Return the list of data resources.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$response = parent::get_items( $request );
$response->data[] = $this->prepare_response_for_collection(
$this->prepare_item_for_response(
(object) array(
'slug' => 'download-ips',
'description' => __( 'An endpoint used for searching download logs for a specific IP address.', 'woocommerce' ),
),
$request
)
);
return $response;
}
}
API/DataCountries.php 0000644 00000002175 15153746747 0010463 0 ustar 00 <?php
/**
* REST API Data countries controller.
*
* Handles requests to the /data/countries endpoint.
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* REST API Data countries controller class.
*
* @internal
* @extends WC_REST_Data_Countries_Controller
*/
class DataCountries extends \WC_REST_Data_Countries_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Register routes.
*
* @since 3.5.0
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/locales',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_locales' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
parent::register_routes();
}
/**
* Get country fields.
*
* @return array
*/
public function get_locales() {
$locales = WC()->countries->get_country_locale();
return rest_ensure_response( $locales );
}
}
API/DataDownloadIPs.php 0000644 00000010230 15153746747 0010662 0 ustar 00 <?php
/**
* REST API Data Download IP Controller
*
* Handles requests to /data/download-ips
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Data Download IP controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class DataDownloadIPs extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'data/download-ips';
/**
* Register routes.
*
* @since 3.5.0
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Return the download IPs matching the passed parameters.
*
* @since 3.5.0
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
global $wpdb;
if ( isset( $request['match'] ) ) {
$downloads = $wpdb->get_results(
$wpdb->prepare(
"SELECT DISTINCT( user_ip_address ) FROM {$wpdb->prefix}wc_download_log
WHERE user_ip_address LIKE %s
LIMIT 10",
$request['match'] . '%'
)
);
} else {
return new \WP_Error( 'woocommerce_rest_data_download_ips_invalid_request', __( 'Invalid request. Please pass the match parameter.', 'woocommerce' ), array( 'status' => 400 ) );
}
$data = array();
if ( ! empty( $downloads ) ) {
foreach ( $downloads as $download ) {
$response = $this->prepare_item_for_response( $download, $request );
$data[] = $this->prepare_response_for_collection( $response );
}
}
return rest_ensure_response( $data );
}
/**
* Prepare the data object for response.
*
* @since 3.5.0
* @param object $item Data object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $item, $request ) {
$data = $this->add_additional_fields_to_object( $item, $request );
$data = $this->filter_response_by_context( $data, 'view' );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $item ) );
/**
* Filter the list returned from the API.
*
* @param WP_REST_Response $response The response object.
* @param array $item The original item.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_data_download_ip', $response, $item, $request );
}
/**
* Prepare links for the request.
*
* @param object $item Data object.
* @return array Links for the given object.
*/
protected function prepare_links( $item ) {
$links = array(
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
),
);
return $links;
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['match'] = array(
'description' => __( 'A partial IP address can be passed and matching results will be returned.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get the schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'data_download_ips',
'type' => 'object',
'properties' => array(
'user_ip_address' => array(
'type' => 'string',
'description' => __( 'IP address.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
}
API/Experiments.php 0000644 00000003510 15153746747 0010213 0 ustar 00 <?php
/**
* REST API Experiment Controller
*
* Handles requests to /experiment
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Data controller.
*
* @extends WC_REST_Data_Controller
*/
class Experiments extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'experiments';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/assignment',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_assignment' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Forward the experiment request to WP.com and return the WP.com response.
*
* @param \WP_REST_Request $request Request data.
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_assignment( $request ) {
$args = $request->get_query_params();
if ( ! isset( $args['experiment_name'] ) ) {
return new \WP_Error(
'woocommerce_rest_experiment_name_required',
__( 'Sorry, experiment_name is required.', 'woocommerce' ),
array( 'status' => 400 )
);
}
unset( $args['rest_route'] );
$abtest = new \WooCommerce\Admin\Experimental_Abtest(
$request->get_param( 'anon_id' ) ?? '',
'woocommerce',
true, // set consent to true here since frontend has checked it already.
true // set true to send request as auth user.
);
$response = $abtest->request_assignment( $args );
if ( is_wp_error( $response ) ) {
return $response;
}
return json_decode( $response['body'], true );
}
}
API/Features.php 0000644 00000003314 15153746747 0007470 0 ustar 00 <?php
/**
* REST API Features Controller
*
* Handles requests to /features
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Features\Features as FeaturesClass;
/**
* Features Controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class Features extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'features';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_features' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check whether a given request has permission to read onboarding profile data.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Return available payment methods.
*
* @param \WP_REST_Request $request Request data.
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_features( $request ) {
return FeaturesClass::get_available_features();
}
}
API/Init.php 0000644 00000020511 15153746747 0006613 0 ustar 00 <?php
/**
* REST API bootstrap.
*/
namespace Automattic\WooCommerce\Admin\API;
use AllowDynamicProperties;
use Automattic\WooCommerce\Admin\Features\Features;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Internal\Admin\Loader;
/**
* Init class.
*
* @internal
*/
#[AllowDynamicProperties]
class Init {
/**
* The single instance of the class.
*
* @var object
*/
protected static $instance = null;
/**
* Get class instance.
*
* @return object Instance.
*/
final public static function instance() {
if ( null === static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Bootstrap REST API.
*/
public function __construct() {
// Hook in data stores.
add_filter( 'woocommerce_data_stores', array( __CLASS__, 'add_data_stores' ) );
// REST API extensions init.
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
// Add currency symbol to orders endpoint response.
add_filter( 'woocommerce_rest_prepare_shop_order_object', array( __CLASS__, 'add_currency_symbol_to_order_response' ) );
}
/**
* Init REST API.
*/
public function rest_api_init() {
$controllers = array(
'Automattic\WooCommerce\Admin\API\Features',
'Automattic\WooCommerce\Admin\API\Notes',
'Automattic\WooCommerce\Admin\API\NoteActions',
'Automattic\WooCommerce\Admin\API\Coupons',
'Automattic\WooCommerce\Admin\API\Data',
'Automattic\WooCommerce\Admin\API\DataCountries',
'Automattic\WooCommerce\Admin\API\DataDownloadIPs',
'Automattic\WooCommerce\Admin\API\Experiments',
'Automattic\WooCommerce\Admin\API\Marketing',
'Automattic\WooCommerce\Admin\API\MarketingOverview',
'Automattic\WooCommerce\Admin\API\MarketingRecommendations',
'Automattic\WooCommerce\Admin\API\MarketingChannels',
'Automattic\WooCommerce\Admin\API\MarketingCampaigns',
'Automattic\WooCommerce\Admin\API\MarketingCampaignTypes',
'Automattic\WooCommerce\Admin\API\Options',
'Automattic\WooCommerce\Admin\API\Orders',
'Automattic\WooCommerce\Admin\API\PaymentGatewaySuggestions',
'Automattic\WooCommerce\Admin\API\Products',
'Automattic\WooCommerce\Admin\API\ProductAttributes',
'Automattic\WooCommerce\Admin\API\ProductAttributeTerms',
'Automattic\WooCommerce\Admin\API\ProductCategories',
'Automattic\WooCommerce\Admin\API\ProductVariations',
'Automattic\WooCommerce\Admin\API\ProductReviews',
'Automattic\WooCommerce\Admin\API\ProductVariations',
'Automattic\WooCommerce\Admin\API\ProductsLowInStock',
'Automattic\WooCommerce\Admin\API\SettingOptions',
'Automattic\WooCommerce\Admin\API\Themes',
'Automattic\WooCommerce\Admin\API\Plugins',
'Automattic\WooCommerce\Admin\API\OnboardingFreeExtensions',
'Automattic\WooCommerce\Admin\API\OnboardingProductTypes',
'Automattic\WooCommerce\Admin\API\OnboardingProfile',
'Automattic\WooCommerce\Admin\API\OnboardingTasks',
'Automattic\WooCommerce\Admin\API\OnboardingThemes',
'Automattic\WooCommerce\Admin\API\OnboardingPlugins',
'Automattic\WooCommerce\Admin\API\NavigationFavorites',
'Automattic\WooCommerce\Admin\API\Taxes',
'Automattic\WooCommerce\Admin\API\MobileAppMagicLink',
'Automattic\WooCommerce\Admin\API\ShippingPartnerSuggestions',
);
$product_form_controllers = array();
if ( Features::is_enabled( 'new-product-management-experience' ) ) {
$product_form_controllers[] = 'Automattic\WooCommerce\Admin\API\ProductForm';
}
if ( Features::is_enabled( 'analytics' ) ) {
$analytics_controllers = array(
'Automattic\WooCommerce\Admin\API\Customers',
'Automattic\WooCommerce\Admin\API\Leaderboards',
'Automattic\WooCommerce\Admin\API\Reports\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Import\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Export\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Products\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Variations\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Products\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Variations\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Revenue\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Orders\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Categories\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Taxes\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Coupons\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Stock\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Downloads\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Customers\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\Controller',
);
// The performance indicators controller must be registered last, after other /stats endpoints have been registered.
$analytics_controllers[] = 'Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators\Controller';
$controllers = array_merge( $controllers, $analytics_controllers, $product_form_controllers );
}
/**
* Filter for the WooCommerce Admin REST controllers.
*
* @since 3.5.0
* @param array $controllers List of rest API controllers.
*/
$controllers = apply_filters( 'woocommerce_admin_rest_controllers', $controllers );
foreach ( $controllers as $controller ) {
$this->$controller = new $controller();
$this->$controller->register_routes();
}
}
/**
* Adds data stores.
*
* @internal
* @param array $data_stores List of data stores.
* @return array
*/
public static function add_data_stores( $data_stores ) {
return array_merge(
$data_stores,
array(
'report-revenue-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore',
'report-orders' => 'Automattic\WooCommerce\Admin\API\Reports\Orders\DataStore',
'report-orders-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore',
'report-products' => 'Automattic\WooCommerce\Admin\API\Reports\Products\DataStore',
'report-variations' => 'Automattic\WooCommerce\Admin\API\Reports\Variations\DataStore',
'report-products-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Products\Stats\DataStore',
'report-variations-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Variations\Stats\DataStore',
'report-categories' => 'Automattic\WooCommerce\Admin\API\Reports\Categories\DataStore',
'report-taxes' => 'Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore',
'report-taxes-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\DataStore',
'report-coupons' => 'Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore',
'report-coupons-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\DataStore',
'report-downloads' => 'Automattic\WooCommerce\Admin\API\Reports\Downloads\DataStore',
'report-downloads-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats\DataStore',
'admin-note' => 'Automattic\WooCommerce\Admin\Notes\DataStore',
'report-customers' => 'Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore',
'report-customers-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\DataStore',
'report-stock-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\DataStore',
)
);
}
/**
* Add the currency symbol (in addition to currency code) to each Order
* object in REST API responses. For use in formatAmount().
*
* @internal
* @param {WP_REST_Response} $response REST response object.
* @returns {WP_REST_Response}
*/
public static function add_currency_symbol_to_order_response( $response ) {
$response_data = $response->get_data();
$currency_code = $response_data['currency'];
$currency_symbol = get_woocommerce_currency_symbol( $currency_code );
$response_data['currency_symbol'] = html_entity_decode( $currency_symbol );
$response->set_data( $response_data );
return $response;
}
}
API/Leaderboards.php 0000644 00000043317 15153746747 0010310 0 ustar 00 <?php
/**
* REST API Leaderboards Controller
*
* Handles requests to /leaderboards
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Categories\DataStore as CategoriesDataStore;
use Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore as CouponsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
use Automattic\WooCommerce\Admin\API\Reports\Products\DataStore as ProductsDataStore;
/**
* Leaderboards controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class Leaderboards extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'leaderboards';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/allowed',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_allowed_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_allowed_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<leaderboard>\w+)',
array(
'args' => array(
'leaderboard' => array(
'type' => 'string',
'enum' => array( 'customers', 'coupons', 'categories', 'products' ),
),
),
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Get the data for the coupons leaderboard.
*
* @param int $per_page Number of rows.
* @param string $after Items after date.
* @param string $before Items before date.
* @param string $persisted_query URL query string.
*/
protected function get_coupons_leaderboard( $per_page, $after, $before, $persisted_query ) {
$coupons_data_store = new CouponsDataStore();
$coupons_data = $per_page > 0 ? $coupons_data_store->get_data(
apply_filters(
'woocommerce_analytics_coupons_query_args',
array(
'orderby' => 'orders_count',
'order' => 'desc',
'after' => $after,
'before' => $before,
'per_page' => $per_page,
'extended_info' => true,
)
)
)->data : array();
$rows = array();
foreach ( $coupons_data as $coupon ) {
$url_query = wp_parse_args(
array(
'filter' => 'single_coupon',
'coupons' => $coupon['coupon_id'],
),
$persisted_query
);
$coupon_url = wc_admin_url( '/analytics/coupons', $url_query );
$coupon_code = isset( $coupon['extended_info'] ) && isset( $coupon['extended_info']['code'] ) ? $coupon['extended_info']['code'] : '';
$rows[] = array(
array(
'display' => "<a href='{$coupon_url}'>{$coupon_code}</a>",
'value' => $coupon_code,
),
array(
'display' => wc_admin_number_format( $coupon['orders_count'] ),
'value' => $coupon['orders_count'],
),
array(
'display' => wc_price( $coupon['amount'] ),
'value' => $coupon['amount'],
),
);
}
return array(
'id' => 'coupons',
'label' => __( 'Top Coupons - Number of Orders', 'woocommerce' ),
'headers' => array(
array(
'label' => __( 'Coupon code', 'woocommerce' ),
),
array(
'label' => __( 'Orders', 'woocommerce' ),
),
array(
'label' => __( 'Amount discounted', 'woocommerce' ),
),
),
'rows' => $rows,
);
}
/**
* Get the data for the categories leaderboard.
*
* @param int $per_page Number of rows.
* @param string $after Items after date.
* @param string $before Items before date.
* @param string $persisted_query URL query string.
*/
protected function get_categories_leaderboard( $per_page, $after, $before, $persisted_query ) {
$categories_data_store = new CategoriesDataStore();
$categories_data = $per_page > 0 ? $categories_data_store->get_data(
apply_filters(
'woocommerce_analytics_categories_query_args',
array(
'orderby' => 'items_sold',
'order' => 'desc',
'after' => $after,
'before' => $before,
'per_page' => $per_page,
'extended_info' => true,
)
)
)->data : array();
$rows = array();
foreach ( $categories_data as $category ) {
$url_query = wp_parse_args(
array(
'filter' => 'single_category',
'categories' => $category['category_id'],
),
$persisted_query
);
$category_url = wc_admin_url( '/analytics/categories', $url_query );
$category_name = isset( $category['extended_info'] ) && isset( $category['extended_info']['name'] ) ? $category['extended_info']['name'] : '';
$rows[] = array(
array(
'display' => "<a href='{$category_url}'>{$category_name}</a>",
'value' => $category_name,
),
array(
'display' => wc_admin_number_format( $category['items_sold'] ),
'value' => $category['items_sold'],
),
array(
'display' => wc_price( $category['net_revenue'] ),
'value' => $category['net_revenue'],
),
);
}
return array(
'id' => 'categories',
'label' => __( 'Top categories - Items sold', 'woocommerce' ),
'headers' => array(
array(
'label' => __( 'Category', 'woocommerce' ),
),
array(
'label' => __( 'Items sold', 'woocommerce' ),
),
array(
'label' => __( 'Net sales', 'woocommerce' ),
),
),
'rows' => $rows,
);
}
/**
* Get the data for the customers leaderboard.
*
* @param int $per_page Number of rows.
* @param string $after Items after date.
* @param string $before Items before date.
* @param string $persisted_query URL query string.
*/
protected function get_customers_leaderboard( $per_page, $after, $before, $persisted_query ) {
$customers_data_store = new CustomersDataStore();
$customers_data = $per_page > 0 ? $customers_data_store->get_data(
apply_filters(
'woocommerce_analytics_customers_query_args',
array(
'orderby' => 'total_spend',
'order' => 'desc',
'order_after' => $after,
'order_before' => $before,
'per_page' => $per_page,
)
)
)->data : array();
$rows = array();
foreach ( $customers_data as $customer ) {
$url_query = wp_parse_args(
array(
'filter' => 'single_customer',
'customers' => $customer['id'],
),
$persisted_query
);
$customer_url = wc_admin_url( '/analytics/customers', $url_query );
$rows[] = array(
array(
'display' => "<a href='{$customer_url}'>{$customer['name']}</a>",
'value' => $customer['name'],
),
array(
'display' => wc_admin_number_format( $customer['orders_count'] ),
'value' => $customer['orders_count'],
),
array(
'display' => wc_price( $customer['total_spend'] ),
'value' => $customer['total_spend'],
),
);
}
return array(
'id' => 'customers',
'label' => __( 'Top Customers - Total Spend', 'woocommerce' ),
'headers' => array(
array(
'label' => __( 'Customer Name', 'woocommerce' ),
),
array(
'label' => __( 'Orders', 'woocommerce' ),
),
array(
'label' => __( 'Total Spend', 'woocommerce' ),
),
),
'rows' => $rows,
);
}
/**
* Get the data for the products leaderboard.
*
* @param int $per_page Number of rows.
* @param string $after Items after date.
* @param string $before Items before date.
* @param string $persisted_query URL query string.
*/
protected function get_products_leaderboard( $per_page, $after, $before, $persisted_query ) {
$products_data_store = new ProductsDataStore();
$products_data = $per_page > 0 ? $products_data_store->get_data(
apply_filters(
'woocommerce_analytics_products_query_args',
array(
'orderby' => 'items_sold',
'order' => 'desc',
'after' => $after,
'before' => $before,
'per_page' => $per_page,
'extended_info' => true,
)
)
)->data : array();
$rows = array();
foreach ( $products_data as $product ) {
$url_query = wp_parse_args(
array(
'filter' => 'single_product',
'products' => $product['product_id'],
),
$persisted_query
);
$product_url = wc_admin_url( '/analytics/products', $url_query );
$product_name = isset( $product['extended_info'] ) && isset( $product['extended_info']['name'] ) ? $product['extended_info']['name'] : '';
$rows[] = array(
array(
'display' => "<a href='{$product_url}'>{$product_name}</a>",
'value' => $product_name,
),
array(
'display' => wc_admin_number_format( $product['items_sold'] ),
'value' => $product['items_sold'],
),
array(
'display' => wc_price( $product['net_revenue'] ),
'value' => $product['net_revenue'],
),
);
}
return array(
'id' => 'products',
'label' => __( 'Top products - Items sold', 'woocommerce' ),
'headers' => array(
array(
'label' => __( 'Product', 'woocommerce' ),
),
array(
'label' => __( 'Items sold', 'woocommerce' ),
),
array(
'label' => __( 'Net sales', 'woocommerce' ),
),
),
'rows' => $rows,
);
}
/**
* Get an array of all leaderboards.
*
* @param int $per_page Number of rows.
* @param string $after Items after date.
* @param string $before Items before date.
* @param string $persisted_query URL query string.
* @return array
*/
public function get_leaderboards( $per_page, $after, $before, $persisted_query ) {
$leaderboards = array(
$this->get_customers_leaderboard( $per_page, $after, $before, $persisted_query ),
$this->get_coupons_leaderboard( $per_page, $after, $before, $persisted_query ),
$this->get_categories_leaderboard( $per_page, $after, $before, $persisted_query ),
$this->get_products_leaderboard( $per_page, $after, $before, $persisted_query ),
);
return apply_filters( 'woocommerce_leaderboards', $leaderboards, $per_page, $after, $before, $persisted_query );
}
/**
* Return all leaderboards.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$persisted_query = json_decode( $request['persisted_query'], true );
switch ( $request['leaderboard'] ) {
case 'customers':
$leaderboards = array( $this->get_customers_leaderboard( $request['per_page'], $request['after'], $request['before'], $persisted_query ) );
break;
case 'coupons':
$leaderboards = array( $this->get_coupons_leaderboard( $request['per_page'], $request['after'], $request['before'], $persisted_query ) );
break;
case 'categories':
$leaderboards = array( $this->get_categories_leaderboard( $request['per_page'], $request['after'], $request['before'], $persisted_query ) );
break;
case 'products':
$leaderboards = array( $this->get_products_leaderboard( $request['per_page'], $request['after'], $request['before'], $persisted_query ) );
break;
default:
$leaderboards = $this->get_leaderboards( $request['per_page'], $request['after'], $request['before'], $persisted_query );
break;
}
$data = array();
if ( ! empty( $leaderboards ) ) {
foreach ( $leaderboards as $leaderboard ) {
$response = $this->prepare_item_for_response( $leaderboard, $request );
$data[] = $this->prepare_response_for_collection( $response );
}
}
return rest_ensure_response( $data );
}
/**
* Returns a list of allowed leaderboards.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_allowed_items( $request ) {
$leaderboards = $this->get_leaderboards( 0, null, null, null );
$data = array();
foreach ( $leaderboards as $leaderboard ) {
$data[] = (object) array(
'id' => $leaderboard['id'],
'label' => $leaderboard['label'],
'headers' => $leaderboard['headers'],
);
}
$objects = array();
foreach ( $data as $item ) {
$prepared = $this->prepare_item_for_response( $item, $request );
$objects[] = $this->prepare_response_for_collection( $prepared );
}
$response = rest_ensure_response( $objects );
$response->header( 'X-WP-Total', count( $data ) );
$response->header( 'X-WP-TotalPages', 1 );
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
return $response;
}
/**
* Prepare the data object for response.
*
* @param object $item Data object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $item, $request ) {
$data = $this->add_additional_fields_to_object( $item, $request );
$data = $this->filter_response_by_context( $data, 'view' );
$response = rest_ensure_response( $data );
/**
* Filter the list returned from the API.
*
* @param WP_REST_Response $response The response object.
* @param array $item The original item.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_leaderboard', $response, $item, $request );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['page'] = array(
'description' => __( 'Current page of the collection.', 'woocommerce' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);
$params['per_page'] = array(
'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
'type' => 'integer',
'default' => 5,
'minimum' => 1,
'maximum' => 20,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['after'] = array(
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['before'] = array(
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['persisted_query'] = array(
'description' => __( 'URL query to persist across links.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get the schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'leaderboard',
'type' => 'object',
'properties' => array(
'id' => array(
'type' => 'string',
'description' => __( 'Leaderboard ID.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'label' => array(
'type' => 'string',
'description' => __( 'Displayed title for the leaderboard.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'headers' => array(
'type' => 'array',
'description' => __( 'Table headers.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'items' => array(
'type' => 'array',
'properties' => array(
'label' => array(
'description' => __( 'Table column header.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
),
'rows' => array(
'type' => 'array',
'description' => __( 'Table rows.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'items' => array(
'type' => 'array',
'properties' => array(
'display' => array(
'description' => __( 'Table cell display.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'value' => array(
'description' => __( 'Table cell value.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get schema for the list of allowed leaderboards.
*
* @return array $schema
*/
public function get_public_allowed_item_schema() {
$schema = $this->get_public_item_schema();
unset( $schema['properties']['rows'] );
return $schema;
}
}
API/Marketing.php 0000644 00000010175 15153746747 0007636 0 ustar 00 <?php
/**
* REST API Marketing Controller
*
* Handles requests to /marketing.
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Admin\PluginsHelper;
use Automattic\WooCommerce\Internal\Admin\Marketing\MarketingSpecs;
defined( 'ABSPATH' ) || exit;
/**
* Marketing Controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class Marketing extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'marketing';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/recommended',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_recommended_plugins' ),
'permission_callback' => array( $this, 'get_recommended_plugins_permissions_check' ),
'args' => array(
'per_page' => $this->get_collection_params()['per_page'],
'category' => array(
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'sanitize_title_with_dashes',
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/knowledge-base',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_knowledge_base_posts' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => array(
'category' => array(
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'sanitize_title_with_dashes',
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check whether a given request has permission to install plugins.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_recommended_plugins_permissions_check( $request ) {
if ( ! current_user_can( 'install_plugins' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage plugins.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Return installed marketing extensions data.
*
* @param \WP_REST_Request $request Request data.
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_recommended_plugins( $request ) {
/**
* MarketingSpecs class.
*
* @var MarketingSpecs $marketing_specs
*/
$marketing_specs = wc_get_container()->get( MarketingSpecs::class );
// Default to marketing category (if no category set).
$category = ( ! empty( $request->get_param( 'category' ) ) ) ? $request->get_param( 'category' ) : 'marketing';
$all_plugins = $marketing_specs->get_recommended_plugins();
$valid_plugins = [];
$per_page = $request->get_param( 'per_page' );
foreach ( $all_plugins as $plugin ) {
// default to marketing if 'categories' is empty on the plugin object (support for legacy api while testing).
$plugin_categories = ( ! empty( $plugin['categories'] ) ) ? $plugin['categories'] : [ 'marketing' ];
if ( ! PluginsHelper::is_plugin_installed( $plugin['plugin'] ) && in_array( $category, $plugin_categories, true ) ) {
$valid_plugins[] = $plugin;
}
}
return rest_ensure_response( array_slice( $valid_plugins, 0, $per_page ) );
}
/**
* Return installed marketing extensions data.
*
* @param \WP_REST_Request $request Request data.
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_knowledge_base_posts( $request ) {
/**
* MarketingSpecs class.
*
* @var MarketingSpecs $marketing_specs
*/
$marketing_specs = wc_get_container()->get( MarketingSpecs::class );
$category = $request->get_param( 'category' );
return rest_ensure_response( $marketing_specs->get_knowledge_base_posts( $category ) );
}
}
API/MarketingCampaignTypes.php 0000644 00000014020 15153746747 0012314 0 ustar 00 <?php
/**
* REST API MarketingCampaignTypes Controller
*
* Handles requests to /marketing/campaign-types.
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Admin\Marketing\MarketingCampaignType;
use Automattic\WooCommerce\Admin\Marketing\MarketingChannels as MarketingChannelsService;
use WC_REST_Controller;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
defined( 'ABSPATH' ) || exit;
/**
* MarketingCampaignTypes Controller.
*
* @internal
* @extends WC_REST_Controller
* @since x.x.x
*/
class MarketingCampaignTypes extends WC_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'marketing/campaign-types';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Retrieves the query params for the collections.
*
* @return array Query parameters for the collection.
*/
public function get_collection_params() {
$params = parent::get_collection_params();
unset( $params['search'] );
return $params;
}
/**
* Check whether a given request has permission to view marketing campaigns.
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Returns an aggregated array of marketing campaigns for all active marketing channels.
*
* @param WP_REST_Request $request Request data.
*
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
/**
* MarketingChannels class.
*
* @var MarketingChannelsService $marketing_channels_service
*/
$marketing_channels_service = wc_get_container()->get( MarketingChannelsService::class );
// Aggregate the supported campaign types from all registered marketing channels.
$responses = [];
foreach ( $marketing_channels_service->get_registered_channels() as $channel ) {
foreach ( $channel->get_supported_campaign_types() as $campaign_type ) {
$response = $this->prepare_item_for_response( $campaign_type, $request );
$responses[] = $this->prepare_response_for_collection( $response );
}
}
return rest_ensure_response( $responses );
}
/**
* Prepares the item for the REST response.
*
* @param MarketingCampaignType $item WordPress representation of the item.
* @param WP_REST_Request $request Request object.
*
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function prepare_item_for_response( $item, $request ) {
$data = [
'id' => $item->get_id(),
'name' => $item->get_name(),
'description' => $item->get_description(),
'channel' => [
'slug' => $item->get_channel()->get_slug(),
'name' => $item->get_channel()->get_name(),
],
'create_url' => $item->get_create_url(),
'icon_url' => $item->get_icon_url(),
];
$context = $request['context'] ?? 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
return rest_ensure_response( $data );
}
/**
* Retrieves the item's schema, conforming to JSON Schema.
*
* @return array Item schema data.
*/
public function get_item_schema() {
$schema = [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'marketing_campaign_type',
'type' => 'object',
'properties' => [
'id' => [
'description' => __( 'The unique identifier for the marketing campaign type.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'name' => [
'description' => __( 'Name of the marketing campaign type.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'description' => [
'description' => __( 'Description of the marketing campaign type.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'channel' => [
'description' => __( 'The marketing channel that this campaign type belongs to.', 'woocommerce' ),
'type' => 'object',
'context' => [ 'view' ],
'readonly' => true,
'properties' => [
'slug' => [
'description' => __( 'The unique identifier of the marketing channel that this campaign type belongs to.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'name' => [
'description' => __( 'The name of the marketing channel that this campaign type belongs to.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
],
],
'create_url' => [
'description' => __( 'URL to the create campaign page for this campaign type.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'icon_url' => [
'description' => __( 'URL to an image/icon for the campaign type.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
],
];
return $this->add_additional_fields_schema( $schema );
}
}
API/MarketingCampaigns.php 0000644 00000015256 15153746747 0011466 0 ustar 00 <?php
/**
* REST API MarketingCampaigns Controller
*
* Handles requests to /marketing/campaigns.
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Admin\Marketing\MarketingCampaign;
use Automattic\WooCommerce\Admin\Marketing\MarketingChannels as MarketingChannelsService;
use Automattic\WooCommerce\Admin\Marketing\Price;
use WC_REST_Controller;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
defined( 'ABSPATH' ) || exit;
/**
* MarketingCampaigns Controller.
*
* @internal
* @extends WC_REST_Controller
* @since x.x.x
*/
class MarketingCampaigns extends WC_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'marketing/campaigns';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check whether a given request has permission to view marketing campaigns.
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Returns an aggregated array of marketing campaigns for all active marketing channels.
*
* @param WP_REST_Request $request Request data.
*
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
/**
* MarketingChannels class.
*
* @var MarketingChannelsService $marketing_channels_service
*/
$marketing_channels_service = wc_get_container()->get( MarketingChannelsService::class );
// Aggregate the campaigns from all registered marketing channels.
$responses = [];
foreach ( $marketing_channels_service->get_registered_channels() as $channel ) {
foreach ( $channel->get_campaigns() as $campaign ) {
$response = $this->prepare_item_for_response( $campaign, $request );
$responses[] = $this->prepare_response_for_collection( $response );
}
}
// Pagination.
$page = $request['page'];
$items_per_page = $request['per_page'];
$offset = ( $page - 1 ) * $items_per_page;
$paginated_results = array_slice( $responses, $offset, $items_per_page );
$response = rest_ensure_response( $paginated_results );
$total_campaigns = count( $responses );
$max_pages = ceil( $total_campaigns / $items_per_page );
$response->header( 'X-WP-Total', $total_campaigns );
$response->header( 'X-WP-TotalPages', (int) $max_pages );
// Add previous and next page links to response header.
$request_params = $request->get_query_params();
$base = add_query_arg( urlencode_deep( $request_params ), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
if ( $page > 1 ) {
$prev_page = $page - 1;
if ( $prev_page > $max_pages ) {
$prev_page = $max_pages;
}
$prev_link = add_query_arg( 'page', $prev_page, $base );
$response->link_header( 'prev', $prev_link );
}
if ( $max_pages > $page ) {
$next_page = $page + 1;
$next_link = add_query_arg( 'page', $next_page, $base );
$response->link_header( 'next', $next_link );
}
return $response;
}
/**
* Prepares the item for the REST response.
*
* @param MarketingCampaign $item WordPress representation of the item.
* @param WP_REST_Request $request Request object.
*
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function prepare_item_for_response( $item, $request ) {
$data = [
'id' => $item->get_id(),
'channel' => $item->get_type()->get_channel()->get_slug(),
'title' => $item->get_title(),
'manage_url' => $item->get_manage_url(),
];
if ( $item->get_cost() instanceof Price ) {
$data['cost'] = [
'value' => wc_format_decimal( $item->get_cost()->get_value() ),
'currency' => $item->get_cost()->get_currency(),
];
}
$context = $request['context'] ?? 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
return rest_ensure_response( $data );
}
/**
* Retrieves the item's schema, conforming to JSON Schema.
*
* @return array Item schema data.
*/
public function get_item_schema() {
$schema = [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'marketing_campaign',
'type' => 'object',
'properties' => [
'id' => [
'description' => __( 'The unique identifier for the marketing campaign.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'channel' => [
'description' => __( 'The unique identifier for the marketing channel that this campaign belongs to.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'title' => [
'description' => __( 'Title of the marketing campaign.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'manage_url' => [
'description' => __( 'URL to the campaign management page.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'cost' => [
'description' => __( 'Cost of the marketing campaign.', 'woocommerce' ),
'context' => [ 'view' ],
'readonly' => true,
'type' => 'object',
'properties' => [
'value' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'currency' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
],
],
],
];
return $this->add_additional_fields_schema( $schema );
}
/**
* Retrieves the query params for the collections.
*
* @return array Query parameters for the collection.
*/
public function get_collection_params() {
$params = parent::get_collection_params();
unset( $params['search'] );
return $params;
}
}
API/MarketingChannels.php 0000644 00000013366 15153746747 0011317 0 ustar 00 <?php
/**
* REST API MarketingChannels Controller
*
* Handles requests to /marketing/channels.
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Admin\Marketing\MarketingChannelInterface;
use Automattic\WooCommerce\Admin\Marketing\MarketingChannels as MarketingChannelsService;
use WC_REST_Controller;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
defined( 'ABSPATH' ) || exit;
/**
* MarketingChannels Controller.
*
* @internal
* @extends WC_REST_Controller
* @since x.x.x
*/
class MarketingChannels extends WC_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'marketing/channels';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check whether a given request has permission to view marketing channels.
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Return installed marketing channels.
*
* @param WP_REST_Request $request Request data.
*
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
/**
* MarketingChannels class.
*
* @var MarketingChannelsService $marketing_channels_service
*/
$marketing_channels_service = wc_get_container()->get( MarketingChannelsService::class );
$channels = $marketing_channels_service->get_registered_channels();
$responses = [];
foreach ( $channels as $item ) {
$response = $this->prepare_item_for_response( $item, $request );
$responses[] = $this->prepare_response_for_collection( $response );
}
return rest_ensure_response( $responses );
}
/**
* Prepares the item for the REST response.
*
* @param MarketingChannelInterface $item WordPress representation of the item.
* @param WP_REST_Request $request Request object.
*
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function prepare_item_for_response( $item, $request ) {
$data = [
'slug' => $item->get_slug(),
'is_setup_completed' => $item->is_setup_completed(),
'settings_url' => $item->get_setup_url(),
'name' => $item->get_name(),
'description' => $item->get_description(),
'product_listings_status' => $item->get_product_listings_status(),
'errors_count' => $item->get_errors_count(),
'icon' => $item->get_icon_url(),
];
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
return rest_ensure_response( $data );
}
/**
* Retrieves the item's schema, conforming to JSON Schema.
*
* @return array Item schema data.
*/
public function get_item_schema() {
$schema = [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'marketing_channel',
'type' => 'object',
'properties' => [
'slug' => [
'description' => __( 'Unique identifier string for the marketing channel extension, also known as the plugin slug.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'name' => [
'description' => __( 'Name of the marketing channel.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'description' => [
'description' => __( 'Description of the marketing channel.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'icon' => [
'description' => __( 'Path to the channel icon.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'is_setup_completed' => [
'type' => 'boolean',
'description' => __( 'Whether or not the marketing channel is set up.', 'woocommerce' ),
'context' => [ 'view' ],
'readonly' => true,
],
'settings_url' => [
'description' => __( 'URL to the settings page, or the link to complete the setup/onboarding if the channel has not been set up yet.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'product_listings_status' => [
'description' => __( 'Status of the marketing channel\'s product listings.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'errors_count' => [
'description' => __( 'Number of channel issues/errors (e.g. account-related errors, product synchronization issues, etc.).', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
],
];
return $this->add_additional_fields_schema( $schema );
}
}
API/MarketingOverview.php 0000644 00000006563 15153746747 0011373 0 ustar 00 <?php
/**
* REST API Marketing Overview Controller
*
* Handles requests to /marketing/overview.
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Admin\Marketing\InstalledExtensions;
use Automattic\WooCommerce\Admin\PluginsHelper;
defined( 'ABSPATH' ) || exit;
/**
* Marketing Overview Controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class MarketingOverview extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'marketing/overview';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/activate-plugin',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'activate_plugin' ),
'permission_callback' => array( $this, 'install_plugins_permissions_check' ),
'args' => array(
'plugin' => array(
'required' => true,
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'sanitize_title_with_dashes',
),
),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/installed-plugins',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_installed_plugins' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Return installed marketing extensions data.
*
* @param \WP_REST_Request $request Request data.
*
* @return \WP_Error|\WP_REST_Response
*/
public function activate_plugin( $request ) {
$plugin_slug = $request->get_param( 'plugin' );
if ( ! PluginsHelper::is_plugin_installed( $plugin_slug ) ) {
return new \WP_Error( 'woocommerce_rest_invalid_plugin', __( 'Invalid plugin.', 'woocommerce' ), 404 );
}
$result = activate_plugin( PluginsHelper::get_plugin_path_from_slug( $plugin_slug ) );
if ( ! is_null( $result ) ) {
return new \WP_Error( 'woocommerce_rest_invalid_plugin', __( 'The plugin could not be activated.', 'woocommerce' ), 500 );
}
// IMPORTANT - Don't return the active plugins data here.
// Instead we will get that data in a separate request to ensure they are loaded.
return rest_ensure_response(
array(
'status' => 'success',
)
);
}
/**
* Check if a given request has access to manage plugins.
*
* @param \WP_REST_Request $request Full details about the request.
*
* @return \WP_Error|boolean
*/
public function install_plugins_permissions_check( $request ) {
if ( ! current_user_can( 'install_plugins' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage plugins.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Return installed marketing extensions data.
*
* @param \WP_REST_Request $request Request data.
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_installed_plugins( $request ) {
return rest_ensure_response( InstalledExtensions::get_data() );
}
}
API/MarketingRecommendations.php 0000644 00000014046 15153746747 0012707 0 ustar 00 <?php
/**
* REST API MarketingRecommendations Controller
*
* Handles requests to /marketing/recommendations.
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Internal\Admin\Marketing\MarketingSpecs;
use WC_REST_Controller;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
defined( 'ABSPATH' ) || exit;
/**
* MarketingRecommendations Controller.
*
* @internal
* @extends WC_REST_Controller
* @since x.x.x
*/
class MarketingRecommendations extends WC_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'marketing/recommendations';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ $this, 'get_items' ],
'permission_callback' => [ $this, 'get_items_permissions_check' ],
'args' => [
'category' => [
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'sanitize_title_with_dashes',
'enum' => [ 'channels', 'extensions' ],
'required' => true,
],
],
],
'schema' => [ $this, 'get_public_item_schema' ],
]
);
}
/**
* Check whether a given request has permission to view marketing recommendations.
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! current_user_can( 'install_plugins' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view marketing channels.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Retrieves a collection of recommendations.
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) {
/**
* MarketingSpecs class.
*
* @var MarketingSpecs $marketing_specs
*/
$marketing_specs = wc_get_container()->get( MarketingSpecs::class );
$category = $request->get_param( 'category' );
if ( 'channels' === $category ) {
$items = $marketing_specs->get_recommended_marketing_channels();
} elseif ( 'extensions' === $category ) {
$items = $marketing_specs->get_recommended_marketing_extensions_excluding_channels();
} else {
return new WP_Error( 'woocommerce_rest_invalid_category', __( 'The specified category for recommendations is invalid. Allowed values: "channels", "extensions".', 'woocommerce' ), array( 'status' => 400 ) );
}
$responses = [];
foreach ( $items as $item ) {
$response = $this->prepare_item_for_response( $item, $request );
$responses[] = $this->prepare_response_for_collection( $response );
}
return rest_ensure_response( $responses );
}
/**
* Prepares the item for the REST response.
*
* @param array $item WordPress representation of the item.
* @param WP_REST_Request $request Request object.
*
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function prepare_item_for_response( $item, $request ) {
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $item, $request );
$data = $this->filter_response_by_context( $data, $context );
return rest_ensure_response( $data );
}
/**
* Retrieves the item's schema, conforming to JSON Schema.
*
* @return array Item schema data.
*/
public function get_item_schema() {
$schema = [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'marketing_recommendation',
'type' => 'object',
'properties' => [
'title' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'description' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'url' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'direct_install' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'icon' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'product' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'plugin' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'categories' => [
'type' => 'array',
'context' => [ 'view' ],
'readonly' => true,
'items' => [
'type' => 'string',
],
],
'subcategories' => [
'type' => 'array',
'context' => [ 'view' ],
'readonly' => true,
'items' => [
'type' => 'object',
'context' => [ 'view' ],
'readonly' => true,
'properties' => [
'slug' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'name' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
],
],
],
'tags' => [
'type' => 'array',
'context' => [ 'view' ],
'readonly' => true,
'items' => [
'type' => 'object',
'context' => [ 'view' ],
'readonly' => true,
'properties' => [
'slug' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'name' => [
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
],
],
],
],
];
return $this->add_additional_fields_schema( $schema );
}
}
API/MobileAppMagicLink.php 0000644 00000004143 15153746747 0011342 0 ustar 00 <?php
/**
* REST API Data countries controller.
*
* Handles requests to the /mobile-app endpoint.
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\Jetpack\Connection\Manager as Jetpack_Connection_Manager;
/**
* REST API Data countries controller class.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class MobileAppMagicLink extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'mobile-app';
/**
* Register routes.
*
* @since 7.0.0
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/send-magic-link',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'send_magic_link' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
parent::register_routes();
}
/**
* Sends request to generate magic link email.
*
* @return \WP_REST_Response|\WP_Error
*/
public function send_magic_link() {
// Attempt to get email from Jetpack.
if ( class_exists( Jetpack_Connection_Manager::class ) ) {
$jetpack_connection_manager = new Jetpack_Connection_Manager();
if ( $jetpack_connection_manager->is_active() ) {
if ( class_exists( 'Jetpack_IXR_Client' ) ) {
$xml = new \Jetpack_IXR_Client(
array(
'user_id' => get_current_user_id(),
)
);
$xml->query( 'jetpack.sendMobileMagicLink', array( 'app' => 'woocommerce' ) );
if ( $xml->isError() ) {
return new \WP_Error(
'error_sending_mobile_magic_link',
sprintf(
'%s: %s',
$xml->getErrorCode(),
$xml->getErrorMessage()
)
);
}
return rest_ensure_response(
array(
'code' => 'success',
)
);
}
}
}
return new \WP_Error( 'jetpack_not_connected', __( 'Jetpack is not connected.', 'woocommerce' ) );
}
}
API/NavigationFavorites.php 0000644 00000011515 15153746747 0011676 0 ustar 00 <?php
/**
* REST API Navigation Favorites controller
*
* Handles requests to the navigation favorites endpoint
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Features\Navigation\Favorites;
/**
* REST API Favorites controller class.
*
* @internal
* @extends WC_REST_CRUD_Controller
*/
class NavigationFavorites extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'navigation/favorites';
/**
* Error code to status code mapping.
*
* @var array
*/
protected $error_to_status_map = array(
'woocommerce_favorites_invalid_request' => 400,
'woocommerce_favorites_already_exists' => 409,
'woocommerce_favorites_does_not_exist' => 404,
'woocommerce_favorites_invalid_user' => 400,
'woocommerce_favorites_unauthenticated' => 401,
);
/**
* Register the routes
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/me',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'current_user_permissions_check' ),
),
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'add_item' ),
'permission_callback' => array( $this, 'current_user_permissions_check' ),
'args' => array(
'item_id' => array(
'required' => true,
),
),
),
array(
'methods' => \WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'current_user_permissions_check' ),
'args' => array(
'item_id' => array(
'required' => true,
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Get all favorites.
*
* @param WP_REST_Request $request Request data.
* @return WP_REST_Response
*/
public function get_items( $request ) {
$response = Favorites::get_all( get_current_user_id() );
if ( is_wp_error( $response ) || ! $response ) {
return rest_ensure_response( $this->prepare_error( $response ) );
}
return rest_ensure_response(
array_map( 'stripslashes', $response )
);
}
/**
* Add a favorite.
*
* @param WP_REST_Request $request Request data.
* @return WP_REST_Response
*/
public function add_item( $request ) {
$user_id = get_current_user_id();
$fav_id = $request->get_param( 'item_id' );
$user = get_userdata( $user_id );
if ( false === $user ) {
return $this->prepare_error(
new \WP_Error(
'woocommerce_favorites_invalid_user',
__( 'Invalid user_id provided', 'woocommerce' )
)
);
}
$response = Favorites::add_item( $fav_id, $user_id );
if ( is_wp_error( $response ) || ! $response ) {
return rest_ensure_response( $this->prepare_error( $response ) );
}
return rest_ensure_response( Favorites::get_all( $user_id ) );
}
/**
* Delete a favorite.
*
* @param WP_REST_Request $request Request data.
* @return WP_REST_Response
*/
public function delete_item( $request ) {
$user_id = get_current_user_id();
$fav_id = $request->get_param( 'item_id' );
$response = Favorites::remove_item( $fav_id, $user_id );
if ( is_wp_error( $response ) || ! $response ) {
return rest_ensure_response( $this->prepare_error( $response ) );
}
return rest_ensure_response( Favorites::get_all( $user_id ) );
}
/**
* Check whether a given request has permission to create favorites.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function add_item_permissions_check( $request ) {
return current_user_can( 'edit_users' );
}
/**
* Check whether a given request has permission to delete notes.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function delete_item_permissions_check( $request ) {
return current_user_can( 'edit_users' );
}
/**
* Always allow for operations that only impact current user
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function current_user_permissions_check( $request ) {
return true;
}
/**
* Accept an instance of WP_Error and add the appropriate data for REST transit.
*
* @param WP_Error $error Error to prepare.
* @return WP_Error
*/
protected function prepare_error( $error ) {
if ( ! is_wp_error( $error ) ) {
return $error;
}
$error->add_data(
array(
'status' => $this->error_to_status_map[ $error->get_error_code() ] ?? 500,
)
);
return $error;
}
}
API/NoteActions.php 0000644 00000004621 15153746747 0010142 0 ustar 00 <?php
/**
* REST API Admin Note Action controller
*
* Handles requests to the admin note action endpoint.
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\Notes as NotesFactory;
/**
* REST API Admin Note Action controller class.
*
* @internal
* @extends WC_REST_CRUD_Controller
*/
class NoteActions extends Notes {
/**
* Register the routes for admin notes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<note_id>[\d-]+)/action/(?P<action_id>[\d-]+)',
array(
'args' => array(
'note_id' => array(
'description' => __( 'Unique ID for the Note.', 'woocommerce' ),
'type' => 'integer',
),
'action_id' => array(
'description' => __( 'Unique ID for the Note Action.', 'woocommerce' ),
'type' => 'integer',
),
),
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'trigger_note_action' ),
// @todo - double check these permissions for taking note actions.
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Trigger a note action.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Request|WP_Error
*/
public function trigger_note_action( $request ) {
$note = NotesFactory::get_note( $request->get_param( 'note_id' ) );
if ( ! $note ) {
return new \WP_Error(
'woocommerce_note_invalid_id',
__( 'Sorry, there is no resource with that ID.', 'woocommerce' ),
array( 'status' => 404 )
);
}
$note->set_is_read( true );
$note->save();
$triggered_action = NotesFactory::get_action_by_id( $note, $request->get_param( 'action_id' ) );
if ( ! $triggered_action ) {
return new \WP_Error(
'woocommerce_note_action_invalid_id',
__( 'Sorry, there is no resource with that ID.', 'woocommerce' ),
array( 'status' => 404 )
);
}
$triggered_note = NotesFactory::trigger_note_action( $note, $triggered_action );
$data = $triggered_note->get_data();
$data = $this->prepare_item_for_response( $data, $request );
$data = $this->prepare_response_for_collection( $data );
return rest_ensure_response( $data );
}
}
API/Notes.php 0000644 00000063453 15153746747 0007014 0 ustar 00 <?php
/**
* REST API Admin Notes controller
*
* Handles requests to the admin notes endpoint.
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\Notes as NotesRepository;
/**
* REST API Admin Notes controller class.
*
* @internal
* @extends WC_REST_CRUD_Controller
*/
class Notes extends \WC_REST_CRUD_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'admin/notes';
/**
* Register the routes for admin notes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d-]+)',
array(
'args' => array(
'id' => array(
'description' => __( 'Unique ID for the resource.', 'woocommerce' ),
'type' => 'integer',
),
),
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/delete/(?P<id>[\d-]+)',
array(
array(
'methods' => \WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'update_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/delete/all',
array(
array(
'methods' => \WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_all_items' ),
'permission_callback' => array( $this, 'update_items_permissions_check' ),
'args' => array(
'status' => array(
'description' => __( 'Status of note.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => Note::get_allowed_statuses(),
'type' => 'string',
),
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/tracker/(?P<note_id>[\d-]+)/user/(?P<user_id>[\d-]+)',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'track_opened_email' ),
'permission_callback' => '__return_true',
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/update',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'batch_update_items' ),
'permission_callback' => array( $this, 'update_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/experimental-activate-promo/(?P<promo_note_name>[\w-]+)',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'activate_promo_note' ),
'permission_callback' => array( $this, 'update_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Get a single note.
*
* @param WP_REST_Request $request Request data.
* @return WP_REST_Response|WP_Error
*/
public function get_item( $request ) {
$note = NotesRepository::get_note( $request->get_param( 'id' ) );
if ( ! $note ) {
return new \WP_Error(
'woocommerce_note_invalid_id',
__( 'Sorry, there is no resource with that ID.', 'woocommerce' ),
array( 'status' => 404 )
);
}
if ( is_wp_error( $note ) ) {
return $note;
}
$data = $this->prepare_note_data_for_response( $note, $request );
return rest_ensure_response( $data );
}
/**
* Get all notes.
*
* @param WP_REST_Request $request Request data.
* @return WP_REST_Response
*/
public function get_items( $request ) {
$query_args = $this->prepare_objects_query( $request );
$notes = NotesRepository::get_notes( 'edit', $query_args );
$data = array();
foreach ( (array) $notes as $note_obj ) {
$note = $this->prepare_item_for_response( $note_obj, $request );
$note = $this->prepare_response_for_collection( $note );
$data[] = $note;
}
$response = rest_ensure_response( $data );
$response->header( 'X-WP-Total', count( $data ) );
return $response;
}
/**
* Checks if user is in tasklist experiment.
*
* @return bool Whether remote inbox notifications are enabled.
*/
private function is_tasklist_experiment_assigned_treatment() {
$anon_id = isset( $_COOKIE['tk_ai'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['tk_ai'] ) ) : '';
$allow_tracking = 'yes' === get_option( 'woocommerce_allow_tracking' );
$abtest = new \WooCommerce\Admin\Experimental_Abtest(
$anon_id,
'woocommerce',
$allow_tracking
);
$date = new \DateTime();
$date->setTimeZone( new \DateTimeZone( 'UTC' ) );
$experiment_name = sprintf(
'woocommerce_tasklist_progression_headercard_%s_%s',
$date->format( 'Y' ),
$date->format( 'm' )
);
$experiment_name_2col = sprintf(
'woocommerce_tasklist_progression_headercard_2col_%s_%s',
$date->format( 'Y' ),
$date->format( 'm' )
);
return $abtest->get_variation( $experiment_name ) === 'treatment' ||
$abtest->get_variation( $experiment_name_2col ) === 'treatment';
}
/**
* Prepare objects query.
*
* @param WP_REST_Request $request Full details about the request.
* @return array
*/
protected function prepare_objects_query( $request ) {
$args = array();
$args['order'] = $request['order'];
$args['orderby'] = $request['orderby'];
$args['per_page'] = $request['per_page'];
$args['page'] = $request['page'];
$args['type'] = isset( $request['type'] ) ? $request['type'] : array();
$args['status'] = isset( $request['status'] ) ? $request['status'] : array();
$args['source'] = isset( $request['source'] ) ? $request['source'] : array();
$args['is_deleted'] = 0;
if ( isset( $request['is_read'] ) ) {
$args['is_read'] = filter_var( $request['is_read'], FILTER_VALIDATE_BOOLEAN );
}
if ( 'date' === $args['orderby'] ) {
$args['orderby'] = 'date_created';
}
/**
* Filter the query arguments for a request.
*
* Enables adding extra arguments or setting defaults for a post
* collection request.
*
* @param array $args Key value array of query var to query value.
* @param WP_REST_Request $request The request used.
* @since 3.9.0
*/
$args = apply_filters( 'woocommerce_rest_notes_object_query', $args, $request );
return $args;
}
/**
* Check whether a given request has permission to read a single note.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'system_status', 'read' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check whether a given request has permission to read notes.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'system_status', 'read' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Update a single note.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Request|WP_Error
*/
public function update_item( $request ) {
$note = NotesRepository::get_note( $request->get_param( 'id' ) );
if ( ! $note ) {
return new \WP_Error(
'woocommerce_note_invalid_id',
__( 'Sorry, there is no resource with that ID.', 'woocommerce' ),
array( 'status' => 404 )
);
}
NotesRepository::update_note( $note, $this->get_requested_updates( $request ) );
return $this->get_item( $request );
}
/**
* Delete a single note.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Request|WP_Error
*/
public function delete_item( $request ) {
$note = NotesRepository::get_note( $request->get_param( 'id' ) );
if ( ! $note ) {
return new \WP_Error(
'woocommerce_note_invalid_id',
__( 'Sorry, there is no note with that ID.', 'woocommerce' ),
array( 'status' => 404 )
);
}
NotesRepository::delete_note( $note );
$data = $this->prepare_note_data_for_response( $note, $request );
return rest_ensure_response( $data );
}
/**
* Delete all notes.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Request|WP_Error
*/
public function delete_all_items( $request ) {
$args = array();
if ( isset( $request['status'] ) ) {
$args['status'] = $request['status'];
}
$notes = NotesRepository::delete_all_notes( $args );
$data = array();
foreach ( (array) $notes as $note_obj ) {
$data[] = $this->prepare_note_data_for_response( $note_obj, $request );
}
$response = rest_ensure_response( $data );
$response->header( 'X-WP-Total', NotesRepository::get_notes_count( array( 'info', 'warning' ), array() ) );
return $response;
}
/**
* Prepare note data.
*
* @param Note $note Note data.
* @param WP_REST_Request $request Request object.
*
* @return WP_REST_Response $response Response data.
*/
public function prepare_note_data_for_response( $note, $request ) {
$note = $note->get_data();
$note = $this->prepare_item_for_response( $note, $request );
return $this->prepare_response_for_collection( $note );
}
/**
* Prepare an array with the the requested updates.
*
* @param WP_REST_Request $request Request object.
* @return array A list of the requested updates values.
*/
protected function get_requested_updates( $request ) {
$requested_updates = array();
if ( ! is_null( $request->get_param( 'status' ) ) ) {
$requested_updates['status'] = $request->get_param( 'status' );
}
if ( ! is_null( $request->get_param( 'date_reminder' ) ) ) {
$requested_updates['date_reminder'] = $request->get_param( 'date_reminder' );
}
if ( ! is_null( $request->get_param( 'is_deleted' ) ) ) {
$requested_updates['is_deleted'] = $request->get_param( 'is_deleted' );
}
if ( ! is_null( $request->get_param( 'is_read' ) ) ) {
$requested_updates['is_read'] = $request->get_param( 'is_read' );
}
return $requested_updates;
}
/**
* Batch update a set of notes.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Request|WP_Error
*/
public function batch_update_items( $request ) {
$data = array();
$note_ids = $request->get_param( 'noteIds' );
if ( ! isset( $note_ids ) || ! is_array( $note_ids ) ) {
return new \WP_Error(
'woocommerce_note_invalid_ids',
__( 'Please provide an array of IDs through the noteIds param.', 'woocommerce' ),
array( 'status' => 422 )
);
}
foreach ( (array) $note_ids as $note_id ) {
$note = NotesRepository::get_note( (int) $note_id );
if ( $note ) {
NotesRepository::update_note( $note, $this->get_requested_updates( $request ) );
$data[] = $this->prepare_note_data_for_response( $note, $request );
}
}
$response = rest_ensure_response( $data );
$response->header( 'X-WP-Total', NotesRepository::get_notes_count( array( 'info', 'warning' ), array() ) );
return $response;
}
/**
* Activate a promo note, create if not exist.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Request|WP_Error
*/
public function activate_promo_note( $request ) {
/**
* Filter allowed promo notes for experimental-activate-promo.
*
* @param array $promo_notes Array of allowed promo notes.
* @since 7.8.0
*/
$allowed_promo_notes = apply_filters( 'woocommerce_admin_allowed_promo_notes', [] );
$promo_note_name = $request->get_param( 'promo_note_name' );
if ( ! in_array( $promo_note_name, $allowed_promo_notes, true ) ) {
return new \WP_Error(
'woocommerce_note_invalid_promo_note_name',
__( 'Please provide a valid promo note name.', 'woocommerce' ),
array( 'status' => 422 )
);
}
$data_store = NotesRepository::load_data_store();
$note_ids = $data_store->get_notes_with_name( $promo_note_name );
if ( empty( $note_ids ) ) {
// Promo note doesn't exist, this could happen in cases where
// user might have disabled RemoteInboxNotications via disabling
// marketing suggestions. Thus we'd have to manually add the note.
$note = new Note();
$note->set_name( $promo_note_name );
$note->set_status( Note::E_WC_ADMIN_NOTE_ACTIONED );
$data_store->create( $note );
} else {
$note = NotesRepository::get_note( $note_ids[0] );
NotesRepository::update_note(
$note,
[
'status' => Note::E_WC_ADMIN_NOTE_ACTIONED,
]
);
}
return rest_ensure_response(
array(
'success' => true,
)
);
}
/**
* Makes sure the current user has access to WRITE the settings APIs.
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|bool
*/
public function update_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Prepare a path or query for serialization to the client.
*
* @param string $query The query, path, or URL to transform.
* @return string A fully formed URL.
*/
public function prepare_query_for_response( $query ) {
if ( empty( $query ) ) {
return $query;
}
if ( 'https://' === substr( $query, 0, 8 ) ) {
return $query;
}
if ( 'http://' === substr( $query, 0, 7 ) ) {
return $query;
}
if ( '?' === substr( $query, 0, 1 ) ) {
return admin_url( 'admin.php' . $query );
}
return admin_url( $query );
}
/**
* Maybe add a nonce to a URL.
*
* @link https://codex.wordpress.org/WordPress_Nonces
*
* @param string $url The URL needing a nonce.
* @param string $action The nonce action.
* @param string $name The nonce anme.
* @return string A fully formed URL.
*/
private function maybe_add_nonce_to_url( string $url, string $action = '', string $name = '' ) : string {
if ( empty( $action ) ) {
return $url;
}
if ( empty( $name ) ) {
// Default paramater name.
$name = '_wpnonce';
}
return add_query_arg( $name, wp_create_nonce( $action ), $url );
}
/**
* Prepare a note object for serialization.
*
* @param array $data Note data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $data, $request ) {
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data['date_created_gmt'] = wc_rest_prepare_date_response( $data['date_created'] );
$data['date_created'] = wc_rest_prepare_date_response( $data['date_created'], false );
$data['date_reminder_gmt'] = wc_rest_prepare_date_response( $data['date_reminder'] );
$data['date_reminder'] = wc_rest_prepare_date_response( $data['date_reminder'], false );
$data['title'] = stripslashes( $data['title'] );
$data['content'] = stripslashes( $data['content'] );
$data['is_snoozable'] = (bool) $data['is_snoozable'];
$data['is_deleted'] = (bool) $data['is_deleted'];
$data['is_read'] = (bool) $data['is_read'];
foreach ( (array) $data['actions'] as $key => $value ) {
$data['actions'][ $key ]->label = stripslashes( $data['actions'][ $key ]->label );
$data['actions'][ $key ]->url = $this->maybe_add_nonce_to_url(
$this->prepare_query_for_response( $data['actions'][ $key ]->query ),
(string) $data['actions'][ $key ]->nonce_action,
(string) $data['actions'][ $key ]->nonce_name
);
$data['actions'][ $key ]->status = stripslashes( $data['actions'][ $key ]->status );
}
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links(
array(
'self' => array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $data['id'] ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
)
);
/**
* Filter a note returned from the API.
*
* Allows modification of the note data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param array $data The original note.
* @param WP_REST_Request $request Request used to generate the response.
* @since 3.9.0
*/
return apply_filters( 'woocommerce_rest_prepare_note', $response, $data, $request );
}
/**
* Track opened emails.
*
* @param WP_REST_Request $request Request object.
*/
public function track_opened_email( $request ) {
$note = NotesRepository::get_note( $request->get_param( 'note_id' ) );
if ( ! $note ) {
return;
}
NotesRepository::record_tracks_event_with_user( $request->get_param( 'user_id' ), 'email_note_opened', array( 'note_name' => $note->get_name() ) );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
'validate_callback' => 'rest_validate_request_arg',
);
$params['orderby'] = array(
'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
'type' => 'string',
'default' => 'date',
'enum' => array(
'note_id',
'date',
'type',
'title',
'status',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['page'] = array(
'description' => __( 'Current page of the collection.', 'woocommerce' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);
$params['per_page'] = array(
'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['type'] = array(
'description' => __( 'Type of note.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => Note::get_allowed_types(),
'type' => 'string',
),
);
$params['status'] = array(
'description' => __( 'Status of note.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => Note::get_allowed_statuses(),
'type' => 'string',
),
);
$params['source'] = array(
'description' => __( 'Source of note.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
return $params;
}
/**
* Get the note's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'note',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'ID of the note record.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'Name of the note.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'type' => array(
'description' => __( 'The type of the note (e.g. error, warning, etc.).', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'locale' => array(
'description' => __( 'Locale used for the note title and content.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'title' => array(
'description' => __( 'Title of the note.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'content' => array(
'description' => __( 'Content of the note.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'content_data' => array(
'description' => __( 'Content data for the note. JSON string. Available for re-localization.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'status' => array(
'description' => __( 'The status of the note (e.g. unactioned, actioned).', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'source' => array(
'description' => __( 'Source of the note.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_created' => array(
'description' => __( 'Date the note was created.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_created_gmt' => array(
'description' => __( 'Date the note was created (GMT).', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_reminder' => array(
'description' => __( 'Date after which the user should be reminded of the note, if any.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true, // @todo Allow date_reminder to be updated.
),
'date_reminder_gmt' => array(
'description' => __( 'Date after which the user should be reminded of the note, if any (GMT).', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'is_snoozable' => array(
'description' => __( 'Whether or not a user can request to be reminded about the note.', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'actions' => array(
'description' => __( 'An array of actions, if any, for the note.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'layout' => array(
'description' => __( 'The layout of the note (e.g. banner, thumbnail, plain).', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'image' => array(
'description' => __( 'The image of the note, if any.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'is_deleted' => array(
'description' => __( 'Registers whether the note is deleted or not', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'is_read' => array(
'description' => __( 'Registers whether the note is read or not', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
}
API/OnboardingFreeExtensions.php 0000644 00000007403 15153746747 0012661 0 ustar 00 <?php
/**
* REST API Onboarding Free Extensions Controller
*
* Handles requests to /onboarding/free-extensions
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\Init as RemoteFreeExtensions;
use WC_REST_Data_Controller;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
/**
* Onboarding Payments Controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class OnboardingFreeExtensions extends WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'onboarding/free-extensions';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_available_extensions' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check whether a given request has permission to read onboarding profile data.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Return available payment methods.
*
* @param WP_REST_Request $request Request data.
*
* @return WP_Error|WP_REST_Response
*/
public function get_available_extensions( $request ) {
$extensions = RemoteFreeExtensions::get_extensions();
/**
* 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.
*
* @since 7.8
*/
if ( false === apply_filters( 'woocommerce_suggest_jetpack', true ) ) {
foreach ( $extensions as &$extension ) {
$extension['plugins'] = array_filter(
$extension['plugins'],
function( $plugin ) {
return 'jetpack' !== $plugin->key;
}
);
}
}
$extensions = $this->replace_jetpack_with_jetpack_boost_for_treatment( $extensions );
return new WP_REST_Response( $extensions );
}
private function replace_jetpack_with_jetpack_boost_for_treatment( array $extensions ) {
$is_treatment = \WooCommerce\Admin\Experimental_Abtest::in_treatment( 'woocommerce_jetpack_copy' );
if ( ! $is_treatment ) {
return $extensions;
}
$has_core_profiler = array_search( 'obw/core-profiler', array_column( $extensions, 'key' ) );
if ( $has_core_profiler === false ) {
return $extensions;
}
$has_jetpack = array_search( 'jetpack', array_column( $extensions[ $has_core_profiler ]['plugins'], 'key' ) );
if ( $has_jetpack === false ) {
return $extensions;
}
$jetpack = &$extensions[ $has_core_profiler ]['plugins'][ $has_jetpack ];
$jetpack->key = 'jetpack-boost';
$jetpack->name = 'Jetpack Boost';
$jetpack->label = __( 'Optimize store performance with Jetpack Boost', 'woocommerce' );
$jetpack->description = __( 'Speed up your store and improve your SEO with performance-boosting tools from Jetpack. Learn more', 'woocommerce' );
$jetpack->learn_more_link = 'https://jetpack.com/boost/';
return $extensions;
}
}
API/OnboardingPlugins.php 0000644 00000027675 15153746747 0011356 0 ustar 00 <?php
/**
* REST API Onboarding Profile Controller
*
* Handles requests to /onboarding/profile
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use ActionScheduler;
use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Admin\PluginsHelper;
use Automattic\WooCommerce\Admin\PluginsInstallLoggers\AsynPluginsInstallLogger;
use WC_REST_Data_Controller;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
/**
* Onboarding Plugins controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class OnboardingPlugins extends WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'onboarding/plugins';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/install-and-activate-async',
array(
array(
'methods' => 'POST',
'callback' => array( $this, 'install_and_activate_async' ),
'permission_callback' => array( $this, 'can_install_and_activate_plugins' ),
'args' => array(
'plugins' => array(
'description' => 'A list of plugins to install',
'type' => 'array',
'items' => 'string',
'sanitize_callback' => function ( $value ) {
return array_map(
function ( $value ) {
return sanitize_text_field( $value );
},
$value
);
},
'required' => true,
),
),
),
'schema' => array( $this, 'get_install_async_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/install-and-activate',
array(
array(
'methods' => 'POST',
'callback' => array( $this, 'install_and_activate' ),
'permission_callback' => array( $this, 'can_install_and_activate_plugins' ),
),
'schema' => array( $this, 'get_install_activate_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/scheduled-installs/(?P<job_id>\w+)',
array(
array(
'methods' => 'GET',
'callback' => array( $this, 'get_scheduled_installs' ),
'permission_callback' => array( $this, 'can_install_plugins' ),
),
'schema' => array( $this, 'get_install_async_schema' ),
)
);
// This is an experimental endpoint and is subject to change in the future.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/jetpack-authorization-url',
array(
array(
'methods' => 'GET',
'callback' => array( $this, 'get_jetpack_authorization_url' ),
'permission_callback' => array( $this, 'can_install_plugins' ),
'args' => array(
'redirect_url' => array(
'description' => 'The URL to redirect to after authorization',
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'required' => true,
),
'from' => array(
'description' => 'from value for the jetpack authorization page',
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'required' => false,
'default' => 'woocommerce-onboarding',
),
),
),
)
);
/*
* This is a temporary solution to override /jetpack/v4/connection/data endpoint
* registered by Jetpack Connection when Jetpack is not installed.
*
* For more details, see https://github.com/woocommerce/woocommerce/issues/38979
*/
if ( Constants::get_constant( 'JETPACK__VERSION' ) === null && wp_is_mobile() ) {
register_rest_route(
'jetpack/v4',
'/connection/data',
array(
array(
'methods' => 'GET',
'permission_callback' => '__return_true',
'callback' => function() {
return new WP_REST_Response( null, 404 );
},
),
),
true
);
}
add_action( 'woocommerce_plugins_install_error', array( $this, 'log_plugins_install_error' ), 10, 4 );
add_action( 'woocommerce_plugins_install_api_error', array( $this, 'log_plugins_install_api_error' ), 10, 2 );
}
/**
* Install and activate a plugin.
*
* @param WP_REST_Request $request WP Request object.
*
* @return WP_REST_Response
*/
public function install_and_activate( WP_REST_Request $request ) {
$response = array();
$response['install'] = PluginsHelper::install_plugins( $request->get_param( 'plugins' ) );
$response['activate'] = PluginsHelper::activate_plugins( $response['install']['installed'] );
return new WP_REST_Response( $response );
}
/**
* Queue plugin install request.
*
* @param WP_REST_Request $request WP_REST_Request object.
*
* @return array
*/
public function install_and_activate_async( WP_REST_Request $request ) {
$plugins = $request->get_param( 'plugins' );
$job_id = uniqid();
WC()->queue()->add( 'woocommerce_plugins_install_and_activate_async_callback', array( $plugins, $job_id ) );
$plugin_status = array();
foreach ( $plugins as $plugin ) {
$plugin_status[ $plugin ] = array(
'status' => 'pending',
'errors' => array(),
);
}
return array(
'job_id' => $job_id,
'status' => 'pending',
'plugins' => $plugin_status,
);
}
/**
* Returns current status of given job.
*
* @param WP_REST_Request $request WP_REST_Request object.
*
* @return array|WP_REST_Response
*/
public function get_scheduled_installs( WP_REST_Request $request ) {
$job_id = $request->get_param( 'job_id' );
$actions = WC()->queue()->search(
array(
'hook' => 'woocommerce_plugins_install_and_activate_async_callback',
'search' => $job_id,
'orderby' => 'date',
'order' => 'DESC',
)
);
$actions = array_filter(
PluginsHelper::get_action_data( $actions ),
function( $action ) use ( $job_id ) {
return $action['job_id'] === $job_id;
}
);
if ( empty( $actions ) ) {
return new WP_REST_Response( null, 404 );
}
$response = array(
'job_id' => $actions[0]['job_id'],
'status' => $actions[0]['status'],
);
$option = get_option( 'woocommerce_onboarding_plugins_install_and_activate_async_' . $job_id );
if ( isset( $option['plugins'] ) ) {
$response['plugins'] = $option['plugins'];
}
return $response;
}
/**
* Return Jetpack authorization URL.
*
* @param WP_REST_Request $request WP_REST_Request object.
*
* @return array
* @throws \Exception If there is an error registering the site.
*/
public function get_jetpack_authorization_url( WP_REST_Request $request ) {
$manager = new Manager( 'woocommerce' );
$errors = new WP_Error();
// Register the site to wp.com.
if ( ! $manager->is_connected() ) {
$result = $manager->try_registration();
if ( is_wp_error( $result ) ) {
$errors->add( $result->get_error_code(), $result->get_error_message() );
}
}
$redirect_url = $request->get_param( 'redirect_url' );
$calypso_env = defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, [ 'development', 'wpcalypso', 'horizon', 'stage' ], true ) ? WOOCOMMERCE_CALYPSO_ENVIRONMENT : 'production';
return [
'success' => ! $errors->has_errors(),
'errors' => $errors->get_error_messages(),
'url' => add_query_arg(
[
'from' => $request->get_param( 'from' ),
'calypso_env' => $calypso_env,
],
$manager->get_authorization_url( null, $redirect_url )
),
];
}
/**
* Check whether the current user has permission to install plugins
*
* @return WP_Error|boolean
*/
public function can_install_plugins() {
if ( ! current_user_can( 'install_plugins' ) ) {
return new WP_Error(
'woocommerce_rest_cannot_update',
__( 'Sorry, you cannot manage plugins.', 'woocommerce' ),
array( 'status' => rest_authorization_required_code() )
);
}
return true;
}
/**
* Check whether the current user has permission to install and activate plugins
*
* @return WP_Error|boolean
*/
public function can_install_and_activate_plugins() {
if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) {
return new WP_Error(
'woocommerce_rest_cannot_update',
__( 'Sorry, you cannot manage plugins.', 'woocommerce' ),
array( 'status' => rest_authorization_required_code() )
);
}
return true;
}
/**
* JSON Schema for both install-async and scheduled-installs endpoints.
*
* @return array
*/
public function get_install_async_schema() {
return array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'Install Async Schema',
'type' => 'object',
'properties' => array(
'type' => 'object',
'properties' => array(
'job_id' => 'integer',
'status' => array(
'type' => 'string',
'enum' => array( 'pending', 'complete', 'failed' ),
),
),
),
);
}
/**
* JSON Schema for install-and-activate endpoint.
*
* @return array
*/
public function get_install_activate_schema() {
$error_schema = array(
'type' => 'object',
'patternProperties' => array(
'^.*$' => array(
'type' => 'string',
),
),
'items' => array(
'type' => 'string',
),
);
$install_schema = array(
'type' => 'object',
'properties' => array(
'installed' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
'results' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
'errors' => array(
'type' => 'object',
'properties' => array(
'errors' => $error_schema,
'error_data' => $error_schema,
),
),
),
);
$activate_schema = array(
'type' => 'object',
'properties' => array(
'activated' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
'active' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
'errors' => array(
'type' => 'object',
'properties' => array(
'errors' => $error_schema,
'error_data' => $error_schema,
),
),
),
);
return array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'Install and Activate Schema',
'type' => 'object',
'properties' => array(
'type' => 'object',
'properties' => array(
'install' => $install_schema,
'activate' => $activate_schema,
),
),
);
}
public function log_plugins_install_error( $slug, $api, $result, $upgrader ) {
$properties = array(
'error_message' => sprintf(
/* translators: %s: plugin slug (example: woocommerce-services) */
__(
'The requested plugin `%s` could not be installed.',
'woocommerce'
),
$slug
),
'type' => 'plugin_info_api_error',
'slug' => $slug,
'api_version' => $api->version,
'api_download_link' => $api->download_link,
'upgrader_skin_message' => implode( ',', $upgrader->skin->get_upgrade_messages() ),
'result' => is_wp_error( $result ) ? $result->get_error_message() : 'null',
);
wc_admin_record_tracks_event( 'coreprofiler_install_plugin_error', $properties );
}
public function log_plugins_install_api_error( $slug, $api ) {
$properties = array(
'error_message' => sprintf(
// translators: %s: plugin slug (example: woocommerce-services).
__(
'The requested plugin `%s` could not be installed. Plugin API call failed.',
'woocommerce'
),
$slug
),
'type' => 'plugin_install_error',
'api_error_message' => $api->get_error_message(),
'slug' => $slug,
);
wc_admin_record_tracks_event( 'coreprofiler_install_plugin_error', $properties );
}
}
API/OnboardingProductTypes.php 0000644 00000003460 15153746747 0012364 0 ustar 00 <?php
/**
* REST API Onboarding Product Types Controller
*
* Handles requests to /onboarding/product-types
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProducts;
defined( 'ABSPATH' ) || exit;
/**
* Onboarding Product Types Controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class OnboardingProductTypes extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'onboarding/product-types';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_product_types' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check whether a given request has permission to read onboarding profile data.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Return available product types.
*
* @param \WP_REST_Request $request Request data.
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_product_types( $request ) {
return OnboardingProducts::get_product_types_with_data();
}
}
API/OnboardingProfile.php 0000644 00000041334 15153746747 0011321 0 ustar 00 <?php
/**
* REST API Onboarding Profile Controller
*
* Handles requests to /onboarding/profile
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile as Profile;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProducts;
use Automattic\Jetpack\Connection\Manager as Jetpack_Connection_Manager;
/**
* Onboarding Profile controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class OnboardingProfile extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'onboarding/profile';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_items' ),
'permission_callback' => array( $this, 'update_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
// This endpoint is experimental. For internal use only.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/experimental_get_email_prefill',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_email_prefill' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check whether a given request has permission to read onboarding profile data.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check whether a given request has permission to edit onboarding profile data.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function update_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Return all onboarding profile data.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-options.php';
$onboarding_data = get_option( Profile::DATA_OPTION, array() );
$onboarding_data['industry'] = isset( $onboarding_data['industry'] ) ? $this->filter_industries( $onboarding_data['industry'] ) : null;
$item_schema = $this->get_item_schema();
$items = array();
foreach ( $item_schema['properties'] as $key => $property_schema ) {
$items[ $key ] = isset( $onboarding_data[ $key ] ) ? $onboarding_data[ $key ] : null;
}
$wccom_auth = \WC_Helper_Options::get( 'auth' );
$items['wccom_connected'] = empty( $wccom_auth['access_token'] ) ? false : true;
$item = $this->prepare_item_for_response( $items, $request );
$data = $this->prepare_response_for_collection( $item );
return rest_ensure_response( $data );
}
/**
* Filter the industries.
*
* @param array $industries list of industries.
* @return array
*/
protected function filter_industries( $industries ) {
return apply_filters(
'woocommerce_admin_onboarding_industries',
$industries
);
}
/**
* Update onboarding profile data.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function update_items( $request ) {
$params = $request->get_json_params();
$query_args = $this->prepare_objects_query( $params );
$onboarding_data = (array) get_option( Profile::DATA_OPTION, array() );
$profile_data = array_merge( $onboarding_data, $query_args );
update_option( Profile::DATA_OPTION, $profile_data );
do_action( 'woocommerce_onboarding_profile_data_updated', $onboarding_data, $query_args );
$result = array(
'status' => 'success',
'message' => __( 'Onboarding profile data has been updated.', 'woocommerce' ),
);
$response = $this->prepare_item_for_response( $result, $request );
$data = $this->prepare_response_for_collection( $response );
return rest_ensure_response( $data );
}
/**
* Returns a default email to be pre-filled in OBW. Prioritizes Jetpack if connected,
* otherwise will default to WordPress general settings.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_email_prefill( $request ) {
$result = array(
'email' => '',
);
// Attempt to get email from Jetpack.
if ( class_exists( Jetpack_Connection_Manager::class ) ) {
$jetpack_connection_manager = new Jetpack_Connection_Manager();
if ( $jetpack_connection_manager->is_active() ) {
$jetpack_user = $jetpack_connection_manager->get_connected_user_data();
$result['email'] = $jetpack_user['email'];
}
}
// Attempt to get email from WordPress general settings.
if ( empty( $result['email'] ) ) {
$result['email'] = get_option( 'admin_email' );
}
return rest_ensure_response( $result );
}
/**
* Prepare objects query.
*
* @param array $params The params sent in the request.
* @return array
*/
protected function prepare_objects_query( $params ) {
$args = array();
$properties = self::get_profile_properties();
foreach ( $properties as $key => $property ) {
if ( isset( $params[ $key ] ) ) {
$args[ $key ] = $params[ $key ];
}
}
/**
* Filter the query arguments for a request.
*
* Enables adding extra arguments or setting defaults for a post
* collection request.
*
* @param array $args Key value array of query var to query value.
* @param array $params The params sent in the request.
*/
$args = apply_filters( 'woocommerce_rest_onboarding_profile_object_query', $args, $params );
return $args;
}
/**
* Prepare the data object for response.
*
* @param object $item Data object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $item, $request ) {
$data = $this->add_additional_fields_to_object( $item, $request );
$data = $this->filter_response_by_context( $data, 'view' );
$response = rest_ensure_response( $data );
/**
* Filter the list returned from the API.
*
* @param WP_REST_Response $response The response object.
* @param array $item The original item.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_onboarding_prepare_profile', $response, $item, $request );
}
/**
* Get onboarding profile properties.
*
* @return array
*/
public static function get_profile_properties() {
$properties = array(
'completed' => array(
'type' => 'boolean',
'description' => __( 'Whether or not the profile was completed.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
),
'skipped' => array(
'type' => 'boolean',
'description' => __( 'Whether or not the profile was skipped.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
),
'industry' => array(
'type' => 'array',
'description' => __( 'Industry.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'object',
),
),
'product_types' => array(
'type' => 'array',
'description' => __( 'Types of products sold.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => array_keys( OnboardingProducts::get_allowed_product_types() ),
'type' => 'string',
),
),
'product_count' => array(
'type' => 'string',
'description' => __( 'Number of products to be added.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
'enum' => array(
'0',
'1-10',
'11-100',
'101-1000',
'1000+',
),
),
'selling_venues' => array(
'type' => 'string',
'description' => __( 'Other places the store is selling products.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
'enum' => array(
'no',
'other',
'brick-mortar',
'brick-mortar-other',
'other-woocommerce',
),
),
'number_employees' => array(
'type' => 'string',
'description' => __( 'Number of employees of the store.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
'enum' => array(
'1',
'<10',
'10-50',
'50-250',
'+250',
'not specified',
),
),
'revenue' => array(
'type' => 'string',
'description' => __( 'Current annual revenue of the store.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
'enum' => array(
'none',
'up-to-2500',
'2500-10000',
'10000-50000',
'50000-250000',
'more-than-250000',
'rather-not-say',
),
),
'other_platform' => array(
'type' => 'string',
'description' => __( 'Name of other platform used to sell.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
'enum' => array(
'shopify',
'bigcommerce',
'magento',
'wix',
'amazon',
'ebay',
'etsy',
'squarespace',
'other',
),
),
'other_platform_name' => array(
'type' => 'string',
'description' => __( 'Name of other platform used to sell (not listed).', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
),
'business_extensions' => array(
'type' => 'array',
'description' => __( 'Extra business extensions to install.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => array(
'jetpack',
'jetpack-boost',
'woocommerce-services',
'woocommerce-payments',
'mailchimp-for-woocommerce',
'creative-mail-by-constant-contact',
'facebook-for-woocommerce',
'google-listings-and-ads',
'pinterest-for-woocommerce',
'mailpoet',
'codistoconnect',
'tiktok-for-business',
'tiktok-for-business:alt',
),
'type' => 'string',
),
),
'theme' => array(
'type' => 'string',
'description' => __( 'Selected store theme.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'sanitize_callback' => 'sanitize_title_with_dashes',
'validate_callback' => 'rest_validate_request_arg',
),
'setup_client' => array(
'type' => 'boolean',
'description' => __( 'Whether or not this store was setup for a client.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
),
'wccom_connected' => array(
'type' => 'boolean',
'description' => __( 'Whether or not the store was connected to WooCommerce.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' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
),
'store_email' => array(
'type' => 'string',
'description' => __( 'Store email address.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => array( __CLASS__, 'rest_validate_marketing_email' ),
),
'is_store_country_set' => array(
'type' => 'boolean',
'description' => __( 'Whether or not this store country is set via onboarding profiler.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
),
'is_plugins_page_skipped' => array(
'type' => 'boolean',
'description' => __( 'Whether or not plugins step in core profiler was skipped.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'validate_callback' => 'rest_validate_request_arg',
),
);
return apply_filters( 'woocommerce_rest_onboarding_profile_properties', $properties );
}
/**
* Optionally validates email if user agreed to marketing or if email is not empty.
*
* @param mixed $value Email value.
* @param WP_REST_Request $request Request object.
* @param string $param Parameter name.
* @return true|WP_Error
*/
public static function rest_validate_marketing_email( $value, $request, $param ) {
$is_agree_marketing = $request->get_param( 'is_agree_marketing' );
if (
( $is_agree_marketing || ! empty( $value ) ) &&
! is_email( $value ) ) {
return new \WP_Error( 'rest_invalid_email', __( 'Invalid email address', 'woocommerce' ) );
};
return true;
}
/**
* Get the schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
// Unset properties used for collection params.
$properties = self::get_profile_properties();
foreach ( $properties as $key => $property ) {
unset( $properties[ $key ]['default'] );
unset( $properties[ $key ]['items'] );
unset( $properties[ $key ]['validate_callback'] );
unset( $properties[ $key ]['sanitize_callback'] );
}
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'onboarding_profile',
'type' => 'object',
'properties' => $properties,
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
// Unset properties used for item schema.
$params = self::get_profile_properties();
foreach ( $params as $key => $param ) {
unset( $params[ $key ]['context'] );
unset( $params[ $key ]['readonly'] );
}
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
return apply_filters( 'woocommerce_rest_onboarding_profile_collection_params', $params );
}
}
API/OnboardingTasks.php 0000644 00000077675 15153746747 0011027 0 ustar 00 <?php
/**
* REST API Onboarding Tasks Controller
*
* Handles requests to complete various onboarding tasks.
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingIndustries;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\DeprecatedExtendedTask;
defined( 'ABSPATH' ) || exit;
/**
* Onboarding Tasks Controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class OnboardingTasks extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'onboarding/tasks';
/**
* Duration to milisecond mapping.
*
* @var array
*/
protected $duration_to_ms = array(
'day' => DAY_IN_SECONDS * 1000,
'hour' => HOUR_IN_SECONDS * 1000,
'week' => WEEK_IN_SECONDS * 1000,
);
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/import_sample_products',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'import_sample_products' ),
'permission_callback' => array( $this, 'create_products_permission_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/create_homepage',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_homepage' ),
'permission_callback' => array( $this, 'create_pages_permission_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/create_product_from_template',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_product_from_template' ),
'permission_callback' => array( $this, 'create_products_permission_check' ),
'args' => array_merge(
$this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ),
array(
'template_name' => array(
'required' => true,
'type' => 'string',
'description' => __( 'Product template name.', 'woocommerce' ),
),
)
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_tasks' ),
'permission_callback' => array( $this, 'get_tasks_permission_check' ),
'args' => array(
'ids' => array(
'description' => __( 'Optional parameter to get only specific task lists by id.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => TaskLists::get_list_ids(),
'type' => 'string',
),
),
),
),
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'get_tasks' ),
'permission_callback' => array( $this, 'get_tasks_permission_check' ),
'args' => $this->get_task_list_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[a-z0-9_\-]+)/hide',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'hide_task_list' ),
'permission_callback' => array( $this, 'hide_task_list_permission_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[a-z0-9_\-]+)/unhide',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'unhide_task_list' ),
'permission_callback' => array( $this, 'hide_task_list_permission_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[a-z0-9_\-]+)/dismiss',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'dismiss_task' ),
'permission_callback' => array( $this, 'get_tasks_permission_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[a-z0-9_\-]+)/undo_dismiss',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'undo_dismiss_task' ),
'permission_callback' => array( $this, 'get_tasks_permission_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[a-z0-9_-]+)/snooze',
array(
'args' => array(
'duration' => array(
'description' => __( 'Time period to snooze the task.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => function( $param, $request, $key ) {
return in_array( $param, array_keys( $this->duration_to_ms ), true );
},
),
'task_list_id' => array(
'description' => __( 'Optional parameter to query specific task list.', 'woocommerce' ),
'type' => 'string',
),
),
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'snooze_task' ),
'permission_callback' => array( $this, 'snooze_task_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[a-z0-9_\-]+)/action',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'action_task' ),
'permission_callback' => array( $this, 'get_tasks_permission_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[a-z0-9_\-]+)/undo_snooze',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'undo_snooze_task' ),
'permission_callback' => array( $this, 'snooze_task_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check if a given request has access to create a product.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function create_products_permission_check( $request ) {
if ( ! wc_rest_check_post_permissions( 'product', 'create' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check if a given request has access to create a product.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function create_pages_permission_check( $request ) {
if ( ! wc_rest_check_post_permissions( 'page', 'create' ) || ! current_user_can( 'manage_options' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create new pages.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check if a given request has access to manage woocommerce.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_tasks_permission_check( $request ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to retrieve onboarding tasks.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check if a given request has permission to hide task lists.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function hide_task_list_permission_check( $request ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you are not allowed to hide task lists.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check if a given request has access to manage woocommerce.
*
* @deprecated 7.8.0 snooze task is deprecated.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function snooze_task_permissions_check( $request ) {
wc_deprecated_function( __CLASS__ . '::' . __FUNCTION__, '7.8.0' );
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to snooze onboarding tasks.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Import sample products from given CSV path.
*
* @param string $csv_file CSV file path.
* @return WP_Error|WP_REST_Response
*/
public static function import_sample_products_from_csv( $csv_file ) {
include_once WC_ABSPATH . 'includes/import/class-wc-product-csv-importer.php';
if ( file_exists( $csv_file ) && class_exists( 'WC_Product_CSV_Importer' ) ) {
// Override locale so we can return mappings from WooCommerce in English language stores.
add_filter( 'locale', '__return_false', 9999 );
$importer_class = apply_filters( 'woocommerce_product_csv_importer_class', 'WC_Product_CSV_Importer' );
$args = array(
'parse' => true,
'mapping' => self::get_header_mappings( $csv_file ),
);
$args = apply_filters( 'woocommerce_product_csv_importer_args', $args, $importer_class );
$importer = new $importer_class( $csv_file, $args );
$import = $importer->import();
return $import;
} else {
return new \WP_Error( 'woocommerce_rest_import_error', __( 'Sorry, the sample products data file was not found.', 'woocommerce' ) );
}
}
/**
* Import sample products from WooCommerce sample CSV.
*
* @internal
* @return WP_Error|WP_REST_Response
*/
public static function import_sample_products() {
$sample_csv_file = Features::is_enabled( 'experimental-fashion-sample-products' ) ? WC_ABSPATH . 'sample-data/experimental_fashion_sample_9_products.csv' :
WC_ABSPATH . 'sample-data/experimental_sample_9_products.csv';
$import = self::import_sample_products_from_csv( $sample_csv_file );
return rest_ensure_response( $import );
}
/**
* Creates a product from a template name passed in through the template_name param.
*
* @internal
* @param WP_REST_Request $request Request data.
* @return WP_REST_Response|WP_Error
*/
public static function create_product_from_template( $request ) {
$template_name = basename( $request->get_param( 'template_name' ) );
$template_path = __DIR__ . '/Templates/' . $template_name . '_product.csv';
$template_path = apply_filters( 'woocommerce_product_template_csv_file_path', $template_path, $template_name );
$import = self::import_sample_products_from_csv( $template_path );
if ( is_wp_error( $import ) || 0 === count( $import['imported'] ) ) {
return new \WP_Error(
'woocommerce_rest_product_creation_error',
/* translators: %s is template name */
__( 'Sorry, creating the product with template failed.', 'woocommerce' ),
array( 'status' => 500 )
);
}
$product = wc_get_product( $import['imported'][0] );
$product->set_status( 'auto-draft' );
$product->save();
return rest_ensure_response(
array(
'id' => $product->get_id(),
)
);
}
/**
* Get header mappings from CSV columns.
*
* @internal
* @param string $file File path.
* @return array Mapped headers.
*/
public static function get_header_mappings( $file ) {
include_once WC_ABSPATH . 'includes/admin/importers/mappings/mappings.php';
$importer_class = apply_filters( 'woocommerce_product_csv_importer_class', 'WC_Product_CSV_Importer' );
$importer = new $importer_class( $file, array() );
$raw_headers = $importer->get_raw_keys();
$default_columns = wc_importer_default_english_mappings( array() );
$special_columns = wc_importer_default_special_english_mappings( array() );
$headers = array();
foreach ( $raw_headers as $key => $field ) {
$index = $field;
$headers[ $index ] = $field;
if ( isset( $default_columns[ $field ] ) ) {
$headers[ $index ] = $default_columns[ $field ];
} else {
foreach ( $special_columns as $regex => $special_key ) {
if ( preg_match( self::sanitize_special_column_name_regex( $regex ), $field, $matches ) ) {
$headers[ $index ] = $special_key . $matches[1];
break;
}
}
}
}
return $headers;
}
/**
* Sanitize special column name regex.
*
* @internal
* @param string $value Raw special column name.
* @return string
*/
public static function sanitize_special_column_name_regex( $value ) {
return '/' . str_replace( array( '%d', '%s' ), '(.*)', trim( quotemeta( $value ) ) ) . '/';
}
/**
* Returns a valid cover block with an image, if one exists, or background as a fallback.
*
* @internal
* @param array $image Image to use for the cover block. Should contain a media ID and image URL.
* @return string Block content.
*/
private static function get_homepage_cover_block( $image ) {
$shop_url = get_permalink( wc_get_page_id( 'shop' ) );
if ( ! empty( $image['url'] ) && ! empty( $image['id'] ) ) {
return '<!-- wp:cover {"url":"' . esc_url( $image['url'] ) . '","id":' . intval( $image['id'] ) . ',"dimRatio":0} -->
<div class="wp-block-cover" style="background-image:url(' . esc_url( $image['url'] ) . ')"><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"' . __( 'Write title…', 'woocommerce' ) . '","textColor":"white","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size">' . __( 'Welcome to the store', 'woocommerce' ) . '</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"align":"center","textColor":"white"} -->
<p class="has-text-color has-text-align-center">' . __( 'Write a short welcome message here', 'woocommerce' ) . '</p>
<!-- /wp:paragraph -->
<!-- wp:buttons {"layout":{"type":"flex","justifyContent":"center"}} -->
<div class="wp-block-buttons"><!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link" href="' . esc_url( $shop_url ) . '">' . __( 'Go shopping', 'woocommerce' ) . '</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div></div>
<!-- /wp:cover -->';
}
return '<!-- wp:cover {"dimRatio":0} -->
<div class="wp-block-cover"><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"' . __( 'Write title…', 'woocommerce' ) . '","textColor":"white","fontSize":"large"} -->
<p class="has-text-color has-text-align-center has-large-font-size">' . __( 'Welcome to the store', 'woocommerce' ) . '</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"align":"center","textColor":"white"} -->
<p class="has-text-color has-text-align-center">' . __( 'Write a short welcome message here', 'woocommerce' ) . '</p>
<!-- /wp:paragraph -->
<!-- wp:buttons {"layout":{"type":"flex","justifyContent":"center"}} -->
<div class="wp-block-buttons"><!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link" href="' . esc_url( $shop_url ) . '">' . __( 'Go shopping', 'woocommerce' ) . '</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div></div>
<!-- /wp:cover -->';
}
/**
* Returns a valid media block with an image, if one exists, or a uninitialized media block the user can set.
*
* @internal
* @param array $image Image to use for the cover block. Should contain a media ID and image URL.
* @param string $align If the image should be aligned to the left or right.
* @return string Block content.
*/
private static function get_homepage_media_block( $image, $align = 'left' ) {
$media_position = 'right' === $align ? '"mediaPosition":"right",' : '';
$css_class = 'right' === $align ? ' has-media-on-the-right' : '';
if ( ! empty( $image['url'] ) && ! empty( $image['id'] ) ) {
return '<!-- wp:media-text {' . $media_position . '"mediaId":' . intval( $image['id'] ) . ',"mediaType":"image"} -->
<div class="wp-block-media-text alignwide' . $css_class . '""><figure class="wp-block-media-text__media"><img src="' . esc_url( $image['url'] ) . '" alt="" class="wp-image-' . intval( $image['id'] ) . '"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"' . __( 'Content…', 'woocommerce' ) . '","fontSize":"large"} -->
<p class="has-large-font-size"></p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:media-text -->';
}
return '<!-- wp:media-text {' . $media_position . '} -->
<div class="wp-block-media-text alignwide' . $css_class . '"><figure class="wp-block-media-text__media"></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"' . __( 'Content…', 'woocommerce' ) . '","fontSize":"large"} -->
<p class="has-large-font-size"></p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:media-text -->';
}
/**
* Returns a homepage template to be inserted into a post. A different template will be used depending on the number of products.
*
* @internal
* @param int $post_id ID of the homepage template.
* @return string Template contents.
*/
private static function get_homepage_template( $post_id ) {
$products = wp_count_posts( 'product' );
if ( $products->publish >= 4 ) {
$images = self::sideload_homepage_images( $post_id, 1 );
$image_1 = ! empty( $images[0] ) ? $images[0] : '';
$template = self::get_homepage_cover_block( $image_1 ) . '
<!-- wp:heading {"align":"center"} -->
<h2 style="text-align:center">' . __( 'Shop by Category', 'woocommerce' ) . '</h2>
<!-- /wp:heading -->
<!-- wp:shortcode -->
[product_categories number="0" parent="0"]
<!-- /wp:shortcode -->
<!-- wp:heading {"align":"center"} -->
<h2 style="text-align:center">' . __( 'New In', 'woocommerce' ) . '</h2>
<!-- /wp:heading -->
<!-- wp:woocommerce/product-new {"columns":4} /-->
<!-- wp:heading {"align":"center"} -->
<h2 style="text-align:center">' . __( 'Fan Favorites', 'woocommerce' ) . '</h2>
<!-- /wp:heading -->
<!-- wp:woocommerce/product-top-rated {"columns":4} /-->
<!-- wp:heading {"align":"center"} -->
<h2 style="text-align:center">' . __( 'On Sale', 'woocommerce' ) . '</h2>
<!-- /wp:heading -->
<!-- wp:woocommerce/product-on-sale {"columns":4} /-->
<!-- wp:heading {"align":"center"} -->
<h2 style="text-align:center">' . __( 'Best Sellers', 'woocommerce' ) . '</h2>
<!-- /wp:heading -->
<!-- wp:woocommerce/product-best-sellers {"columns":4} /-->
';
/**
* Modify the template/content of the default homepage.
*
* @param string $template The default homepage template.
*/
return apply_filters( 'woocommerce_admin_onboarding_homepage_template', $template );
}
$images = self::sideload_homepage_images( $post_id, 3 );
$image_1 = ! empty( $images[0] ) ? $images[0] : '';
$image_2 = ! empty( $images[1] ) ? $images[1] : '';
$image_3 = ! empty( $images[2] ) ? $images[2] : '';
$template = self::get_homepage_cover_block( $image_1 ) . '
<!-- wp:heading {"align":"center"} -->
<h2 style="text-align:center">' . __( 'New Products', 'woocommerce' ) . '</h2>
<!-- /wp:heading -->
<!-- wp:woocommerce/product-new /--> ' .
self::get_homepage_media_block( $image_1, 'right' ) .
self::get_homepage_media_block( $image_2, 'left' ) .
self::get_homepage_media_block( $image_3, 'right' ) . '
<!-- wp:woocommerce/featured-product /-->';
/** This filter is documented in src/API/OnboardingTasks.php. */
return apply_filters( 'woocommerce_admin_onboarding_homepage_template', $template );
}
/**
* Gets the possible industry images from the plugin folder for sideloading. If an image doesn't exist, other.jpg is used a fallback.
*
* @internal
* @return array An array of images by industry.
*/
private static function get_available_homepage_images() {
$industry_images = array();
$industries = OnboardingIndustries::get_allowed_industries();
foreach ( $industries as $industry_slug => $label ) {
$industry_images[ $industry_slug ] = apply_filters( 'woocommerce_admin_onboarding_industry_image', WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/other-small.jpg', $industry_slug );
}
return $industry_images;
}
/**
* Uploads a number of images to a homepage template, depending on the selected industry from the profile wizard.
*
* @internal
* @param int $post_id ID of the homepage template.
* @param int $number_of_images The number of images that should be sideloaded (depending on how many media slots are in the template).
* @return array An array of images that have been attached to the post.
*/
private static function sideload_homepage_images( $post_id, $number_of_images ) {
$profile = get_option( OnboardingProfile::DATA_OPTION, array() );
$images_to_sideload = array();
$available_images = self::get_available_homepage_images();
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
if ( ! empty( $profile['industry'] ) ) {
foreach ( $profile['industry'] as $selected_industry ) {
if ( is_string( $selected_industry ) ) {
$industry_slug = $selected_industry;
} elseif ( is_array( $selected_industry ) && ! empty( $selected_industry['slug'] ) ) {
$industry_slug = $selected_industry['slug'];
} else {
continue;
}
// Capture the first industry for use in our minimum images logic.
$first_industry = isset( $first_industry ) ? $first_industry : $industry_slug;
$images_to_sideload[] = ! empty( $available_images[ $industry_slug ] ) ? $available_images[ $industry_slug ] : $available_images['other'];
}
}
// Make sure we have at least {$number_of_images} images.
if ( count( $images_to_sideload ) < $number_of_images ) {
for ( $i = count( $images_to_sideload ); $i < $number_of_images; $i++ ) {
// Fill up missing image slots with the first selected industry, or other.
$industry = isset( $first_industry ) ? $first_industry : 'other';
$images_to_sideload[] = empty( $available_images[ $industry ] ) ? $available_images['other'] : $available_images[ $industry ];
}
}
$already_sideloaded = array();
$images_for_post = array();
foreach ( $images_to_sideload as $image ) {
// Avoid uploading two of the same image, if an image is repeated.
if ( ! empty( $already_sideloaded[ $image ] ) ) {
$images_for_post[] = $already_sideloaded[ $image ];
continue;
}
$sideload_id = \media_sideload_image( $image, $post_id, null, 'id' );
if ( ! is_wp_error( $sideload_id ) ) {
$sideload_url = wp_get_attachment_url( $sideload_id );
$already_sideloaded[ $image ] = array(
'id' => $sideload_id,
'url' => $sideload_url,
);
$images_for_post[] = $already_sideloaded[ $image ];
}
}
return $images_for_post;
}
/**
* Create a homepage from a template.
*
* @return WP_Error|array
*/
public static function create_homepage() {
$post_id = wp_insert_post(
array(
'post_title' => __( 'Homepage', 'woocommerce' ),
'post_type' => 'page',
'post_status' => 'publish',
'post_content' => '', // Template content is updated below, so images can be attached to the post.
)
);
if ( ! is_wp_error( $post_id ) && 0 < $post_id ) {
$template = self::get_homepage_template( $post_id );
wp_update_post(
array(
'ID' => $post_id,
'post_content' => $template,
)
);
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $post_id );
update_option( 'woocommerce_onboarding_homepage_post_id', $post_id );
// Use the full width template on stores using Storefront.
if ( 'storefront' === get_stylesheet() ) {
update_post_meta( $post_id, '_wp_page_template', 'template-fullwidth.php' );
}
return array(
'status' => 'success',
'message' => __( 'Homepage created', 'woocommerce' ),
'post_id' => $post_id,
'edit_post_link' => htmlspecialchars_decode( get_edit_post_link( $post_id ) ),
);
} else {
return $post_id;
}
}
/**
* Get the query params for task lists.
*
* @return array
*/
public function get_task_list_params() {
$params = array();
$params['ids'] = array(
'description' => __( 'Optional parameter to get only specific task lists by id.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => TaskLists::get_list_ids(),
'type' => 'string',
),
);
$params['extended_tasks'] = array(
'description' => __( 'List of extended deprecated tasks from the client side filter.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => function( $param, $request, $key ) {
$has_valid_keys = true;
foreach ( $param as $task ) {
if ( $has_valid_keys ) {
$has_valid_keys = array_key_exists( 'list_id', $task ) && array_key_exists( 'id', $task );
}
}
return $has_valid_keys;
},
);
return $params;
}
/**
* Get the onboarding tasks.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error
*/
public function get_tasks( $request ) {
$extended_tasks = $request->get_param( 'extended_tasks' );
$task_list_ids = $request->get_param( 'ids' );
TaskLists::maybe_add_extended_tasks( $extended_tasks );
$lists = is_array( $task_list_ids ) && count( $task_list_ids ) > 0 ? TaskLists::get_lists_by_ids( $task_list_ids ) : TaskLists::get_lists();
$json = array_map(
function( $list ) {
return $list->sort_tasks()->get_json();
},
$lists
);
return rest_ensure_response( array_values( apply_filters( 'woocommerce_admin_onboarding_tasks', $json ) ) );
}
/**
* Dismiss a single task.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Request|WP_Error
*/
public function dismiss_task( $request ) {
$id = $request->get_param( 'id' );
$task = TaskLists::get_task( $id );
if ( ! $task && $id ) {
$task = new DeprecatedExtendedTask(
null,
array(
'id' => $id,
'is_dismissable' => true,
)
);
}
if ( ! $task || ! $task->is_dismissable() ) {
return new \WP_Error(
'woocommerce_rest_invalid_task',
__( 'Sorry, no dismissable task with that ID was found.', 'woocommerce' ),
array(
'status' => 404,
)
);
}
$task->dismiss();
return rest_ensure_response( $task->get_json() );
}
/**
* Undo dismissal of a single task.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Request|WP_Error
*/
public function undo_dismiss_task( $request ) {
$id = $request->get_param( 'id' );
$task = TaskLists::get_task( $id );
if ( ! $task && $id ) {
$task = new DeprecatedExtendedTask(
null,
array(
'id' => $id,
'is_dismissable' => true,
)
);
}
if ( ! $task || ! $task->is_dismissable() ) {
return new \WP_Error(
'woocommerce_rest_invalid_task',
__( 'Sorry, no dismissable task with that ID was found.', 'woocommerce' ),
array(
'status' => 404,
)
);
}
$task->undo_dismiss();
return rest_ensure_response( $task->get_json() );
}
/**
* Snooze an onboarding task.
*
* @deprecated 7.8.0 snooze task is deprecated.
*
* @param WP_REST_Request $request Request data.
*
* @return WP_REST_Response|WP_Error
*/
public function snooze_task( $request ) {
wc_deprecated_function( __CLASS__ . '::' . __FUNCTION__, '7.8.0' );
$task_id = $request->get_param( 'id' );
$task_list_id = $request->get_param( 'task_list_id' );
$duration = $request->get_param( 'duration' );
$task = TaskLists::get_task( $task_id, $task_list_id );
if ( ! $task && $task_id ) {
$task = new DeprecatedExtendedTask(
null,
array(
'id' => $task_id,
'is_snoozeable' => true,
)
);
}
if ( ! $task || ! $task->is_snoozeable() ) {
return new \WP_Error(
'woocommerce_rest_invalid_task',
__( 'Sorry, no snoozeable task with that ID was found.', 'woocommerce' ),
array(
'status' => 404,
)
);
}
$task->snooze( isset( $duration ) ? $duration : 'day' );
return rest_ensure_response( $task->get_json() );
}
/**
* Undo snooze of a single task.
*
* @deprecated 7.8.0 undo snooze task is deprecated.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Request|WP_Error
*/
public function undo_snooze_task( $request ) {
wc_deprecated_function( __CLASS__ . '::' . __FUNCTION__, '7.8.0' );
$id = $request->get_param( 'id' );
$task = TaskLists::get_task( $id );
if ( ! $task && $id ) {
$task = new DeprecatedExtendedTask(
null,
array(
'id' => $id,
'is_snoozeable' => true,
)
);
}
if ( ! $task || ! $task->is_snoozeable() ) {
return new \WP_Error(
'woocommerce_rest_invalid_task',
__( 'Sorry, no snoozeable task with that ID was found.', 'woocommerce' ),
array(
'status' => 404,
)
);
}
$task->undo_snooze();
return rest_ensure_response( $task->get_json() );
}
/**
* Hide a task list.
*
* @param WP_REST_Request $request Request data.
*
* @return WP_REST_Response|WP_Error
*/
public function hide_task_list( $request ) {
$id = $request->get_param( 'id' );
$task_list = TaskLists::get_list( $id );
if ( ! $task_list ) {
return new \WP_Error(
'woocommerce_rest_invalid_task_list',
__( 'Sorry, that task list was not found', 'woocommerce' ),
array(
'status' => 404,
)
);
}
$update = $task_list->hide();
$json = $task_list->get_json();
return rest_ensure_response( $json );
}
/**
* Unhide a task list.
*
* @param WP_REST_Request $request Request data.
*
* @return WP_REST_Response|WP_Error
*/
public function unhide_task_list( $request ) {
$id = $request->get_param( 'id' );
$task_list = TaskLists::get_list( $id );
if ( ! $task_list ) {
return new \WP_Error(
'woocommerce_tasks_invalid_task_list',
__( 'Sorry, that task list was not found', 'woocommerce' ),
array(
'status' => 404,
)
);
}
$update = $task_list->unhide();
$json = $task_list->get_json();
return rest_ensure_response( $json );
}
/**
* Action a single task.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Request|WP_Error
*/
public function action_task( $request ) {
$id = $request->get_param( 'id' );
$task = TaskLists::get_task( $id );
if ( ! $task && $id ) {
$task = new DeprecatedExtendedTask(
null,
array(
'id' => $id,
)
);
}
if ( ! $task ) {
return new \WP_Error(
'woocommerce_rest_invalid_task',
__( 'Sorry, no task with that ID was found.', 'woocommerce' ),
array(
'status' => 404,
)
);
}
$task->mark_actioned();
return rest_ensure_response( $task->get_json() );
}
}
API/OnboardingThemes.php 0000644 00000014007 15153746747 0011143 0 ustar 00 <?php
/**
* REST API Onboarding Themes Controller
*
* Handles requests to install and activate themes.
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingThemes as Themes;
defined( 'ABSPATH' ) || exit;
/**
* Onboarding Themes Controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class OnboardingThemes extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'onboarding/themes';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/install',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'install_theme' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/activate',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'activate_theme' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
}
/**
* Check if a given request has access to manage themes.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function update_item_permissions_check( $request ) {
if ( ! current_user_can( 'switch_themes' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage themes.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Installs the requested theme.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|array Theme installation status.
*/
public function install_theme( $request ) {
$allowed_themes = Themes::get_allowed_themes();
$theme = sanitize_text_field( $request['theme'] );
if ( ! in_array( $theme, $allowed_themes, true ) ) {
return new \WP_Error( 'woocommerce_rest_invalid_theme', __( 'Invalid theme.', 'woocommerce' ), 404 );
}
$installed_themes = wp_get_themes();
if ( in_array( $theme, array_keys( $installed_themes ), true ) ) {
return( array(
'slug' => $theme,
'name' => $installed_themes[ $theme ]->get( 'Name' ),
'status' => 'success',
) );
}
include_once ABSPATH . '/wp-admin/includes/admin.php';
include_once ABSPATH . '/wp-admin/includes/theme-install.php';
include_once ABSPATH . '/wp-admin/includes/theme.php';
include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . '/wp-admin/includes/class-theme-upgrader.php';
$api = themes_api(
'theme_information',
array(
'slug' => $theme,
'fields' => array(
'sections' => false,
),
)
);
if ( is_wp_error( $api ) ) {
return new \WP_Error(
'woocommerce_rest_theme_install',
sprintf(
/* translators: %s: theme slug (example: woocommerce-services) */
__( 'The requested theme `%s` could not be installed. Theme API call failed.', 'woocommerce' ),
$theme
),
500
);
}
$upgrader = new \Theme_Upgrader( new \Automatic_Upgrader_Skin() );
$result = $upgrader->install( $api->download_link );
if ( is_wp_error( $result ) || is_null( $result ) ) {
return new \WP_Error(
'woocommerce_rest_theme_install',
sprintf(
/* translators: %s: theme slug (example: woocommerce-services) */
__( 'The requested theme `%s` could not be installed.', 'woocommerce' ),
$theme
),
500
);
}
return array(
'slug' => $theme,
'name' => $api->name,
'status' => 'success',
);
}
/**
* Activate the requested theme.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|array Theme activation status.
*/
public function activate_theme( $request ) {
$allowed_themes = Themes::get_allowed_themes();
$theme = sanitize_text_field( $request['theme'] );
if ( ! in_array( $theme, $allowed_themes, true ) ) {
return new \WP_Error( 'woocommerce_rest_invalid_theme', __( 'Invalid theme.', 'woocommerce' ), 404 );
}
require_once ABSPATH . 'wp-admin/includes/theme.php';
$installed_themes = wp_get_themes();
if ( ! in_array( $theme, array_keys( $installed_themes ), true ) ) {
/* translators: %s: theme slug (example: woocommerce-services) */
return new \WP_Error( 'woocommerce_rest_invalid_theme', sprintf( __( 'Invalid theme %s.', 'woocommerce' ), $theme ), 404 );
}
$result = switch_theme( $theme );
if ( ! is_null( $result ) ) {
return new \WP_Error( 'woocommerce_rest_invalid_theme', sprintf( __( 'The requested theme could not be activated.', 'woocommerce' ), $theme ), 500 );
}
return( array(
'slug' => $theme,
'name' => $installed_themes[ $theme ]->get( 'Name' ),
'status' => 'success',
) );
}
/**
* Get the schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'onboarding_theme',
'type' => 'object',
'properties' => array(
'slug' => array(
'description' => __( 'Theme slug.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'Theme name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'status' => array(
'description' => __( 'Theme status.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
}
API/Options.php 0000644 00000022366 15153746747 0007355 0 ustar 00 <?php
/**
* REST API Options Controller
*
* Handles requests to get and update options in the wp_options table.
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Options Controller.
*
* @deprecated since 6.2.0
*
* @extends WC_REST_Data_Controller
*/
class Options extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'options';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_options' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_options' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
}
/**
* Check if a given request has access to get options.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
$params = ( isset( $request['options'] ) && is_string( $request['options'] ) ) ? explode( ',', $request['options'] ) : array();
if ( ! $params ) {
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'You must supply an array of options.', 'woocommerce' ), 500 );
}
foreach ( $params as $option ) {
if ( ! $this->user_has_permission( $option, $request ) ) {
if ( 'production' !== wp_get_environment_type() ) {
return new \WP_Error(
'woocommerce_rest_cannot_view',
__( 'Sorry, you cannot view these options, please remember to update the option permissions in Options API to allow viewing these options in non-production environments.', 'woocommerce' ),
array( 'status' => rest_authorization_required_code() )
);
}
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view these options.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
}
return true;
}
/**
* Check if the user has permission given an option name.
*
* @param string $option Option name.
* @param WP_REST_Request $request Full details about the request.
* @param bool $is_update If the request is to update the option.
* @return boolean
*/
public function user_has_permission( $option, $request, $is_update = false ) {
$permissions = $this->get_option_permissions( $request );
if ( isset( $permissions[ $option ] ) ) {
return $permissions[ $option ];
}
// Don't allow to update options in non-production environments if the option is not whitelisted. This is to force developers to update the option permissions when adding new options.
if ( 'production' !== wp_get_environment_type() ) {
return false;
}
wc_deprecated_function( 'Automattic\WooCommerce\Admin\API\Options::' . ( $is_update ? 'update_options' : 'get_options' ), '6.3' );
return current_user_can( 'manage_options' );
}
/**
* Check if a given request has access to update options.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function update_item_permissions_check( $request ) {
$params = $request->get_json_params();
if ( ! is_array( $params ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'You must supply an array of options and values.', 'woocommerce' ), 500 );
}
foreach ( $params as $option_name => $option_value ) {
if ( ! $this->user_has_permission( $option_name, $request, true ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage these options.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
}
return true;
}
/**
* Get an array of options and respective permissions for the current user.
*
* @param WP_REST_Request $request Full details about the request.
* @return array
*/
public function get_option_permissions( $request ) {
$permissions = self::get_default_option_permissions();
return apply_filters_deprecated( 'woocommerce_rest_api_option_permissions', array( $permissions, $request ), '6.3.0' );
}
/**
* Get the default available option permissions.
*
* @return array
*/
public static function get_default_option_permissions() {
$is_woocommerce_admin = \Automattic\WooCommerce\Internal\Admin\Homescreen::is_admin_user();
$woocommerce_permissions = array(
'woocommerce_setup_jetpack_opted_in',
'woocommerce_stripe_settings',
'woocommerce-ppcp-settings',
'woocommerce_ppcp-gateway_setting',
'woocommerce_demo_store',
'woocommerce_demo_store_notice',
'woocommerce_ces_tracks_queue',
'woocommerce_navigation_intro_modal_dismissed',
'woocommerce_shipping_dismissed_timestamp',
'woocommerce_allow_tracking',
'woocommerce_task_list_keep_completed',
'woocommerce_task_list_prompt_shown',
'woocommerce_default_homepage_layout',
'woocommerce_setup_jetpack_opted_in',
'woocommerce_no_sales_tax',
'woocommerce_calc_taxes',
'woocommerce_bacs_settings',
'woocommerce_bacs_accounts',
'woocommerce_task_list_prompt_shown',
'woocommerce_settings_shipping_recommendations_hidden',
'woocommerce_task_list_dismissed_tasks',
'woocommerce_setting_payments_recommendations_hidden',
'woocommerce_navigation_favorites_tooltip_hidden',
'woocommerce_admin_transient_notices_queue',
'woocommerce_task_list_welcome_modal_dismissed',
'woocommerce_welcome_from_calypso_modal_dismissed',
'woocommerce_task_list_hidden',
'woocommerce_task_list_complete',
'woocommerce_extended_task_list_hidden',
'woocommerce_ces_shown_for_actions',
'woocommerce_clear_ces_tracks_queue_for_page',
'woocommerce_admin_install_timestamp',
'woocommerce_task_list_tracked_completed_tasks',
'woocommerce_show_marketplace_suggestions',
'woocommerce_task_list_reminder_bar_hidden',
'wc_connect_options',
'woocommerce_admin_created_default_shipping_zones',
'woocommerce_admin_reviewed_default_shipping_zones',
'woocommerce_admin_reviewed_store_location_settings',
'woocommerce_ces_product_feedback_shown',
'woocommerce_marketing_overview_multichannel_banner_dismissed',
'woocommerce_dimension_unit',
'woocommerce_weight_unit',
'woocommerce_product_editor_show_feedback_bar',
'woocommerce_product_tour_modal_hidden',
'woocommerce_block_product_tour_shown',
'woocommerce_revenue_report_date_tour_shown',
'woocommerce_date_type',
'date_format',
'time_format',
'woocommerce_onboarding_profile',
'woocommerce_default_country',
'blogname',
'wcpay_welcome_page_incentives_dismissed',
'wcpay_welcome_page_viewed_timestamp',
'wcpay_welcome_page_exit_survey_more_info_needed_timestamp',
'woocommerce_customize_store_onboarding_tour_hidden',
'woocommerce_admin_customize_store_completed',
// WC Test helper options.
'wc-admin-test-helper-rest-api-filters',
'wc_admin_helper_feature_values',
);
$theme_permissions = array(
'theme_mods_' . get_stylesheet() => current_user_can( 'edit_theme_options' ),
'stylesheet' => current_user_can( 'edit_theme_options' ),
);
return array_merge(
array_fill_keys( $theme_permissions, current_user_can( 'edit_theme_options' ) ),
array_fill_keys( $woocommerce_permissions, $is_woocommerce_admin )
);
}
/**
* Gets an array of options and respective values.
*
* @param WP_REST_Request $request Full details about the request.
* @return array Options object with option values.
*/
public function get_options( $request ) {
$options = array();
if ( empty( $request['options'] ) || ! is_string( $request['options'] ) ) {
return $options;
}
$params = explode( ',', $request['options'] );
foreach ( $params as $option ) {
$options[ $option ] = get_option( $option );
}
return $options;
}
/**
* Updates an array of objects.
*
* @param WP_REST_Request $request Full details about the request.
* @return array Options object with a boolean if the option was updated.
*/
public function update_options( $request ) {
$params = $request->get_json_params();
$updated = array();
if ( ! is_array( $params ) ) {
return array();
}
foreach ( $params as $key => $value ) {
$updated[ $key ] = update_option( $key, $value );
}
return $updated;
}
/**
* Get the schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'options',
'type' => 'object',
'properties' => array(
'options' => array(
'type' => 'array',
'description' => __( 'Array of options with associated values.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
}
API/Orders.php 0000644 00000024260 15153746747 0007153 0 ustar 00 <?php
/**
* REST API Orders Controller
*
* Handles requests to /orders/*
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Controller as ReportsController;
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
use Automattic\WooCommerce\Utilities\OrderUtil;
/**
* Orders controller.
*
* @internal
* @extends WC_REST_Orders_Controller
*/
class Orders extends \WC_REST_Orders_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
// This needs to remain a string to support extensions that filter Order Number.
$params['number'] = array(
'description' => __( 'Limit result set to orders matching part of an order number.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
// Fix the default 'status' value until it can be patched in core.
$params['status']['default'] = array( 'any' );
// Analytics settings may affect the allowed status list.
$params['status']['items']['enum'] = ReportsController::get_order_statuses();
return $params;
}
/**
* Prepare objects query.
*
* @param WP_REST_Request $request Full details about the request.
* @return array
*/
protected function prepare_objects_query( $request ) {
$args = parent::prepare_objects_query( $request );
if ( ! empty( $request['number'] ) ) {
$args = $this->search_partial_order_number( $request['number'], $args );
}
return $args;
}
/**
* Helper method to allow searching by partial order number.
*
* @param int $number Partial order number match.
* @param array $args List of arguments for the request.
*
* @return array Modified args with partial order search included.
*/
private function search_partial_order_number( $number, $args ) {
global $wpdb;
$partial_number = trim( $number );
$limit = intval( $args['posts_per_page'] );
if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
$order_table_name = OrdersTableDataStore::get_orders_table_name();
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $orders_table_name is hardcoded.
$order_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT id
FROM $order_table_name
WHERE type = 'shop_order'
AND id LIKE %s
LIMIT %d",
$wpdb->esc_like( absint( $partial_number ) ) . '%',
$limit
)
);
// phpcs:enable
} else {
$order_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT ID
FROM {$wpdb->prefix}posts
WHERE post_type = 'shop_order'
AND ID LIKE %s
LIMIT %d",
$wpdb->esc_like( absint( $partial_number ) ) . '%',
$limit
)
);
}
// Force WP_Query return empty if don't found any order.
$order_ids = empty( $order_ids ) ? array( 0 ) : $order_ids;
$args['post__in'] = $order_ids;
return $args;
}
/**
* Get product IDs, names, and quantity from order ID.
*
* @param array $order_id ID of order.
* @return array
*/
protected function get_products_by_order_id( $order_id ) {
global $wpdb;
$order_items_table = $wpdb->prefix . 'woocommerce_order_items';
$order_itemmeta_table = $wpdb->prefix . 'woocommerce_order_itemmeta';
$products = $wpdb->get_results(
$wpdb->prepare(
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT
order_id,
order_itemmeta.meta_value as product_id,
order_itemmeta_2.meta_value as product_quantity,
order_itemmeta_3.meta_value as variation_id,
{$wpdb->posts}.post_title as product_name
FROM {$order_items_table} order_items
LEFT JOIN {$order_itemmeta_table} order_itemmeta on order_items.order_item_id = order_itemmeta.order_item_id
LEFT JOIN {$order_itemmeta_table} order_itemmeta_2 on order_items.order_item_id = order_itemmeta_2.order_item_id
LEFT JOIN {$order_itemmeta_table} order_itemmeta_3 on order_items.order_item_id = order_itemmeta_3.order_item_id
LEFT JOIN {$wpdb->posts} on {$wpdb->posts}.ID = order_itemmeta.meta_value
WHERE
order_id = ( %d )
AND order_itemmeta.meta_key = '_product_id'
AND order_itemmeta_2.meta_key = '_qty'
AND order_itemmeta_3.meta_key = '_variation_id'
GROUP BY product_id
", // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$order_id
),
ARRAY_A
);
return $products;
}
/**
* Get customer data from customer_id.
*
* @param array $customer_id ID of customer.
* @return array
*/
protected function get_customer_by_id( $customer_id ) {
global $wpdb;
$customer_lookup_table = $wpdb->prefix . 'wc_customer_lookup';
$customer = $wpdb->get_row(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT * FROM {$customer_lookup_table} WHERE customer_id = ( %d )",
$customer_id
),
ARRAY_A
);
return $customer;
}
/**
* Get formatted item data.
*
* @param WC_Data $object WC_Data instance.
* @return array
*/
protected function get_formatted_item_data( $object ) {
$extra_fields = array( 'customer', 'products' );
$fields = false;
// Determine if the response fields were specified.
if ( ! empty( $this->request['_fields'] ) ) {
$fields = wp_parse_list( $this->request['_fields'] );
if ( 0 === count( $fields ) ) {
$fields = false;
} else {
$fields = array_map( 'trim', $fields );
}
}
// Initially skip line items if we can.
$using_order_class_override = is_a( $object, '\Automattic\WooCommerce\Admin\Overrides\Order' );
if ( $using_order_class_override ) {
$data = $object->get_data_without_line_items();
} else {
$data = $object->get_data();
}
$extra_fields = false === $fields ? array() : array_intersect( $extra_fields, $fields );
$format_decimal = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' );
$format_date = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' );
$format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' );
// Add extra data as necessary.
$extra_data = array();
foreach ( $extra_fields as $field ) {
switch ( $field ) {
case 'customer':
$extra_data['customer'] = $this->get_customer_by_id( $data['customer_id'] );
break;
case 'products':
$extra_data['products'] = $this->get_products_by_order_id( $object->get_id() );
break;
}
}
// Format decimal values.
foreach ( $format_decimal as $key ) {
$data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] );
}
// format total with order currency.
if ( $object instanceof \WC_Order ) {
$data['total_formatted'] = wp_strip_all_tags( html_entity_decode( $object->get_formatted_order_total() ), true );
}
// Format date values.
foreach ( $format_date as $key ) {
$datetime = $data[ $key ];
$data[ $key ] = wc_rest_prepare_date_response( $datetime, false );
$data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime );
}
// Format the order status.
$data['status'] = 'wc-' === substr( $data['status'], 0, 3 ) ? substr( $data['status'], 3 ) : $data['status'];
// Format requested line items.
$formatted_line_items = array();
foreach ( $format_line_items as $key ) {
if ( false === $fields || in_array( $key, $fields, true ) ) {
if ( $using_order_class_override ) {
$line_item_data = $object->get_line_item_data( $key );
} else {
$line_item_data = $data[ $key ];
}
$formatted_line_items[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $line_item_data ) );
}
}
// Refunds.
$data['refunds'] = array();
foreach ( $object->get_refunds() as $refund ) {
$data['refunds'][] = array(
'id' => $refund->get_id(),
'reason' => $refund->get_reason() ? $refund->get_reason() : '',
'total' => '-' . wc_format_decimal( $refund->get_amount(), $this->request['dp'] ),
);
}
return array_merge(
array(
'id' => $object->get_id(),
'parent_id' => $data['parent_id'],
'number' => $data['number'],
'order_key' => $data['order_key'],
'created_via' => $data['created_via'],
'version' => $data['version'],
'status' => $data['status'],
'currency' => $data['currency'],
'date_created' => $data['date_created'],
'date_created_gmt' => $data['date_created_gmt'],
'date_modified' => $data['date_modified'],
'date_modified_gmt' => $data['date_modified_gmt'],
'discount_total' => $data['discount_total'],
'discount_tax' => $data['discount_tax'],
'shipping_total' => $data['shipping_total'],
'shipping_tax' => $data['shipping_tax'],
'cart_tax' => $data['cart_tax'],
'total' => $data['total'],
'total_formatted' => isset( $data['total_formatted'] ) ? $data['total_formatted'] : $data['total'],
'total_tax' => $data['total_tax'],
'prices_include_tax' => $data['prices_include_tax'],
'customer_id' => $data['customer_id'],
'customer_ip_address' => $data['customer_ip_address'],
'customer_user_agent' => $data['customer_user_agent'],
'customer_note' => $data['customer_note'],
'billing' => $data['billing'],
'shipping' => $data['shipping'],
'payment_method' => $data['payment_method'],
'payment_method_title' => $data['payment_method_title'],
'transaction_id' => $data['transaction_id'],
'date_paid' => $data['date_paid'],
'date_paid_gmt' => $data['date_paid_gmt'],
'date_completed' => $data['date_completed'],
'date_completed_gmt' => $data['date_completed_gmt'],
'cart_hash' => $data['cart_hash'],
'meta_data' => $data['meta_data'],
'refunds' => $data['refunds'],
),
$formatted_line_items,
$extra_data
);
}
}
API/PaymentGatewaySuggestions.php 0000644 00000012707 15153746747 0013112 0 ustar 00 <?php
/**
* REST API Payment Gateway Suggestions Controller
*
* Handles requests to install and activate depedent plugins.
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\DefaultPaymentGateways;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\Init as Suggestions;
defined( 'ABSPATH' ) || exit;
/**
* PaymentGatewaySuggetsions Controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class PaymentGatewaySuggestions extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'payment-gateway-suggestions';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_suggestions' ),
'permission_callback' => array( $this, 'get_permission_check' ),
'args' => array(
'force_default_suggestions' => array(
'type' => 'boolean',
'description' => __( 'Return the default payment suggestions when woocommerce_show_marketplace_suggestions and woocommerce_setting_payments_recommendations_hidden options are set to no', 'woocommerce' ),
),
),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/dismiss',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'dismiss_payment_gateway_suggestion' ),
'permission_callback' => array( $this, 'get_permission_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
}
/**
* Check if a given request has access to manage plugins.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_permission_check( $request ) {
if ( ! current_user_can( 'install_plugins' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage plugins.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Return suggested payment gateways.
*
* @param WP_REST_Request $request Full details about the request.
* @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response
*/
public function get_suggestions( $request ) {
$should_display = Suggestions::should_display();
$force_default = $request->get_param( 'force_default_suggestions' );
if ( $should_display ) {
return Suggestions::get_suggestions();
} elseif ( false === $should_display && true === $force_default ) {
return rest_ensure_response( Suggestions::get_suggestions( DefaultPaymentGateways::get_all() ) );
}
return rest_ensure_response( array() );
}
/**
* Dismisses suggested payment gateways.
*
* @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response
*/
public function dismiss_payment_gateway_suggestion() {
$success = Suggestions::dismiss();
return rest_ensure_response( $success );
}
/**
* Get the schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'payment-gateway-suggestions',
'type' => 'object',
'properties' => array(
'content' => array(
'description' => __( 'Suggestion description.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'id' => array(
'description' => __( 'Suggestion ID.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'image' => array(
'description' => __( 'Gateway image.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'is_visible' => array(
'description' => __( 'Suggestion visibility.', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'plugins' => array(
'description' => __( 'Array of plugin slugs.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'recommendation_priority' => array(
'description' => __( 'Priority of recommendation.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'title' => array(
'description' => __( 'Gateway title.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'transaction_processors' => array(
'description' => __( 'Array of transaction processors and their images.', 'woocommerce' ),
'type' => 'object',
'addtionalProperties' => array(
'type' => 'string',
'format' => 'uri',
),
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
}
API/Plugins.php 0000644 00000047571 15153746747 0007350 0 ustar 00 <?php
/**
* REST API Plugins Controller
*
* Handles requests to install and activate depedent plugins.
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use Automattic\WooCommerce\Admin\PaymentMethodSuggestionsDataSourcePoller;
use Automattic\WooCommerce\Admin\PluginsHelper;
use Automattic\WooCommerce\Internal\Admin\Notes\InstallJPAndWCSPlugins;
defined( 'ABSPATH' ) || exit;
/**
* Plugins Controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class Plugins extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'plugins';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/install',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'install_plugins' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/install/status',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_installation_status' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/install/status/(?P<job_id>[a-z0-9_\-]+)',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_job_installation_status' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/active',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'active_plugins' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/installed',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'installed_plugins' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/activate',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'activate_plugins' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/activate/status',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_activation_status' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/activate/status/(?P<job_id>[a-z0-9_\-]+)',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_job_activation_status' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/connect-jetpack',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'connect_jetpack' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_connect_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/request-wccom-connect',
array(
array(
'methods' => 'POST',
'callback' => array( $this, 'request_wccom_connect' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_connect_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/finish-wccom-connect',
array(
array(
'methods' => 'POST',
'callback' => array( $this, 'finish_wccom_connect' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_connect_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/connect-wcpay',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'connect_wcpay' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_connect_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/connect-square',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'connect_square' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
),
'schema' => array( $this, 'get_connect_schema' ),
)
);
}
/**
* Check if a given request has access to manage plugins.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function update_item_permissions_check( $request ) {
if ( ! current_user_can( 'install_plugins' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage plugins.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Install the requested plugin.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|array Plugin Status
*/
public function install_plugin( $request ) {
wc_deprecated_function( 'install_plugin', '4.3', '\Automattic\WooCommerce\Admin\API\Plugins()->install_plugins' );
// This method expects a `plugin` argument to be sent, install plugins requires plugins.
$request['plugins'] = $request['plugin'];
return self::install_plugins( $request );
}
/**
* Installs the requested plugins.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|array Plugin Status
*/
public function install_plugins( $request ) {
$plugins = explode( ',', $request['plugins'] );
if ( empty( $request['plugins'] ) || ! is_array( $plugins ) ) {
return new \WP_Error( 'woocommerce_rest_invalid_plugins', __( 'Plugins must be a non-empty array.', 'woocommerce' ), 404 );
}
if ( isset( $request['async'] ) && $request['async'] ) {
$job_id = PluginsHelper::schedule_install_plugins( $plugins );
return array(
'data' => array(
'job_id' => $job_id,
'plugins' => $plugins,
),
'message' => __( 'Plugin installation has been scheduled.', 'woocommerce' ),
);
}
$data = PluginsHelper::install_plugins( $plugins );
return array(
'data' => array(
'installed' => $data['installed'],
'results' => $data['results'],
'install_time' => $data['time'],
),
'errors' => $data['errors'],
'success' => count( $data['errors']->errors ) === 0,
'message' => count( $data['errors']->errors ) === 0
? __( 'Plugins were successfully installed.', 'woocommerce' )
: __( 'There was a problem installing some of the requested plugins.', 'woocommerce' ),
);
}
/**
* Returns a list of recently scheduled installation jobs.
*
* @param WP_REST_Request $request Full details about the request.
* @return array Jobs.
*/
public function get_installation_status( $request ) {
return PluginsHelper::get_installation_status();
}
/**
* Returns a list of recently scheduled installation jobs.
*
* @param WP_REST_Request $request Full details about the request.
* @return array Job.
*/
public function get_job_installation_status( $request ) {
$job_id = $request->get_param( 'job_id' );
$jobs = PluginsHelper::get_installation_status( $job_id );
return reset( $jobs );
}
/**
* Returns a list of active plugins in API format.
*
* @return array Active plugins
*/
public static function active_plugins() {
return( array(
'plugins' => array_values( PluginsHelper::get_active_plugin_slugs() ),
) );
}
/**
* Returns a list of active plugins.
*
* @internal
* @return array Active plugins
*/
public static function get_active_plugins() {
$data = self::active_plugins();
return $data['plugins'];
}
/**
* Returns a list of installed plugins.
*
* @return array Installed plugins
*/
public function installed_plugins() {
return( array(
'plugins' => PluginsHelper::get_installed_plugin_slugs(),
) );
}
/**
* Activate the requested plugin.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|array Plugin Status
*/
public function activate_plugins( $request ) {
$plugins = explode( ',', $request['plugins'] );
if ( empty( $request['plugins'] ) || ! is_array( $plugins ) ) {
return new \WP_Error( 'woocommerce_rest_invalid_plugins', __( 'Plugins must be a non-empty array.', 'woocommerce' ), 404 );
}
if ( isset( $request['async'] ) && $request['async'] ) {
$job_id = PluginsHelper::schedule_activate_plugins( $plugins );
return array(
'data' => array(
'job_id' => $job_id,
'plugins' => $plugins,
),
'message' => __( 'Plugin activation has been scheduled.', 'woocommerce' ),
);
}
$data = PluginsHelper::activate_plugins( $plugins );
return( array(
'data' => array(
'activated' => $data['activated'],
'active' => $data['active'],
),
'errors' => $data['errors'],
'success' => count( $data['errors']->errors ) === 0,
'message' => count( $data['errors']->errors ) === 0
? __( 'Plugins were successfully activated.', 'woocommerce' )
: __( 'There was a problem activating some of the requested plugins.', 'woocommerce' ),
) );
}
/**
* Returns a list of recently scheduled activation jobs.
*
* @param WP_REST_Request $request Full details about the request.
* @return array Job.
*/
public function get_activation_status( $request ) {
return PluginsHelper::get_activation_status();
}
/**
* Returns a list of recently scheduled activation jobs.
*
* @param WP_REST_Request $request Full details about the request.
* @return array Jobs.
*/
public function get_job_activation_status( $request ) {
$job_id = $request->get_param( 'job_id' );
$jobs = PluginsHelper::get_activation_status( $job_id );
return reset( $jobs );
}
/**
* Generates a Jetpack Connect URL.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|array Connection URL for Jetpack
*/
public function connect_jetpack( $request ) {
if ( ! class_exists( '\Jetpack' ) ) {
return new \WP_Error( 'woocommerce_rest_jetpack_not_active', __( 'Jetpack is not installed or active.', 'woocommerce' ), 404 );
}
$redirect_url = apply_filters( 'woocommerce_admin_onboarding_jetpack_connect_redirect_url', esc_url_raw( $request['redirect_url'] ) );
$connect_url = \Jetpack::init()->build_connect_url( true, $redirect_url, 'woocommerce-onboarding' );
$calypso_env = defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ), true ) ? WOOCOMMERCE_CALYPSO_ENVIRONMENT : 'production';
$connect_url = add_query_arg( array( 'calypso_env' => $calypso_env ), $connect_url );
return( array(
'slug' => 'jetpack',
'name' => __( 'Jetpack', 'woocommerce' ),
'connectAction' => $connect_url,
) );
}
/**
* Kicks off the WCCOM Connect process.
*
* @return WP_Error|array Connection URL for WooCommerce.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 );
}
$redirect_uri = wc_admin_url( '&task=connect&wccom-connected=1' );
$request = \WC_Helper_API::post(
'oauth/request_token',
array(
'body' => array(
'home_url' => home_url(),
'redirect_uri' => $redirect_uri,
),
)
);
$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 );
}
$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 );
}
do_action( 'woocommerce_helper_connect_start' );
$connect_url = add_query_arg(
array(
'home_url' => rawurlencode( home_url() ),
'redirect_uri' => rawurlencode( $redirect_uri ),
'secret' => rawurlencode( $secret ),
'wccom-from' => 'onboarding',
),
\WC_Helper_API::url( 'oauth/authorize' )
);
if ( defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ), true ) ) {
$connect_url = add_query_arg(
array(
'calypso_env' => WOOCOMMERCE_CALYPSO_ENVIRONMENT,
),
$connect_url
);
}
return( array(
'connectAction' => $connect_url,
) );
}
/**
* Finishes connecting to WooCommerce.com.
*
* @param object $rest_request Request details.
* @return WP_Error|array Contains success status.
*/
public function finish_wccom_connect( $rest_request ) {
include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper.php';
include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-api.php';
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 );
}
// Obtain an access token.
$request = \WC_Helper_API::post(
'oauth/access_token',
array(
'body' => array(
'request_token' => wp_unslash( $rest_request['request_token'] ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
'home_url' => home_url(),
),
)
);
$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 );
}
$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 );
}
\WC_Helper_Options::update(
'auth',
array(
'access_token' => $access_token['access_token'],
'access_token_secret' => $access_token['access_token_secret'],
'site_id' => $access_token['site_id'],
'user_id' => get_current_user_id(),
'updated' => time(),
)
);
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 );
}
delete_transient( '_woocommerce_helper_subscriptions' );
\WC_Helper_Updater::flush_updates_cache();
do_action( 'woocommerce_helper_connected' );
return array(
'success' => true,
);
}
/**
* Returns a URL that can be used to connect to Square.
*
* @return WP_Error|array Connect URL.
*/
public function connect_square() {
if ( ! class_exists( '\WooCommerce\Square\Handlers\Connection' ) ) {
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to Square.', 'woocommerce' ), 500 );
}
if ( 'US' === WC()->countries->get_base_country() ) {
$profile = get_option( OnboardingProfile::DATA_OPTION, array() );
if ( ! empty( $profile['industry'] ) ) {
$has_cbd_industry = in_array( 'cbd-other-hemp-derived-products', array_column( $profile['industry'], 'slug' ), true );
}
}
if ( $has_cbd_industry ) {
$url = 'https://squareup.com/t/f_partnerships/d_referrals/p_woocommerce/c_general/o_none/l_us/dt_alldevice/pr_payments/?route=/solutions/cbd';
} else {
$url = \WooCommerce\Square\Handlers\Connection::CONNECT_URL_PRODUCTION;
}
$redirect_url = wp_nonce_url( wc_admin_url( '&task=payments&method=square&square-connect-finish=1' ), 'wc_square_connected' );
$args = array(
'redirect' => rawurlencode( rawurlencode( $redirect_url ) ),
'scopes' => implode(
',',
array(
'MERCHANT_PROFILE_READ',
'PAYMENTS_READ',
'PAYMENTS_WRITE',
'ORDERS_READ',
'ORDERS_WRITE',
'CUSTOMERS_READ',
'CUSTOMERS_WRITE',
'SETTLEMENTS_READ',
'ITEMS_READ',
'ITEMS_WRITE',
'INVENTORY_READ',
'INVENTORY_WRITE',
)
),
);
$connect_url = add_query_arg( $args, $url );
return( array(
'connectUrl' => $connect_url,
) );
}
/**
* Returns a URL that can be used to by WCPay to verify business details with Stripe.
*
* @return WP_Error|array Connect URL.
*/
public function connect_wcpay() {
if ( ! class_exists( 'WC_Payments_Account' ) ) {
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error communicating with the WooPayments plugin.', 'woocommerce' ), 500 );
}
$connect_url = add_query_arg(
array(
'wcpay-connect' => 'WCADMIN_PAYMENT_TASK',
'_wpnonce' => wp_create_nonce( 'wcpay-connect' ),
),
admin_url()
);
return( array(
'connectUrl' => $connect_url,
) );
}
/**
* Get the schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'plugins',
'type' => 'object',
'properties' => array(
'slug' => array(
'description' => __( 'Plugin slug.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'Plugin name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'status' => array(
'description' => __( 'Plugin status.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the schema, conforming to JSON Schema.
*
* @return array
*/
public function get_connect_schema() {
$schema = $this->get_item_schema();
unset( $schema['properties']['status'] );
$schema['properties']['connectAction'] = array(
'description' => __( 'Action that should be completed to connect Jetpack.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
);
return $schema;
}
}
API/ProductAttributeTerms.php 0000644 00000010563 15153746747 0012235 0 ustar 00 <?php
/**
* REST API Product Attribute Terms Controller
*
* Handles requests to /products/attributes/<slug>/terms
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Product attribute terms controller.
*
* @internal
* @extends WC_REST_Product_Attribute_Terms_Controller
*/
class ProductAttributeTerms extends \WC_REST_Product_Attribute_Terms_Controller {
use CustomAttributeTraits;
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Register the routes for custom product attributes.
*/
public function register_routes() {
parent::register_routes();
register_rest_route(
$this->namespace,
'products/attributes/(?P<slug>[a-z0-9_\-]+)/terms',
array(
'args' => array(
'slug' => array(
'description' => __( 'Slug identifier for the resource.', 'woocommerce' ),
'type' => 'string',
),
),
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item_by_slug' ),
'permission_callback' => array( $this, 'get_custom_attribute_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check if a given request has access to read a custom attribute.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_custom_attribute_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'attributes', 'read' ) ) {
return new WP_Error(
'woocommerce_rest_cannot_view',
__( 'Sorry, you cannot view this resource.', 'woocommerce' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
return true;
}
/**
* Get the Attribute's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = parent::get_item_schema();
// Custom attributes substitute slugs for numeric IDs.
$schema['properties']['id']['type'] = array( 'integer', 'string' );
return $schema;
}
/**
* Query custom attribute values by slug.
*
* @param string $slug Attribute slug.
* @return array Attribute values, formatted for response.
*/
protected function get_custom_attribute_values( $slug ) {
global $wpdb;
if ( empty( $slug ) ) {
return array();
}
$attribute_values = array();
// Get the attribute properties.
$attribute = $this->get_custom_attribute_by_slug( $slug );
if ( is_wp_error( $attribute ) ) {
return $attribute;
}
// Find all attribute values assigned to products.
$query_results = $wpdb->get_results(
$wpdb->prepare(
"SELECT meta_value, COUNT(meta_id) AS product_count
FROM {$wpdb->postmeta}
WHERE meta_key = %s
AND meta_value != ''
GROUP BY meta_value",
'attribute_' . esc_sql( $slug )
),
OBJECT_K
);
// Ensure all defined properties are in the response.
$defined_values = wc_get_text_attributes( $attribute[ $slug ]['value'] );
foreach ( $defined_values as $defined_value ) {
if ( array_key_exists( $defined_value, $query_results ) ) {
continue;
}
$query_results[ $defined_value ] = (object) array(
'meta_value' => $defined_value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'product_count' => 0,
);
}
foreach ( $query_results as $term_value => $term ) {
// Mimic the structure of a taxonomy-backed attribute values for response.
$data = array(
'id' => $term_value,
'name' => $term_value,
'slug' => $term_value,
'description' => '',
'menu_order' => 0,
'count' => (int) $term->product_count,
);
$response = rest_ensure_response( $data );
$response->add_links(
array(
'collection' => array(
'href' => rest_url(
$this->namespace . '/products/attributes/' . $slug . '/terms'
),
),
)
);
$response = $this->prepare_response_for_collection( $response );
$attribute_values[ $term_value ] = $response;
}
return array_values( $attribute_values );
}
/**
* Get a single custom attribute.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Request|WP_Error
*/
public function get_item_by_slug( $request ) {
return $this->get_custom_attribute_values( $request['slug'] );
}
}
API/ProductAttributes.php 0000644 00000010730 15153746747 0011401 0 ustar 00 <?php
/**
* REST API Product Attributes Controller
*
* Handles requests to /products/attributes.
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Product categories controller.
*
* @internal
* @extends WC_REST_Product_Attributes_Controller
*/
class ProductAttributes extends \WC_REST_Product_Attributes_Controller {
use CustomAttributeTraits;
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Register the routes for custom product attributes.
*/
public function register_routes() {
parent::register_routes();
register_rest_route(
$this->namespace,
'products/attributes/(?P<slug>[a-z0-9_\-]+)',
array(
'args' => array(
'slug' => array(
'description' => __( 'Slug identifier for the resource.', 'woocommerce' ),
'type' => 'string',
),
),
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item_by_slug' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Get the query params for collections
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['search'] = array(
'description' => __( 'Search by similar attribute name.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get the Attribute's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = parent::get_item_schema();
// Custom attributes substitute slugs for numeric IDs.
$schema['properties']['id']['type'] = array( 'integer', 'string' );
return $schema;
}
/**
* Get a single attribute by it's slug.
*
* @param WP_REST_Request $request The API request.
* @return WP_REST_Response
*/
public function get_item_by_slug( $request ) {
if ( empty( $request['slug'] ) ) {
return array();
}
$attributes = $this->get_custom_attribute_by_slug( $request['slug'] );
if ( is_wp_error( $attributes ) ) {
return $attributes;
}
$response_items = $this->format_custom_attribute_items_for_response( $attributes );
return reset( $response_items );
}
/**
* Format custom attribute items for response (mimic the structure of a taxonomy - backed attribute).
*
* @param array $custom_attributes - CustomAttributeTraits::get_custom_attributes().
* @return array
*/
protected function format_custom_attribute_items_for_response( $custom_attributes ) {
$response = array();
foreach ( $custom_attributes as $attribute_key => $attribute_value ) {
$data = array(
'id' => $attribute_key,
'name' => $attribute_value['name'],
'slug' => $attribute_key,
'type' => 'select',
'order_by' => 'menu_order',
'has_archives' => false,
);
$item_response = rest_ensure_response( $data );
$item_response->add_links( $this->prepare_links( (object) array( 'attribute_id' => $attribute_key ) ) );
$item_response = $this->prepare_response_for_collection(
$item_response
);
$response[] = $item_response;
}
return $response;
}
/**
* Get all attributes, with support for searching (which includes custom attributes).
*
* @param WP_REST_Request $request The API request.
* @return WP_REST_Response
*/
public function get_items( $request ) {
if ( empty( $request['search'] ) ) {
return parent::get_items( $request );
}
$search_string = $request['search'];
$custom_attributes = $this->get_custom_attributes( array( 'name' => $search_string ) );
$matching_attributes = $this->format_custom_attribute_items_for_response( $custom_attributes );
$taxonomy_attributes = wc_get_attribute_taxonomies();
foreach ( $taxonomy_attributes as $attribute_obj ) {
// Skip taxonomy attributes that didn't match the query.
if ( false === stripos( $attribute_obj->attribute_label, $search_string ) ) {
continue;
}
$attribute = $this->prepare_item_for_response( $attribute_obj, $request );
$matching_attributes[] = $this->prepare_response_for_collection( $attribute );
}
$response = rest_ensure_response( $matching_attributes );
$response->header( 'X-WP-Total', count( $matching_attributes ) );
$response->header( 'X-WP-TotalPages', 1 );
return $response;
}
}
API/ProductCategories.php 0000644 00000000712 15153746747 0011337 0 ustar 00 <?php
/**
* REST API Product Categories Controller
*
* Handles requests to /products/categories.
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Product categories controller.
*
* @internal
* @extends WC_REST_Product_Categories_Controller
*/
class ProductCategories extends \WC_REST_Product_Categories_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
}
API/ProductForm.php 0000644 00000006101 15153746747 0010153 0 ustar 00 <?php
/**
* REST API Product Form Controller
*
* Handles requests to retrieve product form data.
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Internal\Admin\ProductForm\FormFactory;
defined( 'ABSPATH' ) || exit;
/**
* ProductForm Controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class ProductForm extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'product-form';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_form_config' ),
'permission_callback' => array( $this, 'get_product_form_permission_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/fields',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_fields' ),
'permission_callback' => array( $this, 'get_product_form_permission_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check if a given request has access to manage woocommerce.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_product_form_permission_check( $request ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to retrieve product form data.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Get the form fields.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error
*/
public function get_fields( $request ) {
$json = array_map(
function( $field ) {
return $field->get_json();
},
FormFactory::get_fields()
);
return rest_ensure_response( $json );
}
/**
* Get the form config.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error
*/
public function get_form_config( $request ) {
$fields = array_map(
function( $field ) {
return $field->get_json();
},
FormFactory::get_fields()
);
$subsections = array_map(
function( $subsection ) {
return $subsection->get_json();
},
FormFactory::get_subsections()
);
$sections = array_map(
function( $section ) {
return $section->get_json();
},
FormFactory::get_sections()
);
$tabs = array_map(
function( $tab ) {
return $tab->get_json();
},
FormFactory::get_tabs()
);
return rest_ensure_response(
array(
'fields' => $fields,
'subsections' => $subsections,
'sections' => $sections,
'tabs' => $tabs,
)
);
}
}
API/ProductReviews.php 0000644 00000002462 15153746747 0010702 0 ustar 00 <?php
/**
* REST API Product Reviews Controller
*
* Handles requests to /products/reviews.
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Product reviews controller.
*
* @internal
* @extends WC_REST_Product_Reviews_Controller
*/
class ProductReviews extends \WC_REST_Product_Reviews_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Prepare links for the request.
*
* @param WP_Comment $review Product review object.
* @return array Links for the given product review.
*/
protected function prepare_links( $review ) {
$links = array(
'self' => array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $review->comment_ID ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
),
);
if ( 0 !== (int) $review->comment_post_ID ) {
$links['up'] = array(
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $review->comment_post_ID ) ),
'embeddable' => true,
);
}
if ( 0 !== (int) $review->user_id ) {
$links['reviewer'] = array(
'href' => rest_url( 'wp/v2/users/' . $review->user_id ),
'embeddable' => true,
);
}
return $links;
}
}
API/ProductVariations.php 0000644 00000013735 15153746747 0011402 0 ustar 00 <?php
/**
* REST API Product Variations Controller
*
* Handles requests to /products/variations.
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Product variations controller.
*
* @internal
* @extends WC_REST_Product_Variations_Controller
*/
class ProductVariations extends \WC_REST_Product_Variations_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Register the routes for products.
*/
public function register_routes() {
parent::register_routes();
// Add a route for listing variations without specifying the parent product ID.
register_rest_route(
$this->namespace,
'/variations',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['search'] = array(
'description' => __( 'Search by similar product name, sku, or attribute value.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Add in conditional search filters for variations.
*
* @internal
* @param string $where Where clause used to search posts.
* @param object $wp_query WP_Query object.
* @return string
*/
public static function add_wp_query_filter( $where, $wp_query ) {
global $wpdb;
$search = $wp_query->get( 'search' );
if ( $search ) {
$like = '%' . $wpdb->esc_like( $search ) . '%';
$conditions = array(
$wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like ), // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->prepare( 'attr_search_meta.meta_value LIKE %s', $like ), // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
);
if ( wc_product_sku_enabled() ) {
$conditions[] = $wpdb->prepare( 'wc_product_meta_lookup.sku LIKE %s', $like );
}
$where .= ' AND (' . implode( ' OR ', $conditions ) . ')';
}
return $where;
}
/**
* Join posts meta tables when variation search query is present.
*
* @internal
* @param string $join Join clause used to search posts.
* @param object $wp_query WP_Query object.
* @return string
*/
public static function add_wp_query_join( $join, $wp_query ) {
global $wpdb;
$search = $wp_query->get( 'search' );
if ( $search ) {
$join .= " LEFT JOIN {$wpdb->postmeta} AS attr_search_meta
ON {$wpdb->posts}.ID = attr_search_meta.post_id
AND attr_search_meta.meta_key LIKE 'attribute_%' ";
}
if ( wc_product_sku_enabled() && ! strstr( $join, 'wc_product_meta_lookup' ) ) {
$join .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup
ON $wpdb->posts.ID = wc_product_meta_lookup.product_id ";
}
return $join;
}
/**
* Add product name and sku filtering to the WC API.
*
* @param WP_REST_Request $request Request data.
* @return array
*/
protected function prepare_objects_query( $request ) {
$args = parent::prepare_objects_query( $request );
if ( ! empty( $request['search'] ) ) {
$args['search'] = $request['search'];
unset( $args['s'] );
}
// Retrieve variations without specifying a parent product.
if ( "/{$this->namespace}/variations" === $request->get_route() ) {
unset( $args['post_parent'] );
}
return $args;
}
/**
* Get a collection of posts and add the post title filter option to WP_Query.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
add_filter( 'posts_where', array( __CLASS__, 'add_wp_query_filter' ), 10, 2 );
add_filter( 'posts_join', array( __CLASS__, 'add_wp_query_join' ), 10, 2 );
add_filter( 'posts_groupby', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_group_by' ), 10, 2 );
$response = parent::get_items( $request );
remove_filter( 'posts_where', array( __CLASS__, 'add_wp_query_filter' ), 10 );
remove_filter( 'posts_join', array( __CLASS__, 'add_wp_query_join' ), 10 );
remove_filter( 'posts_groupby', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_group_by' ), 10 );
return $response;
}
/**
* Get the Product's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = parent::get_item_schema();
$schema['properties']['name'] = array(
'description' => __( 'Product parent name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
);
$schema['properties']['type'] = array(
'description' => __( 'Product type.', 'woocommerce' ),
'type' => 'string',
'default' => 'variation',
'enum' => array( 'variation' ),
'context' => array( 'view', 'edit' ),
);
$schema['properties']['parent_id'] = array(
'description' => __( 'Product parent ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
);
return $schema;
}
/**
* Prepare a single variation output for response.
*
* @param WC_Data $object Object data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_object_for_response( $object, $request ) {
$context = empty( $request['context'] ) ? 'view' : $request['context'];
$response = parent::prepare_object_for_response( $object, $request );
$data = $response->get_data();
$data['name'] = $object->get_name( $context );
$data['type'] = $object->get_type();
$data['parent_id'] = $object->get_parent_id( $context );
$response->set_data( $data );
return $response;
}
}
API/Products.php 0000644 00000023315 15153746747 0007520 0 ustar 00 <?php
/**
* REST API Products Controller
*
* Handles requests to /products/*
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Products controller.
*
* @internal
* @extends WC_REST_Products_Controller
*/
class Products extends \WC_REST_Products_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Local cache of last order dates by ID.
*
* @var array
*/
protected $last_order_dates = array();
/**
* Adds properties that can be embed via ?_embed=1.
*
* @return array
*/
public function get_item_schema() {
$schema = parent::get_item_schema();
$properties_to_embed = array(
'id',
'name',
'slug',
'permalink',
'images',
'description',
'short_description',
);
foreach ( $properties_to_embed as $property ) {
$schema['properties'][ $property ]['context'][] = 'embed';
}
$schema['properties']['last_order_date'] = array(
'description' => __( "The date the last order for this product was placed, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
);
return $schema;
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['low_in_stock'] = array(
'description' => __( 'Limit result set to products that are low or out of stock. (Deprecated)', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'sanitize_callback' => 'wc_string_to_bool',
);
$params['search'] = array(
'description' => __( 'Search by similar product name or sku.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Add product name and sku filtering to the WC API.
*
* @param WP_REST_Request $request Request data.
* @return array
*/
protected function prepare_objects_query( $request ) {
$args = parent::prepare_objects_query( $request );
if ( ! empty( $request['search'] ) ) {
$args['search'] = trim( $request['search'] );
unset( $args['s'] );
}
if ( ! empty( $request['low_in_stock'] ) ) {
$args['low_in_stock'] = $request['low_in_stock'];
$args['post_type'] = array( 'product', 'product_variation' );
}
return $args;
}
/**
* Get a collection of posts and add the post title filter option to WP_Query.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
add_filter( 'posts_fields', array( __CLASS__, 'add_wp_query_fields' ), 10, 2 );
add_filter( 'posts_where', array( __CLASS__, 'add_wp_query_filter' ), 10, 2 );
add_filter( 'posts_join', array( __CLASS__, 'add_wp_query_join' ), 10, 2 );
add_filter( 'posts_groupby', array( __CLASS__, 'add_wp_query_group_by' ), 10, 2 );
$response = parent::get_items( $request );
remove_filter( 'posts_fields', array( __CLASS__, 'add_wp_query_fields' ), 10 );
remove_filter( 'posts_where', array( __CLASS__, 'add_wp_query_filter' ), 10 );
remove_filter( 'posts_join', array( __CLASS__, 'add_wp_query_join' ), 10 );
remove_filter( 'posts_groupby', array( __CLASS__, 'add_wp_query_group_by' ), 10 );
/**
* The low stock query caused performance issues in WooCommerce 5.5.1
* due to a) being slow, and b) multiple requests being made to this endpoint
* from WC Admin.
*
* This is a temporary measure to trigger the user’s browser to cache the
* endpoint response for 1 minute, limiting the amount of requests overall.
*
* https://github.com/woocommerce/woocommerce-admin/issues/7358
*/
if ( $this->is_low_in_stock_request( $request ) ) {
$response->header( 'Cache-Control', 'max-age=300' );
}
return $response;
}
/**
* Check whether the request is for products low in stock.
*
* It matches requests with parameters:
*
* low_in_stock = true
* page = 1
* fields[0] = id
*
* @param string $request WP REST API request.
* @return boolean Whether the request matches.
*/
private function is_low_in_stock_request( $request ) {
if (
$request->get_param( 'low_in_stock' ) === true &&
$request->get_param( 'page' ) === 1 &&
is_array( $request->get_param( '_fields' ) ) &&
count( $request->get_param( '_fields' ) ) === 1 &&
in_array( 'id', $request->get_param( '_fields' ), true )
) {
return true;
}
return false;
}
/**
* Hang onto last order date since it will get removed by wc_get_product().
*
* @param stdClass $object_data Single row from query results.
* @return WC_Data
*/
public function get_object( $object_data ) {
if ( isset( $object_data->last_order_date ) ) {
$this->last_order_dates[ $object_data->ID ] = $object_data->last_order_date;
}
return parent::get_object( $object_data );
}
/**
* Add `low_stock_amount` property to product data
*
* @param WC_Data $object Object data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_object_for_response( $object, $request ) {
$data = parent::prepare_object_for_response( $object, $request );
$object_data = $object->get_data();
$product_id = $object_data['id'];
if ( $request->get_param( 'low_in_stock' ) ) {
if ( is_numeric( $object_data['low_stock_amount'] ) ) {
$data->data['low_stock_amount'] = $object_data['low_stock_amount'];
}
if ( isset( $this->last_order_dates[ $product_id ] ) ) {
$data->data['last_order_date'] = wc_rest_prepare_date_response( $this->last_order_dates[ $product_id ] );
}
}
if ( isset( $data->data['name'] ) ) {
$data->data['name'] = wp_strip_all_tags( $data->data['name'] );
}
return $data;
}
/**
* Add in conditional select fields to the query.
*
* @internal
* @param string $select Select clause used to select fields from the query.
* @param object $wp_query WP_Query object.
* @return string
*/
public static function add_wp_query_fields( $select, $wp_query ) {
if ( $wp_query->get( 'low_in_stock' ) ) {
$fields = array(
'low_stock_amount_meta.meta_value AS low_stock_amount',
'MAX( product_lookup.date_created ) AS last_order_date',
);
$select .= ', ' . implode( ', ', $fields );
}
return $select;
}
/**
* Add in conditional search filters for products.
*
* @internal
* @param string $where Where clause used to search posts.
* @param object $wp_query WP_Query object.
* @return string
*/
public static function add_wp_query_filter( $where, $wp_query ) {
global $wpdb;
$search = $wp_query->get( 'search' );
if ( $search ) {
$title_like = '%' . $wpdb->esc_like( $search ) . '%';
$where .= $wpdb->prepare( " AND ({$wpdb->posts}.post_title LIKE %s", $title_like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$where .= wc_product_sku_enabled() ? $wpdb->prepare( ' OR wc_product_meta_lookup.sku LIKE %s)', $search ) : ')';
}
if ( $wp_query->get( 'low_in_stock' ) ) {
$low_stock_amount = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
$where .= "
AND wc_product_meta_lookup.stock_quantity IS NOT NULL
AND wc_product_meta_lookup.stock_status IN('instock','outofstock')
AND (
(
low_stock_amount_meta.meta_value > ''
AND wc_product_meta_lookup.stock_quantity <= CAST(low_stock_amount_meta.meta_value AS SIGNED)
)
OR (
(
low_stock_amount_meta.meta_value IS NULL OR low_stock_amount_meta.meta_value <= ''
)
AND wc_product_meta_lookup.stock_quantity <= {$low_stock_amount}
)
)";
}
return $where;
}
/**
* Join posts meta tables when product search or low stock query is present.
*
* @internal
* @param string $join Join clause used to search posts.
* @param object $wp_query WP_Query object.
* @return string
*/
public static function add_wp_query_join( $join, $wp_query ) {
global $wpdb;
$search = $wp_query->get( 'search' );
if ( $search && wc_product_sku_enabled() ) {
$join = self::append_product_sorting_table_join( $join );
}
if ( $wp_query->get( 'low_in_stock' ) ) {
$product_lookup_table = $wpdb->prefix . 'wc_order_product_lookup';
$join = self::append_product_sorting_table_join( $join );
$join .= " LEFT JOIN {$wpdb->postmeta} AS low_stock_amount_meta ON {$wpdb->posts}.ID = low_stock_amount_meta.post_id AND low_stock_amount_meta.meta_key = '_low_stock_amount' ";
$join .= " LEFT JOIN {$product_lookup_table} product_lookup ON {$wpdb->posts}.ID = CASE
WHEN {$wpdb->posts}.post_type = 'product' THEN product_lookup.product_id
WHEN {$wpdb->posts}.post_type = 'product_variation' THEN product_lookup.variation_id
END";
}
return $join;
}
/**
* Join wc_product_meta_lookup to posts if not already joined.
*
* @internal
* @param string $sql SQL join.
* @return string
*/
protected static function append_product_sorting_table_join( $sql ) {
global $wpdb;
if ( ! strstr( $sql, 'wc_product_meta_lookup' ) ) {
$sql .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id ";
}
return $sql;
}
/**
* Group by post ID to prevent duplicates.
*
* @internal
* @param string $groupby Group by clause used to organize posts.
* @param object $wp_query WP_Query object.
* @return string
*/
public static function add_wp_query_group_by( $groupby, $wp_query ) {
global $wpdb;
$search = $wp_query->get( 'search' );
$low_in_stock = $wp_query->get( 'low_in_stock' );
if ( empty( $groupby ) && ( $search || $low_in_stock ) ) {
$groupby = $wpdb->posts . '.ID';
}
return $groupby;
}
}
API/ProductsLowInStock.php 0000644 00000023012 15153746747 0011467 0 ustar 00 <?php
/**
* REST API ProductsLowInStock Controller
*
* Handles request to /products/low-in-stock
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* ProductsLowInStock controller.
*
* @internal
* @extends WC_REST_Products_Controller
*/
final class ProductsLowInStock extends \WC_REST_Products_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'products/low-in-stock',
array(
'args' => array(),
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Get low in stock products.
*
* @param WP_REST_Request $request request object.
*
* @return WP_REST_Response|WP_ERROR
*/
public function get_items( $request ) {
$query_results = $this->get_low_in_stock_products(
$request->get_param( 'page' ),
$request->get_param( 'per_page' ),
$request->get_param( 'status' )
);
// set images and attributes.
$query_results['results'] = array_map(
function( $query_result ) {
$product = wc_get_product( $query_result );
$query_result->images = $this->get_images( $product );
$query_result->attributes = $this->get_attributes( $product );
return $query_result;
},
$query_results['results']
);
// set last_order_date.
$query_results['results'] = $this->set_last_order_date( $query_results['results'] );
// convert the post data to the expected API response for the backward compatibility.
$query_results['results'] = array_map( array( $this, 'transform_post_to_api_response' ), $query_results['results'] );
$response = rest_ensure_response( array_values( $query_results['results'] ) );
$response->header( 'X-WP-Total', $query_results['total'] );
$response->header( 'X-WP-TotalPages', $query_results['pages'] );
return $response;
}
/**
* Set the last order date for each data.
*
* @param array $results query result from get_low_in_stock_products.
*
* @return mixed
*/
protected function set_last_order_date( $results = array() ) {
global $wpdb;
if ( 0 === count( $results ) ) {
return $results;
}
$wheres = array();
foreach ( $results as $result ) {
'product_variation' === $result->post_type ?
array_push( $wheres, "(product_id={$result->post_parent} and variation_id={$result->ID})" )
: array_push( $wheres, "product_id={$result->ID}" );
}
count( $wheres ) ? $where_clause = implode( ' or ', $wheres ) : $where_clause = $wheres[0];
$product_lookup_table = $wpdb->prefix . 'wc_order_product_lookup';
$query_string = "
select
product_id,
variation_id,
MAX( wc_order_product_lookup.date_created ) AS last_order_date
from {$product_lookup_table} wc_order_product_lookup
where {$where_clause}
group by product_id
order by date_created desc
";
// phpcs:ignore -- ignore prepare() warning as we're not using any user input here.
$last_order_dates = $wpdb->get_results( $query_string );
$last_order_dates_index = array();
// Make an index with product_id_variation_id as a key
// so that it can be referenced back without looping the whole array.
foreach ( $last_order_dates as $last_order_date ) {
$last_order_dates_index[ $last_order_date->product_id . '_' . $last_order_date->variation_id ] = $last_order_date;
}
foreach ( $results as &$result ) {
'product_variation' === $result->post_type ?
$index_key = $result->post_parent . '_' . $result->ID
: $index_key = $result->ID . '_' . $result->post_parent;
if ( isset( $last_order_dates_index[ $index_key ] ) ) {
$result->last_order_date = $last_order_dates_index[ $index_key ]->last_order_date;
}
}
return $results;
}
/**
* Get low in stock products data.
*
* @param int $page current page.
* @param int $per_page items per page.
* @param string $status post status.
*
* @return array
*/
protected function get_low_in_stock_products( $page = 1, $per_page = 1, $status = 'publish' ) {
global $wpdb;
$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() );
$query_results = $wpdb->get_results(
// phpcs:ignore -- not sure why phpcs complains about this line when prepare() is used here.
$wpdb->prepare( $query_string, $status, $low_stock_threshold, $offset, $per_page ),
OBJECT_K
);
$total_results = $wpdb->get_var( 'SELECT FOUND_ROWS()' );
return array(
'results' => $query_results,
'total' => (int) $total_results,
'pages' => (int) ceil( $total_results / (int) $per_page ),
);
}
/**
* Check to see if store is using sitewide threshold only. Meaning that it does not have any custom
* stock threshold for a product.
*
* @return bool
*/
protected function is_using_sitewide_stock_threshold_only() {
global $wpdb;
$count = $wpdb->get_var( "select count(*) as total from {$wpdb->postmeta} where meta_key='_low_stock_amount'" );
return 0 === (int) $count;
}
/**
* Transform post object to expected API response.
*
* @param object $query_result a row of query result from get_low_in_stock_products().
*
* @return array
*/
protected function transform_post_to_api_response( $query_result ) {
$low_stock_amount = null;
if ( isset( $query_result->low_stock_amount ) ) {
$low_stock_amount = (int) $query_result->low_stock_amount;
}
if ( ! isset( $query_result->last_order_date ) ) {
$query_result->last_order_date = null;
}
return array(
'id' => (int) $query_result->ID,
'images' => $query_result->images,
'attributes' => $query_result->attributes,
'low_stock_amount' => $low_stock_amount,
'last_order_date' => wc_rest_prepare_date_response( $query_result->last_order_date ),
'name' => $query_result->post_title,
'parent_id' => (int) $query_result->post_parent,
'stock_quantity' => (int) $query_result->stock_quantity,
'type' => 'product_variation' === $query_result->post_type ? 'variation' : 'simple',
);
}
/**
* Generate a query.
*
* @param bool $siteside_only generates a query for sitewide low stock threshold only query.
*
* @return string
*/
protected function get_query( $siteside_only = false ) {
global $wpdb;
$query = "
SELECT
SQL_CALC_FOUND_ROWS wp_posts.*,
:postmeta_select
wc_product_meta_lookup.stock_quantity
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
:postmeta_join
WHERE
wp_posts.post_type IN ('product', 'product_variation')
AND wp_posts.post_status = %s
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
";
$postmeta = array(
'select' => '',
'join' => '',
'wheres' => 'AND wc_product_meta_lookup.stock_quantity <= %d',
);
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 (
(
meta.meta_value > ''
AND wc_product_meta_lookup.stock_quantity <= CAST(
meta.meta_value AS SIGNED
)
)
OR (
(
meta.meta_value IS NULL
OR meta.meta_value <= ''
)
AND wc_product_meta_lookup.stock_quantity <= %d
)
)";
}
return strtr(
$query,
array(
':postmeta_select' => $postmeta['select'],
':postmeta_join' => $postmeta['join'],
':postmeta_wheres' => $postmeta['wheres'],
)
);
}
/**
* Get the query params for collections of attachments.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param();
$params['context']['default'] = 'view';
$params['page'] = array(
'description' => __( 'Current page of the collection.', 'woocommerce' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);
$params['per_page'] = array(
'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$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;
}
}
API/Reports/Cache.php 0000644 00000002753 15153746747 0010361 0 ustar 00 <?php
/**
* REST API Reports Cache.
*
* Handles report data object caching.
*/
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit;
/**
* REST API Reports Cache class.
*/
class Cache {
/**
* Cache version. Used to invalidate all cached values.
*/
const VERSION_OPTION = 'woocommerce_reports';
/**
* Invalidate cache.
*/
public static function invalidate() {
\WC_Cache_Helper::get_transient_version( self::VERSION_OPTION, true );
}
/**
* Get cache version number.
*
* @return string
*/
public static function get_version() {
$version = \WC_Cache_Helper::get_transient_version( self::VERSION_OPTION );
return $version;
}
/**
* Get cached value.
*
* @param string $key Cache key.
* @return mixed
*/
public static function get( $key ) {
$transient_version = self::get_version();
$transient_value = get_transient( $key );
if (
isset( $transient_value['value'], $transient_value['version'] ) &&
$transient_value['version'] === $transient_version
) {
return $transient_value['value'];
}
return false;
}
/**
* Update cached value.
*
* @param string $key Cache key.
* @param mixed $value New value.
* @return bool
*/
public static function set( $key, $value ) {
$transient_version = self::get_version();
$transient_value = array(
'version' => $transient_version,
'value' => $value,
);
$result = set_transient( $key, $transient_value, WEEK_IN_SECONDS );
return $result;
}
}
API/Reports/Categories/Controller.php 0000644 00000026677 15153746747 0013601 0 ustar 00 <?php
/**
* REST API Reports categories controller
*
* Handles requests to the /reports/categories endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Categories;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Controller as ReportsController;
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
/**
* REST API Reports categories controller class.
*
* @internal
* @extends \Automattic\WooCommerce\Admin\API\Reports\Controller
*/
class Controller extends ReportsController implements ExportableInterface {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/categories';
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['before'] = $request['before'];
$args['after'] = $request['after'];
$args['interval'] = $request['interval'];
$args['page'] = $request['page'];
$args['per_page'] = $request['per_page'];
$args['orderby'] = $request['orderby'];
$args['order'] = $request['order'];
$args['extended_info'] = $request['extended_info'];
$args['category_includes'] = (array) $request['categories'];
$args['status_is'] = (array) $request['status_is'];
$args['status_is_not'] = (array) $request['status_is_not'];
$args['force_cache_refresh'] = $request['force_cache_refresh'];
return $args;
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$categories_query = new Query( $query_args );
$report_data = $categories_query->get_data();
if ( is_wp_error( $report_data ) ) {
return $report_data;
}
if ( ! isset( $report_data->data ) || ! isset( $report_data->page_no ) || ! isset( $report_data->pages ) ) {
return new \WP_Error( 'woocommerce_rest_reports_categories_invalid_response', __( 'Invalid response from data store.', 'woocommerce' ), array( 'status' => 500 ) );
}
$out_data = array();
foreach ( $report_data->data as $datum ) {
$item = $this->prepare_item_for_response( $datum, $request );
$out_data[] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$out_data,
(int) $report_data->total,
(int) $report_data->page_no,
(int) $report_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param stdClass $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$data = $report;
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $report ) );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_categories', $response, $report, $request );
}
/**
* Prepare links for the request.
*
* @param \Automattic\WooCommerce\Admin\API\Reports\Query $object Object data.
* @return array
*/
protected function prepare_links( $object ) {
$links = array(
'category' => array(
'href' => rest_url( sprintf( '/%s/products/categories/%d', $this->namespace, $object['category_id'] ) ),
),
);
return $links;
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_categories',
'type' => 'object',
'properties' => array(
'category_id' => array(
'description' => __( 'Category ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'items_sold' => array(
'description' => __( 'Amount of items sold.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'net_revenue' => array(
'description' => __( 'Total sales.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'orders_count' => array(
'description' => __( 'Number of orders.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'products_count' => array(
'description' => __( 'Amount of products.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'extended_info' => array(
'name' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Category name.', 'woocommerce' ),
),
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['page'] = array(
'description' => __( 'Current page of the collection.', 'woocommerce' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);
$params['per_page'] = array(
'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['after'] = array(
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['before'] = array(
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
'validate_callback' => 'rest_validate_request_arg',
);
$params['orderby'] = array(
'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
'type' => 'string',
'default' => 'category_id',
'enum' => array(
'category_id',
'items_sold',
'net_revenue',
'orders_count',
'products_count',
'category',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['interval'] = array(
'description' => __( 'Time interval to use for buckets in the returned data.', 'woocommerce' ),
'type' => 'string',
'default' => 'week',
'enum' => array(
'hour',
'day',
'week',
'month',
'quarter',
'year',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['status_is'] = array(
'description' => __( 'Limit result set to items that have the specified order status.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => self::get_order_statuses(),
'type' => 'string',
),
);
$params['status_is_not'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified order status.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => self::get_order_statuses(),
'type' => 'string',
),
);
$params['categories'] = array(
'description' => __( 'Limit result set to all items that have the specified term assigned in the categories taxonomy.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['extended_info'] = array(
'description' => __( 'Add additional piece of info about each category to the report.', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'sanitize_callback' => 'wc_string_to_bool',
'validate_callback' => 'rest_validate_request_arg',
);
$params['force_cache_refresh'] = array(
'description' => __( 'Force retrieval of fresh data instead of from the cache.', 'woocommerce' ),
'type' => 'boolean',
'sanitize_callback' => 'wp_validate_boolean',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get the column names for export.
*
* @return array Key value pair of Column ID => Label.
*/
public function get_export_columns() {
$export_columns = array(
'category' => __( 'Category', 'woocommerce' ),
'items_sold' => __( 'Items sold', 'woocommerce' ),
'net_revenue' => __( 'Net Revenue', 'woocommerce' ),
'products_count' => __( 'Products', 'woocommerce' ),
'orders_count' => __( 'Orders', 'woocommerce' ),
);
/**
* Filter to add or remove column names from the categories report for
* export.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_report_categories_export_columns',
$export_columns
);
}
/**
* Get the column values for export.
*
* @param array $item Single report item/row.
* @return array Key value pair of Column ID => Row Value.
*/
public function prepare_item_for_export( $item ) {
$export_item = array(
'category' => $item['extended_info']['name'],
'items_sold' => $item['items_sold'],
'net_revenue' => $item['net_revenue'],
'products_count' => $item['products_count'],
'orders_count' => $item['orders_count'],
);
/**
* Filter to prepare extra columns in the export item for the
* categories export.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_report_categories_prepare_export_item',
$export_item,
$item
);
}
}
API/Reports/Categories/DataStore.php 0000644 00000025215 15153746747 0013327 0 ustar 00 <?php
/**
* API\Reports\Categories\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Categories;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
/**
* API\Reports\Categories\DataStore.
*/
class DataStore extends ReportsDataStore implements DataStoreInterface {
/**
* Table used to get the data.
*
* @var string
*/
protected static $table_name = 'wc_order_product_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'categories';
/**
* Order by setting used for sorting categories data.
*
* @var string
*/
private $order_by = '';
/**
* Order setting used for sorting categories data.
*
* @var string
*/
private $order = '';
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'category_id' => 'intval',
'items_sold' => 'intval',
'net_revenue' => 'floatval',
'orders_count' => 'intval',
'products_count' => 'intval',
);
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'categories';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$table_name = self::get_db_table_name();
$this->report_columns = array(
'items_sold' => 'SUM(product_qty) as items_sold',
'net_revenue' => 'SUM(product_net_revenue) AS net_revenue',
'orders_count' => "COUNT(DISTINCT {$table_name}.order_id) as orders_count",
'products_count' => "COUNT(DISTINCT {$table_name}.product_id) as products_count",
);
}
/**
* Return the database query with parameters used for Categories report: time span and order status.
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function add_sql_query_params( $query_args ) {
global $wpdb;
$order_product_lookup_table = self::get_db_table_name();
$this->add_time_period_sql_params( $query_args, $order_product_lookup_table );
// join wp_order_product_lookup_table with relationships and taxonomies
// @todo How to handle custom product tables?
$this->subquery->add_sql_clause( 'left_join', "LEFT JOIN {$wpdb->term_relationships} ON {$order_product_lookup_table}.product_id = {$wpdb->term_relationships}.object_id" );
// Adding this (inner) JOIN as a LEFT JOIN for ordering purposes. See comment in add_order_by_params().
$this->subquery->add_sql_clause( 'left_join', "JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id" );
$included_categories = $this->get_included_categories( $query_args );
if ( $included_categories ) {
$this->subquery->add_sql_clause( 'where', "AND {$wpdb->term_relationships}.term_taxonomy_id IN ({$included_categories})" );
// Limit is left out here so that the grouping in code by PHP can be applied correctly.
// This also needs to be put after the term_taxonomy JOIN so that we can match the correct term name.
$this->add_order_by_params( $query_args, 'outer', 'default_results.category_id' );
} else {
$this->add_order_by_params( $query_args, 'inner', "{$wpdb->term_relationships}.term_taxonomy_id" );
}
$this->add_order_status_clause( $query_args, $order_product_lookup_table, $this->subquery );
$this->subquery->add_sql_clause( 'where', "AND {$wpdb->term_taxonomy}.taxonomy = 'product_cat'" );
}
/**
* Fills ORDER BY clause of SQL request based on user supplied parameters.
*
* @param array $query_args Parameters supplied by the user.
* @param string $from_arg Target of the JOIN sql param.
* @param string $id_cell ID cell identifier, like `table_name.id_column_name`.
*/
protected function add_order_by_params( $query_args, $from_arg, $id_cell ) {
global $wpdb;
// Sanitize input: guarantee that the id cell in the join is quoted with backticks.
$id_cell_segments = explode( '.', str_replace( '`', '', $id_cell ) );
$id_cell_identifier = '`' . implode( '`.`', $id_cell_segments ) . '`';
$lookup_table = self::get_db_table_name();
$order_by_clause = $this->add_order_by_clause( $query_args, $this );
$this->add_orderby_order_clause( $query_args, $this );
if ( false !== strpos( $order_by_clause, '_terms' ) ) {
$join = "JOIN {$wpdb->terms} AS _terms ON {$id_cell_identifier} = _terms.term_id";
if ( 'inner' === $from_arg ) {
// Even though this is an (inner) JOIN, we're adding it as a `left_join` to
// affect its order in the query statement. The SqlQuery::$sql_filters variable
// determines the order in which joins are concatenated.
// See: https://github.com/woocommerce/woocommerce-admin/blob/1f261998e7287b77bc13c3d4ee2e84b717da7957/src/API/Reports/SqlQuery.php#L46-L50.
$this->subquery->add_sql_clause( 'left_join', $join );
} else {
$this->add_sql_clause( 'join', $join );
}
}
}
/**
* Maps ordering specified by the user to columns in the database/fields in the data.
*
* @param string $order_by Sorting criterion.
* @return string
*/
protected function normalize_order_by( $order_by ) {
if ( 'date' === $order_by ) {
return 'time_interval';
}
if ( 'category' === $order_by ) {
return '_terms.name';
}
return $order_by;
}
/**
* Returns an array of ids of included categories, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_included_categories_array( $query_args ) {
if ( isset( $query_args['category_includes'] ) && is_array( $query_args['category_includes'] ) && count( $query_args['category_includes'] ) > 0 ) {
return $query_args['category_includes'];
}
return array();
}
/**
* Returns the page of data according to page number and items per page.
*
* @param array $data Data to paginate.
* @param integer $page_no Page number.
* @param integer $items_per_page Number of items per page.
* @return array
*/
protected function page_records( $data, $page_no, $items_per_page ) {
$offset = ( $page_no - 1 ) * $items_per_page;
return array_slice( $data, $offset, $items_per_page );
}
/**
* Enriches the category data.
*
* @param array $categories_data Categories data.
* @param array $query_args Query parameters.
*/
protected function include_extended_info( &$categories_data, $query_args ) {
foreach ( $categories_data as $key => $category_data ) {
$extended_info = new \ArrayObject();
if ( $query_args['extended_info'] ) {
$extended_info['name'] = get_the_category_by_ID( $category_data['category_id'] );
}
$categories_data[ $key ]['extended_info'] = $extended_info;
}
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$table_name = self::get_db_table_name();
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
'fields' => '*',
'category_includes' => array(),
'extended_info' => false,
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$data = (object) array(
'data' => array(),
'total' => 0,
'pages' => 0,
'page_no' => 0,
);
$this->subquery->add_sql_clause( 'select', $this->selected_columns( $query_args ) );
$included_categories = $this->get_included_categories_array( $query_args );
$this->add_sql_query_params( $query_args );
if ( count( $included_categories ) > 0 ) {
$fields = $this->get_fields( $query_args );
$ids_table = $this->get_ids_table( $included_categories, 'category_id' );
$this->add_sql_clause( 'select', $this->format_join_selections( array_merge( array( 'category_id' ), $fields ), array( 'category_id' ) ) );
$this->add_sql_clause( 'from', '(' );
$this->add_sql_clause( 'from', $this->subquery->get_query_statement() );
$this->add_sql_clause( 'from', ") AS {$table_name}" );
$this->add_sql_clause(
'right_join',
"RIGHT JOIN ( {$ids_table} ) AS default_results
ON default_results.category_id = {$table_name}.category_id"
);
$categories_query = $this->get_query_statement();
} else {
$this->subquery->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$categories_query = $this->subquery->get_query_statement();
}
$categories_data = $wpdb->get_results(
$categories_query, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
ARRAY_A
);
if ( null === $categories_data ) {
return new \WP_Error( 'woocommerce_analytics_categories_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce' ), array( 'status' => 500 ) );
}
$record_count = count( $categories_data );
$total_pages = (int) ceil( $record_count / $query_args['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return $data;
}
$categories_data = $this->page_records( $categories_data, $query_args['page'], $query_args['per_page'] );
$this->include_extended_info( $categories_data, $query_args );
$categories_data = array_map( array( $this, 'cast_numbers' ), $categories_data );
$data = (object) array(
'data' => $categories_data,
'total' => $record_count,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
global $wpdb;
$this->subquery = new SqlQuery( $this->context . '_subquery' );
$this->subquery->add_sql_clause( 'select', "{$wpdb->term_taxonomy}.term_id as category_id," );
$this->subquery->add_sql_clause( 'from', self::get_db_table_name() );
$this->subquery->add_sql_clause( 'group_by', "{$wpdb->term_taxonomy}.term_id" );
}
}
API/Reports/Categories/Query.php 0000644 00000002367 15153746747 0012551 0 ustar 00 <?php
/**
* Class for parameter-based Categories Report querying
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'page' => 2,
* 'order' => 'desc',
* 'orderby' => 'items_sold',
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Categories\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Categories;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Query
*/
class Query extends ReportsQuery {
const REPORT_NAME = 'report-categories';
/**
* Valid fields for Categories report.
*
* @return array
*/
protected function get_default_query_vars() {
return array();
}
/**
* Get categories data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_categories_query_args', $this->get_query_vars() );
$results = \WC_Data_Store::load( self::REPORT_NAME )->get_data( $args );
return apply_filters( 'woocommerce_analytics_categories_select_query', $results, $args );
}
}
API/Reports/Controller.php 0000644 00000022026 15153746747 0011474 0 ustar 00 <?php
/**
* REST API Reports controller extended by WC Admin plugin.
*
* Handles requests to the reports endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericController;
/**
* REST API Reports controller class.
*
* @internal
* @extends GenericController
*/
class Controller extends GenericController {
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$data = array();
$reports = array(
array(
'slug' => 'performance-indicators',
'description' => __( 'Batch endpoint for getting specific performance indicators from `stats` endpoints.', 'woocommerce' ),
),
array(
'slug' => 'revenue/stats',
'description' => __( 'Stats about revenue.', 'woocommerce' ),
),
array(
'slug' => 'orders/stats',
'description' => __( 'Stats about orders.', 'woocommerce' ),
),
array(
'slug' => 'products',
'description' => __( 'Products detailed reports.', 'woocommerce' ),
),
array(
'slug' => 'products/stats',
'description' => __( 'Stats about products.', 'woocommerce' ),
),
array(
'slug' => 'variations',
'description' => __( 'Variations detailed reports.', 'woocommerce' ),
),
array(
'slug' => 'variations/stats',
'description' => __( 'Stats about variations.', 'woocommerce' ),
),
array(
'slug' => 'categories',
'description' => __( 'Product categories detailed reports.', 'woocommerce' ),
),
array(
'slug' => 'categories/stats',
'description' => __( 'Stats about product categories.', 'woocommerce' ),
),
array(
'slug' => 'coupons',
'description' => __( 'Coupons detailed reports.', 'woocommerce' ),
),
array(
'slug' => 'coupons/stats',
'description' => __( 'Stats about coupons.', 'woocommerce' ),
),
array(
'slug' => 'taxes',
'description' => __( 'Taxes detailed reports.', 'woocommerce' ),
),
array(
'slug' => 'taxes/stats',
'description' => __( 'Stats about taxes.', 'woocommerce' ),
),
array(
'slug' => 'downloads',
'description' => __( 'Product downloads detailed reports.', 'woocommerce' ),
),
array(
'slug' => 'downloads/files',
'description' => __( 'Product download files detailed reports.', 'woocommerce' ),
),
array(
'slug' => 'downloads/stats',
'description' => __( 'Stats about product downloads.', 'woocommerce' ),
),
array(
'slug' => 'customers',
'description' => __( 'Customers detailed reports.', 'woocommerce' ),
),
);
/**
* Filter the list of allowed reports, so that data can be loaded from third party extensions in addition to WooCommerce core.
* Array items should be in format of array( 'slug' => 'downloads/stats', 'description' => '',
* 'url' => '', and 'path' => '/wc-ext/v1/...'.
*
* @param array $endpoints The list of allowed reports..
*/
$reports = apply_filters( 'woocommerce_admin_reports', $reports );
foreach ( $reports as $report ) {
if ( empty( $report['slug'] ) ) {
continue;
}
if ( empty( $report['path'] ) ) {
$report['path'] = '/' . $this->namespace . '/reports/' . $report['slug'];
}
// Allows a different admin page to be loaded here,
// or allows an empty url if no report exists for a set of performance indicators.
if ( ! isset( $report['url'] ) ) {
if ( '/stats' === substr( $report['slug'], -6 ) ) {
$url_slug = substr( $report['slug'], 0, -6 );
} else {
$url_slug = $report['slug'];
}
$report['url'] = '/analytics/' . $url_slug;
}
$item = $this->prepare_item_for_response( (object) $report, $request );
$data[] = $this->prepare_response_for_collection( $item );
}
return rest_ensure_response( $data );
}
/**
* Get the order number for an order. If no filter is present for `woocommerce_order_number`, we can just return the ID.
* Returns the parent order number if the order is actually a refund.
*
* @param int $order_id Order ID.
* @return string
*/
protected function get_order_number( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order instanceof \WC_Order && ! $order instanceof \WC_Order_Refund ) {
return null;
}
if ( 'shop_order_refund' === $order->get_type() ) {
$order = wc_get_order( $order->get_parent_id() );
}
if ( ! has_filter( 'woocommerce_order_number' ) ) {
return $order->get_id();
}
return $order->get_order_number();
}
/**
* Get the order total with the related currency formatting.
* Returns the parent order total if the order is actually a refund.
*
* @param int $order_id Order ID.
* @return string
*/
protected function get_total_formatted( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order instanceof \WC_Order && ! $order instanceof \WC_Order_Refund ) {
return null;
}
if ( 'shop_order_refund' === $order->get_type() ) {
$order = wc_get_order( $order->get_parent_id() );
}
return wp_strip_all_tags( html_entity_decode( $order->get_formatted_order_total() ), true );
}
/**
* Prepare a report object for serialization.
*
* @param stdClass $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$data = array(
'slug' => $report->slug,
'description' => $report->description,
'path' => $report->path,
);
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links(
array(
'self' => array(
'href' => rest_url( $report->path ),
),
'report' => array(
'href' => $report->url,
),
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
)
);
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report', $response, $report, $request );
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report',
'type' => 'object',
'properties' => array(
'slug' => array(
'description' => __( 'An alphanumeric identifier for the resource.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
),
'description' => array(
'description' => __( 'A human-readable description of the resource.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
),
'path' => array(
'description' => __( 'API path.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
return array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
);
}
/**
* Get order statuses without prefixes.
* Includes unregistered statuses that have been marked "actionable".
*
* @internal
* @return array
*/
public static function get_order_statuses() {
// Allow all statuses selected as "actionable" - this may include unregistered statuses.
// See: https://github.com/woocommerce/woocommerce-admin/issues/5592.
$actionable_statuses = get_option( 'woocommerce_actionable_order_statuses', array() );
// See WC_REST_Orders_V2_Controller::get_collection_params() re: any/trash statuses.
$registered_statuses = array_merge( array( 'any', 'trash' ), array_keys( self::get_order_status_labels() ) );
// Merge the status arrays (using flip to avoid array_unique()).
$allowed_statuses = array_keys( array_merge( array_flip( $registered_statuses ), array_flip( $actionable_statuses ) ) );
return $allowed_statuses;
}
/**
* Get order statuses (and labels) without prefixes.
*
* @internal
* @return array
*/
public static function get_order_status_labels() {
$order_statuses = array();
foreach ( wc_get_order_statuses() as $key => $label ) {
$new_key = str_replace( 'wc-', '', $key );
$order_statuses[ $new_key ] = $label;
}
return $order_statuses;
}
}
API/Reports/Coupons/Controller.php 0000644 00000020242 15153746747 0013120 0 ustar 00 <?php
/**
* REST API Reports coupons controller
*
* Handles requests to the /reports/coupons endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericController;
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
use WP_REST_Request;
use WP_REST_Response;
/**
* REST API Reports coupons controller class.
*
* @internal
* @extends GenericController
*/
class Controller extends GenericController implements ExportableInterface {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/coupons';
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['before'] = $request['before'];
$args['after'] = $request['after'];
$args['page'] = $request['page'];
$args['per_page'] = $request['per_page'];
$args['orderby'] = $request['orderby'];
$args['order'] = $request['order'];
$args['coupons'] = (array) $request['coupons'];
$args['extended_info'] = $request['extended_info'];
$args['force_cache_refresh'] = $request['force_cache_refresh'];
return $args;
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$coupons_query = new Query( $query_args );
$report_data = $coupons_query->get_data();
$data = array();
foreach ( $report_data->data as $coupons_data ) {
$item = $this->prepare_item_for_response( $coupons_data, $request );
$data[] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$data,
(int) $report_data->total,
(int) $report_data->page_no,
(int) $report_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param array $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$response = parent::prepare_item_for_response( $report, $request );
$response->add_links( $this->prepare_links( $report ) );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_coupons', $response, $report, $request );
}
/**
* Prepare links for the request.
*
* @param WC_Reports_Query $object Object data.
* @return array
*/
protected function prepare_links( $object ) {
$links = array(
'coupon' => array(
'href' => rest_url( sprintf( '/%s/coupons/%d', $this->namespace, $object['coupon_id'] ) ),
),
);
return $links;
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_coupons',
'type' => 'object',
'properties' => array(
'coupon_id' => array(
'description' => __( 'Coupon ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'amount' => array(
'description' => __( 'Net discount amount.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'orders_count' => array(
'description' => __( 'Number of orders.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'extended_info' => array(
'code' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon code.', 'woocommerce' ),
),
'date_created' => array(
'type' => 'date-time',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon creation date.', 'woocommerce' ),
),
'date_created_gmt' => array(
'type' => 'date-time',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon creation date in GMT.', 'woocommerce' ),
),
'date_expires' => array(
'type' => 'date-time',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon expiration date.', 'woocommerce' ),
),
'date_expires_gmt' => array(
'type' => 'date-time',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon expiration date in GMT.', 'woocommerce' ),
),
'discount_type' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'enum' => array_keys( wc_get_coupon_types() ),
'description' => __( 'Coupon discount type.', 'woocommerce' ),
),
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['orderby']['default'] = 'coupon_id';
$params['orderby']['enum'] = array(
'coupon_id',
'code',
'amount',
'orders_count',
);
$params['coupons'] = array(
'description' => __( 'Limit result set to coupons assigned specific coupon IDs.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['extended_info'] = array(
'description' => __( 'Add additional piece of info about each coupon to the report.', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'sanitize_callback' => 'wc_string_to_bool',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get the column names for export.
*
* @return array Key value pair of Column ID => Label.
*/
public function get_export_columns() {
$export_columns = array(
'code' => __( 'Coupon code', 'woocommerce' ),
'orders_count' => __( 'Orders', 'woocommerce' ),
'amount' => __( 'Amount discounted', 'woocommerce' ),
'created' => __( 'Created', 'woocommerce' ),
'expires' => __( 'Expires', 'woocommerce' ),
'type' => __( 'Type', 'woocommerce' ),
);
/**
* Filter to add or remove column names from the coupons report for
* export.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_report_coupons_export_columns',
$export_columns
);
}
/**
* Get the column values for export.
*
* @param array $item Single report item/row.
* @return array Key value pair of Column ID => Row Value.
*/
public function prepare_item_for_export( $item ) {
$date_expires = empty( $item['extended_info']['date_expires'] )
? __( 'N/A', 'woocommerce' )
: $item['extended_info']['date_expires'];
$export_item = array(
'code' => $item['extended_info']['code'],
'orders_count' => $item['orders_count'],
'amount' => $item['amount'],
'created' => $item['extended_info']['date_created'],
'expires' => $date_expires,
'type' => $item['extended_info']['discount_type'],
);
/**
* Filter to prepare extra columns in the export item for the coupons
* report.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_report_coupons_prepare_export_item',
$export_item,
$item
);
}
}
API/Reports/Coupons/DataStore.php 0000644 00000036734 15153746747 0012700 0 ustar 00 <?php
/**
* API\Reports\Coupons\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
use Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
/**
* API\Reports\Coupons\DataStore.
*/
class DataStore extends ReportsDataStore implements DataStoreInterface {
/**
* Table used to get the data.
*
* @var string
*/
protected static $table_name = 'wc_order_coupon_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'coupons';
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'coupon_id' => 'intval',
'amount' => 'floatval',
'orders_count' => 'intval',
);
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'coupons';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$table_name = self::get_db_table_name();
$this->report_columns = array(
'coupon_id' => 'coupon_id',
'amount' => 'SUM(discount_amount) as amount',
'orders_count' => "COUNT(DISTINCT {$table_name}.order_id) as orders_count",
);
}
/**
* Set up all the hooks for maintaining and populating table data.
*/
public static function init() {
add_action( 'woocommerce_analytics_delete_order_stats', array( __CLASS__, 'sync_on_order_delete' ), 5 );
}
/**
* Returns an array of ids of included coupons, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return array
*/
protected function get_included_coupons_array( $query_args ) {
if ( isset( $query_args['coupons'] ) && is_array( $query_args['coupons'] ) && count( $query_args['coupons'] ) > 0 ) {
return $query_args['coupons'];
}
return array();
}
/**
* Updates the database query with parameters used for Products report: categories and order status.
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function add_sql_query_params( $query_args ) {
global $wpdb;
$order_coupon_lookup_table = self::get_db_table_name();
$this->add_time_period_sql_params( $query_args, $order_coupon_lookup_table );
$this->get_limit_sql_params( $query_args );
$included_coupons = $this->get_included_coupons( $query_args, 'coupons' );
if ( $included_coupons ) {
$this->subquery->add_sql_clause( 'where', "AND {$order_coupon_lookup_table}.coupon_id IN ({$included_coupons})" );
$this->add_order_by_params( $query_args, 'outer', 'default_results.coupon_id' );
} else {
$this->add_order_by_params( $query_args, 'inner', "{$order_coupon_lookup_table}.coupon_id" );
}
$this->add_order_status_clause( $query_args, $order_coupon_lookup_table, $this->subquery );
}
/**
* Fills ORDER BY clause of SQL request based on user supplied parameters.
*
* @param array $query_args Parameters supplied by the user.
* @param string $from_arg Target of the JOIN sql param.
* @param string $id_cell ID cell identifier, like `table_name.id_column_name`.
*/
protected function add_order_by_params( $query_args, $from_arg, $id_cell ) {
global $wpdb;
// Sanitize input: guarantee that the id cell in the join is quoted with backticks.
$id_cell_segments = explode( '.', str_replace( '`', '', $id_cell ) );
$id_cell_identifier = '`' . implode( '`.`', $id_cell_segments ) . '`';
$lookup_table = self::get_db_table_name();
$order_by_clause = $this->add_order_by_clause( $query_args, $this );
$join = "JOIN {$wpdb->posts} AS _coupons ON {$id_cell_identifier} = _coupons.ID";
$this->add_orderby_order_clause( $query_args, $this );
if ( 'inner' === $from_arg ) {
$this->subquery->clear_sql_clause( 'join' );
if ( false !== strpos( $order_by_clause, '_coupons' ) ) {
$this->subquery->add_sql_clause( 'join', $join );
}
} else {
$this->clear_sql_clause( 'join' );
if ( false !== strpos( $order_by_clause, '_coupons' ) ) {
$this->add_sql_clause( 'join', $join );
}
}
}
/**
* Maps ordering specified by the user to columns in the database/fields in the data.
*
* @param string $order_by Sorting criterion.
* @return string
*/
protected function normalize_order_by( $order_by ) {
if ( 'date' === $order_by ) {
return 'time_interval';
}
if ( 'code' === $order_by ) {
return '_coupons.post_title';
}
return $order_by;
}
/**
* Enriches the coupon data with extra attributes.
*
* @param array $coupon_data Coupon data.
* @param array $query_args Query parameters.
*/
protected function include_extended_info( &$coupon_data, $query_args ) {
foreach ( $coupon_data as $idx => $coupon_datum ) {
$extended_info = new \ArrayObject();
if ( $query_args['extended_info'] ) {
$coupon_id = $coupon_datum['coupon_id'];
$coupon = new \WC_Coupon( $coupon_id );
if ( 0 === $coupon->get_id() ) {
// Deleted or otherwise invalid coupon.
$extended_info = array(
'code' => __( '(Deleted)', 'woocommerce' ),
'date_created' => '',
'date_created_gmt' => '',
'date_expires' => '',
'date_expires_gmt' => '',
'discount_type' => __( 'N/A', 'woocommerce' ),
);
} else {
$gmt_timzone = new \DateTimeZone( 'UTC' );
$date_expires = $coupon->get_date_expires();
if ( is_a( $date_expires, 'DateTime' ) ) {
$date_expires = $date_expires->format( TimeInterval::$iso_datetime_format );
$date_expires_gmt = new \DateTime( $date_expires );
$date_expires_gmt->setTimezone( $gmt_timzone );
$date_expires_gmt = $date_expires_gmt->format( TimeInterval::$iso_datetime_format );
} else {
$date_expires = '';
$date_expires_gmt = '';
}
$date_created = $coupon->get_date_created();
if ( is_a( $date_created, 'DateTime' ) ) {
$date_created = $date_created->format( TimeInterval::$iso_datetime_format );
$date_created_gmt = new \DateTime( $date_created );
$date_created_gmt->setTimezone( $gmt_timzone );
$date_created_gmt = $date_created_gmt->format( TimeInterval::$iso_datetime_format );
} else {
$date_created = '';
$date_created_gmt = '';
}
$extended_info = array(
'code' => $coupon->get_code(),
'date_created' => $date_created,
'date_created_gmt' => $date_created_gmt,
'date_expires' => $date_expires,
'date_expires_gmt' => $date_expires_gmt,
'discount_type' => $coupon->get_discount_type(),
);
}
}
$coupon_data[ $idx ]['extended_info'] = $extended_info;
}
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$table_name = self::get_db_table_name();
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'coupon_id',
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
'fields' => '*',
'coupons' => array(),
'extended_info' => false,
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$data = (object) array(
'data' => array(),
'total' => 0,
'pages' => 0,
'page_no' => 0,
);
$selections = $this->selected_columns( $query_args );
$included_coupons = $this->get_included_coupons_array( $query_args );
$limit_params = $this->get_limit_params( $query_args );
$this->subquery->add_sql_clause( 'select', $selections );
$this->add_sql_query_params( $query_args );
if ( count( $included_coupons ) > 0 ) {
$total_results = count( $included_coupons );
$total_pages = (int) ceil( $total_results / $limit_params['per_page'] );
$fields = $this->get_fields( $query_args );
$ids_table = $this->get_ids_table( $included_coupons, 'coupon_id' );
$this->add_sql_clause( 'select', $this->format_join_selections( $fields, array( 'coupon_id' ) ) );
$this->add_sql_clause( 'from', '(' );
$this->add_sql_clause( 'from', $this->subquery->get_query_statement() );
$this->add_sql_clause( 'from', ") AS {$table_name}" );
$this->add_sql_clause(
'right_join',
"RIGHT JOIN ( {$ids_table} ) AS default_results
ON default_results.coupon_id = {$table_name}.coupon_id"
);
$coupons_query = $this->get_query_statement();
} else {
$this->subquery->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$this->subquery->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
$coupons_query = $this->subquery->get_query_statement();
$this->subquery->clear_sql_clause( array( 'select', 'order_by', 'limit' ) );
$this->subquery->add_sql_clause( 'select', 'coupon_id' );
$coupon_subquery = "SELECT COUNT(*) FROM (
{$this->subquery->get_query_statement()}
) AS tt";
$db_records_count = (int) $wpdb->get_var(
$coupon_subquery // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
);
$total_results = $db_records_count;
$total_pages = (int) ceil( $db_records_count / $limit_params['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return $data;
}
}
$coupon_data = $wpdb->get_results(
$coupons_query, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
ARRAY_A
);
if ( null === $coupon_data ) {
return $data;
}
$this->include_extended_info( $coupon_data, $query_args );
$coupon_data = array_map( array( $this, 'cast_numbers' ), $coupon_data );
$data = (object) array(
'data' => $coupon_data,
'total' => $total_results,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Get coupon ID for an order.
*
* Tries to get the ID from order item meta, then falls back to a query of published coupons.
*
* @param \WC_Order_Item_Coupon $coupon_item The coupon order item object.
* @return int Coupon ID on success, 0 on failure.
*/
public static function get_coupon_id( \WC_Order_Item_Coupon $coupon_item ) {
// First attempt to get coupon ID from order item data.
$coupon_data = $coupon_item->get_meta( 'coupon_data', true );
// Normal checkout orders should have this data.
// See: https://github.com/woocommerce/woocommerce/blob/3dc7df7af9f7ca0c0aa34ede74493e856f276abe/includes/abstracts/abstract-wc-order.php#L1206.
if ( isset( $coupon_data['id'] ) ) {
return $coupon_data['id'];
}
// Try to get the coupon ID using the code.
return wc_get_coupon_id_by_code( $coupon_item->get_code() );
}
/**
* Create or update an an entry in the wc_order_coupon_lookup table for an order.
*
* @since 3.5.0
* @param int $order_id Order ID.
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
*/
public static function sync_order_coupons( $order_id ) {
global $wpdb;
$order = wc_get_order( $order_id );
if ( ! $order ) {
return -1;
}
// Refunds don't affect coupon stats so return successfully if one is called here.
if ( 'shop_order_refund' === $order->get_type() ) {
return true;
}
$table_name = self::get_db_table_name();
$existing_items = $wpdb->get_col(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT coupon_id FROM {$table_name} WHERE order_id = %d",
$order_id
)
);
$existing_items = array_flip( $existing_items );
$coupon_items = $order->get_items( 'coupon' );
$coupon_items_count = count( $coupon_items );
$num_updated = 0;
$num_deleted = 0;
foreach ( $coupon_items as $coupon_item ) {
$coupon_id = self::get_coupon_id( $coupon_item );
unset( $existing_items[ $coupon_id ] );
if ( ! $coupon_id ) {
// Insert a unique, but obviously invalid ID for this deleted coupon.
$num_deleted++;
$coupon_id = -1 * $num_deleted;
}
$result = $wpdb->replace(
self::get_db_table_name(),
array(
'order_id' => $order_id,
'coupon_id' => $coupon_id,
'discount_amount' => $coupon_item->get_discount(),
'date_created' => $order->get_date_created( 'edit' )->date( TimeInterval::$sql_datetime_format ),
),
array(
'%d',
'%d',
'%f',
'%s',
)
);
/**
* Fires when coupon's reports are updated.
*
* @param int $coupon_id Coupon ID.
* @param int $order_id Order ID.
*/
do_action( 'woocommerce_analytics_update_coupon', $coupon_id, $order_id );
// Sum the rows affected. Using REPLACE can affect 2 rows if the row already exists.
$num_updated += 2 === intval( $result ) ? 1 : intval( $result );
}
if ( ! empty( $existing_items ) ) {
$existing_items = array_flip( $existing_items );
$format = array_fill( 0, count( $existing_items ), '%d' );
$format = implode( ',', $format );
array_unshift( $existing_items, $order_id );
$wpdb->query(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"DELETE FROM {$table_name} WHERE order_id = %d AND coupon_id in ({$format})",
$existing_items
)
);
}
return ( $coupon_items_count === $num_updated );
}
/**
* Clean coupons data when an order is deleted.
*
* @param int $order_id Order ID.
*/
public static function sync_on_order_delete( $order_id ) {
global $wpdb;
$wpdb->delete( self::get_db_table_name(), array( 'order_id' => $order_id ) );
/**
* Fires when coupon's reports are removed from database.
*
* @param int $coupon_id Coupon ID.
* @param int $order_id Order ID.
*/
do_action( 'woocommerce_analytics_delete_coupon', 0, $order_id );
ReportsCache::invalidate();
}
/**
* Gets coupons based on the provided arguments.
*
* @todo Upon core merge, including this in core's `class-wc-coupon-data-store-cpt.php` might make more sense.
* @param array $args Array of args to filter the query by. Supports `include`.
* @return array Array of results.
*/
public function get_coupons( $args ) {
global $wpdb;
$query = "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type='shop_coupon'";
$included_coupons = $this->get_included_coupons( $args, 'include' );
if ( ! empty( $included_coupons ) ) {
$query .= " AND ID IN ({$included_coupons})";
}
return $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
$this->subquery = new SqlQuery( $this->context . '_subquery' );
$this->subquery->add_sql_clause( 'from', self::get_db_table_name() );
$this->subquery->add_sql_clause( 'group_by', 'coupon_id' );
}
}
API/Reports/Coupons/Query.php 0000644 00000002245 15153746747 0012105 0 ustar 00 <?php
/**
* Class for parameter-based Coupons Report querying
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'page' => 2,
* 'coupons' => array(5, 120),
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Coupons\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Coupons\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Products report.
*
* @return array
*/
protected function get_default_query_vars() {
return array();
}
/**
* Get product data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_coupons_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-coupons' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_coupons_select_query', $results, $args );
}
}
API/Reports/Coupons/Stats/Controller.php 0000644 00000013425 15153746747 0014223 0 ustar 00 <?php
/**
* REST API Reports coupons stats controller
*
* Handles requests to the /reports/coupons/stats endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericStatsController;
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
use WP_REST_Request;
use WP_REST_Response;
/**
* REST API Reports coupons stats controller class.
*
* @internal
* @extends GenericStatsController
*/
class Controller extends GenericStatsController {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/coupons/stats';
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['before'] = $request['before'];
$args['after'] = $request['after'];
$args['interval'] = $request['interval'];
$args['page'] = $request['page'];
$args['per_page'] = $request['per_page'];
$args['orderby'] = $request['orderby'];
$args['order'] = $request['order'];
$args['coupons'] = (array) $request['coupons'];
$args['segmentby'] = $request['segmentby'];
$args['fields'] = $request['fields'];
$args['force_cache_refresh'] = $request['force_cache_refresh'];
return $args;
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$coupons_query = new Query( $query_args );
try {
$report_data = $coupons_query->get_data();
} catch ( ParameterException $e ) {
return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
$out_data = array(
'totals' => get_object_vars( $report_data->totals ),
'intervals' => array(),
);
foreach ( $report_data->intervals as $interval_data ) {
$item = $this->prepare_item_for_response( (object) $interval_data, $request );
$out_data['intervals'][] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$out_data,
(int) $report_data->total,
(int) $report_data->page_no,
(int) $report_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param stdClass $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$data = get_object_vars( $report );
$response = parent::prepare_item_for_response( $data, $request );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_coupons_stats', $response, $report, $request );
}
/**
* Get the Report's item properties schema.
* Will be used by `get_item_schema` as `totals` and `subtotals`.
*
* @return array
*/
protected function get_item_properties_schema() {
return array(
'amount' => array(
'description' => __( 'Net discount amount.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
'format' => 'currency',
),
'coupons_count' => array(
'description' => __( 'Number of coupons.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'orders_count' => array(
'title' => __( 'Discounted orders', 'woocommerce' ),
'description' => __( 'Number of discounted orders.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
),
);
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = parent::get_item_schema();
$schema['title'] = 'report_coupons_stats';
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['orderby']['enum'] = array(
'date',
'amount',
'coupons_count',
'orders_count',
);
$params['coupons'] = array(
'description' => __( 'Limit result set to coupons assigned specific coupon IDs.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['segmentby'] = array(
'description' => __( 'Segment the response by additional constraint.', 'woocommerce' ),
'type' => 'string',
'enum' => array(
'product',
'variation',
'category',
'coupon',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['fields'] = array(
'description' => __( 'Limit stats fields to the specified items.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
return $params;
}
}
API/Reports/Coupons/Stats/DataStore.php 0000644 00000021305 15153746747 0013762 0 ustar 00 <?php
/**
* API\Reports\Coupons\Stats\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore as CouponsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
/**
* API\Reports\Coupons\Stats\DataStore.
*/
class DataStore extends CouponsDataStore implements DataStoreInterface {
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'date_start' => 'strval',
'date_end' => 'strval',
'date_start_gmt' => 'strval',
'date_end_gmt' => 'strval',
'amount' => 'floatval',
'coupons_count' => 'intval',
'orders_count' => 'intval',
);
/**
* SQL columns to select in the db query.
*
* @var array
*/
protected $report_columns;
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'coupons_stats';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'coupons_stats';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$table_name = self::get_db_table_name();
$this->report_columns = array(
'amount' => 'SUM(discount_amount) as amount',
'coupons_count' => 'COUNT(DISTINCT coupon_id) as coupons_count',
'orders_count' => "COUNT(DISTINCT {$table_name}.order_id) as orders_count",
);
}
/**
* Updates the database query with parameters used for Products Stats report: categories and order status.
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function update_sql_query_params( $query_args ) {
global $wpdb;
$clauses = array(
'where' => '',
'join' => '',
);
$order_coupon_lookup_table = self::get_db_table_name();
$included_coupons = $this->get_included_coupons( $query_args, 'coupons' );
if ( $included_coupons ) {
$clauses['where'] .= " AND {$order_coupon_lookup_table}.coupon_id IN ({$included_coupons})";
}
$order_status_filter = $this->get_status_subquery( $query_args );
if ( $order_status_filter ) {
$clauses['join'] .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_coupon_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id";
$clauses['where'] .= " AND ( {$order_status_filter} )";
}
$this->add_time_period_sql_params( $query_args, $order_coupon_lookup_table );
$this->add_intervals_sql_params( $query_args, $order_coupon_lookup_table );
$clauses['where_time'] = $this->get_sql_clause( 'where_time' );
$this->interval_query->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
$this->interval_query->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$this->interval_query->add_sql_clause( 'select', $this->get_sql_clause( 'select' ) );
$this->interval_query->add_sql_clause( 'select', 'AS time_interval' );
foreach ( array( 'join', 'where_time', 'where' ) as $clause ) {
$this->interval_query->add_sql_clause( $clause, $clauses[ $clause ] );
$this->total_query->add_sql_clause( $clause, $clauses[ $clause ] );
}
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @since 3.5.0
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$table_name = self::get_db_table_name();
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
'fields' => '*',
'interval' => 'week',
'coupons' => array(),
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$data = (object) array(
'data' => array(),
'total' => 0,
'pages' => 0,
'page_no' => 0,
);
$selections = $this->selected_columns( $query_args );
$totals_query = array();
$intervals_query = array();
$limit_params = $this->get_limit_sql_params( $query_args );
$this->update_sql_query_params( $query_args, $totals_query, $intervals_query );
$db_intervals = $wpdb->get_col(
$this->interval_query->get_query_statement()
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
$db_interval_count = count( $db_intervals );
$expected_interval_count = TimeInterval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $limit_params['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return $data;
}
$this->total_query->add_sql_clause( 'select', $selections );
$totals = $wpdb->get_results(
$this->total_query->get_query_statement(),
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $totals ) {
return $data;
}
// @todo remove these assignements when refactoring segmenter classes to use query objects.
$totals_query = array(
'from_clause' => $this->total_query->get_sql_clause( 'join' ),
'where_time_clause' => $this->total_query->get_sql_clause( 'where_time' ),
'where_clause' => $this->total_query->get_sql_clause( 'where' ),
);
$intervals_query = array(
'select_clause' => $this->get_sql_clause( 'select' ),
'from_clause' => $this->interval_query->get_sql_clause( 'join' ),
'where_time_clause' => $this->interval_query->get_sql_clause( 'where_time' ),
'where_clause' => $this->interval_query->get_sql_clause( 'where' ),
'limit' => $this->get_sql_clause( 'limit' ),
);
$segmenter = new Segmenter( $query_args, $this->report_columns );
$totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name );
$totals = (object) $this->cast_numbers( $totals[0] );
// Intervals.
$this->update_intervals_sql_params( $query_args, $db_interval_count, $expected_interval_count, $table_name );
$this->interval_query->add_sql_clause( 'select', ", MAX({$table_name}.date_created) AS datetime_anchor" );
if ( '' !== $selections ) {
$this->interval_query->add_sql_clause( 'select', ', ' . $selections );
}
$intervals = $wpdb->get_results(
$this->interval_query->get_query_statement(),
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $intervals ) {
return $data;
}
$data = (object) array(
'totals' => $totals,
'intervals' => $intervals,
'total' => $expected_interval_count,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
if ( TimeInterval::intervals_missing( $expected_interval_count, $db_interval_count, $limit_params['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
$this->remove_extra_records( $data, $query_args['page'], $limit_params['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] );
} else {
$this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals );
}
$segmenter->add_intervals_segments( $data, $intervals_query, $table_name );
$this->create_interval_subtotals( $data->intervals );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
unset( $this->subquery );
$this->total_query = new SqlQuery( $this->context . '_total' );
$this->total_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query = new SqlQuery( $this->context . '_interval' );
$this->interval_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query->add_sql_clause( 'group_by', 'time_interval' );
}
}
API/Reports/Coupons/Stats/Query.php 0000644 00000002304 15153746747 0013177 0 ustar 00 <?php
/**
* Class for parameter-based Products Report querying
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'page' => 2,
* 'coupons' => array(5, 120),
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Coupons\Stats\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Products report.
*
* @return array
*/
protected function get_default_query_vars() {
return array();
}
/**
* Get product data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_coupons_stats_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-coupons-stats' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_coupons_select_query', $results, $args );
}
}
API/Reports/Coupons/Stats/Segmenter.php 0000644 00000036141 15153746747 0014031 0 ustar 00 <?php
/**
* Class for adding segmenting support to coupons/stats without cluttering the data store.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter;
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
/**
* Date & time interval and numeric range handling class for Reporting API.
*/
class Segmenter extends ReportsSegmenter {
/**
* Returns column => query mapping to be used for product-related product-level segmenting query
* (e.g. coupon discount amount for product X when segmenting by product id or category).
*
* @param string $products_table Name of SQL table containing the product-level segmenting info.
*
* @return array Column => SELECT query mapping.
*/
protected function get_segment_selections_product_level( $products_table ) {
$columns_mapping = array(
'amount' => "SUM($products_table.coupon_amount) as amount",
);
return $columns_mapping;
}
/**
* Returns column => query mapping to be used for order-related product-level segmenting query
* (e.g. orders_count when segmented by category).
*
* @param string $coupons_lookup_table Name of SQL table containing the order-level segmenting info.
*
* @return array Column => SELECT query mapping.
*/
protected function get_segment_selections_order_level( $coupons_lookup_table ) {
$columns_mapping = array(
'coupons_count' => "COUNT(DISTINCT $coupons_lookup_table.coupon_id) as coupons_count",
'orders_count' => "COUNT(DISTINCT $coupons_lookup_table.order_id) as orders_count",
);
return $columns_mapping;
}
/**
* Returns column => query mapping to be used for order-level segmenting query
* (e.g. discount amount when segmented by coupons).
*
* @param string $coupons_lookup_table Name of SQL table containing the order-level info.
* @param array $overrides Array of overrides for default column calculations.
*
* @return array Column => SELECT query mapping.
*/
protected function segment_selections_orders( $coupons_lookup_table, $overrides = array() ) {
$columns_mapping = array(
'amount' => "SUM($coupons_lookup_table.discount_amount) as amount",
'coupons_count' => "COUNT(DISTINCT $coupons_lookup_table.coupon_id) as coupons_count",
'orders_count' => "COUNT(DISTINCT $coupons_lookup_table.order_id) as orders_count",
);
if ( $overrides ) {
$columns_mapping = array_merge( $columns_mapping, $overrides );
}
return $columns_mapping;
}
/**
* Calculate segments for totals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
*
* @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $segmenting_dimension_name Name of the segmenting dimension.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $totals_query Array of SQL clauses for totals query.
* @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
*
* @return array
*/
protected function get_product_related_totals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $totals_query, $unique_orders_table ) {
global $wpdb;
// Product-level numbers and order-level numbers can be fetched by the same query.
$segments_products = $wpdb->get_results(
"SELECT
$segmenting_groupby AS $segmenting_dimension_name
{$segmenting_selections['product_level']}
{$segmenting_selections['order_level']}
FROM
$table_name
$segmenting_from
{$totals_query['from_clause']}
WHERE
1=1
{$totals_query['where_time_clause']}
{$totals_query['where_clause']}
$segmenting_where
GROUP BY
$segmenting_groupby",
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
$totals_segments = $this->merge_segment_totals_results( $segmenting_dimension_name, $segments_products, array() );
return $totals_segments;
}
/**
* Calculate segments for intervals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
*
* @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $segmenting_dimension_name Name of the segmenting dimension.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $intervals_query Array of SQL clauses for intervals query.
* @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
*
* @return array
*/
protected function get_product_related_intervals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $intervals_query, $unique_orders_table ) {
global $wpdb;
// LIMIT offset, rowcount needs to be updated to LIMIT offset, rowcount * max number of segments.
$limit_parts = explode( ',', $intervals_query['limit'] );
$orig_rowcount = intval( $limit_parts[1] );
$segmenting_limit = $limit_parts[0] . ',' . $orig_rowcount * count( $this->get_all_segments() );
// Product-level numbers and order-level numbers can be fetched by the same query.
$segments_products = $wpdb->get_results(
"SELECT
{$intervals_query['select_clause']} AS time_interval,
$segmenting_groupby AS $segmenting_dimension_name
{$segmenting_selections['product_level']}
{$segmenting_selections['order_level']}
FROM
$table_name
$segmenting_from
{$intervals_query['from_clause']}
WHERE
1=1
{$intervals_query['where_time_clause']}
{$intervals_query['where_clause']}
$segmenting_where
GROUP BY
time_interval, $segmenting_groupby
$segmenting_limit",
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
$intervals_segments = $this->merge_segment_intervals_results( $segmenting_dimension_name, $segments_products, array() );
return $intervals_segments;
}
/**
* Calculate segments for totals query where the segmenting property is bound to order (e.g. coupon or customer type).
*
* @param string $segmenting_select SELECT part of segmenting SQL query.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $totals_query Array of SQL clauses for intervals query.
*
* @return array
*/
protected function get_order_related_totals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $totals_query ) {
global $wpdb;
$totals_segments = $wpdb->get_results(
"SELECT
$segmenting_groupby
$segmenting_select
FROM
$table_name
$segmenting_from
{$totals_query['from_clause']}
WHERE
1=1
{$totals_query['where_time_clause']}
{$totals_query['where_clause']}
$segmenting_where
GROUP BY
$segmenting_groupby",
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
// Reformat result.
$totals_segments = $this->reformat_totals_segments( $totals_segments, $segmenting_groupby );
return $totals_segments;
}
/**
* Calculate segments for intervals query where the segmenting property is bound to order (e.g. coupon or customer type).
*
* @param string $segmenting_select SELECT part of segmenting SQL query.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $intervals_query Array of SQL clauses for intervals query.
*
* @return array
*/
protected function get_order_related_intervals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $intervals_query ) {
global $wpdb;
$limit_parts = explode( ',', $intervals_query['limit'] );
$orig_rowcount = intval( $limit_parts[1] );
$segmenting_limit = $limit_parts[0] . ',' . $orig_rowcount * count( $this->get_all_segments() );
$intervals_segments = $wpdb->get_results(
"SELECT
MAX($table_name.date_created) AS datetime_anchor,
{$intervals_query['select_clause']} AS time_interval,
$segmenting_groupby
$segmenting_select
FROM
$table_name
$segmenting_from
{$intervals_query['from_clause']}
WHERE
1=1
{$intervals_query['where_time_clause']}
{$intervals_query['where_clause']}
$segmenting_where
GROUP BY
time_interval, $segmenting_groupby
$segmenting_limit",
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
// Reformat result.
$intervals_segments = $this->reformat_intervals_segments( $intervals_segments, $segmenting_groupby );
return $intervals_segments;
}
/**
* Return array of segments formatted for REST response.
*
* @param string $type Type of segments to return--'totals' or 'intervals'.
* @param array $query_params SQL query parameter array.
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
*
* @return array
* @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified.
*/
protected function get_segments( $type, $query_params, $table_name ) {
global $wpdb;
if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) {
return array();
}
$product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
$unique_orders_table = '';
$segmenting_where = '';
// Product, variation, and category are bound to product, so here product segmenting table is required,
// while coupon and customer are bound to order, so we don't need the extra JOIN for those.
// This also means that segment selections need to be calculated differently.
if ( 'product' === $this->query_args['segmentby'] ) {
$product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
$order_level_columns = $this->get_segment_selections_order_level( $table_name );
$segmenting_selections = array(
'product_level' => $this->prepare_selections( $product_level_columns ),
'order_level' => $this->prepare_selections( $order_level_columns ),
);
$this->report_columns = array_merge( $product_level_columns, $order_level_columns );
$segmenting_from = "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)";
$segmenting_groupby = $product_segmenting_table . '.product_id';
$segmenting_dimension_name = 'product_id';
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'variation' === $this->query_args['segmentby'] ) {
if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) {
throw new ParameterException( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'woocommerce' ) );
}
$product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
$order_level_columns = $this->get_segment_selections_order_level( $table_name );
$segmenting_selections = array(
'product_level' => $this->prepare_selections( $product_level_columns ),
'order_level' => $this->prepare_selections( $order_level_columns ),
);
$this->report_columns = array_merge( $product_level_columns, $order_level_columns );
$segmenting_from = "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)";
$segmenting_where = "AND $product_segmenting_table.product_id = {$this->query_args['product_includes'][0]}";
$segmenting_groupby = $product_segmenting_table . '.variation_id';
$segmenting_dimension_name = 'variation_id';
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'category' === $this->query_args['segmentby'] ) {
$product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
$order_level_columns = $this->get_segment_selections_order_level( $table_name );
$segmenting_selections = array(
'product_level' => $this->prepare_selections( $product_level_columns ),
'order_level' => $this->prepare_selections( $order_level_columns ),
);
$this->report_columns = array_merge( $product_level_columns, $order_level_columns );
$segmenting_from = "
INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)
LEFT JOIN {$wpdb->term_relationships} ON {$product_segmenting_table}.product_id = {$wpdb->term_relationships}.object_id
JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
LEFT JOIN {$wpdb->wc_category_lookup} ON {$wpdb->term_taxonomy}.term_id = {$wpdb->wc_category_lookup}.category_id
";
$segmenting_where = " AND {$wpdb->wc_category_lookup}.category_tree_id IS NOT NULL";
$segmenting_groupby = "{$wpdb->wc_category_lookup}.category_tree_id";
$segmenting_dimension_name = 'category_id';
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'coupon' === $this->query_args['segmentby'] ) {
$coupon_level_columns = $this->segment_selections_orders( $table_name );
$segmenting_selections = $this->prepare_selections( $coupon_level_columns );
$this->report_columns = $coupon_level_columns;
$segmenting_from = '';
$segmenting_groupby = "$table_name.coupon_id";
$segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params );
}
return $segments;
}
}
API/Reports/Customers/Controller.php 0000644 00000056564 15153746747 0013476 0 ustar 00 <?php
/**
* REST API Reports customers controller
*
* Handles requests to the /reports/customers endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Customers;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericController;
use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits;
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/**
* REST API Reports customers controller class.
*
* @internal
* @extends GenericController
*/
class Controller extends GenericController implements ExportableInterface {
/**
* Exportable traits.
*/
use ExportableTraits;
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/customers';
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['registered_before'] = $request['registered_before'];
$args['registered_after'] = $request['registered_after'];
$args['order_before'] = $request['before'];
$args['order_after'] = $request['after'];
$args['page'] = $request['page'];
$args['per_page'] = $request['per_page'];
$args['order'] = $request['order'];
$args['orderby'] = $request['orderby'];
$args['match'] = $request['match'];
$args['search'] = $request['search'];
$args['searchby'] = $request['searchby'];
$args['name_includes'] = $request['name_includes'];
$args['name_excludes'] = $request['name_excludes'];
$args['username_includes'] = $request['username_includes'];
$args['username_excludes'] = $request['username_excludes'];
$args['email_includes'] = $request['email_includes'];
$args['email_excludes'] = $request['email_excludes'];
$args['country_includes'] = $request['country_includes'];
$args['country_excludes'] = $request['country_excludes'];
$args['last_active_before'] = $request['last_active_before'];
$args['last_active_after'] = $request['last_active_after'];
$args['orders_count_min'] = $request['orders_count_min'];
$args['orders_count_max'] = $request['orders_count_max'];
$args['total_spend_min'] = $request['total_spend_min'];
$args['total_spend_max'] = $request['total_spend_max'];
$args['avg_order_value_min'] = $request['avg_order_value_min'];
$args['avg_order_value_max'] = $request['avg_order_value_max'];
$args['last_order_before'] = $request['last_order_before'];
$args['last_order_after'] = $request['last_order_after'];
$args['customers'] = $request['customers'];
$args['users'] = $request['users'];
$args['force_cache_refresh'] = $request['force_cache_refresh'];
$args['filter_empty'] = $request['filter_empty'];
$between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' );
$normalized_params_numeric = TimeInterval::normalize_between_params( $request, $between_params_numeric, false );
$between_params_date = array( 'last_active', 'registered' );
$normalized_params_date = TimeInterval::normalize_between_params( $request, $between_params_date, true );
$args = array_merge( $args, $normalized_params_numeric, $normalized_params_date );
return $args;
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$customers_query = new Query( $query_args );
$report_data = $customers_query->get_data();
$data = array();
foreach ( $report_data->data as $customer_data ) {
$item = $this->prepare_item_for_response( $customer_data, $request );
$data[] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$data,
(int) $report_data->total,
(int) $report_data->page_no,
(int) $report_data->pages
);
}
/**
* Get one report.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_item( $request ) {
$query_args = $this->prepare_reports_query( $request );
$query_args['customers'] = array( $request->get_param( 'id' ) );
$customers_query = new Query( $query_args );
$report_data = $customers_query->get_data();
$data = array();
foreach ( $report_data->data as $customer_data ) {
$item = $this->prepare_item_for_response( $customer_data, $request );
$data[] = $this->prepare_response_for_collection( $item );
}
$response = rest_ensure_response( $data );
$response->header( 'X-WP-Total', (int) $report_data->total );
$response->header( 'X-WP-TotalPages', (int) $report_data->pages );
return $response;
}
/**
* Prepare a report object for serialization.
*
* @param array $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $report, $request );
// Registered date is UTC.
$data['date_registered_gmt'] = wc_rest_prepare_date_response( $data['date_registered'] );
$data['date_registered'] = wc_rest_prepare_date_response( $data['date_registered'], false );
// Last active date is local time.
$data['date_last_active_gmt'] = wc_rest_prepare_date_response( $data['date_last_active'], false );
$data['date_last_active'] = wc_rest_prepare_date_response( $data['date_last_active'] );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $report ) );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
* @since 4.0.0
*/
return apply_filters( 'woocommerce_rest_prepare_report_customers', $response, $report, $request );
}
/**
* Prepare links for the request.
*
* @param array $object Object data.
* @return array
*/
protected function prepare_links( $object ) {
if ( empty( $object['user_id'] ) ) {
return array();
}
return array(
'customer' => array(
'href' => rest_url( sprintf( '/%s/customers/%d', $this->namespace, $object['id'] ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '/%s/customers', $this->namespace ) ),
),
);
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_customers',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Customer ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'user_id' => array(
'description' => __( 'User ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'Name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'username' => array(
'description' => __( 'Username.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'country' => array(
'description' => __( 'Country / Region.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'city' => array(
'description' => __( 'City.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'state' => array(
'description' => __( 'Region.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'postcode' => array(
'description' => __( 'Postal code.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_registered' => array(
'description' => __( 'Date registered.', 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_registered_gmt' => array(
'description' => __( 'Date registered GMT.', 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_last_active' => array(
'description' => __( 'Date last active.', 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_last_active_gmt' => array(
'description' => __( 'Date last active GMT.', 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'orders_count' => array(
'description' => __( 'Order count.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'total_spend' => array(
'description' => __( 'Total spend.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'avg_order_value' => array(
'description' => __( 'Avg order value.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['registered_before'] = array(
'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['registered_after'] = array(
'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['orderby']['default'] = 'date_registered';
$params['orderby']['enum'] = array(
'username',
'name',
'country',
'city',
'state',
'postcode',
'date_registered',
'date_last_active',
'orders_count',
'total_spend',
'avg_order_value',
);
$params['match'] = array(
'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'woocommerce' ),
'type' => 'string',
'default' => 'all',
'enum' => array(
'all',
'any',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['search'] = array(
'description' => __( 'Limit response to objects with a customer field containing the search term. Searches the field provided by `searchby`.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['searchby'] = array(
'description' => 'Limit results with `search` and `searchby` to specific fields containing the search term.',
'type' => 'string',
'default' => 'name',
'enum' => array(
'name',
'username',
'email',
'all',
),
);
$params['name_includes'] = array(
'description' => __( 'Limit response to objects with specific names.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['name_excludes'] = array(
'description' => __( 'Limit response to objects excluding specific names.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['username_includes'] = array(
'description' => __( 'Limit response to objects with specific usernames.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['username_excludes'] = array(
'description' => __( 'Limit response to objects excluding specific usernames.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['email_includes'] = array(
'description' => __( 'Limit response to objects including emails.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['email_excludes'] = array(
'description' => __( 'Limit response to objects excluding emails.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['country_includes'] = array(
'description' => __( 'Limit response to objects with specific countries.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['country_excludes'] = array(
'description' => __( 'Limit response to objects excluding specific countries.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['last_active_before'] = array(
'description' => __( 'Limit response to objects last active before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['last_active_after'] = array(
'description' => __( 'Limit response to objects last active after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['last_active_between'] = array(
'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ),
'items' => array(
'type' => 'string',
),
);
$params['registered_before'] = array(
'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['registered_after'] = array(
'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['registered_between'] = array(
'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ),
'items' => array(
'type' => 'string',
),
);
$params['orders_count_min'] = array(
'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'woocommerce' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['orders_count_max'] = array(
'description' => __( 'Limit response to objects with an order count less than or equal to given integer.', 'woocommerce' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['orders_count_between'] = array(
'description' => __( 'Limit response to objects with an order count between two given integers.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
'items' => array(
'type' => 'integer',
),
);
$params['total_spend_min'] = array(
'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'woocommerce' ),
'type' => 'number',
'validate_callback' => 'rest_validate_request_arg',
);
$params['total_spend_max'] = array(
'description' => __( 'Limit response to objects with a total order spend less than or equal to given number.', 'woocommerce' ),
'type' => 'number',
'validate_callback' => 'rest_validate_request_arg',
);
$params['total_spend_between'] = array(
'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
'items' => array(
'type' => 'integer',
),
);
$params['avg_order_value_min'] = array(
'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'woocommerce' ),
'type' => 'number',
'validate_callback' => 'rest_validate_request_arg',
);
$params['avg_order_value_max'] = array(
'description' => __( 'Limit response to objects with an average order spend less than or equal to given number.', 'woocommerce' ),
'type' => 'number',
'validate_callback' => 'rest_validate_request_arg',
);
$params['avg_order_value_between'] = array(
'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
'items' => array(
'type' => 'integer',
),
);
$params['last_order_before'] = array(
'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['last_order_after'] = array(
'description' => __( 'Limit response to objects with last order after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['customers'] = array(
'description' => __( 'Limit result to items with specified customer ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['users'] = array(
'description' => __( 'Limit result to items with specified user ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['filter_empty'] = array(
'description' => __( 'Filter out results where any of the passed fields are empty', 'woocommerce' ),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
'enum' => array(
'email',
'name',
'country',
'city',
'state',
'postcode',
),
),
);
return $params;
}
/**
* Get the column names for export.
*
* @return array Key value pair of Column ID => Label.
*/
public function get_export_columns() {
$export_columns = array(
'name' => __( 'Name', 'woocommerce' ),
'username' => __( 'Username', 'woocommerce' ),
'last_active' => __( 'Last Active', 'woocommerce' ),
'registered' => __( 'Sign Up', 'woocommerce' ),
'email' => __( 'Email', 'woocommerce' ),
'orders_count' => __( 'Orders', 'woocommerce' ),
'total_spend' => __( 'Total Spend', 'woocommerce' ),
'avg_order_value' => __( 'AOV', 'woocommerce' ),
'country' => __( 'Country / Region', 'woocommerce' ),
'city' => __( 'City', 'woocommerce' ),
'region' => __( 'Region', 'woocommerce' ),
'postcode' => __( 'Postal Code', 'woocommerce' ),
);
/**
* Filter to add or remove column names from the customers report for
* export.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_report_customers_export_columns',
$export_columns
);
}
/**
* Get the column values for export.
*
* @param array $item Single report item/row.
* @return array Key value pair of Column ID => Row Value.
*/
public function prepare_item_for_export( $item ) {
$export_item = array(
'name' => $item['name'],
'username' => $item['username'],
'last_active' => $item['date_last_active'],
'registered' => $item['date_registered'],
'email' => $item['email'],
'orders_count' => $item['orders_count'],
'total_spend' => self::csv_number_format( $item['total_spend'] ),
'avg_order_value' => self::csv_number_format( $item['avg_order_value'] ),
'country' => $item['country'],
'city' => $item['city'],
'region' => $item['state'],
'postcode' => $item['postcode'],
);
/**
* Filter the column values of an item being exported.
*
* @param object $export_item Key value pair of Column ID => Row Value.
* @param object $item Single report item/row.
* @since 4.0.0
*/
return apply_filters(
'woocommerce_report_customers_prepare_export_item',
$export_item,
$item
);
}
}
API/Reports/Customers/DataStore.php 0000644 00000072124 15153746747 0013227 0 ustar 00 <?php
/**
* Admin\API\Reports\Customers\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Customers;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
use Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
use Automattic\WooCommerce\Utilities\OrderUtil;
/**
* Admin\API\Reports\Customers\DataStore.
*/
class DataStore extends ReportsDataStore implements DataStoreInterface {
/**
* Table used to get the data.
*
* @var string
*/
protected static $table_name = 'wc_customer_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'customers';
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'id' => 'intval',
'user_id' => 'intval',
'orders_count' => 'intval',
'total_spend' => 'floatval',
'avg_order_value' => 'floatval',
);
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'customers';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
global $wpdb;
$table_name = self::get_db_table_name();
$orders_count = 'SUM( CASE WHEN parent_id = 0 THEN 1 ELSE 0 END )';
$total_spend = 'SUM( total_sales )';
$this->report_columns = array(
'id' => "{$table_name}.customer_id as id",
'user_id' => 'user_id',
'username' => 'username',
'name' => "CONCAT_WS( ' ', first_name, last_name ) as name", // @xxx: What does this mean for RTL?
'email' => 'email',
'country' => 'country',
'city' => 'city',
'state' => 'state',
'postcode' => 'postcode',
'date_registered' => 'date_registered',
'date_last_active' => 'IF( date_last_active <= "0000-00-00 00:00:00", NULL, date_last_active ) AS date_last_active',
'date_last_order' => "MAX( {$wpdb->prefix}wc_order_stats.date_created ) as date_last_order",
'orders_count' => "{$orders_count} as orders_count",
'total_spend' => "{$total_spend} as total_spend",
'avg_order_value' => "CASE WHEN {$orders_count} = 0 THEN NULL ELSE {$total_spend} / {$orders_count} END AS avg_order_value",
);
}
/**
* Set up all the hooks for maintaining and populating table data.
*/
public static function init() {
add_action( 'woocommerce_new_customer', array( __CLASS__, 'update_registered_customer' ) );
add_action( 'woocommerce_update_customer', array( __CLASS__, 'update_registered_customer' ) );
add_action( 'profile_update', array( __CLASS__, 'update_registered_customer' ) );
add_action( 'added_user_meta', array( __CLASS__, 'update_registered_customer_via_last_active' ), 10, 3 );
add_action( 'updated_user_meta', array( __CLASS__, 'update_registered_customer_via_last_active' ), 10, 3 );
add_action( 'delete_user', array( __CLASS__, 'delete_customer_by_user_id' ) );
add_action( 'remove_user_from_blog', array( __CLASS__, 'delete_customer_by_user_id' ) );
add_action( 'woocommerce_privacy_remove_order_personal_data', array( __CLASS__, 'anonymize_customer' ) );
add_action( 'woocommerce_analytics_delete_order_stats', array( __CLASS__, 'sync_on_order_delete' ), 15, 2 );
}
/**
* Sync customers data after an order was deleted.
*
* When an order is deleted, the customer record is deleted from the
* table if the customer has no other orders.
*
* @param int $order_id Order ID.
* @param int $customer_id Customer ID.
*/
public static function sync_on_order_delete( $order_id, $customer_id ) {
$customer_id = absint( $customer_id );
if ( 0 === $customer_id ) {
return;
}
// Calculate the amount of orders remaining for this customer.
$order_count = self::get_order_count( $customer_id );
if ( 0 === $order_count ) {
self::delete_customer( $customer_id );
}
}
/**
* Sync customers data after an order was updated.
*
* Only updates customer if it is the customers last order.
*
* @param int $post_id of order.
* @return true|-1
*/
public static function sync_order_customer( $post_id ) {
global $wpdb;
if ( ! OrderUtil::is_order( $post_id, array( 'shop_order', 'shop_order_refund' ) ) ) {
return -1;
}
$order = wc_get_order( $post_id );
$customer_id = self::get_existing_customer_id_from_order( $order );
if ( false === $customer_id ) {
return -1;
}
$last_order = self::get_last_order( $customer_id );
if ( ! $last_order || $order->get_id() !== $last_order->get_id() ) {
return -1;
}
list($data, $format) = self::get_customer_order_data_and_format( $order );
$result = $wpdb->update( self::get_db_table_name(), $data, array( 'customer_id' => $customer_id ), $format );
/**
* Fires when a customer is updated.
*
* @param int $customer_id Customer ID.
* @since 4.0.0
*/
do_action( 'woocommerce_analytics_update_customer', $customer_id );
return 1 === $result;
}
/**
* Maps ordering specified by the user to columns in the database/fields in the data.
*
* @param string $order_by Sorting criterion.
* @return string
*/
protected function normalize_order_by( $order_by ) {
if ( 'name' === $order_by ) {
return "CONCAT_WS( ' ', first_name, last_name )";
}
return $order_by;
}
/**
* Fills WHERE clause of SQL request with date-related constraints.
*
* @param array $query_args Parameters supplied by the user.
* @param string $table_name Name of the db table relevant for the date constraint.
*/
protected function add_time_period_sql_params( $query_args, $table_name ) {
global $wpdb;
$this->clear_sql_clause( array( 'where', 'where_time', 'having' ) );
$date_param_mapping = array(
'registered' => array(
'clause' => 'where',
'column' => $table_name . '.date_registered',
),
'order' => array(
'clause' => 'where',
'column' => $wpdb->prefix . 'wc_order_stats.date_created',
),
'last_active' => array(
'clause' => 'where',
'column' => $table_name . '.date_last_active',
),
'last_order' => array(
'clause' => 'having',
'column' => "MAX( {$wpdb->prefix}wc_order_stats.date_created )",
),
);
$match_operator = $this->get_match_operator( $query_args );
$where_time_clauses = array();
$having_time_clauses = array();
foreach ( $date_param_mapping as $query_param => $param_info ) {
$subclauses = array();
$before_arg = $query_param . '_before';
$after_arg = $query_param . '_after';
$column_name = $param_info['column'];
if ( ! empty( $query_args[ $before_arg ] ) ) {
$datetime = new \DateTime( $query_args[ $before_arg ] );
$datetime_str = $datetime->format( TimeInterval::$sql_datetime_format );
$subclauses[] = "{$column_name} <= '$datetime_str'";
}
if ( ! empty( $query_args[ $after_arg ] ) ) {
$datetime = new \DateTime( $query_args[ $after_arg ] );
$datetime_str = $datetime->format( TimeInterval::$sql_datetime_format );
$subclauses[] = "{$column_name} >= '$datetime_str'";
}
if ( $subclauses && ( 'where' === $param_info['clause'] ) ) {
$where_time_clauses[] = '(' . implode( ' AND ', $subclauses ) . ')';
}
if ( $subclauses && ( 'having' === $param_info['clause'] ) ) {
$having_time_clauses[] = '(' . implode( ' AND ', $subclauses ) . ')';
}
}
if ( $where_time_clauses ) {
$this->subquery->add_sql_clause( 'where_time', 'AND ' . implode( " {$match_operator} ", $where_time_clauses ) );
}
if ( $having_time_clauses ) {
$this->subquery->add_sql_clause( 'having', 'AND ' . implode( " {$match_operator} ", $having_time_clauses ) );
}
}
/**
* Updates the database query with parameters used for Customers report: categories and order status.
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function add_sql_query_params( $query_args ) {
global $wpdb;
$customer_lookup_table = self::get_db_table_name();
$order_stats_table_name = $wpdb->prefix . 'wc_order_stats';
$this->add_time_period_sql_params( $query_args, $customer_lookup_table );
$this->get_limit_sql_params( $query_args );
$this->add_order_by_sql_params( $query_args );
$this->subquery->add_sql_clause( 'left_join', "LEFT JOIN {$order_stats_table_name} ON {$customer_lookup_table}.customer_id = {$order_stats_table_name}.customer_id" );
$match_operator = $this->get_match_operator( $query_args );
$where_clauses = array();
$having_clauses = array();
$exact_match_params = array(
'name',
'username',
'email',
'country',
);
foreach ( $exact_match_params as $exact_match_param ) {
if ( ! empty( $query_args[ $exact_match_param . '_includes' ] ) ) {
$exact_match_arguments = $query_args[ $exact_match_param . '_includes' ];
$exact_match_arguments_escaped = array_map( 'esc_sql', explode( ',', $exact_match_arguments ) );
$included = implode( "','", $exact_match_arguments_escaped );
// 'country_includes' is a list of country codes, the others will be a list of customer ids.
$table_column = 'country' === $exact_match_param ? $exact_match_param : 'customer_id';
$where_clauses[] = "{$customer_lookup_table}.{$table_column} IN ('{$included}')";
}
if ( ! empty( $query_args[ $exact_match_param . '_excludes' ] ) ) {
$exact_match_arguments = $query_args[ $exact_match_param . '_excludes' ];
$exact_match_arguments_escaped = array_map( 'esc_sql', explode( ',', $exact_match_arguments ) );
$excluded = implode( "','", $exact_match_arguments_escaped );
// 'country_includes' is a list of country codes, the others will be a list of customer ids.
$table_column = 'country' === $exact_match_param ? $exact_match_param : 'customer_id';
$where_clauses[] = "{$customer_lookup_table}.{$table_column} NOT IN ('{$excluded}')";
}
}
$search_params = array(
'name',
'username',
'email',
'all',
);
if ( ! empty( $query_args['search'] ) ) {
$name_like = '%' . $wpdb->esc_like( $query_args['search'] ) . '%';
if ( empty( $query_args['searchby'] ) || 'name' === $query_args['searchby'] || ! in_array( $query_args['searchby'], $search_params, true ) ) {
$searchby = "CONCAT_WS( ' ', first_name, last_name )";
} elseif ( 'all' === $query_args['searchby'] ) {
$searchby = "CONCAT_WS( ' ', first_name, last_name, username, email )";
} else {
$searchby = $query_args['searchby'];
}
$where_clauses[] = $wpdb->prepare( "{$searchby} LIKE %s", $name_like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
$filter_empty_params = array(
'email',
'name',
'country',
'city',
'state',
'postcode',
);
if ( ! empty( $query_args['filter_empty'] ) ) {
$fields_to_filter_by = array_intersect( $query_args['filter_empty'], $filter_empty_params );
if ( in_array( 'name', $fields_to_filter_by, true ) ) {
$fields_to_filter_by = array_diff( $fields_to_filter_by, array( 'name' ) );
$fields_to_filter_by[] = "CONCAT_WS( ' ', first_name, last_name )";
}
$fields_with_not_condition = array_map(
function ( $field ) {
return $field . ' <> \'\'';
},
$fields_to_filter_by
);
$where_clauses[] = '(' . implode( ' AND ', $fields_with_not_condition ) . ')';
}
// Allow a list of customer IDs to be specified.
if ( ! empty( $query_args['customers'] ) ) {
$included_customers = $this->get_filtered_ids( $query_args, 'customers' );
$where_clauses[] = "{$customer_lookup_table}.customer_id IN ({$included_customers})";
}
// Allow a list of user IDs to be specified.
if ( ! empty( $query_args['users'] ) ) {
$included_users = $this->get_filtered_ids( $query_args, 'users' );
$where_clauses[] = "{$customer_lookup_table}.user_id IN ({$included_users})";
}
$numeric_params = array(
'orders_count' => array(
'column' => 'COUNT( order_id )',
'format' => '%d',
),
'total_spend' => array(
'column' => 'SUM( total_sales )',
'format' => '%f',
),
'avg_order_value' => array(
'column' => '( SUM( total_sales ) / COUNT( order_id ) )',
'format' => '%f',
),
);
foreach ( $numeric_params as $numeric_param => $param_info ) {
$subclauses = array();
$min_param = $numeric_param . '_min';
$max_param = $numeric_param . '_max';
$or_equal = isset( $query_args[ $min_param ] ) && isset( $query_args[ $max_param ] ) ? '=' : '';
if ( isset( $query_args[ $min_param ] ) ) {
$subclauses[] = $wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
"{$param_info['column']} >{$or_equal} {$param_info['format']}",
$query_args[ $min_param ]
);
}
if ( isset( $query_args[ $max_param ] ) ) {
$subclauses[] = $wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
"{$param_info['column']} <{$or_equal} {$param_info['format']}",
$query_args[ $max_param ]
);
}
if ( $subclauses ) {
$having_clauses[] = '(' . implode( ' AND ', $subclauses ) . ')';
}
}
if ( $where_clauses ) {
$preceding_match = empty( $this->get_sql_clause( 'where_time' ) ) ? ' AND ' : " {$match_operator} ";
$this->subquery->add_sql_clause( 'where', $preceding_match . implode( " {$match_operator} ", $where_clauses ) );
}
$order_status_filter = $this->get_status_subquery( $query_args );
if ( $order_status_filter ) {
$this->subquery->add_sql_clause( 'left_join', "AND ( {$order_status_filter} )" );
}
if ( $having_clauses ) {
$preceding_match = empty( $this->get_sql_clause( 'having' ) ) ? ' AND ' : " {$match_operator} ";
$this->subquery->add_sql_clause( 'having', $preceding_match . implode( " {$match_operator} ", $having_clauses ) );
}
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$customers_table_name = self::get_db_table_name();
$order_stats_table_name = $wpdb->prefix . 'wc_order_stats';
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date_registered',
'order_before' => TimeInterval::default_before(),
'order_after' => TimeInterval::default_after(),
'fields' => '*',
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$data = (object) array(
'data' => array(),
'total' => 0,
'pages' => 0,
'page_no' => 0,
);
$selections = $this->selected_columns( $query_args );
$sql_query_params = $this->add_sql_query_params( $query_args );
$count_query = "SELECT COUNT(*) FROM (
{$this->subquery->get_query_statement()}
) as tt
";
$db_records_count = (int) $wpdb->get_var(
$count_query // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
);
$params = $this->get_limit_params( $query_args );
$total_pages = (int) ceil( $db_records_count / $params['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return $data;
}
$this->subquery->clear_sql_clause( 'select' );
$this->subquery->add_sql_clause( 'select', $selections );
$this->subquery->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$this->subquery->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
$customer_data = $wpdb->get_results(
$this->subquery->get_query_statement(), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
ARRAY_A
);
if ( null === $customer_data ) {
return $data;
}
$customer_data = array_map( array( $this, 'cast_numbers' ), $customer_data );
$data = (object) array(
'data' => $customer_data,
'total' => $db_records_count,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Returns an existing customer ID for an order if one exists.
*
* @param object $order WC Order.
* @return int|bool
*/
public static function get_existing_customer_id_from_order( $order ) {
global $wpdb;
if ( ! is_a( $order, 'WC_Order' ) ) {
return false;
}
$user_id = $order->get_customer_id();
if ( 0 === $user_id ) {
$customer_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT customer_id FROM {$wpdb->prefix}wc_order_stats WHERE order_id = %d",
$order->get_id()
)
);
if ( $customer_id ) {
return $customer_id;
}
$email = $order->get_billing_email( 'edit' );
if ( $email ) {
return self::get_guest_id_by_email( $email );
} else {
return false;
}
} else {
return self::get_customer_id_by_user_id( $user_id );
}
}
/**
* Get or create a customer from a given order.
*
* @param object $order WC Order.
* @return int|bool
*/
public static function get_or_create_customer_from_order( $order ) {
if ( ! $order ) {
return false;
}
global $wpdb;
if ( ! is_a( $order, 'WC_Order' ) ) {
return false;
}
$returning_customer_id = self::get_existing_customer_id_from_order( $order );
if ( $returning_customer_id ) {
return $returning_customer_id;
}
list($data, $format) = self::get_customer_order_data_and_format( $order );
$result = $wpdb->insert( self::get_db_table_name(), $data, $format );
$customer_id = $wpdb->insert_id;
/**
* Fires when a new report customer is created.
*
* @param int $customer_id Customer ID.
* @since 4.0.0
*/
do_action( 'woocommerce_analytics_new_customer', $customer_id );
return $result ? $customer_id : false;
}
/**
* Returns a data object and format object of the customers data coming from the order.
*
* @param object $order WC_Order where we get customer info from.
* @param object|null $customer_user WC_Customer registered customer WP user.
* @return array ($data, $format)
*/
public static function get_customer_order_data_and_format( $order, $customer_user = null ) {
$data = array(
'first_name' => $order->get_customer_first_name(),
'last_name' => $order->get_customer_last_name(),
'email' => $order->get_billing_email( 'edit' ),
'city' => $order->get_billing_city( 'edit' ),
'state' => $order->get_billing_state( 'edit' ),
'postcode' => $order->get_billing_postcode( 'edit' ),
'country' => $order->get_billing_country( 'edit' ),
'date_last_active' => gmdate( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() ),
);
$format = array(
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
);
// Add registered customer data.
if ( 0 !== $order->get_user_id() ) {
$user_id = $order->get_user_id();
if ( is_null( $customer_user ) ) {
$customer_user = new \WC_Customer( $user_id );
}
$data['user_id'] = $user_id;
$data['username'] = $customer_user->get_username( 'edit' );
$data['date_registered'] = $customer_user->get_date_created( 'edit' ) ? $customer_user->get_date_created( 'edit' )->date( TimeInterval::$sql_datetime_format ) : null;
$format[] = '%d';
$format[] = '%s';
$format[] = '%s';
}
return array( $data, $format );
}
/**
* Retrieve a guest ID (when user_id is null) by email.
*
* @param string $email Email address.
* @return false|array Customer array if found, boolean false if not.
*/
public static function get_guest_id_by_email( $email ) {
global $wpdb;
$table_name = self::get_db_table_name();
$customer_id = $wpdb->get_var(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT customer_id FROM {$table_name} WHERE email = %s AND user_id IS NULL LIMIT 1",
$email
)
);
return $customer_id ? (int) $customer_id : false;
}
/**
* Retrieve a registered customer row id by user_id.
*
* @param string|int $user_id User ID.
* @return false|int Customer ID if found, boolean false if not.
*/
public static function get_customer_id_by_user_id( $user_id ) {
global $wpdb;
$table_name = self::get_db_table_name();
$customer_id = $wpdb->get_var(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT customer_id FROM {$table_name} WHERE user_id = %d LIMIT 1",
$user_id
)
);
return $customer_id ? (int) $customer_id : false;
}
/**
* Retrieve the last order made by a customer.
*
* @param int $customer_id Customer ID.
* @return object WC_Order|false.
*/
public static function get_last_order( $customer_id ) {
global $wpdb;
$orders_table = $wpdb->prefix . 'wc_order_stats';
$last_order = $wpdb->get_var(
$wpdb->prepare(
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT order_id, date_created_gmt FROM {$orders_table}
WHERE customer_id = %d
ORDER BY date_created_gmt DESC, order_id DESC LIMIT 1",
// phpcs:enable
$customer_id
)
);
if ( ! $last_order ) {
return false;
}
return wc_get_order( absint( $last_order ) );
}
/**
* Retrieve the oldest orders made by a customer.
*
* @param int $customer_id Customer ID.
* @return array Orders.
*/
public static function get_oldest_orders( $customer_id ) {
global $wpdb;
$orders_table = $wpdb->prefix . 'wc_order_stats';
$excluded_statuses = array_map( array( __CLASS__, 'normalize_order_status' ), self::get_excluded_report_order_statuses() );
$excluded_statuses_condition = '';
if ( ! empty( $excluded_statuses ) ) {
$excluded_statuses_str = implode( "','", $excluded_statuses );
$excluded_statuses_condition = "AND status NOT IN ('{$excluded_statuses_str}')";
}
return $wpdb->get_results(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT order_id, date_created FROM {$orders_table} WHERE customer_id = %d {$excluded_statuses_condition} ORDER BY date_created, order_id ASC LIMIT 2",
$customer_id
)
);
}
/**
* Retrieve the amount of orders made by a customer.
*
* @param int $customer_id Customer ID.
* @return int|null Amount of orders for customer or null on failure.
*/
public static function get_order_count( $customer_id ) {
global $wpdb;
$customer_id = absint( $customer_id );
if ( 0 === $customer_id ) {
return null;
}
$result = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT( order_id ) FROM {$wpdb->prefix}wc_order_stats WHERE customer_id = %d",
$customer_id
)
);
if ( is_null( $result ) ) {
return null;
}
return (int) $result;
}
/**
* Update the database with customer data.
*
* @param int $user_id WP User ID to update customer data for.
* @return int|bool|null Number or rows modified or false on failure.
*/
public static function update_registered_customer( $user_id ) {
global $wpdb;
$customer = new \WC_Customer( $user_id );
if ( ! self::is_valid_customer( $user_id ) ) {
return false;
}
$first_name = $customer->get_first_name();
$last_name = $customer->get_last_name();
if ( empty( $first_name ) ) {
$first_name = $customer->get_billing_first_name();
}
if ( empty( $last_name ) ) {
$last_name = $customer->get_billing_last_name();
}
$last_active = $customer->get_meta( 'wc_last_active', true, 'edit' );
$data = array(
'user_id' => $user_id,
'username' => $customer->get_username( 'edit' ),
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $customer->get_email( 'edit' ),
'city' => $customer->get_billing_city( 'edit' ),
'state' => $customer->get_billing_state( 'edit' ),
'postcode' => $customer->get_billing_postcode( 'edit' ),
'country' => $customer->get_billing_country( 'edit' ),
'date_registered' => $customer->get_date_created( 'edit' ) ? $customer->get_date_created( 'edit' )->date( TimeInterval::$sql_datetime_format ) : null,
'date_last_active' => $last_active ? gmdate( 'Y-m-d H:i:s', $last_active ) : null,
);
$format = array(
'%d',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
);
$customer_id = self::get_customer_id_by_user_id( $user_id );
if ( $customer_id ) {
// Preserve customer_id for existing user_id.
$data['customer_id'] = $customer_id;
$format[] = '%d';
}
$results = $wpdb->replace( self::get_db_table_name(), $data, $format );
/**
* Fires when customser's reports are updated.
*
* @param int $customer_id Customer ID.
* @since 4.0.0
*/
do_action( 'woocommerce_analytics_update_customer', $customer_id );
ReportsCache::invalidate();
return $results;
}
/**
* Update the database if the "last active" meta value was changed.
* Function expects to be hooked into the `added_user_meta` and `updated_user_meta` actions.
*
* @param int $meta_id ID of updated metadata entry.
* @param int $user_id ID of the user being updated.
* @param string $meta_key Meta key being updated.
*/
public static function update_registered_customer_via_last_active( $meta_id, $user_id, $meta_key ) {
if ( 'wc_last_active' === $meta_key ) {
self::update_registered_customer( $user_id );
}
}
/**
* Check if a user ID is a valid customer or other user role with past orders.
*
* @param int $user_id User ID.
* @return bool
*/
protected static function is_valid_customer( $user_id ) {
$user = new \WP_User( $user_id );
if ( (int) $user_id !== $user->ID ) {
return false;
}
/**
* Filter the customer roles, used to check if the user is a customer.
*
* @param array List of customer roles.
* @since 4.0.0
*/
$customer_roles = (array) apply_filters( 'woocommerce_analytics_customer_roles', array( 'customer' ) );
if ( empty( $user->roles ) || empty( array_intersect( $user->roles, $customer_roles ) ) ) {
return false;
}
return true;
}
/**
* Delete a customer lookup row.
*
* @param int $customer_id Customer ID.
*/
public static function delete_customer( $customer_id ) {
global $wpdb;
$customer_id = (int) $customer_id;
$num_deleted = $wpdb->delete( self::get_db_table_name(), array( 'customer_id' => $customer_id ) );
if ( $num_deleted ) {
/**
* Fires when a customer is deleted.
*
* @param int $order_id Order ID.
* @since 4.0.0
*/
do_action( 'woocommerce_analytics_delete_customer', $customer_id );
ReportsCache::invalidate();
}
}
/**
* Delete a customer lookup row by WordPress User ID.
*
* @param int $user_id WordPress User ID.
*/
public static function delete_customer_by_user_id( $user_id ) {
global $wpdb;
if ( (int) $user_id < 1 || doing_action( 'wp_uninitialize_site' ) ) {
// Skip the deletion.
return;
}
$user_id = (int) $user_id;
$num_deleted = $wpdb->delete( self::get_db_table_name(), array( 'user_id' => $user_id ) );
if ( $num_deleted ) {
ReportsCache::invalidate();
}
}
/**
* Anonymize the customer data for a single order.
*
* @internal
* @param int $order_id Order id.
* @return void
*/
public static function anonymize_customer( $order_id ) {
global $wpdb;
$customer_id = $wpdb->get_var(
$wpdb->prepare( "SELECT customer_id FROM {$wpdb->prefix}wc_order_stats WHERE order_id = %d", $order_id )
);
if ( ! $customer_id ) {
return;
}
// Long form query because $wpdb->update rejects [deleted].
$deleted_text = __( '[deleted]', 'woocommerce' );
$updated = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}wc_customer_lookup
SET
user_id = NULL,
username = %s,
first_name = %s,
last_name = %s,
email = %s,
country = '',
postcode = %s,
city = %s,
state = %s
WHERE
customer_id = %d",
array(
$deleted_text,
$deleted_text,
$deleted_text,
'deleted@site.invalid',
$deleted_text,
$deleted_text,
$deleted_text,
$customer_id,
)
)
);
// If the customer row was anonymized, flush the cache.
if ( $updated ) {
ReportsCache::invalidate();
}
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
$table_name = self::get_db_table_name();
$this->subquery = new SqlQuery( $this->context . '_subquery' );
$this->subquery->add_sql_clause( 'from', $table_name );
$this->subquery->add_sql_clause( 'select', "{$table_name}.customer_id" );
$this->subquery->add_sql_clause( 'group_by', "{$table_name}.customer_id" );
}
}
API/Reports/Customers/Query.php 0000644 00000002712 15153746747 0012442 0 ustar 00 <?php
/**
* Class for parameter-based Customers Report querying
*
* Example usage:
* $args = array(
* 'registered_before' => '2018-07-19 00:00:00',
* 'registered_after' => '2018-07-05 00:00:00',
* 'page' => 2,
* 'avg_order_value_min' => 100,
* 'country' => 'GB',
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Customers\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Customers;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Customers\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Customers report.
*
* @return array
*/
protected function get_default_query_vars() {
return array(
'per_page' => get_option( 'posts_per_page' ), // not sure if this should be the default.
'page' => 1,
'order' => 'DESC',
'orderby' => 'date_registered',
'fields' => '*',
);
}
/**
* Get product data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_customers_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-customers' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_customers_select_query', $results, $args );
}
}
API/Reports/Customers/Stats/Controller.php 0000644 00000040555 15153746747 0014565 0 ustar 00 <?php
/**
* REST API Reports customers stats controller
*
* Handles requests to the /reports/customers/stats endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Customers\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/**
* REST API Reports customers stats controller class.
*
* @internal
* @extends WC_REST_Reports_Controller
*/
class Controller extends \WC_REST_Reports_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/customers/stats';
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['registered_before'] = $request['registered_before'];
$args['registered_after'] = $request['registered_after'];
$args['match'] = $request['match'];
$args['search'] = $request['search'];
$args['name_includes'] = $request['name_includes'];
$args['name_excludes'] = $request['name_excludes'];
$args['username_includes'] = $request['username_includes'];
$args['username_excludes'] = $request['username_excludes'];
$args['email_includes'] = $request['email_includes'];
$args['email_excludes'] = $request['email_excludes'];
$args['country_includes'] = $request['country_includes'];
$args['country_excludes'] = $request['country_excludes'];
$args['last_active_before'] = $request['last_active_before'];
$args['last_active_after'] = $request['last_active_after'];
$args['orders_count_min'] = $request['orders_count_min'];
$args['orders_count_max'] = $request['orders_count_max'];
$args['total_spend_min'] = $request['total_spend_min'];
$args['total_spend_max'] = $request['total_spend_max'];
$args['avg_order_value_min'] = $request['avg_order_value_min'];
$args['avg_order_value_max'] = $request['avg_order_value_max'];
$args['last_order_before'] = $request['last_order_before'];
$args['last_order_after'] = $request['last_order_after'];
$args['customers'] = $request['customers'];
$args['fields'] = $request['fields'];
$args['force_cache_refresh'] = $request['force_cache_refresh'];
$between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' );
$normalized_params_numeric = TimeInterval::normalize_between_params( $request, $between_params_numeric, false );
$between_params_date = array( 'last_active', 'registered' );
$normalized_params_date = TimeInterval::normalize_between_params( $request, $between_params_date, true );
$args = array_merge( $args, $normalized_params_numeric, $normalized_params_date );
return $args;
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$customers_query = new Query( $query_args );
$report_data = $customers_query->get_data();
$out_data = array(
'totals' => $report_data,
);
return rest_ensure_response( $out_data );
}
/**
* Prepare a report object for serialization.
*
* @param Array $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$data = $report;
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_customers_stats', $response, $report, $request );
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
// @todo Should any of these be 'indicator's?
$totals = array(
'customers_count' => array(
'description' => __( 'Number of customers.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'avg_orders_count' => array(
'description' => __( 'Average number of orders.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'avg_total_spend' => array(
'description' => __( 'Average total spend per customer.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'format' => 'currency',
),
'avg_avg_order_value' => array(
'description' => __( 'Average AOV per customer.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'format' => 'currency',
),
);
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_customers_stats',
'type' => 'object',
'properties' => array(
'totals' => array(
'description' => __( 'Totals data.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => $totals,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['registered_before'] = array(
'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['registered_after'] = array(
'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['match'] = array(
'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'woocommerce' ),
'type' => 'string',
'default' => 'all',
'enum' => array(
'all',
'any',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['search'] = array(
'description' => __( 'Limit response to objects with a customer field containing the search term. Searches the field provided by `searchby`.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['searchby'] = array(
'description' => 'Limit results with `search` and `searchby` to specific fields containing the search term.',
'type' => 'string',
'default' => 'name',
'enum' => array(
'name',
'username',
'email',
'all',
),
);
$params['name_includes'] = array(
'description' => __( 'Limit response to objects with specific names.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['name_excludes'] = array(
'description' => __( 'Limit response to objects excluding specific names.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['username_includes'] = array(
'description' => __( 'Limit response to objects with specific usernames.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['username_excludes'] = array(
'description' => __( 'Limit response to objects excluding specific usernames.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['email_includes'] = array(
'description' => __( 'Limit response to objects including emails.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['email_excludes'] = array(
'description' => __( 'Limit response to objects excluding emails.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['country_includes'] = array(
'description' => __( 'Limit response to objects with specific countries.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['country_excludes'] = array(
'description' => __( 'Limit response to objects excluding specific countries.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['last_active_before'] = array(
'description' => __( 'Limit response to objects last active before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['last_active_after'] = array(
'description' => __( 'Limit response to objects last active after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['last_active_between'] = array(
'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ),
'items' => array(
'type' => 'string',
),
);
$params['registered_before'] = array(
'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['registered_after'] = array(
'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['registered_between'] = array(
'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ),
'items' => array(
'type' => 'string',
),
);
$params['orders_count_min'] = array(
'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'woocommerce' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['orders_count_max'] = array(
'description' => __( 'Limit response to objects with an order count less than or equal to given integer.', 'woocommerce' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['orders_count_between'] = array(
'description' => __( 'Limit response to objects with an order count between two given integers.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
'items' => array(
'type' => 'integer',
),
);
$params['total_spend_min'] = array(
'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'woocommerce' ),
'type' => 'number',
'validate_callback' => 'rest_validate_request_arg',
);
$params['total_spend_max'] = array(
'description' => __( 'Limit response to objects with a total order spend less than or equal to given number.', 'woocommerce' ),
'type' => 'number',
'validate_callback' => 'rest_validate_request_arg',
);
$params['total_spend_between'] = array(
'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
'items' => array(
'type' => 'integer',
),
);
$params['avg_order_value_min'] = array(
'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'woocommerce' ),
'type' => 'number',
'validate_callback' => 'rest_validate_request_arg',
);
$params['avg_order_value_max'] = array(
'description' => __( 'Limit response to objects with an average order spend less than or equal to given number.', 'woocommerce' ),
'type' => 'number',
'validate_callback' => 'rest_validate_request_arg',
);
$params['avg_order_value_between'] = array(
'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
'items' => array(
'type' => 'integer',
),
);
$params['last_order_before'] = array(
'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['last_order_after'] = array(
'description' => __( 'Limit response to objects with last order after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['customers'] = array(
'description' => __( 'Limit result to items with specified customer ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['fields'] = array(
'description' => __( 'Limit stats fields to the specified items.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
$params['force_cache_refresh'] = array(
'description' => __( 'Force retrieval of fresh data instead of from the cache.', 'woocommerce' ),
'type' => 'boolean',
'sanitize_callback' => 'wp_validate_boolean',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
}
API/Reports/Customers/Stats/DataStore.php 0000644 00000007230 15153746747 0014321 0 ustar 00 <?php
/**
* API\Reports\Customers\Stats\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Customers\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
/**
* API\Reports\Customers\Stats\DataStore.
*/
class DataStore extends CustomersDataStore implements DataStoreInterface {
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'customers_count' => 'intval',
'avg_orders_count' => 'floatval',
'avg_total_spend' => 'floatval',
'avg_avg_order_value' => 'floatval',
);
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'customers_stats';
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'customers_stats';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$this->report_columns = array(
'customers_count' => 'COUNT( * ) as customers_count',
'avg_orders_count' => 'AVG( orders_count ) as avg_orders_count',
'avg_total_spend' => 'AVG( total_spend ) as avg_total_spend',
'avg_avg_order_value' => 'AVG( avg_order_value ) as avg_avg_order_value',
);
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$customers_table_name = self::get_db_table_name();
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date_registered',
'fields' => '*',
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$data = (object) array(
'customers_count' => 0,
'avg_orders_count' => 0,
'avg_total_spend' => 0.0,
'avg_avg_order_value' => 0.0,
);
$selections = $this->selected_columns( $query_args );
$this->add_sql_query_params( $query_args );
// Clear SQL clauses set for parent class queries that are different here.
$this->subquery->clear_sql_clause( 'select' );
$this->subquery->add_sql_clause( 'select', 'SUM( total_sales ) AS total_spend,' );
$this->subquery->add_sql_clause(
'select',
'SUM( CASE WHEN parent_id = 0 THEN 1 END ) as orders_count,'
);
$this->subquery->add_sql_clause(
'select',
'CASE WHEN SUM( CASE WHEN parent_id = 0 THEN 1 ELSE 0 END ) = 0 THEN NULL ELSE SUM( total_sales ) / SUM( CASE WHEN parent_id = 0 THEN 1 ELSE 0 END ) END AS avg_order_value'
);
$this->clear_sql_clause( array( 'order_by', 'limit' ) );
$this->add_sql_clause( 'select', $selections );
$this->add_sql_clause( 'from', "({$this->subquery->get_query_statement()}) AS tt" );
$report_data = $wpdb->get_results(
$this->get_query_statement(), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
ARRAY_A
);
if ( null === $report_data ) {
return $data;
}
$data = (object) $this->cast_numbers( $report_data[0] );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
}
API/Reports/Customers/Stats/Query.php 0000644 00000003005 15153746747 0013534 0 ustar 00 <?php
/**
* Class for parameter-based Customers Report Stats querying
*
* Example usage:
* $args = array(
* 'registered_before' => '2018-07-19 00:00:00',
* 'registered_after' => '2018-07-05 00:00:00',
* 'page' => 2,
* 'avg_order_value_min' => 100,
* 'country' => 'GB',
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Customers\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Customers\Stats\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Customers report.
*
* @return array
*/
protected function get_default_query_vars() {
return array(
'per_page' => get_option( 'posts_per_page' ), // not sure if this should be the default.
'page' => 1,
'order' => 'DESC',
'orderby' => 'date_registered',
'fields' => '*', // @todo Needed?
);
}
/**
* Get product data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_customers_stats_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-customers-stats' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_customers_stats_select_query', $results, $args );
}
}
API/Reports/DataStore.php 0000644 00000142667 15153746747 0011255 0 ustar 00 <?php
/**
* Admin\API\Reports\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/**
* Admin\API\Reports\DataStore: Common parent for custom report data stores.
*/
class DataStore extends SqlQuery {
/**
* Cache group for the reports.
*
* @var string
*/
protected $cache_group = 'reports';
/**
* Time out for the cache.
*
* @var int
*/
protected $cache_timeout = 3600;
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = '';
/**
* Table used as a data store for this report.
*
* @var string
*/
protected static $table_name = '';
/**
* Date field name.
*
* @var string
*/
protected $date_column_name = 'date_created';
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array();
/**
* SQL columns to select in the db query.
*
* @var array
*/
protected $report_columns = array();
// @todo This does not really belong here, maybe factor out the comparison as separate class?
/**
* Order by property, used in the cmp function.
*
* @var string
*/
private $order_by = '';
/**
* Order property, used in the cmp function.
*
* @var string
*/
private $order = '';
/**
* Query limit parameters.
*
* @var array
*/
private $limit_parameters = array();
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'reports';
/**
* Subquery object for query nesting.
*
* @var SqlQuery
*/
protected $subquery;
/**
* Totals query object.
*
* @var SqlQuery
*/
protected $total_query;
/**
* Intervals query object.
*
* @var SqlQuery
*/
protected $interval_query;
/**
* Refresh the cache for the current query when true.
*
* @var bool
*/
protected $force_cache_refresh = false;
/**
* Include debugging information in the returned data when true.
*
* @var bool
*/
protected $debug_cache = true;
/**
* Debugging information to include in the returned data.
*
* @var array
*/
protected $debug_cache_data = array();
/**
* Class constructor.
*/
public function __construct() {
self::set_db_table_name();
$this->assign_report_columns();
if ( $this->report_columns ) {
$this->report_columns = apply_filters(
'woocommerce_admin_report_columns',
$this->report_columns,
$this->context,
self::get_db_table_name()
);
}
// Utilize enveloped responses to include debugging info.
// See https://querymonitor.com/blog/2021/05/debugging-wordpress-rest-api-requests/
if ( isset( $_GET['_envelope'] ) ) {
$this->debug_cache = true;
add_filter( 'rest_envelope_response', array( $this, 'add_debug_cache_to_envelope' ), 999, 2 );
}
}
/**
* Get table name from database class.
*/
public static function get_db_table_name() {
global $wpdb;
return isset( $wpdb->{static::$table_name} ) ? $wpdb->{static::$table_name} : $wpdb->prefix . static::$table_name;
}
/**
* Set table name from database class.
*/
protected static function set_db_table_name() {
global $wpdb;
if ( static::$table_name && ! isset( $wpdb->{static::$table_name} ) ) {
$wpdb->{static::$table_name} = $wpdb->prefix . static::$table_name;
}
}
/**
* Whether or not the report should use the caching layer.
*
* Provides an opportunity for plugins to prevent reports from using cache.
*
* @return boolean Whether or not to utilize caching.
*/
protected function should_use_cache() {
/**
* Determines if a report will utilize caching.
*
* @param bool $use_cache Whether or not to use cache.
* @param string $cache_key The report's cache key. Used to identify the report.
*/
return (bool) apply_filters( 'woocommerce_analytics_report_should_use_cache', true, $this->cache_key );
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
if ( isset( $params['force_cache_refresh'] ) ) {
if ( true === $params['force_cache_refresh'] ) {
$this->force_cache_refresh = true;
}
// We don't want this param in the key.
unset( $params['force_cache_refresh'] );
}
if ( true === $this->debug_cache ) {
$this->debug_cache_data['query_args'] = $params;
}
return implode(
'_',
array(
'wc_report',
$this->cache_key,
md5( wp_json_encode( $params ) ),
)
);
}
/**
* Wrapper around Cache::get().
*
* @param string $cache_key Cache key.
* @return mixed
*/
protected function get_cached_data( $cache_key ) {
if ( true === $this->debug_cache ) {
$this->debug_cache_data['should_use_cache'] = $this->should_use_cache();
$this->debug_cache_data['force_cache_refresh'] = $this->force_cache_refresh;
$this->debug_cache_data['cache_hit'] = false;
}
if ( $this->should_use_cache() && false === $this->force_cache_refresh ) {
$cached_data = Cache::get( $cache_key );
$cache_hit = false !== $cached_data;
if ( true === $this->debug_cache ) {
$this->debug_cache_data['cache_hit'] = $cache_hit;
}
return $cached_data;
}
// Cached item has now functionally been refreshed. Reset the option.
$this->force_cache_refresh = false;
return false;
}
/**
* Wrapper around Cache::set().
*
* @param string $cache_key Cache key.
* @param mixed $value New value.
* @return bool
*/
protected function set_cached_data( $cache_key, $value ) {
if ( $this->should_use_cache() ) {
return Cache::set( $cache_key, $value );
}
return true;
}
/**
* Add cache debugging information to an enveloped API response.
*
* @param array $envelope
* @param \WP_REST_Response $response
*
* @return array
*/
public function add_debug_cache_to_envelope( $envelope, $response ) {
if ( 0 !== strncmp( '/wc-analytics', $response->get_matched_route(), 13 ) ) {
return $envelope;
}
if ( ! empty( $this->debug_cache_data ) ) {
$envelope['debug_cache'] = $this->debug_cache_data;
}
return $envelope;
}
/**
* Compares two report data objects by pre-defined object property and ASC/DESC ordering.
*
* @param stdClass $a Object a.
* @param stdClass $b Object b.
* @return string
*/
private function interval_cmp( $a, $b ) {
if ( '' === $this->order_by || '' === $this->order ) {
return 0;
// @todo Should return WP_Error here perhaps?
}
if ( $a[ $this->order_by ] === $b[ $this->order_by ] ) {
// As relative order is undefined in case of equality in usort, second-level sorting by date needs to be enforced
// so that paging is stable.
if ( $a['time_interval'] === $b['time_interval'] ) {
return 0; // This should never happen.
} elseif ( $a['time_interval'] > $b['time_interval'] ) {
return 1;
} elseif ( $a['time_interval'] < $b['time_interval'] ) {
return -1;
}
} elseif ( $a[ $this->order_by ] > $b[ $this->order_by ] ) {
return strtolower( $this->order ) === 'desc' ? -1 : 1;
} elseif ( $a[ $this->order_by ] < $b[ $this->order_by ] ) {
return strtolower( $this->order ) === 'desc' ? 1 : -1;
}
}
/**
* Sorts intervals according to user's request.
*
* They are pre-sorted in SQL, but after adding gaps, they need to be sorted including the added ones.
*
* @param stdClass $data Data object, must contain an array under $data->intervals.
* @param string $sort_by Ordering property.
* @param string $direction DESC/ASC.
*/
protected function sort_intervals( &$data, $sort_by, $direction ) {
$this->sort_array( $data->intervals, $sort_by, $direction );
}
/**
* Sorts array of arrays based on subarray key $sort_by.
*
* @param array $arr Array to sort.
* @param string $sort_by Ordering property.
* @param string $direction DESC/ASC.
*/
protected function sort_array( &$arr, $sort_by, $direction ) {
$this->order_by = $this->normalize_order_by( $sort_by );
$this->order = $direction;
usort( $arr, array( $this, 'interval_cmp' ) );
}
/**
* Fills in interval gaps from DB with 0-filled objects.
*
* @param array $db_intervals Array of all intervals present in the db.
* @param DateTime $start_datetime Start date.
* @param DateTime $end_datetime End date.
* @param string $time_interval Time interval, e.g. day, week, month.
* @param stdClass $data Data with SQL extracted intervals.
* @return stdClass
*/
protected function fill_in_missing_intervals( $db_intervals, $start_datetime, $end_datetime, $time_interval, &$data ) {
// @todo This is ugly and messy.
$local_tz = new \DateTimeZone( wc_timezone_string() );
// At this point, we don't know when we can stop iterating, as the ordering can be based on any value.
$time_ids = array_flip( wp_list_pluck( $data->intervals, 'time_interval' ) );
$db_intervals = array_flip( $db_intervals );
// Totals object used to get all needed properties.
$totals_arr = get_object_vars( $data->totals );
foreach ( $totals_arr as $key => $val ) {
$totals_arr[ $key ] = 0;
}
// @todo Should 'products' be in intervals?
unset( $totals_arr['products'] );
while ( $start_datetime <= $end_datetime ) {
$next_start = TimeInterval::iterate( $start_datetime, $time_interval );
$time_id = TimeInterval::time_interval_id( $time_interval, $start_datetime );
// Either create fill-zero interval or use data from db.
if ( $next_start > $end_datetime ) {
$interval_end = $end_datetime->format( 'Y-m-d H:i:s' );
} else {
$prev_end_timestamp = (int) $next_start->format( 'U' ) - 1;
$prev_end = new \DateTime();
$prev_end->setTimestamp( $prev_end_timestamp );
$prev_end->setTimezone( $local_tz );
$interval_end = $prev_end->format( 'Y-m-d H:i:s' );
}
if ( array_key_exists( $time_id, $time_ids ) ) {
// For interval present in the db for this time frame, just fill in dates.
$record = &$data->intervals[ $time_ids[ $time_id ] ];
$record['date_start'] = $start_datetime->format( 'Y-m-d H:i:s' );
$record['date_end'] = $interval_end;
} elseif ( ! array_key_exists( $time_id, $db_intervals ) ) {
// For intervals present in the db outside of this time frame, do nothing.
// For intervals not present in the db, fabricate it.
$record_arr = array();
$record_arr['time_interval'] = $time_id;
$record_arr['date_start'] = $start_datetime->format( 'Y-m-d H:i:s' );
$record_arr['date_end'] = $interval_end;
$data->intervals[] = array_merge( $record_arr, $totals_arr );
}
$start_datetime = $next_start;
}
return $data;
}
/**
* Converts input datetime parameters to local timezone. If there are no inputs from the user in query_args,
* uses default from $defaults.
*
* @param array $query_args Array of query arguments.
* @param array $defaults Array of default values.
*/
protected function normalize_timezones( &$query_args, $defaults ) {
$local_tz = new \DateTimeZone( wc_timezone_string() );
foreach ( array( 'before', 'after' ) as $query_arg_key ) {
if ( isset( $query_args[ $query_arg_key ] ) && is_string( $query_args[ $query_arg_key ] ) ) {
// Assume that unspecified timezone is a local timezone.
$datetime = new \DateTime( $query_args[ $query_arg_key ], $local_tz );
// In case timezone was forced by using +HH:MM, convert to local timezone.
$datetime->setTimezone( $local_tz );
$query_args[ $query_arg_key ] = $datetime;
} elseif ( isset( $query_args[ $query_arg_key ] ) && is_a( $query_args[ $query_arg_key ], 'DateTime' ) ) {
// In case timezone is in other timezone, convert to local timezone.
$query_args[ $query_arg_key ]->setTimezone( $local_tz );
} else {
$query_args[ $query_arg_key ] = isset( $defaults[ $query_arg_key ] ) ? $defaults[ $query_arg_key ] : null;
}
}
}
/**
* Removes extra records from intervals so that only requested number of records get returned.
*
* @param stdClass $data Data from whose intervals the records get removed.
* @param int $page_no Offset requested by the user.
* @param int $items_per_page Number of records requested by the user.
* @param int $db_interval_count Database interval count.
* @param int $expected_interval_count Expected interval count on the output.
* @param string $order_by Order by field.
* @param string $order ASC or DESC.
*/
protected function remove_extra_records( &$data, $page_no, $items_per_page, $db_interval_count, $expected_interval_count, $order_by, $order ) {
if ( 'date' === strtolower( $order_by ) ) {
$offset = 0;
} else {
if ( 'asc' === strtolower( $order ) ) {
$offset = ( $page_no - 1 ) * $items_per_page;
} else {
$offset = ( $page_no - 1 ) * $items_per_page - $db_interval_count;
}
$offset = $offset < 0 ? 0 : $offset;
}
$count = $expected_interval_count - ( $page_no - 1 ) * $items_per_page;
if ( $count < 0 ) {
$count = 0;
} elseif ( $count > $items_per_page ) {
$count = $items_per_page;
}
$data->intervals = array_slice( $data->intervals, $offset, $count );
}
/**
* Returns expected number of items on the page in case of date ordering.
*
* @param int $expected_interval_count Expected number of intervals in total.
* @param int $items_per_page Number of items per page.
* @param int $page_no Page number.
*
* @return float|int
*/
protected function expected_intervals_on_page( $expected_interval_count, $items_per_page, $page_no ) {
$total_pages = (int) ceil( $expected_interval_count / $items_per_page );
if ( $page_no < $total_pages ) {
return $items_per_page;
} elseif ( $page_no === $total_pages ) {
return $expected_interval_count - ( $page_no - 1 ) * $items_per_page;
} else {
return 0;
}
}
/**
* Returns true if there are any intervals that need to be filled in the response.
*
* @param int $expected_interval_count Expected number of intervals in total.
* @param int $db_records Total number of records for given period in the database.
* @param int $items_per_page Number of items per page.
* @param int $page_no Page number.
* @param string $order asc or desc.
* @param string $order_by Column by which the result will be sorted.
* @param int $intervals_count Number of records for given (possibly shortened) time interval.
*
* @return bool
*/
protected function intervals_missing( $expected_interval_count, $db_records, $items_per_page, $page_no, $order, $order_by, $intervals_count ) {
if ( $expected_interval_count <= $db_records ) {
return false;
}
if ( 'date' === $order_by ) {
$expected_intervals_on_page = $this->expected_intervals_on_page( $expected_interval_count, $items_per_page, $page_no );
return $intervals_count < $expected_intervals_on_page;
}
if ( 'desc' === $order ) {
return $page_no > floor( $db_records / $items_per_page );
}
if ( 'asc' === $order ) {
return $page_no <= ceil( ( $expected_interval_count - $db_records ) / $items_per_page );
}
// Invalid ordering.
return false;
}
/**
* Updates the LIMIT query part for Intervals query of the report.
*
* If there are less records in the database than time intervals, then we need to remap offset in SQL query
* to fetch correct records.
*
* @param array $query_args Query arguments.
* @param int $db_interval_count Database interval count.
* @param int $expected_interval_count Expected interval count on the output.
* @param string $table_name Name of the db table relevant for the date constraint.
*/
protected function update_intervals_sql_params( &$query_args, $db_interval_count, $expected_interval_count, $table_name ) {
if ( $db_interval_count === $expected_interval_count ) {
return;
}
$params = $this->get_limit_params( $query_args );
$local_tz = new \DateTimeZone( wc_timezone_string() );
if ( 'date' === strtolower( $query_args['orderby'] ) ) {
// page X in request translates to slightly different dates in the db, in case some
// records are missing from the db.
$start_iteration = 0;
$end_iteration = 0;
if ( 'asc' === strtolower( $query_args['order'] ) ) {
// ORDER BY date ASC.
$new_start_date = $query_args['after'];
$intervals_to_skip = ( $query_args['page'] - 1 ) * $params['per_page'];
$latest_end_date = $query_args['before'];
for ( $i = 0; $i < $intervals_to_skip; $i++ ) {
if ( $new_start_date > $latest_end_date ) {
$new_start_date = $latest_end_date;
$start_iteration = 0;
break;
}
$new_start_date = TimeInterval::iterate( $new_start_date, $query_args['interval'] );
$start_iteration ++;
}
$new_end_date = clone $new_start_date;
for ( $i = 0; $i < $params['per_page']; $i++ ) {
if ( $new_end_date > $latest_end_date ) {
break;
}
$new_end_date = TimeInterval::iterate( $new_end_date, $query_args['interval'] );
$end_iteration ++;
}
if ( $new_end_date > $latest_end_date ) {
$new_end_date = $latest_end_date;
$end_iteration = 0;
}
if ( $end_iteration ) {
$new_end_date_timestamp = (int) $new_end_date->format( 'U' ) - 1;
$new_end_date->setTimestamp( $new_end_date_timestamp );
}
} else {
// ORDER BY date DESC.
$new_end_date = $query_args['before'];
$intervals_to_skip = ( $query_args['page'] - 1 ) * $params['per_page'];
$earliest_start_date = $query_args['after'];
for ( $i = 0; $i < $intervals_to_skip; $i++ ) {
if ( $new_end_date < $earliest_start_date ) {
$new_end_date = $earliest_start_date;
$end_iteration = 0;
break;
}
$new_end_date = TimeInterval::iterate( $new_end_date, $query_args['interval'], true );
$end_iteration ++;
}
$new_start_date = clone $new_end_date;
for ( $i = 0; $i < $params['per_page']; $i++ ) {
if ( $new_start_date < $earliest_start_date ) {
break;
}
$new_start_date = TimeInterval::iterate( $new_start_date, $query_args['interval'], true );
$start_iteration ++;
}
if ( $new_start_date < $earliest_start_date ) {
$new_start_date = $earliest_start_date;
$start_iteration = 0;
}
if ( $start_iteration ) {
// @todo Is this correct? should it only be added if iterate runs? other two iterate instances, too?
$new_start_date_timestamp = (int) $new_start_date->format( 'U' ) + 1;
$new_start_date->setTimestamp( $new_start_date_timestamp );
}
}
// @todo - Do this without modifying $query_args?
$query_args['adj_after'] = $new_start_date;
$query_args['adj_before'] = $new_end_date;
$adj_after = $new_start_date->format( TimeInterval::$sql_datetime_format );
$adj_before = $new_end_date->format( TimeInterval::$sql_datetime_format );
$this->interval_query->clear_sql_clause( array( 'where_time', 'limit' ) );
$this->interval_query->add_sql_clause( 'where_time', "AND {$table_name}.`{$this->date_column_name}` <= '$adj_before'" );
$this->interval_query->add_sql_clause( 'where_time', "AND {$table_name}.`{$this->date_column_name}` >= '$adj_after'" );
$this->clear_sql_clause( 'limit' );
$this->add_sql_clause( 'limit', 'LIMIT 0,' . $params['per_page'] );
} else {
if ( 'asc' === $query_args['order'] ) {
$offset = ( ( $query_args['page'] - 1 ) * $params['per_page'] ) - ( $expected_interval_count - $db_interval_count );
$offset = $offset < 0 ? 0 : $offset;
$count = $query_args['page'] * $params['per_page'] - ( $expected_interval_count - $db_interval_count );
if ( $count < 0 ) {
$count = 0;
} elseif ( $count > $params['per_page'] ) {
$count = $params['per_page'];
}
$this->clear_sql_clause( 'limit' );
$this->add_sql_clause( 'limit', 'LIMIT ' . $offset . ',' . $count );
}
// Otherwise no change in limit clause.
// @todo - Do this without modifying $query_args?
$query_args['adj_after'] = $query_args['after'];
$query_args['adj_before'] = $query_args['before'];
}
}
/**
* Casts strings returned from the database to appropriate data types for output.
*
* @param array $array Associative array of values extracted from the database.
* @return array|WP_Error
*/
protected function cast_numbers( $array ) {
$retyped_array = array();
$column_types = apply_filters( 'woocommerce_rest_reports_column_types', $this->column_types, $array );
foreach ( $array as $column_name => $value ) {
if ( is_array( $value ) ) {
$value = $this->cast_numbers( $value );
}
if ( isset( $column_types[ $column_name ] ) ) {
$retyped_array[ $column_name ] = $column_types[ $column_name ]( $value );
} else {
$retyped_array[ $column_name ] = $value;
}
}
return $retyped_array;
}
/**
* Returns a list of columns selected by the query_args formatted as a comma separated string.
*
* @param array $query_args User-supplied options.
* @return string
*/
protected function selected_columns( $query_args ) {
$selections = $this->report_columns;
if ( isset( $query_args['fields'] ) && is_array( $query_args['fields'] ) ) {
$keep = array();
foreach ( $query_args['fields'] as $field ) {
if ( isset( $selections[ $field ] ) ) {
$keep[ $field ] = $selections[ $field ];
}
}
$selections = implode( ', ', $keep );
} else {
$selections = implode( ', ', $selections );
}
return $selections;
}
/**
* Get the excluded order statuses used when calculating reports.
*
* @return array
*/
protected static function get_excluded_report_order_statuses() {
$excluded_statuses = \WC_Admin_Settings::get_option( 'woocommerce_excluded_report_order_statuses', array( 'pending', 'failed', 'cancelled' ) );
$excluded_statuses = array_merge( array( 'auto-draft', 'trash' ), array_map( 'esc_sql', $excluded_statuses ) );
return apply_filters( 'woocommerce_analytics_excluded_order_statuses', $excluded_statuses );
}
/**
* Maps order status provided by the user to the one used in the database.
*
* @param string $status Order status.
* @return string
*/
protected static function normalize_order_status( $status ) {
$status = trim( $status );
return 'wc-' . $status;
}
/**
* Normalizes order_by clause to match to SQL query.
*
* @param string $order_by Order by option requested by user.
* @return string
*/
protected function normalize_order_by( $order_by ) {
if ( 'date' === $order_by ) {
return 'time_interval';
}
return $order_by;
}
/**
* Updates start and end dates for intervals so that they represent intervals' borders, not times when data in db were recorded.
*
* E.g. if there are db records for only Tuesday and Thursday this week, the actual week interval is [Mon, Sun], not [Tue, Thu].
*
* @param DateTime $start_datetime Start date.
* @param DateTime $end_datetime End date.
* @param string $time_interval Time interval, e.g. day, week, month.
* @param array $intervals Array of intervals extracted from SQL db.
*/
protected function update_interval_boundary_dates( $start_datetime, $end_datetime, $time_interval, &$intervals ) {
$local_tz = new \DateTimeZone( wc_timezone_string() );
foreach ( $intervals as $key => $interval ) {
$datetime = new \DateTime( $interval['datetime_anchor'], $local_tz );
$prev_start = TimeInterval::iterate( $datetime, $time_interval, true );
// @todo Not sure if the +1/-1 here are correct, especially as they are applied before the ?: below.
$prev_start_timestamp = (int) $prev_start->format( 'U' ) + 1;
$prev_start->setTimestamp( $prev_start_timestamp );
if ( $start_datetime ) {
$date_start = $prev_start < $start_datetime ? $start_datetime : $prev_start;
$intervals[ $key ]['date_start'] = $date_start->format( 'Y-m-d H:i:s' );
} else {
$intervals[ $key ]['date_start'] = $prev_start->format( 'Y-m-d H:i:s' );
}
$next_end = TimeInterval::iterate( $datetime, $time_interval );
$next_end_timestamp = (int) $next_end->format( 'U' ) - 1;
$next_end->setTimestamp( $next_end_timestamp );
if ( $end_datetime ) {
$date_end = $next_end > $end_datetime ? $end_datetime : $next_end;
$intervals[ $key ]['date_end'] = $date_end->format( 'Y-m-d H:i:s' );
} else {
$intervals[ $key ]['date_end'] = $next_end->format( 'Y-m-d H:i:s' );
}
$intervals[ $key ]['interval'] = $time_interval;
}
}
/**
* Change structure of intervals to form a correct response.
*
* Also converts local datetimes to GMT and adds them to the intervals.
*
* @param array $intervals Time interval, e.g. day, week, month.
*/
protected function create_interval_subtotals( &$intervals ) {
foreach ( $intervals as $key => $interval ) {
$start_gmt = TimeInterval::convert_local_datetime_to_gmt( $interval['date_start'] );
$end_gmt = TimeInterval::convert_local_datetime_to_gmt( $interval['date_end'] );
// Move intervals result to subtotals object.
$intervals[ $key ] = array(
'interval' => $interval['time_interval'],
'date_start' => $interval['date_start'],
'date_start_gmt' => $start_gmt->format( TimeInterval::$sql_datetime_format ),
'date_end' => $interval['date_end'],
'date_end_gmt' => $end_gmt->format( TimeInterval::$sql_datetime_format ),
);
unset( $interval['interval'] );
unset( $interval['date_start'] );
unset( $interval['date_end'] );
unset( $interval['datetime_anchor'] );
unset( $interval['time_interval'] );
$intervals[ $key ]['subtotals'] = (object) $this->cast_numbers( $interval );
}
}
/**
* Fills WHERE clause of SQL request with date-related constraints.
*
* @param array $query_args Parameters supplied by the user.
* @param string $table_name Name of the db table relevant for the date constraint.
*/
protected function add_time_period_sql_params( $query_args, $table_name ) {
$this->clear_sql_clause( array( 'from', 'where_time', 'where' ) );
if ( isset( $this->subquery ) ) {
$this->subquery->clear_sql_clause( 'where_time' );
}
if ( isset( $query_args['before'] ) && '' !== $query_args['before'] ) {
if ( is_a( $query_args['before'], 'WC_DateTime' ) ) {
$datetime_str = $query_args['before']->date( TimeInterval::$sql_datetime_format );
} else {
$datetime_str = $query_args['before']->format( TimeInterval::$sql_datetime_format );
}
if ( isset( $this->subquery ) ) {
$this->subquery->add_sql_clause( 'where_time', "AND {$table_name}.`{$this->date_column_name}` <= '$datetime_str'" );
} else {
$this->add_sql_clause( 'where_time', "AND {$table_name}.`{$this->date_column_name}` <= '$datetime_str'" );
}
}
if ( isset( $query_args['after'] ) && '' !== $query_args['after'] ) {
if ( is_a( $query_args['after'], 'WC_DateTime' ) ) {
$datetime_str = $query_args['after']->date( TimeInterval::$sql_datetime_format );
} else {
$datetime_str = $query_args['after']->format( TimeInterval::$sql_datetime_format );
}
if ( isset( $this->subquery ) ) {
$this->subquery->add_sql_clause( 'where_time', "AND {$table_name}.`{$this->date_column_name}` >= '$datetime_str'" );
} else {
$this->add_sql_clause( 'where_time', "AND {$table_name}.`{$this->date_column_name}` >= '$datetime_str'" );
}
}
}
/**
* Fills LIMIT clause of SQL request based on user supplied parameters.
*
* @param array $query_args Parameters supplied by the user.
* @return array
*/
protected function get_limit_sql_params( $query_args ) {
global $wpdb;
$params = $this->get_limit_params( $query_args );
$this->clear_sql_clause( 'limit' );
$this->add_sql_clause( 'limit', $wpdb->prepare( 'LIMIT %d, %d', $params['offset'], $params['per_page'] ) );
return $params;
}
/**
* Fills LIMIT parameters of SQL request based on user supplied parameters.
*
* @param array $query_args Parameters supplied by the user.
* @return array
*/
protected function get_limit_params( $query_args = array() ) {
if ( isset( $query_args['per_page'] ) && is_numeric( $query_args['per_page'] ) ) {
$this->limit_parameters['per_page'] = (int) $query_args['per_page'];
} else {
$this->limit_parameters['per_page'] = get_option( 'posts_per_page' );
}
$this->limit_parameters['offset'] = 0;
if ( isset( $query_args['page'] ) ) {
$this->limit_parameters['offset'] = ( (int) $query_args['page'] - 1 ) * $this->limit_parameters['per_page'];
}
return $this->limit_parameters;
}
/**
* Generates a virtual table given a list of IDs.
*
* @param array $ids Array of IDs.
* @param array $id_field Name of the ID field.
* @param array $other_values Other values that must be contained in the virtual table.
* @return array
*/
protected function get_ids_table( $ids, $id_field, $other_values = array() ) {
global $wpdb;
$selects = array();
foreach ( $ids as $id ) {
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$new_select = $wpdb->prepare( "SELECT %s AS {$id_field}", $id );
foreach ( $other_values as $key => $value ) {
$new_select .= $wpdb->prepare( ", %s AS {$key}", $value );
}
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
array_push( $selects, $new_select );
}
return join( ' UNION ', $selects );
}
/**
* Returns a comma separated list of the fields in the `query_args`, if there aren't, returns `report_columns` keys.
*
* @param array $query_args Parameters supplied by the user.
* @return array
*/
protected function get_fields( $query_args ) {
if ( isset( $query_args['fields'] ) && is_array( $query_args['fields'] ) ) {
return $query_args['fields'];
}
return array_keys( $this->report_columns );
}
/**
* Returns a comma separated list of the field names prepared to be used for a selection after a join with `default_results`.
*
* @param array $fields Array of fields name.
* @param array $default_results_fields Fields to load from `default_results` table.
* @param array $outer_selections Array of fields that are not selected in the inner query.
* @return string
*/
protected function format_join_selections( $fields, $default_results_fields, $outer_selections = array() ) {
foreach ( $fields as $i => $field ) {
foreach ( $default_results_fields as $default_results_field ) {
if ( $field === $default_results_field ) {
$field = esc_sql( $field );
$fields[ $i ] = "default_results.{$field} AS {$field}";
}
}
if ( in_array( $field, $outer_selections, true ) && array_key_exists( $field, $this->report_columns ) ) {
$fields[ $i ] = $this->report_columns[ $field ];
}
}
return implode( ', ', $fields );
}
/**
* Fills ORDER BY clause of SQL request based on user supplied parameters.
*
* @param array $query_args Parameters supplied by the user.
*/
protected function add_order_by_sql_params( $query_args ) {
if ( isset( $query_args['orderby'] ) ) {
$order_by_clause = $this->normalize_order_by( esc_sql( $query_args['orderby'] ) );
} else {
$order_by_clause = '';
}
$this->clear_sql_clause( 'order_by' );
$this->add_sql_clause( 'order_by', $order_by_clause );
$this->add_orderby_order_clause( $query_args, $this );
}
/**
* Fills FROM and WHERE clauses of SQL request for 'Intervals' section of data response based on user supplied parameters.
*
* @param array $query_args Parameters supplied by the user.
* @param string $table_name Name of the db table relevant for the date constraint.
*/
protected function add_intervals_sql_params( $query_args, $table_name ) {
$this->clear_sql_clause( array( 'from', 'where_time', 'where' ) );
$this->add_time_period_sql_params( $query_args, $table_name );
if ( isset( $query_args['interval'] ) && '' !== $query_args['interval'] ) {
$interval = $query_args['interval'];
$this->clear_sql_clause( 'select' );
$this->add_sql_clause( 'select', TimeInterval::db_datetime_format( $interval, $table_name, $this->date_column_name ) );
}
}
/**
* Get join and where clauses for refunds based on user supplied parameters.
*
* @param array $query_args Parameters supplied by the user.
* @return array
*/
protected function get_refund_subquery( $query_args ) {
global $wpdb;
$table_name = $wpdb->prefix . 'wc_order_stats';
$sql_query = array(
'where_clause' => '',
'from_clause' => '',
);
if ( ! isset( $query_args['refunds'] ) ) {
return $sql_query;
}
if ( 'all' === $query_args['refunds'] ) {
$sql_query['where_clause'] .= 'parent_id != 0';
}
if ( 'none' === $query_args['refunds'] ) {
$sql_query['where_clause'] .= 'parent_id = 0';
}
if ( 'full' === $query_args['refunds'] || 'partial' === $query_args['refunds'] ) {
$operator = 'full' === $query_args['refunds'] ? '=' : '!=';
$sql_query['from_clause'] .= " JOIN {$table_name} parent_order_stats ON {$table_name}.parent_id = parent_order_stats.order_id";
$sql_query['where_clause'] .= "parent_order_stats.status {$operator} '{$this->normalize_order_status( 'refunded' )}'";
}
return $sql_query;
}
/**
* Returns an array of products belonging to given categories.
*
* @param array $categories List of categories IDs.
* @return array|stdClass
*/
protected function get_products_by_cat_ids( $categories ) {
$terms = get_terms(
array(
'taxonomy' => 'product_cat',
'include' => $categories,
)
);
if ( is_wp_error( $terms ) || empty( $terms ) ) {
return array();
}
$args = array(
'category' => wc_list_pluck( $terms, 'slug' ),
'limit' => -1,
'return' => 'ids',
);
return wc_get_products( $args );
}
/**
* Get WHERE filter by object ids subquery.
*
* @param string $select_table Select table name.
* @param string $select_field Select table object ID field name.
* @param string $filter_table Lookup table name.
* @param string $filter_field Lookup table object ID field name.
* @param string $compare Comparison string (IN|NOT IN).
* @param string $id_list Comma separated ID list.
*
* @return string
*/
protected function get_object_where_filter( $select_table, $select_field, $filter_table, $filter_field, $compare, $id_list ) {
global $wpdb;
if ( empty( $id_list ) ) {
return '';
}
$lookup_name = isset( $wpdb->$filter_table ) ? $wpdb->$filter_table : $wpdb->prefix . $filter_table;
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
return " {$select_table}.{$select_field} {$compare} (
SELECT
DISTINCT {$filter_table}.{$select_field}
FROM
{$filter_table}
WHERE
{$filter_table}.{$filter_field} IN ({$id_list})
)";
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
/**
* Returns an array of ids of allowed products, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return array
*/
protected function get_included_products_array( $query_args ) {
$included_products = array();
$operator = $this->get_match_operator( $query_args );
if ( isset( $query_args['category_includes'] ) && is_array( $query_args['category_includes'] ) && count( $query_args['category_includes'] ) > 0 ) {
$included_products = $this->get_products_by_cat_ids( $query_args['category_includes'] );
// If no products were found in the specified categories, we will force an empty set
// by matching a product ID of -1, unless the filters are OR/any and products are specified.
if ( empty( $included_products ) ) {
$included_products = array( '-1' );
}
}
if ( isset( $query_args['product_includes'] ) && is_array( $query_args['product_includes'] ) && count( $query_args['product_includes'] ) > 0 ) {
if ( count( $included_products ) > 0 ) {
if ( 'AND' === $operator ) {
// AND results in an intersection between products from selected categories and manually included products.
$included_products = array_intersect( $included_products, $query_args['product_includes'] );
} elseif ( 'OR' === $operator ) {
// OR results in a union of products from selected categories and manually included products.
$included_products = array_merge( $included_products, $query_args['product_includes'] );
}
} else {
$included_products = $query_args['product_includes'];
}
}
return $included_products;
}
/**
* Returns comma separated ids of allowed products, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_included_products( $query_args ) {
$included_products = $this->get_included_products_array( $query_args );
return implode( ',', $included_products );
}
/**
* Returns comma separated ids of allowed variations, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_included_variations( $query_args ) {
return $this->get_filtered_ids( $query_args, 'variation_includes' );
}
/**
* Returns comma separated ids of excluded variations, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_excluded_variations( $query_args ) {
return $this->get_filtered_ids( $query_args, 'variation_excludes' );
}
/**
* Returns an array of ids of disallowed products, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return array
*/
protected function get_excluded_products_array( $query_args ) {
$excluded_products = array();
$operator = $this->get_match_operator( $query_args );
if ( isset( $query_args['category_excludes'] ) && is_array( $query_args['category_excludes'] ) && count( $query_args['category_excludes'] ) > 0 ) {
$excluded_products = $this->get_products_by_cat_ids( $query_args['category_excludes'] );
}
if ( isset( $query_args['product_excludes'] ) && is_array( $query_args['product_excludes'] ) && count( $query_args['product_excludes'] ) > 0 ) {
$excluded_products = array_merge( $excluded_products, $query_args['product_excludes'] );
}
return $excluded_products;
}
/**
* Returns comma separated ids of excluded products, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_excluded_products( $query_args ) {
$excluded_products = $this->get_excluded_products_array( $query_args );
return implode( ',', $excluded_products );
}
/**
* Returns comma separated ids of included categories, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_included_categories( $query_args ) {
return $this->get_filtered_ids( $query_args, 'category_includes' );
}
/**
* Returns comma separated ids of included coupons, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @param string $field Field name in the parameter list.
* @return string
*/
protected function get_included_coupons( $query_args, $field = 'coupon_includes' ) {
return $this->get_filtered_ids( $query_args, $field );
}
/**
* Returns comma separated ids of excluded coupons, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_excluded_coupons( $query_args ) {
return $this->get_filtered_ids( $query_args, 'coupon_excludes' );
}
/**
* Returns comma separated ids of included orders, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_included_orders( $query_args ) {
return $this->get_filtered_ids( $query_args, 'order_includes' );
}
/**
* Returns comma separated ids of excluded orders, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_excluded_orders( $query_args ) {
return $this->get_filtered_ids( $query_args, 'order_excludes' );
}
/**
* Returns comma separated ids of included users, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_included_users( $query_args ) {
return $this->get_filtered_ids( $query_args, 'user_includes' );
}
/**
* Returns comma separated ids of excluded users, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_excluded_users( $query_args ) {
return $this->get_filtered_ids( $query_args, 'user_excludes' );
}
/**
* Returns order status subquery to be used in WHERE SQL query, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @param string $operator AND or OR, based on match query argument.
* @return string
*/
protected function get_status_subquery( $query_args, $operator = 'AND' ) {
global $wpdb;
$subqueries = array();
$excluded_statuses = array();
if ( isset( $query_args['status_is'] ) && is_array( $query_args['status_is'] ) && count( $query_args['status_is'] ) > 0 ) {
$allowed_statuses = array_map( array( $this, 'normalize_order_status' ), esc_sql( $query_args['status_is'] ) );
if ( $allowed_statuses ) {
$subqueries[] = "{$wpdb->prefix}wc_order_stats.status IN ( '" . implode( "','", $allowed_statuses ) . "' )";
}
}
if ( isset( $query_args['status_is_not'] ) && is_array( $query_args['status_is_not'] ) && count( $query_args['status_is_not'] ) > 0 ) {
$excluded_statuses = array_map( array( $this, 'normalize_order_status' ), $query_args['status_is_not'] );
}
if ( ( ! isset( $query_args['status_is'] ) || empty( $query_args['status_is'] ) )
&& ( ! isset( $query_args['status_is_not'] ) || empty( $query_args['status_is_not'] ) )
) {
$excluded_statuses = array_map( array( $this, 'normalize_order_status' ), $this->get_excluded_report_order_statuses() );
}
if ( $excluded_statuses ) {
$subqueries[] = "{$wpdb->prefix}wc_order_stats.status NOT IN ( '" . implode( "','", $excluded_statuses ) . "' )";
}
return implode( " $operator ", $subqueries );
}
/**
* Add order status SQL clauses if included in query.
*
* @param array $query_args Parameters supplied by the user.
* @param string $table_name Database table name.
* @param SqlQuery $sql_query Query object.
*/
protected function add_order_status_clause( $query_args, $table_name, &$sql_query ) {
global $wpdb;
$order_status_filter = $this->get_status_subquery( $query_args );
if ( $order_status_filter ) {
$sql_query->add_sql_clause( 'join', "JOIN {$wpdb->prefix}wc_order_stats ON {$table_name}.order_id = {$wpdb->prefix}wc_order_stats.order_id" );
$sql_query->add_sql_clause( 'where', "AND ( {$order_status_filter} )" );
}
}
/**
* Add order by SQL clause if included in query.
*
* @param array $query_args Parameters supplied by the user.
* @param SqlQuery $sql_query Query object.
* @return string Order by clause.
*/
protected function add_order_by_clause( $query_args, &$sql_query ) {
$order_by_clause = '';
$sql_query->clear_sql_clause( array( 'order_by' ) );
if ( isset( $query_args['orderby'] ) ) {
$order_by_clause = $this->normalize_order_by( esc_sql( $query_args['orderby'] ) );
$sql_query->add_sql_clause( 'order_by', $order_by_clause );
}
// Return ORDER BY clause to allow adding the sort field(s) to query via a JOIN.
return $order_by_clause;
}
/**
* Add order by order SQL clause.
*
* @param array $query_args Parameters supplied by the user.
* @param SqlQuery $sql_query Query object.
*/
protected function add_orderby_order_clause( $query_args, &$sql_query ) {
if ( isset( $query_args['order'] ) ) {
$sql_query->add_sql_clause( 'order_by', esc_sql( $query_args['order'] ) );
} else {
$sql_query->add_sql_clause( 'order_by', 'DESC' );
}
}
/**
* Returns customer subquery to be used in WHERE SQL query, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_customer_subquery( $query_args ) {
global $wpdb;
$customer_filter = '';
if ( isset( $query_args['customer_type'] ) ) {
if ( 'new' === strtolower( $query_args['customer_type'] ) ) {
$customer_filter = " {$wpdb->prefix}wc_order_stats.returning_customer = 0";
} elseif ( 'returning' === strtolower( $query_args['customer_type'] ) ) {
$customer_filter = " {$wpdb->prefix}wc_order_stats.returning_customer = 1";
}
}
return $customer_filter;
}
/**
* Returns product attribute subquery elements used in JOIN and WHERE clauses,
* based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return array
*/
protected function get_attribute_subqueries( $query_args ) {
global $wpdb;
$sql_clauses = array(
'join' => array(),
'where' => array(),
);
$match_operator = $this->get_match_operator( $query_args );
$post_meta_comparators = array(
'=' => 'attribute_is',
'!=' => 'attribute_is_not',
);
foreach ( $post_meta_comparators as $comparator => $arg ) {
if ( ! isset( $query_args[ $arg ] ) || ! is_array( $query_args[ $arg ] ) ) {
continue;
}
foreach ( $query_args[ $arg ] as $attribute_term ) {
// We expect tuples.
if ( ! is_array( $attribute_term ) || 2 !== count( $attribute_term ) ) {
continue;
}
// If the tuple is numeric, assume these are IDs.
if ( is_numeric( $attribute_term[0] ) && is_numeric( $attribute_term[1] ) ) {
$attribute_id = intval( $attribute_term[0] );
$term_id = intval( $attribute_term[1] );
// Invalid IDs.
if ( 0 === $attribute_id || 0 === $term_id ) {
continue;
}
// @todo: Use wc_get_attribute () instead ?
$attr_taxonomy = wc_attribute_taxonomy_name_by_id( $attribute_id );
// Invalid attribute ID.
if ( empty( $attr_taxonomy ) ) {
continue;
}
$attr_term = get_term_by( 'id', $term_id, $attr_taxonomy );
// Invalid term ID.
if ( false === $attr_term ) {
continue;
}
$meta_key = sanitize_title( $attr_taxonomy );
$meta_value = $attr_term->slug;
} else {
// Assume these are a custom attribute slug/value pair.
$meta_key = esc_sql( $attribute_term[0] );
$meta_value = esc_sql( $attribute_term[1] );
}
$join_alias = 'orderitemmeta1';
$table_to_join_on = "{$wpdb->prefix}wc_order_product_lookup";
if ( empty( $sql_clauses['join'] ) ) {
$sql_clauses['join'][] = "JOIN {$wpdb->prefix}woocommerce_order_items orderitems ON orderitems.order_id = {$table_to_join_on}.order_id";
}
// If we're matching all filters (AND), we'll need multiple JOINs on postmeta.
// If not, just one.
if ( 'AND' === $match_operator || 1 === count( $sql_clauses['join'] ) ) {
$join_idx = count( $sql_clauses['join'] );
$join_alias = 'orderitemmeta' . $join_idx;
$sql_clauses['join'][] = "JOIN {$wpdb->prefix}woocommerce_order_itemmeta as {$join_alias} ON {$join_alias}.order_item_id = {$table_to_join_on}.order_item_id";
}
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$sql_clauses['where'][] = $wpdb->prepare( "( {$join_alias}.meta_key = %s AND {$join_alias}.meta_value {$comparator} %s )", $meta_key, $meta_value );
}
}
// If we're matching multiple attributes and all filters (AND), make sure
// we're matching attributes on the same product.
$num_attribute_filters = count( $sql_clauses['join'] );
for ( $i = 2; $i < $num_attribute_filters; $i++ ) {
$join_alias = 'orderitemmeta' . $i;
$sql_clauses['join'][] = "AND orderitemmeta1.order_item_id = {$join_alias}.order_item_id";
}
return $sql_clauses;
}
/**
* Returns logic operator for WHERE subclause based on 'match' query argument.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_match_operator( $query_args ) {
$operator = 'AND';
if ( ! isset( $query_args['match'] ) ) {
return $operator;
}
if ( 'all' === strtolower( $query_args['match'] ) ) {
$operator = 'AND';
} elseif ( 'any' === strtolower( $query_args['match'] ) ) {
$operator = 'OR';
}
return $operator;
}
/**
* Returns filtered comma separated ids, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @param string $field Query field to filter.
* @param string $separator Field separator.
* @return string
*/
protected function get_filtered_ids( $query_args, $field, $separator = ',' ) {
global $wpdb;
$ids_str = '';
$ids = isset( $query_args[ $field ] ) && is_array( $query_args[ $field ] ) ? $query_args[ $field ] : array();
/**
* Filter the IDs before retrieving report data.
*
* Allows filtering of the objects included or excluded from reports.
*
* @param array $ids List of object Ids.
* @param array $query_args The original arguments for the request.
* @param string $field The object type.
* @param string $context The data store context.
*/
$ids = apply_filters( 'woocommerce_analytics_' . $field, $ids, $query_args, $field, $this->context );
if ( ! empty( $ids ) ) {
$placeholders = implode( $separator, array_fill( 0, count( $ids ), '%d' ) );
/* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
$ids_str = $wpdb->prepare( "{$placeholders}", $ids );
/* phpcs:enable */
}
return $ids_str;
}
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {}
}
API/Reports/DataStoreInterface.php 0000644 00000000621 15153746747 0013055 0 ustar 00 <?php
/**
* Reports Data Store Interface
*/
namespace Automattic\WooCommerce\Admin\API\Reports;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WooCommerce Reports data store interface.
*
* @since 3.5.0
*/
interface DataStoreInterface {
/**
* Get the data based on args.
*
* @param array $args Query parameters.
* @return stdClass|WP_Error
*/
public function get_data( $args );
}
API/Reports/Downloads/Controller.php 0000644 00000033675 15153746747 0013442 0 ustar 00 <?php
/**
* REST API Reports downloads controller
*
* Handles requests to the /reports/downloads endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Controller as ReportsController;
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
/**
* REST API Reports downloads controller class.
*
* @internal
* @extends Automattic\WooCommerce\Admin\API\Reports\Controller
*/
class Controller extends ReportsController implements ExportableInterface {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/downloads';
/**
* Get items.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$args = array();
$registered = array_keys( $this->get_collection_params() );
foreach ( $registered as $param_name ) {
if ( isset( $request[ $param_name ] ) ) {
$args[ $param_name ] = $request[ $param_name ];
}
}
$reports = new Query( $args );
$downloads_data = $reports->get_data();
$data = array();
foreach ( $downloads_data->data as $download_data ) {
$item = $this->prepare_item_for_response( $download_data, $request );
$data[] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$data,
(int) $downloads_data->total,
(int) $downloads_data->page_no,
(int) $downloads_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param Array $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$data = $report;
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $report ) );
$response->data['date'] = get_date_from_gmt( $data['date_gmt'], 'Y-m-d H:i:s' );
// Figure out file name.
// Matches https://github.com/woocommerce/woocommerce/blob/4be0018c092e617c5d2b8c46b800eb71ece9ddef/includes/class-wc-download-handler.php#L197.
$product_id = intval( $data['product_id'] );
$_product = wc_get_product( $product_id );
// Make sure the product hasn't been deleted.
if ( $_product ) {
$file_path = $_product->get_file_download_path( $data['download_id'] );
$filename = basename( $file_path );
$response->data['file_name'] = apply_filters( 'woocommerce_file_download_filename', $filename, $product_id );
$response->data['file_path'] = $file_path;
} else {
$response->data['file_name'] = '';
$response->data['file_path'] = '';
}
$customer = new \WC_Customer( $data['user_id'] );
$response->data['username'] = $customer->get_username();
$response->data['order_number'] = $this->get_order_number( $data['order_id'] );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_downloads', $response, $report, $request );
}
/**
* Prepare links for the request.
*
* @param Array $object Object data.
* @return array Links for the given post.
*/
protected function prepare_links( $object ) {
$links = array(
'product' => array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, 'products', $object['product_id'] ) ),
'embeddable' => true,
),
);
return $links;
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_downloads',
'type' => 'object',
'properties' => array(
'id' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'ID.', 'woocommerce' ),
),
'product_id' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product ID.', 'woocommerce' ),
),
'date' => array(
'description' => __( "The date of the download, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_gmt' => array(
'description' => __( 'The date of the download, as GMT.', 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'download_id' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Download ID.', 'woocommerce' ),
),
'file_name' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'File name.', 'woocommerce' ),
),
'file_path' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'File URL.', 'woocommerce' ),
),
'order_id' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Order ID.', 'woocommerce' ),
),
'order_number' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Order Number.', 'woocommerce' ),
),
'user_id' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'User ID for the downloader.', 'woocommerce' ),
),
'username' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'User name of the downloader.', 'woocommerce' ),
),
'ip_address' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'IP address for the downloader.', 'woocommerce' ),
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['page'] = array(
'description' => __( 'Current page of the collection.', 'woocommerce' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);
$params['per_page'] = array(
'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['after'] = array(
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['before'] = array(
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
'validate_callback' => 'rest_validate_request_arg',
);
$params['orderby'] = array(
'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
'type' => 'string',
'default' => 'date',
'enum' => array(
'date',
'product',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['match'] = array(
'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: products, orders, username, ip_address.', 'woocommerce' ),
'type' => 'string',
'default' => 'all',
'enum' => array(
'all',
'any',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['product_includes'] = array(
'description' => __( 'Limit result set to items that have the specified product(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
);
$params['product_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified product(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'wp_parse_id_list',
);
$params['order_includes'] = array(
'description' => __( 'Limit result set to items that have the specified order ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['order_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified order ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['customer_includes'] = array(
'description' => __( 'Limit response to objects that have the specified user ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['customer_excludes'] = array(
'description' => __( 'Limit response to objects that don\'t have the specified user ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['ip_address_includes'] = array(
'description' => __( 'Limit response to objects that have a specified ip address.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
$params['ip_address_excludes'] = array(
'description' => __( 'Limit response to objects that don\'t have a specified ip address.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
$params['force_cache_refresh'] = array(
'description' => __( 'Force retrieval of fresh data instead of from the cache.', 'woocommerce' ),
'type' => 'boolean',
'sanitize_callback' => 'wp_validate_boolean',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get the column names for export.
*
* @return array Key value pair of Column ID => Label.
*/
public function get_export_columns() {
$export_columns = array(
'date' => __( 'Date', 'woocommerce' ),
'product' => __( 'Product title', 'woocommerce' ),
'file_name' => __( 'File name', 'woocommerce' ),
'order_number' => __( 'Order #', 'woocommerce' ),
'user_id' => __( 'User Name', 'woocommerce' ),
'ip_address' => __( 'IP', 'woocommerce' ),
);
/**
* Filter to add or remove column names from the downloads report for
* export.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_filter_downloads_export_columns',
$export_columns
);
}
/**
* Get the column values for export.
*
* @param array $item Single report item/row.
* @return array Key value pair of Column ID => Row Value.
*/
public function prepare_item_for_export( $item ) {
$export_item = array(
'date' => $item['date'],
'product' => $item['_embedded']['product'][0]['name'],
'file_name' => $item['file_name'],
'order_number' => $item['order_number'],
'user_id' => $item['username'],
'ip_address' => $item['ip_address'],
);
/**
* Filter to prepare extra columns in the export item for the downloads
* report.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_report_downloads_prepare_export_item',
$export_item,
$item
);
}
}
API/Reports/Downloads/DataStore.php 0000644 00000030623 15153746747 0013173 0 ustar 00 <?php
/**
* API\Reports\Downloads\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
/**
* API\Reports\Downloads\DataStore.
*/
class DataStore extends ReportsDataStore implements DataStoreInterface {
/**
* Table used to get the data.
*
* @var string
*/
protected static $table_name = 'wc_download_log';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'downloads';
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'id' => 'intval',
'date' => 'strval',
'date_gmt' => 'strval',
'download_id' => 'strval', // String because this can sometimes be a hash.
'file_name' => 'strval',
'product_id' => 'intval',
'order_id' => 'intval',
'user_id' => 'intval',
'ip_address' => 'strval',
);
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'downloads';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$this->report_columns = array(
'id' => 'download_log_id as id',
'date' => 'timestamp as date_gmt',
'download_id' => 'product_permissions.download_id',
'product_id' => 'product_permissions.product_id',
'order_id' => 'product_permissions.order_id',
'user_id' => 'product_permissions.user_id',
'ip_address' => 'user_ip_address as ip_address',
);
}
/**
* Updates the database query with parameters used for downloads report.
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function add_sql_query_params( $query_args ) {
global $wpdb;
$lookup_table = self::get_db_table_name();
$permission_table = $wpdb->prefix . 'woocommerce_downloadable_product_permissions';
$operator = $this->get_match_operator( $query_args );
$where_filters = array();
$join = "JOIN {$permission_table} as product_permissions ON {$lookup_table}.permission_id = product_permissions.permission_id";
$where_time = $this->add_time_period_sql_params( $query_args, $lookup_table );
if ( $where_time ) {
if ( isset( $this->subquery ) ) {
$this->subquery->add_sql_clause( 'where_time', $where_time );
} else {
$this->interval_query->add_sql_clause( 'where_time', $where_time );
}
}
$this->get_limit_sql_params( $query_args );
$where_filters[] = $this->get_object_where_filter(
$lookup_table,
'permission_id',
$permission_table,
'product_id',
'IN',
$this->get_included_products( $query_args )
);
$where_filters[] = $this->get_object_where_filter(
$lookup_table,
'permission_id',
$permission_table,
'product_id',
'NOT IN',
$this->get_excluded_products( $query_args )
);
$where_filters[] = $this->get_object_where_filter(
$lookup_table,
'permission_id',
$permission_table,
'order_id',
'IN',
$this->get_included_orders( $query_args )
);
$where_filters[] = $this->get_object_where_filter(
$lookup_table,
'permission_id',
$permission_table,
'order_id',
'NOT IN',
$this->get_excluded_orders( $query_args )
);
$customer_lookup_table = $wpdb->prefix . 'wc_customer_lookup';
$customer_lookup = "SELECT {$customer_lookup_table}.user_id FROM {$customer_lookup_table} WHERE {$customer_lookup_table}.customer_id IN (%s)";
$included_customers = $this->get_included_customers( $query_args );
$excluded_customers = $this->get_excluded_customers( $query_args );
if ( $included_customers ) {
$where_filters[] = $this->get_object_where_filter(
$lookup_table,
'permission_id',
$permission_table,
'user_id',
'IN',
sprintf( $customer_lookup, $included_customers )
);
}
if ( $excluded_customers ) {
$where_filters[] = $this->get_object_where_filter(
$lookup_table,
'permission_id',
$permission_table,
'user_id',
'NOT IN',
sprintf( $customer_lookup, $excluded_customers )
);
}
$included_ip_addresses = $this->get_included_ip_addresses( $query_args );
$excluded_ip_addresses = $this->get_excluded_ip_addresses( $query_args );
if ( $included_ip_addresses ) {
$where_filters[] = "{$lookup_table}.user_ip_address IN ('{$included_ip_addresses}')";
}
if ( $excluded_ip_addresses ) {
$where_filters[] = "{$lookup_table}.user_ip_address NOT IN ('{$excluded_ip_addresses}')";
}
$where_filters = array_filter( $where_filters );
$where_subclause = implode( " $operator ", $where_filters );
if ( $where_subclause ) {
if ( isset( $this->subquery ) ) {
$this->subquery->add_sql_clause( 'where', "AND ( $where_subclause )" );
} else {
$this->interval_query->add_sql_clause( 'where', "AND ( $where_subclause )" );
}
}
if ( isset( $this->subquery ) ) {
$this->subquery->add_sql_clause( 'join', $join );
} else {
$this->interval_query->add_sql_clause( 'join', $join );
}
$this->add_order_by( $query_args );
}
/**
* Returns comma separated ids of included ip address, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_included_ip_addresses( $query_args ) {
return $this->get_filtered_ip_addresses( $query_args, 'ip_address_includes' );
}
/**
* Returns comma separated ids of excluded ip address, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_excluded_ip_addresses( $query_args ) {
return $this->get_filtered_ip_addresses( $query_args, 'ip_address_excludes' );
}
/**
* Returns filtered comma separated ids, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @param string $field Query field to filter.
* @return string
*/
protected function get_filtered_ip_addresses( $query_args, $field ) {
if ( isset( $query_args[ $field ] ) && is_array( $query_args[ $field ] ) && count( $query_args[ $field ] ) > 0 ) {
$ip_addresses = array_map( 'esc_sql', $query_args[ $field ] );
/**
* Filter the IDs before retrieving report data.
*
* Allows filtering of the objects included or excluded from reports.
*
* @param array $ids List of object Ids.
* @param array $query_args The original arguments for the request.
* @param string $field The object type.
* @param string $context The data store context.
*/
$ip_addresses = apply_filters( 'woocommerce_analytics_' . $field, $ip_addresses, $query_args, $field, $this->context );
return implode( "','", $ip_addresses );
}
return '';
}
/**
* Returns comma separated ids of included customers, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_included_customers( $query_args ) {
return self::get_filtered_ids( $query_args, 'customer_includes' );
}
/**
* Returns comma separated ids of excluded customers, based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return string
*/
protected function get_excluded_customers( $query_args ) {
return self::get_filtered_ids( $query_args, 'customer_excludes' );
}
/**
* Gets WHERE time clause of SQL request with date-related constraints.
*
* @param array $query_args Parameters supplied by the user.
* @param string $table_name Name of the db table relevant for the date constraint.
* @return string
*/
protected function add_time_period_sql_params( $query_args, $table_name ) {
$where_time = '';
if ( $query_args['before'] ) {
$datetime_str = $query_args['before']->format( TimeInterval::$sql_datetime_format );
$where_time .= " AND {$table_name}.timestamp <= '$datetime_str'";
}
if ( $query_args['after'] ) {
$datetime_str = $query_args['after']->format( TimeInterval::$sql_datetime_format );
$where_time .= " AND {$table_name}.timestamp >= '$datetime_str'";
}
return $where_time;
}
/**
* Fills ORDER BY clause of SQL request based on user supplied parameters.
*
* @param array $query_args Parameters supplied by the user.
*/
protected function add_order_by( $query_args ) {
global $wpdb;
$this->clear_sql_clause( 'order_by' );
$order_by = '';
if ( isset( $query_args['orderby'] ) ) {
$order_by = $this->normalize_order_by( esc_sql( $query_args['orderby'] ) );
$this->add_sql_clause( 'order_by', $order_by );
}
if ( false !== strpos( $order_by, '_products' ) ) {
$this->subquery->add_sql_clause( 'join', "JOIN {$wpdb->posts} AS _products ON product_permissions.product_id = _products.ID" );
}
$this->add_orderby_order_clause( $query_args, $this );
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$table_name = self::get_db_table_name();
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'timestamp',
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
'fields' => '*',
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$data = (object) array(
'data' => array(),
'total' => 0,
'pages' => 0,
'page_no' => 0,
);
$selections = $this->selected_columns( $query_args );
$this->add_sql_query_params( $query_args );
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$db_records_count = (int) $wpdb->get_var(
"SELECT COUNT(*) FROM (
{$this->subquery->get_query_statement()}
) AS tt"
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$params = $this->get_limit_params( $query_args );
$total_pages = (int) ceil( $db_records_count / $params['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return $data;
}
$this->subquery->clear_sql_clause( 'select' );
$this->subquery->add_sql_clause( 'select', $selections );
$this->subquery->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$this->subquery->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
$download_data = $wpdb->get_results(
$this->subquery->get_query_statement(), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
ARRAY_A
);
if ( null === $download_data ) {
return $data;
}
$download_data = array_map( array( $this, 'cast_numbers' ), $download_data );
$data = (object) array(
'data' => $download_data,
'total' => $db_records_count,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Maps ordering specified by the user to columns in the database/fields in the data.
*
* @param string $order_by Sorting criterion.
* @return string
*/
protected function normalize_order_by( $order_by ) {
global $wpdb;
if ( 'date' === $order_by ) {
return $wpdb->prefix . 'wc_download_log.timestamp';
}
if ( 'product' === $order_by ) {
return '_products.post_title';
}
return $order_by;
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
$table_name = self::get_db_table_name();
$this->subquery = new SqlQuery( $this->context . '_subquery' );
$this->subquery->add_sql_clause( 'from', $table_name );
$this->subquery->add_sql_clause( 'select', "{$table_name}.download_log_id" );
$this->subquery->add_sql_clause( 'group_by', "{$table_name}.download_log_id" );
}
}
API/Reports/Downloads/Files/Controller.php 0000644 00000001122 15153746747 0014462 0 ustar 00 <?php
/**
* REST API Reports downloads files controller
*
* Handles requests to the /reports/downloads/files endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Files;
defined( 'ABSPATH' ) || exit;
/**
* REST API Reports downloads files controller class.
*
* @internal
* @extends WC_REST_Reports_Controller
*/
class Controller extends \WC_REST_Reports_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/downloads/files';
}
API/Reports/Downloads/Query.php 0000644 00000002260 15153746747 0012406 0 ustar 00 <?php
/**
* Class for parameter-based downloads report querying.
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'page' => 2,
* 'products' => array(1,2,3)
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Downloads\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Downloads\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for downloads report.
*
* @return array
*/
protected function get_default_query_vars() {
return array();
}
/**
* Get downloads data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_downloads_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-downloads' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_downloads_select_query', $results, $args );
}
}
API/Reports/Downloads/Stats/Controller.php 0000644 00000024630 15153746747 0014527 0 ustar 00 <?php
/**
* REST API Reports downloads stats controller
*
* Handles requests to the /reports/downloads/stats endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericStatsController;
use WP_REST_Request;
use WP_REST_Response;
/**
* REST API Reports downloads stats controller class.
*
* @internal
* @extends GenericStatsController
*/
class Controller extends GenericStatsController {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/downloads/stats';
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['before'] = $request['before'];
$args['after'] = $request['after'];
$args['interval'] = $request['interval'];
$args['page'] = $request['page'];
$args['per_page'] = $request['per_page'];
$args['orderby'] = $request['orderby'];
$args['order'] = $request['order'];
$args['match'] = $request['match'];
$args['product_includes'] = (array) $request['product_includes'];
$args['product_excludes'] = (array) $request['product_excludes'];
$args['customer_includes'] = (array) $request['customer_includes'];
$args['customer_excludes'] = (array) $request['customer_excludes'];
$args['order_includes'] = (array) $request['order_includes'];
$args['order_excludes'] = (array) $request['order_excludes'];
$args['ip_address_includes'] = (array) $request['ip_address_includes'];
$args['ip_address_excludes'] = (array) $request['ip_address_excludes'];
$args['fields'] = $request['fields'];
$args['force_cache_refresh'] = $request['force_cache_refresh'];
return $args;
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$downloads_query = new Query( $query_args );
$report_data = $downloads_query->get_data();
$out_data = array(
'totals' => get_object_vars( $report_data->totals ),
'intervals' => array(),
);
foreach ( $report_data->intervals as $interval_data ) {
$item = $this->prepare_item_for_response( $interval_data, $request );
$out_data['intervals'][] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$out_data,
(int) $report_data->total,
(int) $report_data->page_no,
(int) $report_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param array $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$response = parent::prepare_item_for_response( $report, $request );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_downloads_stats', $response, $report, $request );
}
/**
* Get the Report's item properties schema.
* Will be used by `get_item_schema` as `totals` and `subtotals`.
*
* @return array
*/
protected function get_item_properties_schema() {
return array(
'download_count' => array(
'title' => __( 'Downloads', 'woocommerce' ),
'description' => __( 'Number of downloads.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
),
);
}
/**
* Get the Report's schema, conforming to JSON Schema.
* It does not have the segments as in GenericStatsController.
*
* @return array
*/
public function get_item_schema() {
$totals = $this->get_item_properties_schema();
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_orders_stats',
'type' => 'object',
'properties' => array(
'totals' => array(
'description' => __( 'Totals data.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => $totals,
),
'intervals' => array(
'description' => __( 'Reports data grouped by intervals.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'items' => array(
'type' => 'object',
'properties' => array(
'interval' => array(
'description' => __( 'Type of interval.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'enum' => array( 'day', 'week', 'month', 'year' ),
),
'date_start' => array(
'description' => __( "The date the report start, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_start_gmt' => array(
'description' => __( 'The date the report start, as GMT.', 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_end' => array(
'description' => __( "The date the report end, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_end_gmt' => array(
'description' => __( 'The date the report end, as GMT.', 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'subtotals' => array(
'description' => __( 'Interval subtotals.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => $totals,
),
),
),
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['orderby']['enum'] = array(
'date',
'download_count',
);
$params['match'] = array(
'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'woocommerce' ),
'type' => 'string',
'default' => 'all',
'enum' => array(
'all',
'any',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['product_includes'] = array(
'description' => __( 'Limit result set to items that have the specified product(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
);
$params['product_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified product(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
);
$params['order_includes'] = array(
'description' => __( 'Limit result set to items that have the specified order ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['order_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified order ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['customer_includes'] = array(
'description' => __( 'Limit response to objects that have the specified customer ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['customer_excludes'] = array(
'description' => __( 'Limit response to objects that don\'t have the specified customer ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['ip_address_includes'] = array(
'description' => __( 'Limit response to objects that have a specified ip address.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
$params['ip_address_excludes'] = array(
'description' => __( 'Limit response to objects that don\'t have a specified ip address.', 'woocommerce' ),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
$params['fields'] = array(
'description' => __( 'Limit stats fields to the specified items.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
return $params;
}
}
API/Reports/Downloads/Stats/DataStore.php 0000644 00000015317 15153746747 0014274 0 ustar 00 <?php
/**
* API\Reports\Downloads\Stats\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Downloads\DataStore as DownloadsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
/**
* API\Reports\Downloads\Stats\DataStore.
*/
class DataStore extends DownloadsDataStore implements DataStoreInterface {
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'download_count' => 'intval',
);
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'downloads_stats';
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'downloads_stats';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$this->report_columns = array(
'download_count' => 'COUNT(DISTINCT download_log_id) as download_count',
);
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$table_name = self::get_db_table_name();
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'fields' => '*',
'interval' => 'week',
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$selections = $this->selected_columns( $query_args );
$this->add_sql_query_params( $query_args );
$where_time = $this->add_time_period_sql_params( $query_args, $table_name );
$this->add_intervals_sql_params( $query_args, $table_name );
$this->interval_query->add_sql_clause( 'select', $this->get_sql_clause( 'select' ) . ' AS time_interval' );
$this->interval_query->str_replace_clause( 'select', 'date_created', 'timestamp' );
$this->interval_query->str_replace_clause( 'where_time', 'date_created', 'timestamp' );
$db_intervals = $wpdb->get_col(
$this->interval_query->get_query_statement()
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
$db_records_count = count( $db_intervals );
$params = $this->get_limit_params( $query_args );
$expected_interval_count = TimeInterval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $params['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return array();
}
$this->update_intervals_sql_params( $query_args, $db_records_count, $expected_interval_count, $table_name );
$this->interval_query->str_replace_clause( 'where_time', 'date_created', 'timestamp' );
$this->total_query->add_sql_clause( 'select', $selections );
$this->total_query->add_sql_clause( 'where', $this->interval_query->get_sql_clause( 'where' ) );
if ( $where_time ) {
$this->total_query->add_sql_clause( 'where_time', $where_time );
}
$totals = $wpdb->get_results(
$this->total_query->get_query_statement(),
ARRAY_A
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
if ( null === $totals ) {
return new \WP_Error( 'woocommerce_analytics_downloads_stats_result_failed', __( 'Sorry, fetching downloads data failed.', 'woocommerce' ) );
}
$this->interval_query->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$this->interval_query->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
$this->interval_query->add_sql_clause( 'select', ', MAX(timestamp) AS datetime_anchor' );
if ( '' !== $selections ) {
$this->interval_query->add_sql_clause( 'select', ', ' . $selections );
}
$intervals = $wpdb->get_results(
$this->interval_query->get_query_statement(),
ARRAY_A
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
if ( null === $intervals ) {
return new \WP_Error( 'woocommerce_analytics_downloads_stats_result_failed', __( 'Sorry, fetching downloads data failed.', 'woocommerce' ) );
}
$totals = (object) $this->cast_numbers( $totals[0] );
$data = (object) array(
'totals' => $totals,
'intervals' => $intervals,
'total' => $expected_interval_count,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
if ( $this->intervals_missing( $expected_interval_count, $db_records_count, $params['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
$this->remove_extra_records( $data, $query_args['page'], $params['per_page'], $db_records_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] );
} else {
$this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals );
}
$this->create_interval_subtotals( $data->intervals );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Normalizes order_by clause to match to SQL query.
*
* @param string $order_by Order by option requeste by user.
* @return string
*/
protected function normalize_order_by( $order_by ) {
if ( 'date' === $order_by ) {
return 'time_interval';
}
return $order_by;
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
unset( $this->subquery );
$this->total_query = new SqlQuery( $this->context . '_total' );
$this->total_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query = new SqlQuery( $this->context . '_interval' );
$this->interval_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query->add_sql_clause( 'group_by', 'time_interval' );
}
}
API/Reports/Downloads/Stats/Query.php 0000644 00000001577 15153746747 0013516 0 ustar 00 <?php
/**
* Class for parameter-based downloads Reports querying
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Downloads\Stats\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Orders report.
*
* @return array
*/
protected function get_default_query_vars() {
return array();
}
/**
* Get revenue data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_downloads_stats_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-downloads-stats' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_downloads_stats_select_query', $results, $args );
}
}
API/Reports/Export/Controller.php 0000644 00000015431 15153746747 0012757 0 ustar 00 <?php
/**
* REST API Reports Export Controller
*
* Handles requests to:
* - /reports/[report]/export
* - /reports/[report]/export/[id]/status
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Export;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\ReportExporter;
/**
* Reports Export controller.
*
* @internal
* @extends \Automattic\WooCommerce\Admin\API\Reports\Controller
*/
class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/(?P<type>[a-z]+)/export';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'export_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_export_collection_params(),
),
'schema' => array( $this, 'get_export_public_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<export_id>[a-z0-9]+)/status',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'export_status' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_export_status_public_schema' ),
)
);
}
/**
* Get the query params for collections.
*
* @return array
*/
protected function get_export_collection_params() {
$params = array();
$params['report_args'] = array(
'description' => __( 'Parameters to pass on to the exported report.', 'woocommerce' ),
'type' => 'object',
'validate_callback' => 'rest_validate_request_arg', // @todo: use each controller's schema?
);
$params['email'] = array(
'description' => __( 'When true, email a link to download the export to the requesting user.', 'woocommerce' ),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get the Report Export's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_export_public_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_export',
'type' => 'object',
'properties' => array(
'status' => array(
'description' => __( 'Export status.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'message' => array(
'description' => __( 'Export status message.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'export_id' => array(
'description' => __( 'Export ID.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the Export status schema, conforming to JSON Schema.
*
* @return array
*/
public function get_export_status_public_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_export_status',
'type' => 'object',
'properties' => array(
'percent_complete' => array(
'description' => __( 'Percentage complete.', 'woocommerce' ),
'type' => 'int',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'download_url' => array(
'description' => __( 'Export download URL.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Export data based on user request params.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function export_items( $request ) {
$report_type = $request['type'];
$report_args = empty( $request['report_args'] ) ? array() : $request['report_args'];
$send_email = isset( $request['email'] ) ? $request['email'] : false;
$default_export_id = str_replace( '.', '', microtime( true ) );
$export_id = apply_filters( 'woocommerce_admin_export_id', $default_export_id );
$export_id = (string) sanitize_file_name( $export_id );
$total_rows = ReportExporter::queue_report_export( $export_id, $report_type, $report_args, $send_email );
if ( 0 === $total_rows ) {
return rest_ensure_response(
array(
'message' => __( 'There is no data to export for the given request.', 'woocommerce' ),
)
);
}
ReportExporter::update_export_percentage_complete( $report_type, $export_id, 0 );
$response = rest_ensure_response(
array(
'message' => __( 'Your report file is being generated.', 'woocommerce' ),
'export_id' => $export_id,
)
);
// Include a link to the export status endpoint.
$response->add_links(
array(
'status' => array(
'href' => rest_url( sprintf( '%s/reports/%s/export/%s/status', $this->namespace, $report_type, $export_id ) ),
),
)
);
$data = $this->prepare_response_for_collection( $response );
return rest_ensure_response( $data );
}
/**
* Export status based on user request params.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function export_status( $request ) {
$report_type = $request['type'];
$export_id = $request['export_id'];
$percentage = ReportExporter::get_export_percentage_complete( $report_type, $export_id );
if ( false === $percentage ) {
return new \WP_Error(
'woocommerce_admin_reports_export_invalid_id',
__( 'Sorry, there is no export with that ID.', 'woocommerce' ),
array( 'status' => 404 )
);
}
$result = array(
'percent_complete' => $percentage,
);
// @todo - add thing in the links below instead?
if ( 100 === $percentage ) {
$query_args = array(
'action' => ReportExporter::DOWNLOAD_EXPORT_ACTION,
'filename' => "wc-{$report_type}-report-export-{$export_id}",
);
$result['download_url'] = add_query_arg( $query_args, admin_url() );
}
// Wrap the data in a response object.
$response = rest_ensure_response( $result );
// Include a link to the export status endpoint.
$response->add_links(
array(
'self' => array(
'href' => rest_url( sprintf( '%s/reports/%s/export/%s/status', $this->namespace, $report_type, $export_id ) ),
),
)
);
$data = $this->prepare_response_for_collection( $response );
return rest_ensure_response( $data );
}
}
API/Reports/ExportableInterface.php 0000644 00000001160 15153746747 0013273 0 ustar 00 <?php
/**
* Reports Exportable Controller Interface
*/
namespace Automattic\WooCommerce\Admin\API\Reports;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WooCommerce Reports exportable controller interface.
*
* @since 3.5.0
*/
interface ExportableInterface {
/**
* Get the column names for export.
*
* @return array Key value pair of Column ID => Label.
*/
public function get_export_columns();
/**
* Get the column values for export.
*
* @param array $item Single report item/row.
* @return array Key value pair of Column ID => Value.
*/
public function prepare_item_for_export( $item );
}
API/Reports/ExportableTraits.php 0000644 00000001160 15153746747 0012641 0 ustar 00 <?php
/**
* REST API Reports exportable traits
*
* Collection of utility methods for exportable reports.
*/
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit;
/**
* ExportableTraits class.
*/
trait ExportableTraits {
/**
* Format numbers for CSV using store precision setting.
*
* @param string|float $value Numeric value.
* @return string Formatted value.
*/
public static function csv_number_format( $value ) {
$decimals = wc_get_price_decimals();
// See: @woocommerce/currency: getCurrencyFormatDecimal().
return number_format( $value, $decimals, '.', '' );
}
}
API/Reports/GenericController.php 0000644 00000011211 15153746747 0012763 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit;
use WP_REST_Request;
use WP_REST_Response;
/**
* WC REST API Reports controller extended
* to be shared as a generic base for all Analytics controllers.
*
* @internal
* @extends WC_REST_Reports_Controller
*/
abstract class GenericController extends \WC_REST_Reports_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Add pagination headers and links.
*
* @param WP_REST_Request $request Request data.
* @param WP_REST_Response|array $response Response data.
* @param int $total Total results.
* @param int $page Current page.
* @param int $max_pages Total amount of pages.
* @return WP_REST_Response
*/
public function add_pagination_headers( $request, $response, int $total, int $page, int $max_pages ) {
$response = rest_ensure_response( $response );
$response->header( 'X-WP-Total', $total );
$response->header( 'X-WP-TotalPages', $max_pages );
$base = add_query_arg(
$request->get_query_params(),
rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) )
);
if ( $page > 1 ) {
$prev_page = $page - 1;
if ( $prev_page > $max_pages ) {
$prev_page = $max_pages;
}
$prev_link = add_query_arg( 'page', $prev_page, $base );
$response->link_header( 'prev', $prev_link );
}
if ( $max_pages > $page ) {
$next_page = $page + 1;
$next_link = add_query_arg( 'page', $next_page, $base );
$response->link_header( 'next', $next_link );
}
return $response;
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['page'] = array(
'description' => __( 'Current page of the collection.', 'woocommerce' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);
$params['per_page'] = array(
'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['after'] = array(
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['before'] = array(
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
'validate_callback' => 'rest_validate_request_arg',
);
$params['orderby'] = array(
'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
'type' => 'string',
'default' => 'date',
'enum' => array(
'date',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['force_cache_refresh'] = array(
'description' => __( 'Force retrieval of fresh data instead of from the cache.', 'woocommerce' ),
'type' => 'boolean',
'sanitize_callback' => 'wp_validate_boolean',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Prepare a report object for serialization.
*
* @param array $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$data = $report;
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
return rest_ensure_response( $data );
}
}
API/Reports/GenericStatsController.php 0000644 00000011245 15153746747 0014011 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericController;
/**
* Generic base for all Stats controllers.
*
* @internal
* @extends GenericController
*/
abstract class GenericStatsController extends GenericController {
/**
* Get the query params for collections.
* Adds intervals to the generic list.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['interval'] = array(
'description' => __( 'Time interval to use for buckets in the returned data.', 'woocommerce' ),
'type' => 'string',
'default' => 'week',
'enum' => array(
'hour',
'day',
'week',
'month',
'quarter',
'year',
),
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get the Report's item properties schema.
* Will be used by `get_item_schema` as `totals` and `subtotals`.
*
* @return array
*/
abstract protected function get_item_properties_schema();
/**
* Get the Report's schema, conforming to JSON Schema.
*
* Please note, it does not call add_additional_fields_schema,
* as you may want to update the `title` first.
*
* @return array
*/
public function get_item_schema() {
$data_values = $this->get_item_properties_schema();
$segments = array(
'segments' => array(
'description' => __( 'Reports data grouped by segment condition.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'items' => array(
'type' => 'object',
'properties' => array(
'segment_id' => array(
'description' => __( 'Segment identificator.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'subtotals' => array(
'description' => __( 'Interval subtotals.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => $data_values,
),
),
),
),
);
$totals = array_merge( $data_values, $segments );
return array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_stats',
'type' => 'object',
'properties' => array(
'totals' => array(
'description' => __( 'Totals data.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => $totals,
),
'intervals' => array(
'description' => __( 'Reports data grouped by intervals.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'items' => array(
'type' => 'object',
'properties' => array(
'interval' => array(
'description' => __( 'Type of interval.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'enum' => array( 'day', 'week', 'month', 'year' ),
),
'date_start' => array(
'description' => __( "The date the report start, in the site's timezone.", 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_start_gmt' => array(
'description' => __( 'The date the report start, as GMT.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_end' => array(
'description' => __( "The date the report end, in the site's timezone.", 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_end_gmt' => array(
'description' => __( 'The date the report end, as GMT.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'subtotals' => array(
'description' => __( 'Interval subtotals.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => $totals,
),
),
),
),
),
);
}
}
API/Reports/Import/Controller.php 0000644 00000021110 15153746747 0012737 0 ustar 00 <?php
/**
* REST API Reports Import Controller
*
* Handles requests to /reports/import
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Import;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\ReportsSync;
/**
* Reports Imports controller.
*
* @internal
* @extends \Automattic\WooCommerce\Admin\API\Reports\Controller
*/
class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/import';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'import_items' ),
'permission_callback' => array( $this, 'import_permissions_check' ),
'args' => $this->get_import_collection_params(),
),
'schema' => array( $this, 'get_import_public_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/cancel',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'cancel_import' ),
'permission_callback' => array( $this, 'import_permissions_check' ),
),
'schema' => array( $this, 'get_import_public_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/delete',
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'delete_imported_items' ),
'permission_callback' => array( $this, 'import_permissions_check' ),
),
'schema' => array( $this, 'get_import_public_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/status',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_import_status' ),
'permission_callback' => array( $this, 'import_permissions_check' ),
),
'schema' => array( $this, 'get_import_public_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/totals',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_import_totals' ),
'permission_callback' => array( $this, 'import_permissions_check' ),
'args' => $this->get_import_collection_params(),
),
'schema' => array( $this, 'get_import_public_schema' ),
)
);
}
/**
* Makes sure the current user has access to WRITE the settings APIs.
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|bool
*/
public function import_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Import data based on user request params.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function import_items( $request ) {
$query_args = $this->prepare_objects_query( $request );
$import = ReportsSync::regenerate_report_data( $query_args['days'], $query_args['skip_existing'] );
if ( is_wp_error( $import ) ) {
$result = array(
'status' => 'error',
'message' => $import->get_error_message(),
);
} else {
$result = array(
'status' => 'success',
'message' => $import,
);
}
$response = $this->prepare_item_for_response( $result, $request );
$data = $this->prepare_response_for_collection( $response );
return rest_ensure_response( $data );
}
/**
* Prepare request object as query args.
*
* @param WP_REST_Request $request Request data.
* @return array
*/
protected function prepare_objects_query( $request ) {
$args = array();
$args['skip_existing'] = $request['skip_existing'];
$args['days'] = $request['days'];
return $args;
}
/**
* Prepare the data object for response.
*
* @param object $item Data object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $item, $request ) {
$data = $this->add_additional_fields_to_object( $item, $request );
$data = $this->filter_response_by_context( $data, 'view' );
$response = rest_ensure_response( $data );
/**
* Filter the list returned from the API.
*
* @param WP_REST_Response $response The response object.
* @param array $item The original item.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_reports_import', $response, $item, $request );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_import_collection_params() {
$params = array();
$params['days'] = array(
'description' => __( 'Number of days to import.', 'woocommerce' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 0,
);
$params['skip_existing'] = array(
'description' => __( 'Skip importing existing order data.', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'sanitize_callback' => 'wc_string_to_bool',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_import_public_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_import',
'type' => 'object',
'properties' => array(
'status' => array(
'description' => __( 'Regeneration status.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'message' => array(
'description' => __( 'Regenerate data message.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Cancel all queued import actions.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function cancel_import( $request ) {
ReportsSync::clear_queued_actions();
$result = array(
'status' => 'success',
'message' => __( 'All pending and in-progress import actions have been cancelled.', 'woocommerce' ),
);
$response = $this->prepare_item_for_response( $result, $request );
$data = $this->prepare_response_for_collection( $response );
return rest_ensure_response( $data );
}
/**
* Delete all imported items.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function delete_imported_items( $request ) {
$delete = ReportsSync::delete_report_data();
if ( is_wp_error( $delete ) ) {
$result = array(
'status' => 'error',
'message' => $delete->get_error_message(),
);
} else {
$result = array(
'status' => 'success',
'message' => $delete,
);
}
$response = $this->prepare_item_for_response( $result, $request );
$data = $this->prepare_response_for_collection( $response );
return rest_ensure_response( $data );
}
/**
* Get the status of the current import.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_import_status( $request ) {
$result = ReportsSync::get_import_stats();
$response = $this->prepare_item_for_response( $result, $request );
$data = $this->prepare_response_for_collection( $response );
return rest_ensure_response( $data );
}
/**
* Get the total orders and customers based on user supplied params.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_import_totals( $request ) {
$query_args = $this->prepare_objects_query( $request );
$totals = ReportsSync::get_import_totals( $query_args['days'], $query_args['skip_existing'] );
$response = $this->prepare_item_for_response( $totals, $request );
$data = $this->prepare_response_for_collection( $response );
return rest_ensure_response( $data );
}
}
API/Reports/Orders/Controller.php 0000644 00000047477 15153746747 0012753 0 ustar 00 <?php
/**
* REST API Reports orders controller
*
* Handles requests to the /reports/orders endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Orders;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Controller as ReportsController;
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
/**
* REST API Reports orders controller class.
*
* @internal
* @extends \Automattic\WooCommerce\Admin\API\Reports\Controller
*/
class Controller extends ReportsController implements ExportableInterface {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/orders';
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['before'] = $request['before'];
$args['after'] = $request['after'];
$args['page'] = $request['page'];
$args['per_page'] = $request['per_page'];
$args['orderby'] = $request['orderby'];
$args['order'] = $request['order'];
$args['product_includes'] = (array) $request['product_includes'];
$args['product_excludes'] = (array) $request['product_excludes'];
$args['variation_includes'] = (array) $request['variation_includes'];
$args['variation_excludes'] = (array) $request['variation_excludes'];
$args['coupon_includes'] = (array) $request['coupon_includes'];
$args['coupon_excludes'] = (array) $request['coupon_excludes'];
$args['tax_rate_includes'] = (array) $request['tax_rate_includes'];
$args['tax_rate_excludes'] = (array) $request['tax_rate_excludes'];
$args['status_is'] = (array) $request['status_is'];
$args['status_is_not'] = (array) $request['status_is_not'];
$args['customer_type'] = $request['customer_type'];
$args['extended_info'] = $request['extended_info'];
$args['refunds'] = $request['refunds'];
$args['match'] = $request['match'];
$args['order_includes'] = $request['order_includes'];
$args['order_excludes'] = $request['order_excludes'];
$args['attribute_is'] = (array) $request['attribute_is'];
$args['attribute_is_not'] = (array) $request['attribute_is_not'];
$args['force_cache_refresh'] = $request['force_cache_refresh'];
return $args;
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$orders_query = new Query( $query_args );
$report_data = $orders_query->get_data();
$data = array();
foreach ( $report_data->data as $orders_data ) {
$orders_data['order_number'] = $this->get_order_number( $orders_data['order_id'] );
$orders_data['total_formatted'] = $this->get_total_formatted( $orders_data['order_id'] );
$item = $this->prepare_item_for_response( $orders_data, $request );
$data[] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$data,
(int) $report_data->total,
(int) $report_data->page_no,
(int) $report_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param stdClass $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$data = $report;
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $report ) );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_orders', $response, $report, $request );
}
/**
* Prepare links for the request.
*
* @param WC_Reports_Query $object Object data.
* @return array
*/
protected function prepare_links( $object ) {
$links = array(
'order' => array(
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $object['order_id'] ) ),
),
);
return $links;
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_orders',
'type' => 'object',
'properties' => array(
'order_id' => array(
'description' => __( 'Order ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'order_number' => array(
'description' => __( 'Order Number.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_created' => array(
'description' => __( "Date the order was created, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_created_gmt' => array(
'description' => __( 'Date the order was created, as GMT.', 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'status' => array(
'description' => __( 'Order status.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'customer_id' => array(
'description' => __( 'Customer ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'num_items_sold' => array(
'description' => __( 'Number of items sold.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'net_total' => array(
'description' => __( 'Net total revenue.', 'woocommerce' ),
'type' => 'float',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'total_formatted' => array(
'description' => __( 'Net total revenue (formatted).', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'customer_type' => array(
'description' => __( 'Returning or new customer.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'extended_info' => array(
'products' => array(
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'List of order product IDs, names, quantities.', 'woocommerce' ),
),
'coupons' => array(
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'List of order coupons.', 'woocommerce' ),
),
'customer' => array(
'type' => 'object',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Order customer information.', 'woocommerce' ),
),
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['page'] = array(
'description' => __( 'Current page of the collection.', 'woocommerce' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);
$params['per_page'] = array(
'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
'type' => 'integer',
'default' => 10,
'minimum' => 0,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['after'] = array(
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['before'] = array(
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
'validate_callback' => 'rest_validate_request_arg',
);
$params['orderby'] = array(
'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
'type' => 'string',
'default' => 'date',
'enum' => array(
'date',
'num_items_sold',
'net_total',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['product_includes'] = array(
'description' => __( 'Limit result set to items that have the specified product(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
);
$params['product_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified product(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'wp_parse_id_list',
);
$params['variation_includes'] = array(
'description' => __( 'Limit result set to items that have the specified variation(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
);
$params['variation_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified variation(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'wp_parse_id_list',
);
$params['coupon_includes'] = array(
'description' => __( 'Limit result set to items that have the specified coupon(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
);
$params['coupon_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified coupon(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'wp_parse_id_list',
);
$params['tax_rate_includes'] = array(
'description' => __( 'Limit result set to items that have the specified tax rate(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
);
$params['tax_rate_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified tax rate(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'wp_parse_id_list',
);
$params['status_is'] = array(
'description' => __( 'Limit result set to items that have the specified order status.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => self::get_order_statuses(),
'type' => 'string',
),
);
$params['status_is_not'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified order status.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => self::get_order_statuses(),
'type' => 'string',
),
);
$params['customer_type'] = array(
'description' => __( 'Limit result set to returning or new customers.', 'woocommerce' ),
'type' => 'string',
'default' => '',
'enum' => array(
'',
'returning',
'new',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['refunds'] = array(
'description' => __( 'Limit result set to specific types of refunds.', 'woocommerce' ),
'type' => 'string',
'default' => '',
'enum' => array(
'',
'all',
'partial',
'full',
'none',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['extended_info'] = array(
'description' => __( 'Add additional piece of info about each coupon to the report.', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'sanitize_callback' => 'wc_string_to_bool',
'validate_callback' => 'rest_validate_request_arg',
);
$params['order_includes'] = array(
'description' => __( 'Limit result set to items that have the specified order ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['order_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified order ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['attribute_is'] = array(
'description' => __( 'Limit result set to orders that include products with the specified attributes.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'array',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
$params['attribute_is_not'] = array(
'description' => __( 'Limit result set to orders that don\'t include products with the specified attributes.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'array',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
$params['force_cache_refresh'] = array(
'description' => __( 'Force retrieval of fresh data instead of from the cache.', 'woocommerce' ),
'type' => 'boolean',
'sanitize_callback' => 'wp_validate_boolean',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get customer name column export value.
*
* @param array $customer Customer from report row.
* @return string
*/
protected function get_customer_name( $customer ) {
return $customer['first_name'] . ' ' . $customer['last_name'];
}
/**
* Get products column export value.
*
* @param array $products Products from report row.
* @return string
*/
protected function get_products( $products ) {
$products_list = array();
foreach ( $products as $product ) {
$products_list[] = sprintf(
/* translators: 1: numeric product quantity, 2: name of product */
__( '%1$s× %2$s', 'woocommerce' ),
$product['quantity'],
$product['name']
);
}
return implode( ', ', $products_list );
}
/**
* Get coupons column export value.
*
* @param array $coupons Coupons from report row.
* @return string
*/
protected function get_coupons( $coupons ) {
return implode( ', ', wp_list_pluck( $coupons, 'code' ) );
}
/**
* Get the column names for export.
*
* @return array Key value pair of Column ID => Label.
*/
public function get_export_columns() {
$export_columns = array(
'date_created' => __( 'Date', 'woocommerce' ),
'order_number' => __( 'Order #', 'woocommerce' ),
'total_formatted' => __( 'N. Revenue (formatted)', 'woocommerce' ),
'status' => __( 'Status', 'woocommerce' ),
'customer_name' => __( 'Customer', 'woocommerce' ),
'customer_type' => __( 'Customer type', 'woocommerce' ),
'products' => __( 'Product(s)', 'woocommerce' ),
'num_items_sold' => __( 'Items sold', 'woocommerce' ),
'coupons' => __( 'Coupon(s)', 'woocommerce' ),
'net_total' => __( 'N. Revenue', 'woocommerce' ),
);
/**
* Filter to add or remove column names from the orders report for
* export.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_report_orders_export_columns',
$export_columns
);
}
/**
* Get the column values for export.
*
* @param array $item Single report item/row.
* @return array Key value pair of Column ID => Row Value.
*/
public function prepare_item_for_export( $item ) {
$export_item = array(
'date_created' => $item['date_created'],
'order_number' => $item['order_number'],
'total_formatted' => $item['total_formatted'],
'status' => $item['status'],
'customer_name' => isset( $item['extended_info']['customer'] ) ? $this->get_customer_name( $item['extended_info']['customer'] ) : null,
'customer_type' => $item['customer_type'],
'products' => isset( $item['extended_info']['products'] ) ? $this->get_products( $item['extended_info']['products'] ) : null,
'num_items_sold' => $item['num_items_sold'],
'coupons' => isset( $item['extended_info']['coupons'] ) ? $this->get_coupons( $item['extended_info']['coupons'] ) : null,
'net_total' => $item['net_total'],
);
/**
* Filter to prepare extra columns in the export item for the orders
* report.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_report_orders_prepare_export_item',
$export_item,
$item
);
}
}
API/Reports/Orders/DataStore.php 0000644 00000045535 15153746747 0012507 0 ustar 00 <?php
/**
* API\Reports\Orders\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Orders;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
use Automattic\WooCommerce\Admin\API\Reports\Cache;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/**
* API\Reports\Orders\DataStore.
*/
class DataStore extends ReportsDataStore implements DataStoreInterface {
/**
* Dynamically sets the date column name based on configuration
*/
public function __construct() {
$this->date_column_name = get_option( 'woocommerce_date_type', 'date_paid' );
parent::__construct();
}
/**
* Table used to get the data.
*
* @var string
*/
protected static $table_name = 'wc_order_stats';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'orders';
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'order_id' => 'intval',
'parent_id' => 'intval',
'date_created' => 'strval',
'date_created_gmt' => 'strval',
'status' => 'strval',
'customer_id' => 'intval',
'net_total' => 'floatval',
'total_sales' => 'floatval',
'num_items_sold' => 'intval',
'customer_type' => 'strval',
);
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'orders';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$table_name = self::get_db_table_name();
// Avoid ambigious columns in SQL query.
$this->report_columns = array(
'order_id' => "DISTINCT {$table_name}.order_id",
'parent_id' => "{$table_name}.parent_id",
// Add 'date' field based on date type setting.
'date' => "{$table_name}.{$this->date_column_name} AS date",
'date_created' => "{$table_name}.date_created",
'date_created_gmt' => "{$table_name}.date_created_gmt",
'status' => "REPLACE({$table_name}.status, 'wc-', '') as status",
'customer_id' => "{$table_name}.customer_id",
'net_total' => "{$table_name}.net_total",
'total_sales' => "{$table_name}.total_sales",
'num_items_sold' => "{$table_name}.num_items_sold",
'customer_type' => "(CASE WHEN {$table_name}.returning_customer = 0 THEN 'new' ELSE 'returning' END) as customer_type",
);
}
/**
* Updates the database query with parameters used for orders report: coupons and products filters.
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function add_sql_query_params( $query_args ) {
global $wpdb;
$order_stats_lookup_table = self::get_db_table_name();
$order_coupon_lookup_table = $wpdb->prefix . 'wc_order_coupon_lookup';
$order_product_lookup_table = $wpdb->prefix . 'wc_order_product_lookup';
$order_tax_lookup_table = $wpdb->prefix . 'wc_order_tax_lookup';
$operator = $this->get_match_operator( $query_args );
$where_subquery = array();
$have_joined_products_table = false;
$this->add_time_period_sql_params( $query_args, $order_stats_lookup_table );
$this->get_limit_sql_params( $query_args );
$this->add_order_by_sql_params( $query_args );
$status_subquery = $this->get_status_subquery( $query_args );
if ( $status_subquery ) {
if ( empty( $query_args['status_is'] ) && empty( $query_args['status_is_not'] ) ) {
$this->subquery->add_sql_clause( 'where', "AND {$status_subquery}" );
} else {
$where_subquery[] = $status_subquery;
}
}
$included_orders = $this->get_included_orders( $query_args );
if ( $included_orders ) {
$where_subquery[] = "{$order_stats_lookup_table}.order_id IN ({$included_orders})";
}
$excluded_orders = $this->get_excluded_orders( $query_args );
if ( $excluded_orders ) {
$where_subquery[] = "{$order_stats_lookup_table}.order_id NOT IN ({$excluded_orders})";
}
if ( $query_args['customer_type'] ) {
$returning_customer = 'returning' === $query_args['customer_type'] ? 1 : 0;
$where_subquery[] = "{$order_stats_lookup_table}.returning_customer = {$returning_customer}";
}
$refund_subquery = $this->get_refund_subquery( $query_args );
$this->subquery->add_sql_clause( 'from', $refund_subquery['from_clause'] );
if ( $refund_subquery['where_clause'] ) {
$where_subquery[] = $refund_subquery['where_clause'];
}
$included_coupons = $this->get_included_coupons( $query_args );
$excluded_coupons = $this->get_excluded_coupons( $query_args );
if ( $included_coupons || $excluded_coupons ) {
$this->subquery->add_sql_clause( 'join', "LEFT JOIN {$order_coupon_lookup_table} ON {$order_stats_lookup_table}.order_id = {$order_coupon_lookup_table}.order_id" );
}
if ( $included_coupons ) {
$where_subquery[] = "{$order_coupon_lookup_table}.coupon_id IN ({$included_coupons})";
}
if ( $excluded_coupons ) {
$where_subquery[] = "({$order_coupon_lookup_table}.coupon_id IS NULL OR {$order_coupon_lookup_table}.coupon_id NOT IN ({$excluded_coupons}))";
}
$included_products = $this->get_included_products( $query_args );
$excluded_products = $this->get_excluded_products( $query_args );
if ( $included_products || $excluded_products ) {
$this->subquery->add_sql_clause( 'join', "LEFT JOIN {$order_product_lookup_table} product_lookup" );
$this->subquery->add_sql_clause( 'join', "ON {$order_stats_lookup_table}.order_id = product_lookup.order_id" );
}
if ( $included_products ) {
$this->subquery->add_sql_clause( 'join', "AND product_lookup.product_id IN ({$included_products})" );
$where_subquery[] = 'product_lookup.order_id IS NOT NULL';
}
if ( $excluded_products ) {
$this->subquery->add_sql_clause( 'join', "AND product_lookup.product_id IN ({$excluded_products})" );
$where_subquery[] = 'product_lookup.order_id IS NULL';
}
$included_variations = $this->get_included_variations( $query_args );
$excluded_variations = $this->get_excluded_variations( $query_args );
if ( $included_variations || $excluded_variations ) {
$this->subquery->add_sql_clause( 'join', "LEFT JOIN {$order_product_lookup_table} variation_lookup" );
$this->subquery->add_sql_clause( 'join', "ON {$order_stats_lookup_table}.order_id = variation_lookup.order_id" );
}
if ( $included_variations ) {
$this->subquery->add_sql_clause( 'join', "AND variation_lookup.variation_id IN ({$included_variations})" );
$where_subquery[] = 'variation_lookup.order_id IS NOT NULL';
}
if ( $excluded_variations ) {
$this->subquery->add_sql_clause( 'join', "AND variation_lookup.variation_id IN ({$excluded_variations})" );
$where_subquery[] = 'variation_lookup.order_id IS NULL';
}
$included_tax_rates = ! empty( $query_args['tax_rate_includes'] ) ? implode( ',', array_map( 'esc_sql', $query_args['tax_rate_includes'] ) ) : false;
$excluded_tax_rates = ! empty( $query_args['tax_rate_excludes'] ) ? implode( ',', array_map( 'esc_sql', $query_args['tax_rate_excludes'] ) ) : false;
if ( $included_tax_rates || $excluded_tax_rates ) {
$this->subquery->add_sql_clause( 'join', "LEFT JOIN {$order_tax_lookup_table} ON {$order_stats_lookup_table}.order_id = {$order_tax_lookup_table}.order_id" );
}
if ( $included_tax_rates ) {
$where_subquery[] = "{$order_tax_lookup_table}.tax_rate_id IN ({$included_tax_rates})";
}
if ( $excluded_tax_rates ) {
$where_subquery[] = "{$order_tax_lookup_table}.tax_rate_id NOT IN ({$excluded_tax_rates}) OR {$order_tax_lookup_table}.tax_rate_id IS NULL";
}
$attribute_subqueries = $this->get_attribute_subqueries( $query_args );
if ( $attribute_subqueries['join'] && $attribute_subqueries['where'] ) {
$this->subquery->add_sql_clause( 'join', "JOIN {$order_product_lookup_table} ON {$order_stats_lookup_table}.order_id = {$order_product_lookup_table}.order_id" );
// Add JOINs for matching attributes.
foreach ( $attribute_subqueries['join'] as $attribute_join ) {
$this->subquery->add_sql_clause( 'join', $attribute_join );
}
// Add WHEREs for matching attributes.
$where_subquery = array_merge( $where_subquery, $attribute_subqueries['where'] );
}
if ( 0 < count( $where_subquery ) ) {
$this->subquery->add_sql_clause( 'where', 'AND (' . implode( " {$operator} ", $where_subquery ) . ')' );
}
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => $this->date_column_name,
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
'fields' => '*',
'product_includes' => array(),
'product_excludes' => array(),
'coupon_includes' => array(),
'coupon_excludes' => array(),
'tax_rate_includes' => array(),
'tax_rate_excludes' => array(),
'customer_type' => null,
'status_is' => array(),
'extended_info' => false,
'refunds' => null,
'order_includes' => array(),
'order_excludes' => array(),
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$data = (object) array(
'data' => array(),
'total' => 0,
'pages' => 0,
'page_no' => 0,
);
$selections = $this->selected_columns( $query_args );
$params = $this->get_limit_params( $query_args );
$this->add_sql_query_params( $query_args );
/* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
$db_records_count = (int) $wpdb->get_var(
"SELECT COUNT( DISTINCT tt.order_id ) FROM (
{$this->subquery->get_query_statement()}
) AS tt"
);
/* phpcs:enable */
if ( 0 === $params['per_page'] ) {
$total_pages = 0;
} else {
$total_pages = (int) ceil( $db_records_count / $params['per_page'] );
}
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
$data = (object) array(
'data' => array(),
'total' => $db_records_count,
'pages' => 0,
'page_no' => 0,
);
return $data;
}
$this->subquery->clear_sql_clause( 'select' );
$this->subquery->add_sql_clause( 'select', $selections );
$this->subquery->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$this->subquery->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
/* phpcs:disable WordPress.DB.PreparedSQL.NotPrepared */
$orders_data = $wpdb->get_results(
$this->subquery->get_query_statement(),
ARRAY_A
);
/* phpcs:enable */
if ( null === $orders_data ) {
return $data;
}
if ( $query_args['extended_info'] ) {
$this->include_extended_info( $orders_data, $query_args );
}
$orders_data = array_map( array( $this, 'cast_numbers' ), $orders_data );
$data = (object) array(
'data' => $orders_data,
'total' => $db_records_count,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Normalizes order_by clause to match to SQL query.
*
* @param string $order_by Order by option requeste by user.
* @return string
*/
protected function normalize_order_by( $order_by ) {
if ( 'date' === $order_by ) {
return $this->date_column_name;
}
return $order_by;
}
/**
* Enriches the order data.
*
* @param array $orders_data Orders data.
* @param array $query_args Query parameters.
*/
protected function include_extended_info( &$orders_data, $query_args ) {
$mapped_orders = $this->map_array_by_key( $orders_data, 'order_id' );
$related_orders = $this->get_orders_with_parent_id( $mapped_orders );
$order_ids = array_merge( array_keys( $mapped_orders ), array_keys( $related_orders ) );
$products = $this->get_products_by_order_ids( $order_ids );
$coupons = $this->get_coupons_by_order_ids( array_keys( $mapped_orders ) );
$customers = $this->get_customers_by_orders( $orders_data );
$mapped_customers = $this->map_array_by_key( $customers, 'customer_id' );
$mapped_data = array();
foreach ( $products as $product ) {
if ( ! isset( $mapped_data[ $product['order_id'] ] ) ) {
$mapped_data[ $product['order_id'] ]['products'] = array();
}
$is_variation = '0' !== $product['variation_id'];
$product_data = array(
'id' => $is_variation ? $product['variation_id'] : $product['product_id'],
'name' => $product['product_name'],
'quantity' => $product['product_quantity'],
);
if ( $is_variation ) {
$variation = wc_get_product( $product_data['id'] );
/**
* Used to determine the separator for products and their variations titles.
*
* @since 4.0.0
*/
$separator = apply_filters( 'woocommerce_product_variation_title_attributes_separator', ' - ', $variation );
if ( false === strpos( $product_data['name'], $separator ) ) {
$attributes = wc_get_formatted_variation( $variation, true, false );
$product_data['name'] .= $separator . $attributes;
}
}
$mapped_data[ $product['order_id'] ]['products'][] = $product_data;
// If this product's order has another related order, it will be added to our mapped_data.
if ( isset( $related_orders [ $product['order_id'] ] ) ) {
$mapped_data[ $related_orders[ $product['order_id'] ]['order_id'] ] ['products'] [] = $product_data;
}
}
foreach ( $coupons as $coupon ) {
if ( ! isset( $mapped_data[ $coupon['order_id'] ] ) ) {
$mapped_data[ $product['order_id'] ]['coupons'] = array();
}
$mapped_data[ $coupon['order_id'] ]['coupons'][] = array(
'id' => $coupon['coupon_id'],
'code' => wc_format_coupon_code( $coupon['coupon_code'] ),
);
}
foreach ( $orders_data as $key => $order_data ) {
$defaults = array(
'products' => array(),
'coupons' => array(),
'customer' => array(),
);
$orders_data[ $key ]['extended_info'] = isset( $mapped_data[ $order_data['order_id'] ] ) ? array_merge( $defaults, $mapped_data[ $order_data['order_id'] ] ) : $defaults;
if ( $order_data['customer_id'] && isset( $mapped_customers[ $order_data['customer_id'] ] ) ) {
$orders_data[ $key ]['extended_info']['customer'] = $mapped_customers[ $order_data['customer_id'] ];
}
}
}
/**
* Returns oreders that have a parent id
*
* @param array $orders Orders array.
* @return array
*/
protected function get_orders_with_parent_id( $orders ) {
$related_orders = array();
foreach ( $orders as $order ) {
if ( '0' !== $order['parent_id'] ) {
$related_orders[ $order['parent_id'] ] = $order;
}
}
return $related_orders;
}
/**
* Returns the same array index by a given key
*
* @param array $array Array to be looped over.
* @param string $key Key of values used for new array.
* @return array
*/
protected function map_array_by_key( $array, $key ) {
$mapped = array();
foreach ( $array as $item ) {
$mapped[ $item[ $key ] ] = $item;
}
return $mapped;
}
/**
* Get product IDs, names, and quantity from order IDs.
*
* @param array $order_ids Array of order IDs.
* @return array
*/
protected function get_products_by_order_ids( $order_ids ) {
global $wpdb;
$order_product_lookup_table = $wpdb->prefix . 'wc_order_product_lookup';
$included_order_ids = implode( ',', $order_ids );
/* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
$products = $wpdb->get_results(
"SELECT
order_id,
product_id,
variation_id,
post_title as product_name,
product_qty as product_quantity
FROM {$wpdb->posts}
JOIN
{$order_product_lookup_table}
ON {$wpdb->posts}.ID = (
CASE WHEN variation_id > 0
THEN variation_id
ELSE product_id
END
)
WHERE
order_id IN ({$included_order_ids})
",
ARRAY_A
);
/* phpcs:enable */
return $products;
}
/**
* Get customer data from Order data.
*
* @param array $orders Array of orders data.
* @return array
*/
protected function get_customers_by_orders( $orders ) {
global $wpdb;
$customer_lookup_table = $wpdb->prefix . 'wc_customer_lookup';
$customer_ids = array();
foreach ( $orders as $order ) {
if ( $order['customer_id'] ) {
$customer_ids[] = intval( $order['customer_id'] );
}
}
if ( empty( $customer_ids ) ) {
return array();
}
/* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
$customer_ids = implode( ',', $customer_ids );
$customers = $wpdb->get_results(
"SELECT * FROM {$customer_lookup_table} WHERE customer_id IN ({$customer_ids})",
ARRAY_A
);
/* phpcs:enable */
return $customers;
}
/**
* Get coupon information from order IDs.
*
* @param array $order_ids Array of order IDs.
* @return array
*/
protected function get_coupons_by_order_ids( $order_ids ) {
global $wpdb;
$order_coupon_lookup_table = $wpdb->prefix . 'wc_order_coupon_lookup';
$included_order_ids = implode( ',', $order_ids );
/* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
$coupons = $wpdb->get_results(
"SELECT order_id, coupon_id, post_title as coupon_code
FROM {$wpdb->posts}
JOIN {$order_coupon_lookup_table} ON {$order_coupon_lookup_table}.coupon_id = {$wpdb->posts}.ID
WHERE
order_id IN ({$included_order_ids})
",
ARRAY_A
);
/* phpcs:enable */
return $coupons;
}
/**
* Get all statuses that have been synced.
*
* @return array Unique order statuses.
*/
public static function get_all_statuses() {
global $wpdb;
$cache_key = 'orders-all-statuses';
$statuses = Cache::get( $cache_key );
if ( false === $statuses ) {
/* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
$table_name = self::get_db_table_name();
$statuses = $wpdb->get_col(
"SELECT DISTINCT status FROM {$table_name}"
);
/* phpcs:enable */
Cache::set( $cache_key, $statuses );
}
return $statuses;
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
$this->subquery = new SqlQuery( $this->context . '_subquery' );
$this->subquery->add_sql_clause( 'select', self::get_db_table_name() . '.order_id' );
$this->subquery->add_sql_clause( 'from', self::get_db_table_name() );
}
}
API/Reports/Orders/Query.php 0000644 00000002347 15153746747 0011720 0 ustar 00 <?php
/**
* Class for parameter-based Orders Reports querying
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'interval' => 'week',
* 'products' => array(15, 18),
* 'coupons' => array(138),
* 'status_is' => array('completed'),
* 'status_is_not' => array('failed'),
* 'new_customers' => false,
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Orders\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Orders;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Orders\Query
*/
class Query extends ReportsQuery {
/**
* Get order data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_orders_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-orders' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_orders_select_query', $results, $args );
}
}
API/Reports/Orders/Stats/Controller.php 0000644 00000047171 15153746747 0014040 0 ustar 00 <?php
/**
* REST API Reports orders stats controller
*
* Handles requests to the /reports/orders/stats endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
/**
* REST API Reports orders stats controller class.
*
* @internal
* @extends \Automattic\WooCommerce\Admin\API\Reports\Controller
*/
class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/orders/stats';
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['before'] = $request['before'];
$args['after'] = $request['after'];
$args['interval'] = $request['interval'];
$args['page'] = $request['page'];
$args['per_page'] = $request['per_page'];
$args['orderby'] = $request['orderby'];
$args['order'] = $request['order'];
$args['fields'] = $request['fields'];
$args['match'] = $request['match'];
$args['status_is'] = (array) $request['status_is'];
$args['status_is_not'] = (array) $request['status_is_not'];
$args['product_includes'] = (array) $request['product_includes'];
$args['product_excludes'] = (array) $request['product_excludes'];
$args['variation_includes'] = (array) $request['variation_includes'];
$args['variation_excludes'] = (array) $request['variation_excludes'];
$args['coupon_includes'] = (array) $request['coupon_includes'];
$args['coupon_excludes'] = (array) $request['coupon_excludes'];
$args['tax_rate_includes'] = (array) $request['tax_rate_includes'];
$args['tax_rate_excludes'] = (array) $request['tax_rate_excludes'];
$args['customer_type'] = $request['customer_type'];
$args['refunds'] = $request['refunds'];
$args['attribute_is'] = (array) $request['attribute_is'];
$args['attribute_is_not'] = (array) $request['attribute_is_not'];
$args['category_includes'] = (array) $request['categories'];
$args['segmentby'] = $request['segmentby'];
$args['force_cache_refresh'] = $request['force_cache_refresh'];
// For backwards compatibility, `customer` is aliased to `customer_type`.
if ( empty( $request['customer_type'] ) && ! empty( $request['customer'] ) ) {
$args['customer_type'] = $request['customer'];
}
return $args;
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$orders_query = new Query( $query_args );
try {
$report_data = $orders_query->get_data();
} catch ( ParameterException $e ) {
return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
$out_data = array(
'totals' => get_object_vars( $report_data->totals ),
'intervals' => array(),
);
foreach ( $report_data->intervals as $interval_data ) {
$item = $this->prepare_item_for_response( $interval_data, $request );
$out_data['intervals'][] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$out_data,
(int) $report_data->total,
(int) $report_data->page_no,
(int) $report_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param Array $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$data = $report;
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_orders_stats', $response, $report, $request );
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$data_values = array(
'net_revenue' => array(
'description' => __( 'Net sales.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'format' => 'currency',
),
'orders_count' => array(
'title' => __( 'Orders', 'woocommerce' ),
'description' => __( 'Number of orders', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
),
'avg_order_value' => array(
'description' => __( 'Average order value.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
'format' => 'currency',
),
'avg_items_per_order' => array(
'description' => __( 'Average items per order', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'num_items_sold' => array(
'description' => __( 'Number of items sold', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'coupons' => array(
'description' => __( 'Amount discounted by coupons.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'coupons_count' => array(
'description' => __( 'Unique coupons count.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'total_customers' => array(
'description' => __( 'Total distinct customers.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'products' => array(
'description' => __( 'Number of distinct products sold.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
);
$segments = array(
'segments' => array(
'description' => __( 'Reports data grouped by segment condition.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'items' => array(
'type' => 'object',
'properties' => array(
'segment_id' => array(
'description' => __( 'Segment identificator.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'subtotals' => array(
'description' => __( 'Interval subtotals.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => $data_values,
),
),
),
),
);
$totals = array_merge( $data_values, $segments );
// Products is not shown in intervals.
unset( $data_values['products'] );
$intervals = array_merge( $data_values, $segments );
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_orders_stats',
'type' => 'object',
'properties' => array(
'totals' => array(
'description' => __( 'Totals data.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => $totals,
),
'intervals' => array(
'description' => __( 'Reports data grouped by intervals.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'items' => array(
'type' => 'object',
'properties' => array(
'interval' => array(
'description' => __( 'Type of interval.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'enum' => array( 'day', 'week', 'month', 'year' ),
),
'date_start' => array(
'description' => __( "The date the report start, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_start_gmt' => array(
'description' => __( 'The date the report start, as GMT.', 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_end' => array(
'description' => __( "The date the report end, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_end_gmt' => array(
'description' => __( 'The date the report end, as GMT.', 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'subtotals' => array(
'description' => __( 'Interval subtotals.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => $intervals,
),
),
),
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['page'] = array(
'description' => __( 'Current page of the collection.', 'woocommerce' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);
$params['per_page'] = array(
'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['after'] = array(
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['before'] = array(
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
'validate_callback' => 'rest_validate_request_arg',
);
$params['orderby'] = array(
'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
'type' => 'string',
'default' => 'date',
'enum' => array(
'date',
'net_revenue',
'orders_count',
'avg_order_value',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['interval'] = array(
'description' => __( 'Time interval to use for buckets in the returned data.', 'woocommerce' ),
'type' => 'string',
'default' => 'week',
'enum' => array(
'hour',
'day',
'week',
'month',
'quarter',
'year',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['match'] = array(
'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'woocommerce' ),
'type' => 'string',
'default' => 'all',
'enum' => array(
'all',
'any',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['status_is'] = array(
'description' => __( 'Limit result set to items that have the specified order status.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'default' => null,
'items' => array(
'enum' => self::get_order_statuses(),
'type' => 'string',
),
);
$params['status_is_not'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified order status.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => self::get_order_statuses(),
'type' => 'string',
),
);
$params['product_includes'] = array(
'description' => __( 'Limit result set to items that have the specified product(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
);
$params['product_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified product(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
);
$params['variation_includes'] = array(
'description' => __( 'Limit result set to items that have the specified variation(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
);
$params['variation_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified variation(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'wp_parse_id_list',
);
$params['coupon_includes'] = array(
'description' => __( 'Limit result set to items that have the specified coupon(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
);
$params['coupon_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified coupon(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
);
$params['tax_rate_includes'] = array(
'description' => __( 'Limit result set to items that have the specified tax rate(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
);
$params['tax_rate_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified tax rate(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'wp_parse_id_list',
);
$params['customer'] = array(
'description' => __( 'Alias for customer_type (deprecated).', 'woocommerce' ),
'type' => 'string',
'enum' => array(
'new',
'returning',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['customer_type'] = array(
'description' => __( 'Limit result set to orders that have the specified customer_type', 'woocommerce' ),
'type' => 'string',
'enum' => array(
'new',
'returning',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['refunds'] = array(
'description' => __( 'Limit result set to specific types of refunds.', 'woocommerce' ),
'type' => 'string',
'default' => '',
'enum' => array(
'',
'all',
'partial',
'full',
'none',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['attribute_is'] = array(
'description' => __( 'Limit result set to orders that include products with the specified attributes.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'array',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
$params['attribute_is_not'] = array(
'description' => __( 'Limit result set to orders that don\'t include products with the specified attributes.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'array',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
$params['segmentby'] = array(
'description' => __( 'Segment the response by additional constraint.', 'woocommerce' ),
'type' => 'string',
'enum' => array(
'product',
'category',
'variation',
'coupon',
'customer_type', // new vs returning.
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['fields'] = array(
'description' => __( 'Limit stats fields to the specified items.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
$params['force_cache_refresh'] = array(
'description' => __( 'Force retrieval of fresh data instead of from the cache.', 'woocommerce' ),
'type' => 'boolean',
'sanitize_callback' => 'wp_validate_boolean',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
}
API/Reports/Orders/Stats/DataStore.php 0000644 00000064022 15153746747 0013575 0 ustar 00 <?php
/**
* API\Reports\Orders\Stats\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
use Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
use Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
use Automattic\WooCommerce\Utilities\OrderUtil;
/**
* API\Reports\Orders\Stats\DataStore.
*/
class DataStore extends ReportsDataStore implements DataStoreInterface {
/**
* Table used to get the data.
*
* @var string
*/
protected static $table_name = 'wc_order_stats';
/**
* Cron event name.
*/
const CRON_EVENT = 'wc_order_stats_update';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'orders_stats';
/**
* Type for each column to cast values correctly later.
*
* @var array
*/
protected $column_types = array(
'orders_count' => 'intval',
'num_items_sold' => 'intval',
'gross_sales' => 'floatval',
'total_sales' => 'floatval',
'coupons' => 'floatval',
'coupons_count' => 'intval',
'refunds' => 'floatval',
'taxes' => 'floatval',
'shipping' => 'floatval',
'net_revenue' => 'floatval',
'avg_items_per_order' => 'floatval',
'avg_order_value' => 'floatval',
'total_customers' => 'intval',
'products' => 'intval',
'segment_id' => 'intval',
);
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'orders_stats';
/**
* Dynamically sets the date column name based on configuration
*/
public function __construct() {
$this->date_column_name = get_option( 'woocommerce_date_type', 'date_paid' );
parent::__construct();
}
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$table_name = self::get_db_table_name();
// Avoid ambigious columns in SQL query.
$refunds = "ABS( SUM( CASE WHEN {$table_name}.net_total < 0 THEN {$table_name}.net_total ELSE 0 END ) )";
$gross_sales =
"( SUM({$table_name}.total_sales)" .
' + COALESCE( SUM(discount_amount), 0 )' . // SUM() all nulls gives null.
" - SUM({$table_name}.tax_total)" .
" - SUM({$table_name}.shipping_total)" .
" + {$refunds}" .
' ) as gross_sales';
$this->report_columns = array(
'orders_count' => "SUM( CASE WHEN {$table_name}.parent_id = 0 THEN 1 ELSE 0 END ) as orders_count",
'num_items_sold' => "SUM({$table_name}.num_items_sold) as num_items_sold",
'gross_sales' => $gross_sales,
'total_sales' => "SUM({$table_name}.total_sales) AS total_sales",
'coupons' => 'COALESCE( SUM(discount_amount), 0 ) AS coupons', // SUM() all nulls gives null.
'coupons_count' => 'COALESCE( coupons_count, 0 ) as coupons_count',
'refunds' => "{$refunds} AS refunds",
'taxes' => "SUM({$table_name}.tax_total) AS taxes",
'shipping' => "SUM({$table_name}.shipping_total) AS shipping",
'net_revenue' => "SUM({$table_name}.net_total) AS net_revenue",
'avg_items_per_order' => "SUM( {$table_name}.num_items_sold ) / SUM( CASE WHEN {$table_name}.parent_id = 0 THEN 1 ELSE 0 END ) AS avg_items_per_order",
'avg_order_value' => "SUM( {$table_name}.net_total ) / SUM( CASE WHEN {$table_name}.parent_id = 0 THEN 1 ELSE 0 END ) AS avg_order_value",
'total_customers' => "COUNT( DISTINCT( {$table_name}.customer_id ) ) as total_customers",
);
}
/**
* Set up all the hooks for maintaining and populating table data.
*/
public static function init() {
add_action( 'woocommerce_before_delete_order', array( __CLASS__, 'delete_order' ) );
add_action( 'delete_post', array( __CLASS__, 'delete_order' ) );
}
/**
* Updates the totals and intervals database queries with parameters used for Orders report: categories, coupons and order status.
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function orders_stats_sql_filter( $query_args ) {
// phpcs:ignore Generic.Commenting.Todo.TaskFound
// @todo Performance of all of this?
global $wpdb;
$from_clause = '';
$orders_stats_table = self::get_db_table_name();
$product_lookup = $wpdb->prefix . 'wc_order_product_lookup';
$coupon_lookup = $wpdb->prefix . 'wc_order_coupon_lookup';
$tax_rate_lookup = $wpdb->prefix . 'wc_order_tax_lookup';
$operator = $this->get_match_operator( $query_args );
$where_filters = array();
// Products filters.
$where_filters[] = $this->get_object_where_filter(
$orders_stats_table,
'order_id',
$product_lookup,
'product_id',
'IN',
$this->get_included_products( $query_args )
);
$where_filters[] = $this->get_object_where_filter(
$orders_stats_table,
'order_id',
$product_lookup,
'product_id',
'NOT IN',
$this->get_excluded_products( $query_args )
);
// Variations filters.
$where_filters[] = $this->get_object_where_filter(
$orders_stats_table,
'order_id',
$product_lookup,
'variation_id',
'IN',
$this->get_included_variations( $query_args )
);
$where_filters[] = $this->get_object_where_filter(
$orders_stats_table,
'order_id',
$product_lookup,
'variation_id',
'NOT IN',
$this->get_excluded_variations( $query_args )
);
// Coupons filters.
$where_filters[] = $this->get_object_where_filter(
$orders_stats_table,
'order_id',
$coupon_lookup,
'coupon_id',
'IN',
$this->get_included_coupons( $query_args )
);
$where_filters[] = $this->get_object_where_filter(
$orders_stats_table,
'order_id',
$coupon_lookup,
'coupon_id',
'NOT IN',
$this->get_excluded_coupons( $query_args )
);
// Tax rate filters.
$where_filters[] = $this->get_object_where_filter(
$orders_stats_table,
'order_id',
$tax_rate_lookup,
'tax_rate_id',
'IN',
implode( ',', $query_args['tax_rate_includes'] )
);
$where_filters[] = $this->get_object_where_filter(
$orders_stats_table,
'order_id',
$tax_rate_lookup,
'tax_rate_id',
'NOT IN',
implode( ',', $query_args['tax_rate_excludes'] )
);
// Product attribute filters.
$attribute_subqueries = $this->get_attribute_subqueries( $query_args );
if ( $attribute_subqueries['join'] && $attribute_subqueries['where'] ) {
// Build a subquery for getting order IDs by product attribute(s).
// Done here since our use case is a little more complicated than get_object_where_filter() can handle.
$attribute_subquery = new SqlQuery();
$attribute_subquery->add_sql_clause( 'select', "{$orders_stats_table}.order_id" );
$attribute_subquery->add_sql_clause( 'from', $orders_stats_table );
// JOIN on product lookup.
$attribute_subquery->add_sql_clause( 'join', "JOIN {$product_lookup} ON {$orders_stats_table}.order_id = {$product_lookup}.order_id" );
// Add JOINs for matching attributes.
foreach ( $attribute_subqueries['join'] as $attribute_join ) {
$attribute_subquery->add_sql_clause( 'join', $attribute_join );
}
// Add WHEREs for matching attributes.
$attribute_subquery->add_sql_clause( 'where', 'AND (' . implode( " {$operator} ", $attribute_subqueries['where'] ) . ')' );
// Generate subquery statement and add to our where filters.
$where_filters[] = "{$orders_stats_table}.order_id IN (" . $attribute_subquery->get_query_statement() . ')';
}
$where_filters[] = $this->get_customer_subquery( $query_args );
$refund_subquery = $this->get_refund_subquery( $query_args );
$from_clause .= $refund_subquery['from_clause'];
if ( $refund_subquery['where_clause'] ) {
$where_filters[] = $refund_subquery['where_clause'];
}
$where_filters = array_filter( $where_filters );
$where_subclause = implode( " $operator ", $where_filters );
// Append status filter after to avoid matching ANY on default statuses.
$order_status_filter = $this->get_status_subquery( $query_args, $operator );
if ( $order_status_filter ) {
if ( empty( $query_args['status_is'] ) && empty( $query_args['status_is_not'] ) ) {
$operator = 'AND';
}
$where_subclause = implode( " $operator ", array_filter( array( $where_subclause, $order_status_filter ) ) );
}
// To avoid requesting the subqueries twice, the result is applied to all queries passed to the method.
if ( $where_subclause ) {
$this->total_query->add_sql_clause( 'where', "AND ( $where_subclause )" );
$this->total_query->add_sql_clause( 'join', $from_clause );
$this->interval_query->add_sql_clause( 'where', "AND ( $where_subclause )" );
$this->interval_query->add_sql_clause( 'join', $from_clause );
}
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$table_name = self::get_db_table_name();
// These defaults are only applied when not using REST API, as the API has its own defaults that overwrite these for most values (except before, after, etc).
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
'interval' => 'week',
'fields' => '*',
'segmentby' => '',
'match' => 'all',
'status_is' => array(),
'status_is_not' => array(),
'product_includes' => array(),
'product_excludes' => array(),
'coupon_includes' => array(),
'coupon_excludes' => array(),
'tax_rate_includes' => array(),
'tax_rate_excludes' => array(),
'customer_type' => '',
'category_includes' => array(),
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$data = (object) array(
'totals' => (object) array(),
'intervals' => (object) array(),
'total' => 0,
'pages' => 0,
'page_no' => 0,
);
$selections = $this->selected_columns( $query_args );
$this->add_time_period_sql_params( $query_args, $table_name );
$this->add_intervals_sql_params( $query_args, $table_name );
$this->add_order_by_sql_params( $query_args );
$where_time = $this->get_sql_clause( 'where_time' );
$params = $this->get_limit_sql_params( $query_args );
$coupon_join = "LEFT JOIN (
SELECT
order_id,
SUM(discount_amount) AS discount_amount,
COUNT(DISTINCT coupon_id) AS coupons_count
FROM
{$wpdb->prefix}wc_order_coupon_lookup
GROUP BY
order_id
) order_coupon_lookup
ON order_coupon_lookup.order_id = {$wpdb->prefix}wc_order_stats.order_id";
// Additional filtering for Orders report.
$this->orders_stats_sql_filter( $query_args );
$this->total_query->add_sql_clause( 'select', $selections );
$this->total_query->add_sql_clause( 'left_join', $coupon_join );
$this->total_query->add_sql_clause( 'where_time', $where_time );
$totals = $wpdb->get_results(
$this->total_query->get_query_statement(),
ARRAY_A
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
if ( null === $totals ) {
return new \WP_Error( 'woocommerce_analytics_revenue_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce' ) );
}
// phpcs:ignore Generic.Commenting.Todo.TaskFound
// @todo Remove these assignements when refactoring segmenter classes to use query objects.
$totals_query = array(
'from_clause' => $this->total_query->get_sql_clause( 'join' ),
'where_time_clause' => $where_time,
'where_clause' => $this->total_query->get_sql_clause( 'where' ),
);
$intervals_query = array(
'select_clause' => $this->get_sql_clause( 'select' ),
'from_clause' => $this->interval_query->get_sql_clause( 'join' ),
'where_time_clause' => $where_time,
'where_clause' => $this->interval_query->get_sql_clause( 'where' ),
'limit' => $this->get_sql_clause( 'limit' ),
);
$unique_products = $this->get_unique_product_count( $totals_query['from_clause'], $totals_query['where_time_clause'], $totals_query['where_clause'] );
$totals[0]['products'] = $unique_products;
$segmenter = new Segmenter( $query_args, $this->report_columns );
$unique_coupons = $this->get_unique_coupon_count( $totals_query['from_clause'], $totals_query['where_time_clause'], $totals_query['where_clause'] );
$totals[0]['coupons_count'] = $unique_coupons;
$totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name );
$totals = (object) $this->cast_numbers( $totals[0] );
$this->interval_query->add_sql_clause( 'select', $this->get_sql_clause( 'select' ) . ' AS time_interval' );
$this->interval_query->add_sql_clause( 'left_join', $coupon_join );
$this->interval_query->add_sql_clause( 'where_time', $where_time );
$db_intervals = $wpdb->get_col(
$this->interval_query->get_query_statement()
); // phpcs:ignore cache ok, DB call ok, , unprepared SQL ok.
$db_interval_count = count( $db_intervals );
$expected_interval_count = TimeInterval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $params['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return $data;
}
$this->update_intervals_sql_params( $query_args, $db_interval_count, $expected_interval_count, $table_name );
$this->interval_query->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$this->interval_query->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
$this->interval_query->add_sql_clause( 'select', ", MAX({$table_name}.date_created) AS datetime_anchor" );
if ( '' !== $selections ) {
$this->interval_query->add_sql_clause( 'select', ', ' . $selections );
}
$intervals = $wpdb->get_results(
$this->interval_query->get_query_statement(),
ARRAY_A
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
if ( null === $intervals ) {
return new \WP_Error( 'woocommerce_analytics_revenue_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce' ) );
}
if ( isset( $intervals[0] ) ) {
$unique_coupons = $this->get_unique_coupon_count( $intervals_query['from_clause'], $intervals_query['where_time_clause'], $intervals_query['where_clause'], true );
$intervals[0]['coupons_count'] = $unique_coupons;
}
$data = (object) array(
'totals' => $totals,
'intervals' => $intervals,
'total' => $expected_interval_count,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
if ( TimeInterval::intervals_missing( $expected_interval_count, $db_interval_count, $params['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
$this->remove_extra_records( $data, $query_args['page'], $params['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] );
} else {
$this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals );
}
$segmenter->add_intervals_segments( $data, $intervals_query, $table_name );
$this->create_interval_subtotals( $data->intervals );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Get unique products based on user time query
*
* @param string $from_clause From clause with date query.
* @param string $where_time_clause Where clause with date query.
* @param string $where_clause Where clause with date query.
* @return integer Unique product count.
*/
public function get_unique_product_count( $from_clause, $where_time_clause, $where_clause ) {
global $wpdb;
$table_name = self::get_db_table_name();
return $wpdb->get_var(
"SELECT
COUNT( DISTINCT {$wpdb->prefix}wc_order_product_lookup.product_id )
FROM
{$wpdb->prefix}wc_order_product_lookup JOIN {$table_name} ON {$wpdb->prefix}wc_order_product_lookup.order_id = {$table_name}.order_id
{$from_clause}
WHERE
1=1
{$where_time_clause}
{$where_clause}"
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
}
/**
* Get unique coupons based on user time query
*
* @param string $from_clause From clause with date query.
* @param string $where_time_clause Where clause with date query.
* @param string $where_clause Where clause with date query.
* @return integer Unique product count.
*/
public function get_unique_coupon_count( $from_clause, $where_time_clause, $where_clause ) {
global $wpdb;
$table_name = self::get_db_table_name();
return $wpdb->get_var(
"SELECT
COUNT(DISTINCT coupon_id)
FROM
{$wpdb->prefix}wc_order_coupon_lookup JOIN {$table_name} ON {$wpdb->prefix}wc_order_coupon_lookup.order_id = {$table_name}.order_id
{$from_clause}
WHERE
1=1
{$where_time_clause}
{$where_clause}"
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
}
/**
* Add order information to the lookup table when orders are created or modified.
*
* @param int $post_id Post ID.
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
*/
public static function sync_order( $post_id ) {
if ( ! OrderUtil::is_order( $post_id, array( 'shop_order', 'shop_order_refund' ) ) ) {
return -1;
}
$order = wc_get_order( $post_id );
if ( ! $order ) {
return -1;
}
return self::update( $order );
}
/**
* Update the database with stats data.
*
* @param WC_Order|WC_Order_Refund $order Order or refund to update row for.
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
*/
public static function update( $order ) {
global $wpdb;
$table_name = self::get_db_table_name();
if ( ! $order->get_id() || ! $order->get_date_created() ) {
return -1;
}
/**
* Filters order stats data.
*
* @param array $data Data written to order stats lookup table.
* @param WC_Order $order Order object.
*
* @since 4.0.0
*/
$data = apply_filters(
'woocommerce_analytics_update_order_stats_data',
array(
'order_id' => $order->get_id(),
'parent_id' => $order->get_parent_id(),
'date_created' => $order->get_date_created()->date( 'Y-m-d H:i:s' ),
'date_paid' => $order->get_date_paid() ? $order->get_date_paid()->date( 'Y-m-d H:i:s' ) : null,
'date_completed' => $order->get_date_completed() ? $order->get_date_completed()->date( 'Y-m-d H:i:s' ) : null,
'date_created_gmt' => gmdate( 'Y-m-d H:i:s', $order->get_date_created()->getTimestamp() ),
'num_items_sold' => self::get_num_items_sold( $order ),
'total_sales' => $order->get_total(),
'tax_total' => $order->get_total_tax(),
'shipping_total' => $order->get_shipping_total(),
'net_total' => self::get_net_total( $order ),
'status' => self::normalize_order_status( $order->get_status() ),
'customer_id' => $order->get_report_customer_id(),
'returning_customer' => $order->is_returning_customer(),
),
$order
);
$format = array(
'%d',
'%d',
'%s',
'%s',
'%s',
'%s',
'%d',
'%f',
'%f',
'%f',
'%f',
'%s',
'%d',
'%d',
);
if ( 'shop_order_refund' === $order->get_type() ) {
$parent_order = wc_get_order( $order->get_parent_id() );
if ( $parent_order ) {
$data['parent_id'] = $parent_order->get_id();
$data['status'] = self::normalize_order_status( $parent_order->get_status() );
}
/**
* Set date_completed and date_paid the same as date_created to avoid problems
* when they are being used to sort the data, as refunds don't have them filled
*/
$data['date_completed'] = $data['date_created'];
$data['date_paid'] = $data['date_created'];
}
// Update or add the information to the DB.
$result = $wpdb->replace( $table_name, $data, $format );
/**
* Fires when order's stats reports are updated.
*
* @param int $order_id Order ID.
*
* @since 4.0.0.
*/
do_action( 'woocommerce_analytics_update_order_stats', $order->get_id() );
// Check the rows affected for success. Using REPLACE can affect 2 rows if the row already exists.
return ( 1 === $result || 2 === $result );
}
/**
* Deletes the order stats when an order is deleted.
*
* @param int $post_id Post ID.
*/
public static function delete_order( $post_id ) {
global $wpdb;
$order_id = (int) $post_id;
if ( ! OrderUtil::is_order( $post_id, array( 'shop_order', 'shop_order_refund' ) ) ) {
return;
}
// Retrieve customer details before the order is deleted.
$order = wc_get_order( $order_id );
$customer_id = absint( CustomersDataStore::get_existing_customer_id_from_order( $order ) );
// Delete the order.
$wpdb->delete( self::get_db_table_name(), array( 'order_id' => $order_id ) );
/**
* Fires when orders stats are deleted.
*
* @param int $order_id Order ID.
* @param int $customer_id Customer ID.
*
* @since 4.0.0
*/
do_action( 'woocommerce_analytics_delete_order_stats', $order_id, $customer_id );
ReportsCache::invalidate();
}
/**
* Calculation methods.
*/
/**
* Get number of items sold among all orders.
*
* @param array $order WC_Order object.
* @return int
*/
protected static function get_num_items_sold( $order ) {
$num_items = 0;
$line_items = $order->get_items( 'line_item' );
foreach ( $line_items as $line_item ) {
$num_items += $line_item->get_quantity();
}
return $num_items;
}
/**
* Get the net amount from an order without shipping, tax, or refunds.
*
* @param array $order WC_Order object.
* @return float
*/
protected static function get_net_total( $order ) {
$net_total = floatval( $order->get_total() ) - floatval( $order->get_total_tax() ) - floatval( $order->get_shipping_total() );
return (float) $net_total;
}
/**
* Check to see if an order's customer has made previous orders or not
*
* @param array $order WC_Order object.
* @param int|false $customer_id Customer ID. Optional.
* @return bool
*/
public static function is_returning_customer( $order, $customer_id = null ) {
if ( is_null( $customer_id ) ) {
$customer_id = \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore::get_existing_customer_id_from_order( $order );
}
if ( ! $customer_id ) {
return false;
}
$oldest_orders = \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore::get_oldest_orders( $customer_id );
if ( empty( $oldest_orders ) ) {
return false;
}
$first_order = $oldest_orders[0];
$second_order = isset( $oldest_orders[1] ) ? $oldest_orders[1] : false;
$excluded_statuses = self::get_excluded_report_order_statuses();
// Order is older than previous first order.
if ( $order->get_date_created() < wc_string_to_datetime( $first_order->date_created ) &&
! in_array( $order->get_status(), $excluded_statuses, true )
) {
self::set_customer_first_order( $customer_id, $order->get_id() );
return false;
}
// The current order is the oldest known order.
$is_first_order = (int) $order->get_id() === (int) $first_order->order_id;
// Order date has changed and next oldest is now the first order.
$date_change = $second_order &&
$order->get_date_created() > wc_string_to_datetime( $first_order->date_created ) &&
wc_string_to_datetime( $second_order->date_created ) < $order->get_date_created();
// Status has changed to an excluded status and next oldest order is now the first order.
$status_change = $second_order &&
in_array( $order->get_status(), $excluded_statuses, true );
if ( $is_first_order && ( $date_change || $status_change ) ) {
self::set_customer_first_order( $customer_id, $second_order->order_id );
return true;
}
return (int) $order->get_id() !== (int) $first_order->order_id;
}
/**
* Set a customer's first order and all others to returning.
*
* @param int $customer_id Customer ID.
* @param int $order_id Order ID.
*/
protected static function set_customer_first_order( $customer_id, $order_id ) {
global $wpdb;
$orders_stats_table = self::get_db_table_name();
$wpdb->query(
$wpdb->prepare(
// phpcs:ignore Generic.Commenting.Todo.TaskFound
// TODO: use the %i placeholder to prepare the table name when available in the the minimum required WordPress version.
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"UPDATE {$orders_stats_table} SET returning_customer = CASE WHEN order_id = %d THEN false ELSE true END WHERE customer_id = %d",
$order_id,
$customer_id
)
);
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
unset( $this->subquery );
$this->total_query = new SqlQuery( $this->context . '_total' );
$this->total_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query = new SqlQuery( $this->context . '_interval' );
$this->interval_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query->add_sql_clause( 'group_by', 'time_interval' );
}
}
API/Reports/Orders/Stats/Query.php 0000644 00000003003 15153746747 0013004 0 ustar 00 <?php
/**
* Class for parameter-based Order Stats Reports querying
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'interval' => 'week',
* 'categories' => array(15, 18),
* 'coupons' => array(138),
* 'status_in' => array('completed'),
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Orders\Stats\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Orders report.
*
* @return array
*/
protected function get_default_query_vars() {
return array(
'fields' => array(
'net_revenue',
'avg_order_value',
'orders_count',
'avg_items_per_order',
'num_items_sold',
'coupons',
'coupons_count',
'total_customers',
),
);
}
/**
* Get revenue data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_orders_stats_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-orders-stats' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_orders_stats_select_query', $results, $args );
}
}
API/Reports/Orders/Stats/Segmenter.php 0000644 00000051633 15153746747 0013644 0 ustar 00 <?php
/**
* Class for adding segmenting support without cluttering the data stores.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter;
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
/**
* Date & time interval and numeric range handling class for Reporting API.
*/
class Segmenter extends ReportsSegmenter {
/**
* Returns column => query mapping to be used for product-related product-level segmenting query
* (e.g. products sold, revenue from product X when segmenting by category).
*
* @param string $products_table Name of SQL table containing the product-level segmenting info.
*
* @return array Column => SELECT query mapping.
*/
protected function get_segment_selections_product_level( $products_table ) {
$columns_mapping = array(
'num_items_sold' => "SUM($products_table.product_qty) as num_items_sold",
'total_sales' => "SUM($products_table.product_gross_revenue) AS total_sales",
'coupons' => 'SUM( coupon_lookup_left_join.discount_amount ) AS coupons',
'coupons_count' => 'COUNT( DISTINCT( coupon_lookup_left_join.coupon_id ) ) AS coupons_count',
'refunds' => "SUM( CASE WHEN $products_table.product_gross_revenue < 0 THEN $products_table.product_gross_revenue ELSE 0 END ) AS refunds",
'taxes' => "SUM($products_table.tax_amount) AS taxes",
'shipping' => "SUM($products_table.shipping_amount) AS shipping",
'net_revenue' => "SUM($products_table.product_net_revenue) AS net_revenue",
);
return $columns_mapping;
}
/**
* Returns column => query mapping to be used for order-related product-level segmenting query
* (e.g. avg items per order when segmented by category).
*
* @param string $unique_orders_table Name of SQL table containing the order-level segmenting info.
*
* @return array Column => SELECT query mapping.
*/
protected function get_segment_selections_order_level( $unique_orders_table ) {
$columns_mapping = array(
'orders_count' => "COUNT($unique_orders_table.order_id) AS orders_count",
'avg_items_per_order' => "AVG($unique_orders_table.num_items_sold) AS avg_items_per_order",
'avg_order_value' => "SUM($unique_orders_table.net_total) / COUNT($unique_orders_table.order_id) AS avg_order_value",
'total_customers' => "COUNT( DISTINCT( $unique_orders_table.customer_id ) ) AS total_customers",
);
return $columns_mapping;
}
/**
* Returns column => query mapping to be used for order-level segmenting query
* (e.g. avg items per order or Net sales when segmented by coupons).
*
* @param string $order_stats_table Name of SQL table containing the order-level info.
* @param array $overrides Array of overrides for default column calculations.
*
* @return array Column => SELECT query mapping.
*/
protected function segment_selections_orders( $order_stats_table, $overrides = array() ) {
$columns_mapping = array(
'num_items_sold' => "SUM($order_stats_table.num_items_sold) as num_items_sold",
'total_sales' => "SUM($order_stats_table.total_sales) AS total_sales",
'coupons' => "SUM($order_stats_table.discount_amount) AS coupons",
'coupons_count' => 'COUNT( DISTINCT(coupon_lookup_left_join.coupon_id) ) AS coupons_count',
'refunds' => "SUM( CASE WHEN $order_stats_table.parent_id != 0 THEN $order_stats_table.total_sales END ) AS refunds",
'taxes' => "SUM($order_stats_table.tax_total) AS taxes",
'shipping' => "SUM($order_stats_table.shipping_total) AS shipping",
'net_revenue' => "SUM($order_stats_table.net_total) AS net_revenue",
'orders_count' => "COUNT($order_stats_table.order_id) AS orders_count",
'avg_items_per_order' => "AVG($order_stats_table.num_items_sold) AS avg_items_per_order",
'avg_order_value' => "SUM($order_stats_table.net_total) / COUNT($order_stats_table.order_id) AS avg_order_value",
'total_customers' => "COUNT( DISTINCT( $order_stats_table.customer_id ) ) AS total_customers",
);
if ( $overrides ) {
$columns_mapping = array_merge( $columns_mapping, $overrides );
}
return $columns_mapping;
}
/**
* Calculate segments for totals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
*
* @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $segmenting_dimension_name Name of the segmenting dimension.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $totals_query Array of SQL clauses for totals query.
* @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
*
* @return array
*/
protected function get_product_related_totals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $totals_query, $unique_orders_table ) {
global $wpdb;
$product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
// Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued).
// Product-level numbers.
$segments_products = $wpdb->get_results(
"SELECT
$segmenting_groupby AS $segmenting_dimension_name
{$segmenting_selections['product_level']}
FROM
$table_name
$segmenting_from
{$totals_query['from_clause']}
WHERE
1=1
{$totals_query['where_time_clause']}
{$totals_query['where_clause']}
$segmenting_where
GROUP BY
$segmenting_groupby",
ARRAY_A
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
// Order level numbers.
// As there can be 2 same product ids (or variation ids) per one order, the orders first need to be uniqued before calculating averages, customer counts, etc.
$segments_orders = $wpdb->get_results(
"SELECT
$unique_orders_table.$segmenting_dimension_name AS $segmenting_dimension_name
{$segmenting_selections['order_level']}
FROM
(
SELECT
$table_name.order_id,
$segmenting_groupby AS $segmenting_dimension_name,
MAX( num_items_sold ) AS num_items_sold,
MAX( net_total ) as net_total,
MAX( returning_customer ) AS returning_customer,
MAX( $table_name.customer_id ) as customer_id
FROM
$table_name
$segmenting_from
{$totals_query['from_clause']}
WHERE
1=1
{$totals_query['where_time_clause']}
{$totals_query['where_clause']}
$segmenting_where
GROUP BY
$product_segmenting_table.order_id, $segmenting_groupby
) AS $unique_orders_table
GROUP BY
$unique_orders_table.$segmenting_dimension_name",
ARRAY_A
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
$totals_segments = $this->merge_segment_totals_results( $segmenting_dimension_name, $segments_products, $segments_orders );
return $totals_segments;
}
/**
* Calculate segments for intervals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
*
* @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $segmenting_dimension_name Name of the segmenting dimension.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $intervals_query Array of SQL clauses for intervals query.
* @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
*
* @return array
*/
protected function get_product_related_intervals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $intervals_query, $unique_orders_table ) {
global $wpdb;
$product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
// LIMIT offset, rowcount needs to be updated to LIMIT offset, rowcount * max number of segments.
$limit_parts = explode( ',', $intervals_query['limit'] );
$orig_rowcount = intval( $limit_parts[1] );
$segmenting_limit = $limit_parts[0] . ',' . $orig_rowcount * count( $this->get_all_segments() );
// Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued).
// Product-level numbers.
$segments_products = $wpdb->get_results(
"SELECT
{$intervals_query['select_clause']} AS time_interval,
$segmenting_groupby AS $segmenting_dimension_name
{$segmenting_selections['product_level']}
FROM
$table_name
$segmenting_from
{$intervals_query['from_clause']}
WHERE
1=1
{$intervals_query['where_time_clause']}
{$intervals_query['where_clause']}
$segmenting_where
GROUP BY
time_interval, $segmenting_groupby
$segmenting_limit",
ARRAY_A
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
// Order level numbers.
// As there can be 2 same product ids (or variation ids) per one order, the orders first need to be uniqued before calculating averages, customer counts, etc.
$segments_orders = $wpdb->get_results(
"SELECT
$unique_orders_table.time_interval AS time_interval,
$unique_orders_table.$segmenting_dimension_name AS $segmenting_dimension_name
{$segmenting_selections['order_level']}
FROM
(
SELECT
MAX( $table_name.date_created ) AS datetime_anchor,
{$intervals_query['select_clause']} AS time_interval,
$table_name.order_id,
$segmenting_groupby AS $segmenting_dimension_name,
MAX( num_items_sold ) AS num_items_sold,
MAX( net_total ) as net_total,
MAX( returning_customer ) AS returning_customer,
MAX( $table_name.customer_id ) as customer_id
FROM
$table_name
$segmenting_from
{$intervals_query['from_clause']}
WHERE
1=1
{$intervals_query['where_time_clause']}
{$intervals_query['where_clause']}
$segmenting_where
GROUP BY
time_interval, $product_segmenting_table.order_id, $segmenting_groupby
) AS $unique_orders_table
GROUP BY
time_interval, $unique_orders_table.$segmenting_dimension_name
$segmenting_limit",
ARRAY_A
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
$intervals_segments = $this->merge_segment_intervals_results( $segmenting_dimension_name, $segments_products, $segments_orders );
return $intervals_segments;
}
/**
* Calculate segments for totals query where the segmenting property is bound to order (e.g. coupon or customer type).
*
* @param string $segmenting_select SELECT part of segmenting SQL query.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $totals_query Array of SQL clauses for intervals query.
*
* @return array
*/
protected function get_order_related_totals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $totals_query ) {
global $wpdb;
$totals_segments = $wpdb->get_results(
"SELECT
$segmenting_groupby
$segmenting_select
FROM
$table_name
$segmenting_from
{$totals_query['from_clause']}
WHERE
1=1
{$totals_query['where_time_clause']}
{$totals_query['where_clause']}
$segmenting_where
GROUP BY
$segmenting_groupby",
ARRAY_A
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
// Reformat result.
$totals_segments = $this->reformat_totals_segments( $totals_segments, $segmenting_groupby );
return $totals_segments;
}
/**
* Calculate segments for intervals query where the segmenting property is bound to order (e.g. coupon or customer type).
*
* @param string $segmenting_select SELECT part of segmenting SQL query.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $intervals_query Array of SQL clauses for intervals query.
*
* @return array
*/
protected function get_order_related_intervals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $intervals_query ) {
global $wpdb;
$segmenting_limit = '';
$limit_parts = explode( ',', $intervals_query['limit'] );
if ( 2 === count( $limit_parts ) ) {
$orig_rowcount = intval( $limit_parts[1] );
$segmenting_limit = $limit_parts[0] . ',' . $orig_rowcount * count( $this->get_all_segments() );
}
$intervals_segments = $wpdb->get_results(
"SELECT
MAX($table_name.date_created) AS datetime_anchor,
{$intervals_query['select_clause']} AS time_interval,
$segmenting_groupby
$segmenting_select
FROM
$table_name
$segmenting_from
{$intervals_query['from_clause']}
WHERE
1=1
{$intervals_query['where_time_clause']}
{$intervals_query['where_clause']}
$segmenting_where
GROUP BY
time_interval, $segmenting_groupby
$segmenting_limit",
ARRAY_A
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
// Reformat result.
$intervals_segments = $this->reformat_intervals_segments( $intervals_segments, $segmenting_groupby );
return $intervals_segments;
}
/**
* Return array of segments formatted for REST response.
*
* @param string $type Type of segments to return--'totals' or 'intervals'.
* @param array $query_params SQL query parameter array.
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
*
* @return array
* @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified.
*/
protected function get_segments( $type, $query_params, $table_name ) {
global $wpdb;
if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) {
return array();
}
$product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
$unique_orders_table = 'uniq_orders';
$segmenting_from = "LEFT JOIN {$wpdb->prefix}wc_order_coupon_lookup AS coupon_lookup_left_join ON ($table_name.order_id = coupon_lookup_left_join.order_id) ";
$segmenting_where = '';
// Product, variation, and category are bound to product, so here product segmenting table is required,
// while coupon and customer are bound to order, so we don't need the extra JOIN for those.
// This also means that segment selections need to be calculated differently.
if ( 'product' === $this->query_args['segmentby'] ) {
// @todo How to handle shipping taxes when grouped by product?
$product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
$order_level_columns = $this->get_segment_selections_order_level( $unique_orders_table );
$segmenting_selections = array(
'product_level' => $this->prepare_selections( $product_level_columns ),
'order_level' => $this->prepare_selections( $order_level_columns ),
);
$this->report_columns = array_merge( $product_level_columns, $order_level_columns );
$segmenting_from .= "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)";
$segmenting_groupby = $product_segmenting_table . '.product_id';
$segmenting_dimension_name = 'product_id';
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'variation' === $this->query_args['segmentby'] ) {
if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) {
throw new ParameterException( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'woocommerce' ) );
}
$product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
$order_level_columns = $this->get_segment_selections_order_level( $unique_orders_table );
$segmenting_selections = array(
'product_level' => $this->prepare_selections( $product_level_columns ),
'order_level' => $this->prepare_selections( $order_level_columns ),
);
$this->report_columns = array_merge( $product_level_columns, $order_level_columns );
$segmenting_from .= "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)";
$segmenting_where = "AND $product_segmenting_table.product_id = {$this->query_args['product_includes'][0]}";
$segmenting_groupby = $product_segmenting_table . '.variation_id';
$segmenting_dimension_name = 'variation_id';
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'category' === $this->query_args['segmentby'] ) {
$product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
$order_level_columns = $this->get_segment_selections_order_level( $unique_orders_table );
$segmenting_selections = array(
'product_level' => $this->prepare_selections( $product_level_columns ),
'order_level' => $this->prepare_selections( $order_level_columns ),
);
$this->report_columns = array_merge( $product_level_columns, $order_level_columns );
$segmenting_from .= "
INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)
LEFT JOIN {$wpdb->term_relationships} ON {$product_segmenting_table}.product_id = {$wpdb->term_relationships}.object_id
JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
LEFT JOIN {$wpdb->wc_category_lookup} ON {$wpdb->term_taxonomy}.term_id = {$wpdb->wc_category_lookup}.category_id
";
$segmenting_where = " AND {$wpdb->wc_category_lookup}.category_tree_id IS NOT NULL";
$segmenting_groupby = "{$wpdb->wc_category_lookup}.category_tree_id";
$segmenting_dimension_name = 'category_id';
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'coupon' === $this->query_args['segmentby'] ) {
// As there can be 2 or more coupons applied per one order, coupon amount needs to be split.
$coupon_override = array(
'coupons' => 'SUM(coupon_lookup.discount_amount) AS coupons',
);
$coupon_level_columns = $this->segment_selections_orders( $table_name, $coupon_override );
$segmenting_selections = $this->prepare_selections( $coupon_level_columns );
$this->report_columns = $coupon_level_columns;
$segmenting_from .= "
INNER JOIN {$wpdb->prefix}wc_order_coupon_lookup AS coupon_lookup ON ($table_name.order_id = coupon_lookup.order_id)
";
$segmenting_groupby = 'coupon_lookup.coupon_id';
$segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params );
} elseif ( 'customer_type' === $this->query_args['segmentby'] ) {
$customer_level_columns = $this->segment_selections_orders( $table_name );
$segmenting_selections = $this->prepare_selections( $customer_level_columns );
$this->report_columns = $customer_level_columns;
$segmenting_groupby = "$table_name.returning_customer";
$segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params );
}
return $segments;
}
}
API/Reports/ParameterException.php 0000644 00000000506 15153746747 0013147 0 ustar 00 <?php
/**
* WooCommerce Admin Input Parameter Exception Class
*
* Exception class thrown when user provides incorrect parameters.
*/
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit;
/**
* API\Reports\ParameterException class.
*/
class ParameterException extends \WC_Data_Exception {}
API/Reports/PerformanceIndicators/Controller.php 0000644 00000044764 15153746747 0015772 0 ustar 00 <?php
/**
* REST API Performance indicators controller
*
* Handles requests to the /reports/store-performance endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators;
use Automattic\WooCommerce\Admin\API\Reports\GenericController;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use WP_REST_Request;
use WP_REST_Response;
defined( 'ABSPATH' ) || exit;
/**
* REST API Reports Performance indicators controller class.
*
* @internal
* @extends GenericController
*/
class Controller extends GenericController {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/performance-indicators';
/**
* Contains a list of endpoints by report slug.
*
* @var array
*/
protected $endpoints = array();
/**
* Contains a list of active Jetpack module slugs.
*
* @var array
*/
protected $active_jetpack_modules = null;
/**
* Contains a list of allowed stats.
*
* @var array
*/
protected $allowed_stats = array();
/**
* Contains a list of stat labels.
*
* @var array
*/
protected $labels = array();
/**
* Contains a list of endpoints by url.
*
* @var array
*/
protected $urls = array();
/**
* Contains a cache of retrieved stats data, grouped by report slug.
*
* @var array
*/
protected $stats_data = array();
/**
* Constructor.
*/
public function __construct() {
add_filter( 'woocommerce_rest_performance_indicators_data_value', array( $this, 'format_data_value' ), 10, 5 );
}
/**
* Register the routes for reports.
*/
public function register_routes() {
parent::register_routes();
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/allowed',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_allowed_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_allowed_item_schema' ),
)
);
}
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['before'] = $request['before'];
$args['after'] = $request['after'];
$args['stats'] = $request['stats'];
return $args;
}
/**
* Get analytics report data and endpoints.
*/
private function get_analytics_report_data() {
$request = new \WP_REST_Request( 'GET', '/wc-analytics/reports' );
$response = rest_do_request( $request );
if ( is_wp_error( $response ) ) {
return $response;
}
if ( 200 !== $response->get_status() ) {
return new \WP_Error( 'woocommerce_analytics_performance_indicators_result_failed', __( 'Sorry, fetching performance indicators failed.', 'woocommerce' ) );
}
$endpoints = $response->get_data();
foreach ( $endpoints as $endpoint ) {
if ( '/stats' === substr( $endpoint['slug'], -6 ) ) {
$request = new \WP_REST_Request( 'OPTIONS', $endpoint['path'] );
$response = rest_do_request( $request );
if ( is_wp_error( $response ) ) {
return $response;
}
$data = $response->get_data();
$prefix = substr( $endpoint['slug'], 0, -6 );
if ( empty( $data['schema']['properties']['totals']['properties'] ) ) {
continue;
}
foreach ( $data['schema']['properties']['totals']['properties'] as $property_key => $schema_info ) {
if ( empty( $schema_info['indicator'] ) || ! $schema_info['indicator'] ) {
continue;
}
$stat = $prefix . '/' . $property_key;
$this->allowed_stats[] = $stat;
$stat_label = empty( $schema_info['title'] ) ? $schema_info['description'] : $schema_info['title'];
$this->labels[ $stat ] = trim( $stat_label, '.' );
$this->formats[ $stat ] = isset( $schema_info['format'] ) ? $schema_info['format'] : 'number';
}
$this->endpoints[ $prefix ] = $endpoint['path'];
$this->urls[ $prefix ] = $endpoint['_links']['report'][0]['href'];
}
}
}
/**
* Get active Jetpack modules.
*
* @return array List of active Jetpack module slugs.
*/
private function get_active_jetpack_modules() {
if ( is_null( $this->active_jetpack_modules ) ) {
if ( class_exists( '\Jetpack' ) && method_exists( '\Jetpack', 'get_active_modules' ) ) {
$active_modules = \Jetpack::get_active_modules();
$this->active_jetpack_modules = is_array( $active_modules ) ? $active_modules : array();
} else {
$this->active_jetpack_modules = array();
}
}
return $this->active_jetpack_modules;
}
/**
* Set active Jetpack modules.
*
* @internal
* @param array $modules List of active Jetpack module slugs.
*/
public function set_active_jetpack_modules( $modules ) {
$this->active_jetpack_modules = $modules;
}
/**
* Get active Jetpack modules and endpoints.
*/
private function get_jetpack_modules_data() {
$active_modules = $this->get_active_jetpack_modules();
if ( empty( $active_modules ) ) {
return;
}
$items = apply_filters(
'woocommerce_rest_performance_indicators_jetpack_items',
array(
'stats/visitors' => array(
'label' => __( 'Visitors', 'woocommerce' ),
'permission' => 'view_stats',
'format' => 'number',
'module' => 'stats',
),
'stats/views' => array(
'label' => __( 'Views', 'woocommerce' ),
'permission' => 'view_stats',
'format' => 'number',
'module' => 'stats',
),
)
);
foreach ( $items as $item_key => $item ) {
if ( ! in_array( $item['module'], $active_modules, true ) ) {
return;
}
if ( $item['permission'] && ! current_user_can( $item['permission'] ) ) {
return;
}
$stat = 'jetpack/' . $item_key;
$endpoint = 'jetpack/' . $item['module'];
$this->allowed_stats[] = $stat;
$this->labels[ $stat ] = $item['label'];
$this->endpoints[ $endpoint ] = '/jetpack/v4/module/' . $item['module'] . '/data';
$this->formats[ $stat ] = $item['format'];
}
$this->urls['jetpack/stats'] = '/jetpack';
}
/**
* Get information such as allowed stats, stat labels, and endpoint data from stats reports.
*
* @return WP_Error|True
*/
private function get_indicator_data() {
// Data already retrieved.
if ( ! empty( $this->endpoints ) && ! empty( $this->labels ) && ! empty( $this->allowed_stats ) ) {
return true;
}
$this->get_analytics_report_data();
$this->get_jetpack_modules_data();
return true;
}
/**
* Returns a list of allowed performance indicators.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_allowed_items( $request ) {
$indicator_data = $this->get_indicator_data();
if ( is_wp_error( $indicator_data ) ) {
return $indicator_data;
}
$data = array();
foreach ( $this->allowed_stats as $stat ) {
$pieces = $this->get_stats_parts( $stat );
$report = $pieces[0];
$chart = $pieces[1];
$data[] = (object) array(
'stat' => $stat,
'chart' => $chart,
'label' => $this->labels[ $stat ],
);
}
usort( $data, array( $this, 'sort' ) );
$objects = array();
foreach ( $data as $item ) {
$prepared = $this->prepare_item_for_response( $item, $request );
$objects[] = $this->prepare_response_for_collection( $prepared );
}
return $this->add_pagination_headers(
$request,
$objects,
(int) count( $data ),
1,
1
);
}
/**
* Sorts the list of stats. Sorted by custom arrangement.
*
* @internal
* @see https://github.com/woocommerce/woocommerce-admin/issues/1282
* @param object $a First item.
* @param object $b Second item.
* @return order
*/
public function sort( $a, $b ) {
/**
* Custom ordering for store performance indicators.
*
* @see https://github.com/woocommerce/woocommerce-admin/issues/1282
* @param array $indicators A list of ordered indicators.
*/
$stat_order = apply_filters(
'woocommerce_rest_report_sort_performance_indicators',
array(
'revenue/total_sales',
'revenue/net_revenue',
'orders/orders_count',
'orders/avg_order_value',
'products/items_sold',
'revenue/refunds',
'coupons/orders_count',
'coupons/amount',
'taxes/total_tax',
'taxes/order_tax',
'taxes/shipping_tax',
'revenue/shipping',
'downloads/download_count',
)
);
$a = array_search( $a->stat, $stat_order, true );
$b = array_search( $b->stat, $stat_order, true );
if ( false === $a && false === $b ) {
return 0;
} elseif ( false === $a ) {
return 1;
} elseif ( false === $b ) {
return -1;
} else {
return $a - $b;
}
}
/**
* Get report stats data, avoiding duplicate requests for stats that use the same endpoint.
*
* @param string $report Report slug to request data for.
* @param array $query_args Report query args.
* @return WP_REST_Response|WP_Error Report stats data.
*/
private function get_stats_data( $report, $query_args ) {
// Return from cache if we've already requested these report stats.
if ( isset( $this->stats_data[ $report ] ) ) {
return $this->stats_data[ $report ];
}
// Request the report stats.
$request_url = $this->endpoints[ $report ];
$request = new \WP_REST_Request( 'GET', $request_url );
$request->set_param( 'before', $query_args['before'] );
$request->set_param( 'after', $query_args['after'] );
$response = rest_do_request( $request );
// Cache the response.
$this->stats_data[ $report ] = $response;
return $response;
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$indicator_data = $this->get_indicator_data();
if ( is_wp_error( $indicator_data ) ) {
return $indicator_data;
}
$query_args = $this->prepare_reports_query( $request );
if ( empty( $query_args['stats'] ) ) {
return new \WP_Error( 'woocommerce_analytics_performance_indicators_empty_query', __( 'A list of stats to query must be provided.', 'woocommerce' ), 400 );
}
$stats = array();
foreach ( $query_args['stats'] as $stat ) {
$is_error = false;
$pieces = $this->get_stats_parts( $stat );
$report = $pieces[0];
$chart = $pieces[1];
if ( ! in_array( $stat, $this->allowed_stats, true ) ) {
continue;
}
$response = $this->get_stats_data( $report, $query_args );
if ( is_wp_error( $response ) ) {
return $response;
}
$data = $response->get_data();
$format = $this->formats[ $stat ];
$label = $this->labels[ $stat ];
if ( 200 !== $response->get_status() ) {
$stats[] = (object) array(
'stat' => $stat,
'chart' => $chart,
'label' => $label,
'format' => $format,
'value' => null,
);
continue;
}
$stats[] = (object) array(
'stat' => $stat,
'chart' => $chart,
'label' => $label,
'format' => $format,
'value' => apply_filters( 'woocommerce_rest_performance_indicators_data_value', $data, $stat, $report, $chart, $query_args ),
);
}
usort( $stats, array( $this, 'sort' ) );
$objects = array();
foreach ( $stats as $stat ) {
$data = $this->prepare_item_for_response( $stat, $request );
$objects[] = $this->prepare_response_for_collection( $data );
}
$response = rest_ensure_response( $objects );
$response->header( 'X-WP-Total', count( $stats ) );
$response->header( 'X-WP-TotalPages', 1 );
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
return $response;
}
/**
* Prepare a report object for serialization.
*
* @param array $stat_data Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $stat_data, $request ) {
$response = parent::prepare_item_for_response( $stat_data, $request );
$response->add_links( $this->prepare_links( $stat_data ) );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_performance_indicators', $response, $stat_data, $request );
}
/**
* Prepare links for the request.
*
* @param \Automattic\WooCommerce\Admin\API\Reports\Query $object Object data.
* @return array
*/
protected function prepare_links( $object ) {
$pieces = $this->get_stats_parts( $object->stat );
$endpoint = $pieces[0];
$stat = $pieces[1];
$url = isset( $this->urls[ $endpoint ] ) ? $this->urls[ $endpoint ] : '';
$links = array(
'api' => array(
'href' => rest_url( $this->endpoints[ $endpoint ] ),
),
'report' => array(
'href' => $url,
),
);
return $links;
}
/**
* Returns the endpoint part of a stat request (prefix) and the actual stat total we want.
* To allow extensions to namespace (example: fue/emails/sent), we break on the last forward slash.
*
* @param string $full_stat A stat request string like orders/avg_order_value or fue/emails/sent.
* @return array Containing the prefix (endpoint) and suffix (stat).
*/
private function get_stats_parts( $full_stat ) {
$endpoint = substr( $full_stat, 0, strrpos( $full_stat, '/' ) );
$stat = substr( $full_stat, ( strrpos( $full_stat, '/' ) + 1 ) );
return array(
$endpoint,
$stat,
);
}
/**
* Format the data returned from the API for given stats.
*
* @param array $data Data from external endpoint.
* @param string $stat Name of the stat.
* @param string $report Name of the report.
* @param string $chart Name of the chart.
* @param array $query_args Query args.
* @return mixed
*/
public function format_data_value( $data, $stat, $report, $chart, $query_args ) {
if ( 'jetpack/stats' === $report ) {
// Get the index of the field to tally.
$index = array_search( $chart, $data['general']->visits->fields, true );
if ( ! $index ) {
return null;
}
// Loop over provided data and filter by the queried date.
// Note that this is currently limited to 30 days via the Jetpack API
// but the WordPress.com endpoint allows up to 90 days.
$total = 0;
$before = gmdate( 'Y-m-d', strtotime( isset( $query_args['before'] ) ? $query_args['before'] : TimeInterval::default_before() ) );
$after = gmdate( 'Y-m-d', strtotime( isset( $query_args['after'] ) ? $query_args['after'] : TimeInterval::default_after() ) );
foreach ( $data['general']->visits->data as $datum ) {
if ( $datum[0] >= $after && $datum[0] <= $before ) {
$total += $datum[ $index ];
}
}
return $total;
}
if ( isset( $data['totals'] ) && isset( $data['totals'][ $chart ] ) ) {
return $data['totals'][ $chart ];
}
return null;
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$indicator_data = $this->get_indicator_data();
if ( is_wp_error( $indicator_data ) ) {
$allowed_stats = array();
} else {
$allowed_stats = $this->allowed_stats;
}
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_performance_indicator',
'type' => 'object',
'properties' => array(
'stat' => array(
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'enum' => $allowed_stats,
),
'chart' => array(
'description' => __( 'The specific chart this stat referrers to.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'label' => array(
'description' => __( 'Human readable label for the stat.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'format' => array(
'description' => __( 'Format of the stat.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'enum' => array( 'number', 'currency' ),
),
'value' => array(
'description' => __( 'Value of the stat. Returns null if the stat does not exist or cannot be loaded.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get schema for the list of allowed performance indicators.
*
* @return array $schema
*/
public function get_public_allowed_item_schema() {
$schema = $this->get_public_item_schema();
unset( $schema['properties']['value'] );
unset( $schema['properties']['format'] );
return $schema;
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$indicator_data = $this->get_indicator_data();
if ( is_wp_error( $indicator_data ) ) {
$allowed_stats = __( 'There was an issue loading the report endpoints', 'woocommerce' );
} else {
$allowed_stats = implode( ', ', $this->allowed_stats );
}
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['stats'] = array(
'description' => sprintf(
/* translators: Allowed values is a list of stat endpoints. */
__( 'Limit response to specific report stats. Allowed values: %s.', 'woocommerce' ),
$allowed_stats
),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
'enum' => $this->allowed_stats,
),
'default' => $this->allowed_stats,
);
$params['after'] = array(
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['before'] = array(
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
}
API/Reports/Products/Controller.php 0000644 00000026673 15153746747 0013313 0 ustar 00 <?php
/**
* REST API Reports products controller
*
* Handles requests to the /reports/products endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Products;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericController;
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
use WP_REST_Request;
use WP_REST_Response;
/**
* REST API Reports products controller class.
*
* @internal
* @extends GenericController
*/
class Controller extends GenericController implements ExportableInterface {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/products';
/**
* Mapping between external parameter name and name used in query class.
*
* @var array
*/
protected $param_mapping = array(
'categories' => 'category_includes',
'products' => 'product_includes',
'variations' => 'variation_includes',
);
/**
* Get items.
*
* @param WP_REST_Request $request Request data.
*
* @return array|WP_Error
*/
public function get_items( $request ) {
$args = array();
$registered = array_keys( $this->get_collection_params() );
foreach ( $registered as $param_name ) {
if ( isset( $request[ $param_name ] ) ) {
if ( isset( $this->param_mapping[ $param_name ] ) ) {
$args[ $this->param_mapping[ $param_name ] ] = $request[ $param_name ];
} else {
$args[ $param_name ] = $request[ $param_name ];
}
}
}
$reports = new Query( $args );
$products_data = $reports->get_data();
$data = array();
foreach ( $products_data->data as $product_data ) {
$item = $this->prepare_item_for_response( $product_data, $request );
if ( isset( $item->data['extended_info']['name'] ) ) {
$item->data['extended_info']['name'] = wp_strip_all_tags( $item->data['extended_info']['name'] );
}
$data[] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$data,
(int) $products_data->total,
(int) $products_data->page_no,
(int) $products_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param Array $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$response = parent::prepare_item_for_response( $report, $request );
$response->add_links( $this->prepare_links( $report ) );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_products', $response, $report, $request );
}
/**
* Prepare links for the request.
*
* @param Array $object Object data.
* @return array Links for the given post.
*/
protected function prepare_links( $object ) {
$links = array(
'product' => array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, 'products', $object['product_id'] ) ),
),
);
return $links;
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_products',
'type' => 'object',
'properties' => array(
'product_id' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product ID.', 'woocommerce' ),
),
'items_sold' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Number of items sold.', 'woocommerce' ),
),
'net_revenue' => array(
'type' => 'number',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Total Net sales of all items sold.', 'woocommerce' ),
),
'orders_count' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Number of orders product appeared in.', 'woocommerce' ),
),
'extended_info' => array(
'name' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product name.', 'woocommerce' ),
),
'price' => array(
'type' => 'number',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product price.', 'woocommerce' ),
),
'image' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product image.', 'woocommerce' ),
),
'permalink' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product link.', 'woocommerce' ),
),
'category_ids' => array(
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product category IDs.', 'woocommerce' ),
),
'stock_status' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product inventory status.', 'woocommerce' ),
),
'stock_quantity' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product inventory quantity.', 'woocommerce' ),
),
'low_stock_amount' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product inventory threshold for low stock.', 'woocommerce' ),
),
'variations' => array(
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product variations IDs.', 'woocommerce' ),
),
'sku' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product SKU.', 'woocommerce' ),
),
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['orderby']['enum'] = array(
'date',
'net_revenue',
'orders_count',
'items_sold',
'product_name',
'variations',
'sku',
);
$params['categories'] = array(
'description' => __( 'Limit result to items from the specified categories.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['match'] = array(
'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'woocommerce' ),
'type' => 'string',
'default' => 'all',
'enum' => array(
'all',
'any',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['products'] = array(
'description' => __( 'Limit result to items with specified product ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['extended_info'] = array(
'description' => __( 'Add additional piece of info about each product to the report.', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'sanitize_callback' => 'wc_string_to_bool',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get stock status column export value.
*
* @param array $status Stock status from report row.
* @return string
*/
protected function get_stock_status( $status ) {
$statuses = wc_get_product_stock_status_options();
return isset( $statuses[ $status ] ) ? $statuses[ $status ] : '';
}
/**
* Get categories column export value.
*
* @param array $category_ids Category IDs from report row.
* @return string
*/
protected function get_categories( $category_ids ) {
$category_names = get_terms(
array(
'taxonomy' => 'product_cat',
'include' => $category_ids,
'fields' => 'names',
)
);
return implode( ', ', $category_names );
}
/**
* Get the column names for export.
*
* @return array Key value pair of Column ID => Label.
*/
public function get_export_columns() {
$export_columns = array(
'product_name' => __( 'Product title', 'woocommerce' ),
'sku' => __( 'SKU', 'woocommerce' ),
'items_sold' => __( 'Items sold', 'woocommerce' ),
'net_revenue' => __( 'N. Revenue', 'woocommerce' ),
'orders_count' => __( 'Orders', 'woocommerce' ),
'product_cat' => __( 'Category', 'woocommerce' ),
'variations' => __( 'Variations', 'woocommerce' ),
);
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
$export_columns['stock_status'] = __( 'Status', 'woocommerce' );
$export_columns['stock'] = __( 'Stock', 'woocommerce' );
}
/**
* Filter to add or remove column names from the products report for
* export.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_report_products_export_columns',
$export_columns
);
}
/**
* Get the column values for export.
*
* @param array $item Single report item/row.
* @return array Key value pair of Column ID => Row Value.
*/
public function prepare_item_for_export( $item ) {
$export_item = array(
'product_name' => $item['extended_info']['name'],
'sku' => $item['extended_info']['sku'],
'items_sold' => $item['items_sold'],
'net_revenue' => $item['net_revenue'],
'orders_count' => $item['orders_count'],
'product_cat' => $this->get_categories( $item['extended_info']['category_ids'] ),
'variations' => isset( $item['extended_info']['variations'] ) ? count( $item['extended_info']['variations'] ) : 0,
);
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
if ( $item['extended_info']['manage_stock'] ) {
$export_item['stock_status'] = $this->get_stock_status( $item['extended_info']['stock_status'] );
$export_item['stock'] = $item['extended_info']['stock_quantity'];
} else {
$export_item['stock_status'] = __( 'N/A', 'woocommerce' );
$export_item['stock'] = __( 'N/A', 'woocommerce' );
}
}
/**
* Filter to prepare extra columns in the export item for the products
* report.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_report_products_prepare_export_item',
$export_item,
$item
);
}
}
API/Reports/Products/DataStore.php 0000644 00000042526 15153746747 0013051 0 ustar 00 <?php
/**
* API\Reports\Products\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Products;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
use Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
/**
* API\Reports\Products\DataStore.
*/
class DataStore extends ReportsDataStore implements DataStoreInterface {
/**
* Table used to get the data.
*
* @var string
*/
protected static $table_name = 'wc_order_product_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'products';
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'date_start' => 'strval',
'date_end' => 'strval',
'product_id' => 'intval',
'items_sold' => 'intval',
'net_revenue' => 'floatval',
'orders_count' => 'intval',
// Extended attributes.
'name' => 'strval',
'price' => 'floatval',
'image' => 'strval',
'permalink' => 'strval',
'stock_status' => 'strval',
'stock_quantity' => 'intval',
'low_stock_amount' => 'intval',
'category_ids' => 'array_values',
'variations' => 'array_values',
'sku' => 'strval',
);
/**
* Extended product attributes to include in the data.
*
* @var array
*/
protected $extended_attributes = array(
'name',
'price',
'image',
'permalink',
'stock_status',
'stock_quantity',
'manage_stock',
'low_stock_amount',
'category_ids',
'variations',
'sku',
);
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'products';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$table_name = self::get_db_table_name();
$this->report_columns = array(
'product_id' => 'product_id',
'items_sold' => 'SUM(product_qty) as items_sold',
'net_revenue' => 'SUM(product_net_revenue) AS net_revenue',
'orders_count' => "COUNT( DISTINCT ( CASE WHEN product_gross_revenue >= 0 THEN {$table_name}.order_id END ) ) as orders_count",
);
}
/**
* Set up all the hooks for maintaining and populating table data.
*/
public static function init() {
add_action( 'woocommerce_analytics_delete_order_stats', array( __CLASS__, 'sync_on_order_delete' ), 10 );
}
/**
* Fills FROM clause of SQL request based on user supplied parameters.
*
* @param array $query_args Parameters supplied by the user.
* @param string $arg_name Target of the JOIN sql param.
* @param string $id_cell ID cell identifier, like `table_name.id_column_name`.
*/
protected function add_from_sql_params( $query_args, $arg_name, $id_cell ) {
global $wpdb;
$type = 'join';
// Order by product name requires extra JOIN.
switch ( $query_args['orderby'] ) {
case 'product_name':
$join = " JOIN {$wpdb->posts} AS _products ON {$id_cell} = _products.ID";
break;
case 'sku':
$join = " LEFT JOIN {$wpdb->postmeta} AS postmeta ON {$id_cell} = postmeta.post_id AND postmeta.meta_key = '_sku'";
break;
case 'variations':
$type = 'left_join';
$join = "LEFT JOIN ( SELECT post_parent, COUNT(*) AS variations FROM {$wpdb->posts} WHERE post_type = 'product_variation' GROUP BY post_parent ) AS _variations ON {$id_cell} = _variations.post_parent";
break;
default:
$join = '';
break;
}
if ( $join ) {
if ( 'inner' === $arg_name ) {
$this->subquery->add_sql_clause( $type, $join );
} else {
$this->add_sql_clause( $type, $join );
}
}
}
/**
* Updates the database query with parameters used for Products report: categories and order status.
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function add_sql_query_params( $query_args ) {
global $wpdb;
$order_product_lookup_table = self::get_db_table_name();
$this->add_time_period_sql_params( $query_args, $order_product_lookup_table );
$this->get_limit_sql_params( $query_args );
$this->add_order_by_sql_params( $query_args );
$included_products = $this->get_included_products( $query_args );
if ( $included_products ) {
$this->add_from_sql_params( $query_args, 'outer', 'default_results.product_id' );
$this->subquery->add_sql_clause( 'where', "AND {$order_product_lookup_table}.product_id IN ({$included_products})" );
} else {
$this->add_from_sql_params( $query_args, 'inner', "{$order_product_lookup_table}.product_id" );
}
$included_variations = $this->get_included_variations( $query_args );
if ( $included_variations ) {
$this->subquery->add_sql_clause( 'where', "AND {$order_product_lookup_table}.variation_id IN ({$included_variations})" );
}
$order_status_filter = $this->get_status_subquery( $query_args );
if ( $order_status_filter ) {
$this->subquery->add_sql_clause( 'join', "JOIN {$wpdb->prefix}wc_order_stats ON {$order_product_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id" );
$this->subquery->add_sql_clause( 'where', "AND ( {$order_status_filter} )" );
}
}
/**
* Maps ordering specified by the user to columns in the database/fields in the data.
*
* @param string $order_by Sorting criterion.
* @return string
*/
protected function normalize_order_by( $order_by ) {
if ( 'date' === $order_by ) {
return self::get_db_table_name() . '.date_created';
}
if ( 'product_name' === $order_by ) {
return 'post_title';
}
if ( 'sku' === $order_by ) {
return 'meta_value';
}
return $order_by;
}
/**
* Enriches the product data with attributes specified by the extended_attributes.
*
* @param array $products_data Product data.
* @param array $query_args Query parameters.
*/
protected function include_extended_info( &$products_data, $query_args ) {
global $wpdb;
$product_names = array();
foreach ( $products_data as $key => $product_data ) {
$extended_info = new \ArrayObject();
if ( $query_args['extended_info'] ) {
$product_id = $product_data['product_id'];
$product = wc_get_product( $product_id );
// Product was deleted.
if ( ! $product ) {
if ( ! isset( $product_names[ $product_id ] ) ) {
$product_names[ $product_id ] = $wpdb->get_var(
$wpdb->prepare(
"SELECT i.order_item_name
FROM {$wpdb->prefix}woocommerce_order_items i, {$wpdb->prefix}woocommerce_order_itemmeta m
WHERE i.order_item_id = m.order_item_id
AND m.meta_key = '_product_id'
AND m.meta_value = %s
ORDER BY i.order_item_id DESC
LIMIT 1",
$product_id
)
);
}
/* translators: %s is product name */
$products_data[ $key ]['extended_info']['name'] = $product_names[ $product_id ] ? sprintf( __( '%s (Deleted)', 'woocommerce' ), $product_names[ $product_id ] ) : __( '(Deleted)', 'woocommerce' );
continue;
}
$extended_attributes = apply_filters( 'woocommerce_rest_reports_products_extended_attributes', $this->extended_attributes, $product_data );
foreach ( $extended_attributes as $extended_attribute ) {
if ( 'variations' === $extended_attribute ) {
if ( ! $product->is_type( 'variable' ) ) {
continue;
}
$function = 'get_children';
} else {
$function = 'get_' . $extended_attribute;
}
if ( is_callable( array( $product, $function ) ) ) {
$value = $product->{$function}();
$extended_info[ $extended_attribute ] = $value;
}
}
// If there is no set low_stock_amount, use the one in user settings.
if ( '' === $extended_info['low_stock_amount'] ) {
$extended_info['low_stock_amount'] = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
}
$extended_info = $this->cast_numbers( $extended_info );
}
$products_data[ $key ]['extended_info'] = $extended_info;
}
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$table_name = self::get_db_table_name();
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
'fields' => '*',
'category_includes' => array(),
'product_includes' => array(),
'extended_info' => false,
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$data = (object) array(
'data' => array(),
'total' => 0,
'pages' => 0,
'page_no' => 0,
);
$selections = $this->selected_columns( $query_args );
$included_products = $this->get_included_products_array( $query_args );
$params = $this->get_limit_params( $query_args );
$this->add_sql_query_params( $query_args );
if ( count( $included_products ) > 0 ) {
$filtered_products = array_diff( $included_products, array( '-1' ) );
$total_results = count( $filtered_products );
$total_pages = (int) ceil( $total_results / $params['per_page'] );
if ( 'date' === $query_args['orderby'] ) {
$selections .= ", {$table_name}.date_created";
}
$fields = $this->get_fields( $query_args );
$join_selections = $this->format_join_selections( $fields, array( 'product_id' ) );
$ids_table = $this->get_ids_table( $included_products, 'product_id' );
$this->subquery->clear_sql_clause( 'select' );
$this->subquery->add_sql_clause( 'select', $selections );
$this->add_sql_clause( 'select', $join_selections );
$this->add_sql_clause( 'from', '(' );
$this->add_sql_clause( 'from', $this->subquery->get_query_statement() );
$this->add_sql_clause( 'from', ") AS {$table_name}" );
$this->add_sql_clause(
'right_join',
"RIGHT JOIN ( {$ids_table} ) AS default_results
ON default_results.product_id = {$table_name}.product_id"
);
$this->add_sql_clause( 'where', 'AND default_results.product_id != -1' );
$products_query = $this->get_query_statement();
} else {
$count_query = "SELECT COUNT(*) FROM (
{$this->subquery->get_query_statement()}
) AS tt";
$db_records_count = (int) $wpdb->get_var(
$count_query // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
);
$total_results = $db_records_count;
$total_pages = (int) ceil( $db_records_count / $params['per_page'] );
if ( ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) ) {
return $data;
}
$this->subquery->clear_sql_clause( 'select' );
$this->subquery->add_sql_clause( 'select', $selections );
$this->subquery->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$this->subquery->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
$products_query = $this->subquery->get_query_statement();
}
$product_data = $wpdb->get_results(
$products_query, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
ARRAY_A
);
if ( null === $product_data ) {
return $data;
}
$product_data = array_map( array( $this, 'cast_numbers' ), $product_data );
$data = (object) array(
'data' => $product_data,
'total' => $total_results,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
$this->set_cached_data( $cache_key, $data );
}
$this->include_extended_info( $data->data, $query_args );
return $data;
}
/**
* Create or update an entry in the wc_admin_order_product_lookup table for an order.
*
* @since 3.5.0
* @param int $order_id Order ID.
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
*/
public static function sync_order_products( $order_id ) {
global $wpdb;
$order = wc_get_order( $order_id );
if ( ! $order ) {
return -1;
}
$table_name = self::get_db_table_name();
$existing_items = $wpdb->get_col(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT order_item_id FROM {$table_name} WHERE order_id = %d",
$order_id
)
);
$existing_items = array_flip( $existing_items );
$order_items = $order->get_items();
$num_updated = 0;
$decimals = wc_get_price_decimals();
$round_tax = 'no' === get_option( 'woocommerce_tax_round_at_subtotal' );
foreach ( $order_items as $order_item ) {
$order_item_id = $order_item->get_id();
unset( $existing_items[ $order_item_id ] );
$product_qty = $order_item->get_quantity( 'edit' );
$shipping_amount = $order->get_item_shipping_amount( $order_item );
$shipping_tax_amount = $order->get_item_shipping_tax_amount( $order_item );
$coupon_amount = $order->get_item_coupon_amount( $order_item );
// Skip line items without changes to product quantity.
if ( ! $product_qty ) {
$num_updated++;
continue;
}
// Tax amount.
$tax_amount = 0;
$order_taxes = $order->get_taxes();
$tax_data = $order_item->get_taxes();
foreach ( $order_taxes as $tax_item ) {
$tax_item_id = $tax_item->get_rate_id();
$tax_amount += isset( $tax_data['total'][ $tax_item_id ] ) ? (float) $tax_data['total'][ $tax_item_id ] : 0;
}
$net_revenue = round( $order_item->get_total( 'edit' ), $decimals );
if ( $round_tax ) {
$tax_amount = round( $tax_amount, $decimals );
}
$result = $wpdb->replace(
self::get_db_table_name(),
array(
'order_item_id' => $order_item_id,
'order_id' => $order->get_id(),
'product_id' => wc_get_order_item_meta( $order_item_id, '_product_id' ),
'variation_id' => wc_get_order_item_meta( $order_item_id, '_variation_id' ),
'customer_id' => $order->get_report_customer_id(),
'product_qty' => $product_qty,
'product_net_revenue' => $net_revenue,
'date_created' => $order->get_date_created( 'edit' )->date( TimeInterval::$sql_datetime_format ),
'coupon_amount' => $coupon_amount,
'tax_amount' => $tax_amount,
'shipping_amount' => $shipping_amount,
'shipping_tax_amount' => $shipping_tax_amount,
// @todo Can this be incorrect if modified by filters?
'product_gross_revenue' => $net_revenue + $tax_amount + $shipping_amount + $shipping_tax_amount,
),
array(
'%d', // order_item_id.
'%d', // order_id.
'%d', // product_id.
'%d', // variation_id.
'%d', // customer_id.
'%d', // product_qty.
'%f', // product_net_revenue.
'%s', // date_created.
'%f', // coupon_amount.
'%f', // tax_amount.
'%f', // shipping_amount.
'%f', // shipping_tax_amount.
'%f', // product_gross_revenue.
)
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
/**
* Fires when product's reports are updated.
*
* @param int $order_item_id Order Item ID.
* @param int $order_id Order ID.
*/
do_action( 'woocommerce_analytics_update_product', $order_item_id, $order->get_id() );
// Sum the rows affected. Using REPLACE can affect 2 rows if the row already exists.
$num_updated += 2 === intval( $result ) ? 1 : intval( $result );
}
if ( ! empty( $existing_items ) ) {
$existing_items = array_flip( $existing_items );
$format = array_fill( 0, count( $existing_items ), '%d' );
$format = implode( ',', $format );
array_unshift( $existing_items, $order_id );
$wpdb->query(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"DELETE FROM {$table_name} WHERE order_id = %d AND order_item_id in ({$format})",
$existing_items
)
);
}
return ( count( $order_items ) === $num_updated );
}
/**
* Clean products data when an order is deleted.
*
* @param int $order_id Order ID.
*/
public static function sync_on_order_delete( $order_id ) {
global $wpdb;
$wpdb->delete( self::get_db_table_name(), array( 'order_id' => $order_id ) );
/**
* Fires when product's reports are removed from database.
*
* @param int $product_id Product ID.
* @param int $order_id Order ID.
*/
do_action( 'woocommerce_analytics_delete_product', 0, $order_id );
ReportsCache::invalidate();
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
$this->subquery = new SqlQuery( $this->context . '_subquery' );
$this->subquery->add_sql_clause( 'select', 'product_id' );
$this->subquery->add_sql_clause( 'from', self::get_db_table_name() );
$this->subquery->add_sql_clause( 'group_by', 'product_id' );
}
}
API/Reports/Products/Query.php 0000644 00000002352 15153746747 0012261 0 ustar 00 <?php
/**
* Class for parameter-based Products Report querying
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'page' => 2,
* 'categories' => array(15, 18),
* 'products' => array(1,2,3)
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Products\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Products;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Products\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Products report.
*
* @return array
*/
protected function get_default_query_vars() {
return array();
}
/**
* Get product data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_products_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-products' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_products_select_query', $results, $args );
}
}
API/Reports/Products/Stats/Controller.php 0000644 00000017310 15153746747 0014375 0 ustar 00 <?php
/**
* REST API Reports products stats controller
*
* Handles requests to the /reports/products/stats endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Products\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericStatsController;
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
use WP_REST_Request;
use WP_REST_Response;
/**
* REST API Reports products stats controller class.
*
* @internal
* @extends GenericStatsController
*/
class Controller extends GenericStatsController {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/products/stats';
/**
* Mapping between external parameter name and name used in query class.
*
* @var array
*/
protected $param_mapping = array(
'categories' => 'category_includes',
'products' => 'product_includes',
'variations' => 'variation_includes',
);
/**
* Constructor.
*/
public function __construct() {
add_filter( 'woocommerce_analytics_products_stats_select_query', array( $this, 'set_default_report_data' ) );
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$query_args = array(
'fields' => array(
'items_sold',
'net_revenue',
'orders_count',
'products_count',
'variations_count',
),
);
$registered = array_keys( $this->get_collection_params() );
foreach ( $registered as $param_name ) {
if ( isset( $request[ $param_name ] ) ) {
if ( isset( $this->param_mapping[ $param_name ] ) ) {
$query_args[ $this->param_mapping[ $param_name ] ] = $request[ $param_name ];
} else {
$query_args[ $param_name ] = $request[ $param_name ];
}
}
}
$query = new Query( $query_args );
try {
$report_data = $query->get_data();
} catch ( ParameterException $e ) {
return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
$out_data = array(
'totals' => get_object_vars( $report_data->totals ),
'intervals' => array(),
);
foreach ( $report_data->intervals as $interval_data ) {
$item = $this->prepare_item_for_response( $interval_data, $request );
$out_data['intervals'][] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$out_data,
(int) $report_data->total,
(int) $report_data->page_no,
(int) $report_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param array $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$response = parent::prepare_item_for_response( $report, $request );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_products_stats', $response, $report, $request );
}
/**
* Get the Report's item properties schema.
* Will be used by `get_item_schema` as `totals` and `subtotals`.
*
* @return array
*/
protected function get_item_properties_schema() {
return array(
'items_sold' => array(
'title' => __( 'Products sold', 'woocommerce' ),
'description' => __( 'Number of product items sold.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
),
'net_revenue' => array(
'description' => __( 'Net sales.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'format' => 'currency',
),
'orders_count' => array(
'description' => __( 'Number of orders.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
);
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = parent::get_item_schema();
$schema['title'] = 'report_products_stats';
$segment_label = array(
'description' => __( 'Human readable segment label, either product or variation name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'enum' => array( 'day', 'week', 'month', 'year' ),
);
$schema['properties']['totals']['properties']['segments']['items']['properties']['segment_label'] = $segment_label;
$schema['properties']['intervals']['items']['properties']['subtotals']['properties']['segments']['items']['properties']['segment_label'] = $segment_label;
return $this->add_additional_fields_schema( $schema );
}
/**
* Set the default results to 0 if API returns an empty array
*
* @internal
* @param Mixed $results Report data.
* @return object
*/
public function set_default_report_data( $results ) {
if ( empty( $results ) ) {
$results = new \stdClass();
$results->total = 0;
$results->totals = new \stdClass();
$results->totals->items_sold = 0;
$results->totals->net_revenue = 0;
$results->totals->orders_count = 0;
$results->intervals = array();
$results->pages = 1;
$results->page_no = 1;
}
return $results;
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['orderby']['enum'] = array(
'date',
'net_revenue',
'coupons',
'refunds',
'shipping',
'taxes',
'net_revenue',
'orders_count',
'items_sold',
);
$params['categories'] = array(
'description' => __( 'Limit result to items from the specified categories.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['products'] = array(
'description' => __( 'Limit result to items with specified product ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['variations'] = array(
'description' => __( 'Limit result to items with specified variation ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['segmentby'] = array(
'description' => __( 'Segment the response by additional constraint.', 'woocommerce' ),
'type' => 'string',
'enum' => array(
'product',
'category',
'variation',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['fields'] = array(
'description' => __( 'Limit stats fields to the specified items.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
return $params;
}
}
API/Reports/Products/Stats/DataStore.php 0000644 00000023373 15153746747 0014146 0 ustar 00 <?php
/**
* API\Reports\Products\Stats\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Products\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Products\DataStore as ProductsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
/**
* API\Reports\Products\Stats\DataStore.
*/
class DataStore extends ProductsDataStore implements DataStoreInterface {
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'date_start' => 'strval',
'date_end' => 'strval',
'product_id' => 'intval',
'items_sold' => 'intval',
'net_revenue' => 'floatval',
'orders_count' => 'intval',
'products_count' => 'intval',
'variations_count' => 'intval',
);
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'products_stats';
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'products_stats';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$table_name = self::get_db_table_name();
$this->report_columns = array(
'items_sold' => 'SUM(product_qty) as items_sold',
'net_revenue' => 'SUM(product_net_revenue) AS net_revenue',
'orders_count' => "COUNT( DISTINCT ( CASE WHEN product_gross_revenue >= 0 THEN {$table_name}.order_id END ) ) as orders_count",
'products_count' => 'COUNT(DISTINCT product_id) as products_count',
'variations_count' => 'COUNT(DISTINCT variation_id) as variations_count',
);
}
/**
* Updates the database query with parameters used for Products Stats report: categories and order status.
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function update_sql_query_params( $query_args ) {
global $wpdb;
$products_where_clause = '';
$products_from_clause = '';
$order_product_lookup_table = self::get_db_table_name();
$included_products = $this->get_included_products( $query_args );
if ( $included_products ) {
$products_where_clause .= " AND {$order_product_lookup_table}.product_id IN ({$included_products})";
}
$included_variations = $this->get_included_variations( $query_args );
if ( $included_variations ) {
$products_where_clause .= " AND {$order_product_lookup_table}.variation_id IN ({$included_variations})";
}
$order_status_filter = $this->get_status_subquery( $query_args );
if ( $order_status_filter ) {
$products_from_clause .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_product_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id";
$products_where_clause .= " AND ( {$order_status_filter} )";
}
$this->add_time_period_sql_params( $query_args, $order_product_lookup_table );
$this->total_query->add_sql_clause( 'where', $products_where_clause );
$this->total_query->add_sql_clause( 'join', $products_from_clause );
$this->add_intervals_sql_params( $query_args, $order_product_lookup_table );
$this->interval_query->add_sql_clause( 'where', $products_where_clause );
$this->interval_query->add_sql_clause( 'join', $products_from_clause );
$this->interval_query->add_sql_clause( 'select', $this->get_sql_clause( 'select' ) . ' AS time_interval' );
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @since 3.5.0
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$table_name = self::get_db_table_name();
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
'fields' => '*',
'category_includes' => array(),
'interval' => 'week',
'product_includes' => array(),
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$selections = $this->selected_columns( $query_args );
$params = $this->get_limit_params( $query_args );
$this->update_sql_query_params( $query_args );
$this->get_limit_sql_params( $query_args );
$this->interval_query->add_sql_clause( 'where_time', $this->get_sql_clause( 'where_time' ) );
$db_intervals = $wpdb->get_col(
$this->interval_query->get_query_statement()
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
$db_interval_count = count( $db_intervals );
$expected_interval_count = TimeInterval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $params['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return array();
}
$intervals = array();
$this->update_intervals_sql_params( $query_args, $db_interval_count, $expected_interval_count, $table_name );
$this->total_query->add_sql_clause( 'select', $selections );
$this->total_query->add_sql_clause( 'where_time', $this->get_sql_clause( 'where_time' ) );
$totals = $wpdb->get_results(
$this->total_query->get_query_statement(),
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
// @todo remove these assignements when refactoring segmenter classes to use query objects.
$totals_query = array(
'from_clause' => $this->total_query->get_sql_clause( 'join' ),
'where_time_clause' => $this->total_query->get_sql_clause( 'where_time' ),
'where_clause' => $this->total_query->get_sql_clause( 'where' ),
);
$intervals_query = array(
'select_clause' => $this->get_sql_clause( 'select' ),
'from_clause' => $this->interval_query->get_sql_clause( 'join' ),
'where_time_clause' => $this->interval_query->get_sql_clause( 'where_time' ),
'where_clause' => $this->interval_query->get_sql_clause( 'where' ),
'order_by' => $this->get_sql_clause( 'order_by' ),
'limit' => $this->get_sql_clause( 'limit' ),
);
$segmenter = new Segmenter( $query_args, $this->report_columns );
$totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name );
if ( null === $totals ) {
return new \WP_Error( 'woocommerce_analytics_products_stats_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce' ) );
}
$this->interval_query->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$this->interval_query->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
$this->interval_query->add_sql_clause( 'select', ", MAX({$table_name}.date_created) AS datetime_anchor" );
if ( '' !== $selections ) {
$this->interval_query->add_sql_clause( 'select', ', ' . $selections );
}
$intervals = $wpdb->get_results(
$this->interval_query->get_query_statement(),
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $intervals ) {
return new \WP_Error( 'woocommerce_analytics_products_stats_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce' ) );
}
$totals = (object) $this->cast_numbers( $totals[0] );
$data = (object) array(
'totals' => $totals,
'intervals' => $intervals,
'total' => $expected_interval_count,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
if ( TimeInterval::intervals_missing( $expected_interval_count, $db_interval_count, $params['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
$this->remove_extra_records( $data, $query_args['page'], $params['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] );
} else {
$this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals );
}
$segmenter->add_intervals_segments( $data, $intervals_query, $table_name );
$this->create_interval_subtotals( $data->intervals );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Normalizes order_by clause to match to SQL query.
*
* @param string $order_by Order by option requeste by user.
* @return string
*/
protected function normalize_order_by( $order_by ) {
if ( 'date' === $order_by ) {
return 'time_interval';
}
return $order_by;
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
unset( $this->subquery );
$this->total_query = new SqlQuery( $this->context . '_total' );
$this->total_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query = new SqlQuery( $this->context . '_interval' );
$this->interval_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query->add_sql_clause( 'group_by', 'time_interval' );
}
}
API/Reports/Products/Stats/Query.php 0000644 00000002425 15153746747 0013360 0 ustar 00 <?php
/**
* Class for parameter-based Products Stats Report querying
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'page' => 2,
* 'categories' => array(15, 18),
* 'product_ids' => array(1,2,3)
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Products\Stats\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Products\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Products\Stats\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Products report.
*
* @return array
*/
protected function get_default_query_vars() {
return array();
}
/**
* Get product data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_products_stats_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-products-stats' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_products_stats_select_query', $results, $args );
}
}
API/Reports/Products/Stats/Segmenter.php 0000644 00000024312 15153746747 0014203 0 ustar 00 <?php
/**
* Class for adding segmenting support without cluttering the data stores.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Products\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter;
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
/**
* Date & time interval and numeric range handling class for Reporting API.
*/
class Segmenter extends ReportsSegmenter {
/**
* Returns column => query mapping to be used for product-related product-level segmenting query
* (e.g. products sold, revenue from product X when segmenting by category).
*
* @param string $products_table Name of SQL table containing the product-level segmenting info.
*
* @return array Column => SELECT query mapping.
*/
protected function get_segment_selections_product_level( $products_table ) {
$columns_mapping = array(
'items_sold' => "SUM($products_table.product_qty) as items_sold",
'net_revenue' => "SUM($products_table.product_net_revenue ) AS net_revenue",
'orders_count' => "COUNT( DISTINCT $products_table.order_id ) AS orders_count",
'products_count' => "COUNT( DISTINCT $products_table.product_id ) AS products_count",
'variations_count' => "COUNT( DISTINCT $products_table.variation_id ) AS variations_count",
);
return $columns_mapping;
}
/**
* Calculate segments for totals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
*
* @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $segmenting_dimension_name Name of the segmenting dimension.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $totals_query Array of SQL clauses for totals query.
* @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
*
* @return array
*/
protected function get_product_related_totals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $totals_query, $unique_orders_table ) {
global $wpdb;
$product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
// Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued).
// Product-level numbers.
$segments_products = $wpdb->get_results(
"SELECT
$segmenting_groupby AS $segmenting_dimension_name
{$segmenting_selections['product_level']}
FROM
$table_name
$segmenting_from
{$totals_query['from_clause']}
WHERE
1=1
{$totals_query['where_time_clause']}
{$totals_query['where_clause']}
$segmenting_where
GROUP BY
$segmenting_groupby",
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
$totals_segments = $this->merge_segment_totals_results( $segmenting_dimension_name, $segments_products, array() );
return $totals_segments;
}
/**
* Calculate segments for intervals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
*
* @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $segmenting_dimension_name Name of the segmenting dimension.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $intervals_query Array of SQL clauses for intervals query.
* @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
*
* @return array
*/
protected function get_product_related_intervals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $intervals_query, $unique_orders_table ) {
global $wpdb;
$product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
// LIMIT offset, rowcount needs to be updated to a multiple of the number of segments.
preg_match( '/LIMIT (\d+)\s?,\s?(\d+)/', $intervals_query['limit'], $limit_parts );
$segment_count = count( $this->get_all_segments() );
$orig_offset = intval( $limit_parts[1] );
$orig_rowcount = intval( $limit_parts[2] );
$segmenting_limit = $wpdb->prepare( 'LIMIT %d, %d', $orig_offset * $segment_count, $orig_rowcount * $segment_count );
// Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued).
// Product-level numbers.
$segments_products = $wpdb->get_results(
"SELECT
{$intervals_query['select_clause']} AS time_interval,
$segmenting_groupby AS $segmenting_dimension_name
{$segmenting_selections['product_level']}
FROM
$table_name
$segmenting_from
{$intervals_query['from_clause']}
WHERE
1=1
{$intervals_query['where_time_clause']}
{$intervals_query['where_clause']}
$segmenting_where
GROUP BY
time_interval, $segmenting_groupby
$segmenting_limit",
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
$intervals_segments = $this->merge_segment_intervals_results( $segmenting_dimension_name, $segments_products, array() );
return $intervals_segments;
}
/**
* Return array of segments formatted for REST response.
*
* @param string $type Type of segments to return--'totals' or 'intervals'.
* @param array $query_params SQL query parameter array.
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
*
* @return array
* @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified.
*/
protected function get_segments( $type, $query_params, $table_name ) {
global $wpdb;
if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) {
return array();
}
$product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
$unique_orders_table = 'uniq_orders';
$segmenting_where = '';
// Product, variation, and category are bound to product, so here product segmenting table is required,
// while coupon and customer are bound to order, so we don't need the extra JOIN for those.
// This also means that segment selections need to be calculated differently.
if ( 'product' === $this->query_args['segmentby'] ) {
$product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
$segmenting_selections = array(
'product_level' => $this->prepare_selections( $product_level_columns ),
);
$this->report_columns = $product_level_columns;
$segmenting_from = '';
$segmenting_groupby = $product_segmenting_table . '.product_id';
$segmenting_dimension_name = 'product_id';
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'variation' === $this->query_args['segmentby'] ) {
if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) {
throw new ParameterException( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'woocommerce' ) );
}
$product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
$segmenting_selections = array(
'product_level' => $this->prepare_selections( $product_level_columns ),
);
$this->report_columns = $product_level_columns;
$segmenting_from = '';
$segmenting_where = "AND $product_segmenting_table.product_id = {$this->query_args['product_includes'][0]}";
$segmenting_groupby = $product_segmenting_table . '.variation_id';
$segmenting_dimension_name = 'variation_id';
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'category' === $this->query_args['segmentby'] ) {
$product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
$segmenting_selections = array(
'product_level' => $this->prepare_selections( $product_level_columns ),
);
$this->report_columns = $product_level_columns;
$segmenting_from = "
LEFT JOIN {$wpdb->term_relationships} ON {$product_segmenting_table}.product_id = {$wpdb->term_relationships}.object_id
JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
LEFT JOIN {$wpdb->wc_category_lookup} ON {$wpdb->term_taxonomy}.term_id = {$wpdb->wc_category_lookup}.category_id
";
$segmenting_where = " AND {$wpdb->wc_category_lookup}.category_tree_id IS NOT NULL";
$segmenting_groupby = "{$wpdb->wc_category_lookup}.category_tree_id";
$segmenting_dimension_name = 'category_id';
// Restrict our search space for category comparisons.
if ( isset( $this->query_args['category_includes'] ) ) {
$category_ids = implode( ',', $this->get_all_segments() );
$segmenting_where .= " AND {$wpdb->wc_category_lookup}.category_id IN ( $category_ids )";
}
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
}
return $segments;
}
}
API/Reports/Query.php 0000644 00000001121 15153746747 0010447 0 ustar 00 <?php
/**
* Class for parameter-based Reports querying
*/
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit;
/**
* Admin\API\Reports\Query
*/
abstract class Query extends \WC_Object_Query {
/**
* Get report data matching the current query vars.
*
* @return array|object of WC_Product objects
*/
public function get_data() {
/* translators: %s: Method name */
return new \WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass.", 'woocommerce' ), __METHOD__ ), array( 'status' => 405 ) );
}
}
API/Reports/Revenue/Query.php 0000644 00000003077 15153746747 0012074 0 ustar 00 <?php
/**
* Class for parameter-based Revenue Reports querying
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'interval' => 'week',
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Revenue\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Revenue;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Revenue\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Revenue report.
*
* @return array
*/
protected function get_default_query_vars() {
return array(
'per_page' => get_option( 'posts_per_page' ), // not sure if this should be the default.
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => '',
'after' => '',
'interval' => 'week',
'fields' => array(
'orders_count',
'num_items_sold',
'total_sales',
'coupons',
'coupons_count',
'refunds',
'taxes',
'shipping',
'net_revenue',
'gross_sales',
),
);
}
/**
* Get revenue data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_revenue_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-revenue-stats' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_revenue_select_query', $results, $args );
}
}
API/Reports/Revenue/Stats/Controller.php 0000644 00000022444 15153746747 0014207 0 ustar 00 <?php
/**
* REST API Reports revenue stats controller
*
* Handles requests to the /reports/revenue/stats endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Revenue\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericStatsController;
use Automattic\WooCommerce\Admin\API\Reports\Revenue\Query as RevenueQuery;
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits;
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
use WP_REST_Request;
use WP_REST_Response;
/**
* REST API Reports revenue stats controller class.
*
* @internal
* @extends GenericStatsController
*/
class Controller extends GenericStatsController implements ExportableInterface {
/**
* Exportable traits.
*/
use ExportableTraits;
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/revenue/stats';
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['before'] = $request['before'];
$args['after'] = $request['after'];
$args['interval'] = $request['interval'];
$args['page'] = $request['page'];
$args['per_page'] = $request['per_page'];
$args['orderby'] = $request['orderby'];
$args['order'] = $request['order'];
$args['segmentby'] = $request['segmentby'];
$args['fields'] = $request['fields'];
$args['force_cache_refresh'] = $request['force_cache_refresh'];
return $args;
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return WP_REST_Response|WP_Error
*/
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$reports_revenue = new RevenueQuery( $query_args );
try {
$report_data = $reports_revenue->get_data();
} catch ( ParameterException $e ) {
return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
$out_data = array(
'totals' => get_object_vars( $report_data->totals ),
'intervals' => array(),
);
foreach ( $report_data->intervals as $interval_data ) {
$item = $this->prepare_item_for_response( $interval_data, $request );
$out_data['intervals'][] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$out_data,
(int) $report_data->total,
(int) $report_data->page_no,
(int) $report_data->pages
);
}
/**
* Get report items for export.
*
* Returns only the interval data.
*
* @param WP_REST_Request $request Request data.
* @return WP_REST_Response
*/
public function get_export_items( $request ) {
$response = $this->get_items( $request );
$data = $response->get_data();
$intervals = $data['intervals'];
$response->set_data( $intervals );
return $response;
}
/**
* Prepare a report object for serialization.
*
* @param array $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$response = parent::prepare_item_for_response( $report, $request );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_revenue_stats', $response, $report, $request );
}
/**
* Get the Report's item properties schema.
* Will be used by `get_item_schema` as `totals` and `subtotals`.
*
* @return array
*/
protected function get_item_properties_schema() {
return array(
'total_sales' => array(
'description' => __( 'Total sales.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
'format' => 'currency',
),
'net_revenue' => array(
'description' => __( 'Net sales.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
'format' => 'currency',
),
'coupons' => array(
'description' => __( 'Amount discounted by coupons.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'coupons_count' => array(
'description' => __( 'Unique coupons count.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'format' => 'currency',
),
'shipping' => array(
'title' => __( 'Shipping', 'woocommerce' ),
'description' => __( 'Total of shipping.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
'format' => 'currency',
),
'taxes' => array(
'description' => __( 'Total of taxes.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'format' => 'currency',
),
'refunds' => array(
'title' => __( 'Returns', 'woocommerce' ),
'description' => __( 'Total of returns.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
'format' => 'currency',
),
'orders_count' => array(
'description' => __( 'Number of orders.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'num_items_sold' => array(
'description' => __( 'Items sold.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'gross_sales' => array(
'description' => __( 'Gross sales.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
'format' => 'currency',
),
);
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = parent::get_item_schema();
$schema['title'] = 'report_revenue_stats';
// Products is not shown in intervals, only in totals.
$schema['properties']['totals']['properties']['products'] = array(
'description' => __( 'Products sold.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['orderby']['enum'] = array(
'date',
'total_sales',
'coupons',
'refunds',
'shipping',
'taxes',
'net_revenue',
'orders_count',
'items_sold',
'gross_sales',
);
$params['segmentby'] = array(
'description' => __( 'Segment the response by additional constraint.', 'woocommerce' ),
'type' => 'string',
'enum' => array(
'product',
'category',
'variation',
'coupon',
'customer_type', // new vs returning.
),
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get the column names for export.
*
* @return array Key value pair of Column ID => Label.
*/
public function get_export_columns() {
return array(
'date' => __( 'Date', 'woocommerce' ),
'orders_count' => __( 'Orders', 'woocommerce' ),
'gross_sales' => __( 'Gross sales', 'woocommerce' ),
'refunds' => __( 'Returns', 'woocommerce' ),
'coupons' => __( 'Coupons', 'woocommerce' ),
'net_revenue' => __( 'Net sales', 'woocommerce' ),
'taxes' => __( 'Taxes', 'woocommerce' ),
'shipping' => __( 'Shipping', 'woocommerce' ),
'total_sales' => __( 'Total sales', 'woocommerce' ),
);
}
/**
* Get the column values for export.
*
* @param array $item Single report item/row.
* @return array Key value pair of Column ID => Row Value.
*/
public function prepare_item_for_export( $item ) {
$subtotals = (array) $item['subtotals'];
return array(
'date' => $item['date_start'],
'orders_count' => $subtotals['orders_count'],
'gross_sales' => self::csv_number_format( $subtotals['gross_sales'] ),
'refunds' => self::csv_number_format( $subtotals['refunds'] ),
'coupons' => self::csv_number_format( $subtotals['coupons'] ),
'net_revenue' => self::csv_number_format( $subtotals['net_revenue'] ),
'taxes' => self::csv_number_format( $subtotals['taxes'] ),
'shipping' => self::csv_number_format( $subtotals['shipping'] ),
'total_sales' => self::csv_number_format( $subtotals['total_sales'] ),
);
}
}
API/Reports/Segmenter.php 0000644 00000062474 15153746747 0011315 0 ustar 00 <?php
/**
* Class for adding segmenting support without cluttering the data stores.
*/
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore as CouponsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\DataStore as TaxesStatsDataStore;
/**
* Date & time interval and numeric range handling class for Reporting API.
*/
class Segmenter {
/**
* Array of all segment ids.
*
* @var array|bool
*/
protected $all_segment_ids = false;
/**
* Array of all segment labels.
*
* @var array
*/
protected $segment_labels = array();
/**
* Query arguments supplied by the user for data store.
*
* @var array
*/
protected $query_args = '';
/**
* SQL definition for each column.
*
* @var array
*/
protected $report_columns = array();
/**
* Constructor.
*
* @param array $query_args Query arguments supplied by the user for data store.
* @param array $report_columns Report columns lookup from data store.
*/
public function __construct( $query_args, $report_columns ) {
$this->query_args = $query_args;
$this->report_columns = $report_columns;
}
/**
* Filters definitions for SELECT clauses based on query_args and joins them into one string usable in SELECT clause.
*
* @param array $columns_mapping Column name -> SQL statememt mapping.
*
* @return string to be used in SELECT clause statements.
*/
protected function prepare_selections( $columns_mapping ) {
if ( isset( $this->query_args['fields'] ) && is_array( $this->query_args['fields'] ) ) {
$keep = array();
foreach ( $this->query_args['fields'] as $field ) {
if ( isset( $columns_mapping[ $field ] ) ) {
$keep[ $field ] = $columns_mapping[ $field ];
}
}
$selections = implode( ', ', $keep );
} else {
$selections = implode( ', ', $columns_mapping );
}
if ( $selections ) {
$selections = ',' . $selections;
}
return $selections;
}
/**
* Update row-level db result for segments in 'totals' section to the format used for output.
*
* @param array $segments_db_result Results from the SQL db query for segmenting.
* @param string $segment_dimension Name of column used for grouping the result.
*
* @return array Reformatted array.
*/
protected function reformat_totals_segments( $segments_db_result, $segment_dimension ) {
$segment_result = array();
if ( strpos( $segment_dimension, '.' ) ) {
$segment_dimension = substr( strstr( $segment_dimension, '.' ), 1 );
}
$segment_labels = $this->get_segment_labels();
foreach ( $segments_db_result as $segment_data ) {
$segment_id = $segment_data[ $segment_dimension ];
if ( ! isset( $segment_labels[ $segment_id ] ) ) {
continue;
}
unset( $segment_data[ $segment_dimension ] );
$segment_datum = array(
'segment_id' => $segment_id,
'segment_label' => $segment_labels[ $segment_id ],
'subtotals' => $segment_data,
);
$segment_result[ $segment_id ] = $segment_datum;
}
return $segment_result;
}
/**
* Merges segmented results for totals response part.
*
* E.g. $r1 = array(
* 0 => array(
* 'product_id' => 3,
* 'net_amount' => 15,
* ),
* );
* $r2 = array(
* 0 => array(
* 'product_id' => 3,
* 'avg_order_value' => 25,
* ),
* );
*
* $merged = array(
* 3 => array(
* 'segment_id' => 3,
* 'subtotals' => array(
* 'net_amount' => 15,
* 'avg_order_value' => 25,
* )
* ),
* );
*
* @param string $segment_dimension Name of the segment dimension=key in the result arrays used to match records from result sets.
* @param array $result1 Array 1 of segmented figures.
* @param array $result2 Array 2 of segmented figures.
*
* @return array
*/
protected function merge_segment_totals_results( $segment_dimension, $result1, $result2 ) {
$result_segments = array();
$segment_labels = $this->get_segment_labels();
foreach ( $result1 as $segment_data ) {
$segment_id = $segment_data[ $segment_dimension ];
if ( ! isset( $segment_labels[ $segment_id ] ) ) {
continue;
}
unset( $segment_data[ $segment_dimension ] );
$result_segments[ $segment_id ] = array(
'segment_label' => $segment_labels[ $segment_id ],
'segment_id' => $segment_id,
'subtotals' => $segment_data,
);
}
foreach ( $result2 as $segment_data ) {
$segment_id = $segment_data[ $segment_dimension ];
if ( ! isset( $segment_labels[ $segment_id ] ) ) {
continue;
}
unset( $segment_data[ $segment_dimension ] );
if ( ! isset( $result_segments[ $segment_id ] ) ) {
$result_segments[ $segment_id ] = array(
'segment_label' => $segment_labels[ $segment_id ],
'segment_id' => $segment_id,
'subtotals' => array(),
);
}
$result_segments[ $segment_id ]['subtotals'] = array_merge( $result_segments[ $segment_id ]['subtotals'], $segment_data );
}
return $result_segments;
}
/**
* Merges segmented results for intervals response part.
*
* E.g. $r1 = array(
* 0 => array(
* 'product_id' => 3,
* 'time_interval' => '2018-12'
* 'net_amount' => 15,
* ),
* );
* $r2 = array(
* 0 => array(
* 'product_id' => 3,
* 'time_interval' => '2018-12'
* 'avg_order_value' => 25,
* ),
* );
*
* $merged = array(
* '2018-12' => array(
* 'segments' => array(
* 3 => array(
* 'segment_id' => 3,
* 'subtotals' => array(
* 'net_amount' => 15,
* 'avg_order_value' => 25,
* ),
* ),
* ),
* ),
* );
*
* @param string $segment_dimension Name of the segment dimension=key in the result arrays used to match records from result sets.
* @param array $result1 Array 1 of segmented figures.
* @param array $result2 Array 2 of segmented figures.
*
* @return array
*/
protected function merge_segment_intervals_results( $segment_dimension, $result1, $result2 ) {
$result_segments = array();
$segment_labels = $this->get_segment_labels();
foreach ( $result1 as $segment_data ) {
$segment_id = $segment_data[ $segment_dimension ];
if ( ! isset( $segment_labels[ $segment_id ] ) ) {
continue;
}
$time_interval = $segment_data['time_interval'];
if ( ! isset( $result_segments[ $time_interval ] ) ) {
$result_segments[ $time_interval ] = array();
$result_segments[ $time_interval ]['segments'] = array();
}
unset( $segment_data['time_interval'] );
unset( $segment_data['datetime_anchor'] );
unset( $segment_data[ $segment_dimension ] );
$segment_datum = array(
'segment_label' => $segment_labels[ $segment_id ],
'segment_id' => $segment_id,
'subtotals' => $segment_data,
);
$result_segments[ $time_interval ]['segments'][ $segment_id ] = $segment_datum;
}
foreach ( $result2 as $segment_data ) {
$segment_id = $segment_data[ $segment_dimension ];
if ( ! isset( $segment_labels[ $segment_id ] ) ) {
continue;
}
$time_interval = $segment_data['time_interval'];
if ( ! isset( $result_segments[ $time_interval ] ) ) {
$result_segments[ $time_interval ] = array();
$result_segments[ $time_interval ]['segments'] = array();
}
unset( $segment_data['time_interval'] );
unset( $segment_data['datetime_anchor'] );
unset( $segment_data[ $segment_dimension ] );
if ( ! isset( $result_segments[ $time_interval ]['segments'][ $segment_id ] ) ) {
$result_segments[ $time_interval ]['segments'][ $segment_id ] = array(
'segment_label' => $segment_labels[ $segment_id ],
'segment_id' => $segment_id,
'subtotals' => array(),
);
}
$result_segments[ $time_interval ]['segments'][ $segment_id ]['subtotals'] = array_merge( $result_segments[ $time_interval ]['segments'][ $segment_id ]['subtotals'], $segment_data );
}
return $result_segments;
}
/**
* Update row-level db result for segments in 'intervals' section to the format used for output.
*
* @param array $segments_db_result Results from the SQL db query for segmenting.
* @param string $segment_dimension Name of column used for grouping the result.
*
* @return array Reformatted array.
*/
protected function reformat_intervals_segments( $segments_db_result, $segment_dimension ) {
$aggregated_segment_result = array();
if ( strpos( $segment_dimension, '.' ) ) {
$segment_dimension = substr( strstr( $segment_dimension, '.' ), 1 );
}
$segment_labels = $this->get_segment_labels();
foreach ( $segments_db_result as $segment_data ) {
$segment_id = $segment_data[ $segment_dimension ];
if ( ! isset( $segment_labels[ $segment_id ] ) ) {
continue;
}
$time_interval = $segment_data['time_interval'];
if ( ! isset( $aggregated_segment_result[ $time_interval ] ) ) {
$aggregated_segment_result[ $time_interval ] = array();
$aggregated_segment_result[ $time_interval ]['segments'] = array();
}
unset( $segment_data['time_interval'] );
unset( $segment_data['datetime_anchor'] );
unset( $segment_data[ $segment_dimension ] );
$segment_datum = array(
'segment_label' => $segment_labels[ $segment_id ],
'segment_id' => $segment_id,
'subtotals' => $segment_data,
);
$aggregated_segment_result[ $time_interval ]['segments'][ $segment_id ] = $segment_datum;
}
return $aggregated_segment_result;
}
/**
* Fetches all segment ids from db and stores it for later use.
*
* @return void
*/
protected function set_all_segments() {
global $wpdb;
if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) {
$this->all_segment_ids = array();
return;
}
$segments = array();
$segment_labels = array();
if ( 'product' === $this->query_args['segmentby'] ) {
$args = array(
'return' => 'objects',
'limit' => -1,
);
if ( isset( $this->query_args['product_includes'] ) ) {
$args['include'] = $this->query_args['product_includes'];
}
if ( isset( $this->query_args['category_includes'] ) ) {
$categories = $this->query_args['category_includes'];
$args['category'] = array();
foreach ( $categories as $category_id ) {
$terms = get_term_by( 'id', $category_id, 'product_cat' );
$args['category'][] = $terms->slug;
}
}
$segment_objects = wc_get_products( $args );
foreach ( $segment_objects as $segment ) {
$id = $segment->get_id();
$segments[] = $id;
$segment_labels[ $id ] = $segment->get_name();
}
} elseif ( 'variation' === $this->query_args['segmentby'] ) {
$args = array(
'return' => 'objects',
'limit' => -1,
'type' => 'variation',
);
if (
isset( $this->query_args['product_includes'] ) &&
count( $this->query_args['product_includes'] ) === 1
) {
$args['parent'] = $this->query_args['product_includes'][0];
}
if ( isset( $this->query_args['variation_includes'] ) ) {
$args['include'] = $this->query_args['variation_includes'];
}
$segment_objects = wc_get_products( $args );
foreach ( $segment_objects as $segment ) {
$id = $segment->get_id();
$segments[] = $id;
$product_name = $segment->get_name();
$separator = apply_filters( 'woocommerce_product_variation_title_attributes_separator', ' - ', $segment );
$attributes = wc_get_formatted_variation( $segment, true, false );
$segment_labels[ $id ] = $product_name . $separator . $attributes;
}
// If no variations were specified, add a segment for the parent product (variation = 0).
// This is to catch simple products with prior sales converted into variable products.
// See: https://github.com/woocommerce/woocommerce-admin/issues/2719.
if ( isset( $args['parent'] ) && empty( $args['include'] ) ) {
$parent_object = wc_get_product( $args['parent'] );
$segments[] = 0;
$segment_labels[0] = $parent_object->get_name();
}
} elseif ( 'category' === $this->query_args['segmentby'] ) {
$args = array(
'taxonomy' => 'product_cat',
);
if ( isset( $this->query_args['category_includes'] ) ) {
$args['include'] = $this->query_args['category_includes'];
}
// @todo: Look into `wc_get_products` or data store methods and not directly touching the database or post types.
$categories = get_categories( $args );
$segments = wp_list_pluck( $categories, 'cat_ID' );
$segment_labels = wp_list_pluck( $categories, 'name', 'cat_ID' );
} elseif ( 'coupon' === $this->query_args['segmentby'] ) {
$args = array();
if ( isset( $this->query_args['coupons'] ) ) {
$args['include'] = $this->query_args['coupons'];
}
$coupons_store = new CouponsDataStore();
$coupons = $coupons_store->get_coupons( $args );
$segments = wp_list_pluck( $coupons, 'ID' );
$segment_labels = wp_list_pluck( $coupons, 'post_title', 'ID' );
$segment_labels = array_map( 'wc_format_coupon_code', $segment_labels );
} elseif ( 'customer_type' === $this->query_args['segmentby'] ) {
// 0 -- new customer
// 1 -- returning customer
$segments = array( 0, 1 );
} elseif ( 'tax_rate_id' === $this->query_args['segmentby'] ) {
$args = array();
if ( isset( $this->query_args['taxes'] ) ) {
$args['include'] = $this->query_args['taxes'];
}
$taxes = TaxesStatsDataStore::get_taxes( $args );
foreach ( $taxes as $tax ) {
$id = $tax['tax_rate_id'];
$segments[] = $id;
$segment_labels[ $id ] = \WC_Tax::get_rate_code( (object) $tax );
}
} else {
// Catch all default.
$segments = array();
}
$this->all_segment_ids = $segments;
$this->segment_labels = $segment_labels;
}
/**
* Return all segment ids for given segmentby query parameter.
*
* @return array
*/
protected function get_all_segments() {
if ( ! is_array( $this->all_segment_ids ) ) {
$this->set_all_segments();
}
return $this->all_segment_ids;
}
/**
* Return all segment labels for given segmentby query parameter.
*
* @return array
*/
protected function get_segment_labels() {
if ( ! is_array( $this->all_segment_ids ) ) {
$this->set_all_segments();
}
return $this->segment_labels;
}
/**
* Compares two report data objects by pre-defined object property and ASC/DESC ordering.
*
* @param stdClass $a Object a.
* @param stdClass $b Object b.
* @return string
*/
private function segment_cmp( $a, $b ) {
if ( $a['segment_id'] === $b['segment_id'] ) {
return 0;
} elseif ( $a['segment_id'] > $b['segment_id'] ) {
return 1;
} elseif ( $a['segment_id'] < $b['segment_id'] ) {
return - 1;
}
}
/**
* Adds zeroes for segments not present in the data selection.
*
* @param array $segments Array of segments from the database for given data points.
*
* @return array
*/
protected function fill_in_missing_segments( $segments ) {
$segment_subtotals = array();
if ( isset( $this->query_args['fields'] ) && is_array( $this->query_args['fields'] ) ) {
foreach ( $this->query_args['fields'] as $field ) {
if ( isset( $this->report_columns[ $field ] ) ) {
$segment_subtotals[ $field ] = 0;
}
}
} else {
foreach ( $this->report_columns as $field => $sql_clause ) {
$segment_subtotals[ $field ] = 0;
}
}
if ( ! is_array( $segments ) ) {
$segments = array();
}
$all_segment_ids = $this->get_all_segments();
$segment_labels = $this->get_segment_labels();
foreach ( $all_segment_ids as $segment_id ) {
if ( ! isset( $segments[ $segment_id ] ) ) {
$segments[ $segment_id ] = array(
'segment_id' => $segment_id,
'segment_label' => $segment_labels[ $segment_id ],
'subtotals' => $segment_subtotals,
);
}
}
// Using array_values to remove custom keys, so that it gets later converted to JSON as an array.
$segments_no_keys = array_values( $segments );
usort( $segments_no_keys, array( $this, 'segment_cmp' ) );
return $segments_no_keys;
}
/**
* Adds missing segments to intervals, modifies $data.
*
* @param stdClass $data Response data.
*/
protected function fill_in_missing_interval_segments( &$data ) {
foreach ( $data->intervals as $order_id => $interval_data ) {
$data->intervals[ $order_id ]['segments'] = $this->fill_in_missing_segments( $data->intervals[ $order_id ]['segments'] );
}
}
/**
* Calculate segments for totals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
*
* @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $segmenting_dimension_name Name of the segmenting dimension.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $totals_query Array of SQL clauses for totals query.
* @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
*
* @return array
*/
protected function get_product_related_totals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $totals_query, $unique_orders_table ) {
return array();
}
/**
* Calculate segments for intervals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
*
* @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $segmenting_dimension_name Name of the segmenting dimension.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $intervals_query Array of SQL clauses for intervals query.
* @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
*
* @return array
*/
protected function get_product_related_intervals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $intervals_query, $unique_orders_table ) {
return array();
}
/**
* Calculate segments for totals query where the segmenting property is bound to order (e.g. coupon or customer type).
*
* @param string $segmenting_select SELECT part of segmenting SQL query.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $totals_query Array of SQL clauses for intervals query.
*
* @return array
*/
protected function get_order_related_totals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $totals_query ) {
return array();
}
/**
* Calculate segments for intervals query where the segmenting property is bound to order (e.g. coupon or customer type).
*
* @param string $segmenting_select SELECT part of segmenting SQL query.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $intervals_query Array of SQL clauses for intervals query.
*
* @return array
*/
protected function get_order_related_intervals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $intervals_query ) {
return array();
}
/**
* Return array of segments formatted for REST response.
*
* @param string $type Type of segments to return--'totals' or 'intervals'.
* @param array $query_params SQL query parameter array.
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
*
* @return array
*/
protected function get_segments( $type, $query_params, $table_name ) {
return array();
}
/**
* Calculate segments for segmenting property bound to product (e.g. category, product_id, variation_id).
*
* @param string $type Type of segments to return--'totals' or 'intervals'.
* @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $segmenting_dimension_name Name of the segmenting dimension.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $query_params Array of SQL clauses for intervals/totals query.
* @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
*
* @return array
*/
protected function get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ) {
if ( 'totals' === $type ) {
return $this->get_product_related_totals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'intervals' === $type ) {
return $this->get_product_related_intervals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
}
}
/**
* Calculate segments for segmenting property bound to order (e.g. coupon or customer type).
*
* @param string $type Type of segments to return--'totals' or 'intervals'.
* @param string $segmenting_select SELECT part of segmenting SQL query.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $query_params Array of SQL clauses for intervals/totals query.
*
* @return array
*/
protected function get_order_related_segments( $type, $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params ) {
if ( 'totals' === $type ) {
return $this->get_order_related_totals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params );
} elseif ( 'intervals' === $type ) {
return $this->get_order_related_intervals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params );
}
}
/**
* Assign segments to time intervals by updating original $intervals array.
*
* @param array $intervals Result array from intervals SQL query.
* @param array $intervals_segments Result array from interval segments SQL query.
*/
protected function assign_segments_to_intervals( &$intervals, $intervals_segments ) {
$old_keys = array_keys( $intervals );
foreach ( $intervals as $interval ) {
$intervals[ $interval['time_interval'] ] = $interval;
$intervals[ $interval['time_interval'] ]['segments'] = array();
}
foreach ( $old_keys as $key ) {
unset( $intervals[ $key ] );
}
foreach ( $intervals_segments as $time_interval => $segment ) {
if ( isset( $intervals[ $time_interval ] ) ) {
$intervals[ $time_interval ]['segments'] = $segment['segments'];
}
}
// To remove time interval keys (so that REST response is formatted correctly).
$intervals = array_values( $intervals );
}
/**
* Returns an array of segments for totals part of REST response.
*
* @param array $query_params Totals SQL query parameters.
* @param string $table_name Name of the SQL table that is the main order stats table.
*
* @return array
*/
public function get_totals_segments( $query_params, $table_name ) {
$segments = $this->get_segments( 'totals', $query_params, $table_name );
$segments = $this->fill_in_missing_segments( $segments );
return $segments;
}
/**
* Adds an array of segments to data->intervals object.
*
* @param stdClass $data Data object representing the REST response.
* @param array $intervals_query Intervals SQL query parameters.
* @param string $table_name Name of the SQL table that is the main order stats table.
*/
public function add_intervals_segments( &$data, $intervals_query, $table_name ) {
$intervals_segments = $this->get_segments( 'intervals', $intervals_query, $table_name );
$this->assign_segments_to_intervals( $data->intervals, $intervals_segments );
$this->fill_in_missing_interval_segments( $data );
}
}
API/Reports/SqlQuery.php 0000644 00000012236 15153746747 0011140 0 ustar 00 <?php
/**
* Admin\API\Reports\SqlQuery class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Admin\API\Reports\SqlQuery: Common parent for manipulating SQL query clauses.
*/
class SqlQuery {
/**
* List of SQL clauses.
*
* @var array
*/
private $sql_clauses = array(
'select' => array(),
'from' => array(),
'left_join' => array(),
'join' => array(),
'right_join' => array(),
'where' => array(),
'where_time' => array(),
'group_by' => array(),
'having' => array(),
'limit' => array(),
'order_by' => array(),
'union' => array(),
);
/**
* SQL clause merge filters.
*
* @var array
*/
private $sql_filters = array(
'where' => array(
'where',
'where_time',
),
'join' => array(
'right_join',
'join',
'left_join',
),
);
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context;
/**
* Constructor.
*
* @param string $context Optional context passed to filters. Default empty string.
*/
public function __construct( $context = '' ) {
$this->context = $context;
}
/**
* Add a SQL clause to be included when get_data is called.
*
* @param string $type Clause type.
* @param string $clause SQL clause.
*/
public function add_sql_clause( $type, $clause ) {
if ( isset( $this->sql_clauses[ $type ] ) && ! empty( $clause ) ) {
$this->sql_clauses[ $type ][] = $clause;
}
}
/**
* Get SQL clause by type.
*
* @param string $type Clause type.
* @param string $handling Whether to filter the return value (filtered|unfiltered). Default unfiltered.
*
* @return string SQL clause.
*/
protected function get_sql_clause( $type, $handling = 'unfiltered' ) {
if ( ! isset( $this->sql_clauses[ $type ] ) ) {
return '';
}
/**
* Default to bypassing filters for clause retrieval internal to data stores.
* The filters are applied when the full SQL statement is retrieved.
*/
if ( 'unfiltered' === $handling ) {
return implode( ' ', $this->sql_clauses[ $type ] );
}
if ( isset( $this->sql_filters[ $type ] ) ) {
$clauses = array();
foreach ( $this->sql_filters[ $type ] as $subset ) {
$clauses = array_merge( $clauses, $this->sql_clauses[ $subset ] );
}
} else {
$clauses = $this->sql_clauses[ $type ];
}
/**
* Filter SQL clauses by type and context.
*
* @param array $clauses The original arguments for the request.
* @param string $context The data store context.
*/
$clauses = apply_filters( "woocommerce_analytics_clauses_{$type}", $clauses, $this->context );
/**
* Filter SQL clauses by type and context.
*
* @param array $clauses The original arguments for the request.
*/
$clauses = apply_filters( "woocommerce_analytics_clauses_{$type}_{$this->context}", $clauses );
return implode( ' ', $clauses );
}
/**
* Clear SQL clauses by type.
*
* @param string|array $types Clause type.
*/
protected function clear_sql_clause( $types ) {
foreach ( (array) $types as $type ) {
if ( isset( $this->sql_clauses[ $type ] ) ) {
$this->sql_clauses[ $type ] = array();
}
}
}
/**
* Replace strings within SQL clauses by type.
*
* @param string $type Clause type.
* @param string $search String to search for.
* @param string $replace Replacement string.
*/
protected function str_replace_clause( $type, $search, $replace ) {
if ( isset( $this->sql_clauses[ $type ] ) ) {
foreach ( $this->sql_clauses[ $type ] as $key => $sql ) {
$this->sql_clauses[ $type ][ $key ] = str_replace( $search, $replace, $sql );
}
}
}
/**
* Get the full SQL statement.
*
* @return string
*/
public function get_query_statement() {
$join = $this->get_sql_clause( 'join', 'filtered' );
$where = $this->get_sql_clause( 'where', 'filtered' );
$group_by = $this->get_sql_clause( 'group_by', 'filtered' );
$having = $this->get_sql_clause( 'having', 'filtered' );
$order_by = $this->get_sql_clause( 'order_by', 'filtered' );
$union = $this->get_sql_clause( 'union', 'filtered' );
$statement = '';
$statement .= "
SELECT
{$this->get_sql_clause( 'select', 'filtered' )}
FROM
{$this->get_sql_clause( 'from', 'filtered' )}
{$join}
WHERE
1=1
{$where}
";
if ( ! empty( $group_by ) ) {
$statement .= "
GROUP BY
{$group_by}
";
if ( ! empty( $having ) ) {
$statement .= "
HAVING
1=1
{$having}
";
}
}
if ( ! empty( $union ) ) {
$statement .= "
UNION
{$union}
";
}
if ( ! empty( $order_by ) ) {
$statement .= "
ORDER BY
{$order_by}
";
}
return $statement . $this->get_sql_clause( 'limit', 'filtered' );
}
/**
* Reinitialize the clause array.
*/
public function clear_all_clauses() {
$this->sql_clauses = array(
'select' => array(),
'from' => array(),
'left_join' => array(),
'join' => array(),
'right_join' => array(),
'where' => array(),
'where_time' => array(),
'group_by' => array(),
'having' => array(),
'limit' => array(),
'order_by' => array(),
'union' => array(),
);
}
}
API/Reports/Stock/Controller.php 0000644 00000040357 15153746747 0012566 0 ustar 00 <?php
/**
* REST API Reports stock controller
*
* Handles requests to the /reports/stock endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Stock;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericController;
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
use WP_REST_Request;
use WP_REST_Response;
/**
* REST API Reports stock controller class.
*
* @internal
* @extends GenericController
*/
class Controller extends GenericController implements ExportableInterface {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/stock';
/**
* Registered stock status options.
*
* @var array
*/
protected $status_options;
/**
* Constructor.
*/
public function __construct() {
$this->status_options = wc_get_product_stock_status_options();
}
/**
* Maps query arguments from the REST request.
*
* @param WP_REST_Request $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['offset'] = $request['offset'];
$args['order'] = $request['order'];
$args['orderby'] = $request['orderby'];
$args['paged'] = $request['page'];
$args['post__in'] = $request['include'];
$args['post__not_in'] = $request['exclude'];
$args['posts_per_page'] = $request['per_page'];
$args['post_parent__in'] = $request['parent'];
$args['post_parent__not_in'] = $request['parent_exclude'];
if ( 'date' === $args['orderby'] ) {
$args['orderby'] = 'date ID';
} elseif ( 'include' === $args['orderby'] ) {
$args['orderby'] = 'post__in';
} elseif ( 'id' === $args['orderby'] ) {
$args['orderby'] = 'ID'; // ID must be capitalized.
}
$args['post_type'] = array( 'product', 'product_variation' );
if ( 'lowstock' === $request['type'] ) {
$args['low_in_stock'] = true;
} elseif ( in_array( $request['type'], array_keys( $this->status_options ), true ) ) {
$args['stock_status'] = $request['type'];
}
$args['ignore_sticky_posts'] = true;
return $args;
}
/**
* Query products.
*
* @param array $query_args Query args.
* @return array
*/
protected function get_products( $query_args ) {
$query = new \WP_Query();
$result = $query->query( $query_args );
$total_posts = $query->found_posts;
if ( $total_posts < 1 ) {
// Out-of-bounds, run the query again without LIMIT for total count.
unset( $query_args['paged'] );
$count_query = new \WP_Query();
$count_query->query( $query_args );
$total_posts = $count_query->found_posts;
}
return array(
'objects' => array_map( 'wc_get_product', $result ),
'total' => (int) $total_posts,
'pages' => (int) ceil( $total_posts / (int) $query->query_vars['posts_per_page'] ),
);
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
add_filter( 'posts_where', array( __CLASS__, 'add_wp_query_filter' ), 10, 2 );
add_filter( 'posts_join', array( __CLASS__, 'add_wp_query_join' ), 10, 2 );
add_filter( 'posts_groupby', array( __CLASS__, 'add_wp_query_group_by' ), 10, 2 );
add_filter( 'posts_clauses', array( __CLASS__, 'add_wp_query_orderby' ), 10, 2 );
$query_args = $this->prepare_reports_query( $request );
$query_results = $this->get_products( $query_args );
remove_filter( 'posts_where', array( __CLASS__, 'add_wp_query_filter' ), 10 );
remove_filter( 'posts_join', array( __CLASS__, 'add_wp_query_join' ), 10 );
remove_filter( 'posts_groupby', array( __CLASS__, 'add_wp_query_group_by' ), 10 );
remove_filter( 'posts_clauses', array( __CLASS__, 'add_wp_query_orderby' ), 10 );
$objects = array();
foreach ( $query_results['objects'] as $object ) {
$data = $this->prepare_item_for_response( $object, $request );
$objects[] = $this->prepare_response_for_collection( $data );
}
return $this->add_pagination_headers(
$request,
$objects,
(int) $query_results['total'],
(int) $query_args['paged'],
(int) $query_results['pages']
);
}
/**
* Add in conditional search filters for products.
*
* @internal
* @param string $where Where clause used to search posts.
* @param object $wp_query WP_Query object.
* @return string
*/
public static function add_wp_query_filter( $where, $wp_query ) {
global $wpdb;
$stock_status = $wp_query->get( 'stock_status' );
if ( $stock_status ) {
$where .= $wpdb->prepare(
' AND wc_product_meta_lookup.stock_status = %s ',
$stock_status
);
}
if ( $wp_query->get( 'low_in_stock' ) ) {
// We want products with stock < low stock amount, but greater than no stock amount.
$no_stock_amount = absint( max( get_option( 'woocommerce_notify_no_stock_amount' ), 0 ) );
$low_stock_amount = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
$where .= "
AND wc_product_meta_lookup.stock_quantity IS NOT NULL
AND wc_product_meta_lookup.stock_status = 'instock'
AND (
(
low_stock_amount_meta.meta_value > ''
AND wc_product_meta_lookup.stock_quantity <= CAST(low_stock_amount_meta.meta_value AS SIGNED)
AND wc_product_meta_lookup.stock_quantity > {$no_stock_amount}
)
OR (
(
low_stock_amount_meta.meta_value IS NULL OR low_stock_amount_meta.meta_value <= ''
)
AND wc_product_meta_lookup.stock_quantity <= {$low_stock_amount}
AND wc_product_meta_lookup.stock_quantity > {$no_stock_amount}
)
)";
}
return $where;
}
/**
* Join posts meta tables when product search or low stock query is present.
*
* @internal
* @param string $join Join clause used to search posts.
* @param object $wp_query WP_Query object.
* @return string
*/
public static function add_wp_query_join( $join, $wp_query ) {
global $wpdb;
$stock_status = $wp_query->get( 'stock_status' );
if ( $stock_status ) {
$join = self::append_product_sorting_table_join( $join );
}
if ( $wp_query->get( 'low_in_stock' ) ) {
$join = self::append_product_sorting_table_join( $join );
$join .= " LEFT JOIN {$wpdb->postmeta} AS low_stock_amount_meta ON {$wpdb->posts}.ID = low_stock_amount_meta.post_id AND low_stock_amount_meta.meta_key = '_low_stock_amount' ";
}
return $join;
}
/**
* Join wc_product_meta_lookup to posts if not already joined.
*
* @internal
* @param string $sql SQL join.
* @return string
*/
protected static function append_product_sorting_table_join( $sql ) {
global $wpdb;
if ( ! strstr( $sql, 'wc_product_meta_lookup' ) ) {
$sql .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id ";
}
return $sql;
}
/**
* Group by post ID to prevent duplicates.
*
* @internal
* @param string $groupby Group by clause used to organize posts.
* @param object $wp_query WP_Query object.
* @return string
*/
public static function add_wp_query_group_by( $groupby, $wp_query ) {
global $wpdb;
if ( empty( $groupby ) ) {
$groupby = $wpdb->posts . '.ID';
}
return $groupby;
}
/**
* Custom orderby clauses using the lookup tables.
*
* @internal
* @param array $args Query args.
* @param object $wp_query WP_Query object.
* @return array
*/
public static function add_wp_query_orderby( $args, $wp_query ) {
global $wpdb;
$orderby = $wp_query->get( 'orderby' );
$order = esc_sql( $wp_query->get( 'order' ) ? $wp_query->get( 'order' ) : 'desc' );
switch ( $orderby ) {
case 'stock_quantity':
$args['join'] = self::append_product_sorting_table_join( $args['join'] );
$args['orderby'] = " wc_product_meta_lookup.stock_quantity {$order}, wc_product_meta_lookup.product_id {$order} ";
break;
case 'stock_status':
$args['join'] = self::append_product_sorting_table_join( $args['join'] );
$args['orderby'] = " wc_product_meta_lookup.stock_status {$order}, wc_product_meta_lookup.stock_quantity {$order} ";
break;
case 'sku':
$args['join'] = self::append_product_sorting_table_join( $args['join'] );
$args['orderby'] = " wc_product_meta_lookup.sku {$order}, wc_product_meta_lookup.product_id {$order} ";
break;
}
return $args;
}
/**
* Prepare a report object for serialization.
*
* @param WC_Product $product Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $product, $request ) {
$data = array(
'id' => $product->get_id(),
'parent_id' => $product->get_parent_id(),
'name' => wp_strip_all_tags( $product->get_name() ),
'sku' => $product->get_sku(),
'stock_status' => $product->get_stock_status(),
'stock_quantity' => (float) $product->get_stock_quantity(),
'manage_stock' => $product->get_manage_stock(),
'low_stock_amount' => $product->get_low_stock_amount(),
);
if ( '' === $data['low_stock_amount'] ) {
$data['low_stock_amount'] = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
}
$response = parent::prepare_item_for_response( $data, $request );
$response->add_links( $this->prepare_links( $product ) );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param WC_Product $product The original product object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_stock', $response, $product, $request );
}
/**
* Prepare links for the request.
*
* @param WC_Product $product Object data.
* @return array
*/
protected function prepare_links( $product ) {
if ( $product->is_type( 'variation' ) ) {
$links = array(
'product' => array(
'href' => rest_url( sprintf( '/%s/products/%d/variations/%d', $this->namespace, $product->get_parent_id(), $product->get_id() ) ),
),
'parent' => array(
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $product->get_parent_id() ) ),
),
);
} elseif ( $product->get_parent_id() ) {
$links = array(
'product' => array(
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $product->get_id() ) ),
),
'parent' => array(
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $product->get_parent_id() ) ),
),
);
} else {
$links = array(
'product' => array(
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $product->get_id() ) ),
),
);
}
return $links;
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_stock',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'parent_id' => array(
'description' => __( 'Product parent ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'Product name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'sku' => array(
'description' => __( 'Unique identifier.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'stock_status' => array(
'description' => __( 'Stock status.', 'woocommerce' ),
'type' => 'string',
'enum' => array_keys( wc_get_product_stock_status_options() ),
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'stock_quantity' => array(
'description' => __( 'Stock quantity.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'manage_stock' => array(
'description' => __( 'Manage stock.', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
unset( $params['after'], $params['before'], $params['force_cache_refresh'] );
$params['exclude'] = array(
'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
);
$params['include'] = array(
'description' => __( 'Limit result set to specific ids.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
);
$params['offset'] = array(
'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['order']['default'] = 'asc';
$params['orderby']['default'] = 'stock_status';
$params['orderby']['enum'] = array(
'stock_status',
'stock_quantity',
'date',
'id',
'include',
'title',
'sku',
);
$params['parent'] = array(
'description' => __( 'Limit result set to those of particular parent IDs.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'sanitize_callback' => 'wp_parse_id_list',
'default' => array(),
);
$params['parent_exclude'] = array(
'description' => __( 'Limit result set to all items except those of a particular parent ID.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'sanitize_callback' => 'wp_parse_id_list',
'default' => array(),
);
$params['type'] = array(
'description' => __( 'Limit result set to items assigned a stock report type.', 'woocommerce' ),
'type' => 'string',
'default' => 'all',
'enum' => array_merge( array( 'all', 'lowstock' ), array_keys( wc_get_product_stock_status_options() ) ),
);
return $params;
}
/**
* Get the column names for export.
*
* @return array Key value pair of Column ID => Label.
*/
public function get_export_columns() {
$export_columns = array(
'title' => __( 'Product / Variation', 'woocommerce' ),
'sku' => __( 'SKU', 'woocommerce' ),
'stock_status' => __( 'Status', 'woocommerce' ),
'stock_quantity' => __( 'Stock', 'woocommerce' ),
);
/**
* Filter to add or remove column names from the stock report for
* export.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_report_stock_export_columns',
$export_columns
);
}
/**
* Get the column values for export.
*
* @param array $item Single report item/row.
* @return array Key value pair of Column ID => Row Value.
*/
public function prepare_item_for_export( $item ) {
$status = $item['stock_status'];
if ( array_key_exists( $item['stock_status'], $this->status_options ) ) {
$status = $this->status_options[ $item['stock_status'] ];
}
$export_item = array(
'title' => $item['name'],
'sku' => $item['sku'],
'stock_status' => $status,
'stock_quantity' => $item['stock_quantity'],
);
/**
* Filter to prepare extra columns in the export item for the stock
* report.
*
* @since 1.6.0
*/
return apply_filters(
'woocommerce_report_stock_prepare_export_item',
$export_item,
$item
);
}
}
API/Reports/Stock/Stats/Controller.php 0000644 00000007137 15153746747 0013663 0 ustar 00 <?php
/**
* REST API Reports stock stats controller
*
* Handles requests to the /reports/stock/stats endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Stock\Stats;
defined( 'ABSPATH' ) || exit;
/**
* REST API Reports stock stats controller class.
*
* @internal
* @extends WC_REST_Reports_Controller
*/
class Controller extends \WC_REST_Reports_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/stock/stats';
/**
* Get Stock Status Totals.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$stock_query = new Query();
$report_data = $stock_query->get_data();
$out_data = array(
'totals' => $report_data,
);
return rest_ensure_response( $out_data );
}
/**
* Prepare a report object for serialization.
*
* @param WC_Product $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$data = $report;
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @since 6.5.0
*
* @param WP_REST_Response $response The response object.
* @param WC_Product $report The original object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_stock_stats', $response, $report, $request );
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$totals = array(
'products' => array(
'description' => __( 'Number of products.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'lowstock' => array(
'description' => __( 'Number of low stock products.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
);
$status_options = wc_get_product_stock_status_options();
foreach ( $status_options as $status => $label ) {
$totals[ $status ] = array(
/* translators: Stock status. Example: "Number of low stock products */
'description' => sprintf( __( 'Number of %s products.', 'woocommerce' ), $label ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
);
}
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_customers_stats',
'type' => 'object',
'properties' => array(
'totals' => array(
'description' => __( 'Totals data.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => $totals,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
return $params;
}
}
API/Reports/Stock/Stats/DataStore.php 0000644 00000010565 15153746747 0013425 0 ustar 00 <?php
/**
* API\Reports\Stock\Stats\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Stock\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
/**
* API\Reports\Stock\Stats\DataStore.
*/
class DataStore extends ReportsDataStore implements DataStoreInterface {
/**
* Get stock counts for the whole store.
*
* @param array $query Not used for the stock stats data store, but needed for the interface.
* @return array Array of counts.
*/
public function get_data( $query ) {
$report_data = array();
$cache_expire = DAY_IN_SECONDS * 30;
$low_stock_transient_name = 'wc_admin_stock_count_lowstock';
$low_stock_count = get_transient( $low_stock_transient_name );
if ( false === $low_stock_count ) {
$low_stock_count = $this->get_low_stock_count();
set_transient( $low_stock_transient_name, $low_stock_count, $cache_expire );
} else {
$low_stock_count = intval( $low_stock_count );
}
$report_data['lowstock'] = $low_stock_count;
$status_options = wc_get_product_stock_status_options();
foreach ( $status_options as $status => $label ) {
$transient_name = 'wc_admin_stock_count_' . $status;
$count = get_transient( $transient_name );
if ( false === $count ) {
$count = $this->get_count( $status );
set_transient( $transient_name, $count, $cache_expire );
} else {
$count = intval( $count );
}
$report_data[ $status ] = $count;
}
$product_count_transient_name = 'wc_admin_product_count';
$product_count = get_transient( $product_count_transient_name );
if ( false === $product_count ) {
$product_count = $this->get_product_count();
set_transient( $product_count_transient_name, $product_count, $cache_expire );
} else {
$product_count = intval( $product_count );
}
$report_data['products'] = $product_count;
return $report_data;
}
/**
* Get low stock count (products with stock < low stock amount, but greater than no stock amount).
*
* @return int Low stock count.
*/
private function get_low_stock_count() {
global $wpdb;
$no_stock_amount = absint( max( get_option( 'woocommerce_notify_no_stock_amount' ), 0 ) );
$low_stock_amount = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
return (int) $wpdb->get_var(
$wpdb->prepare(
"
SELECT count( DISTINCT posts.ID ) FROM {$wpdb->posts} posts
LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON posts.ID = wc_product_meta_lookup.product_id
LEFT JOIN {$wpdb->postmeta} low_stock_amount_meta ON posts.ID = low_stock_amount_meta.post_id AND low_stock_amount_meta.meta_key = '_low_stock_amount'
WHERE posts.post_type IN ( 'product', 'product_variation' )
AND wc_product_meta_lookup.stock_quantity IS NOT NULL
AND wc_product_meta_lookup.stock_status = 'instock'
AND (
(
low_stock_amount_meta.meta_value > ''
AND wc_product_meta_lookup.stock_quantity <= CAST(low_stock_amount_meta.meta_value AS SIGNED)
AND wc_product_meta_lookup.stock_quantity > %d
)
OR (
(
low_stock_amount_meta.meta_value IS NULL OR low_stock_amount_meta.meta_value <= ''
)
AND wc_product_meta_lookup.stock_quantity <= %d
AND wc_product_meta_lookup.stock_quantity > %d
)
)
",
$no_stock_amount,
$low_stock_amount,
$no_stock_amount
)
);
}
/**
* Get count for the passed in stock status.
*
* @param string $status Status slug.
* @return int Count.
*/
private function get_count( $status ) {
global $wpdb;
return (int) $wpdb->get_var(
$wpdb->prepare(
"
SELECT count( DISTINCT posts.ID ) FROM {$wpdb->posts} posts
LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON posts.ID = wc_product_meta_lookup.product_id
WHERE posts.post_type IN ( 'product', 'product_variation' )
AND wc_product_meta_lookup.stock_status = %s
",
$status
)
);
}
/**
* Get product count for the store.
*
* @return int Product count.
*/
private function get_product_count() {
$query_args = array();
$query_args['post_type'] = array( 'product', 'product_variation' );
$query = new \WP_Query();
$query->query( $query_args );
return intval( $query->found_posts );
}
}
API/Reports/Stock/Stats/Query.php 0000644 00000001316 15153746747 0012636 0 ustar 00 <?php
/**
* Class for stock stats report querying
*
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\Query();
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Stock\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Stock\Stats\Query
*/
class Query extends ReportsQuery {
/**
* Get product data based on the current query vars.
*
* @return array
*/
public function get_data() {
$data_store = \WC_Data_Store::load( 'report-stock-stats' );
$results = $data_store->get_data();
return apply_filters( 'woocommerce_analytics_stock_stats_query', $results );
}
}
API/Reports/Taxes/Controller.php 0000644 00000016544 15153746747 0012570 0 ustar 00 <?php
/**
* REST API Reports taxes controller
*
* Handles requests to the /reports/taxes endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericController;
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits;
use WP_REST_Request;
use WP_REST_Response;
/**
* REST API Reports taxes controller class.
*
* @internal
* @extends GenericController
*/
class Controller extends GenericController implements ExportableInterface {
/**
* Exportable traits.
*/
use ExportableTraits;
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/taxes';
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['before'] = $request['before'];
$args['after'] = $request['after'];
$args['page'] = $request['page'];
$args['per_page'] = $request['per_page'];
$args['orderby'] = $request['orderby'];
$args['order'] = $request['order'];
$args['taxes'] = $request['taxes'];
$args['force_cache_refresh'] = $request['force_cache_refresh'];
return $args;
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$taxes_query = new Query( $query_args );
$report_data = $taxes_query->get_data();
$data = array();
foreach ( $report_data->data as $tax_data ) {
$item = $this->prepare_item_for_response( (object) $tax_data, $request );
$data[] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$data,
(int) $report_data->total,
(int) $report_data->page_no,
(int) $report_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param stdClass $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$response = parent::prepare_item_for_response( $report, $request );
$response->add_links( $this->prepare_links( $report ) );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_taxes', $response, $report, $request );
}
/**
* Prepare links for the request.
*
* @param WC_Reports_Query $object Object data.
* @return array
*/
protected function prepare_links( $object ) {
$links = array(
'tax' => array(
'href' => rest_url( sprintf( '/%s/taxes/%d', $this->namespace, $object->tax_rate_id ) ),
),
);
return $links;
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_taxes',
'type' => 'object',
'properties' => array(
'tax_rate_id' => array(
'description' => __( 'Tax rate ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'name' => array(
'description' => __( 'Tax rate name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'tax_rate' => array(
'description' => __( 'Tax rate.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'country' => array(
'description' => __( 'Country / Region.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'state' => array(
'description' => __( 'State.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'priority' => array(
'description' => __( 'Priority.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'total_tax' => array(
'description' => __( 'Total tax.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'order_tax' => array(
'description' => __( 'Order tax.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'shipping_tax' => array(
'description' => __( 'Shipping tax.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'orders_count' => array(
'description' => __( 'Number of orders.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['orderby']['default'] = 'tax_rate_id';
$params['orderby']['enum'] = array(
'name',
'tax_rate_id',
'tax_code',
'rate',
'order_tax',
'total_tax',
'shipping_tax',
'orders_count',
);
$params['taxes'] = array(
'description' => __( 'Limit result set to items assigned one or more tax rates.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
return $params;
}
/**
* Get the column names for export.
*
* @return array Key value pair of Column ID => Label.
*/
public function get_export_columns() {
return array(
'tax_code' => __( 'Tax code', 'woocommerce' ),
'rate' => __( 'Rate', 'woocommerce' ),
'total_tax' => __( 'Total tax', 'woocommerce' ),
'order_tax' => __( 'Order tax', 'woocommerce' ),
'shipping_tax' => __( 'Shipping tax', 'woocommerce' ),
'orders_count' => __( 'Orders', 'woocommerce' ),
);
}
/**
* Get the column values for export.
*
* @param array $item Single report item/row.
* @return array Key value pair of Column ID => Row Value.
*/
public function prepare_item_for_export( $item ) {
return array(
'tax_code' => \WC_Tax::get_rate_code( $item['tax_rate_id'] ),
'rate' => $item['tax_rate'],
'total_tax' => self::csv_number_format( $item['total_tax'] ),
'order_tax' => self::csv_number_format( $item['order_tax'] ),
'shipping_tax' => self::csv_number_format( $item['shipping_tax'] ),
'orders_count' => $item['orders_count'],
);
}
}
API/Reports/Taxes/DataStore.php 0000644 00000025746 15153746747 0012337 0 ustar 00 <?php
/**
* API\Reports\Taxes\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
use Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
/**
* API\Reports\Taxes\DataStore.
*/
class DataStore extends ReportsDataStore implements DataStoreInterface {
/**
* Table used to get the data.
*
* @var string
*/
protected static $table_name = 'wc_order_tax_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'taxes';
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'tax_rate_id' => 'intval',
'name' => 'strval',
'tax_rate' => 'floatval',
'country' => 'strval',
'state' => 'strval',
'priority' => 'intval',
'total_tax' => 'floatval',
'order_tax' => 'floatval',
'shipping_tax' => 'floatval',
'orders_count' => 'intval',
);
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'taxes';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$table_name = self::get_db_table_name();
$this->report_columns = array(
'tax_rate_id' => "{$table_name}.tax_rate_id",
'name' => 'tax_rate_name as name',
'tax_rate' => 'tax_rate',
'country' => 'tax_rate_country as country',
'state' => 'tax_rate_state as state',
'priority' => 'tax_rate_priority as priority',
'total_tax' => 'SUM(total_tax) as total_tax',
'order_tax' => 'SUM(order_tax) as order_tax',
'shipping_tax' => 'SUM(shipping_tax) as shipping_tax',
'orders_count' => "COUNT( DISTINCT ( CASE WHEN total_tax >= 0 THEN {$table_name}.order_id END ) ) as orders_count",
);
}
/**
* Set up all the hooks for maintaining and populating table data.
*/
public static function init() {
add_action( 'woocommerce_analytics_delete_order_stats', array( __CLASS__, 'sync_on_order_delete' ), 15 );
}
/**
* Fills FROM clause of SQL request based on user supplied parameters.
*
* @param array $query_args Query arguments supplied by the user.
* @param string $order_status_filter Order status subquery.
*/
protected function add_from_sql_params( $query_args, $order_status_filter ) {
global $wpdb;
$table_name = self::get_db_table_name();
if ( $order_status_filter ) {
$this->subquery->add_sql_clause( 'join', "JOIN {$wpdb->prefix}wc_order_stats ON {$table_name}.order_id = {$wpdb->prefix}wc_order_stats.order_id" );
}
if ( isset( $query_args['taxes'] ) && ! empty( $query_args['taxes'] ) ) {
$this->add_sql_clause( 'join', "JOIN {$wpdb->prefix}woocommerce_tax_rates ON default_results.tax_rate_id = {$wpdb->prefix}woocommerce_tax_rates.tax_rate_id" );
} else {
$this->subquery->add_sql_clause( 'join', "JOIN {$wpdb->prefix}woocommerce_tax_rates ON {$table_name}.tax_rate_id = {$wpdb->prefix}woocommerce_tax_rates.tax_rate_id" );
}
}
/**
* Updates the database query with parameters used for Taxes report: categories and order status.
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function add_sql_query_params( $query_args ) {
global $wpdb;
$order_tax_lookup_table = self::get_db_table_name();
$this->add_time_period_sql_params( $query_args, $order_tax_lookup_table );
$this->get_limit_sql_params( $query_args );
$this->add_order_by_sql_params( $query_args );
$order_status_filter = $this->get_status_subquery( $query_args );
$this->add_from_sql_params( $query_args, $order_status_filter );
if ( isset( $query_args['taxes'] ) && ! empty( $query_args['taxes'] ) ) {
$allowed_taxes = self::get_filtered_ids( $query_args, 'taxes' );
$this->subquery->add_sql_clause( 'where', "AND {$order_tax_lookup_table}.tax_rate_id IN ({$allowed_taxes})" );
}
if ( $order_status_filter ) {
$this->subquery->add_sql_clause( 'where', "AND ( {$order_status_filter} )" );
}
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$table_name = self::get_db_table_name();
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'tax_rate_id',
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
'fields' => '*',
'taxes' => array(),
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$data = (object) array(
'data' => array(),
'total' => 0,
'pages' => 0,
'page_no' => 0,
);
$this->add_sql_query_params( $query_args );
$params = $this->get_limit_params( $query_args );
if ( isset( $query_args['taxes'] ) && ! empty( $query_args['taxes'] ) ) {
$total_results = count( $query_args['taxes'] );
$total_pages = (int) ceil( $total_results / $params['per_page'] );
$inner_selections = array( 'tax_rate_id', 'total_tax', 'order_tax', 'shipping_tax', 'orders_count' );
$outer_selections = array( 'name', 'tax_rate', 'country', 'state', 'priority' );
$selections = $this->selected_columns( array( 'fields' => $inner_selections ) );
$fields = $this->get_fields( $query_args );
$join_selections = $this->format_join_selections( $fields, array( 'tax_rate_id' ), $outer_selections );
$ids_table = $this->get_ids_table( $query_args['taxes'], 'tax_rate_id' );
$this->subquery->clear_sql_clause( 'select' );
$this->subquery->add_sql_clause( 'select', $this->selected_columns( array( 'fields' => $inner_selections ) ) );
$this->add_sql_clause( 'select', $join_selections );
$this->add_sql_clause( 'from', '(' );
$this->add_sql_clause( 'from', $this->subquery->get_query_statement() );
$this->add_sql_clause( 'from', ") AS {$table_name}" );
$this->add_sql_clause(
'right_join',
"RIGHT JOIN ( {$ids_table} ) AS default_results
ON default_results.tax_rate_id = {$table_name}.tax_rate_id"
);
$taxes_query = $this->get_query_statement();
} else {
$db_records_count = (int) $wpdb->get_var(
"SELECT COUNT(*) FROM (
{$this->subquery->get_query_statement()}
) AS tt"
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
$total_results = $db_records_count;
$total_pages = (int) ceil( $db_records_count / $params['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return $data;
}
$this->subquery->clear_sql_clause( 'select' );
$this->subquery->add_sql_clause( 'select', $this->selected_columns( $query_args ) );
$this->subquery->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$taxes_query = $this->subquery->get_query_statement();
}
$tax_data = $wpdb->get_results(
$taxes_query,
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $tax_data ) {
return $data;
}
$tax_data = array_map( array( $this, 'cast_numbers' ), $tax_data );
$data = (object) array(
'data' => $tax_data,
'total' => $total_results,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Maps ordering specified by the user to columns in the database/fields in the data.
*
* @param string $order_by Sorting criterion.
* @return string
*/
protected function normalize_order_by( $order_by ) {
global $wpdb;
if ( 'tax_code' === $order_by ) {
return 'CONCAT_WS( "-", NULLIF(tax_rate_country, ""), NULLIF(tax_rate_state, ""), NULLIF(tax_rate_name, ""), NULLIF(tax_rate_priority, "") )';
} elseif ( 'rate' === $order_by ) {
return "CAST({$wpdb->prefix}woocommerce_tax_rates.tax_rate as DECIMAL(7,4))";
}
return $order_by;
}
/**
* Create or update an entry in the wc_order_tax_lookup table for an order.
*
* @param int $order_id Order ID.
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
*/
public static function sync_order_taxes( $order_id ) {
global $wpdb;
$order = wc_get_order( $order_id );
if ( ! $order ) {
return -1;
}
$tax_items = $order->get_items( 'tax' );
$num_updated = 0;
foreach ( $tax_items as $tax_item ) {
$result = $wpdb->replace(
self::get_db_table_name(),
array(
'order_id' => $order->get_id(),
'date_created' => $order->get_date_created( 'edit' )->date( TimeInterval::$sql_datetime_format ),
'tax_rate_id' => $tax_item->get_rate_id(),
'shipping_tax' => $tax_item->get_shipping_tax_total(),
'order_tax' => $tax_item->get_tax_total(),
'total_tax' => (float) $tax_item->get_tax_total() + (float) $tax_item->get_shipping_tax_total(),
),
array(
'%d',
'%s',
'%d',
'%f',
'%f',
'%f',
)
);
/**
* Fires when tax's reports are updated.
*
* @param int $tax_rate_id Tax Rate ID.
* @param int $order_id Order ID.
*/
do_action( 'woocommerce_analytics_update_tax', $tax_item->get_rate_id(), $order->get_id() );
// Sum the rows affected. Using REPLACE can affect 2 rows if the row already exists.
$num_updated += 2 === intval( $result ) ? 1 : intval( $result );
}
return ( count( $tax_items ) === $num_updated );
}
/**
* Clean taxes data when an order is deleted.
*
* @param int $order_id Order ID.
*/
public static function sync_on_order_delete( $order_id ) {
global $wpdb;
$wpdb->delete( self::get_db_table_name(), array( 'order_id' => $order_id ) );
/**
* Fires when tax's reports are removed from database.
*
* @param int $tax_rate_id Tax Rate ID.
* @param int $order_id Order ID.
*/
do_action( 'woocommerce_analytics_delete_tax', 0, $order_id );
ReportsCache::invalidate();
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
$this->subquery = new SqlQuery( $this->context . '_subquery' );
$this->subquery->add_sql_clause( 'select', self::get_db_table_name() . '.tax_rate_id' );
$this->subquery->add_sql_clause( 'from', self::get_db_table_name() );
$this->subquery->add_sql_clause( 'group_by', self::get_db_table_name() . '.tax_rate_id' );
}
}
API/Reports/Taxes/Query.php 0000644 00000002245 15153746747 0011543 0 ustar 00 <?php
/**
* Class for parameter-based Taxes Report querying
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'page' => 2,
* 'taxes' => array(1,2,3)
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Taxes\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Taxes\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Taxes report.
*
* @return array
*/
protected function get_default_query_vars() {
return array();
}
/**
* Get product data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_taxes_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-taxes' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_taxes_select_query', $results, $args );
}
}
API/Reports/Taxes/Stats/Controller.php 0000644 00000015501 15153746747 0013656 0 ustar 00 <?php
/**
* REST API Reports taxes stats controller
*
* Handles requests to the /reports/taxes/stats endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericStatsController;
use WP_REST_Request;
use WP_REST_Response;
/**
* REST API Reports taxes stats controller class.
*
* @internal
* @extends GenericStatsController
*/
class Controller extends GenericStatsController {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/taxes/stats';
/**
* Constructor.
*/
public function __construct() {
add_filter( 'woocommerce_analytics_taxes_stats_select_query', array( $this, 'set_default_report_data' ) );
}
/**
* Set the default results to 0 if API returns an empty array
*
* @internal
* @param Mixed $results Report data.
* @return object
*/
public function set_default_report_data( $results ) {
if ( empty( $results ) ) {
$results = new \stdClass();
$results->total = 0;
$results->totals = new \stdClass();
$results->totals->tax_codes = 0;
$results->totals->total_tax = 0;
$results->totals->order_tax = 0;
$results->totals->shipping_tax = 0;
$results->totals->orders = 0;
$results->intervals = array();
$results->pages = 1;
$results->page_no = 1;
}
return $results;
}
/**
* Maps query arguments from the REST request.
*
* @param array $request Request array.
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['before'] = $request['before'];
$args['after'] = $request['after'];
$args['interval'] = $request['interval'];
$args['page'] = $request['page'];
$args['per_page'] = $request['per_page'];
$args['orderby'] = $request['orderby'];
$args['order'] = $request['order'];
$args['taxes'] = (array) $request['taxes'];
$args['segmentby'] = $request['segmentby'];
$args['fields'] = $request['fields'];
$args['force_cache_refresh'] = $request['force_cache_refresh'];
return $args;
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$taxes_query = new Query( $query_args );
$report_data = $taxes_query->get_data();
$out_data = array(
'totals' => get_object_vars( $report_data->totals ),
'intervals' => array(),
);
foreach ( $report_data->intervals as $interval_data ) {
$item = $this->prepare_item_for_response( (object) $interval_data, $request );
$out_data['intervals'][] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$out_data,
(int) $report_data->total,
(int) $report_data->page_no,
(int) $report_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param stdClass $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$data = get_object_vars( $report );
$response = parent::prepare_item_for_response( $data, $request );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_taxes_stats', $response, $report, $request );
}
/**
* Get the Report's item properties schema.
* Will be used by `get_item_schema` as `totals` and `subtotals`.
*
* @return array
*/
protected function get_item_properties_schema() {
return array(
'total_tax' => array(
'description' => __( 'Total tax.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
'format' => 'currency',
),
'order_tax' => array(
'description' => __( 'Order tax.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
'format' => 'currency',
),
'shipping_tax' => array(
'description' => __( 'Shipping tax.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
'format' => 'currency',
),
'orders_count' => array(
'description' => __( 'Number of orders.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'tax_codes' => array(
'description' => __( 'Amount of tax codes.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
);
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = parent::get_item_schema();
$schema['title'] = 'report_taxes_stats';
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['orderby']['enum'] = array(
'date',
'items_sold',
'total_sales',
'orders_count',
'products_count',
);
$params['taxes'] = array(
'description' => __( 'Limit result set to all items that have the specified term assigned in the taxes taxonomy.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['segmentby'] = array(
'description' => __( 'Segment the response by additional constraint.', 'woocommerce' ),
'type' => 'string',
'enum' => array(
'tax_rate_id',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['fields'] = array(
'description' => __( 'Limit stats fields to the specified items.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
return $params;
}
}
API/Reports/Taxes/Stats/DataStore.php 0000644 00000023636 15153746747 0013431 0 ustar 00 <?php
/**
* API\Reports\Taxes\Stats\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
/**
* API\Reports\Taxes\Stats\DataStore.
*/
class DataStore extends ReportsDataStore implements DataStoreInterface {
/**
* Table used to get the data.
*
* @var string
*/
protected static $table_name = 'wc_order_tax_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'taxes_stats';
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'tax_codes' => 'intval',
'total_tax' => 'floatval',
'order_tax' => 'floatval',
'shipping_tax' => 'floatval',
'orders_count' => 'intval',
);
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'taxes_stats';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$table_name = self::get_db_table_name();
$this->report_columns = array(
'tax_codes' => 'COUNT(DISTINCT tax_rate_id) as tax_codes',
'total_tax' => 'SUM(total_tax) AS total_tax',
'order_tax' => 'SUM(order_tax) as order_tax',
'shipping_tax' => 'SUM(shipping_tax) as shipping_tax',
'orders_count' => "COUNT( DISTINCT ( CASE WHEN parent_id = 0 THEN {$table_name}.order_id END ) ) as orders_count",
);
}
/**
* Updates the database query with parameters used for Taxes Stats report
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function update_sql_query_params( $query_args ) {
global $wpdb;
$taxes_where_clause = '';
$order_tax_lookup_table = self::get_db_table_name();
if ( isset( $query_args['taxes'] ) && ! empty( $query_args['taxes'] ) ) {
$tax_id_placeholders = implode( ',', array_fill( 0, count( $query_args['taxes'] ), '%d' ) );
/* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
$taxes_where_clause .= $wpdb->prepare( " AND {$order_tax_lookup_table}.tax_rate_id IN ({$tax_id_placeholders})", $query_args['taxes'] );
/* phpcs:enable */
}
$order_status_filter = $this->get_status_subquery( $query_args );
if ( $order_status_filter ) {
$taxes_where_clause .= " AND ( {$order_status_filter} )";
}
$this->add_time_period_sql_params( $query_args, $order_tax_lookup_table );
$this->total_query->add_sql_clause( 'where', $taxes_where_clause );
$this->add_intervals_sql_params( $query_args, $order_tax_lookup_table );
$this->interval_query->add_sql_clause( 'where', $taxes_where_clause );
$this->interval_query->add_sql_clause( 'select', $this->get_sql_clause( 'select' ) . ' AS time_interval' );
$this->interval_query->add_sql_clause( 'where_time', $this->get_sql_clause( 'where_time' ) );
}
/**
* Get taxes associated with a store.
*
* @param array $args Array of args to filter the query by. Supports `include`.
* @return array An array of all taxes.
*/
public static function get_taxes( $args ) {
global $wpdb;
$query = "
SELECT
tax_rate_id,
tax_rate_country,
tax_rate_state,
tax_rate_name,
tax_rate_priority
FROM {$wpdb->prefix}woocommerce_tax_rates
";
if ( ! empty( $args['include'] ) ) {
/* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
$tax_placeholders = implode( ',', array_fill( 0, count( $args['include'] ), '%d' ) );
$query .= $wpdb->prepare( " WHERE tax_rate_id IN ({$tax_placeholders})", $args['include'] );
/* phpcs:enable */
}
return $wpdb->get_results( $query, ARRAY_A ); // WPCS: cache ok, DB call ok, unprepared SQL ok.
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$table_name = self::get_db_table_name();
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'tax_rate_id',
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
'fields' => '*',
'taxes' => array(),
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$data = (object) array(
'totals' => (object) array(),
'intervals' => (object) array(),
'total' => 0,
'pages' => 0,
'page_no' => 0,
);
$selections = $this->selected_columns( $query_args );
$params = $this->get_limit_params( $query_args );
$order_stats_join = "JOIN {$wpdb->prefix}wc_order_stats ON {$table_name}.order_id = {$wpdb->prefix}wc_order_stats.order_id";
$this->update_sql_query_params( $query_args );
$this->interval_query->add_sql_clause( 'join', $order_stats_join );
$db_intervals = $wpdb->get_col(
$this->interval_query->get_query_statement()
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
$db_interval_count = count( $db_intervals );
$expected_interval_count = TimeInterval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $params['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return $data;
}
$this->total_query->add_sql_clause( 'select', $selections );
$this->total_query->add_sql_clause( 'join', $order_stats_join );
$this->total_query->add_sql_clause( 'where_time', $this->get_sql_clause( 'where_time' ) );
$totals = $wpdb->get_results(
$this->total_query->get_query_statement(),
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $totals ) {
return new \WP_Error( 'woocommerce_analytics_taxes_stats_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce' ) );
}
// @todo remove these assignements when refactoring segmenter classes to use query objects.
$totals_query = array(
'from_clause' => $this->total_query->get_sql_clause( 'join' ),
'where_time_clause' => $this->total_query->get_sql_clause( 'where_time' ),
'where_clause' => $this->total_query->get_sql_clause( 'where' ),
);
$intervals_query = array(
'select_clause' => $this->get_sql_clause( 'select' ),
'from_clause' => $this->interval_query->get_sql_clause( 'join' ),
'where_time_clause' => $this->interval_query->get_sql_clause( 'where_time' ),
'where_clause' => $this->interval_query->get_sql_clause( 'where' ),
);
$segmenter = new Segmenter( $query_args, $this->report_columns );
$totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name );
$this->update_intervals_sql_params( $query_args, $db_interval_count, $expected_interval_count, $table_name );
if ( '' !== $selections ) {
$this->interval_query->add_sql_clause( 'select', ', ' . $selections );
}
$this->interval_query->add_sql_clause( 'select', ", MAX({$table_name}.date_created) AS datetime_anchor" );
$this->interval_query->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$this->interval_query->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
$intervals = $wpdb->get_results(
$this->interval_query->get_query_statement(),
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $intervals ) {
return new \WP_Error( 'woocommerce_analytics_taxes_stats_result_failed', __( 'Sorry, fetching tax data failed.', 'woocommerce' ) );
}
$totals = (object) $this->cast_numbers( $totals[0] );
$data = (object) array(
'totals' => $totals,
'intervals' => $intervals,
'total' => $expected_interval_count,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
if ( TimeInterval::intervals_missing( $expected_interval_count, $db_interval_count, $params['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
$this->remove_extra_records( $data, $query_args['page'], $params['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] );
} else {
$this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals );
}
$segmenter->add_intervals_segments( $data, $intervals_query, $table_name );
$this->create_interval_subtotals( $data->intervals );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
unset( $this->subquery );
$this->total_query = new SqlQuery( $this->context . '_total' );
$this->total_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query = new SqlQuery( $this->context . '_interval' );
$this->interval_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query->add_sql_clause( 'group_by', 'time_interval' );
}
}
API/Reports/Taxes/Stats/Query.php 0000644 00000002376 15153746747 0012646 0 ustar 00 <?php
/**
* Class for parameter-based Taxes Stats Report querying
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'page' => 2,
* 'categories' => array(15, 18),
* 'product_ids' => array(1,2,3)
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Taxes\Stats\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Taxes report.
*
* @return array
*/
protected function get_default_query_vars() {
return array();
}
/**
* Get tax stats data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_taxes_stats_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-taxes-stats' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_taxes_stats_select_query', $results, $args );
}
}
API/Reports/Taxes/Stats/Segmenter.php 0000644 00000013031 15153746747 0013460 0 ustar 00 <?php
/**
* Class for adding segmenting support without cluttering the data stores.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter;
/**
* Date & time interval and numeric range handling class for Reporting API.
*/
class Segmenter extends ReportsSegmenter {
/**
* Returns column => query mapping to be used for order-related order-level segmenting query (e.g. tax_rate_id).
*
* @param string $lookup_table Name of SQL table containing the order-level segmenting info.
*
* @return array Column => SELECT query mapping.
*/
protected function get_segment_selections_order_level( $lookup_table ) {
$columns_mapping = array(
'tax_codes' => "COUNT(DISTINCT $lookup_table.tax_rate_id) as tax_codes",
'total_tax' => "SUM($lookup_table.total_tax) AS total_tax",
'order_tax' => "SUM($lookup_table.order_tax) as order_tax",
'shipping_tax' => "SUM($lookup_table.shipping_tax) as shipping_tax",
'orders_count' => "COUNT(DISTINCT $lookup_table.order_id) as orders_count",
);
return $columns_mapping;
}
/**
* Calculate segments for totals query where the segmenting property is bound to order (e.g. coupon or customer type).
*
* @param string $segmenting_select SELECT part of segmenting SQL query.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $totals_query Array of SQL clauses for intervals query.
*
* @return array
*/
protected function get_order_related_totals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $totals_query ) {
global $wpdb;
$totals_segments = $wpdb->get_results(
"SELECT
$segmenting_groupby
$segmenting_select
FROM
$table_name
$segmenting_from
{$totals_query['from_clause']}
WHERE
1=1
{$totals_query['where_time_clause']}
{$totals_query['where_clause']}
$segmenting_where
GROUP BY
$segmenting_groupby",
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
// Reformat result.
$totals_segments = $this->reformat_totals_segments( $totals_segments, $segmenting_groupby );
return $totals_segments;
}
/**
* Calculate segments for intervals query where the segmenting property is bound to order (e.g. coupon or customer type).
*
* @param string $segmenting_select SELECT part of segmenting SQL query.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $intervals_query Array of SQL clauses for intervals query.
*
* @return array
*/
protected function get_order_related_intervals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $intervals_query ) {
global $wpdb;
$segmenting_limit = '';
$limit_parts = explode( ',', $intervals_query['limit'] );
if ( 2 === count( $limit_parts ) ) {
$orig_rowcount = intval( $limit_parts[1] );
$segmenting_limit = $limit_parts[0] . ',' . $orig_rowcount * count( $this->get_all_segments() );
}
$intervals_segments = $wpdb->get_results(
"SELECT
MAX($table_name.date_created) AS datetime_anchor,
{$intervals_query['select_clause']} AS time_interval,
$segmenting_groupby
$segmenting_select
FROM
$table_name
$segmenting_from
{$intervals_query['from_clause']}
WHERE
1=1
{$intervals_query['where_time_clause']}
{$intervals_query['where_clause']}
$segmenting_where
GROUP BY
time_interval, $segmenting_groupby
$segmenting_limit",
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
// Reformat result.
$intervals_segments = $this->reformat_intervals_segments( $intervals_segments, $segmenting_groupby );
return $intervals_segments;
}
/**
* Return array of segments formatted for REST response.
*
* @param string $type Type of segments to return--'totals' or 'intervals'.
* @param array $query_params SQL query parameter array.
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
*
* @return array
* @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified.
*/
protected function get_segments( $type, $query_params, $table_name ) {
if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) {
return array();
}
$segmenting_where = '';
$segmenting_from = '';
$segments = array();
if ( 'tax_rate_id' === $this->query_args['segmentby'] ) {
$tax_rate_level_columns = $this->get_segment_selections_order_level( $table_name );
$segmenting_select = $this->prepare_selections( $tax_rate_level_columns );
$this->report_columns = $tax_rate_level_columns;
$segmenting_groupby = $table_name . '.tax_rate_id';
$segments = $this->get_order_related_segments( $type, $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params );
}
return $segments;
}
}
API/Reports/TimeInterval.php 0000644 00000056655 15153746747 0011773 0 ustar 00 <?php
/**
* Class for time interval and numeric range handling for reports.
*/
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit;
/**
* Date & time interval and numeric range handling class for Reporting API.
*/
class TimeInterval {
/**
* Format string for ISO DateTime formatter.
*
* @var string
*/
public static $iso_datetime_format = 'Y-m-d\TH:i:s';
/**
* Format string for use in SQL queries.
*
* @var string
*/
public static $sql_datetime_format = 'Y-m-d H:i:s';
/**
* Converts local datetime to GMT/UTC time.
*
* @param string $datetime_string String representation of local datetime.
* @return DateTime
*/
public static function convert_local_datetime_to_gmt( $datetime_string ) {
$datetime = new \DateTime( $datetime_string, new \DateTimeZone( wc_timezone_string() ) );
$datetime->setTimezone( new \DateTimeZone( 'GMT' ) );
return $datetime;
}
/**
* Returns default 'before' parameter for the reports.
*
* @return DateTime
*/
public static function default_before() {
$datetime = new \WC_DateTime();
// Set local timezone or offset.
if ( get_option( 'timezone_string' ) ) {
$datetime->setTimezone( new \DateTimeZone( wc_timezone_string() ) );
} else {
$datetime->set_utc_offset( wc_timezone_offset() );
}
return $datetime;
}
/**
* Returns default 'after' parameter for the reports.
*
* @return DateTime
*/
public static function default_after() {
$now = time();
$week_back = $now - WEEK_IN_SECONDS;
$datetime = new \WC_DateTime();
$datetime->setTimestamp( $week_back );
// Set local timezone or offset.
if ( get_option( 'timezone_string' ) ) {
$datetime->setTimezone( new \DateTimeZone( wc_timezone_string() ) );
} else {
$datetime->set_utc_offset( wc_timezone_offset() );
}
return $datetime;
}
/**
* Returns date format to be used as grouping clause in SQL.
*
* @param string $time_interval Time interval.
* @param string $table_name Name of the db table relevant for the date constraint.
* @param string $date_column_name Name of the date table column.
* @return mixed
*/
public static function db_datetime_format( $time_interval, $table_name, $date_column_name = 'date_created' ) {
$first_day_of_week = absint( get_option( 'start_of_week' ) );
if ( 1 === $first_day_of_week ) {
// Week begins on Monday, ISO 8601.
$week_format = "DATE_FORMAT({$table_name}.`{$date_column_name}`, '%x-%v')";
} else {
// Week begins on day other than specified by ISO 8601, needs to be in sync with function simple_week_number.
$week_format = "CONCAT(YEAR({$table_name}.`{$date_column_name}`), '-', LPAD( FLOOR( ( DAYOFYEAR({$table_name}.`{$date_column_name}`) + ( ( DATE_FORMAT(MAKEDATE(YEAR({$table_name}.`{$date_column_name}`),1), '%w') - $first_day_of_week + 7 ) % 7 ) - 1 ) / 7 ) + 1 , 2, '0'))";
}
// Whenever this is changed, double check method time_interval_id to make sure they are in sync.
$mysql_date_format_mapping = array(
'hour' => "DATE_FORMAT({$table_name}.`{$date_column_name}`, '%Y-%m-%d %H')",
'day' => "DATE_FORMAT({$table_name}.`{$date_column_name}`, '%Y-%m-%d')",
'week' => $week_format,
'month' => "DATE_FORMAT({$table_name}.`{$date_column_name}`, '%Y-%m')",
'quarter' => "CONCAT(YEAR({$table_name}.`{$date_column_name}`), '-', QUARTER({$table_name}.`{$date_column_name}`))",
'year' => "YEAR({$table_name}.`{$date_column_name}`)",
);
return $mysql_date_format_mapping[ $time_interval ];
}
/**
* Returns quarter for the DateTime.
*
* @param DateTime $datetime Local date & time.
* @return int|null
*/
public static function quarter( $datetime ) {
switch ( (int) $datetime->format( 'm' ) ) {
case 1:
case 2:
case 3:
return 1;
case 4:
case 5:
case 6:
return 2;
case 7:
case 8:
case 9:
return 3;
case 10:
case 11:
case 12:
return 4;
}
return null;
}
/**
* Returns simple week number for the DateTime, for week starting on $first_day_of_week.
*
* The first week of the year is considered to be the week containing January 1.
* The second week starts on the next $first_day_of_week.
*
* @param DateTime $datetime Local date for which the week number is to be calculated.
* @param int $first_day_of_week 0 for Sunday to 6 for Saturday.
* @return int
*/
public static function simple_week_number( $datetime, $first_day_of_week ) {
$beg_of_year_day = new \DateTime( "{$datetime->format('Y')}-01-01" );
$adj_day_beg_of_year = ( (int) $beg_of_year_day->format( 'w' ) - $first_day_of_week + 7 ) % 7;
$days_since_start_of_year = (int) $datetime->format( 'z' ) + 1;
return (int) floor( ( ( $days_since_start_of_year + $adj_day_beg_of_year - 1 ) / 7 ) ) + 1;
}
/**
* Returns ISO 8601 week number for the DateTime, if week starts on Monday,
* otherwise returns simple week number.
*
* @see TimeInterval::simple_week_number()
*
* @param DateTime $datetime Local date for which the week number is to be calculated.
* @param int $first_day_of_week 0 for Sunday to 6 for Saturday.
* @return int
*/
public static function week_number( $datetime, $first_day_of_week ) {
if ( 1 === $first_day_of_week ) {
$week_number = (int) $datetime->format( 'W' );
} else {
$week_number = self::simple_week_number( $datetime, $first_day_of_week );
}
return $week_number;
}
/**
* Returns time interval id for the DateTime.
*
* @param string $time_interval Time interval type (week, day, etc).
* @param DateTime $datetime Date & time.
* @return string
*/
public static function time_interval_id( $time_interval, $datetime ) {
// Whenever this is changed, double check method db_datetime_format to make sure they are in sync.
$php_time_format_for = array(
'hour' => 'Y-m-d H',
'day' => 'Y-m-d',
'week' => 'o-W',
'month' => 'Y-m',
'quarter' => 'Y-' . self::quarter( $datetime ),
'year' => 'Y',
);
// If the week does not begin on Monday.
$first_day_of_week = absint( get_option( 'start_of_week' ) );
if ( 'week' === $time_interval && 1 !== $first_day_of_week ) {
$week_no = self::simple_week_number( $datetime, $first_day_of_week );
$week_no = str_pad( $week_no, 2, '0', STR_PAD_LEFT );
$year_no = $datetime->format( 'Y' );
return "$year_no-$week_no";
}
return $datetime->format( $php_time_format_for[ $time_interval ] );
}
/**
* Calculates number of time intervals between two dates, closed interval on both sides.
*
* @param DateTime $start_datetime Start date & time.
* @param DateTime $end_datetime End date & time.
* @param string $interval Time interval increment, e.g. hour, day, week.
*
* @return int
*/
public static function intervals_between( $start_datetime, $end_datetime, $interval ) {
switch ( $interval ) {
case 'hour':
$end_timestamp = (int) $end_datetime->format( 'U' );
$start_timestamp = (int) $start_datetime->format( 'U' );
$addendum = 0;
// modulo HOUR_IN_SECONDS would normally work, but there are non-full hour timezones, e.g. Nepal.
$start_min_sec = (int) $start_datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $start_datetime->format( 's' );
$end_min_sec = (int) $end_datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $end_datetime->format( 's' );
if ( $end_min_sec < $start_min_sec ) {
$addendum = 1;
}
$diff_timestamp = $end_timestamp - $start_timestamp;
return (int) floor( ( (int) $diff_timestamp ) / HOUR_IN_SECONDS ) + 1 + $addendum;
case 'day':
$days = $start_datetime->diff( $end_datetime )->format( '%r%a' );
$end_hour_min_sec = (int) $end_datetime->format( 'H' ) * HOUR_IN_SECONDS + (int) $end_datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $end_datetime->format( 's' );
$start_hour_min_sec = (int) $start_datetime->format( 'H' ) * HOUR_IN_SECONDS + (int) $start_datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $start_datetime->format( 's' );
if ( $end_hour_min_sec < $start_hour_min_sec ) {
$days++;
}
return $days + 1;
case 'week':
// @todo Optimize? approximately day count / 7, but year end is tricky, a week can have fewer days.
$week_count = 0;
do {
$start_datetime = self::next_week_start( $start_datetime );
$week_count++;
} while ( $start_datetime <= $end_datetime );
return $week_count;
case 'month':
// Year diff in months: (end_year - start_year - 1) * 12.
$year_diff_in_months = ( (int) $end_datetime->format( 'Y' ) - (int) $start_datetime->format( 'Y' ) - 1 ) * 12;
// All the months in end_date year plus months from X to 12 in the start_date year.
$month_diff = (int) $end_datetime->format( 'n' ) + ( 12 - (int) $start_datetime->format( 'n' ) );
// Add months for number of years between end_date and start_date.
$month_diff += $year_diff_in_months + 1;
return $month_diff;
case 'quarter':
// Year diff in quarters: (end_year - start_year - 1) * 4.
$year_diff_in_quarters = ( (int) $end_datetime->format( 'Y' ) - (int) $start_datetime->format( 'Y' ) - 1 ) * 4;
// All the quarters in end_date year plus quarters from X to 4 in the start_date year.
$quarter_diff = self::quarter( $end_datetime ) + ( 4 - self::quarter( $start_datetime ) );
// Add quarters for number of years between end_date and start_date.
$quarter_diff += $year_diff_in_quarters + 1;
return $quarter_diff;
case 'year':
$year_diff = (int) $end_datetime->format( 'Y' ) - (int) $start_datetime->format( 'Y' );
return $year_diff + 1;
}
return 0;
}
/**
* Returns a new DateTime object representing the next hour start/previous hour end if reversed.
*
* @param DateTime $datetime Date and time.
* @param bool $reversed Going backwards in time instead of forward.
* @return DateTime
*/
public static function next_hour_start( $datetime, $reversed = false ) {
$hour_increment = $reversed ? 0 : 1;
$timestamp = (int) $datetime->format( 'U' );
$seconds_into_hour = (int) $datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $datetime->format( 's' );
$hours_offset_timestamp = $timestamp + ( $hour_increment * HOUR_IN_SECONDS - $seconds_into_hour );
if ( $reversed ) {
$hours_offset_timestamp --;
}
$hours_offset_time = new \DateTime();
$hours_offset_time->setTimestamp( $hours_offset_timestamp );
$hours_offset_time->setTimezone( new \DateTimeZone( wc_timezone_string() ) );
return $hours_offset_time;
}
/**
* Returns a new DateTime object representing the next day start, or previous day end if reversed.
*
* @param DateTime $datetime Date and time.
* @param bool $reversed Going backwards in time instead of forward.
* @return DateTime
*/
public static function next_day_start( $datetime, $reversed = false ) {
$oneday = new \DateInterval( 'P1D' );
$new_datetime = clone $datetime;
if ( $reversed ) {
$new_datetime->sub( $oneday );
$new_datetime->setTime( 23, 59, 59 );
} else {
$new_datetime->add( $oneday );
$new_datetime->setTime( 0, 0, 0 );
}
return $new_datetime;
}
/**
* Returns DateTime object representing the next week start, or previous week end if reversed.
*
* The next week start is the first day of the next week at 00:00:00.
* The previous week end is the last day of the previous week at 23:59:59.
* The start day is determined by the "start_of_week" wp_option.
*
* @param DateTime $datetime Date and time.
* @param bool $reversed Going backwards in time instead of forward.
* @return DateTime
*/
public static function next_week_start( $datetime, $reversed = false ) {
$seven_days = new \DateInterval( 'P7D' );
// Default timezone set in wp-settings.php.
$default_timezone = date_default_timezone_get();
// Timezone that the WP site uses in Settings > General.
$original_timezone = $datetime->getTimezone();
// @codingStandardsIgnoreStart
date_default_timezone_set( 'UTC' );
$start_end_timestamp = get_weekstartend( $datetime->format( 'Y-m-d' ) );
date_default_timezone_set( $default_timezone );
// @codingStandardsIgnoreEnd
if ( $reversed ) {
$result = \DateTime::createFromFormat( 'U', $start_end_timestamp['end'] )->sub( $seven_days );
} else {
$result = \DateTime::createFromFormat( 'U', $start_end_timestamp['start'] )->add( $seven_days );
}
return \DateTime::createFromFormat( 'Y-m-d H:i:s', $result->format( 'Y-m-d H:i:s' ), $original_timezone );
}
/**
* Returns a new DateTime object representing the next month start, or previous month end if reversed.
*
* @param DateTime $datetime Date and time.
* @param bool $reversed Going backwards in time instead of forward.
* @return DateTime
*/
public static function next_month_start( $datetime, $reversed = false ) {
$month_increment = 1;
$year = $datetime->format( 'Y' );
$month = (int) $datetime->format( 'm' );
if ( $reversed ) {
$beg_of_month_datetime = new \DateTime( "$year-$month-01 00:00:00", new \DateTimeZone( wc_timezone_string() ) );
$timestamp = (int) $beg_of_month_datetime->format( 'U' );
$end_of_prev_month_timestamp = $timestamp - 1;
$datetime->setTimestamp( $end_of_prev_month_timestamp );
} else {
$month += $month_increment;
if ( $month > 12 ) {
$month = 1;
$year ++;
}
$day = '01';
$datetime = new \DateTime( "$year-$month-$day 00:00:00", new \DateTimeZone( wc_timezone_string() ) );
}
return $datetime;
}
/**
* Returns a new DateTime object representing the next quarter start, or previous quarter end if reversed.
*
* @param DateTime $datetime Date and time.
* @param bool $reversed Going backwards in time instead of forward.
* @return DateTime
*/
public static function next_quarter_start( $datetime, $reversed = false ) {
$year = $datetime->format( 'Y' );
$month = (int) $datetime->format( 'n' );
switch ( $month ) {
case 1:
case 2:
case 3:
if ( $reversed ) {
$month = 1;
} else {
$month = 4;
}
break;
case 4:
case 5:
case 6:
if ( $reversed ) {
$month = 4;
} else {
$month = 7;
}
break;
case 7:
case 8:
case 9:
if ( $reversed ) {
$month = 7;
} else {
$month = 10;
}
break;
case 10:
case 11:
case 12:
if ( $reversed ) {
$month = 10;
} else {
$month = 1;
$year ++;
}
break;
}
$datetime = new \DateTime( "$year-$month-01 00:00:00", new \DateTimeZone( wc_timezone_string() ) );
if ( $reversed ) {
$timestamp = (int) $datetime->format( 'U' );
$end_of_prev_month_timestamp = $timestamp - 1;
$datetime->setTimestamp( $end_of_prev_month_timestamp );
}
return $datetime;
}
/**
* Return a new DateTime object representing the next year start, or previous year end if reversed.
*
* @param DateTime $datetime Date and time.
* @param bool $reversed Going backwards in time instead of forward.
* @return DateTime
*/
public static function next_year_start( $datetime, $reversed = false ) {
$year_increment = 1;
$year = (int) $datetime->format( 'Y' );
$month = '01';
$day = '01';
if ( $reversed ) {
$datetime = new \DateTime( "$year-$month-$day 00:00:00", new \DateTimeZone( wc_timezone_string() ) );
$timestamp = (int) $datetime->format( 'U' );
$end_of_prev_year_timestamp = $timestamp - 1;
$datetime->setTimestamp( $end_of_prev_year_timestamp );
} else {
$year += $year_increment;
$datetime = new \DateTime( "$year-$month-$day 00:00:00", new \DateTimeZone( wc_timezone_string() ) );
}
return $datetime;
}
/**
* Returns beginning of next time interval for provided DateTime.
*
* E.g. for current DateTime, beginning of next day, week, quarter, etc.
*
* @param DateTime $datetime Date and time.
* @param string $time_interval Time interval, e.g. week, day, hour.
* @param bool $reversed Going backwards in time instead of forward.
* @return DateTime
*/
public static function iterate( $datetime, $time_interval, $reversed = false ) {
return call_user_func( array( __CLASS__, "next_{$time_interval}_start" ), $datetime, $reversed );
}
/**
* Returns expected number of items on the page in case of date ordering.
*
* @param int $expected_interval_count Expected number of intervals in total.
* @param int $items_per_page Number of items per page.
* @param int $page_no Page number.
*
* @return float|int
*/
public static function expected_intervals_on_page( $expected_interval_count, $items_per_page, $page_no ) {
$total_pages = (int) ceil( $expected_interval_count / $items_per_page );
if ( $page_no < $total_pages ) {
return $items_per_page;
} elseif ( $page_no === $total_pages ) {
return $expected_interval_count - ( $page_no - 1 ) * $items_per_page;
} else {
return 0;
}
}
/**
* Returns true if there are any intervals that need to be filled in the response.
*
* @param int $expected_interval_count Expected number of intervals in total.
* @param int $db_records Total number of records for given period in the database.
* @param int $items_per_page Number of items per page.
* @param int $page_no Page number.
* @param string $order asc or desc.
* @param string $order_by Column by which the result will be sorted.
* @param int $intervals_count Number of records for given (possibly shortened) time interval.
*
* @return bool
*/
public static function intervals_missing( $expected_interval_count, $db_records, $items_per_page, $page_no, $order, $order_by, $intervals_count ) {
if ( $expected_interval_count <= $db_records ) {
return false;
}
if ( 'date' === $order_by ) {
$expected_intervals_on_page = self::expected_intervals_on_page( $expected_interval_count, $items_per_page, $page_no );
return $intervals_count < $expected_intervals_on_page;
}
if ( 'desc' === $order ) {
return $page_no > floor( $db_records / $items_per_page );
}
if ( 'asc' === $order ) {
return $page_no <= ceil( ( $expected_interval_count - $db_records ) / $items_per_page );
}
// Invalid ordering.
return false;
}
/**
* Normalize "*_between" parameters to "*_min" and "*_max" for numeric values
* and "*_after" and "*_before" for date values.
*
* @param array $request Query params from REST API request.
* @param string|array $param_names One or more param names to handle. Should not include "_between" suffix.
* @param bool $is_date Boolean if the param is date is related.
* @return array Normalized query values.
*/
public static function normalize_between_params( $request, $param_names, $is_date ) {
if ( ! is_array( $param_names ) ) {
$param_names = array( $param_names );
}
$normalized = array();
foreach ( $param_names as $param_name ) {
if ( ! is_array( $request[ $param_name . '_between' ] ) ) {
continue;
}
$range = $request[ $param_name . '_between' ];
if ( 2 !== count( $range ) ) {
continue;
}
$min = $is_date ? '_after' : '_min';
$max = $is_date ? '_before' : '_max';
if ( $range[0] < $range[1] ) {
$normalized[ $param_name . $min ] = $range[0];
$normalized[ $param_name . $max ] = $range[1];
} else {
$normalized[ $param_name . $min ] = $range[1];
$normalized[ $param_name . $max ] = $range[0];
}
}
return $normalized;
}
/**
* Validate a "*_between" range argument (an array with 2 numeric items).
*
* @param mixed $value Parameter value.
* @param WP_REST_Request $request REST Request.
* @param string $param Parameter name.
* @return WP_Error|boolean
*/
public static function rest_validate_between_numeric_arg( $value, $request, $param ) {
if ( ! wp_is_numeric_array( $value ) ) {
return new \WP_Error(
'rest_invalid_param',
/* translators: 1: parameter name */
sprintf( __( '%1$s is not a numerically indexed array.', 'woocommerce' ), $param )
);
}
if (
2 !== count( $value ) ||
! is_numeric( $value[0] ) ||
! is_numeric( $value[1] )
) {
return new \WP_Error(
'rest_invalid_param',
/* translators: %s: parameter name */
sprintf( __( '%s must contain 2 numbers.', 'woocommerce' ), $param )
);
}
return true;
}
/**
* Validate a "*_between" range argument (an array with 2 date items).
*
* @param mixed $value Parameter value.
* @param WP_REST_Request $request REST Request.
* @param string $param Parameter name.
* @return WP_Error|boolean
*/
public static function rest_validate_between_date_arg( $value, $request, $param ) {
if ( ! wp_is_numeric_array( $value ) ) {
return new \WP_Error(
'rest_invalid_param',
/* translators: 1: parameter name */
sprintf( __( '%1$s is not a numerically indexed array.', 'woocommerce' ), $param )
);
}
if (
2 !== count( $value ) ||
! rest_parse_date( $value[0] ) ||
! rest_parse_date( $value[1] )
) {
return new \WP_Error(
'rest_invalid_param',
/* translators: %s: parameter name */
sprintf( __( '%s must contain 2 valid dates.', 'woocommerce' ), $param )
);
}
return true;
}
/**
* Get dates from a timeframe string.
*
* @param int $timeframe Timeframe to use. One of: last_week|last_month|last_quarter|last_6_months|last_year.
* @param DateTime|null $current_date DateTime of current date to compare.
* @return array
*/
public static function get_timeframe_dates( $timeframe, $current_date = null ) {
if ( ! $current_date ) {
$current_date = new \DateTime();
}
$current_year = $current_date->format( 'Y' );
$current_month = $current_date->format( 'm' );
if ( 'last_week' === $timeframe ) {
return array(
'start' => $current_date->modify( 'last week monday' )->format( 'Y-m-d 00:00:00' ),
'end' => $current_date->modify( 'this sunday' )->format( 'Y-m-d 23:59:59' ),
);
}
if ( 'last_month' === $timeframe ) {
return array(
'start' => $current_date->modify( 'first day of previous month' )->format( 'Y-m-d 00:00:00' ),
'end' => $current_date->modify( 'last day of this month' )->format( 'Y-m-d 23:59:59' ),
);
}
if ( 'last_quarter' === $timeframe ) {
switch ( $current_month ) {
case $current_month >= 1 && $current_month <= 3:
return array(
'start' => ( $current_year - 1 ) . '-10-01 00:00:00',
'end' => ( $current_year - 1 ) . '-12-31 23:59:59',
);
case $current_month >= 4 && $current_month <= 6:
return array(
'start' => $current_year . '-01-01 00:00:00',
'end' => $current_year . '-03-31 23:59:59',
);
case $current_month >= 7 && $current_month <= 9:
return array(
'start' => $current_year . '-04-01 00:00:00',
'end' => $current_year . '-06-30 23:59:59',
);
case $current_month >= 10 && $current_month <= 12:
return array(
'start' => $current_year . '-07-01 00:00:00',
'end' => $current_year . '-09-31 23:59:59',
);
}
}
if ( 'last_6_months' === $timeframe ) {
if ( $current_month >= 1 && $current_month <= 6 ) {
return array(
'start' => ( $current_year - 1 ) . '-07-01 00:00:00',
'end' => ( $current_year - 1 ) . '-12-31 23:59:59',
);
}
return array(
'start' => $current_year . '-01-01 00:00:00',
'end' => $current_year . '-06-30 23:59:59',
);
}
if ( 'last_year' === $timeframe ) {
return array(
'start' => ( $current_year - 1 ) . '-01-01 00:00:00',
'end' => ( $current_year - 1 ) . '-12-31 23:59:59',
);
}
return false;
}
}
API/Reports/Variations/Controller.php 0000644 00000035153 15153746747 0013620 0 ustar 00 <?php
/**
* REST API Reports products controller
*
* Handles requests to the /reports/products endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Variations;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Controller as ReportsController;
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits;
/**
* REST API Reports products controller class.
*
* @internal
* @extends ReportsController
*/
class Controller extends ReportsController implements ExportableInterface {
/**
* Exportable traits.
*/
use ExportableTraits;
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/variations';
/**
* Mapping between external parameter name and name used in query class.
*
* @var array
*/
protected $param_mapping = array(
'variations' => 'variation_includes',
);
/**
* Get items.
*
* @param WP_REST_Request $request Request data.
*
* @return array|WP_Error
*/
public function get_items( $request ) {
$args = array();
/**
* Experimental: Filter the list of parameters provided when querying data from the data store.
*
* @ignore
*
* @param array $collection_params List of parameters.
*/
$collection_params = apply_filters(
'experimental_woocommerce_analytics_variations_collection_params',
$this->get_collection_params()
);
$registered = array_keys( $collection_params );
foreach ( $registered as $param_name ) {
if ( isset( $request[ $param_name ] ) ) {
if ( isset( $this->param_mapping[ $param_name ] ) ) {
$args[ $this->param_mapping[ $param_name ] ] = $request[ $param_name ];
} else {
$args[ $param_name ] = $request[ $param_name ];
}
}
}
$reports = new Query( $args );
$products_data = $reports->get_data();
$data = array();
foreach ( $products_data->data as $product_data ) {
$item = $this->prepare_item_for_response( $product_data, $request );
$data[] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$data,
(int) $products_data->total,
(int) $products_data->page_no,
(int) $products_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param array $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$data = $report;
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $report ) );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_variations', $response, $report, $request );
}
/**
* Prepare links for the request.
*
* @param array $object Object data.
* @return array Links for the given post.
*/
protected function prepare_links( $object ) {
$links = array(
'product' => array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, 'products', $object['product_id'] ) ),
),
'variation' => array(
'href' => rest_url( sprintf( '/%s/%s/%d/%s/%d', $this->namespace, 'products', $object['product_id'], 'variation', $object['variation_id'] ) ),
),
);
return $links;
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'report_varitations',
'type' => 'object',
'properties' => array(
'product_id' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product ID.', 'woocommerce' ),
),
'variation_id' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product ID.', 'woocommerce' ),
),
'items_sold' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Number of items sold.', 'woocommerce' ),
),
'net_revenue' => array(
'type' => 'number',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Total Net sales of all items sold.', 'woocommerce' ),
),
'orders_count' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Number of orders product appeared in.', 'woocommerce' ),
),
'extended_info' => array(
'name' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product name.', 'woocommerce' ),
),
'price' => array(
'type' => 'number',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product price.', 'woocommerce' ),
),
'image' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product image.', 'woocommerce' ),
),
'permalink' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product link.', 'woocommerce' ),
),
'attributes' => array(
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product attributes.', 'woocommerce' ),
),
'stock_status' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product inventory status.', 'woocommerce' ),
),
'stock_quantity' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product inventory quantity.', 'woocommerce' ),
),
'low_stock_amount' => array(
'type' => 'integer',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product inventory threshold for low stock.', 'woocommerce' ),
),
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['page'] = array(
'description' => __( 'Current page of the collection.', 'woocommerce' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);
$params['per_page'] = array(
'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['after'] = array(
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['before'] = array(
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['match'] = array(
'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'woocommerce' ),
'type' => 'string',
'default' => 'all',
'enum' => array(
'all',
'any',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
'validate_callback' => 'rest_validate_request_arg',
);
$params['orderby'] = array(
'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
'type' => 'string',
'default' => 'date',
'enum' => array(
'date',
'net_revenue',
'orders_count',
'items_sold',
'sku',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['product_includes'] = array(
'description' => __( 'Limit result set to items that have the specified parent product(s).', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
);
$params['product_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified parent product(s).', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'wp_parse_id_list',
);
$params['variations'] = array(
'description' => __( 'Limit result to items with specified variation ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['extended_info'] = array(
'description' => __( 'Add additional piece of info about each variation to the report.', 'woocommerce' ),
'type' => 'boolean',
'default' => false,
'sanitize_callback' => 'wc_string_to_bool',
'validate_callback' => 'rest_validate_request_arg',
);
$params['attribute_is'] = array(
'description' => __( 'Limit result set to variations that include the specified attributes.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'array',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
$params['attribute_is_not'] = array(
'description' => __( 'Limit result set to variations that don\'t include the specified attributes.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'array',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
$params['category_includes'] = array(
'description' => __( 'Limit result set to variations in the specified categories.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['category_excludes'] = array(
'description' => __( 'Limit result set to variations not in the specified categories.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['force_cache_refresh'] = array(
'description' => __( 'Force retrieval of fresh data instead of from the cache.', 'woocommerce' ),
'type' => 'boolean',
'sanitize_callback' => 'wp_validate_boolean',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get stock status column export value.
*
* @param array $status Stock status from report row.
* @return string
*/
protected function get_stock_status( $status ) {
$statuses = wc_get_product_stock_status_options();
return isset( $statuses[ $status ] ) ? $statuses[ $status ] : '';
}
/**
* Get the column names for export.
*
* @return array Key value pair of Column ID => Label.
*/
public function get_export_columns() {
$export_columns = array(
'product_name' => __( 'Product / Variation title', 'woocommerce' ),
'sku' => __( 'SKU', 'woocommerce' ),
'items_sold' => __( 'Items sold', 'woocommerce' ),
'net_revenue' => __( 'N. Revenue', 'woocommerce' ),
'orders_count' => __( 'Orders', 'woocommerce' ),
);
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
$export_columns['stock_status'] = __( 'Status', 'woocommerce' );
$export_columns['stock'] = __( 'Stock', 'woocommerce' );
}
return $export_columns;
}
/**
* Get the column values for export.
*
* @param array $item Single report item/row.
* @return array Key value pair of Column ID => Row Value.
*/
public function prepare_item_for_export( $item ) {
$export_item = array(
'product_name' => $item['extended_info']['name'],
'sku' => $item['extended_info']['sku'],
'items_sold' => $item['items_sold'],
'net_revenue' => self::csv_number_format( $item['net_revenue'] ),
'orders_count' => $item['orders_count'],
);
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
$export_item['stock_status'] = $this->get_stock_status( $item['extended_info']['stock_status'] );
$export_item['stock'] = $item['extended_info']['stock_quantity'];
}
return $export_item;
}
}
API/Reports/Variations/DataStore.php 0000644 00000042711 15153746750 0013353 0 ustar 00 <?php
/**
* API\Reports\Variations\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Variations;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
/**
* API\Reports\Variations\DataStore.
*/
class DataStore extends ReportsDataStore implements DataStoreInterface {
/**
* Table used to get the data.
*
* @var string
*/
protected static $table_name = 'wc_order_product_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'variations';
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'date_start' => 'strval',
'date_end' => 'strval',
'product_id' => 'intval',
'variation_id' => 'intval',
'items_sold' => 'intval',
'net_revenue' => 'floatval',
'orders_count' => 'intval',
'name' => 'strval',
'price' => 'floatval',
'image' => 'strval',
'permalink' => 'strval',
'sku' => 'strval',
);
/**
* Extended product attributes to include in the data.
*
* @var array
*/
protected $extended_attributes = array(
'name',
'price',
'image',
'permalink',
'stock_status',
'stock_quantity',
'low_stock_amount',
'sku',
);
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'variations';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$table_name = self::get_db_table_name();
$this->report_columns = array(
'product_id' => 'product_id',
'variation_id' => 'variation_id',
'items_sold' => 'SUM(product_qty) as items_sold',
'net_revenue' => 'SUM(product_net_revenue) AS net_revenue',
'orders_count' => "COUNT(DISTINCT {$table_name}.order_id) as orders_count",
);
}
/**
* Fills FROM clause of SQL request based on user supplied parameters.
*
* @param array $query_args Parameters supplied by the user.
* @param string $arg_name Target of the JOIN sql param.
*/
protected function add_from_sql_params( $query_args, $arg_name ) {
global $wpdb;
if ( 'sku' !== $query_args['orderby'] ) {
return;
}
$table_name = self::get_db_table_name();
$join = "LEFT JOIN {$wpdb->postmeta} AS postmeta ON {$table_name}.variation_id = postmeta.post_id AND postmeta.meta_key = '_sku'";
if ( 'inner' === $arg_name ) {
$this->subquery->add_sql_clause( 'join', $join );
} else {
$this->add_sql_clause( 'join', $join );
}
}
/**
* Generate a subquery for order_item_id based on the attribute filters.
*
* @param array $query_args Query arguments supplied by the user.
* @return string
*/
protected function get_order_item_by_attribute_subquery( $query_args ) {
$order_product_lookup_table = self::get_db_table_name();
$attribute_subqueries = $this->get_attribute_subqueries( $query_args );
if ( $attribute_subqueries['join'] && $attribute_subqueries['where'] ) {
// Perform a subquery for DISTINCT order items that match our attribute filters.
$attr_subquery = new SqlQuery( $this->context . '_attribute_subquery' );
$attr_subquery->add_sql_clause( 'select', "DISTINCT {$order_product_lookup_table}.order_item_id" );
$attr_subquery->add_sql_clause( 'from', $order_product_lookup_table );
if ( $this->should_exclude_simple_products( $query_args ) ) {
$attr_subquery->add_sql_clause( 'where', "AND {$order_product_lookup_table}.variation_id != 0" );
}
foreach ( $attribute_subqueries['join'] as $attribute_join ) {
$attr_subquery->add_sql_clause( 'join', $attribute_join );
}
$operator = $this->get_match_operator( $query_args );
$attr_subquery->add_sql_clause( 'where', 'AND (' . implode( " {$operator} ", $attribute_subqueries['where'] ) . ')' );
return "AND {$order_product_lookup_table}.order_item_id IN ({$attr_subquery->get_query_statement()})";
}
return false;
}
/**
* Updates the database query with parameters used for Products report: categories and order status.
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function add_sql_query_params( $query_args ) {
global $wpdb;
$order_product_lookup_table = self::get_db_table_name();
$order_stats_lookup_table = $wpdb->prefix . 'wc_order_stats';
$order_item_meta_table = $wpdb->prefix . 'woocommerce_order_itemmeta';
$where_subquery = array();
$this->add_time_period_sql_params( $query_args, $order_product_lookup_table );
$this->get_limit_sql_params( $query_args );
$this->add_order_by_sql_params( $query_args );
$included_variations = $this->get_included_variations( $query_args );
if ( $included_variations > 0 ) {
$this->add_from_sql_params( $query_args, 'outer' );
} else {
$this->add_from_sql_params( $query_args, 'inner' );
}
$included_products = $this->get_included_products( $query_args );
if ( $included_products ) {
$this->subquery->add_sql_clause( 'where', "AND {$order_product_lookup_table}.product_id IN ({$included_products})" );
}
$excluded_products = $this->get_excluded_products( $query_args );
if ( $excluded_products ) {
$this->subquery->add_sql_clause( 'where', "AND {$order_product_lookup_table}.product_id NOT IN ({$excluded_products})" );
}
if ( $included_variations ) {
$this->subquery->add_sql_clause( 'where', "AND {$order_product_lookup_table}.variation_id IN ({$included_variations})" );
} elseif ( ! $included_products ) {
if ( $this->should_exclude_simple_products( $query_args ) ) {
$this->subquery->add_sql_clause( 'where', "AND {$order_product_lookup_table}.variation_id != 0" );
}
}
$order_status_filter = $this->get_status_subquery( $query_args );
if ( $order_status_filter ) {
$this->subquery->add_sql_clause( 'join', "JOIN {$order_stats_lookup_table} ON {$order_product_lookup_table}.order_id = {$order_stats_lookup_table}.order_id" );
$this->subquery->add_sql_clause( 'where', "AND ( {$order_status_filter} )" );
}
$attribute_order_items_subquery = $this->get_order_item_by_attribute_subquery( $query_args );
if ( $attribute_order_items_subquery ) {
// JOIN on product lookup if we haven't already.
if ( ! $order_status_filter ) {
$this->subquery->add_sql_clause( 'join', "JOIN {$order_product_lookup_table} ON {$order_stats_lookup_table}.order_id = {$order_product_lookup_table}.order_id" );
}
// Add subquery for matching attributes to WHERE.
$this->subquery->add_sql_clause( 'where', $attribute_order_items_subquery );
}
if ( 0 < count( $where_subquery ) ) {
$operator = $this->get_match_operator( $query_args );
$this->subquery->add_sql_clause( 'where', 'AND (' . implode( " {$operator} ", $where_subquery ) . ')' );
}
}
/**
* Maps ordering specified by the user to columns in the database/fields in the data.
*
* @param string $order_by Sorting criterion.
*
* @return string
*/
protected function normalize_order_by( $order_by ) {
if ( 'date' === $order_by ) {
return self::get_db_table_name() . '.date_created';
}
if ( 'sku' === $order_by ) {
return 'meta_value';
}
return $order_by;
}
/**
* Enriches the product data with attributes specified by the extended_attributes.
*
* @param array $products_data Product data.
* @param array $query_args Query parameters.
*/
protected function include_extended_info( &$products_data, $query_args ) {
foreach ( $products_data as $key => $product_data ) {
$extended_info = new \ArrayObject();
if ( $query_args['extended_info'] ) {
$extended_attributes = apply_filters( 'woocommerce_rest_reports_variations_extended_attributes', $this->extended_attributes, $product_data );
$parent_product = wc_get_product( $product_data['product_id'] );
$attributes = array();
// Base extended info off the parent variable product if the variation ID is 0.
// This is caused by simple products with prior sales being converted into variable products.
// See: https://github.com/woocommerce/woocommerce-admin/issues/2719.
$variation_id = (int) $product_data['variation_id'];
$variation_product = ( 0 === $variation_id ) ? $parent_product : wc_get_product( $variation_id );
// Fall back to the parent product if the variation can't be found.
$extended_attributes_product = is_a( $variation_product, 'WC_Product' ) ? $variation_product : $parent_product;
// If both product and variation is not found, set deleted to true.
if ( ! $extended_attributes_product ) {
$extended_info['deleted'] = true;
}
foreach ( $extended_attributes as $extended_attribute ) {
$function = 'get_' . $extended_attribute;
if ( is_callable( array( $extended_attributes_product, $function ) ) ) {
$value = $extended_attributes_product->{$function}();
$extended_info[ $extended_attribute ] = $value;
}
}
// If this is a variation, add its attributes.
// NOTE: We don't fall back to the parent product here because it will include all possible attribute options.
if (
0 < $variation_id &&
is_callable( array( $variation_product, 'get_variation_attributes' ) )
) {
$variation_attributes = $variation_product->get_variation_attributes();
foreach ( $variation_attributes as $attribute_name => $attribute ) {
$name = str_replace( 'attribute_', '', $attribute_name );
$option_term = get_term_by( 'slug', $attribute, $name );
$attributes[] = array(
'id' => wc_attribute_taxonomy_id_by_name( $name ),
'name' => str_replace( 'pa_', '', $name ),
'option' => $option_term && ! is_wp_error( $option_term ) ? $option_term->name : $attribute,
);
}
}
$extended_info['attributes'] = $attributes;
// If there is no set low_stock_amount, use the one in user settings.
if ( '' === $extended_info['low_stock_amount'] ) {
$extended_info['low_stock_amount'] = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
}
$extended_info = $this->cast_numbers( $extended_info );
}
$products_data[ $key ]['extended_info'] = $extended_info;
}
}
/**
* Returns if simple products should be excluded from the report.
*
* @internal
*
* @param array $query_args Query parameters.
*
* @return boolean
*/
protected function should_exclude_simple_products( array $query_args ) {
return apply_filters( 'experimental_woocommerce_analytics_variations_should_exclude_simple_products', true, $query_args );
}
/**
* Fill missing extended_info.name for the deleted products.
*
* @param array $products Product data.
*/
protected function fill_deleted_product_name( array &$products ) {
global $wpdb;
$product_variation_ids = [];
// Find products with missing extended_info.name.
foreach ( $products as $key => $product ) {
if ( ! isset( $product['extended_info']['name'] ) ) {
$product_variation_ids[ $key ] = [
'product_id' => $product['product_id'],
'variation_id' => $product['variation_id'],
];
}
}
if ( ! count( $product_variation_ids ) ) {
return;
}
$where_clauses = implode(
' or ',
array_map(
function( $ids ) {
return "(
product_lookup.product_id = {$ids['product_id']}
and
product_lookup.variation_id = {$ids['variation_id']}
)";
},
$product_variation_ids
)
);
$query = "
select
product_lookup.product_id,
product_lookup.variation_id,
order_items.order_item_name
from
{$wpdb->prefix}wc_order_product_lookup as product_lookup
left join {$wpdb->prefix}woocommerce_order_items as order_items
on product_lookup.order_item_id = order_items.order_item_id
where
{$where_clauses}
group by
product_lookup.product_id,
product_lookup.variation_id,
order_items.order_item_name
";
// phpcs:ignore
$results = $wpdb->get_results( $query );
$index = [];
foreach ( $results as $result ) {
$index[ $result->product_id . '_' . $result->variation_id ] = $result->order_item_name;
}
foreach ( $product_variation_ids as $product_key => $ids ) {
$product = $products[ $product_key ];
$index_key = $product['product_id'] . '_' . $product['variation_id'];
if ( isset( $index[ $index_key ] ) ) {
$products[ $product_key ]['extended_info']['name'] = $index[ $index_key ];
}
}
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @param array $query_args Query parameters.
*
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$table_name = self::get_db_table_name();
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
'fields' => '*',
'product_includes' => array(),
'variation_includes' => array(),
'extended_info' => false,
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$data = (object) array(
'data' => array(),
'total' => 0,
'pages' => 0,
'page_no' => 0,
);
$selections = $this->selected_columns( $query_args );
$included_variations =
( isset( $query_args['variation_includes'] ) && is_array( $query_args['variation_includes'] ) )
? $query_args['variation_includes']
: array();
$params = $this->get_limit_params( $query_args );
$this->add_sql_query_params( $query_args );
if ( count( $included_variations ) > 0 ) {
$total_results = count( $included_variations );
$total_pages = (int) ceil( $total_results / $params['per_page'] );
$this->subquery->clear_sql_clause( 'select' );
$this->subquery->add_sql_clause( 'select', $selections );
if ( 'date' === $query_args['orderby'] ) {
$this->subquery->add_sql_clause( 'select', ", {$table_name}.date_created" );
}
$fields = $this->get_fields( $query_args );
$join_selections = $this->format_join_selections( $fields, array( 'variation_id' ) );
$ids_table = $this->get_ids_table( $included_variations, 'variation_id' );
$this->add_sql_clause( 'select', $join_selections );
$this->add_sql_clause( 'from', '(' );
$this->add_sql_clause( 'from', $this->subquery->get_query_statement() );
$this->add_sql_clause( 'from', ") AS {$table_name}" );
$this->add_sql_clause(
'right_join',
"RIGHT JOIN ( {$ids_table} ) AS default_results
ON default_results.variation_id = {$table_name}.variation_id"
);
$variations_query = $this->get_query_statement();
} else {
$this->subquery->clear_sql_clause( 'select' );
$this->subquery->add_sql_clause( 'select', $selections );
/**
* Experimental: Filter the Variations SQL query allowing extensions to add additional SQL clauses.
*
* @since 7.4.0
* @param array $query_args Query parameters.
* @param SqlQuery $subquery Variations query class.
*/
apply_filters( 'experimental_woocommerce_analytics_variations_additional_clauses', $query_args, $this->subquery );
/* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
$db_records_count = (int) $wpdb->get_var(
"SELECT COUNT(*) FROM (
{$this->subquery->get_query_statement()}
) AS tt"
);
/* phpcs:enable */
$total_results = $db_records_count;
$total_pages = (int) ceil( $db_records_count / $params['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return $data;
}
$this->subquery->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$this->subquery->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
$variations_query = $this->subquery->get_query_statement();
}
/* phpcs:disable WordPress.DB.PreparedSQL.NotPrepared */
$product_data = $wpdb->get_results(
$variations_query,
ARRAY_A
);
/* phpcs:enable */
if ( null === $product_data ) {
return $data;
}
$this->include_extended_info( $product_data, $query_args );
if ( $query_args['extended_info'] ) {
$this->fill_deleted_product_name( $product_data );
}
$product_data = array_map( array( $this, 'cast_numbers' ), $product_data );
$data = (object) array(
'data' => $product_data,
'total' => $total_results,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
$this->subquery = new SqlQuery( $this->context . '_subquery' );
$this->subquery->add_sql_clause( 'select', 'product_id' );
$this->subquery->add_sql_clause( 'from', self::get_db_table_name() );
$this->subquery->add_sql_clause( 'group_by', 'product_id, variation_id' );
}
}
API/Reports/Variations/Query.php 0000644 00000002366 15153746750 0012574 0 ustar 00 <?php
/**
* Class for parameter-based Products Report querying
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'page' => 2,
* 'categories' => array(15, 18),
* 'products' => array(1,2,3)
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Variations\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Variations;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Variations\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Products report.
*
* @return array
*/
protected function get_default_query_vars() {
return array();
}
/**
* Get product data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_variations_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-variations' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_variations_select_query', $results, $args );
}
}
API/Reports/Variations/Stats/Controller.php 0000644 00000023765 15153746750 0014716 0 ustar 00 <?php
/**
* REST API Reports variations stats controller
*
* Handles requests to the /reports/variations/stats endpoint.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Variations\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericStatsController;
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
use WP_REST_Request;
use WP_REST_Response;
/**
* REST API Reports variations stats controller class.
*
* @internal
* @extends GenericStatsController
*/
class Controller extends GenericStatsController {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/variations/stats';
/**
* Mapping between external parameter name and name used in query class.
*
* @var array
*/
protected $param_mapping = array(
'variations' => 'variation_includes',
);
/**
* Constructor.
*/
public function __construct() {
add_filter( 'woocommerce_analytics_variations_stats_select_query', array( $this, 'set_default_report_data' ) );
}
/**
* Get all reports.
*
* @param WP_REST_Request $request Request data.
* @return array|WP_Error
*/
public function get_items( $request ) {
$query_args = array(
'fields' => array(
'items_sold',
'net_revenue',
'orders_count',
'variations_count',
),
);
/**
* Experimental: Filter the list of parameters provided when querying data from the data store.
*
* @ignore
*
* @param array $collection_params List of parameters.
*/
$collection_params = apply_filters( 'experimental_woocommerce_analytics_variations_stats_collection_params', $this->get_collection_params() );
$registered = array_keys( $collection_params );
foreach ( $registered as $param_name ) {
if ( isset( $request[ $param_name ] ) ) {
if ( isset( $this->param_mapping[ $param_name ] ) ) {
$query_args[ $this->param_mapping[ $param_name ] ] = $request[ $param_name ];
} else {
$query_args[ $param_name ] = $request[ $param_name ];
}
}
}
$query = new Query( $query_args );
try {
$report_data = $query->get_data();
} catch ( ParameterException $e ) {
return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
$out_data = array(
'totals' => get_object_vars( $report_data->totals ),
'intervals' => array(),
);
foreach ( $report_data->intervals as $interval_data ) {
$item = $this->prepare_item_for_response( $interval_data, $request );
$out_data['intervals'][] = $this->prepare_response_for_collection( $item );
}
return $this->add_pagination_headers(
$request,
$out_data,
(int) $report_data->total,
(int) $report_data->page_no,
(int) $report_data->pages
);
}
/**
* Prepare a report object for serialization.
*
* @param array $report Report data.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $report, $request ) {
$response = parent::prepare_item_for_response( $report, $request );
/**
* Filter a report returned from the API.
*
* Allows modification of the report data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $report The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_variations_stats', $response, $report, $request );
}
/**
* Get the Report's item properties schema.
* Will be used by `get_item_schema` as `totals` and `subtotals`.
*
* @return array
*/
protected function get_item_properties_schema() {
return array(
'items_sold' => array(
'title' => __( 'Variations Sold', 'woocommerce' ),
'description' => __( 'Number of variation items sold.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'indicator' => true,
),
'net_revenue' => array(
'description' => __( 'Net sales.', 'woocommerce' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'format' => 'currency',
),
'orders_count' => array(
'description' => __( 'Number of orders.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
);
}
/**
* Get the Report's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = parent::get_item_schema();
$schema['title'] = 'report_variations_stats';
$segment_label = array(
'description' => __( 'Human readable segment label, either product or variation name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'enum' => array( 'day', 'week', 'month', 'year' ),
);
$schema['properties']['totals']['properties']['segments']['items']['properties']['segment_label'] = $segment_label;
$schema['properties']['intervals']['items']['properties']['subtotals']['properties']['segments']['items']['properties']['segment_label'] = $segment_label;
return $this->add_additional_fields_schema( $schema );
}
/**
* Set the default results to 0 if API returns an empty array
*
* @param Mixed $results Report data.
* @return object
*/
public function set_default_report_data( $results ) {
if ( empty( $results ) ) {
$results = new \stdClass();
$results->total = 0;
$results->totals = new \stdClass();
$results->totals->items_sold = 0;
$results->totals->net_revenue = 0;
$results->totals->orders_count = 0;
$results->intervals = array();
$results->pages = 1;
$results->page_no = 1;
}
return $results;
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['match'] = array(
'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'woocommerce' ),
'type' => 'string',
'default' => 'all',
'enum' => array(
'all',
'any',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['orderby']['enum'] = array(
'date',
'net_revenue',
'coupons',
'refunds',
'shipping',
'taxes',
'net_revenue',
'orders_count',
'items_sold',
);
$params['category_includes'] = array(
'description' => __( 'Limit result to items from the specified categories.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['category_excludes'] = array(
'description' => __( 'Limit result set to variations not in the specified categories.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['product_includes'] = array(
'description' => __( 'Limit result set to items that have the specified parent product(s).', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
);
$params['product_excludes'] = array(
'description' => __( 'Limit result set to items that don\'t have the specified parent product(s).', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'wp_parse_id_list',
);
$params['variations'] = array(
'description' => __( 'Limit result to items with specified variation ids.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),
);
$params['segmentby'] = array(
'description' => __( 'Segment the response by additional constraint.', 'woocommerce' ),
'type' => 'string',
'enum' => array(
'product',
'category',
'variation',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['fields'] = array(
'description' => __( 'Limit stats fields to the specified items.', 'woocommerce' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
),
);
$params['attribute_is'] = array(
'description' => __( 'Limit result set to orders that include products with the specified attributes.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'array',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
$params['attribute_is_not'] = array(
'description' => __( 'Limit result set to orders that don\'t include products with the specified attributes.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'array',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
}
API/Reports/Variations/Stats/DataStore.php 0000644 00000026356 15153746750 0014460 0 ustar 00 <?php
/**
* API\Reports\Products\Stats\DataStore class file.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Variations\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Variations\DataStore as VariationsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
/**
* API\Reports\Variations\Stats\DataStore.
*/
class DataStore extends VariationsDataStore implements DataStoreInterface {
/**
* Mapping columns to data type to return correct response types.
*
* @var array
*/
protected $column_types = array(
'items_sold' => 'intval',
'net_revenue' => 'floatval',
'orders_count' => 'intval',
'variations_count' => 'intval',
);
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'variations_stats';
/**
* Data store context used to pass to filters.
*
* @var string
*/
protected $context = 'variations_stats';
/**
* Assign report columns once full table name has been assigned.
*/
protected function assign_report_columns() {
$table_name = self::get_db_table_name();
$this->report_columns = array(
'items_sold' => 'SUM(product_qty) as items_sold',
'net_revenue' => 'SUM(product_net_revenue) AS net_revenue',
'orders_count' => "COUNT( DISTINCT ( CASE WHEN product_gross_revenue >= 0 THEN {$table_name}.order_id END ) ) as orders_count",
'variations_count' => 'COUNT(DISTINCT variation_id) as variations_count',
);
}
/**
* Updates the database query with parameters used for Products Stats report: categories and order status.
*
* @param array $query_args Query arguments supplied by the user.
*/
protected function update_sql_query_params( $query_args ) {
global $wpdb;
$products_where_clause = '';
$products_from_clause = '';
$where_subquery = array();
$order_product_lookup_table = self::get_db_table_name();
$order_item_meta_table = $wpdb->prefix . 'woocommerce_order_itemmeta';
$included_products = $this->get_included_products( $query_args );
if ( $included_products ) {
$products_where_clause .= " AND {$order_product_lookup_table}.product_id IN ({$included_products})";
}
$excluded_products = $this->get_excluded_products( $query_args );
if ( $excluded_products ) {
$products_where_clause .= "AND {$order_product_lookup_table}.product_id NOT IN ({$excluded_products})";
}
$included_variations = $this->get_included_variations( $query_args );
if ( $included_variations ) {
$products_where_clause .= " AND {$order_product_lookup_table}.variation_id IN ({$included_variations})";
} elseif ( $this->should_exclude_simple_products( $query_args ) ) {
$products_where_clause .= " AND {$order_product_lookup_table}.variation_id != 0";
}
$order_status_filter = $this->get_status_subquery( $query_args );
if ( $order_status_filter ) {
$products_from_clause .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_product_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id";
$products_where_clause .= " AND ( {$order_status_filter} )";
}
$attribute_order_items_subquery = $this->get_order_item_by_attribute_subquery( $query_args );
if ( $attribute_order_items_subquery ) {
// JOIN on product lookup if we haven't already.
if ( ! $order_status_filter ) {
$products_from_clause .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_product_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id";
}
// Add subquery for matching attributes to WHERE.
$products_where_clause .= $attribute_order_items_subquery;
}
if ( 0 < count( $where_subquery ) ) {
$operator = $this->get_match_operator( $query_args );
$products_where_clause .= 'AND (' . implode( " {$operator} ", $where_subquery ) . ')';
}
$this->add_time_period_sql_params( $query_args, $order_product_lookup_table );
$this->total_query->add_sql_clause( 'where', $products_where_clause );
$this->total_query->add_sql_clause( 'join', $products_from_clause );
$this->add_intervals_sql_params( $query_args, $order_product_lookup_table );
$this->interval_query->add_sql_clause( 'where', $products_where_clause );
$this->interval_query->add_sql_clause( 'join', $products_from_clause );
$this->interval_query->add_sql_clause( 'select', $this->get_sql_clause( 'select' ) . ' AS time_interval' );
}
/**
* Returns if simple products should be excluded from the report.
*
* @internal
*
* @param array $query_args Query parameters.
*
* @return boolean
*/
protected function should_exclude_simple_products( array $query_args ) {
return apply_filters( 'experimental_woocommerce_analytics_variations_stats_should_exclude_simple_products', true, $query_args );
}
/**
* Returns the report data based on parameters supplied by the user.
*
* @since 3.5.0
* @param array $query_args Query parameters.
* @return stdClass|WP_Error Data.
*/
public function get_data( $query_args ) {
global $wpdb;
$table_name = self::get_db_table_name();
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => TimeInterval::default_before(),
'after' => TimeInterval::default_after(),
'fields' => '*',
'category_includes' => array(),
'interval' => 'week',
'product_includes' => array(),
'variation_includes' => array(),
);
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$this->initialize_queries();
$selections = $this->selected_columns( $query_args );
$params = $this->get_limit_params( $query_args );
$this->update_sql_query_params( $query_args );
$this->get_limit_sql_params( $query_args );
$this->interval_query->add_sql_clause( 'where_time', $this->get_sql_clause( 'where_time' ) );
/* phpcs:disable WordPress.DB.PreparedSQL.NotPrepared */
$db_intervals = $wpdb->get_col(
$this->interval_query->get_query_statement()
);
/* phpcs:enable */
$db_interval_count = count( $db_intervals );
$expected_interval_count = TimeInterval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $params['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return array();
}
$intervals = array();
$this->update_intervals_sql_params( $query_args, $db_interval_count, $expected_interval_count, $table_name );
$this->total_query->add_sql_clause( 'select', $selections );
$this->total_query->add_sql_clause( 'where_time', $this->get_sql_clause( 'where_time' ) );
/* phpcs:disable WordPress.DB.PreparedSQL.NotPrepared */
$totals = $wpdb->get_results(
$this->total_query->get_query_statement(),
ARRAY_A
);
/* phpcs:enable */
// @todo remove these assignements when refactoring segmenter classes to use query objects.
$totals_query = array(
'from_clause' => $this->total_query->get_sql_clause( 'join' ),
'where_time_clause' => $this->total_query->get_sql_clause( 'where_time' ),
'where_clause' => $this->total_query->get_sql_clause( 'where' ),
);
$intervals_query = array(
'select_clause' => $this->get_sql_clause( 'select' ),
'from_clause' => $this->interval_query->get_sql_clause( 'join' ),
'where_time_clause' => $this->interval_query->get_sql_clause( 'where_time' ),
'where_clause' => $this->interval_query->get_sql_clause( 'where' ),
'order_by' => $this->get_sql_clause( 'order_by' ),
'limit' => $this->get_sql_clause( 'limit' ),
);
$segmenter = new Segmenter( $query_args, $this->report_columns );
$totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name );
if ( null === $totals ) {
return new \WP_Error( 'woocommerce_analytics_variations_stats_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce' ) );
}
$this->interval_query->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
$this->interval_query->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
$this->interval_query->add_sql_clause( 'select', ", MAX({$table_name}.date_created) AS datetime_anchor" );
if ( '' !== $selections ) {
$this->interval_query->add_sql_clause( 'select', ', ' . $selections );
}
/* phpcs:disable WordPress.DB.PreparedSQL.NotPrepared */
$intervals = $wpdb->get_results(
$this->interval_query->get_query_statement(),
ARRAY_A
);
/* phpcs:enable */
if ( null === $intervals ) {
return new \WP_Error( 'woocommerce_analytics_variations_stats_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce' ) );
}
$totals = (object) $this->cast_numbers( $totals[0] );
$data = (object) array(
'totals' => $totals,
'intervals' => $intervals,
'total' => $expected_interval_count,
'pages' => $total_pages,
'page_no' => (int) $query_args['page'],
);
if ( TimeInterval::intervals_missing( $expected_interval_count, $db_interval_count, $params['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
$this->remove_extra_records( $data, $query_args['page'], $params['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] );
} else {
$this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals );
}
$segmenter->add_intervals_segments( $data, $intervals_query, $table_name );
$this->create_interval_subtotals( $data->intervals );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Normalizes order_by clause to match to SQL query.
*
* @param string $order_by Order by option requeste by user.
* @return string
*/
protected function normalize_order_by( $order_by ) {
if ( 'date' === $order_by ) {
return 'time_interval';
}
return $order_by;
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
unset( $this->subquery );
$this->total_query = new SqlQuery( $this->context . '_total' );
$this->total_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query = new SqlQuery( $this->context . '_interval' );
$this->interval_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query->add_sql_clause( 'group_by', 'time_interval' );
}
}
API/Reports/Variations/Stats/Query.php 0000644 00000002446 15153746750 0013671 0 ustar 00 <?php
/**
* Class for parameter-based Variations Stats Report querying
*
* Example usage:
* $args = array(
* 'before' => '2018-07-19 00:00:00',
* 'after' => '2018-07-05 00:00:00',
* 'page' => 2,
* 'categories' => array(15, 18),
* 'product_ids' => array(1,2,3)
* );
* $report = new \Automattic\WooCommerce\Admin\API\Reports\Variations\Stats\Query( $args );
* $mydata = $report->get_data();
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Variations\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/**
* API\Reports\Variations\Stats\Query
*/
class Query extends ReportsQuery {
/**
* Valid fields for Products report.
*
* @return array
*/
protected function get_default_query_vars() {
return array();
}
/**
* Get variations data based on the current query vars.
*
* @return array
*/
public function get_data() {
$args = apply_filters( 'woocommerce_analytics_variations_stats_query_args', $this->get_query_vars() );
$data_store = \WC_Data_Store::load( 'report-variations-stats' );
$results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_analytics_variations_stats_select_query', $results, $args );
}
}
API/Reports/Variations/Stats/Segmenter.php 0000644 00000017667 15153746750 0014530 0 ustar 00 <?php
/**
* Class for adding segmenting support without cluttering the data stores.
*/
namespace Automattic\WooCommerce\Admin\API\Reports\Variations\Stats;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter;
use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
/**
* Date & time interval and numeric range handling class for Reporting API.
*/
class Segmenter extends ReportsSegmenter {
/**
* Returns column => query mapping to be used for product-related product-level segmenting query
* (e.g. products sold, revenue from product X when segmenting by category).
*
* @param string $products_table Name of SQL table containing the product-level segmenting info.
*
* @return array Column => SELECT query mapping.
*/
protected function get_segment_selections_product_level( $products_table ) {
$columns_mapping = array(
'items_sold' => "SUM($products_table.product_qty) as items_sold",
'net_revenue' => "SUM($products_table.product_net_revenue ) AS net_revenue",
'orders_count' => "COUNT( DISTINCT $products_table.order_id ) AS orders_count",
'variations_count' => "COUNT( DISTINCT $products_table.variation_id ) AS variations_count",
);
return $columns_mapping;
}
/**
* Calculate segments for totals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
*
* @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $segmenting_dimension_name Name of the segmenting dimension.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $totals_query Array of SQL clauses for totals query.
* @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
*
* @return array
*/
protected function get_product_related_totals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $totals_query, $unique_orders_table ) {
global $wpdb;
$product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
// Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued).
// Product-level numbers.
/* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
$segments_products = $wpdb->get_results(
"SELECT
$segmenting_groupby AS $segmenting_dimension_name
{$segmenting_selections['product_level']}
FROM
$table_name
$segmenting_from
{$totals_query['from_clause']}
WHERE
1=1
{$totals_query['where_time_clause']}
{$totals_query['where_clause']}
$segmenting_where
GROUP BY
$segmenting_groupby",
ARRAY_A
);
/* phpcs:enable */
$totals_segments = $this->merge_segment_totals_results( $segmenting_dimension_name, $segments_products, array() );
return $totals_segments;
}
/**
* Calculate segments for intervals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
*
* @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
* @param string $segmenting_from FROM part of segmenting SQL query.
* @param string $segmenting_where WHERE part of segmenting SQL query.
* @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
* @param string $segmenting_dimension_name Name of the segmenting dimension.
* @param string $table_name Name of SQL table which is the stats table for orders.
* @param array $intervals_query Array of SQL clauses for intervals query.
* @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
*
* @return array
*/
protected function get_product_related_intervals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $intervals_query, $unique_orders_table ) {
global $wpdb;
$product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
// LIMIT offset, rowcount needs to be updated to a multiple of the number of segments.
preg_match( '/LIMIT (\d+)\s?,\s?(\d+)/', $intervals_query['limit'], $limit_parts );
$segment_count = count( $this->get_all_segments() );
$orig_offset = intval( $limit_parts[1] );
$orig_rowcount = intval( $limit_parts[2] );
$segmenting_limit = $wpdb->prepare( 'LIMIT %d, %d', $orig_offset * $segment_count, $orig_rowcount * $segment_count );
// Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued).
// Product-level numbers.
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$segments_products = $wpdb->get_results(
"SELECT
{$intervals_query['select_clause']} AS time_interval,
$segmenting_groupby AS $segmenting_dimension_name
{$segmenting_selections['product_level']}
FROM
$table_name
$segmenting_from
{$intervals_query['from_clause']}
WHERE
1=1
{$intervals_query['where_time_clause']}
{$intervals_query['where_clause']}
$segmenting_where
GROUP BY
time_interval, $segmenting_groupby
$segmenting_limit",
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
ARRAY_A
);
$intervals_segments = $this->merge_segment_intervals_results( $segmenting_dimension_name, $segments_products, array() );
return $intervals_segments;
}
/**
* Return array of segments formatted for REST response.
*
* @param string $type Type of segments to return--'totals' or 'intervals'.
* @param array $query_params SQL query parameter array.
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
*
* @return array
* @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified.
*/
protected function get_segments( $type, $query_params, $table_name ) {
global $wpdb;
if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) {
return array();
}
$product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
$unique_orders_table = 'uniq_orders';
$segmenting_where = '';
// Product, variation, and category are bound to product, so here product segmenting table is required,
// while coupon and customer are bound to order, so we don't need the extra JOIN for those.
// This also means that segment selections need to be calculated differently.
if ( 'variation' === $this->query_args['segmentby'] ) {
$product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
$segmenting_selections = array(
'product_level' => $this->prepare_selections( $product_level_columns ),
);
$this->report_columns = $product_level_columns;
$segmenting_from = '';
$segmenting_groupby = $product_segmenting_table . '.variation_id';
$segmenting_dimension_name = 'variation_id';
// Restrict our search space for variation comparisons.
if ( isset( $this->query_args['variation_includes'] ) ) {
$variation_ids = implode( ',', $this->get_all_segments() );
$segmenting_where = " AND $product_segmenting_table.variation_id IN ( $variation_ids )";
}
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
}
return $segments;
}
}
API/SettingOptions.php 0000644 00000001556 15153746750 0010703 0 ustar 00 <?php
/**
* REST API Setting Options Controller
*
* Handles requests to /settings/{option}
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
/**
* Setting Options controller.
*
* @internal
* @extends WC_REST_Setting_Options_Controller
*/
class SettingOptions extends \WC_REST_Setting_Options_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Invalidates API cache when updating settings options.
*
* @param WP_REST_Request $request Full details about the request.
* @return array Of WP_Error or WP_REST_Response.
*/
public function batch_items( $request ) {
// Invalidate the API cache.
ReportsCache::invalidate();
// Process the request.
return parent::batch_items( $request );
}
}
API/ShippingPartnerSuggestions.php 0000644 00000013364 15153746750 0013262 0 ustar 00 <?php
/**
* Handles requests for shipping partner suggestions.
*/
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Admin\Features\ShippingPartnerSuggestions\DefaultShippingPartners;
use Automattic\WooCommerce\Admin\Features\ShippingPartnerSuggestions\ShippingPartnerSuggestions as Suggestions;
defined( 'ABSPATH' ) || exit;
/**
* ShippingPartnerSuggestions Controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class ShippingPartnerSuggestions extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'shipping-partner-suggestions';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_suggestions' ),
'permission_callback' => array( $this, 'get_permission_check' ),
'args' => array(
'force_default_suggestions' => array(
'type' => 'boolean',
'description' => __( 'Return the default shipping partner suggestions when woocommerce_show_marketplace_suggestions option is set to no', 'woocommerce' ),
),
),
),
'schema' => array( $this, 'get_suggestions_schema' ),
)
);
}
/**
* Check if a given request has access to manage plugins.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_permission_check( $request ) {
if ( ! current_user_can( 'install_plugins' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage plugins.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check if suggestions should be shown in the settings screen.
*
* @return bool
*/
private function should_display() {
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
return false;
}
/**
* The return value can be controlled via woocommerce_allow_shipping_partner_suggestions filter.
*
* @since 7.4.1
*/
return apply_filters( 'woocommerce_allow_shipping_partner_suggestions', true );
}
/**
* Return suggested shipping partners.
*
* @param WP_REST_Request $request Full details about the request.
* @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response
*/
public function get_suggestions( $request ) {
$should_display = $this->should_display();
$force_default = $request->get_param( 'force_default_suggestions' );
if ( $should_display ) {
return Suggestions::get_suggestions();
} elseif ( false === $should_display && true === $force_default ) {
return rest_ensure_response( Suggestions::get_suggestions( DefaultShippingPartners::get_all() ) );
}
return rest_ensure_response( Suggestions::get_suggestions( DefaultShippingPartners::get_all() ) );
}
/**
* Get the schema, conforming to JSON Schema.
*
* @return array
*/
public static function get_suggestions_schema() {
$feature_def = array(
'type' => 'array',
'items' => array(
'type' => 'object',
'properties' => array(
'icon' => array(
'type' => 'string',
),
'title' => array(
'type' => 'string',
),
'description' => array(
'type' => 'string',
),
),
),
);
$layout_def = array(
'type' => 'object',
'properties' => array(
'image' => array(
'type' => 'string',
'description' => '',
),
'features' => $feature_def,
),
);
$item_schema = array(
'type' => 'object',
'required' => array( 'name', 'is_visible', 'available_layouts' ),
// require layout_row or layout_column. One of them must exist.
'anyOf' => array(
array(
'required' => 'layout_row',
),
array(
'required' => 'layout_column',
),
),
'properties' => array(
'name' => array(
'description' => __( 'Plugin name.', 'woocommerce' ),
'type' => 'string',
'required' => true,
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'slug' => array(
'description' => __( 'Plugin slug used in https://wordpress.org/plugins/{slug}.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'layout_row' => $layout_def,
'layout_column' => $layout_def,
'description' => array(
'description' => __( 'Description', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'learn_more_link' => array(
'description' => __( 'Learn more link .', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'is_visible' => array(
'description' => __( 'Suggestion visibility.', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'available_layouts' => array(
'description' => __( 'Available layouts -- single, dual, or both', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'string',
'enum' => array( 'row', 'column' ),
),
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'shipping-partner-suggestions',
'type' => 'array',
'items' => array( $item_schema ),
);
return $schema;
}
}
API/Taxes.php 0000644 00000011634 15153746750 0006774 0 ustar 00 <?php
/**
* REST API Taxes Controller
*
* Handles requests to /taxes/*
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
/**
* Taxes controller.
*
* @internal
* @extends WC_REST_Taxes_Controller
*/
class Taxes extends \WC_REST_Taxes_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-analytics';
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['search'] = array(
'description' => __( 'Search by similar tax code.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['include'] = array(
'description' => __( 'Limit result set to items that have the specified rate ID(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
/**
* Get all taxes and allow filtering by tax code.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
global $wpdb;
$prepared_args = array();
$prepared_args['order'] = $request['order'];
$prepared_args['number'] = $request['per_page'];
if ( ! empty( $request['offset'] ) ) {
$prepared_args['offset'] = $request['offset'];
} else {
$prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number'];
}
$orderby_possibles = array(
'id' => 'tax_rate_id',
'order' => 'tax_rate_order',
);
$prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ];
$prepared_args['class'] = $request['class'];
$prepared_args['search'] = $request['search'];
$prepared_args['include'] = $request['include'];
/**
* Filter arguments, before passing to $wpdb->get_results(), when querying taxes via the REST API.
*
* @param array $prepared_args Array of arguments for $wpdb->get_results().
* @param WP_REST_Request $request The current request.
*/
$prepared_args = apply_filters( 'woocommerce_rest_tax_query', $prepared_args, $request );
$query = "
SELECT *
FROM {$wpdb->prefix}woocommerce_tax_rates
WHERE 1 = 1
";
// Filter by tax class.
if ( ! empty( $prepared_args['class'] ) ) {
$class = 'standard' !== $prepared_args['class'] ? sanitize_title( $prepared_args['class'] ) : '';
$query .= " AND tax_rate_class = '$class'";
}
// Filter by tax code.
$tax_code_search = $prepared_args['search'];
if ( $tax_code_search ) {
$code_like = '%' . $wpdb->esc_like( $tax_code_search ) . '%';
$query .= $wpdb->prepare( ' AND CONCAT_WS( "-", NULLIF(tax_rate_country, ""), NULLIF(tax_rate_state, ""), NULLIF(tax_rate_name, ""), NULLIF(tax_rate_priority, "") ) LIKE %s', $code_like );
}
// Filter by included tax rate IDs.
$included_taxes = array_map( 'absint', $prepared_args['include'] );
if ( ! empty( $included_taxes ) ) {
$included_taxes = implode( ',', $prepared_args['include'] );
$query .= " AND tax_rate_id IN ({$included_taxes})";
}
// Order tax rates.
$order_by = sprintf( ' ORDER BY %s', sanitize_key( $prepared_args['orderby'] ) );
// Pagination.
$pagination = sprintf( ' LIMIT %d, %d', $prepared_args['offset'], $prepared_args['number'] );
// Query taxes.
$results = $wpdb->get_results( $query . $order_by . $pagination ); // @codingStandardsIgnoreLine.
$taxes = array();
foreach ( $results as $tax ) {
$data = $this->prepare_item_for_response( $tax, $request );
$taxes[] = $this->prepare_response_for_collection( $data );
}
$response = rest_ensure_response( $taxes );
// Store pagination values for headers then unset for count query.
$per_page = (int) $prepared_args['number'];
$page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
// Query only for ids.
$wpdb->get_results( str_replace( 'SELECT *', 'SELECT tax_rate_id', $query ) ); // @codingStandardsIgnoreLine.
// Calculate totals.
$total_taxes = (int) $wpdb->num_rows;
$response->header( 'X-WP-Total', (int) $total_taxes );
$max_pages = ceil( $total_taxes / $per_page );
$response->header( 'X-WP-TotalPages', (int) $max_pages );
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
if ( $page > 1 ) {
$prev_page = $page - 1;
if ( $prev_page > $max_pages ) {
$prev_page = $max_pages;
}
$prev_link = add_query_arg( 'page', $prev_page, $base );
$response->link_header( 'prev', $prev_link );
}
if ( $max_pages > $page ) {
$next_page = $page + 1;
$next_link = add_query_arg( 'page', $next_page, $base );
$response->link_header( 'next', $next_link );
}
return $response;
}
}
API/Templates/digital_product.csv 0000644 00000000067 15153746750 0013025 0 ustar 00 Type,Name,Published
"simple, downloadable, virtual",,-1 API/Templates/external_product.csv 0000644 00000000040 15153746750 0013221 0 ustar 00 Type,Name,Published
external,,-1 API/Templates/grouped_product.csv 0000644 00000000040 15153746750 0013044 0 ustar 00 Type,Name,Published
grouped,,-1
API/Templates/physical_product.csv 0000644 00000000036 15153746750 0013220 0 ustar 00 Type,Name,Published
simple,,-1 API/Templates/variable_product.csv 0000644 00000000040 15153746750 0013164 0 ustar 00 Type,Name,Published
variable,,-1 API/Themes.php 0000644 00000014141 15153746750 0007131 0 ustar 00 <?php
/**
* REST API Themes Controller
*
* Handles requests to /themes
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Overrides\ThemeUpgrader;
use Automattic\WooCommerce\Admin\Overrides\ThemeUpgraderSkin;
/**
* Themes controller.
*
* @internal
* @extends WC_REST_Data_Controller
*/
class Themes extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'themes';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'upload_theme' ),
'permission_callback' => array( $this, 'upload_theme_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check whether a given request has permission to edit upload plugins/themes.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function upload_theme_permissions_check( $request ) {
if ( ! current_user_can( 'upload_themes' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you are not allowed to install themes on this site.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Upload and install a theme.
*
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function upload_theme( $request ) {
if (
! isset( $_FILES['pluginzip'] ) || ! isset( $_FILES['pluginzip']['tmp_name'] ) || ! is_uploaded_file( $_FILES['pluginzip']['tmp_name'] ) || ! is_file( $_FILES['pluginzip']['tmp_name'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
return new \WP_Error( 'woocommerce_rest_invalid_file', __( 'Specified file failed upload test.', 'woocommerce' ) );
}
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . '/wp-admin/includes/admin.php';
include_once ABSPATH . '/wp-admin/includes/theme-install.php';
include_once ABSPATH . '/wp-admin/includes/theme.php';
include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . '/wp-admin/includes/class-theme-upgrader.php';
$_GET['package'] = true;
$file_upload = new \File_Upload_Upgrader( 'pluginzip', 'package' );
$upgrader = new ThemeUpgrader( new ThemeUpgraderSkin() );
$install = $upgrader->install( $file_upload->package );
if ( $install || is_wp_error( $install ) ) {
$file_upload->cleanup();
}
if ( ! is_wp_error( $install ) && isset( $install['destination_name'] ) ) {
$theme = $install['destination_name'];
$result = array(
'status' => 'success',
'message' => $upgrader->strings['process_success'],
'theme' => $theme,
);
/**
* Fires when a theme is successfully installed.
*
* @param string $theme The theme name.
*/
do_action( 'woocommerce_theme_installed', $theme );
} else {
if ( is_wp_error( $install ) && $install->get_error_code() ) {
$error_message = isset( $upgrader->strings[ $install->get_error_code() ] ) ? $upgrader->strings[ $install->get_error_code() ] : $install->get_error_data();
} else {
$error_message = $upgrader->strings['process_failed'];
}
$result = array(
'status' => 'error',
'message' => $error_message,
);
}
$response = $this->prepare_item_for_response( $result, $request );
$data = $this->prepare_response_for_collection( $response );
return rest_ensure_response( $data );
}
/**
* Prepare the data object for response.
*
* @param object $item Data object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $item, $request ) {
$data = $this->add_additional_fields_to_object( $item, $request );
$data = $this->filter_response_by_context( $data, 'view' );
$response = rest_ensure_response( $data );
/**
* Filter the list returned from the API.
*
* @param WP_REST_Response $response The response object.
* @param array $item The original item.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_themes', $response, $item, $request );
}
/**
* Get the schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'upload_theme',
'type' => 'object',
'properties' => array(
'status' => array(
'description' => __( 'Theme installation status.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'message' => array(
'description' => __( 'Theme installation message.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'theme' => array(
'description' => __( 'Uploaded theme.', 'woocommerce' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['pluginzip'] = array(
'description' => __( 'A zip file of the theme to be uploaded.', 'woocommerce' ),
'type' => 'file',
'validate_callback' => 'rest_validate_request_arg',
);
return apply_filters( 'woocommerce_rest_themes_collection_params', $params );
}
}
BlockTemplates/BlockContainerInterface.php 0000644 00000000272 15153746750 0014722 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\BlockTemplates;
/**
* Interface for block containers.
*/
interface BlockContainerInterface extends BlockInterface, ContainerInterface {}
BlockTemplates/BlockInterface.php 0000644 00000003523 15153746750 0013061 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\BlockTemplates;
/**
* Interface for block configuration used to specify blocks in BlockTemplate.
*/
interface BlockInterface {
/**
* Key for the block name in the block configuration.
*/
public const NAME_KEY = 'blockName';
/**
* Key for the block ID in the block configuration.
*/
public const ID_KEY = 'id';
/**
* Key for the internal order in the block configuration.
*/
public const ORDER_KEY = 'order';
/**
* Key for the block attributes in the block configuration.
*/
public const ATTRIBUTES_KEY = 'attributes';
/**
* Get the block name.
*/
public function get_name(): string;
/**
* Get the block ID.
*/
public function get_id(): string;
/**
* Get the block order.
*/
public function get_order(): int;
/**
* Set the block order.
*
* @param int $order The block order.
*/
public function set_order( int $order );
/**
* Get the block attributes.
*/
public function get_attributes(): array;
/**
* Set the block attributes.
*
* @param array $attributes The block attributes.
*/
public function set_attributes( array $attributes );
/**
* Get the parent container that the block belongs to.
*/
public function &get_parent(): ContainerInterface;
/**
* Get the root template that the block belongs to.
*/
public function &get_root_template(): BlockTemplateInterface;
/**
* Remove the block from its parent.
*/
public function remove();
/**
* Check if the block is detached from its parent or root template.
*
* @return bool True if the block is detached from its parent or root template.
*/
public function is_detached(): bool;
/**
* Get the block configuration as a formatted template.
*
* @return array The block configuration as a formatted template.
*/
public function get_formatted_template(): array;
}
BlockTemplates/BlockTemplateInterface.php 0000644 00000001261 15153746750 0014552 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\BlockTemplates;
/**
* Interface for block-based template.
*/
interface BlockTemplateInterface extends ContainerInterface {
/**
* Get the template ID.
*/
public function get_id(): string;
/**
* Get the template title.
*/
public function get_title(): string;
/**
* Get the template description.
*/
public function get_description(): string;
/**
* Get the template area.
*/
public function get_area(): string;
/**
* Generate a block ID based on a base.
*
* @param string $id_base The base to use when generating an ID.
* @return string
*/
public function generate_block_id( string $id_base ): string;
}
BlockTemplates/ContainerInterface.php 0000644 00000001536 15153746750 0013753 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\BlockTemplates;
/**
* Interface for block containers.
*/
interface ContainerInterface {
/**
* Get the root template that the block belongs to.
*/
public function &get_root_template(): BlockTemplateInterface;
/**
* Get the block configuration as a formatted template.
*/
public function get_formatted_template(): array;
/**
* Get a block by ID.
*
* @param string $block_id The block ID.
*/
public function get_block( string $block_id ): ?BlockInterface;
/**
* Removes a block from the container.
*
* @param string $block_id The block ID.
*
* @throws \UnexpectedValueException If the block container is not an ancestor of the block.
*/
public function remove_block( string $block_id );
/**
* Removes all blocks from the container.
*/
public function remove_blocks();
}
Composer/Package.php 0000644 00000004577 15153746750 0010431 0 ustar 00 <?php
/**
* Returns information about the package and handles init.
*/
/**
* This namespace isn't compatible with the PSR-4
* which ensures that the copy in the standalone plugin will not be autoloaded.
*/
namespace Automattic\WooCommerce\Admin\Composer;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Notes;
use Automattic\WooCommerce\Admin\Notes\NotesUnavailableException;
use Automattic\WooCommerce\Internal\Admin\FeaturePlugin;
/**
* Main package class.
*/
class Package {
/**
* Version.
*
* @var string
*/
const VERSION = '3.3.0';
/**
* Package active.
*
* @var bool
*/
private static $package_active = false;
/**
* Active version
*
* @var bool
*/
private static $active_version = null;
/**
* Init the package.
*
* Only initialize for WP 5.3 or greater.
*/
public static function init() {
// Avoid double initialization when the feature plugin is in use.
if (defined( 'WC_ADMIN_VERSION_NUMBER' ) ) {
self::$active_version = WC_ADMIN_VERSION_NUMBER;
return;
}
$feature_plugin_instance = FeaturePlugin::instance();
// Indicate to the feature plugin that the core package exists.
if ( ! defined( 'WC_ADMIN_PACKAGE_EXISTS' ) ) {
define( 'WC_ADMIN_PACKAGE_EXISTS', true );
}
self::$package_active = true;
self::$active_version = self::VERSION;
$feature_plugin_instance->init();
// Unhook the custom Action Scheduler data store class in active older versions of WC Admin.
remove_filter( 'action_scheduler_store_class', array( $feature_plugin_instance, 'replace_actionscheduler_store_class' ) );
}
/**
* Return the version of the package.
*
* @return string
*/
public static function get_version() {
return self::VERSION;
}
/**
* Return the active version of WC Admin.
*
* @return string
*/
public static function get_active_version() {
return self::$active_version;
}
/**
* Return whether the package is active.
*
* @return bool
*/
public static function is_package_active() {
return self::$package_active;
}
/**
* Return the path to the package.
*
* @return string
*/
public static function get_path() {
return dirname( __DIR__ );
}
/**
* Checks if notes have been initialized.
*/
private static function is_notes_initialized() {
try {
Notes::load_data_store();
} catch ( NotesUnavailableException $e ) {
return false;
}
return true;
}
}
DataSourcePoller.php 0000644 00000014001 15153746750 0010476 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin;
/**
* Specs data source poller class.
* This handles polling specs from JSON endpoints, and
* stores the specs in to the database as an option.
*/
abstract class DataSourcePoller {
/**
* Get class instance.
*/
abstract public static function get_instance();
/**
* Name of data sources filter.
*/
const FILTER_NAME = 'data_source_poller_data_sources';
/**
* Name of data source specs filter.
*/
const FILTER_NAME_SPECS = 'data_source_poller_specs';
/**
* Id of DataSourcePoller.
*
* @var string
*/
protected $id = array();
/**
* Default data sources array.
*
* @var array
*/
protected $data_sources = array();
/**
* Default args.
*
* @var array
*/
protected $args = array();
/**
* The logger instance.
*
* @var WC_Logger|null
*/
protected static $logger = null;
/**
* Constructor.
*
* @param string $id id of DataSourcePoller.
* @param array $data_sources urls for data sources.
* @param array $args Options for DataSourcePoller.
*/
public function __construct( $id, $data_sources = array(), $args = array() ) {
$this->data_sources = $data_sources;
$this->id = $id;
$arg_defaults = array(
'spec_key' => 'id',
'transient_name' => 'woocommerce_admin_' . $id . '_specs',
'transient_expiry' => 7 * DAY_IN_SECONDS,
);
$this->args = wp_parse_args( $args, $arg_defaults );
}
/**
* Get the logger instance.
*
* @return WC_Logger
*/
protected static function get_logger() {
if ( is_null( self::$logger ) ) {
self::$logger = wc_get_logger();
}
return self::$logger;
}
/**
* Returns the key identifier of spec, this can easily be overwritten. Defaults to id.
*
* @param mixed $spec a JSON parsed spec coming from the JSON feed.
* @return string|boolean
*/
protected function get_spec_key( $spec ) {
$key = $this->args['spec_key'];
if ( isset( $spec->$key ) ) {
return $spec->$key;
}
return false;
}
/**
* Reads the data sources for specs and persists those specs.
*
* @return array list of specs.
*/
public function get_specs_from_data_sources() {
$locale = get_user_locale();
$specs_group = get_transient( $this->args['transient_name'] ) ?? array();
$specs = isset( $specs_group[ $locale ] ) ? $specs_group[ $locale ] : array();
if ( ! is_array( $specs ) || empty( $specs ) ) {
$this->read_specs_from_data_sources();
$specs_group = get_transient( $this->args['transient_name'] );
$specs = isset( $specs_group[ $locale ] ) ? $specs_group[ $locale ] : array();
}
$specs = apply_filters( self::FILTER_NAME_SPECS, $specs, $this->id );
return $specs !== false ? $specs : array();
}
/**
* Reads the data sources for specs and persists those specs.
*
* @return bool Whether any specs were read.
*/
public function read_specs_from_data_sources() {
$specs = array();
$data_sources = apply_filters( self::FILTER_NAME, $this->data_sources, $this->id );
// Note that this merges the specs from the data sources based on the
// id - last one wins.
foreach ( $data_sources as $url ) {
$specs_from_data_source = self::read_data_source( $url );
$this->merge_specs( $specs_from_data_source, $specs, $url );
}
$specs_group = get_transient( $this->args['transient_name'] ) ?? array();
$locale = get_user_locale();
$specs_group[ $locale ] = $specs;
// Persist the specs as a transient.
set_transient(
$this->args['transient_name'],
$specs_group,
$this->args['transient_expiry']
);
return count( $specs ) !== 0;
}
/**
* Delete the specs transient.
*
* @return bool success of failure of transient deletion.
*/
public function delete_specs_transient() {
return delete_transient( $this->args['transient_name'] );
}
/**
* Read a single data source and return the read specs
*
* @param string $url The URL to read the specs from.
*
* @return array The specs that have been read from the data source.
*/
protected static function read_data_source( $url ) {
$logger_context = array( 'source' => $url );
$logger = self::get_logger();
$response = wp_remote_get(
add_query_arg(
'locale',
get_user_locale(),
$url
),
array(
'user-agent' => 'WooCommerce/' . WC_VERSION . '; ' . home_url( '/' ),
)
);
if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
$logger->error(
'Error getting data feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $response, true ), $logger_context );
return [];
}
$body = $response['body'];
$specs = json_decode( $body );
if ( $specs === null ) {
$logger->error(
'Empty response in data feed',
$logger_context
);
return [];
}
if ( ! is_array( $specs ) ) {
$logger->error(
'Data feed is not an array',
$logger_context
);
return [];
}
return $specs;
}
/**
* Merge the specs.
*
* @param Array $specs_to_merge_in The specs to merge in to $specs.
* @param Array $specs The list of specs being merged into.
* @param string $url The url of the feed being merged in (for error reporting).
*/
protected function merge_specs( $specs_to_merge_in, &$specs, $url ) {
foreach ( $specs_to_merge_in as $spec ) {
if ( ! $this->validate_spec( $spec, $url ) ) {
continue;
}
$id = $this->get_spec_key( $spec );
$specs[ $id ] = $spec;
}
}
/**
* Validate the spec.
*
* @param object $spec The spec to validate.
* @param string $url The url of the feed that provided the spec.
*
* @return bool The result of the validation.
*/
protected function validate_spec( $spec, $url ) {
$logger = self::get_logger();
$logger_context = array( 'source' => $url );
if ( ! $this->get_spec_key( $spec ) ) {
$logger->error(
'Spec is invalid because the id is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
return true;
}
}
DateTimeProvider/CurrentDateTimeProvider.php 0000644 00000000763 15153746750 0015261 0 ustar 00 <?php
/**
* A provider for getting the current DateTime.
*/
namespace Automattic\WooCommerce\Admin\DateTimeProvider;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\DateTimeProvider\DateTimeProviderInterface;
/**
* Current DateTime Provider.
*
* Uses the current DateTime.
*/
class CurrentDateTimeProvider implements DateTimeProviderInterface {
/**
* Returns the current DateTime.
*
* @return DateTime
*/
public function get_now() {
return new \DateTime();
}
}
DateTimeProvider/DateTimeProviderInterface.php 0000644 00000000602 15153746750 0015527 0 ustar 00 <?php
/**
* Interface for a provider for getting the current DateTime,
* designed to be mockable for unit tests.
*/
namespace Automattic\WooCommerce\Admin\DateTimeProvider;
defined( 'ABSPATH' ) || exit;
/**
* DateTime Provider Interface.
*/
interface DateTimeProviderInterface {
/**
* Returns the current DateTime.
*
* @return DateTime
*/
public function get_now();
}
DeprecatedClassFacade.php 0000644 00000005105 15153746750 0011405 0 ustar 00 <?php
/**
* A facade to allow deprecating an entire class. Calling instance or static
* functions on the facade triggers a deprecation notice before calling the
* underlying function.
*
* Use it by extending DeprecatedClassFacade in your facade class, setting the
* static $facade_over_classname string to the name of the class to build
* a facade over, and setting the static $deprecated_in_version to the version
* that the class was deprecated in. Eg.:
*
* class DeprecatedGoose extends DeprecatedClassFacade {
* static $facade_over_classname = 'Goose';
* static $deprecated_in_version = '1.7.0';
* }
*/
namespace Automattic\WooCommerce\Admin;
defined( 'ABSPATH' ) || exit;
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
/**
* A facade to allow deprecating an entire class.
*/
class DeprecatedClassFacade {
/**
* The instance that this facade covers over.
*
* @var object
*/
protected $instance;
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname;
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '';
/**
* Constructor.
*/
public function __construct() {
$this->instance = new static::$facade_over_classname();
}
/**
* Log a deprecation to the error log.
*
* @param string $function The name of the deprecated function being called.
*/
private static function log_deprecation( $function ) {
error_log( // phpcs:ignore
sprintf(
'%1$s is deprecated since version %2$s! Use %3$s instead.',
static::class . '::' . $function,
static::$deprecated_in_version,
static::$facade_over_classname . '::' . $function
)
);
}
/**
* Executes when calling any function on an instance of this class.
*
* @param string $name The name of the function being called.
* @param array $arguments An array of the arguments to the function call.
*/
public function __call( $name, $arguments ) {
self::log_deprecation( $name );
return call_user_func_array(
array(
$this->instance,
$name,
),
$arguments
);
}
/**
* Executes when calling any static function on this class.
*
* @param string $name The name of the function being called.
* @param array $arguments An array of the arguments to the function call.
*/
public static function __callStatic( $name, $arguments ) {
self::log_deprecation( $name );
return call_user_func_array(
array(
static::$facade_over_classname,
$name,
),
$arguments
);
}
}
FeaturePlugin.php 0000644 00000015136 15153746750 0010052 0 ustar 00 <?php
/**
* WooCommerce Admin: Feature plugin main class.
*/
namespace Automattic\WooCommerce\Internal\Admin;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Admin\Notes\Notes;
use Automattic\WooCommerce\Internal\Admin\Notes\OrderMilestones;
use Automattic\WooCommerce\Internal\Admin\Notes\WooSubscriptionsNotes;
use Automattic\WooCommerce\Internal\Admin\Notes\TrackingOptIn;
use Automattic\WooCommerce\Internal\Admin\Notes\WooCommercePayments;
use Automattic\WooCommerce\Internal\Admin\Notes\InstallJPAndWCSPlugins;
use Automattic\WooCommerce\Internal\Admin\Notes\TestCheckout;
use Automattic\WooCommerce\Internal\Admin\Notes\SellingOnlineCourses;
use Automattic\WooCommerce\Internal\Admin\Notes\MerchantEmailNotifications;
use Automattic\WooCommerce\Internal\Admin\Notes\MagentoMigration;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\PluginsHelper;
use Automattic\WooCommerce\Admin\PluginsInstaller;
use Automattic\WooCommerce\Admin\ReportExporter;
use Automattic\WooCommerce\Admin\ReportsSync;
use Automattic\WooCommerce\Internal\Admin\CategoryLookup;
use Automattic\WooCommerce\Internal\Admin\Events;
use Automattic\WooCommerce\Internal\Admin\Onboarding\Onboarding;
/**
* Feature plugin main class.
*
* @internal This file will not be bundled with woo core, only the feature plugin.
* @internal Note this is not called WC_Admin due to a class already existing in core with that name.
*/
class FeaturePlugin {
/**
* The single instance of the class.
*
* @var object
*/
protected static $instance = null;
/**
* Constructor
*
* @return void
*/
protected function __construct() {}
/**
* Get class instance.
*
* @return object Instance.
*/
final public static function instance() {
if ( null === static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init the feature plugin, only if we can detect both Gutenberg and WooCommerce.
*/
public function init() {
// Bail if WC isn't initialized (This can be called from WCAdmin's entrypoint).
if ( ! defined( 'WC_ABSPATH' ) ) {
return;
}
// Load the page controller functions file first to prevent fatal errors when disabling WooCommerce Admin.
$this->define_constants();
require_once WC_ADMIN_ABSPATH . '/includes/react-admin/page-controller-functions.php';
require_once WC_ADMIN_ABSPATH . '/src/Admin/Notes/DeprecatedNotes.php';
require_once WC_ADMIN_ABSPATH . '/includes/react-admin/core-functions.php';
require_once WC_ADMIN_ABSPATH . '/includes/react-admin/feature-config.php';
require_once WC_ADMIN_ABSPATH . '/includes/react-admin/wc-admin-update-functions.php';
require_once WC_ADMIN_ABSPATH . '/includes/react-admin/class-experimental-abtest.php';
if ( did_action( 'plugins_loaded' ) ) {
self::on_plugins_loaded();
} else {
// Make sure we hook into `plugins_loaded` before core's Automattic\WooCommerce\Package::init().
// If core is network activated but we aren't, the packaged version of WooCommerce Admin will
// attempt to use a data store that hasn't been loaded yet - because we've defined our constants here.
// See: https://github.com/woocommerce/woocommerce-admin/issues/3869.
add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), 9 );
}
}
/**
* Setup plugin once all other plugins are loaded.
*
* @return void
*/
public function on_plugins_loaded() {
$this->hooks();
$this->includes();
}
/**
* Define Constants.
*/
protected function define_constants() {
$this->define( 'WC_ADMIN_APP', 'wc-admin-app' );
$this->define( 'WC_ADMIN_ABSPATH', WC_ABSPATH );
$this->define( 'WC_ADMIN_DIST_JS_FOLDER', 'assets/client/admin/' );
$this->define( 'WC_ADMIN_DIST_CSS_FOLDER', 'assets/client/admin/' );
$this->define( 'WC_ADMIN_PLUGIN_FILE', WC_PLUGIN_FILE );
/**
* Define the WC Admin Images Folder URL.
*
* @deprecated 6.7.0
* @var string
*/
if ( ! defined( 'WC_ADMIN_IMAGES_FOLDER_URL' ) ) {
/**
* Define the WC Admin Images Folder URL.
*
* @deprecated 6.7.0
* @var string
*/
define( 'WC_ADMIN_IMAGES_FOLDER_URL', plugins_url( 'assets/images', WC_PLUGIN_FILE ) );
}
/**
* Define the current WC Admin version.
*
* @deprecated 6.4.0
* @var string
*/
if ( ! defined( 'WC_ADMIN_VERSION_NUMBER' ) ) {
/**
* Define the current WC Admin version.
*
* @deprecated 6.4.0
* @var string
*/
define( 'WC_ADMIN_VERSION_NUMBER', '3.3.0' );
}
}
/**
* Include WC Admin classes.
*/
public function includes() {
// Initialize Database updates, option migrations, and Notes.
Events::instance()->init();
Notes::init();
// Initialize Plugins Installer.
PluginsInstaller::init();
PluginsHelper::init();
// Initialize API.
API\Init::instance();
if ( Features::is_enabled( 'onboarding' ) ) {
Onboarding::init();
}
if ( Features::is_enabled( 'analytics' ) ) {
// Initialize Reports syncing.
ReportsSync::init();
CategoryLookup::instance()->init();
// Initialize Reports exporter.
ReportExporter::init();
}
// Admin note providers.
// @todo These should be bundled in the features/ folder, but loading them from there currently has a load order issue.
new WooSubscriptionsNotes();
new OrderMilestones();
new TrackingOptIn();
new WooCommercePayments();
new InstallJPAndWCSPlugins();
new TestCheckout();
new SellingOnlineCourses();
new MagentoMigration();
// Initialize MerchantEmailNotifications.
MerchantEmailNotifications::init();
}
/**
* Set up our admin hooks and plugin loader.
*/
protected function hooks() {
add_filter( 'woocommerce_admin_features', array( $this, 'replace_supported_features' ), 0 );
Loader::get_instance();
WCAdminAssets::get_instance();
}
/**
* Overwrites the allowed features array using a local `feature-config.php` file.
*
* @param array $features Array of feature slugs.
*/
public function replace_supported_features( $features ) {
/**
* Get additional feature config
*
* @since 6.5.0
*/
$feature_config = apply_filters( 'woocommerce_admin_get_feature_config', wc_admin_get_feature_config() );
$features = array_keys( array_filter( $feature_config ) );
return $features;
}
/**
* Define constant if not already set.
*
* @param string $name Constant name.
* @param string|bool $value Constant value.
*/
protected function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* Prevent cloning.
*/
private function __clone() {}
/**
* Prevent unserializing.
*/
public function __wakeup() {
die();
}
}
Features/AsyncProductEditorCategoryField/Init.php 0000644 00000004641 15153746750 0016207 0 ustar 00 <?php
/**
* WooCommerce Async Product Editor Category Field.
*/
namespace Automattic\WooCommerce\Admin\Features\AsyncProductEditorCategoryField;
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
use Automattic\WooCommerce\Admin\PageController;
/**
* Loads assets related to the async category field for the product editor.
*/
class Init {
const FEATURE_ID = 'async-product-editor-category-field';
/**
* Constructor
*/
public function __construct() {
if ( Features::is_enabled( self::FEATURE_ID ) ) {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_filter( 'woocommerce_taxonomy_args_product_cat', array( $this, 'add_metabox_args' ) );
}
}
/**
* Adds meta_box_cb callback arguments for custom metabox.
*
* @param array $args Category taxonomy args.
* @return array $args category taxonomy args.
*/
public function add_metabox_args( $args ) {
if ( ! isset( $args['meta_box_cb'] ) ) {
$args['meta_box_cb'] = 'WC_Meta_Box_Product_Categories::output';
$args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_checkboxes';
}
return $args;
}
/**
* Enqueue scripts needed for the product form block editor.
*/
public function enqueue_scripts() {
if ( ! PageController::is_embed_page() ) {
return;
}
WCAdminAssets::register_script( 'wp-admin-scripts', 'product-category-metabox', true );
wp_localize_script(
'wc-admin-product-category-metabox',
'wc_product_category_metabox_params',
array(
'search_categories_nonce' => wp_create_nonce( 'search-categories' ),
'search_taxonomy_terms_nonce' => wp_create_nonce( 'search-taxonomy-terms' ),
)
);
wp_enqueue_script( 'product-category-metabox' );
}
/**
* Enqueue styles needed for the rich text editor.
*/
public function enqueue_styles() {
if ( ! PageController::is_embed_page() ) {
return;
}
$version = Constants::get_constant( 'WC_VERSION' );
wp_register_style(
'woocommerce_admin_product_category_metabox_styles',
WCAdminAssets::get_url( 'product-category-metabox/style', 'css' ),
array(),
$version
);
wp_style_add_data( 'woocommerce_admin_product_category_metabox_styles', 'rtl', 'replace' );
wp_enqueue_style( 'woocommerce_admin_product_category_metabox_styles' );
}
}
Features/Features.php 0000644 00000027274 15153746750 0010642 0 ustar 00 <?php
/**
* Features loader for features developed in WooCommerce Admin.
*/
namespace Automattic\WooCommerce\Admin\Features;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Internal\Admin\Loader;
use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
/**
* Features Class.
*/
class Features {
/**
* Class instance.
*
* @var Loader instance
*/
protected static $instance = null;
/**
* Optional features
*
* @var array
*/
protected static $optional_features = array(
'navigation' => array( 'default' => 'no' ),
'settings' => array( 'default' => 'no' ),
'analytics' => array( 'default' => 'yes' ),
'remote-inbox-notifications' => array( 'default' => 'yes' ),
);
/**
* Beta features
*
* @var array
*/
protected static $beta_features = array(
'navigation',
'new-product-management-experience',
'settings',
);
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*/
public function __construct() {
$this->register_internal_class_aliases();
// Load feature before WooCommerce update hooks.
add_action( 'init', array( __CLASS__, 'load_features' ), 4 );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'maybe_load_beta_features_modal' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_scripts' ), 15 );
add_filter( 'admin_body_class', array( __CLASS__, 'add_admin_body_classes' ) );
add_filter( 'update_option_woocommerce_allow_tracking', array( __CLASS__, 'maybe_disable_features' ), 10, 2 );
}
/**
* Gets a build configured array of enabled WooCommerce Admin features/sections, but does not respect optionally disabled features.
*
* @return array Enabled Woocommerce Admin features/sections.
*/
public static function get_features() {
return apply_filters( 'woocommerce_admin_features', array() );
}
/**
* Gets the optional feature options as an associative array that can be toggled on or off.
*
* @return array
*/
public static function get_optional_feature_options() {
$features = [];
foreach ( array_keys( self::$optional_features ) as $optional_feature_key ) {
$feature_class = self::get_feature_class( $optional_feature_key );
if ( $feature_class ) {
$features[ $optional_feature_key ] = $feature_class::TOGGLE_OPTION_NAME;
}
}
return $features;
}
/**
* Returns if a specific wc-admin feature exists in the current environment.
*
* @param string $feature Feature slug.
* @return bool Returns true if the feature exists.
*/
public static function exists( $feature ) {
$features = self::get_features();
return in_array( $feature, $features, true );
}
/**
* Get the feature class as a string.
*
* @param string $feature Feature name.
* @return string|null
*/
public static function get_feature_class( $feature ) {
$feature = str_replace( '-', '', ucwords( strtolower( $feature ), '-' ) );
$feature_class = 'Automattic\\WooCommerce\\Admin\\Features\\' . $feature;
if ( class_exists( $feature_class ) ) {
return $feature_class;
}
// Handle features contained in subdirectory.
if ( class_exists( $feature_class . '\\Init' ) ) {
return $feature_class . '\\Init';
}
return null;
}
/**
* Class loader for enabled WooCommerce Admin features/sections.
*/
public static function load_features() {
$features = self::get_features();
foreach ( $features as $feature ) {
$feature_class = self::get_feature_class( $feature );
if ( $feature_class ) {
new $feature_class();
}
}
}
/**
* Gets a build configured array of enabled WooCommerce Admin respecting optionally disabled features.
*
* @return array Enabled Woocommerce Admin features/sections.
*/
public static function get_available_features() {
$features = self::get_features();
$optional_feature_keys = array_keys( self::$optional_features );
$optional_features_unavailable = [];
/**
* Filter allowing WooCommerce Admin optional features to be disabled.
*
* @param bool $disabled False.
*/
if ( apply_filters( 'woocommerce_admin_disabled', false ) ) {
return array_values( array_diff( $features, $optional_feature_keys ) );
}
foreach ( $optional_feature_keys as $optional_feature_key ) {
$feature_class = self::get_feature_class( $optional_feature_key );
if ( $feature_class ) {
$default = isset( self::$optional_features[ $optional_feature_key ]['default'] ) ?
self::$optional_features[ $optional_feature_key ]['default'] :
'no';
// Check if the feature is currently being enabled, if it is continue.
/* phpcs:disable WordPress.Security.NonceVerification */
$feature_option = $feature_class::TOGGLE_OPTION_NAME;
if ( isset( $_POST[ $feature_option ] ) && '1' === $_POST[ $feature_option ] ) {
continue;
}
if ( 'yes' !== get_option( $feature_class::TOGGLE_OPTION_NAME, $default ) ) {
$optional_features_unavailable[] = $optional_feature_key;
}
}
}
return array_values( array_diff( $features, $optional_features_unavailable ) );
}
/**
* Check if a feature is enabled.
*
* @param string $feature Feature slug.
* @return bool
*/
public static function is_enabled( $feature ) {
$available_features = self::get_available_features();
return in_array( $feature, $available_features, true );
}
/**
* Enable a toggleable optional feature.
*
* @param string $feature Feature name.
* @return bool
*/
public static function enable( $feature ) {
$features = self::get_optional_feature_options();
if ( isset( $features[ $feature ] ) ) {
update_option( $features[ $feature ], 'yes' );
return true;
}
return false;
}
/**
* Disable a toggleable optional feature.
*
* @param string $feature Feature name.
* @return bool
*/
public static function disable( $feature ) {
$features = self::get_optional_feature_options();
if ( isset( $features[ $feature ] ) ) {
update_option( $features[ $feature ], 'no' );
return true;
}
return false;
}
/**
* Disable features when opting out of tracking.
*
* @param string $old_value Old value.
* @param string $value New value.
*/
public static function maybe_disable_features( $old_value, $value ) {
if ( 'yes' === $value ) {
return;
}
foreach ( self::$beta_features as $feature ) {
self::disable( $feature );
}
}
/**
* Adds the Features section to the advanced tab of WooCommerce Settings
*
* @deprecated 7.0 The WooCommerce Admin features are now handled by the WooCommerce features engine (see the FeaturesController class).
*
* @param array $sections Sections.
* @return array
*/
public static function add_features_section( $sections ) {
return $sections;
}
/**
* Adds the Features settings.
*
* @deprecated 7.0 The WooCommerce Admin features are now handled by the WooCommerce features engine (see the FeaturesController class).
*
* @param array $settings Settings.
* @param string $current_section Current section slug.
* @return array
*/
public static function add_features_settings( $settings, $current_section ) {
return $settings;
}
/**
* Conditionally loads the beta features tracking modal.
*
* @param string $hook Page hook.
*/
public static function maybe_load_beta_features_modal( $hook ) {
if (
'woocommerce_page_wc-settings' !== $hook ||
! isset( $_GET['tab'] ) || 'advanced' !== $_GET['tab'] || // phpcs:ignore CSRF ok.
! isset( $_GET['section'] ) || 'features' !== $_GET['section'] // phpcs:ignore CSRF ok.
) {
return;
}
$tracking_enabled = get_option( 'woocommerce_allow_tracking', 'no' );
if ( empty( self::$beta_features ) ) {
return;
}
if ( 'yes' === $tracking_enabled ) {
return;
}
$rtl = is_rtl() ? '.rtl' : '';
wp_enqueue_style(
'wc-admin-beta-features-tracking-modal',
WCAdminAssets::get_url( "beta-features-tracking-modal/style{$rtl}", 'css' ),
array( 'wp-components' ),
WCAdminAssets::get_file_version( 'css' )
);
wp_enqueue_script(
'wc-admin-beta-features-tracking-modal',
WCAdminAssets::get_url( 'wp-admin-scripts/beta-features-tracking-modal', 'js' ),
array( 'wp-i18n', 'wp-element', WC_ADMIN_APP ),
WCAdminAssets::get_file_version( 'js' ),
true
);
}
/**
* Loads the required scripts on the correct pages.
*/
public static function load_scripts() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
$features = self::get_features();
$enabled_features = array();
foreach ( $features as $key ) {
$enabled_features[ $key ] = self::is_enabled( $key );
}
wp_add_inline_script( WC_ADMIN_APP, 'window.wcAdminFeatures = ' . wp_json_encode( $enabled_features ), 'before' );
}
/**
* Adds body classes to the main wp-admin wrapper, allowing us to better target elements in specific scenarios.
*
* @param string $admin_body_class Body class to add.
*/
public static function add_admin_body_classes( $admin_body_class = '' ) {
if ( ! PageController::is_admin_or_embed_page() ) {
return $admin_body_class;
}
$classes = explode( ' ', trim( $admin_body_class ) );
$features = self::get_features();
foreach ( $features as $feature_key ) {
$classes[] = sanitize_html_class( 'woocommerce-feature-enabled-' . $feature_key );
}
$admin_body_class = implode( ' ', array_unique( $classes ) );
return " $admin_body_class ";
}
/**
* Alias internal features classes to make them backward compatible.
* We've moved our feature classes to src-internal as part of merging this
* repository with WooCommerce Core to form a monorepo.
* See https://wp.me/p90Yrv-2HY for details.
*/
private function register_internal_class_aliases() {
$aliases = array(
// new class => original class (this will be aliased).
'Automattic\WooCommerce\Internal\Admin\WCPayPromotion\Init' => 'Automattic\WooCommerce\Admin\Features\WcPayPromotion\Init',
'Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\Init' => 'Automattic\WooCommerce\Admin\Features\RemoteFreeExtensions\Init',
'Automattic\WooCommerce\Internal\Admin\ActivityPanels' => 'Automattic\WooCommerce\Admin\Features\ActivityPanels',
'Automattic\WooCommerce\Internal\Admin\Analytics' => 'Automattic\WooCommerce\Admin\Features\Analytics',
'Automattic\WooCommerce\Internal\Admin\Coupons' => 'Automattic\WooCommerce\Admin\Features\Coupons',
'Automattic\WooCommerce\Internal\Admin\CouponsMovedTrait' => 'Automattic\WooCommerce\Admin\Features\CouponsMovedTrait',
'Automattic\WooCommerce\Internal\Admin\CustomerEffortScoreTracks' => 'Automattic\WooCommerce\Admin\Features\CustomerEffortScoreTracks',
'Automattic\WooCommerce\Internal\Admin\Homescreen' => 'Automattic\WooCommerce\Admin\Features\Homescreen',
'Automattic\WooCommerce\Internal\Admin\Marketing' => 'Automattic\WooCommerce\Admin\Features\Marketing',
'Automattic\WooCommerce\Internal\Admin\MobileAppBanner' => 'Automattic\WooCommerce\Admin\Features\MobileAppBanner',
'Automattic\WooCommerce\Internal\Admin\RemoteInboxNotifications' => 'Automattic\WooCommerce\Admin\Features\RemoteInboxNotifications',
'Automattic\WooCommerce\Internal\Admin\SettingsNavigationFeature' => 'Automattic\WooCommerce\Admin\Features\Settings',
'Automattic\WooCommerce\Internal\Admin\ShippingLabelBanner' => 'Automattic\WooCommerce\Admin\Features\ShippingLabelBanner',
'Automattic\WooCommerce\Internal\Admin\ShippingLabelBannerDisplayRules' => 'Automattic\WooCommerce\Admin\Features\ShippingLabelBannerDisplayRules',
'Automattic\WooCommerce\Internal\Admin\WcPayWelcomePage' => 'Automattic\WooCommerce\Admin\Features\WcPayWelcomePage',
);
foreach ( $aliases as $new_class => $orig_class ) {
class_alias( $new_class, $orig_class );
}
}
}
Features/Navigation/CoreMenu.php 0000644 00000030141 15153746750 0012663 0 ustar 00 <?php
/**
* WooCommerce Navigation Core Menu
*
* @package Woocommerce Admin
*/
namespace Automattic\WooCommerce\Admin\Features\Navigation;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\Navigation\Menu;
use Automattic\WooCommerce\Admin\Features\Navigation\Screen;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
/**
* CoreMenu class. Handles registering Core menu items.
*/
class CoreMenu {
/**
* Class instance.
*
* @var Menu instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
final public static function instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init.
*/
public function init() {
add_action( 'admin_menu', array( $this, 'register_post_types' ) );
// Add this after we've finished migrating menu items to avoid hiding these items.
add_action( 'admin_menu', array( $this, 'add_dashboard_menu_items' ), PHP_INT_MAX );
}
/**
* Add registered admin settings as menu items.
*/
public static function get_setting_items() {
// Let the Settings feature add pages to the navigation if enabled.
if ( Features::is_enabled( 'settings' ) ) {
return array();
}
// Calling this method adds pages to the below tabs filter on non-settings pages.
\WC_Admin_Settings::get_settings_pages();
$tabs = apply_filters( 'woocommerce_settings_tabs_array', array() );
$menu_items = array();
$order = 0;
foreach ( $tabs as $key => $setting ) {
$order += 10;
$menu_items[] = (
array(
'parent' => 'woocommerce-settings',
'title' => $setting,
'capability' => 'manage_woocommerce',
'id' => 'settings-' . $key,
'url' => 'admin.php?page=wc-settings&tab=' . $key,
'order' => $order,
)
);
}
return $menu_items;
}
/**
* Get unfulfilled order count
*
* @return array
*/
public static function get_shop_order_count() {
$status_counts = array_map( 'wc_orders_count', array( 'processing', 'on-hold' ) );
return array_sum( $status_counts );
}
/**
* Get all menu categories.
*
* @return array
*/
public static function get_categories() {
$analytics_enabled = Features::is_enabled( 'analytics' );
return array(
array(
'title' => __( 'Orders', 'woocommerce' ),
'id' => 'woocommerce-orders',
'badge' => self::get_shop_order_count(),
'order' => 10,
),
array(
'title' => __( 'Products', 'woocommerce' ),
'id' => 'woocommerce-products',
'order' => 20,
),
$analytics_enabled ?
array(
'title' => __( 'Analytics', 'woocommerce' ),
'id' => 'woocommerce-analytics',
'order' => 30,
) : null,
$analytics_enabled ?
array(
'title' => __( 'Reports', 'woocommerce' ),
'id' => 'woocommerce-reports',
'parent' => 'woocommerce-analytics',
'order' => 200,
) : null,
array(
'title' => __( 'Marketing', 'woocommerce' ),
'id' => 'woocommerce-marketing',
'order' => 40,
),
array(
'title' => __( 'Settings', 'woocommerce' ),
'id' => 'woocommerce-settings',
'menuId' => 'secondary',
'order' => 20,
'url' => 'admin.php?page=wc-settings',
),
array(
'title' => __( 'Tools', 'woocommerce' ),
'id' => 'woocommerce-tools',
'menuId' => 'secondary',
'order' => 30,
),
);
}
/**
* Get all menu items.
*
* @return array
*/
public static function get_items() {
$order_items = self::get_order_menu_items();
$product_items = Menu::get_post_type_items( 'product', array( 'parent' => 'woocommerce-products' ) );
$product_tag_items = Menu::get_taxonomy_items(
'product_tag',
array(
'parent' => 'woocommerce-products',
'order' => 30,
)
);
$product_cat_items = Menu::get_taxonomy_items(
'product_cat',
array(
'parent' => 'woocommerce-products',
'order' => 20,
)
);
$coupon_items = Menu::get_post_type_items( 'shop_coupon', array( 'parent' => 'woocommerce-marketing' ) );
$setting_items = self::get_setting_items();
$wca_items = array();
$wca_pages = \Automattic\WooCommerce\Admin\PageController::get_instance()->get_pages();
foreach ( $wca_pages as $page ) {
if ( ! isset( $page['nav_args'] ) ) {
continue;
}
$path = isset( $page['path'] ) ? $page['path'] : null;
$item = array_merge(
array(
'id' => $page['id'],
'url' => $path,
'title' => $page['title'][0],
'capability' => isset( $page['capability'] ) ? $page['capability'] : 'manage_woocommerce',
),
$page['nav_args']
);
// Don't allow top-level items to be added to the primary menu.
if ( ! isset( $item['parent'] ) || 'woocommerce' === $item['parent'] ) {
$item['menuId'] = 'plugins';
}
$wca_items[] = $item;
}
$home_item = array();
$setup_tasks_remaining = TaskLists::setup_tasks_remaining();
if ( defined( '\Automattic\WooCommerce\Internal\Admin\Homescreen::MENU_SLUG' ) ) {
$home_item = array(
'id' => 'woocommerce-home',
'title' => __( 'Home', 'woocommerce' ),
'url' => \Automattic\WooCommerce\Internal\Admin\Homescreen::MENU_SLUG,
'order' => 0,
'matchExpression' => 'page=wc-admin((?!path=).)*$',
'badge' => $setup_tasks_remaining ? $setup_tasks_remaining : null,
);
}
$customers_item = array();
if ( Features::is_enabled( 'analytics' ) ) {
$customers_item = array(
'id' => 'woocommerce-analytics-customers',
'title' => __( 'Customers', 'woocommerce' ),
'url' => 'wc-admin&path=/customers',
'order' => 50,
);
}
$add_product_mvp = array();
if ( Features::is_enabled( 'new-product-management-experience' ) ) {
$add_product_mvp = array(
'id' => 'woocommerce-add-product-mbp',
'title' => __( 'Add New (MVP)', 'woocommerce' ),
'url' => 'admin.php?page=wc-admin&path=/add-product',
'parent' => 'woocommerce-products',
'order' => 50,
);
}
return array_merge(
array(
$home_item,
$customers_item,
$order_items['all'],
$order_items['new'],
$product_items['all'],
$product_cat_items['default'],
$product_tag_items['default'],
array(
'id' => 'woocommerce-product-attributes',
'title' => __( 'Attributes', 'woocommerce' ),
'url' => 'edit.php?post_type=product&page=product_attributes',
'capability' => 'manage_product_terms',
'order' => 40,
'parent' => 'woocommerce-products',
'matchExpression' => 'edit.php(?=.*[?|&]page=product_attributes(&|$|#))|edit-tags.php(?=.*[?|&]taxonomy=pa_)(?=.*[?|&]post_type=product(&|$|#))',
),
array_merge( $product_items['new'], array( 'order' => 50 ) ),
$coupon_items['default'],
// Marketplace category.
array(
'title' => __( 'Marketplace', 'woocommerce' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-marketplace',
'url' => 'wc-addons',
'menuId' => 'secondary',
'order' => 10,
),
$add_product_mvp,
),
// Tools category.
self::get_tool_items(),
// WooCommerce Admin items.
$wca_items,
// Settings category.
$setting_items,
// Legacy report items.
self::get_legacy_report_items()
);
}
/**
* Supplies menu items for orders.
*
* This varies depending on whether we are actively using traditional post type-based orders or the new custom
* table-based orders.
*
* @return ?array
*/
private static function get_order_menu_items(): ?array {
if ( ! wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ) {
return Menu::get_post_type_items( 'shop_order', array( 'parent' => 'woocommerce-orders' ) );
}
$main_orders_menu = array(
'title' => __( 'Orders', 'woocommerce' ),
'capability' => 'edit_others_shop_orders',
'id' => 'woocommerce-orders-default',
'url' => 'admin.php?page=wc-orders',
'parent' => 'woocommerce-orders',
);
$all_orders_entry = $main_orders_menu;
$all_orders_entry['id'] = 'woocommerce-orders-all-items';
$all_orders_entry['order'] = 10;
$new_orders_entry = $main_orders_menu;
$new_orders_entry['title'] = __( 'Add order', 'woocommerce' );
$new_orders_entry['id'] = 'woocommerce-orders-add-item';
$new_orders_entry['url'] = 'admin.php?page=TBD';
$new_orders_entry['order'] = 20;
return array(
'default' => $main_orders_menu,
'all' => $all_orders_entry,
'new' => $new_orders_entry,
);
}
/**
* Get items for tools category.
*
* @return array
*/
public static function get_tool_items() {
$tabs = array(
'status' => __( 'System status', 'woocommerce' ),
'tools' => __( 'Utilities', 'woocommerce' ),
'logs' => __( 'Logs', 'woocommerce' ),
);
$tabs = apply_filters( 'woocommerce_admin_status_tabs', $tabs );
$order = 1;
$items = array(
array(
'parent' => 'woocommerce-tools',
'title' => __( 'Import / Export', 'woocommerce' ),
'capability' => 'import',
'id' => 'tools-import-export',
'url' => 'import.php',
'migrate' => false,
'order' => 0,
),
);
foreach ( $tabs as $key => $tab ) {
$items[] = array(
'parent' => 'woocommerce-tools',
'title' => $tab,
'capability' => 'manage_woocommerce',
'id' => 'tools-' . $key,
'url' => 'wc-status&tab=' . $key,
'order' => $order,
);
$order++;
}
return $items;
}
/**
* Get legacy report items.
*
* @return array
*/
public static function get_legacy_report_items() {
$reports = \WC_Admin_Reports::get_reports();
$menu_items = array();
$order = 0;
foreach ( $reports as $key => $report ) {
$menu_items[] = array(
'parent' => 'woocommerce-reports',
'title' => $report['title'],
'capability' => 'view_woocommerce_reports',
'id' => $key,
'url' => 'wc-reports&tab=' . $key,
'order' => $order,
);
$order++;
}
return $menu_items;
}
/**
* Register all core post types.
*/
public function register_post_types() {
Screen::register_post_type( 'shop_order' );
Screen::register_post_type( 'product' );
Screen::register_post_type( 'shop_coupon' );
}
/**
* Add the dashboard items to the WP menu to create a quick-access flyout menu.
*/
public function add_dashboard_menu_items() {
global $submenu, $menu;
$mapped_items = Menu::get_mapped_menu_items();
$top_level = $mapped_items['woocommerce'];
// phpcs:disable
if ( ! isset( $submenu['woocommerce'] ) || empty( $top_level ) ) {
return;
}
$menuIds = array(
'primary',
'secondary',
'favorites',
);
foreach ( $menuIds as $menuId ) {
foreach( $top_level[ $menuId ] as $item ) {
// Skip specific categories.
if (
in_array(
$item['id'],
array(
'woocommerce-tools',
),
true
)
) {
continue;
}
// Use the link from the first item if it's a category.
if ( ! isset( $item['url'] ) ) {
$categoryMenuId = $menuId === 'favorites' ? 'plugins' : $menuId;
$category_items = $mapped_items[ $item['id'] ][ $categoryMenuId ];
if ( ! empty( $category_items ) ) {
$first_item = $category_items[0];
$submenu['woocommerce'][] = array(
$item['title'],
$first_item['capability'],
isset( $first_item['url'] ) ? $first_item['url'] : null,
$item['title'],
);
}
continue;
}
// Show top-level items.
$submenu['woocommerce'][] = array(
$item['title'],
$item['capability'],
isset( $item['url'] ) ? $item['url'] : null,
$item['title'],
);
}
}
// phpcs:enable
}
/**
* Get items excluded from WooCommerce menu migration.
*
* @return array
*/
public static function get_excluded_items() {
$excluded_items = array(
'woocommerce',
'wc-reports',
'wc-settings',
'wc-status',
);
return apply_filters( 'woocommerce_navigation_core_excluded_items', $excluded_items );
}
}
Features/Navigation/Favorites.php 0000644 00000005171 15153746750 0013115 0 ustar 00 <?php
/**
* WooCommerce Navigation Favorite
*
* @package Woocommerce Navigation
*/
namespace Automattic\WooCommerce\Admin\Features\Navigation;
use Automattic\WooCommerce\Internal\Admin\WCAdminUser;
/**
* Contains logic for the WooCommerce Navigation menu.
*/
class Favorites {
/**
* Array index of menu capability.
*
* @var int
*/
const META_NAME = 'navigation_favorites';
/**
* Favorites instance.
*
* @var Favorites|null
*/
protected static $instance = null;
/**
* Get class instance.
*/
final public static function instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Set given favorites string to the user meta data.
*
* @param string|number $user_id User id.
* @param array $favorites Array of favorite values to set.
*/
private static function set_meta_value( $user_id, $favorites ) {
WCAdminUser::update_user_data_field( $user_id, self::META_NAME, wp_json_encode( (array) $favorites ) );
}
/**
* Add item to favorites
*
* @param string $item_id Identifier of item to add.
* @param string|number $user_id Identifier of user to add to.
* @return WP_Error|Boolean Throws exception if item already exists.
*/
public static function add_item( $item_id, $user_id ) {
$all_favorites = self::get_all( $user_id );
if ( in_array( $item_id, $all_favorites, true ) ) {
return new \WP_Error(
'woocommerce_favorites_already_exists',
__( 'Favorite already exists', 'woocommerce' )
);
}
$all_favorites[] = $item_id;
self::set_meta_value( $user_id, $all_favorites );
return true;
}
/**
* Remove item from favorites
*
* @param string $item_id Identifier of item to remove.
* @param string|number $user_id Identifier of user to remove from.
* @return \WP_Error|Boolean Throws exception if item does not exist.
*/
public static function remove_item( $item_id, $user_id ) {
$all_favorites = self::get_all( $user_id );
if ( ! in_array( $item_id, $all_favorites, true ) ) {
return new \WP_Error(
'woocommerce_favorites_does_not_exist',
__( 'Favorite item not found', 'woocommerce' )
);
}
$remaining = array_values( array_diff( $all_favorites, [ $item_id ] ) );
self::set_meta_value( $user_id, $remaining );
return true;
}
/**
* Get all registered favorites.
*
* @param string|number $user_id Identifier of user to query.
* @return WP_Error|Array
*/
public static function get_all( $user_id ) {
$response = WCAdminUser::get_user_data_field( $user_id, self::META_NAME );
return $response ? json_decode( $response, true ) : array();
}
}
Features/Navigation/Init.php 0000644 00000007714 15153746750 0012063 0 ustar 00 <?php
/**
* Navigation Experience
*
* @package Woocommerce Admin
*/
namespace Automattic\WooCommerce\Admin\Features\Navigation;
use Automattic\WooCommerce\Internal\Admin\Survey;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\Navigation\Screen;
use Automattic\WooCommerce\Admin\Features\Navigation\Menu;
use Automattic\WooCommerce\Admin\Features\Navigation\CoreMenu;
use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
/**
* Contains logic for the Navigation
*/
class Init {
/**
* Option name used to toggle this feature.
*/
const TOGGLE_OPTION_NAME = 'woocommerce_navigation_enabled';
/**
* Determines if the feature has been toggled on or off.
*
* @var boolean
*/
protected static $is_updated = false;
/**
* Hook into WooCommerce.
*/
public function __construct() {
add_action( 'update_option_' . self::TOGGLE_OPTION_NAME, array( $this, 'reload_page_on_toggle' ), 10, 2 );
add_action( 'woocommerce_settings_saved', array( $this, 'maybe_reload_page' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_opt_out_scripts' ) );
if ( Features::is_enabled( 'navigation' ) ) {
Menu::instance()->init();
CoreMenu::instance()->init();
Screen::instance()->init();
}
}
/**
* Add the feature toggle to the features settings.
*
* @deprecated 7.0 The WooCommerce Admin features are now handled by the WooCommerce features engine (see the FeaturesController class).
*
* @param array $features Feature sections.
* @return array
*/
public static function add_feature_toggle( $features ) {
return $features;
}
/**
* Determine if sufficient versions are present to support Navigation feature
*/
public function is_nav_compatible() {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
$gutenberg_minimum_version = '9.0.0'; // https://github.com/WordPress/gutenberg/releases/tag/v9.0.0.
$wp_minimum_version = '5.6';
$has_gutenberg = is_plugin_active( 'gutenberg/gutenberg.php' );
$gutenberg_version = $has_gutenberg ? get_plugin_data( WP_PLUGIN_DIR . '/gutenberg/gutenberg.php' )['Version'] : false;
if ( $gutenberg_version && version_compare( $gutenberg_version, $gutenberg_minimum_version, '>=' ) ) {
return true;
}
// Get unmodified $wp_version.
include ABSPATH . WPINC . '/version.php';
// Strip '-src' from the version string. Messes up version_compare().
$wp_version = str_replace( '-src', '', $wp_version );
if ( version_compare( $wp_version, $wp_minimum_version, '>=' ) ) {
return true;
}
return false;
}
/**
* Reloads the page when the option is toggled to make sure all nav features are loaded.
*
* @param string $old_value Old value.
* @param string $value New value.
*/
public static function reload_page_on_toggle( $old_value, $value ) {
if ( $old_value === $value ) {
return;
}
if ( 'yes' !== $value ) {
update_option( 'woocommerce_navigation_show_opt_out', 'yes' );
}
self::$is_updated = true;
}
/**
* Reload the page if the setting has been updated.
*/
public static function maybe_reload_page() {
if ( ! isset( $_SERVER['REQUEST_URI'] ) || ! self::$is_updated ) {
return;
}
wp_safe_redirect( wp_unslash( $_SERVER['REQUEST_URI'] ) );
exit();
}
/**
* Enqueue the opt out scripts.
*/
public function maybe_enqueue_opt_out_scripts() {
if ( get_option( 'woocommerce_navigation_show_opt_out', 'no' ) !== 'yes' ) {
return;
}
$rtl = is_rtl() ? '.rtl' : '';
wp_enqueue_style(
'wc-admin-navigation-opt-out',
WCAdminAssets::get_url( "navigation-opt-out/style{$rtl}", 'css' ),
array( 'wp-components' ),
WCAdminAssets::get_file_version( 'css' )
);
WCAdminAssets::register_script( 'wp-admin-scripts', 'navigation-opt-out', true );
wp_localize_script(
'wc-admin-navigation-opt-out',
'surveyData',
array(
'url' => Survey::get_url( '/new-navigation-opt-out' ),
)
);
delete_option( 'woocommerce_navigation_show_opt_out' );
}
}
Features/Navigation/Menu.php 0000644 00000054435 15153746750 0012066 0 ustar 00 <?php
/**
* WooCommerce Navigation Menu
*
* @package Woocommerce Navigation
*/
namespace Automattic\WooCommerce\Admin\Features\Navigation;
use Automattic\WooCommerce\Admin\Features\Navigation\Favorites;
use Automattic\WooCommerce\Admin\Features\Navigation\Screen;
use Automattic\WooCommerce\Admin\Features\Navigation\CoreMenu;
/**
* Contains logic for the WooCommerce Navigation menu.
*/
class Menu {
/**
* Class instance.
*
* @var Menu instance
*/
protected static $instance = null;
/**
* Array index of menu capability.
*
* @var int
*/
const CAPABILITY = 1;
/**
* Array index of menu callback.
*
* @var int
*/
const CALLBACK = 2;
/**
* Array index of menu callback.
*
* @var int
*/
const SLUG = 3;
/**
* Array index of menu CSS class string.
*
* @var int
*/
const CSS_CLASSES = 4;
/**
* Array of usable menu IDs.
*/
const MENU_IDS = array(
'primary',
'favorites',
'plugins',
'secondary',
);
/**
* Store menu items.
*
* @var array
*/
protected static $menu_items = array();
/**
* Store categories with menu item IDs.
*
* @var array
*/
protected static $categories = array(
'woocommerce' => array(),
);
/**
* Registered callbacks or URLs with migration boolean as key value pairs.
*
* @var array
*/
protected static $callbacks = array();
/**
* Get class instance.
*/
final public static function instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init.
*/
public function init() {
add_action( 'admin_menu', array( $this, 'add_core_items' ), 100 );
add_filter( 'admin_enqueue_scripts', array( $this, 'enqueue_data' ), 20 );
add_filter( 'admin_menu', array( $this, 'migrate_core_child_items' ), PHP_INT_MAX - 1 );
add_filter( 'admin_menu', array( $this, 'migrate_menu_items' ), PHP_INT_MAX - 2 );
}
/**
* Convert a WordPress menu callback to a URL.
*
* @param string $callback Menu callback.
* @return string
*/
public static function get_callback_url( $callback ) {
// Return the full URL.
if ( strpos( $callback, 'http' ) === 0 ) {
return $callback;
}
$pos = strpos( $callback, '?' );
$file = $pos > 0 ? substr( $callback, 0, $pos ) : $callback;
if ( file_exists( ABSPATH . "/wp-admin/$file" ) ) {
return $callback;
}
return 'admin.php?page=' . $callback;
}
/**
* Get the parent key if one exists.
*
* @param string $callback Callback or URL.
* @return string|null
*/
public static function get_parent_key( $callback ) {
global $submenu;
if ( ! $submenu ) {
return null;
}
// This is already a parent item.
if ( isset( $submenu[ $callback ] ) ) {
return null;
}
foreach ( $submenu as $key => $menu ) {
foreach ( $menu as $item ) {
if ( $item[ self::CALLBACK ] === $callback ) {
return $key;
}
}
}
return null;
}
/**
* Adds a top level menu item to the navigation.
*
* @param array $args Array containing the necessary arguments.
* $args = array(
* 'id' => (string) The unique ID of the menu item. Required.
* 'title' => (string) Title of the menu item. Required.
* 'url' => (string) URL or callback to be used. Required.
* 'order' => (int) Menu item order.
* 'migrate' => (bool) Whether or not to hide the item in the wp admin menu.
* 'menuId' => (string) The ID of the menu to add the category to.
* ).
*/
private static function add_category( $args ) {
if ( ! isset( $args['id'] ) || isset( self::$menu_items[ $args['id'] ] ) ) {
return;
}
$defaults = array(
'id' => '',
'title' => '',
'order' => 100,
'migrate' => true,
'menuId' => 'primary',
'isCategory' => true,
);
$menu_item = wp_parse_args( $args, $defaults );
$menu_item['title'] = wp_strip_all_tags( wp_specialchars_decode( $menu_item['title'] ) );
unset( $menu_item['url'] );
unset( $menu_item['capability'] );
if ( ! isset( $menu_item['parent'] ) ) {
$menu_item['parent'] = 'woocommerce';
$menu_item['backButtonLabel'] = __(
'WooCommerce Home',
'woocommerce'
);
}
self::$menu_items[ $menu_item['id'] ] = $menu_item;
self::$categories[ $menu_item['id'] ] = array();
self::$categories[ $menu_item['parent'] ][] = $menu_item['id'];
if ( isset( $args['url'] ) ) {
self::$callbacks[ $args['url'] ] = $menu_item['migrate'];
}
}
/**
* Adds a child menu item to the navigation.
*
* @param array $args Array containing the necessary arguments.
* $args = array(
* 'id' => (string) The unique ID of the menu item. Required.
* 'title' => (string) Title of the menu item. Required.
* 'parent' => (string) Parent menu item ID.
* 'capability' => (string) Capability to view this menu item.
* 'url' => (string) URL or callback to be used. Required.
* 'order' => (int) Menu item order.
* 'migrate' => (bool) Whether or not to hide the item in the wp admin menu.
* 'menuId' => (string) The ID of the menu to add the item to.
* 'matchExpression' => (string) A regular expression used to identify if the menu item is active.
* ).
*/
private static function add_item( $args ) {
if ( ! isset( $args['id'] ) ) {
return;
}
if ( isset( self::$menu_items[ $args['id'] ] ) ) {
error_log( // phpcs:ignore
sprintf(
/* translators: 1: Duplicate menu item path. */
esc_html__( 'You have attempted to register a duplicate item with WooCommerce Navigation: %1$s', 'woocommerce' ),
'`' . $args['id'] . '`'
)
);
return;
}
$defaults = array(
'id' => '',
'title' => '',
'capability' => 'manage_woocommerce',
'url' => '',
'order' => 100,
'migrate' => true,
'menuId' => 'primary',
);
$menu_item = wp_parse_args( $args, $defaults );
$menu_item['title'] = wp_strip_all_tags( wp_specialchars_decode( $menu_item['title'] ) );
$menu_item['url'] = self::get_callback_url( $menu_item['url'] );
if ( ! isset( $menu_item['parent'] ) ) {
$menu_item['parent'] = 'woocommerce';
}
$menu_item['menuId'] = self::get_item_menu_id( $menu_item );
self::$menu_items[ $menu_item['id'] ] = $menu_item;
self::$categories[ $menu_item['parent'] ][] = $menu_item['id'];
if ( isset( $args['url'] ) ) {
self::$callbacks[ $args['url'] ] = $menu_item['migrate'];
}
}
/**
* Get an item's menu ID from its parent.
*
* @param array $item Item args.
* @return string
*/
public static function get_item_menu_id( $item ) {
$favorites = Favorites::get_all( get_current_user_id() );
if ( is_array( $favorites ) && ! empty( $favorites ) && in_array( $item['id'], $favorites, true ) ) {
return 'favorites';
}
if ( isset( $item['parent'] ) && isset( self::$menu_items[ $item['parent'] ] ) ) {
$menu_id = self::$menu_items[ $item['parent'] ]['menuId'];
return 'favorites' === $menu_id
? 'plugins'
: $menu_id;
}
return $item['menuId'];
}
/**
* Adds a plugin category.
*
* @param array $args Array containing the necessary arguments.
* $args = array(
* 'id' => (string) The unique ID of the menu item. Required.
* 'title' => (string) Title of the menu item. Required.
* 'url' => (string) URL or callback to be used. Required.
* 'migrate' => (bool) Whether or not to hide the item in the wp admin menu.
* 'order' => (int) Menu item order.
* ).
*/
public static function add_plugin_category( $args ) {
$category_args = array_merge(
$args,
array(
'menuId' => 'plugins',
)
);
if ( ! isset( $category_args['parent'] ) ) {
unset( $category_args['order'] );
}
$menu_id = self::get_item_menu_id( $category_args );
if ( ! in_array( $menu_id, array( 'plugins', 'favorites' ), true ) ) {
return;
}
$category_args['menuId'] = $menu_id;
self::add_category( $category_args );
}
/**
* Adds a plugin item.
*
* @param array $args Array containing the necessary arguments.
* $args = array(
* 'id' => (string) The unique ID of the menu item. Required.
* 'title' => (string) Title of the menu item. Required.
* 'parent' => (string) Parent menu item ID.
* 'capability' => (string) Capability to view this menu item.
* 'url' => (string) URL or callback to be used. Required.
* 'migrate' => (bool) Whether or not to hide the item in the wp admin menu.
* 'order' => (int) Menu item order.
* 'matchExpression' => (string) A regular expression used to identify if the menu item is active.
* ).
*/
public static function add_plugin_item( $args ) {
if ( ! isset( $args['parent'] ) ) {
unset( $args['order'] );
}
$item_args = array_merge(
$args,
array(
'menuId' => 'plugins',
)
);
$menu_id = self::get_item_menu_id( $item_args );
if ( 'plugins' !== $menu_id ) {
return;
}
self::add_item( $item_args );
}
/**
* Adds a plugin setting item.
*
* @param array $args Array containing the necessary arguments.
* $args = array(
* 'id' => (string) The unique ID of the menu item. Required.
* 'title' => (string) Title of the menu item. Required.
* 'capability' => (string) Capability to view this menu item.
* 'url' => (string) URL or callback to be used. Required.
* 'migrate' => (bool) Whether or not to hide the item in the wp admin menu.
* ).
*/
public static function add_setting_item( $args ) {
unset( $args['order'] );
if ( isset( $args['parent'] ) || isset( $args['menuId'] ) ) {
error_log( // phpcs:ignore
sprintf(
/* translators: 1: Duplicate menu item path. */
esc_html__( 'The item ID %1$s attempted to register using an invalid option. The arguments `menuId` and `parent` are not allowed for add_setting_item()', 'woocommerce' ),
'`' . $args['id'] . '`'
)
);
}
$item_args = array_merge(
$args,
array(
'menuId' => 'secondary',
'parent' => 'woocommerce-settings',
)
);
self::add_item( $item_args );
}
/**
* Get menu item templates for a given post type.
*
* @param string $post_type Post type to add.
* @param array $menu_args Arguments merged with the returned menu items.
* @return array
*/
public static function get_post_type_items( $post_type, $menu_args = array() ) {
$post_type_object = get_post_type_object( $post_type );
if ( ! $post_type_object || ! $post_type_object->show_in_menu ) {
return;
}
$parent = isset( $menu_args['parent'] ) ? $menu_args['parent'] . '-' : '';
$match_expression = isset( $_GET['post'] ) && get_post_type( intval( $_GET['post'] ) ) === $post_type // phpcs:ignore WordPress.Security.NonceVerification
? '(edit.php|post.php)'
: null;
return array(
'default' => array_merge(
array(
'title' => esc_attr( $post_type_object->labels->menu_name ),
'capability' => $post_type_object->cap->edit_posts,
'id' => $parent . $post_type,
'url' => "edit.php?post_type={$post_type}",
'matchExpression' => $match_expression,
),
$menu_args
),
'all' => array_merge(
array(
'title' => esc_attr( $post_type_object->labels->all_items ),
'capability' => $post_type_object->cap->edit_posts,
'id' => "{$parent}{$post_type}-all-items",
'url' => "edit.php?post_type={$post_type}",
'order' => 10,
'matchExpression' => $match_expression,
),
$menu_args
),
'new' => array_merge(
array(
'title' => esc_attr( $post_type_object->labels->add_new ),
'capability' => $post_type_object->cap->create_posts,
'id' => "{$parent}{$post_type}-add-new",
'url' => "post-new.php?post_type={$post_type}",
'order' => 20,
),
$menu_args
),
);
}
/**
* Get menu item templates for a given taxonomy.
*
* @param string $taxonomy Taxonomy to add.
* @param array $menu_args Arguments merged with the returned menu items.
* @return array
*/
public static function get_taxonomy_items( $taxonomy, $menu_args = array() ) {
$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! $taxonomy_object || ! $taxonomy_object->show_in_menu ) {
return;
}
$parent = isset( $menu_args['parent'] ) ? $menu_args['parent'] . '-' : '';
$product_type_query = ! empty( $taxonomy_object->object_type )
? "&post_type={$taxonomy_object->object_type[0]}"
: '';
$match_expression = 'term.php'; // Match term.php pages.
$match_expression .= "(?=.*[?|&]taxonomy={$taxonomy}(&|$|#))"; // Lookahead to match a taxonomy URL param.
$match_expression .= '|'; // Or.
$match_expression .= 'edit-tags.php'; // Match edit-tags.php pages.
$match_expression .= "(?=.*[?|&]taxonomy={$taxonomy}(&|$|#))"; // Lookahead to match a taxonomy URL param.
return array(
'default' => array_merge(
array(
'title' => esc_attr( $taxonomy_object->labels->menu_name ),
'capability' => $taxonomy_object->cap->edit_terms,
'id' => $parent . $taxonomy,
'url' => "edit-tags.php?taxonomy={$taxonomy}{$product_type_query}",
'matchExpression' => $match_expression,
),
$menu_args
),
'all' => array_merge(
array(
'title' => esc_attr( $taxonomy_object->labels->all_items ),
'capability' => $taxonomy_object->cap->edit_terms,
'id' => "{$parent}{$taxonomy}-all-items",
'url' => "edit-tags.php?taxonomy={$taxonomy}{$product_type_query}",
'matchExpression' => $match_expression,
'order' => 10,
),
$menu_args
),
);
}
/**
* Add core menu items.
*/
public function add_core_items() {
$categories = CoreMenu::get_categories();
foreach ( $categories as $category ) {
self::add_category( $category );
}
$items = CoreMenu::get_items();
foreach ( $items as $item ) {
if ( isset( $item['is_category'] ) && $item['is_category'] ) {
self::add_category( $item );
} else {
self::add_item( $item );
}
}
}
/**
* Add an item or taxonomy.
*
* @param array $menu_item Menu item.
*/
public function add_item_and_taxonomy( $menu_item ) {
if ( in_array( $menu_item[2], CoreMenu::get_excluded_items(), true ) ) {
return;
}
$menu_item[2] = htmlspecialchars_decode( $menu_item[2] );
// Don't add already added items.
$callbacks = self::get_callbacks();
if ( array_key_exists( $menu_item[2], $callbacks ) ) {
return;
}
// Don't add these Product submenus because they are added elsewhere.
if ( in_array( $menu_item[2], array( 'product_importer', 'product_exporter', 'product_attributes' ), true ) ) {
return;
}
self::add_plugin_item(
array(
'title' => $menu_item[0],
'capability' => $menu_item[1],
'id' => sanitize_title( $menu_item[0] ),
'url' => $menu_item[2],
)
);
// Determine if migrated items are a taxonomy or post_type. If they are, register them.
$parsed_url = wp_parse_url( $menu_item[2] );
$query_string = isset( $parsed_url['query'] ) ? $parsed_url['query'] : false;
if ( $query_string ) {
$query = array();
parse_str( $query_string, $query );
if ( isset( $query['taxonomy'] ) ) {
Screen::register_taxonomy( $query['taxonomy'] );
} elseif ( isset( $query['post_type'] ) ) {
Screen::register_post_type( $query['post_type'] );
}
}
}
/**
* Migrate any remaining WooCommerce child items.
*
* @param array $menu Menu items.
* @return array
*/
public function migrate_core_child_items( $menu ) {
global $submenu;
if ( ! isset( $submenu['woocommerce'] ) && ! isset( $submenu['edit.php?post_type=product'] ) ) {
return $menu;
}
$main_items = isset( $submenu['woocommerce'] ) ? $submenu['woocommerce'] : array();
$product_items = isset( $submenu['edit.php?post_type=product'] ) ? $submenu['edit.php?post_type=product'] : array();
foreach ( $main_items as $key => $menu_item ) {
self::add_item_and_taxonomy( $menu_item );
// phpcs:disable
if ( ! isset( $menu_item[ self::CSS_CLASSES ] ) ) {
$submenu['woocommerce'][ $key ][] .= ' hide-if-js';
} else if ( strpos( $submenu['woocommerce'][ $key ][ self::CSS_CLASSES ], 'hide-if-js' ) !== false ) {
continue;
} else {
$submenu['woocommerce'][ $key ][ self::CSS_CLASSES ] .= ' hide-if-js';
}
// phpcs:enable
}
foreach ( $product_items as $key => $menu_item ) {
self::add_item_and_taxonomy( $menu_item );
}
return $menu;
}
/**
* Check if a menu item's callback is registered in the menu.
*
* @param array $menu_item Menu item args.
* @return bool
*/
public static function has_callback( $menu_item ) {
if ( ! $menu_item || ! isset( $menu_item[ self::CALLBACK ] ) ) {
return false;
}
$callback = $menu_item[ self::CALLBACK ];
if (
isset( self::$callbacks[ $callback ] ) &&
self::$callbacks[ $callback ]
) {
return true;
}
if (
isset( self::$callbacks[ self::get_callback_url( $callback ) ] ) &&
self::$callbacks[ self::get_callback_url( $callback ) ]
) {
return true;
}
return false;
}
/**
* Hides all WP admin menus items and adds screen IDs to check for new items.
*/
public static function migrate_menu_items() {
global $menu, $submenu;
foreach ( $menu as $key => $menu_item ) {
if ( self::has_callback( $menu_item ) ) {
// Disable phpcs since we need to override submenu classes.
// Note that `phpcs:ignore WordPress.Variables.GlobalVariables.OverrideProhibited` does not work to disable this check.
// phpcs:disable
$menu[ $key ][ self::CSS_CLASSES ] .= ' hide-if-js';
// phps:enable
continue;
}
// WordPress core menus make the parent item the same URL as the first child.
$has_children = isset( $submenu[ $menu_item[ self::CALLBACK ] ] ) && isset( $submenu[ $menu_item[ self::CALLBACK ] ][0] );
$first_child = $has_children ? $submenu[ $menu_item[ self::CALLBACK ] ][0] : null;
if ( 'woocommerce' !== $menu_item[2] && self::has_callback( $first_child ) ) {
// Disable phpcs since we need to override submenu classes.
// Note that `phpcs:ignore WordPress.Variables.GlobalVariables.OverrideProhibited` does not work to disable this check.
// phpcs:disable
$menu[ $key ][ self::CSS_CLASSES ] .= ' hide-if-js';
// phps:enable
}
}
// Remove excluded submenu items
if ( isset( $submenu['woocommerce'] ) ) {
foreach ( $submenu['woocommerce'] as $key => $submenu_item ) {
if ( in_array( $submenu_item[ self::CALLBACK ], CoreMenu::get_excluded_items(), true ) ) {
if ( isset( $submenu['woocommerce'][ $key ][ self::CSS_CLASSES ] ) ) {
$submenu['woocommerce'][ $key ][ self::CSS_CLASSES ] .= ' hide-if-js';
} else {
$submenu['woocommerce'][ $key ][] = 'hide-if-js';
}
}
}
}
foreach ( $submenu as $parent_key => $parent ) {
foreach ( $parent as $key => $menu_item ) {
if ( self::has_callback( $menu_item ) ) {
// Disable phpcs since we need to override submenu classes.
// Note that `phpcs:ignore WordPress.Variables.GlobalVariables.OverrideProhibited` does not work to disable this check.
// phpcs:disable
if ( ! isset( $menu_item[ self::SLUG ] ) ) {
$submenu[ $parent_key ][ $key ][] = '';
}
if ( ! isset( $menu_item[ self::CSS_CLASSES ] ) ) {
$submenu[ $parent_key ][ $key ][] .= ' hide-if-js';
} else {
$submenu[ $parent_key ][ $key ][ self::CSS_CLASSES ] .= ' hide-if-js';
}
// phps:enable
}
}
}
foreach ( array_keys( self::$callbacks ) as $callback ) {
Screen::add_screen( $callback );
}
}
/**
* Add a callback to identify and hide pages in the WP menu.
*/
public static function hide_wp_menu_item( $callback ) {
self::$callbacks[ $callback ] = true;
}
/**
* Get registered menu items.
*
* @return array
*/
public static function get_items() {
return apply_filters( 'woocommerce_navigation_menu_items', self::$menu_items );
}
/**
* Get registered menu items.
*
* @return array
*/
public static function get_category_items( $category ) {
if ( ! isset( self::$categories[ $category ] ) ) {
return array();
}
$menu_item_ids = self::$categories[ $category ];
$category_menu_items = array();
foreach ( $menu_item_ids as $id ) {
if ( isset( self::$menu_items[ $id ] ) ) {
$category_menu_items[] = self::$menu_items[ $id ];
}
}
return apply_filters( 'woocommerce_navigation_menu_category_items', $category_menu_items );
}
/**
* Get registered callbacks.
*
* @return array
*/
public static function get_callbacks() {
return apply_filters( 'woocommerce_navigation_callbacks', self::$callbacks );
}
/**
* Gets the menu item data mapped by category and menu ID.
*
* @return array
*/
public static function get_mapped_menu_items() {
$menu_items = self::get_items();
$mapped_items = array();
// Sort the items by order and title.
$order = array_column( $menu_items, 'order' );
$title = array_column( $menu_items, 'title' );
array_multisort( $order, SORT_ASC, $title, SORT_ASC, $menu_items );
foreach ( $menu_items as $id => $menu_item ) {
$category_id = $menu_item[ 'parent' ];
$menu_id = $menu_item[ 'menuId' ];
if ( ! isset( $mapped_items[ $category_id ] ) ) {
$mapped_items[ $category_id ] = array();
foreach ( self::MENU_IDS as $available_menu_id ) {
$mapped_items[ $category_id ][ $available_menu_id ] = array();
}
}
// Incorrect menu ID.
if ( ! isset( $mapped_items[ $category_id ][ $menu_id ] ) ) {
continue;
}
// Remove the item if the user cannot access it.
if ( isset( $menu_item[ 'capability' ] ) && ! current_user_can( $menu_item[ 'capability' ] ) ) {
continue;
}
$mapped_items[ $category_id ][ $menu_id ][] = $menu_item;
}
return $mapped_items;
}
/**
* Add the menu to the page output.
*
* @param array $menu Menu items.
* @return array
*/
public function enqueue_data( $menu ) {
$data = array(
'menuItems' => array_values( self::get_items() ),
'rootBackUrl' => get_dashboard_url(),
);
wp_add_inline_script( WC_ADMIN_APP, 'window.wcNavigation = ' . wp_json_encode( $data ), 'before' );
}
}
Features/Navigation/Screen.php 0000644 00000013106 15153746750 0012367 0 ustar 00 <?php
/**
* WooCommerce Navigation Screen
*
* @package Woocommerce Navigation
*/
namespace Automattic\WooCommerce\Admin\Features\Navigation;
use Automattic\WooCommerce\Admin\Features\Navigation\Menu;
/**
* Contains logic for the WooCommerce Navigation menu.
*/
class Screen {
/**
* Class instance.
*
* @var Screen instance
*/
protected static $instance = null;
/**
* Screen IDs of registered pages.
*
* @var array
*/
protected static $screen_ids = array();
/**
* Registered post types.
*
* @var array
*/
protected static $post_types = array();
/**
* Registered taxonomies.
*
* @var array
*/
protected static $taxonomies = array();
/**
* Get class instance.
*/
final public static function instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init.
*/
public function init() {
add_filter( 'admin_body_class', array( $this, 'add_body_class' ) );
}
/**
* Returns an array of filtered screen ids.
*/
public static function get_screen_ids() {
return apply_filters( 'woocommerce_navigation_screen_ids', self::$screen_ids );
}
/**
* Returns an array of registered post types.
*/
public static function get_post_types() {
return apply_filters( 'woocommerce_navigation_post_types', self::$post_types );
}
/**
* Returns an array of registered post types.
*/
public static function get_taxonomies() {
return apply_filters( 'woocommerce_navigation_taxonomies', self::$taxonomies );
}
/**
* Check if we're on a WooCommerce page
*
* @return bool
*/
public static function is_woocommerce_page() {
global $pagenow;
// Get taxonomy if on a taxonomy screen.
$taxonomy = '';
if ( in_array( $pagenow, array( 'edit-tags.php', 'term.php' ), true ) ) {
if ( isset( $_GET['taxonomy'] ) ) { // phpcs:ignore CSRF ok.
$taxonomy = sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ); // phpcs:ignore CSRF ok.
}
}
$taxonomies = self::get_taxonomies();
// Get post type if on a post screen.
$post_type = '';
if ( in_array( $pagenow, array( 'edit.php', 'post.php', 'post-new.php' ), true ) ) {
if ( isset( $_GET['post'] ) ) { // phpcs:ignore CSRF ok.
$post_type = get_post_type( (int) $_GET['post'] ); // phpcs:ignore CSRF ok.
} elseif ( isset( $_GET['post_type'] ) ) { // phpcs:ignore CSRF ok.
$post_type = sanitize_text_field( wp_unslash( $_GET['post_type'] ) ); // phpcs:ignore CSRF ok.
}
}
$post_types = self::get_post_types();
// Get current screen ID.
$current_screen = get_current_screen();
$screen_ids = self::get_screen_ids();
$current_screen_id = $current_screen ? $current_screen->id : null;
if (
in_array( $post_type, $post_types, true ) ||
in_array( $taxonomy, $taxonomies, true ) ||
self::is_woocommerce_core_taxonomy( $taxonomy ) ||
in_array( $current_screen_id, $screen_ids, true )
) {
return true;
}
return false;
}
/**
* Check if a given taxonomy is a WooCommerce core related taxonomy.
*
* @param string $taxonomy Taxonomy.
* @return bool
*/
public static function is_woocommerce_core_taxonomy( $taxonomy ) {
if ( in_array( $taxonomy, array( 'product_cat', 'product_tag' ), true ) ) {
return true;
}
if ( 'pa_' === substr( $taxonomy, 0, 3 ) ) {
return true;
}
return false;
}
/**
* Add navigation classes to body.
*
* @param string $classes Classes.
* @return string
*/
public function add_body_class( $classes ) {
if ( self::is_woocommerce_page() ) {
$classes .= ' has-woocommerce-navigation';
/**
* Adds the ability to skip disabling of the WP toolbar.
*
* @param boolean $bool WP Toolbar disabled.
*/
if ( apply_filters( 'woocommerce_navigation_wp_toolbar_disabled', true ) ) {
$classes .= ' is-wp-toolbar-disabled';
}
}
return $classes;
}
/**
* Adds a screen ID to the list of screens that use the navigtion.
* Finds the parent if none is given to grab the correct screen ID.
*
* @param string $callback Callback or URL for page.
* @param string|null $parent Parent screen ID.
*/
public static function add_screen( $callback, $parent = null ) {
global $submenu;
$plugin_page = self::get_plugin_page( $callback );
if ( ! $parent ) {
$parent = Menu::get_parent_key( $callback );
}
$screen_id = get_plugin_page_hookname( $plugin_page, $parent );
// This screen has already been added.
if ( in_array( $screen_id, self::$screen_ids, true ) ) {
return;
}
self::$screen_ids[] = $screen_id;
}
/**
* Get the plugin page slug.
*
* @param string $callback Callback.
* @return string
*/
public static function get_plugin_page( $callback ) {
$url = Menu::get_callback_url( $callback );
$parts = wp_parse_url( $url );
if ( ! isset( $parts['query'] ) ) {
return $callback;
}
parse_str( $parts['query'], $query );
if ( ! isset( $query['page'] ) ) {
return $callback;
}
$plugin_page = wp_unslash( $query['page'] );
$plugin_page = plugin_basename( $plugin_page );
return $plugin_page;
}
/**
* Register post type for use in WooCommerce Navigation screens.
*
* @param string $post_type Post type to add.
*/
public static function register_post_type( $post_type ) {
if ( ! in_array( $post_type, self::$post_types, true ) ) {
self::$post_types[] = $post_type;
}
}
/**
* Register taxonomy for use in WooCommerce Navigation screens.
*
* @param string $taxonomy Taxonomy to add.
*/
public static function register_taxonomy( $taxonomy ) {
if ( ! in_array( $taxonomy, self::$taxonomies, true ) ) {
self::$taxonomies[] = $taxonomy;
}
}
}
Features/NewProductManagementExperience.php 0000644 00000005022 15153746750 0015146 0 ustar 00 <?php
/**
* WooCommerce New Product Management Experience
*/
namespace Automattic\WooCommerce\Admin\Features;
use Automattic\WooCommerce\Admin\Features\TransientNotices;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Internal\Admin\Loader;
use WP_Block_Editor_Context;
/**
* Loads assets related to the new product management experience page.
*/
class NewProductManagementExperience {
/**
* Option name used to toggle this feature.
*/
const TOGGLE_OPTION_NAME = 'woocommerce_new_product_management_enabled';
/**
* Constructor
*/
public function __construct() {
$this->maybe_show_disabled_notice();
if ( ! Features::is_enabled( 'new-product-management-experience' ) ) {
return;
}
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
add_action( 'get_edit_post_link', array( $this, 'update_edit_product_link' ), 10, 2 );
}
/**
* Maybe show disabled notice.
*/
public function maybe_show_disabled_notice() {
$new_product_experience_param = 'new-product-experience-disabled';
if ( isset( $_GET[ $new_product_experience_param ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
TransientNotices::add(
array(
'user_id' => get_current_user_id(),
'id' => 'new-product-experience-disbled',
'status' => 'success',
'content' => __( '🌟‎ ‎ Thanks for the feedback. We’ll put it to good use!', 'woocommerce' ),
)
);
$url = isset( $_SERVER['REQUEST_URI'] ) ? wc_clean( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
$url = remove_query_arg( 'new-product-experience-disabled', $url );
wp_safe_redirect( $url );
exit;
}
}
/**
* Enqueue styles needed for the rich text editor.
*/
public function enqueue_styles() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
wp_enqueue_style( 'wp-edit-blocks' );
wp_enqueue_style( 'wp-format-library' );
wp_enqueue_editor();
/**
* Enqueue any block editor related assets.
*
* @since 7.1.0
*/
do_action( 'enqueue_block_editor_assets' );
}
/**
* Update the edit product links when the new experience is enabled.
*
* @param string $link The edit link.
* @param int $post_id Post ID.
* @return string
*/
public function update_edit_product_link( $link, $post_id ) {
$product = wc_get_product( $post_id );
if ( ! $product ) {
return $link;
}
if ( $product->get_type() === 'simple' ) {
return admin_url( 'admin.php?page=wc-admin&path=/product/' . $product->get_id() );
}
return $link;
}
}
Features/Onboarding.php 0000644 00000006356 15153746750 0011144 0 ustar 00 <?php
/**
* WooCommerce Onboarding
*/
namespace Automattic\WooCommerce\Admin\Features;
use Automattic\WooCommerce\Admin\DeprecatedClassFacade;
/**
* Contains backend logic for the onboarding profile and checklist feature.
*
* @deprecated since 6.3.0, use WooCommerce\Internal\Admin\Onboarding.
*/
class Onboarding extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Admin\Features\Onboarding';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '6.3.0';
/**
* Hook into WooCommerce.
*/
public function __construct() {
}
/**
* Get a list of allowed industries for the onboarding wizard.
*
* @deprecated 6.3.0
* @return array
*/
public static function get_allowed_industries() {
wc_deprecated_function( 'get_allowed_industries', '6.3', '\Automattic\WooCommerce\Internal\Admin\OnboardingIndustries::get_allowed_industries()' );
return \Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingIndustries::get_allowed_industries();
}
/**
* Get a list of allowed product types for the onboarding wizard.
*
* @deprecated 6.3.0
* @return array
*/
public static function get_allowed_product_types() {
wc_deprecated_function( 'get_allowed_product_types', '6.3', '\Automattic\WooCommerce\Internal\Admin\OnboardingProducts::get_allowed_product_types()' );
return \Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProducts::get_allowed_product_types();
}
/**
* Get a list of themes for the onboarding wizard.
*
* @deprecated 6.3.0
* @return array
*/
public static function get_themes() {
wc_deprecated_function( 'get_themes', '6.3', '\Automattic\WooCommerce\Internal\Admin\OnboardingThemes::get_themes()' );
return \Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingThemes::get_themes();
}
/**
* Get theme data used in onboarding theme browser.
*
* @deprecated 6.3.0
* @param WP_Theme $theme Theme to gather data from.
* @return array
*/
public static function get_theme_data( $theme ) {
wc_deprecated_function( 'get_theme_data', '6.3', '\Automattic\WooCommerce\Internal\Admin\OnboardingThemes::get_theme_data()' );
return \Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingThemes::get_theme_data();
}
/**
* Gets an array of themes that can be installed & activated via the onboarding wizard.
*
* @deprecated 6.3.0
* @return array
*/
public static function get_allowed_themes() {
wc_deprecated_function( 'get_allowed_themes', '6.3', '\Automattic\WooCommerce\Internal\Admin\OnboardingThemes::get_allowed_themes()' );
return \Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingThemes::get_allowed_themes();
}
/**
* Get dynamic product data from API.
*
* @deprecated 6.3.0
* @param array $product_types Array of product types.
* @return array
*/
public static function get_product_data( $product_types ) {
wc_deprecated_function( 'get_product_data', '6.3', '\Automattic\WooCommerce\Internal\Admin\OnboardingProducts::get_product_data()' );
return \Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProducts::get_product_data();
}
}
Features/OnboardingTasks/DeprecatedExtendedTask.php 0000644 00000006322 15153746750 0016507 0 ustar 00 <?php
/**
* A temporary class for creating tasks on the fly from deprecated tasks.
*/
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks;
/**
* DeprecatedExtendedTask class.
*/
class DeprecatedExtendedTask extends Task {
/**
* ID.
*
* @var string
*/
public $id = '';
/**
* Additional info.
*
* @var string|null
*/
public $additional_info = '';
/**
* Content.
*
* @var string
*/
public $content = '';
/**
* Whether the task is complete or not.
*
* @var boolean
*/
public $is_complete = false;
/**
* Snoozeable.
*
* @var boolean
*/
public $is_snoozeable = false;
/**
* Dismissable.
*
* @var boolean
*/
public $is_dismissable = false;
/**
* Whether the store is capable of viewing the task.
*
* @var bool
*/
public $can_view = true;
/**
* Level.
*
* @var int
*/
public $level = 3;
/**
* Time.
*
* @var string|null
*/
public $time;
/**
* Title.
*
* @var string
*/
public $title = '';
/**
* Constructor.
*
* @param TaskList $task_list Parent task list.
* @param array $args Array of task args.
*/
public function __construct( $task_list, $args ) {
parent::__construct( $task_list );
$task_args = wp_parse_args(
$args,
array(
'id' => null,
'is_dismissable' => false,
'is_snoozeable' => false,
'can_view' => true,
'level' => 3,
'additional_info' => null,
'content' => '',
'title' => '',
'is_complete' => false,
'time' => null,
)
);
$this->id = $task_args['id'];
$this->additional_info = $task_args['additional_info'];
$this->content = $task_args['content'];
$this->is_complete = $task_args['is_complete'];
$this->is_dismissable = $task_args['is_dismissable'];
$this->is_snoozeable = $task_args['is_snoozeable'];
$this->can_view = $task_args['can_view'];
$this->level = $task_args['level'];
$this->time = $task_args['time'];
$this->title = $task_args['title'];
}
/**
* ID.
*
* @return string
*/
public function get_id() {
return $this->id;
}
/**
* Additional info.
*
* @return string
*/
public function get_additional_info() {
return $this->additional_info;
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return $this->content;
}
/**
* Level.
*
* @return int
*/
public function get_level() {
return $this->level;
}
/**
* Title
*
* @return string
*/
public function get_title() {
return $this->title;
}
/**
* Time
*
* @return string|null
*/
public function get_time() {
return $this->time;
}
/**
* Check if a task is snoozeable.
*
* @return bool
*/
public function is_snoozeable() {
return $this->is_snoozeable;
}
/**
* Check if a task is dismissable.
*
* @return bool
*/
public function is_dismissable() {
return $this->is_dismissable;
}
/**
* Check if a task is dismissable.
*
* @return bool
*/
public function is_complete() {
return $this->is_complete;
}
/**
* Check if a task is dismissable.
*
* @return bool
*/
public function can_view() {
return $this->can_view;
}
}
Features/OnboardingTasks/DeprecatedOptions.php 0000644 00000005000 15153746750 0015547 0 ustar 00 <?php
/**
* Filters for maintaining backwards compatibility with deprecated options.
*/
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\TaskList;
use WC_Install;
/**
* DeprecatedOptions class.
*/
class DeprecatedOptions {
/**
* Initialize.
*/
public static function init() {
add_filter( 'pre_option_woocommerce_task_list_hidden', array( __CLASS__, 'get_deprecated_options' ), 10, 2 );
add_filter( 'pre_option_woocommerce_extended_task_list_hidden', array( __CLASS__, 'get_deprecated_options' ), 10, 2 );
add_action( 'pre_update_option_woocommerce_task_list_hidden', array( __CLASS__, 'update_deprecated_options' ), 10, 3 );
add_action( 'pre_update_option_woocommerce_extended_task_list_hidden', array( __CLASS__, 'update_deprecated_options' ), 10, 3 );
}
/**
* Get the values from the correct source when attempting to retrieve deprecated options.
*
* @param string $pre_option Pre option value.
* @param string $option Option name.
* @return string
*/
public static function get_deprecated_options( $pre_option, $option ) {
if ( defined( 'WC_INSTALLING' ) && WC_INSTALLING === true ) {
return $pre_option;
}
$hidden = get_option( 'woocommerce_task_list_hidden_lists', array() );
switch ( $option ) {
case 'woocommerce_task_list_hidden':
return in_array( 'setup', $hidden, true ) ? 'yes' : 'no';
case 'woocommerce_extended_task_list_hidden':
return in_array( 'extended', $hidden, true ) ? 'yes' : 'no';
}
}
/**
* Updates the new option names when deprecated options are updated.
* This is a temporary fallback until we can fully remove the old task list components.
*
* @param string $value New value.
* @param string $old_value Old value.
* @param string $option Option name.
* @return string
*/
public static function update_deprecated_options( $value, $old_value, $option ) {
switch ( $option ) {
case 'woocommerce_task_list_hidden':
$task_list = TaskLists::get_list( 'setup' );
if ( ! $task_list ) {
return;
}
$update = 'yes' === $value ? $task_list->hide() : $task_list->unhide();
delete_option( 'woocommerce_task_list_hidden' );
return false;
case 'woocommerce_extended_task_list_hidden':
$task_list = TaskLists::get_list( 'extended' );
if ( ! $task_list ) {
return;
}
$update = 'yes' === $value ? $task_list->hide() : $task_list->unhide();
delete_option( 'woocommerce_extended_task_list_hidden' );
return false;
}
}
}
Features/OnboardingTasks/Init.php 0000644 00000002116 15153746750 0013043 0 ustar 00 <?php
/**
* WooCommerce Onboarding Tasks
*/
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\DeprecatedOptions;
/**
* Contains the logic for completing onboarding tasks.
*/
class Init {
/**
* Class instance.
*
* @var OnboardingTasks instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
public function __construct() {
DeprecatedOptions::init();
TaskLists::init();
}
/**
* Get task item data for settings filter.
*
* @return array
*/
public static function get_settings() {
$settings = array();
$wc_pay_is_connected = false;
if ( class_exists( '\WC_Payments' ) ) {
$wc_payments_gateway = \WC_Payments::get_gateway();
$wc_pay_is_connected = method_exists( $wc_payments_gateway, 'is_connected' )
? $wc_payments_gateway->is_connected()
: false;
}
return $settings;
}
}
Features/OnboardingTasks/Task.php 0000644 00000030111 15153746750 0013036 0 ustar 00 <?php
/**
* Handles task related methods.
*/
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks;
use Automattic\WooCommerce\Internal\Admin\WCAdminUser;
/**
* Task class.
*/
abstract class Task {
/**
* Task traits.
*/
use TaskTraits;
/**
* Name of the dismiss option.
*
* @var string
*/
const DISMISSED_OPTION = 'woocommerce_task_list_dismissed_tasks';
/**
* Name of the snooze option.
*
* @var string
*
* @deprecated 7.2.0
*/
const SNOOZED_OPTION = 'woocommerce_task_list_remind_me_later_tasks';
/**
* Name of the actioned option.
*
* @var string
*/
const ACTIONED_OPTION = 'woocommerce_task_list_tracked_completed_actions';
/**
* Option name of completed tasks.
*
* @var string
*/
const COMPLETED_OPTION = 'woocommerce_task_list_tracked_completed_tasks';
/**
* Name of the active task transient.
*
* @var string
*/
const ACTIVE_TASK_TRANSIENT = 'wc_onboarding_active_task';
/**
* Parent task list.
*
* @var TaskList
*/
protected $task_list;
/**
* Duration to milisecond mapping.
*
* @var string
*/
protected $duration_to_ms = array(
'day' => DAY_IN_SECONDS * 1000,
'hour' => HOUR_IN_SECONDS * 1000,
'week' => WEEK_IN_SECONDS * 1000,
);
/**
* Constructor
*
* @param TaskList|null $task_list Parent task list.
*/
public function __construct( $task_list = null ) {
$this->task_list = $task_list;
}
/**
* ID.
*
* @return string
*/
abstract public function get_id();
/**
* Title.
*
* @return string
*/
abstract public function get_title();
/**
* Content.
*
* @return string
*/
abstract public function get_content();
/**
* Time.
*
* @return string
*/
abstract public function get_time();
/**
* Parent ID.
*
* @return string
*/
public function get_parent_id() {
if ( ! $this->task_list ) {
return '';
}
return $this->task_list->get_list_id();
}
/**
* Get task list options.
*
* @return array
*/
public function get_parent_options() {
if ( ! $this->task_list ) {
return array();
}
return $this->task_list->options;
}
/**
* Get custom option.
*
* @param string $option_name name of custom option.
* @return mixed|null
*/
public function get_parent_option( $option_name ) {
if ( $this->task_list && isset( $this->task_list->options[ $option_name ] ) ) {
return $this->task_list->options[ $option_name ];
}
return null;
}
/**
* Prefix event for track event naming.
*
* @param string $event_name Event name.
* @return string
*/
public function prefix_event( $event_name ) {
if ( ! $this->task_list ) {
return '';
}
return $this->task_list->prefix_event( $event_name );
}
/**
* Additional info.
*
* @return string
*/
public function get_additional_info() {
return '';
}
/**
* Additional data.
*
* @return mixed
*/
public function get_additional_data() {
return null;
}
/**
* Badge.
*
* @return string
*/
public function get_badge() {
return '';
}
/**
* Level.
*
* @deprecated 7.2.0
*
* @return string
*/
public function get_level() {
wc_deprecated_function( __CLASS__ . '::' . __FUNCTION__, '7.2.0' );
return 3;
}
/**
* Action label.
*
* @return string
*/
public function get_action_label() {
return __( "Let's go", 'woocommerce' );
}
/**
* Action URL.
*
* @return string
*/
public function get_action_url() {
return null;
}
/**
* Check if a task is dismissable.
*
* @return bool
*/
public function is_dismissable() {
return false;
}
/**
* Bool for task dismissal.
*
* @return bool
*/
public function is_dismissed() {
if ( ! $this->is_dismissable() ) {
return false;
}
$dismissed = get_option( self::DISMISSED_OPTION, array() );
return in_array( $this->get_id(), $dismissed, true );
}
/**
* Dismiss the task.
*
* @return bool
*/
public function dismiss() {
if ( ! $this->is_dismissable() ) {
return false;
}
$dismissed = get_option( self::DISMISSED_OPTION, array() );
$dismissed[] = $this->get_id();
$update = update_option( self::DISMISSED_OPTION, array_unique( $dismissed ) );
if ( $update ) {
$this->record_tracks_event( 'dismiss_task', array( 'task_name' => $this->get_id() ) );
}
return $update;
}
/**
* Undo task dismissal.
*
* @return bool
*/
public function undo_dismiss() {
$dismissed = get_option( self::DISMISSED_OPTION, array() );
$dismissed = array_diff( $dismissed, array( $this->get_id() ) );
$update = update_option( self::DISMISSED_OPTION, $dismissed );
if ( $update ) {
$this->record_tracks_event( 'undo_dismiss_task', array( 'task_name' => $this->get_id() ) );
}
return $update;
}
/**
* Check if a task is snoozeable.
*
* @deprecated 7.2.0
*
* @return bool
*/
public function is_snoozeable() {
wc_deprecated_function( __CLASS__ . '::' . __FUNCTION__, '7.2.0' );
return false;
}
/**
* Get the snoozed until datetime.
*
* @deprecated 7.2.0
*
* @return string
*/
public function get_snoozed_until() {
wc_deprecated_function( __CLASS__ . '::' . __FUNCTION__, '7.2.0' );
$snoozed_tasks = get_option( self::SNOOZED_OPTION, array() );
if ( isset( $snoozed_tasks[ $this->get_id() ] ) ) {
return $snoozed_tasks[ $this->get_id() ];
}
return null;
}
/**
* Bool for task snoozed.
*
* @deprecated 7.2.0
*
* @return bool
*/
public function is_snoozed() {
wc_deprecated_function( __CLASS__ . '::' . __FUNCTION__, '7.2.0' );
if ( ! $this->is_snoozeable() ) {
return false;
}
$snoozed = get_option( self::SNOOZED_OPTION, array() );
return isset( $snoozed[ $this->get_id() ] ) && $snoozed[ $this->get_id() ] > ( time() * 1000 );
}
/**
* Snooze the task.
*
* @param string $duration Duration to snooze. day|hour|week.
*
* @deprecated 7.2.0
*
* @return bool
*/
public function snooze( $duration = 'day' ) {
wc_deprecated_function( __CLASS__ . '::' . __FUNCTION__, '7.2.0' );
if ( ! $this->is_snoozeable() ) {
return false;
}
$snoozed = get_option( self::SNOOZED_OPTION, array() );
$snoozed_until = $this->duration_to_ms[ $duration ] + ( time() * 1000 );
$snoozed[ $this->get_id() ] = $snoozed_until;
$update = update_option( self::SNOOZED_OPTION, $snoozed );
if ( $update ) {
if ( $update ) {
$this->record_tracks_event( 'remindmelater_task', array( 'task_name' => $this->get_id() ) );
}
}
return $update;
}
/**
* Undo task snooze.
*
* @deprecated 7.2.0
*
* @return bool
*/
public function undo_snooze() {
wc_deprecated_function( __CLASS__ . '::' . __FUNCTION__, '7.2.0' );
$snoozed = get_option( self::SNOOZED_OPTION, array() );
unset( $snoozed[ $this->get_id() ] );
$update = update_option( self::SNOOZED_OPTION, $snoozed );
if ( $update ) {
$this->record_tracks_event( 'undo_remindmelater_task', array( 'task_name' => $this->get_id() ) );
}
return $update;
}
/**
* Check if a task list has previously been marked as complete.
*
* @return bool
*/
public function has_previously_completed() {
$complete = get_option( self::COMPLETED_OPTION, array() );
return in_array( $this->get_id(), $complete, true );
}
/**
* Track task completion if task is viewable.
*/
public function possibly_track_completion() {
if ( ! $this->is_complete() ) {
return;
}
if ( $this->has_previously_completed() ) {
return;
}
$completed_tasks = get_option( self::COMPLETED_OPTION, array() );
$completed_tasks[] = $this->get_id();
update_option( self::COMPLETED_OPTION, $completed_tasks );
$this->record_tracks_event( 'task_completed', array( 'task_name' => $this->get_id() ) );
}
/**
* Set this as the active task across page loads.
*/
public function set_active() {
if ( $this->is_complete() ) {
return;
}
set_transient(
self::ACTIVE_TASK_TRANSIENT,
$this->get_id(),
DAY_IN_SECONDS
);
}
/**
* Check if this is the active task.
*/
public function is_active() {
return get_transient( self::ACTIVE_TASK_TRANSIENT ) === $this->get_id();
}
/**
* Check if the store is capable of viewing the task.
*
* @return bool
*/
public function can_view() {
return true;
}
/**
* Check if task is disabled.
*
* @deprecated 7.2.0
*
* @return bool
*/
public function is_disabled() {
wc_deprecated_function( __CLASS__ . '::' . __FUNCTION__, '7.2.0' );
return false;
}
/**
* Check if the task is complete.
*
* @return bool
*/
public function is_complete() {
return self::is_actioned();
}
/**
* Check if the task has been visited.
*
* @return bool
*/
public function is_visited() {
$user_id = get_current_user_id();
$response = WCAdminUser::get_user_data_field( $user_id, 'task_list_tracked_started_tasks' );
$tracked_tasks = $response ? json_decode( $response, true ) : array();
return isset( $tracked_tasks[ $this->get_id() ] ) && $tracked_tasks[ $this->get_id() ] > 0;
}
/**
* Check if should record event when task is viewed
*
* @return bool
*/
public function get_record_view_event(): bool {
return false;
}
/**
* Get the task as JSON.
*
* @return array
*/
public function get_json() {
$this->possibly_track_completion();
return array(
'id' => $this->get_id(),
'parentId' => $this->get_parent_id(),
'title' => $this->get_title(),
'badge' => $this->get_badge(),
'canView' => $this->can_view(),
'content' => $this->get_content(),
'additionalInfo' => $this->get_additional_info(),
'actionLabel' => $this->get_action_label(),
'actionUrl' => $this->get_action_url(),
'isComplete' => $this->is_complete(),
'time' => $this->get_time(),
'level' => 3,
'isActioned' => $this->is_actioned(),
'isDismissed' => $this->is_dismissed(),
'isDismissable' => $this->is_dismissable(),
'isSnoozed' => false,
'isSnoozeable' => false,
'isVisited' => $this->is_visited(),
'isDisabled' => false,
'snoozedUntil' => null,
'additionalData' => self::convert_object_to_camelcase( $this->get_additional_data() ),
'eventPrefix' => $this->prefix_event( '' ),
'recordViewEvent' => $this->get_record_view_event(),
);
}
/**
* Convert object keys to camelcase.
*
* @param array $data Data to convert.
* @return object
*/
public static function convert_object_to_camelcase( $data ) {
if ( ! is_array( $data ) ) {
return $data;
}
$new_object = (object) array();
foreach ( $data as $key => $value ) {
$new_key = lcfirst( implode( '', array_map( 'ucfirst', explode( '_', $key ) ) ) );
$new_object->$new_key = $value;
}
return $new_object;
}
/**
* Mark a task as actioned. Used to verify an action has taken place in some tasks.
*
* @return bool
*/
public function mark_actioned() {
$actioned = get_option( self::ACTIONED_OPTION, array() );
$actioned[] = $this->get_id();
$update = update_option( self::ACTIONED_OPTION, array_unique( $actioned ) );
if ( $update ) {
$this->record_tracks_event( 'actioned_task', array( 'task_name' => $this->get_id() ) );
}
return $update;
}
/**
* Check if a task has been actioned.
*
* @return bool
*/
public function is_actioned() {
return self::is_task_actioned( $this->get_id() );
}
/**
* Check if a provided task ID has been actioned.
*
* @param string $id Task ID.
* @return bool
*/
public static function is_task_actioned( $id ) {
$actioned = get_option( self::ACTIONED_OPTION, array() );
return in_array( $id, $actioned, true );
}
/**
* Sorting function for tasks.
*
* @param Task $a Task a.
* @param Task $b Task b.
* @param array $sort_by list of columns with sort order.
* @return int
*/
public static function sort( $a, $b, $sort_by = array() ) {
$result = 0;
foreach ( $sort_by as $data ) {
$key = $data['key'];
$a_val = $a->$key ?? false;
$b_val = $b->$key ?? false;
if ( 'asc' === $data['order'] ) {
$result = $a_val <=> $b_val;
} else {
$result = $b_val <=> $a_val;
}
if ( 0 !== $result ) {
break;
}
}
return $result;
}
}
Features/OnboardingTasks/TaskList.php 0000644 00000022162 15153746750 0013701 0 ustar 00 <?php
/**
* Handles storage and retrieval of a task list
*/
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use Automattic\WooCommerce\Admin\WCAdminHelper;
/**
* Task List class.
*/
class TaskList {
/**
* Task traits.
*/
use TaskTraits;
/**
* Option name hidden task lists.
*/
const HIDDEN_OPTION = 'woocommerce_task_list_hidden_lists';
/**
* Option name of completed task lists.
*/
const COMPLETED_OPTION = 'woocommerce_task_list_completed_lists';
/**
* Option name of hidden reminder bar.
*/
const REMINDER_BAR_HIDDEN_OPTION = 'woocommerce_task_list_reminder_bar_hidden';
/**
* ID.
*
* @var string
*/
public $id = '';
/**
* ID.
*
* @var string
*/
public $hidden_id = '';
/**
* ID.
*
* @var boolean
*/
public $display_progress_header = false;
/**
* Title.
*
* @var string
*/
public $title = '';
/**
* Tasks.
*
* @var array
*/
public $tasks = array();
/**
* Sort keys.
*
* @var array
*/
public $sort_by = array();
/**
* Event prefix.
*
* @var string|null
*/
public $event_prefix = null;
/**
* Task list visibility.
*
* @var boolean
*/
public $visible = true;
/**
* Array of custom options.
*
* @var array
*/
public $options = array();
/**
* Array of TaskListSection.
*
* @deprecated 7.2.0
*
* @var array
*/
private $sections = array();
/**
* Key value map of task class and id used for sections.
*
* @deprecated 7.2.0
*
* @var array
*/
public $task_class_id_map = array();
/**
* Constructor
*
* @param array $data Task list data.
*/
public function __construct( $data = array() ) {
$defaults = array(
'id' => null,
'hidden_id' => null,
'title' => '',
'tasks' => array(),
'sort_by' => array(),
'event_prefix' => null,
'options' => array(),
'visible' => true,
'display_progress_header' => false,
);
$data = wp_parse_args( $data, $defaults );
$this->id = $data['id'];
$this->hidden_id = $data['hidden_id'];
$this->title = $data['title'];
$this->sort_by = $data['sort_by'];
$this->event_prefix = $data['event_prefix'];
$this->options = $data['options'];
$this->visible = $data['visible'];
$this->display_progress_header = $data['display_progress_header'];
foreach ( $data['tasks'] as $task_name ) {
$class = 'Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\\' . $task_name;
$task = new $class( $this );
$this->add_task( $task );
}
$this->possibly_remove_reminder_bar();
}
/**
* Check if the task list is hidden.
*
* @return bool
*/
public function is_hidden() {
$hidden = get_option( self::HIDDEN_OPTION, array() );
return in_array( $this->hidden_id ? $this->hidden_id : $this->id, $hidden, true );
}
/**
* Check if the task list is visible.
*
* @return bool
*/
public function is_visible() {
if ( ! $this->visible || ! count( $this->get_viewable_tasks() ) > 0 ) {
return false;
}
return ! $this->is_hidden();
}
/**
* Hide the task list.
*
* @return bool
*/
public function hide() {
if ( $this->is_hidden() ) {
return;
}
$viewable_tasks = $this->get_viewable_tasks();
$completed_count = array_reduce(
$viewable_tasks,
function( $total, $task ) {
return $task->is_complete() ? $total + 1 : $total;
},
0
);
$this->record_tracks_event(
'completed',
array(
'action' => 'remove_card',
'completed_task_count' => $completed_count,
'incomplete_task_count' => count( $viewable_tasks ) - $completed_count,
)
);
$hidden = get_option( self::HIDDEN_OPTION, array() );
$hidden[] = $this->hidden_id ? $this->hidden_id : $this->id;
$this->maybe_set_default_layout( $hidden );
return update_option( self::HIDDEN_OPTION, array_unique( $hidden ) );
}
/**
* Sets the default homepage layout to two_columns if "setup" tasklist is completed or hidden.
*
* @param array $completed_or_hidden_tasklist_ids Array of tasklist ids.
*/
public function maybe_set_default_layout( $completed_or_hidden_tasklist_ids ) {
if ( in_array( 'setup', $completed_or_hidden_tasklist_ids, true ) ) {
update_option( 'woocommerce_default_homepage_layout', 'two_columns' );
}
}
/**
* Undo hiding of the task list.
*
* @return bool
*/
public function unhide() {
$hidden = get_option( self::HIDDEN_OPTION, array() );
$hidden = array_diff( $hidden, array( $this->hidden_id ? $this->hidden_id : $this->id ) );
return update_option( self::HIDDEN_OPTION, $hidden );
}
/**
* Check if all viewable tasks are complete.
*
* @return bool
*/
public function is_complete() {
foreach ( $this->get_viewable_tasks() as $viewable_task ) {
if ( $viewable_task->is_complete() === false ) {
return false;
}
}
return true;
}
/**
* Check if a task list has previously been marked as complete.
*
* @return bool
*/
public function has_previously_completed() {
$complete = get_option( self::COMPLETED_OPTION, array() );
return in_array( $this->get_list_id(), $complete, true );
}
/**
* Add task to the task list.
*
* @param Task $task Task class.
*/
public function add_task( $task ) {
if ( ! is_subclass_of( $task, 'Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task' ) ) {
return new \WP_Error(
'woocommerce_task_list_invalid_task',
__( 'Task is not a subclass of `Task`', 'woocommerce' )
);
}
if ( array_search( $task, $this->tasks, true ) ) {
return;
}
$this->tasks[] = $task;
}
/**
* Get only visible tasks in list.
*
* @param string $task_id id of task.
* @return Task
*/
public function get_task( $task_id ) {
return current(
array_filter(
$this->tasks,
function( $task ) use ( $task_id ) {
return $task->get_id() === $task_id;
}
)
);
}
/**
* Get only visible tasks in list.
*
* @return array
*/
public function get_viewable_tasks() {
return array_values(
array_filter(
$this->tasks,
function( $task ) {
return $task->can_view();
}
)
);
}
/**
* Get task list sections.
*
* @deprecated 7.2.0
*
* @return array
*/
public function get_sections() {
wc_deprecated_function( __CLASS__ . '::' . __FUNCTION__, '7.2.0' );
return $this->sections;
}
/**
* Track list completion of viewable tasks.
*/
public function possibly_track_completion() {
if ( ! $this->is_complete() ) {
return;
}
if ( $this->has_previously_completed() ) {
return;
}
$completed_lists = get_option( self::COMPLETED_OPTION, array() );
$completed_lists[] = $this->get_list_id();
update_option( self::COMPLETED_OPTION, $completed_lists );
$this->maybe_set_default_layout( $completed_lists );
$this->record_tracks_event( 'tasks_completed' );
}
/**
* Sorts the attached tasks array.
*
* @param array $sort_by list of columns with sort order.
* @return TaskList returns $this, for chaining.
*/
public function sort_tasks( $sort_by = array() ) {
$sort_by = count( $sort_by ) > 0 ? $sort_by : $this->sort_by;
if ( 0 !== count( $sort_by ) ) {
usort(
$this->tasks,
function( $a, $b ) use ( $sort_by ) {
return Task::sort( $a, $b, $sort_by );
}
);
}
return $this;
}
/**
* Prefix event for track event naming.
*
* @param string $event_name Event name.
* @return string
*/
public function prefix_event( $event_name ) {
if ( null !== $this->event_prefix ) {
return $this->event_prefix . $event_name;
}
return $this->get_list_id() . '_tasklist_' . $event_name;
}
/**
* Returns option to keep completed task list.
*
* @return string
*/
public function get_keep_completed_task_list() {
return get_option( 'woocommerce_task_list_keep_completed', 'no' );
}
/**
* Remove reminder bar four weeks after store creation.
*/
public static function possibly_remove_reminder_bar() {
$bar_hidden = get_option( self::REMINDER_BAR_HIDDEN_OPTION, 'no' );
$active_for_four_weeks = WCAdminHelper::is_wc_admin_active_for( WEEK_IN_SECONDS * 4 );
if ( 'yes' === $bar_hidden || ! $active_for_four_weeks ) {
return;
}
update_option( self::REMINDER_BAR_HIDDEN_OPTION, 'yes' );
}
/**
* Get the list for use in JSON.
*
* @return array
*/
public function get_json() {
$this->possibly_track_completion();
$tasks_json = array();
foreach ( $this->tasks as $task ) {
$json = $task->get_json();
if ( $json['canView'] ) {
$tasks_json[] = $json;
}
}
return array(
'id' => $this->get_list_id(),
'title' => $this->title,
'isHidden' => $this->is_hidden(),
'isVisible' => $this->is_visible(),
'isComplete' => $this->is_complete(),
'tasks' => $tasks_json,
'eventPrefix' => $this->prefix_event( '' ),
'displayProgressHeader' => $this->display_progress_header,
'keepCompletedTaskList' => $this->get_keep_completed_task_list(),
);
}
}
Features/OnboardingTasks/TaskListSection.php 0000644 00000004466 15153746750 0015235 0 ustar 00 <?php
/**
* Handles storage and retrieval of a task list section
*/
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks;
/**
* Task List section class.
*
* @deprecated 7.2.0
*/
class TaskListSection {
/**
* Title.
*
* @var string
*/
public $id = '';
/**
* Title.
*
* @var string
*/
public $title = '';
/**
* Description.
*
* @var string
*/
public $description = '';
/**
* Image.
*
* @var string
*/
public $image = '';
/**
* Tasks.
*
* @var array
*/
public $task_names = array();
/**
* Parent task list.
*
* @var TaskList
*/
protected $task_list;
/**
* Constructor
*
* @param array $data Task list data.
* @param TaskList|null $task_list Parent task list.
*/
public function __construct( $data = array(), $task_list = null ) {
$defaults = array(
'id' => '',
'title' => '',
'description' => '',
'image' => '',
'tasks' => array(),
);
$data = wp_parse_args( $data, $defaults );
$this->task_list = $task_list;
$this->id = $data['id'];
$this->title = $data['title'];
$this->description = $data['description'];
$this->image = $data['image'];
$this->task_names = $data['task_names'];
}
/**
* Returns if section is complete.
*
* @return boolean;
*/
private function is_complete() {
$complete = true;
foreach ( $this->task_names as $task_name ) {
if ( null !== $this->task_list && isset( $this->task_list->task_class_id_map[ $task_name ] ) ) {
$task = $this->task_list->get_task( $this->task_list->task_class_id_map[ $task_name ] );
if ( $task->can_view() && ! $task->is_complete() ) {
$complete = false;
break;
}
}
}
return $complete;
}
/**
* Get the list for use in JSON.
*
* @return array
*/
public function get_json() {
return array(
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
'image' => $this->image,
'tasks' => array_map(
function( $task_name ) {
if ( null !== $this->task_list && isset( $this->task_list->task_class_id_map[ $task_name ] ) ) {
return $this->task_list->task_class_id_map[ $task_name ];
}
return '';
},
$this->task_names
),
'isComplete' => $this->is_complete(),
);
}
}
Features/OnboardingTasks/TaskLists.php 0000644 00000025177 15153746750 0014075 0 ustar 00 <?php
/**
* Handles storage and retrieval of task lists
*/
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\DeprecatedExtendedTask;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\ReviewShippingOptions;
/**
* Task Lists class.
*/
class TaskLists {
/**
* Class instance.
*
* @var TaskLists instance
*/
protected static $instance = null;
/**
* An array of all registered lists.
*
* @var array
*/
protected static $lists = array();
/**
* Boolean value to indicate if default tasks have been added.
*
* @var boolean
*/
protected static $default_tasks_loaded = false;
/**
* The contents of this array is used in init_tasks() to run their init() methods.
* If the classes do not have an init() method then nothing is executed.
* Beyond that, adding tasks to this list has no effect, see init_default_lists() for the list of tasks.
* that are added for each task list.
*
* @var array
*/
const DEFAULT_TASKS = array(
'StoreDetails',
'Products',
'WooCommercePayments',
'Payments',
'Tax',
'Shipping',
'Marketing',
'Appearance',
'AdditionalPayments',
'ReviewShippingOptions',
'GetMobileApp',
);
/**
* Get class instance.
*/
final public static function instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Initialize the task lists.
*/
public static function init() {
self::init_default_lists();
add_action( 'admin_init', array( __CLASS__, 'set_active_task' ), 5 );
add_action( 'init', array( __CLASS__, 'init_tasks' ) );
add_action( 'admin_menu', array( __CLASS__, 'menu_task_count' ) );
add_filter( 'woocommerce_admin_shared_settings', array( __CLASS__, 'task_list_preloaded_settings' ), 20 );
}
/**
* Check if an experiment is the treatment or control.
*
* @param string $name Name prefix of experiment.
* @return bool
*/
public static function is_experiment_treatment( $name ) {
$anon_id = isset( $_COOKIE['tk_ai'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['tk_ai'] ) ) : '';
$allow_tracking = 'yes' === get_option( 'woocommerce_allow_tracking' );
$abtest = new \WooCommerce\Admin\Experimental_Abtest(
$anon_id,
'woocommerce',
$allow_tracking
);
$date = new \DateTime();
$date->setTimeZone( new \DateTimeZone( 'UTC' ) );
$experiment_name = sprintf(
'%s_%s_%s',
$name,
$date->format( 'Y' ),
$date->format( 'm' )
);
return $abtest->get_variation( $experiment_name ) === 'treatment';
}
/**
* Initialize default lists.
*/
public static function init_default_lists() {
$tasks = array(
'CustomizeStore',
'StoreDetails',
'Products',
'Appearance',
'WooCommercePayments',
'Payments',
'Tax',
'Shipping',
'Marketing',
);
if ( Features::is_enabled( 'core-profiler' ) ) {
$key = array_search( 'StoreDetails', $tasks, true );
if ( false !== $key ) {
unset( $tasks[ $key ] );
}
}
// Remove the old Personalize your store task if the new CustomizeStore is enabled.
$task_to_remove = Features::is_enabled( 'customize-store' ) ? 'Appearance' : 'CustomizeStore';
$store_customisation_task_index = array_search( $task_to_remove, $tasks, true );
if ( false !== $store_customisation_task_index ) {
unset( $tasks[ $store_customisation_task_index ] );
}
self::add_list(
array(
'id' => 'setup',
'title' => __( 'Get ready to start selling', 'woocommerce' ),
'tasks' => $tasks,
'display_progress_header' => true,
'event_prefix' => 'tasklist_',
'options' => array(
'use_completed_title' => true,
),
'visible' => true,
)
);
self::add_list(
array(
'id' => 'extended',
'title' => __( 'Things to do next', 'woocommerce' ),
'sort_by' => array(
array(
'key' => 'is_complete',
'order' => 'asc',
),
array(
'key' => 'level',
'order' => 'asc',
),
),
'tasks' => array(
'AdditionalPayments',
'GetMobileApp',
),
)
);
if ( Features::is_enabled( 'shipping-smart-defaults' ) ) {
self::add_task(
'extended',
new ReviewShippingOptions(
self::get_list( 'extended' )
)
);
// Tasklist that will never be shown in homescreen,
// used for having tasks that are accessed by other means.
self::add_list(
array(
'id' => 'secret_tasklist',
'hidden_id' => 'setup',
'tasks' => array(
'ExperimentalShippingRecommendation',
),
'event_prefix' => 'secret_tasklist_',
'visible' => false,
)
);
}
if ( has_filter( 'woocommerce_admin_experimental_onboarding_tasklists' ) ) {
/**
* Filter to override default task lists.
*
* @since 7.4
* @param array $lists Array of tasklists.
*/
self::$lists = apply_filters( 'woocommerce_admin_experimental_onboarding_tasklists', self::$lists );
}
}
/**
* Initialize tasks.
*/
public static function init_tasks() {
foreach ( self::DEFAULT_TASKS as $task ) {
$class = 'Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\\' . $task;
if ( ! method_exists( $class, 'init' ) ) {
continue;
}
$class::init();
}
}
/**
* Temporarily store the active task to persist across page loads when necessary.
* Most tasks do not need this.
*/
public static function set_active_task() {
if ( ! isset( $_GET[ Task::ACTIVE_TASK_TRANSIENT ] ) || ! current_user_can( 'manage_woocommerce' ) ) { // phpcs:ignore csrf ok.
return;
}
$referer = wp_get_referer();
if ( ! $referer || 0 !== strpos( $referer, wc_admin_url() ) ) {
return;
}
$task_id = sanitize_title_with_dashes( wp_unslash( $_GET[ Task::ACTIVE_TASK_TRANSIENT ] ) ); // phpcs:ignore csrf ok.
$task = self::get_task( $task_id );
if ( ! $task ) {
return;
}
$task->set_active();
}
/**
* Add a task list.
*
* @param array $args Task list properties.
* @return \WP_Error|TaskList
*/
public static function add_list( $args ) {
if ( isset( self::$lists[ $args['id'] ] ) ) {
return new \WP_Error(
'woocommerce_task_list_exists',
__( 'Task list ID already exists', 'woocommerce' )
);
}
self::$lists[ $args['id'] ] = new TaskList( $args );
return self::$lists[ $args['id'] ];
}
/**
* Add task to a given task list.
*
* @param string $list_id List ID to add the task to.
* @param Task $task Task object.
*
* @return \WP_Error|Task
*/
public static function add_task( $list_id, $task ) {
if ( ! isset( self::$lists[ $list_id ] ) ) {
return new \WP_Error(
'woocommerce_task_list_invalid_list',
__( 'Task list ID does not exist', 'woocommerce' )
);
}
self::$lists[ $list_id ]->add_task( $task );
}
/**
* Add default extended task lists.
*
* @param array $extended_tasks list of extended tasks.
*/
public static function maybe_add_extended_tasks( $extended_tasks ) {
$tasks = $extended_tasks ?? array();
foreach ( self::$lists as $task_list ) {
if ( 'extended' !== substr( $task_list->id, 0, 8 ) ) {
continue;
}
foreach ( $tasks as $args ) {
$task = new DeprecatedExtendedTask( $task_list, $args );
$task_list->add_task( $task );
}
}
}
/**
* Get all task lists.
*
* @return array
*/
public static function get_lists() {
return self::$lists;
}
/**
* Get all task lists.
*
* @param array $ids list of task list ids.
* @return array
*/
public static function get_lists_by_ids( $ids ) {
return array_filter(
self::$lists,
function( $list ) use ( $ids ) {
return in_array( $list->get_list_id(), $ids, true );
}
);
}
/**
* Get all task list ids.
*
* @return array
*/
public static function get_list_ids() {
return array_keys( self::$lists );
}
/**
* Clear all task lists.
*/
public static function clear_lists() {
self::$lists = array();
return self::$lists;
}
/**
* Get visible task lists.
*/
public static function get_visible() {
return array_filter(
self::get_lists(),
function ( $task_list ) {
return $task_list->is_visible();
}
);
}
/**
* Retrieve a task list by ID.
*
* @param String $id Task list ID.
*
* @return TaskList|null
*/
public static function get_list( $id ) {
if ( isset( self::$lists[ $id ] ) ) {
return self::$lists[ $id ];
}
return null;
}
/**
* Retrieve single task.
*
* @param String $id Task ID.
* @param String $task_list_id Task list ID.
*
* @return Object
*/
public static function get_task( $id, $task_list_id = null ) {
$task_list = $task_list_id ? self::get_list( $task_list_id ) : null;
if ( $task_list_id && ! $task_list ) {
return null;
}
$tasks_to_search = $task_list ? $task_list->tasks : array_reduce(
self::get_lists(),
function ( $all, $curr ) {
return array_merge( $all, $curr->tasks );
},
array()
);
foreach ( $tasks_to_search as $task ) {
if ( $id === $task->get_id() ) {
return $task;
}
}
return null;
}
/**
* Return number of setup tasks remaining
*
* @return number
*/
public static function setup_tasks_remaining() {
$setup_list = self::get_list( 'setup' );
if ( ! $setup_list || $setup_list->is_hidden() || $setup_list->is_complete() ) {
return;
}
$remaining_tasks = array_values(
array_filter(
$setup_list->get_viewable_tasks(),
function( $task ) {
return ! $task->is_complete();
}
)
);
return count( $remaining_tasks );
}
/**
* Add badge to homescreen menu item for remaining tasks
*/
public static function menu_task_count() {
global $submenu;
$tasks_count = self::setup_tasks_remaining();
if ( ! $tasks_count || ! isset( $submenu['woocommerce'] ) ) {
return;
}
foreach ( $submenu['woocommerce'] as $key => $menu_item ) {
if ( 0 === strpos( $menu_item[0], _x( 'Home', 'Admin menu name', 'woocommerce' ) ) ) {
$submenu['woocommerce'][ $key ][0] .= ' <span class="awaiting-mod update-plugins remaining-tasks-badge woocommerce-task-list-remaining-tasks-badge"><span class="count-' . esc_attr( $tasks_count ) . '">' . absint( $tasks_count ) . '</span></span>'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
break;
}
}
}
/**
* Add visible list ids to component settings.
*
* @param array $settings Component settings.
*
* @return array
*/
public static function task_list_preloaded_settings( $settings ) {
$settings['visibleTaskListIds'] = array_keys( self::get_visible() );
return $settings;
}
}
Features/OnboardingTasks/TaskTraits.php 0000644 00000001713 15153746750 0014233 0 ustar 00 <?php
/**
* Task and TaskList Traits
*/
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks;
defined( 'ABSPATH' ) || exit;
/**
* TaskTraits class.
*/
trait TaskTraits {
/**
* Record a tracks event with the prefixed event name.
*
* @param string $event_name Event name.
* @param array $args Array of tracks arguments.
* @return string Prefixed event name.
*/
public function record_tracks_event( $event_name, $args = array() ) {
if ( ! $this->get_list_id() ) {
return;
}
$prefixed_event_name = $this->prefix_event( $event_name );
wc_admin_record_tracks_event(
$prefixed_event_name,
$args
);
return $prefixed_event_name;
}
/**
* Get the task list ID.
*
* @return string
*/
public function get_list_id() {
$namespaced_class = get_class( $this );
return is_subclass_of( $namespaced_class, 'Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task' )
? $this->get_parent_id()
: $this->id;
}
}
Features/OnboardingTasks/Tasks/AdditionalPayments.php 0000644 00000011066 15153746750 0017022 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\Payments;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\WooCommercePayments;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\Init;
/**
* Payments Task
*/
class AdditionalPayments extends Payments {
/**
* Used to cache is_complete() method result.
*
* @var null
*/
private $is_complete_result = null;
/**
* Used to cache can_view() method result.
*
* @var null
*/
private $can_view_result = null;
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'payments';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
return __(
'Set up additional payment options',
'woocommerce'
);
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return __(
'Choose payment providers and enable payment methods at checkout.',
'woocommerce'
);
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return __( '2 minutes', 'woocommerce' );
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
if ( null === $this->is_complete_result ) {
$this->is_complete_result = self::has_enabled_additional_gateways();
}
return $this->is_complete_result;
}
/**
* Task visibility.
*
* @return bool
*/
public function can_view() {
if ( ! Features::is_enabled( 'payment-gateway-suggestions' ) ) {
// Hide task if feature not enabled.
return false;
}
if ( null !== $this->can_view_result ) {
return $this->can_view_result;
}
// Show task if woocommerce-payments is connected or if there are any suggested gateways in other category enabled.
$this->can_view_result = (
WooCommercePayments::is_connected() ||
self::has_enabled_other_category_gateways()
);
// Early return if task is not visible.
if ( ! $this->can_view_result ) {
return false;
}
// Show task if there are any suggested gateways in additional category.
$this->can_view_result = ! empty( self::get_suggestion_gateways( 'category_additional' ) );
return $this->can_view_result;
}
/**
* Check if the store has any enabled gateways in other category.
*
* @return bool
*/
private static function has_enabled_other_category_gateways() {
$other_gateways = self::get_suggestion_gateways( 'category_other' );
$other_gateways_ids = wp_list_pluck( $other_gateways, 'id' );
return self::has_enabled_gateways(
function( $gateway ) use ( $other_gateways_ids ) {
return in_array( $gateway->id, $other_gateways_ids, true );
}
);
}
/**
* Check if the store has any enabled gateways in additional category.
*
* @return bool
*/
private static function has_enabled_additional_gateways() {
$additional_gateways = self::get_suggestion_gateways( 'category_additional' );
$additional_gateways_ids = wp_list_pluck( $additional_gateways, 'id' );
return self::has_enabled_gateways(
function( $gateway ) use ( $additional_gateways_ids ) {
return 'yes' === $gateway->enabled
&& in_array( $gateway->id, $additional_gateways_ids, true );
}
);
}
/**
* Check if the store has any enabled gateways based on the given criteria.
*
* @param callable|null $filter A callback function to filter the gateways.
* @return bool
*/
private static function has_enabled_gateways( $filter = null ) {
$gateways = WC()->payment_gateways->get_available_payment_gateways();
$enabled_gateways = array_filter(
$gateways,
function( $gateway ) use ( $filter ) {
if ( is_callable( $filter ) ) {
return 'yes' === $gateway->enabled && call_user_func( $filter, $gateway );
} else {
return 'yes' === $gateway->enabled;
}
}
);
return ! empty( $enabled_gateways );
}
/**
* Get the list of gateways to suggest.
*
* @param string $filter_by Filter by category. "category_additional" or "category_other".
*
* @return array
*/
private static function get_suggestion_gateways( $filter_by = 'category_additional' ) {
$country = wc_get_base_location()['country'];
$plugin_suggestions = Init::get_suggestions();
$plugin_suggestions = array_filter(
$plugin_suggestions,
function( $plugin ) use ( $country, $filter_by ) {
if ( ! isset( $plugin->{$filter_by} ) || ! isset( $plugin->plugins[0] ) ) {
return false;
}
return in_array( $country, $plugin->{$filter_by}, true );
}
);
return $plugin_suggestions;
}
}
Features/OnboardingTasks/Tasks/Appearance.php 0000644 00000002513 15153746750 0015265 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Internal\Admin\Loader;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\Products;
use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
/**
* Appearance Task
*/
class Appearance extends Task {
/**
* Constructor.
*/
public function __construct() {
if ( ! $this->is_complete() ) {
add_action( 'load-theme-install.php', array( $this, 'mark_actioned' ) );
}
}
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'appearance';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
return __( 'Choose your theme', 'woocommerce' );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return __(
"Choose a theme that best fits your brand's look and feel, then make it your own. Change the colors, add your logo, and create pages.",
'woocommerce'
);
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return __( '2 minutes', 'woocommerce' );
}
/**
* Action label.
*
* @return string
*/
public function get_action_label() {
return __( 'Choose theme', 'woocommerce' );
}
}
Features/OnboardingTasks/Tasks/CustomizeStore.php 0000644 00000013237 15153746750 0016232 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use Jetpack_Gutenberg;
/**
* Customize Your Store Task
*/
class CustomizeStore extends Task {
/**
* Constructor
*
* @param TaskList $task_list Parent task list.
*/
public function __construct( $task_list ) {
parent::__construct( $task_list );
add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_site_editor_scripts' ) );
add_action( 'after_switch_theme', array( $this, 'mark_task_as_complete' ) );
}
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'customize-store';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
return __( 'Customize your store ', 'woocommerce' );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return '';
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return '';
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
return get_option( 'woocommerce_admin_customize_store_completed' ) === 'yes';
}
/**
* Task visibility.
*
* @return bool
*/
public function can_view() {
return true;
}
/**
* Possibly add site editor scripts.
*/
public function possibly_add_site_editor_scripts() {
$is_customize_store_pages = (
isset( $_GET['page'] ) &&
'wc-admin' === $_GET['page'] &&
isset( $_GET['path'] ) &&
str_starts_with( wc_clean( wp_unslash( $_GET['path'] ) ), '/customize-store' )
);
if ( ! $is_customize_store_pages ) {
return;
}
// See: https://github.com/WordPress/WordPress/blob/master/wp-admin/site-editor.php.
if ( ! wp_is_block_theme() ) {
wp_die( esc_html__( 'The theme you are currently using is not compatible.', 'woocommerce' ) );
}
global $editor_styles;
// Flag that we're loading the block editor.
$current_screen = get_current_screen();
$current_screen->is_block_editor( true );
// Default to is-fullscreen-mode to avoid jumps in the UI.
add_filter(
'admin_body_class',
static function( $classes ) {
return "$classes is-fullscreen-mode";
}
);
$block_editor_context = new \WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
$indexed_template_types = array();
foreach ( get_default_block_template_types() as $slug => $template_type ) {
$template_type['slug'] = (string) $slug;
$indexed_template_types[] = $template_type;
}
$custom_settings = array(
'siteUrl' => site_url(),
'postsPerPage' => get_option( 'posts_per_page' ),
'styles' => get_block_editor_theme_styles(),
'defaultTemplateTypes' => $indexed_template_types,
'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(),
'supportsLayout' => wp_theme_has_theme_json(),
'supportsTemplatePartsMode' => ! wp_is_block_theme() && current_theme_supports( 'block-template-parts' ),
);
// Add additional back-compat patterns registered by `current_screen` et al.
$custom_settings['__experimentalAdditionalBlockPatterns'] = \WP_Block_Patterns_Registry::get_instance()->get_all_registered( true );
$custom_settings['__experimentalAdditionalBlockPatternCategories'] = \WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true );
$editor_settings = get_block_editor_settings( $custom_settings, $block_editor_context );
$active_global_styles_id = \WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
$active_theme = get_stylesheet();
$preload_paths = array(
array( '/wp/v2/media', 'OPTIONS' ),
'/wp/v2/types?context=view',
'/wp/v2/types/wp_template?context=edit',
'/wp/v2/types/wp_template-part?context=edit',
'/wp/v2/templates?context=edit&per_page=-1',
'/wp/v2/template-parts?context=edit&per_page=-1',
'/wp/v2/themes?context=edit&status=active',
'/wp/v2/global-styles/' . $active_global_styles_id . '?context=edit',
'/wp/v2/global-styles/' . $active_global_styles_id,
'/wp/v2/global-styles/themes/' . $active_theme,
);
block_editor_rest_api_preload( $preload_paths, $block_editor_context );
wp_add_inline_script(
'wp-blocks',
sprintf(
'window.wcBlockSettings = %s;',
wp_json_encode( $editor_settings )
)
);
// Preload server-registered block schemas.
wp_add_inline_script(
'wp-blocks',
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
);
wp_add_inline_script(
'wp-blocks',
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( isset( $editor_settings['blockCategories'] ) ? $editor_settings['blockCategories'] : array() ) ),
'after'
);
wp_enqueue_script( 'wp-editor' );
wp_enqueue_script( 'wp-format-library' ); // Not sure if this is needed.
wp_enqueue_script( 'wp-router' );
wp_enqueue_style( 'wp-editor' );
wp_enqueue_style( 'wp-edit-site' );
wp_enqueue_style( 'wp-format-library' );
wp_enqueue_media();
if (
current_theme_supports( 'wp-block-styles' ) &&
( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 )
) {
wp_enqueue_style( 'wp-block-library-theme' );
}
/** This action is documented in wp-admin/edit-form-blocks.php
*
* @since 8.0.3
*/
do_action( 'enqueue_block_editor_assets' );
// Load Jetpack's block editor assets because they are not enqueued by default.
if ( class_exists( 'Jetpack_Gutenberg' ) ) {
Jetpack_Gutenberg::enqueue_block_editor_assets();
}
}
/**
* Mark task as complete.
*/
public function mark_task_as_complete() {
update_option( 'woocommerce_admin_customize_store_completed', 'yes' );
}
}
Features/OnboardingTasks/Tasks/ExperimentalShippingRecommendation.php 0000644 00000003216 15153746750 0022253 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use Automattic\WooCommerce\Admin\PluginsHelper;
/**
* Shipping Task
*/
class ExperimentalShippingRecommendation extends Task {
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'shipping-recommendation';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
return __( 'Set up shipping', 'woocommerce' );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return '';
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return '';
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
return self::has_plugins_active() && self::has_jetpack_connected();
}
/**
* Task visibility.
*
* @return bool
*/
public function can_view() {
return Features::is_enabled( 'shipping-smart-defaults' );
}
/**
* Action URL.
*
* @return string
*/
public function get_action_url() {
return '';
}
/**
* Check if the store has any shipping zones.
*
* @return bool
*/
public static function has_plugins_active() {
return PluginsHelper::is_plugin_active( 'woocommerce-services' ) &&
PluginsHelper::is_plugin_active( 'jetpack' );
}
/**
* Check if the Jetpack is connected.
*
* @return bool
*/
public static function has_jetpack_connected() {
if ( class_exists( '\Jetpack' ) && is_callable( '\Jetpack::is_connection_ready' ) ) {
return \Jetpack::is_connection_ready();
}
return false;
}
}
Features/OnboardingTasks/Tasks/GetMobileApp.php 0000644 00000005014 15153746750 0015535 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use Automattic\Jetpack\Connection\Manager; // https://github.com/Automattic/jetpack/blob/trunk/projects/packages/connection/src/class-manager.php .
/**
* Get Mobile App Task
*/
class GetMobileApp extends Task {
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'get-mobile-app';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
return __( 'Get the free WooCommerce mobile app', 'woocommerce' );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return '';
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return '';
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
return get_option( 'woocommerce_admin_dismissed_mobile_app_modal' ) === 'yes';
}
/**
* Task visibility.
* Can view under these conditions:
* - Jetpack is installed and connected && current site user has a wordpress.com account connected to jetpack
* - Jetpack is not connected && current user is capable of installing plugins
*
* @return bool
*/
public function can_view() {
$jetpack_can_be_installed = current_user_can( 'manage_woocommerce' ) && current_user_can( 'install_plugins' ) && ! self::is_jetpack_connected();
$jetpack_is_installed_and_current_user_connected = self::is_current_user_connected();
return $jetpack_can_be_installed || $jetpack_is_installed_and_current_user_connected;
}
/**
* Determines if site has any users connected to WordPress.com via JetPack
*
* @return bool
*/
private static function is_jetpack_connected() {
if ( class_exists( '\Automattic\Jetpack\Connection\Manager' ) && method_exists( '\Automattic\Jetpack\Connection\Manager', 'is_active' ) ) {
$connection = new Manager();
return $connection->is_active();
}
return false;
}
/**
* Determines if the current user is connected to Jetpack.
*
* @return bool
*/
private static function is_current_user_connected() {
if ( class_exists( '\Automattic\Jetpack\Connection\Manager' ) && method_exists( '\Automattic\Jetpack\Connection\Manager', 'is_user_connected' ) ) {
$connection = new Manager();
return $connection->is_connection_owner();
}
return false;
}
/**
* Action URL.
*
* @return string
*/
public function get_action_url() {
return admin_url( 'admin.php?page=wc-admin&mobileAppModal=true' );
}
}
Features/OnboardingTasks/Tasks/Marketing.php 0000644 00000004736 15153746750 0015160 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\Init as RemoteFreeExtensions;
/**
* Marketing Task
*/
class Marketing extends Task {
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'marketing';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
if ( true === $this->get_parent_option( 'use_completed_title' ) ) {
if ( $this->is_complete() ) {
return __( 'You added sales channels', 'woocommerce' );
}
return __( 'Get more sales', 'woocommerce' );
}
return __( 'Set up marketing tools', 'woocommerce' );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return __(
'Add recommended marketing tools to reach new customers and grow your business',
'woocommerce'
);
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return __( '2 minutes', 'woocommerce' );
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
return self::has_installed_extensions();
}
/**
* Task visibility.
*
* @return bool
*/
public function can_view() {
return Features::is_enabled( 'remote-free-extensions' ) && count( self::get_plugins() ) > 0;
}
/**
* Get the marketing plugins.
*
* @return array
*/
public static function get_plugins() {
$bundles = RemoteFreeExtensions::get_extensions(
array(
'task-list/reach',
'task-list/grow',
)
);
return array_reduce(
$bundles,
function( $plugins, $bundle ) {
$visible = array();
foreach ( $bundle['plugins'] as $plugin ) {
if ( $plugin->is_visible ) {
$visible[] = $plugin;
}
}
return array_merge( $plugins, $visible );
},
array()
);
}
/**
* Check if the store has installed marketing extensions.
*
* @return bool
*/
public static function has_installed_extensions() {
$plugins = self::get_plugins();
$remaining = array();
$installed = array();
foreach ( $plugins as $plugin ) {
if ( ! $plugin->is_installed ) {
$remaining[] = $plugin;
} else {
$installed[] = $plugin;
}
}
// Make sure the task has been actioned and a marketing extension has been installed.
if ( count( $installed ) > 0 && Task::is_task_actioned( 'marketing' ) ) {
return true;
}
return false;
}
}
Features/OnboardingTasks/Tasks/Payments.php 0000644 00000003746 15153746750 0015037 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
/**
* Payments Task
*/
class Payments extends Task {
/**
* Used to cache is_complete() method result.
* @var null
*/
private $is_complete_result = null;
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'payments';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
if ( true === $this->get_parent_option( 'use_completed_title' ) ) {
if ( $this->is_complete() ) {
return __( 'You set up payments', 'woocommerce' );
}
return __( 'Set up payments', 'woocommerce' );
}
return __( 'Set up payments', 'woocommerce' );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return __(
'Choose payment providers and enable payment methods at checkout.',
'woocommerce'
);
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return __( '2 minutes', 'woocommerce' );
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
if ( $this->is_complete_result === null ) {
$this->is_complete_result = self::has_gateways();
}
return $this->is_complete_result;
}
/**
* Task visibility.
*
* @return bool
*/
public function can_view() {
$woocommerce_payments = $this->task_list->get_task( 'woocommerce-payments' );
return Features::is_enabled( 'payment-gateway-suggestions' ) && ! $woocommerce_payments->can_view();
}
/**
* Check if the store has any enabled gateways.
*
* @return bool
*/
public static function has_gateways() {
$gateways = WC()->payment_gateways->get_available_payment_gateways();
$enabled_gateways = array_filter(
$gateways,
function( $gateway ) {
return 'yes' === $gateway->enabled && 'woocommerce_payments' !== $gateway->id;
}
);
return ! empty( $enabled_gateways );
}
}
Features/OnboardingTasks/Tasks/Products.php 0000644 00000007634 15153746750 0015042 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
/**
* Products Task
*/
class Products extends Task {
/**
* Constructor
*
* @param TaskList $task_list Parent task list.
*/
public function __construct( $task_list ) {
parent::__construct( $task_list );
add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_manual_return_notice_script' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_import_return_notice_script' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_load_sample_return_notice_script' ) );
}
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'products';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
if ( $this->get_parent_option( 'use_completed_title' ) === true ) {
if ( $this->is_complete() ) {
return __( 'You added products', 'woocommerce' );
}
return __( 'Add products', 'woocommerce' );
}
return __( 'Add my products', 'woocommerce' );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return __(
'Start by adding the first product to your store. You can add your products manually, via CSV, or import them from another service.',
'woocommerce'
);
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return __( '1 minute per product', 'woocommerce' );
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
return self::has_products();
}
/**
* Addtional data.
*
* @return array
*/
public function get_additional_data() {
return array(
'has_products' => self::has_products(),
);
}
/**
* Adds a return to task list notice when completing the manual product task.
*
* @param string $hook Page hook.
*/
public function possibly_add_manual_return_notice_script( $hook ) {
global $post;
if ( $hook !== 'post.php' || $post->post_type !== 'product' ) {
return;
}
if ( ! $this->is_active() || ! $this->is_complete() ) {
return;
}
WCAdminAssets::register_script( 'wp-admin-scripts', 'onboarding-product-notice', true );
// Clear the active task transient to only show notice once per active session.
delete_transient( self::ACTIVE_TASK_TRANSIENT );
}
/**
* Adds a return to task list notice when completing the import product task.
*
* @param string $hook Page hook.
*/
public function possibly_add_import_return_notice_script( $hook ) {
$step = isset( $_GET['step'] ) ? $_GET['step'] : ''; // phpcs:ignore csrf ok, sanitization ok.
if ( $hook !== 'product_page_product_importer' || $step !== 'done' ) {
return;
}
if ( ! $this->is_active() || $this->is_complete() ) {
return;
}
WCAdminAssets::register_script( 'wp-admin-scripts', 'onboarding-product-import-notice', true );
}
/**
* Adds a return to task list notice when completing the loading sample products action.
*
* @param string $hook Page hook.
*/
public function possibly_add_load_sample_return_notice_script( $hook ) {
if ( $hook !== 'edit.php' || get_query_var( 'post_type' ) !== 'product' ) {
return;
}
$referer = wp_get_referer();
if ( ! $referer || strpos( $referer, wc_admin_url() ) !== 0 ) {
return;
}
if ( ! isset( $_GET[ Task::ACTIVE_TASK_TRANSIENT ] ) ) {
return;
}
$task_id = sanitize_title_with_dashes( wp_unslash( $_GET[ Task::ACTIVE_TASK_TRANSIENT ] ) );
if ( $task_id !== $this->get_id() || ! $this->is_complete() ) {
return;
}
WCAdminAssets::register_script( 'wp-admin-scripts', 'onboarding-load-sample-products-notice', true );
}
/**
* Check if the store has any published products.
*
* @return bool
*/
public static function has_products() {
$counts = wp_count_posts('product');
return isset( $counts->publish ) && $counts->publish > 0;
}
}
Features/OnboardingTasks/Tasks/Purchase.php 0000644 00000012127 15153746750 0015002 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProducts;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingThemes;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
/**
* Purchase Task
*/
class Purchase extends Task {
/**
* Constructor
*
* @param TaskList $task_list Parent task list.
*/
public function __construct( $task_list ) {
parent::__construct( $task_list );
add_action( 'update_option_woocommerce_onboarding_profile', array( $this, 'clear_dismissal' ), 10, 2 );
}
/**
* Clear dismissal on onboarding product type changes.
*
* @param array $old_value Old value.
* @param array $new_value New value.
*/
public function clear_dismissal( $old_value, $new_value ) {
$product_types = isset( $new_value['product_types'] ) ? (array) $new_value['product_types'] : array();
$previous_product_types = isset( $old_value['product_types'] ) ? (array) $old_value['product_types'] : array();
if ( empty( array_diff( $product_types, $previous_product_types ) ) ) {
return;
}
$this->undo_dismiss();
}
/**
* Get the task arguments.
* ID.
*
* @return string
*/
public function get_id() {
return 'purchase';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
$products = $this->get_paid_products_and_themes();
$first_product = count( $products['purchaseable'] ) >= 1 ? $products['purchaseable'][0] : false;
if ( ! $first_product ) {
return null;
}
$product_label = isset( $first_product['label'] ) ? $first_product['label'] : $first_product['title'];
$additional_count = count( $products['purchaseable'] ) - 1;
if ( $this->get_parent_option( 'use_completed_title' ) && $this->is_complete() ) {
return count( $products['purchaseable'] ) === 1
? sprintf(
/* translators: %1$s: a purchased product name */
__(
'You added %1$s',
'woocommerce'
),
$product_label
)
: sprintf(
/* translators: %1$s: a purchased product name, %2$d the number of other products purchased */
_n(
'You added %1$s and %2$d other product',
'You added %1$s and %2$d other products',
$additional_count,
'woocommerce'
),
$product_label,
$additional_count
);
}
return count( $products['purchaseable'] ) === 1
? sprintf(
/* translators: %1$s: a purchaseable product name */
__(
'Add %s to my store',
'woocommerce'
),
$product_label
)
: sprintf(
/* translators: %1$s: a purchaseable product name, %2$d the number of other products to purchase */
_n(
'Add %1$s and %2$d more product to my store',
'Add %1$s and %2$d more products to my store',
$additional_count,
'woocommerce'
),
$product_label,
$additional_count
);
}
/**
* Content.
*
* @return string
*/
public function get_content() {
$products = $this->get_paid_products_and_themes();
if ( count( $products['remaining'] ) === 1 ) {
return isset( $products['purchaseable'][0]['description'] ) ? $products['purchaseable'][0]['description'] : $products['purchaseable'][0]['excerpt'];
}
return sprintf(
/* translators: %1$s: list of product names comma separated, %2%s the last product name */
__(
'Good choice! You chose to add %1$s and %2$s to your store.',
'woocommerce'
),
implode( ', ', array_slice( $products['remaining'], 0, -1 ) ) . ( count( $products['remaining'] ) > 2 ? ',' : '' ),
end( $products['remaining'] )
);
}
/**
* Action label.
*
* @return string
*/
public function get_action_label() {
return __( 'Purchase & install now', 'woocommerce' );
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return __( '2 minutes', 'woocommerce' );
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
$products = $this->get_paid_products_and_themes();
return count( $products['remaining'] ) === 0;
}
/**
* Dismissable.
*
* @return bool
*/
public function is_dismissable() {
return true;
}
/**
* Task visibility.
*
* @return bool
*/
public function can_view() {
$products = $this->get_paid_products_and_themes();
return count( $products['purchaseable'] ) > 0;
}
/**
* Get purchaseable and remaining products.
*
* @return array purchaseable and remaining products and themes.
*/
public static function get_paid_products_and_themes() {
$relevant_products = OnboardingProducts::get_relevant_products();
$profiler_data = get_option( OnboardingProfile::DATA_OPTION, array() );
$theme = isset( $profiler_data['theme'] ) ? $profiler_data['theme'] : null;
$paid_theme = $theme ? OnboardingThemes::get_paid_theme_by_slug( $theme ) : null;
if ( $paid_theme ) {
$relevant_products['purchaseable'][] = $paid_theme;
if ( isset( $paid_theme['is_installed'] ) && false === $paid_theme['is_installed'] ) {
$relevant_products['remaining'][] = $paid_theme['title'];
}
}
return $relevant_products;
}
}
Features/OnboardingTasks/Tasks/ReviewShippingOptions.php 0000644 00000002177 15153746750 0017553 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
/**
* Review Shipping Options Task
*/
class ReviewShippingOptions extends Task {
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'review-shipping';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
return __( 'Review shipping options', 'woocommerce' );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return '';
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return '';
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
return get_option( 'woocommerce_admin_reviewed_default_shipping_zones' ) === 'yes';
}
/**
* Task visibility.
*
* @return bool
*/
public function can_view() {
return get_option( 'woocommerce_admin_created_default_shipping_zones' ) === 'yes';
}
/**
* Action URL.
*
* @return string
*/
public function get_action_url() {
return admin_url( 'admin.php?page=wc-settings&tab=shipping' );
}
}
Features/OnboardingTasks/Tasks/Shipping.php 0000644 00000012244 15153746750 0015011 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use WC_Data_Store;
/**
* Shipping Task
*/
class Shipping extends Task {
const ZONE_COUNT_TRANSIENT_NAME = 'woocommerce_shipping_task_zone_count_transient';
/**
* Constructor
*
* @param TaskList $task_list Parent task list.
*/
public function __construct( $task_list = null ) {
parent::__construct( $task_list );
// wp_ajax_woocommerce_shipping_zone_methods_save_changes
// and wp_ajax_woocommerce_shipping_zones_save_changes get fired
// when a new zone is added or an existing one has been changed.
add_action( 'wp_ajax_woocommerce_shipping_zones_save_changes', array( __CLASS__, 'delete_zone_count_transient' ), 9 );
add_action( 'wp_ajax_woocommerce_shipping_zone_methods_save_changes', array( __CLASS__, 'delete_zone_count_transient' ), 9 );
add_action( 'woocommerce_shipping_zone_method_added', array( __CLASS__, 'delete_zone_count_transient' ), 9 );
add_action( 'woocommerce_after_shipping_zone_object_save', array( __CLASS__, 'delete_zone_count_transient' ), 9 );
}
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'shipping';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
if ( true === $this->get_parent_option( 'use_completed_title' ) ) {
if ( $this->is_complete() ) {
return __( 'You added shipping costs', 'woocommerce' );
}
return __( 'Add shipping costs', 'woocommerce' );
}
return __( 'Set up shipping', 'woocommerce' );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return __(
"Set your store location and where you'll ship to.",
'woocommerce'
);
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return __( '1 minute', 'woocommerce' );
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
return self::has_shipping_zones();
}
/**
* Task visibility.
*
* @return bool
*/
public function can_view() {
if ( Features::is_enabled( 'shipping-smart-defaults' ) ) {
if ( 'yes' === get_option( 'woocommerce_admin_created_default_shipping_zones' ) ) {
// If the user has already created a default shipping zone, we don't need to show the task.
return false;
}
/**
* Do not display the task when:
* - The store sells digital products only
* Display the task when:
* - We don't know where the store's located
* - The store is located in the UK, Australia or Canada
*/
if ( self::is_selling_digital_type_only() ) {
return false;
}
$default_store_country = wc_format_country_state_string( get_option( 'woocommerce_default_country', '' ) )['country'];
// Check if a store address is set so that we don't default to WooCommerce's default country US.
// Similar logic: https://github.com/woocommerce/woocommerce/blob/059d542394b48468587f252dcb6941c6425cd8d3/plugins/woocommerce-admin/client/profile-wizard/steps/store-details/index.js#L511-L516.
$store_country = '';
if ( ! empty( get_option( 'woocommerce_store_address', '' ) ) || 'US' !== $default_store_country ) {
$store_country = $default_store_country;
}
// Unknown country.
if ( empty( $store_country ) ) {
return true;
}
return in_array( $store_country, array( 'CA', 'AU', 'GB', 'ES', 'IT', 'DE', 'FR', 'MX', 'CO', 'CL', 'AR', 'PE', 'BR', 'UY', 'GT', 'NL', 'AT', 'BE' ), true );
}
return self::has_physical_products();
}
/**
* Action URL.
*
* @return string
*/
public function get_action_url() {
return self::has_shipping_zones()
? admin_url( 'admin.php?page=wc-settings&tab=shipping' )
: null;
}
/**
* Check if the store has any shipping zones.
*
* @return bool
*/
public static function has_shipping_zones() {
$zone_count = get_transient( self::ZONE_COUNT_TRANSIENT_NAME );
if ( false !== $zone_count ) {
return (int) $zone_count > 0;
}
$zone_count = count( WC_Data_Store::load( 'shipping-zone' )->get_zones() );
set_transient( self::ZONE_COUNT_TRANSIENT_NAME, $zone_count );
return $zone_count > 0;
}
/**
* Check if the store has physical products.
*
* @return bool
*/
public static function has_physical_products() {
$profiler_data = get_option( OnboardingProfile::DATA_OPTION, array() );
$product_types = isset( $profiler_data['product_types'] ) ? $profiler_data['product_types'] : array();
return in_array( 'physical', $product_types, true );
}
/**
* Delete the zone count transient used in has_shipping_zones() method
* to refresh the cache.
*/
public static function delete_zone_count_transient() {
delete_transient( self::ZONE_COUNT_TRANSIENT_NAME );
}
/**
* Check if the store sells digital products only.
*
* @return bool
*/
private static function is_selling_digital_type_only() {
$profiler_data = get_option( OnboardingProfile::DATA_OPTION, array() );
$product_types = isset( $profiler_data['product_types'] ) ? $profiler_data['product_types'] : array();
return array( 'downloads' ) === $product_types;
}
}
Features/OnboardingTasks/Tasks/StoreCreation.php 0000644 00000002044 15153746750 0016006 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\Onboarding;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
/**
* Store Details Task
*/
class StoreCreation extends Task {
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'store_creation';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
/* translators: Store name */
return sprintf( __( 'You created %s', 'woocommerce' ), get_bloginfo( 'name' ) );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return '';
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return '';
}
/**
* Time.
*
* @return string
*/
public function get_action_url() {
return '';
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
return true;
}
/**
* Check if task is disabled.
*
* @return bool
*/
public function is_disabled() {
return true;
}
}
Features/OnboardingTasks/Tasks/StoreDetails.php 0000644 00000004207 15153746750 0015632 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
/**
* Store Details Task
*/
class StoreDetails extends Task {
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'store_details';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
if ( true === $this->get_parent_option( 'use_completed_title' ) ) {
if ( $this->is_complete() ) {
return __( 'You added store details', 'woocommerce' );
}
return __( 'Add store details', 'woocommerce' );
}
return __( 'Store details', 'woocommerce' );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return __(
'Your store address is required to set the origin country for shipping, currencies, and payment options.',
'woocommerce'
);
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return __( '4 minutes', 'woocommerce' );
}
/**
* Time.
*
* @return string
*/
public function get_action_url() {
return ! $this->is_complete() ? admin_url( 'admin.php?page=wc-settings&tab=general&tutorial=true' ) : admin_url( 'admin.php?page=wc-settings&tab=general' );
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
$country = WC()->countries->get_base_country();
$country_locale = WC()->countries->get_country_locale();
$locale = $country_locale[ $country ] ?? array();
$hide_postcode = $locale['postcode']['hidden'] ?? false;
// If postcode is hidden, just check that the store address and city are set.
if ( $hide_postcode ) {
return get_option( 'woocommerce_store_address', '' ) !== '' && get_option( 'woocommerce_store_city', '' ) !== '';
}
// Mark as completed if the store address, city and postcode are set. We don't need to check the country because it's set by default.
return get_option( 'woocommerce_store_address', '' ) !== '' && get_option( 'woocommerce_store_city', '' ) !== '' &&
get_option( 'woocommerce_store_postcode', '' ) !== '';
}
}
Features/OnboardingTasks/Tasks/Tax.php 0000644 00000010045 15153746750 0013761 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\DataStore as TaxDataStore;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use Automattic\WooCommerce\Admin\PluginsHelper;
use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
/**
* Tax Task
*/
class Tax extends Task {
/**
* Used to cache is_complete() method result.
* @var null
*/
private $is_complete_result = null;
/**
* Constructor
*
* @param TaskList $task_list Parent task list.
*/
public function __construct( $task_list ) {
parent::__construct( $task_list );
add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_return_notice_script' ) );
}
/**
* Adds a return to task list notice when completing the task.
*/
public function possibly_add_return_notice_script() {
$page = isset( $_GET['page'] ) ? $_GET['page'] : ''; // phpcs:ignore csrf ok, sanitization ok.
$tab = isset( $_GET['tab'] ) ? $_GET['tab'] : ''; // phpcs:ignore csrf ok, sanitization ok.
if ( $page !== 'wc-settings' || $tab !== 'tax' ) {
return;
}
if ( ! $this->is_active() || $this->is_complete() ) {
return;
}
WCAdminAssets::register_script( 'wp-admin-scripts', 'onboarding-tax-notice', true );
}
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'tax';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
if ( $this->get_parent_option( 'use_completed_title' ) === true ) {
if ( $this->is_complete() ) {
return __( 'You added tax rates', 'woocommerce' );
}
return __( 'Add tax rates', 'woocommerce' );
}
return __( 'Set up tax rates', 'woocommerce' );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return self::can_use_automated_taxes()
? __(
'Good news! WooCommerce Services and Jetpack can automate your sales tax calculations for you.',
'woocommerce'
)
: __(
'Set your store location and configure tax rate settings.',
'woocommerce'
);
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return __( '1 minute', 'woocommerce' );
}
/**
* Action label.
*
* @return string
*/
public function get_action_label() {
return self::can_use_automated_taxes()
? __( 'Yes please', 'woocommerce' )
: __( "Let's go", 'woocommerce' );
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
if ( $this->is_complete_result === null ) {
$wc_connect_taxes_enabled = get_option( 'wc_connect_taxes_enabled' );
$is_wc_connect_taxes_enabled = ( $wc_connect_taxes_enabled === 'yes' ) || ( $wc_connect_taxes_enabled === true ); // seems that in some places boolean is used, and other places 'yes' | 'no' is used
$this->is_complete_result = $is_wc_connect_taxes_enabled ||
count( TaxDataStore::get_taxes( array() ) ) > 0 ||
get_option( 'woocommerce_no_sales_tax' ) !== false;
}
return $this->is_complete_result;
}
/**
* Addtional data.
*
* @return array
*/
public function get_additional_data() {
return array(
'avalara_activated' => PluginsHelper::is_plugin_active( 'woocommerce-avatax' ),
'tax_jar_activated' => class_exists( 'WC_Taxjar' ),
'woocommerce_tax_countries' => self::get_automated_support_countries(),
);
}
/**
* Check if the store has any enabled gateways.
*
* @return bool
*/
public static function can_use_automated_taxes() {
if ( ! class_exists( 'WC_Taxjar' ) ) {
return false;
}
return in_array( WC()->countries->get_base_country(), self::get_automated_support_countries(), true );
}
/**
* Get an array of countries that support automated tax.
*
* @return array
*/
public static function get_automated_support_countries() {
// https://developers.taxjar.com/api/reference/#countries .
$tax_supported_countries = array_merge(
array( 'US', 'CA', 'AU', 'GB' ),
WC()->countries->get_european_union_countries()
);
return $tax_supported_countries;
}
}
Features/OnboardingTasks/Tasks/TourInAppMarketplace.php 0000644 00000002277 15153746750 0017267 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
/**
* Tour In-App Marketplace task
*/
class TourInAppMarketplace extends Task {
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'tour-in-app-marketplace';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
return __(
'Discover ways of extending your store with a tour of the Woo Marketplace',
'woocommerce'
);
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return '';
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return '';
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
return get_option( 'woocommerce_admin_dismissed_in_app_marketplace_tour' ) === 'yes';
}
/**
* Action URL.
*
* @return string
*/
public function get_action_url() {
return admin_url( 'admin.php?page=wc-admin&path=%2Fextensions&tutorial=true' );
}
/**
* Check if should record event when task is viewed
*
* @return bool
*/
public function get_record_view_event(): bool {
return true;
}
}
Features/OnboardingTasks/Tasks/WooCommercePayments.php 0000644 00000011433 15153746750 0017167 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use Automattic\WooCommerce\Admin\PluginsHelper;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\Init as Suggestions;
use Automattic\WooCommerce\Internal\Admin\WCPayPromotion\Init as WCPayPromotionInit;
/**
* WooCommercePayments Task
*/
class WooCommercePayments extends Task {
/**
* Used to cache is_complete() method result.
*
* @var null
*/
private $is_complete_result = null;
/**
* ID.
*
* @return string
*/
public function get_id() {
return 'woocommerce-payments';
}
/**
* Title.
*
* @return string
*/
public function get_title() {
return __( 'Set up WooPayments', 'woocommerce' );
}
/**
* Badge.
*
* @return string
*/
public function get_badge() {
/**
* Filter WooPayments onboarding task badge.
*
* @param string $badge Badge content.
* @since 8.2.0
*/
return apply_filters( 'woocommerce_admin_woopayments_onboarding_task_badge', '' );
}
/**
* Content.
*
* @return string
*/
public function get_content() {
return __(
"You're only one step away from getting paid. Verify your business details to start managing transactions with WooPayments.",
'woocommerce'
);
}
/**
* Time.
*
* @return string
*/
public function get_time() {
return __( '2 minutes', 'woocommerce' );
}
/**
* Action label.
*
* @return string
*/
public function get_action_label() {
return __( 'Finish setup', 'woocommerce' );
}
/**
* Additional info.
*
* @return string
*/
public function get_additional_info() {
if ( WCPayPromotionInit::is_woopay_eligible() ) {
return __(
'By using WooPayments you agree to be bound by our <a href="https://wordpress.com/tos/" target="_blank">Terms of Service</a> (including WooPay <a href="https://wordpress.com/tos/#more-woopay-specifically" target="_blank">merchant terms</a>) and acknowledge that you have read our <a href="https://automattic.com/privacy/" target="_blank">Privacy Policy</a>',
'woocommerce'
);
}
return __(
'By using WooPayments you agree to be bound by our <a href="https://wordpress.com/tos/" target="_blank">Terms of Service</a> and acknowledge that you have read our <a href="https://automattic.com/privacy/" target="_blank">Privacy Policy</a>',
'woocommerce'
);
}
/**
* Task completion.
*
* @return bool
*/
public function is_complete() {
if ( null === $this->is_complete_result ) {
$this->is_complete_result = self::is_connected();
}
return $this->is_complete_result;
}
/**
* Task visibility.
*
* @return bool
*/
public function can_view() {
$payments = $this->task_list->get_task( 'payments' );
return ! $payments->is_complete() && // Do not re-display the task if the "add payments" task has already been completed.
self::is_installed() &&
self::is_supported();
}
/**
* Check if the plugin was requested during onboarding.
*
* @return bool
*/
public static function is_requested() {
$profiler_data = get_option( OnboardingProfile::DATA_OPTION, array() );
$product_types = isset( $profiler_data['product_types'] ) ? $profiler_data['product_types'] : array();
$business_extensions = isset( $profiler_data['business_extensions'] ) ? $profiler_data['business_extensions'] : array();
$subscriptions_and_us = in_array( 'subscriptions', $product_types, true ) && 'US' === WC()->countries->get_base_country();
return in_array( 'woocommerce-payments', $business_extensions, true ) || $subscriptions_and_us;
}
/**
* Check if the plugin is installed.
*
* @return bool
*/
public static function is_installed() {
$installed_plugins = PluginsHelper::get_installed_plugin_slugs();
return in_array( 'woocommerce-payments', $installed_plugins, true );
}
/**
* Check if WooCommerce Payments is connected.
*
* @return bool
*/
public static function is_connected() {
if ( class_exists( '\WC_Payments' ) ) {
$wc_payments_gateway = \WC_Payments::get_gateway();
return method_exists( $wc_payments_gateway, 'is_connected' )
? $wc_payments_gateway->is_connected()
: false;
}
return false;
}
/**
* Check if the store is in a supported country.
*
* @return bool
*/
public static function is_supported() {
$suggestions = Suggestions::get_suggestions();
$suggestion_plugins = array_merge(
...array_filter(
array_column( $suggestions, 'plugins' ),
function( $plugins ) {
return is_array( $plugins );
}
)
);
$woocommerce_payments_ids = array_search( 'woocommerce-payments', $suggestion_plugins, true );
if ( false !== $woocommerce_payments_ids ) {
return true;
}
return false;
}
}
Features/PaymentGatewaySuggestions/DefaultPaymentGateways.php 0000644 00000106474 15153746750 0020705 0 ustar 00 <?php
/**
* Gets a list of fallback methods if remote fetching is disabled.
*/
namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions;
defined( 'ABSPATH' ) || exit;
/**
* Default Payment Gateways
*/
class DefaultPaymentGateways {
/**
* This is the default priority for countries that are not in the $recommendation_priority_map.
* Priority is used to determine which payment gateway to recommend first.
* The lower the number, the higher the priority.
*
* @var array
*/
private static $recommendation_priority = array(
'woocommerce_payments' => 1,
'woocommerce_payments:with-in-person-payments' => 1,
'woocommerce_payments:without-in-person-payments' => 1,
'stripe' => 2,
'woo-mercado-pago-custom' => 3,
// PayPal Payments.
'ppcp-gateway' => 4,
'mollie_wc_gateway_banktransfer' => 5,
'razorpay' => 5,
'payfast' => 5,
'payubiz' => 6,
'square_credit_card' => 6,
'klarna_payments' => 6,
// Klarna Checkout.
'kco' => 6,
'paystack' => 6,
'eway' => 7,
'amazon_payments_advanced' => 7,
'affirm' => 8,
'afterpay' => 9,
'zipmoney' => 10,
'payoneer-checkout' => 11,
);
/**
* Get default specs.
*
* @return array Default specs.
*/
public static function get_all() {
$payment_gateways = array(
array(
'id' => 'affirm',
'title' => __( 'Affirm', 'woocommerce' ),
'content' => __( 'Affirm’s tailored Buy Now Pay Later programs remove price as a barrier, turning browsers into buyers, increasing average order value, and expanding your customer base.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/affirm.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/affirm.png',
'plugins' => array(),
'external_link' => 'https://woocommerce.com/products/woocommerce-gateway-affirm',
'is_visible' => array(
self::get_rules_for_countries(
array(
'US',
'CA',
)
),
),
'category_other' => array(),
'category_additional' => array(
'US',
'CA',
),
),
array(
'id' => 'afterpay',
'title' => __( 'Afterpay', 'woocommerce' ),
'content' => __( 'Afterpay allows customers to receive products immediately and pay for purchases over four installments, always interest-free.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/afterpay.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/afterpay.png',
'plugins' => array( 'afterpay-gateway-for-woocommerce' ),
'is_visible' => array(
self::get_rules_for_countries(
array(
'US',
'CA',
'AU',
)
),
),
'category_other' => array(),
'category_additional' => array(
'US',
'CA',
'AU',
),
),
array(
'id' => 'amazon_payments_advanced',
'title' => __( 'Amazon Pay', 'woocommerce' ),
'content' => __( 'Enable a familiar, fast checkout for hundreds of millions of active Amazon customers globally.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/amazonpay.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/amazonpay.png',
'plugins' => array( 'woocommerce-gateway-amazon-payments-advanced' ),
'is_visible' => array(
self::get_rules_for_countries(
array(
'US',
'AT',
'BE',
'CY',
'DK',
'ES',
'FR',
'DE',
'GB',
'HU',
'IE',
'IT',
'LU',
'NL',
'PT',
'SL',
'SE',
'JP',
)
),
),
'category_other' => array(),
'category_additional' => array(
'US',
'AT',
'BE',
'CY',
'DK',
'ES',
'FR',
'DE',
'GB',
'HU',
'IE',
'IT',
'LU',
'NL',
'PT',
'SL',
'SE',
'JP',
),
),
array(
'id' => 'bacs',
'title' => __( 'Direct bank transfer', 'woocommerce' ),
'content' => __( 'Take payments via bank transfer.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/bacs.svg',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/bacs.png',
'is_visible' => array(
self::get_rules_for_cbd( false ),
),
'is_offline' => true,
),
array(
'id' => 'cod',
'title' => __( 'Cash on delivery', 'woocommerce' ),
'content' => __( 'Take payments in cash upon delivery.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/cod.svg',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/cod.png',
'is_visible' => array(
self::get_rules_for_cbd( false ),
),
'is_offline' => true,
),
array(
'id' => 'eway',
'title' => __( 'Eway', 'woocommerce' ),
'content' => __( 'The Eway extension for WooCommerce allows you to take credit card payments directly on your store without redirecting your customers to a third party site to make payment.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/eway.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/eway.png',
'plugins' => array( 'woocommerce-gateway-eway' ),
'is_visible' => array(
self::get_rules_for_countries(
array(
'NZ',
'HK',
'SG',
'AU',
)
),
self::get_rules_for_cbd( false ),
),
'category_other' => array(
'NZ',
'HK',
'SG',
'AU',
),
'category_additional' => array(),
),
array(
'id' => 'kco',
'title' => __( 'Klarna Checkout', 'woocommerce' ),
'content' => __( 'Choose the payment that you want, pay now, pay later or slice it. No credit card numbers, no passwords, no worries.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/klarna-black.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/klarna.png',
'plugins' => array( 'klarna-checkout-for-woocommerce' ),
'is_visible' => array(
self::get_rules_for_countries(
array(
'NO',
'SE',
'FI',
)
),
self::get_rules_for_cbd( false ),
),
'category_other' => array(
'NO',
'SE',
'FI',
),
'category_additional' => array(),
),
array(
'id' => 'klarna_payments',
'title' => __( 'Klarna Payments', 'woocommerce' ),
'content' => __( 'Choose the payment that you want, pay now, pay later or slice it. No credit card numbers, no passwords, no worries.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/klarna-black.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/klarna.png',
'plugins' => array( 'klarna-payments-for-woocommerce' ),
'is_visible' => array(
self::get_rules_for_countries(
array(
'MX',
'US',
'CA',
'AT',
'BE',
'CH',
'DK',
'ES',
'FI',
'FR',
'DE',
'GB',
'IT',
'NL',
'NO',
'PL',
'SE',
'NZ',
'AU',
)
),
self::get_rules_for_cbd( false ),
),
'category_other' => array(),
'category_additional' => array(
'MX',
'US',
'CA',
'AT',
'BE',
'CH',
'DK',
'ES',
'FI',
'FR',
'DE',
'GB',
'IT',
'NL',
'NO',
'PL',
'SE',
'NZ',
'AU',
),
),
array(
'id' => 'mollie_wc_gateway_banktransfer',
'title' => __( 'Mollie', 'woocommerce' ),
'content' => __( 'Effortless payments by Mollie: Offer global and local payment methods, get onboarded in minutes, and supported in your language.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/mollie.svg',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/mollie.png',
'plugins' => array( 'mollie-payments-for-woocommerce' ),
'is_visible' => array(
self::get_rules_for_countries(
array(
'AT',
'BE',
'CH',
'ES',
'FI',
'FR',
'DE',
'GB',
'IT',
'NL',
'PL',
)
),
),
'category_other' => array(
'AT',
'BE',
'CH',
'ES',
'FI',
'FR',
'DE',
'GB',
'IT',
'NL',
'PL',
),
'category_additional' => array(),
),
array(
'id' => 'payfast',
'title' => __( 'Payfast', 'woocommerce' ),
'content' => __( 'The Payfast extension for WooCommerce enables you to accept payments by Credit Card and EFT via one of South Africa’s most popular payment gateways. No setup fees or monthly subscription costs. Selecting this extension will configure your store to use South African rands as the selected currency.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/payfast.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/payfast.png',
'plugins' => array( 'woocommerce-payfast-gateway' ),
'is_visible' => array(
self::get_rules_for_countries( array( 'ZA' ) ),
self::get_rules_for_cbd( false ),
),
'category_other' => array( 'ZA' ),
'category_additional' => array(),
),
array(
'id' => 'payoneer-checkout',
'title' => __( 'Payoneer Checkout', 'woocommerce' ),
'content' => __( 'Payoneer Checkout is the next generation of payment processing platforms, giving merchants around the world the solutions and direction they need to succeed in today’s hyper-competitive global market.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/payoneer.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/payoneer.png',
'plugins' => array( 'payoneer-checkout' ),
'is_visible' => array(
self::get_rules_for_countries(
array(
'HK',
'CN',
)
),
),
'category_other' => array(),
'category_additional' => array(
'HK',
'CN',
),
),
array(
'id' => 'paystack',
'title' => __( 'Paystack', 'woocommerce' ),
'content' => __( 'Paystack helps African merchants accept one-time and recurring payments online with a modern, safe, and secure payment gateway.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/paystack.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/paystack.png',
'plugins' => array( 'woo-paystack' ),
'is_visible' => array(
self::get_rules_for_countries( array( 'ZA', 'GH', 'NG' ) ),
self::get_rules_for_cbd( false ),
),
'category_other' => array( 'ZA', 'GH', 'NG' ),
'category_additional' => array(),
),
array(
'id' => 'payubiz',
'title' => __( 'PayU for WooCommerce', 'woocommerce' ),
'content' => __( 'Enable PayU’s exclusive plugin for WooCommerce to start accepting payments in 100+ payment methods available in India including credit cards, debit cards, UPI, & more!', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/payu.svg',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/payu.png',
'plugins' => array( 'payu-india' ),
'is_visible' => array(
(object) array(
'type' => 'base_location_country',
'value' => 'IN',
'operation' => '=',
),
self::get_rules_for_cbd( false ),
),
'category_other' => array( 'IN' ),
'category_additional' => array(),
),
array(
'id' => 'ppcp-gateway',
'title' => __( 'PayPal Payments', 'woocommerce' ),
'content' => __( "Safe and secure payments using credit cards or your customer's PayPal account.", 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/paypal.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/paypal.png',
'plugins' => array( 'woocommerce-paypal-payments' ),
'is_visible' => array(
self::get_rules_for_countries(
array(
'US',
'CA',
'MX',
'BR',
'AR',
'CL',
'CO',
'EC',
'PE',
'UY',
'VE',
'AT',
'BE',
'BG',
'HR',
'CH',
'CY',
'CZ',
'DK',
'EE',
'ES',
'FI',
'FR',
'DE',
'GB',
'GR',
'HU',
'IE',
'IT',
'LV',
'LT',
'LU',
'MT',
'NL',
'NO',
'PL',
'PT',
'RO',
'SK',
'SL',
'SE',
'AU',
'NZ',
'HK',
'JP',
'SG',
'CN',
'ID',
'IN',
)
),
self::get_rules_for_cbd( false ),
),
'category_other' => array(
'US',
'CA',
'MX',
'BR',
'AR',
'CL',
'CO',
'EC',
'PE',
'UY',
'VE',
'AT',
'BE',
'BG',
'HR',
'CH',
'CY',
'CZ',
'DK',
'EE',
'ES',
'FI',
'FR',
'DE',
'GB',
'GR',
'HU',
'IE',
'IT',
'LV',
'LT',
'LU',
'MT',
'NL',
'NO',
'PL',
'PT',
'RO',
'SK',
'SL',
'SE',
'AU',
'NZ',
'HK',
'JP',
'SG',
'CN',
'ID',
),
'category_additional' => array(
'US',
'CA',
'ZA',
'NG',
'GH',
'EC',
'VE',
'AR',
'CL',
'CO',
'PE',
'UY',
'MX',
'BR',
'AT',
'BE',
'BG',
'HR',
'CH',
'CY',
'CZ',
'DK',
'EE',
'ES',
'FI',
'FR',
'DE',
'GB',
'GR',
'HU',
'IE',
'IT',
'LV',
'LT',
'LU',
'MT',
'NL',
'NO',
'PL',
'PT',
'RO',
'SK',
'SL',
'SE',
'AU',
'NZ',
'HK',
'JP',
'SG',
'CN',
'ID',
'IN',
),
),
array(
'id' => 'razorpay',
'title' => __( 'Razorpay', 'woocommerce' ),
'content' => __( 'The official Razorpay extension for WooCommerce allows you to accept credit cards, debit cards, netbanking, wallet, and UPI payments.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/razorpay.svg',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/razorpay.png',
'plugins' => array( 'woo-razorpay' ),
'is_visible' => array(
(object) array(
'type' => 'base_location_country',
'value' => 'IN',
'operation' => '=',
),
self::get_rules_for_cbd( false ),
),
'category_other' => array( 'IN' ),
'category_additional' => array(),
),
array(
'id' => 'square_credit_card',
'title' => __( 'Square', 'woocommerce' ),
'content' => __( 'Securely accept credit and debit cards with one low rate, no surprise fees (custom rates available). Sell online and in store and track sales and inventory in one place.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/square-black.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/square.png',
'plugins' => array( 'woocommerce-square' ),
'is_visible' => array(
(object) array(
'type' => 'or',
'operands' => (object) array(
array(
self::get_rules_for_countries( array( 'US' ) ),
self::get_rules_for_cbd( true ),
),
array(
self::get_rules_for_countries(
array(
'US',
'CA',
'IE',
'ES',
'FR',
'GB',
'AU',
'JP',
)
),
(object) array(
'type' => 'or',
'operands' => (object) array(
self::get_rules_for_selling_venues( array( 'brick-mortar', 'brick-mortar-other' ) ),
self::get_rules_selling_offline(),
),
),
),
),
),
),
'category_other' => array(
'US',
'CA',
'IE',
'ES',
'FR',
'GB',
'AU',
'JP',
),
'category_additional' => array(),
),
array(
'id' => 'stripe',
'title' => __( ' Stripe', 'woocommerce' ),
'content' => __( 'Accept debit and credit cards in 135+ currencies, methods such as Alipay, and one-touch checkout with Apple Pay.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/stripe.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/stripe.png',
'plugins' => array( 'woocommerce-gateway-stripe' ),
'is_visible' => array(
// https://stripe.com/global.
self::get_rules_for_countries(
array(
'US',
'CA',
'MX',
'BR',
'AT',
'BE',
'BG',
'CH',
'CY',
'CZ',
'DK',
'EE',
'ES',
'FI',
'FR',
'DE',
'GB',
'GR',
'HU',
'IE',
'IT',
'LV',
'LT',
'LU',
'MT',
'NL',
'NO',
'PL',
'PT',
'RO',
'SK',
'SL',
'SE',
'AU',
'NZ',
'HK',
'JP',
'SG',
'ID',
'IN',
)
),
self::get_rules_for_cbd( false ),
),
'category_other' => array(
'US',
'CA',
'MX',
'BR',
'AT',
'BE',
'BG',
'CH',
'CY',
'CZ',
'DK',
'EE',
'ES',
'FI',
'FR',
'DE',
'GB',
'GR',
'HU',
'IE',
'IT',
'LV',
'LT',
'LU',
'MT',
'NL',
'NO',
'PL',
'PT',
'RO',
'SK',
'SL',
'SE',
'AU',
'NZ',
'HK',
'JP',
'SG',
'ID',
'IN',
),
'category_additional' => array(),
),
array(
'id' => 'woo-mercado-pago-custom',
'title' => __( 'Mercado Pago Checkout Pro & Custom', 'woocommerce' ),
'content' => __( 'Accept credit and debit cards, offline (cash or bank transfer) and logged-in payments with money in Mercado Pago. Safe and secure payments with the leading payment processor in LATAM.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/mercadopago.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/mercadopago.png',
'plugins' => array( 'woocommerce-mercadopago' ),
'is_visible' => array(
self::get_rules_for_countries(
array(
'AR',
'CL',
'CO',
'EC',
'PE',
'UY',
'MX',
'BR',
)
),
),
'is_local_partner' => true,
'category_other' => array(
'AR',
'CL',
'CO',
'EC',
'PE',
'UY',
'MX',
'BR',
),
'category_additional' => array(),
),
// This is for backwards compatibility only (WC < 5.10.0-dev or WCA < 2.9.0-dev).
array(
'id' => 'woocommerce_payments',
'title' => __( 'WooPayments', 'woocommerce' ),
'content' => __(
'Manage transactions without leaving your WordPress Dashboard. Only with WooPayments.',
'woocommerce'
),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/wcpay.svg',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/wcpay.svg',
'plugins' => array( 'woocommerce-payments' ),
'description' => __( 'With WooPayments, you can securely accept major cards, Apple Pay, and payments in over 100 currencies. Track cash flow and manage recurring revenue directly from your store’s dashboard - with no setup costs or monthly fees.', 'woocommerce' ),
'is_visible' => array(
self::get_rules_for_cbd( false ),
self::get_rules_for_countries( self::get_wcpay_countries() ),
(object) array(
'type' => 'plugin_version',
'plugin' => 'woocommerce',
'version' => '5.10.0-dev',
'operator' => '<',
),
(object) array(
'type' => 'or',
'operands' => (object) array(
(object) array(
'type' => 'not',
'operand' => [
(object) array(
'type' => 'plugins_activated',
'plugins' => [ 'woocommerce-admin' ],
),
],
),
(object) array(
'type' => 'plugin_version',
'plugin' => 'woocommerce-admin',
'version' => '2.9.0-dev',
'operator' => '<',
),
),
),
),
),
array(
'id' => 'woocommerce_payments:without-in-person-payments',
'title' => __( 'WooPayments', 'woocommerce' ),
'content' => __(
'Manage transactions without leaving your WordPress Dashboard. Only with WooPayments.',
'woocommerce'
),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/wcpay.svg',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/wcpay.svg',
'plugins' => array( 'woocommerce-payments' ),
'description' => __( 'With WooPayments, you can securely accept major cards, Apple Pay, and payments in over 100 currencies. Track cash flow and manage recurring revenue directly from your store’s dashboard - with no setup costs or monthly fees.', 'woocommerce' ),
'is_visible' => array(
self::get_rules_for_cbd( false ),
self::get_rules_for_countries( array_diff( self::get_wcpay_countries(), array( 'US', 'CA' ) ) ),
(object) array(
'type' => 'or',
// Older versions of WooCommerce Admin require the ID to be `woocommerce-payments` to show the suggestion card.
'operands' => (object) array(
(object) array(
'type' => 'plugin_version',
'plugin' => 'woocommerce-admin',
'version' => '2.9.0-dev',
'operator' => '>=',
),
(object) array(
'type' => 'plugin_version',
'plugin' => 'woocommerce',
'version' => '5.10.0-dev',
'operator' => '>=',
),
),
),
),
),
// This is the same as the above, but with a different description for countries that support in-person payments such as US and CA.
array(
'id' => 'woocommerce_payments:with-in-person-payments',
'title' => __( 'WooPayments', 'woocommerce' ),
'content' => __(
'Manage transactions without leaving your WordPress Dashboard. Only with WooPayments.',
'woocommerce'
),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/wcpay.svg',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/wcpay.svg',
'plugins' => array( 'woocommerce-payments' ),
'description' => __( 'With WooPayments, you can securely accept major cards, Apple Pay, and payments in over 100 currencies – with no setup costs or monthly fees – and you can now accept in-person payments with the Woo mobile app.', 'woocommerce' ),
'is_visible' => array(
self::get_rules_for_cbd( false ),
self::get_rules_for_countries( array( 'US', 'CA' ) ),
(object) array(
'type' => 'or',
// Older versions of WooCommerce Admin require the ID to be `woocommerce-payments` to show the suggestion card.
'operands' => (object) array(
(object) array(
'type' => 'plugin_version',
'plugin' => 'woocommerce-admin',
'version' => '2.9.0-dev',
'operator' => '>=',
),
(object) array(
'type' => 'plugin_version',
'plugin' => 'woocommerce',
'version' => '5.10.0-dev',
'operator' => '>=',
),
),
),
),
),
array(
'id' => 'zipmoney',
'title' => __( 'Zip Co - Buy Now, Pay Later', 'woocommerce' ),
'content' => __( 'Give your customers the power to pay later, interest free and watch your sales grow.', 'woocommerce' ),
'image' => WC_ADMIN_IMAGES_FOLDER_URL . '/onboarding/zipco.png',
'image_72x72' => WC_ADMIN_IMAGES_FOLDER_URL . '/payment_methods/72x72/zipco.png',
'plugins' => array( 'zipmoney-payments-woocommerce' ),
'is_visible' => array(
self::get_rules_for_countries(
array(
'US',
'NZ',
'AU',
)
),
),
'category_other' => array(),
'category_additional' => array(
'US',
'NZ',
'AU',
),
),
);
$base_location = wc_get_base_location();
$country = $base_location['country'];
foreach ( $payment_gateways as $index => $payment_gateway ) {
$payment_gateways[ $index ]['recommendation_priority'] = self::get_recommendation_priority( $payment_gateway['id'], $country );
}
return $payment_gateways;
}
/**
* Get array of countries supported by WCPay depending on feature flag.
*
* @return array Array of countries.
*/
public static function get_wcpay_countries() {
return array( 'US', 'PR', 'AU', 'CA', 'CY', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'IE', 'IT', 'LU', 'LT', 'LV', 'NO', 'NZ', 'MT', 'AT', 'BE', 'NL', 'PL', 'PT', 'CH', 'HK', 'SI', 'SK', 'SG', 'BG', 'CZ', 'HR', 'HU', 'RO', 'SE', 'JP', 'AE' );
}
/**
* Get rules that match the store base location to one of the provided countries.
*
* @param array $countries Array of countries to match.
* @return object Rules to match.
*/
public static function get_rules_for_countries( $countries ) {
$rules = array();
foreach ( $countries as $country ) {
$rules[] = (object) array(
'type' => 'base_location_country',
'value' => $country,
'operation' => '=',
);
}
return (object) array(
'type' => 'or',
'operands' => $rules,
);
}
/**
* Get rules that match the store's selling venues.
*
* @param array $selling_venues Array of venues to match.
* @return object Rules to match.
*/
public static function get_rules_for_selling_venues( $selling_venues ) {
$rules = array();
foreach ( $selling_venues as $venue ) {
$rules[] = (object) array(
'type' => 'option',
'transformers' => array(
(object) array(
'use' => 'dot_notation',
'arguments' => (object) array(
'path' => 'selling_venues',
),
),
),
'option_name' => 'woocommerce_onboarding_profile',
'operation' => '=',
'value' => $venue,
'default' => array(),
);
}
return (object) array(
'type' => 'or',
'operands' => $rules,
);
}
/**
* Get rules for when selling offline for core profiler.
*
* @return object Rules to match.
*/
public static function get_rules_selling_offline() {
return (object) array(
'type' => 'option',
'transformers' => array(
(object) array(
'use' => 'dot_notation',
'arguments' => (object) array(
'path' => 'selling_online_answer',
),
),
),
'option_name' => 'woocommerce_onboarding_profile',
'operation' => 'contains',
'value' => 'no_im_selling_offline',
'default' => array(),
);
}
/**
* Get default rules for CBD based on given argument.
*
* @param bool $should_have Whether or not the store should have CBD as an industry (true) or not (false).
* @return array Rules to match.
*/
public static function get_rules_for_cbd( $should_have ) {
return (object) array(
'type' => 'option',
'transformers' => array(
(object) array(
'use' => 'dot_notation',
'arguments' => (object) array(
'path' => 'industry',
),
),
(object) array(
'use' => 'array_column',
'arguments' => (object) array(
'key' => 'slug',
),
),
),
'option_name' => 'woocommerce_onboarding_profile',
'operation' => $should_have ? 'contains' : '!contains',
'value' => 'cbd-other-hemp-derived-products',
'default' => array(),
);
}
/**
* Get recommendation priority for a given payment gateway by id and country.
* If country is not supported, return null.
*
* @param string $gateway_id Payment gateway id.
* @param string $country_code Store country code.
* @return int|null Priority. Priority is 0-indexed, so 0 is the highest priority.
*/
private static function get_recommendation_priority( $gateway_id, $country_code ) {
$recommendation_priority_map = array(
'US' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'square_credit_card',
'amazon_payments_advanced',
'affirm',
'afterpay',
'klarna_payments',
'zipmoney',
],
'CA' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'square_credit_card',
'affirm',
'afterpay',
'klarna_payments',
],
'AT' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'mollie_wc_gateway_banktransfer',
'klarna_payments',
'amazon_payments_advanced',
],
'BE' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'mollie_wc_gateway_banktransfer',
'klarna_payments',
'amazon_payments_advanced',
],
'BG' => [ 'stripe', 'ppcp-gateway' ],
'HR' => [ 'ppcp-gateway' ],
'CH' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'mollie_wc_gateway_banktransfer',
'klarna_payments',
],
'CY' => [ 'stripe', 'ppcp-gateway', 'amazon_payments_advanced' ],
'CZ' => [ 'stripe', 'ppcp-gateway' ],
'DK' => [
'stripe',
'ppcp-gateway',
'klarna_payments',
'amazon_payments_advanced',
],
'EE' => [ 'stripe', 'ppcp-gateway' ],
'ES' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'mollie_wc_gateway_banktransfer',
'square_credit_card',
'klarna_payments',
'amazon_payments_advanced',
],
'FI' => [
'stripe',
'ppcp-gateway',
'mollie_wc_gateway_banktransfer',
'kco',
'klarna_payments',
],
'FR' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'mollie_wc_gateway_banktransfer',
'square_credit_card',
'klarna_payments',
'amazon_payments_advanced',
],
'DE' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'mollie_wc_gateway_banktransfer',
'klarna_payments',
'amazon_payments_advanced',
],
'GB' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'mollie_wc_gateway_banktransfer',
'square_credit_card',
'klarna_payments',
'amazon_payments_advanced',
],
'GR' => [ 'stripe', 'ppcp-gateway' ],
'HU' => [ 'stripe', 'ppcp-gateway', 'amazon_payments_advanced' ],
'IE' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'square_credit_card',
'amazon_payments_advanced',
],
'IT' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'mollie_wc_gateway_banktransfer',
'klarna_payments',
'amazon_payments_advanced',
],
'LV' => [ 'stripe', 'ppcp-gateway' ],
'LT' => [ 'stripe', 'ppcp-gateway' ],
'LU' => [ 'stripe', 'ppcp-gateway', 'amazon_payments_advanced' ],
'MT' => [ 'stripe', 'ppcp-gateway' ],
'NL' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'mollie_wc_gateway_banktransfer',
'klarna_payments',
'amazon_payments_advanced',
],
'NO' => [ 'stripe', 'ppcp-gateway', 'kco', 'klarna_payments' ],
'PL' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'mollie_wc_gateway_banktransfer',
'klarna_payments',
],
'PT' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'amazon_payments_advanced',
],
'RO' => [ 'stripe', 'ppcp-gateway' ],
'SK' => [ 'stripe', 'ppcp-gateway' ],
'SL' => [ 'stripe', 'ppcp-gateway', 'amazon_payments_advanced' ],
'SE' => [
'stripe',
'ppcp-gateway',
'kco',
'klarna_payments',
'amazon_payments_advanced',
],
'MX' => [
'stripe',
'woo-mercado-pago-custom',
'ppcp-gateway',
'klarna_payments',
],
'BR' => [ 'stripe', 'woo-mercado-pago-custom', 'ppcp-gateway' ],
'AR' => [ 'woo-mercado-pago-custom', 'ppcp-gateway' ],
'BO' => [],
'CL' => [ 'woo-mercado-pago-custom', 'ppcp-gateway' ],
'CO' => [ 'woo-mercado-pago-custom', 'ppcp-gateway' ],
'EC' => [ 'woo-mercado-pago-custom', 'ppcp-gateway' ],
'FK' => [],
'GF' => [],
'GY' => [],
'PY' => [],
'PE' => [ 'woo-mercado-pago-custom', 'ppcp-gateway' ],
'SR' => [],
'UY' => [ 'woo-mercado-pago-custom', 'ppcp-gateway' ],
'VE' => [ 'ppcp-gateway' ],
'AU' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'square_credit_card',
'eway',
'afterpay',
'klarna_payments',
'zipmoney',
],
'NZ' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'eway',
'klarna_payments',
'zipmoney',
],
'HK' => [
'woocommerce_payments',
'stripe',
'ppcp-gateway',
'eway',
'payoneer-checkout',
],
'JP' => [
'stripe',
'ppcp-gateway',
'square_credit_card',
'amazon_payments_advanced',
],
'SG' => [ 'woocommerce_payments', 'stripe', 'ppcp-gateway', 'eway' ],
'CN' => [ 'ppcp-gateway', 'payoneer-checkout' ],
'FJ' => [],
'GU' => [],
'ID' => [ 'stripe', 'ppcp-gateway' ],
'IN' => [ 'stripe', 'razorpay', 'payubiz', 'ppcp-gateway' ],
'ZA' => [ 'payfast', 'paystack' ],
'NG' => [ 'paystack' ],
'GH' => [ 'paystack' ],
);
// If the country code is not in the list, return default priority.
if ( ! isset( $recommendation_priority_map[ $country_code ] ) ) {
return self::get_default_recommendation_priority( $gateway_id );
}
$index = array_search( $gateway_id, $recommendation_priority_map[ $country_code ], true );
// If the gateway is not in the list, return the last index + 1.
if ( false === $index ) {
return count( $recommendation_priority_map[ $country_code ] );
}
return $index;
}
/**
* Get the default recommendation priority for a payment gateway.
* This is used when a country is not in the $recommendation_priority_map array.
*
* @param string $id Payment gateway id.
* @return int Priority.
*/
private static function get_default_recommendation_priority( $id ) {
if ( ! $id || ! array_key_exists( $id, self::$recommendation_priority ) ) {
return null;
}
return self::$recommendation_priority[ $id ];
}
}
Features/PaymentGatewaySuggestions/EvaluateSuggestion.php 0000644 00000001534 15153746750 0020063 0 ustar 00 <?php
/**
* Evaluates the spec and returns a status.
*/
namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RuleEvaluator;
/**
* Evaluates the spec and returns the evaluated suggestion.
*/
class EvaluateSuggestion {
/**
* Evaluates the spec and returns the suggestion.
*
* @param object|array $spec The suggestion to evaluate.
* @return object The evaluated suggestion.
*/
public static function evaluate( $spec ) {
$rule_evaluator = new RuleEvaluator();
$suggestion = is_array( $spec ) ? (object) $spec : clone $spec;
if ( isset( $suggestion->is_visible ) ) {
$is_visible = $rule_evaluator->evaluate( $suggestion->is_visible );
$suggestion->is_visible = $is_visible;
}
return $suggestion;
}
}
Features/PaymentGatewaySuggestions/Init.php 0000644 00000005517 15153746750 0015155 0 ustar 00 <?php
/**
* Handles running payment gateway suggestion specs
*/
namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\DefaultPaymentGateways;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\PaymentGatewaysController;
/**
* Remote Payment Methods engine.
* This goes through the specs and gets eligible payment gateways.
*/
class Init {
/**
* Option name for dismissed payment method suggestions.
*/
const RECOMMENDED_PAYMENT_PLUGINS_DISMISS_OPTION = 'woocommerce_setting_payments_recommendations_hidden';
/**
* Constructor.
*/
public function __construct() {
PaymentGatewaysController::init();
}
/**
* Go through the specs and run them.
*
* @param array|null $specs payment suggestion spec array.
* @return array
*/
public static function get_suggestions( array $specs = null ) {
$suggestions = array();
if ( null === $specs ) {
$specs = self::get_specs();
}
foreach ( $specs as $spec ) {
$suggestion = EvaluateSuggestion::evaluate( $spec );
$suggestions[] = $suggestion;
}
return array_values(
array_filter(
$suggestions,
function( $suggestion ) {
return ! property_exists( $suggestion, 'is_visible' ) || $suggestion->is_visible;
}
)
);
}
/**
* Delete the specs transient.
*/
public static function delete_specs_transient() {
PaymentGatewaySuggestionsDataSourcePoller::get_instance()->delete_specs_transient();
}
/**
* Get specs or fetch remotely if they don't exist.
*/
public static function get_specs() {
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
return apply_filters( 'woocommerce_admin_payment_gateway_suggestion_specs', DefaultPaymentGateways::get_all() );
}
$specs = PaymentGatewaySuggestionsDataSourcePoller::get_instance()->get_specs_from_data_sources();
// Fetch specs if they don't yet exist.
if ( false === $specs || ! is_array( $specs ) || 0 === count( $specs ) ) {
return apply_filters( 'woocommerce_admin_payment_gateway_suggestion_specs', DefaultPaymentGateways::get_all() );
}
return apply_filters( 'woocommerce_admin_payment_gateway_suggestion_specs', $specs );
}
/**
* Check if suggestions should be shown in the settings screen.
*
* @return bool
*/
public static function should_display() {
if ( 'yes' === get_option( self::RECOMMENDED_PAYMENT_PLUGINS_DISMISS_OPTION, 'no' ) ) {
return false;
}
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
return false;
}
return apply_filters( 'woocommerce_allow_payment_recommendations', true );
}
/**
* Dismiss the suggestions.
*/
public static function dismiss() {
return update_option( self::RECOMMENDED_PAYMENT_PLUGINS_DISMISS_OPTION, 'yes' );
}
}
Features/PaymentGatewaySuggestions/PaymentGatewaySuggestionsDataSourcePoller.php 0000644 00000002154 15153746750 0024567 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions;
use Automattic\WooCommerce\Admin\DataSourcePoller;
/**
* Specs data source poller class for payment gateway suggestions.
*/
class PaymentGatewaySuggestionsDataSourcePoller extends DataSourcePoller {
/**
* Data Source Poller ID.
*/
const ID = 'payment_gateway_suggestions';
/**
* Default data sources array.
*/
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/payment-gateway-suggestions/1.0/suggestions.json',
);
/**
* Class instance.
*
* @var Analytics instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
// Add country query param to data sources.
$base_location = wc_get_base_location();
$data_sources = array_map(
function( $url ) use ( $base_location ) {
return add_query_arg(
'country',
$base_location['country'],
$url
);
},
self::DATA_SOURCES
);
self::$instance = new self( self::ID, $data_sources );
}
return self::$instance;
}
}
Features/PaymentGatewaySuggestions/PaymentGatewaysController.php 0000644 00000010671 15153746750 0021435 0 ustar 00 <?php
/**
* Logic for extending WC_REST_Payment_Gateways_Controller.
*/
namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions;
use Automattic\WooCommerce\Admin\Features\TransientNotices;
defined( 'ABSPATH' ) || exit;
/**
* PaymentGateway class
*/
class PaymentGatewaysController {
/**
* Initialize payment gateway changes.
*/
public static function init() {
add_filter( 'woocommerce_rest_prepare_payment_gateway', array( __CLASS__, 'extend_response' ), 10, 3 );
add_filter( 'admin_init', array( __CLASS__, 'possibly_do_connection_return_action' ) );
add_action( 'woocommerce_admin_payment_gateway_connection_return', array( __CLASS__, 'handle_successfull_connection' ) );
}
/**
* Add necessary fields to REST API response.
*
* @param WP_REST_Response $response Response data.
* @param WC_Payment_Gateway $gateway Payment gateway object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public static function extend_response( $response, $gateway, $request ) {
$data = $response->get_data();
$data['needs_setup'] = $gateway->needs_setup();
$data['post_install_scripts'] = self::get_post_install_scripts( $gateway );
$data['settings_url'] = method_exists( $gateway, 'get_settings_url' )
? $gateway->get_settings_url()
: admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=' . strtolower( $gateway->id ) );
$return_url = wc_admin_url( '&task=payments&connection-return=' . strtolower( $gateway->id ) . '&_wpnonce=' . wp_create_nonce( 'connection-return' ) );
$data['connection_url'] = method_exists( $gateway, 'get_connection_url' )
? $gateway->get_connection_url( $return_url )
: null;
$data['setup_help_text'] = method_exists( $gateway, 'get_setup_help_text' )
? $gateway->get_setup_help_text()
: null;
$data['required_settings_keys'] = method_exists( $gateway, 'get_required_settings_keys' )
? $gateway->get_required_settings_keys()
: array();
$response->set_data( $data );
return $response;
}
/**
* Get payment gateway scripts for post-install.
*
* @param WC_Payment_Gateway $gateway Payment gateway object.
* @return array Install scripts.
*/
public static function get_post_install_scripts( $gateway ) {
$scripts = array();
$wp_scripts = wp_scripts();
$handles = method_exists( $gateway, 'get_post_install_script_handles' )
? $gateway->get_post_install_script_handles()
: array();
foreach ( $handles as $handle ) {
if ( isset( $wp_scripts->registered[ $handle ] ) ) {
$scripts[] = $wp_scripts->registered[ $handle ];
}
}
return $scripts;
}
/**
* Call an action after a gating has been successfully returned.
*/
public static function possibly_do_connection_return_action() {
if (
! isset( $_GET['page'] ) ||
'wc-admin' !== $_GET['page'] ||
! isset( $_GET['task'] ) ||
'payments' !== $_GET['task'] ||
! isset( $_GET['connection-return'] ) ||
! isset( $_GET['_wpnonce'] ) ||
! wp_verify_nonce( wc_clean( wp_unslash( $_GET['_wpnonce'] ) ), 'connection-return' )
) {
return;
}
$gateway_id = sanitize_text_field( wp_unslash( $_GET['connection-return'] ) );
do_action( 'woocommerce_admin_payment_gateway_connection_return', $gateway_id );
}
/**
* Handle a successful gateway connection.
*
* @param string $gateway_id Gateway ID.
*/
public static function handle_successfull_connection( $gateway_id ) {
// phpcs:disable WordPress.Security.NonceVerification
if ( ! isset( $_GET['success'] ) || 1 !== intval( $_GET['success'] ) ) {
return;
}
// phpcs:enable WordPress.Security.NonceVerification
$payment_gateways = WC()->payment_gateways()->payment_gateways();
$payment_gateway = isset( $payment_gateways[ $gateway_id ] ) ? $payment_gateways[ $gateway_id ] : null;
if ( ! $payment_gateway ) {
return;
}
$payment_gateway->update_option( 'enabled', 'yes' );
TransientNotices::add(
array(
'user_id' => get_current_user_id(),
'id' => 'payment-gateway-connection-return-' . str_replace( ',', '-', $gateway_id ),
'status' => 'success',
'content' => sprintf(
/* translators: the title of the payment gateway */
__( '%s connected successfully', 'woocommerce' ),
$payment_gateway->method_title
),
)
);
wc_admin_record_tracks_event(
'tasklist_payment_connect_method',
array(
'payment_method' => $gateway_id,
)
);
wp_safe_redirect( wc_admin_url() );
}
}
Features/ProductBlockEditor/BlockRegistry.php 0000644 00000007700 15153746750 0015401 0 ustar 00 <?php
/**
* WooCommerce Product Editor Block Registration
*/
namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor;
use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
/**
* Product block registration and style registration functionality.
*/
class BlockRegistry {
/**
* The directory where blocks are stored after build.
*/
const BLOCKS_DIR = 'product-editor/blocks';
/**
* Array of all available product blocks.
*/
const PRODUCT_BLOCKS = [
'woocommerce/conditional',
'woocommerce/product-catalog-visibility-field',
'woocommerce/product-checkbox-field',
'woocommerce/product-collapsible',
'woocommerce/product-description-field',
'woocommerce/product-images-field',
'woocommerce/product-inventory-email-field',
'woocommerce/product-sku-field',
'woocommerce/product-name-field',
'woocommerce/product-pricing-field',
'woocommerce/product-radio-field',
'woocommerce/product-regular-price-field',
'woocommerce/product-sale-price-field',
'woocommerce/product-schedule-sale-fields',
'woocommerce/product-section',
'woocommerce/product-shipping-class-field',
'woocommerce/product-shipping-dimensions-fields',
'woocommerce/product-summary-field',
'woocommerce/product-tab',
'woocommerce/product-tag-field',
'woocommerce/product-inventory-quantity-field',
'woocommerce/product-toggle-field',
'woocommerce/product-variation-items-field',
'woocommerce/product-variations-fields',
'woocommerce/product-password-field',
'woocommerce/product-has-variations-notice',
'woocommerce/product-taxonomy-field',
];
/**
* Get a file path for a given block file.
*
* @param string $path File path.
*/
private function get_file_path( $path ) {
return WC_ABSPATH . WCAdminAssets::get_path( 'js' ) . trailingslashit( self::BLOCKS_DIR ) . $path;
}
/**
* Initialize all blocks.
*/
public function init() {
add_filter( 'block_categories_all', array( $this, 'register_categories' ), 10, 2 );
$this->register_product_blocks();
}
/**
* Register all the product blocks.
*/
private function register_product_blocks() {
foreach ( self::PRODUCT_BLOCKS as $block_name ) {
$this->register_block( $block_name );
}
}
/**
* Register product related block categories.
*
* @param array[] $block_categories Array of categories for block types.
* @param WP_Block_Editor_Context $editor_context The current block editor context.
*/
public function register_categories( $block_categories, $editor_context ) {
if ( INIT::EDITOR_CONTEXT_NAME === $editor_context->name ) {
$block_categories[] = array(
'slug' => 'woocommerce',
'title' => __( 'WooCommerce', 'woocommerce' ),
'icon' => null,
);
}
return $block_categories;
}
/**
* Get the block name without the "woocommerce/" prefix.
*
* @param string $block_name Block name.
*
* @return string
*/
private function remove_block_prefix( $block_name ) {
if ( 0 === strpos( $block_name, 'woocommerce/' ) ) {
return substr_replace( $block_name, '', 0, strlen( 'woocommerce/' ) );
}
return $block_name;
}
/**
* Register a single block.
*
* @param string $block_name Block name.
*
* @return WP_Block_Type|false The registered block type on success, or false on failure.
*/
private function register_block( $block_name ) {
$block_name = $this->remove_block_prefix( $block_name );
$block_json_file = $this->get_file_path( $block_name . '/block.json' );
if ( ! file_exists( $block_json_file ) ) {
return false;
}
// phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$metadata = json_decode( file_get_contents( $block_json_file ), true );
if ( ! is_array( $metadata ) || ! $metadata['name'] ) {
return false;
}
$registry = \WP_Block_Type_Registry::get_instance();
if ( $registry->is_registered( $metadata['name'] ) ) {
$registry->unregister( $metadata['name'] );
}
return register_block_type_from_metadata( $block_json_file );
}
}
Features/ProductBlockEditor/Init.php 0000644 00000015121 15153746750 0013515 0 ustar 00 <?php
/**
* WooCommerce Product Block Editor
*/
namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\SimpleProductTemplate;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Internal\Admin\BlockTemplateRegistry\BlockTemplateRegistry;
use WP_Block_Editor_Context;
/**
* Loads assets related to the product block editor.
*/
class Init {
/**
* The context name used to identify the editor.
*/
const EDITOR_CONTEXT_NAME = 'woocommerce/edit-product';
/**
* Supported post types.
*
* @var array
*/
private $supported_post_types = array( 'simple' );
/**
* Redirection controller.
*
* @var RedirectionController
*/
private $redirection_controller;
/**
* Constructor
*/
public function __construct() {
if ( Features::is_enabled( 'product-variation-management' ) ) {
array_push( $this->supported_post_types, 'variable' );
}
$this->redirection_controller = new RedirectionController( $this->supported_post_types );
if ( \Automattic\WooCommerce\Utilities\FeaturesUtil::feature_is_enabled( 'product_block_editor' ) ) {
// Register the product block template.
$template_registry = wc_get_container()->get( BlockTemplateRegistry::class );
$template_registry->register( new SimpleProductTemplate() );
if ( ! Features::is_enabled( 'new-product-management-experience' ) ) {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_conflicting_styles' ), 100 );
add_action( 'get_edit_post_link', array( $this, 'update_edit_product_link' ), 10, 2 );
}
add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_filter( 'woocommerce_register_post_type_product', array( $this, 'add_product_template' ) );
add_action( 'current_screen', array( $this, 'set_current_screen_to_block_editor_if_wc_admin' ) );
$block_registry = new BlockRegistry();
$block_registry->init();
$tracks = new Tracks();
$tracks->init();
}
}
/**
* Enqueue scripts needed for the product form block editor.
*/
public function enqueue_scripts() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
$post_type_object = get_post_type_object( 'product' );
$block_editor_context = new WP_Block_Editor_Context( array( 'name' => self::EDITOR_CONTEXT_NAME ) );
$editor_settings = array();
if ( ! empty( $post_type_object->template ) ) {
$editor_settings['template'] = $post_type_object->template;
$editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
}
$editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context );
$script_handle = 'wc-admin-edit-product';
wp_register_script( $script_handle, '', array(), '0.1.0', true );
wp_enqueue_script( $script_handle );
wp_add_inline_script(
$script_handle,
'var productBlockEditorSettings = productBlockEditorSettings || ' . wp_json_encode( $editor_settings ) . ';',
'before'
);
wp_add_inline_script(
$script_handle,
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( $editor_settings['blockCategories'] ) ),
'before'
);
wp_tinymce_inline_scripts();
wp_enqueue_media();
}
/**
* Enqueue styles needed for the rich text editor.
*/
public function enqueue_styles() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
wp_enqueue_style( 'wp-edit-blocks' );
wp_enqueue_style( 'wp-format-library' );
wp_enqueue_editor();
/**
* Enqueue any block editor related assets.
*
* @since 7.1.0
*/
do_action( 'enqueue_block_editor_assets' );
}
/**
* Dequeue conflicting styles.
*/
public function dequeue_conflicting_styles() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
// Dequeing this to avoid conflicts, until we remove the 'woocommerce-page' class.
wp_dequeue_style( 'woocommerce-blocktheme' );
}
/**
* Update the edit product links when the new experience is enabled.
*
* @param string $link The edit link.
* @param int $post_id Post ID.
* @return string
*/
public function update_edit_product_link( $link, $post_id ) {
$product = wc_get_product( $post_id );
if ( ! $product ) {
return $link;
}
if ( $product->get_type() === 'simple' ) {
return admin_url( 'admin.php?page=wc-admin&path=/product/' . $product->get_id() );
}
return $link;
}
/**
* Enqueue styles needed for the rich text editor.
*
* @param array $args Array of post type arguments.
* @return array Array of post type arguments.
*/
public function add_product_template( $args ) {
if ( ! isset( $args['template'] ) ) {
// Get the template from the registry.
$template_registry = wc_get_container()->get( BlockTemplateRegistry::class );
$template = $template_registry->get_registered( 'simple-product' );
if ( isset( $template ) ) {
$args['template_lock'] = 'all';
$args['template'] = $template->get_formatted_template();
}
}
return $args;
}
/**
* Adds fields so that we can store user preferences for the variations block.
*
* @param array $user_data_fields User data fields.
* @return array
*/
public function add_user_data_fields( $user_data_fields ) {
return array_merge(
$user_data_fields,
array(
'variable_product_block_tour_shown',
'product_block_variable_options_notice_dismissed',
'variable_items_without_price_notice_dismissed'
)
);
}
/**
* Sets the current screen to the block editor if a wc-admin page.
*/
public function set_current_screen_to_block_editor_if_wc_admin() {
$screen = get_current_screen();
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
// (no idea why I need that phpcs:ignore above, but I'm tired trying to re-write this comment to get it to pass)
// we can't check the 'path' query param because client-side routing is used within wc-admin,
// so this action handler is only called on the initial page load from the server, which might
// not be the product edit page (it mostly likely isn't).
if ( PageController::is_admin_page() ) {
$screen->is_block_editor( true );
wp_add_inline_script(
'wp-blocks',
'wp.blocks && wp.blocks.unstable__bootstrapServerSideBlockDefinitions && wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
);
}
}
}
Features/ProductBlockEditor/ProductTemplates/AbstractProductFormTemplate.php 0000644 00000003572 15153746750 0023544 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
use Automattic\WooCommerce\Internal\Admin\BlockTemplates\AbstractBlockTemplate;
/**
* Block template class.
*/
abstract class AbstractProductFormTemplate extends AbstractBlockTemplate implements ProductFormTemplateInterface {
/**
* Get the template area.
*/
public function get_area(): string {
return 'product-form';
}
/**
* Get a group block by ID.
*
* @param string $group_id The group block ID.
* @throws \UnexpectedValueException If block is not of type GroupInterface.
*/
public function get_group_by_id( string $group_id ): ?GroupInterface {
$group = $this->get_block( $group_id );
if ( $group && ! $group instanceof GroupInterface ) {
throw new \UnexpectedValueException( 'Block with specified ID is not a group.' );
}
return $group;
}
/**
* Get a section block by ID.
*
* @param string $section_id The section block ID.
* @throws \UnexpectedValueException If block is not of type SectionInterface.
*/
public function get_section_by_id( string $section_id ): ?SectionInterface {
$section = $this->get_block( $section_id );
if ( $section && ! $section instanceof SectionInterface ) {
throw new \UnexpectedValueException( 'Block with specified ID is not a section.' );
}
return $section;
}
/**
* Get a block by ID.
*
* @param string $block_id The block block ID.
*/
public function get_block_by_id( string $block_id ): ?BlockInterface {
return $this->get_block( $block_id );
}
/**
* Add a custom block type to this template.
*
* @param array $block_config The block data.
*/
public function add_group( array $block_config ): GroupInterface {
$block = new Group( $block_config, $this->get_root_template(), $this );
return $this->add_inner_block( $block );
}
}
Features/ProductBlockEditor/ProductTemplates/Group.php 0000644 00000003727 15153746750 0017216 0 ustar 00 <?php
/**
* WooCommerce Product Group Block class.
*/
namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\ContainerInterface;
use Automattic\WooCommerce\Internal\Admin\BlockTemplates\BlockContainerTrait;
/**
* Class for Group block.
*/
class Group extends ProductBlock implements GroupInterface {
use BlockContainerTrait;
/**
* Group Block constructor.
*
* @param array $config The block configuration.
* @param BlockTemplateInterface $root_template The block template that this block belongs to.
* @param ContainerInterface|null $parent The parent block container.
*
* @throws \ValueError If the block configuration is invalid.
* @throws \ValueError If the parent block container does not belong to the same template as the block.
* @throws \InvalidArgumentException If blockName key and value are passed into block configuration.
*/
public function __construct( array $config, BlockTemplateInterface &$root_template, ContainerInterface &$parent = null ) {
if ( ! empty( $config['blockName'] ) ) {
throw new \InvalidArgumentException( 'Unexpected key "blockName", this defaults to "woocommerce/product-tab".' );
}
if ( $config['id'] && ( empty( $config['attributes'] ) || empty( $config['attributes']['id'] ) ) ) {
$config['attributes'] = empty( $config['attributes'] ) ? [] : $config['attributes'];
$config['attributes']['id'] = $config['id'];
}
parent::__construct( array_merge( array( 'blockName' => 'woocommerce/product-tab' ), $config ), $root_template, $parent );
}
/**
* Add a section block type to this template.
*
* @param array $block_config The block data.
*/
public function add_section( array $block_config ): SectionInterface {
$block = new Section( $block_config, $this->get_root_template(), $this );
return $this->add_inner_block( $block );
}
}
Features/ProductBlockEditor/ProductTemplates/GroupInterface.php 0000644 00000001312 15153746750 0021023 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockContainerInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
/**
* Interface for block containers.
*/
interface GroupInterface extends BlockContainerInterface {
/**
* Adds a new section block.
*
* @param array $block_config block config.
* @return SectionInterface new block section.
*/
public function add_section( array $block_config ): SectionInterface;
/**
* Adds a new block to the section block.
*
* @param array $block_config block config.
*/
public function add_block( array $block_config ): BlockInterface;
}
Features/ProductBlockEditor/ProductTemplates/ProductBlock.php 0000644 00000001523 15153746750 0020505 0 ustar 00 <?php
/**
* WooCommerce Product Block class.
*/
namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\ContainerInterface;
use Automattic\WooCommerce\Internal\Admin\BlockTemplates\AbstractBlock;
use Automattic\WooCommerce\Internal\Admin\BlockTemplates\BlockContainerTrait;
/**
* Class for Product block.
*/
class ProductBlock extends AbstractBlock implements ContainerInterface {
use BlockContainerTrait;
/**
* Adds block to the section block.
*
* @param array $block_config The block data.
*/
public function &add_block( array $block_config ): BlockInterface {
$block = new ProductBlock( $block_config, $this->get_root_template(), $this );
return $this->add_inner_block( $block );
}
}
Features/ProductBlockEditor/ProductTemplates/ProductFormTemplateInterface.php 0000644 00000002124 15153746750 0023671 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface;
/**
* Interface for block containers.
*/
interface ProductFormTemplateInterface extends BlockTemplateInterface {
/**
* Adds a new group block.
*
* @param array $block_config block config.
* @return BlockInterface new block section.
*/
public function add_group( array $block_config ): GroupInterface;
/**
* Gets Group block by id.
*
* @param string $group_id group id.
* @return GroupInterface|null
*/
public function get_group_by_id( string $group_id ): ?GroupInterface;
/**
* Gets Section block by id.
*
* @param string $section_id section id.
* @return SectionInterface|null
*/
public function get_section_by_id( string $section_id ): ?SectionInterface;
/**
* Gets Block by id.
*
* @param string $block_id block id.
* @return BlockInterface|null
*/
public function get_block_by_id( string $block_id ): ?BlockInterface;
}
Features/ProductBlockEditor/ProductTemplates/Section.php 0000644 00000003200 15153746750 0017510 0 ustar 00 <?php
/**
* WooCommerce Section Block class.
*/
namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\ContainerInterface;
/**
* Class for Section block.
*/
class Section extends ProductBlock implements SectionInterface {
/**
* Section Block constructor.
*
* @param array $config The block configuration.
* @param BlockTemplateInterface $root_template The block template that this block belongs to.
* @param ContainerInterface|null $parent The parent block container.
*
* @throws \ValueError If the block configuration is invalid.
* @throws \ValueError If the parent block container does not belong to the same template as the block.
* @throws \InvalidArgumentException If blockName key and value are passed into block configuration.
*/
public function __construct( array $config, BlockTemplateInterface &$root_template, ContainerInterface &$parent = null ) {
if ( ! empty( $config['blockName'] ) ) {
throw new \InvalidArgumentException( 'Unexpected key "blockName", this defaults to "woocommerce/product-section".' );
}
parent::__construct( array_merge( array( 'blockName' => 'woocommerce/product-section' ), $config ), $root_template, $parent );
}
/**
* Add a section block type to this template.
*
* @param array $block_config The block data.
*/
public function add_section( array $block_config ): SectionInterface {
$block = new Section( $block_config, $this->get_root_template(), $this );
return $this->add_inner_block( $block );
}
}
Features/ProductBlockEditor/ProductTemplates/SectionInterface.php 0000644 00000001315 15153746750 0021336 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockContainerInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
/**
* Interface for block containers.
*/
interface SectionInterface extends BlockContainerInterface {
/**
* Adds a new section block.
*
* @param array $block_config block config.
* @return SectionInterface new block section.
*/
public function add_section( array $block_config ): SectionInterface;
/**
* Adds a new block to the section block.
*
* @param array $block_config block config.
*/
public function add_block( array $block_config ): BlockInterface;
}
Features/ProductBlockEditor/ProductTemplates/SimpleProductTemplate.php 0000644 00000057715 15153746750 0022416 0 ustar 00 <?php
/**
* SimpleProductTemplate
*/
namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates;
use Automattic\WooCommerce\Admin\Features\Features;
/**
* Simple Product Template.
*/
class SimpleProductTemplate extends AbstractProductFormTemplate implements ProductFormTemplateInterface {
/**
* The context name used to identify the editor.
*/
const GROUP_IDS = array(
'GENERAL' => 'general',
'ORGANIZATION' => 'organization',
'PRICING' => 'pricing',
'INVENTORY' => 'inventory',
'SHIPPING' => 'shipping',
'VARIATIONS' => 'variations',
);
/**
* SimpleProductTemplate constructor.
*/
public function __construct() {
$this->add_group_blocks();
$this->add_general_group_blocks();
$this->add_organization_group_blocks();
$this->add_pricing_group_blocks();
$this->add_inventory_group_blocks();
$this->add_shipping_group_blocks();
$this->add_variation_group_blocks();
}
/**
* Get the template ID.
*/
public function get_id(): string {
return 'simple-product';
}
/**
* Get the template title.
*/
public function get_title(): string {
return __( 'Simple Product Template', 'woocommerce' );
}
/**
* Get the template description.
*/
public function get_description(): string {
return __( 'Template for the simple product form', 'woocommerce' );
}
/**
* Adds the group blocks to the template.
*/
private function add_group_blocks() {
$this->add_group(
[
'id' => $this::GROUP_IDS['GENERAL'],
'order' => 10,
'attributes' => [
'title' => __( 'General', 'woocommerce' ),
],
]
);
$this->add_group(
[
'id' => $this::GROUP_IDS['ORGANIZATION'],
'order' => 15,
'attributes' => [
'title' => __( 'Organization', 'woocommerce' ),
],
]
);
$this->add_group(
[
'id' => $this::GROUP_IDS['PRICING'],
'order' => 20,
'attributes' => [
'title' => __( 'Pricing', 'woocommerce' ),
],
]
);
$this->add_group(
[
'id' => $this::GROUP_IDS['INVENTORY'],
'order' => 30,
'attributes' => [
'title' => __( 'Inventory', 'woocommerce' ),
],
]
);
$this->add_group(
[
'id' => $this::GROUP_IDS['SHIPPING'],
'order' => 40,
'attributes' => [
'title' => __( 'Shipping', 'woocommerce' ),
],
]
);
if ( Features::is_enabled( 'product-variation-management' ) ) {
$this->add_group(
[
'id' => $this::GROUP_IDS['VARIATIONS'],
'order' => 50,
'attributes' => [
'title' => __( 'Variations', 'woocommerce' ),
],
]
);
}
}
/**
* Adds the general group blocks to the template.
*/
private function add_general_group_blocks() {
$general_group = $this->get_group_by_id( $this::GROUP_IDS['GENERAL'] );
// Basic Details Section.
$basic_details = $general_group->add_section(
[
'id' => 'basic-details',
'order' => 10,
'attributes' => [
'title' => __( 'Basic details', 'woocommerce' ),
'description' => __( 'This info will be displayed on the product page, category pages, social media, and search results.', 'woocommerce' ),
],
]
);
$basic_details->add_block(
[
'id' => 'product-name',
'blockName' => 'woocommerce/product-name-field',
'order' => 10,
'attributes' => [
'name' => 'Product name',
'autoFocus' => true,
],
]
);
$basic_details->add_block(
[
'id' => 'product-summary',
'blockName' => 'woocommerce/product-summary-field',
'order' => 20,
]
);
$pricing_columns = $basic_details->add_block(
[
'id' => 'product-pricing-columns',
'blockName' => 'core/columns',
'order' => 30,
]
);
$pricing_column_1 = $pricing_columns->add_block(
[
'id' => 'product-pricing-column-1',
'blockName' => 'core/column',
'order' => 10,
'attributes' => [
'templateLock' => 'all',
],
]
);
$pricing_column_1->add_block(
[
'id' => 'product-regular-price',
'blockName' => 'woocommerce/product-regular-price-field',
'order' => 10,
'attributes' => [
'name' => 'regular_price',
'label' => __( 'List price', 'woocommerce' ),
/* translators: PricingTab: This is a link tag to the pricing tab. */
'help' => __( 'Manage more settings in <PricingTab>Pricing.</PricingTab>', 'woocommerce' ),
],
]
);
$pricing_column_2 = $pricing_columns->add_block(
[
'id' => 'product-pricing-column-2',
'blockName' => 'core/column',
'order' => 20,
'attributes' => [
'templateLock' => 'all',
],
]
);
$pricing_column_2->add_block(
[
'id' => 'product-sale-price',
'blockName' => 'woocommerce/product-sale-price-field',
'order' => 10,
'attributes' => [
'label' => __( 'Sale price', 'woocommerce' ),
],
]
);
// Description section.
$description_section = $general_group->add_section(
[
'id' => 'product-description-section',
'order' => 20,
'attributes' => [
'title' => __( 'Description', 'woocommerce' ),
'description' => __( 'What makes this product unique? What are its most important features? Enrich the product page by adding rich content using blocks.', 'woocommerce' ),
],
]
);
$description_section->add_block(
[
'id' => 'product-description',
'blockName' => 'woocommerce/product-description-field',
'order' => 10,
]
);
// Images section.
$images_section = $general_group->add_section(
[
'id' => 'product-images-section',
'order' => 30,
'attributes' => [
'title' => __( 'Images', 'woocommerce' ),
'description' => sprintf(
/* translators: %1$s: Images guide link opening tag. %2$s: Images guide link closing tag. */
__( 'Drag images, upload new ones or select files from your library. For best results, use JPEG files that are 1000 by 1000 pixels or larger. %1$sHow to prepare images?%2$s', 'woocommerce' ),
'<a href="http://woocommerce.com/#" target="_blank" rel="noreferrer">',
'</a>'
),
],
]
);
$images_section->add_block(
[
'id' => 'product-images',
'blockName' => 'woocommerce/product-images-field',
'order' => 10,
'attributes' => [
'images' => [],
],
]
);
}
/**
* Adds the organization group blocks to the template.
*/
private function add_organization_group_blocks() {
$organization_group = $this->get_group_by_id( $this::GROUP_IDS['ORGANIZATION'] );
// Product Catalog Section.
$product_catalog_section = $organization_group->add_section(
[
'id' => 'product-catalog-section',
'order' => 10,
'attributes' => [
'title' => __( 'Product catalog', 'woocommerce' ),
],
]
);
$product_catalog_section->add_block(
[
'id' => 'product-categories',
'blockName' => 'woocommerce/product-taxonomy-field',
'order' => 10,
'attributes' => [
'slug' => 'product_cat',
'property' => 'categories',
'label' => __( 'Categories', 'woocommerce' ),
'createTitle' => __( 'Create new category', 'woocommerce' ),
'dialogNameHelpText' => __( 'Shown to customers on the product page.', 'woocommerce' ),
'parentTaxonomyText' => __( 'Parent category', 'woocommerce' ),
],
]
);
$product_catalog_section->add_block(
[
'id' => 'product-tags',
'blockName' => 'woocommerce/product-tag-field',
'attributes' => [
'name' => 'tags',
],
]
);
$product_catalog_section->add_block(
[
'id' => 'product-catalog-search-visibility',
'blockName' => 'woocommerce/product-catalog-visibility-field',
'order' => 20,
'attributes' => [
'label' => __( 'Hide in product catalog', 'woocommerce' ),
'visibility' => 'search',
],
]
);
$product_catalog_section->add_block(
[
'id' => 'product-catalog-catalog-visibility',
'blockName' => 'woocommerce/product-catalog-visibility-field',
'order' => 30,
'attributes' => [
'label' => __( 'Hide from search results', 'woocommerce' ),
'visibility' => 'catalog',
],
]
);
$product_catalog_section->add_block(
[
'id' => 'product-enable-product-reviews',
'blockName' => 'woocommerce/product-checkbox-field',
'order' => 40,
'attributes' => [
'label' => __( 'Enable product reviews', 'woocommerce' ),
'property' => 'reviews_allowed',
],
]
);
$product_catalog_section->add_block(
[
'id' => 'product-post-password',
'blockName' => 'woocommerce/product-password-field',
'order' => 50,
'attributes' => [
'label' => __( 'Require a password', 'woocommerce' ),
],
]
);
// Attributes section.
$product_catalog_section = $organization_group->add_section(
[
'id' => 'product-attributes-section',
'order' => 20,
'attributes' => [
'title' => __( 'Attributes', 'woocommerce' ),
],
]
);
$product_catalog_section->add_block(
[
'id' => 'product-attributes',
'blockName' => 'woocommerce/product-attributes-field',
'order' => 10,
]
);
}
/**
* Adds the pricing group blocks to the template.
*/
private function add_pricing_group_blocks() {
$pricing_group = $this->get_group_by_id( $this::GROUP_IDS['PRICING'] );
$pricing_group->add_block(
[
'id' => 'pricing-has-variations-notice',
'blockName' => 'woocommerce/product-has-variations-notice',
'order' => 10,
'attributes' => [
'content' => __( 'This product has options, such as size or color. You can now manage each variation\'s price and other details individually.', 'woocommerce' ),
'buttonText' => __( 'Go to Variations', 'woocommerce' ),
'type' => 'info',
],
]
);
// Product Pricing Section.
$product_pricing_section = $pricing_group->add_section(
[
'id' => 'product-pricing-section',
'order' => 20,
'attributes' => [
'title' => __( 'Pricing', 'woocommerce' ),
'description' => sprintf(
/* translators: %1$s: Images guide link opening tag. %2$s: Images guide link closing tag.*/
__( 'Set a competitive price, put the product on sale, and manage tax calculations. %1$sHow to price your product?%2$s', 'woocommerce' ),
'<a href="https://woocommerce.com/posts/how-to-price-products-strategies-expert-tips/" target="_blank" rel="noreferrer">',
'</a>'
),
'blockGap' => 'unit-40',
],
]
);
$pricing_columns = $product_pricing_section->add_block(
[
'id' => 'product-pricing-group-pricing-columns',
'blockName' => 'core/columns',
'order' => 10,
]
);
$pricing_column_1 = $pricing_columns->add_block(
[
'id' => 'product-pricing-group-pricing-column-1',
'blockName' => 'core/column',
'order' => 10,
'attributes' => [
'templateLock' => 'all',
],
]
);
$pricing_column_1->add_block(
[
'id' => 'product-pricing-regular-price',
'blockName' => 'woocommerce/product-regular-price-field',
'order' => 10,
'attributes' => [
'name' => 'regular_price',
'label' => __( 'List price', 'woocommerce' ),
],
]
);
$pricing_column_2 = $pricing_columns->add_block(
[
'id' => 'product-pricing-group-pricing-column-2',
'blockName' => 'core/column',
'order' => 20,
'attributes' => [
'templateLock' => 'all',
],
]
);
$pricing_column_2->add_block(
[
'id' => 'product-pricing-sale-price',
'blockName' => 'woocommerce/product-sale-price-field',
'order' => 10,
'attributes' => [
'label' => __( 'Sale price', 'woocommerce' ),
],
]
);
$product_pricing_section->add_block(
[
'id' => 'product-pricing-schedule-sale-fields',
'blockName' => 'woocommerce/product-schedule-sale-fields',
'order' => 20,
]
);
$product_pricing_section->add_block(
[
'id' => 'product-sale-tax',
'blockName' => 'woocommerce/product-radio-field',
'order' => 30,
'attributes' => [
'title' => __( 'Charge sales tax on', 'woocommerce' ),
'property' => 'tax_status',
'options' => [
[
'label' => __( 'Product and shipping', 'woocommerce' ),
'value' => 'taxable',
],
[
'label' => __( 'Only shipping', 'woocommerce' ),
'value' => 'shipping',
],
[
'label' => __( "Don't charge tax", 'woocommerce' ),
'value' => 'none',
],
],
],
]
);
$pricing_advanced_block = $product_pricing_section->add_block(
[
'id' => 'product-pricing-advanced',
'blockName' => 'woocommerce/product-collapsible',
'order' => 40,
'attributes' => [
'toggleText' => __( 'Advanced', 'woocommerce' ),
'initialCollapsed' => true,
'persistRender' => true,
],
]
);
$pricing_advanced_block->add_block(
[
'id' => 'product-tax-class',
'blockName' => 'woocommerce/product-radio-field',
'order' => 10,
'attributes' => [
'title' => __( 'Tax class', 'woocommerce' ),
'description' => sprintf(
/* translators: %1$s: Learn more link opening tag. %2$s: Learn more link closing tag.*/
__( 'Apply a tax rate if this product qualifies for tax reduction or exemption. %1$sLearn more%2$s.', 'woocommerce' ),
'<a href="https://woocommerce.com/document/setting-up-taxes-in-woocommerce/#shipping-tax-class" target="_blank" rel="noreferrer">',
'</a>'
),
'property' => 'tax_class',
'options' => [
[
'label' => __( 'Standard', 'woocommerce' ),
'value' => '',
],
[
'label' => __( 'Reduced rate', 'woocommerce' ),
'value' => 'reduced-rate',
],
[
'label' => __( 'Zero rate', 'woocommerce' ),
'value' => 'zero-rate',
],
],
],
]
);
}
/**
* Adds the inventory group blocks to the template.
*/
private function add_inventory_group_blocks() {
$inventory_group = $this->get_group_by_id( $this::GROUP_IDS['INVENTORY'] );
$inventory_group->add_block(
[
'id' => 'product_variation_notice_inventory_tab',
'blockName' => 'woocommerce/product-has-variations-notice',
'order' => 10,
'attributes' => [
'content' => __( 'This product has options, such as size or color. You can now manage each variation\'s price and other details individually.', 'woocommerce' ),
'buttonText' => __( 'Go to Variations', 'woocommerce' ),
'type' => 'info',
],
]
);
// Product Pricing Section.
$product_inventory_section = $inventory_group->add_section(
[
'id' => 'product-inventory-section',
'order' => 20,
'attributes' => [
'title' => __( 'Inventory', 'woocommerce' ),
'description' => sprintf(
/* translators: %1$s: Inventory settings link opening tag. %2$s: Inventory settings link closing tag.*/
__( 'Set up and manage inventory for this product, including status and available quantity. %1$sManage store inventory settings%2$s', 'woocommerce' ),
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=products§ion=inventory' ) . '" target="_blank" rel="noreferrer">',
'</a>'
),
'blockGap' => 'unit-40',
],
]
);
$product_inventory_inner_section = $product_inventory_section->add_section(
[
'id' => 'product-inventory-inner-section',
'order' => 10,
]
);
$product_inventory_inner_section->add_block(
[
'id' => 'product-sku-field',
'blockName' => 'woocommerce/product-sku-field',
'order' => 10,
]
);
$product_inventory_inner_section->add_block(
[
'id' => 'product-track-stock',
'blockName' => 'woocommerce/product-toggle-field',
'order' => 20,
'attributes' => [
'label' => __( 'Track stock quantity for this product', 'woocommerce' ),
'property' => 'manage_stock',
'disabled' => 'yes' !== get_option( 'woocommerce_manage_stock' ),
'disabledCopy' => sprintf(
/* translators: %1$s: Learn more link opening tag. %2$s: Learn more link closing tag.*/
__( 'Per your %1$sstore settings%2$s, inventory management is <strong>disabled</strong>.', 'woocommerce' ),
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=products§ion=inventory' ) . '" target="_blank" rel="noreferrer">',
'</a>'
),
],
]
);
$product_inventory_quantity_conditional = $product_inventory_inner_section->add_block(
[
'id' => 'product-inventory-quantity-conditional-wrapper',
'blockName' => 'woocommerce/conditional',
'order' => 30,
'attributes' => [
'mustMatch' => [
'manage_stock' => [ true ],
],
],
]
);
$product_inventory_quantity_conditional->add_block(
[
'id' => 'product-inventory-quantity',
'blockName' => 'woocommerce/product-inventory-quantity-field',
'order' => 10,
]
);
$product_stock_status_conditional = $product_inventory_section->add_block(
[
'id' => 'product-stock-status-conditional-wrapper',
'blockName' => 'woocommerce/conditional',
'order' => 20,
'attributes' => [
'mustMatch' => [
'manage_stock' => [ false ],
],
],
]
);
$product_stock_status_conditional->add_block(
[
'id' => 'product-stock-status',
'blockName' => 'woocommerce/product-radio-field',
'order' => 10,
'attributes' => [
'title' => __( 'Stock status', 'woocommerce' ),
'property' => 'stock_status',
'options' => [
[
'label' => __( 'In stock', 'woocommerce' ),
'value' => 'instock',
],
[
'label' => __( 'Out of stock', 'woocommerce' ),
'value' => 'outofstock',
],
[
'label' => __( 'On backorder', 'woocommerce' ),
'value' => 'onbackorder',
],
],
],
]
);
$product_inventory_advanced = $product_inventory_section->add_block(
[
'id' => 'product-inventory-advanced',
'blockName' => 'woocommerce/product-collapsible',
'order' => 30,
'attributes' => [
'toggleText' => __( 'Advanced', 'woocommerce' ),
'initialCollapsed' => true,
'persistRender' => true,
],
]
);
$product_inventory_advanced_wrapper = $product_inventory_advanced->add_block(
[
'blockName' => 'woocommerce/product-section',
'order' => 10,
'attributes' => [
'blockGap' => 'unit-40',
],
]
);
$product_out_of_stock_conditional = $product_inventory_advanced_wrapper->add_block(
[
'id' => 'product-out-of-stock-conditional-wrapper',
'blockName' => 'woocommerce/conditional',
'order' => 10,
'attributes' => [
'mustMatch' => [
'manage_stock' => [ true ],
],
],
]
);
$product_out_of_stock_conditional->add_block(
[
'id' => 'product-out-of-stock',
'blockName' => 'woocommerce/product-radio-field',
'order' => 10,
'attributes' => [
'title' => __( 'When out of stock', 'woocommerce' ),
'property' => 'backorders',
'options' => [
[
'label' => __( 'Allow purchases', 'woocommerce' ),
'value' => 'yes',
],
[
'label' => __(
'Allow purchases, but notify customers',
'woocommerce'
),
'value' => 'notify',
],
[
'label' => __( "Don't allow purchases", 'woocommerce' ),
'value' => 'no',
],
],
],
]
);
$product_out_of_stock_conditional->add_block(
[
'id' => 'product-inventory-email',
'blockName' => 'woocommerce/product-inventory-email-field',
'order' => 20,
]
);
$product_inventory_advanced_wrapper->add_block(
[
'id' => 'product-limit-purchase',
'blockName' => 'woocommerce/product-checkbox-field',
'order' => 20,
'attributes' => [
'title' => __(
'Restrictions',
'woocommerce'
),
'label' => __(
'Limit purchases to 1 item per order',
'woocommerce'
),
'property' => 'sold_individually',
'tooltip' => __(
'When checked, customers will be able to purchase only 1 item in a single order. This is particularly useful for items that have limited quantity, like art or handmade goods.',
'woocommerce'
),
],
]
);
}
/**
* Adds the shipping group blocks to the template.
*/
private function add_shipping_group_blocks() {
$shipping_group = $this->get_group_by_id( $this::GROUP_IDS['SHIPPING'] );
$shipping_group->add_block(
[
'id' => 'product_variation_notice_shipping_tab',
'blockName' => 'woocommerce/product-has-variations-notice',
'order' => 10,
'attributes' => [
'content' => __( 'This product has options, such as size or color. You can now manage each variation\'s price and other details individually.', 'woocommerce' ),
'buttonText' => __( 'Go to Variations', 'woocommerce' ),
'type' => 'info',
],
]
);
// Product Pricing Section.
$product_fee_and_dimensions_section = $shipping_group->add_section(
[
'id' => 'product-fee-and-dimensions-section',
'order' => 20,
'attributes' => [
'title' => __( 'Fees & dimensions', 'woocommerce' ),
'description' => sprintf(
/* translators: %1$s: How to get started? link opening tag. %2$s: How to get started? link closing tag.*/
__( 'Set up shipping costs and enter dimensions used for accurate rate calculations. %1$sHow to get started?%2$s.', 'woocommerce' ),
'<a href="https://woocommerce.com/posts/how-to-calculate-shipping-costs-for-your-woocommerce-store/" target="_blank" rel="noreferrer">',
'</a>'
),
],
]
);
$product_fee_and_dimensions_section->add_block(
[
'id' => 'product-shipping-class',
'blockName' => 'woocommerce/product-shipping-class-field',
'order' => 10,
]
);
$product_fee_and_dimensions_section->add_block(
[
'id' => 'product-shipping-dimensions',
'blockName' => 'woocommerce/product-shipping-dimensions-fields',
'order' => 20,
]
);
}
/**
* Adds the variation group blocks to the template.
*/
private function add_variation_group_blocks() {
$variation_group = $this->get_group_by_id( $this::GROUP_IDS['VARIATIONS'] );
if ( ! $variation_group ) {
return;
}
$variation_fields = $variation_group->add_block(
[
'id' => 'product_variation-field-group',
'blockName' => 'woocommerce/product-variations-fields',
'order' => 10,
'attributes' => [
'description' => sprintf(
/* translators: %1$s: Sell your product in multiple variations like size or color. strong opening tag. %2$s: Sell your product in multiple variations like size or color. strong closing tag.*/
__( '%1$sSell your product in multiple variations like size or color.%2$s Get started by adding options for the buyers to choose on the product page.', 'woocommerce' ),
'<strong>',
'</strong>'
),
],
]
);
$variation_options_section = $variation_fields->add_block(
[
'id' => 'product-variation-options-section',
'blockName' => 'woocommerce/product-section',
'order' => 10,
'attributes' => [
'title' => __( 'Variation options', 'woocommerce' ),
],
]
);
$variation_options_section->add_block(
[
'id' => 'product-variation-options',
'blockName' => 'woocommerce/product-variations-options-field',
]
);
$variation_section = $variation_fields->add_block(
[
'id' => 'product-variation-section',
'blockName' => 'woocommerce/product-section',
'order' => 20,
'attributes' => [
'title' => __( 'Variations', 'woocommerce' ),
],
]
);
$variation_section->add_block(
[
'id' => 'product-variation-items',
'blockName' => 'woocommerce/product-variation-items-field',
'order' => 10,
]
);
}
}
Features/ProductBlockEditor/RedirectionController.php 0000644 00000010131 15153746750 0017121 0 ustar 00 <?php
/**
* WooCommerce Product Editor Redirection Controller
*/
namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor;
use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
/**
* Handle redirecting to the old or new editor based on features and support.
*/
class RedirectionController {
/**
* Supported post types.
*
* @var array
*/
private $supported_post_types;
/**
* Set up the hooks used for redirection.
*
* @param array $supported_post_types Array of supported post types.
*/
public function __construct( $supported_post_types ) {
$this->supported_post_types = $supported_post_types;
if ( \Automattic\WooCommerce\Utilities\FeaturesUtil::feature_is_enabled( 'product_block_editor' ) ) {
add_action( 'current_screen', array( $this, 'maybe_redirect_to_new_editor' ), 30, 0 );
add_action( 'current_screen', array( $this, 'redirect_non_supported_product_types' ), 30, 0 );
} else {
add_action( 'current_screen', array( $this, 'maybe_redirect_to_old_editor' ), 30, 0 );
}
}
/**
* Check if the current screen is the legacy add product screen.
*/
protected function is_legacy_add_new_screen(): bool {
$screen = get_current_screen();
return 'post' === $screen->base && 'product' === $screen->post_type && 'add' === $screen->action;
}
/**
* Check if the current screen is the legacy edit product screen.
*/
protected function is_legacy_edit_screen(): bool {
$screen = get_current_screen();
return 'post' === $screen->base
&& 'product' === $screen->post_type
&& isset( $_GET['post'] )
&& isset( $_GET['action'] )
&& 'edit' === $_GET['action'];
}
/**
* Check if a product is supported by the new experience.
*
* @param integer $product_id Product ID.
*/
protected function is_product_supported( $product_id ): bool {
$product = $product_id ? wc_get_product( $product_id ) : null;
$digital_product = $product->is_downloadable() || $product->is_virtual();
return $product && in_array( $product->get_type(), $this->supported_post_types, true ) && ! $digital_product;
}
/**
* Redirects from old product form to the new product form if the
* feature `product_block_editor` is enabled.
*/
public function maybe_redirect_to_new_editor(): void {
if ( $this->is_legacy_add_new_screen() ) {
wp_safe_redirect( admin_url( 'admin.php?page=wc-admin&path=/add-product' ) );
exit();
}
if ( $this->is_legacy_edit_screen() ) {
$product_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : null;
if ( ! $this->is_product_supported( $product_id ) ) {
return;
}
wp_safe_redirect( admin_url( 'admin.php?page=wc-admin&path=/product/' . $product_id ) );
exit();
}
}
/**
* Redirects from new product form to the old product form if the
* feature `product_block_editor` is enabled.
*/
public function maybe_redirect_to_old_editor(): void {
$route = $this->get_parsed_route();
if ( 'add-product' === $route['page'] ) {
wp_safe_redirect( admin_url( 'post-new.php?post_type=product' ) );
exit();
}
if ( 'product' === $route['page'] ) {
wp_safe_redirect( admin_url( 'post.php?post=' . $route['product_id'] . '&action=edit' ) );
exit();
}
}
/**
* Get the parsed WooCommerce Admin path.
*/
protected function get_parsed_route(): array {
if ( ! \Automattic\WooCommerce\Admin\PageController::is_admin_page() || ! isset( $_GET['path'] ) ) {
return array(
'page' => null,
'product_id' => null,
);
}
$path = esc_url_raw( wp_unslash( $_GET['path'] ) );
$path_pieces = explode( '/', wp_parse_url( $path, PHP_URL_PATH ) );
return array(
'page' => $path_pieces[1],
'product_id' => 'product' === $path_pieces[1] ? absint( $path_pieces[2] ) : null,
);
}
/**
* Redirect non supported product types to legacy editor.
*/
public function redirect_non_supported_product_types(): void {
$route = $this->get_parsed_route();
$product_id = $route['product_id'];
if ( 'product' === $route['page'] && ! $this->is_product_supported( $product_id ) ) {
wp_safe_redirect( admin_url( 'post.php?post=' . $route['product_id'] . '&action=edit' ) );
exit();
}
}
}
Features/ProductBlockEditor/Tracks.php 0000644 00000002263 15153746750 0014044 0 ustar 00 <?php
/**
* WooCommerce Product Block Editor
*/
namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor;
/**
* Add tracks for the product block editor.
*/
class Tracks {
/**
* Initialize the tracks.
*/
public function init() {
add_filter( 'woocommerce_product_source', array( $this, 'add_product_source' ) );
}
/**
* Check if a URL is a product editor page.
*
* @param string $url Url to check.
* @return boolean
*/
protected function is_product_editor_page( $url ) {
$query_string = wp_parse_url( wp_get_referer(), PHP_URL_QUERY );
parse_str( $query_string, $query );
if ( ! isset( $query['page'] ) || 'wc-admin' !== $query['page'] || ! isset( $query['path'] ) ) {
return false;
}
$path_pieces = explode( '/', $query['path'] );
$route = $path_pieces[1];
return 'add-product' === $route || 'product' === $route;
}
/**
* Update the product source if we're on the product editor page.
*
* @param string $source Source of product.
* @return string
*/
public function add_product_source( $source ) {
if ( $this->is_product_editor_page( wp_get_referer() ) ) {
return 'product-block-editor-v1';
}
return $source;
}
}
Features/ShippingPartnerSuggestions/DefaultShippingPartners.php 0000644 00000020727 15153746750 0021235 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\ShippingPartnerSuggestions;
/**
* Default Shipping Partners
*/
class DefaultShippingPartners {
/**
* Get default specs.
*
* @return array Default specs.
*/
public static function get_all() {
$asset_base_url = WC()->plugin_url() . '/assets/images/shipping_partners/';
$column_layout_features = array(
array(
'icon' => $asset_base_url . 'timer.svg',
'title' => __( 'Save time', 'woocommerce' ),
'description' => __(
'Automatically import order information to quickly print your labels.',
'woocommerce'
),
),
array(
'icon' => $asset_base_url . 'discount.svg',
'title' => __( 'Save money', 'woocommerce' ),
'description' => __(
'Shop for the best shipping rates, and access pre-negotiated discounted rates.',
'woocommerce'
),
),
array(
'icon' => $asset_base_url . 'star.svg',
'title' => __( 'Wow your shoppers', 'woocommerce' ),
'description' => __(
'Keep your customers informed with tracking notifications.',
'woocommerce'
),
),
);
$check_icon = $asset_base_url . 'check.svg';
return array(
array(
'name' => 'ShipStation',
'slug' => 'woocommerce-shipstation-integration',
'description' => __( 'Powerful yet easy-to-use solution:', 'woocommerce' ),
'layout_column' => array(
'image' => $asset_base_url . 'shipstation-column.svg',
'features' => $column_layout_features,
),
'layout_row' => array(
'image' => $asset_base_url . 'shipstation-row.svg',
'features' => array(
array(
'icon' => $check_icon,
'description' => __(
'Print labels from Royal Mail, Parcel Force, DPD, and many more',
'woocommerce'
),
),
array(
'icon' => $check_icon,
'description' => __(
'Shop for the best rates, in real-time',
'woocommerce'
),
),
array(
'icon' => $check_icon,
'description' => __( 'Connect selling channels easily', 'woocommerce' ),
),
array(
'icon' => $check_icon,
'description' => __( 'Advance automated workflows', 'woocommerce' ),
),
array(
'icon' => $check_icon,
'description' => __( '30-days free trial', 'woocommerce' ),
),
),
),
'learn_more_link' => 'https://wordpress.org/plugins/woocommerce-shipstation-integration/',
'is_visible' => array(
self::get_rules_for_countries( array( 'AU', 'CA', 'GB' ) ),
),
'available_layouts' => array( 'row', 'column' ),
),
array(
'name' => 'Skydropx',
'slug' => 'skydropx-cotizador-y-envios',
'layout_column' => array(
'image' => $asset_base_url . 'skydropx-column.svg',
'features' => $column_layout_features,
),
'description' => '',
'learn_more_link' => 'https://wordpress.org/plugins/skydropx-cotizador-y-envios/',
'is_visible' => array(
self::get_rules_for_countries( array( 'MX', 'CO' ) ),
),
'available_layouts' => array( 'column' ),
),
array(
'name' => 'Envia',
'slug' => '',
'description' => '',
'layout_column' => array(
'image' => $asset_base_url . 'envia-column.svg',
'features' => $column_layout_features,
),
'learn_more_link' => 'https://woocommerce.com/products/envia-shipping-and-fulfillment/',
'is_visible' => array(
self::get_rules_for_countries( array( 'CL', 'AR', 'PE', 'BR', 'UY', 'GT' ) ),
),
'available_layouts' => array( 'column' ),
),
array(
'name' => 'Sendcloud',
'slug' => 'sendcloud-shipping',
'description' => __( 'All-in-one shipping tool:', 'woocommerce' ),
'layout_column' => array(
'image' => $asset_base_url . 'sendcloud-column.svg',
'features' => $column_layout_features,
),
'layout_row' => array(
'image' => $asset_base_url . 'sendcloud-row.svg',
'features' => array(
array(
'icon' => $check_icon,
'description' => __( 'Print labels from 80+ carriers', 'woocommerce' ),
),
array(
'icon' => $check_icon,
'description' => __(
'Process orders in just a few clicks',
'woocommerce'
),
),
array(
'icon' => $check_icon,
'description' => __( 'Customize checkout options', 'woocommerce' ),
),
array(
'icon' => $check_icon,
'description' => __( 'Self-service tracking & returns', 'woocommerce' ),
),
array(
'icon' => $check_icon,
'description' => __( 'Start with a free plan', 'woocommerce' ),
),
),
),
'learn_more_link' => 'https://wordpress.org/plugins/sendcloud-shipping/',
'is_visible' => array(
self::get_rules_for_countries( array( 'NL', 'AT', 'BE', 'FR', 'DE', 'ES', 'GB', 'IT' ) ),
),
'available_layouts' => array( 'row', 'column' ),
),
array(
'name' => 'Packlink',
'slug' => 'packlink-pro-shipping',
'description' => __( 'Optimize your full shipping process:', 'woocommerce' ),
'layout_column' => array(
'image' => $asset_base_url . 'packlink-column.svg',
'features' => $column_layout_features,
),
'layout_row' => array(
'image' => $asset_base_url . 'packlink-row.svg',
'features' => array(
array(
'icon' => $check_icon,
'description' => __(
'Automated, real-time order import',
'woocommerce'
),
),
array(
'icon' => $check_icon,
'description' => __(
'Direct access to leading carriers',
'woocommerce'
),
),
array(
'icon' => $check_icon,
'description' => __(
'Access competitive shipping prices',
'woocommerce'
),
),
array(
'icon' => $check_icon,
'description' => __( 'Quickly bulk print labels', 'woocommerce' ),
),
array(
'icon' => $check_icon,
'description' => __( 'Free shipping platform', 'woocommerce' ),
),
),
),
'learn_more_link' => 'https://wordpress.org/plugins/packlink-pro-shipping/',
'is_visible' => array(
self::get_rules_for_countries( array( 'FR', 'DE', 'ES', 'IT' ) ),
),
'available_layouts' => array( 'row', 'column' ),
),
array(
'name' => 'WooCommerce Shipping',
'slug' => 'woocommerce-services',
'description' => __( 'Save time and money by printing your shipping labels right from your computer with WooCommerce Shipping. Try WooCommerce Shipping for free.', 'woocommerce' ),
'dependencies' => array( 'jetpack' ),
'layout_column' => array(
'image' => $asset_base_url . 'wcs-column.svg',
'features' => array(
array(
'icon' => $asset_base_url . 'printer.svg',
'title' => __( 'Buy postage when you need it', 'woocommerce' ),
'description' => __( 'No need to wonder where that stampbook went.', 'woocommerce' ),
),
array(
'icon' => $asset_base_url . 'paper.svg',
'title' => __( 'Print at home', 'woocommerce' ),
'description' => __( 'Pick up an order, then just pay, print, package and post.', 'woocommerce' ),
),
array(
'icon' => $asset_base_url . 'discount.svg',
'title' => __( 'Discounted rates', 'woocommerce' ),
'description' => __( 'Access discounted shipping rates with DHL and USPS.', 'woocommerce' ),
),
),
),
'learn_more_link' => 'https://woocommerce.com/products/shipping/',
'is_visible' => array(
self::get_rules_for_countries( array( 'US' ) ),
),
'available_layouts' => array( 'column' ),
),
);
}
/**
* Get rules that match the store base location to one of the provided countries.
*
* @param array $countries Array of countries to match.
* @return object Rules to match.
*/
public static function get_rules_for_countries( $countries ) {
$rules = array();
foreach ( $countries as $country ) {
$rules[] = (object) array(
'type' => 'base_location_country',
'value' => $country,
'operation' => '=',
);
}
return (object) array(
'type' => 'or',
'operands' => $rules,
);
}
}
Features/ShippingPartnerSuggestions/ShippingPartnerSuggestions.php 0000644 00000003676 15153746750 0022004 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\ShippingPartnerSuggestions;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RuleEvaluator;
/**
* Class ShippingPartnerSuggestions
*/
class ShippingPartnerSuggestions {
/**
* Go through the specs and run them.
*
* @param array|null $specs shipping partner suggestion spec array.
* @return array
*/
public static function get_suggestions( $specs = null ) {
$suggestions = array();
if ( null === $specs ) {
$specs = self::get_specs_from_datasource();
}
$rule_evaluator = new RuleEvaluator();
foreach ( $specs as &$spec ) {
$spec = is_array( $spec ) ? (object) $spec : $spec;
if ( isset( $spec->is_visible ) ) {
$is_visible = $rule_evaluator->evaluate( $spec->is_visible );
if ( $is_visible ) {
$spec->is_visible = true;
$suggestions[] = $spec;
}
}
}
return $suggestions;
}
/**
* Get specs or fetch remotely if they don't exist.
*/
public static function get_specs_from_datasource() {
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
/**
* It can be used to modify shipping partner suggestions spec.
*
* @since 7.4.1
*/
return apply_filters( 'woocommerce_admin_shipping_partner_suggestions_specs', DefaultShippingPartners::get_all() );
}
$specs = ShippingPartnerSuggestionsDataSourcePoller::get_instance()->get_specs_from_data_sources();
// Fetch specs if they don't yet exist.
if ( false === $specs || ! is_array( $specs ) || 0 === count( $specs ) ) {
/**
* It can be used to modify shipping partner suggestions spec.
*
* @since 7.4.1
*/
return apply_filters( 'woocommerce_admin_shipping_partner_suggestions_specs', DefaultShippingPartners::get_all() );
}
/**
* It can be used to modify shipping partner suggestions spec.
*
* @since 7.4.1
*/
return apply_filters( 'woocommerce_admin_shipping_partner_suggestions_specs', $specs );
}
}
Features/ShippingPartnerSuggestions/ShippingPartnerSuggestionsDataSourcePoller.php 0000644 00000001552 15153746750 0025124 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\Features\ShippingPartnerSuggestions;
use Automattic\WooCommerce\Admin\DataSourcePoller;
/**
* Specs data source poller class for shipping partner suggestions.
*/
class ShippingPartnerSuggestionsDataSourcePoller extends DataSourcePoller {
/**
* Data Source Poller ID.
*/
const ID = 'shipping_partner_suggestions';
/**
* Default data sources array.
*/
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/shipping-partner-suggestions/1.0/suggestions.json',
);
/**
* Class instance.
*
* @var ShippingPartnerSuggestionsDataSourcePoller instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self( self::ID, self::DATA_SOURCES );
}
return self::$instance;
}
}
Features/TransientNotices.php 0000644 00000005450 15153746750 0012350 0 ustar 00 <?php
/**
* WooCommerce Transient Notices
*/
namespace Automattic\WooCommerce\Admin\Features;
use Automattic\WooCommerce\Internal\Admin\Loader;
/**
* Shows print shipping label banner on edit order page.
*/
class TransientNotices {
/**
* Option name for the queue.
*/
const QUEUE_OPTION = 'woocommerce_admin_transient_notices_queue';
/**
* Constructor
*/
public function __construct() {
add_filter( 'woocommerce_admin_preload_options', array( $this, 'preload_options' ) );
}
/**
* Get all notices in the queue.
*
* @return array
*/
public static function get_queue() {
return get_option( self::QUEUE_OPTION, array() );
}
/**
* Get all notices in the queue by a given user ID.
*
* @param int $user_id User ID.
* @return array
*/
public static function get_queue_by_user( $user_id ) {
$notices = self::get_queue();
return array_filter(
$notices,
function( $notice ) use ( $user_id ) {
return ! isset( $notice['user_id'] ) ||
null === $notice['user_id'] ||
$user_id === $notice['user_id'];
}
);
}
/**
* Get a notice by ID.
*
* @param array $notice_id Notice of ID to get.
* @return array|null
*/
public static function get( $notice_id ) {
$queue = self::get_queue();
if ( isset( $queue[ $notice_id ] ) ) {
return $queue[ $notice_id ];
}
return null;
}
/**
* Add a notice to be shown.
*
* @param array $notice Notice.
* $notice = array(
* 'id' => (string) Unique ID for the notice. Required.
* 'user_id' => (int|null) User ID to show the notice to.
* 'status' => (string) info|error|success
* 'content' => (string) Content to be shown for the notice. Required.
* 'options' => (array) Array of options to be passed to the notice component.
* See https://developer.wordpress.org/block-editor/reference-guides/data/data-core-notices/#createNotice for available options.
* ).
*/
public static function add( $notice ) {
$queue = self::get_queue();
$defaults = array(
'user_id' => null,
'status' => 'info',
'options' => array(),
);
$notice_data = array_merge( $defaults, $notice );
$notice_data['options'] = (object) $notice_data['options'];
$queue[ $notice['id'] ] = $notice_data;
update_option( self::QUEUE_OPTION, $queue );
}
/**
* Remove a notice by ID.
*
* @param array $notice_id Notice of ID to remove.
*/
public static function remove( $notice_id ) {
$queue = self::get_queue();
unset( $queue[ $notice_id ] );
update_option( self::QUEUE_OPTION, $queue );
}
/**
* Preload options to prime state of the application.
*
* @param array $options Array of options to preload.
* @return array
*/
public function preload_options( $options ) {
$options[] = self::QUEUE_OPTION;
return $options;
}
}
Loader.php 0000644 00000050404 15153746750 0006503 0 ustar 00 <?php
/**
* Register the scripts, styles, and includes needed for pieces of the WooCommerce Admin experience.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Admin\PluginsHelper;
use Automattic\WooCommerce\Internal\Admin\BlockTemplateRegistry\BlockTemplatesController;
use Automattic\WooCommerce\Internal\Admin\ProductReviews\Reviews;
use Automattic\WooCommerce\Internal\Admin\ProductReviews\ReviewsCommentsOverrides;
use Automattic\WooCommerce\Internal\Admin\Settings;
/**
* Loader Class.
*/
class Loader {
/**
* Class instance.
*
* @var Loader instance
*/
protected static $instance = null;
/**
* An array of classes to load from the includes folder.
*
* @var array
*/
protected static $classes = array();
/**
* WordPress capability required to use analytics features.
*
* @var string
*/
protected static $required_capability = null;
/**
* An array of dependencies that have been preloaded (to avoid duplicates).
*
* @var array
*/
protected $preloaded_dependencies = array(
'script' => array(),
'style' => array(),
);
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
* Hooks added here should be removed in `wc_admin_initialize` via the feature plugin.
*/
public function __construct() {
Features::get_instance();
WCAdminSharedSettings::get_instance();
Translations::get_instance();
WCAdminUser::get_instance();
Settings::get_instance();
SiteHealth::get_instance();
SystemStatusReport::get_instance();
wc_get_container()->get( Reviews::class );
wc_get_container()->get( ReviewsCommentsOverrides::class );
wc_get_container()->get( BlockTemplatesController::class );
add_filter( 'admin_body_class', array( __CLASS__, 'add_admin_body_classes' ) );
add_filter( 'admin_title', array( __CLASS__, 'update_admin_title' ) );
add_action( 'in_admin_header', array( __CLASS__, 'embed_page_header' ) );
add_action( 'admin_head', array( __CLASS__, 'remove_notices' ) );
add_action( 'admin_head', array( __CLASS__, 'smart_app_banner' ) );
add_action( 'admin_notices', array( __CLASS__, 'inject_before_notices' ), -9999 );
add_action( 'admin_notices', array( __CLASS__, 'inject_after_notices' ), PHP_INT_MAX );
// Added this hook to delete the field woocommerce_onboarding_homepage_post_id when deleting the homepage.
add_action( 'trashed_post', array( __CLASS__, 'delete_homepage' ) );
/*
* Remove the emoji script as it always defaults to replacing emojis with Twemoji images.
* Gutenberg has also disabled emojis. More on that here -> https://github.com/WordPress/gutenberg/pull/6151
*/
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
add_action( 'admin_init', array( __CLASS__, 'deactivate_wc_admin_plugin' ) );
add_action( 'load-themes.php', array( __CLASS__, 'add_appearance_theme_view_tracks_event' ) );
}
/**
* If WooCommerce Admin is installed and activated, it will attempt to deactivate and show a notice.
*/
public static function deactivate_wc_admin_plugin() {
$plugin_path = PluginsHelper::get_plugin_path_from_slug( 'woocommerce-admin' );
if ( is_plugin_active( $plugin_path ) ) {
$path = PluginsHelper::get_plugin_path_from_slug( 'woocommerce-admin' );
deactivate_plugins( $path );
$notice_action = is_network_admin() ? 'network_admin_notices' : 'admin_notices';
add_action(
$notice_action,
function() {
echo '<div class="error"><p>';
printf(
/* translators: %s: is referring to the plugin's name. */
esc_html__( 'The %1$s plugin has been deactivated as the latest improvements are now included with the %2$s plugin.', 'woocommerce' ),
'<code>WooCommerce Admin</code>',
'<code>WooCommerce</code>'
);
echo '</p></div>';
}
);
}
}
/**
* Returns breadcrumbs for the current page.
*/
private static function get_embed_breadcrumbs() {
return wc_admin_get_breadcrumbs();
}
/**
* Outputs breadcrumbs via PHP for the initial load of an embedded page.
*
* @param array $section Section to create breadcrumb from.
*/
private static function output_heading( $section ) {
echo esc_html( $section );
}
/**
* Set up a div for the header embed to render into.
* The initial contents here are meant as a place loader for when the PHP page initialy loads.
*/
public static function embed_page_header() {
if ( ! PageController::is_admin_page() && ! PageController::is_embed_page() ) {
return;
}
if ( ! PageController::is_embed_page() ) {
return;
}
$sections = self::get_embed_breadcrumbs();
$sections = is_array( $sections ) ? $sections : array( $sections );
?>
<div id="woocommerce-embedded-root" class="is-embed-loading">
<div class="woocommerce-layout">
<div class="woocommerce-layout__header is-embed-loading">
<h1 class="woocommerce-layout__header-heading">
<?php self::output_heading( end( $sections ) ); ?>
</h1>
</div>
</div>
</div>
<?php
}
/**
* Adds body classes to the main wp-admin wrapper, allowing us to better target elements in specific scenarios.
*
* @param string $admin_body_class Body class to add.
*/
public static function add_admin_body_classes( $admin_body_class = '' ) {
if ( ! PageController::is_admin_or_embed_page() ) {
return $admin_body_class;
}
$classes = explode( ' ', trim( $admin_body_class ) );
$classes[] = 'woocommerce-admin-page';
if ( PageController::is_embed_page() ) {
$classes[] = 'woocommerce-embed-page';
}
/**
* Some routes or features like onboarding hide the wp-admin navigation and masterbar.
* Setting `woocommerce_admin_is_loading` to true allows us to premeptively hide these
* elements while the JS app loads.
* This class needs to be removed by those feature components (like <ProfileWizard />).
*
* @param bool $is_loading If WooCommerce Admin is loading a fullscreen view.
*/
$is_loading = apply_filters( 'woocommerce_admin_is_loading', false );
if ( PageController::is_admin_page() && $is_loading ) {
$classes[] = 'woocommerce-admin-is-loading';
}
$admin_body_class = implode( ' ', array_unique( $classes ) );
return " $admin_body_class ";
}
/**
* Adds an iOS "Smart App Banner" for display on iOS Safari.
* See https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html
*/
public static function smart_app_banner() {
if ( PageController::is_admin_or_embed_page() ) {
echo "
<meta name='apple-itunes-app' content='app-id=1389130815'>
";
}
}
/**
* Removes notices that should not be displayed on WC Admin pages.
*/
public static function remove_notices() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
// Hello Dolly.
if ( function_exists( 'hello_dolly' ) ) {
remove_action( 'admin_notices', 'hello_dolly' );
}
}
/**
* Runs before admin notices action and hides them.
*/
public static function inject_before_notices() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
// The JITMs won't be shown in the Onboarding Wizard.
$is_onboarding = isset( $_GET['path'] ) && '/setup-wizard' === wc_clean( wp_unslash( $_GET['path'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
$maybe_hide_jitm = $is_onboarding ? '-hide' : '';
echo '<div class="woocommerce-layout__jitm' . sanitize_html_class( $maybe_hide_jitm ) . '" id="jp-admin-notices"></div>';
// Wrap the notices in a hidden div to prevent flickering before
// they are moved elsewhere in the page by WordPress Core.
echo '<div class="woocommerce-layout__notice-list-hide" id="wp__notice-list">';
if ( PageController::is_admin_page() ) {
// Capture all notices and hide them. WordPress Core looks for
// `.wp-header-end` and appends notices after it if found.
// https://github.com/WordPress/WordPress/blob/f6a37e7d39e2534d05b9e542045174498edfe536/wp-admin/js/common.js#L737 .
echo '<div class="wp-header-end" id="woocommerce-layout__notice-catcher"></div>';
}
}
/**
* Runs after admin notices and closes div.
*/
public static function inject_after_notices() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
// Close the hidden div used to prevent notices from flickering before
// they are inserted elsewhere in the page.
echo '</div>';
}
/**
* Edits Admin title based on section of wc-admin.
*
* @param string $admin_title Modifies admin title.
* @todo Can we do some URL rewriting so we can figure out which page they are on server side?
*/
public static function update_admin_title( $admin_title ) {
if (
! did_action( 'current_screen' ) ||
! PageController::is_admin_page()
) {
return $admin_title;
}
$sections = self::get_embed_breadcrumbs();
$pieces = array();
foreach ( $sections as $section ) {
$pieces[] = is_array( $section ) ? $section[1] : $section;
}
$pieces = array_reverse( $pieces );
$title = implode( ' ‹ ', $pieces );
/* translators: %1$s: updated title, %2$s: blog info name */
return sprintf( __( '%1$s ‹ %2$s', 'woocommerce' ), $title, get_bloginfo( 'name' ) );
}
/**
* Set up a div for the app to render into.
*/
public static function page_wrapper() {
?>
<div class="wrap">
<div id="root"></div>
</div>
<?php
}
/**
* Hooks extra necessary data into the component settings array already set in WooCommerce core.
*
* @param array $settings Array of component settings.
* @return array Array of component settings.
*/
public static function add_component_settings( $settings ) {
if ( ! is_admin() ) {
return $settings;
}
if ( ! function_exists( 'wc_blocks_container' ) ) {
global $wp_locale;
// inject data not available via older versions of wc_blocks/woo.
$settings['orderStatuses'] = self::get_order_statuses( wc_get_order_statuses() );
$settings['stockStatuses'] = self::get_order_statuses( wc_get_product_stock_status_options() );
$settings['currency'] = self::get_currency_settings();
$settings['locale'] = [
'siteLocale' => isset( $settings['siteLocale'] )
? $settings['siteLocale']
: get_locale(),
'userLocale' => isset( $settings['l10n']['userLocale'] )
? $settings['l10n']['userLocale']
: get_user_locale(),
'weekdaysShort' => isset( $settings['l10n']['weekdaysShort'] )
? $settings['l10n']['weekdaysShort']
: array_values( $wp_locale->weekday_abbrev ),
];
}
$preload_data_endpoints = apply_filters( 'woocommerce_component_settings_preload_endpoints', array() );
if ( class_exists( 'Jetpack' ) ) {
$preload_data_endpoints['jetpackStatus'] = '/jetpack/v4/connection';
}
if ( ! empty( $preload_data_endpoints ) ) {
$preload_data = array_reduce(
array_values( $preload_data_endpoints ),
'rest_preload_api_request'
);
}
$preload_options = apply_filters( 'woocommerce_admin_preload_options', array() );
if ( ! empty( $preload_options ) ) {
foreach ( $preload_options as $option ) {
$settings['preloadOptions'][ $option ] = get_option( $option );
}
}
$preload_settings = apply_filters( 'woocommerce_admin_preload_settings', array() );
if ( ! empty( $preload_settings ) ) {
$setting_options = new \WC_REST_Setting_Options_V2_Controller();
foreach ( $preload_settings as $group ) {
$group_settings = $setting_options->get_group_settings( $group );
$preload_settings = [];
foreach ( $group_settings as $option ) {
if ( array_key_exists( 'id', $option ) && array_key_exists( 'value', $option ) ) {
$preload_settings[ $option['id'] ] = $option['value'];
}
}
$settings['preloadSettings'][ $group ] = $preload_settings;
}
}
$user_controller = new \WP_REST_Users_Controller();
$request = new \WP_REST_Request();
$request->set_query_params( array( 'context' => 'edit' ) );
$user_response = $user_controller->get_current_item( $request );
$current_user_data = is_wp_error( $user_response ) ? (object) array() : $user_response->get_data();
$settings['currentUserData'] = $current_user_data;
$settings['reviewsEnabled'] = get_option( 'woocommerce_enable_reviews' );
$settings['manageStock'] = get_option( 'woocommerce_manage_stock' );
$settings['commentModeration'] = get_option( 'comment_moderation' );
$settings['notifyLowStockAmount'] = get_option( 'woocommerce_notify_low_stock_amount' );
// @todo On merge, once plugin images are added to core WooCommerce, `wcAdminAssetUrl` can be retired,
// and `wcAssetUrl` can be used in its place throughout the codebase.
$settings['wcAdminAssetUrl'] = WC_ADMIN_IMAGES_FOLDER_URL;
$settings['wcVersion'] = WC_VERSION;
$settings['siteUrl'] = site_url();
$settings['shopUrl'] = get_permalink( wc_get_page_id( 'shop' ) );
$settings['homeUrl'] = home_url();
$settings['dateFormat'] = get_option( 'date_format' );
$settings['timeZone'] = wc_timezone_string();
$settings['plugins'] = array(
'installedPlugins' => PluginsHelper::get_installed_plugin_slugs(),
'activePlugins' => Plugins::get_active_plugins(),
);
// Plugins that depend on changing the translation work on the server but not the client -
// WooCommerce Branding is an example of this - so pass through the translation of
// 'WooCommerce' to wcSettings.
$settings['woocommerceTranslation'] = __( 'WooCommerce', 'woocommerce' );
// We may have synced orders with a now-unregistered status.
// E.g An extension that added statuses is now inactive or removed.
$settings['unregisteredOrderStatuses'] = self::get_unregistered_order_statuses();
// The separator used for attributes found in Variation titles.
$settings['variationTitleAttributesSeparator'] = apply_filters( 'woocommerce_product_variation_title_attributes_separator', ' - ', new \WC_Product() );
if ( ! empty( $preload_data_endpoints ) ) {
$settings['dataEndpoints'] = isset( $settings['dataEndpoints'] )
? $settings['dataEndpoints']
: [];
foreach ( $preload_data_endpoints as $key => $endpoint ) {
// Handle error case: rest_do_request() doesn't guarantee success.
if ( empty( $preload_data[ $endpoint ] ) ) {
$settings['dataEndpoints'][ $key ] = array();
} else {
$settings['dataEndpoints'][ $key ] = $preload_data[ $endpoint ]['body'];
}
}
}
$settings = self::get_custom_settings( $settings );
if ( PageController::is_embed_page() ) {
$settings['embedBreadcrumbs'] = self::get_embed_breadcrumbs();
}
$settings['allowMarketplaceSuggestions'] = WC_Marketplace_Suggestions::allow_suggestions();
$settings['connectNonce'] = wp_create_nonce( 'connect' );
$settings['wcpay_welcome_page_connect_nonce'] = wp_create_nonce( 'wcpay-connect' );
return $settings;
}
/**
* Format order statuses by removing a leading 'wc-' if present.
*
* @param array $statuses Order statuses.
* @return array formatted statuses.
*/
public static function get_order_statuses( $statuses ) {
$formatted_statuses = array();
foreach ( $statuses as $key => $value ) {
$formatted_key = preg_replace( '/^wc-/', '', $key );
$formatted_statuses[ $formatted_key ] = $value;
}
return $formatted_statuses;
}
/**
* Get all order statuses present in analytics tables that aren't registered.
*
* @return array Unregistered order statuses.
*/
public static function get_unregistered_order_statuses() {
$registered_statuses = wc_get_order_statuses();
$all_synced_statuses = OrdersDataStore::get_all_statuses();
$unregistered_statuses = array_diff( $all_synced_statuses, array_keys( $registered_statuses ) );
$formatted_status_keys = self::get_order_statuses( array_fill_keys( $unregistered_statuses, '' ) );
$formatted_statuses = array_keys( $formatted_status_keys );
return array_combine( $formatted_statuses, $formatted_statuses );
}
/**
* Register the admin settings for use in the WC REST API
*
* @param array $groups Array of setting groups.
* @return array
*/
public static function add_settings_group( $groups ) {
$groups[] = array(
'id' => 'wc_admin',
'label' => __( 'WooCommerce Admin', 'woocommerce' ),
'description' => __( 'Settings for WooCommerce admin reporting.', 'woocommerce' ),
);
return $groups;
}
/**
* Add WC Admin specific settings
*
* @param array $settings Array of settings in wc admin group.
* @return array
*/
public static function add_settings( $settings ) {
$unregistered_statuses = self::get_unregistered_order_statuses();
$registered_statuses = self::get_order_statuses( wc_get_order_statuses() );
$all_statuses = array_merge( $unregistered_statuses, $registered_statuses );
$settings[] = array(
'id' => 'woocommerce_excluded_report_order_statuses',
'option_key' => 'woocommerce_excluded_report_order_statuses',
'label' => __( 'Excluded report order statuses', 'woocommerce' ),
'description' => __( 'Statuses that should not be included when calculating report totals.', 'woocommerce' ),
'default' => array( 'pending', 'cancelled', 'failed' ),
'type' => 'multiselect',
'options' => $all_statuses,
);
$settings[] = array(
'id' => 'woocommerce_actionable_order_statuses',
'option_key' => 'woocommerce_actionable_order_statuses',
'label' => __( 'Actionable order statuses', 'woocommerce' ),
'description' => __( 'Statuses that require extra action on behalf of the store admin.', 'woocommerce' ),
'default' => array( 'processing', 'on-hold' ),
'type' => 'multiselect',
'options' => $all_statuses,
);
$settings[] = array(
'id' => 'woocommerce_default_date_range',
'option_key' => 'woocommerce_default_date_range',
'label' => __( 'Default Date Range', 'woocommerce' ),
'description' => __( 'Default Date Range', 'woocommerce' ),
'default' => 'period=month&compare=previous_year',
'type' => 'text',
);
return $settings;
}
/**
* Gets custom settings used for WC Admin.
*
* @param array $settings Array of settings to merge into.
* @return array
*/
public static function get_custom_settings( $settings ) {
$wc_rest_settings_options_controller = new \WC_REST_Setting_Options_Controller();
$wc_admin_group_settings = $wc_rest_settings_options_controller->get_group_settings( 'wc_admin' );
$settings['wcAdminSettings'] = array();
foreach ( $wc_admin_group_settings as $setting ) {
if ( ! empty( $setting['id'] ) ) {
$settings['wcAdminSettings'][ $setting['id'] ] = $setting['value'];
}
}
return $settings;
}
/**
* Return an object defining the currecy options for the site's current currency
*
* @return array Settings for the current currency {
* Array of settings.
*
* @type string $code Currency code.
* @type string $precision Number of decimals.
* @type string $symbol Symbol for currency.
* }
*/
public static function get_currency_settings() {
$code = get_woocommerce_currency();
return apply_filters(
'wc_currency_settings',
array(
'code' => $code,
'precision' => wc_get_price_decimals(),
'symbol' => html_entity_decode( get_woocommerce_currency_symbol( $code ) ),
'symbolPosition' => get_option( 'woocommerce_currency_pos' ),
'decimalSeparator' => wc_get_price_decimal_separator(),
'thousandSeparator' => wc_get_price_thousand_separator(),
'priceFormat' => html_entity_decode( get_woocommerce_price_format() ),
)
);
}
/**
* Delete woocommerce_onboarding_homepage_post_id field when the homepage is deleted
*
* @param int $post_id The deleted post id.
*/
public static function delete_homepage( $post_id ) {
if ( 'page' !== get_post_type( $post_id ) ) {
return;
}
$homepage_id = intval( get_option( 'woocommerce_onboarding_homepage_post_id', false ) );
if ( $homepage_id === $post_id ) {
delete_option( 'woocommerce_onboarding_homepage_post_id' );
}
}
/**
* Adds the appearance_theme_view Tracks event.
*/
public static function add_appearance_theme_view_tracks_event() {
wc_admin_record_tracks_event( 'appearance_theme_view', array() );
}
}
Marketing/InstalledExtensions.php 0000644 00000042437 15153746750 0013224 0 ustar 00 <?php
/**
* InstalledExtensions class file.
*/
namespace Automattic\WooCommerce\Admin\Marketing;
use Automattic\WooCommerce\Admin\PluginsHelper;
/**
* Installed Marketing Extensions class.
*/
class InstalledExtensions {
/**
* Gets an array of plugin data for the "Installed marketing extensions" card.
*
* Valid extensions statuses are: installed, activated, configured
*/
public static function get_data() {
$data = [];
$automatewoo = self::get_automatewoo_extension_data();
$aw_referral = self::get_aw_referral_extension_data();
$aw_birthdays = self::get_aw_birthdays_extension_data();
$mailchimp = self::get_mailchimp_extension_data();
$facebook = self::get_facebook_extension_data();
$pinterest = self::get_pinterest_extension_data();
$google = self::get_google_extension_data();
$amazon_ebay = self::get_amazon_ebay_extension_data();
$mailpoet = self::get_mailpoet_extension_data();
$klaviyo = self::get_klaviyo_extension_data();
$creative_mail = self::get_creative_mail_extension_data();
$tiktok = self::get_tiktok_extension_data();
$jetpack_crm = self::get_jetpack_crm_extension_data();
$zapier = self::get_zapier_extension_data();
$salesforce = self::get_salesforce_extension_data();
$vimeo = self::get_vimeo_extension_data();
$trustpilot = self::get_trustpilot_extension_data();
if ( $automatewoo ) {
$data[] = $automatewoo;
}
if ( $aw_referral ) {
$data[] = $aw_referral;
}
if ( $aw_birthdays ) {
$data[] = $aw_birthdays;
}
if ( $mailchimp ) {
$data[] = $mailchimp;
}
if ( $facebook ) {
$data[] = $facebook;
}
if ( $pinterest ) {
$data[] = $pinterest;
}
if ( $google ) {
$data[] = $google;
}
if ( $amazon_ebay ) {
$data[] = $amazon_ebay;
}
if ( $mailpoet ) {
$data[] = $mailpoet;
}
if ( $klaviyo ) {
$data[] = $klaviyo;
}
if ( $creative_mail ) {
$data[] = $creative_mail;
}
if ( $tiktok ) {
$data[] = $tiktok;
}
if ( $jetpack_crm ) {
$data[] = $jetpack_crm;
}
if ( $zapier ) {
$data[] = $zapier;
}
if ( $salesforce ) {
$data[] = $salesforce;
}
if ( $vimeo ) {
$data[] = $vimeo;
}
if ( $trustpilot ) {
$data[] = $trustpilot;
}
return $data;
}
/**
* Get allowed plugins.
*
* @return array
*/
public static function get_allowed_plugins() {
return [
'automatewoo',
'mailchimp-for-woocommerce',
'creative-mail-by-constant-contact',
'facebook-for-woocommerce',
'pinterest-for-woocommerce',
'google-listings-and-ads',
'hubspot-for-woocommerce',
'woocommerce-amazon-ebay-integration',
'mailpoet',
];
}
/**
* Get AutomateWoo extension data.
*
* @return array|bool
*/
protected static function get_automatewoo_extension_data() {
$slug = 'automatewoo';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/automatewoo.svg';
if ( 'activated' === $data['status'] && function_exists( 'AW' ) ) {
$data['settingsUrl'] = admin_url( 'admin.php?page=automatewoo-settings' );
$data['docsUrl'] = 'https://automatewoo.com/docs/';
$data['status'] = 'configured'; // Currently no configuration step.
}
return $data;
}
/**
* Get AutomateWoo Refer a Friend extension data.
*
* @return array|bool
*/
protected static function get_aw_referral_extension_data() {
$slug = 'automatewoo-referrals';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/automatewoo.svg';
if ( 'activated' === $data['status'] ) {
$data['docsUrl'] = 'https://automatewoo.com/docs/refer-a-friend/';
$data['status'] = 'configured';
if ( function_exists( 'AW_Referrals' ) ) {
$data['settingsUrl'] = admin_url( 'admin.php?page=automatewoo-settings&tab=referrals' );
}
}
return $data;
}
/**
* Get AutomateWoo Birthdays extension data.
*
* @return array|bool
*/
protected static function get_aw_birthdays_extension_data() {
$slug = 'automatewoo-birthdays';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/automatewoo.svg';
if ( 'activated' === $data['status'] ) {
$data['docsUrl'] = 'https://automatewoo.com/docs/getting-started-with-birthdays/';
$data['status'] = 'configured';
if ( function_exists( 'AW_Birthdays' ) ) {
$data['settingsUrl'] = admin_url( 'admin.php?page=automatewoo-settings&tab=birthdays' );
}
}
return $data;
}
/**
* Get MailChimp extension data.
*
* @return array|bool
*/
protected static function get_mailchimp_extension_data() {
$slug = 'mailchimp-for-woocommerce';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/mailchimp.svg';
if ( 'activated' === $data['status'] && function_exists( 'mailchimp_is_configured' ) ) {
$data['docsUrl'] = 'https://mailchimp.com/help/connect-or-disconnect-mailchimp-for-woocommerce/';
$data['settingsUrl'] = admin_url( 'admin.php?page=mailchimp-woocommerce' );
if ( mailchimp_is_configured() ) {
$data['status'] = 'configured';
}
}
return $data;
}
/**
* Get Facebook extension data.
*
* @return array|bool
*/
protected static function get_facebook_extension_data() {
$slug = 'facebook-for-woocommerce';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/facebook-icon.svg';
if ( 'activated' === $data['status'] && function_exists( 'facebook_for_woocommerce' ) ) {
$integration = facebook_for_woocommerce()->get_integration();
if ( $integration->is_configured() ) {
$data['status'] = 'configured';
}
$data['settingsUrl'] = facebook_for_woocommerce()->get_settings_url();
$data['docsUrl'] = facebook_for_woocommerce()->get_documentation_url();
}
return $data;
}
/**
* Get Pinterest extension data.
*
* @return array|bool
*/
protected static function get_pinterest_extension_data() {
$slug = 'pinterest-for-woocommerce';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/pinterest.svg';
$data['docsUrl'] = 'https://woocommerce.com/document/pinterest-for-woocommerce/?utm_medium=product';
if ( 'activated' === $data['status'] && class_exists( 'Pinterest_For_Woocommerce' ) ) {
$pinterest_onboarding_completed = Pinterest_For_Woocommerce()::is_setup_complete();
if ( $pinterest_onboarding_completed ) {
$data['status'] = 'configured';
$data['settingsUrl'] = admin_url( 'admin.php?page=wc-admin&path=/pinterest/settings' );
} else {
$data['settingsUrl'] = admin_url( 'admin.php?page=wc-admin&path=/pinterest/landing' );
}
}
return $data;
}
/**
* Get Google extension data.
*
* @return array|bool
*/
protected static function get_google_extension_data() {
$slug = 'google-listings-and-ads';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/google.svg';
if ( 'activated' === $data['status'] && function_exists( 'woogle_get_container' ) && class_exists( '\Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService' ) ) {
$merchant_center = woogle_get_container()->get( \Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService::class );
if ( $merchant_center->is_setup_complete() ) {
$data['status'] = 'configured';
$data['settingsUrl'] = admin_url( 'admin.php?page=wc-admin&path=/google/settings' );
} else {
$data['settingsUrl'] = admin_url( 'admin.php?page=wc-admin&path=/google/start' );
}
$data['docsUrl'] = 'https://woocommerce.com/document/google-listings-and-ads/?utm_medium=product';
}
return $data;
}
/**
* Get Amazon / Ebay extension data.
*
* @return array|bool
*/
protected static function get_amazon_ebay_extension_data() {
$slug = 'woocommerce-amazon-ebay-integration';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/amazon-ebay.svg';
if ( 'activated' === $data['status'] && class_exists( '\CodistoConnect' ) ) {
$codisto_merchantid = get_option( 'codisto_merchantid' );
// Use same check as codisto admin tabs.
if ( is_numeric( $codisto_merchantid ) ) {
$data['status'] = 'configured';
}
$data['settingsUrl'] = admin_url( 'admin.php?page=codisto-settings' );
$data['docsUrl'] = 'https://woocommerce.com/document/multichannel-for-woocommerce-google-amazon-ebay-walmart-integration/?utm_medium=product';
}
return $data;
}
/**
* Get MailPoet extension data.
*
* @return array|bool
*/
protected static function get_mailpoet_extension_data() {
$slug = 'mailpoet';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/mailpoet.svg';
if ( 'activated' === $data['status'] && class_exists( '\MailPoet\API\API' ) ) {
$mailpoet_api = \MailPoet\API\API::MP( 'v1' );
if ( ! method_exists( $mailpoet_api, 'isSetupComplete' ) || $mailpoet_api->isSetupComplete() ) {
$data['status'] = 'configured';
$data['settingsUrl'] = admin_url( 'admin.php?page=mailpoet-settings' );
} else {
$data['settingsUrl'] = admin_url( 'admin.php?page=mailpoet-newsletters' );
}
$data['docsUrl'] = 'https://kb.mailpoet.com/';
$data['supportUrl'] = 'https://www.mailpoet.com/support/';
}
return $data;
}
/**
* Get Klaviyo extension data.
*
* @return array|bool
*/
protected static function get_klaviyo_extension_data() {
$slug = 'klaviyo';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = plugins_url( 'assets/images/marketing/klaviyo.png', WC_PLUGIN_FILE );
if ( 'activated' === $data['status'] ) {
$klaviyo_options = get_option( 'klaviyo_settings' );
if ( isset( $klaviyo_options['klaviyo_public_api_key'] ) ) {
$data['status'] = 'configured';
}
$data['settingsUrl'] = admin_url( 'admin.php?page=klaviyo_settings' );
}
return $data;
}
/**
* Get Creative Mail for WooCommerce extension data.
*
* @return array|bool
*/
protected static function get_creative_mail_extension_data() {
$slug = 'creative-mail-by-constant-contact';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/creative-mail-by-constant-contact.png';
if ( 'activated' === $data['status'] && class_exists( '\CreativeMail\Helpers\OptionsHelper' ) ) {
if ( ! method_exists( '\CreativeMail\Helpers\OptionsHelper', 'get_instance_id' ) || \CreativeMail\Helpers\OptionsHelper::get_instance_id() !== null ) {
$data['status'] = 'configured';
$data['settingsUrl'] = admin_url( 'admin.php?page=creativemail_settings' );
} else {
$data['settingsUrl'] = admin_url( 'admin.php?page=creativemail' );
}
$data['docsUrl'] = 'https://app.creativemail.com/kb/help/WooCommerce';
$data['supportUrl'] = 'https://app.creativemail.com/kb/help/';
}
return $data;
}
/**
* Get TikTok for WooCommerce extension data.
*
* @return array|bool
*/
protected static function get_tiktok_extension_data() {
$slug = 'tiktok-for-business';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/tiktok.jpg';
if ( 'activated' === $data['status'] ) {
if ( false !== get_option( 'tt4b_access_token' ) ) {
$data['status'] = 'configured';
}
$data['settingsUrl'] = admin_url( 'admin.php?page=tiktok' );
$data['docsUrl'] = 'https://woocommerce.com/document/tiktok-for-woocommerce/';
$data['supportUrl'] = 'https://ads.tiktok.com/athena/user-feedback/?identify_key=6a1e079024806640c5e1e695d13db80949525168a052299b4970f9c99cb5ac78';
}
return $data;
}
/**
* Get Jetpack CRM for WooCommerce extension data.
*
* @return array|bool
*/
protected static function get_jetpack_crm_extension_data() {
$slug = 'zero-bs-crm';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/jetpack-crm.png';
if ( 'activated' === $data['status'] ) {
$data['status'] = 'configured';
$data['settingsUrl'] = admin_url( 'admin.php?page=zerobscrm-plugin-settings' );
$data['docsUrl'] = 'https://kb.jetpackcrm.com/';
$data['supportUrl'] = 'https://kb.jetpackcrm.com/crm-support/';
}
return $data;
}
/**
* Get WooCommerce Zapier extension data.
*
* @return array|bool
*/
protected static function get_zapier_extension_data() {
$slug = 'woocommerce-zapier';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/zapier.png';
if ( 'activated' === $data['status'] ) {
$data['status'] = 'configured';
$data['settingsUrl'] = admin_url( 'admin.php?page=wc-settings&tab=wc_zapier' );
$data['docsUrl'] = 'https://docs.om4.io/woocommerce-zapier/';
}
return $data;
}
/**
* Get Salesforce extension data.
*
* @return array|bool
*/
protected static function get_salesforce_extension_data() {
$slug = 'integration-with-salesforce';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/salesforce.jpg';
if ( 'activated' === $data['status'] && class_exists( '\Integration_With_Salesforce_Admin' ) ) {
if ( ! method_exists( '\Integration_With_Salesforce_Admin', 'get_connection_status' ) || \Integration_With_Salesforce_Admin::get_connection_status() ) {
$data['status'] = 'configured';
}
$data['settingsUrl'] = admin_url( 'admin.php?page=integration-with-salesforce' );
$data['docsUrl'] = 'https://woocommerce.com/document/salesforce-integration/';
$data['supportUrl'] = 'https://wpswings.com/submit-query/';
}
return $data;
}
/**
* Get Vimeo extension data.
*
* @return array|bool
*/
protected static function get_vimeo_extension_data() {
$slug = 'vimeo';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/vimeo.png';
if ( 'activated' === $data['status'] && class_exists( '\Tribe\Vimeo_WP\Vimeo\Vimeo_Auth' ) ) {
if ( method_exists( '\Tribe\Vimeo_WP\Vimeo\Vimeo_Auth', 'has_access_token' ) ) {
$vimeo_auth = new \Tribe\Vimeo_WP\Vimeo\Vimeo_Auth();
if ( $vimeo_auth->has_access_token() ) {
$data['status'] = 'configured';
}
} else {
$data['status'] = 'configured';
}
$data['settingsUrl'] = admin_url( 'options-general.php?page=vimeo_settings' );
$data['docsUrl'] = 'https://woocommerce.com/document/vimeo/';
$data['supportUrl'] = 'https://vimeo.com/help/contact';
}
return $data;
}
/**
* Get Trustpilot extension data.
*
* @return array|bool
*/
protected static function get_trustpilot_extension_data() {
$slug = 'trustpilot-reviews';
if ( ! PluginsHelper::is_plugin_installed( $slug ) ) {
return false;
}
$data = self::get_extension_base_data( $slug );
$data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/trustpilot.png';
if ( 'activated' === $data['status'] ) {
$data['status'] = 'configured';
$data['settingsUrl'] = admin_url( 'admin.php?page=woocommerce-trustpilot-settings-page' );
$data['docsUrl'] = 'https://woocommerce.com/document/trustpilot-reviews/';
$data['supportUrl'] = 'https://support.trustpilot.com/hc/en-us/requests/new';
}
return $data;
}
/**
* Get an array of basic data for a given extension.
*
* @param string $slug Plugin slug.
*
* @return array|false
*/
protected static function get_extension_base_data( $slug ) {
$status = PluginsHelper::is_plugin_active( $slug ) ? 'activated' : 'installed';
$plugin_data = PluginsHelper::get_plugin_data( $slug );
if ( ! $plugin_data ) {
return false;
}
return [
'slug' => $slug,
'status' => $status,
'name' => $plugin_data['Name'],
'description' => html_entity_decode( wp_trim_words( $plugin_data['Description'], 20 ) ),
'supportUrl' => 'https://woocommerce.com/my-account/create-a-ticket/?utm_medium=product',
];
}
}
Marketing/MarketingCampaign.php 0000644 00000004537 15153746750 0012605 0 ustar 00 <?php
/**
* Represents a marketing/ads campaign for marketing channels.
*
* Marketing channels (implementing MarketingChannelInterface) can use this class to map their campaign data and present it to WooCommerce core.
*/
namespace Automattic\WooCommerce\Admin\Marketing;
/**
* MarketingCampaign class
*
* @since x.x.x
*/
class MarketingCampaign {
/**
* The unique identifier.
*
* @var string
*/
protected $id;
/**
* The marketing campaign type.
*
* @var MarketingCampaignType
*/
protected $type;
/**
* Title of the marketing campaign.
*
* @var string
*/
protected $title;
/**
* The URL to the channel's campaign management page.
*
* @var string
*/
protected $manage_url;
/**
* The cost of the marketing campaign with the currency.
*
* @var Price
*/
protected $cost;
/**
* MarketingCampaign constructor.
*
* @param string $id The marketing campaign's unique identifier.
* @param MarketingCampaignType $type The marketing campaign type.
* @param string $title The title of the marketing campaign.
* @param string $manage_url The URL to the channel's campaign management page.
* @param Price|null $cost The cost of the marketing campaign with the currency.
*/
public function __construct( string $id, MarketingCampaignType $type, string $title, string $manage_url, Price $cost = null ) {
$this->id = $id;
$this->type = $type;
$this->title = $title;
$this->manage_url = $manage_url;
$this->cost = $cost;
}
/**
* Returns the marketing campaign's unique identifier.
*
* @return string
*/
public function get_id(): string {
return $this->id;
}
/**
* Returns the marketing campaign type.
*
* @return MarketingCampaignType
*/
public function get_type(): MarketingCampaignType {
return $this->type;
}
/**
* Returns the title of the marketing campaign.
*
* @return string
*/
public function get_title(): string {
return $this->title;
}
/**
* Returns the URL to manage the marketing campaign.
*
* @return string
*/
public function get_manage_url(): string {
return $this->manage_url;
}
/**
* Returns the cost of the marketing campaign with the currency.
*
* @return Price|null
*/
public function get_cost(): ?Price {
return $this->cost;
}
}
Marketing/MarketingCampaignType.php 0000644 00000005577 15153746750 0013454 0 ustar 00 <?php
/**
* Represents a marketing campaign type supported by a marketing channel.
*
* Marketing channels (implementing MarketingChannelInterface) can use this class to define what kind of campaigns they support.
*/
namespace Automattic\WooCommerce\Admin\Marketing;
/**
* MarketingCampaignType class
*
* @since x.x.x
*/
class MarketingCampaignType {
/**
* The unique identifier.
*
* @var string
*/
protected $id;
/**
* The marketing channel that this campaign type belongs to.
*
* @var MarketingChannelInterface
*/
protected $channel;
/**
* Name of the marketing campaign type.
*
* @var string
*/
protected $name;
/**
* Description of the marketing campaign type.
*
* @var string
*/
protected $description;
/**
* The URL to the create campaign page.
*
* @var string
*/
protected $create_url;
/**
* The URL to an image/icon for the campaign type.
*
* @var string
*/
protected $icon_url;
/**
* MarketingCampaignType constructor.
*
* @param string $id A unique identifier for the campaign type.
* @param MarketingChannelInterface $channel The marketing channel that this campaign type belongs to.
* @param string $name Name of the marketing campaign type.
* @param string $description Description of the marketing campaign type.
* @param string $create_url The URL to the create campaign page.
* @param string $icon_url The URL to an image/icon for the campaign type.
*/
public function __construct( string $id, MarketingChannelInterface $channel, string $name, string $description, string $create_url, string $icon_url ) {
$this->id = $id;
$this->channel = $channel;
$this->name = $name;
$this->description = $description;
$this->create_url = $create_url;
$this->icon_url = $icon_url;
}
/**
* Returns the marketing campaign's unique identifier.
*
* @return string
*/
public function get_id(): string {
return $this->id;
}
/**
* Returns the marketing channel that this campaign type belongs to.
*
* @return MarketingChannelInterface
*/
public function get_channel(): MarketingChannelInterface {
return $this->channel;
}
/**
* Returns the name of the marketing campaign type.
*
* @return string
*/
public function get_name(): string {
return $this->name;
}
/**
* Returns the description of the marketing campaign type.
*
* @return string
*/
public function get_description(): string {
return $this->description;
}
/**
* Returns the URL to the create campaign page.
*
* @return string
*/
public function get_create_url(): string {
return $this->create_url;
}
/**
* Returns the URL to an image/icon for the campaign type.
*
* @return string
*/
public function get_icon_url(): string {
return $this->icon_url;
}
}
Marketing/MarketingChannelInterface.php 0000644 00000004377 15153746750 0014261 0 ustar 00 <?php
/**
* Represents a marketing channel for the multichannel-marketing feature.
*
* This interface will be implemented by third-party extensions to register themselves as marketing channels.
*/
namespace Automattic\WooCommerce\Admin\Marketing;
/**
* MarketingChannelInterface interface
*
* @since x.x.x
*/
interface MarketingChannelInterface {
public const PRODUCT_LISTINGS_NOT_APPLICABLE = 'not-applicable';
public const PRODUCT_LISTINGS_SYNC_IN_PROGRESS = 'sync-in-progress';
public const PRODUCT_LISTINGS_SYNC_FAILED = 'sync-failed';
public const PRODUCT_LISTINGS_SYNCED = 'synced';
/**
* Returns the unique identifier string for the marketing channel extension, also known as the plugin slug.
*
* @return string
*/
public function get_slug(): string;
/**
* Returns the name of the marketing channel.
*
* @return string
*/
public function get_name(): string;
/**
* Returns the description of the marketing channel.
*
* @return string
*/
public function get_description(): string;
/**
* Returns the path to the channel icon.
*
* @return string
*/
public function get_icon_url(): string;
/**
* Returns the setup status of the marketing channel.
*
* @return bool
*/
public function is_setup_completed(): bool;
/**
* Returns the URL to the settings page, or the link to complete the setup/onboarding if the channel has not been set up yet.
*
* @return string
*/
public function get_setup_url(): string;
/**
* Returns the status of the marketing channel's product listings.
*
* @return string
*/
public function get_product_listings_status(): string;
/**
* Returns the number of channel issues/errors (e.g. account-related errors, product synchronization issues, etc.).
*
* @return int The number of issues to resolve, or 0 if there are no issues with the channel.
*/
public function get_errors_count(): int;
/**
* Returns an array of marketing campaign types that the channel supports.
*
* @return MarketingCampaignType[] Array of marketing campaign type objects.
*/
public function get_supported_campaign_types(): array;
/**
* Returns an array of the channel's marketing campaigns.
*
* @return MarketingCampaign[]
*/
public function get_campaigns(): array;
}
Marketing/MarketingChannels.php 0000644 00000003130 15153746750 0012605 0 ustar 00 <?php
/**
* Handles the registration of marketing channels and acts as their repository.
*/
namespace Automattic\WooCommerce\Admin\Marketing;
use Exception;
/**
* MarketingChannels repository class
*
* @since x.x.x
*/
class MarketingChannels {
/**
* The registered marketing channels.
*
* @var MarketingChannelInterface[]
*/
private $registered_channels = [];
/**
* Registers a marketing channel.
*
* @param MarketingChannelInterface $channel The marketing channel to register.
*
* @return void
*
* @throws Exception If the given marketing channel is already registered.
*/
public function register( MarketingChannelInterface $channel ): void {
if ( isset( $this->registered_channels[ $channel->get_slug() ] ) ) {
throw new Exception( __( 'Marketing channel cannot be registered because there is already a channel registered with the same slug!', 'woocommerce' ) );
}
$this->registered_channels[ $channel->get_slug() ] = $channel;
}
/**
* Unregisters all marketing channels.
*
* @return void
*/
public function unregister_all(): void {
unset( $this->registered_channels );
}
/**
* Returns an array of all registered marketing channels.
*
* @return MarketingChannelInterface[]
*/
public function get_registered_channels(): array {
/**
* Filter the list of registered marketing channels.
*
* @param MarketingChannelInterface[] $channels Array of registered marketing channels.
*
* @since x.x.x
*/
$channels = apply_filters( 'woocommerce_marketing_channels', $this->registered_channels );
return array_values( $channels );
}
}
Marketing/Price.php 0000644 00000001523 15153746750 0010256 0 ustar 00 <?php
/**
* Represents a price with a currency.
*/
namespace Automattic\WooCommerce\Admin\Marketing;
/**
* Price class
*
* @since x.x.x
*/
class Price {
/**
* The price.
*
* @var string
*/
protected $value;
/**
* The currency of the price.
*
* @var string
*/
protected $currency;
/**
* Price constructor.
*
* @param string $value The value of the price.
* @param string $currency The currency of the price.
*/
public function __construct( string $value, string $currency ) {
$this->value = $value;
$this->currency = $currency;
}
/**
* Get value of the price.
*
* @return string
*/
public function get_value(): string {
return $this->value;
}
/**
* Get the currency of the price.
*
* @return string
*/
public function get_currency(): string {
return $this->currency;
}
}
Notes/DataStore.php 0000644 00000042757 15153746750 0010267 0 ustar 00 <?php
/**
* WC Admin Note Data_Store class file.
*/
namespace Automattic\WooCommerce\Admin\Notes;
defined( 'ABSPATH' ) || exit;
/**
* WC Admin Note Data Store (Custom Tables)
*/
class DataStore extends \WC_Data_Store_WP implements \WC_Object_Data_Store_Interface {
// Extensions should define their own contexts and use them to avoid applying woocommerce_note_where_clauses when not needed.
const WC_ADMIN_NOTE_OPER_GLOBAL = 'global';
/**
* Method to create a new note in the database.
*
* @param Note $note Admin note.
*/
public function create( &$note ) {
$date_created = time();
$note->set_date_created( $date_created );
global $wpdb;
$note_to_be_inserted = array(
'name' => $note->get_name(),
'type' => $note->get_type(),
'locale' => $note->get_locale(),
'title' => $note->get_title(),
'content' => $note->get_content(),
'status' => $note->get_status(),
'source' => $note->get_source(),
'is_snoozable' => (int) $note->get_is_snoozable(),
'layout' => $note->get_layout(),
'image' => $note->get_image(),
'is_deleted' => (int) $note->get_is_deleted(),
);
$note_to_be_inserted['content_data'] = wp_json_encode( $note->get_content_data() );
$note_to_be_inserted['date_created'] = gmdate( 'Y-m-d H:i:s', $date_created );
$note_to_be_inserted['date_reminder'] = null;
$wpdb->insert( $wpdb->prefix . 'wc_admin_notes', $note_to_be_inserted );
$note_id = $wpdb->insert_id;
$note->set_id( $note_id );
$this->save_actions( $note );
$note->apply_changes();
/**
* Fires when an admin note is created.
*
* @param int $note_id Note ID.
*/
do_action( 'woocommerce_note_created', $note_id );
}
/**
* Method to read a note.
*
* @param Note $note Admin note.
* @throws \Exception Throws exception when invalid data is found.
*/
public function read( &$note ) {
global $wpdb;
$note->set_defaults();
$note_row = false;
$note_id = $note->get_id();
if ( 0 !== $note_id || '0' !== $note_id ) {
$note_row = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}wc_admin_notes WHERE note_id = %d LIMIT 1",
$note->get_id()
)
);
}
if ( 0 === $note->get_id() || '0' === $note->get_id() ) {
$this->read_actions( $note );
$note->set_object_read( true );
/**
* Fires when an admin note is loaded.
*
* @param int $note_id Note ID.
*/
do_action( 'woocommerce_note_loaded', $note );
} elseif ( $note_row ) {
$note->set_name( $note_row->name );
$note->set_type( $note_row->type );
$note->set_locale( $note_row->locale );
$note->set_title( $note_row->title );
$note->set_content( $note_row->content );
// The default for 'content_value' used to be an array, so there might be rows with invalid data!
$content_data = json_decode( $note_row->content_data );
if ( ! $content_data ) {
$content_data = new \stdClass();
} elseif ( is_array( $content_data ) ) {
$content_data = (object) $content_data;
}
$note->set_content_data( $content_data );
$note->set_status( $note_row->status );
$note->set_source( $note_row->source );
$note->set_date_created( $note_row->date_created );
$note->set_date_reminder( $note_row->date_reminder );
$note->set_is_snoozable( $note_row->is_snoozable );
$note->set_is_deleted( (bool) $note_row->is_deleted );
isset( $note_row->is_read ) && $note->set_is_read( (bool) $note_row->is_read );
$note->set_layout( $note_row->layout );
$note->set_image( $note_row->image );
$this->read_actions( $note );
$note->set_object_read( true );
/**
* Fires when an admin note is loaded.
*
* @param int $note_id Note ID.
*/
do_action( 'woocommerce_note_loaded', $note );
} else {
throw new \Exception( __( 'Invalid admin note', 'woocommerce' ) );
}
}
/**
* Updates a note in the database.
*
* @param Note $note Admin note.
*/
public function update( &$note ) {
global $wpdb;
if ( $note->get_id() ) {
$date_created = $note->get_date_created();
$date_created_timestamp = $date_created->getTimestamp();
$date_created_to_db = gmdate( 'Y-m-d H:i:s', $date_created_timestamp );
$date_reminder = $note->get_date_reminder();
if ( is_null( $date_reminder ) ) {
$date_reminder_to_db = null;
} else {
$date_reminder_timestamp = $date_reminder->getTimestamp();
$date_reminder_to_db = gmdate( 'Y-m-d H:i:s', $date_reminder_timestamp );
}
$wpdb->update(
$wpdb->prefix . 'wc_admin_notes',
array(
'name' => $note->get_name(),
'type' => $note->get_type(),
'locale' => $note->get_locale(),
'title' => $note->get_title(),
'content' => $note->get_content(),
'content_data' => wp_json_encode( $note->get_content_data() ),
'status' => $note->get_status(),
'source' => $note->get_source(),
'date_created' => $date_created_to_db,
'date_reminder' => $date_reminder_to_db,
'is_snoozable' => $note->get_is_snoozable(),
'layout' => $note->get_layout(),
'image' => $note->get_image(),
'is_deleted' => $note->get_is_deleted(),
'is_read' => $note->get_is_read(),
),
array( 'note_id' => $note->get_id() )
);
}
$this->save_actions( $note );
$note->apply_changes();
/**
* Fires when an admin note is updated.
*
* @param int $note_id Note ID.
*/
do_action( 'woocommerce_note_updated', $note->get_id() );
}
/**
* Deletes a note from the database.
*
* @param Note $note Admin note.
* @param array $args Array of args to pass to the delete method (not used).
*/
public function delete( &$note, $args = array() ) {
$note_id = $note->get_id();
if ( $note_id ) {
global $wpdb;
$wpdb->delete( $wpdb->prefix . 'wc_admin_notes', array( 'note_id' => $note_id ) );
$wpdb->delete( $wpdb->prefix . 'wc_admin_note_actions', array( 'note_id' => $note_id ) );
$note->set_id( null );
}
/**
* Fires when an admin note is deleted.
*
* @param int $note_id Note ID.
*/
do_action( 'woocommerce_note_deleted', $note_id );
}
/**
* Read actions from the database.
*
* @param Note $note Admin note.
*/
private function read_actions( &$note ) {
global $wpdb;
$db_actions = $wpdb->get_results(
$wpdb->prepare(
"SELECT action_id, name, label, query, status, actioned_text, nonce_action, nonce_name
FROM {$wpdb->prefix}wc_admin_note_actions
WHERE note_id = %d",
$note->get_id()
)
);
$note_actions = array();
if ( $db_actions ) {
foreach ( $db_actions as $action ) {
$note_actions[] = (object) array(
'id' => (int) $action->action_id,
'name' => $action->name,
'label' => $action->label,
'query' => $action->query,
'status' => $action->status,
'actioned_text' => $action->actioned_text,
'nonce_action' => $action->nonce_action,
'nonce_name' => $action->nonce_name,
);
}
}
$note->set_actions( $note_actions );
}
/**
* Save actions to the database.
* This function clears old actions, then re-inserts new if any changes are found.
*
* @param Note $note Note object.
*
* @return bool|void
*/
private function save_actions( &$note ) {
global $wpdb;
$changed_props = array_keys( $note->get_changes() );
if ( ! in_array( 'actions', $changed_props, true ) ) {
return false;
}
// Process action removal. Actions are removed from
// the note if they aren't part of the changeset.
// See Note::add_action().
$changed_actions = $note->get_actions( 'edit' );
$actions_to_keep = array();
foreach ( $changed_actions as $action ) {
if ( ! empty( $action->id ) ) {
$actions_to_keep[] = (int) $action->id;
}
}
$clear_actions_query = $wpdb->prepare(
"DELETE FROM {$wpdb->prefix}wc_admin_note_actions WHERE note_id = %d",
$note->get_id()
);
if ( $actions_to_keep ) {
$clear_actions_query .= sprintf( ' AND action_id NOT IN (%s)', implode( ',', $actions_to_keep ) );
}
$wpdb->query( $clear_actions_query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
// Update/insert the actions in this changeset.
foreach ( $changed_actions as $action ) {
$action_data = array(
'note_id' => $note->get_id(),
'name' => $action->name,
'label' => $action->label,
'query' => $action->query,
'status' => $action->status,
'actioned_text' => $action->actioned_text,
'nonce_action' => $action->nonce_action,
'nonce_name' => $action->nonce_name,
);
$data_format = array(
'%d',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
);
if ( ! empty( $action->id ) ) {
$action_data['action_id'] = $action->id;
$data_format[] = '%d';
}
$wpdb->replace(
$wpdb->prefix . 'wc_admin_note_actions',
$action_data,
$data_format
);
}
// Update actions from DB (to grab new IDs).
$this->read_actions( $note );
}
/**
* Return an ordered list of notes.
*
* @param array $args Query arguments.
* @param string $context Optional argument that the woocommerce_note_where_clauses filter can use to determine whether to apply extra conditions. Extensions should define their own contexts and use them to avoid adding to notes where clauses when not needed.
* @return array An array of objects containing a note id.
*/
public function get_notes( $args = array(), $context = self::WC_ADMIN_NOTE_OPER_GLOBAL ) {
global $wpdb;
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date_created',
);
$args = wp_parse_args( $args, $defaults );
$offset = $args['per_page'] * ( $args['page'] - 1 );
$where_clauses = $this->get_notes_where_clauses( $args, $context );
// sanitize order and orderby.
$order_by = '`' . str_replace( '`', '', $args['orderby'] ) . '`';
$order_dir = 'asc' === strtolower( $args['order'] ) ? 'ASC' : 'DESC';
$query = $wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT * FROM {$wpdb->prefix}wc_admin_notes WHERE 1=1{$where_clauses} ORDER BY {$order_by} {$order_dir} LIMIT %d, %d",
$offset,
$args['per_page']
);
return $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
/**
* Return an ordered list of notes, without paging or applying the 'woocommerce_note_where_clauses' filter.
* INTERNAL: This method is not intended to be used by external code, and may change without notice.
*
* @param array $args Query arguments.
* @return array An array of database records.
*/
public function lookup_notes( $args = array() ) {
global $wpdb;
$defaults = array(
'order' => 'DESC',
'orderby' => 'date_created',
);
$args = wp_parse_args( $args, $defaults );
$where_clauses = $this->args_to_where_clauses( $args );
// sanitize order and orderby.
$order_by = '`' . str_replace( '`', '', $args['orderby'] ) . '`';
$order_dir = 'asc' === strtolower( $args['order'] ) ? 'ASC' : 'DESC';
$query = "SELECT * FROM {$wpdb->prefix}wc_admin_notes WHERE 1=1{$where_clauses} ORDER BY {$order_by} {$order_dir}";
return $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
/**
* Return a count of notes.
*
* @param string $type Comma separated list of note types.
* @param string $status Comma separated list of statuses.
* @param string $context Optional argument that the woocommerce_note_where_clauses filter can use to determine whether to apply extra conditions. Extensions should define their own contexts and use them to avoid adding to notes where clauses when not needed.
* @return string Count of objects with given type, status and context.
*/
public function get_notes_count( $type = array(), $status = array(), $context = self::WC_ADMIN_NOTE_OPER_GLOBAL ) {
global $wpdb;
$where_clauses = $this->get_notes_where_clauses(
array(
'type' => $type,
'status' => $status,
),
$context
);
if ( ! empty( $where_clauses ) ) {
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
return $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}wc_admin_notes WHERE 1=1{$where_clauses}" );
}
return $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}wc_admin_notes" );
}
/**
* Parses the query arguments passed in as arrays and escapes the values.
*
* @param array $args the query arguments.
* @param string $key the key of the specific argument.
* @param array|null $allowed_types optional allowed_types if only a specific set is allowed.
* @return array the escaped array of argument values.
*/
private function get_escaped_arguments_array_by_key( $args = array(), $key = '', $allowed_types = null ) {
$arg_array = array();
if ( isset( $args[ $key ] ) ) {
foreach ( $args[ $key ] as $args_type ) {
$args_type = trim( $args_type );
$allowed = is_null( $allowed_types ) || in_array( $args_type, $allowed_types, true );
if ( $allowed ) {
$arg_array[] = sprintf( "'%s'", esc_sql( $args_type ) );
}
}
}
return $arg_array;
}
/**
* Return where clauses for getting notes by status and type. For use in both the count and listing queries.
* Applies woocommerce_note_where_clauses filter.
*
* @uses args_to_where_clauses
* @param array $args Array of args to pass.
* @param string $context Optional argument that the woocommerce_note_where_clauses filter can use to determine whether to apply extra conditions. Extensions should define their own contexts and use them to avoid adding to notes where clauses when not needed.
* @return string Where clauses for the query.
*/
public function get_notes_where_clauses( $args = array(), $context = self::WC_ADMIN_NOTE_OPER_GLOBAL ) {
$where_clauses = $this->args_to_where_clauses( $args );
/**
* Filter the notes WHERE clause before retrieving the data.
*
* Allows modification of the notes select criterial.
*
* @param string $where_clauses The generated WHERE clause.
* @param array $args The original arguments for the request.
* @param string $context Optional argument that the woocommerce_note_where_clauses filter can use to determine whether to apply extra conditions. Extensions should define their own contexts and use them to avoid adding to notes where clauses when not needed.
*/
return apply_filters( 'woocommerce_note_where_clauses', $where_clauses, $args, $context );
}
/**
* Return where clauses for notes queries without applying woocommerce_note_where_clauses filter.
* INTERNAL: This method is not intended to be used by external code, and may change without notice.
*
* @param array $args Array of arguments for query conditionals.
* @return string Where clauses.
*/
protected function args_to_where_clauses( $args = array() ) {
$allowed_types = Note::get_allowed_types();
$where_type_array = $this->get_escaped_arguments_array_by_key( $args, 'type', $allowed_types );
$allowed_statuses = Note::get_allowed_statuses();
$where_status_array = $this->get_escaped_arguments_array_by_key( $args, 'status', $allowed_statuses );
$escaped_is_deleted = '';
if ( isset( $args['is_deleted'] ) ) {
$escaped_is_deleted = esc_sql( $args['is_deleted'] );
}
$where_name_array = $this->get_escaped_arguments_array_by_key( $args, 'name' );
$where_excluded_name_array = $this->get_escaped_arguments_array_by_key( $args, 'excluded_name' );
$where_source_array = $this->get_escaped_arguments_array_by_key( $args, 'source' );
$escaped_where_types = implode( ',', $where_type_array );
$escaped_where_status = implode( ',', $where_status_array );
$escaped_where_names = implode( ',', $where_name_array );
$escaped_where_excluded_names = implode( ',', $where_excluded_name_array );
$escaped_where_source = implode( ',', $where_source_array );
$where_clauses = '';
if ( ! empty( $escaped_where_types ) ) {
$where_clauses .= " AND type IN ($escaped_where_types)";
}
if ( ! empty( $escaped_where_status ) ) {
$where_clauses .= " AND status IN ($escaped_where_status)";
}
if ( ! empty( $escaped_where_names ) ) {
$where_clauses .= " AND name IN ($escaped_where_names)";
}
if ( ! empty( $escaped_where_excluded_names ) ) {
$where_clauses .= " AND name NOT IN ($escaped_where_excluded_names)";
}
if ( ! empty( $escaped_where_source ) ) {
$where_clauses .= " AND source IN ($escaped_where_source)";
}
if ( isset( $args['is_read'] ) ) {
$where_clauses .= $args['is_read'] ? ' AND is_read = 1' : ' AND is_read = 0';
}
$where_clauses .= $escaped_is_deleted ? ' AND is_deleted = 1' : ' AND is_deleted = 0';
return $where_clauses;
}
/**
* Find all the notes with a given name.
*
* @param string $name Name to search for.
* @return array An array of matching note ids.
*/
public function get_notes_with_name( $name ) {
global $wpdb;
return $wpdb->get_col(
$wpdb->prepare(
"SELECT note_id FROM {$wpdb->prefix}wc_admin_notes WHERE name = %s ORDER BY note_id ASC",
$name
)
);
}
/**
* Find the ids of all notes with a given type.
*
* @param string $note_type Type to search for.
* @return array An array of matching note ids.
*/
public function get_note_ids_by_type( $note_type ) {
global $wpdb;
return $wpdb->get_col(
$wpdb->prepare(
"SELECT note_id FROM {$wpdb->prefix}wc_admin_notes WHERE type = %s ORDER BY note_id ASC",
$note_type
)
);
}
}
Notes/DeprecatedNotes.php 0000644 00000035462 15153746750 0011445 0 ustar 00 <?php
/**
* Define deprecated classes to support changing the naming convention of
* admin notes.
*/
namespace Automattic\WooCommerce\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\DeprecatedClassFacade;
// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
/**
* WC_Admin_Note.
*
* @deprecated since 4.8.0, use Note
*/
class WC_Admin_Note extends DeprecatedClassFacade {
// These constants must be redeclared as to not break plugins that use them.
const E_WC_ADMIN_NOTE_ERROR = Note::E_WC_ADMIN_NOTE_ERROR;
const E_WC_ADMIN_NOTE_WARNING = Note::E_WC_ADMIN_NOTE_WARNING;
const E_WC_ADMIN_NOTE_UPDATE = Note::E_WC_ADMIN_NOTE_UPDATE;
const E_WC_ADMIN_NOTE_INFORMATIONAL = Note::E_WC_ADMIN_NOTE_INFORMATIONAL;
const E_WC_ADMIN_NOTE_MARKETING = Note::E_WC_ADMIN_NOTE_MARKETING;
const E_WC_ADMIN_NOTE_SURVEY = Note::E_WC_ADMIN_NOTE_SURVEY;
const E_WC_ADMIN_NOTE_PENDING = Note::E_WC_ADMIN_NOTE_PENDING;
const E_WC_ADMIN_NOTE_UNACTIONED = Note::E_WC_ADMIN_NOTE_UNACTIONED;
const E_WC_ADMIN_NOTE_ACTIONED = Note::E_WC_ADMIN_NOTE_ACTIONED;
const E_WC_ADMIN_NOTE_SNOOZED = Note::E_WC_ADMIN_NOTE_SNOOZED;
const E_WC_ADMIN_NOTE_EMAIL = Note::E_WC_ADMIN_NOTE_EMAIL;
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Admin\Notes\Note';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
/**
* Note constructor. Loads note data.
*
* @param mixed $data Note data, object, or ID.
*/
public function __construct( $data = '' ) {
$this->instance = new static::$facade_over_classname( $data );
}
}
/**
* WC_Admin_Notes.
*
* @deprecated since 4.8.0, use Notes
*/
class WC_Admin_Notes extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Admin\Notes\Notes';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Coupon_Page_Moved.
*
* @deprecated since 4.8.0, use CouponPageMoved
*/
class WC_Admin_Notes_Coupon_Page_Moved extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\CouponPageMoved';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Customize_Store_With_Blocks.
*
* @deprecated since 4.8.0, use CustomizeStoreWithBlocks
*/
class WC_Admin_Notes_Customize_Store_With_Blocks extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\CustomizeStoreWithBlocks';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Edit_Products_On_The_Move.
*
* @deprecated since 4.8.0, use EditProductsOnTheMove
*/
class WC_Admin_Notes_Edit_Products_On_The_Move extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\EditProductsOnTheMove';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_EU_VAT_Number.
*
* @deprecated since 4.8.0, use EUVATNumber
*/
class WC_Admin_Notes_EU_VAT_Number extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\EUVATNumber';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Facebook_Marketing_Expert.
*
* @deprecated since 4.8.0, use FacebookMarketingExpert
*/
class WC_Admin_Notes_Facebook_Marketing_Expert extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Admin\Notes\FacebookMarketingExpert';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_First_Product.
*
* @deprecated since 4.8.0, use FirstProduct
*/
class WC_Admin_Notes_First_Product extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\FirstProduct';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Giving_Feedback_Notes.
*
* @deprecated since 4.8.0, use GivingFeedbackNotes
*/
class WC_Admin_Notes_Giving_Feedback_Notes extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\GivingFeedbackNotes';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Install_JP_And_WCS_Plugins.
*
* @deprecated since 4.8.0, use InstallJPAndWCSPlugins
*/
class WC_Admin_Notes_Install_JP_And_WCS_Plugins extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\InstallJPAndWCSPlugins';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Launch_Checklist.
*
* @deprecated since 4.8.0, use LaunchChecklist
*/
class WC_Admin_Notes_Launch_Checklist extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\LaunchChecklist';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Migrate_From_Shopify.
*
* @deprecated since 4.8.0, use MigrateFromShopify
*/
class WC_Admin_Notes_Migrate_From_Shopify extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\MigrateFromShopify';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Mobile_App.
*
* @deprecated since 4.8.0, use MobileApp
*/
class WC_Admin_Notes_Mobile_App extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\MobileApp';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_New_Sales_Record.
*
* @deprecated since 4.8.0, use NewSalesRecord
*/
class WC_Admin_Notes_New_Sales_Record extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\NewSalesRecord';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Onboarding_Email_Marketing.
*
* @deprecated since 4.8.0, use OnboardingEmailMarketing
*/
class WC_Admin_Notes_Onboarding_Email_Marketing extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Admin\Notes\OnboardingEmailMarketing';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Onboarding_Payments.
*
* @deprecated since 4.8.0, use OnboardingPayments
*/
class WC_Admin_Notes_Onboarding_Payments extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\OnboardingPayments';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Online_Clothing_Store.
*
* @deprecated since 4.8.0, use OnlineClothingStore
*/
class WC_Admin_Notes_Online_Clothing_Store extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\OnlineClothingStore';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Order_Milestones.
*
* @deprecated since 4.8.0, use OrderMilestones
*/
class WC_Admin_Notes_Order_Milestones extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\OrderMilestones';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Performance_On_Mobile.
*
* @deprecated since 4.8.0, use PerformanceOnMobile
*/
class WC_Admin_Notes_Performance_On_Mobile extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\PerformanceOnMobile';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Personalize_Store.
*
* @deprecated since 4.8.0, use PersonalizeStore
*/
class WC_Admin_Notes_Personalize_Store extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\PersonalizeStore';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Real_Time_Order_Alerts.
*
* @deprecated since 4.8.0, use RealTimeOrderAlerts
*/
class WC_Admin_Notes_Real_Time_Order_Alerts extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\RealTimeOrderAlerts';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Selling_Online_Courses.
*
* @deprecated since 4.8.0, use SellingOnlineCourses
*/
class WC_Admin_Notes_Selling_Online_Courses extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\SellingOnlineCourses';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Test_Checkout.
*
* @deprecated since 4.8.0, use TestCheckout
*/
class WC_Admin_Notes_Test_Checkout extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\TestCheckout';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Tracking_Opt_In.
*
* @deprecated since 4.8.0, use TrackingOptIn
*/
class WC_Admin_Notes_Tracking_Opt_In extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\TrackingOptIn';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_Woo_Subscriptions_Notes.
*
* @deprecated since 4.8.0, use WooSubscriptionsNotes
*/
class WC_Admin_Notes_Woo_Subscriptions_Notes extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\WooSubscriptionsNotes';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_WooCommerce_Payments.
*
* @deprecated since 4.8.0, use WooCommercePayments
*/
class WC_Admin_Notes_WooCommerce_Payments extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\WooCommercePayments';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
/**
* WC_Admin_Notes_WooCommerce_Subscriptions.
*
* @deprecated since 4.8.0, use WooCommerceSubscriptions
*/
class WC_Admin_Notes_WooCommerce_Subscriptions extends DeprecatedClassFacade {
/**
* The name of the non-deprecated class that this facade covers.
*
* @var string
*/
protected static $facade_over_classname = 'Automattic\WooCommerce\Internal\Admin\Notes\WooCommerceSubscriptions';
/**
* The version that this class was deprecated in.
*
* @var string
*/
protected static $deprecated_in_version = '4.8.0';
}
Notes/Note.php 0000644 00000045753 15153746750 0007305 0 ustar 00 <?php
/**
* WooCommerce Admin (Dashboard) Notes.
*
* The WooCommerce admin notes class gets admin notes data from storage and checks validity.
*/
namespace Automattic\WooCommerce\Admin\Notes;
defined( 'ABSPATH' ) || exit;
/**
* Note class.
*/
class Note extends \WC_Data {
// Note types.
const E_WC_ADMIN_NOTE_ERROR = 'error'; // used for presenting error conditions.
const E_WC_ADMIN_NOTE_WARNING = 'warning'; // used for presenting warning conditions.
const E_WC_ADMIN_NOTE_UPDATE = 'update'; // i.e. used when a new version is available.
const E_WC_ADMIN_NOTE_INFORMATIONAL = 'info'; // used for presenting informational messages.
const E_WC_ADMIN_NOTE_MARKETING = 'marketing'; // used for adding marketing messages.
const E_WC_ADMIN_NOTE_SURVEY = 'survey'; // used for adding survey messages.
const E_WC_ADMIN_NOTE_EMAIL = 'email'; // used for adding notes that will be sent by email.
// Note status codes.
const E_WC_ADMIN_NOTE_PENDING = 'pending'; // the note is pending - hidden but not actioned.
const E_WC_ADMIN_NOTE_UNACTIONED = 'unactioned'; // the note has not yet been actioned by a user.
const E_WC_ADMIN_NOTE_ACTIONED = 'actioned'; // the note has had its action completed by a user.
const E_WC_ADMIN_NOTE_SNOOZED = 'snoozed'; // the note has been snoozed by a user.
const E_WC_ADMIN_NOTE_SENT = 'sent'; // the note has been sent by email to the user.
/**
* This is the name of this object type.
*
* @var string
*/
protected $object_type = 'admin-note';
/**
* Cache group.
*
* @var string
*/
protected $cache_group = 'admin-note';
/**
* Note constructor. Loads note data.
*
* @param mixed $data Note data, object, or ID.
*/
public function __construct( $data = '' ) {
// Set default data here to allow `content_data` to be an object.
$this->data = array(
'name' => '-',
'type' => self::E_WC_ADMIN_NOTE_INFORMATIONAL,
'locale' => 'en_US',
'title' => '-',
'content' => '-',
'content_data' => new \stdClass(),
'status' => self::E_WC_ADMIN_NOTE_UNACTIONED,
'source' => 'woocommerce',
'date_created' => '0000-00-00 00:00:00',
'date_reminder' => '',
'is_snoozable' => false,
'actions' => array(),
'layout' => 'plain',
'image' => '',
'is_deleted' => false,
'is_read' => false,
);
parent::__construct( $data );
if ( $data instanceof Note ) {
$this->set_id( absint( $data->get_id() ) );
} elseif ( is_numeric( $data ) ) {
$this->set_id( $data );
} elseif ( is_object( $data ) && ! empty( $data->note_id ) ) {
$this->set_id( $data->note_id );
unset( $data->icon ); // Icons are deprecated.
$this->set_props( (array) $data );
$this->set_object_read( true );
} else {
$this->set_object_read( true );
}
$this->data_store = Notes::load_data_store();
if ( $this->get_id() > 0 ) {
$this->data_store->read( $this );
}
}
/**
* Merge changes with data and clear.
*
* @since 3.0.0
*/
public function apply_changes() {
$this->data = array_replace_recursive( $this->data, $this->changes ); // @codingStandardsIgnoreLine
// Note actions need to be replaced wholesale.
// Merging arrays doesn't allow for deleting note actions.
if ( isset( $this->changes['actions'] ) ) {
$this->data['actions'] = $this->changes['actions'];
}
$this->changes = array();
}
/*
|--------------------------------------------------------------------------
| Helpers
|--------------------------------------------------------------------------
|
| Methods for getting allowed types, statuses.
|
*/
/**
* Get allowed types.
*
* @return array
*/
public static function get_allowed_types() {
$allowed_types = array(
self::E_WC_ADMIN_NOTE_ERROR,
self::E_WC_ADMIN_NOTE_WARNING,
self::E_WC_ADMIN_NOTE_UPDATE,
self::E_WC_ADMIN_NOTE_INFORMATIONAL,
self::E_WC_ADMIN_NOTE_MARKETING,
self::E_WC_ADMIN_NOTE_SURVEY,
self::E_WC_ADMIN_NOTE_EMAIL,
);
return apply_filters( 'woocommerce_note_types', $allowed_types );
}
/**
* Get allowed statuses.
*
* @return array
*/
public static function get_allowed_statuses() {
$allowed_statuses = array(
self::E_WC_ADMIN_NOTE_PENDING,
self::E_WC_ADMIN_NOTE_ACTIONED,
self::E_WC_ADMIN_NOTE_UNACTIONED,
self::E_WC_ADMIN_NOTE_SNOOZED,
self::E_WC_ADMIN_NOTE_SENT,
);
return apply_filters( 'woocommerce_note_statuses', $allowed_statuses );
}
/*
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
|
| Methods for getting data from the note object.
|
*/
/**
* Returns all data for this object.
*
* Override \WC_Data::get_data() to avoid errantly including meta data
* from ID collisions with the posts table.
*
* @return array
*/
public function get_data() {
return array_merge( array( 'id' => $this->get_id() ), $this->data );
}
/**
* Get note name.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_name( $context = 'view' ) {
return $this->get_prop( 'name', $context );
}
/**
* Get note type.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_type( $context = 'view' ) {
return $this->get_prop( 'type', $context );
}
/**
* Get note locale.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_locale( $context = 'view' ) {
return $this->get_prop( 'locale', $context );
}
/**
* Get note title.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_title( $context = 'view' ) {
return $this->get_prop( 'title', $context );
}
/**
* Get note content.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_content( $context = 'view' ) {
return $this->get_prop( 'content', $context );
}
/**
* Get note content data (i.e. values that would be needed for re-localization)
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return object
*/
public function get_content_data( $context = 'view' ) {
return $this->get_prop( 'content_data', $context );
}
/**
* Get note status.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_status( $context = 'view' ) {
return $this->get_prop( 'status', $context );
}
/**
* Get note source.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_source( $context = 'view' ) {
return $this->get_prop( 'source', $context );
}
/**
* Get date note was created.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
*/
public function get_date_created( $context = 'view' ) {
return $this->get_prop( 'date_created', $context );
}
/**
* Get date on which user should be reminded of the note (if any).
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
*/
public function get_date_reminder( $context = 'view' ) {
return $this->get_prop( 'date_reminder', $context );
}
/**
* Get note snoozability.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return bool Whether or not the note can be snoozed.
*/
public function get_is_snoozable( $context = 'view' ) {
return $this->get_prop( 'is_snoozable', $context );
}
/**
* Get actions on the note (if any).
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return array
*/
public function get_actions( $context = 'view' ) {
return $this->get_prop( 'actions', $context );
}
/**
* Get action by action name on the note.
*
* @param string $action_name The action name.
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return object the action.
*/
public function get_action( $action_name, $context = 'view' ) {
$actions = $this->get_prop( 'actions', $context );
$matching_action = null;
foreach ( $actions as $i => $action ) {
if ( $action->name === $action_name ) {
$matching_action =& $actions[ $i ];
break;
}
}
return $matching_action;
}
/**
* Get note layout (the old notes won't have one).
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return array
*/
public function get_layout( $context = 'view' ) {
return $this->get_prop( 'layout', $context );
}
/**
* Get note image (if any).
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return array
*/
public function get_image( $context = 'view' ) {
return $this->get_prop( 'image', $context );
}
/**
* Get deleted status.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return array
*/
public function get_is_deleted( $context = 'view' ) {
return $this->get_prop( 'is_deleted', $context );
}
/**
* Get is_read status.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return array
*/
public function get_is_read( $context = 'view' ) {
return $this->get_prop( 'is_read', $context );
}
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
|
| Methods for setting note data. These should not update anything in the
| database itself and should only change what is stored in the class
| object.
|
*/
/**
* Set note name.
*
* @param string $name Note name.
*/
public function set_name( $name ) {
// Don't allow empty names.
if ( empty( $name ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note name prop cannot be empty.', 'woocommerce' ) );
}
$this->set_prop( 'name', $name );
}
/**
* Set note type.
*
* @param string $type Note type.
*/
public function set_type( $type ) {
if ( empty( $type ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note type prop cannot be empty.', 'woocommerce' ) );
}
if ( ! in_array( $type, self::get_allowed_types(), true ) ) {
$this->error(
'admin_note_invalid_data',
sprintf(
/* translators: %s: admin note type. */
__( 'The admin note type prop (%s) is not one of the supported types.', 'woocommerce' ),
$type
)
);
}
$this->set_prop( 'type', $type );
}
/**
* Set note locale.
*
* @param string $locale Note locale.
*/
public function set_locale( $locale ) {
if ( empty( $locale ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note locale prop cannot be empty.', 'woocommerce' ) );
}
$this->set_prop( 'locale', $locale );
}
/**
* Set note title.
*
* @param string $title Note title.
*/
public function set_title( $title ) {
if ( empty( $title ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note title prop cannot be empty.', 'woocommerce' ) );
}
$this->set_prop( 'title', $title );
}
/**
* Set note icon (Deprecated).
*
* @param string $icon Note icon.
*/
public function set_icon( $icon ) {
wc_deprecated_function( 'set_icon', '4.3' );
}
/**
* Set note content.
*
* @param string $content Note content.
*/
public function set_content( $content ) {
$allowed_html = array(
'br' => array(),
'em' => array(),
'strong' => array(),
'a' => array(
'href' => true,
'rel' => true,
'name' => true,
'target' => true,
'download' => array(
'valueless' => 'y',
),
),
'p' => array(),
);
$content = wp_kses( $content, $allowed_html );
if ( empty( $content ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note content prop cannot be empty.', 'woocommerce' ) );
}
$this->set_prop( 'content', $content );
}
/**
* Set note data for potential re-localization.
*
* @todo Set a default empty array? https://github.com/woocommerce/woocommerce-admin/pull/1763#pullrequestreview-212442921.
* @param object $content_data Note data.
*/
public function set_content_data( $content_data ) {
$allowed_type = false;
// Make sure $content_data is stdClass Object or an array.
if ( ! ( $content_data instanceof \stdClass ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note content_data prop must be an instance of stdClass.', 'woocommerce' ) );
}
$this->set_prop( 'content_data', $content_data );
}
/**
* Set note status.
*
* @param string $status Note status.
*/
public function set_status( $status ) {
if ( empty( $status ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note status prop cannot be empty.', 'woocommerce' ) );
}
if ( ! in_array( $status, self::get_allowed_statuses(), true ) ) {
$this->error(
'admin_note_invalid_data',
sprintf(
/* translators: %s: admin note status property. */
__( 'The admin note status prop (%s) is not one of the supported statuses.', 'woocommerce' ),
$status
)
);
}
$this->set_prop( 'status', $status );
}
/**
* Set note source.
*
* @param string $source Note source.
*/
public function set_source( $source ) {
if ( empty( $source ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note source prop cannot be empty.', 'woocommerce' ) );
}
$this->set_prop( 'source', $source );
}
/**
* Set date note was created. NULL is not allowed
*
* @param string|integer $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed.
*/
public function set_date_created( $date ) {
if ( empty( $date ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note date prop cannot be empty.', 'woocommerce' ) );
}
if ( is_string( $date ) ) {
$date = wc_string_to_timestamp( $date );
}
$this->set_date_prop( 'date_created', $date );
}
/**
* Set date admin should be reminded of note. NULL IS allowed
*
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.
*/
public function set_date_reminder( $date ) {
if ( is_string( $date ) ) {
$date = wc_string_to_timestamp( $date );
}
$this->set_date_prop( 'date_reminder', $date );
}
/**
* Set note snoozability.
*
* @param bool $is_snoozable Whether or not the note can be snoozed.
*/
public function set_is_snoozable( $is_snoozable ) {
return $this->set_prop( 'is_snoozable', $is_snoozable );
}
/**
* Clear actions from a note.
*/
public function clear_actions() {
$this->set_prop( 'actions', array() );
}
/**
* Set note layout.
*
* @param string $layout Note layout.
*/
public function set_layout( $layout ) {
// If we don't receive a layout we will set it by default as "plain".
if ( empty( $layout ) ) {
$layout = 'plain';
}
$valid_layouts = array( 'banner', 'plain', 'thumbnail' );
if ( in_array( $layout, $valid_layouts, true ) ) {
$this->set_prop( 'layout', $layout );
} else {
$this->error( 'admin_note_invalid_data', __( 'The admin note layout has a wrong prop value.', 'woocommerce' ) );
}
}
/**
* Set note image.
*
* @param string $image Note image.
*/
public function set_image( $image ) {
$this->set_prop( 'image', $image );
}
/**
* Set note deleted status. NULL is not allowed
*
* @param bool $is_deleted Note deleted status.
*/
public function set_is_deleted( $is_deleted ) {
$this->set_prop( 'is_deleted', $is_deleted );
}
/**
* Set note is_read status. NULL is not allowed
*
* @param bool $is_read Note is_read status.
*/
public function set_is_read( $is_read ) {
$this->set_prop( 'is_read', $is_read );
}
/**
* Add an action to the note
*
* @param string $name Action name (not presented to user).
* @param string $label Action label (presented as button label).
* @param string $url Action URL, if navigation needed. Optional.
* @param string $status Status to transition parent Note to upon click. Defaults to 'actioned'.
* @param boolean $primary Deprecated since version 3.4.0.
* @param string $actioned_text The label to display after the note has been actioned but before it is dismissed in the UI.
*/
public function add_action(
$name,
$label,
$url = '',
$status = self::E_WC_ADMIN_NOTE_ACTIONED,
$primary = false,
$actioned_text = ''
) {
$name = wc_clean( $name );
$label = wc_clean( $label );
$query = esc_url_raw( $url );
$status = wc_clean( $status );
$actioned_text = wc_clean( $actioned_text );
if ( empty( $name ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note action name prop cannot be empty.', 'woocommerce' ) );
}
if ( empty( $label ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note action label prop cannot be empty.', 'woocommerce' ) );
}
$action = array(
'name' => $name,
'label' => $label,
'query' => $query,
'status' => $status,
'actioned_text' => $actioned_text,
'nonce_name' => null,
'nonce_action' => null,
);
$note_actions = $this->get_prop( 'actions', 'edit' );
$note_actions[] = (object) $action;
$this->set_prop( 'actions', $note_actions );
}
/**
* Set actions on a note.
*
* @param array $actions Note actions.
*/
public function set_actions( $actions ) {
$this->set_prop( 'actions', $actions );
}
/**
* Add a nonce to an existing note action.
*
* @link https://codex.wordpress.org/WordPress_Nonces
*
* @param string $note_action_name Name of action to add a nonce to.
* @param string $nonce_action The nonce action.
* @param string $nonce_name The nonce Name. This is used as the paramater name in the resulting URL for the action.
* @return void
* @throws \Exception If note name cannot be found.
*/
public function add_nonce_to_action( string $note_action_name, string $nonce_action, string $nonce_name ) {
$actions = $this->get_prop( 'actions', 'edit' );
$matching_action = null;
foreach ( $actions as $i => $action ) {
if ( $action->name === $note_action_name ) {
$matching_action =& $actions[ $i ];
}
}
if ( empty( $matching_action ) ) {
throw new \Exception( sprintf( 'Could not find action %s in note %s', $note_action_name, $this->get_name() ) );
}
$matching_action->nonce_action = $nonce_action;
$matching_action->nonce_name = $nonce_name;
$this->set_actions( $actions );
}
}
Notes/NoteTraits.php 0000644 00000016143 15153746750 0010463 0 ustar 00 <?php
/**
* WC Admin Note Traits
*
* WC Admin Note Traits class that houses shared functionality across notes.
*/
namespace Automattic\WooCommerce\Admin\Notes;
use Automattic\WooCommerce\Admin\WCAdminHelper;
defined( 'ABSPATH' ) || exit;
/**
* NoteTraits class.
*/
trait NoteTraits {
/**
* Test how long WooCommerce Admin has been active.
*
* @param int $seconds Time in seconds to check.
* @return bool Whether or not WooCommerce admin has been active for $seconds.
*/
private static function wc_admin_active_for( $seconds ) {
return WCAdminHelper::is_wc_admin_active_for( $seconds );
}
/**
* Test if WooCommerce Admin has been active within a pre-defined range.
*
* @param string $range range available in WC_ADMIN_STORE_AGE_RANGES.
* @param int $custom_start custom start in range.
* @return bool Whether or not WooCommerce admin has been active within the range.
*/
private static function is_wc_admin_active_in_date_range( $range, $custom_start = null ) {
return WCAdminHelper::is_wc_admin_active_in_date_range( $range, $custom_start );
}
/**
* Check if the note has been previously added.
*
* @throws NotesUnavailableException Throws exception when notes are unavailable.
*/
public static function note_exists() {
$data_store = Notes::load_data_store();
$note_ids = $data_store->get_notes_with_name( self::NOTE_NAME );
return ! empty( $note_ids );
}
/**
* Checks if a note can and should be added.
*
* @return bool
* @throws NotesUnavailableException Throws exception when notes are unavailable.
*/
public static function can_be_added() {
$note = self::get_note();
if ( ! $note instanceof Note && ! $note instanceof WC_Admin_Note ) {
return;
}
if ( self::note_exists() ) {
return false;
}
if (
'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) &&
Note::E_WC_ADMIN_NOTE_MARKETING === $note->get_type()
) {
return false;
}
return true;
}
/**
* Add the note if it passes predefined conditions.
*
* @throws NotesUnavailableException Throws exception when notes are unavailable.
*/
public static function possibly_add_note() {
$note = self::get_note();
if ( ! self::can_be_added() ) {
return;
}
$note->save();
}
/**
* Alias this method for backwards compatibility.
*
* @throws NotesUnavailableException Throws exception when notes are unavailable.
*/
public static function add_note() {
self::possibly_add_note();
}
/**
* Should this note exist? (Default implementation is generous. Override as needed.)
*/
public static function is_applicable() {
return true;
}
/**
* Delete this note if it is not applicable, unless has been soft-deleted or actioned already.
*/
public static function delete_if_not_applicable() {
if ( ! self::is_applicable() ) {
$data_store = Notes::load_data_store();
$note_ids = $data_store->get_notes_with_name( self::NOTE_NAME );
if ( ! empty( $note_ids ) ) {
$note = Notes::get_note( $note_ids[0] );
if ( ! $note->get_is_deleted() && ( Note::E_WC_ADMIN_NOTE_ACTIONED !== $note->get_status() ) ) {
return self::possibly_delete_note();
}
}
}
}
/**
* Possibly delete the note, if it exists in the database. Note that this
* is a hard delete, for where it doesn't make sense to soft delete or
* action the note.
*
* @throws NotesUnavailableException Throws exception when notes are unavailable.
*/
public static function possibly_delete_note() {
$data_store = Notes::load_data_store();
$note_ids = $data_store->get_notes_with_name( self::NOTE_NAME );
foreach ( $note_ids as $note_id ) {
$note = Notes::get_note( $note_id );
if ( $note ) {
$data_store->delete( $note );
}
}
}
/**
* Update the note if it passes predefined conditions.
*
* @throws NotesUnavailableException Throws exception when notes are unavailable.
*/
public static function possibly_update_note() {
$note_in_db = Notes::get_note_by_name( self::NOTE_NAME );
if ( ! $note_in_db ) {
return;
}
if ( ! method_exists( self::class, 'get_note' ) ) {
return;
}
$note = self::get_note();
if ( ! $note instanceof Note && ! $note instanceof WC_Admin_Note ) {
return;
}
$need_save = in_array(
true,
array(
self::update_note_field_if_changed( $note_in_db, $note, 'title' ),
self::update_note_field_if_changed( $note_in_db, $note, 'content' ),
self::update_note_field_if_changed( $note_in_db, $note, 'content_data' ),
self::update_note_field_if_changed( $note_in_db, $note, 'type' ),
self::update_note_field_if_changed( $note_in_db, $note, 'locale' ),
self::update_note_field_if_changed( $note_in_db, $note, 'source' ),
self::update_note_field_if_changed( $note_in_db, $note, 'actions' )
),
true
);
if ( $need_save ) {
$note_in_db->save();
}
}
/**
* Get if the note has been actioned.
*
* @return bool
* @throws NotesUnavailableException Throws exception when notes are unavailable.
*/
public static function has_note_been_actioned() {
$data_store = Notes::load_data_store();
$note_ids = $data_store->get_notes_with_name( self::NOTE_NAME );
if ( ! empty( $note_ids ) ) {
$note = Notes::get_note( $note_ids[0] );
if ( Note::E_WC_ADMIN_NOTE_ACTIONED === $note->get_status() ) {
return true;
}
}
return false;
}
/**
* Update a note field of note1 if it's different from note2 with getter and setter.
*
* @param Note $note1 Note to update.
* @param Note $note2 Note to compare against.
* @param string $field_name Field to update.
* @return bool True if the field was updated.
*/
private static function update_note_field_if_changed( $note1, $note2, $field_name ) {
// We need to serialize the stdObject to compare it.
$note1_field_value = self::possibly_convert_object_to_array(
call_user_func( array( $note1, 'get_' . $field_name ) )
);
$note2_field_value = self::possibly_convert_object_to_array(
call_user_func( array( $note2, 'get_' . $field_name ) )
);
if ( 'actions' === $field_name ) {
// We need to individually compare the action fields because action object from db is different from action object of note.
// For example, action object from db has "id".
$diff = array_udiff(
$note1_field_value,
$note2_field_value,
function( $action1, $action2 ) {
if ( $action1->name === $action2->name &&
$action1->label === $action2->label &&
$action1->query === $action2->query ) {
return 0;
}
return -1;
}
);
$need_update = count( $diff ) > 0;
} else {
$need_update = $note1_field_value !== $note2_field_value;
}
if ( $need_update ) {
call_user_func(
array( $note1, 'set_' . $field_name ),
// Get note2 field again because it may have been changed during the comparison.
call_user_func( array( $note2, 'get_' . $field_name ) )
);
return true;
}
return false;
}
/**
* Convert a value to array if it's a stdClass.
*
* @param mixed $obj variable to convert.
* @return mixed
*/
private static function possibly_convert_object_to_array( $obj ) {
if ( $obj instanceof \stdClass ) {
return (array) $obj;
}
return $obj;
}
}
Notes/Notes.php 0000644 00000033451 15153746750 0007460 0 ustar 00 <?php
/**
* Handles storage and retrieval of admin notes
*/
namespace Automattic\WooCommerce\Admin\Notes;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Admin Notes class.
*/
class Notes {
/**
* Hook used for recurring "unsnooze" action.
*/
const UNSNOOZE_HOOK = 'wc_admin_unsnooze_admin_notes';
/**
* Hook appropriate actions.
*/
public static function init() {
add_action( 'admin_init', array( __CLASS__, 'schedule_unsnooze_notes' ) );
add_action( 'admin_init', array( __CLASS__, 'possibly_delete_survey_notes' ) );
add_action( 'update_option_woocommerce_show_marketplace_suggestions', array( __CLASS__, 'possibly_delete_marketing_notes' ), 10, 2 );
}
/**
* Get notes from the database.
*
* @param string $context Getting notes for what context. Valid values: view, edit.
* @param array $args Arguments to pass to the query( e.g. per_page and page).
* @return array Array of arrays.
*/
public static function get_notes( $context = 'edit', $args = array() ) {
$data_store = self::load_data_store();
$raw_notes = $data_store->get_notes( $args );
$notes = array();
foreach ( (array) $raw_notes as $raw_note ) {
try {
$note = new Note( $raw_note );
/**
* Filter the note from db. This is used to modify the note before it is returned.
*
* @since 6.9.0
* @param Note $note The note object from the database.
*/
$note = apply_filters( 'woocommerce_get_note_from_db', $note );
$note_id = $note->get_id();
$notes[ $note_id ] = $note->get_data();
$notes[ $note_id ]['name'] = $note->get_name( $context );
$notes[ $note_id ]['type'] = $note->get_type( $context );
$notes[ $note_id ]['locale'] = $note->get_locale( $context );
$notes[ $note_id ]['title'] = $note->get_title( $context );
$notes[ $note_id ]['content'] = $note->get_content( $context );
$notes[ $note_id ]['content_data'] = $note->get_content_data( $context );
$notes[ $note_id ]['status'] = $note->get_status( $context );
$notes[ $note_id ]['source'] = $note->get_source( $context );
$notes[ $note_id ]['date_created'] = $note->get_date_created( $context );
$notes[ $note_id ]['date_reminder'] = $note->get_date_reminder( $context );
$notes[ $note_id ]['actions'] = $note->get_actions( $context );
$notes[ $note_id ]['layout'] = $note->get_layout( $context );
$notes[ $note_id ]['image'] = $note->get_image( $context );
$notes[ $note_id ]['is_deleted'] = $note->get_is_deleted( $context );
} catch ( \Exception $e ) {
wc_caught_exception( $e, __CLASS__ . '::' . __FUNCTION__, array( $note_id ) );
}
}
return $notes;
}
/**
* Get admin note using it's ID
*
* @param int $note_id Note ID.
* @return Note|bool
*/
public static function get_note( $note_id ) {
if ( false !== $note_id ) {
try {
return new Note( $note_id );
} catch ( \Exception $e ) {
wc_caught_exception( $e, __CLASS__ . '::' . __FUNCTION__, array( $note_id ) );
return false;
}
}
return false;
}
/**
* Get admin note using its name.
*
* This is a shortcut for the common pattern of looking up note ids by name and then passing the first id to get_note().
* It will behave unpredictably when more than one note with the given name exists.
*
* @param string $note_name Note name.
* @return Note|bool
**/
public static function get_note_by_name( $note_name ) {
$data_store = self::load_data_store();
$note_ids = $data_store->get_notes_with_name( $note_name );
if ( empty( $note_ids ) ) {
return false;
}
return self::get_note( $note_ids[0] );
}
/**
* Get the total number of notes
*
* @param string $type Comma separated list of note types.
* @param string $status Comma separated list of statuses.
* @return int
*/
public static function get_notes_count( $type = array(), $status = array() ) {
$data_store = self::load_data_store();
return $data_store->get_notes_count( $type, $status );
}
/**
* Deletes admin notes with a given name.
*
* @param string|array $names Name(s) to search for.
*/
public static function delete_notes_with_name( $names ) {
if ( is_string( $names ) ) {
$names = array( $names );
} elseif ( ! is_array( $names ) ) {
return;
}
$data_store = self::load_data_store();
foreach ( $names as $name ) {
$note_ids = $data_store->get_notes_with_name( $name );
foreach ( (array) $note_ids as $note_id ) {
$note = self::get_note( $note_id );
if ( $note ) {
$note->delete();
}
}
}
}
/**
* Update a note.
*
* @param Note $note The note that will be updated.
* @param array $requested_updates a list of requested updates.
*/
public static function update_note( $note, $requested_updates ) {
$note_changed = false;
if ( isset( $requested_updates['status'] ) ) {
$note->set_status( $requested_updates['status'] );
$note_changed = true;
}
if ( isset( $requested_updates['date_reminder'] ) ) {
$note->set_date_reminder( $requested_updates['date_reminder'] );
$note_changed = true;
}
if ( isset( $requested_updates['is_deleted'] ) ) {
$note->set_is_deleted( $requested_updates['is_deleted'] );
$note_changed = true;
}
if ( isset( $requested_updates['is_read'] ) ) {
$note->set_is_read( $requested_updates['is_read'] );
$note_changed = true;
}
if ( $note_changed ) {
$note->save();
}
}
/**
* Soft delete of a note.
*
* @param Note $note The note that will be deleted.
*/
public static function delete_note( $note ) {
$note->set_is_deleted( 1 );
$note->save();
}
/**
* Soft delete of all the admin notes. Returns the deleted items.
*
* @param array $args Arguments to pass to the query (ex: status).
* @return array Array of notes.
*/
public static function delete_all_notes( $args = array() ) {
$data_store = self::load_data_store();
$defaults = array(
'order' => 'desc',
'orderby' => 'date_created',
'per_page' => 25,
'page' => 1,
'type' => array(
Note::E_WC_ADMIN_NOTE_INFORMATIONAL,
Note::E_WC_ADMIN_NOTE_MARKETING,
Note::E_WC_ADMIN_NOTE_WARNING,
Note::E_WC_ADMIN_NOTE_SURVEY,
),
'is_deleted' => 0,
);
$args = wp_parse_args( $args, $defaults );
// Here we filter for the same params we are using to show the note list in client side.
$raw_notes = $data_store->get_notes( $args );
$notes = array();
foreach ( (array) $raw_notes as $raw_note ) {
$note = self::get_note( $raw_note->note_id );
if ( $note ) {
self::delete_note( $note );
array_push( $notes, $note );
}
}
return $notes;
}
/**
* Clear note snooze status if the reminder date has been reached.
*/
public static function unsnooze_notes() {
$data_store = self::load_data_store();
$raw_notes = $data_store->get_notes(
array(
'status' => array( Note::E_WC_ADMIN_NOTE_SNOOZED ),
)
);
$now = new \DateTime();
foreach ( $raw_notes as $raw_note ) {
$note = self::get_note( $raw_note->note_id );
if ( false === $note ) {
continue;
}
$date_reminder = $note->get_date_reminder( 'edit' );
if ( $date_reminder < $now ) {
$note->set_status( Note::E_WC_ADMIN_NOTE_UNACTIONED );
$note->set_date_reminder( null );
$note->save();
}
}
}
/**
* Schedule unsnooze notes event.
*/
public static function schedule_unsnooze_notes() {
if ( ! wp_next_scheduled( self::UNSNOOZE_HOOK ) ) {
wp_schedule_event( time() + 5, 'hourly', self::UNSNOOZE_HOOK );
}
}
/**
* Unschedule unsnooze notes event.
*/
public static function clear_queued_actions() {
wp_clear_scheduled_hook( self::UNSNOOZE_HOOK );
}
/**
* Delete marketing notes if marketing has been opted out.
*
* @param string $old_value Old value.
* @param string $value New value.
*/
public static function possibly_delete_marketing_notes( $old_value, $value ) {
if ( 'no' !== $value ) {
return;
}
$data_store = self::load_data_store();
$note_ids = $data_store->get_note_ids_by_type( Note::E_WC_ADMIN_NOTE_MARKETING );
foreach ( $note_ids as $note_id ) {
$note = self::get_note( $note_id );
if ( $note ) {
$note->delete();
}
}
}
/**
* Delete actioned survey notes.
*/
public static function possibly_delete_survey_notes() {
$data_store = self::load_data_store();
$note_ids = $data_store->get_note_ids_by_type( Note::E_WC_ADMIN_NOTE_SURVEY );
foreach ( $note_ids as $note_id ) {
$note = self::get_note( $note_id );
if ( $note && ( $note->get_status() === Note::E_WC_ADMIN_NOTE_ACTIONED ) ) {
$note->set_is_deleted( 1 );
$note->save();
}
}
}
/**
* Get the status of a given note by name.
*
* @param string $note_name Name of the note.
* @return string|bool The note status.
*/
public static function get_note_status( $note_name ) {
$note = self::get_note_by_name( $note_name );
if ( ! $note ) {
return false;
}
return $note->get_status();
}
/**
* Get action by id.
*
* @param Note $note The note that has of the action.
* @param int $action_id Action ID.
* @return object|bool The found action.
*/
public static function get_action_by_id( $note, $action_id ) {
$actions = $note->get_actions( 'edit' );
$found_action = false;
foreach ( $actions as $action ) {
if ( $action->id === $action_id ) {
$found_action = $action;
}
}
return $found_action;
}
/**
* Trigger note action.
*
* @param Note $note The note that has the triggered action.
* @param object $triggered_action The triggered action.
* @return Note|bool
*/
public static function trigger_note_action( $note, $triggered_action ) {
/**
* Fires when an admin note action is taken.
*
* @param string $name The triggered action name.
* @param Note $note The corresponding Note.
*/
do_action( 'woocommerce_note_action', $triggered_action->name, $note );
/**
* Fires when an admin note action is taken.
* For more specific targeting of note actions.
*
* @param Note $note The corresponding Note.
*/
do_action( 'woocommerce_note_action_' . $triggered_action->name, $note );
// Update the note with the status for this action.
if ( ! empty( $triggered_action->status ) ) {
$note->set_status( $triggered_action->status );
}
$note->save();
$event_params = array(
'note_name' => $note->get_name(),
'note_type' => $note->get_type(),
'note_title' => $note->get_title(),
'note_content' => $note->get_content(),
'action_name' => $triggered_action->name,
'action_label' => $triggered_action->label,
'screen' => self::get_screen_name(),
);
if ( in_array( $note->get_type(), array( 'error', 'update' ), true ) ) {
wc_admin_record_tracks_event( 'store_alert_action', $event_params );
} else {
self::record_tracks_event_without_cookies( 'inbox_action_click', $event_params );
}
return $note;
}
/**
* Record tracks event for a specific user.
*
* @param int $user_id The user id we want to record for the event.
* @param string $event_name Name of the event to record.
* @param array $params The params to send to the event recording.
*/
public static function record_tracks_event_with_user( $user_id, $event_name, $params ) {
// We save the current user id to set it back after the event recording.
$current_user_id = get_current_user_id();
wp_set_current_user( $user_id );
self::record_tracks_event_without_cookies( $event_name, $params );
wp_set_current_user( $current_user_id );
}
/**
* Record tracks event without using cookies.
*
* @param string $event_name Name of the event to record.
* @param array $params The params to send to the event recording.
*/
private static function record_tracks_event_without_cookies( $event_name, $params ) {
// We save the cookie to set it back after the event recording.
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$anon_id = isset( $_COOKIE['tk_ai'] ) ? $_COOKIE['tk_ai'] : null;
unset( $_COOKIE['tk_ai'] );
wc_admin_record_tracks_event( $event_name, $params );
if ( isset( $anon_id ) ) {
setcookie( 'tk_ai', $anon_id );
}
}
/**
* Get screen name.
*
* @return string The screen name.
*/
public static function get_screen_name() {
$screen_name = '';
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
parse_str( wp_parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_QUERY ), $queries ); // phpcs:ignore sanitization ok.
}
if ( isset( $queries ) ) {
$page = isset( $queries['page'] ) ? $queries['page'] : null;
$path = isset( $queries['path'] ) ? $queries['path'] : null;
$post_type = isset( $queries['post_type'] ) ? $queries['post_type'] : null;
$post = isset( $queries['post'] ) ? get_post_type( $queries['post'] ) : null;
}
if ( isset( $page ) ) {
$current_page = 'wc-admin' === $page ? 'home_screen' : $page;
$screen_name = isset( $path ) ? substr( str_replace( '/', '_', $path ), 1 ) : $current_page;
} elseif ( isset( $post_type ) ) {
$screen_name = $post_type;
} elseif ( isset( $post ) ) {
$screen_name = $post;
}
return $screen_name;
}
/**
* Loads the data store.
*
* If the "admin-note" data store is unavailable, attempts to load it
* will result in an exception.
* This method catches that exception and throws a custom one instead.
*
* @return \WC_Data_Store The "admin-note" data store.
* @throws NotesUnavailableException Throws exception if data store loading fails.
*/
public static function load_data_store() {
try {
return \WC_Data_Store::load( 'admin-note' );
} catch ( \Exception $e ) {
throw new NotesUnavailableException(
'woocommerce_admin_notes_unavailable',
__( 'Notes are unavailable because the "admin-note" data store cannot be loaded.', 'woocommerce' )
);
}
}
}
Notes/NotesUnavailableException.php 0000644 00000000541 15153746750 0013475 0 ustar 00 <?php
/**
* WooCommerce Admin Notes Unavailable Exception Class
*
* Exception class thrown when an attempt to use notes is made but notes are unavailable.
*/
namespace Automattic\WooCommerce\Admin\Notes;
defined( 'ABSPATH' ) || exit;
/**
* Notes\NotesUnavailableException class.
*/
class NotesUnavailableException extends \WC_Data_Exception {}
Overrides/Order.php 0000644 00000007123 15153746750 0010312 0 ustar 00 <?php
/**
* WC Admin Order
*
* WC Admin Order class that adds some functionality on top of general WooCommerce WC_Order.
*/
namespace Automattic\WooCommerce\Admin\Overrides;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
use Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore;
/**
* WC_Order subclass.
*/
class Order extends \WC_Order {
/**
* Order traits.
*/
use OrderTraits;
/**
* Holds refund amounts and quantities for the order.
*
* @var void|array
*/
protected $refunded_line_items;
/**
* Caches the customer ID.
*
* @var int
*/
public $customer_id = null;
/**
* Get only core class data in array format.
*
* @return array
*/
public function get_data_without_line_items() {
return array_merge(
array(
'id' => $this->get_id(),
),
$this->data,
array(
'number' => $this->get_order_number(),
'meta_data' => $this->get_meta_data(),
)
);
}
/**
* Get order line item data by type.
*
* @param string $type Order line item type.
* @return array|bool Array of line items on success, boolean false on failure.
*/
public function get_line_item_data( $type ) {
$type_to_items = array(
'line_items' => 'line_item',
'tax_lines' => 'tax',
'shipping_lines' => 'shipping',
'fee_lines' => 'fee',
'coupon_lines' => 'coupon',
);
if ( isset( $type_to_items[ $type ] ) ) {
return $this->get_items( $type_to_items[ $type ] );
}
return false;
}
/**
* Add filter(s) required to hook this class to substitute WC_Order.
*/
public static function add_filters() {
add_filter( 'woocommerce_order_class', array( __CLASS__, 'order_class_name' ), 10, 3 );
}
/**
* Filter function to swap class WC_Order for this one in cases when it's suitable.
*
* @param string $classname Name of the class to be created.
* @param string $order_type Type of order object to be created.
* @param number $order_id Order id to create.
*
* @return string
*/
public static function order_class_name( $classname, $order_type, $order_id ) {
// @todo - Only substitute class when necessary (during sync).
if ( 'WC_Order' === $classname ) {
return '\Automattic\WooCommerce\Admin\Overrides\Order';
} else {
return $classname;
}
}
/**
* Get the customer ID used for reports in the customer lookup table.
*
* @return int
*/
public function get_report_customer_id() {
if ( is_null( $this->customer_id ) ) {
$this->customer_id = CustomersDataStore::get_or_create_customer_from_order( $this );
}
return $this->customer_id;
}
/**
* Returns true if the customer has made an earlier order.
*
* @return bool
*/
public function is_returning_customer() {
return OrdersStatsDataStore::is_returning_customer( $this, $this->get_report_customer_id() );
}
/**
* Get the customer's first name.
*/
public function get_customer_first_name() {
if ( $this->get_user_id() ) {
return get_user_meta( $this->get_user_id(), 'first_name', true );
}
if ( '' !== $this->get_billing_first_name( 'edit' ) ) {
return $this->get_billing_first_name( 'edit' );
} else {
return $this->get_shipping_first_name( 'edit' );
}
}
/**
* Get the customer's last name.
*/
public function get_customer_last_name() {
if ( $this->get_user_id() ) {
return get_user_meta( $this->get_user_id(), 'last_name', true );
}
if ( '' !== $this->get_billing_last_name( 'edit' ) ) {
return $this->get_billing_last_name( 'edit' );
} else {
return $this->get_shipping_last_name( 'edit' );
}
}
}
Overrides/OrderRefund.php 0000644 00000004034 15153746750 0011454 0 ustar 00 <?php
/**
* WC Admin Order Refund
*
* WC Admin Order Refund class that adds some functionality on top of general WooCommerce WC_Order_Refund.
*/
namespace Automattic\WooCommerce\Admin\Overrides;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
/**
* WC_Order_Refund subclass.
*/
class OrderRefund extends \WC_Order_Refund {
/**
* Order traits.
*/
use OrderTraits;
/**
* Caches the customer ID.
*
* @var int
*/
public $customer_id = null;
/**
* Add filter(s) required to hook this class to substitute WC_Order_Refund.
*/
public static function add_filters() {
add_filter( 'woocommerce_order_class', array( __CLASS__, 'order_class_name' ), 10, 3 );
}
/**
* Filter function to swap class WC_Order_Refund for this one in cases when it's suitable.
*
* @param string $classname Name of the class to be created.
* @param string $order_type Type of order object to be created.
* @param number $order_id Order id to create.
*
* @return string
*/
public static function order_class_name( $classname, $order_type, $order_id ) {
// @todo - Only substitute class when necessary (during sync).
if ( 'WC_Order_Refund' === $classname ) {
return '\Automattic\WooCommerce\Admin\Overrides\OrderRefund';
} else {
return $classname;
}
}
/**
* Get the customer ID of the parent order used for reports in the customer lookup table.
*
* @return int|bool Customer ID of parent order, or false if parent order not found.
*/
public function get_report_customer_id() {
if ( is_null( $this->customer_id ) ) {
$parent_order = \wc_get_order( $this->get_parent_id() );
if ( ! $parent_order ) {
$this->customer_id = false;
}
$this->customer_id = CustomersDataStore::get_or_create_customer_from_order( $parent_order );
}
return $this->customer_id;
}
/**
* Returns null since refunds should not be counted towards returning customer counts.
*
* @return null
*/
public function is_returning_customer() {
return null;
}
}
Overrides/OrderTraits.php 0000644 00000005077 15153746750 0011507 0 ustar 00 <?php
/**
* WC Admin Order Trait
*
* WC Admin Order Trait class that houses shared functionality across order and refund classes.
*/
namespace Automattic\WooCommerce\Admin\Overrides;
defined( 'ABSPATH' ) || exit;
/**
* OrderTraits class.
*/
trait OrderTraits {
/**
* Calculate shipping amount for line item/product as a total shipping amount ratio based on quantity.
*
* @param WC_Order_Item $item Line item from order.
*
* @return float|int
*/
public function get_item_shipping_amount( $item ) {
// Shipping amount loosely based on woocommerce code in includes/admin/meta-boxes/views/html-order-item(s).php
// distributed simply based on number of line items.
$product_qty = $item->get_quantity( 'edit' );
$order_items = $this->get_item_count();
if ( 0 === $order_items ) {
return 0;
}
$total_shipping_amount = (float) $this->get_shipping_total();
return $total_shipping_amount / $order_items * $product_qty;
}
/**
* Calculate shipping tax amount for line item/product as a total shipping tax amount ratio based on quantity.
*
* Loosely based on code in includes/admin/meta-boxes/views/html-order-item(s).php.
*
* @todo If WC is currently not tax enabled, but it was before (or vice versa), would this work correctly?
*
* @param WC_Order_Item $item Line item from order.
*
* @return float|int
*/
public function get_item_shipping_tax_amount( $item ) {
$order_items = $this->get_item_count();
if ( 0 === $order_items ) {
return 0;
}
$product_qty = $item->get_quantity( 'edit' );
$order_taxes = $this->get_taxes();
$line_items_shipping = $this->get_items( 'shipping' );
$total_shipping_tax_amount = 0;
foreach ( $line_items_shipping as $item_id => $shipping_item ) {
$tax_data = $shipping_item->get_taxes();
if ( $tax_data ) {
foreach ( $order_taxes as $tax_item ) {
$tax_item_id = $tax_item->get_rate_id();
$tax_item_total = isset( $tax_data['total'][ $tax_item_id ] ) ? (float) $tax_data['total'][ $tax_item_id ] : 0;
$total_shipping_tax_amount += $tax_item_total;
}
}
}
return $total_shipping_tax_amount / $order_items * $product_qty;
}
/**
* Calculates coupon amount for specified line item/product.
*
* Coupon calculation based on woocommerce code in includes/admin/meta-boxes/views/html-order-item.php.
*
* @param WC_Order_Item $item Line item from order.
*
* @return float
*/
public function get_item_coupon_amount( $item ) {
return floatval( $item->get_subtotal( 'edit' ) - $item->get_total( 'edit' ) );
}
}
Overrides/ThemeUpgrader.php 0000644 00000004004 15153746750 0011766 0 ustar 00 <?php
/**
* Theme upgrader used in REST API response.
*/
namespace Automattic\WooCommerce\Admin\Overrides;
defined( 'ABSPATH' ) || exit;
/**
* Admin\Overrides\ThemeUpgrader Class.
*/
class ThemeUpgrader extends \Theme_Upgrader {
/**
* Install a theme package.
*
* @param string $package The full local path or URI of the package.
* @param array $args {
* Optional. Other arguments for installing a theme package. Default empty array.
*
* @type bool $clear_update_cache Whether to clear the updates cache if successful.
* Default true.
* }
*
* @return bool|WP_Error True if the installation was successful, false or a WP_Error object otherwise.
*/
public function install( $package, $args = array() ) {
$defaults = array(
'clear_update_cache' => true,
);
$parsed_args = wp_parse_args( $args, $defaults );
$this->init();
$this->install_strings();
add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
add_filter( 'upgrader_post_install', array( $this, 'check_parent_theme_filter' ), 10, 3 );
if ( $parsed_args['clear_update_cache'] ) {
// Clear cache so wp_update_themes() knows about the new theme.
add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
}
$result = $this->run(
array(
'package' => $package,
'destination' => get_theme_root(),
'clear_destination' => false, // Do not overwrite files.
'clear_working' => true,
'hook_extra' => array(
'type' => 'theme',
'action' => 'install',
),
)
);
remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
remove_filter( 'upgrader_post_install', array( $this, 'check_parent_theme_filter' ) );
if ( $result && ! is_wp_error( $result ) ) {
// Refresh the Theme Update information.
wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
}
return $result;
}
}
Overrides/ThemeUpgraderSkin.php 0000644 00000001446 15153746750 0012622 0 ustar 00 <?php
/**
* Theme upgrader skin used in REST API response.
*/
namespace Automattic\WooCommerce\Admin\Overrides;
defined( 'ABSPATH' ) || exit;
/**
* Admin\Overrides\ThemeUpgraderSkin Class.
*/
class ThemeUpgraderSkin extends \Theme_Upgrader_Skin {
/**
* Avoid undefined property error from \Theme_Upgrader::check_parent_theme_filter().
*
* @var array
*/
public $api;
/**
* Hide the skin header display.
*/
public function header() {}
/**
* Hide the skin footer display.
*/
public function footer() {}
/**
* Hide the skin feedback display.
*
* @param string $string String to display.
* @param mixed ...$args Optional text replacements.
*/
public function feedback( $string, ...$args ) {}
/**
* Hide the skin after display.
*/
public function after() {}
}
PageController.php 0000644 00000042127 15153746750 0010220 0 ustar 00 <?php
/**
* PageController
*/
namespace Automattic\WooCommerce\Admin;
use Automattic\WooCommerce\Admin\Features\Navigation\Screen;
use Automattic\WooCommerce\Internal\Admin\Loader;
defined( 'ABSPATH' ) || exit;
/**
* PageController
*/
class PageController {
/**
* App entry point.
*/
const APP_ENTRY_POINT = 'wc-admin';
// JS-powered page root.
const PAGE_ROOT = 'wc-admin';
/**
* Singleton instance of self.
*
* @var PageController
*/
private static $instance = false;
/**
* Current page ID (or false if not registered with this controller).
*
* @var string
*/
private $current_page = null;
/**
* Registered pages
* Contains information (breadcrumbs, menu info) about JS powered pages and classic WooCommerce pages.
*
* @var array
*/
private $pages = array();
/**
* We want a single instance of this class so we can accurately track registered menus and pages.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
* Hooks added here should be removed in `wc_admin_initialize` via the feature plugin.
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'register_page_handler' ) );
add_action( 'admin_menu', array( $this, 'register_store_details_page' ) );
// priority is 20 to run after https://github.com/woocommerce/woocommerce/blob/a55ae325306fc2179149ba9b97e66f32f84fdd9c/includes/admin/class-wc-admin-menus.php#L165.
add_action( 'admin_head', array( $this, 'remove_app_entry_page_menu_item' ), 20 );
}
/**
* Connect an existing page to wc-admin.
*
* @param array $options {
* Array describing the page.
*
* @type string id Id to reference the page.
* @type string|array title Page title. Used in menus and breadcrumbs.
* @type string|null parent Parent ID. Null for new top level page.
* @type string path Path for this page. E.g. admin.php?page=wc-settings&tab=checkout
* @type string capability Capability needed to access the page.
* @type string icon Icon. Dashicons helper class, base64-encoded SVG, or 'none'.
* @type int position Menu item position.
* @type boolean js_page If this is a JS-powered page.
* }
*/
public function connect_page( $options ) {
if ( ! is_array( $options['title'] ) ) {
$options['title'] = array( $options['title'] );
}
/**
* Filter the options when connecting or registering a page.
*
* Use the `js_page` option to determine if registering.
*
* @param array $options {
* Array describing the page.
*
* @type string id Id to reference the page.
* @type string|array title Page title. Used in menus and breadcrumbs.
* @type string|null parent Parent ID. Null for new top level page.
* @type string screen_id The screen ID that represents the connected page. (Not required for registering).
* @type string path Path for this page. E.g. admin.php?page=wc-settings&tab=checkout
* @type string capability Capability needed to access the page.
* @type string icon Icon. Dashicons helper class, base64-encoded SVG, or 'none'.
* @type int position Menu item position.
* @type boolean js_page If this is a JS-powered page.
* }
*/
$options = apply_filters( 'woocommerce_navigation_connect_page_options', $options );
// @todo check for null ID, or collision.
$this->pages[ $options['id'] ] = $options;
}
/**
* Determine the current page ID, if it was registered with this controller.
*/
public function determine_current_page() {
$current_url = '';
$current_screen_id = $this->get_current_screen_id();
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$current_url = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) );
}
$current_query = wp_parse_url( $current_url, PHP_URL_QUERY );
parse_str( (string) $current_query, $current_pieces );
$current_path = empty( $current_pieces['page'] ) ? '' : $current_pieces['page'];
$current_path .= empty( $current_pieces['path'] ) ? '' : '&path=' . $current_pieces['path'];
foreach ( $this->pages as $page ) {
if ( isset( $page['js_page'] ) && $page['js_page'] ) {
// Check registered admin pages.
if (
$page['path'] === $current_path
) {
$this->current_page = $page;
return;
}
} else {
// Check connected admin pages.
if (
isset( $page['screen_id'] ) &&
$page['screen_id'] === $current_screen_id
) {
$this->current_page = $page;
return;
}
}
}
$this->current_page = false;
}
/**
* Get breadcrumbs for WooCommerce Admin Page navigation.
*
* @return array Navigation pieces (breadcrumbs).
*/
public function get_breadcrumbs() {
$current_page = $this->get_current_page();
// Bail if this isn't a page registered with this controller.
if ( false === $current_page ) {
// Filter documentation below.
return apply_filters( 'woocommerce_navigation_get_breadcrumbs', array( '' ), $current_page );
}
if ( 1 === count( $current_page['title'] ) ) {
$breadcrumbs = $current_page['title'];
} else {
// If this page has multiple title pieces, only link the first one.
$breadcrumbs = array_merge(
array(
array( $current_page['path'], reset( $current_page['title'] ) ),
),
array_slice( $current_page['title'], 1 )
);
}
if ( isset( $current_page['parent'] ) ) {
$parent_id = $current_page['parent'];
while ( $parent_id ) {
if ( isset( $this->pages[ $parent_id ] ) ) {
$parent = $this->pages[ $parent_id ];
if ( 0 === strpos( $parent['path'], self::PAGE_ROOT ) ) {
$parent['path'] = 'admin.php?page=' . $parent['path'];
}
array_unshift( $breadcrumbs, array( $parent['path'], reset( $parent['title'] ) ) );
$parent_id = isset( $parent['parent'] ) ? $parent['parent'] : false;
} else {
$parent_id = false;
}
}
}
$woocommerce_breadcrumb = array( 'admin.php?page=' . self::PAGE_ROOT, __( 'WooCommerce', 'woocommerce' ) );
array_unshift( $breadcrumbs, $woocommerce_breadcrumb );
/**
* The navigation breadcrumbs for the current page.
*
* @param array $breadcrumbs Navigation pieces (breadcrumbs).
* @param array|boolean $current_page The connected page data or false if not identified.
*/
return apply_filters( 'woocommerce_navigation_get_breadcrumbs', $breadcrumbs, $current_page );
}
/**
* Get the current page.
*
* @return array|boolean Current page or false if not registered with this controller.
*/
public function get_current_page() {
// If 'current_screen' hasn't fired yet, the current page calculation
// will fail which causes `false` to be returned for all subsquent calls.
if ( ! did_action( 'current_screen' ) ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Current page retrieval should be called on or after the `current_screen` hook.', 'woocommerce' ), '0.16.0' );
}
if ( is_null( $this->current_page ) ) {
$this->determine_current_page();
}
return $this->current_page;
}
/**
* Returns the current screen ID.
*
* This is slightly different from WP's get_current_screen, in that it attaches an action,
* so certain pages like 'add new' pages can have different breadcrumbs or handling.
* It also catches some more unique dynamic pages like taxonomy/attribute management.
*
* Format:
* - {$current_screen->action}-{$current_screen->action}-tab-section
* - {$current_screen->action}-{$current_screen->action}-tab
* - {$current_screen->action}-{$current_screen->action} if no tab is present
* - {$current_screen->action} if no action or tab is present
*
* @return string Current screen ID.
*/
public function get_current_screen_id() {
$current_screen = get_current_screen();
if ( ! $current_screen ) {
// Filter documentation below.
return apply_filters( 'woocommerce_navigation_current_screen_id', false, $current_screen );
}
$screen_pieces = array( $current_screen->id );
if ( $current_screen->action ) {
$screen_pieces[] = $current_screen->action;
}
if (
! empty( $current_screen->taxonomy ) &&
isset( $current_screen->post_type ) &&
'product' === $current_screen->post_type
) {
// Editing a product attribute.
if ( 0 === strpos( $current_screen->taxonomy, 'pa_' ) ) {
$screen_pieces = array( 'product_page_product_attribute-edit' );
}
// Editing a product taxonomy term.
if ( ! empty( $_GET['tag_ID'] ) ) {
$screen_pieces = array( $current_screen->taxonomy );
}
}
// Pages with default tab values.
$pages_with_tabs = apply_filters(
'woocommerce_navigation_pages_with_tabs',
array(
'wc-reports' => 'orders',
'wc-settings' => 'general',
'wc-status' => 'status',
'wc-addons' => 'browse-extensions',
)
);
// Tabs that have sections as well.
$wc_emails = \WC_Emails::instance();
$wc_email_ids = array_map( 'sanitize_title', array_keys( $wc_emails->get_emails() ) );
$tabs_with_sections = apply_filters(
'woocommerce_navigation_page_tab_sections',
array(
'products' => array( '', 'inventory', 'downloadable' ),
'shipping' => array( '', 'options', 'classes' ),
'checkout' => array( 'bacs', 'cheque', 'cod', 'paypal' ),
'email' => $wc_email_ids,
'advanced' => array(
'',
'keys',
'webhooks',
'legacy_api',
'woocommerce_com',
),
'browse-extensions' => array( 'helper' ),
)
);
if ( ! empty( $_GET['page'] ) ) {
$page = wc_clean( wp_unslash( $_GET['page'] ) );
if ( in_array( $page, array_keys( $pages_with_tabs ) ) ) {
if ( ! empty( $_GET['tab'] ) ) {
$tab = wc_clean( wp_unslash( $_GET['tab'] ) );
} else {
$tab = $pages_with_tabs[ $page ];
}
$screen_pieces[] = $tab;
if ( ! empty( $_GET['section'] ) ) {
$section = wc_clean( wp_unslash( $_GET['section'] ) );
if (
isset( $tabs_with_sections[ $tab ] ) &&
in_array( $section, array_keys( $tabs_with_sections[ $tab ] ) )
) {
$screen_pieces[] = $section;
}
}
// Editing a shipping zone.
if ( ( 'shipping' === $tab ) && isset( $_GET['zone_id'] ) ) {
$screen_pieces[] = 'edit_zone';
}
}
}
/**
* The current screen id.
*
* Used for identifying pages to render the WooCommerce Admin header.
*
* @param string|boolean $screen_id The screen id or false if not identified.
* @param WP_Screen $current_screen The current WP_Screen.
*/
return apply_filters( 'woocommerce_navigation_current_screen_id', implode( '-', $screen_pieces ), $current_screen );
}
/**
* Returns the path from an ID.
*
* @param string $id ID to get path for.
* @return string Path for the given ID, or the ID on lookup miss.
*/
public function get_path_from_id( $id ) {
if ( isset( $this->pages[ $id ] ) && isset( $this->pages[ $id ]['path'] ) ) {
return $this->pages[ $id ]['path'];
}
return $id;
}
/**
* Returns true if we are on a page connected to this controller.
*
* @return boolean
*/
public function is_connected_page() {
$current_page = $this->get_current_page();
if ( false === $current_page ) {
$is_connected_page = false;
} else {
$is_connected_page = isset( $current_page['js_page'] ) ? ! $current_page['js_page'] : true;
}
// Disable embed on the block editor.
$current_screen = did_action( 'current_screen' ) ? get_current_screen() : false;
if ( ! empty( $current_screen ) && method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
$is_connected_page = false;
}
/**
* Whether or not the current page is an existing page connected to this controller.
*
* Used to determine if the WooCommerce Admin header should be rendered.
*
* @param boolean $is_connected_page True if the current page is connected.
* @param array|boolean $current_page The connected page data or false if not identified.
*/
return apply_filters( 'woocommerce_navigation_is_connected_page', $is_connected_page, $current_page );
}
/**
* Returns true if we are on a page registed with this controller.
*
* @return boolean
*/
public function is_registered_page() {
$current_page = $this->get_current_page();
if ( false === $current_page ) {
$is_registered_page = false;
} else {
$is_registered_page = isset( $current_page['js_page'] ) && $current_page['js_page'];
}
/**
* Whether or not the current page was registered with this controller.
*
* Used to determine if this is a JS-powered WooCommerce Admin page.
*
* @param boolean $is_registered_page True if the current page was registered with this controller.
* @param array|boolean $current_page The registered page data or false if not identified.
*/
return apply_filters( 'woocommerce_navigation_is_registered_page', $is_registered_page, $current_page );
}
/**
* Adds a JS powered page to wc-admin.
*
* @param array $options {
* Array describing the page.
*
* @type string id Id to reference the page.
* @type string title Page title. Used in menus and breadcrumbs.
* @type string|null parent Parent ID. Null for new top level page.
* @type string path Path for this page, full path in app context; ex /analytics/report
* @type string capability Capability needed to access the page.
* @type string icon Icon. Dashicons helper class, base64-encoded SVG, or 'none'.
* @type int position Menu item position.
* @type int order Navigation item order.
* }
*/
public function register_page( $options ) {
$defaults = array(
'id' => null,
'parent' => null,
'title' => '',
'capability' => 'view_woocommerce_reports',
'path' => '',
'icon' => '',
'position' => null,
'js_page' => true,
);
$options = wp_parse_args( $options, $defaults );
if ( 0 !== strpos( $options['path'], self::PAGE_ROOT ) ) {
$options['path'] = self::PAGE_ROOT . '&path=' . $options['path'];
}
if ( null !== $options['position'] ) {
$options['position'] = intval( round( $options['position'] ) );
}
if ( is_null( $options['parent'] ) ) {
add_menu_page(
$options['title'],
$options['title'],
$options['capability'],
$options['path'],
array( __CLASS__, 'page_wrapper' ),
$options['icon'],
$options['position']
);
} else {
$parent_path = $this->get_path_from_id( $options['parent'] );
// @todo check for null path.
add_submenu_page(
$parent_path,
$options['title'],
$options['title'],
$options['capability'],
$options['path'],
array( __CLASS__, 'page_wrapper' )
);
}
$this->connect_page( $options );
}
/**
* Get registered pages.
*
* @return array
*/
public function get_pages() {
return $this->pages;
}
/**
* Set up a div for the app to render into.
*/
public static function page_wrapper() {
Loader::page_wrapper();
}
/**
* Connects existing WooCommerce pages.
*
* @todo The entry point for the embed needs moved to this class as well.
*/
public function register_page_handler() {
require_once WC_ADMIN_ABSPATH . 'includes/react-admin/connect-existing-pages.php';
}
/**
* Registers the store details (profiler) page.
*/
public function register_store_details_page() {
wc_admin_register_page(
array(
'title' => __( 'Setup Wizard', 'woocommerce' ),
'parent' => '',
'path' => '/setup-wizard',
)
);
}
/**
* Remove the menu item for the app entry point page.
*/
public function remove_app_entry_page_menu_item() {
global $submenu;
// User does not have capabilites to see the submenu.
if ( ! current_user_can( 'manage_woocommerce' ) || empty( $submenu['woocommerce'] ) ) {
return;
}
$wc_admin_key = null;
foreach ( $submenu['woocommerce'] as $submenu_key => $submenu_item ) {
// Our app entry page menu item has no title.
if ( is_null( $submenu_item[0] ) && self::APP_ENTRY_POINT === $submenu_item[2] ) {
$wc_admin_key = $submenu_key;
break;
}
}
if ( ! $wc_admin_key ) {
return;
}
unset( $submenu['woocommerce'][ $wc_admin_key ] );
}
/**
* Returns true if we are on a JS powered admin page or
* a "classic" (non JS app) powered admin page (an embedded page).
*/
public static function is_admin_or_embed_page() {
return self::is_admin_page() || self::is_embed_page();
}
/**
* Returns true if we are on a JS powered admin page.
*/
public static function is_admin_page() {
// phpcs:disable WordPress.Security.NonceVerification
return isset( $_GET['page'] ) && 'wc-admin' === $_GET['page'];
// phpcs:enable WordPress.Security.NonceVerification
}
/**
* Returns true if we are on a "classic" (non JS app) powered admin page.
*
* TODO: See usage in `admin.php`. This needs refactored and implemented properly in core.
*/
public static function is_embed_page() {
return wc_admin_is_connected_page() || ( ! self::is_admin_page() && class_exists( 'Automattic\WooCommerce\Admin\Features\Navigation\Screen' ) && Screen::is_woocommerce_page() );
}
}
PluginsHelper.php 0000644 00000034472 15153746750 0010065 0 ustar 00 <?php
/**
* PluginsHelper
*
* Helper class for the site's plugins.
*/
namespace Automattic\WooCommerce\Admin;
use ActionScheduler;
use ActionScheduler_DBStore;
use ActionScheduler_QueueRunner;
use Automatic_Upgrader_Skin;
use Automattic\WooCommerce\Admin\PluginsInstallLoggers\AsyncPluginsInstallLogger;
use Automattic\WooCommerce\Admin\PluginsInstallLoggers\PluginsInstallLogger;
use Plugin_Upgrader;
use WP_Error;
use WP_Upgrader;
defined( 'ABSPATH' ) || exit;
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
/**
* Class PluginsHelper
*/
class PluginsHelper {
/**
* Initialize hooks.
*/
public static function init() {
add_action( 'woocommerce_plugins_install_callback', array( __CLASS__, 'install_plugins' ), 10, 2 );
add_action( 'woocommerce_plugins_install_and_activate_async_callback', array( __CLASS__, 'install_and_activate_plugins_async_callback' ), 10, 2 );
add_action( 'woocommerce_plugins_activate_callback', array( __CLASS__, 'activate_plugins' ), 10, 2 );
}
/**
* Get the path to the plugin file relative to the plugins directory from the plugin slug.
*
* E.g. 'woocommerce' returns 'woocommerce/woocommerce.php'
*
* @param string $slug Plugin slug to get path for.
*
* @return string|false
*/
public static function get_plugin_path_from_slug( $slug ) {
$plugins = get_plugins();
if ( strstr( $slug, '/' ) ) {
// The slug is already a plugin path.
return $slug;
}
foreach ( $plugins as $plugin_path => $data ) {
$path_parts = explode( '/', $plugin_path );
if ( $path_parts[0] === $slug ) {
return $plugin_path;
}
}
return false;
}
/**
* Get an array of installed plugin slugs.
*
* @return array
*/
public static function get_installed_plugin_slugs() {
return array_map(
function ( $plugin_path ) {
$path_parts = explode( '/', $plugin_path );
return $path_parts[0];
},
array_keys( get_plugins() )
);
}
/**
* Get an array of installed plugins with their file paths as a key value pair.
*
* @return array
*/
public static function get_installed_plugins_paths() {
$plugins = get_plugins();
$installed_plugins = array();
foreach ( $plugins as $path => $plugin ) {
$path_parts = explode( '/', $path );
$slug = $path_parts[0];
$installed_plugins[ $slug ] = $path;
}
return $installed_plugins;
}
/**
* Get an array of active plugin slugs.
*
* @return array
*/
public static function get_active_plugin_slugs() {
return array_map(
function ( $plugin_path ) {
$path_parts = explode( '/', $plugin_path );
return $path_parts[0];
},
get_option( 'active_plugins', array() )
);
}
/**
* Checks if a plugin is installed.
*
* @param string $plugin Path to the plugin file relative to the plugins directory or the plugin directory name.
*
* @return bool
*/
public static function is_plugin_installed( $plugin ) {
$plugin_path = self::get_plugin_path_from_slug( $plugin );
return $plugin_path ? array_key_exists( $plugin_path, get_plugins() ) : false;
}
/**
* Checks if a plugin is active.
*
* @param string $plugin Path to the plugin file relative to the plugins directory or the plugin directory name.
*
* @return bool
*/
public static function is_plugin_active( $plugin ) {
$plugin_path = self::get_plugin_path_from_slug( $plugin );
return $plugin_path ? in_array( $plugin_path, get_option( 'active_plugins', array() ), true ) : false;
}
/**
* Get plugin data.
*
* @param string $plugin Path to the plugin file relative to the plugins directory or the plugin directory name.
*
* @return array|false
*/
public static function get_plugin_data( $plugin ) {
$plugin_path = self::get_plugin_path_from_slug( $plugin );
$plugins = get_plugins();
return isset( $plugins[ $plugin_path ] ) ? $plugins[ $plugin_path ] : false;
}
/**
* Install an array of plugins.
*
* @param array $plugins Plugins to install.
* @param PluginsInstallLogger|null $logger an optional logger.
*
* @return array
*/
public static function install_plugins( $plugins, PluginsInstallLogger $logger = null ) {
/**
* Filter the list of plugins to install.
*
* @param array $plugins A list of the plugins to install.
*
* @since 6.4.0
*/
$plugins = apply_filters( 'woocommerce_admin_plugins_pre_install', $plugins );
if ( empty( $plugins ) || ! is_array( $plugins ) ) {
return new WP_Error(
'woocommerce_plugins_invalid_plugins',
__( 'Plugins must be a non-empty array.', 'woocommerce' )
);
}
require_once ABSPATH . 'wp-admin/includes/plugin.php';
include_once ABSPATH . '/wp-admin/includes/admin.php';
include_once ABSPATH . '/wp-admin/includes/plugin-install.php';
include_once ABSPATH . '/wp-admin/includes/plugin.php';
include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php';
$existing_plugins = self::get_installed_plugins_paths();
$installed_plugins = array();
$results = array();
$time = array();
$errors = new WP_Error();
$install_start_time = time();
foreach ( $plugins as $plugin ) {
$slug = sanitize_key( $plugin );
$logger && $logger->install_requested( $plugin );
if ( isset( $existing_plugins[ $slug ] ) ) {
$installed_plugins[] = $plugin;
$logger && $logger->installed( $plugin, 0 );
continue;
}
$start_time = microtime( true );
$api = plugins_api(
'plugin_information',
array(
'slug' => $slug,
'fields' => array(
'sections' => false,
),
)
);
if ( is_wp_error( $api ) ) {
$properties = array(
'error_message' => sprintf(
// translators: %s: plugin slug (example: woocommerce-services).
__(
'The requested plugin `%s` could not be installed. Plugin API call failed.',
'woocommerce'
),
$slug
),
'api_error_message' => $api->get_error_message(),
'slug' => $slug,
);
wc_admin_record_tracks_event( 'install_plugin_error', $properties );
/**
* Action triggered when a plugin API call failed.
*
* @param string $slug The plugin slug.
* @param WP_Error $api The API response.
*
* @since 6.4.0
*/
do_action( 'woocommerce_plugins_install_api_error', $slug, $api );
$error_message = sprintf(
/* translators: %s: plugin slug (example: woocommerce-services) */
__( 'The requested plugin `%s` could not be installed. Plugin API call failed.', 'woocommerce' ),
$slug
);
$errors->add( $plugin, $error_message );
$logger && $logger->add_error( $plugin, $error_message );
continue;
}
$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
$result = $upgrader->install( $api->download_link );
// result can be false or WP_Error.
$results[ $plugin ] = $result;
$time[ $plugin ] = round( ( microtime( true ) - $start_time ) * 1000 );
if ( is_wp_error( $result ) || is_null( $result ) ) {
$properties = array(
'error_message' => sprintf(
/* translators: %s: plugin slug (example: woocommerce-services) */
__(
'The requested plugin `%s` could not be installed.',
'woocommerce'
),
$slug
),
'slug' => $slug,
'api_version' => $api->version,
'api_download_link' => $api->download_link,
'upgrader_skin_message' => implode( ',', $upgrader->skin->get_upgrade_messages() ),
'result' => is_wp_error( $result ) ? $result->get_error_message() : 'null',
);
wc_admin_record_tracks_event( 'install_plugin_error', $properties );
/**
* Action triggered when a plugin installation fails.
*
* @param string $slug The plugin slug.
* @param object $api The plugin API object.
* @param WP_Error|null $result The result of the plugin installation.
* @param Plugin_Upgrader $upgrader The plugin upgrader.
*
* @since 6.4.0
*/
do_action( 'woocommerce_plugins_install_error', $slug, $api, $result, $upgrader );
$install_error_message = sprintf(
/* translators: %s: plugin slug (example: woocommerce-services) */
__( 'The requested plugin `%s` could not be installed. Upgrader install failed.', 'woocommerce' ),
$slug
);
$errors->add(
$plugin,
$install_error_message
);
$logger && $logger->add_error( $plugin, $install_error_message );
continue;
}
$installed_plugins[] = $plugin;
$logger && $logger->installed( $plugin, $time[ $plugin ] );
}
$data = array(
'installed' => $installed_plugins,
'results' => $results,
'errors' => $errors,
'time' => $time,
);
$logger && $logger->complete( array_merge( $data, array( 'start_time' => $install_start_time ) ) );
return $data;
}
/**
* Callback regsitered by OnboardingPlugins::install_and_activate_async.
*
* It is used to call install_plugins and activate_plugins with a custom logger.
*
* @param array $plugins A list of plugins to install.
* @param string $job_id An unique job I.D.
* @return bool
*/
public static function install_and_activate_plugins_async_callback( array $plugins, string $job_id ) {
$option_name = 'woocommerce_onboarding_plugins_install_and_activate_async_' . $job_id;
$logger = new AsyncPluginsInstallLogger( $option_name );
self::install_plugins( $plugins, $logger );
self::activate_plugins( $plugins, $logger );
return true;
}
/**
* Schedule plugin installation.
*
* @param array $plugins Plugins to install.
*
* @return string Job ID.
*/
public static function schedule_install_plugins( $plugins ) {
if ( empty( $plugins ) || ! is_array( $plugins ) ) {
return new WP_Error(
'woocommerce_plugins_invalid_plugins',
__( 'Plugins must be a non-empty array.', 'woocommerce' ),
404
);
}
$job_id = uniqid();
WC()->queue()->schedule_single( time() + 5, 'woocommerce_plugins_install_callback', array( $plugins ) );
return $job_id;
}
/**
* Activate the requested plugins.
*
* @param array $plugins Plugins.
* @param PluginsInstallLogger|null $logger Logger.
*
* @return WP_Error|array Plugin Status
*/
public static function activate_plugins( $plugins, PluginsInstallLogger $logger = null ) {
if ( empty( $plugins ) || ! is_array( $plugins ) ) {
return new WP_Error(
'woocommerce_plugins_invalid_plugins',
__( 'Plugins must be a non-empty array.', 'woocommerce' ),
404
);
}
require_once ABSPATH . 'wp-admin/includes/plugin.php';
// the mollie-payments-for-woocommerce plugin calls `WP_Filesystem()` during it's activation hook, which crashes without this include.
require_once ABSPATH . 'wp-admin/includes/file.php';
/**
* Filter the list of plugins to activate.
*
* @param array $plugins A list of the plugins to activate.
*
* @since 6.4.0
*/
$plugins = apply_filters( 'woocommerce_admin_plugins_pre_activate', $plugins );
$plugin_paths = self::get_installed_plugins_paths();
$errors = new WP_Error();
$activated_plugins = array();
foreach ( $plugins as $plugin ) {
$slug = $plugin;
$path = isset( $plugin_paths[ $slug ] ) ? $plugin_paths[ $slug ] : false;
if ( ! $path ) {
/* translators: %s: plugin slug (example: woocommerce-services) */
$message = sprintf( __( 'The requested plugin `%s`. is not yet installed.', 'woocommerce' ), $slug );
$errors->add(
$plugin,
$message
);
$logger && $logger->add_error( $plugin, $message );
continue;
}
$result = activate_plugin( $path );
if ( ! is_plugin_active( $path ) ) {
/**
* Action triggered when a plugin activation fails.
*
* @param string $slug The plugin slug.
* @param null|WP_Error $result The result of the plugin activation.
*
* @since 6.4.0
*/
do_action( 'woocommerce_plugins_activate_error', $slug, $result );
/* translators: %s: plugin slug (example: woocommerce-services) */
$message = sprintf( __( 'The requested plugin `%s` could not be activated.', 'woocommerce' ), $slug );
$errors->add(
$plugin,
$message
);
$logger && $logger->add_error( $plugin, $message );
continue;
}
$activated_plugins[] = $plugin;
$logger && $logger->activated( $plugin );
}
$data = array(
'activated' => $activated_plugins,
'active' => self::get_active_plugin_slugs(),
'errors' => $errors,
);
return $data;
}
/**
* Schedule plugin activation.
*
* @param array $plugins Plugins to activate.
*
* @return string Job ID.
*/
public static function schedule_activate_plugins( $plugins ) {
if ( empty( $plugins ) || ! is_array( $plugins ) ) {
return new WP_Error(
'woocommerce_plugins_invalid_plugins',
__( 'Plugins must be a non-empty array.', 'woocommerce' ),
404
);
}
$job_id = uniqid();
WC()->queue()->schedule_single(
time() + 5,
'woocommerce_plugins_activate_callback',
array( $plugins, $job_id )
);
return $job_id;
}
/**
* Installation status.
*
* @param int $job_id Job ID.
*
* @return array Job data.
*/
public static function get_installation_status( $job_id = null ) {
$actions = WC()->queue()->search(
array(
'hook' => 'woocommerce_plugins_install_callback',
'search' => $job_id,
'orderby' => 'date',
'order' => 'DESC',
)
);
return self::get_action_data( $actions );
}
/**
* Gets the plugin data for the first action.
*
* @param array $actions Array of AS actions.
*
* @return array Array of action data.
*/
public static function get_action_data( $actions ) {
$data = array();
foreach ( $actions as $action_id => $action ) {
$store = new ActionScheduler_DBStore();
$args = $action->get_args();
$data[] = array(
'job_id' => $args[1],
'plugins' => $args[0],
'status' => $store->get_status( $action_id ),
);
}
return $data;
}
/**
* Activation status.
*
* @param int $job_id Job ID.
*
* @return array Array of action data.
*/
public static function get_activation_status( $job_id = null ) {
$actions = WC()->queue()->search(
array(
'hook' => 'woocommerce_plugins_activate_callback',
'search' => $job_id,
'orderby' => 'date',
'order' => 'DESC',
)
);
return self::get_action_data( $actions );
}
}
PluginsInstallLoggers/AsyncPluginsInstallLogger.php 0000644 00000011725 15153746750 0016701 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\PluginsInstallLoggers;
/**
* A logger to log plugin installation progress in real time to an option.
*/
class AsyncPluginsInstallLogger implements PluginsInstallLogger {
/**
* Variable to store logs.
*
* @var string $option_name option name to store logs.
*/
private $option_name;
/**
* Constructor.
*
* @param string $option_name option name.
*/
public function __construct( string $option_name ) {
$this->option_name = $option_name;
add_option(
$this->option_name,
array(
'created_time' => time(),
'status' => 'pending',
'plugins' => array(),
),
'',
'no'
);
// Set status as failed in case we run out of exectuion time.
register_shutdown_function(
function () {
$error = error_get_last();
if ( isset( $error['type'] ) && E_ERROR === $error['type'] ) {
$option = $this->get();
$option['status'] = 'failed';
$this->update( $option );
}
}
);
}
/**
* Update the option.
*
* @param array $data New data.
*
* @return bool
*/
private function update( array $data ) {
return update_option( $this->option_name, $data );
}
/**
* Retreive the option.
*
* @return false|mixed|void
*/
private function get() {
return get_option( $this->option_name );
}
/**
* Add requested plugin.
*
* @param string $plugin_name plugin name.
*
* @return void
*/
public function install_requested( string $plugin_name ) {
$option = $this->get();
if ( ! isset( $option['plugins'][ $plugin_name ] ) ) {
$option['plugins'][ $plugin_name ] = array(
'status' => 'installing',
'errors' => array(),
'install_duration' => 0,
);
}
$this->update( $option );
}
/**
* Add installed plugin.
*
* @param string $plugin_name plugin name.
* @param int $duration time took to install plugin.
*
* @return void
*/
public function installed( string $plugin_name, int $duration ) {
$option = $this->get();
$option['plugins'][ $plugin_name ]['status'] = 'installed';
$option['plugins'][ $plugin_name ]['install_duration'] = $duration;
$this->update( $option );
}
/**
* Change status to activated.
*
* @param string $plugin_name plugin name.
*
* @return void
*/
public function activated( string $plugin_name ) {
$option = $this->get();
$option['plugins'][ $plugin_name ]['status'] = 'activated';
$this->update( $option );
}
/**
* Add an error.
*
* @param string $plugin_name plugin name.
* @param string|null $error_message error message.
*
* @return void
*/
public function add_error( string $plugin_name, string $error_message = null ) {
$option = $this->get();
$option['plugins'][ $plugin_name ]['errors'][] = $error_message;
$option['plugins'][ $plugin_name ]['status'] = 'failed';
$option['status'] = 'failed';
$this->update( $option );
}
/**
* Record completed_time.
*
* @param array $data return data from install_plugins().
* @return void
*/
public function complete( $data = array() ) {
$option = $this->get();
$option['complete_time'] = time();
$option['status'] = 'complete';
$this->track( $data );
$this->update( $option );
}
private function get_plugin_track_key( $id ) {
$slug = explode( ':', $id )[0];
$key = preg_match( '/^woocommerce(-|_)payments$/', $slug )
? 'wcpay'
: explode( ':', str_replace( '-', '_', $slug ) )[0];
return $key;
}
/**
* Returns time frame for a given time in milliseconds.
*
* @param int $timeInMs - time in milliseconds
*
* @return string - Time frame.
*/
function get_timeframe( $timeInMs ) {
$time_frames = [
[
'name' => '0-2s',
'max' => 2,
],
[
'name' => '2-5s',
'max' => 5,
],
[
'name' => '5-10s',
'max' => 10,
],
[
'name' => '10-15s',
'max' => 15,
],
[
'name' => '15-20s',
'max' => 20,
],
[
'name' => '20-30s',
'max' => 30,
],
[
'name' => '30-60s',
'max' => 60,
],
[ 'name' => '>60s' ],
];
foreach ( $time_frames as $time_frame ) {
if ( ! isset( $time_frame['max'] ) ) {
return $time_frame['name'];
}
if ( $timeInMs < $time_frame['max'] * 1000 ) {
return $time_frame['name'];
}
}
}
private function track( $data ) {
$track_data = array(
'success' => true,
'installed_extensions' => array_map(
function( $extension ) {
return $this->get_plugin_track_key( $extension );
},
$data['installed']
),
'total_time' => $this->get_timeframe( ( time() - $data['start_time'] ) * 1000 ),
);
foreach ( $data['installed'] as $plugin ) {
if ( ! isset( $data['time'][ $plugin ] ) ) {
continue;
}
$track_data[ 'install_time_' . $this->get_plugin_track_key( $plugin ) ] = $this->get_timeframe( $data['time'][ $plugin ] );
}
wc_admin_record_tracks_event( 'coreprofiler_store_extensions_installed_and_activated', $track_data );
}
}
PluginsInstallLoggers/PluginsInstallLogger.php 0000644 00000002376 15153746750 0015705 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\PluginsInstallLoggers;
/**
* A logger used in PluginsHelper::install_plugins to log the installation progress.
*/
interface PluginsInstallLogger {
/**
* Called when a plugin install requested.
*
* @param string $plugin_name plugin name.
* @return mixed
*/
public function install_requested( string $plugin_name );
/**
* Called when a plugin installed successfully.
*
* @param string $plugin_name plugin name.
* @param int $duration # of seconds it took to install $plugin_name.
* @return mixed
*/
public function installed( string $plugin_name, int $duration);
/**
* Called when a plugin activated successfully.
*
* @param string $plugin_name plugin name.
* @return mixed
*/
public function activated( string $plugin_name );
/**
* Called when an error occurred while installing a plugin.
*
* @param string $plugin_name plugin name.
* @param string|null $error_message error message.
* @return mixed
*/
public function add_error( string $plugin_name, string $error_message = null);
/**
* Called when all plugins are processed.
*
* @param array $data return data from install_plugins().
* @return mixed
*/
public function complete( $data = array() );
}
PluginsInstaller.php 0000644 00000006564 15153746750 0010604 0 ustar 00 <?php
/**
* PluginsInstaller
*
* Installer to allow plugin installation via URL query.
*/
namespace Automattic\WooCommerce\Admin;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Plugins;
use Automattic\WooCommerce\Admin\Features\TransientNotices;
/**
* Class PluginsInstaller
*/
class PluginsInstaller {
/**
* Constructor
*/
public static function init() {
add_action( 'admin_init', array( __CLASS__, 'possibly_install_activate_plugins' ) );
}
/**
* Check if an install or activation is being requested via URL query.
*/
public static function possibly_install_activate_plugins() {
/* phpcs:disable WordPress.Security.NonceVerification.Recommended */
if (
! isset( $_GET['plugin_action'] ) ||
! isset( $_GET['plugins'] ) ||
! current_user_can( 'install_plugins' ) ||
! isset( $_GET['nonce'] )
) {
return;
}
$nonce = sanitize_text_field( wp_unslash( $_GET['nonce'] ) );
if ( ! wp_verify_nonce( $nonce, 'install-plugin' ) ) {
wp_nonce_ays( 'install-plugin' );
}
$plugins = sanitize_text_field( wp_unslash( $_GET['plugins'] ) );
$plugin_action = sanitize_text_field( wp_unslash( $_GET['plugin_action'] ) );
/* phpcs:enable WordPress.Security.NonceVerification.Recommended */
$plugins_api = new Plugins();
$install_result = null;
$activate_result = null;
switch ( $plugin_action ) {
case 'install':
$install_result = $plugins_api->install_plugins( array( 'plugins' => $plugins ) );
break;
case 'activate':
$activate_result = $plugins_api->activate_plugins( array( 'plugins' => $plugins ) );
break;
case 'install-activate':
$install_result = $plugins_api->install_plugins( array( 'plugins' => $plugins ) );
$activate_result = $plugins_api->activate_plugins( array( 'plugins' => implode( ',', $install_result['data']['installed'] ) ) );
break;
}
self::cache_results( $plugins, $install_result, $activate_result );
self::redirect_to_referer();
}
/**
* Display the results of installation and activation on the page.
*
* @param string $plugins Comma separated list of plugins.
* @param array $install_result Result of installation.
* @param array $activate_result Result of activation.
*/
public static function cache_results( $plugins, $install_result, $activate_result ) {
if ( ! $install_result && ! $activate_result ) {
return;
}
if ( is_wp_error( $install_result ) || is_wp_error( $activate_result ) ) {
$message = $activate_result ? $activate_result->get_error_message() : $install_result->get_error_message();
} else {
$message = $activate_result ? $activate_result['message'] : $install_result['message'];
}
TransientNotices::add(
array(
'user_id' => get_current_user_id(),
'id' => 'plugin-installer-' . str_replace( ',', '-', $plugins ),
'status' => 'success',
'content' => $message,
)
);
}
/**
* Redirect back to the referring page if one exists.
*/
public static function redirect_to_referer() {
$referer = wp_get_referer();
if ( $referer && 0 !== strpos( $referer, wp_login_url() ) ) {
wp_safe_redirect( $referer );
exit();
}
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
return;
}
$url = remove_query_arg( 'plugin_action', wp_unslash( $_SERVER['REQUEST_URI'] ) ); // phpcs:ignore sanitization ok.
$url = remove_query_arg( 'plugins', $url );
wp_safe_redirect( $url );
exit();
}
}
PluginsProvider/PluginsProvider.php 0000644 00000003510 15153746750 0013561 0 ustar 00 <?php
/**
* A provider for getting access to plugin queries.
*/
namespace Automattic\WooCommerce\Admin\PluginsProvider;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\PluginsProvider\PluginsProviderInterface;
use Automattic\WooCommerce\Admin\PluginsHelper;
/**
* Plugins Provider.
*
* Uses the live PluginsHelper.
*/
class PluginsProvider implements PluginsProviderInterface {
/**
* The deactivated plugin slug.
*
* @var string
*/
private static $deactivated_plugin_slug = '';
/**
* Get an array of active plugin slugs.
*
* @return array
*/
public function get_active_plugin_slugs() {
return array_filter(
PluginsHelper::get_active_plugin_slugs(),
function( $p ) {
return $p !== self::$deactivated_plugin_slug;
}
);
}
/**
* Set the deactivated plugin. This is needed because the deactivated_plugin
* hook happens before the option is updated which means that getting the
* active plugins includes the deactivated plugin.
*
* @param string $plugin_path The path to the plugin being deactivated.
*/
public static function set_deactivated_plugin( $plugin_path ) {
self::$deactivated_plugin_slug = explode( '/', $plugin_path )[0];
}
/**
* Get plugin data.
*
* @param string $plugin Path to the plugin file relative to the plugins directory or the plugin directory name.
*
* @return array|false
*/
public function get_plugin_data( $plugin ) {
return PluginsHelper::get_plugin_data( $plugin );
}
/**
* Get the path to the plugin file relative to the plugins directory from the plugin slug.
*
* E.g. 'woocommerce' returns 'woocommerce/woocommerce.php'
*
* @param string $slug Plugin slug to get path for.
*
* @return string|false
*/
public function get_plugin_path_from_slug( $slug ) {
return PluginsHelper::get_plugin_path_from_slug( $slug );
}
}
PluginsProvider/PluginsProviderInterface.php 0000644 00000001650 15153746750 0015405 0 ustar 00 <?php
/**
* Interface for a provider for getting access to plugin queries,
* designed to be mockable for unit tests.
*/
namespace Automattic\WooCommerce\Admin\PluginsProvider;
defined( 'ABSPATH' ) || exit;
/**
* Plugins Provider Interface
*/
interface PluginsProviderInterface {
/**
* Get an array of active plugin slugs.
*
* @return array
*/
public function get_active_plugin_slugs();
/**
* Get plugin data.
*
* @param string $plugin Path to the plugin file relative to the plugins directory or the plugin directory name.
*
* @return array|false
*/
public function get_plugin_data( $plugin );
/**
* Get the path to the plugin file relative to the plugins directory from the plugin slug.
*
* E.g. 'woocommerce' returns 'woocommerce/woocommerce.php'
*
* @param string $slug Plugin slug to get path for.
*
* @return string|false
*/
public function get_plugin_path_from_slug( $slug );
}
RemoteInboxNotifications/BaseLocationCountryRuleProcessor.php 0000644 00000003560 15153746750 0020742 0 ustar 00 <?php
/**
* Rule processor that performs a comparison operation against the base
* location - country.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor that performs a comparison operation against the base
* location - country.
*/
class BaseLocationCountryRuleProcessor implements RuleProcessorInterface {
/**
* Performs a comparison operation against the base location - country.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
$base_location = wc_get_base_location();
if ( ! $base_location ) {
return false;
}
$onboarding_profile = get_option( 'woocommerce_onboarding_profile', array() );
$is_address_default = 'US' === $base_location['country'] && 'CA' === $base_location['state'] && empty( get_option( 'woocommerce_store_address', '' ) );
$is_store_country_set = isset( $onboarding_profile['is_store_country_set'] ) && $onboarding_profile['is_store_country_set'];
// Return false if the location is the default country and if onboarding hasn't been finished or the store address not been updated.
if ( $is_address_default && OnboardingProfile::needs_completion() && ! $is_store_country_set ) {
return false;
}
return ComparisonOperation::compare(
$base_location['country'],
$rule->value,
$rule->operation
);
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->value ) ) {
return false;
}
if ( ! isset( $rule->operation ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/BaseLocationStateRuleProcessor.php 0000644 00000002256 15153746750 0020360 0 ustar 00 <?php
/**
* Rule processor that performs a comparison operation against the base
* location - state.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor that performs a comparison operation against the base
* location - state.
*/
class BaseLocationStateRuleProcessor implements RuleProcessorInterface {
/**
* Performs a comparison operation against the base location - state.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
$base_location = wc_get_base_location();
if ( ! $base_location ) {
return false;
}
return ComparisonOperation::compare(
$base_location['state'],
$rule->value,
$rule->operation
);
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->value ) ) {
return false;
}
if ( ! isset( $rule->operation ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/ComparisonOperation.php 0000644 00000003510 15153746750 0016251 0 ustar 00 <?php
/**
* Compare two operands using the specified operation.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Compare two operands using the specified operation.
*/
class ComparisonOperation {
/**
* Compare two operands using the specified operation.
*
* @param object $left_operand The left hand operand.
* @param object $right_operand The right hand operand.
* @param string $operation The operation used to compare the operands.
*/
public static function compare( $left_operand, $right_operand, $operation ) {
switch ( $operation ) {
case '=':
return $left_operand === $right_operand;
case '<':
return $left_operand < $right_operand;
case '<=':
return $left_operand <= $right_operand;
case '>':
return $left_operand > $right_operand;
case '>=':
return $left_operand >= $right_operand;
case '!=':
return $left_operand !== $right_operand;
case 'contains':
if ( is_array( $left_operand ) && is_string( $right_operand ) ) {
return in_array( $right_operand, $left_operand, true );
}
return strpos( $right_operand, $left_operand ) !== false;
case '!contains':
if ( is_array( $left_operand ) && is_string( $right_operand ) ) {
return ! in_array( $right_operand, $left_operand, true );
}
return strpos( $right_operand, $left_operand ) === false;
case 'in':
if ( is_array( $right_operand ) && is_string( $left_operand ) ) {
return in_array( $left_operand, $right_operand, true );
}
return strpos( $left_operand, $right_operand ) !== false;
case '!in':
if ( is_array( $right_operand ) && is_string( $left_operand ) ) {
return ! in_array( $left_operand, $right_operand, true );
}
return strpos( $left_operand, $right_operand ) === false;
}
return false;
}
}
RemoteInboxNotifications/DataSourcePoller.php 0000644 00000012316 15153746750 0015472 0 ustar 00 <?php
/**
* Handles polling and storage of specs
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Specs data source poller class.
* This handles polling specs from JSON endpoints, and
* stores the specs in to the database as an option.
*/
class DataSourcePoller extends \Automattic\WooCommerce\Admin\DataSourcePoller {
const ID = 'remote_inbox_notifications';
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/inbox-notifications/1.0/notifications.json',
);
/**
* Class instance.
*
* @var Analytics instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self(
self::ID,
self::DATA_SOURCES,
array(
'spec_key' => 'slug',
)
);
}
return self::$instance;
}
/**
* Validate the spec.
*
* @param object $spec The spec to validate.
* @param string $url The url of the feed that provided the spec.
*
* @return bool The result of the validation.
*/
protected function validate_spec( $spec, $url ) {
$logger = self::get_logger();
$logger_context = array( 'source' => $url );
if ( ! isset( $spec->slug ) ) {
$logger->error(
'Spec is invalid because the slug is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
if ( ! isset( $spec->status ) ) {
$logger->error(
'Spec is invalid because the status is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
if ( ! isset( $spec->locales ) || ! is_array( $spec->locales ) ) {
$logger->error(
'Spec is invalid because the status is missing or empty in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
if ( null === SpecRunner::get_locale( $spec->locales ) ) {
$logger->error(
'Spec is invalid because the locale could not be retrieved in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
if ( ! isset( $spec->type ) ) {
$logger->error(
'Spec is invalid because the type is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
if ( isset( $spec->actions ) && is_array( $spec->actions ) ) {
foreach ( $spec->actions as $action ) {
if ( ! $this->validate_action( $action, $url ) ) {
$logger->error(
'Spec is invalid because an action is invalid in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
}
}
if ( isset( $spec->rules ) && is_array( $spec->rules ) ) {
foreach ( $spec->rules as $rule ) {
if ( ! isset( $rule->type ) ) {
$logger->error(
'Spec is invalid because a rule type is empty in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $rule, true ), $logger_context );
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
$processor = GetRuleProcessor::get_processor( $rule->type );
if ( ! $processor->validate( $rule ) ) {
$logger->error(
'Spec is invalid because a rule is invalid in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $rule, true ), $logger_context );
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
}
}
return true;
}
/**
* Validate the action.
*
* @param object $action The action to validate.
* @param string $url The url of the feed containing the action (for error reporting).
*
* @return bool The result of the validation.
*/
private function validate_action( $action, $url ) {
$logger = self::get_logger();
$logger_context = array( 'source' => $url );
if ( ! isset( $action->locales ) || ! is_array( $action->locales ) ) {
$logger->error(
'Action is invalid because it has empty or missing locales in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $action, true ), $logger_context );
return false;
}
if ( null === SpecRunner::get_action_locale( $action->locales ) ) {
$logger->error(
'Action is invalid because the locale could not be retrieved in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $action, true ), $logger_context );
return false;
}
if ( ! isset( $action->name ) ) {
$logger->error(
'Action is invalid because the name is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $action, true ), $logger_context );
return false;
}
if ( ! isset( $action->status ) ) {
$logger->error(
'Action is invalid because the status is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $action, true ), $logger_context );
return false;
}
return true;
}
}
RemoteInboxNotifications/EvaluateAndGetStatus.php 0000644 00000003026 15153746750 0016315 0 ustar 00 <?php
/**
* Evaluates the spec and returns a status.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
/**
* Evaluates the spec and returns a status.
*/
class EvaluateAndGetStatus {
/**
* Evaluates the spec and returns a status.
*
* @param array $spec The spec to evaluate.
* @param string $current_status The note's current status.
* @param object $stored_state Stored state.
* @param object $rule_evaluator Evaluates rules into true/false.
*
* @return string The evaluated status.
*/
public static function evaluate( $spec, $current_status, $stored_state, $rule_evaluator ) {
// No rules should leave the note alone.
if ( ! isset( $spec->rules ) ) {
return $current_status;
}
$evaluated_result = $rule_evaluator->evaluate(
$spec->rules,
$stored_state,
array(
'slug' => $spec->slug,
'source' => 'remote-inbox-notifications',
)
);
// Pending notes should be the spec status if the spec passes,
// left alone otherwise.
if ( Note::E_WC_ADMIN_NOTE_PENDING === $current_status ) {
return $evaluated_result
? $spec->status
: Note::E_WC_ADMIN_NOTE_PENDING;
}
// When allow_redisplay isn't set, just leave the note alone.
if ( ! isset( $spec->allow_redisplay ) || ! $spec->allow_redisplay ) {
return $current_status;
}
// allow_redisplay is set, unaction the note if eval to true.
return $evaluated_result
? Note::E_WC_ADMIN_NOTE_UNACTIONED
: $current_status;
}
}
RemoteInboxNotifications/EvaluationLogger.php 0000644 00000003360 15153746750 0015530 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
/**
* Class EvaluationLogger
*
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications
*/
class EvaluationLogger {
/**
* Slug of the spec.
*
* @var string
*/
private $slug;
/**
* Results of rules in the given spec.
*
* @var array
*/
private $results = array();
/**
* Logger class to use.
*
* @var WC_Logger_Interface|null
*/
private $logger;
/**
* Logger source.
*
* @var string logger source.
*/
private $source = '';
/**
* EvaluationLogger constructor.
*
* @param string $slug Slug of a spec that is being evaluated.
* @param null $source Logger source.
* @param \WC_Logger_Interface $logger Logger class to use.
*/
public function __construct( $slug, $source = null, \WC_Logger_Interface $logger = null ) {
$this->slug = $slug;
if ( null === $logger ) {
$logger = wc_get_logger();
}
if ( $source ) {
$this->source = $source;
}
$this->logger = $logger;
}
/**
* Add evaluation result of a rule.
*
* @param string $rule_type name of the rule being tested.
* @param boolean $result result of a given rule.
*/
public function add_result( $rule_type, $result ) {
array_push(
$this->results,
array(
'rule' => $rule_type,
'result' => $result ? 'passed' : 'failed',
)
);
}
/**
* Log the results.
*/
public function log() {
if ( false === defined( 'WC_ADMIN_DEBUG_RULE_EVALUATOR' ) || true !== constant( 'WC_ADMIN_DEBUG_RULE_EVALUATOR' ) ) {
return;
}
foreach ( $this->results as $result ) {
$this->logger->debug(
"[{$this->slug}] {$result['rule']}: {$result['result']}",
array( 'source' => $this->source )
);
}
}
}
RemoteInboxNotifications/FailRuleProcessor.php 0000644 00000001261 15153746750 0015662 0 ustar 00 <?php
/**
* Rule processor that fails.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor that fails.
*/
class FailRuleProcessor implements RuleProcessorInterface {
/**
* Fails the rule.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool Always false.
*/
public function process( $rule, $stored_state ) {
return false;
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
return true;
}
}
RemoteInboxNotifications/GetRuleProcessor.php 0000644 00000003745 15153746750 0015537 0 ustar 00 <?php
/**
* Gets the processor for the specified rule type.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Class encapsulating getting the processor for a given rule type.
*/
class GetRuleProcessor {
/**
* Get the processor for the specified rule type.
*
* @param string $rule_type The rule type.
*
* @return RuleProcessorInterface The matching processor for the specified rule type, or a FailRuleProcessor if no matching processor is found.
*/
public static function get_processor( $rule_type ) {
switch ( $rule_type ) {
case 'plugins_activated':
return new PluginsActivatedRuleProcessor();
case 'publish_after_time':
return new PublishAfterTimeRuleProcessor();
case 'publish_before_time':
return new PublishBeforeTimeRuleProcessor();
case 'not':
return new NotRuleProcessor();
case 'or':
return new OrRuleProcessor();
case 'fail':
return new FailRuleProcessor();
case 'pass':
return new PassRuleProcessor();
case 'plugin_version':
return new PluginVersionRuleProcessor();
case 'stored_state':
return new StoredStateRuleProcessor();
case 'order_count':
return new OrderCountRuleProcessor();
case 'wcadmin_active_for':
return new WCAdminActiveForRuleProcessor();
case 'product_count':
return new ProductCountRuleProcessor();
case 'onboarding_profile':
return new OnboardingProfileRuleProcessor();
case 'is_ecommerce':
return new IsEcommerceRuleProcessor();
case 'base_location_country':
return new BaseLocationCountryRuleProcessor();
case 'base_location_state':
return new BaseLocationStateRuleProcessor();
case 'note_status':
return new NoteStatusRuleProcessor();
case 'option':
return new OptionRuleProcessor();
case 'wca_updated':
return new WooCommerceAdminUpdatedRuleProcessor();
case 'total_payments_value':
return new TotalPaymentsVolumeProcessor();
}
return new FailRuleProcessor();
}
}
RemoteInboxNotifications/IsEcommerceRuleProcessor.php 0000644 00000002161 15153746750 0017202 0 ustar 00 <?php
/**
* Rule processor that passes (or fails) when the site is on the eCommerce
* plan.
*
* @package WooCommerce\Admin\Classes
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor that passes (or fails) when the site is on the eCommerce
* plan.
*/
class IsEcommerceRuleProcessor implements RuleProcessorInterface {
/**
* Passes (or fails) based on whether the site is on the eCommerce plan or
* not.
*
* @param object $rule The rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
if ( ! function_exists( 'wc_calypso_bridge_is_ecommerce_plan' ) ) {
return false === $rule->value;
}
return (bool) wc_calypso_bridge_is_ecommerce_plan() === $rule->value;
}
/**
* Validate the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->value ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/NotRuleProcessor.php 0000644 00000002476 15153746750 0015560 0 ustar 00 <?php
/**
* Rule processor that negates the rules in the rule's operand.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor that negates the rules in the rule's operand.
*/
class NotRuleProcessor implements RuleProcessorInterface {
/**
* The rule evaluator to use.
*
* @var RuleEvaluator
*/
protected $rule_evaluator;
/**
* Constructor.
*
* @param RuleEvaluator $rule_evaluator The rule evaluator to use.
*/
public function __construct( $rule_evaluator = null ) {
$this->rule_evaluator = null === $rule_evaluator
? new RuleEvaluator()
: $rule_evaluator;
}
/**
* Evaluates the rules in the operand and negates the result.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
$evaluated_operand = $this->rule_evaluator->evaluate(
$rule->operand,
$stored_state
);
return ! $evaluated_operand;
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->operand ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/NoteStatusRuleProcessor.php 0000644 00000002452 15153746750 0017123 0 ustar 00 <?php
/**
* Rule processor that compares against the status of another note. For
* example, this could be used to conditionally create a note only if another
* note has not been actioned.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Notes;
/**
* Rule processor that compares against the status of another note.
*/
class NoteStatusRuleProcessor implements RuleProcessorInterface {
/**
* Compare against the status of another note.
*
* @param object $rule The rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
$status = Notes::get_note_status( $rule->note_name );
if ( ! $status ) {
return false;
}
return ComparisonOperation::compare(
$status,
$rule->status,
$rule->operation
);
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->note_name ) ) {
return false;
}
if ( ! isset( $rule->status ) ) {
return false;
}
if ( ! isset( $rule->operation ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/OnboardingProfileRuleProcessor.php 0000644 00000002600 15153746750 0020410 0 ustar 00 <?php
/**
* Rule processor that performs a comparison operation against a value in the
* onboarding profile.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor that performs a comparison operation against a value in the
* onboarding profile.
*/
class OnboardingProfileRuleProcessor implements RuleProcessorInterface {
/**
* Performs a comparison operation against a value in the onboarding
* profile.
*
* @param object $rule The rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
$onboarding_profile = get_option( 'woocommerce_onboarding_profile' );
if ( empty( $onboarding_profile ) ) {
return false;
}
if ( ! isset( $onboarding_profile[ $rule->index ] ) ) {
return false;
}
return ComparisonOperation::compare(
$onboarding_profile[ $rule->index ],
$rule->value,
$rule->operation
);
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->index ) ) {
return false;
}
if ( ! isset( $rule->value ) ) {
return false;
}
if ( ! isset( $rule->operation ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/OptionRuleProcessor.php 0000644 00000005523 15153746750 0016264 0 ustar 00 <?php
/**
* Rule processor that performs a comparison operation against an option value.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor that performs a comparison operation against an option value.
*/
class OptionRuleProcessor implements RuleProcessorInterface {
/**
* Performs a comparison operation against the option value.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
$is_contains = $rule->operation && strpos( $rule->operation, 'contains' ) !== false;
$default_value = $is_contains ? array() : false;
$default = isset( $rule->default ) ? $rule->default : $default_value;
$option_value = $this->get_option_value( $rule, $default, $is_contains );
if ( isset( $rule->transformers ) && is_array( $rule->transformers ) ) {
$option_value = TransformerService::apply( $option_value, $rule->transformers, $default );
}
return ComparisonOperation::compare(
$option_value,
$rule->value,
$rule->operation
);
}
/**
* Retrieves the option value and handles logging if necessary.
*
* @param object $rule The specific rule being processed.
* @param mixed $default The default value.
* @param bool $is_contains Indicates whether the operation is "contains".
*
* @return mixed The option value.
*/
private function get_option_value( $rule, $default, $is_contains ) {
$option_value = get_option( $rule->option_name, $default );
$is_contains_valid = $is_contains && ( is_array( $option_value ) || ( is_string( $option_value ) && is_string( $rule->value ) ) );
if ( $is_contains && ! $is_contains_valid ) {
$logger = wc_get_logger();
$logger->warning(
sprintf(
'ComparisonOperation "%s" option value "%s" is not an array, defaulting to empty array.',
$rule->operation,
$rule->option_name
),
array(
'option_value' => $option_value,
'rule' => $rule,
)
);
$option_value = array();
}
return $option_value;
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->option_name ) ) {
return false;
}
if ( ! isset( $rule->value ) ) {
return false;
}
if ( ! isset( $rule->operation ) ) {
return false;
}
if ( isset( $rule->transformers ) && is_array( $rule->transformers ) ) {
foreach ( $rule->transformers as $transform_args ) {
$transformer = TransformerService::create_transformer( $transform_args->use );
if ( ! $transformer->validate( $transform_args->arguments ) ) {
return false;
}
}
}
return true;
}
}
RemoteInboxNotifications/OrRuleProcessor.php 0000644 00000002750 15153746750 0015373 0 ustar 00 <?php
/**
* Rule processor that performs an OR operation on the rule's left and right
* operands.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor that performs an OR operation on the rule's left and right
* operands.
*/
class OrRuleProcessor implements RuleProcessorInterface {
/**
* Rule evaluator to use.
*
* @var RuleEvaluator
*/
private $rule_evaluator;
/**
* Constructor.
*
* @param RuleEvaluator $rule_evaluator The rule evaluator to use.
*/
public function __construct( $rule_evaluator = null ) {
$this->rule_evaluator = null === $rule_evaluator
? new RuleEvaluator()
: $rule_evaluator;
}
/**
* Performs an OR operation on the rule's left and right operands.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
foreach ( $rule->operands as $operand ) {
$evaluated_operand = $this->rule_evaluator->evaluate(
$operand,
$stored_state
);
if ( $evaluated_operand ) {
return true;
}
}
return false;
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->operands ) || ! is_array( $rule->operands ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/OrderCountRuleProcessor.php 0000644 00000002474 15153746750 0017102 0 ustar 00 <?php
/**
* Rule processor for publishing based on the number of orders.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor for publishing based on the number of orders.
*/
class OrderCountRuleProcessor implements RuleProcessorInterface {
/**
* The orders provider.
*
* @var OrdersProvider
*/
protected $orders_provider;
/**
* Constructor.
*
* @param object $orders_provider The orders provider.
*/
public function __construct( $orders_provider = null ) {
$this->orders_provider = null === $orders_provider
? new OrdersProvider()
: $orders_provider;
}
/**
* Process the rule.
*
* @param object $rule The rule to process.
* @param object $stored_state Stored state.
*
* @return bool Whether the rule passes or not.
*/
public function process( $rule, $stored_state ) {
$count = $this->orders_provider->get_order_count();
return ComparisonOperation::compare(
$count,
$rule->value,
$rule->operation
);
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->value ) ) {
return false;
}
if ( ! isset( $rule->operation ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/OrdersProvider.php 0000644 00000001277 15153746750 0015237 0 ustar 00 <?php
/**
* Provider for order-related queries and operations.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Provider for order-related queries and operations.
*/
class OrdersProvider {
/**
* Allowed order statuses for calculating milestones.
*
* @var array
*/
protected $allowed_statuses = array(
'pending',
'processing',
'completed',
);
/**
* Returns the number of orders.
*
* @return integer The number of orders.
*/
public function get_order_count() {
$status_counts = array_map( 'wc_orders_count', $this->allowed_statuses );
$orders_count = array_sum( $status_counts );
return $orders_count;
}
}
RemoteInboxNotifications/PassRuleProcessor.php 0000644 00000001407 15153746750 0015717 0 ustar 00 <?php
/**
* Rule processor that passes. This is required because an empty set of rules
* (or predicate) evaluates to false.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor that passes.
*/
class PassRuleProcessor implements RuleProcessorInterface {
/**
* Passes the rule.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool Always true.
*/
public function process( $rule, $stored_state ) {
return true;
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
return true;
}
}
RemoteInboxNotifications/PluginVersionRuleProcessor.php 0000644 00000003554 15153746750 0017622 0 ustar 00 <?php
/**
* Rule processor for sending when the provided plugin is activated and
* matches the specified version.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\PluginsProvider\PluginsProvider;
/**
* Rule processor for sending when the provided plugin is activated and
* matches the specified version.
*/
class PluginVersionRuleProcessor implements RuleProcessorInterface {
/**
* Plugins provider instance.
*
* @var PluginsProviderInterface
*/
private $plugins_provider;
/**
* Constructor.
*
* @param PluginsProviderInterface $plugins_provider The plugins provider.
*/
public function __construct( $plugins_provider = null ) {
$this->plugins_provider = null === $plugins_provider
? new PluginsProvider()
: $plugins_provider;
}
/**
* Process the rule.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool Whether the rule passes or not.
*/
public function process( $rule, $stored_state ) {
$active_plugin_slugs = $this->plugins_provider->get_active_plugin_slugs();
if ( ! in_array( $rule->plugin, $active_plugin_slugs, true ) ) {
return false;
}
$plugin_data = $this->plugins_provider->get_plugin_data( $rule->plugin );
if ( ! $plugin_data ) {
return false;
}
$plugin_version = $plugin_data['Version'];
return version_compare( $plugin_version, $rule->version, $rule->operator );
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->plugin ) ) {
return false;
}
if ( ! isset( $rule->version ) ) {
return false;
}
if ( ! isset( $rule->operator ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/PluginsActivatedRuleProcessor.php 0000644 00000003131 15153746750 0020253 0 ustar 00 <?php
/**
* Rule processor for sending when the provided plugins are activated.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\PluginsProvider\PluginsProvider;
/**
* Rule processor for sending when the provided plugins are activated.
*/
class PluginsActivatedRuleProcessor implements RuleProcessorInterface {
/**
* The plugins provider.
*
* @var PluginsProviderInterface
*/
protected $plugins_provider;
/**
* Constructor.
*
* @param PluginsProviderInterface $plugins_provider The plugins provider.
*/
public function __construct( $plugins_provider = null ) {
$this->plugins_provider = null === $plugins_provider
? new PluginsProvider()
: $plugins_provider;
}
/**
* Process the rule.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool Whether the rule passes or not.
*/
public function process( $rule, $stored_state ) {
if ( 0 === count( $rule->plugins ) ) {
return false;
}
$active_plugin_slugs = $this->plugins_provider->get_active_plugin_slugs();
foreach ( $rule->plugins as $plugin_slug ) {
if ( ! in_array( $plugin_slug, $active_plugin_slugs, true ) ) {
return false;
}
}
return true;
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->plugins ) || ! is_array( $rule->plugins ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/ProductCountRuleProcessor.php 0000644 00000003104 15153746750 0017436 0 ustar 00 <?php
/**
* Rule processor that performs a comparison operation against the number of
* products.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor that performs a comparison operation against the number of
* products.
*/
class ProductCountRuleProcessor implements RuleProcessorInterface {
/**
* The product query.
*
* @var WC_Product_Query
*/
protected $product_query;
/**
* Constructor.
*
* @param object $product_query The product query.
*/
public function __construct( $product_query = null ) {
$this->product_query = null === $product_query
? new \WC_Product_Query(
array(
'limit' => 1,
'paginate' => true,
'return' => 'ids',
'status' => array( 'publish' ),
)
)
: $product_query;
}
/**
* Performs a comparison operation against the number of products.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
$products = $this->product_query->get_products();
return ComparisonOperation::compare(
$products->total,
$rule->value,
$rule->operation
);
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->value ) ) {
return false;
}
if ( ! isset( $rule->operation ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/PublishAfterTimeRuleProcessor.php 0000644 00000002566 15153746750 0020227 0 ustar 00 <?php
/**
* Rule processor for sending after a specified date/time.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\DateTimeProvider\CurrentDateTimeProvider;
/**
* Rule processor for sending after a specified date/time.
*/
class PublishAfterTimeRuleProcessor implements RuleProcessorInterface {
/**
* The DateTime provider.
*
* @var DateTimeProviderInterface
*/
protected $date_time_provider;
/**
* Constructor.
*
* @param DateTimeProviderInterface $date_time_provider The DateTime provider.
*/
public function __construct( $date_time_provider = null ) {
$this->date_time_provider = null === $date_time_provider
? new CurrentDateTimeProvider()
: $date_time_provider;
}
/**
* Process the rule.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool Whether the rule passes or not.
*/
public function process( $rule, $stored_state ) {
return $this->date_time_provider->get_now() >= new \DateTime( $rule->publish_after );
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->publish_after ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/PublishBeforeTimeRuleProcessor.php 0000644 00000002573 15153746750 0020366 0 ustar 00 <?php
/**
* Rule processor for sending before a specified date/time.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\DateTimeProvider\CurrentDateTimeProvider;
/**
* Rule processor for sending before a specified date/time.
*/
class PublishBeforeTimeRuleProcessor implements RuleProcessorInterface {
/**
* The DateTime provider.
*
* @var DateTimeProviderInterface
*/
protected $date_time_provider;
/**
* Constructor.
*
* @param DateTimeProviderInterface $date_time_provider The DateTime provider.
*/
public function __construct( $date_time_provider = null ) {
$this->date_time_provider = null === $date_time_provider
? new CurrentDateTimeProvider()
: $date_time_provider;
}
/**
* Process the rule.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool Whether the rule passes or not.
*/
public function process( $rule, $stored_state ) {
return $this->date_time_provider->get_now() <= new \DateTime( $rule->publish_before );
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->publish_before ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/RemoteInboxNotificationsEngine.php 0000644 00000014770 15153746750 0020403 0 ustar 00 <?php
/**
* Handles running specs
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\PluginsProvider\PluginsProvider;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use Automattic\WooCommerce\Admin\Notes\Note;
/**
* Remote Inbox Notifications engine.
* This goes through the specs and runs (creates admin notes) for those
* specs that are able to be triggered.
*/
class RemoteInboxNotificationsEngine {
const STORED_STATE_OPTION_NAME = 'wc_remote_inbox_notifications_stored_state';
const WCA_UPDATED_OPTION_NAME = 'wc_remote_inbox_notifications_wca_updated';
/**
* Initialize the engine.
*/
public static function init() {
// Init things that need to happen before admin_init.
add_action( 'init', array( __CLASS__, 'on_init' ), 0, 0 );
// Continue init via admin_init.
add_action( 'admin_init', array( __CLASS__, 'on_admin_init' ) );
// Trigger when the profile data option is updated (during onboarding).
add_action(
'update_option_' . OnboardingProfile::DATA_OPTION,
array( __CLASS__, 'update_profile_option' ),
10,
2
);
// Hook into WCA updated. This is hooked up here rather than in
// on_admin_init because that runs too late to hook into the action.
add_action( 'woocommerce_run_on_woocommerce_admin_updated', array( __CLASS__, 'run_on_woocommerce_admin_updated' ) );
add_action(
'woocommerce_updated',
function() {
$next_hook = WC()->queue()->get_next(
'woocommerce_run_on_woocommerce_admin_updated',
array(),
'woocommerce-remote-inbox-engine'
);
if ( null === $next_hook ) {
WC()->queue()->schedule_single(
time(),
'woocommerce_run_on_woocommerce_admin_updated',
array(),
'woocommerce-remote-inbox-engine'
);
}
}
);
add_filter( 'woocommerce_get_note_from_db', array( __CLASS__, 'get_note_from_db' ), 10, 1 );
}
/**
* This is triggered when the profile option is updated and if the
* profiler is being completed, triggers a run of the engine.
*
* @param mixed $old_value Old value.
* @param mixed $new_value New value.
*/
public static function update_profile_option( $old_value, $new_value ) {
// Return early if we're not completing the profiler.
if (
( isset( $old_value['completed'] ) && $old_value['completed'] ) ||
! isset( $new_value['completed'] ) ||
! $new_value['completed']
) {
return;
}
self::run();
}
/**
* Init is continued via admin_init so that WC is loaded when the product
* query is used, otherwise the query generates a "0 = 1" in the WHERE
* condition and thus doesn't return any results.
*/
public static function on_admin_init() {
add_action( 'activated_plugin', array( __CLASS__, 'run' ) );
add_action( 'deactivated_plugin', array( __CLASS__, 'run_on_deactivated_plugin' ), 10, 1 );
StoredStateSetupForProducts::admin_init();
// Pre-fetch stored state so it has the correct initial values.
self::get_stored_state();
}
/**
* An init hook is used here so that StoredStateSetupForProducts can set
* up a hook that gets triggered by action-scheduler - this is needed
* because the admin_init hook doesn't get triggered by WP Cron.
*/
public static function on_init() {
StoredStateSetupForProducts::init();
}
/**
* Go through the specs and run them.
*/
public static function run() {
$specs = DataSourcePoller::get_instance()->get_specs_from_data_sources();
if ( $specs === false || count( $specs ) === 0 ) {
return;
}
$stored_state = self::get_stored_state();
foreach ( $specs as $spec ) {
SpecRunner::run_spec( $spec, $stored_state );
}
}
/**
* Set an option indicating that WooCommerce Admin has just been updated,
* run the specs, then clear that option. This lets the
* WooCommerceAdminUpdatedRuleProcessor trigger on WCA update.
*/
public static function run_on_woocommerce_admin_updated() {
update_option( self::WCA_UPDATED_OPTION_NAME, true, false );
self::run();
update_option( self::WCA_UPDATED_OPTION_NAME, false, false );
}
/**
* Gets the stored state option, and does the initial set up if it doesn't
* already exist.
*
* @return object The stored state option.
*/
public static function get_stored_state() {
$stored_state = get_option( self::STORED_STATE_OPTION_NAME );
if ( $stored_state === false ) {
$stored_state = new \stdClass();
$stored_state = StoredStateSetupForProducts::init_stored_state(
$stored_state
);
add_option(
self::STORED_STATE_OPTION_NAME,
$stored_state,
'',
false
);
}
return $stored_state;
}
/**
* The deactivated_plugin hook happens before the option is updated
* (https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/plugin.php#L826)
* so this captures the deactivated plugin path and pushes it into the
* PluginsProvider.
*
* @param string $plugin Path to the plugin file relative to the plugins directory.
*/
public static function run_on_deactivated_plugin( $plugin ) {
PluginsProvider::set_deactivated_plugin( $plugin );
self::run();
}
/**
* Update the stored state option.
*
* @param object $stored_state The stored state.
*/
public static function update_stored_state( $stored_state ) {
update_option( self::STORED_STATE_OPTION_NAME, $stored_state, false );
}
/**
* Get the note. This is used to display localized note.
*
* @param Note $note_from_db The note object created from db.
* @return Note The note.
*/
public static function get_note_from_db( $note_from_db ) {
if ( ! $note_from_db instanceof Note || get_user_locale() === $note_from_db->get_locale() ) {
return $note_from_db;
}
$specs = DataSourcePoller::get_instance()->get_specs_from_data_sources();
foreach ( $specs as $spec ) {
if ( $spec->slug !== $note_from_db->get_name() ) {
continue;
}
$locale = SpecRunner::get_locale( $spec->locales, true );
if ( $locale === null ) {
// No locale found, so don't update the note.
break;
}
$localized_actions = SpecRunner::get_actions( $spec );
// Manually copy the action id from the db to the localized action, since they were not being provided.
foreach ( $localized_actions as $localized_action ) {
$action = $note_from_db->get_action( $localized_action->name );
if ( $action ) {
$localized_action->id = $action->id;
}
}
$note_from_db->set_title( $locale->title );
$note_from_db->set_content( $locale->content );
$note_from_db->set_actions( $localized_actions );
}
return $note_from_db;
}
}
RemoteInboxNotifications/RuleEvaluator.php 0000644 00000004474 15153746750 0015062 0 ustar 00 <?php
/**
* Evaluate the given rules as an AND operation - return false early if a
* rule evaluates to false.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Evaluate the given rules as an AND operation - return false early if a
* rule evaluates to false.
*/
class RuleEvaluator {
/**
* GetRuleProcessor to use.
*
* @var GetRuleProcessor
*/
private $get_rule_processor;
/**
* Constructor.
*
* @param GetRuleProcessor $get_rule_processor The GetRuleProcessor to use.
*/
public function __construct( $get_rule_processor = null ) {
$this->get_rule_processor = null === $get_rule_processor
? new GetRuleProcessor()
: $get_rule_processor;
}
/**
* Evaluate the given rules as an AND operation - return false early if a
* rule evaluates to false.
*
* @param array|object $rules The rule or rules being processed.
* @param object|null $stored_state Stored state.
* @param array $logger_args Arguments for the event logger. `slug` is required.
*
* @throws \InvalidArgumentException Thrown when $logger_args is missing slug.
*
* @return bool The result of the operation.
*/
public function evaluate( $rules, $stored_state = null, $logger_args = array() ) {
if ( is_bool( $rules ) ) {
return $rules;
}
if ( ! is_array( $rules ) ) {
$rules = array( $rules );
}
if ( 0 === count( $rules ) ) {
return false;
}
$evaluation_logger = null;
if ( count( $logger_args ) ) {
if ( ! array_key_exists( 'slug', $logger_args ) ) {
throw new \InvalidArgumentException( 'Missing required field: slug in $logger_args.' );
}
array_key_exists( 'source', $logger_args ) ? $source = $logger_args['source'] : $source = null;
$evaluation_logger = new EvaluationLogger( $logger_args['slug'], $source );
}
foreach ( $rules as $rule ) {
if ( ! is_object( $rule ) ) {
return false;
}
$processor = $this->get_rule_processor->get_processor( $rule->type );
$processor_result = $processor->process( $rule, $stored_state );
$evaluation_logger && $evaluation_logger->add_result( $rule->type, $processor_result );
if ( ! $processor_result ) {
$evaluation_logger && $evaluation_logger->log();
return false;
}
}
$evaluation_logger && $evaluation_logger->log();
return true;
}
}
RemoteInboxNotifications/RuleProcessorInterface.php 0000644 00000001221 15153746750 0016703 0 ustar 00 <?php
/**
* Interface for a rule processor.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor interface
*/
interface RuleProcessorInterface {
/**
* Processes a rule, returning the boolean result of the processing.
*
* @param object $rule The rule to process.
* @param object $stored_state Stored state.
*
* @return bool The result of the processing.
*/
public function process( $rule, $stored_state );
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule );
}
RemoteInboxNotifications/SpecRunner.php 0000644 00000011143 15153746750 0014343 0 ustar 00 <?php
/**
* Runs a single spec.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\Notes;
/**
* Runs a single spec.
*/
class SpecRunner {
/**
* Run the spec.
*
* @param object $spec The spec to run.
* @param object $stored_state Stored state.
*/
public static function run_spec( $spec, $stored_state ) {
$data_store = Notes::load_data_store();
// Create or update the note.
$existing_note_ids = $data_store->get_notes_with_name( $spec->slug );
if ( count( $existing_note_ids ) === 0 ) {
$note = new Note();
$note->set_status( Note::E_WC_ADMIN_NOTE_PENDING );
} else {
$note = Notes::get_note( $existing_note_ids[0] );
if ( $note === false ) {
return;
}
}
// Evaluate the spec and get the new note status.
$previous_status = $note->get_status();
$status = EvaluateAndGetStatus::evaluate(
$spec,
$previous_status,
$stored_state,
new RuleEvaluator()
);
// If the status is changing, update the created date to now.
if ( $previous_status !== $status ) {
$note->set_date_created( time() );
}
// Get the matching locale or fall back to en-US.
$locale = self::get_locale( $spec->locales );
if ( $locale === null ) {
return;
}
// Set up the note.
$note->set_title( $locale->title );
$note->set_content( $locale->content );
$note->set_content_data( isset( $spec->content_data ) ? $spec->content_data : (object) array() );
$note->set_status( $status );
$note->set_type( $spec->type );
$note->set_name( $spec->slug );
if ( isset( $spec->source ) ) {
$note->set_source( $spec->source );
}
// Recreate actions.
$note->set_actions( self::get_actions( $spec ) );
$note->save();
}
/**
* Get the URL for an action.
*
* @param object $action The action.
*
* @return string The URL for the action.
*/
private static function get_url( $action ) {
if ( ! isset( $action->url ) ) {
return '';
}
if ( isset( $action->url_is_admin_query ) && $action->url_is_admin_query ) {
if ( strpos( $action->url, '&path' ) === 0 ) {
return wc_admin_url( $action->url );
}
return admin_url( $action->url );
}
return $action->url;
}
/**
* Get the locale for the WordPress locale, or fall back to the en_US
* locale.
*
* @param Array $locales The locales to search through.
*
* @returns object The locale that was found, or null if no matching locale was found.
*/
public static function get_locale( $locales ) {
$wp_locale = get_user_locale();
$matching_wp_locales = array_values(
array_filter(
$locales,
function( $l ) use ( $wp_locale ) {
return $wp_locale === $l->locale;
}
)
);
if ( count( $matching_wp_locales ) !== 0 ) {
return $matching_wp_locales[0];
}
// Fall back to en_US locale.
$en_us_locales = array_values(
array_filter(
$locales,
function( $l ) {
return $l->locale === 'en_US';
}
)
);
if ( count( $en_us_locales ) !== 0 ) {
return $en_us_locales[0];
}
return null;
}
/**
* Get the action locale that matches the note locale, or fall back to the
* en_US locale.
*
* @param Array $action_locales The locales from the spec's action.
*
* @return object The matching locale, or the en_US fallback locale, or null if neither was found.
*/
public static function get_action_locale( $action_locales ) {
$wp_locale = get_user_locale();
$matching_wp_locales = array_values(
array_filter(
$action_locales,
function ( $l ) use ( $wp_locale ) {
return $wp_locale === $l->locale;
}
)
);
if ( count( $matching_wp_locales ) !== 0 ) {
return $matching_wp_locales[0];
}
// Fall back to en_US locale.
$en_us_locales = array_values(
array_filter(
$action_locales,
function( $l ) {
return $l->locale === 'en_US';
}
)
);
if ( count( $en_us_locales ) !== 0 ) {
return $en_us_locales[0];
}
return null;
}
/**
* Get the actions for a note.
*
* @param object $spec The spec.
*
* @return array The actions.
*/
public static function get_actions( $spec ) {
$note = new Note();
$actions = isset( $spec->actions ) ? $spec->actions : array();
foreach ( $actions as $action ) {
$action_locale = self::get_action_locale( $action->locales );
$url = self::get_url( $action );
$note->add_action(
$action->name,
( $action_locale === null || ! isset( $action_locale->label ) )
? ''
: $action_locale->label,
$url,
$action->status
);
}
return $note->get_actions();
}
}
RemoteInboxNotifications/StoredStateRuleProcessor.php 0000644 00000002346 15153746750 0017255 0 ustar 00 <?php
/**
* Rule processor that performs a comparison operation against a value in the
* stored state object.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor that performs a comparison operation against a value in the
* stored state object.
*/
class StoredStateRuleProcessor implements RuleProcessorInterface {
/**
* Performs a comparison operation against a value in the stored state object.
*
* @param object $rule The rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
if ( ! isset( $stored_state->{$rule->index} ) ) {
return false;
}
return ComparisonOperation::compare(
$stored_state->{$rule->index},
$rule->value,
$rule->operation
);
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->index ) ) {
return false;
}
if ( ! isset( $rule->value ) ) {
return false;
}
if ( ! isset( $rule->operation ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/StoredStateSetupForProducts.php 0000644 00000007200 15153746750 0017733 0 ustar 00 <?php
/**
* Handles stored state setup for products.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\PluginsProvider\PluginsProvider;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\SpecRunner;
/**
* Handles stored state setup for products.
*/
class StoredStateSetupForProducts {
const ASYNC_RUN_REMOTE_NOTIFICATIONS_ACTION_NAME =
'woocommerce_admin/stored_state_setup_for_products/async/run_remote_notifications';
/**
* Initialize the class via the admin_init hook.
*/
public static function admin_init() {
add_action( 'product_page_product_importer', array( __CLASS__, 'run_on_product_importer' ) );
add_action( 'transition_post_status', array( __CLASS__, 'run_on_transition_post_status' ), 10, 3 );
}
/**
* Initialize the class via the init hook.
*/
public static function init() {
add_action( self::ASYNC_RUN_REMOTE_NOTIFICATIONS_ACTION_NAME, array( __CLASS__, 'run_remote_notifications' ) );
}
/**
* Run the remote notifications engine. This is triggered by
* action-scheduler after a product is added. It also cleans up from
* setting the product count increment.
*/
public static function run_remote_notifications() {
RemoteInboxNotificationsEngine::run();
}
/**
* Set initial stored state values.
*
* @param object $stored_state The stored state.
*
* @return object The stored state.
*/
public static function init_stored_state( $stored_state ) {
$stored_state->there_were_no_products = ! self::are_there_products();
$stored_state->there_are_now_products = ! $stored_state->there_were_no_products;
return $stored_state;
}
/**
* Are there products query.
*
* @return bool
*/
private static function are_there_products() {
$query = new \WC_Product_Query(
array(
'limit' => 1,
'paginate' => true,
'return' => 'ids',
'status' => array( 'publish' ),
)
);
$products = $query->get_products();
$count = $products->total;
return $count > 0;
}
/**
* Runs on product importer steps.
*/
public static function run_on_product_importer() {
// We're only interested in when the importer completes.
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( ! isset( $_REQUEST['step'] ) ) {
return;
}
if ( 'done' !== $_REQUEST['step'] ) {
return;
}
// phpcs:enable
self::update_stored_state_and_possibly_run_remote_notifications();
}
/**
* Runs when a post status transitions, but we're only interested if it is
* a product being published.
*
* @param string $new_status The new status.
* @param string $old_status The old status.
* @param Post $post The post.
*/
public static function run_on_transition_post_status( $new_status, $old_status, $post ) {
if (
'product' !== $post->post_type ||
'publish' !== $new_status
) {
return;
}
self::update_stored_state_and_possibly_run_remote_notifications();
}
/**
* Enqueues an async action (using action-scheduler) to run remote
* notifications.
*/
private static function update_stored_state_and_possibly_run_remote_notifications() {
$stored_state = RemoteInboxNotificationsEngine::get_stored_state();
// If the stored_state is the same, we don't need to run remote notifications to avoid unnecessary action scheduling.
if ( true === $stored_state->there_are_now_products ) {
return;
}
$stored_state->there_are_now_products = true;
RemoteInboxNotificationsEngine::update_stored_state( $stored_state );
// Run self::run_remote_notifications asynchronously.
as_enqueue_async_action( self::ASYNC_RUN_REMOTE_NOTIFICATIONS_ACTION_NAME );
}
}
RemoteInboxNotifications/TotalPaymentsVolumeProcessor.php 0000644 00000003410 15153746750 0020151 0 ustar 00 <?php
/**
* Rule processor that passes when a store's payments volume exceeds a provided amount.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Revenue\Query as RevenueQuery;
use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/**
* Rule processor that passes when a store's payments volume exceeds a provided amount.
*/
class TotalPaymentsVolumeProcessor implements RuleProcessorInterface {
/**
* Compare against the store's total payments volume.
*
* @param object $rule The rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
$dates = TimeInterval::get_timeframe_dates( $rule->timeframe );
$reports_revenue = new RevenueQuery(
array(
'before' => $dates['end'],
'after' => $dates['start'],
'interval' => 'year',
'fields' => array( 'total_sales' ),
)
);
$report_data = $reports_revenue->get_data();
$value = $report_data->totals->total_sales;
return ComparisonOperation::compare(
$value,
$rule->value,
$rule->operation
);
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
$allowed_timeframes = array(
'last_week',
'last_month',
'last_quarter',
'last_6_months',
'last_year',
);
if ( ! isset( $rule->timeframe ) || ! in_array( $rule->timeframe, $allowed_timeframes, true ) ) {
return false;
}
if ( ! isset( $rule->value ) ) {
return false;
}
if ( ! isset( $rule->operation ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/TransformerInterface.php 0000644 00000001424 15153746750 0016403 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
use stdClass;
/**
* An interface to define a transformer.
*
* Interface TransformerInterface
*
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications
*/
interface TransformerInterface {
/**
* Transform given value to a different value.
*
* @param mixed $value a value to transform.
* @param stdClass|null $arguments arguments.
* @param string|null $default default value.
*
* @return mixed|null
*/
public function transform( $value, stdClass $arguments = null, $default = null);
/**
* Validate Transformer arguments.
*
* @param stdClass|null $arguments arguments to validate.
*
* @return mixed
*/
public function validate( stdClass $arguments = null );
}
RemoteInboxNotifications/TransformerService.php 0000644 00000003756 15153746750 0016115 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
use InvalidArgumentException;
use stdClass;
/**
* A simple service class for the Transformer classes.
*
* Class TransformerService
*
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications
*/
class TransformerService {
/**
* Create a transformer object by name.
*
* @param string $name name of the transformer.
*
* @return TransformerInterface|null
*/
public static function create_transformer( $name ) {
$camel_cased = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $name ) ) );
$classname = __NAMESPACE__ . '\\Transformers\\' . $camel_cased;
if ( ! class_exists( $classname ) ) {
return null;
}
return new $classname();
}
/**
* Apply transformers to the given value.
*
* @param mixed $target_value a value to transform.
* @param array $transformer_configs transform configuration.
* @param string $default default value.
*
* @throws InvalidArgumentException Throws when one of the requried arguments is missing.
* @return mixed|null
*/
public static function apply( $target_value, array $transformer_configs, $default ) {
foreach ( $transformer_configs as $transformer_config ) {
if ( ! isset( $transformer_config->use ) ) {
throw new InvalidArgumentException( 'Missing required config value: use' );
}
if ( ! isset( $transformer_config->arguments ) ) {
$transformer_config->arguments = null;
}
$transformer = self::create_transformer( $transformer_config->use );
if ( null === $transformer ) {
throw new InvalidArgumentException( "Unable to find a transformer by name: {$transformer_config->use}" );
}
$transformed_value = $transformer->transform( $target_value, $transformer_config->arguments, $default );
// if the transformer returns null, then return the previously transformed value.
if ( null === $transformed_value ) {
return $target_value;
}
$target_value = $transformed_value;
}
return $target_value;
}
}
RemoteInboxNotifications/Transformers/ArrayColumn.php 0000644 00000002161 15153746750 0017200 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\TransformerInterface;
use InvalidArgumentException;
use stdClass;
/**
* Search array value by one of its key.
*
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers
*/
class ArrayColumn implements TransformerInterface {
/**
* Search array value by one of its key.
*
* @param mixed $value a value to transform.
* @param stdClass|null $arguments required arguments 'key'.
* @param string|null $default default value.
*
* @throws InvalidArgumentException Throws when the required argument 'key' is missing.
*
* @return mixed
*/
public function transform( $value, stdClass $arguments = null, $default = null ) {
return array_column( $value, $arguments->key );
}
/**
* Validate Transformer arguments.
*
* @param stdClass|null $arguments arguments to validate.
*
* @return mixed
*/
public function validate( stdClass $arguments = null ) {
if ( ! isset( $arguments->key ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/Transformers/ArrayFlatten.php 0000644 00000002000 15153746750 0017330 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\TransformerInterface;
use stdClass;
/**
* Flatten nested array.
*
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers
*/
class ArrayFlatten implements TransformerInterface {
/**
* Search a given value in the array.
*
* @param mixed $value a value to transform.
* @param stdClass|null $arguments arguments.
* @param string|null $default default value.
*
* @return mixed|null
*/
public function transform( $value, stdClass $arguments = null, $default = null ) {
$return = array();
array_walk_recursive(
$value,
function( $item ) use ( &$return ) {
$return[] = $item;
}
);
return $return;
}
/**
* Validate Transformer arguments.
*
* @param stdClass|null $arguments arguments to validate.
*
* @return mixed
*/
public function validate( stdClass $arguments = null ) {
return true;
}
}
RemoteInboxNotifications/Transformers/ArrayKeys.php 0000644 00000001626 15153746750 0016663 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\TransformerInterface;
use stdClass;
/**
* Search array value by one of its key.
*
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers
*/
class ArrayKeys implements TransformerInterface {
/**
* Search array value by one of its key.
*
* @param mixed $value a value to transform.
* @param stdClass|null $arguments arguments.
* @param string|null $default default value.
*
* @return mixed
*/
public function transform( $value, stdClass $arguments = null, $default = null ) {
return array_keys( $value );
}
/**
* Validate Transformer arguments.
*
* @param stdClass|null $arguments arguments to validate.
*
* @return mixed
*/
public function validate( stdClass $arguments = null ) {
return true;
}
}
RemoteInboxNotifications/Transformers/ArraySearch.php 0000644 00000002306 15153746750 0017151 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\TransformerInterface;
use InvalidArgumentException;
use stdClass;
/**
* Searches a given a given value in the array.
*
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers
*/
class ArraySearch implements TransformerInterface {
/**
* Search a given value in the array.
*
* @param mixed $value a value to transform.
* @param stdClass|null $arguments required argument 'value'.
* @param string|null $default default value.
*
* @throws InvalidArgumentException Throws when the required 'value' is missing.
*
* @return mixed|null
*/
public function transform( $value, stdClass $arguments = null, $default = null ) {
$key = array_search( $arguments->value, $value, true );
if ( false !== $key ) {
return $value[ $key ];
}
return null;
}
/**
* Validate Transformer arguments.
*
* @param stdClass|null $arguments arguments to validate.
*
* @return mixed
*/
public function validate( stdClass $arguments = null ) {
if ( ! isset( $arguments->value ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/Transformers/ArrayValues.php 0000644 00000001632 15153746750 0017204 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\TransformerInterface;
use stdClass;
/**
* Search array value by one of its key.
*
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers
*/
class ArrayValues implements TransformerInterface {
/**
* Search array value by one of its key.
*
* @param mixed $value a value to transform.
* @param stdClass|null $arguments arguments.
* @param string|null $default default value.
*
* @return mixed
*/
public function transform( $value, stdClass $arguments = null, $default = null ) {
return array_values( $value );
}
/**
* Validate Transformer arguments.
*
* @param stdClass|null $arguments arguments to validate.
*
* @return mixed
*/
public function validate( stdClass $arguments = null ) {
return true;
}
}
RemoteInboxNotifications/Transformers/Count.php 0000644 00000001562 15153746750 0016040 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\TransformerInterface;
use stdClass;
/**
* Count elements in Array.
*
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers
*/
class Count implements TransformerInterface {
/**
* Count elements in Array.
*
* @param array $value an array to count.
* @param stdClass|null $arguments arguments.
* @param string|null $default default value.
*
* @return number
*/
public function transform( $value, stdClass $arguments = null, $default = null ) {
return count( $value );
}
/**
* Validate Transformer arguments.
*
* @param stdClass|null $arguments arguments to validate.
*
* @return mixed
*/
public function validate( stdClass $arguments = null ) {
return true;
}
}
RemoteInboxNotifications/Transformers/DotNotation.php 0000644 00000003545 15153746750 0017215 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\TransformerInterface;
use InvalidArgumentException;
use stdClass;
/**
* Find an array value by dot notation.
*
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers
*/
class DotNotation implements TransformerInterface {
/**
* Find given path from the given value.
*
* @param mixed $value a value to transform.
* @param stdClass|null $arguments required argument 'path'.
* @param string|null $default default value.
*
* @throws InvalidArgumentException Throws when the required 'path' is missing.
*
* @return mixed
*/
public function transform( $value, stdclass $arguments = null, $default = null ) {
if ( is_object( $value ) ) {
// if the value is an object, convert it to an array.
$value = json_decode( wp_json_encode( $value ), true );
}
return $this->get( $value, $arguments->path, $default );
}
/**
* Find the given $path in $array by dot notation.
*
* @param array $array an array to search in.
* @param string $path a path in the given array.
* @param null $default default value to return if $path was not found.
*
* @return mixed|null
*/
public function get( $array, $path, $default = null ) {
if ( isset( $array[ $path ] ) ) {
return $array[ $path ];
}
foreach ( explode( '.', $path ) as $segment ) {
if ( ! is_array( $array ) || ! array_key_exists( $segment, $array ) ) {
return $default;
}
$array = $array[ $segment ];
}
return $array;
}
/**
* Validate Transformer arguments.
*
* @param stdClass|null $arguments arguments to validate.
*
* @return mixed
*/
public function validate( stdClass $arguments = null ) {
if ( ! isset( $arguments->path ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/Transformers/PrepareUrl.php 0000644 00000002057 15153746750 0017031 0 ustar 00 <?php
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\TransformerInterface;
use stdClass;
/**
* Prepare site URL for comparison.
*
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers
*/
class PrepareUrl implements TransformerInterface {
/**
* Prepares the site URL by removing the protocol and trailing slash.
*
* @param mixed $value a value to transform.
* @param stdClass|null $arguments arguments.
* @param string|null $default default value.
*
* @return mixed|null
*/
public function transform( $value, stdClass $arguments = null, $default = null ) {
$url_parts = wp_parse_url( rtrim( $value, '/' ) );
return isset( $url_parts['path'] ) ? $url_parts['host'] . $url_parts['path'] : $url_parts['host'];
}
/**
* Validate Transformer arguments.
*
* @param stdClass|null $arguments arguments to validate.
*
* @return mixed
*/
public function validate( stdClass $arguments = null ) {
return true;
}
}
RemoteInboxNotifications/WCAdminActiveForProvider.php 0000644 00000000767 15153746750 0017071 0 ustar 00 <?php
/**
* WCAdmin active for provider.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
use Automattic\WooCommerce\Admin\WCAdminHelper;
defined( 'ABSPATH' ) || exit;
/**
* WCAdminActiveForProvider class
*/
class WCAdminActiveForProvider {
/**
* Get the number of seconds that the store has been active.
*
* @return number Number of seconds.
*/
public function get_wcadmin_active_for_in_seconds() {
return WCAdminHelper::get_wcadmin_active_for_in_seconds();
}
}
RemoteInboxNotifications/WCAdminActiveForRuleProcessor.php 0000644 00000003374 15153746750 0020103 0 ustar 00 <?php
/**
* Rule processor for publishing if wc-admin has been active for at least the
* given number of seconds.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor for publishing if wc-admin has been active for at least the
* given number of seconds.
*/
class WCAdminActiveForRuleProcessor implements RuleProcessorInterface {
/**
* Provides the amount of time wcadmin has been active for.
*
* @var WCAdminActiveForProvider
*/
protected $wcadmin_active_for_provider;
/**
* Constructor
*
* @param object $wcadmin_active_for_provider Provides the amount of time wcadmin has been active for.
*/
public function __construct( $wcadmin_active_for_provider = null ) {
$this->wcadmin_active_for_provider = null === $wcadmin_active_for_provider
? new WCAdminActiveForProvider()
: $wcadmin_active_for_provider;
}
/**
* Performs a comparison operation against the amount of time wc-admin has
* been active for in days.
*
* @param object $rule The rule being processed.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
$active_for_seconds = $this->wcadmin_active_for_provider->get_wcadmin_active_for_in_seconds();
$rule_seconds = $rule->days * DAY_IN_SECONDS;
return ComparisonOperation::compare(
$active_for_seconds,
$rule_seconds,
$rule->operation
);
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->days ) ) {
return false;
}
if ( ! isset( $rule->operation ) ) {
return false;
}
return true;
}
}
RemoteInboxNotifications/WooCommerceAdminUpdatedRuleProcessor.php 0000644 00000001561 15153746750 0021511 0 ustar 00 <?php
/**
* Rule processor for sending when WooCommerce Admin has been updated.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Rule processor for sending when WooCommerce Admin has been updated.
*/
class WooCommerceAdminUpdatedRuleProcessor implements RuleProcessorInterface {
/**
* Process the rule.
*
* @param object $rule The specific rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool Whether the rule passes or not.
*/
public function process( $rule, $stored_state ) {
return get_option( RemoteInboxNotificationsEngine::WCA_UPDATED_OPTION_NAME, false );
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
return true;
}
}
ReportCSVEmail.php 0000644 00000007500 15153746750 0010073 0 ustar 00 <?php
/**
* Handles emailing users CSV Export download links.
*/
namespace Automattic\WooCommerce\Admin;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Include dependencies.
*/
if ( ! class_exists( 'WC_Email', false ) ) {
include_once WC_ABSPATH . 'includes/emails/class-wc-email.php';
}
/**
* ReportCSVEmail Class.
*/
class ReportCSVEmail extends \WC_Email {
/**
* Report labels.
*
* @var array
*/
protected $report_labels;
/**
* Report type (e.g. 'customers').
*
* @var string
*/
protected $report_type;
/**
* Download URL.
*
* @var string
*/
protected $download_url;
/**
* Constructor.
*/
public function __construct() {
$this->id = 'admin_report_export_download';
$this->template_base = WC()->plugin_path() . '/includes/react-admin/emails/';
$this->template_html = 'html-admin-report-export-download.php';
$this->template_plain = 'plain-admin-report-export-download.php';
$this->report_labels = array(
'categories' => __( 'Categories', 'woocommerce' ),
'coupons' => __( 'Coupons', 'woocommerce' ),
'customers' => __( 'Customers', 'woocommerce' ),
'downloads' => __( 'Downloads', 'woocommerce' ),
'orders' => __( 'Orders', 'woocommerce' ),
'products' => __( 'Products', 'woocommerce' ),
'revenue' => __( 'Revenue', 'woocommerce' ),
'stock' => __( 'Stock', 'woocommerce' ),
'taxes' => __( 'Taxes', 'woocommerce' ),
'variations' => __( 'Variations', 'woocommerce' ),
);
// Call parent constructor.
parent::__construct();
}
/**
* This email has no user-facing settings.
*/
public function init_form_fields() {}
/**
* This email has no user-facing settings.
*/
public function init_settings() {}
/**
* Return email type.
*
* @return string
*/
public function get_email_type() {
return class_exists( 'DOMDocument' ) ? 'html' : 'plain';
}
/**
* Get email heading.
*
* @return string
*/
public function get_default_heading() {
return __( 'Your Report Download', 'woocommerce' );
}
/**
* Get email subject.
*
* @return string
*/
public function get_default_subject() {
return __( '[{site_title}]: Your {report_name} Report download is ready', 'woocommerce' );
}
/**
* Get content html.
*
* @return string
*/
public function get_content_html() {
return wc_get_template_html(
$this->template_html,
array(
'report_name' => $this->report_type,
'download_url' => $this->download_url,
'email_heading' => $this->get_heading(),
'sent_to_admin' => true,
'plain_text' => false,
'email' => $this,
),
'',
$this->template_base
);
}
/**
* Get content plain.
*
* @return string
*/
public function get_content_plain() {
return wc_get_template_html(
$this->template_plain,
array(
'report_name' => $this->report_type,
'download_url' => $this->download_url,
'email_heading' => $this->get_heading(),
'sent_to_admin' => true,
'plain_text' => true,
'email' => $this,
),
'',
$this->template_base
);
}
/**
* Trigger the sending of this email.
*
* @param int $user_id User ID to email.
* @param string $report_type The type of report export being emailed.
* @param string $download_url The URL for downloading the report.
*/
public function trigger( $user_id, $report_type, $download_url ) {
$user = new \WP_User( $user_id );
$this->recipient = $user->user_email;
$this->download_url = $download_url;
if ( isset( $this->report_labels[ $report_type ] ) ) {
$this->report_type = $this->report_labels[ $report_type ];
$this->placeholders['{report_name}'] = $this->report_type;
}
$this->send(
$this->get_recipient(),
$this->get_subject(),
$this->get_content(),
$this->get_headers(),
$this->get_attachments()
);
}
}
ReportCSVExporter.php 0000644 00000022772 15153746750 0010664 0 ustar 00 <?php
/**
* Handles reports CSV export batches.
*/
namespace Automattic\WooCommerce\Admin;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
/**
* Include dependencies.
*/
if ( ! class_exists( 'WC_CSV_Batch_Exporter', false ) ) {
include_once WC_ABSPATH . 'includes/export/abstract-wc-csv-batch-exporter.php';
}
/**
* ReportCSVExporter Class.
*/
class ReportCSVExporter extends \WC_CSV_Batch_Exporter {
/**
* Type of report being exported.
*
* @var string
*/
protected $report_type;
/**
* Parameters for the report query.
*
* @var array
*/
protected $report_args;
/**
* REST controller for the report.
*
* @var WC_REST_Reports_Controller
*/
protected $controller;
/**
* Constructor.
*
* @param string $type Report type. E.g. 'customers'.
* @param array $args Report parameters.
*/
public function __construct( $type = false, $args = array() ) {
parent::__construct();
self::maybe_create_directory();
if ( ! empty( $type ) ) {
$this->set_report_type( $type );
$this->set_column_names( $this->get_report_columns() );
}
if ( ! empty( $args ) ) {
$this->set_report_args( $args );
}
}
/**
* Create the directory for reports if it does not yet exist.
*/
public static function maybe_create_directory() {
$reports_dir = self::get_reports_directory();
$files = array(
array(
'base' => $reports_dir,
'file' => '.htaccess',
'content' => 'DirectoryIndex index.php index.html' . PHP_EOL . 'deny from all',
),
array(
'base' => $reports_dir,
'file' => 'index.html',
'content' => '',
),
);
foreach ( $files as $file ) {
if ( ! file_exists( trailingslashit( $file['base'] ) ) ) {
wp_mkdir_p( $file['base'] );
}
if ( ! file_exists( trailingslashit( $file['base'] ) . $file['file'] ) ) {
$file_handle = @fopen( trailingslashit( $file['base'] ) . $file['file'], 'wb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_fopen
if ( $file_handle ) {
fwrite( $file_handle, $file['content'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite
fclose( $file_handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
}
}
}
}
/**
* Get report uploads directory.
*
* @return string
*/
public static function get_reports_directory() {
$upload_dir = wp_upload_dir();
return trailingslashit( $upload_dir['basedir'] ) . 'woocommerce_uploads/reports/';
}
/**
* Get file path to export to.
*
* @return string
*/
protected function get_file_path() {
return self::get_reports_directory() . $this->get_filename();
}
/**
* Setter for report type.
*
* @param string $type The report type. E.g. customers.
*/
public function set_report_type( $type ) {
$this->report_type = $type;
$this->export_type = "admin_{$type}_report";
$this->filename = "wc-{$type}-report-export";
$this->controller = $this->map_report_controller();
}
/**
* Setter for report args.
*
* @param array $args The report args.
*/
public function set_report_args( $args ) {
// Use our own internal limit and include all extended info.
$report_args = array_merge(
$args,
array(
'per_page' => $this->get_limit(),
'extended_info' => true,
)
);
// Should this happen externally?
if ( isset( $report_args['page'] ) ) {
$this->set_page( $report_args['page'] );
}
$this->report_args = $report_args;
}
/**
* Get a REST controller instance for the report type.
*
* @return bool|WC_REST_Reports_Controller Report controller instance or boolean false on error.
*/
protected function map_report_controller() {
// @todo - Add filter to this list.
$controller_map = array(
'products' => 'Automattic\WooCommerce\Admin\API\Reports\Products\Controller',
'variations' => 'Automattic\WooCommerce\Admin\API\Reports\Variations\Controller',
'orders' => 'Automattic\WooCommerce\Admin\API\Reports\Orders\Controller',
'categories' => 'Automattic\WooCommerce\Admin\API\Reports\Categories\Controller',
'taxes' => 'Automattic\WooCommerce\Admin\API\Reports\Taxes\Controller',
'coupons' => 'Automattic\WooCommerce\Admin\API\Reports\Coupons\Controller',
'stock' => 'Automattic\WooCommerce\Admin\API\Reports\Stock\Controller',
'downloads' => 'Automattic\WooCommerce\Admin\API\Reports\Downloads\Controller',
'customers' => 'Automattic\WooCommerce\Admin\API\Reports\Customers\Controller',
'revenue' => 'Automattic\WooCommerce\Admin\API\Reports\Revenue\Stats\Controller',
);
if ( isset( $controller_map[ $this->report_type ] ) ) {
// Load the controllers if accessing outside the REST API.
return new $controller_map[ $this->report_type ]();
}
// Should this do something else?
return false;
}
/**
* Get the report columns from the controller.
*
* @return array Array of report column names.
*/
protected function get_report_columns() {
// Default to the report's defined export columns.
if ( $this->controller instanceof ExportableInterface ) {
return $this->controller->get_export_columns();
}
// Fallback to generating columns from the report schema.
$report_columns = array();
$report_schema = $this->controller->get_item_schema();
if ( isset( $report_schema['properties'] ) ) {
foreach ( $report_schema['properties'] as $column_name => $column_info ) {
// Expand extended info columns into export.
if ( 'extended_info' === $column_name ) {
// Remove columns with questionable CSV values, like markup.
$extended_info = array_diff( array_keys( $column_info ), array( 'image' ) );
$report_columns = array_merge( $report_columns, $extended_info );
} else {
$report_columns[] = $column_name;
}
}
}
return $report_columns;
}
/**
* Get total % complete.
*
* Forces an int from parent::get_percent_complete(), which can return a float.
*
* @return int Percent complete.
*/
public function get_percent_complete() {
return intval( parent::get_percent_complete() );
}
/**
* Get total number of rows in export.
*
* @return int Number of rows to export.
*/
public function get_total_rows() {
return $this->total_rows;
}
/**
* Prepare data for export.
*/
public function prepare_data_to_export() {
$request = new \WP_REST_Request( 'GET', "/wc-analytics/reports/{$this->report_type}" );
$params = $this->controller->get_collection_params();
$defaults = array();
foreach ( $params as $arg => $options ) {
if ( isset( $options['default'] ) ) {
$defaults[ $arg ] = $options['default'];
}
}
$request->set_attributes( array( 'args' => $params ) );
$request->set_default_params( $defaults );
$request->set_query_params( $this->report_args );
$request->sanitize_params();
// Does the controller have an export-specific item retrieval method?
// @todo - Potentially revisit. This is only for /revenue/stats/.
if ( is_callable( array( $this->controller, 'get_export_items' ) ) ) {
$response = $this->controller->get_export_items( $request );
} else {
$response = $this->controller->get_items( $request );
}
// Use WP_REST_Server::response_to_data() to embed links in data.
add_filter( 'woocommerce_rest_check_permissions', '__return_true' );
$rest_server = rest_get_server();
$report_data = $rest_server->response_to_data( $response, true );
remove_filter( 'woocommerce_rest_check_permissions', '__return_true' );
$report_meta = $response->get_headers();
$this->total_rows = $report_meta['X-WP-Total'];
$this->row_data = array_map( array( $this, 'generate_row_data' ), $report_data );
}
/**
* Generate row data from a raw report item.
*
* @param object $item Report item data.
* @return array CSV row data.
*/
protected function get_raw_row_data( $item ) {
$columns = $this->get_column_names();
$row = array();
// Expand extended info.
if ( isset( $item['extended_info'] ) ) {
// Pull extended info property from report item object.
$extended_info = (array) $item['extended_info'];
unset( $item['extended_info'] );
// Merge extended info columns into report item object.
$item = array_merge( $item, $extended_info );
}
foreach ( $columns as $column_id => $column_name ) {
$value = isset( $item[ $column_name ] ) ? $item[ $column_name ] : null;
if ( has_filter( "woocommerce_export_{$this->export_type}_column_{$column_name}" ) ) {
// Filter for 3rd parties.
$value = apply_filters( "woocommerce_export_{$this->export_type}_column_{$column_name}", '', $item );
} elseif ( is_callable( array( $this, "get_column_value_{$column_name}" ) ) ) {
// Handle special columns which don't map 1:1 to item data.
$value = $this->{"get_column_value_{$column_name}"}( $item, $this->export_type );
} elseif ( ! is_scalar( $value ) ) {
// Ensure that the value is somewhat readable in CSV.
$value = wp_json_encode( $value );
}
$row[ $column_id ] = $value;
}
return $row;
}
/**
* Get the export row for a given report item.
*
* @param object $item Report item data.
* @return array CSV row data.
*/
protected function generate_row_data( $item ) {
// Default to the report's export method.
if ( $this->controller instanceof ExportableInterface ) {
$row = $this->controller->prepare_item_for_export( $item );
} else {
// Fallback to raw report data.
$row = $this->get_raw_row_data( $item );
}
return apply_filters( "woocommerce_export_{$this->export_type}_row_data", $row, $item );
}
}
ReportExporter.php 0000644 00000014437 15153746750 0010307 0 ustar 00 <?php
/**
* Handles reports CSV export.
*/
namespace Automattic\WooCommerce\Admin;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use Automattic\WooCommerce\Admin\Schedulers\SchedulerTraits;
/**
* ReportExporter Class.
*/
class ReportExporter {
/**
* Slug to identify the scheduler.
*
* @var string
*/
public static $name = 'report_exporter';
/**
* Scheduler traits.
*/
use SchedulerTraits {
init as scheduler_init;
}
/**
* Export status option name.
*/
const EXPORT_STATUS_OPTION = 'woocommerce_admin_report_export_status';
/**
* Export file download action.
*/
const DOWNLOAD_EXPORT_ACTION = 'woocommerce_admin_download_report_csv';
/**
* Get all available scheduling actions.
* Used to determine action hook names and clear events.
*
* @return array
*/
public static function get_scheduler_actions() {
return array(
'export_report' => 'woocommerce_admin_report_export',
'email_report_download_link' => 'woocommerce_admin_email_report_download_link',
);
}
/**
* Add action dependencies.
*
* @return array
*/
public static function get_dependencies() {
return array(
'email_report_download_link' => self::get_action( 'export_report' ),
);
}
/**
* Hook in action methods.
*/
public static function init() {
// Initialize scheduled action handlers.
self::scheduler_init();
// Report download handler.
add_action( 'admin_init', array( __CLASS__, 'download_export_file' ) );
}
/**
* Queue up actions for a full report export.
*
* @param string $export_id Unique ID for report (timestamp expected).
* @param string $report_type Report type. E.g. 'customers'.
* @param array $report_args Report parameters, passed to data query.
* @param bool $send_email Optional. Send an email when the export is complete.
* @return int Number of items to export.
*/
public static function queue_report_export( $export_id, $report_type, $report_args = array(), $send_email = false ) {
$exporter = new ReportCSVExporter( $report_type, $report_args );
$exporter->prepare_data_to_export();
$total_rows = $exporter->get_total_rows();
$batch_size = $exporter->get_limit();
$num_batches = (int) ceil( $total_rows / $batch_size );
// Create batches, like initial import.
$report_batch_args = array( $export_id, $report_type, $report_args );
if ( 0 < $num_batches ) {
self::queue_batches( 1, $num_batches, 'export_report', $report_batch_args );
if ( $send_email ) {
$email_action_args = array( get_current_user_id(), $export_id, $report_type );
self::schedule_action( 'email_report_download_link', $email_action_args );
}
}
return $total_rows;
}
/**
* Process a report export action.
*
* @param int $page_number Page number for this action.
* @param string $export_id Unique ID for report (timestamp expected).
* @param string $report_type Report type. E.g. 'customers'.
* @param array $report_args Report parameters, passed to data query.
* @return void
*/
public static function export_report( $page_number, $export_id, $report_type, $report_args ) {
$report_args['page'] = $page_number;
$exporter = new ReportCSVExporter( $report_type, $report_args );
$exporter->set_filename( "wc-{$report_type}-report-export-{$export_id}" );
$exporter->generate_file();
self::update_export_percentage_complete( $report_type, $export_id, $exporter->get_percent_complete() );
}
/**
* Generate a key to reference an export status.
*
* @param string $report_type Report type. E.g. 'customers'.
* @param string $export_id Unique ID for report (timestamp expected).
* @return string Status key.
*/
protected static function get_status_key( $report_type, $export_id ) {
return $report_type . ':' . $export_id;
}
/**
* Update the completion percentage of a report export.
*
* @param string $report_type Report type. E.g. 'customers'.
* @param string $export_id Unique ID for report (timestamp expected).
* @param int $percentage Completion percentage.
* @return void
*/
public static function update_export_percentage_complete( $report_type, $export_id, $percentage ) {
$exports_status = get_option( self::EXPORT_STATUS_OPTION, array() );
$status_key = self::get_status_key( $report_type, $export_id );
$exports_status[ $status_key ] = $percentage;
update_option( self::EXPORT_STATUS_OPTION, $exports_status );
}
/**
* Get the completion percentage of a report export.
*
* @param string $report_type Report type. E.g. 'customers'.
* @param string $export_id Unique ID for report (timestamp expected).
* @return bool|int Completion percentage, or false if export not found.
*/
public static function get_export_percentage_complete( $report_type, $export_id ) {
$exports_status = get_option( self::EXPORT_STATUS_OPTION, array() );
$status_key = self::get_status_key( $report_type, $export_id );
if ( isset( $exports_status[ $status_key ] ) ) {
return $exports_status[ $status_key ];
}
return false;
}
/**
* Serve the export file.
*/
public static function download_export_file() {
// @todo - add nonce? (nonces are good for 24 hours)
if (
isset( $_GET['action'] ) &&
! empty( $_GET['filename'] ) &&
self::DOWNLOAD_EXPORT_ACTION === wp_unslash( $_GET['action'] ) && // WPCS: input var ok, sanitization ok.
current_user_can( 'view_woocommerce_reports' )
) {
$exporter = new ReportCSVExporter();
$exporter->set_filename( wp_unslash( $_GET['filename'] ) ); // WPCS: input var ok, sanitization ok.
$exporter->export();
}
}
/**
* Process a report export email action.
*
* @param int $user_id User ID that requested the email.
* @param string $export_id Unique ID for report (timestamp expected).
* @param string $report_type Report type. E.g. 'customers'.
* @return void
*/
public static function email_report_download_link( $user_id, $export_id, $report_type ) {
$percent_complete = self::get_export_percentage_complete( $report_type, $export_id );
if ( 100 === $percent_complete ) {
$query_args = array(
'action' => self::DOWNLOAD_EXPORT_ACTION,
'filename' => "wc-{$report_type}-report-export-{$export_id}",
);
$download_url = add_query_arg( $query_args, admin_url() );
\WC_Emails::instance();
$email = new ReportCSVEmail();
$email->trigger( $user_id, $report_type, $download_url );
}
}
}
ReportsSync.php 0000644 00000013704 15153746750 0007572 0 ustar 00 <?php
/**
* Report table sync related functions and actions.
*/
namespace Automattic\WooCommerce\Admin;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Internal\Admin\Schedulers\CustomersScheduler;
use Automattic\WooCommerce\Internal\Admin\Schedulers\OrdersScheduler;
use Automattic\WooCommerce\Internal\Admin\Schedulers\ImportScheduler;
/**
* ReportsSync Class.
*/
class ReportsSync {
/**
* Hook in sync methods.
*/
public static function init() {
// Initialize scheduler hooks.
foreach ( self::get_schedulers() as $scheduler ) {
$scheduler::init();
}
add_action( 'woocommerce_update_product', array( __CLASS__, 'clear_stock_count_cache' ) );
add_action( 'woocommerce_new_product', array( __CLASS__, 'clear_stock_count_cache' ) );
add_action( 'update_option_woocommerce_notify_low_stock_amount', array( __CLASS__, 'clear_stock_count_cache' ) );
add_action( 'update_option_woocommerce_notify_no_stock_amount', array( __CLASS__, 'clear_stock_count_cache' ) );
}
/**
* Get classes for syncing data.
*
* @return array
* @throws \Exception Throws exception when invalid data is found.
*/
public static function get_schedulers() {
$schedulers = apply_filters(
'woocommerce_analytics_report_schedulers',
array(
new CustomersScheduler(),
new OrdersScheduler(),
)
);
foreach ( $schedulers as $scheduler ) {
if ( ! is_subclass_of( $scheduler, 'Automattic\WooCommerce\Internal\Admin\Schedulers\ImportScheduler' ) ) {
throw new \Exception( __( 'Report sync schedulers should be derived from the Automattic\WooCommerce\Internal\Admin\Schedulers\ImportScheduler class.', 'woocommerce' ) );
}
}
return $schedulers;
}
/**
* Returns true if an import is in progress.
*
* @return bool
*/
public static function is_importing() {
foreach ( self::get_schedulers() as $scheduler ) {
if ( $scheduler::is_importing() ) {
return true;
}
}
return false;
}
/**
* Regenerate data for reports.
*
* @param int|bool $days Number of days to import.
* @param bool $skip_existing Skip existing records.
* @return string
*/
public static function regenerate_report_data( $days, $skip_existing ) {
if ( self::is_importing() ) {
return new \WP_Error( 'wc_admin_import_in_progress', __( 'An import is already in progress. Please allow the previous import to complete before beginning a new one.', 'woocommerce' ) );
}
self::reset_import_stats( $days, $skip_existing );
foreach ( self::get_schedulers() as $scheduler ) {
$scheduler::schedule_action( 'import_batch_init', array( $days, $skip_existing ) );
}
/**
* Fires when report data regeneration begins.
*
* @param int|bool $days Number of days to import.
* @param bool $skip_existing Skip existing records.
*/
do_action( 'woocommerce_analytics_regenerate_init', $days, $skip_existing );
return __( 'Report table data is being rebuilt. Please allow some time for data to fully populate.', 'woocommerce' );
}
/**
* Update the import stat totals and counts.
*
* @param int|bool $days Number of days to import.
* @param bool $skip_existing Skip existing records.
*/
public static function reset_import_stats( $days, $skip_existing ) {
$import_stats = get_option( ImportScheduler::IMPORT_STATS_OPTION, array() );
$totals = self::get_import_totals( $days, $skip_existing );
foreach ( self::get_schedulers() as $scheduler ) {
$import_stats[ $scheduler::$name ]['imported'] = 0;
$import_stats[ $scheduler::$name ]['total'] = $totals[ $scheduler::$name ];
}
// Update imported from date if older than previous.
$previous_import_date = isset( $import_stats['imported_from'] ) ? $import_stats['imported_from'] : null;
$current_import_date = $days ? gmdate( 'Y-m-d 00:00:00', time() - ( DAY_IN_SECONDS * $days ) ) : -1;
if ( ! $previous_import_date || -1 === $current_import_date || new \DateTime( $previous_import_date ) > new \DateTime( $current_import_date ) ) {
$import_stats['imported_from'] = $current_import_date;
}
update_option( ImportScheduler::IMPORT_STATS_OPTION, $import_stats );
}
/**
* Get stats for current import.
*
* @return array
*/
public static function get_import_stats() {
$import_stats = get_option( ImportScheduler::IMPORT_STATS_OPTION, array() );
$import_stats['is_importing'] = self::is_importing();
return $import_stats;
}
/**
* Get the import totals for all syncs.
*
* @param int|bool $days Number of days to import.
* @param bool $skip_existing Skip existing records.
* @return array
*/
public static function get_import_totals( $days, $skip_existing ) {
$totals = array();
foreach ( self::get_schedulers() as $scheduler ) {
$items = $scheduler::get_items( 1, 1, $days, $skip_existing );
$totals[ $scheduler::$name ] = $items->total;
}
return $totals;
}
/**
* Clears all queued actions.
*/
public static function clear_queued_actions() {
foreach ( self::get_schedulers() as $scheduler ) {
$scheduler::clear_queued_actions();
}
}
/**
* Delete all data for reports.
*
* @return string
*/
public static function delete_report_data() {
// Cancel all pending import jobs.
self::clear_queued_actions();
foreach ( self::get_schedulers() as $scheduler ) {
$scheduler::schedule_action( 'delete_batch_init', array() );
}
// Delete import options.
delete_option( ImportScheduler::IMPORT_STATS_OPTION );
return __( 'Report table data is being deleted.', 'woocommerce' );
}
/**
* Clear the count cache when products are added or updated, or when
* the no/low stock options are changed.
*
* @param int $id Post/product ID.
*/
public static function clear_stock_count_cache( $id ) {
delete_transient( 'wc_admin_stock_count_lowstock' );
delete_transient( 'wc_admin_product_count' );
$status_options = wc_get_product_stock_status_options();
foreach ( $status_options as $status => $label ) {
delete_transient( 'wc_admin_stock_count_' . $status );
}
}
}
Schedulers/SchedulerTraits.php 0000644 00000023360 15153746750 0012504 0 ustar 00 <?php
/**
* Traits for scheduling actions and dependencies.
*/
namespace Automattic\WooCommerce\Admin\Schedulers;
defined( 'ABSPATH' ) || exit;
/**
* SchedulerTraits class.
*/
trait SchedulerTraits {
/**
* Action scheduler group.
*
* @var string|null
*/
public static $group = 'wc-admin-data';
/**
* Queue instance.
*
* @var WC_Queue_Interface
*/
protected static $queue = null;
/**
* Add all actions as hooks.
*/
public static function init() {
foreach ( self::get_actions() as $action_name => $action_hook ) {
$method = new \ReflectionMethod( static::class, $action_name );
add_action( $action_hook, array( static::class, 'do_action_or_reschedule' ), 10, $method->getNumberOfParameters() );
}
}
/**
* Get queue instance.
*
* @return WC_Queue_Interface
*/
public static function queue() {
if ( is_null( self::$queue ) ) {
self::$queue = WC()->queue();
}
return self::$queue;
}
/**
* Set queue instance.
*
* @param WC_Queue_Interface $queue Queue instance.
*/
public static function set_queue( $queue ) {
self::$queue = $queue;
}
/**
* Gets the default scheduler actions for batching and scheduling actions.
*/
public static function get_default_scheduler_actions() {
return array(
'schedule_action' => 'wc-admin_schedule_action_' . static::$name,
'queue_batches' => 'wc-admin_queue_batches_' . static::$name,
);
}
/**
* Gets the actions for this specific scheduler.
*
* @return array
*/
public static function get_scheduler_actions() {
return array();
}
/**
* Get all available scheduling actions.
* Used to determine action hook names and clear events.
*/
public static function get_actions() {
return array_merge(
static::get_default_scheduler_actions(),
static::get_scheduler_actions()
);
}
/**
* Get an action tag name from the action name.
*
* @param string $action_name The action name.
* @return string|null
*/
public static function get_action( $action_name ) {
$actions = static::get_actions();
return isset( $actions[ $action_name ] ) ? $actions[ $action_name ] : null;
}
/**
* Returns an array of actions and dependencies as key => value pairs.
*
* @return array
*/
public static function get_dependencies() {
return array();
}
/**
* Get dependencies associated with an action.
*
* @param string $action_name The action slug.
* @return string|null
*/
public static function get_dependency( $action_name ) {
$dependencies = static::get_dependencies();
return isset( $dependencies[ $action_name ] ) ? $dependencies[ $action_name ] : null;
}
/**
* Batch action size.
*/
public static function get_batch_sizes() {
return array(
'queue_batches' => 100,
);
}
/**
* Returns the batch size for an action.
*
* @param string $action Single batch action name.
* @return int Batch size.
*/
public static function get_batch_size( $action ) {
$batch_sizes = static::get_batch_sizes();
$batch_size = isset( $batch_sizes[ $action ] ) ? $batch_sizes[ $action ] : 25;
/**
* Filter the batch size for regenerating a report table.
*
* @param int $batch_size Batch size.
* @param string $action Batch action name.
*/
return apply_filters( 'woocommerce_analytics_regenerate_batch_size', $batch_size, static::$name, $action );
}
/**
* Flatten multidimensional arrays to store for scheduling.
*
* @param array $args Argument array.
* @return string
*/
public static function flatten_args( $args ) {
$flattened = array();
foreach ( $args as $arg ) {
if ( is_array( $arg ) ) {
$flattened[] = self::flatten_args( $arg );
} else {
$flattened[] = $arg;
}
}
$string = '[' . implode( ',', $flattened ) . ']';
return $string;
}
/**
* Check if existing jobs exist for an action and arguments.
*
* @param string $action_name Action name.
* @param array $args Array of arguments to pass to action.
* @return bool
*/
public static function has_existing_jobs( $action_name, $args ) {
$existing_jobs = self::queue()->search(
array(
'status' => 'pending',
'per_page' => 1,
'claimed' => false,
'hook' => static::get_action( $action_name ),
'search' => self::flatten_args( $args ),
'group' => self::$group,
)
);
if ( $existing_jobs ) {
$existing_job = current( $existing_jobs );
// Bail out if there's a pending single action, or a pending scheduled actions.
if (
( static::get_action( $action_name ) === $existing_job->get_hook() ) ||
(
static::get_action( 'schedule_action' ) === $existing_job->get_hook() &&
in_array( self::get_action( $action_name ), $existing_job->get_args(), true )
)
) {
return true;
}
}
return false;
}
/**
* Get the next blocking job for an action.
*
* @param string $action_name Action name.
* @return false|ActionScheduler_Action
*/
public static function get_next_blocking_job( $action_name ) {
$dependency = self::get_dependency( $action_name );
if ( ! $dependency ) {
return false;
}
$blocking_jobs = self::queue()->search(
array(
'status' => 'pending',
'orderby' => 'date',
'order' => 'DESC',
'per_page' => 1,
'search' => $dependency, // search is used instead of hook to find queued batch creation.
'group' => static::$group,
)
);
$next_job_schedule = null;
if ( is_array( $blocking_jobs ) ) {
foreach ( $blocking_jobs as $blocking_job ) {
$next_job_schedule = self::get_next_action_time( $blocking_job );
// Ensure that the next schedule is a DateTime (it can be null).
if ( is_a( $next_job_schedule, 'DateTime' ) ) {
return $blocking_job;
}
}
}
return false;
}
/**
* Check for blocking jobs and reschedule if any exist.
*/
public static function do_action_or_reschedule() {
$action_hook = current_action();
$action_name = array_search( $action_hook, static::get_actions(), true );
$args = func_get_args();
// Check if any blocking jobs exist and schedule after they've completed
// or schedule to run now if no blocking jobs exist.
$blocking_job = static::get_next_blocking_job( $action_name );
if ( $blocking_job ) {
$after = new \DateTime();
self::queue()->schedule_single(
self::get_next_action_time( $blocking_job )->getTimestamp() + 5,
$action_hook,
$args,
static::$group
);
} else {
call_user_func_array( array( static::class, $action_name ), $args );
}
}
/**
* Get the DateTime for the next scheduled time an action should run.
* This function allows backwards compatibility with Action Scheduler < v3.0.
*
* @param \ActionScheduler_Action $action Action.
* @return DateTime|null
*/
public static function get_next_action_time( $action ) {
if ( method_exists( $action->get_schedule(), 'get_next' ) ) {
$after = new \DateTime();
$next_job_schedule = $action->get_schedule()->get_next( $after );
} else {
$next_job_schedule = $action->get_schedule()->next();
}
return $next_job_schedule;
}
/**
* Schedule an action to run and check for dependencies.
*
* @param string $action_name Action name.
* @param array $args Array of arguments to pass to action.
*/
public static function schedule_action( $action_name, $args = array() ) {
// Check for existing jobs and bail if they already exist.
if ( static::has_existing_jobs( $action_name, $args ) ) {
return;
}
$action_hook = static::get_action( $action_name );
if ( ! $action_hook ) {
return;
}
if (
// Skip scheduling if Action Scheduler tables have not been initialized.
! get_option( 'schema-ActionScheduler_StoreSchema' ) ||
apply_filters( 'woocommerce_analytics_disable_action_scheduling', false )
) {
call_user_func_array( array( static::class, $action_name ), $args );
return;
}
self::queue()->schedule_single( time() + 5, $action_hook, $args, static::$group );
}
/**
* Queue a large number of batch jobs, respecting the batch size limit.
* Reduces a range of batches down to "single batch" jobs.
*
* @param int $range_start Starting batch number.
* @param int $range_end Ending batch number.
* @param string $single_batch_action Action to schedule for a single batch.
* @param array $action_args Action arguments.
* @return void
*/
public static function queue_batches( $range_start, $range_end, $single_batch_action, $action_args = array() ) {
$batch_size = static::get_batch_size( 'queue_batches' );
$range_size = 1 + ( $range_end - $range_start );
$action_timestamp = time() + 5;
if ( $range_size > $batch_size ) {
// If the current batch range is larger than a single batch,
// split the range into $queue_batch_size chunks.
$chunk_size = (int) ceil( $range_size / $batch_size );
for ( $i = 0; $i < $batch_size; $i++ ) {
$batch_start = (int) ( $range_start + ( $i * $chunk_size ) );
$batch_end = (int) min( $range_end, $range_start + ( $chunk_size * ( $i + 1 ) ) - 1 );
if ( $batch_start > $range_end ) {
return;
}
self::schedule_action(
'queue_batches',
array( $batch_start, $batch_end, $single_batch_action, $action_args )
);
}
} else {
// Otherwise, queue the single batches.
for ( $i = $range_start; $i <= $range_end; $i++ ) {
$batch_action_args = array_merge( array( $i ), $action_args );
self::schedule_action( $single_batch_action, $batch_action_args );
}
}
}
/**
* Clears all queued actions.
*/
public static function clear_queued_actions() {
if ( version_compare( \ActionScheduler_Versions::instance()->latest_version(), '3.0', '>=' ) ) {
\ActionScheduler::store()->cancel_actions_by_group( static::$group );
} else {
$actions = static::get_actions();
foreach ( $actions as $action ) {
self::queue()->cancel_all( $action, null, static::$group );
}
}
}
}
WCAdminHelper.php 0000644 00000005316 15153746750 0007721 0 ustar 00 <?php
/**
* WCAdminHelper
*
* Helper class for generic WCAdmin functions.
*/
namespace Automattic\WooCommerce\Admin;
defined( 'ABSPATH' ) || exit;
/**
* Class WCAdminHelper
*/
class WCAdminHelper {
/**
* WC Admin timestamp option name.
*/
const WC_ADMIN_TIMESTAMP_OPTION = 'woocommerce_admin_install_timestamp';
const WC_ADMIN_STORE_AGE_RANGES = array(
'week-1' => array(
'start' => 0,
'end' => WEEK_IN_SECONDS,
),
'week-1-4' => array(
'start' => WEEK_IN_SECONDS,
'end' => WEEK_IN_SECONDS * 4,
),
'month-1-3' => array(
'start' => MONTH_IN_SECONDS,
'end' => MONTH_IN_SECONDS * 3,
),
'month-3-6' => array(
'start' => MONTH_IN_SECONDS * 3,
'end' => MONTH_IN_SECONDS * 6,
),
'month-6+' => array(
'start' => MONTH_IN_SECONDS * 6,
),
);
/**
* Get the number of seconds that the store has been active.
*
* @return number Number of seconds.
*/
public static function get_wcadmin_active_for_in_seconds() {
$install_timestamp = get_option( self::WC_ADMIN_TIMESTAMP_OPTION );
if ( ! is_numeric( $install_timestamp ) ) {
$install_timestamp = time();
update_option( self::WC_ADMIN_TIMESTAMP_OPTION, $install_timestamp );
}
return time() - $install_timestamp;
}
/**
* Test how long WooCommerce Admin has been active.
*
* @param int $seconds Time in seconds to check.
* @return bool Whether or not WooCommerce admin has been active for $seconds.
*/
public static function is_wc_admin_active_for( $seconds ) {
$wc_admin_active_for = self::get_wcadmin_active_for_in_seconds();
return ( $wc_admin_active_for >= $seconds );
}
/**
* Test if WooCommerce Admin has been active within a pre-defined range.
*
* @param string $range range available in WC_ADMIN_STORE_AGE_RANGES.
* @param int $custom_start custom start in range.
* @throws \InvalidArgumentException Throws exception when invalid $range is passed in.
* @return bool Whether or not WooCommerce admin has been active within the range.
*/
public static function is_wc_admin_active_in_date_range( $range, $custom_start = null ) {
if ( ! array_key_exists( $range, self::WC_ADMIN_STORE_AGE_RANGES ) ) {
throw new \InvalidArgumentException(
sprintf(
'"%s" range is not supported, use one of: %s',
$range,
implode( ', ', array_keys( self::WC_ADMIN_STORE_AGE_RANGES ) )
)
);
}
$wc_admin_active_for = self::get_wcadmin_active_for_in_seconds();
$range_data = self::WC_ADMIN_STORE_AGE_RANGES[ $range ];
$start = null !== $custom_start ? $custom_start : $range_data['start'];
if ( $range_data && $wc_admin_active_for >= $start ) {
return isset( $range_data['end'] ) ? $wc_admin_active_for < $range_data['end'] : true;
}
return false;
}
}
Admin.php 0000644 00000005132 15154015256 0006312 0 ustar 00 <?php
namespace AIOSEO\Plugin\Lite\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Admin as CommonAdmin;
/**
* Abstract class that Pro and Lite both extend.
*
* @since 4.0.0
*/
class Admin extends CommonAdmin\Admin {
/**
* Connect class instance.
*
* @since 4.2.7
*
* @var Connect
*/
public $connect = null;
/**
* Class constructor.
*
* @since 4.0.0
*/
public function __construct() {
if ( ! wp_doing_cron() ) {
parent::__construct();
}
$this->connect = new Connect();
}
/**
* Actually adds the menu items to the admin bar.
*
* @since 4.0.0
*
* @return void
*/
protected function addAdminBarMenuItems() {
// Add an upsell to Pro.
if ( current_user_can( $this->getPageRequiredCapability( '' ) ) ) {
$this->adminBarMenuItems['aioseo-pro-upgrade'] = [
'parent' => 'aioseo-main',
'title' => '<span class="aioseo-menu-highlight lite">' . __( 'Upgrade to Pro', 'all-in-one-seo-pack' ) . '</span>',
'id' => 'aioseo-pro-upgrade',
'href' => apply_filters(
'aioseo_upgrade_link',
esc_url( admin_url( 'admin.php?page=aioseo-tools&aioseo-redirect-upgrade=1' ) )
),
'meta' => [ 'target' => '_blank' ],
];
}
parent::addAdminBarMenuItems();
}
/**
* Add the menu inside of WordPress.
*
* @since 4.0.0
*
* @return void
*/
public function addMenu() {
parent::addMenu();
$capability = $this->getPageRequiredCapability( '' );
// We use the global submenu, because we are adding an external link here.
if ( current_user_can( $capability ) ) {
global $submenu;
$submenu[ $this->pageSlug ][] = [
'<span class="aioseo-menu-highlight lite">' . esc_html__( 'Upgrade to Pro', 'all-in-one-seo-pack' ) . '</span>',
$capability,
apply_filters(
'aioseo_upgrade_link',
esc_url( admin_url( 'admin.php?page=aioseo-tools&aioseo-redirect-upgrade=1' ) )
)
];
}
}
/**
* Check the query args to see if we need to redirect to an external URL.
*
* @since 4.2.3
*
* @return void
*/
protected function checkForRedirects() {
$mappedUrls = [
// Added to resolve an issue with the open_basedir in the IIS.
'aioseo-redirect-upgrade' => apply_filters(
'aioseo_upgrade_link',
aioseo()->helpers->utmUrl( AIOSEO_MARKETING_URL . 'lite-upgrade/', 'admin-bar', null, false )
)
];
foreach ( $mappedUrls as $queryArg => $redirectUrl ) {
if ( isset( $_GET[ $queryArg ] ) ) { // phpcs:ignore HM.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Recommended
wp_redirect( $redirectUrl );
}
}
}
} License.php 0000644 00000027104 15154015256 0006647 0 ustar 00 <?php
namespace AIOSEO\BrokenLinkChecker\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\BrokenLinkChecker\Models;
/**
* Handles license update/removal and related notices.
*
* @since 1.0.0
*/
class License {
/**
* The base URL for the licensing API.
*
* @since 1.0.0
*
* @var string
*/
private $baseUrl = 'https://blc-licensing.aioseo.com/v1/';
/**
* Options class instance.
*
* @since 1.0.0
*
* @var \AIOSEO\BrokenLinkChecker\Options\Options
*/
protected $options = null;
/**
* InternalOptions class instance.
*
* @since 1.0.0
*t
* @var \AIOSEO\BrokenLinkChecker\Options\InternalOptions
*/
protected $internalOptions = null;
/**
* Class constructor.
*
* @since 1.0.0
*/
public function __construct() {
$this->internalOptions = aioseoBrokenLinkChecker()->internalOptions;
add_action( 'init', [ $this, 'checkIfNeedsValidation' ] );
}
/**
* Checks if we should validate the license key or not.
*
* @since 1.0.0
*
* @return void
*/
public function checkIfNeedsValidation() {
if ( ! $this->internalOptions->internal->license->licenseKey ) {
if ( $this->needsReset() ) {
$this->internalOptions->internal->license->reset(
[
'expires',
'expired',
'invalid',
'disabled',
'activationsError',
'connectionError',
'requestError',
'level'
]
);
}
return;
}
// Validate the license key every 12 hours.
$timestamp = $this->internalOptions->internal->license->lastChecked;
if ( time() < $timestamp ) {
return;
}
$success = $this->activate();
if ( $success || aioseoBrokenLinkChecker()->core->cache->get( 'failed_update' ) ) {
aioseoBrokenLinkChecker()->core->cache->delete( 'failed_update' );
$this->internalOptions->internal->license->lastChecked = strtotime( '+12 hours' );
return;
}
// If update failed, check again after one hour. If the second check fails too, we'll wait 12 hours.
aioseoBrokenLinkChecker()->core->cache->update( 'failed_update', time() );
$this->internalOptions->internal->license->lastChecked = strtotime( '+1 hour' );
}
/**
* Validate the license key.
*
* @since 1.0.0
*
* @return bool Whether or not it was activated.
*/
public function activate() {
$this->internalOptions->internal->license->reset(
[
'expires',
'expired',
'invalid',
'disabled',
'activationsError',
'connectionError',
'requestError',
'level'
]
);
$licenseKey = $this->internalOptions->internal->license->licenseKey;
if ( empty( $licenseKey ) ) {
return false;
}
$site = aioseoBrokenLinkChecker()->helpers->getSite();
$domains = [
'domain' => $site->domain,
'path' => $site->path
];
$response = $this->sendLicenseRequest( 'activate', $licenseKey, [ $domains ] );
if ( empty( $response ) ) {
// Something bad happened, error unknown.
$this->internalOptions->internal->license->connectionError = true;
return false;
}
if ( ! empty( $response->error ) ) {
if ( 'missing-key-or-domain' === $response->error ) {
$this->internalOptions->internal->license->requestError = true;
return false;
}
if ( 'missing-license' === $response->error ) {
$this->internalOptions->internal->license->invalid = true;
return false;
}
if ( 'disabled' === $response->error ) {
$this->internalOptions->internal->license->disabled = true;
return false;
}
if ( 'activations' === $response->error ) {
$this->internalOptions->internal->license->activationsError = true;
return false;
}
if ( 'expired' === $response->error ) {
$this->internalOptions->internal->license->expires = strtotime( $response->expires );
$this->internalOptions->internal->license->expired = true;
return false;
}
}
// Something bad happened, error unknown.
if ( empty( $response->success ) || empty( $response->level ) || empty( $response->broken_links_count ) ) {
return false;
}
$oldQuota = $this->internalOptions->internal->license->quota;
$this->internalOptions->internal->license->level = $response->level;
$this->internalOptions->internal->license->expires = strtotime( $response->expires );
$this->internalOptions->internal->license->quota = intval( $response->broken_links_count );
// Set the remaining quota if it's never been set or if the user's plan has changed.
if (
! $this->internalOptions->internal->license->quotaRemaining ||
( intval( $response->broken_links_count ) !== (int) $oldQuota )
) {
$this->internalOptions->internal->license->quotaRemaining = intval( $response->broken_links_count );
}
// Cancel all Link Status scans. The next request will fire off a new one.
if ( function_exists( 'as_unschedule_all_actions' ) ) {
as_unschedule_all_actions( aioseoBrokenLinkChecker()->main->linkStatus->actionName );
}
return true;
}
/**
* Deactivate the license key.
*
* @since 1.0.0
*
* @return bool Whether or not it was deactivated.
*/
public function deactivate() {
$licenseKey = $this->internalOptions->internal->license->licenseKey;
if ( empty( $licenseKey ) ) {
return false;
}
$site = aioseoBrokenLinkChecker()->helpers->getSite();
$domains = [
'domain' => $site->domain,
'path' => $site->path
];
$response = $this->sendLicenseRequest( 'deactivate', $licenseKey, [ $domains ] );
if ( empty( $response ) ) {
// Something bad happened, error unknown.
$this->internalOptions->internal->license->connectionError = true;
return false;
}
if ( ! empty( $response->error ) ) {
if ( 'missing-key-or-domain' === $response->error || 'not-activated' === $response->error ) {
$this->internalOptions->internal->license->requestError = true;
return false;
}
if ( 'missing-license' === $response->error ) {
$this->internalOptions->internal->license->invalid = true;
return false;
}
if ( 'disabled' === $response->error ) {
$this->internalOptions->internal->license->disabled = true;
return false;
}
}
$this->internalOptions->internal->license->reset(
[
'expires',
'expired',
'invalid',
'disabled',
'activationsError',
'connectionError',
'requestError',
'level'
]
);
// Cancel all Link Status scans.
as_unschedule_all_actions( aioseoBrokenLinkChecker()->main->linkStatus->actionName );
return true;
}
/**
* Returns the URL to check licenses.
*
* @since 1.0.0
*
* @return string The URL.
*/
public function getUrl() {
if ( defined( 'AIOSEO_BROKEN_LINK_CHECKER_LICENSING_URL' ) ) {
return AIOSEO_BROKEN_LINK_CHECKER_LICENSING_URL;
}
return $this->baseUrl;
}
/**
* Checks to see if the current license is expired.
*
* @since 1.0.0
*
* @return bool Whether the license is expired.
*/
public function isExpired() {
$networkIsExpired = false;
$licenseKey = $this->internalOptions->internal->license->licenseKey;
if ( empty( $licenseKey ) ) {
return $networkIsExpired;
}
$expired = $this->internalOptions->internal->license->expired || $this->internalOptions->internal->license->expires < time();
if ( $expired ) {
$didActivationAttempt = $this->maybeReactivateExpiredLicense();
// If we tried to activate the license again, start over. Otherwise, return true.
return $didActivationAttempt ? $this->isExpired() : true;
}
$expires = $this->internalOptions->internal->license->expires;
return 0 !== $expires && $expires < time();
}
/**
* Checks to see if the current license is disabled.
*
* @since 1.0.0
*
* @return bool Whether the license is disabled.
*/
public function isDisabled() {
$networkIsDisabled = false;
$licenseKey = $this->internalOptions->internal->license->licenseKey;
if ( empty( $licenseKey ) ) {
return $networkIsDisabled;
}
return $this->internalOptions->internal->license->disabled;
}
/**
* Checks to see if the current license is invalid.
*
* @since 1.0.0
*
* @return bool Whether the license is invalid.
*/
public function isInvalid() {
$networkIsInvalid = false;
$licenseKey = $this->internalOptions->internal->license->licenseKey;
if ( empty( $licenseKey ) ) {
return $networkIsInvalid;
}
return $this->internalOptions->internal->license->invalid;
}
/**
* Checks to see if the current license is active.
*
* @since 1.0.0
*
* @return bool Whether the license is active.
*/
public function isActive() {
$networkIsActive = false;
$licenseKey = $this->internalOptions->internal->license->licenseKey;
if ( empty( $licenseKey ) ) {
return $networkIsActive;
}
return ! $this->isExpired() && ! $this->isDisabled() && ! $this->isInvalid();
}
/**
* Get the license level for the activated license.
*
* @since 1.0.0
*
* @return string The license level.
*/
public function getLicenseLevel() {
return $this->internalOptions->internal->license->level;
}
/**
* Checks if the license data needs to be reset.
*
* @since 1.0.0
*
* @return bool Whether the license data needs to be reet.
*/
private function needsReset() {
if ( ! empty( $this->internalOptions->internal->license->licenseKey ) ) {
return false;
}
if ( $this->internalOptions->internal->license->level ) {
return true;
}
if ( $this->internalOptions->internal->license->invalid ) {
return true;
}
if ( $this->internalOptions->internal->license->disabled ) {
return true;
}
$expired = $this->internalOptions->internal->license->expired;
if ( $expired ) {
return true;
}
$expires = $this->internalOptions->internal->license->expires;
return 0 !== $expires;
}
/**
* Sends the license request.
*
* @since 1.0.0
*
* @param string $type The type of request, either activate or deactivate.
* @param string $licenseKey The license key we are using for this request.
* @param array $domains List of domains to activate or deactivate.
* @return Object|null The JSON response as an object.
*/
public function sendLicenseRequest( $type, $licenseKey, $domains ) {
$payload = [
'sku' => 'aioseo-broken-link-checker',
'version' => AIOSEO_BROKEN_LINK_CHECKER_VERSION,
'php_version' => PHP_VERSION,
'license' => $licenseKey,
'domains' => $domains,
'wp_version' => get_bloginfo( 'version' )
];
return aioseoBrokenLinkChecker()->helpers->sendRequest( $this->getUrl() . $type . '/', $payload );
}
/**
* Checks if the current site is licensed at the network level.
*
* @since 1.0.0
*
* @return bool Whether the site is licensed at the network level.
*/
public function isNetworkLicensed() {
if ( ! property_exists( aioseoBrokenLinkChecker(), 'networkLicense' ) ) {
return false;
}
return aioseoBrokenLinkChecker()->networkLicense->isActive();
}
/**
* Whether the current license plan is the free plan.
*
* @since 1.0.0
*
* @return bool
*/
public function isFree() {
return 'free' === strtolower( (string) $this->getLicenseLevel() );
}
/**
* Checks if the license is expired and attempts to activate it again.
*
* @since 1.1.0
*
* @return bool True if an attempt was made to activate the license, false if not.
*/
private function maybeReactivateExpiredLicense() {
// If the license is expired, send out a request to check if it's still expired.
// We cache this for a few hours so we don't spam the server.
$transientName = 'expired_license_check';
if ( aioseoBrokenLinkChecker()->core->cache->get( $transientName ) ) {
return false;
}
$this->activate();
aioseoBrokenLinkChecker()->core->cache->update( $transientName, true, 4 * HOUR_IN_SECONDS );
return true;
}
} Notices/NotConnected.php 0000644 00000007140 15154015256 0011252 0 ustar 00 <?php
namespace AIOSEO\BrokenLinkChecker\Admin\Notices;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Not Connected notice.
*
* @since 1.2.1
*/
class NotConnected {
/**
* Class constructor.
*
* @since 1.2.1
*/
public function __construct() {
add_action( 'wp_ajax_aioseo-blc-dismiss-not-connected', [ $this, 'dismissNotice' ] );
}
/**
* Go through all the checks to see if we should show the notice.
*
* @since 1.2.1
*
* @return void
*/
public function maybeShowNotice() {
// Don't show to users that cannot interact with the plugin.
if ( ! current_user_can( 'edit_posts' ) || ! current_user_can( 'manage_options' ) ) {
return;
}
if ( aioseoBrokenLinkChecker()->admin->isBlcScreen() ) {
return;
}
// Make sure the user is not connected/licensed.
if ( aioseoBrokenLinkChecker()->license->isActive() ) {
return;
}
$dismissed = get_user_meta( get_current_user_id(), '_aioseo_blc_not_connected', true );
if ( ! empty( $dismissed ) && $dismissed > time() ) {
return;
}
$this->showNotice();
add_action( 'admin_footer', [ $this, 'printScript' ] );
}
/**
* Actually show the review plugin 2.0.
*
* @since 1.2.1
*
* @return void
*/
public function showNotice() {
$string = sprintf(
// Translators: 1 - The plugin name ("Broken Link Checker").
__( 'Your site is not connected with %1$s. %2$sConnect now%3$s to start scanning for broken links and fix them to improve your SEO.', 'aioseo-broken-link-checker' ), // phpcs:ignore Generic.Files.LineLength.MaxExceeded
'<strong>' . esc_html( AIOSEO_BROKEN_LINK_CHECKER_PLUGIN_NAME ) . '</strong>',
'<a href="' . esc_url( admin_url( 'admin.php?page=broken-link-checker#/settings' ) ) . '">',
'</a>'
);
?>
<div class="notice notice-error aioseo-blc-not-connected is-dismissible">
<div class="step-3">
<p><?php echo $string; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
</div>
</div>
<?php
}
/**
* Dismiss the notice.
*
* @since 1.2.1
*
* @return void
*/
public function dismissNotice() {
if ( ! isset( $_POST['action'] ) || 'aioseo-blc-dismiss-not-connected' !== $_POST['action'] ) {
return;
}
check_ajax_referer( 'aioseo-blc-dismiss-not-connected', 'nonce' );
update_user_meta( get_current_user_id(), '_aioseo_blc_not_connected', strtotime( '+1 week' ) );
wp_send_json_success();
}
/**
* Print the script for dismissing the notice.
*
* @since 1.2.1
*
* @return void
*/
public function printScript() {
// Create a nonce.
$nonce = wp_create_nonce( 'aioseo-blc-dismiss-not-connected' );
?>
<style>
@keyframes dismissBtnVisible {
from { opacity: 0.99; }
to { opacity: 1; }
}
.aioseo-blc-not-connected button.notice-dismiss {
animation-duration: 0.001s;
animation-name: dismissBtnVisible;
}
</style>
<script>
window.addEventListener('load', function () {
dismissNotice = function (dismissBtn) {
dismissBtn.addEventListener('click', function (event) {
var httpRequest = new XMLHttpRequest(),
postData = ''
// Build the data to send in our request.
postData += '&action=aioseo-blc-dismiss-not-connected'
postData += '&nonce=<?php echo esc_html( $nonce ); ?>'
httpRequest.open('POST', '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>')
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
httpRequest.send(postData)
})
}
dismissBtn = document.querySelector('.aioseo-blc-not-connected .notice-dismiss')
dismissNotice(dismissBtn)
});
</script>
<?php
}
} Notices/Review.php 0000644 00000024427 15154015256 0010137 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin\Notices;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Review Plugin Notice.
*
* @since 4.0.0
*/
class Review {
/**
* Class Constructor.
*
* @since 4.0.0
*/
public function __construct() {
add_action( 'wp_ajax_aioseo-dismiss-review-plugin-cta', [ $this, 'dismissNotice' ] );
}
/**
* Go through all the checks to see if we should show the notice.
*
* @since 4.0.0
*
* @return void
*/
public function maybeShowNotice() {
$dismissed = get_user_meta( get_current_user_id(), '_aioseo_plugin_review_dismissed', true );
if ( '3' === $dismissed || '4' === $dismissed ) {
return;
}
if ( ! empty( $dismissed ) && $dismissed > time() ) {
return;
}
// Only show to users that interact with our pluign.
if ( ! current_user_can( 'publish_posts' ) ) {
return;
}
// Only show if plugin has been active for over 10 days.
if ( ! aioseo()->internalOptions->internal->firstActivated ) {
aioseo()->internalOptions->internal->firstActivated = time();
}
$activated = aioseo()->internalOptions->internal->firstActivated( time() );
if ( $activated > strtotime( '-10 days' ) ) {
return;
}
if ( get_option( 'aioseop_options' ) || get_option( 'aioseo_options_v3' ) ) {
$this->showNotice();
} else {
$this->showNotice2();
}
// Print the script to the footer.
add_action( 'admin_footer', [ $this, 'printScript' ] );
}
/**
* Actually show the review plugin.
*
* @since 4.0.0
*
* @return void
*/
public function showNotice() {
$feedbackUrl = add_query_arg(
[
'wpf7528_24' => untrailingslashit( home_url() ),
'wpf7528_26' => aioseo()->options->has( 'general' ) && aioseo()->options->general->has( 'licenseKey' )
? aioseo()->options->general->licenseKey
: '',
'wpf7528_27' => aioseo()->pro ? 'pro' : 'lite',
'wpf7528_28' => AIOSEO_VERSION,
'utm_source' => aioseo()->pro ? 'proplugin' : 'liteplugin',
'utm_medium' => 'review-notice',
'utm_campaign' => 'feedback',
'utm_content' => AIOSEO_VERSION,
],
'https://aioseo.com/plugin-feedback/'
);
$string1 = sprintf(
// Translators: 1 - The plugin short name ("AIOSEO").
__( 'Are you enjoying %1$s?', 'all-in-one-seo-pack' ),
AIOSEO_PLUGIN_NAME
);
$string2 = __( 'Yes I love it', 'all-in-one-seo-pack' );
$string3 = __( 'Not Really...', 'all-in-one-seo-pack' );
$string4 = sprintf(
// Translators: 1 - The plugin name ("All in One SEO").
__( 'We\'re sorry to hear you aren\'t enjoying %1$s. We would love a chance to improve. Could you take a minute and let us know what we can do better?', 'all-in-one-seo-pack' ),
AIOSEO_PLUGIN_NAME
); // phpcs:ignore Generic.Files.LineLength.MaxExceeded
$string5 = __( 'Give feedback', 'all-in-one-seo-pack' );
$string6 = __( 'No thanks', 'all-in-one-seo-pack' );
$string7 = __( 'That\'s awesome! Could you please do us a BIG favor and give it a 5-star rating on WordPress to help us spread the word and boost our motivation?', 'all-in-one-seo-pack' );
// Translators: 1 - The plugin name ("All in One SEO").
$string9 = __( 'Ok, you deserve it', 'all-in-one-seo-pack' );
$string10 = __( 'Nope, maybe later', 'all-in-one-seo-pack' );
$string11 = __( 'I already did', 'all-in-one-seo-pack' );
?>
<div class="notice notice-info aioseo-review-plugin-cta is-dismissible">
<div class="step-1">
<p><?php echo esc_html( $string1 ); ?></p>
<p>
<a href="#" class="aioseo-review-switch-step-3" data-step="3"><?php echo esc_html( $string2 ); ?></a> 🙂 |
<a href="#" class="aioseo-review-switch-step-2" data-step="2"><?php echo esc_html( $string3 ); ?></a>
</p>
</div>
<div class="step-2" style="display:none;">
<p><?php echo esc_html( $string4 ); ?></p>
<p>
<a href="<?php echo esc_url( $feedbackUrl ); ?>" class="aioseo-dismiss-review-notice" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $string5 ); ?></a>
<a href="#" class="aioseo-dismiss-review-notice" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $string6 ); ?></a>
</p>
</div>
<div class="step-3" style="display:none;">
<p><?php echo esc_html( $string7 ); ?></p>
<p>
<a href="https://wordpress.org/support/plugin/all-in-one-seo-pack/reviews/?filter=5#new-post" class="aioseo-dismiss-review-notice" target="_blank" rel="noopener noreferrer">
<?php echo esc_html( $string9 ); ?>
</a> •
<a href="#" class="aioseo-dismiss-review-notice-delay" target="_blank" rel="noopener noreferrer">
<?php echo esc_html( $string10 ); ?>
</a> •
<a href="#" class="aioseo-dismiss-review-notice" target="_blank" rel="noopener noreferrer">
<?php echo esc_html( $string11 ); ?>
</a>
</p>
</div>
</div>
<?php
}
/**
* Actually show the review plugin 2.0.
*
* @since 4.2.2
*
* @return void
*/
public function showNotice2() {
$string1 = sprintf(
// Translators: 1 - The plugin name ("All in One SEO").
__( 'Hey, we noticed you have been using %1$s for some time - that’s awesome! Could you please do us a BIG favor and give it a 5-star rating on WordPress to help us spread the word and boost our motivation?', 'all-in-one-seo-pack' ), // phpcs:ignore Generic.Files.LineLength.MaxExceeded
'<strong>' . esc_html( AIOSEO_PLUGIN_NAME ) . '</strong>'
);
// Translators: 1 - The plugin name ("All in One SEO").
$string9 = __( 'Ok, you deserve it', 'all-in-one-seo-pack' );
$string10 = __( 'Nope, maybe later', 'all-in-one-seo-pack' );
$string11 = __( 'I already did', 'all-in-one-seo-pack' );
?>
<div class="notice notice-info aioseo-review-plugin-cta is-dismissible">
<div class="step-3">
<p><?php echo $string1; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
<p>
<a href="https://wordpress.org/support/plugin/all-in-one-seo-pack/reviews/?filter=5#new-post" class="aioseo-dismiss-review-notice" target="_blank" rel="noopener noreferrer">
<?php echo esc_html( $string9 ); ?>
</a> •
<a href="#" class="aioseo-dismiss-review-notice-delay" target="_blank" rel="noopener noreferrer">
<?php echo esc_html( $string10 ); ?>
</a> •
<a href="#" class="aioseo-dismiss-review-notice" target="_blank" rel="noopener noreferrer">
<?php echo esc_html( $string11 ); ?>
</a>
</p>
</div>
</div>
<?php
}
/**
* Print the script for dismissing the notice.
*
* @since 4.0.13
*
* @return void
*/
public function printScript() {
// Create a nonce.
$nonce = wp_create_nonce( 'aioseo-dismiss-review' );
?>
<style>
.aioseop-notice-review_plugin_cta .aioseo-action-buttons {
display: none;
}
@keyframes dismissBtnVisible {
from { opacity: 0.99; }
to { opacity: 1; }
}
.aioseo-review-plugin-cta button.notice-dismiss {
animation-duration: 0.001s;
animation-name: dismissBtnVisible;
}
</style>
<script>
window.addEventListener('load', function () {
var aioseoSetupButton,
dismissBtn
aioseoSetupButton = function (dismissBtn) {
var notice = document.querySelector('.notice.aioseo-review-plugin-cta'),
delay = false,
relay = true,
stepOne = notice.querySelector('.step-1'),
stepTwo = notice.querySelector('.step-2'),
stepThree = notice.querySelector('.step-3')
// Add an event listener to the dismiss button.
dismissBtn.addEventListener('click', function (event) {
var httpRequest = new XMLHttpRequest(),
postData = ''
// Build the data to send in our request.
postData += '&delay=' + delay
postData += '&relay=' + relay
postData += '&action=aioseo-dismiss-review-plugin-cta'
postData += '&nonce=<?php echo esc_html( $nonce ); ?>'
httpRequest.open('POST', '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>')
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
httpRequest.send(postData)
})
notice.addEventListener('click', function (event) {
if (event.target.matches('.aioseo-review-switch-step-3')) {
event.preventDefault()
stepOne.style.display = 'none'
stepTwo.style.display = 'none'
stepThree.style.display = 'block'
}
if (event.target.matches('.aioseo-review-switch-step-2')) {
event.preventDefault()
stepOne.style.display = 'none'
stepThree.style.display = 'none'
stepTwo.style.display = 'block'
}
if (event.target.matches('.aioseo-dismiss-review-notice-delay')) {
event.preventDefault()
delay = true
relay = false
dismissBtn.click()
}
if (event.target.matches('.aioseo-dismiss-review-notice')) {
if ('#' === event.target.getAttribute('href')) {
event.preventDefault()
}
relay = false
dismissBtn.click()
}
})
}
dismissBtn = document.querySelector('.aioseo-review-plugin-cta .notice-dismiss')
if (!dismissBtn) {
document.addEventListener('animationstart', function (event) {
if (event.animationName == 'dismissBtnVisible') {
dismissBtn = document.querySelector('.aioseo-review-plugin-cta .notice-dismiss')
if (dismissBtn) {
aioseoSetupButton(dismissBtn)
}
}
}, false)
} else {
aioseoSetupButton(dismissBtn)
}
});
</script>
<?php
}
/**
* Dismiss the review plugin CTA.
*
* @since 4.0.0
*
* @return string The successful response.
*/
public function dismissNotice() {
// Early exit if we're not on a aioseo-dismiss-review-plugin-cta action.
if ( ! isset( $_POST['action'] ) || 'aioseo-dismiss-review-plugin-cta' !== $_POST['action'] ) {
return;
}
check_ajax_referer( 'aioseo-dismiss-review', 'nonce' );
$delay = isset( $_POST['delay'] ) ? 'true' === sanitize_text_field( wp_unslash( $_POST['delay'] ) ) : false;
$relay = isset( $_POST['relay'] ) ? 'true' === sanitize_text_field( wp_unslash( $_POST['relay'] ) ) : false;
if ( ! $delay ) {
update_user_meta( get_current_user_id(), '_aioseo_plugin_review_dismissed', $relay ? '4' : '3' );
return wp_send_json_success();
}
update_user_meta( get_current_user_id(), '_aioseo_plugin_review_dismissed', strtotime( '+1 week' ) );
return wp_send_json_success();
}
} Notifications.php 0000644 00000023303 15154015256 0010073 0 ustar 00 <?php
namespace AIOSEO\BrokenLinkChecker\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\BrokenLinkChecker\Models;
/**
* Handles our notifications.
*
* @since 1.0.0
*/
class Notifications {
/**
* The URL of the notifications endpoint.
*
* @since 1.0.0
*
* @var string
*/
private $url = 'https://blc-plugin-cdn.aioseo.com/wp-content/notifications.json';
/**
* The review notice class instance.
*
* @since 1.2.0
*
* @var Notices\Review
*/
private $reviewNotice;
/**
* The Not Connected notice class instance.
*
* @since 1.2.1
*
* @var Notices\NotConnected
*/
private $notConnectedNotice;
/**
* Class constructor.
*
* @since 1.0.0
*/
public function __construct() {
add_action( 'aioseo_blc_admin_notifications_update', [ $this, 'update' ] );
if ( ! is_admin() ) {
return;
}
add_action( 'init', [ $this, 'init' ], 2 );
add_action( 'admin_notices', [ $this, 'renderNotices' ] );
}
/**
* Initialize notifications.
*
* @since 1.0.0
*
* @return void
*/
public function init() {
// If our tables do not exist, create them now.
if ( ! aioseoBrokenLinkChecker()->core->db->tableExists( 'aioseo_blc_notifications' ) ) {
aioseoBrokenLinkChecker()->updates->addInitialTables();
return;
}
$this->checkForUpdates();
$this->notConnectedNotice = new Notices\NotConnected();
$this->reviewNotice = new Notices\Review();
}
/**
* Renders the notices.
*
* @since 1.2.0
*
* @return void
*/
public function renderNotices() {
if ( ! is_admin() ) {
return;
}
$this->notConnectedNotice->maybeShowNotice();
$this->reviewNotice->maybeShowNotice();
}
/**
* Checks if we should update our notifications.
*
* @since 1.0.0
*
* @return void
*/
private function checkForUpdates() {
$nextRun = aioseoBrokenLinkChecker()->core->cache->get( 'admin_notifications_update' );
if ( null !== $nextRun && time() < $nextRun ) {
return;
}
aioseoBrokenLinkChecker()->actionScheduler->scheduleAsync( 'aioseo_blc_admin_notifications_update' );
aioseoBrokenLinkChecker()->core->cache->update( 'admin_notifications_update', time() + DAY_IN_SECONDS );
}
/**
* Pulls in the notifications from our remote endpoint and stores them in the DB.
*
* @since 1.0.0
*
* @return void
*/
public function update() {
$notifications = $this->fetch();
if ( empty( $notifications ) ) {
return;
}
foreach ( $notifications as $notification ) {
// First, let's check to see if the notification exists. If so, we want to override it.
$aioseoNotification = aioseoBrokenLinkChecker()->core->db
->start( 'aioseo_blc_notifications' )
->where( 'notification_id', $notification->id )
->run()
->model( 'AIOSEO\\BrokenLinkChecker\\Models\\Notification' );
$buttons = [
'button1' => [
'label' => ! empty( $notification->btns->main->text ) ? sanitize_text_field( $notification->btns->main->text ) : null,
'url' => ! empty( $notification->btns->main->url ) ? esc_url_raw( $notification->btns->main->url ) : null
],
'button2' => [
'label' => ! empty( $notification->btns->alt->text ) ? sanitize_text_field( $notification->btns->alt->text ) : null,
'url' => ! empty( $notification->btns->alt->url ) ? esc_url_raw( $notification->btns->alt->url ) : null
]
];
if ( ! $aioseoNotification->exists() ) {
$aioseoNotification = new Models\Notification();
$aioseoNotification->slug = uniqid();
$aioseoNotification->dismissed = 0;
}
$aioseoNotification->notification_id = $notification->id;
$aioseoNotification->title = sanitize_text_field( $notification->title );
$aioseoNotification->content = sanitize_text_field( $notification->content );
$aioseoNotification->type = ! empty( $notification->notification_type ) ? sanitize_text_field( $notification->notification_type ) : 'info';
$aioseoNotification->level = $notification->type;
$aioseoNotification->start = ! empty( $notification->start ) ? sanitize_text_field( $notification->start ) : null;
$aioseoNotification->end = ! empty( $notification->end ) ? sanitize_text_field( $notification->end ) : null;
$aioseoNotification->button1_label = $buttons['button1']['label'];
$aioseoNotification->button1_action = $buttons['button1']['url'];
$aioseoNotification->button2_label = $buttons['button2']['label'];
$aioseoNotification->button2_action = $buttons['button2']['url'];
$aioseoNotification->save();
// Trigger the drawer to open.
aioseoBrokenLinkChecker()->core->cache->update( 'show_notifications_drawer', true );
}
}
/**
* Pulls in the notifications from the remote feed.
*
* @since 1.0.0
*
* @return array A list of notifications.
*/
private function fetch() {
$response = aioseoBrokenLinkChecker()->helpers->wpRemoteGet( $this->getUrl() );
if ( is_wp_error( $response ) ) {
return [];
}
$body = wp_remote_retrieve_body( $response );
if ( empty( $body ) ) {
return [];
}
$notifications = json_decode( $body );
if ( empty( $notifications ) ) {
return [];
}
return $this->verify( $notifications );
}
/**
* Verifies a notification to see if it's valid before it is stored.
*
* @since 1.0.0
*
* @param array $notifications List of notifications items to verify.
* @return array List of verified notifications.
*/
private function verify( $notifications ) {
if ( ! is_array( $notifications ) || empty( $notifications ) ) {
return [];
}
$data = [];
foreach ( $notifications as $notification ) {
// The content and type should never be empty. If they are, ignore the notification.
if ( empty( $notification->content ) || empty( $notification->type ) ) {
continue;
}
if ( ! is_array( $notification->type ) ) {
$notification->type = [ $notification->type ];
}
foreach ( $notification->type as $type ) {
$type = sanitize_text_field( $type );
// Ignore the notification if not a single type matches.
if ( ! $this->validateType( $type ) ) {
continue 2;
}
}
// Ignore the notification if it already expired.
if ( ! empty( $notification->end ) && time() > strtotime( $notification->end ) ) {
continue;
}
// Ignore the notification if it existed before installing Broken Link Checker.
// Prevents spamming the user with notifications after activation.
$activated = aioseoBrokenLinkChecker()->internalOptions->internal->firstActivated( time() );
if ( ! empty( $notification->start ) && $activated > strtotime( $notification->start ) ) {
continue;
}
$data[] = $notification;
}
return $data;
}
/**
* Validates the notification type.
*
* @since 1.0.0
*
* @param string $type The notification type we are targeting.
* @return bool Whether the notification is valid.
*/
public function validateType( $type ) {
if ( 'all' === $type ) {
return true;
}
// If we are targeting unlicensed users.
if ( 'free' === $type && ! aioseoBrokenLinkChecker()->license->isActive() ) {
return true;
}
// If we are targeting licensed users.
if ( 'licensed' === $type && aioseoBrokenLinkChecker()->license->isActive() ) {
return true;
}
// Store notice if version matches.
if ( $this->versionMatch( aioseoBrokenLinkChecker()->version, $type ) ) {
return true;
}
return false;
}
/**
* Checks whether two versions are equal.
*
* @since 1.0.0
*
* @param string $currentVersion The current version being used.
* @param string|array $compareVersion The version to compare with.
* @return bool Whether it is a match.
*/
private function versionMatch( $currentVersion, $compareVersion ) {
if ( is_array( $compareVersion ) ) {
foreach ( $compareVersion as $compare_single ) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName
$recursiveResult = $this->versionMatch( $currentVersion, $compare_single ); // phpcs:ignore Squiz.NamingConventions.ValidVariableName
if ( $recursiveResult ) {
return true;
}
}
return false;
}
$currentParse = explode( '.', $currentVersion );
if ( strpos( $compareVersion, '-' ) ) {
$compareParse = explode( '-', $compareVersion );
} elseif ( strpos( $compareVersion, '.' ) ) {
$compareParse = explode( '.', $compareVersion );
} else {
return false;
}
$currentCount = count( $currentParse );
$compareCount = count( $compareParse );
for ( $i = 0; $i < $currentCount || $i < $compareCount; $i++ ) {
if ( isset( $compareParse[ $i ] ) && 'x' === strtolower( $compareParse[ $i ] ) ) {
unset( $compareParse[ $i ] );
}
if ( ! isset( $currentParse[ $i ] ) ) {
unset( $compareParse[ $i ] );
} elseif ( ! isset( $compareParse[ $i ] ) ) {
unset( $currentParse[ $i ] );
}
}
foreach ( $compareParse as $index => $subNumber ) {
if ( $currentParse[ $index ] !== $subNumber ) {
return false;
}
}
return true;
}
/**
* Returns the URL for the notifications endpoint.
*
* @since 1.0.0
*
* @return string The URL.
*/
private function getUrl() {
if ( defined( 'AIOSEO_BROKEN_LINK_CHECKER_NOTIFICATIONS_URL' ) ) {
return AIOSEO_BROKEN_LINK_CHECKER_NOTIFICATIONS_URL;
}
return $this->url;
}
/**
* Extends a notice by a (default) 1 week start date.
*
* @since 1.0.0
*
* @param string $notice The notice name.
* @param string $start How long to extend the notice.
* @return void
*/
public function remindMeLater( $notice, $start = '+1 week' ) {
$notification = Models\Notification::getNotificationByName( $notice );
if ( ! $notification->exists() ) {
return;
}
$notification->start = gmdate( 'Y-m-d H:i:s', strtotime( $start ) );
$notification->save();
}
} ActivityPanels.php 0000644 00000003120 15154512347 0010217 0 ustar 00 <?php
/**
* WooCommerce Activity Panel.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\Notes\Notes;
/**
* Contains backend logic for the activity panel feature.
*/
class ActivityPanels {
/**
* Class instance.
*
* @var ActivityPanels instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WooCommerce.
*/
public function __construct() {
add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) );
// Run after Automattic\WooCommerce\Internal\Admin\Loader.
add_filter( 'woocommerce_components_settings', array( $this, 'component_settings' ), 20 );
// New settings injection.
add_filter( 'woocommerce_admin_shared_settings', array( $this, 'component_settings' ), 20 );
}
/**
* Adds fields so that we can store activity panel last read and open times.
*
* @param array $user_data_fields User data fields.
* @return array
*/
public function add_user_data_fields( $user_data_fields ) {
return array_merge(
$user_data_fields,
array(
'activity_panel_inbox_last_read',
'activity_panel_reviews_last_read',
)
);
}
/**
* Add alert count to the component settings.
*
* @param array $settings Component settings.
*/
public function component_settings( $settings ) {
$settings['alertCount'] = Notes::get_notes_count( array( 'error', 'update' ), array( 'unactioned' ) );
return $settings;
}
}
Analytics.php 0000644 00000021702 15154512347 0007215 0 ustar 00 <?php
/**
* WooCommerce Analytics.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\API\Reports\Cache;
use Automattic\WooCommerce\Admin\Features\Features;
/**
* Contains backend logic for the Analytics feature.
*/
class Analytics {
/**
* Option name used to toggle this feature.
*/
const TOGGLE_OPTION_NAME = 'woocommerce_analytics_enabled';
/**
* Clear cache tool identifier.
*/
const CACHE_TOOL_ID = 'clear_woocommerce_analytics_cache';
/**
* Class instance.
*
* @var Analytics instance
*/
protected static $instance = null;
/**
* Determines if the feature has been toggled on or off.
*
* @var boolean
*/
protected static $is_updated = false;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WooCommerce.
*/
public function __construct() {
add_action( 'update_option_' . self::TOGGLE_OPTION_NAME, array( $this, 'reload_page_on_toggle' ), 10, 2 );
add_action( 'woocommerce_settings_saved', array( $this, 'maybe_reload_page' ) );
if ( ! Features::is_enabled( 'analytics' ) ) {
return;
}
add_filter( 'woocommerce_component_settings_preload_endpoints', array( $this, 'add_preload_endpoints' ) );
add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) );
add_action( 'admin_menu', array( $this, 'register_pages' ) );
add_filter( 'woocommerce_debug_tools', array( $this, 'register_cache_clear_tool' ) );
}
/**
* Add the feature toggle to the features settings.
*
* @deprecated 7.0 The WooCommerce Admin features are now handled by the WooCommerce features engine (see the FeaturesController class).
*
* @param array $features Feature sections.
* @return array
*/
public static function add_feature_toggle( $features ) {
return $features;
}
/**
* Reloads the page when the option is toggled to make sure all Analytics features are loaded.
*
* @param string $old_value Old value.
* @param string $value New value.
*/
public static function reload_page_on_toggle( $old_value, $value ) {
if ( $old_value === $value ) {
return;
}
self::$is_updated = true;
}
/**
* Reload the page if the setting has been updated.
*/
public static function maybe_reload_page() {
if ( ! isset( $_SERVER['REQUEST_URI'] ) || ! self::$is_updated ) {
return;
}
wp_safe_redirect( wp_unslash( $_SERVER['REQUEST_URI'] ) );
exit();
}
/**
* Preload data from the countries endpoint.
*
* @param array $endpoints Array of preloaded endpoints.
* @return array
*/
public function add_preload_endpoints( $endpoints ) {
$endpoints['performanceIndicators'] = '/wc-analytics/reports/performance-indicators/allowed';
$endpoints['leaderboards'] = '/wc-analytics/leaderboards/allowed';
return $endpoints;
}
/**
* Adds fields so that we can store user preferences for the columns to display on a report.
*
* @param array $user_data_fields User data fields.
* @return array
*/
public function add_user_data_fields( $user_data_fields ) {
return array_merge(
$user_data_fields,
array(
'categories_report_columns',
'coupons_report_columns',
'customers_report_columns',
'orders_report_columns',
'products_report_columns',
'revenue_report_columns',
'taxes_report_columns',
'variations_report_columns',
'dashboard_sections',
'dashboard_chart_type',
'dashboard_chart_interval',
'dashboard_leaderboard_rows',
)
);
}
/**
* Register the cache clearing tool on the WooCommerce > Status > Tools page.
*
* @param array $debug_tools Available debug tool registrations.
* @return array Filtered debug tool registrations.
*/
public function register_cache_clear_tool( $debug_tools ) {
$settings_url = add_query_arg(
array(
'page' => 'wc-admin',
'path' => '/analytics/settings',
),
get_admin_url( null, 'admin.php' )
);
$debug_tools[ self::CACHE_TOOL_ID ] = array(
'name' => __( 'Clear analytics cache', 'woocommerce' ),
'button' => __( 'Clear', 'woocommerce' ),
'desc' => sprintf(
/* translators: 1: opening link tag, 2: closing tag */
__( 'This tool will reset the cached values used in WooCommerce Analytics. If numbers still look off, try %1$sReimporting Historical Data%2$s.', 'woocommerce' ),
'<a href="' . esc_url( $settings_url ) . '">',
'</a>'
),
'callback' => array( $this, 'run_clear_cache_tool' ),
);
return $debug_tools;
}
/**
* Registers report pages.
*/
public function register_pages() {
$report_pages = self::get_report_pages();
foreach ( $report_pages as $report_page ) {
if ( ! is_null( $report_page ) ) {
wc_admin_register_page( $report_page );
}
}
}
/**
* Get report pages.
*/
public static function get_report_pages() {
$overview_page = array(
'id' => 'woocommerce-analytics',
'title' => __( 'Analytics', 'woocommerce' ),
'path' => '/analytics/overview',
'icon' => 'dashicons-chart-bar',
'position' => 57, // After WooCommerce & Product menu items.
);
$report_pages = array(
$overview_page,
array(
'id' => 'woocommerce-analytics-overview',
'title' => __( 'Overview', 'woocommerce' ),
'parent' => 'woocommerce-analytics',
'path' => '/analytics/overview',
'nav_args' => array(
'order' => 10,
'parent' => 'woocommerce-analytics',
),
),
array(
'id' => 'woocommerce-analytics-products',
'title' => __( 'Products', 'woocommerce' ),
'parent' => 'woocommerce-analytics',
'path' => '/analytics/products',
'nav_args' => array(
'order' => 20,
'parent' => 'woocommerce-analytics',
),
),
array(
'id' => 'woocommerce-analytics-revenue',
'title' => __( 'Revenue', 'woocommerce' ),
'parent' => 'woocommerce-analytics',
'path' => '/analytics/revenue',
'nav_args' => array(
'order' => 30,
'parent' => 'woocommerce-analytics',
),
),
array(
'id' => 'woocommerce-analytics-orders',
'title' => __( 'Orders', 'woocommerce' ),
'parent' => 'woocommerce-analytics',
'path' => '/analytics/orders',
'nav_args' => array(
'order' => 40,
'parent' => 'woocommerce-analytics',
),
),
array(
'id' => 'woocommerce-analytics-variations',
'title' => __( 'Variations', 'woocommerce' ),
'parent' => 'woocommerce-analytics',
'path' => '/analytics/variations',
'nav_args' => array(
'order' => 50,
'parent' => 'woocommerce-analytics',
),
),
array(
'id' => 'woocommerce-analytics-categories',
'title' => __( 'Categories', 'woocommerce' ),
'parent' => 'woocommerce-analytics',
'path' => '/analytics/categories',
'nav_args' => array(
'order' => 60,
'parent' => 'woocommerce-analytics',
),
),
array(
'id' => 'woocommerce-analytics-coupons',
'title' => __( 'Coupons', 'woocommerce' ),
'parent' => 'woocommerce-analytics',
'path' => '/analytics/coupons',
'nav_args' => array(
'order' => 70,
'parent' => 'woocommerce-analytics',
),
),
array(
'id' => 'woocommerce-analytics-taxes',
'title' => __( 'Taxes', 'woocommerce' ),
'parent' => 'woocommerce-analytics',
'path' => '/analytics/taxes',
'nav_args' => array(
'order' => 80,
'parent' => 'woocommerce-analytics',
),
),
array(
'id' => 'woocommerce-analytics-downloads',
'title' => __( 'Downloads', 'woocommerce' ),
'parent' => 'woocommerce-analytics',
'path' => '/analytics/downloads',
'nav_args' => array(
'order' => 90,
'parent' => 'woocommerce-analytics',
),
),
'yes' === get_option( 'woocommerce_manage_stock' ) ? array(
'id' => 'woocommerce-analytics-stock',
'title' => __( 'Stock', 'woocommerce' ),
'parent' => 'woocommerce-analytics',
'path' => '/analytics/stock',
'nav_args' => array(
'order' => 100,
'parent' => 'woocommerce-analytics',
),
) : null,
array(
'id' => 'woocommerce-analytics-customers',
'title' => __( 'Customers', 'woocommerce' ),
'parent' => 'woocommerce',
'path' => '/customers',
),
array(
'id' => 'woocommerce-analytics-settings',
'title' => __( 'Settings', 'woocommerce' ),
'parent' => 'woocommerce-analytics',
'path' => '/analytics/settings',
'nav_args' => array(
'title' => __( 'Analytics', 'woocommerce' ),
'parent' => 'woocommerce-settings',
),
),
);
/**
* The analytics report items used in the menu.
*
* @since 6.4.0
*/
return apply_filters( 'woocommerce_analytics_report_menu_items', $report_pages );
}
/**
* "Clear" analytics cache by invalidating it.
*/
public function run_clear_cache_tool() {
Cache::invalidate();
return __( 'Analytics cache cleared.', 'woocommerce' );
}
}
BlockTemplateRegistry/BlockTemplateRegistry.php 0000644 00000003213 15154512347 0016021 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\BlockTemplateRegistry;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface;
/**
* Block template registry.
*/
final class BlockTemplateRegistry {
/**
* Class instance.
*
* @var BlockTemplateRegistry|null
*/
private static $instance = null;
/**
* Templates.
*
* @var array
*/
protected $templates = array();
/**
* Get the instance of the class.
*/
public static function get_instance(): BlockTemplateRegistry {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Register a single template.
*
* @param BlockTemplateInterface $template Template to register.
*
* @throws \ValueError If a template with the same ID already exists.
*/
public function register( BlockTemplateInterface $template ) {
$id = $template->get_id();
if ( isset( $this->templates[ $id ] ) ) {
throw new \ValueError( 'A template with the specified ID already exists in the registry.' );
}
/**
* Fires when a template is registered.
*
* @param BlockTemplateInterface $template Template that was registered.
*
* @since 8.2.0
*/
do_action( 'woocommerce_block_template_register', $template );
$this->templates[ $id ] = $template;
}
/**
* Get the registered templates.
*/
public function get_all_registered(): array {
return $this->templates;
}
/**
* Get a single registered template.
*
* @param string $id ID of the template.
*/
public function get_registered( $id ): BlockTemplateInterface {
return isset( $this->templates[ $id ] ) ? $this->templates[ $id ] : null;
}
}
BlockTemplateRegistry/BlockTemplatesController.php 0000644 00000002675 15154512347 0016532 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\BlockTemplateRegistry;
/**
* Block template controller.
*/
class BlockTemplatesController {
/**
* Block template registry
*
* @var BlockTemplateRegistry
*/
private $block_template_registry;
/**
* Block template transformer.
*
* @var TemplateTransformer
*/
private $template_transformer;
/**
* Init.
*/
public function init( $block_template_registry, $template_transformer ) {
$this->block_template_registry = $block_template_registry;
$this->template_transformer = $template_transformer;
add_action( 'rest_api_init', array( $this, 'register_templates' ) );
}
/**
* Register templates in the blocks endpoint.
*/
public function register_templates() {
$templates = $this->block_template_registry->get_all_registered();
foreach ( $templates as $template ) {
add_filter( 'pre_get_block_templates', function( $query_result, $query, $template_type ) use( $template ) {
if ( ! isset( $query['area'] ) || $query['area'] !== $template->get_area() ) {
return $query_result;
}
$wp_block_template = $this->template_transformer->transform( $template );
$query_result[] = $wp_block_template;
return $query_result;
}, 10, 3 );
}
}
} BlockTemplateRegistry/TemplateTransformer.php 0000644 00000002575 15154512347 0015552 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\BlockTemplateRegistry;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface;
/**
* Template transformer.
*/
class TemplateTransformer {
/**
* Transform the WooCommerceBlockTemplate to a WP_Block_Template.
*
* @param object $block_template The product template.
*/
public function transform( BlockTemplateInterface $block_template ): \WP_Block_Template {
$template = new \WP_Block_Template();
$template->id = $block_template->get_id();
$template->theme = 'woocommerce/woocommerce';
$template->content = $block_template->get_formatted_template();
$template->source = 'plugin';
$template->slug = $block_template->get_id();
$template->type = 'wp_template';
$template->title = $block_template->get_title();
$template->description = $block_template->get_description();
$template->status = 'publish';
$template->has_theme_file = true;
$template->origin = 'plugin';
$template->is_custom = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are.
$template->post_types = array(); // Don't appear in any Edit Post template selector dropdown.
$template->area = $block_template->get_area();
return $template;
}
} BlockTemplates/AbstractBlock.php 0000644 00000012175 15154512347 0012721 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\BlockTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\ContainerInterface;
/**
* Block configuration used to specify blocks in BlockTemplate.
*/
class AbstractBlock implements BlockInterface {
/**
* The block name.
*
* @var string
*/
private $name;
/**
* The block ID.
*
* @var string
*/
private $id;
/**
* The block order.
*
* @var int
*/
private $order = 10;
/**
* The block attributes.
*
* @var array
*/
private $attributes = [];
/**
* The block template that this block belongs to.
*
* @var BlockTemplate
*/
private $root_template;
/**
* The parent container.
*
* @var ContainerInterface
*/
private $parent;
/**
* Block constructor.
*
* @param array $config The block configuration.
* @param BlockTemplateInterface $root_template The block template that this block belongs to.
* @param BlockContainerInterface|null $parent The parent block container.
*
* @throws \ValueError If the block configuration is invalid.
* @throws \ValueError If the parent block container does not belong to the same template as the block.
*/
public function __construct( array $config, BlockTemplateInterface &$root_template, ContainerInterface &$parent = null ) {
$this->validate( $config, $root_template, $parent );
$this->root_template = $root_template;
$this->parent = is_null( $parent ) ? $root_template : $parent;
$this->name = $config[ self::NAME_KEY ];
if ( ! isset( $config[ self::ID_KEY ] ) ) {
$this->id = $this->root_template->generate_block_id( $this->get_name() );
} else {
$this->id = $config[ self::ID_KEY ];
}
if ( isset( $config[ self::ORDER_KEY ] ) ) {
$this->order = $config[ self::ORDER_KEY ];
}
if ( isset( $config[ self::ATTRIBUTES_KEY ] ) ) {
$this->attributes = $config[ self::ATTRIBUTES_KEY ];
}
}
/**
* Validate block configuration.
*
* @param array $config The block configuration.
* @param BlockTemplateInterface $root_template The block template that this block belongs to.
* @param ContainerInterface|null $parent The parent block container.
*
* @throws \ValueError If the block configuration is invalid.
* @throws \ValueError If the parent block container does not belong to the same template as the block.
*/
protected function validate( array $config, BlockTemplateInterface &$root_template, ContainerInterface &$parent = null ) {
if ( isset( $parent ) && ( $parent->get_root_template() !== $root_template ) ) {
throw new \ValueError( 'The parent block must belong to the same template as the block.' );
}
if ( ! isset( $config[ self::NAME_KEY ] ) || ! is_string( $config[ self::NAME_KEY ] ) ) {
throw new \ValueError( 'The block name must be specified.' );
}
if ( isset( $config[ self::ORDER_KEY ] ) && ! is_int( $config[ self::ORDER_KEY ] ) ) {
throw new \ValueError( 'The block order must be an integer.' );
}
if ( isset( $config[ self::ATTRIBUTES_KEY ] ) && ! is_array( $config[ self::ATTRIBUTES_KEY ] ) ) {
throw new \ValueError( 'The block attributes must be an array.' );
}
}
/**
* Get the block name.
*/
public function get_name(): string {
return $this->name;
}
/**
* Get the block ID.
*/
public function get_id(): string {
return $this->id;
}
/**
* Get the block order.
*/
public function get_order(): int {
return $this->order;
}
/**
* Set the block order.
*
* @param int $order The block order.
*/
public function set_order( int $order ) {
$this->order = $order;
}
/**
* Get the block attributes.
*/
public function get_attributes(): array {
return $this->attributes;
}
/**
* Set the block attributes.
*
* @param array $attributes The block attributes.
*/
public function set_attributes( array $attributes ) {
$this->attributes = $attributes;
}
/**
* Get the template that this block belongs to.
*/
public function &get_root_template(): BlockTemplateInterface {
return $this->root_template;
}
/**
* Get the parent block container.
*/
public function &get_parent(): ContainerInterface {
return $this->parent;
}
/**
* Remove the block from its parent.
*/
public function remove() {
$this->parent->remove_block( $this->id );
}
/**
* Check if the block is detached from its parent block container or the template it belongs to.
*
* @return bool True if the block is detached from its parent block container or the template it belongs to.
*/
public function is_detached(): bool {
$is_in_parent = $this->parent->get_block( $this->id ) === $this;
$is_in_root_template = $this->get_root_template()->get_block( $this->id ) === $this;
return ! ( $is_in_parent && $is_in_root_template );
}
/**
* Get the block configuration as a formatted template.
*
* @return array The block configuration as a formatted template.
*/
public function get_formatted_template(): array {
$arr = [
$this->get_name(),
$this->get_attributes(),
];
return $arr;
}
}
BlockTemplates/AbstractBlockTemplate.php 0000644 00000006152 15154512347 0014413 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\BlockTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface;
/**
* Block template class.
*/
abstract class AbstractBlockTemplate implements BlockTemplateInterface {
use BlockContainerTrait;
/**
* Get the template ID.
*/
abstract public function get_id(): string;
/**
* Get the template title.
*/
public function get_title(): string {
return '';
}
/**
* Get the template description.
*/
public function get_description(): string {
return '';
}
/**
* Get the template area.
*/
public function get_area(): string {
return 'uncategorized';
}
/**
* The block cache.
*
* @var BlockInterface[]
*/
private $block_cache = [];
/**
* Get a block by ID.
*
* @param string $block_id The block ID.
*/
public function get_block( string $block_id ): ?BlockInterface {
return $this->block_cache[ $block_id ] ?? null;
}
/**
* Caches a block in the template. This is an internal method and should not be called directly
* except for from the BlockContainerTrait's add_inner_block() method.
*
* @param BlockInterface $block The block to cache.
*
* @throws \ValueError If a block with the specified ID already exists in the template.
* @throws \ValueError If the block template that the block belongs to is not this template.
*
* @ignore
*/
public function cache_block( BlockInterface &$block ) {
$id = $block->get_id();
if ( isset( $this->block_cache[ $id ] ) ) {
throw new \ValueError( 'A block with the specified ID already exists in the template.' );
}
if ( $block->get_root_template() !== $this ) {
throw new \ValueError( 'The block template that the block belongs to must be the same as this template.' );
}
$this->block_cache[ $id ] = $block;
}
/**
* Uncaches a block in the template. This is an internal method and should not be called directly
* except for from the BlockContainerTrait's remove_block() method.
*
* @param string $block_id The block ID.
*
* @ignore
*/
public function uncache_block( string $block_id ) {
if ( isset( $this->block_cache[ $block_id ] ) ) {
unset( $this->block_cache[ $block_id ] );
}
}
/**
* Generate a block ID based on a base.
*
* @param string $id_base The base to use when generating an ID.
* @return string
*/
public function generate_block_id( string $id_base ): string {
$instance_count = 0;
do {
$instance_count++;
$block_id = $id_base . '-' . $instance_count;
} while ( isset( $this->block_cache[ $block_id ] ) );
return $block_id;
}
/**
* Get the root template.
*/
public function &get_root_template(): BlockTemplateInterface {
return $this;
}
/**
* Get the inner blocks as a formatted template.
*/
public function get_formatted_template(): array {
$inner_blocks = $this->get_inner_blocks_sorted_by_order();
$inner_blocks_formatted_template = array_map(
function( BlockInterface $block ) {
return $block->get_formatted_template();
},
$inner_blocks
);
return $inner_blocks_formatted_template;
}
}
BlockTemplates/Block.php 0000644 00000001356 15154512347 0011234 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\BlockTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockContainerInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface;
/**
* Generic block with container properties to be used in BlockTemplate.
*/
class Block extends AbstractBlock implements BlockContainerInterface {
use BlockContainerTrait;
/**
* Add an inner block to this block.
*
* @param array $block_config The block data.
*/
public function &add_block( array $block_config ): BlockInterface {
$block = new Block( $block_config, $this->get_root_template(), $this );
return $this->add_inner_block( $block );
}
}
BlockTemplates/BlockContainerTrait.php 0000644 00000023452 15154512347 0014104 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\BlockTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\ContainerInterface;
/**
* Trait for block containers.
*/
trait BlockContainerTrait {
/**
* The inner blocks.
*
* @var BlockInterface[]
*/
private $inner_blocks = [];
// phpcs doesn't take into account exceptions thrown by called methods.
// phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
/**
* Add a block to the block container.
*
* @param BlockInterface $block The block.
*
* @throws \ValueError If the block configuration is invalid.
* @throws \ValueError If a block with the specified ID already exists in the template.
* @throws \UnexpectedValueException If the block container is not the parent of the block.
* @throws \UnexpectedValueException If the block container's root template is not the same as the block's root template.
*/
protected function &add_inner_block( BlockInterface $block ): BlockInterface {
if ( $block->get_parent() !== $this ) {
throw new \UnexpectedValueException( 'The block container is not the parent of the block.' );
}
if ( $block->get_root_template() !== $this->get_root_template() ) {
throw new \UnexpectedValueException( 'The block container\'s root template is not the same as the block\'s root template.' );
}
$is_detached = method_exists( $this, 'is_detached' ) && $this->is_detached();
if ( $is_detached ) {
BlockTemplateLogger::get_instance()->warning(
'Block added to detached container. Block will not be included in the template, since the container will not be included in the template.',
[
'block' => $block,
'container' => $this,
'template' => $this->get_root_template(),
]
);
} else {
$this->get_root_template()->cache_block( $block );
}
$this->inner_blocks[] = &$block;
$this->do_after_add_block_action( $block );
$this->do_after_add_specific_block_action( $block );
return $block;
}
// phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
/**
* Checks if a block is a descendant of the block container.
*
* @param BlockInterface $block The block.
*/
private function is_block_descendant( BlockInterface $block ): bool {
$parent = $block->get_parent();
if ( $parent === $this ) {
return true;
}
if ( ! $parent instanceof BlockInterface ) {
return false;
}
return $this->is_block_descendant( $parent );
}
/**
* Get a block by ID.
*
* @param string $block_id The block ID.
*/
public function get_block( string $block_id ): ?BlockInterface {
foreach ( $this->inner_blocks as $block ) {
if ( $block->get_id() === $block_id ) {
return $block;
}
}
foreach ( $this->inner_blocks as $block ) {
if ( $block instanceof ContainerInterface ) {
$block = $block->get_block( $block_id );
if ( $block ) {
return $block;
}
}
}
return null;
}
/**
* Remove a block from the block container.
*
* @param string $block_id The block ID.
*
* @throws \UnexpectedValueException If the block container is not an ancestor of the block.
*/
public function remove_block( string $block_id ) {
$root_template = $this->get_root_template();
$block = $root_template->get_block( $block_id );
if ( ! $block ) {
return;
}
if ( ! $this->is_block_descendant( $block ) ) {
throw new \UnexpectedValueException( 'The block container is not an ancestor of the block.' );
}
// If the block is a container, remove all of its blocks.
if ( $block instanceof ContainerInterface ) {
$block->remove_blocks();
}
$parent = $block->get_parent();
$parent->remove_inner_block( $block );
}
/**
* Remove all blocks from the block container.
*/
public function remove_blocks() {
array_map(
function ( BlockInterface $block ) {
$this->remove_block( $block->get_id() );
},
$this->inner_blocks
);
}
/**
* Remove a block from the block container's inner blocks. This is an internal method and should not be called directly
* except for from the BlockContainerTrait's remove_block() method.
*
* @param BlockInterface $block The block.
*/
public function remove_inner_block( BlockInterface $block ) {
// Remove block from root template's cache.
$root_template = $this->get_root_template();
$root_template->uncache_block( $block->get_id() );
$this->inner_blocks = array_filter(
$this->inner_blocks,
function ( BlockInterface $inner_block ) use ( $block ) {
return $inner_block !== $block;
}
);
BlockTemplateLogger::get_instance()->info(
'Block removed from template.',
[
'block' => $block,
'template' => $root_template,
]
);
$this->do_after_remove_block_action( $block );
$this->do_after_remove_specific_block_action( $block );
}
/**
* Get the inner blocks sorted by order.
*/
private function get_inner_blocks_sorted_by_order(): array {
$sorted_inner_blocks = $this->inner_blocks;
usort(
$sorted_inner_blocks,
function( BlockInterface $a, BlockInterface $b ) {
return $a->get_order() <=> $b->get_order();
}
);
return $sorted_inner_blocks;
}
/**
* Get the inner blocks as a formatted template.
*/
public function get_formatted_template(): array {
$arr = [
$this->get_name(),
$this->get_attributes(),
];
$inner_blocks = $this->get_inner_blocks_sorted_by_order();
if ( ! empty( $inner_blocks ) ) {
$arr[] = array_map(
function( BlockInterface $block ) {
return $block->get_formatted_template();
},
$inner_blocks
);
}
return $arr;
}
/**
* Do the `woocommerce_block_template_after_add_block` action.
* Handle exceptions thrown by the action.
*
* @param BlockInterface $block The block.
*/
private function do_after_add_block_action( BlockInterface $block ) {
try {
/**
* Action called after a block is added to a block container.
*
* This action can be used to perform actions after a block is added to the block container,
* such as adding a dependent block.
*
* @param BlockInterface $block The block.
*
* @since 8.2.0
*/
do_action( 'woocommerce_block_template_after_add_block', $block );
} catch ( \Exception $e ) {
$this->handle_exception_doing_action(
'Error after adding block to template.',
'woocommerce_block_template_after_add_block',
$block,
$e
);
}
}
/**
* Do the `woocommerce_block_template_area_{template_area}_after_add_block_{block_id}` action.
* Handle exceptions thrown by the action.
*
* @param BlockInterface $block The block.
*/
private function do_after_add_specific_block_action( BlockInterface $block ) {
try {
/**
* Action called after a specific block is added to a template with a specific area.
*
* This action can be used to perform actions after a specific block is added to a template with a specific area,
* such as adding a dependent block.
*
* @param BlockInterface $block The block.
*
* @since 8.2.0
*/
do_action( "woocommerce_block_template_area_{$this->get_root_template()->get_area()}_after_add_block_{$block->get_id()}", $block );
} catch ( \Exception $e ) {
$this->handle_exception_doing_action(
'Error after adding block to template.',
"woocommerce_block_template_area_{$this->get_root_template()->get_area()}_after_add_block_{$block->get_id()}",
$block,
$e
);
}
}
/**
* Do the `woocommerce_block_template_after_remove_block` action.
* Handle exceptions thrown by the action.
*
* @param BlockInterface $block The block.
*/
private function do_after_remove_block_action( BlockInterface $block ) {
try {
/**
* Action called after a block is removed from a block container.
*
* This action can be used to perform actions after a block is removed from the block container,
* such as removing a dependent block.
*
* @param BlockInterface $block The block.
*
* @since 8.2.0
*/
do_action( 'woocommerce_block_template_after_remove_block', $block );
} catch ( \Exception $e ) {
$this->handle_exception_doing_action(
'Error after removing block from template.',
'woocommerce_block_template_after_remove_block',
$block,
$e
);
}
}
/**
* Do the `woocommerce_block_template_area_{template_area}_after_remove_block_{block_id}` action.
* Handle exceptions thrown by the action.
*
* @param BlockInterface $block The block.
*/
private function do_after_remove_specific_block_action( BlockInterface $block ) {
try {
/**
* Action called after a specific block is removed from a template with a specific area.
*
* This action can be used to perform actions after a specific block is removed from a template with a specific area,
* such as removing a dependent block.
*
* @param BlockInterface $block The block.
*
* @since 8.2.0
*/
do_action( "woocommerce_block_template_area_{$this->get_root_template()->get_area()}_after_remove_block_{$block->get_id()}", $block );
} catch ( \Exception $e ) {
$this->handle_exception_doing_action(
'Error after removing block from template.',
"woocommerce_block_template_area_{$this->get_root_template()->get_area()}_after_remove_block_{$block->get_id()}",
$block,
$e
);
}
}
/**
* Handle an exception thrown by an action.
*
* @param string $message The message.
* @param string $action_tag The action tag.
* @param BlockInterface $block The block.
* @param \Exception $e The exception.
*/
private function handle_exception_doing_action( string $message, string $action_tag, BlockInterface $block, \Exception $e ) {
BlockTemplateLogger::get_instance()->error(
$message,
[
'exception' => $e,
'action' => $action_tag,
'container' => $this,
'block' => $block,
'template' => $this->get_root_template(),
],
);
}
}
BlockTemplates/BlockTemplate.php 0000644 00000001400 15154512347 0012716 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\BlockTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\ContainerInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface;
/**
* Block template class.
*/
class BlockTemplate extends AbstractBlockTemplate {
/**
* Get the template ID.
*/
public function get_id(): string {
return 'woocommerce-block-template';
}
/**
* Add an inner block to this template.
*
* @param array $block_config The block data.
*/
public function add_block( array $block_config ): BlockInterface {
$block = new Block( $block_config, $this->get_root_template(), $this );
return $this->add_inner_block( $block );
}
}
BlockTemplates/BlockTemplateLogger.php 0000644 00000011140 15154512350 0014052 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\BlockTemplates;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockContainerInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface;
/**
* Logger for block template modifications.
*/
class BlockTemplateLogger {
/**
* Singleton instance.
*
* @var BlockTemplateLogger
*/
protected static $instance = null;
/**
* Logger instance.
*
* @var \WC_Logger
*/
protected $logger = null;
/**
* Get the singleton instance.
*/
public static function get_instance(): BlockTemplateLogger {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*/
protected function __construct() {
$this->logger = wc_get_logger();
}
/**
* Log an informational message.
*
* @param string $message Message to log.
* @param array $info Additional info to log.
*/
public function info( string $message, array $info = [] ) {
$this->logger->info(
$this->format_message( $message, $info ),
[ 'source' => 'block_template' ]
);
}
/**
* Log a warning message.
*
* @param string $message Message to log.
* @param array $info Additional info to log.
*/
public function warning( string $message, array $info = [] ) {
$this->logger->warning(
$this->format_message( $message, $info ),
[ 'source' => 'block_template' ]
);
}
/**
* Log an error message.
*
* @param string $message Message to log.
* @param array $info Additional info to log.
*/
public function error( string $message, array $info = [] ) {
$this->logger->error(
$this->format_message( $message, $info ),
[ 'source' => 'block_template' ]
);
}
/**
* Format a message for logging.
*
* @param string $message Message to log.
* @param array $info Additional info to log.
*/
private function format_message( string $message, array $info = [] ): string {
$formatted_message = sprintf(
"%s\n%s",
$message,
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
print_r( $this->format_info( $info ), true ),
);
return $formatted_message;
}
/**
* Format info for logging.
*
* @param array $info Info to log.
*/
private function format_info( array $info ): array {
$formatted_info = $info;
if ( isset( $info['exception'] ) && $info['exception'] instanceof \Exception ) {
$formatted_info['exception'] = $this->format_exception( $info['exception'] );
}
if ( isset( $info['container'] ) ) {
if ( $info['container'] instanceof BlockContainerInterface ) {
$formatted_info['container'] = $this->format_block( $info['container'] );
} elseif ( $info['container'] instanceof BlockTemplateInterface ) {
$formatted_info['container'] = $this->format_template( $info['container'] );
} elseif ( $info['container'] instanceof BlockInterface ) {
$formatted_info['container'] = $this->format_block( $info['container'] );
}
}
if ( isset( $info['block'] ) && $info['block'] instanceof BlockInterface ) {
$formatted_info['block'] = $this->format_block( $info['block'] );
}
if ( isset( $info['template'] ) && $info['template'] instanceof BlockTemplateInterface ) {
$formatted_info['template'] = $this->format_template( $info['template'] );
}
return $formatted_info;
}
/**
* Format an exception for logging.
*
* @param \Exception $exception Exception to format.
*/
private function format_exception( \Exception $exception ): array {
return [
'message' => $exception->getMessage(),
'source' => "{$exception->getFile()}: {$exception->getLine()}",
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
'trace' => print_r( $this->format_exception_trace( $exception->getTrace() ), true ),
];
}
/**
* Format an exception trace for logging.
*
* @param array $trace Exception trace to format.
*/
private function format_exception_trace( array $trace ): array {
$formatted_trace = [];
foreach ( $trace as $source ) {
$formatted_trace[] = "{$source['file']}: {$source['line']}";
}
return $formatted_trace;
}
/**
* Format a block template for logging.
*
* @param BlockTemplateInterface $template Template to format.
*/
private function format_template( BlockTemplateInterface $template ): string {
return "{$template->get_id()} (area: {$template->get_area()})";
}
/**
* Format a block for logging.
*
* @param BlockInterface $block Block to format.
*/
private function format_block( BlockInterface $block ): string {
return "{$block->get_id()} (name: {$block->get_name()})";
}
}
CategoryLookup.php 0000644 00000017764 15154512350 0010244 0 ustar 00 <?php
/**
* Keeps the product category lookup table in sync with live data.
*/
namespace Automattic\WooCommerce\Internal\Admin;
defined( 'ABSPATH' ) || exit;
/**
* \Automattic\WooCommerce\Internal\Admin\CategoryLookup class.
*/
class CategoryLookup {
/**
* Stores changes to categories we need to sync.
*
* @var array
*/
protected $edited_product_cats = array();
/**
* The single instance of the class.
*
* @var object
*/
protected static $instance = null;
/**
* Constructor
*
* @return void
*/
protected function __construct() {}
/**
* Get class instance.
*
* @return object Instance.
*/
final public static function instance() {
if ( null === static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init hooks.
*/
public function init() {
add_action( 'generate_category_lookup_table', array( $this, 'regenerate' ) );
add_action( 'edit_product_cat', array( $this, 'before_edit' ), 99 );
add_action( 'edited_product_cat', array( $this, 'on_edit' ), 99 );
add_action( 'created_product_cat', array( $this, 'on_create' ), 99 );
add_action( 'init', array( $this, 'define_category_lookup_tables_in_wpdb' ) );
}
/**
* Regenerate all lookup table data.
*/
public function regenerate() {
global $wpdb;
$wpdb->query( "TRUNCATE TABLE $wpdb->wc_category_lookup" );
$terms = get_terms(
'product_cat',
array(
'hide_empty' => false,
'fields' => 'id=>parent',
)
);
$hierarchy = array();
$inserts = array();
$this->unflatten_terms( $hierarchy, $terms, 0 );
$this->get_term_insert_values( $inserts, $hierarchy );
if ( ! $inserts ) {
return;
}
$insert_string = implode(
'),(',
array_map(
function( $item ) {
return implode( ',', $item );
},
$inserts
)
);
$wpdb->query( "INSERT IGNORE INTO $wpdb->wc_category_lookup (category_tree_id,category_id) VALUES ({$insert_string})" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
/**
* Store edits so we know when the parent ID changes.
*
* @param int $category_id Term ID being edited.
*/
public function before_edit( $category_id ) {
$category = get_term( $category_id, 'product_cat' );
$this->edited_product_cats[ $category_id ] = $category->parent;
}
/**
* When a product category gets edited, see if we need to sync the table.
*
* @param int $category_id Term ID being edited.
*/
public function on_edit( $category_id ) {
global $wpdb;
if ( ! isset( $this->edited_product_cats[ $category_id ] ) ) {
return;
}
$category_object = get_term( $category_id, 'product_cat' );
$prev_parent = $this->edited_product_cats[ $category_id ];
$new_parent = $category_object->parent;
// No edits - no need to modify relationships.
if ( $prev_parent === $new_parent ) {
return;
}
$this->delete( $category_id, $prev_parent );
$this->update( $category_id );
}
/**
* When a product category gets created, add a new lookup row.
*
* @param int $category_id Term ID being created.
*/
public function on_create( $category_id ) {
// If WooCommerce is being installed on a multisite, lookup tables haven't been created yet.
if ( 'yes' === get_transient( 'wc_installing' ) ) {
return;
}
$this->update( $category_id );
}
/**
* Delete lookup table data from a tree.
*
* @param int $category_id Category ID to delete.
* @param int $category_tree_id Tree to delete from.
* @return void
*/
protected function delete( $category_id, $category_tree_id ) {
global $wpdb;
if ( ! $category_tree_id ) {
return;
}
$ancestors = get_ancestors( $category_tree_id, 'product_cat', 'taxonomy' );
$ancestors[] = $category_tree_id;
$children = get_term_children( $category_id, 'product_cat' );
$children[] = $category_id;
$id_list = implode( ',', array_map( 'intval', array_unique( array_filter( $children ) ) ) );
foreach ( $ancestors as $ancestor ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->wc_category_lookup WHERE category_tree_id = %d AND category_id IN ({$id_list})", $ancestor ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
}
/**
* Updates lookup table data for a category by ID.
*
* @param int $category_id Category ID to update.
*/
protected function update( $category_id ) {
global $wpdb;
$ancestors = get_ancestors( $category_id, 'product_cat', 'taxonomy' );
$children = get_term_children( $category_id, 'product_cat' );
$inserts = array();
$inserts[] = $this->get_insert_sql( $category_id, $category_id );
$children_ids = array_map( 'intval', array_unique( array_filter( $children ) ) );
foreach ( $ancestors as $ancestor ) {
$inserts[] = $this->get_insert_sql( $category_id, $ancestor );
foreach ( $children_ids as $child_category_id ) {
$inserts[] = $this->get_insert_sql( $child_category_id, $ancestor );
}
}
$insert_string = implode( ',', $inserts );
$wpdb->query( "INSERT IGNORE INTO $wpdb->wc_category_lookup (category_id, category_tree_id) VALUES {$insert_string}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
/**
* Get category lookup table values to insert.
*
* @param int $category_id Category ID to insert.
* @param int $category_tree_id Tree to insert into.
* @return string
*/
protected function get_insert_sql( $category_id, $category_tree_id ) {
global $wpdb;
return $wpdb->prepare( '(%d,%d)', $category_id, $category_tree_id );
}
/**
* Used to construct insert query recursively.
*
* @param array $inserts Array of data to insert.
* @param array $terms Terms to insert.
* @param array $parents Parent IDs the terms belong to.
*/
protected function get_term_insert_values( &$inserts, $terms, $parents = array() ) {
foreach ( $terms as $term ) {
$insert_parents = array_merge( array( $term['term_id'] ), $parents );
foreach ( $insert_parents as $parent ) {
$inserts[] = array(
$parent,
$term['term_id'],
);
}
$this->get_term_insert_values( $inserts, $term['descendants'], $insert_parents );
}
}
/**
* Convert flat terms array into nested array.
*
* @param array $hierarchy Array to put terms into.
* @param array $terms Array of terms (id=>parent).
* @param integer $parent Parent ID.
*/
protected function unflatten_terms( &$hierarchy, &$terms, $parent = 0 ) {
foreach ( $terms as $term_id => $parent_id ) {
if ( (int) $parent_id === $parent ) {
$hierarchy[ $term_id ] = array(
'term_id' => $term_id,
'descendants' => array(),
);
unset( $terms[ $term_id ] );
}
}
foreach ( $hierarchy as $term_id => $terms_array ) {
$this->unflatten_terms( $hierarchy[ $term_id ]['descendants'], $terms, $term_id );
}
}
/**
* Get category descendants.
*
* @param int $category_id The category ID to lookup.
* @return array
*/
protected function get_descendants( $category_id ) {
global $wpdb;
return wp_parse_id_list(
$wpdb->get_col(
$wpdb->prepare(
"SELECT category_id FROM $wpdb->wc_category_lookup WHERE category_tree_id = %d",
$category_id
)
)
);
}
/**
* Return all ancestor category ids for a category.
*
* @param int $category_id The category ID to lookup.
* @return array
*/
protected function get_ancestors( $category_id ) {
global $wpdb;
return wp_parse_id_list(
$wpdb->get_col(
$wpdb->prepare(
"SELECT category_tree_id FROM $wpdb->wc_category_lookup WHERE category_id = %d",
$category_id
)
)
);
}
/**
* Add category lookup table to $wpdb object.
*/
public static function define_category_lookup_tables_in_wpdb() {
global $wpdb;
// List of tables without prefixes.
$tables = array(
'wc_category_lookup' => 'wc_category_lookup',
);
foreach ( $tables as $name => $table ) {
$wpdb->$name = $wpdb->prefix . $table;
$wpdb->tables[] = $table;
}
}
}
Coupons.php 0000644 00000006153 15154512350 0006711 0 ustar 00 <?php
/**
* WooCommerce Marketing > Coupons.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Internal\Admin\Notes\CouponPageMoved;
use Automattic\WooCommerce\Admin\PageController;
/**
* Contains backend logic for the Coupons feature.
*/
class Coupons {
use CouponsMovedTrait;
/**
* Class instance.
*
* @var Coupons instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WooCommerce.
*/
public function __construct() {
if ( ! is_admin() ) {
return;
}
// If the main marketing feature is disabled, don't modify coupon behavior.
if ( ! Features::is_enabled( 'marketing' ) ) {
return;
}
// Only support coupon modifications if coupons are enabled.
if ( ! wc_coupons_enabled() ) {
return;
}
( new CouponPageMoved() )->init();
add_action( 'admin_enqueue_scripts', array( $this, 'maybe_add_marketing_coupon_script' ) );
add_action( 'woocommerce_register_post_type_shop_coupon', array( $this, 'move_coupons' ) );
add_action( 'admin_head', array( $this, 'fix_coupon_menu_highlight' ), 99 );
add_action( 'admin_menu', array( $this, 'maybe_add_coupon_menu_redirect' ) );
}
/**
* Maybe add menu item back in original spot to help people transition
*/
public function maybe_add_coupon_menu_redirect() {
if ( ! $this->should_display_legacy_menu() ) {
return;
}
add_submenu_page(
'woocommerce',
__( 'Coupons', 'woocommerce' ),
__( 'Coupons', 'woocommerce' ),
'manage_options',
'coupons-moved',
[ $this, 'coupon_menu_moved' ]
);
}
/**
* Call back for transition menu item
*/
public function coupon_menu_moved() {
wp_safe_redirect( $this->get_legacy_coupon_url(), 301 );
exit();
}
/**
* Modify registered post type shop_coupon
*
* @param array $args Array of post type parameters.
*
* @return array the filtered parameters.
*/
public function move_coupons( $args ) {
$args['show_in_menu'] = current_user_can( 'manage_woocommerce' ) ? 'woocommerce-marketing' : true;
return $args;
}
/**
* Undo WC modifications to $parent_file for 'shop_coupon'
*/
public function fix_coupon_menu_highlight() {
global $parent_file, $post_type;
if ( $post_type === 'shop_coupon' ) {
$parent_file = 'woocommerce-marketing'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
}
}
/**
* Maybe add our wc-admin coupon scripts if viewing coupon pages
*/
public function maybe_add_marketing_coupon_script() {
$curent_screen = PageController::get_instance()->get_current_page();
if ( ! isset( $curent_screen['id'] ) || $curent_screen['id'] !== 'woocommerce-coupons' ) {
return;
}
$rtl = is_rtl() ? '-rtl' : '';
wp_enqueue_style(
'wc-admin-marketing-coupons',
WCAdminAssets::get_url( "marketing-coupons/style{$rtl}", 'css' ),
array(),
WCAdminAssets::get_file_version( 'css' )
);
WCAdminAssets::register_script( 'wp-admin-scripts', 'marketing-coupons', true );
}
}
CouponsMovedTrait.php 0000644 00000004233 15154512351 0010706 0 ustar 00 <?php
/**
* A Trait to help with managing the legacy coupon menu.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\Features\Features;
/**
* CouponsMovedTrait trait.
*/
trait CouponsMovedTrait {
/**
* The GET query key for the legacy menu.
*
* @var string
*/
protected static $query_key = 'legacy_coupon_menu';
/**
* The key for storing an option in the DB.
*
* @var string
*/
protected static $option_key = 'wc_admin_show_legacy_coupon_menu';
/**
* Get the URL for the legacy coupon management.
*
* @return string The unescaped URL for the legacy coupon management page.
*/
protected static function get_legacy_coupon_url() {
return self::get_coupon_url( [ self::$query_key => true ] );
}
/**
* Get the URL for the coupon management page.
*
* @param array $args Additional URL query arguments.
*
* @return string
*/
protected static function get_coupon_url( $args = [] ) {
$args = array_merge(
[
'post_type' => 'shop_coupon',
],
$args
);
return add_query_arg( $args, admin_url( 'edit.php' ) );
}
/**
* Get the new URL for managing coupons.
*
* @param string $page The management page.
*
* @return string
*/
protected static function get_management_url( $page ) {
$path = '';
switch ( $page ) {
case 'coupon':
case 'coupons':
return self::get_coupon_url();
case 'marketing':
$path = self::get_marketing_path();
break;
}
return "wc-admin&path={$path}";
}
/**
* Get the WC Admin path for the marking page.
*
* @return string
*/
protected static function get_marketing_path() {
return '/marketing/overview';
}
/**
* Whether we should display the legacy coupon menu item.
*
* @return bool
*/
protected static function should_display_legacy_menu() {
return ( get_option( self::$option_key, 1 ) && ! Features::is_enabled( 'navigation' ) );
}
/**
* Set whether we should display the legacy coupon menu item.
*
* @param bool $display Whether the menu should be displayed or not.
*/
protected static function display_legacy_menu( $display = false ) {
update_option( self::$option_key, $display ? 1 : 0 );
}
}
CustomerEffortScoreTracks.php 0000644 00000042273 15154512351 0012402 0 ustar 00 <?php
/**
* WooCommerce Customer effort score tracks
*
* @package WooCommerce\Admin\Features
*/
namespace Automattic\WooCommerce\Internal\Admin;
defined( 'ABSPATH' ) || exit;
/**
* Triggers customer effort score on several different actions.
*/
class CustomerEffortScoreTracks {
/**
* Option name for the CES Tracks queue.
*/
const CES_TRACKS_QUEUE_OPTION_NAME = 'woocommerce_ces_tracks_queue';
/**
* Option name for the clear CES Tracks queue for page.
*/
const CLEAR_CES_TRACKS_QUEUE_FOR_PAGE_OPTION_NAME =
'woocommerce_clear_ces_tracks_queue_for_page';
/**
* Option name for the set of actions that have been shown.
*/
const SHOWN_FOR_ACTIONS_OPTION_NAME = 'woocommerce_ces_shown_for_actions';
/**
* Action name for product add/publish.
*/
const PRODUCT_ADD_PUBLISH_ACTION_NAME = 'product_add_publish';
/**
* Action name for product update.
*/
const PRODUCT_UPDATE_ACTION_NAME = 'product_update';
/**
* Action name for shop order update.
*/
const SHOP_ORDER_UPDATE_ACTION_NAME = 'shop_order_update';
/**
* Action name for settings change.
*/
const SETTINGS_CHANGE_ACTION_NAME = 'settings_change';
/**
* Action name for add product categories.
*/
const ADD_PRODUCT_CATEGORIES_ACTION_NAME = 'add_product_categories';
/**
* Action name for add product tags.
*/
const ADD_PRODUCT_TAGS_ACTION_NAME = 'add_product_tags';
/*
* Action name for add product attributes.
*/
const ADD_PRODUCT_ATTRIBUTES_ACTION_NAME = 'add_product_attributes';
/**
* Action name for import products.
*/
const IMPORT_PRODUCTS_ACTION_NAME = 'import_products';
/**
* Action name for search.
*/
const SEARCH_ACTION_NAME = 'ces_search';
/**
* Label for the snackbar that appears when a user submits the survey.
*
* @var string
*/
private $onsubmit_label;
/**
* Constructor. Sets up filters to hook into WooCommerce.
*/
public function __construct() {
$this->enable_survey_enqueing_if_tracking_is_enabled();
}
/**
* Add actions that require woocommerce_allow_tracking.
*/
private function enable_survey_enqueing_if_tracking_is_enabled() {
// Only hook up the action handlers if in wp-admin.
if ( ! is_admin() ) {
return;
}
// Do not hook up the action handlers if a mobile device is used.
if ( wp_is_mobile() ) {
return;
}
// Only enqueue a survey if tracking is allowed.
$allow_tracking = 'yes' === get_option( 'woocommerce_allow_tracking', 'no' );
if ( ! $allow_tracking ) {
return;
}
add_action( 'admin_init', array( $this, 'maybe_clear_ces_tracks_queue' ) );
add_action( 'woocommerce_update_options', array( $this, 'run_on_update_options' ), 10, 3 );
add_action( 'product_cat_add_form', array( $this, 'add_script_track_product_categories' ), 10, 3 );
add_action( 'product_tag_add_form', array( $this, 'add_script_track_product_tags' ), 10, 3 );
add_action( 'woocommerce_attribute_added', array( $this, 'run_on_add_product_attributes' ), 10, 3 );
add_action( 'load-edit.php', array( $this, 'run_on_load_edit_php' ), 10, 3 );
add_action( 'product_page_product_importer', array( $this, 'run_on_product_import' ), 10, 3 );
// Only hook up the transition_post_status action handler
// if on the edit page.
global $pagenow;
if ( 'post.php' === $pagenow ) {
add_action(
'transition_post_status',
array(
$this,
'run_on_transition_post_status',
),
10,
3
);
}
$this->onsubmit_label = __( 'Thank you for your feedback!', 'woocommerce' );
}
/**
* Returns a generated script for tracking tags added on edit-tags.php page.
* CES survey is triggered via direct access to wc/customer-effort-score store
* via wp.data.dispatch method.
*
* Due to lack of options to directly hook ourselves into the ajax post request
* initiated by edit-tags.php page, we infer a successful request by observing
* an increase of the number of rows in tags table
*
* @param string $action Action name for the survey.
* @param string $title Title for the snackbar.
* @param string $first_question The text for the first question.
* @param string $second_question The text for the second question.
*
* @return string Generated JavaScript to append to page.
*/
private function get_script_track_edit_php( $action, $title, $first_question, $second_question ) {
return sprintf(
"(function( $ ) {
'use strict';
// Hook on submit button and sets a 500ms interval function
// to determine successful add tag or otherwise.
$('#addtag #submit').on( 'click', function() {
const initialCount = $('.tags tbody > tr').length;
const interval = setInterval( function() {
if ( $('.tags tbody > tr').length > initialCount ) {
// New tag detected.
clearInterval( interval );
wp.data.dispatch('wc/customer-effort-score').addCesSurvey({ action: '%s', title: '%s', firstQuestion: '%s', secondQuestion: '%s', onsubmitLabel: '%s' });
} else {
// Form is no longer loading, most likely failed.
if ( $( '#addtag .submit .spinner.is-active' ).length < 1 ) {
clearInterval( interval );
}
}
}, 500 );
});
})( jQuery );",
esc_js( $action ),
esc_js( $title ),
esc_js( $first_question ),
esc_js( $second_question ),
esc_js( $this->onsubmit_label )
);
}
/**
* Get the current published product count.
*
* @return integer The current published product count.
*/
private function get_product_count() {
$query = new \WC_Product_Query(
array(
'limit' => 1,
'paginate' => true,
'return' => 'ids',
'status' => array( 'publish' ),
)
);
$products = $query->get_products();
$product_count = intval( $products->total );
return $product_count;
}
/**
* Get the current shop order count.
*
* @return integer The current shop order count.
*/
private function get_shop_order_count() {
$query = new \WC_Order_Query(
array(
'limit' => 1,
'paginate' => true,
'return' => 'ids',
)
);
$shop_orders = $query->get_orders();
$shop_order_count = intval( $shop_orders->total );
return $shop_order_count;
}
/**
* Return whether the action has already been shown.
*
* @param string $action The action to check.
*
* @return bool Whether the action has already been shown.
*/
private function has_been_shown( $action ) {
$shown_for_features = get_option( self::SHOWN_FOR_ACTIONS_OPTION_NAME, array() );
$has_been_shown = in_array( $action, $shown_for_features, true );
return $has_been_shown;
}
/**
* Enqueue the item to the CES tracks queue.
*
* @param array $item The item to enqueue.
*/
private function enqueue_to_ces_tracks( $item ) {
$queue = get_option(
self::CES_TRACKS_QUEUE_OPTION_NAME,
array()
);
$has_duplicate = array_filter(
$queue,
function ( $queue_item ) use ( $item ) {
return $queue_item['action'] === $item['action'];
}
);
if ( $has_duplicate ) {
return;
}
$queue[] = $item;
update_option(
self::CES_TRACKS_QUEUE_OPTION_NAME,
$queue
);
}
/**
* Enqueue the CES survey on using search dynamically.
*
* @param string $search_area Search area such as "product" or "shop_order".
* @param string $page_now Value of window.pagenow.
* @param string $admin_page Value of window.adminpage.
*/
public function enqueue_ces_survey_for_search( $search_area, $page_now, $admin_page ) {
if ( $this->has_been_shown( self::SEARCH_ACTION_NAME ) ) {
return;
}
$this->enqueue_to_ces_tracks(
array(
'action' => self::SEARCH_ACTION_NAME,
'title' => __(
'How easy was it to use search?',
'woocommerce'
),
'firstQuestion' => __(
'The search feature in WooCommerce is easy to use.',
'woocommerce'
),
'secondQuestion' => __(
'The search\'s functionality meets my needs.',
'woocommerce'
),
'onsubmit_label' => $this->onsubmit_label,
'pagenow' => $page_now,
'adminpage' => $admin_page,
'props' => (object) array(
'search_area' => $search_area,
),
)
);
}
/**
* Hook into the post status lifecycle, to detect relevant user actions
* that we want to survey about.
*
* @param string $new_status The new status.
* @param string $old_status The old status.
* @param Post $post The post.
*/
public function run_on_transition_post_status(
$new_status,
$old_status,
$post
) {
if ( 'product' === $post->post_type ) {
$this->maybe_enqueue_ces_survey_for_product( $new_status, $old_status );
} elseif ( 'shop_order' === $post->post_type ) {
$this->enqueue_ces_survey_for_edited_shop_order();
}
}
/**
* Maybe enqueue the CES survey, if product is being added or edited.
*
* @param string $new_status The new status.
* @param string $old_status The old status.
*/
private function maybe_enqueue_ces_survey_for_product(
$new_status,
$old_status
) {
if ( 'publish' !== $new_status ) {
return;
}
if ( 'publish' !== $old_status ) {
$this->enqueue_ces_survey_for_new_product();
} else {
$this->enqueue_ces_survey_for_edited_product();
}
}
/**
* Enqueue the CES survey trigger for a new product.
*/
private function enqueue_ces_survey_for_new_product() {
if ( $this->has_been_shown( self::PRODUCT_ADD_PUBLISH_ACTION_NAME ) ) {
return;
}
$this->enqueue_to_ces_tracks(
array(
'action' => self::PRODUCT_ADD_PUBLISH_ACTION_NAME,
'title' => __(
'How easy was it to add a product?',
'woocommerce'
),
'firstQuestion' => __(
'The product creation screen is easy to use.',
'woocommerce'
),
'secondQuestion' => __(
'The product creation screen\'s functionality meets my needs.',
'woocommerce'
),
'onsubmit_label' => $this->onsubmit_label,
'pagenow' => 'product',
'adminpage' => 'post-php',
'props' => array(
'product_count' => $this->get_product_count(),
),
)
);
}
/**
* Enqueue the CES survey trigger for an existing product.
*/
private function enqueue_ces_survey_for_edited_product() {
if ( $this->has_been_shown( self::PRODUCT_UPDATE_ACTION_NAME ) ) {
return;
}
$this->enqueue_to_ces_tracks(
array(
'action' => self::PRODUCT_UPDATE_ACTION_NAME,
'title' => __(
'How easy was it to edit your product?',
'woocommerce'
),
'firstQuestion' => __(
'The product update process is easy to complete.',
'woocommerce'
),
'secondQuestion' => __(
'The product update process meets my needs.',
'woocommerce'
),
'onsubmit_label' => $this->onsubmit_label,
'pagenow' => 'product',
'adminpage' => 'post-php',
'props' => array(
'product_count' => $this->get_product_count(),
),
)
);
}
/**
* Enqueue the CES survey trigger for an existing shop order.
*/
private function enqueue_ces_survey_for_edited_shop_order() {
if ( $this->has_been_shown( self::SHOP_ORDER_UPDATE_ACTION_NAME ) ) {
return;
}
$this->enqueue_to_ces_tracks(
array(
'action' => self::SHOP_ORDER_UPDATE_ACTION_NAME,
'title' => __(
'How easy was it to update an order?',
'woocommerce'
),
'firstQuestion' => __(
'The order details screen is easy to use.',
'woocommerce'
),
'secondQuestion' => __(
'The order details screen\'s functionality meets my needs.',
'woocommerce'
),
'onsubmit_label' => $this->onsubmit_label,
'pagenow' => 'shop_order',
'adminpage' => 'post-php',
'props' => array(
'order_count' => $this->get_shop_order_count(),
),
)
);
}
/**
* Maybe clear the CES tracks queue, executed on every page load. If the
* clear option is set it clears the queue. In practice, this executes a
* page load after the queued CES tracks are displayed on the client, which
* sets the clear option.
*/
public function maybe_clear_ces_tracks_queue() {
$clear_ces_tracks_queue_for_page = get_option(
self::CLEAR_CES_TRACKS_QUEUE_FOR_PAGE_OPTION_NAME,
false
);
if ( ! $clear_ces_tracks_queue_for_page ) {
return;
}
$queue = get_option(
self::CES_TRACKS_QUEUE_OPTION_NAME,
array()
);
$remaining_items = array_filter(
$queue,
function ( $item ) use ( $clear_ces_tracks_queue_for_page ) {
return $clear_ces_tracks_queue_for_page['pagenow'] !== $item['pagenow']
|| $clear_ces_tracks_queue_for_page['adminpage'] !== $item['adminpage'];
}
);
update_option(
self::CES_TRACKS_QUEUE_OPTION_NAME,
array_values( $remaining_items )
);
update_option( self::CLEAR_CES_TRACKS_QUEUE_FOR_PAGE_OPTION_NAME, false );
}
/**
* Appends a script to footer to trigger CES on adding product categories.
*/
public function add_script_track_product_categories() {
if ( $this->has_been_shown( self::ADD_PRODUCT_CATEGORIES_ACTION_NAME ) ) {
return;
}
wc_enqueue_js(
$this->get_script_track_edit_php(
self::ADD_PRODUCT_CATEGORIES_ACTION_NAME,
__( 'How easy was it to add product category?', 'woocommerce' ),
__( 'The product category details screen is easy to use.', 'woocommerce' ),
__( "The product category details screen's functionality meets my needs.", 'woocommerce' )
)
);
}
/**
* Appends a script to footer to trigger CES on adding product tags.
*/
public function add_script_track_product_tags() {
if ( $this->has_been_shown( self::ADD_PRODUCT_TAGS_ACTION_NAME ) ) {
return;
}
wc_enqueue_js(
$this->get_script_track_edit_php(
self::ADD_PRODUCT_TAGS_ACTION_NAME,
__( 'How easy was it to add a product tag?', 'woocommerce' ),
__( 'The product tag details screen is easy to use.', 'woocommerce' ),
__( "The product tag details screen's functionality meets my needs.", 'woocommerce' )
)
);
}
/**
* Maybe enqueue the CES survey on product import, if step is done.
*/
public function run_on_product_import() {
// We're only interested in when the importer completes.
if ( empty( $_GET['step'] ) || 'done' !== $_GET['step'] ) { // phpcs:ignore CSRF ok.
return;
}
if ( $this->has_been_shown( self::IMPORT_PRODUCTS_ACTION_NAME ) ) {
return;
}
$this->enqueue_to_ces_tracks(
array(
'action' => self::IMPORT_PRODUCTS_ACTION_NAME,
'title' => __(
'How easy was it to import products?',
'woocommerce'
),
'firstQuestion' => __(
'The product import process is easy to complete.',
'woocommerce'
),
'secondQuestion' => __(
'The product import process meets my needs.',
'woocommerce'
),
'onsubmit_label' => $this->onsubmit_label,
'pagenow' => 'product_page_product_importer',
'adminpage' => 'product_page_product_importer',
'props' => (object) array(),
)
);
}
/**
* Enqueue the CES survey trigger for setting changes.
*/
public function run_on_update_options() {
// $current_tab is set when WC_Admin_Settings::save_settings is called.
global $current_tab;
global $current_section;
if ( $this->has_been_shown( self::SETTINGS_CHANGE_ACTION_NAME ) ) {
return;
}
$props = array(
'settings_area' => $current_tab,
);
if ( $current_section ) {
$props['settings_section'] = $current_section;
}
$this->enqueue_to_ces_tracks(
array(
'action' => self::SETTINGS_CHANGE_ACTION_NAME,
'title' => __(
'How easy was it to update your settings?',
'woocommerce'
),
'firstQuestion' => __(
'The settings screen is easy to use.',
'woocommerce'
),
'secondQuestion' => __(
'The settings screen\'s functionality meets my needs.',
'woocommerce'
),
'onsubmit_label' => $this->onsubmit_label,
'pagenow' => 'woocommerce_page_wc-settings',
'adminpage' => 'woocommerce_page_wc-settings',
'props' => (object) $props,
)
);
}
/**
* Enqueue the CES survey on adding new product attributes.
*/
public function run_on_add_product_attributes() {
if ( $this->has_been_shown( self::ADD_PRODUCT_ATTRIBUTES_ACTION_NAME ) ) {
return;
}
$this->enqueue_to_ces_tracks(
array(
'action' => self::ADD_PRODUCT_ATTRIBUTES_ACTION_NAME,
'title' => __(
'How easy was it to add a product attribute?',
'woocommerce'
),
'firstQuestion' => __(
'Product attributes are easy to use.',
'woocommerce'
),
'secondQuestion' => __(
'Product attributes\' functionality meets my needs.',
'woocommerce'
),
'onsubmit_label' => $this->onsubmit_label,
'pagenow' => 'product_page_product_attributes',
'adminpage' => 'product_page_product_attributes',
'props' => (object) array(),
)
);
}
/**
* Determine on initiating CES survey on searching for product or orders.
*/
public function run_on_load_edit_php() {
$allowed_types = array( 'product', 'shop_order' );
$post_type = get_current_screen()->post_type;
// We're only interested for certain post types.
if ( ! in_array( $post_type, $allowed_types, true ) ) {
return;
}
// Determine whether request is search by "s" GET parameter.
if ( empty( $_GET['s'] ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended
return;
}
$page_now = 'edit-' . $post_type;
$this->enqueue_ces_survey_for_search( $post_type, $page_now, 'edit-php' );
}
}
Events.php 0000644 00000021674 15154512351 0006535 0 ustar 00 <?php
/**
* Handle cron events.
*/
namespace Automattic\WooCommerce\Internal\Admin;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\DataSourcePoller;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RemoteInboxNotificationsEngine;
use Automattic\WooCommerce\Internal\Admin\Notes\AddFirstProduct;
use Automattic\WooCommerce\Internal\Admin\Notes\ChoosingTheme;
use Automattic\WooCommerce\Internal\Admin\Notes\CouponPageMoved;
use Automattic\WooCommerce\Internal\Admin\Notes\CustomizeStoreWithBlocks;
use Automattic\WooCommerce\Internal\Admin\Notes\CustomizingProductCatalog;
use Automattic\WooCommerce\Internal\Admin\Notes\EditProductsOnTheMove;
use Automattic\WooCommerce\Internal\Admin\Notes\EUVATNumber;
use Automattic\WooCommerce\Internal\Admin\Notes\FirstProduct;
use Automattic\WooCommerce\Internal\Admin\Notes\InstallJPAndWCSPlugins;
use Automattic\WooCommerce\Internal\Admin\Notes\LaunchChecklist;
use Automattic\WooCommerce\Internal\Admin\Notes\MagentoMigration;
use Automattic\WooCommerce\Internal\Admin\Notes\ManageOrdersOnTheGo;
use Automattic\WooCommerce\Internal\Admin\Notes\MarketingJetpack;
use Automattic\WooCommerce\Internal\Admin\Notes\MerchantEmailNotifications;
use Automattic\WooCommerce\Internal\Admin\Notes\MigrateFromShopify;
use Automattic\WooCommerce\Internal\Admin\Notes\MobileApp;
use Automattic\WooCommerce\Internal\Admin\Notes\NewSalesRecord;
use Automattic\WooCommerce\Internal\Admin\Notes\OnboardingPayments;
use Automattic\WooCommerce\Internal\Admin\Notes\OnlineClothingStore;
use Automattic\WooCommerce\Internal\Admin\Notes\OrderMilestones;
use Automattic\WooCommerce\Internal\Admin\Notes\PaymentsMoreInfoNeeded;
use Automattic\WooCommerce\Internal\Admin\Notes\PaymentsRemindMeLater;
use Automattic\WooCommerce\Internal\Admin\Notes\PerformanceOnMobile;
use Automattic\WooCommerce\Internal\Admin\Notes\PersonalizeStore;
use Automattic\WooCommerce\Internal\Admin\Notes\RealTimeOrderAlerts;
use Automattic\WooCommerce\Internal\Admin\Notes\SellingOnlineCourses;
use Automattic\WooCommerce\Internal\Admin\Notes\TestCheckout;
use Automattic\WooCommerce\Internal\Admin\Notes\TrackingOptIn;
use Automattic\WooCommerce\Internal\Admin\Notes\UnsecuredReportFiles;
use Automattic\WooCommerce\Internal\Admin\Notes\WooCommercePayments;
use Automattic\WooCommerce\Internal\Admin\Notes\WooCommerceSubscriptions;
use Automattic\WooCommerce\Internal\Admin\Notes\WooSubscriptionsNotes;
use Automattic\WooCommerce\Internal\Admin\Schedulers\MailchimpScheduler;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\PaymentGatewaySuggestionsDataSourcePoller;
use Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\RemoteFreeExtensionsDataSourcePoller;
/**
* Events Class.
*/
class Events {
/**
* The single instance of the class.
*
* @var object
*/
protected static $instance = null;
/**
* Constructor
*
* @return void
*/
protected function __construct() {}
/**
* Array of note class to be added or updated.
*
* @var array
*/
private static $note_classes_to_added_or_updated = array(
AddFirstProduct::class,
ChoosingTheme::class,
CustomizeStoreWithBlocks::class,
CustomizingProductCatalog::class,
EditProductsOnTheMove::class,
EUVATNumber::class,
FirstProduct::class,
LaunchChecklist::class,
MagentoMigration::class,
ManageOrdersOnTheGo::class,
MarketingJetpack::class,
MigrateFromShopify::class,
MobileApp::class,
NewSalesRecord::class,
OnboardingPayments::class,
OnlineClothingStore::class,
PaymentsMoreInfoNeeded::class,
PaymentsRemindMeLater::class,
PerformanceOnMobile::class,
PersonalizeStore::class,
RealTimeOrderAlerts::class,
TestCheckout::class,
TrackingOptIn::class,
WooCommercePayments::class,
WooCommerceSubscriptions::class,
);
/**
* The other note classes that are added in other places.
*
* @var array
*/
private static $other_note_classes = array(
CouponPageMoved::class,
InstallJPAndWCSPlugins::class,
OrderMilestones::class,
SellingOnlineCourses::class,
UnsecuredReportFiles::class,
WooSubscriptionsNotes::class,
);
/**
* Get class instance.
*
* @return object Instance.
*/
final public static function instance() {
if ( null === static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Cron event handlers.
*/
public function init() {
add_action( 'wc_admin_daily', array( $this, 'do_wc_admin_daily' ) );
add_filter( 'woocommerce_get_note_from_db', array( $this, 'get_note_from_db' ), 10, 1 );
// Initialize the WC_Notes_Refund_Returns Note to attach hook.
\WC_Notes_Refund_Returns::init();
}
/**
* Daily events to run.
*
* Note: Order_Milestones::possibly_add_note is hooked to this as well.
*/
public function do_wc_admin_daily() {
$this->possibly_add_notes();
$this->possibly_delete_notes();
$this->possibly_update_notes();
$this->possibly_refresh_data_source_pollers();
if ( $this->is_remote_inbox_notifications_enabled() ) {
DataSourcePoller::get_instance()->read_specs_from_data_sources();
RemoteInboxNotificationsEngine::run();
}
if ( $this->is_merchant_email_notifications_enabled() ) {
MerchantEmailNotifications::run();
}
if ( Features::is_enabled( 'onboarding' ) ) {
( new MailchimpScheduler() )->run();
}
}
/**
* Get note.
*
* @param Note $note_from_db The note object from the database.
*/
public function get_note_from_db( $note_from_db ) {
if ( ! $note_from_db instanceof Note || get_user_locale() === $note_from_db->get_locale() ) {
return $note_from_db;
}
$note_classes = array_merge( self::$note_classes_to_added_or_updated, self::$other_note_classes );
foreach ( $note_classes as $note_class ) {
if ( defined( "$note_class::NOTE_NAME" ) && $note_class::NOTE_NAME === $note_from_db->get_name() ) {
$note_from_class = method_exists( $note_class, 'get_note' ) ? $note_class::get_note() : null;
if ( $note_from_class instanceof Note ) {
$note = clone $note_from_db;
$note->set_title( $note_from_class->get_title() );
$note->set_content( $note_from_class->get_content() );
$actions = $note_from_class->get_actions();
foreach ( $actions as $action ) {
$matching_action = $note->get_action( $action->name );
if ( $matching_action && $matching_action->id ) {
$action->id = $matching_action->id;
}
}
$note->set_actions( $actions );
return $note;
}
break;
}
}
return $note_from_db;
}
/**
* Adds notes that should be added.
*/
protected function possibly_add_notes() {
foreach ( self::$note_classes_to_added_or_updated as $note_class ) {
if ( method_exists( $note_class, 'possibly_add_note' ) ) {
$note_class::possibly_add_note();
}
}
}
/**
* Deletes notes that should be deleted.
*/
protected function possibly_delete_notes() {
PaymentsRemindMeLater::delete_if_not_applicable();
PaymentsMoreInfoNeeded::delete_if_not_applicable();
}
/**
* Updates notes that should be updated.
*/
protected function possibly_update_notes() {
foreach ( self::$note_classes_to_added_or_updated as $note_class ) {
if ( method_exists( $note_class, 'possibly_update_note' ) ) {
$note_class::possibly_update_note();
}
}
}
/**
* Checks if remote inbox notifications are enabled.
*
* @return bool Whether remote inbox notifications are enabled.
*/
protected function is_remote_inbox_notifications_enabled() {
// Check if the feature flag is disabled.
if ( ! Features::is_enabled( 'remote-inbox-notifications' ) ) {
return false;
}
// Check if the site has opted out of marketplace suggestions.
if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) !== 'yes' ) {
return false;
}
// All checks have passed.
return true;
}
/**
* Checks if merchant email notifications are enabled.
*
* @return bool Whether merchant email notifications are enabled.
*/
protected function is_merchant_email_notifications_enabled() {
// Check if the feature flag is disabled.
if ( get_option( 'woocommerce_merchant_email_notifications', 'no' ) !== 'yes' ) {
return false;
}
// All checks have passed.
return true;
}
/**
* Refresh transient for the following DataSourcePollers on wc_admin_daily cron job.
* - PaymentGatewaySuggestionsDataSourcePoller
* - RemoteFreeExtensionsDataSourcePoller
*/
protected function possibly_refresh_data_source_pollers() {
$completed_tasks = get_option( 'woocommerce_task_list_tracked_completed_tasks', array() );
if ( ! in_array( 'payments', $completed_tasks, true ) && ! in_array( 'woocommerce-payments', $completed_tasks, true ) ) {
PaymentGatewaySuggestionsDataSourcePoller::get_instance()->read_specs_from_data_sources();
}
if ( ! in_array( 'store_details', $completed_tasks, true ) && ! in_array( 'marketing', $completed_tasks, true ) ) {
RemoteFreeExtensionsDataSourcePoller::get_instance()->read_specs_from_data_sources();
}
}
}
Homescreen.php 0000644 00000020505 15154512351 0007351 0 ustar 00 <?php
/**
* WooCommerce Homescreen.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\Shipping;
/**
* Contains backend logic for the homescreen feature.
*/
class Homescreen {
/**
* Menu slug.
*/
const MENU_SLUG = 'wc-admin';
/**
* Class instance.
*
* @var Homescreen instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WooCommerce.
*/
public function __construct() {
add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) );
add_action( 'admin_menu', array( $this, 'register_page' ) );
// In WC Core 5.1 $submenu manipulation occurs in admin_menu, not admin_head. See https://github.com/woocommerce/woocommerce/pull/29088.
if ( version_compare( WC_VERSION, '5.1', '>=' ) ) {
// priority is 20 to run after admin_menu hook for woocommerce runs, so that submenu is populated.
add_action( 'admin_menu', array( $this, 'possibly_remove_woocommerce_menu' ) );
add_action( 'admin_menu', array( $this, 'update_link_structure' ), 20 );
} else {
// priority is 20 to run after https://github.com/woocommerce/woocommerce/blob/a55ae325306fc2179149ba9b97e66f32f84fdd9c/includes/admin/class-wc-admin-menus.php#L165.
add_action( 'admin_head', array( $this, 'update_link_structure' ), 20 );
}
add_filter( 'woocommerce_admin_preload_options', array( $this, 'preload_options' ) );
if ( Features::is_enabled( 'shipping-smart-defaults' ) ) {
add_filter(
'woocommerce_admin_shared_settings',
array( $this, 'maybe_set_default_shipping_options_on_home' ),
9999
);
}
}
/**
* Set free shipping in the same country as the store default
* Flag rate in all other countries when any of the following conditions are ture
*
* - The store sells physical products, has JP and WCS installed and connected, and is located in the US.
* - The store sells physical products, and is not located in US/Canada/Australia/UK (irrelevant if JP is installed or not).
* - The store sells physical products and is located in US, but JP and WCS are not installed.
*
* @param array $settings shared admin settings.
* @return array
*/
public function maybe_set_default_shipping_options_on_home( $settings ) {
if ( ! function_exists( 'get_current_screen' ) ) {
return $settings;
}
$current_screen = get_current_screen();
// Abort if it's not the homescreen.
if ( ! isset( $current_screen->id ) || 'woocommerce_page_wc-admin' !== $current_screen->id ) {
return $settings;
}
// Abort if we already created the shipping options.
$already_created = get_option( 'woocommerce_admin_created_default_shipping_zones' );
if ( $already_created === 'yes' ) {
return $settings;
}
$zone_count = count( \WC_Data_Store::load( 'shipping-zone' )->get_zones() );
if ( $zone_count ) {
update_option( 'woocommerce_admin_created_default_shipping_zones', 'yes' );
update_option( 'woocommerce_admin_reviewed_default_shipping_zones', 'yes' );
return $settings;
}
$user_skipped_obw = $settings['onboarding']['profile']['skipped'] ?? false;
$store_address = $settings['preloadSettings']['general']['woocommerce_store_address'] ?? '';
$product_types = $settings['onboarding']['profile']['product_types'] ?? array();
$user_has_set_store_country = $settings['onboarding']['profile']['is_store_country_set'] ?? false;
// Do not proceed if user has not filled out their country in the onboarding profiler.
if ( ! $user_has_set_store_country ) {
return $settings;
}
// If user skipped the obw or has not completed the store_details
// then we assume the user is going to sell physical products.
if ( $user_skipped_obw || '' === $store_address ) {
$product_types[] = 'physical';
}
if ( false === in_array( 'physical', $product_types, true ) ) {
return $settings;
}
$country_code = wc_format_country_state_string( $settings['preloadSettings']['general']['woocommerce_default_country'] )['country'];
$country_name = WC()->countries->get_countries()[ $country_code ] ?? null;
$is_jetpack_installed = in_array( 'jetpack', $settings['plugins']['installedPlugins'] ?? array(), true );
$is_wcs_installed = in_array( 'woocommerce-services', $settings['plugins']['installedPlugins'] ?? array(), true );
if (
( 'US' === $country_code && $is_jetpack_installed )
||
( ! in_array( $country_code, array( 'CA', 'AU', 'GB', 'ES', 'IT', 'DE', 'FR', 'MX', 'CO', 'CL', 'AR', 'PE', 'BR', 'UY', 'GT', 'NL', 'AT', 'BE' ), true ) )
||
( 'US' === $country_code && false === $is_jetpack_installed && false === $is_wcs_installed )
) {
$zone = new \WC_Shipping_Zone();
$zone->set_zone_name( $country_name );
$zone->add_location( $country_code, 'country' );
$zone->add_shipping_method( 'free_shipping' );
update_option( 'woocommerce_admin_created_default_shipping_zones', 'yes' );
Shipping::delete_zone_count_transient();
}
return $settings;
}
/**
* Adds fields so that we can store performance indicators, row settings, and chart type settings for users.
*
* @param array $user_data_fields User data fields.
* @return array
*/
public function add_user_data_fields( $user_data_fields ) {
return array_merge(
$user_data_fields,
array(
'homepage_layout',
'homepage_stats',
'task_list_tracked_started_tasks',
'help_panel_highlight_shown',
)
);
}
/**
* Registers home page.
*/
public function register_page() {
// Register a top-level item for users who cannot view the core WooCommerce menu.
if ( ! self::is_admin_user() ) {
wc_admin_register_page(
array(
'id' => 'woocommerce-home',
'title' => __( 'WooCommerce', 'woocommerce' ),
'path' => self::MENU_SLUG,
'capability' => 'read',
)
);
return;
}
wc_admin_register_page(
array(
'id' => 'woocommerce-home',
'title' => __( 'Home', 'woocommerce' ),
'parent' => 'woocommerce',
'path' => self::MENU_SLUG,
'order' => 0,
'capability' => 'read',
)
);
}
/**
* Check if the user can access the top-level WooCommerce item.
*
* @return bool
*/
public static function is_admin_user() {
if ( ! class_exists( 'WC_Admin_Menus', false ) ) {
include_once WC_ABSPATH . 'includes/admin/class-wc-admin-menus.php';
}
if ( method_exists( 'WC_Admin_Menus', 'can_view_woocommerce_menu_item' ) ) {
return \WC_Admin_Menus::can_view_woocommerce_menu_item() || current_user_can( 'manage_woocommerce' );
} else {
// We leave this line for WC versions <= 6.2.
return current_user_can( 'edit_others_shop_orders' ) || current_user_can( 'manage_woocommerce' );
}
}
/**
* Possibly remove the WooCommerce menu item if it was purely used to access wc-admin pages.
*/
public function possibly_remove_woocommerce_menu() {
global $menu;
if ( self::is_admin_user() ) {
return;
}
foreach ( $menu as $key => $menu_item ) {
if ( self::MENU_SLUG !== $menu_item[2] || 'read' !== $menu_item[1] ) {
continue;
}
unset( $menu[ $key ] );
}
}
/**
* Update the WooCommerce menu structure to make our main dashboard/handler
* the top level link for 'WooCommerce'.
*/
public function update_link_structure() {
global $submenu;
// User does not have capabilites to see the submenu.
if ( ! current_user_can( 'manage_woocommerce' ) || empty( $submenu['woocommerce'] ) ) {
return;
}
$wc_admin_key = null;
foreach ( $submenu['woocommerce'] as $submenu_key => $submenu_item ) {
if ( self::MENU_SLUG === $submenu_item[2] ) {
$wc_admin_key = $submenu_key;
break;
}
}
if ( ! $wc_admin_key ) {
return;
}
$menu = $submenu['woocommerce'][ $wc_admin_key ];
// Move menu item to top of array.
unset( $submenu['woocommerce'][ $wc_admin_key ] );
array_unshift( $submenu['woocommerce'], $menu );
}
/**
* Preload options to prime state of the application.
*
* @param array $options Array of options to preload.
* @return array
*/
public function preload_options( $options ) {
$options[] = 'woocommerce_default_homepage_layout';
$options[] = 'woocommerce_admin_install_timestamp';
return $options;
}
}
Marketing/MarketingSpecs.php 0000644 00000013404 15154512351 0012121 0 ustar 00 <?php
/**
* Marketing Specs Handler
*
* Fetches the specifications for the marketing feature from WC.com API.
*/
namespace Automattic\WooCommerce\Internal\Admin\Marketing;
/**
* Marketing Specifications Class.
*
* @internal
* @since x.x.x
*/
class MarketingSpecs {
/**
* Name of recommended plugins transient.
*
* @var string
*/
const RECOMMENDED_PLUGINS_TRANSIENT = 'wc_marketing_recommended_plugins';
/**
* Name of knowledge base post transient.
*
* @var string
*/
const KNOWLEDGE_BASE_TRANSIENT = 'wc_marketing_knowledge_base';
/**
* Slug of the category specifying marketing extensions on the WooCommerce.com store.
*
* @var string
*/
const MARKETING_EXTENSION_CATEGORY_SLUG = 'marketing';
/**
* Slug of the subcategory specifying marketing channels on the WooCommerce.com store.
*
* @var string
*/
const MARKETING_CHANNEL_SUBCATEGORY_SLUG = 'sales-channels';
/**
* Load recommended plugins from WooCommerce.com
*
* @return array
*/
public function get_recommended_plugins(): array {
$plugins = get_transient( self::RECOMMENDED_PLUGINS_TRANSIENT );
if ( false === $plugins ) {
$request = wp_remote_get(
'https://woocommerce.com/wp-json/wccom/marketing-tab/1.3/recommendations.json',
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
);
$plugins = [];
if ( ! is_wp_error( $request ) && 200 === $request['response']['code'] ) {
$plugins = json_decode( $request['body'], true );
}
set_transient(
self::RECOMMENDED_PLUGINS_TRANSIENT,
$plugins,
// Expire transient in 15 minutes if remote get failed.
// Cache an empty result to avoid repeated failed requests.
empty( $plugins ) ? 900 : 3 * DAY_IN_SECONDS
);
}
return array_values( $plugins );
}
/**
* Return only the recommended marketing channels from WooCommerce.com.
*
* @return array
*/
public function get_recommended_marketing_channels(): array {
return array_filter( $this->get_recommended_plugins(), [ $this, 'is_marketing_channel_plugin' ] );
}
/**
* Return all recommended marketing extensions EXCEPT the marketing channels from WooCommerce.com.
*
* @return array
*/
public function get_recommended_marketing_extensions_excluding_channels(): array {
return array_filter(
$this->get_recommended_plugins(),
function ( array $plugin_data ) {
return $this->is_marketing_plugin( $plugin_data ) && ! $this->is_marketing_channel_plugin( $plugin_data );
}
);
}
/**
* Returns whether a plugin is a marketing extension.
*
* @param array $plugin_data The plugin properties returned by the API.
*
* @return bool
*/
protected function is_marketing_plugin( array $plugin_data ): bool {
$categories = $plugin_data['categories'] ?? [];
return in_array( self::MARKETING_EXTENSION_CATEGORY_SLUG, $categories, true );
}
/**
* Returns whether a plugin is a marketing channel.
*
* @param array $plugin_data The plugin properties returned by the API.
*
* @return bool
*/
protected function is_marketing_channel_plugin( array $plugin_data ): bool {
if ( ! $this->is_marketing_plugin( $plugin_data ) ) {
return false;
}
$subcategories = $plugin_data['subcategories'] ?? [];
foreach ( $subcategories as $subcategory ) {
if ( isset( $subcategory['slug'] ) && self::MARKETING_CHANNEL_SUBCATEGORY_SLUG === $subcategory['slug'] ) {
return true;
}
}
return false;
}
/**
* Load knowledge base posts from WooCommerce.com
*
* @param string|null $term Term of posts to retrieve.
* @return array
*/
public function get_knowledge_base_posts( ?string $term ): array {
$terms = array(
'marketing' => array(
'taxonomy' => 'category',
'term_id' => 1744,
'argument' => 'categories',
),
'coupons' => array(
'taxonomy' => 'post_tag',
'term_id' => 1377,
'argument' => 'tags',
),
);
// Default to the marketing category (if no term is set on the kb component).
if ( empty( $term ) || ! array_key_exists( $term, $terms ) ) {
$term = 'marketing';
}
$term_id = $terms[ $term ]['term_id'];
$argument = $terms[ $term ]['argument'];
$kb_transient = self::KNOWLEDGE_BASE_TRANSIENT . '_' . strtolower( $term );
$posts = get_transient( $kb_transient );
if ( false === $posts ) {
$request_url = add_query_arg(
array(
$argument => $term_id,
'page' => 1,
'per_page' => 8,
'_embed' => 1,
),
'https://woocommerce.com/wp-json/wp/v2/posts?utm_medium=product'
);
$request = wp_remote_get(
$request_url,
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
);
$posts = [];
if ( ! is_wp_error( $request ) && 200 === $request['response']['code'] ) {
$raw_posts = json_decode( $request['body'], true );
foreach ( $raw_posts as $raw_post ) {
$post = [
'title' => html_entity_decode( $raw_post['title']['rendered'] ),
'date' => $raw_post['date_gmt'],
'link' => $raw_post['link'],
'author_name' => isset( $raw_post['author_name'] ) ? html_entity_decode( $raw_post['author_name'] ) : '',
'author_avatar' => isset( $raw_post['author_avatar_url'] ) ? $raw_post['author_avatar_url'] : '',
];
$featured_media = $raw_post['_embedded']['wp:featuredmedia'] ?? [];
if ( count( $featured_media ) > 0 ) {
$image = current( $featured_media );
$post['image'] = add_query_arg(
array(
'resize' => '650,340',
'crop' => 1,
),
$image['source_url']
);
}
$posts[] = $post;
}
}
set_transient(
$kb_transient,
$posts,
// Expire transient in 15 minutes if remote get failed.
empty( $posts ) ? 900 : DAY_IN_SECONDS
);
}
return $posts;
}
}
Marketing.php 0000644 00000007404 15154512351 0007205 0 ustar 00 <?php
/**
* WooCommerce Marketing.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Marketing\InstalledExtensions;
use Automattic\WooCommerce\Admin\PageController;
/**
* Contains backend logic for the Marketing feature.
*/
class Marketing {
use CouponsMovedTrait;
/**
* Class instance.
*
* @var Marketing instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WooCommerce.
*/
public function __construct() {
if ( ! is_admin() ) {
return;
}
add_action( 'admin_menu', array( $this, 'register_pages' ), 5 );
add_action( 'admin_menu', array( $this, 'add_parent_menu_item' ), 6 );
add_filter( 'woocommerce_admin_shared_settings', array( $this, 'component_settings' ), 30 );
}
/**
* Add main marketing menu item.
*
* Uses priority of 9 so other items can easily be added at the default priority (10).
*/
public function add_parent_menu_item() {
if ( ! Features::is_enabled( 'navigation' ) ) {
add_menu_page(
__( 'Marketing', 'woocommerce' ),
__( 'Marketing', 'woocommerce' ),
'manage_woocommerce',
'woocommerce-marketing',
null,
'dashicons-megaphone',
58
);
}
PageController::get_instance()->connect_page(
[
'id' => 'woocommerce-marketing',
'title' => 'Marketing',
'capability' => 'manage_woocommerce',
'path' => 'wc-admin&path=/marketing',
]
);
}
/**
* Registers report pages.
*/
public function register_pages() {
$this->register_overview_page();
$controller = PageController::get_instance();
$defaults = [
'parent' => 'woocommerce-marketing',
'existing_page' => false,
];
$marketing_pages = apply_filters( 'woocommerce_marketing_menu_items', [] );
foreach ( $marketing_pages as $marketing_page ) {
if ( ! is_array( $marketing_page ) ) {
continue;
}
$marketing_page = array_merge( $defaults, $marketing_page );
if ( $marketing_page['existing_page'] ) {
$controller->connect_page( $marketing_page );
} else {
$controller->register_page( $marketing_page );
}
}
}
/**
* Register the main Marketing page, which is Marketing > Overview.
*
* This is done separately because we need to ensure the page is registered properly and
* that the link is done properly. For some reason the normal page registration process
* gives us the wrong menu link.
*/
protected function register_overview_page() {
global $submenu;
// First register the page.
PageController::get_instance()->register_page(
[
'id' => 'woocommerce-marketing-overview',
'title' => __( 'Overview', 'woocommerce' ),
'path' => 'wc-admin&path=/marketing',
'parent' => 'woocommerce-marketing',
'nav_args' => array(
'parent' => 'woocommerce-marketing',
'order' => 10,
),
]
);
// Now fix the path, since register_page() gets it wrong.
if ( ! isset( $submenu['woocommerce-marketing'] ) ) {
return;
}
foreach ( $submenu['woocommerce-marketing'] as &$item ) {
// The "slug" (aka the path) is the third item in the array.
if ( 0 === strpos( $item[2], 'wc-admin' ) ) {
$item[2] = 'admin.php?page=' . $item[2];
}
}
}
/**
* Add settings for marketing feature.
*
* @param array $settings Component settings.
* @return array
*/
public function component_settings( $settings ) {
// Bail early if not on a wc-admin powered page.
if ( ! PageController::is_admin_page() ) {
return $settings;
}
$settings['marketing']['installedExtensions'] = InstalledExtensions::get_data();
return $settings;
}
}
Marketplace.php 0000644 00000002331 15154512351 0007506 0 ustar 00 <?php
/**
* WooCommerce Marketplace.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Utilities\FeaturesUtil;
/**
* Contains backend logic for the Marketplace feature.
*/
class Marketplace {
/**
* Class initialization, to be executed when the class is resolved by the container.
*/
final public function init() {
if ( FeaturesUtil::feature_is_enabled( 'marketplace' ) ) {
add_action( 'admin_menu', array( $this, 'register_pages' ), 70 );
}
}
/**
* Registers report pages.
*/
public function register_pages() {
$marketplace_pages = self::get_marketplace_pages();
foreach ( $marketplace_pages as $marketplace_page ) {
if ( ! is_null( $marketplace_page ) ) {
wc_admin_register_page( $marketplace_page );
}
}
}
/**
* Get report pages.
*/
public static function get_marketplace_pages() {
$marketplace_pages = array(
array(
'id' => 'woocommerce-marketplace',
'parent' => 'woocommerce',
'title' => __( 'Extensions', 'woocommerce' ),
'path' => '/extensions',
),
);
/**
* The marketplace items used in the menu.
*
* @since 8.0
*/
return apply_filters( 'woocommerce_marketplace_menu_items', $marketplace_pages );
}
}
MobileAppBanner.php 0000644 00000001674 15154512351 0010265 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin;
defined( 'ABSPATH' ) || exit;
/**
* Determine if the mobile app banner shows on Android devices
*/
class MobileAppBanner {
/**
* Class instance.
*
* @var Analytics instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WooCommerce.
*/
public function __construct() {
add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) );
}
/**
* Adds fields so that we can store user preferences for the mobile app banner
*
* @param array $user_data_fields User data fields.
* @return array
*/
public function add_user_data_fields( $user_data_fields ) {
return array_merge(
$user_data_fields,
array(
'android_app_banner_dismissed',
)
);
}
}
Notes/AddFirstProduct.php 0000644 00000005445 15154512351 0011420 0 ustar 00 <?php
/**
* WooCommerce Admin: Add First Product.
*
* Adds a note (type `email`) to bring the client back to the store setup flow.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Add_First_Product.
*/
class AddFirstProduct {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-add-first-product-note';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
if ( ! self::wc_admin_active_for( 2 * DAY_IN_SECONDS ) || self::wc_admin_active_for( 5 * DAY_IN_SECONDS ) ) {
return;
}
// Don't show if there is a product.
$query = new \WC_Product_Query(
array(
'limit' => 1,
'return' => 'ids',
'status' => array( 'publish' ),
)
);
$products = $query->get_products();
if ( 0 !== count( $products ) ) {
return;
}
// Don't show if there is an orders.
$args = array(
'limit' => 1,
'return' => 'ids',
);
$orders = wc_get_orders( $args );
if ( 0 !== count( $orders ) ) {
return;
}
// If you're updating the following please use sprintf to separate HTML tags.
// https://github.com/woocommerce/woocommerce-admin/pull/6617#discussion_r596889685.
$content_lines = array(
'{greetings}<br/><br/>',
/* translators: %s: line break */
sprintf( __( 'Nice one; you\'ve created a WooCommerce store! Now it\'s time to add your first product and get ready to start selling.%s', 'woocommerce' ), '<br/><br/>' ),
__( 'There are three ways to add your products: you can <strong>create products manually, import them at once via CSV file</strong>, or <strong>migrate them from another service</strong>.<br/><br/>', 'woocommerce' ),
/* translators: %1$s is an open anchor tag (<a>) and %2$s is a close link tag (</a>). */
sprintf( __( '%1$1sExplore our docs%2$2s for more information, or just get started!', 'woocommerce' ), '<a href="https://woocommerce.com/document/managing-products/?utm_source=help_panel&utm_medium=product">', '</a>' ),
);
$additional_data = array(
'role' => 'administrator',
);
$note = new Note();
$note->set_title( __( 'Add your first product', 'woocommerce' ) );
$note->set_content( implode( '', $content_lines ) );
$note->set_content_data( (object) $additional_data );
$note->set_image(
plugins_url(
'/images/admin_notes/dashboard-widget-setup.png',
WC_ADMIN_PLUGIN_FILE
)
);
$note->set_type( Note::E_WC_ADMIN_NOTE_EMAIL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'add-first-product', __( 'Add a product', 'woocommerce' ), admin_url( 'admin.php?page=wc-admin&task=products' ) );
return $note;
}
}
Notes/ChoosingTheme.php 0000644 00000002706 15154512351 0011110 0 ustar 00 <?php
/**
* WooCommerce Admin (Dashboard) choosing a theme note
*
* Adds notes to the merchant's inbox about choosing a theme.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Giving_Feedback_Notes
*/
class ChoosingTheme {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-choosing-a-theme';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
// We need to show choosing a theme notification after 1 day of install.
if ( ! self::is_wc_admin_active_in_date_range( 'week-1', DAY_IN_SECONDS ) ) {
return;
}
// Otherwise, create our new note.
$note = new Note();
$note->set_title( __( 'Choosing a theme?', 'woocommerce' ) );
$note->set_content( __( 'Check out the themes that are compatible with WooCommerce and choose one aligned with your brand and business needs.', 'woocommerce' ) );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_MARKETING );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'visit-the-theme-marketplace',
__( 'Visit the theme marketplace', 'woocommerce' ),
'https://woocommerce.com/product-category/themes/?utm_source=inbox&utm_medium=product'
);
return $note;
}
}
Notes/CouponPageMoved.php 0000644 00000007463 15154512351 0011414 0 ustar 00 <?php
/**
* WooCommerce Admin Coupon Page Moved provider.
*
* Adds a notice when the store manager access the coupons page via the old WooCommerce > Coupons menu.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\Notes;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
use Automattic\WooCommerce\Internal\Admin\CouponsMovedTrait;
use stdClass;
use WC_Data_Store;
/**
* Coupon_Page_Moved class.
*/
class CouponPageMoved {
use NoteTraits, CouponsMovedTrait;
const NOTE_NAME = 'wc-admin-coupon-page-moved';
/**
* Initialize our hooks.
*/
public function init() {
if ( ! wc_coupons_enabled() ) {
return;
}
add_action( 'admin_init', [ $this, 'possibly_add_note' ] );
add_action( 'admin_init', [ $this, 'redirect_to_coupons' ] );
add_action( 'woocommerce_newly_installed', [ $this, 'disable_legacy_menu_for_new_install' ] );
}
/**
* Checks if a note can and should be added.
*
* @return bool
*/
public static function can_be_added() {
if ( ! wc_coupons_enabled() ) {
return false;
}
// Don't add the notice if the legacy coupon menu is already disabled.
if ( ! self::should_display_legacy_menu() ) {
return false;
}
// Don't add the notice if it's been hidden by the user before.
if ( self::has_dismissed_note() ) {
return false;
}
// If we already have a notice, don't add a new one.
if ( self::has_unactioned_note() ) {
return false;
}
return isset( $_GET[ self::$query_key ] ) && (bool) $_GET[ self::$query_key ]; // phpcs:ignore WordPress.Security.NonceVerification
}
/**
* Get the note object for this class.
*
* @return Note
*/
public static function get_note() {
$note = new Note();
$note->set_title( __( 'Coupon management has moved!', 'woocommerce' ) );
$note->set_content( __( 'Coupons can now be managed from Marketing > Coupons. Click the button below to remove the legacy WooCommerce > Coupons menu item.', 'woocommerce' ) );
$note->set_type( Note::E_WC_ADMIN_NOTE_UPDATE );
$note->set_name( self::NOTE_NAME );
$note->set_content_data( new stdClass() );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'remove-legacy-coupon-menu',
__( 'Remove legacy coupon menu', 'woocommerce' ),
wc_admin_url( '&action=remove-coupon-menu' ),
Note::E_WC_ADMIN_NOTE_ACTIONED
);
return $note;
}
/**
* Find notes that have not been actioned.
*
* @return bool
*/
protected static function has_unactioned_note() {
$note = Notes::get_note_by_name( self::NOTE_NAME );
if ( ! $note ) {
return false;
}
return $note->get_status() === 'unactioned';
}
/**
* Whether any notes have been dismissed by the user previously.
*
* @return bool
*/
protected static function has_dismissed_note() {
$note = Notes::get_note_by_name( self::NOTE_NAME );
if ( ! $note ) {
return false;
}
return ! $note->get_is_deleted();
}
/**
* Get the data store object.
*
* @return DataStore The data store object.
*/
protected static function get_data_store() {
return WC_Data_Store::load( 'admin-note' );
}
/**
* Safe redirect to the coupon page to force page refresh.
*/
public function redirect_to_coupons() {
/* phpcs:disable WordPress.Security.NonceVerification */
if (
! isset( $_GET['page'] ) ||
'wc-admin' !== $_GET['page'] ||
! isset( $_GET['action'] ) ||
'remove-coupon-menu' !== $_GET['action'] ||
! defined( 'WC_ADMIN_PLUGIN_FILE' )
) {
return;
}
/* phpcs:enable */
$this->display_legacy_menu( false );
wp_safe_redirect( self::get_management_url( 'coupons' ) );
exit;
}
/**
* Disable legacy coupon menu when installing for the first time.
*/
public function disable_legacy_menu_for_new_install() {
$this->display_legacy_menu( false );
}
}
Notes/CustomizeStoreWithBlocks.php 0000644 00000004543 15154512351 0013346 0 ustar 00 <?php
/**
* WooCommerce Admin: Customize your online store with WooCommerce blocks.
*
* Adds a note to customize the client online store with WooCommerce blocks.
*
* @package WooCommerce\Admin
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Customize_Store_With_Blocks.
*/
class CustomizeStoreWithBlocks {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-customize-store-with-blocks';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
$onboarding_profile = get_option( 'woocommerce_onboarding_profile', array() );
// Confirm that $onboarding_profile is set.
if ( empty( $onboarding_profile ) ) {
return;
}
// Make sure that the person who filled out the OBW was not setting up
// the store for their customer/client.
if (
! isset( $onboarding_profile['setup_client'] ) ||
$onboarding_profile['setup_client']
) {
return;
}
// We want to show the note after fourteen days.
if ( ! self::is_wc_admin_active_in_date_range( 'week-1-4', 14 * DAY_IN_SECONDS ) ) {
return;
}
// Don't show if there aren't products.
$query = new \WC_Product_Query(
array(
'limit' => 1,
'return' => 'ids',
'status' => array( 'publish' ),
)
);
$products = $query->get_products();
if ( 0 === count( $products ) ) {
return;
}
$note = new Note();
$note->set_title( __( 'Customize your online store with WooCommerce blocks', 'woocommerce' ) );
$note->set_content( __( 'With our blocks, you can select and display products, categories, filters, and more virtually anywhere on your site — no need to use shortcodes or edit lines of code. Learn more about how to use each one of them.', 'woocommerce' ) );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_content_data( (object) array() );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'customize-store-with-blocks',
__( 'Learn more', 'woocommerce' ),
'https://woocommerce.com/posts/how-to-customize-your-online-store-with-woocommerce-blocks/?utm_source=inbox&utm_medium=product',
Note::E_WC_ADMIN_NOTE_ACTIONED
);
return $note;
}
}
Notes/CustomizingProductCatalog.php 0000644 00000004215 15154512351 0013520 0 ustar 00 <?php
/**
* WooCommerce Admin: How to customize your product catalog note provider
*
* Adds a note with a link to the customizer a day after adding the first product
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Class CustomizingProductCatalog
*
* @package Automattic\WooCommerce\Admin\Notes
*/
class CustomizingProductCatalog {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-customizing-product-catalog';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
$query = new \WC_Product_Query(
array(
'limit' => 1,
'paginate' => true,
'status' => array( 'publish' ),
'orderby' => 'post_date',
'order' => 'DESC',
)
);
$products = $query->get_products();
// we need at least 1 product.
if ( 0 === $products->total ) {
return;
}
$product = $products->products[0];
$created_timestamp = $product->get_date_created()->getTimestamp();
$is_a_day_old = ( time() - $created_timestamp ) >= DAY_IN_SECONDS;
// the product must be at least 1 day old.
if ( ! $is_a_day_old ) {
return;
}
// store must not been active more than 14 days.
if ( self::wc_admin_active_for( DAY_IN_SECONDS * 14 ) ) {
return;
}
$note = new Note();
$note->set_title( __( 'How to customize your product catalog', 'woocommerce' ) );
$note->set_content( __( 'You want your product catalog and images to look great and align with your brand. This guide will give you all the tips you need to get your products looking great in your store.', 'woocommerce' ) );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_content_data( (object) array() );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'day-after-first-product',
__( 'Learn more', 'woocommerce' ),
'https://woocommerce.com/document/woocommerce-customizer/?utm_source=inbox&utm_medium=product'
);
return $note;
}
}
Notes/EUVATNumber.php 0000644 00000003212 15154512351 0010402 0 ustar 00 <?php
/**
* WooCommerce Admin: EU VAT Number Note.
*
* Adds a note for EU store to install the EU VAT Number extension.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* EU_VAT_Number
*/
class EUVATNumber {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-eu-vat-number';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
if ( 'yes' !== get_option( 'wc_connect_taxes_enabled', 'no' ) ) {
return;
}
$country_code = WC()->countries->get_base_country();
$eu_countries = WC()->countries->get_european_union_countries();
if ( ! in_array( $country_code, $eu_countries, true ) ) {
return;
}
$content = __( "If your store is based in the EU, we recommend using the EU VAT Number extension in addition to automated taxes. It provides your checkout with a field to collect and validate a customer's EU VAT number, if they have one.", 'woocommerce' );
$note = new Note();
$note->set_title( __( 'Collect and validate EU VAT numbers at checkout', 'woocommerce' ) );
$note->set_content( $content );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_MARKETING );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'learn-more',
__( 'Learn more', 'woocommerce' ),
'https://woocommerce.com/products/eu-vat-number/?utm_medium=product',
Note::E_WC_ADMIN_NOTE_ACTIONED
);
return $note;
}
}
Notes/EditProductsOnTheMove.php 0000644 00000003264 15154512351 0012552 0 ustar 00 <?php
/**
* WooCommerce Admin Edit products on the move note.
*
* Adds a note to download the mobile app.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Edit_Products_On_The_Move
*/
class EditProductsOnTheMove {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-edit-products-on-the-move';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
// Only add this note if this store is at least a year old.
$year_in_seconds = 365 * DAY_IN_SECONDS;
if ( ! self::wc_admin_active_for( $year_in_seconds ) ) {
return;
}
// Check that the previous mobile app notes have not been actioned.
if ( MobileApp::has_note_been_actioned() ) {
return;
}
if ( RealTimeOrderAlerts::has_note_been_actioned() ) {
return;
}
if ( ManageOrdersOnTheGo::has_note_been_actioned() ) {
return;
}
if ( PerformanceOnMobile::has_note_been_actioned() ) {
return;
}
$note = new Note();
$note->set_title( __( 'Edit products on the move', 'woocommerce' ) );
$note->set_content( __( 'Edit and create new products from your mobile devices with the Woo app', 'woocommerce' ) );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'learn-more',
__( 'Learn more', 'woocommerce' ),
'https://woocommerce.com/mobile/?utm_source=inbox&utm_medium=product'
);
return $note;
}
}
Notes/EmailNotification.php 0000644 00000012250 15154512351 0011745 0 ustar 00 <?php
/**
* Handles emailing user notes.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use Automattic\WooCommerce\Admin\Notes\Notes;
/**
* Include dependencies.
*/
if ( ! class_exists( 'WC_Email', false ) ) {
include_once WC_ABSPATH . 'includes/emails/class-wc-email.php';
}
/**
* EmailNotification Class.
*/
class EmailNotification extends \WC_Email {
/**
* Constructor.
*
* @param Note $note The notification to send.
*/
public function __construct( $note ) {
$this->note = $note;
$this->id = 'merchant_notification';
$this->template_base = WC_ADMIN_ABSPATH . 'includes/react-admin/emails/';
$this->placeholders = array(
'{greetings}' => __( 'Hi there,', 'woocommerce' ),
);
// Call parent constructor.
parent::__construct();
}
/**
* This email has no user-facing settings.
*/
public function init_form_fields() {}
/**
* This email has no user-facing settings.
*/
public function init_settings() {}
/**
* Return template filename.
*
* @param string $type Type of email to send.
* @return string
*/
public function get_template_filename( $type = 'html' ) {
if ( ! in_array( $type, array( 'html', 'plain' ), true ) ) {
return;
}
$content_data = $this->note->get_content_data();
$template_filename = "{$type}-merchant-notification.php";
if ( isset( $content_data->{"template_{$type}"} ) && file_exists( $this->template_base . $content_data->{ "template_{$type}" } ) ) {
$template_filename = $content_data[ "template_{$type}" ];
}
return $template_filename;
}
/**
* Return email type.
*
* @return string
*/
public function get_email_type() {
return class_exists( 'DOMDocument' ) ? 'html' : 'plain';
}
/**
* Get email heading.
*
* @return string
*/
public function get_default_heading() {
$content_data = $this->note->get_content_data();
if ( isset( $content_data->heading ) ) {
return $content_data->heading;
}
return $this->note->get_title();
}
/**
* Get email headers.
*
* @return string
*/
public function get_headers() {
$header = 'Content-Type: ' . $this->get_content_type() . "\r\n";
return apply_filters( 'woocommerce_email_headers', $header, $this->id, $this->object, $this );
}
/**
* Get email subject.
*
* @return string
*/
public function get_default_subject() {
return $this->note->get_title();
}
/**
* Get note content.
*
* @return string
*/
public function get_note_content() {
return $this->note->get_content();
}
/**
* Get note image.
*
* @return string
*/
public function get_image() {
return $this->note->get_image();
}
/**
* Get email action.
*
* @return stdClass
*/
public function get_actions() {
return $this->note->get_actions();
}
/**
* Get content html.
*
* @return string
*/
public function get_content_html() {
return wc_get_template_html(
$this->get_template_filename( 'html' ),
array(
'email_actions' => $this->get_actions(),
'email_content' => $this->format_string( $this->get_note_content() ),
'email_heading' => $this->format_string( $this->get_heading() ),
'email_image' => $this->get_image(),
'sent_to_admin' => true,
'plain_text' => false,
'email' => $this,
'opened_tracking_url' => $this->opened_tracking_url,
'trigger_note_action_url' => $this->trigger_note_action_url,
),
'',
$this->template_base
);
}
/**
* Get content plain.
*
* @return string
*/
public function get_content_plain() {
return wc_get_template_html(
$this->get_template_filename( 'plain' ),
array(
'email_heading' => $this->format_string( $this->get_heading() ),
'email_content' => $this->format_string( $this->get_note_content() ),
'email_actions' => $this->get_actions(),
'sent_to_admin' => true,
'plain_text' => true,
'email' => $this,
'trigger_note_action_url' => $this->trigger_note_action_url,
),
'',
$this->template_base
);
}
/**
* Trigger the sending of this email.
*
* @param string $user_email Email to send the note.
* @param int $user_id User id to to track the note.
* @param string $user_name User's name.
*/
public function trigger( $user_email, $user_id, $user_name ) {
$this->recipient = $user_email;
$this->opened_tracking_url = sprintf(
'%1$s/wp-json/wc-analytics/admin/notes/tracker/%2$d/user/%3$d',
site_url(),
$this->note->get_id(),
$user_id
);
$this->trigger_note_action_url = sprintf(
'%1$s&external_redirect=1¬e=%2$d&user=%3$d&action=',
wc_admin_url(),
$this->note->get_id(),
$user_id
);
if ( $user_name ) {
/* translators: %s = merchant name */
$this->placeholders['{greetings}'] = sprintf( __( 'Hi %s,', 'woocommerce' ), $user_name );
}
$this->send(
$this->get_recipient(),
$this->get_subject(),
$this->get_content(),
$this->get_headers(),
$this->get_attachments()
);
Notes::record_tracks_event_with_user( $user_id, 'email_note_sent', array( 'note_name' => $this->note->get_name() ) );
}
}
Notes/FirstProduct.php 0000644 00000004175 15154512351 0011006 0 ustar 00 <?php
/**
* WooCommerce Admin: Do you need help with adding your first product?
*
* Adds a note to ask the client if they need help adding their first product.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* First_Product.
*/
class FirstProduct {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-first-product';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
// We want to show the note after seven days.
if ( ! self::is_wc_admin_active_in_date_range( 'week-1-4' ) ) {
return;
}
$onboarding_profile = get_option( 'woocommerce_onboarding_profile', array() );
// Confirm that $onboarding_profile is set.
if ( empty( $onboarding_profile ) ) {
return;
}
// Make sure that the person who filled out the OBW was not setting up
// the store for their customer/client.
if (
! isset( $onboarding_profile['setup_client'] ) ||
$onboarding_profile['setup_client']
) {
return;
}
// Don't show if there are products.
$query = new \WC_Product_Query(
array(
'limit' => 1,
'paginate' => true,
'return' => 'ids',
'status' => array( 'publish' ),
)
);
$products = $query->get_products();
$count = $products->total;
if ( 0 !== $count ) {
return;
}
$note = new Note();
$note->set_title( __( 'Do you need help with adding your first product?', 'woocommerce' ) );
$note->set_content( __( 'This video tutorial will help you go through the process of adding your first product in WooCommerce.', 'woocommerce' ) );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_content_data( (object) array() );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'first-product-watch-tutorial',
__( 'Watch tutorial', 'woocommerce' ),
'https://www.youtube.com/watch?v=sFtXa00Jf_o&list=PLHdG8zvZd0E575Ia8Mu3w1h750YLXNfsC&index=24'
);
return $note;
}
}
Notes/GivingFeedbackNotes.php 0000644 00000003002 15154512351 0012203 0 ustar 00 <?php
/**
* WooCommerce Admin (Dashboard) Giving feedback notes provider
*
* Adds notes to the merchant's inbox about giving feedback.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
use Automattic\WooCommerce\Internal\Admin\Survey;
/**
* Giving_Feedback_Notes
*/
class GivingFeedbackNotes {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-store-notice-giving-feedback-2';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
if ( ! self::is_wc_admin_active_in_date_range( 'week-1-4' ) ) {
return;
}
// Otherwise, create our new note.
$note = new Note();
$note->set_title( __( 'You\'re invited to share your experience', 'woocommerce' ) );
$note->set_content( __( 'Now that you’ve chosen us as a partner, our goal is to make sure we\'re providing the right tools to meet your needs. We\'re looking forward to having your feedback on the store setup experience so we can improve it in the future.', 'woocommerce' ) );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'share-feedback',
__( 'Share feedback', 'woocommerce' ),
Survey::get_url( '/store-setup-survey' )
);
return $note;
}
}
Notes/InstallJPAndWCSPlugins.php 0000644 00000011122 15154512351 0012546 0 ustar 00 <?php
/**
* WooCommerce Admin Add Install Jetpack and WooCommerce Shipping & Tax Plugin Note Provider.
*
* Adds a note to the merchant's inbox prompting them to install the Jetpack
* and WooCommerce Shipping & Tax plugins after it fails to install during
* WooCommerce setup.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\Notes;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
use Automattic\WooCommerce\Admin\PluginsHelper;
/**
* Install_JP_And_WCS_Plugins
*/
class InstallJPAndWCSPlugins {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-install-jp-and-wcs-plugins';
/**
* Constructor.
*/
public function __construct() {
add_action( 'woocommerce_note_action_install-jp-and-wcs-plugins', array( $this, 'install_jp_and_wcs_plugins' ) );
add_action( 'activated_plugin', array( $this, 'action_note' ) );
add_action( 'woocommerce_plugins_install_api_error', array( $this, 'on_install_error' ) );
add_action( 'woocommerce_plugins_install_error', array( $this, 'on_install_error' ) );
add_action( 'woocommerce_plugins_activate_error', array( $this, 'on_install_error' ) );
}
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
$content = __( 'We noticed that there was a problem during the Jetpack and WooCommerce Shipping & Tax install. Please try again and enjoy all the advantages of having the plugins connected to your store! Sorry for the inconvenience. The "Jetpack" and "WooCommerce Shipping & Tax" plugins will be installed & activated for free.', 'woocommerce' );
$note = new Note();
$note->set_title( __( 'Uh oh... There was a problem during the Jetpack and WooCommerce Shipping & Tax install. Please try again.', 'woocommerce' ) );
$note->set_content( $content );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'install-jp-and-wcs-plugins',
__( 'Install plugins', 'woocommerce' ),
false,
Note::E_WC_ADMIN_NOTE_ACTIONED
);
return $note;
}
/**
* Action the Install Jetpack and WooCommerce Shipping & Tax note, if any exists,
* and as long as both the Jetpack and WooCommerce Shipping & Tax plugins have been
* activated.
*/
public static function action_note() {
// Make sure that both plugins are active before actioning the note.
$active_plugin_slugs = PluginsHelper::get_active_plugin_slugs();
$jp_active = in_array( 'jetpack', $active_plugin_slugs, true );
$wcs_active = in_array( 'woocommerce-services', $active_plugin_slugs, true );
if ( ! $jp_active || ! $wcs_active ) {
return;
}
// Action any notes with a matching name.
$data_store = Notes::load_data_store();
$note_ids = $data_store->get_notes_with_name( self::NOTE_NAME );
foreach ( $note_ids as $note_id ) {
$note = Notes::get_note( $note_id );
if ( $note ) {
$note->set_status( Note::E_WC_ADMIN_NOTE_ACTIONED );
$note->save();
}
}
}
/**
* Install the Jetpack and WooCommerce Shipping & Tax plugins in response to the action
* being clicked in the admin note.
*
* @param Note $note The note being actioned.
*/
public function install_jp_and_wcs_plugins( $note ) {
if ( self::NOTE_NAME !== $note->get_name() ) {
return;
}
$this->install_and_activate_plugin( 'jetpack' );
$this->install_and_activate_plugin( 'woocommerce-services' );
}
/**
* Installs and activates the specified plugin.
*
* @param string $plugin The plugin slug.
*/
private function install_and_activate_plugin( $plugin ) {
$install_request = array( 'plugin' => $plugin );
$installer = new \Automattic\WooCommerce\Admin\API\OnboardingPlugins();
$result = $installer->install_plugin( $install_request );
// @todo Use the error statuses to decide whether or not to action the note.
if ( is_wp_error( $result ) ) {
return;
}
$activate_request = array( 'plugins' => $plugin );
$installer->activate_plugins( $activate_request );
}
/**
* Create an alert notification in response to an error installing a plugin.
*
* @param string $slug The slug of the plugin being installed.
*/
public function on_install_error( $slug ) {
// Exit early if we're not installing the Jetpack or the WooCommerce Shipping & Tax plugins.
if ( 'jetpack' !== $slug && 'woocommerce-services' !== $slug ) {
return;
}
self::possibly_add_note();
}
}
Notes/LaunchChecklist.php 0000644 00000003270 15154512351 0011415 0 ustar 00 <?php
/**
* WooCommerce Admin Launch Checklist Note.
*
* Adds a note to cover pre-launch checklist items for store owners.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Launch_Checklist
*/
class LaunchChecklist {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-launch-checklist';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
// Only add this note if completing the task list or completed 3 tasks in 10 days.
$completed_tasks = get_option( 'woocommerce_task_list_tracked_completed_tasks', array() );
$ten_days_in_seconds = 10 * DAY_IN_SECONDS;
if (
! get_option( 'woocommerce_task_list_complete' ) &&
(
count( $completed_tasks ) < 3 ||
self::is_wc_admin_active_in_date_range( 'week-1-4', $ten_days_in_seconds )
)
) {
return;
}
$content = __( 'To make sure you never get that sinking "what did I forget" feeling, we\'ve put together the essential pre-launch checklist.', 'woocommerce' );
$note = new Note();
$note->set_title( __( 'Ready to launch your store?', 'woocommerce' ) );
$note->set_content( $content );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'learn-more', __( 'Learn more', 'woocommerce' ), 'https://woocommerce.com/posts/pre-launch-checklist-the-essentials/?utm_source=inbox&utm_medium=product' );
return $note;
}
}
Notes/MagentoMigration.php 0000644 00000004714 15154512351 0011621 0 ustar 00 <?php
/**
* WooCommerce Admin note on how to migrate from Magento.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Features\Onboarding;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* MagentoMigration
*/
class MagentoMigration {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-magento-migration';
/**
* Attach hooks.
*/
public function __construct() {
add_action( 'update_option_' . OnboardingProfile::DATA_OPTION, array( __CLASS__, 'possibly_add_note' ) );
add_action( 'woocommerce_admin_magento_migration_note', array( __CLASS__, 'save_note' ) );
}
/**
* Add the note if it passes predefined conditions.
*/
public static function possibly_add_note() {
$onboarding_profile = get_option( OnboardingProfile::DATA_OPTION, array() );
if ( empty( $onboarding_profile ) ) {
return;
}
if (
! isset( $onboarding_profile['other_platform'] ) ||
'magento' !== $onboarding_profile['other_platform']
) {
return;
}
if (
! isset( $onboarding_profile['setup_client'] ) ||
$onboarding_profile['setup_client']
) {
return;
}
WC()->queue()->schedule_single( time() + ( 5 * MINUTE_IN_SECONDS ), 'woocommerce_admin_magento_migration_note' );
}
/**
* Save the note to the database.
*/
public static function save_note() {
$note = self::get_note();
if ( self::note_exists() ) {
return;
}
$note->save();
}
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
$note = new Note();
$note->set_title( __( 'How to Migrate from Magento to WooCommerce', 'woocommerce' ) );
$note->set_content( __( 'Changing platforms might seem like a big hurdle to overcome, but it is easier than you might think to move your products, customers, and orders to WooCommerce. This article will help you with going through this process.', 'woocommerce' ) );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'learn-more',
__( 'Learn more', 'woocommerce' ),
'https://woocommerce.com/posts/how-migrate-from-magento-to-woocommerce/?utm_source=inbox'
);
return $note;
}
}
Notes/ManageOrdersOnTheGo.php 0000644 00000003051 15154512351 0012141 0 ustar 00 <?php
/**
* WooCommerce Admin Manage orders on the go note.
*
* Adds a note to download the mobile app to manage orders on the go.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Manage_Orders_On_The_Go
*/
class ManageOrdersOnTheGo {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-manage-orders-on-the-go';
/**
* Get the note.
*
* @return Note|null
*/
public static function get_note() {
// Only add this note if this store is at least 6 months old.
if ( ! self::is_wc_admin_active_in_date_range( 'month-6+' ) ) {
return;
}
// Check that the previous mobile app notes have not been actioned.
if ( MobileApp::has_note_been_actioned() ) {
return;
}
if ( RealTimeOrderAlerts::has_note_been_actioned() ) {
return;
}
$note = new Note();
$note->set_title( __( 'Manage your orders on the go', 'woocommerce' ) );
$note->set_content( __( 'Look for orders, customer info, and process refunds in one click with the Woo app.', 'woocommerce' ) );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'learn-more',
__( 'Learn more', 'woocommerce' ),
'https://woocommerce.com/mobile/?utm_source=inbox&utm_medium=product'
);
return $note;
}
}
Notes/MarketingJetpack.php 0000644 00000007265 15154512351 0011604 0 ustar 00 <?php
/**
* WooCommerce Admin Jetpack Marketing Note Provider.
*
* Adds notes to the merchant's inbox concerning Jetpack Backup.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\Notes;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
use Automattic\WooCommerce\Admin\PluginsHelper;
/**
* Suggest Jetpack Backup to Woo users.
*
* Note: This should probably live in the Jetpack plugin in the future.
*
* @see https://developer.woocommerce.com/2020/10/16/using-the-admin-notes-inbox-in-woocommerce/
*/
class MarketingJetpack {
// Shared Note Traits.
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-marketing-jetpack-backup';
/**
* Product IDs that include Backup.
*/
const BACKUP_IDS = [
2010,
2011,
2012,
2013,
2014,
2015,
2100,
2101,
2102,
2103,
2005,
2006,
2000,
2003,
2001,
2004,
];
/**
* Maybe add a note on Jetpack Backups for Jetpack sites older than a week without Backups.
*/
public static function possibly_add_note() {
/**
* Check if Jetpack is installed.
*/
$installed_plugins = PluginsHelper::get_installed_plugin_slugs();
if ( ! in_array( 'jetpack', $installed_plugins, true ) ) {
return;
}
$data_store = \WC_Data_Store::load( 'admin-note' );
// Do we already have this note?
$note_ids = $data_store->get_notes_with_name( self::NOTE_NAME );
if ( ! empty( $note_ids ) ) {
$note_id = array_pop( $note_ids );
$note = Notes::get_note( $note_id );
if ( false === $note ) {
return;
}
// If Jetpack Backups was purchased after the note was created, mark this note as actioned.
if ( self::has_backups() && Note::E_WC_ADMIN_NOTE_ACTIONED !== $note->get_status() ) {
$note->set_status( Note::E_WC_ADMIN_NOTE_ACTIONED );
$note->save();
}
return;
}
// Check requirements.
if ( ! self::is_wc_admin_active_in_date_range( 'week-1-4', DAY_IN_SECONDS * 3 ) || ! self::can_be_added() || self::has_backups() ) {
return;
}
// Add note.
$note = self::get_note();
$note->save();
}
/**
* Get the note.
*/
public static function get_note() {
$note = new Note();
$note->set_title( __( 'Protect your WooCommerce Store with Jetpack Backup.', 'woocommerce' ) );
$note->set_content( __( 'Store downtime means lost sales. One-click restores get you back online quickly if something goes wrong.', 'woocommerce' ) );
$note->set_type( Note::E_WC_ADMIN_NOTE_MARKETING );
$note->set_name( self::NOTE_NAME );
$note->set_layout( 'thumbnail' );
$note->set_image(
WC_ADMIN_IMAGES_FOLDER_URL . '/admin_notes/marketing-jetpack-2x.png'
);
$note->set_content_data( (object) array() );
$note->set_source( 'woocommerce-admin-notes' );
$note->add_action(
'jetpack-backup-woocommerce',
__( 'Get backups', 'woocommerce' ),
esc_url( 'https://jetpack.com/upgrade/backup-woocommerce/?utm_source=inbox&utm_medium=automattic_referred&utm_campaign=jp_backup_to_woo' ),
Note::E_WC_ADMIN_NOTE_ACTIONED
);
return $note;
}
/**
* Check if this blog already has a Jetpack Backups product.
*
* @return boolean Whether or not this blog has backups.
*/
protected static function has_backups() {
$product_ids = [];
$plan = get_option( 'jetpack_active_plan' );
if ( ! empty( $plan ) ) {
$product_ids[] = $plan['product_id'];
}
$products = get_option( 'jetpack_site_products' );
if ( ! empty( $products ) ) {
foreach ( $products as $product ) {
$product_ids[] = $product['product_id'];
}
}
return (bool) array_intersect( self::BACKUP_IDS, $product_ids );
}
}
Notes/MerchantEmailNotifications.php 0000644 00000006601 15154512351 0013615 0 ustar 00 <?php
/**
* Handles merchant email notifications
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\Notes;
defined( 'ABSPATH' ) || exit;
/**
* Merchant email notifications.
* This gets all non-sent notes type `email` and sends them.
*/
class MerchantEmailNotifications {
/**
* Initialize the merchant email notifications.
*/
public static function init() {
add_action( 'admin_init', array( __CLASS__, 'trigger_notification_action' ) );
}
/**
* Trigger the note action.
*/
public static function trigger_notification_action() {
/* phpcs:disable WordPress.Security.NonceVerification */
if (
! isset( $_GET['external_redirect'] ) ||
1 !== intval( $_GET['external_redirect'] ) ||
! isset( $_GET['user'] ) ||
! isset( $_GET['note'] ) ||
! isset( $_GET['action'] )
) {
return;
}
$note_id = intval( $_GET['note'] );
$action_id = intval( $_GET['action'] );
$user_id = intval( $_GET['user'] );
/* phpcs:enable */
$note = Notes::get_note( $note_id );
if ( ! $note || Note::E_WC_ADMIN_NOTE_EMAIL !== $note->get_type() ) {
return;
}
$triggered_action = Notes::get_action_by_id( $note, $action_id );
if ( ! $triggered_action ) {
return;
}
Notes::trigger_note_action( $note, $triggered_action );
$url = $triggered_action->query;
// We will use "wp_safe_redirect" when it's an internal redirect.
if ( strpos( $url, 'http' ) === false ) {
wp_safe_redirect( $url );
} else {
header( 'Location: ' . $url );
}
exit();
}
/**
* Send all the notifications type `email`.
*/
public static function run() {
$data_store = Notes::load_data_store();
$notes = $data_store->get_notes(
array(
'type' => array( Note::E_WC_ADMIN_NOTE_EMAIL ),
'status' => array( 'unactioned' ),
)
);
foreach ( $notes as $note ) {
$note = Notes::get_note( $note->note_id );
if ( $note ) {
self::send_merchant_notification( $note );
$note->set_status( 'sent' );
$note->save();
}
}
}
/**
* Send the notification to the merchant.
*
* @param object $note The note to send.
*/
public static function send_merchant_notification( $note ) {
\WC_Emails::instance();
$users = self::get_notification_recipients( $note );
$email = new EmailNotification( $note );
foreach ( $users as $user ) {
if ( is_email( $user->user_email ) ) {
$name = self::get_merchant_preferred_name( $user );
$email->trigger( $user->user_email, $user->ID, $name );
}
}
}
/**
* Get the preferred name for user. First choice is
* the user's first name, and then display_name.
*
* @param WP_User $user Recipient to send the note to.
* @return string User's name.
*/
public static function get_merchant_preferred_name( $user ) {
$first_name = get_user_meta( $user->ID, 'first_name', true );
if ( $first_name ) {
return $first_name;
}
if ( $user->display_name ) {
return $user->display_name;
}
return '';
}
/**
* Get users by role to notify.
*
* @param object $note The note to send.
* @return array Users to notify
*/
public static function get_notification_recipients( $note ) {
$content_data = $note->get_content_data();
$role = 'administrator';
if ( isset( $content_data->role ) ) {
$role = $content_data->role;
}
$args = array( 'role' => $role );
return get_users( $args );
}
}
Notes/MigrateFromShopify.php 0000644 00000004330 15154512351 0012125 0 ustar 00 <?php
/**
* WooCommerce Admin: Migrate from Shopify to WooCommerce.
*
* Adds a note to ask the client if they want to migrate from Shopify to WooCommerce.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Migrate_From_Shopify.
*/
class MigrateFromShopify {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-migrate-from-shopify';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
// We want to show the note after two days.
$two_days = 2 * DAY_IN_SECONDS;
if ( ! self::is_wc_admin_active_in_date_range( 'week-1', $two_days ) ) {
return;
}
$onboarding_profile = get_option( 'woocommerce_onboarding_profile', array() );
if (
! isset( $onboarding_profile['setup_client'] ) ||
! isset( $onboarding_profile['selling_venues'] ) ||
! isset( $onboarding_profile['other_platform'] )
) {
return;
}
// Make sure the client is not setup.
if ( $onboarding_profile['setup_client'] ) {
return;
}
// We will show the notification when the client already is selling and is using Shopify.
if (
'other' !== $onboarding_profile['selling_venues'] ||
'shopify' !== $onboarding_profile['other_platform']
) {
return;
}
$note = new Note();
$note->set_title( __( 'Do you want to migrate from Shopify to WooCommerce?', 'woocommerce' ) );
$note->set_content( __( 'Changing eCommerce platforms might seem like a big hurdle to overcome, but it is easier than you might think to move your products, customers, and orders to WooCommerce. This article will help you with going through this process.', 'woocommerce' ) );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_content_data( (object) array() );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'migrate-from-shopify',
__( 'Learn more', 'woocommerce' ),
'https://woocommerce.com/posts/migrate-from-shopify-to-woocommerce/?utm_source=inbox&utm_medium=product',
Note::E_WC_ADMIN_NOTE_ACTIONED
);
return $note;
}
}
Notes/MobileApp.php 0000644 00000002615 15154512351 0010223 0 ustar 00 <?php
/**
* WooCommerce Admin Mobile App Note Provider.
*
* Adds a note to the merchant's inbox showing the benefits of the mobile app.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Mobile_App
*/
class MobileApp {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-mobile-app';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
// We want to show the mobile app note after day 2.
$two_days_in_seconds = 2 * DAY_IN_SECONDS;
if ( ! self::is_wc_admin_active_in_date_range( 'week-1', $two_days_in_seconds ) ) {
return;
}
$content = __( 'Install the WooCommerce mobile app to manage orders, receive sales notifications, and view key metrics — wherever you are.', 'woocommerce' );
$note = new Note();
$note->set_title( __( 'Install Woo mobile app', 'woocommerce' ) );
$note->set_content( $content );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'learn-more', __( 'Learn more', 'woocommerce' ), 'https://woocommerce.com/mobile/?utm_medium=product' );
return $note;
}
}
Notes/NewSalesRecord.php 0000644 00000012404 15154512351 0011230 0 ustar 00 <?php
/**
* WooCommerce Admin (Dashboard) New Sales Record Note Provider.
*
* Adds a note to the merchant's inbox when the previous day's sales are a new record.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\Notes;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* New_Sales_Record
*/
class NewSalesRecord {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-new-sales-record';
/**
* Option name for the sales record date in ISO 8601 (YYYY-MM-DD) date.
*/
const RECORD_DATE_OPTION_KEY = 'woocommerce_sales_record_date';
/**
* Option name for the sales record amount.
*/
const RECORD_AMOUNT_OPTION_KEY = 'woocommerce_sales_record_amount';
/**
* Returns the total of yesterday's sales.
*
* @param string $date Date for sales to sum (i.e. YYYY-MM-DD).
* @return floatval
*/
public static function sum_sales_for_date( $date ) {
$order_query = new \WC_Order_Query( array( 'date_created' => $date ) );
$orders = $order_query->get_orders();
$total = 0;
foreach ( (array) $orders as $order ) {
$total += $order->get_total();
}
return $total;
}
/**
* Possibly add a sales record note.
*/
public static function possibly_add_note() {
/**
* Filter to allow for disabling sales record milestones.
*
* @since 3.7.0
*
* @param boolean default true
*/
$sales_record_notes_enabled = apply_filters( 'woocommerce_admin_sales_record_milestone_enabled', true );
if ( ! $sales_record_notes_enabled ) {
return;
}
$yesterday = gmdate( 'Y-m-d', current_time( 'timestamp', 0 ) - DAY_IN_SECONDS );
$total = self::sum_sales_for_date( $yesterday );
// No sales yesterday? Bail.
if ( 0 >= $total ) {
return;
}
$record_date = get_option( self::RECORD_DATE_OPTION_KEY, '' );
$record_amt = floatval( get_option( self::RECORD_AMOUNT_OPTION_KEY, 0 ) );
// No previous entry? Just enter what we have and return without generating a note.
if ( empty( $record_date ) ) {
update_option( self::RECORD_DATE_OPTION_KEY, $yesterday );
update_option( self::RECORD_AMOUNT_OPTION_KEY, $total );
return;
}
// Otherwise, if yesterdays total bested the record, update AND generate a note.
if ( $total > $record_amt ) {
update_option( self::RECORD_DATE_OPTION_KEY, $yesterday );
update_option( self::RECORD_AMOUNT_OPTION_KEY, $total );
// We only want one sales record note at any time in the inbox, so we delete any other first.
Notes::delete_notes_with_name( self::NOTE_NAME );
$note = self::get_note_with_record_data( $record_date, $record_amt, $yesterday, $total );
$note->save();
}
}
/**
* Get the note with record data.
*
* @param string $record_date record date Y-m-d.
* @param float $record_amt record amount.
* @param string $yesterday yesterday's date Y-m-d.
* @param string $total total sales for yesterday.
*
* @return Note
*/
public static function get_note_with_record_data( $record_date, $record_amt, $yesterday, $total ) {
// Use F jS (March 7th) format for English speaking countries.
if ( substr( get_user_locale(), 0, 2 ) === 'en' ) {
$date_format = 'F jS';
} else {
// otherwise, fallback to the system date format.
$date_format = get_option( 'date_format' );
}
$formatted_yesterday = date_i18n( $date_format, strtotime( $yesterday ) );
$formatted_total = html_entity_decode( wp_strip_all_tags( wc_price( $total ) ) );
$formatted_record_date = date_i18n( $date_format, strtotime( $record_date ) );
$formatted_record_amt = html_entity_decode( wp_strip_all_tags( wc_price( $record_amt ) ) );
$content = sprintf(
/* translators: 1 and 4: Date (e.g. October 16th), 2 and 3: Amount (e.g. $160.00) */
__( 'Woohoo, %1$s was your record day for sales! Net sales was %2$s beating the previous record of %3$s set on %4$s.', 'woocommerce' ),
$formatted_yesterday,
$formatted_total,
$formatted_record_amt,
$formatted_record_date
);
$content_data = (object) array(
'old_record_date' => $record_date,
'old_record_amt' => $record_amt,
'new_record_date' => $yesterday,
'new_record_amt' => $total,
);
$report_url = '?page=wc-admin&path=/analytics/revenue&period=custom&compare=previous_year&after=' . $yesterday . '&before=' . $yesterday;
// And now, create our new note.
$note = new Note();
$note->set_title( __( 'New sales record!', 'woocommerce' ) );
$note->set_content( $content );
$note->set_content_data( $content_data );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'view-report', __( 'View report', 'woocommerce' ), $report_url );
return $note;
}
/**
* Get the note. This is used for localizing the note.
*
* @return Note
*/
public static function get_note() {
$note = Notes::get_note_by_name( self::NOTE_NAME );
if ( ! $note ) {
return false;
}
$content_data = $note->get_content_data();
return self::get_note_with_record_data(
$content_data->old_record_date,
$content_data->old_record_amt,
$content_data->new_record_date,
$content_data->new_record_amt
);
}
}
Notes/OnboardingPayments.php 0000644 00000003364 15154512351 0012160 0 ustar 00 <?php
/**
* WooCommerce Admin: Payments reminder note.
*
* Adds a notes to complete the payment methods.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Onboarding_Payments.
*/
class OnboardingPayments {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-onboarding-payments-reminder';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
// We want to show the note after five days.
if ( ! self::is_wc_admin_active_in_date_range( 'week-1-4', 5 * DAY_IN_SECONDS ) ) {
return;
}
// Check to see if any gateways have been added.
$gateways = WC()->payment_gateways->get_available_payment_gateways();
$enabled_gateways = array_filter(
$gateways,
function( $gateway ) {
return 'yes' === $gateway->enabled;
}
);
if ( ! empty( $enabled_gateways ) ) {
return;
}
$note = new Note();
$note->set_title( __( 'Start accepting payments on your store!', 'woocommerce' ) );
$note->set_content( __( 'Take payments with the provider that’s right for you - choose from 100+ payment gateways for WooCommerce.', 'woocommerce' ) );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_content_data( (object) array() );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'view-payment-gateways',
__( 'Learn more', 'woocommerce' ),
'https://woocommerce.com/product-category/woocommerce-extensions/payment-gateways/?utm_medium=product',
Note::E_WC_ADMIN_NOTE_ACTIONED,
true
);
return $note;
}
}
Notes/OnlineClothingStore.php 0000644 00000005271 15154512351 0012305 0 ustar 00 <?php
/**
* WooCommerce Admin: Start your online clothing store.
*
* Adds a note to ask the client if they are considering starting an online
* clothing store.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Online_Clothing_Store.
*/
class OnlineClothingStore {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-online-clothing-store';
/**
* Returns whether the industries includes fashion-apparel-accessories.
*
* @param array $industries The industries to search.
*
* @return bool Whether the industries includes fashion-apparel-accessories.
*/
private static function is_in_fashion_industry( $industries ) {
foreach ( $industries as $industry ) {
if ( 'fashion-apparel-accessories' === $industry['slug'] ) {
return true;
}
}
return false;
}
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
// We want to show the note after two days.
if ( ! self::is_wc_admin_active_in_date_range( 'week-1', 2 * DAY_IN_SECONDS ) ) {
return;
}
$onboarding_profile = get_option( 'woocommerce_onboarding_profile', array() );
// Confirm that $onboarding_profile is set.
if ( empty( $onboarding_profile ) ) {
return;
}
// Make sure that the person who filled out the OBW was not setting up
// the store for their customer/client.
if (
! isset( $onboarding_profile['setup_client'] ) ||
$onboarding_profile['setup_client']
) {
return;
}
// We need to show the notification when the industry is
// fashion/apparel/accessories.
if ( ! isset( $onboarding_profile['industry'] ) ) {
return;
}
if ( ! self::is_in_fashion_industry( $onboarding_profile['industry'] ) ) {
return;
}
$note = new Note();
$note->set_title( __( 'Start your online clothing store', 'woocommerce' ) );
$note->set_content( __( 'Starting a fashion website is exciting but it may seem overwhelming as well. In this article, we\'ll walk you through the setup process, teach you to create successful product listings, and show you how to market to your ideal audience.', 'woocommerce' ) );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_content_data( (object) array() );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'online-clothing-store',
__( 'Learn more', 'woocommerce' ),
'https://woocommerce.com/posts/starting-an-online-clothing-store/?utm_source=inbox&utm_medium=product',
Note::E_WC_ADMIN_NOTE_ACTIONED
);
return $note;
}
}
Notes/OrderMilestones.php 0000644 00000022226 15154512351 0011471 0 ustar 00 <?php
/**
* WooCommerce Admin (Dashboard) Order Milestones Note Provider.
*
* Adds a note to the merchant's inbox when certain order milestones are reached.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\Notes;
/**
* Order_Milestones
*/
class OrderMilestones {
/**
* Name of the "other milestones" note.
*/
const NOTE_NAME = 'wc-admin-orders-milestone';
/**
* Option key name to store last order milestone.
*/
const LAST_ORDER_MILESTONE_OPTION_KEY = 'woocommerce_admin_last_orders_milestone';
/**
* Hook to process order milestones.
*/
const PROCESS_ORDERS_MILESTONE_HOOK = 'wc_admin_process_orders_milestone';
/**
* Allowed order statuses for calculating milestones.
*
* @var array
*/
protected $allowed_statuses = array(
'pending',
'processing',
'completed',
);
/**
* Orders count cache.
*
* @var int
*/
protected $orders_count = null;
/**
* Further order milestone thresholds.
*
* @var array
*/
protected $milestones = array(
1,
10,
100,
250,
500,
1000,
5000,
10000,
500000,
1000000,
);
/**
* Delay hook attachment until after the WC post types have been registered.
*
* This is required for retrieving the order count.
*/
public function __construct() {
/**
* Filter Order statuses that will count towards milestones.
*
* @since 3.5.0
*
* @param array $allowed_statuses Order statuses that will count towards milestones.
*/
$this->allowed_statuses = apply_filters( 'woocommerce_admin_order_milestone_statuses', $this->allowed_statuses );
add_action( 'woocommerce_after_register_post_type', array( $this, 'init' ) );
register_deactivation_hook( WC_PLUGIN_FILE, array( $this, 'clear_scheduled_event' ) );
}
/**
* Hook everything up.
*/
public function init() {
if ( ! wp_next_scheduled( self::PROCESS_ORDERS_MILESTONE_HOOK ) ) {
wp_schedule_event( time(), 'hourly', self::PROCESS_ORDERS_MILESTONE_HOOK );
}
add_action( 'wc_admin_installed', array( $this, 'backfill_last_milestone' ) );
add_action( self::PROCESS_ORDERS_MILESTONE_HOOK, array( $this, 'possibly_add_note' ) );
}
/**
* Clear out our hourly milestone hook upon plugin deactivation.
*/
public function clear_scheduled_event() {
wp_clear_scheduled_hook( self::PROCESS_ORDERS_MILESTONE_HOOK );
}
/**
* Get the total count of orders (in the allowed statuses).
*
* @param bool $no_cache Optional. Skip cache.
* @return int Total orders count.
*/
public function get_orders_count( $no_cache = false ) {
if ( $no_cache || is_null( $this->orders_count ) ) {
$status_counts = array_map( 'wc_orders_count', $this->allowed_statuses );
$this->orders_count = array_sum( $status_counts );
}
return $this->orders_count;
}
/**
* Backfill the store's current milestone.
*
* Used to avoid celebrating milestones that were reached before plugin activation.
*/
public function backfill_last_milestone() {
// If the milestone notes have been disabled via filter, bail.
if ( ! $this->are_milestones_enabled() ) {
return;
}
$this->set_last_milestone( $this->get_current_milestone() );
}
/**
* Get the store's last milestone.
*
* @return int Last milestone reached.
*/
public function get_last_milestone() {
return get_option( self::LAST_ORDER_MILESTONE_OPTION_KEY, 0 );
}
/**
* Update the last reached milestone.
*
* @param int $milestone Last milestone reached.
*/
public function set_last_milestone( $milestone ) {
update_option( self::LAST_ORDER_MILESTONE_OPTION_KEY, $milestone );
}
/**
* Calculate the current orders milestone.
*
* Based on the threshold values in $this->milestones.
*
* @return int Current orders milestone.
*/
public function get_current_milestone() {
$milestone_reached = 0;
$orders_count = $this->get_orders_count();
foreach ( $this->milestones as $milestone ) {
if ( $milestone <= $orders_count ) {
$milestone_reached = $milestone;
}
}
return $milestone_reached;
}
/**
* Get the appropriate note title for a given milestone.
*
* @param int $milestone Order milestone.
* @return string Note title for the milestone.
*/
public static function get_note_title_for_milestone( $milestone ) {
switch ( $milestone ) {
case 1:
return __( 'First order received', 'woocommerce' );
case 10:
case 100:
case 250:
case 500:
case 1000:
case 5000:
case 10000:
case 500000:
case 1000000:
return sprintf(
/* translators: Number of orders processed. */
__( 'Congratulations on processing %s orders!', 'woocommerce' ),
wc_format_decimal( $milestone )
);
default:
return '';
}
}
/**
* Get the appropriate note content for a given milestone.
*
* @param int $milestone Order milestone.
* @return string Note content for the milestone.
*/
public static function get_note_content_for_milestone( $milestone ) {
switch ( $milestone ) {
case 1:
return __( 'Congratulations on getting your first order! Now is a great time to learn how to manage your orders.', 'woocommerce' );
case 10:
return __( "You've hit the 10 orders milestone! Look at you go. Browse some WooCommerce success stories for inspiration.", 'woocommerce' );
case 100:
case 250:
case 500:
case 1000:
case 5000:
case 10000:
case 500000:
case 1000000:
return __( 'Another order milestone! Take a look at your Orders Report to review your orders to date.', 'woocommerce' );
default:
return '';
}
}
/**
* Get the appropriate note action for a given milestone.
*
* @param int $milestone Order milestone.
* @return array Note actoion (name, label, query) for the milestone.
*/
public static function get_note_action_for_milestone( $milestone ) {
switch ( $milestone ) {
case 1:
return array(
'name' => 'learn-more',
'label' => __( 'Learn more', 'woocommerce' ),
'query' => 'https://woocommerce.com/document/managing-orders/?utm_source=inbox&utm_medium=product',
);
case 10:
return array(
'name' => 'browse',
'label' => __( 'Browse', 'woocommerce' ),
'query' => 'https://woocommerce.com/success-stories/?utm_source=inbox&utm_medium=product',
);
case 100:
case 250:
case 500:
case 1000:
case 5000:
case 10000:
case 500000:
case 1000000:
return array(
'name' => 'review-orders',
'label' => __( 'Review your orders', 'woocommerce' ),
'query' => '?page=wc-admin&path=/analytics/orders',
);
default:
return array(
'name' => '',
'label' => '',
'query' => '',
);
}
}
/**
* Convenience method to see if the milestone notes are enabled.
*
* @return boolean True if milestone notifications are enabled.
*/
public function are_milestones_enabled() {
/**
* Filter to allow for disabling order milestones.
*
* @since 3.7.0
*
* @param boolean default true
*/
$milestone_notes_enabled = apply_filters( 'woocommerce_admin_order_milestones_enabled', true );
return $milestone_notes_enabled;
}
/**
* Get the note. This is used for localizing the note.
*
* @return Note
*/
public static function get_note() {
$note = Notes::get_note_by_name( self::NOTE_NAME );
if ( ! $note ) {
return false;
}
$content_data = $note->get_content_data();
if ( ! isset( $content_data->current_milestone ) ) {
return false;
}
return self::get_note_by_milestone(
$content_data->current_milestone
);
}
/**
* Get the note by milestones.
*
* @param int $current_milestone Current milestone.
*
* @return Note
*/
public static function get_note_by_milestone( $current_milestone ) {
$content_data = (object) array(
'current_milestone' => $current_milestone,
);
$note = new Note();
$note->set_title( self::get_note_title_for_milestone( $current_milestone ) );
$note->set_content( self::get_note_content_for_milestone( $current_milestone ) );
$note->set_content_data( $content_data );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note_action = self::get_note_action_for_milestone( $current_milestone );
$note->add_action( $note_action['name'], $note_action['label'], $note_action['query'] );
return $note;
}
/**
* Checks if a note can and should be added.
*
* @return bool
*/
public function can_be_added() {
// If the milestone notes have been disabled via filter, bail.
if ( ! $this->are_milestones_enabled() ) {
return false;
}
$last_milestone = $this->get_last_milestone();
$current_milestone = $this->get_current_milestone();
if ( $current_milestone <= $last_milestone ) {
return false;
}
return true;
}
/**
* Add milestone notes for other significant thresholds.
*/
public function possibly_add_note() {
if ( ! self::can_be_added() ) {
return;
}
$current_milestone = $this->get_current_milestone();
$this->set_last_milestone( $current_milestone );
// We only want one milestone note at any time.
Notes::delete_notes_with_name( self::NOTE_NAME );
$note = $this->get_note_by_milestone( $current_milestone );
$note->save();
}
}
Notes/PaymentsMoreInfoNeeded.php 0000644 00000004010 15154512351 0012706 0 ustar 00 <?php
/**
* WooCommerce Admin Payments More Info Needed Inbox Note Provider
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
use Automattic\WooCommerce\Internal\Admin\WcPayWelcomePage;
defined( 'ABSPATH' ) || exit;
/**
* PaymentsMoreInfoNeeded
*/
class PaymentsMoreInfoNeeded {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-payments-more-info-needed';
/**
* Should this note exist?
*/
public static function is_applicable() {
return self::should_display_note();
}
/**
* Returns true if we should display the note.
*
* @return bool
*/
public static function should_display_note() {
// WCPay welcome page must not be visible.
if ( WcPayWelcomePage::instance()->must_be_visible() ) {
return false;
}
// More than 30 days since viewing the welcome page.
$exit_survey_timestamp = get_option( 'wcpay_welcome_page_exit_survey_more_info_needed_timestamp', false );
if ( ! $exit_survey_timestamp ||
( time() - $exit_survey_timestamp < 30 * DAY_IN_SECONDS )
) {
return false;
}
return true;
}
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
if ( ! self::should_display_note() ) {
return;
}
$content = __( 'We recently asked you if you wanted more information about WooPayments. Run your business and manage your payments in one place with the solution built and supported by WooCommerce.', 'woocommerce' );
$note = new Note();
$note->set_title( __( 'Payments made simple with WooPayments', 'woocommerce' ) );
$note->set_content( $content );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'learn-more', __( 'Learn more here', 'woocommerce' ), 'https://woocommerce.com/payments/' );
return $note;
}
}
Notes/PaymentsRemindMeLater.php 0000644 00000003676 15154512351 0012574 0 ustar 00 <?php
/**
* WooCommerce Admin Payment Reminder Me later
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
use Automattic\WooCommerce\Internal\Admin\WcPayWelcomePage;
defined( 'ABSPATH' ) || exit;
/**
* PaymentsRemindMeLater
*/
class PaymentsRemindMeLater {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-payments-remind-me-later';
/**
* Should this note exist?
*/
public static function is_applicable() {
return self::should_display_note();
}
/**
* Returns true if we should display the note.
*
* @return bool
*/
public static function should_display_note() {
// WCPay welcome page must be visible.
if ( ! WcPayWelcomePage::instance()->must_be_visible() ) {
return false;
}
// Less than 3 days since viewing welcome page.
$view_timestamp = get_option( 'wcpay_welcome_page_viewed_timestamp', false );
if ( ! $view_timestamp ||
( time() - $view_timestamp < 3 * DAY_IN_SECONDS )
) {
return false;
}
return true;
}
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
if ( ! self::should_display_note() ) {
return;
}
$content = __( 'Save up to $800 in fees by managing transactions with WooPayments. With WooPayments, you can securely accept major cards, Apple Pay, and payments in over 100 currencies.', 'woocommerce' );
$note = new Note();
$note->set_title( __( 'Save big with WooPayments', 'woocommerce' ) );
$note->set_content( $content );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'learn-more', __( 'Learn more', 'woocommerce' ), admin_url( 'admin.php?page=wc-admin&path=/wc-pay-welcome-page' ) );
return $note;
}
}
Notes/PerformanceOnMobile.php 0000644 00000003215 15154512351 0012236 0 ustar 00 <?php
/**
* WooCommerce Admin Performance on mobile note.
*
* Adds a note to download the mobile app, performance on mobile.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Performance_On_Mobile
*/
class PerformanceOnMobile {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-performance-on-mobile';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
// Only add this note if this store is at least 9 months old.
$nine_months_in_seconds = MONTH_IN_SECONDS * 9;
if ( ! self::wc_admin_active_for( $nine_months_in_seconds ) ) {
return;
}
// Check that the previous mobile app notes have not been actioned.
if ( MobileApp::has_note_been_actioned() ) {
return;
}
if ( RealTimeOrderAlerts::has_note_been_actioned() ) {
return;
}
if ( ManageOrdersOnTheGo::has_note_been_actioned() ) {
return;
}
$note = new Note();
$note->set_title( __( 'Track your store performance on mobile', 'woocommerce' ) );
$note->set_content( __( 'Monitor your sales and high performing products with the Woo app.', 'woocommerce' ) );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'learn-more',
__( 'Learn more', 'woocommerce' ),
'https://woocommerce.com/mobile/?utm_source=inbox&utm_medium=product'
);
return $note;
}
}
Notes/PersonalizeStore.php 0000644 00000003646 15154512351 0011670 0 ustar 00 <?php
/**
* WooCommerce Admin Personalize Your Store Note Provider.
*
* Adds a note to the merchant's inbox prompting them to personalize their store.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Personalize_Store
*/
class PersonalizeStore {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-personalize-store';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
// Only show the note to stores with homepage.
$homepage_id = get_option( 'woocommerce_onboarding_homepage_post_id', false );
if ( ! $homepage_id ) {
return;
}
// Show the note after task list is done.
$is_task_list_complete = get_option( 'woocommerce_task_list_complete', false );
// We want to show the note after day 5.
$five_days_in_seconds = 5 * DAY_IN_SECONDS;
if ( ! self::is_wc_admin_active_in_date_range( 'week-1-4', $five_days_in_seconds ) && ! $is_task_list_complete ) {
return;
}
$content = __( 'The homepage is one of the most important entry points in your store. When done right it can lead to higher conversions and engagement. Don\'t forget to personalize the homepage that we created for your store during the onboarding.', 'woocommerce' );
$note = new Note();
$note->set_title( __( 'Personalize your store\'s homepage', 'woocommerce' ) );
$note->set_content( $content );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'personalize-homepage', __( 'Personalize homepage', 'woocommerce' ), admin_url( 'post.php?post=' . $homepage_id . '&action=edit' ), Note::E_WC_ADMIN_NOTE_ACTIONED );
return $note;
}
}
Notes/RealTimeOrderAlerts.php 0000644 00000003012 15154512351 0012214 0 ustar 00 <?php
/**
* WooCommerce Admin Real Time Order Alerts Note.
*
* Adds a note to download the mobile app to monitor store activity.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Real_Time_Order_Alerts
*/
class RealTimeOrderAlerts {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-real-time-order-alerts';
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
// Only add this note if the store is 3 months old.
if ( ! self::is_wc_admin_active_in_date_range( 'month-3-6' ) ) {
return;
}
// Check that the previous mobile app note was not actioned.
if ( MobileApp::has_note_been_actioned() ) {
return;
}
$content = __( 'Get notifications about store activity, including new orders and product reviews directly on your mobile devices with the Woo app.', 'woocommerce' );
$note = new Note();
$note->set_title( __( 'Get real-time order alerts anywhere', 'woocommerce' ) );
$note->set_content( $content );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'learn-more', __( 'Learn more', 'woocommerce' ), 'https://woocommerce.com/mobile/?utm_source=inbox&utm_medium=product' );
return $note;
}
}
Notes/SellingOnlineCourses.php 0000644 00000004600 15154512351 0012455 0 ustar 00 <?php
/**
* WooCommerce Admin: Selling Online Courses note
*
* Adds a note to encourage selling online courses.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
/**
* Selling_Online_Courses
*/
class SellingOnlineCourses {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-selling-online-courses';
/**
* Attach hooks.
*/
public function __construct() {
add_action(
'update_option_' . OnboardingProfile::DATA_OPTION,
array( $this, 'check_onboarding_profile' ),
10,
3
);
}
/**
* Check to see if the profiler options match before possibly adding note.
*
* @param object $old_value The old option value.
* @param object $value The new option value.
* @param string $option The name of the option.
*/
public static function check_onboarding_profile( $old_value, $value, $option ) {
// Skip adding if this store is in the education/learning industry.
if ( ! isset( $value['industry'] ) ) {
return;
}
$industry_slugs = array_column( $value['industry'], 'slug' );
if ( ! in_array( 'education-and-learning', $industry_slugs, true ) ) {
return;
}
self::possibly_add_note();
}
/**
* Get the note.
*
* @return Note
*/
public static function get_note() {
$note = new Note();
$note->set_title( __( 'Do you want to sell online courses?', 'woocommerce' ) );
$note->set_content( __( 'Online courses are a great solution for any business that can teach a new skill. Since courses don’t require physical product development or shipping, they’re affordable, fast to create, and can generate passive income for years to come. In this article, we provide you more information about selling courses using WooCommerce.', 'woocommerce' ) );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_MARKETING );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'learn-more',
__( 'Learn more', 'woocommerce' ),
'https://woocommerce.com/posts/how-to-sell-online-courses-wordpress/?utm_source=inbox&utm_medium=product',
Note::E_WC_ADMIN_NOTE_ACTIONED
);
return $note;
}
}
Notes/TestCheckout.php 0000644 00000005327 15154512352 0010764 0 ustar 00 <?php
/**
* WooCommerce Admin Test Checkout.
*
* Adds a note to remind the user to test their store checkout.
*
* @package WooCommerce\Admin
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Test_Checkout
*/
class TestCheckout {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-test-checkout';
/**
* Completed tasks option name.
*/
const TASK_LIST_TRACKED_TASKS = 'woocommerce_task_list_tracked_completed_tasks';
/**
* Constructor.
*/
public function __construct() {
add_action( 'update_option_' . self::TASK_LIST_TRACKED_TASKS, array( $this, 'possibly_add_note' ) );
}
/**
* Get the note.
*
* @return Note|null
*/
public static function get_note() {
$onboarding_profile = get_option( 'woocommerce_onboarding_profile', array() );
// Confirm that $onboarding_profile is set.
if ( empty( $onboarding_profile ) ) {
return;
}
// Make sure that the person who filled out the OBW was not setting up
// the store for their customer/client.
if (
! isset( $onboarding_profile['setup_client'] ) ||
$onboarding_profile['setup_client']
) {
return;
}
// Make sure payments task was completed.
$completed_tasks = get_option( self::TASK_LIST_TRACKED_TASKS, array() );
if ( ! in_array( 'payments', $completed_tasks, true ) ) {
return;
}
// Make sure that products were added within the previous 1/2 hour.
$query = new \WC_Product_Query(
array(
'limit' => 1,
'status' => 'publish',
'orderby' => 'date',
'order' => 'ASC',
)
);
$products = $query->get_products();
if ( 0 === count( $products ) ) {
return;
}
$oldest_product_timestamp = $products[0]->get_date_created()->getTimestamp();
$half_hour_in_seconds = 30 * MINUTE_IN_SECONDS;
if ( ( time() - $oldest_product_timestamp ) > $half_hour_in_seconds ) {
return;
}
$content = __( 'Make sure that your checkout is working properly before you launch your store. Go through your checkout process in its entirety: from adding a product to your cart, choosing a shipping location, and making a payment.', 'woocommerce' );
$note = new Note();
$note->set_title( __( 'Don\'t forget to test your checkout', 'woocommerce' ) );
$note->set_content( $content );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'test-checkout', __( 'Test checkout', 'woocommerce' ), wc_get_page_permalink( 'shop' ) );
return $note;
}
}
Notes/TrackingOptIn.php 0000644 00000005405 15154512352 0011070 0 ustar 00 <?php
/**
* WooCommerce Admin Usage Tracking Opt In Note Provider.
*
* Adds a Usage Tracking Opt In extension note.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* Tracking_Opt_In
*/
class TrackingOptIn {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-usage-tracking-opt-in';
/**
* Attach hooks.
*/
public function __construct() {
add_action( 'woocommerce_note_action_tracking-opt-in', array( $this, 'opt_in_to_tracking' ) );
}
/**
* Get the note.
*
* @return Note|null
*/
public static function get_note() {
// Only show this note to stores that are opted out.
if ( 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) ) {
return;
}
// We want to show the note after one week.
if ( ! self::is_wc_admin_active_in_date_range( 'week-1-4' ) ) {
return;
}
/* translators: 1: open link to WooCommerce.com settings, 2: open link to WooCommerce.com tracking documentation, 3: close link tag. */
$content_format = __(
'Gathering usage data allows us to improve WooCommerce. Your store will be considered as we evaluate new features, judge the quality of an update, or determine if an improvement makes sense. You can always visit the %1$sSettings%3$s and choose to stop sharing data. %2$sRead more%3$s about what data we collect.',
'woocommerce'
);
$note_content = sprintf(
$content_format,
'<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=woocommerce_com' ) ) . '" target="_blank">',
'<a href="https://woocommerce.com/usage-tracking?utm_medium=product" target="_blank">',
'</a>'
);
$note = new Note();
$note->set_title( __( 'Help WooCommerce improve with usage tracking', 'woocommerce' ) );
$note->set_content( $note_content );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'tracking-opt-in', __( 'Activate usage tracking', 'woocommerce' ), false, Note::E_WC_ADMIN_NOTE_ACTIONED, true );
return $note;
}
/**
* Opt in to usage tracking when note is actioned.
*
* @param Note $note Note being acted upon.
*/
public function opt_in_to_tracking( $note ) {
if ( self::NOTE_NAME === $note->get_name() ) {
// Opt in to tracking and schedule the first data update.
// Same mechanism as in WC_Admin_Setup_Wizard::wc_setup_store_setup_save().
update_option( 'woocommerce_allow_tracking', 'yes' );
wp_schedule_single_event( time() + 10, 'woocommerce_tracker_send_event', array( true ) );
}
}
}
Notes/UnsecuredReportFiles.php 0000644 00000004112 15154512352 0012462 0 ustar 00 <?php
/**
* WooCommerce Admin Unsecured Files Note.
*
* Adds a warning about potentially unsecured files.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
if ( ! class_exists( Note::class ) ) {
class_alias( WC_Admin_Note::class, Note::class );
}
/**
* Unsecured_Report_Files
*/
class UnsecuredReportFiles {
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-remove-unsecured-report-files';
/**
* Get the note.
*
* @return Note|null
*/
public static function get_note() {
$note = new Note();
$note->set_title( __( 'Potentially unsecured files were found in your uploads directory', 'woocommerce' ) );
$note->set_content(
sprintf(
/* translators: 1: opening analytics docs link tag. 2: closing link tag */
__( 'Files that may contain %1$sstore analytics%2$s reports were found in your uploads directory - we recommend assessing and deleting any such files.', 'woocommerce' ),
'<a href="https://woocommerce.com/document/woocommerce-analytics/" target="_blank">',
'</a>'
)
);
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_ERROR );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'learn-more',
__( 'Learn more', 'woocommerce' ),
'https://developer.woocommerce.com/?p=10410',
Note::E_WC_ADMIN_NOTE_UNACTIONED,
true
);
$note->add_action(
'dismiss',
__( 'Dismiss', 'woocommerce' ),
wc_admin_url(),
Note::E_WC_ADMIN_NOTE_ACTIONED,
false
);
return $note;
}
/**
* Add the note if it passes predefined conditions.
*/
public static function possibly_add_note() {
$note = self::get_note();
if ( self::note_exists() ) {
return;
}
$note->save();
}
/**
* Check if the note has been previously added.
*/
public static function note_exists() {
$data_store = \WC_Data_Store::load( 'admin-note' );
$note_ids = $data_store->get_notes_with_name( self::NOTE_NAME );
return ! empty( $note_ids );
}
}
Notes/WooCommercePayments.php 0000644 00000014400 15154512352 0012307 0 ustar 00 <?php
/**
* WooCommerce Admin WooCommerce Payments Note Provider.
*
* Adds a note to the merchant's inbox showing the benefits of the WooCommerce Payments.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\Notes;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
/**
* WooCommerce_Payments
*/
class WooCommercePayments {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-woocommerce-payments';
/**
* Name of the note for use in the database.
*/
const PLUGIN_SLUG = 'woocommerce-payments';
/**
* Name of the note for use in the database.
*/
const PLUGIN_FILE = 'woocommerce-payments/woocommerce-payments.php';
/**
* Attach hooks.
*/
public function __construct() {
add_action( 'init', array( $this, 'install_on_action' ) );
add_action( 'wc-admin-woocommerce-payments_add_note', array( $this, 'add_note' ) );
}
/**
* Maybe add a note on WooCommerce Payments for US based sites older than a week without the plugin installed.
*/
public static function possibly_add_note() {
if ( ! self::is_wc_admin_active_in_date_range( 'week-1-4' ) || 'US' !== WC()->countries->get_base_country() ) {
return;
}
$data_store = Notes::load_data_store();
// We already have this note? Then mark the note as actioned.
$note_ids = $data_store->get_notes_with_name( self::NOTE_NAME );
if ( ! empty( $note_ids ) ) {
$note_id = array_pop( $note_ids );
$note = Notes::get_note( $note_id );
if ( false === $note ) {
return;
}
// If the WooCommerce Payments plugin was installed after the note was created, make sure it's marked as actioned.
if ( self::is_installed() && Note::E_WC_ADMIN_NOTE_ACTIONED !== $note->get_status() ) {
$note->set_status( Note::E_WC_ADMIN_NOTE_ACTIONED );
$note->save();
}
return;
}
$current_date = new \DateTime();
$publish_date = new \DateTime( '2020-04-14' );
if ( $current_date >= $publish_date ) {
$note = self::get_note();
if ( self::can_be_added() ) {
$note->save();
}
return;
} else {
$hook_name = sprintf( '%s_add_note', self::NOTE_NAME );
if ( ! WC()->queue()->get_next( $hook_name ) ) {
WC()->queue()->schedule_single( $publish_date->getTimestamp(), $hook_name );
}
}
}
/**
* Add a note about WooCommerce Payments.
*
* @return Note
*/
public static function get_note() {
$note = new Note();
$note->set_title( __( 'Try the new way to get paid', 'woocommerce' ) );
$note->set_content(
__( 'Securely accept credit and debit cards on your site. Manage transactions without leaving your WordPress dashboard. Only with <strong>WooPayments</strong>.', 'woocommerce' ) .
'<br><br>' .
sprintf(
/* translators: 1: opening link tag, 2: closing tag */
__( 'By clicking "Get started", you agree to our %1$sTerms of Service%2$s', 'woocommerce' ),
'<a href="https://wordpress.com/tos/" target="_blank">',
'</a>'
)
);
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_MARKETING );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'learn-more', __( 'Learn more', 'woocommerce' ), 'https://woocommerce.com/payments/?utm_medium=product', Note::E_WC_ADMIN_NOTE_UNACTIONED );
$note->add_action( 'get-started', __( 'Get started', 'woocommerce' ), wc_admin_url( '&action=setup-woocommerce-payments' ), Note::E_WC_ADMIN_NOTE_ACTIONED, true );
$note->add_nonce_to_action( 'get-started', 'setup-woocommerce-payments', '' );
// Create the note as "actioned" if the plugin is already installed.
if ( self::is_installed() ) {
$note->set_status( Note::E_WC_ADMIN_NOTE_ACTIONED );
}
return $note;
}
/**
* Check if the WooCommerce Payments plugin is active or installed.
*/
protected static function is_installed() {
if ( defined( 'WC_Payments' ) ) {
return true;
}
include_once ABSPATH . '/wp-admin/includes/plugin.php';
return 0 === validate_plugin( self::PLUGIN_FILE );
}
/**
* Install and activate WooCommerce Payments.
*
* @return boolean Whether the plugin was successfully activated.
*/
private function install_and_activate_wcpay() {
$install_request = array( 'plugins' => self::PLUGIN_SLUG );
$installer = new \Automattic\WooCommerce\Admin\API\Plugins();
$result = $installer->install_plugins( $install_request );
if ( is_wp_error( $result ) ) {
return false;
}
wc_admin_record_tracks_event( 'woocommerce_payments_install', array( 'context' => 'inbox' ) );
$activate_request = array( 'plugins' => self::PLUGIN_SLUG );
$result = $installer->activate_plugins( $activate_request );
if ( is_wp_error( $result ) ) {
return false;
}
return true;
}
/**
* Install & activate WooCommerce Payments plugin, and redirect to setup.
*/
public function install_on_action() {
// TODO: Need to validate this request more strictly since we're taking install actions directly?
if (
! isset( $_GET['page'] ) ||
'wc-admin' !== $_GET['page'] ||
! isset( $_GET['action'] ) ||
'setup-woocommerce-payments' !== $_GET['action']
) {
return;
}
$data_store = Notes::load_data_store();
// We already have this note? Then mark the note as actioned.
$note_ids = $data_store->get_notes_with_name( self::NOTE_NAME );
if ( empty( $note_ids ) ) {
return;
}
$note_id = array_pop( $note_ids );
$note = Notes::get_note( $note_id );
if ( false === $note ) {
return;
}
$action = $note->get_action( 'get-started' );
if ( ! $action ||
( isset( $action->nonce_action ) &&
(
empty( $_GET['_wpnonce'] ) ||
! wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), $action->nonce_action ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
)
)
) {
return;
}
if ( ! current_user_can( 'install_plugins' ) ) {
return;
}
$this->install_and_activate_wcpay();
// WooCommerce Payments is installed at this point, so link straight into the onboarding flow.
$connect_url = add_query_arg(
array(
'wcpay-connect' => '1',
'_wpnonce' => wp_create_nonce( 'wcpay-connect' ),
),
admin_url()
);
wp_safe_redirect( $connect_url );
exit;
}
}
Notes/WooCommerceSubscriptions.php 0000644 00000003602 15154512352 0013360 0 ustar 00 <?php
/**
* WooCommerce Admin: WooCommerce Subscriptions.
*
* Adds a note to learn more about WooCommerce Subscriptions.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
/**
* WooCommerce_Subscriptions.
*/
class WooCommerceSubscriptions {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-woocommerce-subscriptions';
/**
* Get the note.
*
* @return Note|null
*/
public static function get_note() {
$onboarding_data = get_option( OnboardingProfile::DATA_OPTION, array() );
if ( ! isset( $onboarding_data['product_types'] ) || ! in_array( 'subscriptions', $onboarding_data['product_types'], true ) ) {
return;
}
if ( ! self::is_wc_admin_active_in_date_range( 'week-1', DAY_IN_SECONDS ) ) {
return;
}
$note = new Note();
$note->set_title( __( 'Do you need more info about WooCommerce Subscriptions?', 'woocommerce' ) );
$note->set_content( __( 'WooCommerce Subscriptions allows you to introduce a variety of subscriptions for physical or virtual products and services. Create product-of-the-month clubs, weekly service subscriptions or even yearly software billing packages. Add sign-up fees, offer free trials, or set expiration periods.', 'woocommerce' ) );
$note->set_type( Note::E_WC_ADMIN_NOTE_MARKETING );
$note->set_name( self::NOTE_NAME );
$note->set_content_data( (object) array() );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'learn-more',
__( 'Learn More', 'woocommerce' ),
'https://woocommerce.com/products/woocommerce-subscriptions/?utm_source=inbox&utm_medium=product',
Note::E_WC_ADMIN_NOTE_UNACTIONED,
true
);
return $note;
}
}
Notes/WooSubscriptionsNotes.php 0000644 00000034446 15154512352 0012730 0 ustar 00 <?php
/**
* WooCommerce Admin (Dashboard) WooCommerce.com Extension Subscriptions Note Provider.
*
* Adds notes to the merchant's inbox concerning WooCommerce.com extension subscriptions.
*/
namespace Automattic\WooCommerce\Internal\Admin\Notes;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\Notes;
use Automattic\WooCommerce\Admin\PageController;
/**
* Woo_Subscriptions_Notes
*/
class WooSubscriptionsNotes {
const LAST_REFRESH_OPTION_KEY = 'woocommerce_admin-wc-helper-last-refresh';
const NOTE_NAME = 'wc-admin-wc-helper-connection';
const CONNECTION_NOTE_NAME = 'wc-admin-wc-helper-connection';
const SUBSCRIPTION_NOTE_NAME = 'wc-admin-wc-helper-subscription';
const NOTIFY_WHEN_DAYS_LEFT = 60;
/**
* We want to bubble up expiration notices when they cross certain age
* thresholds. PHP 5.2 doesn't support constant arrays, so we do this.
*
* @return array
*/
private function get_bump_thresholds() {
return array( 60, 45, 20, 7, 1 ); // days.
}
/**
* Hook all the things.
*/
public function __construct() {
add_action( 'admin_head', array( $this, 'admin_head' ) );
add_action( 'update_option_woocommerce_helper_data', array( $this, 'update_option_woocommerce_helper_data' ), 10, 2 );
}
/**
* Reacts to changes in the helper option.
*
* @param array $old_value The previous value of the option.
* @param array $value The new value of the option.
*/
public function update_option_woocommerce_helper_data( $old_value, $value ) {
if ( ! is_array( $old_value ) ) {
$old_value = array();
}
if ( ! is_array( $value ) ) {
$value = array();
}
$old_auth = array_key_exists( 'auth', $old_value ) ? $old_value['auth'] : array();
$new_auth = array_key_exists( 'auth', $value ) ? $value['auth'] : array();
$old_token = array_key_exists( 'access_token', $old_auth ) ? $old_auth['access_token'] : '';
$new_token = array_key_exists( 'access_token', $new_auth ) ? $new_auth['access_token'] : '';
// The site just disconnected.
if ( ! empty( $old_token ) && empty( $new_token ) ) {
$this->remove_notes();
$this->add_no_connection_note();
return;
}
// The site is connected.
if ( $this->is_connected() ) {
$this->remove_notes();
$this->refresh_subscription_notes();
return;
}
}
/**
* Runs on `admin_head` hook. Checks the connection and refreshes subscription notes on relevant pages.
*/
public function admin_head() {
if ( ! PageController::is_admin_or_embed_page() ) {
// To avoid unnecessarily calling Helper API, we only want to refresh subscription notes,
// if the request is initiated from the wc admin dashboard or a WC related page which includes
// the Activity button in WC header.
return;
}
$this->check_connection();
if ( $this->is_connected() ) {
$refresh_notes = false;
// Did the user just do something on the helper page?.
if ( isset( $_GET['wc-helper-status'] ) ) { // @codingStandardsIgnoreLine.
$refresh_notes = true;
}
// Has it been more than a day since we last checked?
// Note: We do it this way and not wp_scheduled_task since WC_Helper_Options is not loaded for cron.
$time_now_gmt = current_time( 'timestamp', 0 );
$last_refresh = intval( get_option( self::LAST_REFRESH_OPTION_KEY, 0 ) );
if ( $last_refresh + DAY_IN_SECONDS <= $time_now_gmt ) {
update_option( self::LAST_REFRESH_OPTION_KEY, $time_now_gmt );
$refresh_notes = true;
}
if ( $refresh_notes ) {
$this->refresh_subscription_notes();
}
}
}
/**
* Checks the connection. Adds a note (as necessary) if there is no connection.
*/
public function check_connection() {
if ( ! $this->is_connected() ) {
$data_store = Notes::load_data_store();
$note_ids = $data_store->get_notes_with_name( self::CONNECTION_NOTE_NAME );
if ( ! empty( $note_ids ) ) {
// We already have a connection note. Exit early.
return;
}
$this->remove_notes();
$this->add_no_connection_note();
}
}
/**
* Whether or not we think the site is currently connected to WooCommerce.com.
*
* @return bool
*/
public function is_connected() {
$auth = \WC_Helper_Options::get( 'auth' );
return ( ! empty( $auth['access_token'] ) );
}
/**
* Returns the WooCommerce.com provided site ID for this site.
*
* @return int|false
*/
public function get_connected_site_id() {
if ( ! $this->is_connected() ) {
return false;
}
$auth = \WC_Helper_Options::get( 'auth' );
return absint( $auth['site_id'] );
}
/**
* Returns an array of product_ids whose subscriptions are active on this site.
*
* @return array
*/
public function get_subscription_active_product_ids() {
$site_id = $this->get_connected_site_id();
if ( ! $site_id ) {
return array();
}
$product_ids = array();
if ( $this->is_connected() ) {
$subscriptions = \WC_Helper::get_subscriptions();
foreach ( (array) $subscriptions as $subscription ) {
if ( in_array( $site_id, $subscription['connections'], true ) ) {
$product_ids[] = $subscription['product_id'];
}
}
}
return $product_ids;
}
/**
* Clears all connection or subscription notes.
*/
public function remove_notes() {
Notes::delete_notes_with_name( self::CONNECTION_NOTE_NAME );
Notes::delete_notes_with_name( self::SUBSCRIPTION_NOTE_NAME );
}
/**
* Adds a note prompting to connect to WooCommerce.com.
*/
public function add_no_connection_note() {
$note = self::get_note();
$note->save();
}
/**
* Get the WooCommerce.com connection note
*/
public static function get_note() {
$note = new Note();
$note->set_title( __( 'Connect to WooCommerce.com', 'woocommerce' ) );
$note->set_content( __( 'Connect to get important product notifications and updates.', 'woocommerce' ) );
$note->set_content_data( (object) array() );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::CONNECTION_NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action(
'connect',
__( 'Connect', 'woocommerce' ),
'?page=wc-addons§ion=helper',
Note::E_WC_ADMIN_NOTE_UNACTIONED
);
return $note;
}
/**
* Gets the product_id (if any) associated with a note.
*
* @param Note $note The note object to interrogate.
* @return int|false
*/
public function get_product_id_from_subscription_note( &$note ) {
$content_data = $note->get_content_data();
if ( property_exists( $content_data, 'product_id' ) ) {
return intval( $content_data->product_id );
}
return false;
}
/**
* Removes notes for product_ids no longer active on this site.
*/
public function prune_inactive_subscription_notes() {
$active_product_ids = $this->get_subscription_active_product_ids();
$data_store = Notes::load_data_store();
$note_ids = $data_store->get_notes_with_name( self::SUBSCRIPTION_NOTE_NAME );
foreach ( (array) $note_ids as $note_id ) {
$note = Notes::get_note( $note_id );
$product_id = $this->get_product_id_from_subscription_note( $note );
if ( ! empty( $product_id ) ) {
if ( ! in_array( $product_id, $active_product_ids, true ) ) {
$note->delete();
}
}
}
}
/**
* Finds a note for a given product ID, if the note exists at all.
*
* @param int $product_id The product ID to search for.
* @return Note|false
*/
public function find_note_for_product_id( $product_id ) {
$product_id = intval( $product_id );
$data_store = Notes::load_data_store();
$note_ids = $data_store->get_notes_with_name( self::SUBSCRIPTION_NOTE_NAME );
foreach ( (array) $note_ids as $note_id ) {
$note = Notes::get_note( $note_id );
$found_product_id = $this->get_product_id_from_subscription_note( $note );
if ( $product_id === $found_product_id ) {
return $note;
}
}
return false;
}
/**
* Deletes a note for a given product ID, if the note exists at all.
*
* @param int $product_id The product ID to search for.
*/
public function delete_any_note_for_product_id( $product_id ) {
$product_id = intval( $product_id );
$note = $this->find_note_for_product_id( $product_id );
if ( $note ) {
$note->delete();
}
}
/**
* Adds or updates a note for an expiring subscription.
*
* @param array $subscription The subscription to work with.
*/
public function add_or_update_subscription_expiring( $subscription ) {
$product_id = $subscription['product_id'];
$product_name = $subscription['product_name'];
$expires = intval( $subscription['expires'] );
$time_now_gmt = current_time( 'timestamp', 0 );
$days_until_expiration = intval( ceil( ( $expires - $time_now_gmt ) / DAY_IN_SECONDS ) );
$note = $this->find_note_for_product_id( $product_id );
if ( $note ) {
$content_data = $note->get_content_data();
if ( property_exists( $content_data, 'days_until_expiration' ) ) {
// Note: There is no reason this property should not exist. This is just defensive programming.
$note_days_until_expiration = intval( $content_data->days_until_expiration );
if ( $days_until_expiration === $note_days_until_expiration ) {
// Note is already up to date. Bail.
return;
}
// If we have a note and we are at or have crossed a threshold, we should delete
// the old note and create a new one, thereby "bumping" the note to the top of the inbox.
$bump_thresholds = $this->get_bump_thresholds();
$crossing_threshold = false;
foreach ( (array) $bump_thresholds as $bump_threshold ) {
if ( ( $note_days_until_expiration > $bump_threshold ) && ( $days_until_expiration <= $bump_threshold ) ) {
$note->delete();
$note = false;
continue;
}
}
}
}
$note_title = sprintf(
/* translators: name of the extension subscription expiring soon */
__( '%s subscription expiring soon', 'woocommerce' ),
$product_name
);
$note_content = sprintf(
/* translators: number of days until the subscription expires */
__( 'Your subscription expires in %d days. Enable autorenew to avoid losing updates and access to support.', 'woocommerce' ),
$days_until_expiration
);
$note_content_data = (object) array(
'product_id' => $product_id,
'product_name' => $product_name,
'expired' => false,
'days_until_expiration' => $days_until_expiration,
);
if ( ! $note ) {
$note = new Note();
}
// Reset everything in case we are repurposing an expired note as an expiring note.
$note->set_title( $note_title );
$note->set_type( Note::E_WC_ADMIN_NOTE_WARNING );
$note->set_name( self::SUBSCRIPTION_NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->clear_actions();
$note->add_action(
'enable-autorenew',
__( 'Enable Autorenew', 'woocommerce' ),
'https://woocommerce.com/my-account/my-subscriptions/?utm_medium=product'
);
$note->set_content( $note_content );
$note->set_content_data( $note_content_data );
$note->save();
}
/**
* Adds a note for an expired subscription, or updates an expiring note to expired.
*
* @param array $subscription The subscription to work with.
*/
public function add_or_update_subscription_expired( $subscription ) {
$product_id = $subscription['product_id'];
$product_name = $subscription['product_name'];
$product_page = $subscription['product_url'];
$expires = intval( $subscription['expires'] );
$expires_date = gmdate( 'F jS', $expires );
$note = $this->find_note_for_product_id( $product_id );
if ( $note ) {
$note_content_data = $note->get_content_data();
if ( $note_content_data->expired ) {
// We've already got a full fledged expired note for this. Bail.
// Expired notes' content don't change with time.
return;
}
}
$note_title = sprintf(
/* translators: name of the extension subscription that expired */
__( '%s subscription expired', 'woocommerce' ),
$product_name
);
$note_content = sprintf(
/* translators: date the subscription expired, e.g. Jun 7th 2018 */
__( 'Your subscription expired on %s. Get a new subscription to continue receiving updates and access to support.', 'woocommerce' ),
$expires_date
);
$note_content_data = (object) array(
'product_id' => $product_id,
'product_name' => $product_name,
'expired' => true,
'expires' => $expires,
'expires_date' => $expires_date,
);
if ( ! $note ) {
$note = new Note();
}
$note->set_title( $note_title );
$note->set_content( $note_content );
$note->set_content_data( $note_content_data );
$note->set_type( Note::E_WC_ADMIN_NOTE_WARNING );
$note->set_name( self::SUBSCRIPTION_NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->clear_actions();
$note->add_action(
'renew-subscription',
__( 'Renew Subscription', 'woocommerce' ),
$product_page
);
$note->save();
}
/**
* For each active subscription on this site, checks the expiration date and creates/updates/deletes notes.
*/
public function refresh_subscription_notes() {
if ( ! $this->is_connected() ) {
return;
}
$this->prune_inactive_subscription_notes();
$subscriptions = \WC_Helper::get_subscriptions();
$active_product_ids = $this->get_subscription_active_product_ids();
foreach ( (array) $subscriptions as $subscription ) {
// Only concern ourselves with active products.
$product_id = $subscription['product_id'];
if ( ! in_array( $product_id, $active_product_ids, true ) ) {
continue;
}
// If the subscription will auto-renew, clean up and exit.
if ( $subscription['autorenew'] ) {
$this->delete_any_note_for_product_id( $product_id );
continue;
}
// If the subscription is not expiring by the first threshold, clean up and exit.
$bump_thresholds = $this->get_bump_thresholds();
$first_threshold = DAY_IN_SECONDS * $bump_thresholds[0];
$expires = intval( $subscription['expires'] );
$time_now_gmt = current_time( 'timestamp', 0 );
if ( $expires > $time_now_gmt + $first_threshold ) {
$this->delete_any_note_for_product_id( $product_id );
continue;
}
// Otherwise, if the subscription can still have auto-renew enabled, let them know that now.
if ( $expires > $time_now_gmt ) {
$this->add_or_update_subscription_expiring( $subscription );
continue;
}
// If we got this far, the subscription has completely expired, let them know.
$this->add_or_update_subscription_expired( $subscription );
}
}
}
Onboarding/Onboarding.php 0000644 00000001073 15154512352 0011425 0 ustar 00 <?php
/**
* WooCommerce Onboarding
*/
namespace Automattic\WooCommerce\Internal\Admin\Onboarding;
/**
* Initializes backend logic for the onboarding process.
*/
class Onboarding {
/**
* Initialize onboarding functionality.
*/
public static function init() {
OnboardingHelper::instance()->init();
OnboardingIndustries::init();
OnboardingJetpack::instance()->init();
OnboardingMailchimp::instance()->init();
OnboardingProfile::init();
OnboardingSetupWizard::instance()->init();
OnboardingSync::instance()->init();
OnboardingThemes::init();
}
}
Onboarding/OnboardingHelper.php 0000644 00000011502 15154512352 0012563 0 ustar 00 <?php
/**
* WooCommerce Onboarding Helper
*/
namespace Automattic\WooCommerce\Internal\Admin\Onboarding;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;
/**
* Contains backend logic for the onboarding profile and checklist feature.
*/
class OnboardingHelper {
/**
* Class instance.
*
* @var OnboardingHelper instance
*/
private static $instance = null;
/**
* Get class instance.
*/
final public static function instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init.
*/
public function init() {
if ( ! is_admin() ) {
return;
}
add_action( 'current_screen', array( $this, 'add_help_tab' ), 60 );
add_action( 'current_screen', array( $this, 'reset_task_list' ) );
add_action( 'current_screen', array( $this, 'reset_extended_task_list' ) );
}
/**
* Update the help tab setup link to reset the onboarding profiler.
*/
public function add_help_tab() {
if ( ! function_exists( 'wc_get_screen_ids' ) ) {
return;
}
$screen = get_current_screen();
if ( ! $screen || ! in_array( $screen->id, wc_get_screen_ids(), true ) ) {
return;
}
// Remove the old help tab if it exists.
$help_tabs = $screen->get_help_tabs();
foreach ( $help_tabs as $help_tab ) {
if ( 'woocommerce_onboard_tab' !== $help_tab['id'] ) {
continue;
}
$screen->remove_help_tab( 'woocommerce_onboard_tab' );
}
// Add the new help tab.
$help_tab = array(
'title' => __( 'Setup wizard', 'woocommerce' ),
'id' => 'woocommerce_onboard_tab',
);
$setup_list = TaskLists::get_list( 'setup' );
$extended_list = TaskLists::get_list( 'extended' );
if ( $setup_list ) {
$help_tab['content'] = '<h2>' . __( 'WooCommerce Onboarding', 'woocommerce' ) . '</h2>';
$help_tab['content'] .= '<h3>' . __( 'Profile Setup Wizard', 'woocommerce' ) . '</h3>';
$help_tab['content'] .= '<p>' . __( 'If you need to access the setup wizard again, please click on the button below.', 'woocommerce' ) . '</p>' .
'<p><a href="' . wc_admin_url( '&path=/setup-wizard' ) . '" class="button button-primary">' . __( 'Setup wizard', 'woocommerce' ) . '</a></p>';
$help_tab['content'] .= '<h3>' . __( 'Task List', 'woocommerce' ) . '</h3>';
$help_tab['content'] .= '<p>' . __( 'If you need to enable or disable the task lists, please click on the button below.', 'woocommerce' ) . '</p>' .
( $setup_list->is_hidden()
? '<p><a href="' . wc_admin_url( '&reset_task_list=1' ) . '" class="button button-primary">' . __( 'Enable', 'woocommerce' ) . '</a></p>'
: '<p><a href="' . wc_admin_url( '&reset_task_list=0' ) . '" class="button button-primary">' . __( 'Disable', 'woocommerce' ) . '</a></p>'
);
}
if ( $extended_list ) {
$help_tab['content'] .= '<h3>' . __( 'Extended task List', 'woocommerce' ) . '</h3>';
$help_tab['content'] .= '<p>' . __( 'If you need to enable or disable the extended task lists, please click on the button below.', 'woocommerce' ) . '</p>' .
( $extended_list->is_hidden()
? '<p><a href="' . wc_admin_url( '&reset_extended_task_list=1' ) . '" class="button button-primary">' . __( 'Enable', 'woocommerce' ) . '</a></p>'
: '<p><a href="' . wc_admin_url( '&reset_extended_task_list=0' ) . '" class="button button-primary">' . __( 'Disable', 'woocommerce' ) . '</a></p>'
);
}
$screen->add_help_tab( $help_tab );
}
/**
* Reset the onboarding task list and redirect to the dashboard.
*/
public function reset_task_list() {
if (
! PageController::is_admin_page() ||
! isset( $_GET['reset_task_list'] ) // phpcs:ignore CSRF ok.
) {
return;
}
$task_list = TaskLists::get_list( 'setup' );
if ( ! $task_list ) {
return;
}
$show = 1 === absint( $_GET['reset_task_list'] ); // phpcs:ignore CSRF ok.
$update = $show ? $task_list->unhide() : $task_list->hide(); // phpcs:ignore CSRF ok.
if ( $update ) {
wc_admin_record_tracks_event(
'tasklist_toggled',
array(
'status' => $show ? 'enabled' : 'disabled',
)
);
}
wp_safe_redirect( wc_admin_url() );
exit;
}
/**
* Reset the extended task list and redirect to the dashboard.
*/
public function reset_extended_task_list() {
if (
! PageController::is_admin_page() ||
! isset( $_GET['reset_extended_task_list'] ) // phpcs:ignore CSRF ok.
) {
return;
}
$task_list = TaskLists::get_list( 'extended' );
if ( ! $task_list ) {
return;
}
$show = 1 === absint( $_GET['reset_extended_task_list'] ); // phpcs:ignore CSRF ok.
$update = $show ? $task_list->unhide() : $task_list->hide(); // phpcs:ignore CSRF ok.
if ( $update ) {
wc_admin_record_tracks_event(
'extended_tasklist_toggled',
array(
'status' => $show ? 'disabled' : 'enabled',
)
);
}
wp_safe_redirect( wc_admin_url() );
exit;
}
}
Onboarding/OnboardingIndustries.php 0000644 00000005020 15154512352 0013473 0 ustar 00 <?php
/**
* WooCommerce Onboarding Industries
*/
namespace Automattic\WooCommerce\Internal\Admin\Onboarding;
/**
* Logic around onboarding industries.
*/
class OnboardingIndustries {
/**
* Init.
*/
public static function init() {
add_filter( 'woocommerce_admin_onboarding_preloaded_data', array( __CLASS__, 'preload_data' ) );
}
/**
* Get a list of allowed industries for the onboarding wizard.
*
* @return array
*/
public static function get_allowed_industries() {
/* With "use_description" we turn the description input on. With "description_label" we set the input label */
return apply_filters(
'woocommerce_admin_onboarding_industries',
array(
'fashion-apparel-accessories' => array(
'label' => __( 'Fashion, apparel, and accessories', 'woocommerce' ),
'use_description' => false,
'description_label' => '',
),
'health-beauty' => array(
'label' => __( 'Health and beauty', 'woocommerce' ),
'use_description' => false,
'description_label' => '',
),
'electronics-computers' => array(
'label' => __( 'Electronics and computers', 'woocommerce' ),
'use_description' => false,
'description_label' => '',
),
'food-drink' => array(
'label' => __( 'Food and drink', 'woocommerce' ),
'use_description' => false,
'description_label' => '',
),
'home-furniture-garden' => array(
'label' => __( 'Home, furniture, and garden', 'woocommerce' ),
'use_description' => false,
'description_label' => '',
),
'cbd-other-hemp-derived-products' => array(
'label' => __( 'CBD and other hemp-derived products', 'woocommerce' ),
'use_description' => false,
'description_label' => '',
),
'education-and-learning' => array(
'label' => __( 'Education and learning', 'woocommerce' ),
'use_description' => false,
'description_label' => '',
),
'other' => array(
'label' => __( 'Other', 'woocommerce' ),
'use_description' => true,
'description_label' => __( 'Description', 'woocommerce' ),
),
)
);
}
/**
* Add preloaded data to onboarding.
*
* @param array $settings Component settings.
* @return array
*/
public static function preload_data( $settings ) {
$settings['onboarding']['industries'] = self::get_allowed_industries();
return $settings;
}
}
Onboarding/OnboardingJetpack.php 0000644 00000003464 15154512352 0012735 0 ustar 00 <?php
/**
* WooCommerce Onboarding Jetpack
*/
namespace Automattic\WooCommerce\Internal\Admin\Onboarding;
/**
* Contains logic around Jetpack setup during onboarding.
*/
class OnboardingJetpack {
/**
* Class instance.
*
* @var OnboardingJetpack instance
*/
private static $instance = null;
/**
* Get class instance.
*/
final public static function instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init.
*/
public function init() {
add_action( 'woocommerce_admin_plugins_pre_activate', array( $this, 'activate_and_install_jetpack_ahead_of_wcpay' ) );
add_action( 'woocommerce_admin_plugins_pre_install', array( $this, 'activate_and_install_jetpack_ahead_of_wcpay' ) );
// Always hook into Jetpack connection even if outside of admin.
add_action( 'jetpack_site_registered', array( $this, 'set_woocommerce_setup_jetpack_opted_in' ) );
}
/**
* Sets the woocommerce_setup_jetpack_opted_in to true when Jetpack connects to WPCOM.
*/
public function set_woocommerce_setup_jetpack_opted_in() {
update_option( 'woocommerce_setup_jetpack_opted_in', true );
}
/**
* Ensure that Jetpack gets installed and activated ahead of WooCommerce Payments
* if both are being installed/activated at the same time.
*
* See: https://github.com/Automattic/woocommerce-payments/issues/1663
* See: https://github.com/Automattic/jetpack/issues/19624
*
* @param array $plugins A list of plugins to install or activate.
*
* @return array
*/
public function activate_and_install_jetpack_ahead_of_wcpay( $plugins ) {
if ( in_array( 'jetpack', $plugins, true ) && in_array( 'woocommerce-payments', $plugins, true ) ) {
array_unshift( $plugins, 'jetpack' );
$plugins = array_unique( $plugins );
}
return $plugins;
}
}
Onboarding/OnboardingMailchimp.php 0000644 00000002301 15154512352 0013244 0 ustar 00 <?php
/**
* WooCommerce Onboarding Mailchimp
*/
namespace Automattic\WooCommerce\Internal\Admin\Onboarding;
use Automattic\WooCommerce\Internal\Admin\Schedulers\MailchimpScheduler;
/**
* Logic around updating Mailchimp during onboarding.
*/
class OnboardingMailchimp {
/**
* Class instance.
*
* @var OnboardingMailchimp instance
*/
private static $instance = null;
/**
* Get class instance.
*/
final public static function instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init.
*/
public function init() {
add_action( 'woocommerce_onboarding_profile_data_updated', array( $this, 'on_profile_data_updated' ), 10, 2 );
}
/**
* Reset MailchimpScheduler if profile data is being updated with a new email.
*
* @param array $existing_data Existing option data.
* @param array $updating_data Updating option data.
*/
public function on_profile_data_updated( $existing_data, $updating_data ) {
if (
isset( $existing_data['store_email'] ) &&
isset( $updating_data['store_email'] ) &&
$existing_data['store_email'] !== $updating_data['store_email']
) {
MailchimpScheduler::reset();
}
}
}
Onboarding/OnboardingProducts.php 0000644 00000012534 15154512352 0013155 0 ustar 00 <?php
/**
* WooCommerce Onboarding Products
*/
namespace Automattic\WooCommerce\Internal\Admin\Onboarding;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use Automattic\WooCommerce\Admin\Loader;
use Automattic\WooCommerce\Admin\PluginsHelper;
/**
* Class for handling product types and data around product types.
*/
class OnboardingProducts {
/**
* Name of product data transient.
*
* @var string
*/
const PRODUCT_DATA_TRANSIENT = 'wc_onboarding_product_data';
/**
* Get a list of allowed product types for the onboarding wizard.
*
* @return array
*/
public static function get_allowed_product_types() {
$products = array(
'physical' => array(
'label' => __( 'Physical products', 'woocommerce' ),
'default' => true,
),
'downloads' => array(
'label' => __( 'Downloads', 'woocommerce' ),
),
'subscriptions' => array(
'label' => __( 'Subscriptions', 'woocommerce' ),
),
'memberships' => array(
'label' => __( 'Memberships', 'woocommerce' ),
'product' => 958589,
),
'bookings' => array(
'label' => __( 'Bookings', 'woocommerce' ),
'product' => 390890,
),
'product-bundles' => array(
'label' => __( 'Bundles', 'woocommerce' ),
'product' => 18716,
),
'product-add-ons' => array(
'label' => __( 'Customizable products', 'woocommerce' ),
'product' => 18618,
),
);
$base_location = wc_get_base_location();
$has_cbd_industry = false;
if ( 'US' === $base_location['country'] ) {
$profile = get_option( OnboardingProfile::DATA_OPTION, array() );
if ( ! empty( $profile['industry'] ) ) {
$has_cbd_industry = in_array( 'cbd-other-hemp-derived-products', array_column( $profile['industry'], 'slug' ), true );
}
}
if ( ! Features::is_enabled( 'subscriptions' ) || 'US' !== $base_location['country'] || $has_cbd_industry ) {
$products['subscriptions']['product'] = 27147;
}
return apply_filters( 'woocommerce_admin_onboarding_product_types', $products );
}
/**
* Get dynamic product data from API.
*
* @param array $product_types Array of product types.
* @return array
*/
public static function get_product_data( $product_types ) {
$locale = get_user_locale();
// Transient value is an array of product data keyed by locale.
$transient_value = get_transient( self::PRODUCT_DATA_TRANSIENT );
$transient_value = is_array( $transient_value ) ? $transient_value : array();
$woocommerce_products = $transient_value[ $locale ] ?? false;
if ( false === $woocommerce_products ) {
$woocommerce_products = wp_remote_get(
add_query_arg(
array(
'locale' => $locale,
),
'https://woocommerce.com/wp-json/wccom-extensions/1.0/search'
),
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
);
if ( is_wp_error( $woocommerce_products ) ) {
return $product_types;
}
$transient_value[ $locale ] = $woocommerce_products;
set_transient( self::PRODUCT_DATA_TRANSIENT, $transient_value, DAY_IN_SECONDS );
}
$data = json_decode( $woocommerce_products['body'] );
$products = array();
$product_data = array();
// Map product data by ID.
if ( isset( $data ) && isset( $data->products ) ) {
foreach ( $data->products as $product_datum ) {
if ( isset( $product_datum->id ) ) {
$products[ $product_datum->id ] = $product_datum;
}
}
}
// Loop over product types and append data.
foreach ( $product_types as $key => $product_type ) {
$product_data[ $key ] = $product_types[ $key ];
if ( isset( $product_type['product'] ) && isset( $products[ $product_type['product'] ] ) ) {
$price = html_entity_decode( $products[ $product_type['product'] ]->price );
$yearly_price = (float) str_replace( '$', '', $price );
$product_data[ $key ]['yearly_price'] = $yearly_price;
$product_data[ $key ]['description'] = $products[ $product_type['product'] ]->excerpt;
$product_data[ $key ]['more_url'] = $products[ $product_type['product'] ]->link;
$product_data[ $key ]['slug'] = strtolower( preg_replace( '~[^\pL\d]+~u', '-', $products[ $product_type['product'] ]->slug ) );
}
}
return $product_data;
}
/**
* Get the allowed product types with the polled data.
*
* @return array
*/
public static function get_product_types_with_data() {
return self::get_product_data( self::get_allowed_product_types() );
}
/**
* Get relevant purchaseable products for the site.
*
* @return array
*/
public static function get_relevant_products() {
$profiler_data = get_option( OnboardingProfile::DATA_OPTION, array() );
$installed = PluginsHelper::get_installed_plugin_slugs();
$product_types = isset( $profiler_data['product_types'] ) ? $profiler_data['product_types'] : array();
$product_data = self::get_product_types_with_data();
$purchaseable = array();
$remaining = array();
foreach ( $product_types as $type ) {
if ( ! isset( $product_data[ $type ]['slug'] ) ) {
continue;
}
$purchaseable[] = $product_data[ $type ];
if ( ! in_array( $product_data[ $type ]['slug'], $installed, true ) ) {
$remaining[] = $product_data[ $type ]['label'];
}
}
return array(
'purchaseable' => $purchaseable,
'remaining' => $remaining,
);
}
}
Onboarding/OnboardingProfile.php 0000644 00000003564 15154512352 0012755 0 ustar 00 <?php
/**
* WooCommerce Onboarding Setup Wizard
*/
namespace Automattic\WooCommerce\Internal\Admin\Onboarding;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Admin\WCAdminHelper;
/**
* Contains backend logic for the onboarding profile and checklist feature.
*/
class OnboardingProfile {
/**
* Profile data option name.
*/
const DATA_OPTION = 'woocommerce_onboarding_profile';
/**
* Add onboarding actions.
*/
public static function init() {
add_action( 'update_option_' . self::DATA_OPTION, array( __CLASS__, 'trigger_complete' ), 10, 2 );
}
/**
* Trigger the woocommerce_onboarding_profile_completed action
*
* @param array $old_value Previous value.
* @param array $value Current value.
*/
public static function trigger_complete( $old_value, $value ) {
if ( isset( $old_value['completed'] ) && $old_value['completed'] ) {
return;
}
if ( ! isset( $value['completed'] ) || ! $value['completed'] ) {
return;
}
/**
* Action hook fired when the onboarding profile (or onboarding wizard,
* or profiler) is completed.
*
* @since 1.5.0
*/
do_action( 'woocommerce_onboarding_profile_completed' );
}
/**
* Check if the profiler still needs to be completed.
*
* @return bool
*/
public static function needs_completion() {
$onboarding_data = get_option( self::DATA_OPTION, array() );
$is_completed = isset( $onboarding_data['completed'] ) && true === $onboarding_data['completed'];
$is_skipped = isset( $onboarding_data['skipped'] ) && true === $onboarding_data['skipped'];
// @todo When merging to WooCommerce Core, we should set the `completed` flag to true during the upgrade progress.
// https://github.com/woocommerce/woocommerce-admin/pull/2300#discussion_r287237498.
return ! $is_completed && ! $is_skipped;
}
}
Onboarding/OnboardingSetupWizard.php 0000644 00000020721 15154512352 0013630 0 ustar 00 <?php
/**
* WooCommerce Onboarding Setup Wizard
*/
namespace Automattic\WooCommerce\Internal\Admin\Onboarding;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Admin\WCAdminHelper;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;
/**
* Contains backend logic for the onboarding profile and checklist feature.
*/
class OnboardingSetupWizard {
/**
* Class instance.
*
* @var OnboardingSetupWizard instance
*/
private static $instance = null;
/**
* Get class instance.
*/
final public static function instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Add onboarding actions.
*/
public function init() {
if ( ! is_admin() ) {
return;
}
// Old settings injection.
// Run after Automattic\WooCommerce\Internal\Admin\Loader.
add_filter( 'woocommerce_components_settings', array( $this, 'component_settings' ), 20 );
// New settings injection.
add_filter( 'woocommerce_admin_shared_settings', array( $this, 'component_settings' ), 20 );
add_filter( 'woocommerce_admin_preload_settings', array( $this, 'preload_settings' ) );
add_filter( 'admin_body_class', array( $this, 'add_loading_classes' ) );
add_action( 'admin_init', array( $this, 'do_admin_redirects' ) );
add_action( 'current_screen', array( $this, 'redirect_to_profiler' ) );
add_filter( 'woocommerce_show_admin_notice', array( $this, 'remove_old_install_notice' ), 10, 2 );
}
/**
* Test whether the context of execution comes from async action scheduler.
* Note: this is a polyfill for wc_is_running_from_async_action_scheduler()
* which was introduced in WC 4.0.
*
* @return bool
*/
private function is_running_from_async_action_scheduler() {
if ( function_exists( '\wc_is_running_from_async_action_scheduler' ) ) {
return \wc_is_running_from_async_action_scheduler();
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
return isset( $_REQUEST['action'] ) && 'as_async_request_queue_runner' === $_REQUEST['action'];
}
/**
* Handle redirects to setup/welcome page after install and updates.
*
* For setup wizard, transient must be present, the user must have access rights, and we must ignore the network/bulk plugin updaters.
*/
public function do_admin_redirects() {
// Don't run this fn from Action Scheduler requests, as it would clear _wc_activation_redirect transient.
// That means OBW would never be shown.
if ( $this->is_running_from_async_action_scheduler() ) {
return;
}
// Setup wizard redirect.
if ( get_transient( '_wc_activation_redirect' ) && apply_filters( 'woocommerce_enable_setup_wizard', true ) ) {
$do_redirect = true;
$current_page = isset( $_GET['page'] ) ? wc_clean( wp_unslash( $_GET['page'] ) ) : false; // phpcs:ignore WordPress.Security.NonceVerification
$is_onboarding_path = ! isset( $_GET['path'] ) || '/setup-wizard' === wc_clean( wp_unslash( $_GET['page'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
// On these pages, or during these events, postpone the redirect.
if ( wp_doing_ajax() || is_network_admin() || ! current_user_can( 'manage_woocommerce' ) ) {
$do_redirect = false;
}
// On these pages, or during these events, disable the redirect.
if (
( 'wc-admin' === $current_page && $is_onboarding_path ) ||
apply_filters( 'woocommerce_prevent_automatic_wizard_redirect', false ) ||
isset( $_GET['activate-multi'] ) // phpcs:ignore WordPress.Security.NonceVerification
) {
delete_transient( '_wc_activation_redirect' );
$do_redirect = false;
}
if ( $do_redirect ) {
delete_transient( '_wc_activation_redirect' );
wp_safe_redirect( wc_admin_url() );
exit;
}
}
}
/**
* Trigger the woocommerce_onboarding_profile_completed action
*
* @param array $old_value Previous value.
* @param array $value Current value.
*/
public function trigger_profile_completed_action( $old_value, $value ) {
if ( isset( $old_value['completed'] ) && $old_value['completed'] ) {
return;
}
if ( ! isset( $value['completed'] ) || ! $value['completed'] ) {
return;
}
/**
* Action hook fired when the onboarding profile (or onboarding wizard,
* or profiler) is completed.
*
* @since 1.5.0
*/
do_action( 'woocommerce_onboarding_profile_completed' );
}
/**
* Returns true if the profiler should be displayed (not completed and not skipped).
*
* @return bool
*/
private function should_show() {
if ( $this->is_setup_wizard() ) {
return true;
}
return OnboardingProfile::needs_completion();
}
/**
* Redirect to the profiler on homepage if completion is needed.
*/
public function redirect_to_profiler() {
if ( ! $this->is_homepage() || ! OnboardingProfile::needs_completion() ) {
return;
}
wp_safe_redirect( wc_admin_url( '&path=/setup-wizard' ) );
exit;
}
/**
* Check if the current page is the profile wizard.
*
* @return bool
*/
private function is_setup_wizard() {
/* phpcs:disable WordPress.Security.NonceVerification */
return isset( $_GET['page'] ) &&
'wc-admin' === $_GET['page'] &&
isset( $_GET['path'] ) &&
'/setup-wizard' === $_GET['path'];
/* phpcs: enable */
}
/**
* Check if the current page is the homepage.
*
* @return bool
*/
private function is_homepage() {
/* phpcs:disable WordPress.Security.NonceVerification */
return isset( $_GET['page'] ) &&
'wc-admin' === $_GET['page'] &&
! isset( $_GET['path'] );
/* phpcs: enable */
}
/**
* Determine if the current page is one of the WC Admin pages.
*
* @return bool
*/
private function is_woocommerce_page() {
$current_page = PageController::get_instance()->get_current_page();
if ( ! $current_page || ! isset( $current_page['path'] ) ) {
return false;
}
return 0 === strpos( $current_page['path'], 'wc-admin' );
}
/**
* Add profiler items to component settings.
*
* @param array $settings Component settings.
*
* @return array
*/
public function component_settings( $settings ) {
$profile = (array) get_option( OnboardingProfile::DATA_OPTION, array() );
$settings['onboarding'] = array(
'profile' => $profile,
);
// Only fetch if the onboarding wizard OR the task list is incomplete or currently shown
// or the current page is one of the WooCommerce Admin pages.
if (
( ! $this->should_show() && ! count( TaskLists::get_visible() )
||
! $this->is_woocommerce_page()
)
) {
return $settings;
}
include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-options.php';
$wccom_auth = \WC_Helper_Options::get( 'auth' );
$profile['wccom_connected'] = empty( $wccom_auth['access_token'] ) ? false : true;
$settings['onboarding']['currencySymbols'] = get_woocommerce_currency_symbols();
$settings['onboarding']['euCountries'] = WC()->countries->get_european_union_countries();
$settings['onboarding']['localeInfo'] = include WC()->plugin_path() . '/i18n/locale-info.php';
$settings['onboarding']['profile'] = $profile;
if ( $this->is_setup_wizard() ) {
$settings['onboarding']['pageCount'] = (int) ( wp_count_posts( 'page' ) )->publish;
$settings['onboarding']['postCount'] = (int) ( wp_count_posts( 'post' ) )->publish;
$settings['onboarding']['isBlockTheme'] = wc_current_theme_is_fse_theme();
}
return apply_filters( 'woocommerce_admin_onboarding_preloaded_data', $settings );
}
/**
* Preload WC setting options to prime state of the application.
*
* @param array $options Array of options to preload.
* @return array
*/
public function preload_settings( $options ) {
$options[] = 'general';
return $options;
}
/**
* Set the admin full screen class when loading to prevent flashes of unstyled content.
*
* @param bool $classes Body classes.
* @return array
*/
public function add_loading_classes( $classes ) {
/* phpcs:disable WordPress.Security.NonceVerification */
if ( $this->is_setup_wizard() ) {
$classes .= ' woocommerce-admin-full-screen';
}
/* phpcs: enable */
return $classes;
}
/**
* Remove the install notice that prompts the user to visit the old onboarding setup wizard.
*
* @param bool $show Show or hide the notice.
* @param string $notice The slug of the notice.
* @return bool
*/
public function remove_old_install_notice( $show, $notice ) {
if ( 'install' === $notice ) {
return false;
}
return $show;
}
}
Onboarding/OnboardingSync.php 0000644 00000007721 15154512352 0012270 0 ustar 00 <?php
/**
* WooCommerce Onboarding
*/
namespace Automattic\WooCommerce\Internal\Admin\Onboarding;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;
/**
* Contains backend logic for the onboarding profile and checklist feature.
*/
class OnboardingSync {
/**
* Class instance.
*
* @var OnboardingSync instance
*/
private static $instance = null;
/**
* Get class instance.
*/
final public static function instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init.
*/
public function init() {
add_action( 'update_option_' . OnboardingProfile::DATA_OPTION, array( $this, 'send_profile_data_on_update' ), 10, 2 );
add_action( 'woocommerce_helper_connected', array( $this, 'send_profile_data_on_connect' ) );
if ( ! is_admin() ) {
return;
}
add_action( 'current_screen', array( $this, 'redirect_wccom_install' ) );
}
/**
* Send profile data to WooCommerce.com.
*/
private function send_profile_data() {
if ( 'yes' !== get_option( 'woocommerce_allow_tracking', 'no' ) ) {
return;
}
if ( ! class_exists( '\WC_Helper_API' ) || ! method_exists( '\WC_Helper_API', 'put' ) ) {
return;
}
if ( ! class_exists( '\WC_Helper_Options' ) ) {
return;
}
$auth = \WC_Helper_Options::get( 'auth' );
if ( empty( $auth['access_token'] ) || empty( $auth['access_token_secret'] ) ) {
return false;
}
$profile = get_option( OnboardingProfile::DATA_OPTION, array() );
$base_location = wc_get_base_location();
$defaults = array(
'plugins' => 'skipped',
'industry' => array(),
'product_types' => array(),
'product_count' => '0',
'selling_venues' => 'no',
'number_employees' => '1',
'revenue' => 'none',
'other_platform' => 'none',
'business_extensions' => array(),
'theme' => get_stylesheet(),
'setup_client' => false,
'store_location' => $base_location['country'],
'default_currency' => get_woocommerce_currency(),
);
// Prepare industries as an array of slugs if they are in array format.
if ( isset( $profile['industry'] ) && is_array( $profile['industry'] ) ) {
$industry_slugs = array();
foreach ( $profile['industry'] as $industry ) {
$industry_slugs[] = is_array( $industry ) ? $industry['slug'] : $industry;
}
$profile['industry'] = $industry_slugs;
}
$body = wp_parse_args( $profile, $defaults );
\WC_Helper_API::put(
'profile',
array(
'authenticated' => true,
'body' => wp_json_encode( $body ),
'headers' => array(
'Content-Type' => 'application/json',
),
)
);
}
/**
* Send profiler data on profiler change to completion.
*
* @param array $old_value Previous value.
* @param array $value Current value.
*/
public function send_profile_data_on_update( $old_value, $value ) {
if ( ! isset( $value['completed'] ) || ! $value['completed'] ) {
return;
}
$this->send_profile_data();
}
/**
* Send profiler data after a site is connected.
*/
public function send_profile_data_on_connect() {
$profile = get_option( OnboardingProfile::DATA_OPTION, array() );
if ( ! isset( $profile['completed'] ) || ! $profile['completed'] ) {
return;
}
$this->send_profile_data();
}
/**
* Redirects the user to the task list if the task list is enabled and finishing a wccom checkout.
*
* @todo Once URL params are added to the redirect, we can check those instead of the referer.
*/
public function redirect_wccom_install() {
$task_list = TaskLists::get_list( 'setup' );
if (
! $task_list ||
$task_list->is_hidden() ||
! isset( $_SERVER['HTTP_REFERER'] ) ||
0 !== strpos( $_SERVER['HTTP_REFERER'], 'https://woocommerce.com/checkout?utm_medium=product' ) // phpcs:ignore sanitization ok.
) {
return;
}
wp_safe_redirect( wc_admin_url() );
}
}
Onboarding/OnboardingThemes.php 0000644 00000015676 15154512352 0012611 0 ustar 00 <?php
/**
* WooCommerce Onboarding Themes
*/
namespace Automattic\WooCommerce\Internal\Admin\Onboarding;
use Automattic\WooCommerce\Admin\Loader;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Admin\WCAdminHelper;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Init as OnboardingTasks;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;
use Automattic\WooCommerce\Admin\Schedulers\MailchimpScheduler;
/**
* Logic around onboarding themes.
*/
class OnboardingThemes {
/**
* Name of themes transient.
*
* @var string
*/
const THEMES_TRANSIENT = 'wc_onboarding_themes';
/**
* Init.
*/
public static function init() {
add_action( 'woocommerce_theme_installed', array( __CLASS__, 'delete_themes_transient' ) );
add_action( 'after_switch_theme', array( __CLASS__, 'delete_themes_transient' ) );
add_filter( 'woocommerce_rest_prepare_themes', array( __CLASS__, 'add_uploaded_theme_data' ) );
add_filter( 'woocommerce_admin_onboarding_preloaded_data', array( __CLASS__, 'preload_data' ) );
}
/**
* Get puchasable theme by slug.
*
* @param string $price_string string of price.
* @return float|null
*/
private static function get_price_from_string( $price_string ) {
$price_match = null;
// Parse price from string as it includes the currency symbol.
preg_match( '/\\d+\.\d{2}\s*/', $price_string, $price_match );
if ( count( $price_match ) > 0 ) {
return (float) $price_match[0];
}
return null;
}
/**
* Get puchasable theme by slug.
*
* @param string $slug from theme.
* @return array|null
*/
public static function get_paid_theme_by_slug( $slug ) {
$themes = self::get_themes();
$theme_key = array_search( $slug, array_column( $themes, 'slug' ), true );
$theme = false !== $theme_key ? $themes[ $theme_key ] : null;
if ( $theme && isset( $theme['id'] ) && isset( $theme['price'] ) ) {
$price = self::get_price_from_string( $theme['price'] );
if ( $price && $price > 0 ) {
return $themes[ $theme_key ];
}
}
return null;
}
/**
* Sort themes returned from WooCommerce.com
*
* @param array $themes Array of themes from WooCommerce.com.
* @return array
*/
public static function sort_woocommerce_themes( $themes ) {
usort(
$themes,
function ( $product_1, $product_2 ) {
if ( ! property_exists( $product_1, 'id' ) || ! property_exists( $product_1, 'slug' ) ) {
return 1;
}
if ( ! property_exists( $product_2, 'id' ) || ! property_exists( $product_2, 'slug' ) ) {
return 1;
}
if ( in_array( 'Storefront', array( $product_1->slug, $product_2->slug ), true ) ) {
return 'Storefront' === $product_1->slug ? -1 : 1;
}
return $product_1->id < $product_2->id ? 1 : -1;
}
);
return $themes;
}
/**
* Get a list of themes for the onboarding wizard.
*
* @return array
*/
public static function get_themes() {
$themes = get_transient( self::THEMES_TRANSIENT );
if ( false === $themes ) {
$theme_data = wp_remote_get(
'https://woocommerce.com/wp-json/wccom-extensions/1.0/search?category=themes',
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
);
$themes = array();
if ( ! is_wp_error( $theme_data ) ) {
$theme_data = json_decode( $theme_data['body'] );
$woo_themes = property_exists( $theme_data, 'products' ) ? $theme_data->products : array();
$sorted_themes = self::sort_woocommerce_themes( $woo_themes );
foreach ( $sorted_themes as $theme ) {
$slug = sanitize_title_with_dashes( $theme->slug );
$themes[ $slug ] = (array) $theme;
$themes[ $slug ]['is_installed'] = false;
$themes[ $slug ]['has_woocommerce_support'] = true;
$themes[ $slug ]['slug'] = $slug;
}
}
$installed_themes = wp_get_themes();
foreach ( $installed_themes as $slug => $theme ) {
$theme_data = self::get_theme_data( $theme );
if ( isset( $themes[ $slug ] ) ) {
$themes[ $slug ]['is_installed'] = true;
$themes[ $slug ]['image'] = $theme_data['image'];
} else {
$themes[ $slug ] = $theme_data;
}
}
$active_theme = get_option( 'stylesheet' );
/**
* The active theme may no be set if active_theme is not compatible with current version of WordPress.
* In this case, we should not add active theme to onboarding themes.
*/
if ( isset( $themes[ $active_theme ] ) ) {
// Add the WooCommerce support tag for default themes that don't explicitly declare support.
if ( function_exists( 'wc_is_wp_default_theme_active' ) && wc_is_wp_default_theme_active() ) {
$themes[ $active_theme ]['has_woocommerce_support'] = true;
}
$themes = array( $active_theme => $themes[ $active_theme ] ) + $themes;
}
set_transient( self::THEMES_TRANSIENT, $themes, DAY_IN_SECONDS );
}
$themes = apply_filters( 'woocommerce_admin_onboarding_themes', $themes );
return array_values( $themes );
}
/**
* Get theme data used in onboarding theme browser.
*
* @param WP_Theme $theme Theme to gather data from.
* @return array
*/
public static function get_theme_data( $theme ) {
return array(
'slug' => sanitize_text_field( $theme->stylesheet ),
'title' => $theme->get( 'Name' ),
'price' => '0.00',
'is_installed' => true,
'image' => $theme->get_screenshot(),
'has_woocommerce_support' => true,
);
}
/**
* Add theme data to response from themes controller.
*
* @param WP_REST_Response $response Rest response.
* @return WP_REST_Response
*/
public static function add_uploaded_theme_data( $response ) {
if ( ! isset( $response->data['theme'] ) ) {
return $response;
}
$theme = wp_get_theme( $response->data['theme'] );
$response->data['theme_data'] = self::get_theme_data( $theme );
return $response;
}
/**
* Delete the stored themes transient.
*/
public static function delete_themes_transient() {
delete_transient( self::THEMES_TRANSIENT );
}
/**
* Add preloaded data to onboarding.
*
* @param array $settings Component settings.
*
* @return array
*/
public static function preload_data( $settings ) {
$settings['onboarding']['activeTheme'] = get_option( 'stylesheet' );
$settings['onboarding']['themes'] = self::get_themes();
return $settings;
}
/**
* Gets an array of themes that can be installed & activated via the onboarding wizard.
*
* @return array
*/
public static function get_allowed_themes() {
$allowed_themes = array();
$themes = self::get_themes();
foreach ( $themes as $theme ) {
$price = preg_replace( '/&#?[a-z0-9]+;/i', '', $theme['price'] );
if ( $theme['is_installed'] || '0.00' === $price ) {
$allowed_themes[] = $theme['slug'];
}
}
return apply_filters( 'woocommerce_admin_onboarding_themes_whitelist', $allowed_themes );
}
}
Orders/COTRedirectionController.php 0000644 00000005405 15154512352 0013403 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\Orders;
use Automattic\WooCommerce\Internal\Traits\AccessiblePrivateMethods;
/**
* When Custom Order Tables are not the default order store (ie, posts are authoritative), we should take care of
* redirecting requests for the order editor and order admin list table to the equivalent posts-table screens.
*
* If the redirect logic is problematic, it can be unhooked using code like the following example:
*
* remove_action(
* 'admin_page_access_denied',
* array( wc_get_container()->get( COTRedirectionController::class ), 'handle_hpos_admin_requests' )
* );
*/
class COTRedirectionController {
use AccessiblePrivateMethods;
/**
* Add hooks needed to perform our magic.
*/
public function setup(): void {
// Only take action in cases where access to the admin screen would otherwise be denied.
self::add_action( 'admin_page_access_denied', array( $this, 'handle_hpos_admin_requests' ) );
}
/**
* Listen for denied admin requests and, if they appear to relate to HPOS admin screens, potentially
* redirect the user to the equivalent CPT-driven screens.
*
* @param array|null $query_params The query parameters to use when determining the redirect. If not provided, the $_GET superglobal will be used.
*/
private function handle_hpos_admin_requests( $query_params = null ) {
$query_params = is_array( $query_params ) ? $query_params : $_GET;
if ( ! isset( $query_params['page'] ) || 'wc-orders' !== $query_params['page'] ) {
return;
}
$params = wp_unslash( $query_params );
$action = $params['action'] ?? '';
unset( $params['page'] );
if ( 'edit' === $action && isset( $params['id'] ) ) {
$params['post'] = $params['id'];
unset( $params['id'] );
$new_url = add_query_arg( $params, get_admin_url( null, 'post.php' ) );
} elseif ( 'new' === $action ) {
unset( $params['action'] );
$params['post_type'] = 'shop_order';
$new_url = add_query_arg( $params, get_admin_url( null, 'post-new.php' ) );
} else {
// If nonce parameters are present and valid, rebuild them for the CPT admin list table.
if ( isset( $params['_wpnonce'] ) && check_admin_referer( 'bulk-orders' ) ) {
$params['_wp_http_referer'] = get_admin_url( null, 'edit.php?post_type=shop_order' );
$params['_wpnonce'] = wp_create_nonce( 'bulk-posts' );
}
// If an `id` array parameter is present, rename as `post`.
if ( isset( $params['id'] ) && is_array( $params['id'] ) ) {
$params['post'] = $params['id'];
unset( $params['id'] );
}
$params['post_type'] = 'shop_order';
$new_url = add_query_arg( $params, get_admin_url( null, 'edit.php' ) );
}
if ( ! empty( $new_url ) && wp_safe_redirect( $new_url, 301 ) ) {
exit;
}
}
}
Orders/Edit.php 0000644 00000031146 15154512352 0007410 0 ustar 00 <?php
/**
* Renders order edit page, works with both post and order object.
*/
namespace Automattic\WooCommerce\Internal\Admin\Orders;
use Automattic\WooCommerce\Internal\Admin\Orders\MetaBoxes\CustomMetaBox;
use Automattic\WooCommerce\Internal\Admin\Orders\MetaBoxes\TaxonomiesMetaBox;
/**
* Class Edit.
*/
class Edit {
/**
* Screen ID for the edit order screen.
*
* @var string
*/
private $screen_id;
/**
* Instance of the CustomMetaBox class. Used to render meta box for custom meta.
*
* @var CustomMetaBox
*/
private $custom_meta_box;
/**
* Instance of the TaxonomiesMetaBox class. Used to render meta box for taxonomies.
*
* @var TaxonomiesMetaBox
*/
private $taxonomies_meta_box;
/**
* Instance of WC_Order to be used in metaboxes.
*
* @var \WC_Order
*/
private $order;
/**
* Action name that the form is currently handling. Could be new_order or edit_order.
*
* @var string
*/
private $current_action;
/**
* Message to be displayed to the user. Index of message from the messages array registered when declaring shop_order post type.
*
* @var int
*/
private $message;
/**
* Controller for orders page. Used to determine redirection URLs.
*
* @var PageController
*/
private $orders_page_controller;
/**
* Hooks all meta-boxes for order edit page. This is static since this may be called by post edit form rendering.
*
* @param string $screen_id Screen ID.
* @param string $title Title of the page.
*/
public static function add_order_meta_boxes( string $screen_id, string $title ) {
/* Translators: %s order type name. */
add_meta_box( 'woocommerce-order-data', sprintf( __( '%s data', 'woocommerce' ), $title ), 'WC_Meta_Box_Order_Data::output', $screen_id, 'normal', 'high' );
add_meta_box( 'woocommerce-order-items', __( 'Items', 'woocommerce' ), 'WC_Meta_Box_Order_Items::output', $screen_id, 'normal', 'high' );
/* Translators: %s order type name. */
add_meta_box( 'woocommerce-order-notes', sprintf( __( '%s notes', 'woocommerce' ), $title ), 'WC_Meta_Box_Order_Notes::output', $screen_id, 'side', 'default' );
add_meta_box( 'woocommerce-order-downloads', __( 'Downloadable product permissions', 'woocommerce' ) . wc_help_tip( __( 'Note: Permissions for order items will automatically be granted when the order status changes to processing/completed.', 'woocommerce' ) ), 'WC_Meta_Box_Order_Downloads::output', $screen_id, 'normal', 'default' );
/* Translators: %s order type name. */
add_meta_box( 'woocommerce-order-actions', sprintf( __( '%s actions', 'woocommerce' ), $title ), 'WC_Meta_Box_Order_Actions::output', $screen_id, 'side', 'high' );
}
/**
* Hooks metabox save functions for order edit page.
*
* @return void
*/
public static function add_save_meta_boxes() {
/**
* Save Order Meta Boxes.
*
* In order:
* Save the order items.
* Save the order totals.
* Save the order downloads.
* Save order data - also updates status and sends out admin emails if needed. Last to show latest data.
* Save actions - sends out other emails. Last to show latest data.
*/
add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Items::save', 10 );
add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Downloads::save', 30, 2 );
add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40 );
add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Actions::save', 50, 2 );
}
/**
* Enqueue necessary scripts for order edit page.
*/
private function enqueue_scripts() {
if ( wp_is_mobile() ) {
wp_enqueue_script( 'jquery-touch-punch' );
}
wp_enqueue_script( 'post' ); // Ensure existing JS libraries are still available for backward compat.
}
/**
* Returns the PageController for this edit form. This method is protected to allow child classes to overwrite the PageController object and return custom links.
*
* @since 8.0.0
*
* @return PageController PageController object.
*/
protected function get_page_controller() {
if ( ! isset( $this->orders_page_controller ) ) {
$this->orders_page_controller = wc_get_container()->get( PageController::class );
}
return $this->orders_page_controller;
}
/**
* Setup hooks, actions and variables needed to render order edit page.
*
* @param \WC_Order $order Order object.
*/
public function setup( \WC_Order $order ) {
$this->order = $order;
$current_screen = get_current_screen();
$current_screen->is_block_editor( false );
$this->screen_id = $current_screen->id;
if ( ! isset( $this->custom_meta_box ) ) {
$this->custom_meta_box = wc_get_container()->get( CustomMetaBox::class );
}
if ( ! isset( $this->taxonomies_meta_box ) ) {
$this->taxonomies_meta_box = wc_get_container()->get( TaxonomiesMetaBox::class );
}
$this->add_save_meta_boxes();
$this->handle_order_update();
$this->add_order_meta_boxes( $this->screen_id, __( 'Order', 'woocommerce' ) );
$this->add_order_specific_meta_box();
$this->add_order_taxonomies_meta_box();
/**
* From wp-admin/includes/meta-boxes.php.
*
* Fires after all built-in meta boxes have been added. Custom metaboxes may be enqueued here.
*
* @since 3.8.0.
*/
do_action( 'add_meta_boxes', $this->screen_id, $this->order );
/**
* Provides an opportunity to inject custom meta boxes into the order editor screen. This
* hook is an analog of `add_meta_boxes_<POST_TYPE>` as provided by WordPress core.
*
* @since 7.4.0
*
* @oaram WC_Order $order The order being edited.
*/
do_action( 'add_meta_boxes_' . $this->screen_id, $this->order );
$this->enqueue_scripts();
}
/**
* Set the current action for the form.
*
* @param string $action Action name.
*/
public function set_current_action( string $action ) {
$this->current_action = $action;
}
/**
* Hooks meta box for order specific meta.
*/
private function add_order_specific_meta_box() {
add_meta_box(
'order_custom',
__( 'Custom Fields', 'woocommerce' ),
array( $this, 'render_custom_meta_box' ),
$this->screen_id,
'normal'
);
}
/**
* Render custom meta box.
*
* @return void
*/
private function add_order_taxonomies_meta_box() {
$this->taxonomies_meta_box->add_taxonomies_meta_boxes( $this->screen_id, $this->order->get_type() );
}
/**
* Takes care of updating order data. Fires action that metaboxes can hook to for order data updating.
*
* @return void
*/
public function handle_order_update() {
if ( ! isset( $this->order ) ) {
return;
}
if ( 'edit_order' !== sanitize_text_field( wp_unslash( $_POST['action'] ?? '' ) ) ) {
return;
}
check_admin_referer( $this->get_order_edit_nonce_action() );
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitized later on by taxonomies_meta_box object.
$taxonomy_input = isset( $_POST['tax_input'] ) ? wp_unslash( $_POST['tax_input'] ) : null;
$this->taxonomies_meta_box->save_taxonomies( $this->order, $taxonomy_input );
/**
* Save meta for shop order.
*
* @param int Order ID.
* @param \WC_Order Post object.
*
* @since 2.1.0
*/
do_action( 'woocommerce_process_shop_order_meta', $this->order->get_id(), $this->order );
$this->custom_meta_box->handle_metadata_changes($this->order);
// Order updated message.
$this->message = 1;
$this->redirect_order( $this->order );
}
/**
* Helper method to redirect to order edit page.
*
* @since 8.0.0
*
* @param \WC_Order $order Order object.
*/
private function redirect_order( \WC_Order $order ) {
$redirect_to = $this->get_page_controller()->get_edit_url( $order->get_id() );
if ( isset( $this->message ) ) {
$redirect_to = add_query_arg( 'message', $this->message, $redirect_to );
}
wp_safe_redirect(
/**
* Filter the URL used to redirect after an order is updated. Similar to the WP post's `redirect_post_location` filter.
*
* @param string $redirect_to The redirect destination URL.
* @param int $order_id The order ID.
* @param \WC_Order $order The order object.
*
* @since 8.0.0
*/
apply_filters(
'woocommerce_redirect_order_location',
$redirect_to,
$order->get_id(),
$order
)
);
exit;
}
/**
* Helper method to get the name of order edit nonce.
*
* @return string Nonce action name.
*/
private function get_order_edit_nonce_action() {
return 'update-order_' . $this->order->get_id();
}
/**
* Render meta box for order specific meta.
*/
public function render_custom_meta_box() {
$this->custom_meta_box->output( $this->order );
}
/**
* Render order edit page.
*/
public function display() {
/**
* This is used by the order edit page to show messages in the notice fields.
* It should be similar to post_updated_messages filter, i.e.:
* array(
* {order_type} => array(
* 1 => 'Order updated.',
* 2 => 'Custom field updated.',
* ...
* ).
*
* The index to be displayed is computed from the $_GET['message'] variable.
*
* @since 7.4.0.
*/
$messages = apply_filters( 'woocommerce_order_updated_messages', array() );
$message = $this->message;
if ( isset( $_GET['message'] ) ) {
$message = absint( $_GET['message'] );
}
if ( isset( $message ) ) {
$message = $messages[ $this->order->get_type() ][ $message ] ?? false;
}
$this->render_wrapper_start( '', $message );
$this->render_meta_boxes();
$this->render_wrapper_end();
}
/**
* Helper function to render wrapper start.
*
* @param string $notice Notice to display, if any.
* @param string $message Message to display, if any.
*/
private function render_wrapper_start( $notice = '', $message = '' ) {
$post_type = get_post_type_object( $this->order->get_type() );
$edit_page_url = $this->get_page_controller()->get_edit_url( $this->order->get_id() );
$form_action = 'edit_order';
$referer = wp_get_referer();
$new_page_url = $this->get_page_controller()->get_new_page_url( $this->order->get_type() );
?>
<div class="wrap">
<h1 class="wp-heading-inline">
<?php
echo 'new_order' === $this->current_action ? esc_html( $post_type->labels->add_new_item ) : esc_html( $post_type->labels->edit_item );
?>
</h1>
<?php
if ( 'edit_order' === $this->current_action ) {
echo ' <a href="' . esc_url( $new_page_url ) . '" class="page-title-action">' . esc_html( $post_type->labels->add_new ) . '</a>';
}
?>
<hr class="wp-header-end">
<?php
if ( $notice ) :
?>
<div id="notice" class="notice notice-warning"><p
id="has-newer-autosave"><?php echo wp_kses_post( $notice ); ?></p></div>
<?php endif; ?>
<?php if ( $message ) : ?>
<div id="message" class="updated notice notice-success is-dismissible">
<p><?php echo wp_kses_post( $message ); ?></p></div>
<?php
endif;
?>
<form name="order" action="<?php echo esc_url( $edit_page_url ); ?>" method="post" id="order"
<?php
/**
* Fires inside the order edit form tag.
*
* @param \WC_Order $order Order object.
*
* @since 6.9.0
*/
do_action( 'order_edit_form_tag', $this->order );
?>
>
<?php wp_nonce_field( $this->get_order_edit_nonce_action() ); ?>
<?php
/**
* Fires at the top of the order edit form. Can be used as a replacement for edit_form_top hook for HPOS.
*
* @param \WC_Order $order Order object.
*
* @since 8.0.0
*/
do_action( 'order_edit_form_top', $this->order );
?>
<input type="hidden" id="hiddenaction" name="action" value="<?php echo esc_attr( $form_action ); ?>"/>
<input type="hidden" id="original_order_status" name="original_order_status" value="<?php echo esc_attr( $this->order->get_status() ); ?>"/>
<input type="hidden" id="referredby" name="referredby" value="<?php echo $referer ? esc_url( $referer ) : ''; ?>"/>
<input type="hidden" id="post_ID" name="post_ID" value="<?php echo esc_attr( $this->order->get_id() ); ?>"/>
<div id="poststuff">
<div id="post-body"
class="metabox-holder columns-<?php echo ( 1 === get_current_screen()->get_columns() ) ? '1' : '2'; ?>">
<?php
}
/**
* Helper function to render meta boxes.
*/
private function render_meta_boxes() {
?>
<div id="postbox-container-1" class="postbox-container">
<?php do_meta_boxes( $this->screen_id, 'side', $this->order ); ?>
</div>
<div id="postbox-container-2" class="postbox-container">
<?php
do_meta_boxes( $this->screen_id, 'normal', $this->order );
do_meta_boxes( $this->screen_id, 'advanced', $this->order );
?>
</div>
<?php
}
/**
* Helper function to render wrapper end.
*/
private function render_wrapper_end() {
?>
</div> <!-- /post-body -->
</div> <!-- /poststuff -->
</form>
</div> <!-- /wrap -->
<?php
}
}
Orders/EditLock.php 0000644 00000016770 15154512352 0010227 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\Orders;
/**
* This class takes care of the edit lock logic when HPOS is enabled.
* For better interoperability with WordPress, edit locks are stored in the same format as posts. That is, as a metadata
* in the order object (key: '_edit_lock') in the format "timestamp:user_id".
*
* @since 7.8.0
*/
class EditLock {
const META_KEY_NAME = '_edit_lock';
/**
* Obtains lock information for a given order. If the lock has expired or it's assigned to an invalid user,
* the order is no longer considered locked.
*
* @param \WC_Order $order Order to check.
* @return bool|array
*/
public function get_lock( \WC_Order $order ) {
$lock = $order->get_meta( self::META_KEY_NAME, true, 'edit' );
if ( ! $lock ) {
return false;
}
$lock = explode( ':', $lock );
if ( 2 !== count( $lock ) ) {
return false;
}
$time = absint( $lock[0] );
$user_id = isset( $lock[1] ) ? absint( $lock[1] ) : 0;
if ( ! $time || ! get_user_by( 'id', $user_id ) ) {
return false;
}
/** This filter is documented in WP's wp-admin/includes/ajax-actions.php */
$time_window = apply_filters( 'wp_check_post_lock_window', 150 ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingSinceComment
if ( time() >= ( $time + $time_window ) ) {
return false;
}
return compact( 'time', 'user_id' );
}
/**
* Checks whether the order is being edited (i.e. locked) by another user.
*
* @param \WC_Order $order Order to check.
* @return bool TRUE if order is locked and currently being edited by another user. FALSE otherwise.
*/
public function is_locked_by_another_user( \WC_Order $order ) : bool {
$lock = $this->get_lock( $order );
return $lock && ( get_current_user_id() !== $lock['user_id'] );
}
/**
* Checks whether the order is being edited by any user.
*
* @param \WC_Order $order Order to check.
* @return boolean TRUE if order is locked and currently being edited by a user. FALSE otherwise.
*/
public function is_locked( \WC_Order $order ) : bool {
return (bool) $this->get_lock( $order );
}
/**
* Assigns an order's edit lock to the current user.
*
* @param \WC_Order $order The order to apply the lock to.
* @return array|bool FALSE if no user is logged-in, an array in the same format as {@see get_lock()} otherwise.
*/
public function lock( \WC_Order $order ) {
$user_id = get_current_user_id();
if ( ! $user_id ) {
return false;
}
$order->update_meta_data( self::META_KEY_NAME, time() . ':' . $user_id );
$order->save_meta_data();
return $order->get_meta( self::META_KEY_NAME, true, 'edit' );
}
/**
* Hooked to 'heartbeat_received' on the edit order page to refresh the lock on an order being edited by the current user.
*
* @param array $response The heartbeat response to be sent.
* @param array $data Data sent through the heartbeat.
* @return array Response to be sent.
*/
public function refresh_lock_ajax( $response, $data ) {
$order_id = absint( $data['wc-refresh-order-lock'] ?? 0 );
if ( ! $order_id ) {
return $response;
}
unset( $response['wp-refresh-post-lock'] );
$order = wc_get_order( $order_id );
if ( ! $order || ( ! current_user_can( get_post_type_object( $order->get_type() )->cap->edit_post, $order->get_id() ) && ! current_user_can( 'manage_woocommerce' ) ) ) {
return $response;
}
$response['wc-refresh-order-lock'] = array();
if ( ! $this->is_locked_by_another_user( $order ) ) {
$response['wc-refresh-order-lock']['lock'] = $this->lock( $order );
} else {
$current_lock = $this->get_lock( $order );
$user = get_user_by( 'id', $current_lock['user_id'] );
$response['wc-refresh-order-lock']['error'] = array(
// translators: %s is a user's name.
'message' => sprintf( __( '%s has taken over and is currently editing.', 'woocommerce' ), $user->display_name ),
'user_name' => $user->display_name,
'user_avatar_src' => get_option( 'show_avatars' ) ? get_avatar_url( $user->ID, array( 'size' => 64 ) ) : '',
'user_avatar_src_2x' => get_option( 'show_avatars' ) ? get_avatar_url( $user->ID, array( 'size' => 128 ) ) : '',
);
}
return $response;
}
/**
* Hooked to 'heartbeat_received' on the orders screen to refresh the locked status of orders in the list table.
*
* @param array $response The heartbeat response to be sent.
* @param array $data Data sent through the heartbeat.
* @return array Response to be sent.
*/
public function check_locked_orders_ajax( $response, $data ) {
if ( empty( $data['wc-check-locked-orders'] ) || ! is_array( $data['wc-check-locked-orders'] ) ) {
return $response;
}
$response['wc-check-locked-orders'] = array();
$order_ids = array_unique( array_map( 'absint', $data['wc-check-locked-orders'] ) );
foreach ( $order_ids as $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
continue;
}
if ( ! $this->is_locked_by_another_user( $order ) || ( ! current_user_can( get_post_type_object( $order->get_type() )->cap->edit_post, $order->get_id() ) && ! current_user_can( 'manage_woocommerce' ) ) ) {
continue;
}
$response['wc-check-locked-orders'][ $order_id ] = true;
}
return $response;
}
/**
* Outputs HTML for the lock dialog based on the status of the lock on the order (if any).
* Depending on who owns the lock, this could be a message with the chance to take over or a message indicating that
* someone else has taken over the order.
*
* @param \WC_Order $order Order object.
* @return void
*/
public function render_dialog( $order ) {
$locked = $this->is_locked_by_another_user( $order );
$lock = $this->get_lock( $order );
$user = get_user_by( 'id', $lock['user_id'] );
$edit_url = wc_get_container()->get( \Automattic\WooCommerce\Internal\Admin\Orders\PageController::class )->get_edit_url( $order->get_id() );
$sendback_url = wp_get_referer();
if ( ! $sendback_url ) {
$sendback_url = wc_get_container()->get( \Automattic\WooCommerce\Internal\Admin\Orders\PageController::class )->get_base_page_url( $order->get_type() );
}
$sendback_text = __( 'Go back', 'woocommerce' );
?>
<div id="post-lock-dialog" class="notification-dialog-wrap <?php echo $locked ? '' : 'hidden'; ?> order-lock-dialog">
<div class="notification-dialog-background"></div>
<div class="notification-dialog">
<?php if ( $locked ) : ?>
<div class="post-locked-message">
<div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div>
<p class="currently-editing wp-tab-first" tabindex="0">
<?php
// translators: %s is a user's name.
echo esc_html( sprintf( __( '%s is currently editing this order. Do you want to take over?', 'woocommerce' ), esc_html( $user->display_name ) ) );
?>
</p>
<p>
<a class="button" href="<?php echo esc_url( $sendback_url ); ?>"><?php echo esc_html( $sendback_text ); ?></a>
<a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'claim-lock', '1', wp_nonce_url( $edit_url, 'claim-lock-' . $order->get_id() ) ) ); ?>"><?php esc_html_e( 'Take over', 'woocommerce' ); ?></a>
</p>
</div>
<?php else : ?>
<div class="post-taken-over">
<div class="post-locked-avatar"></div>
<p class="wp-tab-first" tabindex="0">
<span class="currently-editing"></span><br />
</p>
<p><a class="button button-primary wp-tab-last" href="<?php echo esc_url( $sendback_url ); ?>"><?php echo esc_html( $sendback_text ); ?></a></p>
</div>
<?php endif; ?>
</div>
</div>
<?php
}
}
Orders/ListTable.php 0000644 00000136351 15154512352 0010412 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\Orders;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
use Automattic\WooCommerce\Utilities\OrderUtil;
use WC_Order;
use WP_List_Table;
use WP_Screen;
/**
* Admin list table for orders as managed by the OrdersTableDataStore.
*/
class ListTable extends WP_List_Table {
/**
* Order type.
*
* @var string
*/
private $order_type;
/**
* Request vars.
*
* @var array
*/
private $request = array();
/**
* Contains the arguments to be used in the order query.
*
* @var array
*/
private $order_query_args = array();
/**
* Tracks if a filter (ie, date or customer filter) has been applied.
*
* @var bool
*/
private $has_filter = false;
/**
* Page controller instance for this request.
*
* @var PageController
*/
private $page_controller;
/**
* Tracks whether we're currently inside the trash.
*
* @var boolean
*/
private $is_trash = false;
/**
* Caches order counts by status.
*
* @var array
*/
private $status_count_cache = null;
/**
* Sets up the admin list table for orders (specifically, for orders managed by the OrdersTableDataStore).
*
* @see WC_Admin_List_Table_Orders for the corresponding class used in relation to the traditional WP Post store.
*/
public function __construct() {
parent::__construct(
array(
'singular' => 'order',
'plural' => 'orders',
'ajax' => false,
)
);
}
/**
* Init method, invoked by DI container.
*
* @internal This method is not intended to be used directly (except for testing).
* @param PageController $page_controller Page controller instance for this request.
*/
final public function init( PageController $page_controller ) {
$this->page_controller = $page_controller;
}
/**
* Performs setup work required before rendering the table.
*
* @param array $args Args to initialize this list table.
*
* @return void
*/
public function setup( $args = array() ): void {
$this->order_type = $args['order_type'] ?? 'shop_order';
add_action( 'admin_notices', array( $this, 'bulk_action_notices' ) );
add_filter( "manage_{$this->screen->id}_columns", array( $this, 'get_columns' ), 0 );
add_filter( 'set_screen_option_edit_' . $this->order_type . '_per_page', array( $this, 'set_items_per_page' ), 10, 3 );
add_filter( 'default_hidden_columns', array( $this, 'default_hidden_columns' ), 10, 2 );
add_action( 'admin_footer', array( $this, 'enqueue_scripts' ) );
$this->items_per_page();
set_screen_options();
add_action( 'manage_' . wc_get_page_screen_id( $this->order_type ) . '_custom_column', array( $this, 'render_column' ), 10, 2 );
}
/**
* Generates content for a single row of the table.
*
* @since 7.8.0
*
* @param \WC_Order $order The current order.
*/
public function single_row( $order ) {
/**
* Filters the list of CSS class names for a given order row in the orders list table.
*
* @since 7.8.0
*
* @param string[] $classes An array of CSS class names.
* @param \WC_Order $order The order object.
*/
$css_classes = apply_filters(
'woocommerce_' . $this->order_type . '_list_table_order_css_classes',
array(
'order-' . $order->get_id(),
'type-' . $order->get_type(),
'status-' . $order->get_status(),
),
$order
);
$css_classes = array_unique( array_map( 'trim', $css_classes ) );
// Is locked?
$edit_lock = wc_get_container()->get( EditLock::class );
if ( $edit_lock->is_locked_by_another_user( $order ) ) {
$css_classes[] = 'wp-locked';
}
echo '<tr id="order-' . esc_attr( $order->get_id() ) . '" class="' . esc_attr( implode( ' ', $css_classes ) ) . '">';
$this->single_row_columns( $order );
echo '</tr>';
}
/**
* Render individual column.
*
* @param string $column_id Column ID to render.
* @param WC_Order $order Order object.
*/
public function render_column( $column_id, $order ) {
if ( ! $order ) {
return;
}
if ( is_callable( array( $this, 'render_' . $column_id . '_column' ) ) ) {
call_user_func( array( $this, 'render_' . $column_id . '_column' ), $order );
}
}
/**
* Handles output for the default column.
*
* @param \WC_Order $order Current WooCommerce order object.
* @param string $column_name Identifier for the custom column.
*/
public function column_default( $order, $column_name ) {
/**
* Fires for each custom column for a specific order type. This hook takes precedence over the generic
* action `manage_{$this->screen->id}_custom_column`.
*
* @param string $column_name Identifier for the custom column.
* @param \WC_Order $order Current WooCommerce order object.
*
* @since 7.3.0
*/
do_action( 'woocommerce_' . $this->order_type . '_list_table_custom_column', $column_name, $order );
/**
* Fires for each custom column in the Custom Order Table in the administrative screen.
*
* @param string $column_name Identifier for the custom column.
* @param \WC_Order $order Current WooCommerce order object.
*
* @since 7.0.0
*/
do_action( "manage_{$this->screen->id}_custom_column", $column_name, $order );
}
/**
* Sets up an items-per-page control.
*/
private function items_per_page(): void {
add_screen_option(
'per_page',
array(
'default' => 20,
'option' => 'edit_' . $this->order_type . '_per_page',
)
);
}
/**
* Saves the items-per-page setting.
*
* @param mixed $default The default value.
* @param string $option The option being configured.
* @param int $value The submitted option value.
*
* @return mixed
*/
public function set_items_per_page( $default, string $option, int $value ) {
return 'edit_' . $this->order_type . '_per_page' === $option ? absint( $value ) : $default;
}
/**
* Render the table.
*
* @return void
*/
public function display() {
$post_type = get_post_type_object( $this->order_type );
$title = esc_html( $post_type->labels->name );
$add_new = esc_html( $post_type->labels->add_new );
$new_page_link = $this->page_controller->get_new_page_url( $this->order_type );
$search_label = '';
if ( ! empty( $this->order_query_args['s'] ) ) {
$search_label = '<span class="subtitle">';
$search_label .= sprintf(
/* translators: %s: Search query. */
__( 'Search results for: %s', 'woocommerce' ),
'<strong>' . esc_html( $this->order_query_args['s'] ) . '</strong>'
);
$search_label .= '</span>';
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo wp_kses_post(
"
<div class='wrap'>
<h1 class='wp-heading-inline'>{$title}</h1>
<a href='" . esc_url( $new_page_link ) . "' class='page-title-action'>{$add_new}</a>
{$search_label}
<hr class='wp-header-end'>"
);
if ( $this->should_render_blank_state() ) {
$this->render_blank_state();
return;
}
$this->views();
echo '<form id="wc-orders-filter" method="get" action="' . esc_url( get_admin_url( null, 'admin.php' ) ) . '">';
$this->print_hidden_form_fields();
$this->search_box( esc_html__( 'Search orders', 'woocommerce' ), 'orders-search-input' );
parent::display();
echo '</form> </div>';
}
/**
* Renders advice in the event that no orders exist yet.
*
* @return void
*/
public function render_blank_state(): void {
?>
<div class="woocommerce-BlankState">
<h2 class="woocommerce-BlankState-message">
<?php esc_html_e( 'When you receive a new order, it will appear here.', 'woocommerce' ); ?>
</h2>
<div class="woocommerce-BlankState-buttons">
<a class="woocommerce-BlankState-cta button-primary button" target="_blank" href="https://docs.woocommerce.com/document/managing-orders/?utm_source=blankslate&utm_medium=product&utm_content=ordersdoc&utm_campaign=woocommerceplugin"><?php esc_html_e( 'Learn more about orders', 'woocommerce' ); ?></a>
</div>
<?php
/**
* Renders after the 'blank state' message for the order list table has rendered.
*
* @since 6.6.1
*/
do_action( 'wc_marketplace_suggestions_orders_empty_state' ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingSinceComment
?>
</div>
<?php
}
/**
* Retrieves the list of bulk actions available for this table.
*
* @return array
*/
protected function get_bulk_actions() {
$selected_status = $this->order_query_args['status'] ?? false;
if ( array( 'trash' ) === $selected_status ) {
$actions = array(
'untrash' => __( 'Restore', 'woocommerce' ),
'delete' => __( 'Delete permanently', 'woocommerce' ),
);
} else {
$actions = array(
'mark_processing' => __( 'Change status to processing', 'woocommerce' ),
'mark_on-hold' => __( 'Change status to on-hold', 'woocommerce' ),
'mark_completed' => __( 'Change status to completed', 'woocommerce' ),
'mark_cancelled' => __( 'Change status to cancelled', 'woocommerce' ),
'trash' => __( 'Move to Trash', 'woocommerce' ),
);
}
if ( wc_string_to_bool( get_option( 'woocommerce_allow_bulk_remove_personal_data', 'no' ) ) ) {
$actions['remove_personal_data'] = __( 'Remove personal data', 'woocommerce' );
}
return $actions;
}
/**
* Gets a list of CSS classes for the WP_List_Table table tag.
*
* @since 7.8.0
*
* @return string[] Array of CSS classes for the table tag.
*/
protected function get_table_classes() {
/**
* Filters the list of CSS class names for the orders list table.
*
* @since 7.8.0
*
* @param string[] $classes An array of CSS class names.
* @param string $order_type The order type.
*/
$css_classes = apply_filters(
'woocommerce_' . $this->order_type . '_list_table_css_classes',
array_merge(
parent::get_table_classes(),
array(
'wc-orders-list-table',
'wc-orders-list-table-' . $this->order_type,
)
),
$this->order_type
);
return array_unique( array_map( 'trim', $css_classes ) );
}
/**
* Prepares the list of items for displaying.
*/
public function prepare_items() {
$limit = $this->get_items_per_page( 'edit_' . $this->order_type . '_per_page' );
$this->order_query_args = array(
'limit' => $limit,
'page' => $this->get_pagenum(),
'paginate' => true,
'type' => $this->order_type,
);
foreach ( array( 'status', 's', 'm', '_customer_user' ) as $query_var ) {
$this->request[ $query_var ] = sanitize_text_field( wp_unslash( $_REQUEST[ $query_var ] ?? '' ) );
}
/**
* Allows 3rd parties to filter the initial request vars before defaults and other logic is applied.
*
* @param array $request Request to be passed to `wc_get_orders()`.
*
* @since 7.3.0
*/
$this->request = apply_filters( 'woocommerce_' . $this->order_type . '_list_table_request', $this->request );
$this->set_status_args();
$this->set_order_args();
$this->set_date_args();
$this->set_customer_args();
$this->set_search_args();
/**
* Provides an opportunity to modify the query arguments used in the (Custom Order Table-powered) order list
* table.
*
* @since 6.9.0
*
* @param array $query_args Arguments to be passed to `wc_get_orders()`.
*/
$order_query_args = (array) apply_filters( 'woocommerce_order_list_table_prepare_items_query_args', $this->order_query_args );
/**
* Same as `woocommerce_order_list_table_prepare_items_query_args` but for a specific order type.
*
* @param array $query_args Arguments to be passed to `wc_get_orders()`.
*
* @since 7.3.0
*/
$order_query_args = apply_filters( 'woocommerce_' . $this->order_type . '_list_table_prepare_items_query_args', $order_query_args );
// We must ensure the 'paginate' argument is set.
$order_query_args['paginate'] = true;
$orders = wc_get_orders( $order_query_args );
$this->items = $orders->orders;
$max_num_pages = $orders->max_num_pages;
// Check in case the user has attempted to page beyond the available range of orders.
if ( 0 === $max_num_pages && $this->order_query_args['page'] > 1 ) {
$count_query_args = $order_query_args;
$count_query_args['page'] = 1;
$count_query_args['limit'] = 1;
$order_count = wc_get_orders( $count_query_args );
$max_num_pages = (int) ceil( $order_count->total / $order_query_args['limit'] );
}
$this->set_pagination_args(
array(
'total_items' => $orders->total ?? 0,
'per_page' => $limit,
'total_pages' => $max_num_pages,
)
);
// Are we inside the trash?
$this->is_trash = 'trash' === $this->request['status'];
}
/**
* Updates the WC Order Query arguments as needed to support orderable columns.
*/
private function set_order_args() {
$sortable = $this->get_sortable_columns();
$field = sanitize_text_field( wp_unslash( $_GET['orderby'] ?? '' ) );
$direction = strtoupper( sanitize_text_field( wp_unslash( $_GET['order'] ?? '' ) ) );
if ( ! in_array( $field, $sortable, true ) ) {
$this->order_query_args['orderby'] = 'date';
$this->order_query_args['order'] = 'DESC';
return;
}
$this->order_query_args['orderby'] = $field;
$this->order_query_args['order'] = in_array( $direction, array( 'ASC', 'DESC' ), true ) ? $direction : 'ASC';
}
/**
* Implements date (month-based) filtering.
*/
private function set_date_args() {
$year_month = sanitize_text_field( wp_unslash( $_GET['m'] ?? '' ) );
if ( empty( $year_month ) || ! preg_match( '/^[0-9]{6}$/', $year_month ) ) {
return;
}
$year = (int) substr( $year_month, 0, 4 );
$month = (int) substr( $year_month, 4, 2 );
if ( $month < 0 || $month > 12 ) {
return;
}
$last_day_of_month = date_create( "$year-$month" )->format( 'Y-m-t' );
$this->order_query_args['date_created'] = "$year-$month-01..." . $last_day_of_month;
$this->has_filter = true;
}
/**
* Implements filtering of orders by customer.
*/
private function set_customer_args() {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$customer = (int) wp_unslash( $_GET['_customer_user'] ?? '' );
if ( $customer < 1 ) {
return;
}
$this->order_query_args['customer'] = $customer;
$this->has_filter = true;
}
/**
* Implements filtering of orders by status.
*/
private function set_status_args() {
$status = array_filter( array_map( 'trim', (array) $this->request['status'] ) );
if ( empty( $status ) || in_array( 'all', $status, true ) ) {
/**
* Allows 3rd parties to set the default list of statuses for a given order type.
*
* @param string[] $statuses Statuses.
*
* @since 7.3.0
*/
$status = apply_filters(
'woocommerce_' . $this->order_type . '_list_table_default_statuses',
array_intersect(
array_keys( wc_get_order_statuses() ),
get_post_stati( array( 'show_in_admin_all_list' => true ), 'names' )
)
);
} else {
$this->has_filter = true;
}
$this->order_query_args['status'] = $status;
}
/**
* Implements order search.
*/
private function set_search_args(): void {
$search_term = trim( sanitize_text_field( $this->request['s'] ) );
if ( ! empty( $search_term ) ) {
$this->order_query_args['s'] = $search_term;
$this->has_filter = true;
}
}
/**
* Get the list of views for this table (all orders, completed orders, etc, each with a count of the number of
* corresponding orders).
*
* @return array
*/
public function get_views() {
$view_counts = array();
$view_links = array();
$statuses = $this->get_visible_statuses();
$current = ! empty( $this->request['status'] ) ? sanitize_text_field( $this->request['status'] ) : 'all';
$all_count = 0;
foreach ( array_keys( $statuses ) as $slug ) {
$total_in_status = $this->count_orders_by_status( $slug );
if ( $total_in_status > 0 ) {
$view_counts[ $slug ] = $total_in_status;
}
if ( ( get_post_status_object( $slug ) )->show_in_admin_all_list && 'auto-draft' !== $slug ) {
$all_count += $total_in_status;
}
}
$view_links['all'] = $this->get_view_link( 'all', __( 'All', 'woocommerce' ), $all_count, '' === $current || 'all' === $current );
foreach ( $view_counts as $slug => $count ) {
$view_links[ $slug ] = $this->get_view_link( $slug, $statuses[ $slug ], $count, $slug === $current );
}
return $view_links;
}
/**
* Count orders by status.
*
* @param string|string[] $status The order status we are interested in.
*
* @return int
*/
private function count_orders_by_status( $status ): int {
global $wpdb;
// Compute all counts and cache if necessary.
if ( is_null( $this->status_count_cache ) ) {
$orders_table = OrdersTableDataStore::get_orders_table_name();
$res = $wpdb->get_results(
$wpdb->prepare(
"SELECT status, COUNT(*) AS cnt FROM {$orders_table} WHERE type = %s GROUP BY status", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$this->order_type
),
ARRAY_A
);
$this->status_count_cache =
$res
? array_combine( array_column( $res, 'status' ), array_map( 'absint', array_column( $res, 'cnt' ) ) )
: array();
}
$status = (array) $status;
$count = array_sum( array_intersect_key( $this->status_count_cache, array_flip( $status ) ) );
/**
* Allows 3rd parties to modify the count of orders by status.
*
* @param int $count Number of orders for the given status.
* @param string[] $status List of order statuses in the count.
* @since 7.3.0
*/
return apply_filters(
'woocommerce_' . $this->order_type . '_list_table_order_count',
$count,
$status
);
}
/**
* Checks whether the blank state should be rendered or not. This depends on whether there are others with a visible
* status.
*
* @return boolean TRUE when the blank state should be rendered, FALSE otherwise.
*/
private function should_render_blank_state(): bool {
return ( ! $this->has_filter ) && 0 === $this->count_orders_by_status( array_keys( $this->get_visible_statuses() ) );
}
/**
* Returns a list of slug and labels for order statuses that should be visible in the status list.
*
* @return array slug => label array of order statuses.
*/
private function get_visible_statuses(): array {
return array_intersect_key(
array_merge(
wc_get_order_statuses(),
array(
'trash' => ( get_post_status_object( 'trash' ) )->label,
'draft' => ( get_post_status_object( 'draft' ) )->label,
'auto-draft' => ( get_post_status_object( 'auto-draft' ) )->label,
)
),
array_flip( get_post_stati( array( 'show_in_admin_status_list' => true ) ) )
);
}
/**
* Form a link to use in the list of table views.
*
* @param string $slug Slug used to identify the view (usually the order status slug).
* @param string $name Human-readable name of the view (usually the order status label).
* @param int $count Number of items in this view.
* @param bool $current If this is the current view.
*
* @return string
*/
private function get_view_link( string $slug, string $name, int $count, bool $current ): string {
$base_url = get_admin_url( null, 'admin.php?page=wc-orders' . ( 'shop_order' === $this->order_type ? '' : '--' . $this->order_type ) );
$url = esc_url( add_query_arg( 'status', $slug, $base_url ) );
$name = esc_html( $name );
$count = absint( $count );
$class = $current ? 'class="current"' : '';
return "<a href='$url' $class>$name <span class='count'>($count)</span></a>";
}
/**
* Extra controls to be displayed between bulk actions and pagination.
*
* @param string $which Either 'top' or 'bottom'.
*/
protected function extra_tablenav( $which ) {
echo '<div class="alignleft actions">';
if ( 'top' === $which ) {
ob_start();
$this->months_filter();
$this->customers_filter();
/**
* Fires before the "Filter" button on the list table for orders and other order types.
*
* @since 7.3.0
*
* @param string $order_type The order type.
* @param string $which The location of the extra table nav: 'top' or 'bottom'.
*/
do_action( 'woocommerce_order_list_table_restrict_manage_orders', $this->order_type, $which );
$output = ob_get_clean();
if ( ! empty( $output ) ) {
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
submit_button( __( 'Filter', 'woocommerce' ), '', 'filter_action', false, array( 'id' => 'order-query-submit' ) );
}
}
if ( $this->is_trash && $this->has_items() && current_user_can( 'edit_others_shop_orders' ) ) {
submit_button( __( 'Empty Trash', 'woocommerce' ), 'apply', 'delete_all', false );
}
/**
* Fires immediately following the closing "actions" div in the tablenav for the order
* list table.
*
* @since 7.3.0
*
* @param string $order_type The order type.
* @param string $which The location of the extra table nav: 'top' or 'bottom'.
*/
do_action( 'woocommerce_order_list_table_extra_tablenav', $this->order_type, $which );
echo '</div>';
}
/**
* Render the months filter dropdown.
*
* @return void
*/
private function months_filter() {
// XXX: [review] we may prefer to move this logic outside of the ListTable class.
global $wp_locale;
global $wpdb;
$orders_table = esc_sql( OrdersTableDataStore::get_orders_table_name() );
$utc_offset = wc_timezone_offset();
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$order_dates = $wpdb->get_results(
"
SELECT DISTINCT YEAR( t.date_created_local ) AS year,
MONTH( t.date_created_local ) AS month
FROM ( SELECT DATE_ADD( date_created_gmt, INTERVAL $utc_offset SECOND ) AS date_created_local FROM $orders_table WHERE status != 'trash' ) t
ORDER BY year DESC, month DESC
"
);
$m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
echo '<select name="m" id="filter-by-date">';
echo '<option ' . selected( $m, 0, false ) . ' value="0">' . esc_html__( 'All dates', 'woocommerce' ) . '</option>';
foreach ( $order_dates as $date ) {
$month = zeroise( $date->month, 2 );
$month_year_text = sprintf(
/* translators: 1: Month name, 2: 4-digit year. */
esc_html_x( '%1$s %2$d', 'order dates dropdown', 'woocommerce' ),
$wp_locale->get_month( $month ),
$date->year
);
printf(
'<option %1$s value="%2$s">%3$s</option>\n',
selected( $m, $date->year . $month, false ),
esc_attr( $date->year . $month ),
esc_html( $month_year_text )
);
}
echo '</select>';
}
/**
* Render the customer filter dropdown.
*
* @return void
*/
public function customers_filter() {
$user_string = '';
$user_id = '';
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( ! empty( $_GET['_customer_user'] ) ) {
$user_id = absint( $_GET['_customer_user'] );
$user = get_user_by( 'id', $user_id );
$user_string = sprintf(
/* translators: 1: user display name 2: user ID 3: user email */
esc_html__( '%1$s (#%2$s – %3$s)', 'woocommerce' ),
$user->display_name,
absint( $user->ID ),
$user->user_email
);
}
// Note: use of htmlspecialchars (below) is to prevent XSS when rendered by selectWoo.
?>
<select class="wc-customer-search" name="_customer_user" data-placeholder="<?php esc_attr_e( 'Filter by registered customer', 'woocommerce' ); ?>" data-allow_clear="true">
<option value="<?php echo esc_attr( $user_id ); ?>" selected="selected"><?php echo htmlspecialchars( wp_kses_post( $user_string ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></option>
</select>
<?php
}
/**
* Get list columns.
*
* @return array
*/
public function get_columns() {
/**
* Filters the list of columns.
*
* @param array $columns List of sortable columns.
*
* @since 7.3.0
*/
return apply_filters(
'woocommerce_' . $this->order_type . '_list_table_columns',
array(
'cb' => '<input type="checkbox" />',
'order_number' => esc_html__( 'Order', 'woocommerce' ),
'order_date' => esc_html__( 'Date', 'woocommerce' ),
'order_status' => esc_html__( 'Status', 'woocommerce' ),
'billing_address' => esc_html__( 'Billing', 'woocommerce' ),
'shipping_address' => esc_html__( 'Ship to', 'woocommerce' ),
'order_total' => esc_html__( 'Total', 'woocommerce' ),
'wc_actions' => esc_html__( 'Actions', 'woocommerce' ),
)
);
}
/**
* Defines the default sortable columns.
*
* @return string[]
*/
public function get_sortable_columns() {
/**
* Filters the list of sortable columns.
*
* @param array $sortable_columns List of sortable columns.
*
* @since 7.3.0
*/
return apply_filters(
'woocommerce_' . $this->order_type . '_list_table_sortable_columns',
array(
'order_number' => 'ID',
'order_date' => 'date',
'order_total' => 'order_total',
)
);
}
/**
* Specify the columns we wish to hide by default.
*
* @param array $hidden Columns set to be hidden.
* @param WP_Screen $screen Screen object.
*
* @return array
*/
public function default_hidden_columns( array $hidden, WP_Screen $screen ) {
if ( isset( $screen->id ) && wc_get_page_screen_id( 'shop-order' ) === $screen->id ) {
$hidden = array_merge(
$hidden,
array(
'billing_address',
'shipping_address',
'wc_actions',
)
);
}
return $hidden;
}
/**
* Checklist column, used for selecting items for processing by a bulk action.
*
* @param WC_Order $item The order object for the current row.
*
* @return string
*/
public function column_cb( $item ) {
ob_start();
?>
<input id="cb-select-<?php echo esc_attr( $item->get_id() ); ?>" type="checkbox" name="id[]" value="<?php echo esc_attr( $item->get_id() ); ?>" />
<div class="locked-indicator">
<span class="locked-indicator-icon" aria-hidden="true"></span>
<span class="screen-reader-text">
<?php
// translators: %s is an order ID.
echo esc_html( sprintf( __( 'Order %s is locked.', 'woocommerce' ), $item->get_id() ) );
?>
</span>
</div>
<?php
return ob_get_clean();
}
/**
* Renders the order number, customer name and provides a preview link.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_order_number_column( WC_Order $order ): void {
$buyer = '';
if ( $order->get_billing_first_name() || $order->get_billing_last_name() ) {
/* translators: 1: first name 2: last name */
$buyer = trim( sprintf( _x( '%1$s %2$s', 'full name', 'woocommerce' ), $order->get_billing_first_name(), $order->get_billing_last_name() ) );
} elseif ( $order->get_billing_company() ) {
$buyer = trim( $order->get_billing_company() );
} elseif ( $order->get_customer_id() ) {
$user = get_user_by( 'id', $order->get_customer_id() );
$buyer = ucwords( $user->display_name );
}
/**
* Filter buyer name in list table orders.
*
* @since 3.7.0
*
* @param string $buyer Buyer name.
* @param WC_Order $order Order data.
*/
$buyer = apply_filters( 'woocommerce_admin_order_buyer_name', $buyer, $order );
if ( $order->get_status() === 'trash' ) {
echo '<strong>#' . esc_attr( $order->get_order_number() ) . ' ' . esc_html( $buyer ) . '</strong>';
} else {
echo '<a href="#" class="order-preview" data-order-id="' . absint( $order->get_id() ) . '" title="' . esc_attr( __( 'Preview', 'woocommerce' ) ) . '">' . esc_html( __( 'Preview', 'woocommerce' ) ) . '</a>';
echo '<a href="' . esc_url( $this->get_order_edit_link( $order ) ) . '" class="order-view"><strong>#' . esc_attr( $order->get_order_number() ) . ' ' . esc_html( $buyer ) . '</strong></a>';
}
}
/**
* Get the edit link for an order.
*
* @param WC_Order $order Order object.
*
* @return string Edit link for the order.
*/
private function get_order_edit_link( WC_Order $order ) : string {
return $this->page_controller->get_edit_url( $order->get_id() );
}
/**
* Renders the order date.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_order_date_column( WC_Order $order ): void {
$order_timestamp = $order->get_date_created() ? $order->get_date_created()->getTimestamp() : '';
if ( ! $order_timestamp ) {
echo '–';
return;
}
// Check if the order was created within the last 24 hours, and not in the future.
if ( $order_timestamp > strtotime( '-1 day', time() ) && $order_timestamp <= time() ) {
$show_date = sprintf(
/* translators: %s: human-readable time difference */
_x( '%s ago', '%s = human-readable time difference', 'woocommerce' ),
human_time_diff( $order->get_date_created()->getTimestamp(), time() )
);
} else {
$show_date = $order->get_date_created()->date_i18n( apply_filters( 'woocommerce_admin_order_date_format', __( 'M j, Y', 'woocommerce' ) ) ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
}
printf(
'<time datetime="%1$s" title="%2$s">%3$s</time>',
esc_attr( $order->get_date_created()->date( 'c' ) ),
esc_html( $order->get_date_created()->date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ) ),
esc_html( $show_date )
);
}
/**
* Renders the order status.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_order_status_column( WC_Order $order ): void {
$tooltip = '';
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
$comment_count = get_comment_count( $order->get_id() );
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
$approved_comments_count = absint( $comment_count['approved'] );
if ( $approved_comments_count ) {
$latest_notes = wc_get_order_notes(
array(
'order_id' => $order->get_id(),
'limit' => 1,
'orderby' => 'date_created_gmt',
)
);
$latest_note = current( $latest_notes );
if ( isset( $latest_note->content ) && 1 === $approved_comments_count ) {
$tooltip = wc_sanitize_tooltip( $latest_note->content );
} elseif ( isset( $latest_note->content ) ) {
/* translators: %d: notes count */
$tooltip = wc_sanitize_tooltip( $latest_note->content . '<br/><small style="display:block">' . sprintf( _n( 'Plus %d other note', 'Plus %d other notes', ( $approved_comments_count - 1 ), 'woocommerce' ), $approved_comments_count - 1 ) . '</small>' );
} else {
/* translators: %d: notes count */
$tooltip = wc_sanitize_tooltip( sprintf( _n( '%d note', '%d notes', $approved_comments_count, 'woocommerce' ), $approved_comments_count ) );
}
}
// Gracefully handle legacy statuses.
if ( in_array( $order->get_status(), array( 'trash', 'draft', 'auto-draft' ), true ) ) {
$status_name = ( get_post_status_object( $order->get_status() ) )->label;
} else {
$status_name = wc_get_order_status_name( $order->get_status() );
}
if ( $tooltip ) {
printf( '<mark class="order-status %s tips" data-tip="%s"><span>%s</span></mark>', esc_attr( sanitize_html_class( 'status-' . $order->get_status() ) ), wp_kses_post( $tooltip ), esc_html( $status_name ) );
} else {
printf( '<mark class="order-status %s"><span>%s</span></mark>', esc_attr( sanitize_html_class( 'status-' . $order->get_status() ) ), esc_html( $status_name ) );
}
}
/**
* Renders order billing information.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_billing_address_column( WC_Order $order ): void {
$address = $order->get_formatted_billing_address();
if ( $address ) {
echo esc_html( preg_replace( '#<br\s*/?>#i', ', ', $address ) );
if ( $order->get_payment_method() ) {
/* translators: %s: payment method */
echo '<span class="description">' . sprintf( esc_html__( 'via %s', 'woocommerce' ), esc_html( $order->get_payment_method_title() ) ) . '</span>';
}
} else {
echo '–';
}
}
/**
* Renders order shipping information.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_shipping_address_column( WC_Order $order ): void {
$address = $order->get_formatted_shipping_address();
if ( $address ) {
echo '<a target="_blank" href="' . esc_url( $order->get_shipping_address_map_url() ) . '">' . esc_html( preg_replace( '#<br\s*/?>#i', ', ', $address ) ) . '</a>';
if ( $order->get_shipping_method() ) {
/* translators: %s: shipping method */
echo '<span class="description">' . sprintf( esc_html__( 'via %s', 'woocommerce' ), esc_html( $order->get_shipping_method() ) ) . '</span>';
}
} else {
echo '–';
}
}
/**
* Renders the order total.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_order_total_column( WC_Order $order ): void {
if ( $order->get_payment_method_title() ) {
/* translators: %s: method */
echo '<span class="tips" data-tip="' . esc_attr( sprintf( __( 'via %s', 'woocommerce' ), $order->get_payment_method_title() ) ) . '">' . wp_kses_post( $order->get_formatted_order_total() ) . '</span>';
} else {
echo wp_kses_post( $order->get_formatted_order_total() );
}
}
/**
* Renders order actions.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_wc_actions_column( WC_Order $order ): void {
echo '<p>';
/**
* Fires before the order action buttons (within the actions column for the order list table)
* are registered.
*
* @param WC_Order $order Current order object.
* @since 6.7.0
*/
do_action( 'woocommerce_admin_order_actions_start', $order );
$actions = array();
if ( $order->has_status( array( 'pending', 'on-hold' ) ) ) {
$actions['processing'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=processing&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ),
'name' => __( 'Processing', 'woocommerce' ),
'action' => 'processing',
);
}
if ( $order->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) {
$actions['complete'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ),
'name' => __( 'Complete', 'woocommerce' ),
'action' => 'complete',
);
}
/**
* Provides an opportunity to modify the action buttons within the order list table.
*
* @param array $action Order actions.
* @param WC_Order $order Current order object.
* @since 6.7.0
*/
$actions = apply_filters( 'woocommerce_admin_order_actions', $actions, $order );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo wc_render_action_buttons( $actions );
/**
* Fires after the order action buttons (within the actions column for the order list table)
* are rendered.
*
* @param WC_Order $order Current order object.
* @since 6.7.0
*/
do_action( 'woocommerce_admin_order_actions_end', $order );
echo '</p>';
}
/**
* Outputs hidden fields used to retain state when filtering.
*
* @return void
*/
private function print_hidden_form_fields(): void {
echo '<input type="hidden" name="page" value="wc-orders' . ( 'shop_order' === $this->order_type ? '' : '--' . $this->order_type ) . '" >'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$state_params = array(
'paged',
'status',
);
foreach ( $state_params as $param ) {
if ( ! isset( $_GET[ $param ] ) ) {
continue;
}
echo '<input type="hidden" name="' . esc_attr( $param ) . '" value="' . esc_attr( sanitize_text_field( wp_unslash( $_GET[ $param ] ) ) ) . '" >';
}
}
/**
* Gets the current action selected from the bulk actions dropdown.
*
* @return string|false The action name. False if no action was selected.
*/
public function current_action() {
if ( ! empty( $_REQUEST['delete_all'] ) ) {
return 'delete_all';
}
return parent::current_action();
}
/**
* Handle bulk actions.
*/
public function handle_bulk_actions() {
$action = $this->current_action();
if ( ! $action ) {
return;
}
check_admin_referer( 'bulk-orders' );
$redirect_to = remove_query_arg( array( 'deleted', 'ids' ), wp_get_referer() );
$redirect_to = add_query_arg( 'paged', $this->get_pagenum(), $redirect_to );
if ( 'delete_all' === $action ) {
// Get all trashed orders.
$ids = wc_get_orders(
array(
'type' => $this->order_type,
'status' => 'trash',
'limit' => -1,
'return' => 'ids',
)
);
$action = 'delete';
} else {
$ids = isset( $_REQUEST['id'] ) ? array_reverse( array_map( 'absint', (array) $_REQUEST['id'] ) ) : array();
}
/**
* Allows 3rd parties to modify order IDs about to be affected by a bulk action.
*
* @param array Array of order IDs.
*/
$ids = apply_filters( // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingSinceComment
'woocommerce_bulk_action_ids',
$ids,
$action,
'order'
);
if ( ! $ids ) {
wp_safe_redirect( $redirect_to );
exit;
}
$report_action = '';
$changed = 0;
$action_handled = true;
if ( 'remove_personal_data' === $action ) {
$report_action = 'removed_personal_data';
$changed = $this->do_bulk_action_remove_personal_data( $ids );
} elseif ( 'trash' === $action ) {
$changed = $this->do_delete( $ids );
$report_action = 'trashed';
} elseif ( 'delete' === $action ) {
$changed = $this->do_delete( $ids, true );
$report_action = 'deleted';
} elseif ( 'untrash' === $action ) {
$changed = $this->do_untrash( $ids );
$report_action = 'untrashed';
} elseif ( false !== strpos( $action, 'mark_' ) ) {
$order_statuses = wc_get_order_statuses();
$new_status = substr( $action, 5 );
$report_action = 'marked_' . $new_status;
if ( isset( $order_statuses[ 'wc-' . $new_status ] ) ) {
$changed = $this->do_bulk_action_mark_orders( $ids, $new_status );
} else {
$action_handled = false;
}
} else {
$action_handled = false;
}
// Custom action.
if ( ! $action_handled ) {
$screen = get_current_screen()->id;
/**
* This action is documented in /wp-admin/edit.php (it is a core WordPress hook).
*
* @since 7.2.0
*
* @param string $redirect_to The URL to redirect to after processing the bulk actions.
* @param string $action The current bulk action.
* @param int[] $ids IDs for the orders to be processed.
*/
$custom_sendback = apply_filters( "handle_bulk_actions-{$screen}", $redirect_to, $action, $ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}
if ( ! empty( $custom_sendback ) ) {
$redirect_to = $custom_sendback;
} elseif ( $changed ) {
$redirect_to = add_query_arg(
array(
'bulk_action' => $report_action,
'changed' => $changed,
'ids' => implode( ',', $ids ),
),
$redirect_to
);
}
wp_safe_redirect( $redirect_to );
exit;
}
/**
* Implements the "remove personal data" bulk action.
*
* @param array $order_ids The Order IDs.
* @return int Number of orders modified.
*/
private function do_bulk_action_remove_personal_data( $order_ids ): int {
$changed = 0;
foreach ( $order_ids as $id ) {
$order = wc_get_order( $id );
if ( ! $order ) {
continue;
}
do_action( 'woocommerce_remove_order_personal_data', $order ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
$changed++;
}
return $changed;
}
/**
* Implements the "mark <status>" bulk action.
*
* @param array $order_ids The order IDs to change.
* @param string $new_status The new order status.
* @return int Number of orders modified.
*/
private function do_bulk_action_mark_orders( $order_ids, $new_status ): int {
$changed = 0;
// Initialize payment gateways in case order has hooked status transition actions.
WC()->payment_gateways();
foreach ( $order_ids as $id ) {
$order = wc_get_order( $id );
if ( ! $order ) {
continue;
}
$order->update_status( $new_status, __( 'Order status changed by bulk edit.', 'woocommerce' ), true );
do_action( 'woocommerce_order_edit_status', $id, $new_status ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
$changed++;
}
return $changed;
}
/**
* Handles bulk trashing of orders.
*
* @param int[] $ids Order IDs to be trashed.
* @param bool $force_delete When set, the order will be completed deleted. Otherwise, it will be trashed.
*
* @return int Number of orders that were trashed.
*/
private function do_delete( array $ids, bool $force_delete = false ): int {
$changed = 0;
foreach ( $ids as $id ) {
$order = wc_get_order( $id );
$order->delete( $force_delete );
$updated_order = wc_get_order( $id );
if ( ( $force_delete && false === $updated_order ) || ( ! $force_delete && $updated_order->get_status() === 'trash' ) ) {
$changed++;
}
}
return $changed;
}
/**
* Handles bulk restoration of trashed orders.
*
* @param array $ids Order IDs to be restored to their previous status.
*
* @return int Number of orders that were restored from the trash.
*/
private function do_untrash( array $ids ): int {
$orders_store = wc_get_container()->get( OrdersTableDataStore::class );
$changed = 0;
foreach ( $ids as $id ) {
if ( $orders_store->untrash_order( wc_get_order( $id ) ) ) {
$changed++;
}
}
return $changed;
}
/**
* Show confirmation message that order status changed for number of orders.
*/
public function bulk_action_notices() {
if ( empty( $_REQUEST['bulk_action'] ) ) {
return;
}
$order_statuses = wc_get_order_statuses();
$number = absint( $_REQUEST['changed'] ?? 0 );
$bulk_action = wc_clean( wp_unslash( $_REQUEST['bulk_action'] ) );
$message = '';
// Check if any status changes happened.
foreach ( $order_statuses as $slug => $name ) {
if ( 'marked_' . str_replace( 'wc-', '', $slug ) === $bulk_action ) { // WPCS: input var ok, CSRF ok.
/* translators: %s: orders count */
$message = sprintf( _n( '%s order status changed.', '%s order statuses changed.', $number, 'woocommerce' ), number_format_i18n( $number ) );
break;
}
}
switch ( $bulk_action ) {
case 'removed_personal_data':
/* translators: %s: orders count */
$message = sprintf( _n( 'Removed personal data from %s order.', 'Removed personal data from %s orders.', $number, 'woocommerce' ), number_format_i18n( $number ) );
echo '<div class="updated"><p>' . esc_html( $message ) . '</p></div>';
break;
case 'trashed':
/* translators: %s: orders count */
$message = sprintf( _n( '%s order moved to the Trash.', '%s orders moved to the Trash.', $number, 'woocommerce' ), number_format_i18n( $number ) );
break;
case 'untrashed':
/* translators: %s: orders count */
$message = sprintf( _n( '%s order restored from the Trash.', '%s orders restored from the Trash.', $number, 'woocommerce' ), number_format_i18n( $number ) );
break;
case 'deleted':
/* translators: %s: orders count */
$message = sprintf( _n( '%s order permanently deleted.', '%s orders permanently deleted.', $number, 'woocommerce' ), number_format_i18n( $number ) );
break;
}
if ( ! empty( $message ) ) {
echo '<div class="updated"><p>' . esc_html( $message ) . '</p></div>';
}
}
/**
* Enqueue list table scripts.
*
* @return void
*/
public function enqueue_scripts(): void {
echo $this->get_order_preview_template(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
wp_enqueue_script( 'wc-orders' );
}
/**
* Returns the HTML for the order preview template.
*
* @return string HTML template.
*/
public function get_order_preview_template(): string {
$order_edit_url_placeholder =
wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled()
? esc_url( admin_url( 'admin.php?page=wc-orders&action=edit' ) ) . '&id={{ data.data.id }}'
: esc_url( admin_url( 'post.php?action=edit' ) ) . '&post={{ data.data.id }}';
ob_start();
?>
<script type="text/template" id="tmpl-wc-modal-view-order">
<div class="wc-backbone-modal wc-order-preview">
<div class="wc-backbone-modal-content">
<section class="wc-backbone-modal-main" role="main">
<header class="wc-backbone-modal-header">
<mark class="order-status status-{{ data.status }}"><span>{{ data.status_name }}</span></mark>
<?php /* translators: %s: order ID */ ?>
<h1><?php echo esc_html( sprintf( __( 'Order #%s', 'woocommerce' ), '{{ data.order_number }}' ) ); ?></h1>
<button class="modal-close modal-close-link dashicons dashicons-no-alt">
<span class="screen-reader-text"><?php esc_html_e( 'Close modal panel', 'woocommerce' ); ?></span>
</button>
</header>
<article>
<?php do_action( 'woocommerce_admin_order_preview_start' ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment ?>
<div class="wc-order-preview-addresses">
<div class="wc-order-preview-address">
<h2><?php esc_html_e( 'Billing details', 'woocommerce' ); ?></h2>
{{{ data.formatted_billing_address }}}
<# if ( data.data.billing.email ) { #>
<strong><?php esc_html_e( 'Email', 'woocommerce' ); ?></strong>
<a href="mailto:{{ data.data.billing.email }}">{{ data.data.billing.email }}</a>
<# } #>
<# if ( data.data.billing.phone ) { #>
<strong><?php esc_html_e( 'Phone', 'woocommerce' ); ?></strong>
<a href="tel:{{ data.data.billing.phone }}">{{ data.data.billing.phone }}</a>
<# } #>
<# if ( data.payment_via ) { #>
<strong><?php esc_html_e( 'Payment via', 'woocommerce' ); ?></strong>
{{{ data.payment_via }}}
<# } #>
</div>
<# if ( data.needs_shipping ) { #>
<div class="wc-order-preview-address">
<h2><?php esc_html_e( 'Shipping details', 'woocommerce' ); ?></h2>
<# if ( data.ship_to_billing ) { #>
{{{ data.formatted_billing_address }}}
<# } else { #>
<a href="{{ data.shipping_address_map_url }}" target="_blank">{{{ data.formatted_shipping_address }}}</a>
<# } #>
<# if ( data.shipping_via ) { #>
<strong><?php esc_html_e( 'Shipping method', 'woocommerce' ); ?></strong>
{{ data.shipping_via }}
<# } #>
</div>
<# } #>
<# if ( data.data.customer_note ) { #>
<div class="wc-order-preview-note">
<strong><?php esc_html_e( 'Note', 'woocommerce' ); ?></strong>
{{ data.data.customer_note }}
</div>
<# } #>
</div>
{{{ data.item_html }}}
<?php do_action( 'woocommerce_admin_order_preview_end' ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment ?>
</article>
<footer>
<div class="inner">
{{{ data.actions_html }}}
<a class="button button-primary button-large" aria-label="<?php esc_attr_e( 'Edit this order', 'woocommerce' ); ?>" href="<?php echo $order_edit_url_placeholder; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"><?php esc_html_e( 'Edit', 'woocommerce' ); ?></a>
</div>
</footer>
</section>
</div>
</div>
<div class="wc-backbone-modal-backdrop modal-close"></div>
</script>
<?php
$html = ob_get_clean();
return $html;
}
}
Orders/MetaBoxes/CustomMetaBox.php 0000644 00000036731 15154512352 0013151 0 ustar 00 <?php
/**
* Meta box to edit and add custom meta values for an order.
*/
namespace Automattic\WooCommerce\Internal\Admin\Orders\MetaBoxes;
use WC_Data_Store;
use WC_Meta_Data;
use WC_Order;
use WP_Ajax_Response;
/**
* Class CustomMetaBox.
*/
class CustomMetaBox {
/**
* Update nonce shared among different meta rows.
*
* @var string
*/
private $update_nonce;
/**
* Helper method to get formatted meta data array with proper keys. This can be directly fed to `list_meta()` method.
*
* @param \WC_Order $order Order object.
*
* @return array Meta data.
*/
private function get_formatted_order_meta_data( \WC_Order $order ) {
$metadata = $order->get_meta_data();
$metadata_to_list = array();
foreach ( $metadata as $meta ) {
$data = $meta->get_data();
if ( is_protected_meta( $data['key'], 'order' ) ) {
continue;
}
$metadata_to_list[] = array(
'meta_id' => $data['id'],
'meta_key' => $data['key'], // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- False positive, not a meta query.
'meta_value' => maybe_serialize( $data['value'] ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- False positive, not a meta query.
);
}
return $metadata_to_list;
}
/**
* Renders the meta box to manage custom meta.
*
* @param \WP_Post|\WC_Order $order_or_post Post or order object that we are rendering for.
*/
public function output( $order_or_post ) {
if ( is_a( $order_or_post, \WP_Post::class ) ) {
$order = wc_get_order( $order_or_post );
} else {
$order = $order_or_post;
}
$this->render_custom_meta_form( $this->get_formatted_order_meta_data( $order ), $order );
}
/**
* Helper method to render layout and actual HTML
*
* @param array $metadata_to_list List of metadata to render.
* @param \WC_Order $order Order object.
*/
private function render_custom_meta_form( array $metadata_to_list, \WC_Order $order ) {
?>
<div id="postcustomstuff">
<div id="ajax-response"></div>
<?php
list_meta( $metadata_to_list );
$this->render_meta_form( $order );
?>
</div>
<p>
<?php
printf(
/* translators: 1: opening documentation tag 2: closing documentation tag. */
esc_html( __( 'Custom fields can be used to add extra metadata to an order that you can %1$suse in your theme%2$s.', 'woocommerce' ) ),
'<a href="' . esc_attr__( 'https://wordpress.org/support/article/custom-fields/', 'woocommerce' ) . '">',
'</a>'
);
?>
</p>
<?php
}
/**
* Compute keys to display in autofill when adding new meta key entry in custom meta box.
* Currently, returns empty keys, will be implemented after caching is merged.
*
* @param array|null $keys Keys to display in autofill.
* @param \WP_Post|\WC_Order $order Order object.
*
* @return array|mixed Array of keys to display in autofill.
*/
public function order_meta_keys_autofill( $keys, $order ) {
if ( is_a( $order, \WC_Order::class ) ) {
return array();
}
return $keys;
}
/**
* Reimplementation of WP core's `meta_form` function. Renders meta form box.
*
* @param \WC_Order $order WC_Order object.
*
* @return void
*/
public function render_meta_form( \WC_Order $order ) : void {
$meta_key_input_id = 'metakeyselect';
$keys = $this->order_meta_keys_autofill( null, $order );
/**
* Filters values for the meta key dropdown in the Custom Fields meta box.
*
* Compatibility filter for `postmeta_form_keys` filter.
*
* @since 6.9.0
*
* @param array|null $keys Pre-defined meta keys to be used in place of a postmeta query. Default null.
* @param \WC_Order $order The current post object.
*/
$keys = apply_filters( 'postmeta_form_keys', $keys, $order );
?>
<p><strong><?php esc_html_e( 'Add New Custom Field:', 'woocommerce' ); ?></strong></p>
<table id="newmeta">
<thead>
<tr>
<th class="left"><label for="<?php echo esc_attr( $meta_key_input_id ); ?>"><?php esc_html_e( 'Name', 'woocommerce' ); ?></label></th>
<th><label for="metavalue"><?php esc_html_e( 'Value', 'woocommerce' ); ?></label></th>
</tr>
</thead>
<tbody>
<tr>
<td id="newmetaleft" class="left">
<?php if ( $keys ) { ?>
<select id="metakeyselect" name="metakeyselect">
<option value="#NONE#"><?php esc_html_e( '— Select —', 'woocommerce' ); ?></option>
<?php
foreach ( $keys as $key ) {
if ( is_protected_meta( $key, 'post' ) || ! current_user_can( 'edit_others_shop_order', $order->get_id() ) ) {
continue;
}
echo "\n<option value='" . esc_attr( $key ) . "'>" . esc_html( $key ) . '</option>';
}
?>
</select>
<input class="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
<span id="enternew"><?php esc_html_e( 'Enter new', 'woocommerce' ); ?></span>
<span id="cancelnew" class="hidden"><?php esc_html_e( 'Cancel', 'woocommerce' ); ?></span></a>
<?php } else { ?>
<input type="text" id="metakeyinput" name="metakeyinput" value="" />
<?php } ?>
</td>
<td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td>
</tr>
<tr><td colspan="2">
<div class="submit">
<?php
submit_button(
__( 'Add Custom Field', 'woocommerce' ),
'',
'addmeta',
false,
array(
'id' => 'newmeta-submit',
'data-wp-lists' => 'add:the-list:newmeta',
)
);
?>
</div>
<?php wp_nonce_field( 'add-meta', '_ajax_nonce-add-meta', false ); ?>
</td></tr>
</tbody>
</table>
<?php
}
/**
* Helper method to verify order edit permissions.
*
* @param int $order_id Order ID.
*
* @return ?WC_Order WC_Order object if the user can edit the order, die otherwise.
*/
private function verify_order_edit_permission_for_ajax( int $order_id ): ?WC_Order {
if ( ! current_user_can( 'manage_woocommerce' ) || ! current_user_can( 'edit_others_shop_orders' ) ) {
wp_send_json_error( 'missing_capabilities' );
wp_die();
}
$order = wc_get_order( $order_id );
if ( ! $order ) {
wp_send_json_error( 'invalid_order_id' );
wp_die();
}
return $order;
}
/**
* Reimplementation of WP core's `wp_ajax_add_meta` method to support order custom meta updates with custom tables.
*/
public function add_meta_ajax() {
if ( ! check_ajax_referer( 'add-meta', '_ajax_nonce-add-meta' ) ) {
wp_send_json_error( 'invalid_nonce' );
wp_die();
}
$order_id = (int) $_POST['order_id'] ?? 0;
$order = $this->verify_order_edit_permission_for_ajax( $order_id );
if ( isset( $_POST['metakeyselect'] ) && '#NONE#' === $_POST['metakeyselect'] && empty( $_POST['metakeyinput'] ) ) {
wp_die( 1 );
}
if ( isset( $_POST['metakeyinput'] ) ) { // add meta.
$meta_key = sanitize_text_field( wp_unslash( $_POST['metakeyinput'] ) );
$meta_value = sanitize_text_field( wp_unslash( $_POST['metavalue'] ?? '' ) );
$this->handle_add_meta( $order, $meta_key, $meta_value );
} else { // update.
$meta = wp_unslash( $_POST['meta'] ?? array() ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitization done below in array_walk.
$this->handle_update_meta( $order, $meta );
}
}
/**
* Part of WP Core's `wp_ajax_add_meta`. This is re-implemented to support updating meta for custom tables.
*
* @param WC_Order $order Order object.
* @param string $meta_key Meta key.
* @param string $meta_value Meta value.
*
* @return void
*/
private function handle_add_meta( WC_Order $order, string $meta_key, string $meta_value ) {
$count = 0;
if ( is_protected_meta( $meta_key ) ) {
wp_send_json_error( 'protected_meta' );
wp_die();
}
$metas_for_current_key = wp_list_filter( $order->get_meta_data(), array( 'key' => $meta_key ) );
$meta_ids = wp_list_pluck( $metas_for_current_key, 'id' );
$order->add_meta_data( $meta_key, $meta_value );
$order->save_meta_data();
$metas_for_current_key_with_new = wp_list_filter( $order->get_meta_data(), array( 'key' => $meta_key ) );
$meta_id = 0;
$new_meta_ids = wp_list_pluck( $metas_for_current_key_with_new, 'id' );
$new_meta_ids = array_values( array_diff( $new_meta_ids, $meta_ids ) );
if ( count( $new_meta_ids ) > 0 ) {
$meta_id = $new_meta_ids[0];
}
$response = new WP_Ajax_Response(
array(
'what' => 'meta',
'id' => $meta_id,
'data' => $this->list_meta_row(
array(
'meta_id' => $meta_id,
'meta_key' => $meta_key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- false positive, not a meta query.
'meta_value' => $meta_value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- false positive, not a meta query.
),
$count
),
'position' => 1,
)
);
$response->send();
}
/**
* Handles updating metadata.
*
* @param WC_Order $order Order object.
* @param array $meta Meta object to update.
*
* @return void
*/
private function handle_update_meta( WC_Order $order, array $meta ) {
if ( ! is_array( $meta ) ) {
wp_send_json_error( 'invalid_meta' );
wp_die();
}
array_walk( $meta, 'sanitize_text_field' );
$mid = (int) key( $meta );
if ( ! $mid ) {
wp_send_json_error( 'invalid_meta_id' );
wp_die();
}
$key = $meta[ $mid ]['key'];
$value = $meta[ $mid ]['value'];
if ( is_protected_meta( $key ) ) {
wp_send_json_error( 'protected_meta' );
wp_die();
}
if ( '' === trim( $key ) ) {
wp_send_json_error( 'invalid_meta_key' );
wp_die();
}
$count = 0;
$order->update_meta_data( $key, $value, $mid );
$order->save_meta_data();
$response = new WP_Ajax_Response(
array(
'what' => 'meta',
'id' => $mid,
'old_id' => $mid,
'data' => $this->list_meta_row(
array(
'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- false positive, not a meta query.
'meta_value' => $value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- false positive, not a meta query.
'meta_id' => $mid,
),
$count
),
'position' => 0,
)
);
$response->send();
}
/**
* Outputs a single row of public meta data in the Custom Fields meta box.
*
* @since 2.5.0
*
* @param array $entry Meta entry.
* @param int $count Sequence number of meta entries.
* @return string
*/
private function list_meta_row( array $entry, int &$count ) : string {
if ( is_protected_meta( $entry['meta_key'], 'post' ) ) {
return '';
}
if ( ! $this->update_nonce ) {
$this->update_nonce = wp_create_nonce( 'add-meta' );
}
$r = '';
++ $count;
if ( is_serialized( $entry['meta_value'] ) ) {
if ( is_serialized_string( $entry['meta_value'] ) ) {
// This is a serialized string, so we should display it.
$entry['meta_value'] = maybe_unserialize( $entry['meta_value'] ); // // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- false positive, not a meta query.
} else {
// This is a serialized array/object so we should NOT display it.
--$count;
return '';
}
}
$entry['meta_key'] = esc_attr( $entry['meta_key'] ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- false positive, not a meta query.
$entry['meta_value'] = esc_textarea( $entry['meta_value'] ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- false positive, not a meta query.
$entry['meta_id'] = (int) $entry['meta_id'];
$delete_nonce = wp_create_nonce( 'delete-meta_' . $entry['meta_id'] );
$r .= "\n\t<tr id='meta-{$entry['meta_id']}'>";
$r .= "\n\t\t<td class='left'><label class='screen-reader-text' for='meta-{$entry['meta_id']}-key'>" . __( 'Key', 'woocommerce' ) . "</label><input name='meta[{$entry['meta_id']}][key]' id='meta-{$entry['meta_id']}-key' type='text' size='20' value='{$entry['meta_key']}' />";
$r .= "\n\t\t<div class='submit'>";
$r .= get_submit_button( __( 'Delete', 'woocommerce' ), 'deletemeta small', "deletemeta[{$entry['meta_id']}]", false, array( 'data-wp-lists' => "delete:the-list:meta-{$entry['meta_id']}::_ajax_nonce:$delete_nonce" ) );
$r .= "\n\t\t";
$r .= get_submit_button( __( 'Update', 'woocommerce' ), 'updatemeta small', "meta-{$entry['meta_id']}-submit", false, array( 'data-wp-lists' => "add:the-list:meta-{$entry['meta_id']}::_ajax_nonce-add-meta={$this->update_nonce}" ) );
$r .= '</div>';
$r .= wp_nonce_field( 'change-meta', '_ajax_nonce', false, false );
$r .= '</td>';
$r .= "\n\t\t<td><label class='screen-reader-text' for='meta-{$entry['meta_id']}-value'>" . __( 'Value', 'woocommerce' ) . "</label><textarea name='meta[{$entry['meta_id']}][value]' id='meta-{$entry['meta_id']}-value' rows='2' cols='30'>{$entry['meta_value']}</textarea></td>\n\t</tr>";
return $r;
}
/**
* Reimplementation of WP core's `wp_ajax_delete_meta` method to support order custom meta updates with custom tables.
*
* @return void
*/
public function delete_meta_ajax() {
$meta_id = (int) $_POST['id'] ?? 0;
$order_id = (int) $_POST['order_id'] ?? 0;
if ( ! $meta_id || ! $order_id ) {
wp_send_json_error( 'invalid_meta_id' );
wp_die();
}
check_ajax_referer( "delete-meta_$meta_id" );
$order = $this->verify_order_edit_permission_for_ajax( $order_id );
$meta_to_delete = wp_list_filter( $order->get_meta_data(), array( 'id' => $meta_id ) );
if ( empty( $meta_to_delete ) ) {
wp_send_json_error( 'invalid_meta_id' );
wp_die();
}
$order->delete_meta_data_by_mid( $meta_id );
if ( $order->save() ) {
wp_die( 1 );
}
wp_die( 0 );
}
/**
* Handle the possible changes in order metadata coming from an order edit page in admin
* (labeled "custom fields" in the UI).
*
* This method expects the $_POST array to contain a 'meta' key that is an associative
* array of [meta item id => [ 'key' => meta item name, 'value' => meta item value ];
* and also to contain (possibly empty) 'metakeyinput' and 'metavalue' keys.
*
* @param WC_Order $order The order to handle.
*/
public function handle_metadata_changes( $order ) {
$has_meta_changes = false;
$order_meta = $order->get_meta_data();
$order_meta =
array_combine(
array_map( fn( $meta ) => $meta->id, $order_meta ),
$order_meta
);
// phpcs:disable WordPress.Security.ValidatedSanitizedInput, WordPress.Security.NonceVerification.Missing
foreach ( ( $_POST['meta'] ?? array() ) as $request_meta_id => $request_meta_data ) {
$request_meta_id = wp_unslash( $request_meta_id );
$request_meta_key = wp_unslash( $request_meta_data['key'] );
$request_meta_value = wp_unslash( $request_meta_data['value'] );
if ( array_key_exists( $request_meta_id, $order_meta ) &&
( $order_meta[ $request_meta_id ]->key !== $request_meta_key || $order_meta[ $request_meta_id ]->value !== $request_meta_value ) ) {
$order->update_meta_data( $request_meta_key, $request_meta_value, $request_meta_id );
$has_meta_changes = true;
}
}
$request_new_key = wp_unslash( $_POST['metakeyinput'] ?? '' );
$request_new_value = wp_unslash( $_POST['metavalue'] ?? '' );
if ( '' !== $request_new_key ) {
$order->add_meta_data( $request_new_key, $request_new_value );
$has_meta_changes = true;
}
// phpcs:enable WordPress.Security.ValidatedSanitizedInput, WordPress.Security.NonceVerification.Missing
if ( $has_meta_changes ) {
$order->save();
}
}
}
Orders/MetaBoxes/TaxonomiesMetaBox.php 0000644 00000010430 15154512352 0014011 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\Orders\MetaBoxes;
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
/**
* TaxonomiesMetaBox class, renders taxonomy sidebar widget on order edit screen.
*/
class TaxonomiesMetaBox {
/**
* Order Table data store class.
*
* @var OrdersTableDataStore
*/
private $orders_table_data_store;
/**
* Dependency injection init method.
*
* @param OrdersTableDataStore $orders_table_data_store Order Table data store class.
*
* @return void
*/
public function init( OrdersTableDataStore $orders_table_data_store ) {
$this->orders_table_data_store = $orders_table_data_store;
}
/**
* Registers meta boxes to be rendered in order edit screen for taxonomies.
*
* Note: This is re-implementation of part of WP core's `register_and_do_post_meta_boxes` function. Since the code block that add meta box for taxonomies is not filterable, we have to re-implement it.
*
* @param string $screen_id Screen ID.
* @param string $order_type Order type to register meta boxes for.
*
* @return void
*/
public function add_taxonomies_meta_boxes( string $screen_id, string $order_type ) {
include_once ABSPATH . 'wp-admin/includes/meta-boxes.php';
$taxonomies = get_object_taxonomies( $order_type );
// All taxonomies.
foreach ( $taxonomies as $tax_name ) {
$taxonomy = get_taxonomy( $tax_name );
if ( ! $taxonomy->show_ui || false === $taxonomy->meta_box_cb ) {
continue;
}
if ( 'post_categories_meta_box' === $taxonomy->meta_box_cb ) {
$taxonomy->meta_box_cb = array( $this, 'order_categories_meta_box' );
}
if ( 'post_tags_meta_box' === $taxonomy->meta_box_cb ) {
$taxonomy->meta_box_cb = array( $this, 'order_tags_meta_box' );
}
$label = $taxonomy->labels->name;
if ( ! is_taxonomy_hierarchical( $tax_name ) ) {
$tax_meta_box_id = 'tagsdiv-' . $tax_name;
} else {
$tax_meta_box_id = $tax_name . 'div';
}
add_meta_box(
$tax_meta_box_id,
$label,
$taxonomy->meta_box_cb,
$screen_id,
'side',
'core',
array(
'taxonomy' => $tax_name,
'__back_compat_meta_box' => true,
)
);
}
}
/**
* Save handler for taxonomy data.
*
* @param \WC_Abstract_Order $order Order object.
* @param array|null $taxonomy_input Taxonomy input passed from input.
*/
public function save_taxonomies( \WC_Abstract_Order $order, $taxonomy_input ) {
if ( ! isset( $taxonomy_input ) ) {
return;
}
$sanitized_tax_input = $this->sanitize_tax_input( $taxonomy_input );
$sanitized_tax_input = $this->orders_table_data_store->init_default_taxonomies( $order, $sanitized_tax_input );
$this->orders_table_data_store->set_custom_taxonomies( $order, $sanitized_tax_input );
}
/**
* Sanitize taxonomy input by calling sanitize callbacks for each registered taxonomy.
*
* @param array|null $taxonomy_data Nonce verified taxonomy input.
*
* @return array Sanitized taxonomy input.
*/
private function sanitize_tax_input( $taxonomy_data ) : array {
$sanitized_tax_input = array();
if ( ! is_array( $taxonomy_data ) ) {
return $sanitized_tax_input;
}
// Convert taxonomy input to term IDs, to avoid ambiguity.
foreach ( $taxonomy_data as $taxonomy => $terms ) {
$tax_object = get_taxonomy( $taxonomy );
if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) {
$sanitized_tax_input[ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy, $terms ) );
}
}
return $sanitized_tax_input;
}
/**
* Add the categories meta box to the order screen. This is just a wrapper around the post_categories_meta_box.
*
* @param \WC_Abstract_Order $order Order object.
* @param array $box Meta box args.
*
* @return void
*/
public function order_categories_meta_box( $order, $box ) {
$post = get_post( $order->get_id() );
post_categories_meta_box( $post, $box );
}
/**
* Add the tags meta box to the order screen. This is just a wrapper around the post_tags_meta_box.
*
* @param \WC_Abstract_Order $order Order object.
* @param array $box Meta box args.
*
* @return void
*/
public function order_tags_meta_box( $order, $box ) {
$post = get_post( $order->get_id() );
post_tags_meta_box( $post, $box );
}
}
Orders/PageController.php 0000644 00000037746 15154512352 0011457 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\Orders;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
use Automattic\WooCommerce\Internal\Traits\AccessiblePrivateMethods;
/**
* Controls the different pages/screens associated to the "Orders" menu page.
*/
class PageController {
use AccessiblePrivateMethods;
/**
* The order type.
*
* @var string
*/
private $order_type = '';
/**
* Instance of the posts redirection controller.
*
* @var PostsRedirectionController
*/
private $redirection_controller;
/**
* Instance of the orders list table.
*
* @var ListTable
*/
private $orders_table;
/**
* Instance of orders edit form.
*
* @var Edit
*/
private $order_edit_form;
/**
* Current action.
*
* @var string
*/
private $current_action = '';
/**
* Order object to be used in edit/new form.
*
* @var \WC_Order
*/
private $order;
/**
* Verify that user has permission to edit orders.
*
* @return void
*/
private function verify_edit_permission() {
if ( 'edit_order' === $this->current_action && ( ! isset( $this->order ) || ! $this->order ) ) {
wp_die( esc_html__( 'You attempted to edit an order that does not exist. Perhaps it was deleted?', 'woocommerce' ) );
}
if ( $this->order->get_type() !== $this->order_type ) {
wp_die( esc_html__( 'Order type mismatch.', 'woocommerce' ) );
}
if ( ! current_user_can( get_post_type_object( $this->order_type )->cap->edit_post, $this->order->get_id() ) && ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( esc_html__( 'You do not have permission to edit this order', 'woocommerce' ) );
}
if ( 'trash' === $this->order->get_status() ) {
wp_die( esc_html__( 'You cannot edit this item because it is in the Trash. Please restore it and try again.', 'woocommerce' ) );
}
}
/**
* Verify that user has permission to create order.
*
* @return void
*/
private function verify_create_permission() {
if ( ! current_user_can( get_post_type_object( $this->order_type )->cap->publish_posts ) && ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( esc_html__( 'You don\'t have permission to create a new order', 'woocommerce' ) );
}
if ( isset( $this->order ) ) {
$this->verify_edit_permission();
}
}
/**
* Claims the lock for the order being edited/created (unless it belongs to someone else).
* Also handles the 'claim-lock' action which allows taking over the order forcefully.
*
* @return void
*/
private function handle_edit_lock() {
if ( ! $this->order ) {
return;
}
$edit_lock = wc_get_container()->get( EditLock::class );
$locked = $edit_lock->is_locked_by_another_user( $this->order );
// Take over order?
if ( ! empty( $_GET['claim-lock'] ) && wp_verify_nonce( $_GET['_wpnonce'] ?? '', 'claim-lock-' . $this->order->get_id() ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.ValidatedSanitizedInput.MissingUnslash
$edit_lock->lock( $this->order );
wp_safe_redirect( $this->get_edit_url( $this->order->get_id() ) );
exit;
}
if ( ! $locked ) {
$edit_lock->lock( $this->order );
}
add_action(
'admin_footer',
function() use ( $edit_lock ) {
$edit_lock->render_dialog( $this->order );
}
);
}
/**
* Sets up the page controller, including registering the menu item.
*
* @return void
*/
public function setup(): void {
global $plugin_page, $pagenow;
$this->redirection_controller = new PostsRedirectionController( $this );
// Register menu.
if ( 'admin_menu' === current_action() ) {
$this->register_menu();
} else {
add_action( 'admin_menu', 'register_menu', 9 );
}
// Not on an Orders page.
if ( 'admin.php' !== $pagenow || 0 !== strpos( $plugin_page, 'wc-orders' ) ) {
return;
}
$this->set_order_type();
$this->set_action();
$page_suffix = ( 'shop_order' === $this->order_type ? '' : '--' . $this->order_type );
self::add_action( 'load-woocommerce_page_wc-orders' . $page_suffix, array( $this, 'handle_load_page_action' ) );
self::add_action( 'admin_title', array( $this, 'set_page_title' ) );
}
/**
* Perform initialization for the current action.
*/
private function handle_load_page_action() {
$screen = get_current_screen();
$screen->post_type = $this->order_type;
if ( method_exists( $this, 'setup_action_' . $this->current_action ) ) {
$this->{"setup_action_{$this->current_action}"}();
}
}
/**
* Set the document title for Orders screens to match what it would be with the shop_order CPT.
*
* @param string $admin_title The admin screen title before it's filtered.
*
* @return string The filtered admin title.
*/
private function set_page_title( $admin_title ) {
if ( ! $this->is_order_screen( $this->order_type ) ) {
return $admin_title;
}
$wp_order_type = get_post_type_object( $this->order_type );
$labels = get_post_type_labels( $wp_order_type );
if ( $this->is_order_screen( $this->order_type, 'list' ) ) {
$admin_title = sprintf(
// translators: 1: The label for an order type 2: The name of the website.
esc_html__( '%1$s ‹ %2$s — WordPress', 'woocommerce' ),
esc_html( $labels->name ),
esc_html( get_bloginfo( 'name' ) )
);
} elseif ( $this->is_order_screen( $this->order_type, 'edit' ) ) {
$admin_title = sprintf(
// translators: 1: The label for an order type 2: The title of the order 3: The name of the website.
esc_html__( '%1$s #%2$s ‹ %3$s — WordPress', 'woocommerce' ),
esc_html( $labels->edit_item ),
absint( $this->order->get_id() ),
esc_html( get_bloginfo( 'name' ) )
);
} elseif ( $this->is_order_screen( $this->order_type, 'new' ) ) {
$admin_title = sprintf(
// translators: 1: The label for an order type 2: The name of the website.
esc_html__( '%1$s ‹ %2$s — WordPress', 'woocommerce' ),
esc_html( $labels->add_new_item ),
esc_html( get_bloginfo( 'name' ) )
);
}
return $admin_title;
}
/**
* Determines the order type for the current screen.
*
* @return void
*/
private function set_order_type() {
global $plugin_page;
$this->order_type = str_replace( array( 'wc-orders--', 'wc-orders' ), '', $plugin_page );
$this->order_type = empty( $this->order_type ) ? 'shop_order' : $this->order_type;
$wc_order_type = wc_get_order_type( $this->order_type );
$wp_order_type = get_post_type_object( $this->order_type );
if ( ! $wc_order_type || ! $wp_order_type || ! $wp_order_type->show_ui || ! current_user_can( $wp_order_type->cap->edit_posts ) ) {
wp_die();
}
}
/**
* Sets the current action based on querystring arguments. Defaults to 'list_orders'.
*
* @return void
*/
private function set_action(): void {
switch ( isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '' ) {
case 'edit':
$this->current_action = 'edit_order';
break;
case 'new':
$this->current_action = 'new_order';
break;
default:
$this->current_action = 'list_orders';
break;
}
}
/**
* Registers the "Orders" menu.
*
* @return void
*/
public function register_menu(): void {
$order_types = wc_get_order_types( 'admin-menu' );
foreach ( $order_types as $order_type ) {
$post_type = get_post_type_object( $order_type );
add_submenu_page(
'woocommerce',
$post_type->labels->name,
$post_type->labels->menu_name,
$post_type->cap->edit_posts,
'wc-orders' . ( 'shop_order' === $order_type ? '' : '--' . $order_type ),
array( $this, 'output' )
);
}
// In some cases (such as if the authoritative order store was changed earlier in the current request) we
// need an extra step to remove the menu entry for the menu post type.
add_action(
'admin_init',
function() use ( $order_types ) {
foreach ( $order_types as $order_type ) {
remove_submenu_page( 'woocommerce', 'edit.php?post_type=' . $order_type );
}
}
);
}
/**
* Outputs content for the current orders screen.
*
* @return void
*/
public function output(): void {
switch ( $this->current_action ) {
case 'edit_order':
case 'new_order':
$this->order_edit_form->display();
break;
case 'list_orders':
default:
$this->orders_table->prepare_items();
$this->orders_table->display();
break;
}
}
/**
* Handles initialization of the orders list table.
*
* @return void
*/
private function setup_action_list_orders(): void {
$this->orders_table = wc_get_container()->get( ListTable::class );
$this->orders_table->setup(
array(
'order_type' => $this->order_type,
)
);
if ( $this->orders_table->current_action() ) {
$this->orders_table->handle_bulk_actions();
}
$this->strip_http_referer();
}
/**
* Perform a redirect to remove the `_wp_http_referer` and `_wpnonce` strings if present in the URL (see also
* wp-admin/edit.php where a similar process takes place), otherwise the size of this field builds to an
* unmanageable length over time.
*/
private function strip_http_referer(): void {
$current_url = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) );
$stripped_url = remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), $current_url );
if ( $stripped_url !== $current_url ) {
wp_safe_redirect( $stripped_url );
exit;
}
}
/**
* Prepares the order edit form for creating or editing an order.
*
* @see \Automattic\WooCommerce\Internal\Admin\Orders\Edit.
* @since 8.1.0
*/
private function prepare_order_edit_form(): void {
if ( ! $this->order || ! in_array( $this->current_action, array( 'new_order', 'edit_order' ), true ) ) {
return;
}
$this->order_edit_form = $this->order_edit_form ?? new Edit();
$this->order_edit_form->setup( $this->order );
$this->order_edit_form->set_current_action( $this->current_action );
}
/**
* Handles initialization of the orders edit form.
*
* @return void
*/
private function setup_action_edit_order(): void {
global $theorder;
$this->order = wc_get_order( absint( isset( $_GET['id'] ) ? $_GET['id'] : 0 ) );
$this->verify_edit_permission();
$this->handle_edit_lock();
$theorder = $this->order;
$this->prepare_order_edit_form();
}
/**
* Handles initialization of the orders edit form with a new order.
*
* @return void
*/
private function setup_action_new_order(): void {
global $theorder;
$this->verify_create_permission();
$order_class_name = wc_get_order_type( $this->order_type )['class_name'];
if ( ! $order_class_name || ! class_exists( $order_class_name ) ) {
wp_die();
}
$this->order = new $order_class_name();
$this->order->set_object_read( false );
$this->order->set_status( 'auto-draft' );
$this->order->set_created_via( 'admin' );
$this->order->save();
$this->handle_edit_lock();
// Schedule auto-draft cleanup. We re-use the WP event here on purpose.
if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) {
wp_schedule_event( time(), 'daily', 'wp_scheduled_auto_draft_delete' );
}
$theorder = $this->order;
$this->prepare_order_edit_form();
}
/**
* Returns the current order type.
*
* @return string
*/
public function get_order_type() {
return $this->order_type;
}
/**
* Helper method to generate a link to the main orders screen.
*
* @return string Orders screen URL.
*/
public function get_orders_url(): string {
return wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ?
admin_url( 'admin.php?page=wc-orders' ) :
admin_url( 'edit.php?post_type=shop_order' );
}
/**
* Helper method to generate edit link for an order.
*
* @param int $order_id Order ID.
*
* @return string Edit link.
*/
public function get_edit_url( int $order_id ) : string {
if ( ! wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ) {
return admin_url( 'post.php?post=' . absint( $order_id ) ) . '&action=edit';
}
$order = wc_get_order( $order_id );
// Confirm we could obtain the order object (since it's possible it will not exist, due to a sync issue, or may
// have been deleted in a separate concurrent request).
if ( false === $order ) {
wc_get_logger()->debug(
sprintf(
/* translators: %d order ID. */
__( 'Attempted to determine the edit URL for order %d, however the order does not exist.', 'woocommerce' ),
$order_id
)
);
$order_type = 'shop_order';
} else {
$order_type = $order->get_type();
}
return add_query_arg(
array(
'action' => 'edit',
'id' => absint( $order_id ),
),
$this->get_base_page_url( $order_type )
);
}
/**
* Helper method to generate a link for creating order.
*
* @param string $order_type The order type. Defaults to 'shop_order'.
* @return string
*/
public function get_new_page_url( $order_type = 'shop_order' ) : string {
$url = wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ?
add_query_arg( 'action', 'new', $this->get_base_page_url( $order_type ) ) :
admin_url( 'post-new.php?post_type=' . $order_type );
return $url;
}
/**
* Helper method to generate a link to the main screen for a custom order type.
*
* @param string $order_type The order type.
*
* @return string
*
* @throws \Exception When an invalid order type is passed.
*/
public function get_base_page_url( $order_type ): string {
$order_types_with_ui = wc_get_order_types( 'admin-menu' );
if ( ! in_array( $order_type, $order_types_with_ui, true ) ) {
// translators: %s is a custom order type.
throw new \Exception( sprintf( __( 'Invalid order type: %s.', 'woocommerce' ), esc_html( $order_type ) ) );
}
return admin_url( 'admin.php?page=wc-orders' . ( 'shop_order' === $order_type ? '' : '--' . $order_type ) );
}
/**
* Helper method to check if the current admin screen is related to orders.
*
* @param string $type Optional. The order type to check for. Default shop_order.
* @param string $action Optional. The purpose of the screen to check for. 'list', 'edit', or 'new'.
* Leave empty to check for any order screen.
*
* @return bool
*/
public function is_order_screen( $type = 'shop_order', $action = '' ) : bool {
if ( ! did_action( 'current_screen' ) ) {
wc_doing_it_wrong(
__METHOD__,
sprintf(
// translators: %s is the name of a function.
esc_html__( '%s must be called after the current_screen action.', 'woocommerce' ),
esc_html( __METHOD__ )
),
'7.9.0'
);
return false;
}
$valid_types = wc_get_order_types( 'view-order' );
if ( ! in_array( $type, $valid_types, true ) ) {
wc_doing_it_wrong(
__METHOD__,
sprintf(
// translators: %s is the name of an order type.
esc_html__( '%s is not a valid order type.', 'woocommerce' ),
esc_html( $type )
),
'7.9.0'
);
return false;
}
if ( wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ) {
if ( $action ) {
switch ( $action ) {
case 'edit':
$is_action = 'edit_order' === $this->current_action;
break;
case 'list':
$is_action = 'list_orders' === $this->current_action;
break;
case 'new':
$is_action = 'new_order' === $this->current_action;
break;
default:
$is_action = false;
break;
}
}
$type_match = $type === $this->order_type;
$action_match = ! $action || $is_action;
} else {
$screen = get_current_screen();
if ( $action ) {
switch ( $action ) {
case 'edit':
$screen_match = 'post' === $screen->base && filter_input( INPUT_GET, 'post', FILTER_VALIDATE_INT );
break;
case 'list':
$screen_match = 'edit' === $screen->base;
break;
case 'new':
$screen_match = 'post' === $screen->base && 'add' === $screen->action;
break;
default:
$screen_match = false;
break;
}
}
$type_match = $type === $screen->post_type;
$action_match = ! $action || $screen_match;
}
return $type_match && $action_match;
}
}
Orders/PostsRedirectionController.php 0000644 00000011537 15154512352 0014071 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\Orders;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
/**
* When {@see OrdersTableDataStore} is in use, this class takes care of redirecting admins from CPT-based URLs
* to the new ones.
*/
class PostsRedirectionController {
/**
* Instance of the PageController class.
*
* @var PageController
*/
private $page_controller;
/**
* Constructor.
*
* @param PageController $page_controller Page controller instance. Used to generate links/URLs.
*/
public function __construct( PageController $page_controller ) {
$this->page_controller = $page_controller;
if ( ! wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ) {
return;
}
add_action(
'load-edit.php',
function() {
$this->maybe_redirect_to_orders_page();
}
);
add_action(
'load-post-new.php',
function() {
$this->maybe_redirect_to_new_order_page();
}
);
add_action(
'load-post.php',
function() {
$this->maybe_redirect_to_edit_order_page();
}
);
}
/**
* If needed, performs a redirection to the main orders page.
*
* @return void
*/
private function maybe_redirect_to_orders_page(): void {
$post_type = $_GET['post_type'] ?? ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! $post_type || ! in_array( $post_type, wc_get_order_types( 'admin-menu' ), true ) ) {
return;
}
// Respect query args, except for 'post_type'.
$query_args = wp_unslash( $_GET );
$action = $query_args['action'] ?? '';
$posts = $query_args['post'] ?? array();
unset( $query_args['post_type'], $query_args['post'], $query_args['_wpnonce'], $query_args['_wp_http_referer'], $query_args['action'] );
// Remap 'post_status' arg.
if ( isset( $query_args['post_status'] ) ) {
$query_args['status'] = $query_args['post_status'];
unset( $query_args['post_status'] );
}
$new_url = $this->page_controller->get_base_page_url( $post_type );
$new_url = add_query_arg( $query_args, $new_url );
// Handle bulk actions.
if ( $action && in_array( $action, array( 'trash', 'untrash', 'delete', 'mark_processing', 'mark_on-hold', 'mark_completed', 'mark_cancelled' ), true ) ) {
check_admin_referer( 'bulk-posts' );
$new_url = add_query_arg(
array(
'action' => $action,
'id' => $posts,
'_wp_http_referer' => $this->page_controller->get_orders_url(),
'_wpnonce' => wp_create_nonce( 'bulk-orders' ),
),
$new_url
);
}
wp_safe_redirect( $new_url, 301 );
exit;
}
/**
* If needed, performs a redirection to the new order page.
*
* @return void
*/
private function maybe_redirect_to_new_order_page(): void {
$post_type = $_GET['post_type'] ?? ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! $post_type || ! in_array( $post_type, wc_get_order_types( 'admin-menu' ), true ) ) {
return;
}
// Respect query args, except for 'post_type'.
$query_args = wp_unslash( $_GET );
unset( $query_args['post_type'] );
$new_url = $this->page_controller->get_new_page_url( $post_type );
$new_url = add_query_arg( $query_args, $new_url );
wp_safe_redirect( $new_url, 301 );
exit;
}
/**
* If needed, performs a redirection to the edit order page.
*
* @return void
*/
private function maybe_redirect_to_edit_order_page(): void {
$post_id = absint( $_GET['post'] ?? 0 );
$redirect_from_types = wc_get_order_types( 'admin-menu' );
$redirect_from_types[] = 'shop_order_placehold';
if ( ! $post_id || ! in_array( get_post_type( $post_id ), $redirect_from_types, true ) || ! isset( $_GET['action'] ) ) {
return;
}
// Respect query args, except for 'post'.
$query_args = wp_unslash( $_GET );
$action = $query_args['action'];
unset( $query_args['post'], $query_args['_wpnonce'], $query_args['_wp_http_referer'], $query_args['action'] );
$new_url = '';
switch ( $action ) {
case 'edit':
$new_url = $this->page_controller->get_edit_url( $post_id );
break;
case 'trash':
case 'untrash':
case 'delete':
// Re-generate nonce if validation passes.
check_admin_referer( $action . '-post_' . $post_id );
$new_url = add_query_arg(
array(
'action' => $action,
'order' => array( $post_id ),
'_wp_http_referer' => $this->page_controller->get_orders_url(),
'_wpnonce' => wp_create_nonce( 'bulk-orders' ),
),
$this->page_controller->get_orders_url()
);
break;
default:
break;
}
if ( ! $new_url ) {
return;
}
$new_url = add_query_arg( $query_args, $new_url );
wp_safe_redirect( $new_url, 301 );
exit;
}
}
ProductForm/Component.php 0000644 00000005547 15154512352 0011501 0 ustar 00 <?php
/**
* Abstract class for product form components.
*/
namespace Automattic\WooCommerce\Internal\Admin\ProductForm;
/**
* Component class.
*/
abstract class Component {
/**
* Product Component traits.
*/
use ComponentTrait;
/**
* Component additional arguments.
*
* @var array
*/
protected $additional_args;
/**
* Constructor
*
* @param string $id Component id.
* @param string $plugin_id Plugin id.
* @param array $additional_args Array containing additional arguments.
*/
public function __construct( $id, $plugin_id, $additional_args ) {
$this->id = $id;
$this->plugin_id = $plugin_id;
$this->additional_args = $additional_args;
}
/**
* Component arguments.
*
* @return array
*/
public function get_additional_args() {
return $this->additional_args;
}
/**
* Component arguments.
*
* @param string $key key of argument.
* @return mixed
*/
public function get_additional_argument( $key ) {
return self::get_argument_from_path( $this->additional_args, $key );
}
/**
* Get the component as JSON.
*
* @return array
*/
public function get_json() {
return array_merge(
array(
'id' => $this->get_id(),
'plugin_id' => $this->get_plugin_id(),
),
$this->get_additional_args()
);
}
/**
* Sorting function for product form component.
*
* @param Component $a Component a.
* @param Component $b Component b.
* @param array $sort_by key and order to sort by.
* @return int
*/
public static function sort( $a, $b, $sort_by = array() ) {
$key = $sort_by['key'];
$a_val = $a->get_additional_argument( $key );
$b_val = $b->get_additional_argument( $key );
if ( 'asc' === $sort_by['order'] ) {
return $a_val <=> $b_val;
} else {
return $b_val <=> $a_val;
}
}
/**
* Gets argument by dot notation path.
*
* @param array $arguments Arguments array.
* @param string $path Path for argument key.
* @param string $delimiter Path delimiter, default: '.'.
* @return mixed|null
*/
public static function get_argument_from_path( $arguments, $path, $delimiter = '.' ) {
$path_keys = explode( $delimiter, $path );
$num_keys = count( $path_keys );
$val = $arguments;
for ( $i = 0; $i < $num_keys; $i++ ) {
$key = $path_keys[ $i ];
if ( array_key_exists( $key, $val ) ) {
$val = $val[ $key ];
} else {
$val = null;
break;
}
}
return $val;
}
/**
* Array of required arguments.
*
* @var array
*/
protected $required_arguments = array();
/**
* Get missing arguments of args array.
*
* @param array $args field arguments.
* @return array
*/
public function get_missing_arguments( $args ) {
return array_values(
array_filter(
$this->required_arguments,
function( $arg_key ) use ( $args ) {
return null === self::get_argument_from_path( $args, $arg_key );
}
)
);
}
}
ProductForm/ComponentTrait.php 0000644 00000001315 15154512352 0012472 0 ustar 00 <?php
/**
* Product Form Traits
*/
namespace Automattic\WooCommerce\Internal\Admin\ProductForm;
defined( 'ABSPATH' ) || exit;
/**
* ComponentTrait class.
*/
trait ComponentTrait {
/**
* Component ID.
*
* @var string
*/
protected $id;
/**
* Plugin ID.
*
* @var string
*/
protected $plugin_id;
/**
* Product form component location.
*
* @var string
*/
protected $location;
/**
* Product form component order.
*
* @var number
*/
protected $order;
/**
* Return id.
*
* @return string
*/
public function get_id() {
return $this->id;
}
/**
* Return plugin id.
*
* @return string
*/
public function get_plugin_id() {
return $this->plugin_id;
}
}
ProductForm/Field.php 0000644 00000002422 15154512352 0010547 0 ustar 00 <?php
/**
* Handles product form field related methods.
*/
namespace Automattic\WooCommerce\Internal\Admin\ProductForm;
/**
* Field class.
*/
class Field extends Component {
/**
* Constructor
*
* @param string $id Field id.
* @param string $plugin_id Plugin id.
* @param array $additional_args Array containing the necessary arguments.
* $args = array(
* 'type' => (string) Field type. Required.
* 'section' => (string) Field location. Required.
* 'order' => (int) Field order.
* 'properties' => (array) Field properties.
* ).
* @throws \Exception If there are missing arguments.
*/
public function __construct( $id, $plugin_id, $additional_args ) {
parent::__construct( $id, $plugin_id, $additional_args );
$this->required_arguments = array(
'type',
'section',
'properties.name',
'properties.label',
);
$missing_arguments = self::get_missing_arguments( $additional_args );
if ( count( $missing_arguments ) > 0 ) {
throw new \Exception(
sprintf(
/* translators: 1: Missing arguments list. */
esc_html__( 'You are missing required arguments of WooCommerce ProductForm Field: %1$s', 'woocommerce' ),
join( ', ', $missing_arguments )
)
);
}
}
}
ProductForm/FormFactory.php 0000644 00000016476 15154512352 0011775 0 ustar 00 <?php
/**
* WooCommerce Product Form Factory
*
* @package Woocommerce ProductForm
*/
namespace Automattic\WooCommerce\Internal\Admin\ProductForm;
use WP_Error;
/**
* Factory that contains logic for the WooCommerce Product Form.
*/
class FormFactory {
/**
* Class instance.
*
* @var Form instance
*/
protected static $instance = null;
/**
* Store form fields.
*
* @var array
*/
protected static $form_fields = array();
/**
* Store form cards.
*
* @var array
*/
protected static $form_subsections = array();
/**
* Store form sections.
*
* @var array
*/
protected static $form_sections = array();
/**
* Store form tabs.
*
* @var array
*/
protected static $form_tabs = array();
/**
* Get class instance.
*/
final public static function instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init.
*/
public function init() { }
/**
* Adds a field to the product form.
*
* @param string $id Field id.
* @param string $plugin_id Plugin id.
* @param array $args Array containing the necessary arguments.
* $args = array(
* 'type' => (string) Field type. Required.
* 'section' => (string) Field location. Required.
* 'order' => (int) Field order.
* 'properties' => (array) Field properties.
* 'name' => (string) Field name.
* ).
* @return Field|WP_Error New field or WP_Error.
*/
public static function add_field( $id, $plugin_id, $args ) {
$new_field = self::create_item( 'field', 'Field', $id, $plugin_id, $args );
if ( is_wp_error( $new_field ) ) {
return $new_field;
}
self::$form_fields[ $id ] = $new_field;
return $new_field;
}
/**
* Adds a Subsection to the product form.
*
* @param string $id Subsection id.
* @param string $plugin_id Plugin id.
* @param array $args Array containing the necessary arguments.
* @return Subsection|WP_Error New subsection or WP_Error.
*/
public static function add_subsection( $id, $plugin_id, $args = array() ) {
$new_subsection = self::create_item( 'subsection', 'Subsection', $id, $plugin_id, $args );
if ( is_wp_error( $new_subsection ) ) {
return $new_subsection;
}
self::$form_subsections[ $id ] = $new_subsection;
return $new_subsection;
}
/**
* Adds a section to the product form.
*
* @param string $id Card id.
* @param string $plugin_id Plugin id.
* @param array $args Array containing the necessary arguments.
* @return Section|WP_Error New section or WP_Error.
*/
public static function add_section( $id, $plugin_id, $args ) {
$new_section = self::create_item( 'section', 'Section', $id, $plugin_id, $args );
if ( is_wp_error( $new_section ) ) {
return $new_section;
}
self::$form_sections[ $id ] = $new_section;
return $new_section;
}
/**
* Adds a tab to the product form.
*
* @param string $id Card id.
* @param string $plugin_id Plugin id.
* @param array $args Array containing the necessary arguments.
* @return Tab|WP_Error New section or WP_Error.
*/
public static function add_tab( $id, $plugin_id, $args ) {
$new_tab = self::create_item( 'tab', 'Tab', $id, $plugin_id, $args );
if ( is_wp_error( $new_tab ) ) {
return $new_tab;
}
self::$form_tabs[ $id ] = $new_tab;
return $new_tab;
}
/**
* Returns list of registered fields.
*
* @param array $sort_by key and order to sort by.
* @return array list of registered fields.
*/
public static function get_fields( $sort_by = array(
'key' => 'order',
'order' => 'asc',
) ) {
return self::get_items( 'field', 'Field', $sort_by );
}
/**
* Returns list of registered cards.
*
* @param array $sort_by key and order to sort by.
* @return array list of registered cards.
*/
public static function get_subsections( $sort_by = array(
'key' => 'order',
'order' => 'asc',
) ) {
return self::get_items( 'subsection', 'Subsection', $sort_by );
}
/**
* Returns list of registered sections.
*
* @param array $sort_by key and order to sort by.
* @return array list of registered sections.
*/
public static function get_sections( $sort_by = array(
'key' => 'order',
'order' => 'asc',
) ) {
return self::get_items( 'section', 'Section', $sort_by );
}
/**
* Returns list of registered tabs.
*
* @param array $sort_by key and order to sort by.
* @return array list of registered tabs.
*/
public static function get_tabs( $sort_by = array(
'key' => 'order',
'order' => 'asc',
) ) {
return self::get_items( 'tab', 'Tab', $sort_by );
}
/**
* Returns list of registered items.
*
* @param string $type Form component type.
* @return array List of registered items.
*/
private static function get_item_list( $type ) {
$mapping = array(
'field' => self::$form_fields,
'subsection' => self::$form_subsections,
'section' => self::$form_sections,
'tab' => self::$form_tabs,
);
if ( array_key_exists( $type, $mapping ) ) {
return $mapping[ $type ];
}
return array();
}
/**
* Returns list of registered items.
*
* @param string $type Form component type.
* @param class-string $class_name Class of component type.
* @param array $sort_by key and order to sort by.
* @return array list of registered items.
*/
private static function get_items( $type, $class_name, $sort_by = array(
'key' => 'order',
'order' => 'asc',
) ) {
$item_list = self::get_item_list( $type );
$class = 'Automattic\\WooCommerce\\Internal\\Admin\\ProductForm\\' . $class_name;
$items = array_values( $item_list );
if ( class_exists( $class ) && method_exists( $class, 'sort' ) ) {
usort(
$items,
function ( $a, $b ) use ( $sort_by, $class ) {
return $class::sort( $a, $b, $sort_by );
}
);
}
return $items;
}
/**
* Creates a new item.
*
* @param string $type Form component type.
* @param class-string $class_name Class of component type.
* @param string $id Item id.
* @param string $plugin_id Plugin id.
* @param array $args additional arguments for item.
* @return Field|Card|Section|Tab|WP_Error New product form item or WP_Error.
*/
private static function create_item( $type, $class_name, $id, $plugin_id, $args ) {
$item_list = self::get_item_list( $type );
$class = 'Automattic\\WooCommerce\\Internal\\Admin\\ProductForm\\' . $class_name;
if ( ! class_exists( $class ) ) {
return new WP_Error(
'wc_product_form_' . $type . '_missing_form_class',
sprintf(
/* translators: 1: missing class name. */
esc_html__( '%1$s class does not exist.', 'woocommerce' ),
$class
)
);
}
if ( isset( $item_list[ $id ] ) ) {
return new WP_Error(
'wc_product_form_' . $type . '_duplicate_field_id',
sprintf(
/* translators: 1: Item type 2: Duplicate registered item id. */
esc_html__( 'You have attempted to register a duplicate form %1$s with WooCommerce Form: %2$s', 'woocommerce' ),
$type,
'`' . $id . '`'
)
);
}
$defaults = array(
'order' => 20,
);
$item_arguments = wp_parse_args( $args, $defaults );
try {
return new $class( $id, $plugin_id, $item_arguments );
} catch ( \Exception $e ) {
return new WP_Error(
'wc_product_form_' . $type . '_class_creation',
$e->getMessage()
);
}
}
}
ProductForm/Section.php 0000644 00000002234 15154512352 0011131 0 ustar 00 <?php
/**
* Handles product form section related methods.
*/
namespace Automattic\WooCommerce\Internal\Admin\ProductForm;
/**
* Section class.
*/
class Section extends Component {
/**
* Constructor
*
* @param string $id Section id.
* @param string $plugin_id Plugin id.
* @param array $additional_args Array containing additional arguments.
* $args = array(
* 'order' => (int) Section order.
* 'title' => (string) Section description.
* 'description' => (string) Section description.
* ).
* @throws \Exception If there are missing arguments.
*/
public function __construct( $id, $plugin_id, $additional_args ) {
parent::__construct( $id, $plugin_id, $additional_args );
$this->required_arguments = array(
'title',
);
$missing_arguments = self::get_missing_arguments( $additional_args );
if ( count( $missing_arguments ) > 0 ) {
throw new \Exception(
sprintf(
/* translators: 1: Missing arguments list. */
esc_html__( 'You are missing required arguments of WooCommerce ProductForm Section: %1$s', 'woocommerce' ),
join( ', ', $missing_arguments )
)
);
}
}
}
ProductForm/Subsection.php 0000644 00000000304 15154512352 0011637 0 ustar 00 <?php
/**
* Handles product form SubSection related methods.
*/
namespace Automattic\WooCommerce\Internal\Admin\ProductForm;
/**
* SubSection class.
*/
class Subsection extends Component {}
ProductForm/Tab.php 0000644 00000002322 15154512352 0010231 0 ustar 00 <?php
/**
* Handles product form tab related methods.
*/
namespace Automattic\WooCommerce\Internal\Admin\ProductForm;
/**
* Field class.
*/
class Tab extends Component {
/**
* Constructor
*
* @param string $id Field id.
* @param string $plugin_id Plugin id.
* @param array $additional_args Array containing the necessary arguments.
* $args = array(
* 'name' => (string) Tab name. Required.
* 'title' => (string) Tab title. Required.
* 'order' => (int) Tab order.
* 'properties' => (array) Tab properties.
* ).
* @throws \Exception If there are missing arguments.
*/
public function __construct( $id, $plugin_id, $additional_args ) {
parent::__construct( $id, $plugin_id, $additional_args );
$this->required_arguments = array(
'name',
'title',
);
$missing_arguments = self::get_missing_arguments( $additional_args );
if ( count( $missing_arguments ) > 0 ) {
throw new \Exception(
sprintf(
/* translators: 1: Missing arguments list. */
esc_html__( 'You are missing required arguments of WooCommerce ProductForm Tab: %1$s', 'woocommerce' ),
join( ', ', $missing_arguments )
)
);
}
}
}
ProductReviews/Reviews.php 0000644 00000052471 15154512352 0011702 0 ustar 00 <?php
/**
* Products > Reviews
*/
namespace Automattic\WooCommerce\Internal\Admin\ProductReviews;
use Automattic\WooCommerce\Internal\Traits\AccessiblePrivateMethods;
use WP_Ajax_Response;
use WP_Comment;
use WP_Screen;
/**
* Handles backend logic for the Reviews component.
*/
class Reviews {
use AccessiblePrivateMethods;
/**
* Admin page identifier.
*/
const MENU_SLUG = 'product-reviews';
/**
* Reviews page hook name.
*
* @var string|null
*/
protected $reviews_page_hook = null;
/**
* Reviews list table instance.
*
* @var ReviewsListTable|null
*/
protected $reviews_list_table;
/**
* Constructor.
*/
public function __construct() {
self::add_action( 'admin_menu', [ $this, 'add_reviews_page' ] );
self::add_action( 'admin_enqueue_scripts', [ $this, 'load_javascript' ] );
// These ajax callbacks need a low priority to ensure they run before their WordPress core counterparts.
self::add_action( 'wp_ajax_edit-comment', [ $this, 'handle_edit_review' ], -1 );
self::add_action( 'wp_ajax_replyto-comment', [ $this, 'handle_reply_to_review' ], -1 );
self::add_filter( 'parent_file', [ $this, 'edit_review_parent_file' ] );
self::add_filter( 'gettext', [ $this, 'edit_comments_screen_text' ], 10, 2 );
self::add_action( 'admin_notices', [ $this, 'display_notices' ] );
}
/**
* Gets the required capability to access the reviews page and manage product reviews.
*
* @param string $context The context for which the capability is needed (e.g. `view` or `moderate`).
* @return string
*/
public static function get_capability( string $context = 'view' ) : string {
/**
* Filters whether the current user can manage product reviews.
*
* This is aligned to {@see \wc_rest_check_product_reviews_permissions()}
*
* @since 6.7.0
*
* @param string $capability The capability (defaults to `moderate_comments` for viewing and `edit_products` for editing).
* @param string $context The context for which the capability is needed.
*/
return (string) apply_filters( 'woocommerce_product_reviews_page_capability', $context === 'view' ? 'moderate_comments' : 'edit_products', $context );
}
/**
* Registers the Product Reviews submenu page.
*
* @return void
*/
private function add_reviews_page() : void {
$this->reviews_page_hook = add_submenu_page(
'edit.php?post_type=product',
__( 'Reviews', 'woocommerce' ),
__( 'Reviews', 'woocommerce' ) . $this->get_pending_count_bubble(),
static::get_capability(),
static::MENU_SLUG,
[ $this, 'render_reviews_list_table' ]
);
self::add_action( "load-{$this->reviews_page_hook}", array( $this, 'load_reviews_screen' ) );
}
/**
* Retrieves the URL to the product reviews page.
*
* @return string
*/
public static function get_reviews_page_url() : string {
return add_query_arg(
[
'post_type' => 'product',
'page' => static::MENU_SLUG,
],
admin_url( 'edit.php' )
);
}
/**
* Determines whether the current page is the reviews page.
*
* @global WP_Screen $current_screen
*
* @return bool
*/
public function is_reviews_page() : bool {
global $current_screen;
return isset( $current_screen->base ) && $current_screen->base === 'product_page_' . static::MENU_SLUG;
}
/**
* Loads the JavaScript required for inline replies and quick edit.
*
* @return void
*/
private function load_javascript() : void {
if ( $this->is_reviews_page() ) {
wp_enqueue_script( 'admin-comments' );
enqueue_comment_hotkeys_js();
}
}
/**
* Determines if the object is a review or a reply to a review.
*
* @param WP_Comment|mixed $object Object to check.
* @return bool
*/
protected function is_review_or_reply( $object ) : bool {
$is_review_or_reply = $object instanceof WP_Comment && in_array( $object->comment_type, [ 'review', 'comment' ], true ) && get_post_type( $object->comment_post_ID ) === 'product';
/**
* Filters whether the object is a review or a reply to a review.
*
* @since 6.7.0
*
* @param bool $is_review_or_reply Whether the object in context is a review or a reply to a review.
* @param WP_Comment|mixed $object The object in context.
*/
return (bool) apply_filters( 'woocommerce_product_reviews_is_product_review_or_reply', $is_review_or_reply, $object );
}
/**
* Ajax callback for editing a review.
*
* This functionality is taken from {@see wp_ajax_edit_comment()} and is largely copy and pasted. The only thing
* we want to change is the review row HTML in the response. WordPress core uses a comment list table and we need
* to use our own {@see ReviewsListTable} class to support our custom columns.
*
* This ajax callback is registered with a lower priority than WordPress core's so that our code can run
* first. If the supplied comment ID is not a review or a reply to a review, then we `return` early from this method
* to allow the WordPress core callback to take over.
*
* @return void
*/
private function handle_edit_review(): void {
// Don't interfere with comment functionality relating to the reviews meta box within the product editor.
if ( sanitize_text_field( wp_unslash( $_POST['mode'] ?? '' ) ) === 'single' ) {
return;
}
check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' );
$comment_id = isset( $_POST['comment_ID'] ) ? (int) sanitize_text_field( wp_unslash( $_POST['comment_ID'] ) ) : 0;
if ( empty( $comment_id ) || ! current_user_can( 'edit_comment', $comment_id ) ) {
wp_die( -1 );
}
$review = get_comment( $comment_id );
// Bail silently if this is not a review, or a reply to a review. That allows `wp_ajax_edit_comment()` to handle any further actions.
if ( ! $this->is_review_or_reply( $review ) ) {
return;
}
if ( empty( $review->comment_ID ) ) {
wp_die( -1 );
}
if ( empty( $_POST['content'] ) ) {
wp_die( esc_html__( 'Error: Please type your review text.', 'woocommerce' ) );
}
if ( isset( $_POST['status'] ) ) {
$_POST['comment_status'] = sanitize_text_field( wp_unslash( $_POST['status'] ) );
}
$updated = edit_comment();
if ( is_wp_error( $updated ) ) {
wp_die( esc_html( $updated->get_error_message() ) );
}
$position = isset( $_POST['position'] ) ? (int) sanitize_text_field( wp_unslash( $_POST['position'] ) ) : -1;
$wp_list_table = $this->make_reviews_list_table();
ob_start();
$wp_list_table->single_row( $review );
$review_list_item = ob_get_clean();
$x = new WP_Ajax_Response();
$x->add(
array(
'what' => 'edit_comment',
'id' => $review->comment_ID,
'data' => $review_list_item,
'position' => $position,
)
);
$x->send();
}
/**
* Ajax callback for replying to a review inline.
*
* This functionality is taken from {@see wp_ajax_replyto_comment()} and is largely copy and pasted. The only thing
* we want to change is the review row HTML in the response. WordPress core uses a comment list table and we need
* to use our own {@see ReviewsListTable} class to support our custom columns.
*
* This ajax callback is registered with a lower priority than WordPress core's so that our code can run
* first. If the supplied comment ID is not a review or a reply to a review, then we `return` early from this method
* to allow the WordPress core callback to take over.
*
* @return void
*/
private function handle_reply_to_review() : void {
// Don't interfere with comment functionality relating to the reviews meta box within the product editor.
if ( sanitize_text_field( wp_unslash( $_POST['mode'] ?? '' ) ) === 'single' ) {
return;
}
check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' );
$comment_post_ID = isset( $_POST['comment_post_ID'] ) ? (int) sanitize_text_field( wp_unslash( $_POST['comment_post_ID'] ) ) : 0; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
$post = get_post( $comment_post_ID ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
if ( ! $post ) {
wp_die( -1 );
}
// Inline Review replies will use the `detail` mode. If that's not what we have, then let WordPress core take over.
if ( isset( $_REQUEST['mode'] ) && $_REQUEST['mode'] === 'dashboard' ) {
return;
}
// If this is not a a reply to a review, bail silently to let WordPress core take over.
if ( get_post_type( $post ) !== 'product' ) {
return;
}
if ( ! current_user_can( 'edit_post', $comment_post_ID ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
wp_die( -1 );
}
if ( empty( $post->post_status ) ) {
wp_die( 1 );
} elseif ( in_array( $post->post_status, array( 'draft', 'pending', 'trash' ), true ) ) {
wp_die( esc_html__( 'Error: You can\'t reply to a review on a draft product.', 'woocommerce' ) );
}
$user = wp_get_current_user();
if ( $user->exists() ) {
$user_ID = $user->ID;
$comment_author = wp_slash( $user->display_name );
$comment_author_email = wp_slash( $user->user_email );
$comment_author_url = wp_slash( $user->user_url );
// WordPress core already sanitizes `content` during the `pre_comment_content` hook, which is why it's not needed here, {@see wp_filter_comment()} and {@see kses_init_filters()}.
$comment_content = isset( $_POST['content'] ) ? wp_unslash( $_POST['content'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$comment_type = isset( $_POST['comment_type'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_type'] ) ) : 'comment';
if ( current_user_can( 'unfiltered_html' ) ) {
if ( ! isset( $_POST['_wp_unfiltered_html_comment'] ) ) {
$_POST['_wp_unfiltered_html_comment'] = '';
}
if ( wp_create_nonce( 'unfiltered-html-comment' ) != $_POST['_wp_unfiltered_html_comment'] ) {
kses_remove_filters(); // Start with a clean slate.
kses_init_filters(); // Set up the filters.
remove_filter( 'pre_comment_content', 'wp_filter_post_kses' );
add_filter( 'pre_comment_content', 'wp_filter_kses' );
}
}
} else {
wp_die( esc_html__( 'Sorry, you must be logged in to reply to a review.', 'woocommerce' ) );
}
if ( $comment_content === '' ) {
wp_die( esc_html__( 'Error: Please type your reply text.', 'woocommerce' ) );
}
$comment_parent = 0;
if ( isset( $_POST['comment_ID'] ) ) {
$comment_parent = absint( wp_unslash( $_POST['comment_ID'] ) );
}
$comment_auto_approved = false;
$commentdata = compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID' );
// Automatically approve parent comment.
if ( ! empty( $_POST['approve_parent'] ) ) {
$parent = get_comment( $comment_parent );
if ( $parent && $parent->comment_approved === '0' && $parent->comment_post_ID === $comment_post_ID ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
if ( ! current_user_can( 'edit_comment', $parent->comment_ID ) ) {
wp_die( -1 );
}
if ( wp_set_comment_status( $parent, 'approve' ) ) {
$comment_auto_approved = true;
}
}
}
$comment_id = wp_new_comment( $commentdata );
if ( is_wp_error( $comment_id ) ) {
wp_die( esc_html( $comment_id->get_error_message() ) );
}
$comment = get_comment( $comment_id );
if ( ! $comment ) {
wp_die( 1 );
}
$position = ( isset( $_POST['position'] ) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1';
ob_start();
$wp_list_table = $this->make_reviews_list_table();
$wp_list_table->single_row( $comment );
$comment_list_item = ob_get_clean();
$response = array(
'what' => 'comment',
'id' => $comment->comment_ID,
'data' => $comment_list_item,
'position' => $position,
);
$counts = wp_count_comments();
$response['supplemental'] = array(
'in_moderation' => $counts->moderated,
'i18n_comments_text' => sprintf(
/* translators: %s: Number of reviews. */
_n( '%s Review', '%s Reviews', $counts->approved, 'woocommerce' ),
number_format_i18n( $counts->approved )
),
'i18n_moderation_text' => sprintf(
/* translators: %s: Number of reviews. */
_n( '%s Review in moderation', '%s Reviews in moderation', $counts->moderated, 'woocommerce' ),
number_format_i18n( $counts->moderated )
),
);
if ( $comment_auto_approved && isset( $parent ) ) {
$response['supplemental']['parent_approved'] = $parent->comment_ID;
$response['supplemental']['parent_post_id'] = $parent->comment_post_ID;
}
$x = new WP_Ajax_Response();
$x->add( $response );
$x->send();
}
/**
* Displays notices on the Reviews page.
*
* @return void
*/
protected function display_notices() : void {
if ( $this->is_reviews_page() ) {
$this->maybe_display_reviews_bulk_action_notice();
}
}
/**
* May display the bulk action admin notice.
*
* @return void
*/
protected function maybe_display_reviews_bulk_action_notice() : void {
$messages = $this->get_bulk_action_notice_messages();
echo ! empty( $messages ) ? '<div id="moderated" class="updated"><p>' . implode( "<br/>\n", $messages ) . '</p></div>' : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Gets the applicable bulk action admin notice messages.
*
* @return array
*/
protected function get_bulk_action_notice_messages() : array {
$approved = isset( $_REQUEST['approved'] ) ? (int) $_REQUEST['approved'] : 0;
$unapproved = isset( $_REQUEST['unapproved'] ) ? (int) $_REQUEST['unapproved'] : 0;
$deleted = isset( $_REQUEST['deleted'] ) ? (int) $_REQUEST['deleted'] : 0;
$trashed = isset( $_REQUEST['trashed'] ) ? (int) $_REQUEST['trashed'] : 0;
$untrashed = isset( $_REQUEST['untrashed'] ) ? (int) $_REQUEST['untrashed'] : 0;
$spammed = isset( $_REQUEST['spammed'] ) ? (int) $_REQUEST['spammed'] : 0;
$unspammed = isset( $_REQUEST['unspammed'] ) ? (int) $_REQUEST['unspammed'] : 0;
$messages = [];
if ( $approved > 0 ) {
/* translators: %s is an integer higher than 0 (1, 2, 3...) */
$messages[] = sprintf( _n( '%s review approved', '%s reviews approved', $approved, 'woocommerce' ), $approved );
}
if ( $unapproved > 0 ) {
/* translators: %s is an integer higher than 0 (1, 2, 3...) */
$messages[] = sprintf( _n( '%s review unapproved', '%s reviews unapproved', $unapproved, 'woocommerce' ), $unapproved );
}
if ( $spammed > 0 ) {
$ids = isset( $_REQUEST['ids'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['ids'] ) ) : 0;
/* translators: %s is an integer higher than 0 (1, 2, 3...) */
$messages[] = sprintf( _n( '%s review marked as spam.', '%s reviews marked as spam.', $spammed, 'woocommerce' ), $spammed ) . ' <a href="' . esc_url( wp_nonce_url( "edit-comments.php?doaction=undo&action=unspam&ids=$ids", 'bulk-comments' ) ) . '">' . __( 'Undo', 'woocommerce' ) . '</a><br />';
}
if ( $unspammed > 0 ) {
/* translators: %s is an integer higher than 0 (1, 2, 3...) */
$messages[] = sprintf( _n( '%s review restored from the spam', '%s reviews restored from the spam', $unspammed, 'woocommerce' ), $unspammed );
}
if ( $trashed > 0 ) {
$ids = isset( $_REQUEST['ids'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['ids'] ) ) : 0;
/* translators: %s is an integer higher than 0 (1, 2, 3...) */
$messages[] = sprintf( _n( '%s review moved to the Trash.', '%s reviews moved to the Trash.', $trashed, 'woocommerce' ), $trashed ) . ' <a href="' . esc_url( wp_nonce_url( "edit-comments.php?doaction=undo&action=untrash&ids=$ids", 'bulk-comments' ) ) . '">' . __( 'Undo', 'woocommerce' ) . '</a><br />';
}
if ( $untrashed > 0 ) {
/* translators: %s is an integer higher than 0 (1, 2, 3...) */
$messages[] = sprintf( _n( '%s review restored from the Trash', '%s reviews restored from the Trash', $untrashed, 'woocommerce' ), $untrashed );
}
if ( $deleted > 0 ) {
/* translators: %s is an integer higher than 0 (1, 2, 3...) */
$messages[] = sprintf( _n( '%s review permanently deleted', '%s reviews permanently deleted', $deleted, 'woocommerce' ), $deleted );
}
return $messages;
}
/**
* Counts the number of pending product reviews/replies, and returns the notification bubble if there's more than zero.
*
* @return string Empty string if there are no pending reviews, or bubble HTML if there are.
*/
protected function get_pending_count_bubble() : string {
$count = (int) get_comments(
[
'type__in' => [ 'review', 'comment' ],
'status' => '0',
'post_type' => 'product',
'count' => true,
]
);
/**
* Provides an opportunity to alter the pending comment count used within
* the product reviews admin list table.
*
* @since 7.0.0
*
* @param array $count Current count of comments pending review.
*/
$count = apply_filters( 'woocommerce_product_reviews_pending_count', $count );
if ( empty( $count ) ) {
return '';
}
return ' <span class="awaiting-mod count-' . esc_attr( $count ) . '"><span class="pending-count">' . esc_html( $count ) . '</span></span>';
}
/**
* Highlights Product -> Reviews admin menu item when editing a review or a reply to a review.
*
* @global string $submenu_file
*
* @param string|mixed $parent_file Parent menu item.
* @return string
*/
protected function edit_review_parent_file( $parent_file ) {
global $submenu_file, $current_screen;
if ( isset( $current_screen->id, $_GET['c'] ) && $current_screen->id === 'comment' ) {
$comment_id = absint( $_GET['c'] );
$comment = get_comment( $comment_id );
if ( isset( $comment->comment_parent ) && $comment->comment_parent > 0 ) {
$comment = get_comment( $comment->comment_parent );
}
if ( isset( $comment->comment_post_ID ) && get_post_type( $comment->comment_post_ID ) === 'product' ) {
$parent_file = 'edit.php?post_type=product';
$submenu_file = 'product-reviews'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
}
return $parent_file;
}
/**
* Replaces Edit/Moderate Comment title/headline with Edit Review, when editing/moderating a review.
*
* @param string|mixed $translation Translated text.
* @param string|mixed $text Text to translate.
* @return string|mixed Translated text.
*/
protected function edit_comments_screen_text( $translation, $text ) {
global $comment;
// Bail out if not a text we should replace.
if ( ! in_array( $text, [ 'Edit Comment', 'Moderate Comment' ], true ) ) {
return $translation;
}
// Try to get comment from query params when not in context already.
if ( ! $comment && isset( $_GET['action'], $_GET['c'] ) && $_GET['action'] === 'editcomment' ) {
$comment_id = absint( $_GET['c'] );
$comment = get_comment( $comment_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
$is_reply = isset( $comment->comment_parent ) && $comment->comment_parent > 0;
// Only replace the translated text if we are editing a comment left on a product (ie. a review).
if ( isset( $comment->comment_post_ID ) && get_post_type( $comment->comment_post_ID ) === 'product' ) {
if ( $text === 'Edit Comment' ) {
$translation = $is_reply
? __( 'Edit Review Reply', 'woocommerce' )
: __( 'Edit Review', 'woocommerce' );
} elseif ( $text === 'Moderate Comment' ) {
$translation = $is_reply
? __( 'Moderate Review Reply', 'woocommerce' )
: __( 'Moderate Review', 'woocommerce' );
}
}
return $translation;
}
/**
* Returns a new instance of `ReviewsListTable`, with the screen argument specified.
*
* @return ReviewsListTable
*/
protected function make_reviews_list_table() : ReviewsListTable {
return new ReviewsListTable( [ 'screen' => $this->reviews_page_hook ? $this->reviews_page_hook : 'product_page_product-reviews' ] );
}
/**
* Initializes the list table.
*
* @return void
*/
protected function load_reviews_screen() : void {
$this->reviews_list_table = $this->make_reviews_list_table();
$this->reviews_list_table->process_bulk_action();
}
/**
* Renders the Reviews page.
*
* @return void
*/
public function render_reviews_list_table() : void {
$this->reviews_list_table->prepare_items();
ob_start();
?>
<div class="wrap">
<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
<?php $this->reviews_list_table->views(); ?>
<form id="reviews-filter" method="get">
<?php $page = isset( $_REQUEST['page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) : static::MENU_SLUG; ?>
<input type="hidden" name="page" value="<?php echo esc_attr( $page ); ?>" />
<input type="hidden" name="post_type" value="product" />
<input type="hidden" name="pagegen_timestamp" value="<?php echo esc_attr( current_time( 'mysql', true ) ); ?>" />
<?php $this->reviews_list_table->search_box( __( 'Search Reviews', 'woocommerce' ), 'reviews' ); ?>
<?php $this->reviews_list_table->display(); ?>
</form>
</div>
<?php
wp_comment_reply( '-1', true, 'detail' );
wp_comment_trashnotice();
/**
* Filters the contents of the product reviews list table output.
*
* @since 6.7.0
*
* @param string $output The HTML output of the list table.
* @param ReviewsListTable $reviews_list_table The reviews list table instance.
*/
echo apply_filters( 'woocommerce_product_reviews_list_table', ob_get_clean(), $this->reviews_list_table ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
ProductReviews/ReviewsCommentsOverrides.php 0000644 00000010544 15154512352 0015266 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\ProductReviews;
use Automattic\WooCommerce\Internal\Traits\AccessiblePrivateMethods;
use WP_Comment_Query;
use WP_Screen;
/**
* Tweaks the WordPress comments page to exclude reviews.
*/
class ReviewsCommentsOverrides {
use AccessiblePrivateMethods;
const REVIEWS_MOVED_NOTICE_ID = 'product_reviews_moved';
/**
* Constructor.
*/
public function __construct() {
self::add_action( 'admin_notices', array( $this, 'display_notices' ) );
self::add_filter( 'woocommerce_dismiss_admin_notice_capability', array( $this, 'get_dismiss_capability' ), 10, 2 );
self::add_filter( 'comments_list_table_query_args', array( $this, 'exclude_reviews_from_comments' ) );
}
/**
* Renders admin notices.
*/
protected function display_notices() : void {
$screen = get_current_screen();
if ( empty( $screen ) || $screen->base !== 'edit-comments' ) {
return;
}
$this->maybe_display_reviews_moved_notice();
}
/**
* May render an admin notice informing the user that reviews were moved to a new page.
*
* @return void
*/
protected function maybe_display_reviews_moved_notice() : void {
if ( $this->should_display_reviews_moved_notice() ) {
$this->display_reviews_moved_notice();
}
}
/**
* Checks if the admin notice informing the user that reviews were moved to a new page should be displayed.
*
* @return bool
*/
protected function should_display_reviews_moved_notice() : bool {
// Do not display if the user does not have the capability to see the new page.
if ( ! WC()->call_function( 'current_user_can', Reviews::get_capability() ) ) {
return false;
}
// Do not display if the current user has dismissed this notice.
if ( WC()->call_function( 'get_user_meta', get_current_user_id(), 'dismissed_' . static::REVIEWS_MOVED_NOTICE_ID . '_notice', true ) ) {
return false;
}
return true;
}
/**
* Renders an admin notice informing the user that reviews were moved to a new page.
*
* @return void
*/
protected function display_reviews_moved_notice() : void {
$dismiss_url = wp_nonce_url(
add_query_arg(
[
'wc-hide-notice' => urlencode( static::REVIEWS_MOVED_NOTICE_ID ),
]
),
'woocommerce_hide_notices_nonce',
'_wc_notice_nonce'
);
?>
<div class="notice notice-info is-dismissible">
<p><strong><?php esc_html_e( 'Product reviews have moved!', 'woocommerce' ); ?></strong></p>
<p><?php esc_html_e( 'Product reviews can now be managed from Products > Reviews.', 'woocommerce' ); ?></p>
<p class="submit">
<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=product&page=product-reviews' ) ); ?>" class="button-primary"><?php esc_html_e( 'Visit new location', 'woocommerce' ); ?></a>
</p>
<button type="button" class="notice-dismiss" onclick="window.location = '<?php echo esc_url( $dismiss_url ); ?>';"><span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'woocommerce' ); ?></span></button>
</div>
<?php
}
/**
* Gets the capability required to dismiss the notice.
*
* This is required so that users who do not have the manage_woocommerce capability (e.g. Editors) can still dismiss
* the notice displayed in the Comments page.
*
* @param string|mixed $default_capability The default required capability.
* @param string|mixed $notice_name The notice name.
* @return string
*/
protected function get_dismiss_capability( $default_capability, $notice_name ) {
return $notice_name === self::REVIEWS_MOVED_NOTICE_ID ? Reviews::get_capability() : $default_capability;
}
/**
* Excludes product reviews from showing in the comments page.
*
* @param array|mixed $args {@see WP_Comment_Query} query args.
* @return array
*/
protected function exclude_reviews_from_comments( $args ) : array {
$screen = get_current_screen();
// We only wish to intervene if the edit comments screen has been requested.
if ( ! $screen instanceof WP_Screen || 'edit-comments' !== $screen->id ) {
return $args;
}
if ( ! empty( $args['post_type'] ) && $args['post_type'] !== 'any' ) {
$post_types = (array) $args['post_type'];
} else {
$post_types = get_post_types();
}
$index = array_search( 'product', $post_types );
if ( $index !== false ) {
unset( $post_types[ $index ] );
}
if ( ! is_array( $args ) ) {
$args = [];
}
$args['post_type'] = $post_types;
return $args;
}
}
ProductReviews/ReviewsListTable.php 0000644 00000133171 15154512352 0013503 0 ustar 00 <?php
/**
* Product > Reviews
*/
namespace Automattic\WooCommerce\Internal\Admin\ProductReviews;
use WC_Product;
use WP_Comment;
use WP_Comments_List_Table;
use WP_List_Table;
use WP_Post;
/**
* Handles the Product Reviews page.
*/
class ReviewsListTable extends WP_List_Table {
/**
* Memoization flag to determine if the current user can edit the current review.
*
* @var bool
*/
private $current_user_can_edit_review = false;
/**
* Memoization flag to determine if the current user can moderate reviews.
*
* @var bool
*/
private $current_user_can_moderate_reviews;
/**
* Current rating of reviews to display.
*
* @var int
*/
private $current_reviews_rating = 0;
/**
* Current product the reviews should be displayed for.
*
* @var WC_Product|null Product or null for all products.
*/
private $current_product_for_reviews;
/**
* Constructor.
*
* @param array|string $args Array or string of arguments.
*/
public function __construct( $args = [] ) {
parent::__construct(
wp_parse_args(
$args,
[
'plural' => 'product-reviews',
'singular' => 'product-review',
]
)
);
$this->current_user_can_moderate_reviews = current_user_can( Reviews::get_capability( 'moderate' ) );
}
/**
* Prepares reviews for display.
*
* @return void
*/
public function prepare_items() : void {
$this->set_review_status();
$this->set_review_type();
$this->current_reviews_rating = isset( $_REQUEST['review_rating'] ) ? absint( $_REQUEST['review_rating'] ) : 0;
$this->set_review_product();
$args = [
'number' => $this->get_per_page(),
'post_type' => 'product',
];
// Include the order & orderby arguments.
$args = wp_parse_args( $this->get_sort_arguments(), $args );
// Handle the review item types filter.
$args = wp_parse_args( $this->get_filter_type_arguments(), $args );
// Handle the reviews rating filter.
$args = wp_parse_args( $this->get_filter_rating_arguments(), $args );
// Handle the review product filter.
$args = wp_parse_args( $this->get_filter_product_arguments(), $args );
// Include the review status arguments.
$args = wp_parse_args( $this->get_status_arguments(), $args );
// Include the search argument.
$args = wp_parse_args( $this->get_search_arguments(), $args );
// Include the offset argument.
$args = wp_parse_args( $this->get_offset_arguments(), $args );
/**
* Provides an opportunity to alter the comment query arguments used within
* the product reviews admin list table.
*
* @since 7.0.0
*
* @param array $args Comment query args.
*/
$args = (array) apply_filters( 'woocommerce_product_reviews_list_table_prepare_items_args', $args );
$comments = get_comments( $args );
update_comment_cache( $comments );
$this->items = $comments;
$this->set_pagination_args(
[
'total_items' => get_comments( $this->get_total_comments_arguments( $args ) ),
'per_page' => $this->get_per_page(),
]
);
}
/**
* Returns the number of items to show per page.
*
* @return int Customized per-page value if available, or 20 as the default.
*/
protected function get_per_page() : int {
return $this->get_items_per_page( 'edit_comments_per_page' );
}
/**
* Sets the product to filter reviews by.
*
* @return void
*/
protected function set_review_product() : void {
$product_id = isset( $_REQUEST['product_id'] ) ? absint( $_REQUEST['product_id'] ) : null;
$product = $product_id ? wc_get_product( $product_id ) : null;
if ( $product instanceof WC_Product ) {
$this->current_product_for_reviews = $product;
}
}
/**
* Sets the `$comment_status` global based on the current request.
*
* @global string $comment_status
*
* @return void
*/
protected function set_review_status() : void {
global $comment_status;
$comment_status = sanitize_text_field( wp_unslash( $_REQUEST['comment_status'] ?? 'all' ) ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
if ( ! in_array( $comment_status, [ 'all', 'moderated', 'approved', 'spam', 'trash' ], true ) ) {
$comment_status = 'all'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
}
/**
* Sets the `$comment_type` global based on the current request.
*
* @global string $comment_type
*
* @return void
*/
protected function set_review_type() : void {
global $comment_type;
$review_type = sanitize_text_field( wp_unslash( $_REQUEST['review_type'] ?? 'all' ) );
if ( 'all' !== $review_type && ! empty( $review_type ) ) {
$comment_type = $review_type; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
}
/**
* Builds the `orderby` and `order` arguments based on the current request.
*
* @return array
*/
protected function get_sort_arguments() : array {
$orderby = sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ?? '' ) );
$order = sanitize_text_field( wp_unslash( $_REQUEST['order'] ?? '' ) );
$args = [];
if ( ! in_array( $orderby, $this->get_sortable_columns(), true ) ) {
$orderby = 'comment_date_gmt';
}
// If ordering by "rating", then we need to adjust to sort by meta value.
if ( 'rating' === $orderby ) {
$orderby = 'meta_value_num';
$args['meta_key'] = 'rating';
}
if ( ! in_array( strtolower( $order ), [ 'asc', 'desc' ], true ) ) {
$order = 'desc';
}
return wp_parse_args(
[
'orderby' => $orderby,
'order' => strtolower( $order ),
],
$args
);
}
/**
* Builds the `type` argument based on the current request.
*
* @return array
*/
protected function get_filter_type_arguments() : array {
$args = [];
$item_type = isset( $_REQUEST['review_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['review_type'] ) ) : 'all';
if ( 'all' === $item_type ) {
return $args;
}
$args['type'] = $item_type;
return $args;
}
/**
* Builds the `meta_query` arguments based on the current request.
*
* @return array
*/
protected function get_filter_rating_arguments() : array {
$args = [];
if ( empty( $this->current_reviews_rating ) ) {
return $args;
}
$args['meta_query'] = [
[
'key' => 'rating',
'value' => (int) $this->current_reviews_rating,
'compare' => '=',
'type' => 'NUMERIC',
],
];
return $args;
}
/**
* Gets the `post_id` argument based on the current request.
*
* @return array
*/
public function get_filter_product_arguments() : array {
$args = [];
if ( $this->current_product_for_reviews instanceof WC_Product ) {
$args['post_id'] = $this->current_product_for_reviews->get_id();
}
return $args;
}
/**
* Gets the `status` argument based on the current request.
*
* @return array
*/
protected function get_status_arguments() : array {
$args = [];
global $comment_status;
if ( ! empty( $comment_status ) && 'all' !== $comment_status && array_key_exists( $comment_status, $this->get_status_filters() ) ) {
$args['status'] = $this->convert_status_to_query_value( $comment_status );
}
return $args;
}
/**
* Gets the `search` argument based on the current request.
*
* @return array
*/
protected function get_search_arguments() : array {
$args = [];
if ( ! empty( $_REQUEST['s'] ) ) {
$args['search'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) );
}
return $args;
}
/**
* Returns the `offset` argument based on the current request.
*
* @return array
*/
protected function get_offset_arguments() : array {
$args = [];
if ( isset( $_REQUEST['start'] ) ) {
$args['offset'] = absint( wp_unslash( $_REQUEST['start'] ) );
} else {
$args['offset'] = ( $this->get_pagenum() - 1 ) * $this->get_per_page();
}
return $args;
}
/**
* Returns the arguments used to count the total number of comments.
*
* @param array $default_query_args Query args for the main request.
* @return array
*/
protected function get_total_comments_arguments( array $default_query_args ) : array {
return wp_parse_args(
[
'count' => true,
'offset' => 0,
'number' => 0,
],
$default_query_args
);
}
/**
* Displays the product reviews HTML table.
*
* Reimplements {@see WP_Comment_::display()} but we change the ID to match the one output by {@see WP_Comments_List_Table::display()}.
* This will automatically handle additional CSS for consistency with the comments page.
*
* @return void
*/
public function display() : void {
$this->display_tablenav( 'top' );
$this->screen->render_screen_reader_content( 'heading_list' );
?>
<table class="wp-list-table <?php echo esc_attr( implode( ' ', $this->get_table_classes() ) ); ?>">
<thead>
<tr>
<?php $this->print_column_headers(); ?>
</tr>
</thead>
<tbody id="the-comment-list" data-wp-lists="list:comment">
<?php $this->display_rows_or_placeholder(); ?>
</tbody>
<tfoot>
<tr>
<?php $this->print_column_headers( false ); ?>
</tr>
</tfoot>
</table>
<?php
$this->display_tablenav( 'bottom' );
}
/**
* Render a single row HTML.
*
* @global WP_Post $post
* @global WP_Comment $comment
*
* @param WP_Comment|mixed $item Review or reply being rendered.
* @return void
*/
public function single_row( $item ) : void {
global $post, $comment;
// Overrides the comment global for properly rendering rows.
$comment = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$the_comment_class = (string) wp_get_comment_status( $comment->comment_ID );
$the_comment_class = implode( ' ', get_comment_class( $the_comment_class, $comment->comment_ID, $comment->comment_post_ID ) );
// Sets the post for the product in context.
$post = get_post( $comment->comment_post_ID ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$this->current_user_can_edit_review = current_user_can( 'edit_comment', $comment->comment_ID );
?>
<tr id="comment-<?php echo esc_attr( $comment->comment_ID ); ?>" class="comment <?php echo esc_attr( $the_comment_class ); ?>">
<?php $this->single_row_columns( $comment ); ?>
</tr>
<?php
}
/**
* Generate and display row actions links.
*
* @see WP_Comments_List_Table::handle_row_actions() for consistency.
*
* @global string $comment_status Status for the current listed comments.
*
* @param WP_Comment|mixed $item The product review or reply in context.
* @param string|mixed $column_name Current column name.
* @param string|mixed $primary Primary column name.
* @return string
*/
protected function handle_row_actions( $item, $column_name, $primary ) : string {
global $comment_status;
if ( $primary !== $column_name || ! $this->current_user_can_edit_review ) {
return '';
}
$review_status = wp_get_comment_status( $item );
$url = add_query_arg(
[
'c' => urlencode( $item->comment_ID ),
],
admin_url( 'comment.php' )
);
$approve_url = wp_nonce_url( add_query_arg( 'action', 'approvecomment', $url ), "approve-comment_$item->comment_ID" );
$unapprove_url = wp_nonce_url( add_query_arg( 'action', 'unapprovecomment', $url ), "approve-comment_$item->comment_ID" );
$spam_url = wp_nonce_url( add_query_arg( 'action', 'spamcomment', $url ), "delete-comment_$item->comment_ID" );
$unspam_url = wp_nonce_url( add_query_arg( 'action', 'unspamcomment', $url ), "delete-comment_$item->comment_ID" );
$trash_url = wp_nonce_url( add_query_arg( 'action', 'trashcomment', $url ), "delete-comment_$item->comment_ID" );
$untrash_url = wp_nonce_url( add_query_arg( 'action', 'untrashcomment', $url ), "delete-comment_$item->comment_ID" );
$delete_url = wp_nonce_url( add_query_arg( 'action', 'deletecomment', $url ), "delete-comment_$item->comment_ID" );
$actions = [
'approve' => '',
'unapprove' => '',
'reply' => '',
'quickedit' => '',
'edit' => '',
'spam' => '',
'unspam' => '',
'trash' => '',
'untrash' => '',
'delete' => '',
];
if ( $comment_status && 'all' !== $comment_status ) {
if ( 'approved' === $review_status ) {
$actions['unapprove'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="vim-u vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $unapprove_url ),
esc_attr( "delete:the-comment-list:comment-{$item->comment_ID}:e7e7d3:action=dim-comment&new=unapproved" ),
esc_attr__( 'Unapprove this review', 'woocommerce' ),
esc_html__( 'Unapprove', 'woocommerce' )
);
} elseif ( 'unapproved' === $review_status ) {
$actions['approve'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="vim-a vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $approve_url ),
esc_attr( "delete:the-comment-list:comment-{$item->comment_ID}:e7e7d3:action=dim-comment&new=approved" ),
esc_attr__( 'Approve this review', 'woocommerce' ),
esc_html__( 'Approve', 'woocommerce' )
);
}
} else {
$actions['approve'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="vim-a aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $approve_url ),
esc_attr( "dim:the-comment-list:comment-{$item->comment_ID}:unapproved:e7e7d3:e7e7d3:new=approved" ),
esc_attr__( 'Approve this review', 'woocommerce' ),
esc_html__( 'Approve', 'woocommerce' )
);
$actions['unapprove'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="vim-u aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $unapprove_url ),
esc_attr( "dim:the-comment-list:comment-{$item->comment_ID}:unapproved:e7e7d3:e7e7d3:new=unapproved" ),
esc_attr__( 'Unapprove this review', 'woocommerce' ),
esc_html__( 'Unapprove', 'woocommerce' )
);
}
if ( 'spam' !== $review_status ) {
$actions['spam'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="vim-s vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $spam_url ),
esc_attr( "delete:the-comment-list:comment-{$item->comment_ID}::spam=1" ),
esc_attr__( 'Mark this review as spam', 'woocommerce' ),
/* translators: "Mark as spam" link. */
esc_html_x( 'Spam', 'verb', 'woocommerce' )
);
} else {
$actions['unspam'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="vim-z vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $unspam_url ),
esc_attr( "delete:the-comment-list:comment-{$item->comment_ID}:66cc66:unspam=1" ),
esc_attr__( 'Restore this review from the spam', 'woocommerce' ),
esc_html_x( 'Not Spam', 'review', 'woocommerce' )
);
}
if ( 'trash' === $review_status ) {
$actions['untrash'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="vim-z vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $untrash_url ),
esc_attr( "delete:the-comment-list:comment-{$item->comment_ID}:66cc66:untrash=1" ),
esc_attr__( 'Restore this review from the Trash', 'woocommerce' ),
esc_html__( 'Restore', 'woocommerce' )
);
}
if ( 'spam' === $review_status || 'trash' === $review_status || ! EMPTY_TRASH_DAYS ) {
$actions['delete'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="delete vim-d vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $delete_url ),
esc_attr( "delete:the-comment-list:comment-{$item->comment_ID}::delete=1" ),
esc_attr__( 'Delete this review permanently', 'woocommerce' ),
esc_html__( 'Delete Permanently', 'woocommerce' )
);
} else {
$actions['trash'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="delete vim-d vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $trash_url ),
esc_attr( "delete:the-comment-list:comment-{$item->comment_ID}::trash=1" ),
esc_attr__( 'Move this review to the Trash', 'woocommerce' ),
esc_html_x( 'Trash', 'verb', 'woocommerce' )
);
}
if ( 'spam' !== $review_status && 'trash' !== $review_status ) {
$actions['edit'] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
esc_url(
add_query_arg(
[
'action' => 'editcomment',
'c' => urlencode( $item->comment_ID ),
],
admin_url( 'comment.php' )
)
),
esc_attr__( 'Edit this review', 'woocommerce' ),
esc_html__( 'Edit', 'woocommerce' )
);
$format = '<button type="button" data-comment-id="%d" data-post-id="%d" data-action="%s" class="%s button-link" aria-expanded="false" aria-label="%s">%s</button>';
$actions['quickedit'] = sprintf(
$format,
esc_attr( $item->comment_ID ),
esc_attr( $item->comment_post_ID ),
'edit',
'vim-q comment-inline',
esc_attr__( 'Quick edit this review inline', 'woocommerce' ),
esc_html__( 'Quick Edit', 'woocommerce' )
);
$actions['reply'] = sprintf(
$format,
esc_attr( $item->comment_ID ),
esc_attr( $item->comment_post_ID ),
'replyto',
'vim-r comment-inline',
esc_attr__( 'Reply to this review', 'woocommerce' ),
esc_html__( 'Reply', 'woocommerce' )
);
}
$always_visible = 'excerpt' === get_user_setting( 'posts_list_mode', 'list' );
$output = '<div class="' . ( $always_visible ? 'row-actions visible' : 'row-actions' ) . '">';
$i = 0;
foreach ( array_filter( $actions ) as $action => $link ) {
++$i;
if ( ( ( 'approve' === $action || 'unapprove' === $action ) && 2 === $i ) || 1 === $i ) {
$sep = '';
} else {
$sep = ' | ';
}
if ( ( 'reply' === $action || 'quickedit' === $action ) && ! wp_doing_ajax() ) {
$action .= ' hide-if-no-js';
} elseif ( ( 'untrash' === $action && 'trash' === $review_status ) || ( 'unspam' === $action && 'spam' === $review_status ) ) {
if ( '1' === get_comment_meta( $item->comment_ID, '_wp_trash_meta_status', true ) ) {
$action .= ' approve';
} else {
$action .= ' unapprove';
}
}
$output .= "<span class='$action'>$sep$link</span>";
}
$output .= '</div>';
$output .= '<button type="button" class="toggle-row"><span class="screen-reader-text">' . esc_html__( 'Show more details', 'woocommerce' ) . '</span></button>';
return $output;
}
/**
* Gets the columns for the table.
*
* @return array Table columns and their headings.
*/
public function get_columns() : array {
$columns = [
'cb' => '<input type="checkbox" />',
'type' => _x( 'Type', 'review type', 'woocommerce' ),
'author' => __( 'Author', 'woocommerce' ),
'rating' => __( 'Rating', 'woocommerce' ),
'comment' => _x( 'Review', 'column name', 'woocommerce' ),
'response' => __( 'Product', 'woocommerce' ),
'date' => _x( 'Submitted on', 'column name', 'woocommerce' ),
];
/**
* Filters the table columns.
*
* @since 6.7.0
*
* @param array $columns
*/
return (array) apply_filters( 'woocommerce_product_reviews_table_columns', $columns );
}
/**
* Gets the name of the default primary column.
*
* @return string Name of the primary colum.
*/
protected function get_primary_column_name() : string {
return 'comment';
}
/**
* Gets a list of sortable columns.
*
* Key is the column ID and value is which database column we perform the sorting on.
* The `rating` column uses a unique key instead, as that requires sorting by meta value.
*
* @return array
*/
protected function get_sortable_columns() : array {
return [
'author' => 'comment_author',
'response' => 'comment_post_ID',
'date' => 'comment_date_gmt',
'type' => 'comment_type',
'rating' => 'rating',
];
}
/**
* Returns a list of available bulk actions.
*
* @global string $comment_status
*
* @return array
*/
protected function get_bulk_actions() : array {
global $comment_status;
$actions = [];
if ( in_array( $comment_status, [ 'all', 'approved' ], true ) ) {
$actions['unapprove'] = __( 'Unapprove', 'woocommerce' );
}
if ( in_array( $comment_status, [ 'all', 'moderated' ], true ) ) {
$actions['approve'] = __( 'Approve', 'woocommerce' );
}
if ( in_array( $comment_status, [ 'all', 'moderated', 'approved', 'trash' ], true ) ) {
$actions['spam'] = _x( 'Mark as spam', 'review', 'woocommerce' );
}
if ( 'trash' === $comment_status ) {
$actions['untrash'] = __( 'Restore', 'woocommerce' );
} elseif ( 'spam' === $comment_status ) {
$actions['unspam'] = _x( 'Not spam', 'review', 'woocommerce' );
}
if ( in_array( $comment_status, [ 'trash', 'spam' ], true ) || ! EMPTY_TRASH_DAYS ) {
$actions['delete'] = __( 'Delete permanently', 'woocommerce' );
} else {
$actions['trash'] = __( 'Move to Trash', 'woocommerce' );
}
return $actions;
}
/**
* Returns the current action select in bulk actions menu.
*
* This is overridden in order to support `delete_all` for use in {@see ReviewsListTable::process_bulk_action()}
*
* {@see WP_Comments_List_Table::current_action()} for reference.
*
* @return string|false
*/
public function current_action() {
if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) {
return 'delete_all';
}
return parent::current_action();
}
/**
* Processes the bulk actions.
*
* @return void
*/
public function process_bulk_action() : void {
if ( ! $this->current_user_can_moderate_reviews ) {
return;
}
if ( $this->current_action() ) {
check_admin_referer( 'bulk-product-reviews' );
$query_string = remove_query_arg( [ 'page', '_wpnonce' ], wp_unslash( ( $_SERVER['QUERY_STRING'] ?? '' ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
// Replace current nonce with bulk-comments nonce.
$comments_nonce = wp_create_nonce( 'bulk-comments' );
$query_string = add_query_arg( '_wpnonce', $comments_nonce, $query_string );
// Redirect to edit-comments.php, which will handle processing the action for us.
wp_safe_redirect( esc_url_raw( admin_url( 'edit-comments.php?' . $query_string ) ) );
exit;
} elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
wp_safe_redirect( remove_query_arg( [ '_wp_http_referer', '_wpnonce' ] ) );
exit;
}
}
/**
* Returns an array of supported statuses and their labels.
*
* @return array
*/
protected function get_status_filters() : array {
return [
/* translators: %s: Number of reviews. */
'all' => _nx_noop(
'All <span class="count">(%s)</span>',
'All <span class="count">(%s)</span>',
'product reviews',
'woocommerce'
),
/* translators: %s: Number of reviews. */
'moderated' => _nx_noop(
'Pending <span class="count">(%s)</span>',
'Pending <span class="count">(%s)</span>',
'product reviews',
'woocommerce'
),
/* translators: %s: Number of reviews. */
'approved' => _nx_noop(
'Approved <span class="count">(%s)</span>',
'Approved <span class="count">(%s)</span>',
'product reviews',
'woocommerce'
),
/* translators: %s: Number of reviews. */
'spam' => _nx_noop(
'Spam <span class="count">(%s)</span>',
'Spam <span class="count">(%s)</span>',
'product reviews',
'woocommerce'
),
/* translators: %s: Number of reviews. */
'trash' => _nx_noop(
'Trash <span class="count">(%s)</span>',
'Trash <span class="count">(%s)</span>',
'product reviews',
'woocommerce'
),
];
}
/**
* Returns the available status filters.
*
* @see WP_Comments_List_Table::get_views() for consistency.
*
* @global int $post_id
* @global string $comment_status
* @global string $comment_type
*
* @return array An associative array of fully-formed comment status links. Includes 'All', 'Pending', 'Approved', 'Spam', and 'Trash'.
*/
protected function get_views() : array {
global $post_id, $comment_status, $comment_type;
$status_links = [];
$status_labels = $this->get_status_filters();
if ( ! EMPTY_TRASH_DAYS ) {
unset( $status_labels['trash'] );
}
$link = $this->get_view_url( (string) $comment_type, (int) $post_id );
foreach ( $status_labels as $status => $label ) {
$current_link_attributes = '';
if ( $status === $comment_status ) {
$current_link_attributes = ' class="current" aria-current="page"';
}
$link = add_query_arg( 'comment_status', urlencode( $status ), $link );
$number_reviews_for_status = $this->get_review_count( $status, (int) $post_id );
$count_html = sprintf(
'<span class="%s-count">%s</span>',
( 'moderated' === $status ) ? 'pending' : $status,
number_format_i18n( $number_reviews_for_status )
);
$status_links[ $status ] = '<a href="' . esc_url( $link ) . '"' . $current_link_attributes . '>' . sprintf( translate_nooped_plural( $label, $number_reviews_for_status ), $count_html ) . '</a>';
}
return $status_links;
}
/**
* Gets the base URL for a view, excluding the status (that should be appended).
*
* @param string $comment_type Comment type filter.
* @param int $post_id Current post ID.
* @return string
*/
protected function get_view_url( string $comment_type, int $post_id ) : string {
$link = Reviews::get_reviews_page_url();
if ( ! empty( $comment_type ) && 'all' !== $comment_type ) {
$link = add_query_arg( 'comment_type', urlencode( $comment_type ), $link );
}
if ( ! empty( $post_id ) ) {
$link = add_query_arg( 'p', absint( $post_id ), $link );
}
return $link;
}
/**
* Gets the number of reviews (including review replies) for a given status.
*
* @param string $status Status key from {@see ReviewsListTable::get_status_filters()}.
* @param int $product_id ID of the product if we're filtering by product in this request. Otherwise, `0` for no product filters.
* @return int
*/
protected function get_review_count( string $status, int $product_id ) : int {
return (int) get_comments(
[
'type__in' => [ 'review', 'comment' ],
'status' => $this->convert_status_to_query_value( $status ),
'post_type' => 'product',
'post_id' => $product_id,
'count' => true,
]
);
}
/**
* Converts a status key into its equivalent `comment_approved` database column value.
*
* @param string $status Status key from {@see ReviewsListTable::get_status_filters()}.
* @return string
*/
protected function convert_status_to_query_value( string $status ) : string {
// These keys exactly match the database column.
if ( in_array( $status, [ 'spam', 'trash' ], true ) ) {
return $status;
}
switch ( $status ) {
case 'moderated':
return '0';
case 'approved':
return '1';
default:
return 'all';
}
}
/**
* Outputs the text to display when there are no reviews to display.
*
* @see WP_List_Table::no_items()
*
* @global string $comment_status
*
* @return void
*/
public function no_items() : void {
global $comment_status;
if ( 'moderated' === $comment_status ) {
esc_html_e( 'No reviews awaiting moderation.', 'woocommerce' );
} else {
esc_html_e( 'No reviews found.', 'woocommerce' );
}
}
/**
* Renders the checkbox column.
*
* @param WP_Comment|mixed $item Review or reply being rendered.
* @return void
*/
protected function column_cb( $item ) : void {
ob_start();
if ( $this->current_user_can_edit_review ) {
?>
<label class="screen-reader-text" for="cb-select-<?php echo esc_attr( $item->comment_ID ); ?>"><?php esc_html_e( 'Select review', 'woocommerce' ); ?></label>
<input
id="cb-select-<?php echo esc_attr( $item->comment_ID ); ?>"
type="checkbox"
name="delete_comments[]"
value="<?php echo esc_attr( $item->comment_ID ); ?>"
/>
<?php
}
echo $this->filter_column_output( 'cb', ob_get_clean(), $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Renders the review column.
*
* @see WP_Comments_List_Table::column_comment() for consistency.
*
* @param WP_Comment|mixed $item Review or reply being rendered.
* @return void
*/
protected function column_comment( $item ) : void {
$in_reply_to = $this->get_in_reply_to_review_text( $item );
ob_start();
if ( $in_reply_to ) {
echo $in_reply_to . '<br><br>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
printf(
'%1$s%2$s%3$s',
'<div class="comment-text">',
get_comment_text( $item->comment_ID ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'</div>'
);
if ( $this->current_user_can_edit_review ) {
?>
<div id="inline-<?php echo esc_attr( $item->comment_ID ); ?>" class="hidden">
<textarea class="comment" rows="1" cols="1"><?php echo esc_textarea( $item->comment_content ); ?></textarea>
<div class="author-email"><?php echo esc_attr( $item->comment_author_email ); ?></div>
<div class="author"><?php echo esc_attr( $item->comment_author ); ?></div>
<div class="author-url"><?php echo esc_attr( $item->comment_author_url ); ?></div>
<div class="comment_status"><?php echo esc_html( $item->comment_approved ); ?></div>
</div>
<?php
}
echo $this->filter_column_output( 'comment', ob_get_clean(), $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Gets the in-reply-to-review text.
*
* @param WP_Comment|mixed $reply Reply to review.
* @return string
*/
private function get_in_reply_to_review_text( $reply ) : string {
$review = $reply->comment_parent ? get_comment( $reply->comment_parent ) : null;
if ( ! $review ) {
return '';
}
$parent_review_link = get_comment_link( $review );
$review_author_name = get_comment_author( $review );
return sprintf(
/* translators: %s: Parent review link with review author name. */
ent2ncr( __( 'In reply to %s.', 'woocommerce' ) ),
'<a href="' . esc_url( $parent_review_link ) . '">' . esc_html( $review_author_name ) . '</a>'
);
}
/**
* Renders the author column.
*
* @see WP_Comments_List_Table::column_author() for consistency.
*
* @param WP_Comment|mixed $item Review or reply being rendered.
* @return void
*/
protected function column_author( $item ) : void {
global $comment_status;
$author_url = $this->get_item_author_url();
$author_url_display = $this->get_item_author_url_for_display( $author_url );
if ( get_option( 'show_avatars' ) ) {
$author_avatar = get_avatar( $item, 32, 'mystery' );
} else {
$author_avatar = '';
}
ob_start();
echo '<strong>' . $author_avatar; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
comment_author();
echo '</strong><br>';
if ( ! empty( $author_url ) ) :
?>
<a title="<?php echo esc_attr( $author_url ); ?>" href="<?php echo esc_url( $author_url ); ?>" rel="noopener noreferrer"><?php echo esc_html( $author_url_display ); ?></a>
<br>
<?php
endif;
if ( $this->current_user_can_edit_review ) :
if ( ! empty( $item->comment_author_email ) && is_email( $item->comment_author_email ) ) :
?>
<a href="mailto:<?php echo esc_attr( $item->comment_author_email ); ?>"><?php echo esc_html( $item->comment_author_email ); ?></a><br>
<?php
endif;
$link = add_query_arg(
[
's' => urlencode( get_comment_author_IP( $item->comment_ID ) ),
'page' => Reviews::MENU_SLUG,
'mode' => 'detail',
],
'admin.php'
);
if ( 'spam' === $comment_status ) :
$link = add_query_arg( [ 'comment_status' => 'spam' ], $link );
endif;
?>
<a href="<?php echo esc_url( $link ); ?>"><?php comment_author_IP( $item->comment_ID ); ?></a>
<?php
endif;
echo $this->filter_column_output( 'author', ob_get_clean(), $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Gets the item author URL.
*
* @return string
*/
private function get_item_author_url() : string {
$author_url = get_comment_author_url();
$protocols = [ 'https://', 'http://' ];
if ( in_array( $author_url, $protocols ) ) {
$author_url = '';
}
return $author_url;
}
/**
* Gets the item author URL for display.
*
* @param string $author_url The review or reply author URL (raw).
* @return string
*/
private function get_item_author_url_for_display( $author_url ) : string {
$author_url_display = untrailingslashit( preg_replace( '|^http(s)?://(www\.)?|i', '', $author_url ) );
if ( strlen( $author_url_display ) > 50 ) {
$author_url_display = wp_html_excerpt( $author_url_display, 49, '…' );
}
return $author_url_display;
}
/**
* Renders the "submitted on" column.
*
* Note that the output is consistent with {@see WP_Comments_List_Table::column_date()}.
*
* @param WP_Comment|mixed $item Review or reply being rendered.
* @return void
*/
protected function column_date( $item ) : void {
$submitted = sprintf(
/* translators: 1 - Product review date, 2: Product review time. */
__( '%1$s at %2$s', 'woocommerce' ),
/* translators: Review date format. See https://www.php.net/manual/datetime.format.php */
get_comment_date( __( 'Y/m/d', 'woocommerce' ), $item ),
/* translators: Review time format. See https://www.php.net/manual/datetime.format.php */
get_comment_date( __( 'g:i a', 'woocommerce' ), $item )
);
ob_start();
?>
<div class="submitted-on">
<?php
if ( 'approved' === wp_get_comment_status( $item ) && ! empty( $item->comment_post_ID ) ) :
printf(
'<a href="%1$s">%2$s</a>',
esc_url( get_comment_link( $item ) ),
esc_html( $submitted )
);
else :
echo esc_html( $submitted );
endif;
?>
</div>
<?php
echo $this->filter_column_output( 'date', ob_get_clean(), $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Renders the product column.
*
* @see WP_Comments_List_Table::column_response() for consistency.
*
* @param WP_Comment|mixed $item Review or reply being rendered.
* @return void
*/
protected function column_response( $item ) : void {
$product_post = get_post();
ob_start();
if ( $product_post ) :
?>
<div class="response-links">
<?php
if ( current_user_can( 'edit_product', $product_post->ID ) ) :
$post_link = "<a href='" . esc_url( get_edit_post_link( $product_post->ID ) ) . "' class='comments-edit-item-link'>";
$post_link .= esc_html( get_the_title( $product_post->ID ) ) . '</a>';
else :
$post_link = esc_html( get_the_title( $product_post->ID ) );
endif;
echo $post_link; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$post_type_object = get_post_type_object( $product_post->post_type );
?>
<a href="<?php echo esc_url( get_permalink( $product_post->ID ) ); ?>" class="comments-view-item-link">
<?php echo esc_html( $post_type_object->labels->view_item ); ?>
</a>
<span class="post-com-count-wrapper post-com-count-<?php echo esc_attr( $product_post->ID ); ?>">
<?php $this->comments_bubble( $product_post->ID, get_pending_comments_num( $product_post->ID ) ); ?>
</span>
</div>
<?php
endif;
echo $this->filter_column_output( 'response', ob_get_clean(), $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Renders the type column.
*
* @param WP_Comment|mixed $item Review or reply being rendered.
* @return void
*/
protected function column_type( $item ) : void {
$type = 'review' === $item->comment_type
? '☆ ' . __( 'Review', 'woocommerce' )
: __( 'Reply', 'woocommerce' );
echo $this->filter_column_output( 'type', esc_html( $type ), $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Renders the rating column.
*
* @param WP_Comment|mixed $item Review or reply being rendered.
* @return void
*/
protected function column_rating( $item ) : void {
$rating = get_comment_meta( $item->comment_ID, 'rating', true );
ob_start();
if ( ! empty( $rating ) && is_numeric( $rating ) ) {
$rating = (int) $rating;
$accessibility_label = sprintf(
/* translators: 1: number representing a rating */
__( '%1$d out of 5', 'woocommerce' ),
$rating
);
$stars = str_repeat( '★', $rating );
$stars .= str_repeat( '☆', 5 - $rating );
?>
<span aria-label="<?php echo esc_attr( $accessibility_label ); ?>"><?php echo esc_html( $stars ); ?></span>
<?php
}
echo $this->filter_column_output( 'rating', ob_get_clean(), $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Renders any custom columns.
*
* @param WP_Comment|mixed $item Review or reply being rendered.
* @param string|mixed $column_name Name of the column being rendered.
* @return void
*/
protected function column_default( $item, $column_name ) : void {
ob_start();
/**
* Fires when the default column output is displayed for a single row.
*
* This action can be used to render custom columns that have been added.
*
* @since 6.7.0
*
* @param WP_Comment $item The review or reply being rendered.
*/
do_action( 'woocommerce_product_reviews_table_column_' . $column_name, $item );
echo $this->filter_column_output( $column_name, ob_get_clean(), $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Runs a filter hook for a given column content.
*
* @param string|mixed $column_name The column being output.
* @param string|mixed $output The output content (may include HTML).
* @param WP_Comment|mixed $item The review or reply being rendered.
* @return string
*/
protected function filter_column_output( $column_name, $output, $item ) : string {
/**
* Filters the output of a column.
*
* @since 6.7.0
*
* @param string $output The column output.
* @param WP_Comment $item The product review being rendered.
*/
return (string) apply_filters( 'woocommerce_product_reviews_table_column_' . $column_name . '_content', $output, $item );
}
/**
* Renders the extra controls to be displayed between bulk actions and pagination.
*
* @global string $comment_status
* @global string $comment_type
*
* @param string|mixed $which Position (top or bottom).
* @return void
*/
protected function extra_tablenav( $which ) : void {
global $comment_status, $comment_type;
echo '<div class="alignleft actions">';
if ( 'top' === $which ) {
ob_start();
echo '<input type="hidden" name="comment_status" value="' . esc_attr( $comment_status ?? 'all' ) . '" />';
$this->review_type_dropdown( $comment_type );
$this->review_rating_dropdown( $this->current_reviews_rating );
$this->product_search( $this->current_product_for_reviews );
echo ob_get_clean(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
submit_button( __( 'Filter', 'woocommerce' ), '', 'filter_action', false, [ 'id' => 'post-query-submit' ] );
}
if ( ( 'spam' === $comment_status || 'trash' === $comment_status ) && $this->has_items() && $this->current_user_can_moderate_reviews ) {
wp_nonce_field( 'bulk-destroy', '_destroy_nonce' );
$title = 'spam' === $comment_status
? esc_attr__( 'Empty Spam', 'woocommerce' )
: esc_attr__( 'Empty Trash', 'woocommerce' );
submit_button( $title, 'apply', 'delete_all', false );
}
echo '</div>';
}
/**
* Displays a review type drop-down for filtering reviews in the Product Reviews list table.
*
* @see WP_Comments_List_Table::comment_type_dropdown() for consistency.
*
* @param string|mixed $current_type The current comment item type slug.
* @return void
*/
protected function review_type_dropdown( $current_type ) : void {
/**
* Sets the possible options used in the Product Reviews List Table's filter-by-review-type
* selector.
*
* @since 7.0.0
*
* @param array Map of possible review types.
*/
$item_types = apply_filters(
'woocommerce_product_reviews_list_table_item_types',
array(
'all' => __( 'All types', 'woocommerce' ),
'comment' => __( 'Replies', 'woocommerce' ),
'review' => __( 'Reviews', 'woocommerce' ),
)
);
?>
<label class="screen-reader-text" for="filter-by-review-type"><?php esc_html_e( 'Filter by review type', 'woocommerce' ); ?></label>
<select id="filter-by-review-type" name="review_type">
<?php foreach ( $item_types as $type => $label ) : ?>
<option value="<?php echo esc_attr( $type ); ?>" <?php selected( $type, $current_type ); ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
<?php
}
/**
* Displays a review rating drop-down for filtering reviews in the Product Reviews list table.
*
* @param int|string|mixed $current_rating Rating to display reviews for.
* @return void
*/
public function review_rating_dropdown( $current_rating ) : void {
$rating_options = [
'0' => __( 'All ratings', 'woocommerce' ),
'1' => '★',
'2' => '★★',
'3' => '★★★',
'4' => '★★★★',
'5' => '★★★★★',
];
?>
<label class="screen-reader-text" for="filter-by-review-rating"><?php esc_html_e( 'Filter by review rating', 'woocommerce' ); ?></label>
<select id="filter-by-review-rating" name="review_rating">
<?php foreach ( $rating_options as $rating => $label ) : ?>
<?php
$title = 0 === (int) $rating
? $label
: sprintf(
/* translators: %s: Star rating (1-5). */
__( '%s-star rating', 'woocommerce' ),
$rating
);
?>
<option value="<?php echo esc_attr( $rating ); ?>" <?php selected( $rating, (string) $current_rating ); ?> title="<?php echo esc_attr( $title ); ?>"><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
<?php
}
/**
* Displays a product search input for filtering reviews by product in the Product Reviews list table.
*
* @param WC_Product|null $current_product The current product (or null when displaying all reviews).
* @return void
*/
protected function product_search( ?WC_Product $current_product ) : void {
?>
<label class="screen-reader-text" for="filter-by-product"><?php esc_html_e( 'Filter by product', 'woocommerce' ); ?></label>
<select
id="filter-by-product"
class="wc-product-search"
name="product_id"
style="width: 200px;"
data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>"
data-action="woocommerce_json_search_products"
data-allow_clear="true">
<?php if ( $current_product instanceof WC_Product ) : ?>
<option value="<?php echo esc_attr( $current_product->get_id() ); ?>" selected="selected"><?php echo esc_html( $current_product->get_formatted_name() ); ?></option>
<?php endif; ?>
</select>
<?php
}
/**
* Displays a review count bubble.
*
* Based on {@see WP_List_Table::comments_bubble()}, but overridden, so we can customize the URL and text output.
*
* @param int|mixed $post_id The product ID.
* @param int|mixed $pending_comments Number of pending reviews.
*
* @return void
*/
protected function comments_bubble( $post_id, $pending_comments ) : void {
$approved_review_count = get_comments_number();
$approved_reviews_number = number_format_i18n( $approved_review_count );
$pending_reviews_number = number_format_i18n( $pending_comments );
$approved_only_phrase = sprintf(
/* translators: %s: Number of reviews. */
_n( '%s review', '%s reviews', $approved_review_count, 'woocommerce' ),
$approved_reviews_number
);
$approved_phrase = sprintf(
/* translators: %s: Number of reviews. */
_n( '%s approved review', '%s approved reviews', $approved_review_count, 'woocommerce' ),
$approved_reviews_number
);
$pending_phrase = sprintf(
/* translators: %s: Number of reviews. */
_n( '%s pending review', '%s pending reviews', $pending_comments, 'woocommerce' ),
$pending_reviews_number
);
if ( ! $approved_review_count && ! $pending_comments ) {
// No reviews at all.
printf(
'<span aria-hidden="true">—</span><span class="screen-reader-text">%s</span>',
esc_html__( 'No reviews', 'woocommerce' )
);
} elseif ( $approved_review_count && 'trash' === get_post_status( $post_id ) ) {
// Don't link the comment bubble for a trashed product.
printf(
'<span class="post-com-count post-com-count-approved"><span class="comment-count-approved" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></span>',
esc_html( $approved_reviews_number ),
$pending_comments ? esc_html( $approved_phrase ) : esc_html( $approved_only_phrase )
);
} elseif ( $approved_review_count ) {
// Link the comment bubble to approved reviews.
printf(
'<a href="%s" class="post-com-count post-com-count-approved"><span class="comment-count-approved" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
esc_url(
add_query_arg(
[
'product_id' => urlencode( $post_id ),
'comment_status' => 'approved',
],
Reviews::get_reviews_page_url()
)
),
esc_html( $approved_reviews_number ),
$pending_comments ? esc_html( $approved_phrase ) : esc_html( $approved_only_phrase )
);
} else {
// Don't link the comment bubble when there are no approved reviews.
printf(
'<span class="post-com-count post-com-count-no-comments"><span class="comment-count comment-count-no-comments" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></span>',
esc_html( $approved_reviews_number ),
$pending_comments ? esc_html__( 'No approved reviews', 'woocommerce' ) : esc_html__( 'No reviews', 'woocommerce' )
);
}
if ( $pending_comments ) {
printf(
'<a href="%s" class="post-com-count post-com-count-pending"><span class="comment-count-pending" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
esc_url(
add_query_arg(
[
'product_id' => urlencode( $post_id ),
'comment_status' => 'moderated',
],
Reviews::get_reviews_page_url()
)
),
esc_html( $pending_reviews_number ),
esc_html( $pending_phrase )
);
} else {
printf(
'<span class="post-com-count post-com-count-pending post-com-count-no-pending"><span class="comment-count comment-count-no-pending" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></span>',
esc_html( $pending_reviews_number ),
$approved_review_count ? esc_html__( 'No pending reviews', 'woocommerce' ) : esc_html__( 'No reviews', 'woocommerce' )
);
}
}
}
ProductReviews/ReviewsUtil.php 0000644 00000001535 15154512352 0012533 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\ProductReviews;
/**
* A utility class for handling comments that are product reviews.
*/
class ReviewsUtil {
/**
* Removes product reviews from the edit-comments page to fix the "Mine" tab counter.
*
* @param array|mixed $clauses A compacted array of comment query clauses.
* @return array|mixed
*/
public static function comments_clauses_without_product_reviews( $clauses ) {
global $wpdb, $current_screen;
if ( isset( $current_screen->base ) && 'edit-comments' === $current_screen->base ) {
$clauses['join'] .= " LEFT JOIN {$wpdb->posts} AS wp_posts_to_exclude_reviews ON comment_post_ID = wp_posts_to_exclude_reviews.ID ";
$clauses['where'] .= ( $clauses['where'] ? ' AND ' : '' ) . " wp_posts_to_exclude_reviews.post_type NOT IN ('product') ";
}
return $clauses;
}
}
RemoteFreeExtensions/DefaultFreeExtensions.php 0000644 00000100530 15154512352 0015642 0 ustar 00 <?php
/**
* Gets a list of fallback methods if remote fetching is disabled.
*/
namespace Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\DefaultPaymentGateways;
defined( 'ABSPATH' ) || exit;
/**
* Default Free Extensions
*/
class DefaultFreeExtensions {
/**
* Get default specs.
*
* @return array Default specs.
*/
public static function get_all() {
$bundles = array(
array(
'key' => 'obw/basics',
'title' => __( 'Get the basics', 'woocommerce' ),
'plugins' => array(
self::get_plugin( 'woocommerce-payments' ),
self::get_plugin( 'woocommerce-services:shipping' ),
self::get_plugin( 'woocommerce-services:tax' ),
self::get_plugin( 'jetpack' ),
),
),
array(
'key' => 'obw/grow',
'title' => __( 'Grow your store', 'woocommerce' ),
'plugins' => array(
self::get_plugin( 'mailpoet' ),
self::get_plugin( 'codistoconnect' ),
self::get_plugin( 'google-listings-and-ads' ),
self::get_plugin( 'pinterest-for-woocommerce' ),
self::get_plugin( 'facebook-for-woocommerce' ),
),
),
array(
'key' => 'task-list/reach',
'title' => __( 'Reach out to customers', 'woocommerce' ),
'plugins' => array(
self::get_plugin( 'mailpoet:alt' ),
self::get_plugin( 'mailchimp-for-woocommerce' ),
self::get_plugin( 'klaviyo' ),
self::get_plugin( 'creative-mail-by-constant-contact' ),
),
),
array(
'key' => 'task-list/grow',
'title' => __( 'Grow your store', 'woocommerce' ),
'plugins' => array(
self::get_plugin( 'google-listings-and-ads:alt' ),
self::get_plugin( 'tiktok-for-business' ),
self::get_plugin( 'pinterest-for-woocommerce:alt' ),
self::get_plugin( 'facebook-for-woocommerce:alt' ),
self::get_plugin( 'codistoconnect:alt' ),
),
),
array(
'key' => 'obw/core-profiler',
'title' => __( 'Grow your store', 'woocommerce' ),
'plugins' => self::with_core_profiler_fields(
array(
self::get_plugin( 'woocommerce-payments' ),
self::get_plugin( 'woocommerce-services:shipping' ),
self::get_plugin( 'jetpack' ),
self::get_plugin( 'pinterest-for-woocommerce' ),
self::get_plugin( 'mailpoet' ),
self::get_plugin( 'google-listings-and-ads' ),
self::get_plugin( 'woocommerce-services:tax' ),
)
),
),
);
$bundles = wp_json_encode( $bundles );
return json_decode( $bundles );
}
/**
* Get the plugin arguments by slug.
*
* @param string $slug Slug.
* @return array
*/
public static function get_plugin( $slug ) {
$plugins = array(
'google-listings-and-ads' => array(
'min_php_version' => '7.4',
'name' => __( 'Google Listings & Ads', 'woocommerce' ),
'description' => sprintf(
/* translators: 1: opening product link tag. 2: closing link tag */
__( 'Drive sales with %1$sGoogle Listings and Ads%2$s', 'woocommerce' ),
'<a href="https://woocommerce.com/products/google-listings-and-ads" target="_blank">',
'</a>'
),
'image_url' => plugins_url( '/assets/images/onboarding/google.svg', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=wc-admin&path=%2Fgoogle%2Fstart',
'is_built_by_wc' => true,
'is_visible' => array(
array(
'type' => 'not',
'operand' => array(
array(
'type' => 'plugins_activated',
'plugins' => array( 'google-listings-and-ads' ),
),
),
),
),
),
'google-listings-and-ads:alt' => array(
'name' => __( 'Google Listings & Ads', 'woocommerce' ),
'description' => __( 'Reach more shoppers and drive sales for your store. Integrate with Google to list your products for free and launch paid ad campaigns.', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/google.svg', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=wc-admin&path=%2Fgoogle%2Fstart',
'is_built_by_wc' => true,
),
'facebook-for-woocommerce' => array(
'name' => __( 'Facebook for WooCommerce', 'woocommerce' ),
'description' => __( 'List products and create ads on Facebook and Instagram with <a href="https://woocommerce.com/products/facebook/">Facebook for WooCommerce</a>', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/facebook.png', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=wc-facebook',
'is_visible' => false,
'is_built_by_wc' => false,
),
'facebook-for-woocommerce:alt' => array(
'name' => __( 'Facebook for WooCommerce', 'woocommerce' ),
'description' => __( 'List products and create ads on Facebook and Instagram.', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/facebook.png', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=wc-facebook',
'is_visible' => false,
'is_built_by_wc' => false,
),
'pinterest-for-woocommerce' => array(
'name' => __( 'Pinterest for WooCommerce', 'woocommerce' ),
'description' => __( 'Get your products in front of Pinners searching for ideas and things to buy.', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/pinterest.png', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=wc-admin&path=%2Fpinterest%2Flanding',
'is_built_by_wc' => true,
'min_php_version' => '7.3',
),
'pinterest-for-woocommerce:alt' => array(
'name' => __( 'Pinterest for WooCommerce', 'woocommerce' ),
'description' => __( 'Get your products in front of Pinterest users searching for ideas and things to buy. Get started with Pinterest and make your entire product catalog browsable.', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/pinterest.png', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=wc-admin&path=%2Fpinterest%2Flanding',
'is_built_by_wc' => true,
),
'mailpoet' => array(
'name' => __( 'MailPoet', 'woocommerce' ),
'description' => __( 'Create and send purchase follow-up emails, newsletters, and promotional campaigns straight from your dashboard.', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/mailpoet.png', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=mailpoet-newsletters',
'is_built_by_wc' => true,
),
'mailchimp-for-woocommerce' => array(
'name' => __( 'Mailchimp', 'woocommerce' ),
'description' => __( 'Send targeted campaigns, recover abandoned carts and much more with Mailchimp.', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/mailchimp-for-woocommerce.png', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=mailchimp-woocommerce',
'is_built_by_wc' => false,
),
'klaviyo' => array(
'name' => __( 'Klaviyo', 'woocommerce' ),
'description' => __( 'Grow and retain customers with intelligent, impactful email and SMS marketing automation and a consolidated view of customer interactions.', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/klaviyo.png', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=klaviyo_settings',
'is_built_by_wc' => false,
),
'creative-mail-by-constant-contact' => array(
'name' => __( 'Creative Mail for WooCommerce', 'woocommerce' ),
'description' => __( 'Create on-brand store campaigns, fast email promotions and customer retargeting with Creative Mail.', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/creative-mail-by-constant-contact.png', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=creativemail',
'is_built_by_wc' => false,
),
'codistoconnect' => array(
'name' => __( 'Codisto for WooCommerce', 'woocommerce' ),
'description' => sprintf(
/* translators: 1: opening product link tag. 2: closing link tag */
__( 'Sell on Amazon, eBay, Walmart and more directly from WooCommerce with %1$sCodisto%2$s', 'woocommerce' ),
'<a href="https://woocommerce.com/pt-br/products/amazon-ebay-integration/?quid=c247a85321c9e93e7c3c6f1eb072e6e5" target="_blank">',
'</a>'
),
'image_url' => plugins_url( '/assets/images/onboarding/codistoconnect.png', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=codisto-settings',
'is_built_by_wc' => true,
),
'codistoconnect:alt' => array(
'name' => __( 'Codisto for WooCommerce', 'woocommerce' ),
'description' => __( 'Sell on Amazon, eBay, Walmart and more directly from WooCommerce.', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/codistoconnect.png', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=codisto-settings',
'is_built_by_wc' => true,
),
'woocommerce-payments' => array(
'name' => __( 'WooPayments', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/wcpay.svg', WC_PLUGIN_FILE ),
'description' => sprintf(
/* translators: 1: opening product link tag. 2: closing link tag */
__( 'Accept credit cards and other popular payment methods with %1$sWooPayments%2$s', 'woocommerce' ),
'<a href="https://woocommerce.com/products/woocommerce-payments" target="_blank">',
'</a>'
),
'is_visible' => array(
array(
'type' => 'or',
'operands' => array(
array(
'type' => 'base_location_country',
'value' => 'US',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'PR',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'AU',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'CA',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'DE',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'ES',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'FR',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'GB',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'IE',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'IT',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'NZ',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'AT',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'BE',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'NL',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'PL',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'PT',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'CH',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'HK',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'SG',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'CY',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'DK',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'EE',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'FI',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'GR',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'LU',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'LT',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'LV',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'NO',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'MT',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'SI',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'SK',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'BG',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'CZ',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'HR',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'HU',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'RO',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'SE',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'JP',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'AE',
'operation' => '=',
),
),
),
DefaultPaymentGateways::get_rules_for_cbd( false ),
),
'is_built_by_wc' => true,
'min_wp_version' => '5.9',
),
'woocommerce-services:shipping' => array(
'name' => __( 'WooCommerce Shipping', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/woo.svg', WC_PLUGIN_FILE ),
'description' => sprintf(
/* translators: 1: opening product link tag. 2: closing link tag */
__( 'Print shipping labels with %1$sWooCommerce Shipping%2$s', 'woocommerce' ),
'<a href="https://woocommerce.com/products/shipping" target="_blank">',
'</a>'
),
'is_visible' => array(
array(
'type' => 'base_location_country',
'value' => 'US',
'operation' => '=',
),
array(
'type' => 'not',
'operand' => array(
array(
'type' => 'plugins_activated',
'plugins' => array( 'woocommerce-services' ),
),
),
),
array(
'type' => 'or',
'operands' => array(
array(
array(
'type' => 'option',
'transformers' => array(
array(
'use' => 'dot_notation',
'arguments' => array(
'path' => 'product_types',
),
),
array(
'use' => 'count',
),
),
'option_name' => 'woocommerce_onboarding_profile',
'value' => 1,
'default' => array(),
'operation' => '!=',
),
),
array(
array(
'type' => 'option',
'transformers' => array(
array(
'use' => 'dot_notation',
'arguments' => array(
'path' => 'product_types.0',
),
),
),
'option_name' => 'woocommerce_onboarding_profile',
'value' => 'downloads',
'default' => '',
'operation' => '!=',
),
),
),
),
),
'is_built_by_wc' => true,
),
'woocommerce-services:tax' => array(
'name' => __( 'WooCommerce Tax', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/woo.svg', WC_PLUGIN_FILE ),
'description' => sprintf(
/* translators: 1: opening product link tag. 2: closing link tag */
__( 'Get automated sales tax with %1$sWooCommerce Tax%2$s', 'woocommerce' ),
'<a href="https://woocommerce.com/products/tax" target="_blank">',
'</a>'
),
'is_visible' => array(
array(
'type' => 'or',
'operands' => array(
array(
'type' => 'base_location_country',
'value' => 'US',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'FR',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'GB',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'DE',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'CA',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'AU',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'GR',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'BE',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'PT',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'DK',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'SE',
'operation' => '=',
),
),
),
array(
'type' => 'not',
'operand' => array(
array(
'type' => 'plugins_activated',
'plugins' => array( 'woocommerce-services' ),
),
),
),
),
'is_built_by_wc' => true,
),
'jetpack' => array(
'name' => __( 'Jetpack', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/jetpack.svg', WC_PLUGIN_FILE ),
'description' => sprintf(
/* translators: 1: opening product link tag. 2: closing link tag */
__( 'Enhance speed and security with %1$sJetpack%2$s', 'woocommerce' ),
'<a href="https://woocommerce.com/products/jetpack" target="_blank">',
'</a>'
),
'is_visible' => array(
array(
'type' => 'not',
'operand' => array(
array(
'type' => 'plugins_activated',
'plugins' => array( 'jetpack' ),
),
),
),
),
'is_built_by_wc' => false,
'min_wp_version' => '6.0',
),
'mailpoet' => array(
'name' => __( 'MailPoet', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/mailpoet.png', WC_PLUGIN_FILE ),
'description' => sprintf(
/* translators: 1: opening product link tag. 2: closing link tag */
__( 'Level up your email marketing with %1$sMailPoet%2$s', 'woocommerce' ),
'<a href="https://woocommerce.com/products/mailpoet" target="_blank">',
'</a>'
),
'manage_url' => 'admin.php?page=mailpoet-newsletters',
'is_visible' => array(
array(
'type' => 'not',
'operand' => array(
array(
'type' => 'plugins_activated',
'plugins' => array( 'mailpoet' ),
),
),
),
),
'is_built_by_wc' => true,
),
'mailpoet:alt' => array(
'name' => __( 'MailPoet', 'woocommerce' ),
'description' => __( 'Create and send purchase follow-up emails, newsletters, and promotional campaigns straight from your dashboard.', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/mailpoet.png', WC_PLUGIN_FILE ),
'manage_url' => 'admin.php?page=mailpoet-newsletters',
'is_built_by_wc' => true,
),
'tiktok-for-business' => array(
'name' => __( 'TikTok for WooCommerce', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/tiktok.svg', WC_PLUGIN_FILE ),
'description' =>
__( 'Grow your online sales by promoting your products on TikTok to over one billion monthly active users around the world.', 'woocommerce' ),
'manage_url' => 'admin.php?page=tiktok',
'is_visible' => array(
array(
'type' => 'or',
'operands' => array(
array(
'type' => 'base_location_country',
'value' => 'US',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'CA',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'MX',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'AT',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'BE',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'CZ',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'DK',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'FI',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'FR',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'DE',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'GR',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'HU',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'IE',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'IT',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'NL',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'PL',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'PT',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'RO',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'ES',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'SE',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'GB',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'CH',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'NO',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'AU',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'NZ',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'SG',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'MY',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'PH',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'ID',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'VN',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'TH',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'KR',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'IL',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'AE',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'RU',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'UA',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'TR',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'SA',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'BR',
'operation' => '=',
),
array(
'type' => 'base_location_country',
'value' => 'JP',
'operation' => '=',
),
),
),
),
'is_built_by_wc' => false,
),
'tiktok-for-business:alt' => array(
'name' => __( 'TikTok for WooCommerce', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/onboarding/tiktok.svg', WC_PLUGIN_FILE ),
'description' => sprintf(
/* translators: 1: opening product link tag. 2: closing link tag */
__( 'Create ad campaigns and reach one billion global users with %1$sTikTok for WooCommerce%2$s', 'woocommerce' ),
'<a href="https://woocommerce.com/products/tiktok-for-woocommerce" target="_blank">',
'</a>'
),
'manage_url' => 'admin.php?page=tiktok',
'is_built_by_wc' => false,
'is_visible' => false,
),
);
$plugin = $plugins[ $slug ];
$plugin['key'] = $slug;
return $plugin;
}
/**
* Decorate plugin data with core profiler fields.
*
* - Updated description for the core-profiler.
* - Adds learn_more_link and label.
* - Adds install_priority, which is used to sort the plugins. The value is determined by the plugin size. Lower = smaller.
*
* @param array $plugins Array of plugins.
*
* @return array
*/
public static function with_core_profiler_fields( array $plugins ) {
$_plugins = array(
'woocommerce-payments' => array(
'label' => __( 'Get paid with WooPayments', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/core-profiler/logo-woo.svg', WC_PLUGIN_FILE ),
'description' => __( "Securely accept payments and manage payment activity straight from your store's dashboard", 'woocommerce' ),
'learn_more_link' => 'https://woocommerce.com/products/woocommerce-payments',
'install_priority' => 5,
),
'woocommerce-services:shipping' => array(
'label' => __( 'Print shipping labels with WooCommerce Shipping', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/core-profiler/logo-woo.svg', WC_PLUGIN_FILE ),
'description' => __( 'Print USPS and DHL labels directly from your dashboard and save on shipping.', 'woocommerce' ),
'learn_more_link' => 'https://woocommerce.com/woocommerce-shipping',
'install_priority' => 3,
),
'jetpack' => array(
'label' => __( 'Boost content creation with Jetpack AI Assistant', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/core-profiler/logo-jetpack.svg', WC_PLUGIN_FILE ),
'description' => __( 'Save time on content creation — unlock high-quality blog posts and pages using AI.', 'woocommerce' ),
'learn_more_link' => 'https://woocommerce.com/products/jetpack',
'install_priority' => 8,
),
'pinterest-for-woocommerce' => array(
'label' => __( 'Showcase your products with Pinterest', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/core-profiler/logo-pinterest.svg', WC_PLUGIN_FILE ),
'description' => __( 'Get your products in front of a highly engaged audience.', 'woocommerce' ),
'learn_more_link' => 'https://woocommerce.com/products/pinterest-for-woocommerce',
'install_priority' => 2,
),
'mailpoet' => array(
'label' => __( 'Reach your customers with MailPoet', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/core-profiler/logo-mailpoet.svg', WC_PLUGIN_FILE ),
'description' => __( 'Send purchase follow-up emails, newsletters, and promotional campaigns.', 'woocommerce' ),
'learn_more_link' => 'https://woocommerce.com/products/mailpoet',
'install_priority' => 7,
),
'tiktok-for-business' => array(
'label' => __( 'Create ad campaigns with TikTok', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/core-profiler/logo-tiktok.svg', WC_PLUGIN_FILE ),
'description' => __( 'Create advertising campaigns and reach one billion global users.', 'woocommerce' ),
'learn_more_link' => 'https://woocommerce.com/products/tiktok-for-woocommerce',
'install_priority' => 1,
),
'google-listings-and-ads' => array(
'label' => __( 'Drive sales with Google Listings & Ads', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/core-profiler/logo-google.svg', WC_PLUGIN_FILE ),
'description' => __( 'Reach millions of active shoppers across Google with free product listings and ads.', 'woocommerce' ),
'learn_more_link' => 'https://woocommerce.com/products/google-listings-and-ads',
'install_priority' => 6,
),
'woocommerce-services:tax' => array(
'label' => __( 'Get automated tax rates with WooCommerce Tax', 'woocommerce' ),
'image_url' => plugins_url( '/assets/images/core-profiler/logo-woo.svg', WC_PLUGIN_FILE ),
'description' => __( 'Automatically calculate how much sales tax should be collected – by city, country, or state.', 'woocommerce' ),
'learn_more_link' => 'https://woocommerce.com/products/tax',
'install_priority' => 4,
),
);
// Copy shipping for the core-profiler and remove is_visible conditions, except for the country restriction.
$_plugins['woocommerce-services:shipping']['is_visible'] = [
array(
'type' => 'base_location_country',
'value' => 'US',
'operation' => '=',
),
];
$remove_plugins_activated_rule = function( $is_visible ) {
$is_visible = array_filter(
array_map(
function( $rule ) {
if ( is_object( $rule ) || ! isset( $rule['operand'] ) ) {
return $rule;
}
return array_filter(
$rule['operand'],
function( $operand ) {
return 'plugins_activated' !== $operand['type'];
}
);
},
$is_visible
)
);
return empty( $is_visible ) ? true : $is_visible;
};
foreach ( $plugins as &$plugin ) {
if ( isset( $_plugins[ $plugin['key'] ] ) ) {
$plugin = array_merge( $plugin, $_plugins[ $plugin['key'] ] );
if ( isset( $plugin['is_visible'] ) && is_array( $plugin['is_visible'] ) ) {
$plugin['is_visible'] = $remove_plugins_activated_rule( $plugin['is_visible'] );
}
}
}
return $plugins;
}
}
RemoteFreeExtensions/EvaluateExtension.php 0000644 00000003136 15154512353 0015044 0 ustar 00 <?php
/**
* Evaluates the spec and returns a status.
*/
namespace Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\PluginsHelper;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RuleEvaluator;
/**
* Evaluates the extension and returns it.
*/
class EvaluateExtension {
/**
* Evaluates the extension and returns it.
*
* @param object $extension The extension to evaluate.
* @return object The evaluated extension.
*/
public static function evaluate( $extension ) {
global $wp_version;
$rule_evaluator = new RuleEvaluator();
if ( isset( $extension->is_visible ) ) {
$is_visible = $rule_evaluator->evaluate( $extension->is_visible );
$extension->is_visible = $is_visible;
} else {
$extension->is_visible = true;
}
// Run PHP and WP version chcecks.
if ( true === $extension->is_visible ) {
if ( isset( $extension->min_php_version ) && ! version_compare( PHP_VERSION, $extension->min_php_version, '>=' ) ) {
$extension->is_visible = false;
}
if ( isset( $extension->min_wp_version ) && ! version_compare( $wp_version, $extension->min_wp_version, '>=' ) ) {
$extension->is_visible = false;
}
}
$installed_plugins = PluginsHelper::get_installed_plugin_slugs();
$activated_plugins = PluginsHelper::get_active_plugin_slugs();
$extension->is_installed = in_array( explode( ':', $extension->key )[0], $installed_plugins, true );
$extension->is_activated = in_array( explode( ':', $extension->key )[0], $activated_plugins, true );
return $extension;
}
}
RemoteFreeExtensions/Init.php 0000644 00000004004 15154512353 0012277 0 ustar 00 <?php
/**
* Handles running payment method specs
*/
namespace Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\DefaultFreeExtensions;
/**
* Remote Payment Methods engine.
* This goes through the specs and gets eligible payment methods.
*/
class Init {
/**
* Constructor.
*/
public function __construct() {
add_action( 'woocommerce_updated', array( __CLASS__, 'delete_specs_transient' ) );
}
/**
* Go through the specs and run them.
*
* @param array $allowed_bundles Optional array of allowed bundles to be returned.
* @return array
*/
public static function get_extensions( $allowed_bundles = array() ) {
$bundles = array();
$specs = self::get_specs();
foreach ( $specs as $spec ) {
$spec = (object) $spec;
$bundle = (array) $spec;
$bundle['plugins'] = array();
if ( ! empty( $allowed_bundles ) && ! in_array( $spec->key, $allowed_bundles, true ) ) {
continue;
}
foreach ( $spec->plugins as $plugin ) {
$extension = EvaluateExtension::evaluate( (object) $plugin );
if ( ! property_exists( $extension, 'is_visible' ) || $extension->is_visible ) {
$bundle['plugins'][] = $extension;
}
}
$bundles[] = $bundle;
}
return $bundles;
}
/**
* Delete the specs transient.
*/
public static function delete_specs_transient() {
RemoteFreeExtensionsDataSourcePoller::get_instance()->delete_specs_transient();
}
/**
* Get specs or fetch remotely if they don't exist.
*/
public static function get_specs() {
if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
return DefaultFreeExtensions::get_all();
}
$specs = RemoteFreeExtensionsDataSourcePoller::get_instance()->get_specs_from_data_sources();
// Fetch specs if they don't yet exist.
if ( false === $specs || ! is_array( $specs ) || 0 === count( $specs ) ) {
return DefaultFreeExtensions::get_all();
}
return $specs;
}
}
RemoteFreeExtensions/RemoteFreeExtensionsDataSourcePoller.php 0000644 00000001371 15154512353 0020646 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions;
/**
* Specs data source poller class for remote free extensions.
*/
class RemoteFreeExtensionsDataSourcePoller extends \Automattic\WooCommerce\Admin\DataSourcePoller {
const ID = 'remote_free_extensions';
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/obw-free-extensions/3.0/extensions.json',
);
/**
* Class instance.
*
* @var Analytics instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self(
self::ID,
self::DATA_SOURCES,
array(
'spec_key' => 'key',
)
);
}
return self::$instance;
}
}
RemoteInboxNotifications.php 0000644 00000001644 15154512353 0012253 0 ustar 00 <?php
/**
* Remote Inbox Notifications feature.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RemoteInboxNotificationsEngine;
/**
* Remote Inbox Notifications feature logic.
*/
class RemoteInboxNotifications {
/**
* Option name used to toggle this feature.
*/
const TOGGLE_OPTION_NAME = 'woocommerce_show_marketplace_suggestions';
/**
* Class instance.
*
* @var RemoteInboxNotifications instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WooCommerce.
*/
public function __construct() {
if ( Features::is_enabled( 'remote-inbox-notifications' ) ) {
RemoteInboxNotificationsEngine::init();
}
}
}
Schedulers/CustomersScheduler.php 0000644 00000007055 15154512353 0013214 0 ustar 00 <?php
/**
* Customer syncing related functions and actions.
*/
namespace Automattic\WooCommerce\Internal\Admin\Schedulers;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
use Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
/**
* CustomersScheduler Class.
*/
class CustomersScheduler extends ImportScheduler {
/**
* Slug to identify the scheduler.
*
* @var string
*/
public static $name = 'customers';
/**
* Attach customer lookup update hooks.
*
* @internal
*/
public static function init() {
CustomersDataStore::init();
parent::init();
}
/**
* Add customer dependencies.
*
* @internal
* @return array
*/
public static function get_dependencies() {
return array(
'delete_batch_init' => OrdersScheduler::get_action( 'delete_batch_init' ),
);
}
/**
* Get the customer IDs and total count that need to be synced.
*
* @internal
* @param int $limit Number of records to retrieve.
* @param int $page Page number.
* @param int|bool $days Number of days prior to current date to limit search results.
* @param bool $skip_existing Skip already imported customers.
*/
public static function get_items( $limit = 10, $page = 1, $days = false, $skip_existing = false ) {
$customer_roles = apply_filters( 'woocommerce_analytics_import_customer_roles', array( 'customer' ) );
$query_args = array(
'fields' => 'ID',
'orderby' => 'ID',
'order' => 'ASC',
'number' => $limit,
'paged' => $page,
'role__in' => $customer_roles,
);
if ( is_int( $days ) ) {
$query_args['date_query'] = array(
'after' => gmdate( 'Y-m-d 00:00:00', time() - ( DAY_IN_SECONDS * $days ) ),
);
}
if ( $skip_existing ) {
add_action( 'pre_user_query', array( __CLASS__, 'exclude_existing_customers_from_query' ) );
}
$customer_query = new \WP_User_Query( $query_args );
remove_action( 'pre_user_query', array( __CLASS__, 'exclude_existing_customers_from_query' ) );
return (object) array(
'total' => $customer_query->get_total(),
'ids' => $customer_query->get_results(),
);
}
/**
* Exclude users that already exist in our customer lookup table.
*
* Meant to be hooked into 'pre_user_query' action.
*
* @internal
* @param WP_User_Query $wp_user_query WP_User_Query to modify.
*/
public static function exclude_existing_customers_from_query( $wp_user_query ) {
global $wpdb;
$wp_user_query->query_where .= " AND NOT EXISTS (
SELECT ID FROM {$wpdb->prefix}wc_customer_lookup
WHERE {$wpdb->prefix}wc_customer_lookup.user_id = {$wpdb->users}.ID
)";
}
/**
* Get total number of rows imported.
*
* @internal
* @return int
*/
public static function get_total_imported() {
global $wpdb;
return $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}wc_customer_lookup" );
}
/**
* Imports a single customer.
*
* @internal
* @param int $user_id User ID.
* @return void
*/
public static function import( $user_id ) {
CustomersDataStore::update_registered_customer( $user_id );
}
/**
* Delete a batch of customers.
*
* @internal
* @param int $batch_size Number of items to delete.
* @return void
*/
public static function delete( $batch_size ) {
global $wpdb;
$customer_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT customer_id FROM {$wpdb->prefix}wc_customer_lookup ORDER BY customer_id ASC LIMIT %d",
$batch_size
)
);
foreach ( $customer_ids as $customer_id ) {
CustomersDataStore::delete_customer( $customer_id );
}
}
}
Schedulers/ImportInterface.php 0000644 00000001306 15154512353 0012455 0 ustar 00 <?php
/**
* Import related abstract functions.
*/
namespace Automattic\WooCommerce\Internal\Admin\Schedulers;
interface ImportInterface {
/**
* Get items based on query and return IDs along with total available.
*
* @internal
* @param int $limit Number of records to retrieve.
* @param int $page Page number.
* @param int|bool $days Number of days prior to current date to limit search results.
* @param bool $skip_existing Skip already imported items.
*/
public static function get_items( $limit, $page, $days, $skip_existing );
/**
* Get total number of items already imported.
*
* @internal
* @return null
*/
public static function get_total_imported();
}
Schedulers/ImportScheduler.php 0000644 00000011457 15154512353 0012503 0 ustar 00 <?php
/**
* Import related functions and actions.
*/
namespace Automattic\WooCommerce\Internal\Admin\Schedulers;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
use Automattic\WooCommerce\Admin\Schedulers\SchedulerTraits;
/**
* ImportScheduler class.
*/
abstract class ImportScheduler implements ImportInterface {
/**
* Import stats option name.
*/
const IMPORT_STATS_OPTION = 'woocommerce_admin_import_stats';
/**
* Scheduler traits.
*/
use SchedulerTraits {
get_batch_sizes as get_scheduler_batch_sizes;
}
/**
* Returns true if an import is in progress.
*
* @internal
* @return bool
*/
public static function is_importing() {
$pending_jobs = self::queue()->search(
array(
'status' => 'pending',
'per_page' => 1,
'claimed' => false,
'search' => 'import',
'group' => self::$group,
)
);
if ( empty( $pending_jobs ) ) {
$in_progress = self::queue()->search(
array(
'status' => 'in-progress',
'per_page' => 1,
'search' => 'import',
'group' => self::$group,
)
);
}
return ! empty( $pending_jobs ) || ! empty( $in_progress );
}
/**
* Get batch sizes.
*
* @internal
* @retun array
*/
public static function get_batch_sizes() {
return array_merge(
self::get_scheduler_batch_sizes(),
array(
'delete' => 10,
'import' => 25,
'queue' => 100,
)
);
}
/**
* Get all available scheduling actions.
* Used to determine action hook names and clear events.
*
* @internal
* @return array
*/
public static function get_scheduler_actions() {
return array(
'import_batch_init' => 'wc-admin_import_batch_init_' . static::$name,
'import_batch' => 'wc-admin_import_batch_' . static::$name,
'delete_batch_init' => 'wc-admin_delete_batch_init_' . static::$name,
'delete_batch' => 'wc-admin_delete_batch_' . static::$name,
'import' => 'wc-admin_import_' . static::$name,
);
}
/**
* Queue the imports into multiple batches.
*
* @internal
* @param integer|boolean $days Number of days to import.
* @param boolean $skip_existing Skip exisiting records.
*/
public static function import_batch_init( $days, $skip_existing ) {
$batch_size = static::get_batch_size( 'import' );
$items = static::get_items( 1, 1, $days, $skip_existing );
if ( 0 === $items->total ) {
return;
}
$num_batches = ceil( $items->total / $batch_size );
self::queue_batches( 1, $num_batches, 'import_batch', array( $days, $skip_existing ) );
}
/**
* Imports a batch of items to update.
*
* @internal
* @param int $batch_number Batch number to import (essentially a query page number).
* @param int|bool $days Number of days to import.
* @param bool $skip_existing Skip exisiting records.
* @return void
*/
public static function import_batch( $batch_number, $days, $skip_existing ) {
$batch_size = static::get_batch_size( 'import' );
$properties = array(
'batch_number' => $batch_number,
'batch_size' => $batch_size,
'type' => static::$name,
);
wc_admin_record_tracks_event( 'import_job_start', $properties );
// When we are skipping already imported items, the table of items to import gets smaller in
// every batch, so we want to always import the first page.
$page = $skip_existing ? 1 : $batch_number;
$items = static::get_items( $batch_size, $page, $days, $skip_existing );
foreach ( $items->ids as $id ) {
static::import( $id );
}
$import_stats = get_option( self::IMPORT_STATS_OPTION, array() );
$imported_count = absint( $import_stats[ static::$name ]['imported'] ) + count( $items->ids );
$import_stats[ static::$name ]['imported'] = $imported_count;
update_option( self::IMPORT_STATS_OPTION, $import_stats );
$properties['imported_count'] = $imported_count;
wc_admin_record_tracks_event( 'import_job_complete', $properties );
}
/**
* Queue item deletion in batches.
*
* @internal
*/
public static function delete_batch_init() {
global $wpdb;
$batch_size = static::get_batch_size( 'delete' );
$count = static::get_total_imported();
if ( 0 === $count ) {
return;
}
$num_batches = ceil( $count / $batch_size );
self::queue_batches( 1, $num_batches, 'delete_batch' );
}
/**
* Delete a batch by passing the count to be deleted to the child delete method.
*
* @internal
* @return void
*/
public static function delete_batch() {
wc_admin_record_tracks_event( 'delete_import_data_job_start', array( 'type' => static::$name ) );
$batch_size = static::get_batch_size( 'delete' );
static::delete( $batch_size );
ReportsCache::invalidate();
wc_admin_record_tracks_event( 'delete_import_data_job_complete', array( 'type' => static::$name ) );
}
}
Schedulers/MailchimpScheduler.php 0000644 00000007160 15154512353 0013130 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\Schedulers;
/**
* Class MailchimpScheduler
*
* @package Automattic\WooCommerce\Admin\Schedulers
*/
class MailchimpScheduler {
const SUBSCRIBE_ENDPOINT = 'https://woocommerce.com/wp-json/wccom/v1/subscribe';
const SUBSCRIBE_ENDPOINT_DEV = 'http://woocommerce.test/wp-json/wccom/v1/subscribe';
const SUBSCRIBED_OPTION_NAME = 'woocommerce_onboarding_subscribed_to_mailchimp';
const SUBSCRIBED_ERROR_COUNT_OPTION_NAME = 'woocommerce_onboarding_subscribed_to_mailchimp_error_count';
const MAX_ERROR_THRESHOLD = 3;
const LOGGER_CONTEXT = 'mailchimp_scheduler';
/**
* The logger instance.
*
* @var \WC_Logger_Interface|null
*/
private $logger;
/**
* MailchimpScheduler constructor.
*
* @internal
* @param \WC_Logger_Interface|null $logger Logger instance.
*/
public function __construct( \WC_Logger_Interface $logger = null ) {
if ( null === $logger ) {
$logger = wc_get_logger();
}
$this->logger = $logger;
}
/**
* Attempt to subscribe store_email to MailChimp.
*
* @internal
*/
public function run() {
// Abort if we've already subscribed to MailChimp.
if ( 'yes' === get_option( self::SUBSCRIBED_OPTION_NAME ) ) {
return false;
}
$profile_data = get_option( 'woocommerce_onboarding_profile' );
if ( ! isset( $profile_data['is_agree_marketing'] ) || false === $profile_data['is_agree_marketing'] ) {
return false;
}
// Abort if store_email doesn't exist.
if ( ! isset( $profile_data['store_email'] ) ) {
return false;
}
// Abort if failed requests reaches the threshold.
if ( intval( get_option( self::SUBSCRIBED_ERROR_COUNT_OPTION_NAME, 0 ) ) >= self::MAX_ERROR_THRESHOLD ) {
return false;
}
$response = $this->make_request( $profile_data['store_email'] );
if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
$this->handle_request_error();
return false;
}
$body = json_decode( $response['body'] );
if ( isset( $body->success ) && true === $body->success ) {
update_option( self::SUBSCRIBED_OPTION_NAME, 'yes' );
return true;
}
$this->handle_request_error( $body );
return false;
}
/**
* Make an HTTP request to the API.
*
* @internal
* @param string $store_email Email address to subscribe.
*
* @return mixed
*/
public function make_request( $store_email ) {
if ( true === defined( 'WP_ENVIRONMENT_TYPE' ) && 'development' === constant( 'WP_ENVIRONMENT_TYPE' ) ) {
$subscribe_endpoint = self::SUBSCRIBE_ENDPOINT_DEV;
} else {
$subscribe_endpoint = self::SUBSCRIBE_ENDPOINT;
}
return wp_remote_post(
$subscribe_endpoint,
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
'method' => 'POST',
'body' => array(
'email' => $store_email,
),
)
);
}
/**
* Reset options.
*
* @internal
*/
public static function reset() {
delete_option( self::SUBSCRIBED_OPTION_NAME );
delete_option( self::SUBSCRIBED_ERROR_COUNT_OPTION_NAME );
}
/**
* Handle subscribe API error.
*
* @internal
* @param string $extra_msg Extra message to log.
*/
private function handle_request_error( $extra_msg = null ) {
// phpcs:ignore
$msg = isset( $extra_msg ) ? 'Incorrect response from Mailchimp API with: ' . print_r( $extra_msg, true ) : 'Error getting a response from Mailchimp API.';
$this->logger->error( $msg, array( 'source' => self::LOGGER_CONTEXT ) );
$accumulated_error_count = intval( get_option( self::SUBSCRIBED_ERROR_COUNT_OPTION_NAME, 0 ) ) + 1;
update_option( self::SUBSCRIBED_ERROR_COUNT_OPTION_NAME, $accumulated_error_count );
}
}
Schedulers/OrdersScheduler.php 0000644 00000020553 15154512353 0012464 0 ustar 00 <?php
/**
* Order syncing related functions and actions.
*/
namespace Automattic\WooCommerce\Internal\Admin\Schedulers;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore as CouponsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\Products\DataStore as ProductsDataStore;
use Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore as TaxesDataStore;
use Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
use Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
use Automattic\WooCommerce\Admin\Overrides\Order;
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
use Automattic\WooCommerce\Utilities\OrderUtil;
/**
* OrdersScheduler Class.
*/
class OrdersScheduler extends ImportScheduler {
/**
* Slug to identify the scheduler.
*
* @var string
*/
public static $name = 'orders';
/**
* Attach order lookup update hooks.
*
* @internal
*/
public static function init() {
// Activate WC_Order extension.
\Automattic\WooCommerce\Admin\Overrides\Order::add_filters();
\Automattic\WooCommerce\Admin\Overrides\OrderRefund::add_filters();
// Order and refund data must be run on these hooks to ensure meta data is set.
add_action( 'woocommerce_update_order', array( __CLASS__, 'possibly_schedule_import' ) );
add_action( 'woocommerce_create_order', array( __CLASS__, 'possibly_schedule_import' ) );
add_action( 'woocommerce_refund_created', array( __CLASS__, 'possibly_schedule_import' ) );
OrdersStatsDataStore::init();
CouponsDataStore::init();
ProductsDataStore::init();
TaxesDataStore::init();
parent::init();
}
/**
* Add customer dependencies.
*
* @internal
* @return array
*/
public static function get_dependencies() {
return array(
'import_batch_init' => \Automattic\WooCommerce\Internal\Admin\Schedulers\CustomersScheduler::get_action( 'import_batch_init' ),
);
}
/**
* Get the order/refund IDs and total count that need to be synced.
*
* @internal
* @param int $limit Number of records to retrieve.
* @param int $page Page number.
* @param int|bool $days Number of days prior to current date to limit search results.
* @param bool $skip_existing Skip already imported orders.
*/
public static function get_items( $limit = 10, $page = 1, $days = false, $skip_existing = false ) {
if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
return self::get_items_from_orders_table( $limit, $page, $days, $skip_existing );
} else {
return self::get_items_from_posts_table( $limit, $page, $days, $skip_existing );
}
}
/**
* Helper method to ger order/refund IDS and total count that needs to be synced.
*
* @internal
* @param int $limit Number of records to retrieve.
* @param int $page Page number.
* @param int|bool $days Number of days prior to current date to limit search results.
* @param bool $skip_existing Skip already imported orders.
*
* @return object Total counts.
*/
private static function get_items_from_posts_table( $limit, $page, $days, $skip_existing ) {
global $wpdb;
$where_clause = '';
$offset = $page > 1 ? ( $page - 1 ) * $limit : 0;
if ( is_int( $days ) ) {
$days_ago = gmdate( 'Y-m-d 00:00:00', time() - ( DAY_IN_SECONDS * $days ) );
$where_clause .= " AND post_date_gmt >= '{$days_ago}'";
}
if ( $skip_existing ) {
$where_clause .= " AND NOT EXISTS (
SELECT 1 FROM {$wpdb->prefix}wc_order_stats
WHERE {$wpdb->prefix}wc_order_stats.order_id = {$wpdb->posts}.ID
)";
}
$count = $wpdb->get_var(
"SELECT COUNT(*) FROM {$wpdb->posts}
WHERE post_type IN ( 'shop_order', 'shop_order_refund' )
AND post_status NOT IN ( 'wc-auto-draft', 'auto-draft', 'trash' )
{$where_clause}"
); // phpcs:ignore unprepared SQL ok.
$order_ids = absint( $count ) > 0 ? $wpdb->get_col(
$wpdb->prepare(
"SELECT ID FROM {$wpdb->posts}
WHERE post_type IN ( 'shop_order', 'shop_order_refund' )
AND post_status NOT IN ( 'wc-auto-draft', 'auto-draft', 'trash' )
{$where_clause}
ORDER BY post_date_gmt ASC
LIMIT %d
OFFSET %d",
$limit,
$offset
)
) : array(); // phpcs:ignore unprepared SQL ok.
return (object) array(
'total' => absint( $count ),
'ids' => $order_ids,
);
}
/**
* Helper method to ger order/refund IDS and total count that needs to be synced from HPOS.
*
* @internal
* @param int $limit Number of records to retrieve.
* @param int $page Page number.
* @param int|bool $days Number of days prior to current date to limit search results.
* @param bool $skip_existing Skip already imported orders.
*
* @return object Total counts.
*/
private static function get_items_from_orders_table( $limit, $page, $days, $skip_existing ) {
global $wpdb;
$where_clause = '';
$offset = $page > 1 ? ( $page - 1 ) * $limit : 0;
$order_table = OrdersTableDataStore::get_orders_table_name();
if ( is_int( $days ) ) {
$days_ago = gmdate( 'Y-m-d 00:00:00', time() - ( DAY_IN_SECONDS * $days ) );
$where_clause .= " AND orders.date_created_gmt >= '{$days_ago}'";
}
if ( $skip_existing ) {
$where_clause .= "AND NOT EXiSTS (
SELECT 1 FROM {$wpdb->prefix}wc_order_stats
WHERE {$wpdb->prefix}wc_order_stats.order_id = orders.id
)
";
}
$count = $wpdb->get_var(
"
SELECT COUNT(*) FROM {$order_table} AS orders
WHERE type in ( 'shop_order', 'shop_order_refund' )
AND status NOT IN ( 'wc-auto-draft', 'trash', 'auto-draft' )
{$where_clause}
"
); // phpcs:ignore unprepared SQL ok.
$order_ids = absint( $count ) > 0 ? $wpdb->get_col(
$wpdb->prepare(
"SELECT id FROM {$order_table} AS orders
WHERE type IN ( 'shop_order', 'shop_order_refund' )
AND status NOT IN ( 'wc-auto-draft', 'auto-draft', 'trash' )
{$where_clause}
ORDER BY date_created_gmt ASC
LIMIT %d
OFFSET %d",
$limit,
$offset
)
) : array(); // phpcs:ignore unprepared SQL ok.
return (object) array(
'total' => absint( $count ),
'ids' => $order_ids,
);
}
/**
* Get total number of rows imported.
*
* @internal
*/
public static function get_total_imported() {
global $wpdb;
return $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}wc_order_stats" );
}
/**
* Schedule this import if the post is an order or refund.
*
* @param int $order_id Post ID.
*
* @internal
*/
public static function possibly_schedule_import( $order_id ) {
if ( ! OrderUtil::is_order( $order_id, array( 'shop_order' ) ) && 'woocommerce_refund_created' !== current_filter() ) {
return;
}
self::schedule_action( 'import', array( $order_id ) );
}
/**
* Imports a single order or refund to update lookup tables for.
* If an error is encountered in one of the updates, a retry action is scheduled.
*
* @internal
* @param int $order_id Order or refund ID.
* @return void
*/
public static function import( $order_id ) {
$order = wc_get_order( $order_id );
// If the order isn't found for some reason, skip the sync.
if ( ! $order ) {
return;
}
$type = $order->get_type();
// If the order isn't the right type, skip sync.
if ( 'shop_order' !== $type && 'shop_order_refund' !== $type ) {
return;
}
// If the order has no id or date created, skip sync.
if ( ! $order->get_id() || ! $order->get_date_created() ) {
return;
}
$results = array(
OrdersStatsDataStore::sync_order( $order_id ),
ProductsDataStore::sync_order_products( $order_id ),
CouponsDataStore::sync_order_coupons( $order_id ),
TaxesDataStore::sync_order_taxes( $order_id ),
CustomersDataStore::sync_order_customer( $order_id ),
);
if ( 'shop_order' === $type ) {
$order_refunds = $order->get_refunds();
foreach ( $order_refunds as $refund ) {
OrdersStatsDataStore::sync_order( $refund->get_id() );
}
}
ReportsCache::invalidate();
}
/**
* Delete a batch of orders.
*
* @internal
* @param int $batch_size Number of items to delete.
* @return void
*/
public static function delete( $batch_size ) {
global $wpdb;
$order_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT order_id FROM {$wpdb->prefix}wc_order_stats ORDER BY order_id ASC LIMIT %d",
$batch_size
)
);
foreach ( $order_ids as $order_id ) {
OrdersStatsDataStore::delete_order( $order_id );
}
}
}
Settings.php 0000644 00000031114 15154512353 0007061 0 ustar 00 <?php
/**
* WooCommerce Settings.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\API\Plugins;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Admin\API\Reports\Orders\DataStore as OrdersDataStore;
use Automattic\WooCommerce\Admin\PluginsHelper;
use Automattic\WooCommerce\Internal\Admin\WCPayPromotion\Init as WCPayPromotionInit;
use Automattic\WooCommerce\Utilities\FeaturesUtil;
use WC_Marketplace_Suggestions;
/**
* Contains logic in regards to WooCommerce Admin Settings.
*/
class Settings {
/**
* Class instance.
*
* @var Settings instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WooCommerce.
*/
public function __construct() {
// Old settings injection.
add_filter( 'woocommerce_components_settings', array( $this, 'add_component_settings' ) );
// New settings injection.
add_filter( 'woocommerce_admin_shared_settings', array( $this, 'add_component_settings' ) );
add_filter( 'woocommerce_settings_groups', array( $this, 'add_settings_group' ) );
add_filter( 'woocommerce_settings-wc_admin', array( $this, 'add_settings' ) );
}
/**
* Format order statuses by removing a leading 'wc-' if present.
*
* @param array $statuses Order statuses.
* @return array formatted statuses.
*/
public static function get_order_statuses( $statuses ) {
$formatted_statuses = array();
foreach ( $statuses as $key => $value ) {
$formatted_key = preg_replace( '/^wc-/', '', $key );
$formatted_statuses[ $formatted_key ] = $value;
}
return $formatted_statuses;
}
/**
* Get all order statuses present in analytics tables that aren't registered.
*
* @return array Unregistered order statuses.
*/
private function get_unregistered_order_statuses() {
$registered_statuses = wc_get_order_statuses();
$all_synced_statuses = OrdersDataStore::get_all_statuses();
$unregistered_statuses = array_diff( $all_synced_statuses, array_keys( $registered_statuses ) );
$formatted_status_keys = self::get_order_statuses( array_fill_keys( $unregistered_statuses, '' ) );
$formatted_statuses = array_keys( $formatted_status_keys );
return array_combine( $formatted_statuses, $formatted_statuses );
}
/**
* Return an object defining the currecy options for the site's current currency
*
* @return array Settings for the current currency {
* Array of settings.
*
* @type string $code Currency code.
* @type string $precision Number of decimals.
* @type string $symbol Symbol for currency.
* }
*/
public static function get_currency_settings() {
$code = get_woocommerce_currency();
//phpcs:ignore
return apply_filters(
'wc_currency_settings',
array(
'code' => $code,
'precision' => wc_get_price_decimals(),
'symbol' => html_entity_decode( get_woocommerce_currency_symbol( $code ) ),
'symbolPosition' => get_option( 'woocommerce_currency_pos' ),
'decimalSeparator' => wc_get_price_decimal_separator(),
'thousandSeparator' => wc_get_price_thousand_separator(),
'priceFormat' => html_entity_decode( get_woocommerce_price_format() ),
)
);
}
/**
* Hooks extra necessary data into the component settings array already set in WooCommerce core.
*
* @param array $settings Array of component settings.
* @return array Array of component settings.
*/
public function add_component_settings( $settings ) {
if ( ! is_admin() ) {
return $settings;
}
if ( ! function_exists( 'wc_blocks_container' ) ) {
global $wp_locale;
// inject data not available via older versions of wc_blocks/woo.
$settings['orderStatuses'] = self::get_order_statuses( wc_get_order_statuses() );
$settings['stockStatuses'] = self::get_order_statuses( wc_get_product_stock_status_options() );
$settings['currency'] = self::get_currency_settings();
$settings['locale'] = array(
'siteLocale' => isset( $settings['siteLocale'] )
? $settings['siteLocale']
: get_locale(),
'userLocale' => isset( $settings['l10n']['userLocale'] )
? $settings['l10n']['userLocale']
: get_user_locale(),
'weekdaysShort' => isset( $settings['l10n']['weekdaysShort'] )
? $settings['l10n']['weekdaysShort']
: array_values( $wp_locale->weekday_abbrev ),
);
}
//phpcs:ignore
$preload_data_endpoints = apply_filters( 'woocommerce_component_settings_preload_endpoints', array() );
if ( class_exists( 'Jetpack' ) ) {
$preload_data_endpoints['jetpackStatus'] = '/jetpack/v4/connection';
}
if ( ! empty( $preload_data_endpoints ) ) {
$preload_data = array_reduce(
array_values( $preload_data_endpoints ),
'rest_preload_api_request'
);
}
//phpcs:ignore
$preload_options = apply_filters( 'woocommerce_admin_preload_options', array() );
if ( ! empty( $preload_options ) ) {
foreach ( $preload_options as $option ) {
$settings['preloadOptions'][ $option ] = get_option( $option );
}
}
//phpcs:ignore
$preload_settings = apply_filters( 'woocommerce_admin_preload_settings', array() );
if ( ! empty( $preload_settings ) ) {
$setting_options = new \WC_REST_Setting_Options_V2_Controller();
foreach ( $preload_settings as $group ) {
$group_settings = $setting_options->get_group_settings( $group );
$preload_settings = array();
foreach ( $group_settings as $option ) {
if ( array_key_exists( 'id', $option ) && array_key_exists( 'value', $option ) ) {
$preload_settings[ $option['id'] ] = $option['value'];
}
}
$settings['preloadSettings'][ $group ] = $preload_settings;
}
}
$user_controller = new \WP_REST_Users_Controller();
$request = new \WP_REST_Request();
$request->set_query_params( array( 'context' => 'edit' ) );
$user_response = $user_controller->get_current_item( $request );
$current_user_data = is_wp_error( $user_response ) ? (object) array() : $user_response->get_data();
$settings['currentUserData'] = $current_user_data;
$settings['reviewsEnabled'] = get_option( 'woocommerce_enable_reviews' );
$settings['manageStock'] = get_option( 'woocommerce_manage_stock' );
$settings['commentModeration'] = get_option( 'comment_moderation' );
$settings['notifyLowStockAmount'] = get_option( 'woocommerce_notify_low_stock_amount' );
/**
* Deprecate wcAdminAssetUrl as we no longer need it after The Merge.
* Use wcAssetUrl instead.
*
* @deprecated 6.7.0
* @var string
*/
$settings['wcAdminAssetUrl'] = WC_ADMIN_IMAGES_FOLDER_URL;
$settings['wcVersion'] = WC_VERSION;
$settings['siteUrl'] = site_url();
$settings['shopUrl'] = get_permalink( wc_get_page_id( 'shop' ) );
$settings['homeUrl'] = home_url();
$settings['dateFormat'] = get_option( 'date_format' );
$settings['timeZone'] = wc_timezone_string();
$settings['plugins'] = array(
'installedPlugins' => PluginsHelper::get_installed_plugin_slugs(),
'activePlugins' => Plugins::get_active_plugins(),
);
// Plugins that depend on changing the translation work on the server but not the client -
// WooCommerce Branding is an example of this - so pass through the translation of
// 'WooCommerce' to wcSettings.
$settings['woocommerceTranslation'] = __( 'WooCommerce', 'woocommerce' );
// We may have synced orders with a now-unregistered status.
// E.g An extension that added statuses is now inactive or removed.
$settings['unregisteredOrderStatuses'] = $this->get_unregistered_order_statuses();
// The separator used for attributes found in Variation titles.
//phpcs:ignore
$settings['variationTitleAttributesSeparator'] = apply_filters( 'woocommerce_product_variation_title_attributes_separator', ' - ', new \WC_Product() );
if ( ! empty( $preload_data_endpoints ) ) {
$settings['dataEndpoints'] = isset( $settings['dataEndpoints'] )
? $settings['dataEndpoints']
: array();
foreach ( $preload_data_endpoints as $key => $endpoint ) {
// Handle error case: rest_do_request() doesn't guarantee success.
if ( empty( $preload_data[ $endpoint ] ) ) {
$settings['dataEndpoints'][ $key ] = array();
} else {
$settings['dataEndpoints'][ $key ] = $preload_data[ $endpoint ]['body'];
}
}
}
$settings = $this->get_custom_settings( $settings );
if ( PageController::is_embed_page() ) {
$settings['embedBreadcrumbs'] = wc_admin_get_breadcrumbs();
}
$settings['allowMarketplaceSuggestions'] = WC_Marketplace_Suggestions::allow_suggestions();
$settings['connectNonce'] = wp_create_nonce( 'connect' );
$settings['wcpay_welcome_page_connect_nonce'] = wp_create_nonce( 'wcpay-connect' );
$settings['features'] = $this->get_features();
$settings['isWooPayEligible'] = WCPayPromotionInit::is_woopay_eligible();
return $settings;
}
/**
* Removes non necesary feature properties for the client side.
*
* @return array
*/
public function get_features() {
$features = FeaturesUtil::get_features( true, true );
$new_features = array();
foreach ( array_keys( $features ) as $feature_id ) {
$new_features[ $feature_id ] = array(
'is_enabled' => $features[ $feature_id ]['is_enabled'],
'is_experimental' => $features[ $feature_id ]['is_experimental'] ?? false,
);
}
return $new_features;
}
/**
* Register the admin settings for use in the WC REST API
*
* @param array $groups Array of setting groups.
* @return array
*/
public function add_settings_group( $groups ) {
$groups[] = array(
'id' => 'wc_admin',
'label' => __( 'WooCommerce Admin', 'woocommerce' ),
'description' => __( 'Settings for WooCommerce admin reporting.', 'woocommerce' ),
);
return $groups;
}
/**
* Add WC Admin specific settings
*
* @param array $settings Array of settings in wc admin group.
* @return array
*/
public function add_settings( $settings ) {
$unregistered_statuses = $this->get_unregistered_order_statuses();
$registered_statuses = self::get_order_statuses( wc_get_order_statuses() );
$all_statuses = array_merge( $unregistered_statuses, $registered_statuses );
$settings[] = array(
'id' => 'woocommerce_excluded_report_order_statuses',
'option_key' => 'woocommerce_excluded_report_order_statuses',
'label' => __( 'Excluded report order statuses', 'woocommerce' ),
'description' => __( 'Statuses that should not be included when calculating report totals.', 'woocommerce' ),
'default' => array( 'pending', 'cancelled', 'failed' ),
'type' => 'multiselect',
'options' => $all_statuses,
);
$settings[] = array(
'id' => 'woocommerce_actionable_order_statuses',
'option_key' => 'woocommerce_actionable_order_statuses',
'label' => __( 'Actionable order statuses', 'woocommerce' ),
'description' => __( 'Statuses that require extra action on behalf of the store admin.', 'woocommerce' ),
'default' => array( 'processing', 'on-hold' ),
'type' => 'multiselect',
'options' => $all_statuses,
);
$settings[] = array(
'id' => 'woocommerce_default_date_range',
'option_key' => 'woocommerce_default_date_range',
'label' => __( 'Default Date Range', 'woocommerce' ),
'description' => __( 'Default Date Range', 'woocommerce' ),
'default' => 'period=month&compare=previous_year',
'type' => 'text',
);
$settings[] = array(
'id' => 'woocommerce_date_type',
'option_key' => 'woocommerce_date_type',
'label' => __( 'Date Type', 'woocommerce' ),
'description' => __( 'Database date field considered for Revenue and Orders reports', 'woocommerce' ),
'type' => 'select',
'options' => array(
'date_created' => 'date_created',
'date_paid' => 'date_paid',
'date_completed' => 'date_completed',
),
);
return $settings;
}
/**
* Gets custom settings used for WC Admin.
*
* @param array $settings Array of settings to merge into.
* @return array
*/
private function get_custom_settings( $settings ) {
$wc_rest_settings_options_controller = new \WC_REST_Setting_Options_Controller();
$wc_admin_group_settings = $wc_rest_settings_options_controller->get_group_settings( 'wc_admin' );
$settings['wcAdminSettings'] = array();
foreach ( $wc_admin_group_settings as $setting ) {
if ( ! empty( $setting['id'] ) ) {
$settings['wcAdminSettings'][ $setting['id'] ] = $setting['value'];
}
}
return $settings;
}
}
SettingsNavigationFeature.php 0000644 00000010521 15154512353 0012414 0 ustar 00 <?php
/**
* WooCommerce Settings.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\PageController;
/**
* Contains backend logic for the Settings feature.
*/
class SettingsNavigationFeature {
/**
* Option name used to toggle this feature.
*/
const TOGGLE_OPTION_NAME = 'woocommerce_settings_enabled';
/**
* Class instance.
*
* @var Settings instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WooCommerce.
*/
public function __construct() {
if ( ! is_admin() ) {
return;
}
add_filter( 'woocommerce_settings_features', array( $this, 'add_feature_toggle' ) );
if ( 'yes' !== get_option( 'woocommerce_settings_enabled', 'no' ) ) {
return;
}
add_filter( 'woocommerce_admin_shared_settings', array( __CLASS__, 'add_component_settings' ) );
// Run this after the original WooCommerce settings have been added.
add_action( 'admin_menu', array( $this, 'register_pages' ), 60 );
add_action( 'init', array( $this, 'redirect_core_settings_pages' ) );
}
/**
* Add the necessary data to initially load the WooCommerce Settings pages.
*
* @param array $settings Array of component settings.
* @return array Array of component settings.
*/
public static function add_component_settings( $settings ) {
if ( ! is_admin() ) {
return $settings;
}
$setting_pages = \WC_Admin_Settings::get_settings_pages();
$pages = array();
foreach ( $setting_pages as $setting_page ) {
$pages = $setting_page->add_settings_page( $pages );
}
$settings['settingsPages'] = $pages;
return $settings;
}
/**
* Add the feature toggle to the features settings.
*
* @param array $features Feature sections.
* @return array
*/
public static function add_feature_toggle( $features ) {
$features[] = array(
'title' => __( 'Settings', 'woocommerce' ),
'desc' => __(
'Adds the new WooCommerce settings UI.',
'woocommerce'
),
'id' => 'woocommerce_settings_enabled',
'type' => 'checkbox',
);
return $features;
}
/**
* Registers settings pages.
*/
public function register_pages() {
$controller = PageController::get_instance();
$setting_pages = \WC_Admin_Settings::get_settings_pages();
$settings = array();
foreach ( $setting_pages as $setting_page ) {
$settings = $setting_page->add_settings_page( $settings );
}
$order = 0;
foreach ( $settings as $key => $setting ) {
$order += 10;
$settings_page = array(
'parent' => 'woocommerce-settings',
'title' => $setting,
'id' => 'settings-' . $key,
'path' => "/settings/$key",
'nav_args' => array(
'capability' => 'manage_woocommerce',
'order' => $order,
'parent' => 'woocommerce-settings',
),
);
// Replace the old menu with the first settings item.
if ( 10 === $order ) {
$this->replace_settings_page( $settings_page );
}
$controller->register_page( $settings_page );
}
}
/**
* Replace the Settings page in the original WooCommerce menu.
*
* @param array $page Page used to replace the original.
*/
protected function replace_settings_page( $page ) {
global $submenu;
// Check if WooCommerce parent menu has been registered.
if ( ! isset( $submenu['woocommerce'] ) ) {
return;
}
foreach ( $submenu['woocommerce'] as &$item ) {
// The "slug" (aka the path) is the third item in the array.
if ( 0 === strpos( $item[2], 'wc-settings' ) ) {
$item[2] = wc_admin_url( "&path={$page['path']}" );
}
}
}
/**
* Redirect the old settings page URLs to the new ones.
*/
public function redirect_core_settings_pages() {
/* phpcs:disable WordPress.Security.NonceVerification */
if ( ! isset( $_GET['page'] ) || 'wc-settings' !== $_GET['page'] ) {
return;
}
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
$setting_pages = \WC_Admin_Settings::get_settings_pages();
$default_setting = isset( $setting_pages[0] ) ? $setting_pages[0]->get_id() : '';
$setting = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : $default_setting;
/* phpcs:enable */
wp_safe_redirect( wc_admin_url( "&path=/settings/$setting" ) );
exit;
}
}
ShippingLabelBanner.php 0000644 00000010226 15154512353 0011131 0 ustar 00 <?php
/**
* WooCommerce Shipping Label banner.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\Jetpack\Connection\Manager as Jetpack_Connection_Manager;
/**
* Shows print shipping label banner on edit order page.
*/
class ShippingLabelBanner {
/**
* Singleton for the display rules class
*
* @var ShippingLabelBannerDisplayRules
*/
private $shipping_label_banner_display_rules;
/**
* Constructor
*/
public function __construct() {
if ( ! is_admin() ) {
return;
}
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 6, 2 );
}
/**
* Check if WooCommerce Shipping makes sense for this merchant.
*
* @return bool
*/
private function should_show_meta_box() {
if ( ! $this->shipping_label_banner_display_rules ) {
$jetpack_version = null;
$jetpack_connected = null;
$wcs_version = null;
$wcs_tos_accepted = null;
if ( defined( 'JETPACK__VERSION' ) ) {
$jetpack_version = JETPACK__VERSION;
}
if ( class_exists( Jetpack_Connection_Manager::class ) ) {
$jetpack_connected = ( new Jetpack_Connection_Manager() )->has_connected_owner();
}
if ( class_exists( '\WC_Connect_Loader' ) ) {
$wcs_version = \WC_Connect_Loader::get_wcs_version();
}
if ( class_exists( '\WC_Connect_Options' ) ) {
$wcs_tos_accepted = \WC_Connect_Options::get_option( 'tos_accepted' );
}
$incompatible_plugins = class_exists( '\WC_Shipping_Fedex_Init' ) ||
class_exists( '\WC_Shipping_UPS_Init' ) ||
class_exists( '\WC_Integration_ShippingEasy' ) ||
class_exists( '\WC_ShipStation_Integration' );
$this->shipping_label_banner_display_rules =
new ShippingLabelBannerDisplayRules(
$jetpack_version,
$jetpack_connected,
$wcs_version,
$wcs_tos_accepted,
$incompatible_plugins
);
}
return $this->shipping_label_banner_display_rules->should_display_banner();
}
/**
* Add metabox to order page.
*
* @param string $post_type current post type.
* @param \WP_Post $post Current post object.
*/
public function add_meta_boxes( $post_type, $post ) {
if ( 'shop_order' !== $post_type ) {
return;
}
$order = wc_get_order( $post );
if ( $this->should_show_meta_box() ) {
add_meta_box(
'woocommerce-admin-print-label',
__( 'Shipping Label', 'woocommerce' ),
array( $this, 'meta_box' ),
null,
'normal',
'high',
array(
'context' => 'shipping_label',
'order' => $post->ID,
'items' => $this->count_shippable_items( $order ),
)
);
add_action( 'admin_enqueue_scripts', array( $this, 'add_print_shipping_label_script' ) );
}
}
/**
* Count shippable items
*
* @param \WC_Order $order Current order.
* @return int
*/
private function count_shippable_items( \WC_Order $order ) {
$count = 0;
foreach ( $order->get_items() as $item ) {
if ( $item instanceof \WC_Order_Item_Product ) {
$product = $item->get_product();
if ( $product && $product->needs_shipping() ) {
$count += $item->get_quantity();
}
}
}
return $count;
}
/**
* Adds JS to order page to render shipping banner.
*
* @param string $hook current page hook.
*/
public function add_print_shipping_label_script( $hook ) {
$rtl = is_rtl() ? '.rtl' : '';
wp_enqueue_style(
'print-shipping-label-banner-style',
WCAdminAssets::get_url( "print-shipping-label-banner/style{$rtl}", 'css' ),
array( 'wp-components' ),
WCAdminAssets::get_file_version( 'css' )
);
WCAdminAssets::register_script( 'wp-admin-scripts', 'print-shipping-label-banner', true );
$payload = array(
'nonce' => wp_create_nonce( 'wp_rest' ),
'baseURL' => get_rest_url(),
'wcs_server_connection' => true,
);
wp_localize_script( 'print-shipping-label-banner', 'wcConnectData', $payload );
}
/**
* Render placeholder metabox.
*
* @param \WP_Post $post current post.
* @param array $args empty args.
*/
public function meta_box( $post, $args ) {
?>
<div id="wc-admin-shipping-banner-root" class="woocommerce <?php echo esc_attr( 'wc-admin-shipping-banner' ); ?>" data-args="<?php echo esc_attr( wp_json_encode( $args['args'] ) ); ?>">
</div>
<?php
}
}
ShippingLabelBannerDisplayRules.php 0000644 00000012266 15154512353 0013500 0 ustar 00 <?php
/**
* WooCommerce Shipping Label Banner Display Rules.
*/
namespace Automattic\WooCommerce\Internal\Admin;
/**
* Determines whether or not the Shipping Label Banner should be displayed
*/
class ShippingLabelBannerDisplayRules {
/**
* Holds the installed Jetpack version.
*
* @var string
*/
private $jetpack_version;
/**
* Whether or not the installed Jetpack is connected.
*
* @var bool
*/
private $jetpack_connected;
/**
* Holds the installed WooCommerce Shipping & Tax version.
*
* @var string
*/
private $wcs_version;
/**
* Whether or not there're plugins installed incompatible with the banner.
*
* @var bool
*/
private $no_incompatible_plugins_installed;
/**
* Whether or not the WooCommerce Shipping & Tax ToS has been accepted.
*
* @var bool
*/
private $wcs_tos_accepted;
/**
* Minimum supported Jetpack version.
*
* @var string
*/
private $min_jetpack_version = '4.4';
/**
* Minimum supported WooCommerce Shipping & Tax version.
*
* @var string
*/
private $min_wcs_version = '1.22.5';
/**
* Supported countries by USPS, see: https://webpmt.usps.gov/pmt010.cfm
*
* @var array
*/
private $supported_countries = array( 'US', 'AS', 'PR', 'VI', 'GU', 'MP', 'UM', 'FM', 'MH' );
/**
* Array of supported currency codes.
*
* @var array
*/
private $supported_currencies = array( 'USD' );
/**
* Constructor.
*
* @param string $jetpack_version Installed Jetpack version to check.
* @param bool $jetpack_connected Is Jetpack connected?.
* @param string $wcs_version Installed WooCommerce Shipping & Tax version to check.
* @param bool $wcs_tos_accepted WooCommerce Shipping & Tax Terms of Service accepted?.
* @param bool $incompatible_plugins_installed Are there any incompatible plugins installed?.
*/
public function __construct( $jetpack_version, $jetpack_connected, $wcs_version, $wcs_tos_accepted, $incompatible_plugins_installed ) {
$this->jetpack_version = $jetpack_version;
$this->jetpack_connected = $jetpack_connected;
$this->wcs_version = $wcs_version;
$this->wcs_tos_accepted = $wcs_tos_accepted;
$this->no_incompatible_plugins_installed = ! $incompatible_plugins_installed;
}
/**
* Determines whether banner is eligible for display (does not include a/b logic).
*/
public function should_display_banner() {
return $this->banner_not_dismissed() &&
$this->jetpack_installed_and_active() &&
$this->jetpack_up_to_date() &&
$this->jetpack_connected &&
$this->no_incompatible_plugins_installed &&
$this->order_has_shippable_products() &&
$this->store_in_us_and_usd() &&
( $this->wcs_not_installed() || (
$this->wcs_up_to_date() && ! $this->wcs_tos_accepted
) );
}
/**
* Checks if the banner was not dismissed by the user.
*
* @return bool
*/
private function banner_not_dismissed() {
$dismissed_timestamp_ms = get_option( 'woocommerce_shipping_dismissed_timestamp' );
if ( ! is_numeric( $dismissed_timestamp_ms ) ) {
return true;
}
$dismissed_timestamp_ms = intval( $dismissed_timestamp_ms );
$dismissed_timestamp = intval( round( $dismissed_timestamp_ms / 1000 ) );
$expired_timestamp = $dismissed_timestamp + 24 * 60 * 60; // 24 hours from click time
$dismissed_for_good = -1 === $dismissed_timestamp_ms;
$dismissed_24h = time() < $expired_timestamp;
return ! $dismissed_for_good && ! $dismissed_24h;
}
/**
* Checks if jetpack is installed and active.
*
* @return bool
*/
private function jetpack_installed_and_active() {
return ! ! $this->jetpack_version;
}
/**
* Checks if Jetpack version is supported.
*
* @return bool
*/
private function jetpack_up_to_date() {
return version_compare( $this->jetpack_version, $this->min_jetpack_version, '>=' );
}
/**
* Checks if there's a shippable product in the current order.
*
* @return bool
*/
private function order_has_shippable_products() {
$post = get_post();
if ( ! $post ) {
return false;
}
$order = wc_get_order( get_post()->ID );
if ( ! $order ) {
return false;
}
// At this point (no packaging data), only show if there's at least one existing and shippable product.
foreach ( $order->get_items() as $item ) {
if ( $item instanceof \WC_Order_Item_Product ) {
$product = $item->get_product();
if ( $product && $product->needs_shipping() ) {
return true;
}
}
}
return false;
}
/**
* Checks if the store is in the US and has its default currency set to USD.
*
* @return bool
*/
private function store_in_us_and_usd() {
$base_currency = get_woocommerce_currency();
$base_location = wc_get_base_location();
return in_array( $base_currency, $this->supported_currencies, true ) && in_array( $base_location['country'], $this->supported_countries, true );
}
/**
* Checks if WooCommerce Shipping & Tax is not installed.
*
* @return bool
*/
private function wcs_not_installed() {
return ! $this->wcs_version;
}
/**
* Checks if WooCommerce Shipping & Tax is up to date.
*/
private function wcs_up_to_date() {
return $this->wcs_version && version_compare( $this->wcs_version, $this->min_wcs_version, '>=' );
}
}
SiteHealth.php 0000644 00000040333 15154512353 0007316 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WP Site Health class.
*
* @since 4.0.0
*/
class SiteHealth {
/**
* Class Constructor.
*
* @since 4.0.0
*/
public function __construct() {
add_filter( 'site_status_tests', [ $this, 'registerTests' ], 0 );
add_filter( 'debug_information', [ $this, 'addDebugInfo' ], 0 );
}
/**
* Add AIOSEO WP Site Health tests.
*
* @since 4.0.0
*
* @param array $tests The current filters array.
* @return array
*/
public function registerTests( $tests ) {
$tests['direct']['aioseo_site_public'] = [
'label' => 'AIOSEO Site Public',
'test' => [ $this, 'testCheckSitePublic' ],
];
$tests['direct']['aioseo_site_info'] = [
'label' => 'AIOSEO Site Info',
'test' => [ $this, 'testCheckSiteInfo' ],
];
$tests['direct']['aioseo_google_search_console'] = [
'label' => 'AIOSEO Google Search Console',
'test' => [ $this, 'testCheckGoogleSearchConsole' ],
];
$tests['direct']['aioseo_plugin_update'] = [
'label' => 'AIOSEO Plugin Update',
'test' => [ $this, 'testCheckPluginUpdate' ],
];
$tests['direct']['aioseo_schema_markup'] = [
'label' => 'AIOSEO Schema Markup',
'test' => [ $this, 'testCheckSchemaMarkup' ],
];
return $tests;
}
/**
* Adds our site health debug info.
*
* @since 4.0.0
*
* @param array $debugInfo The debug info.
* @return array $debugInfo The debug info.
*/
public function addDebugInfo( $debugInfo ) {
$fields = [];
$noindexed = $this->noindexed();
if ( $noindexed ) {
$fields['noindexed'] = $this->field(
__( 'Noindexed content', 'all-in-one-seo-pack' ),
implode( ', ', $noindexed )
);
}
$nofollowed = $this->nofollowed();
if ( $nofollowed ) {
$fields['nofollowed'] = $this->field(
__( 'Nofollowed content', 'all-in-one-seo-pack' ),
implode( ', ', $nofollowed )
);
}
if ( ! count( $fields ) ) {
return $debugInfo;
}
$debugInfo['aioseo'] = [
'label' => __( 'SEO', 'all-in-one-seo-pack' ),
'description' => sprintf(
// Translators: 1 - The plugin short name ("AIOSEO").
__( 'The fields below contain important SEO information from %1$s that may effect your site.', 'all-in-one-seo-pack' ),
AIOSEO_PLUGIN_SHORT_NAME
),
'private' => false,
'show_count' => true,
'fields' => $fields,
];
return $debugInfo;
}
/**
* Checks whether the site is public.
*
* @since 4.0.0
*
* @return array The test result.
*/
public function testCheckSitePublic() {
$test = 'aioseo_site_public';
if ( ! get_option( 'blog_public' ) ) {
return $this->result(
$test,
'critical',
__( 'Your site does not appear in search results', 'all-in-one-seo-pack' ),
__( 'Your site is set to private. This means WordPress asks search engines to exclude your website from search results.', 'all-in-one-seo-pack' ),
$this->actionLink( admin_url( 'options-reading.php' ), __( 'Go to Settings > Reading', 'all-in-one-seo-pack' ) )
);
}
return $this->result(
$test,
'good',
__( 'Your site appears in search results', 'all-in-one-seo-pack' ),
__( 'Your site is set to public. Search engines will index your website and it will appear in search results.', 'all-in-one-seo-pack' )
);
}
/**
* Checks whether the site title and tagline are set.
*
* @since 4.0.0
*
* @return array The test result.
*/
public function testCheckSiteInfo() {
$siteTitle = get_bloginfo( 'name' );
$siteTagline = get_bloginfo( 'description' );
if ( ! $siteTitle || ! $siteTagline ) {
return $this->result(
'aioseo_site_info',
'recommended',
__( 'Your Site Title and/or Tagline are blank', 'all-in-one-seo-pack' ),
sprintf(
// Translators: 1 - The plugin short name ("AIOSEO").
__(
'Your Site Title and/or Tagline are blank. We recommend setting both of these values as %1$s requires these for various features, including our schema markup',
'all-in-one-seo-pack'
),
AIOSEO_PLUGIN_SHORT_NAME
),
$this->actionLink( admin_url( 'options-general.php' ), __( 'Go to Settings > General', 'all-in-one-seo-pack' ) )
);
}
return $this->result(
'aioseo_site_info',
'good',
__( 'Your Site Title and Tagline are set', 'all-in-one-seo-pack' ),
sprintf(
// Translators: 1 - The plugin short name ("AIOSEO").
__( 'Great! These are required for %1$s\'s schema markup and are often used as fallback values for various other features.', 'all-in-one-seo-pack' ),
AIOSEO_PLUGIN_SHORT_NAME
)
);
}
/**
* Checks whether Google Search Console is connected.
*
* @since 4.6.2
*
* @return array The test result.
*/
public function testCheckGoogleSearchConsole() {
$googleSearchConsole = aioseo()->searchStatistics->api->auth->isConnected();
if ( ! $googleSearchConsole ) {
return $this->result(
'aioseo_google_search_console',
'recommended',
__( 'Connect Your Site with Google Search Console', 'all-in-one-seo-pack' ),
__( 'Sync your site with Google Search Console and get valuable insights right inside your WordPress dashboard. Track keyword rankings and search performance for individual posts with actionable insights to help you rank higher in search results!', 'all-in-one-seo-pack' ), // phpcs:ignore Generic.Files.LineLength.MaxExceeded
$this->actionLink( admin_url( 'admin.php?page=aioseo-settings&aioseo-scroll=google-search-console-settings&aioseo-highlight=google-search-console-settings#/webmaster-tools?activetool=googleSearchConsole' ), __( 'Connect to Google Search Console', 'all-in-one-seo-pack' ) ) // phpcs:ignore Generic.Files.LineLength.MaxExceeded
);
}
return $this->result(
'aioseo_google_search_console',
'good',
__( 'Google Search Console is Connected', 'all-in-one-seo-pack' ),
__( 'Awesome! Google Search Console is connected to your site. This will help you monitor and maintain your site\'s presence in Google Search results.', 'all-in-one-seo-pack' )
);
}
/**
* Checks whether the required settings for our schema markup are set.
*
* @since 4.0.0
*
* @return array The test result.
*/
public function testCheckSchemaMarkup() {
$menuPath = admin_url( 'admin.php?page=aioseo-search-appearance' );
if ( 'organization' === aioseo()->options->searchAppearance->global->schema->siteRepresents ) {
if (
! aioseo()->options->searchAppearance->global->schema->organizationName ||
(
! aioseo()->options->searchAppearance->global->schema->organizationLogo &&
! aioseo()->helpers->getSiteLogoUrl()
)
) {
return $this->result(
'aioseo_schema_markup',
'recommended',
__( 'Your Organization Name and/or Logo are blank', 'all-in-one-seo-pack' ),
sprintf(
// Translators: 1 - The plugin short name ("AIOSEO").
__( 'Your Organization Name and/or Logo are blank. These values are required for %1$s\'s Organization schema markup.', 'all-in-one-seo-pack' ),
AIOSEO_PLUGIN_SHORT_NAME
),
$this->actionLink( $menuPath, __( 'Go to Schema Settings', 'all-in-one-seo-pack' ) )
);
}
return $this->result(
'aioseo_schema_markup',
'good',
__( 'Your Organization Name and Logo are set', 'all-in-one-seo-pack' ),
sprintf(
// Translators: 1 - The plugin short name ("AIOSEO").
__( 'Awesome! These are required for %1$s\'s Organization schema markup.', 'all-in-one-seo-pack' ),
AIOSEO_PLUGIN_SHORT_NAME
)
);
}
if (
! aioseo()->options->searchAppearance->global->schema->person ||
(
'manual' === aioseo()->options->searchAppearance->global->schema->person &&
(
! aioseo()->options->searchAppearance->global->schema->personName ||
! aioseo()->options->searchAppearance->global->schema->personLogo
)
)
) {
return $this->result(
'aioseo_schema_markup',
'recommended',
__( 'Your Person Name and/or Image are blank', 'all-in-one-seo-pack' ),
sprintf(
// Translators: 1 - The plugin short name ("AIOSEO").
__( 'Your Person Name and/or Image are blank. These values are required for %1$s\'s Person schema markup.', 'all-in-one-seo-pack' ),
AIOSEO_PLUGIN_SHORT_NAME
),
$this->actionLink( $menuPath, __( 'Go to Schema Settings', 'all-in-one-seo-pack' ) )
);
}
return $this->result(
'aioseo_schema_markup',
'good',
__( 'Your Person Name and Image are set', 'all-in-one-seo-pack' ),
sprintf(
// Translators: 1 - The plugin short name ("AIOSEO").
__( 'Awesome! These are required for %1$s\'s Person schema markup.', 'all-in-one-seo-pack' ),
AIOSEO_PLUGIN_SHORT_NAME
)
);
}
/**
* Checks if the plugin should be updated.
*
* @since 4.7.2
*
* @return bool Whether the plugin should be updated.
*/
public function shouldUpdate() {
$response = wp_remote_get( 'https://api.wordpress.org/plugins/info/1.0/all-in-one-seo-pack.json' );
$body = wp_remote_retrieve_body( $response );
if ( ! $body ) {
// Something went wrong.
return false;
}
$pluginData = json_decode( $body );
return version_compare( AIOSEO_VERSION, $pluginData->version, '<' );
}
/**
* Checks whether the required settings for our schema markup are set.
*
* @since 4.0.0
*
* @return array The test result.
*/
public function testCheckPluginUpdate() {
if ( $this->shouldUpdate() ) {
return $this->result(
'aioseo_plugin_update',
'critical',
sprintf(
// Translators: 1 - The plugin short name ("AIOSEO").
__( '%1$s needs to be updated', 'all-in-one-seo-pack' ),
AIOSEO_PLUGIN_SHORT_NAME
),
sprintf(
// Translators: 1 - The plugin short name ("AIOSEO").
__( 'An update is available for %1$s. Upgrade to the latest version to receive all the latest features, bug fixes and security improvements.', 'all-in-one-seo-pack' ),
AIOSEO_PLUGIN_SHORT_NAME
),
$this->actionLink( admin_url( 'plugins.php' ), __( 'Go to Plugins', 'all-in-one-seo-pack' ) )
);
}
return $this->result(
'aioseo_plugin_update',
'good',
sprintf(
// Translators: 1 - The plugin short name ("AIOSEO").
__( '%1$s is updated to the latest version', 'all-in-one-seo-pack' ),
AIOSEO_PLUGIN_SHORT_NAME
),
__( 'Fantastic! By updating to the latest version, you have access to all the latest features, bug fixes and security improvements.', 'all-in-one-seo-pack' )
);
}
/**
* Returns a list of noindexed content.
*
* @since 4.0.0
*
* @return array $noindexed A list of noindexed content.
*/
protected function noindexed() {
$globalDefault = aioseo()->options->searchAppearance->advanced->globalRobotsMeta->default;
if (
! $globalDefault &&
aioseo()->options->searchAppearance->advanced->globalRobotsMeta->noindex
) {
return [
__( 'Your entire site is set to globally noindex content.', 'all-in-one-seo-pack' )
];
}
$noindexed = [];
if (
! $globalDefault &&
aioseo()->options->searchAppearance->advanced->globalRobotsMeta->noindexPaginated
) {
$noindexed[] = __( 'Paginated Content', 'all-in-one-seo-pack' );
}
$archives = [
'author' => __( 'Author Archives', 'all-in-one-seo-pack' ),
'date' => __( 'Date Archives', 'all-in-one-seo-pack' ),
'search' => __( 'Search Page', 'all-in-one-seo-pack' )
];
// Archives.
foreach ( $archives as $name => $type ) {
if (
! aioseo()->options->searchAppearance->archives->{ $name }->advanced->robotsMeta->default &&
aioseo()->options->searchAppearance->archives->{ $name }->advanced->robotsMeta->noindex
) {
$noindexed[] = $type;
}
}
foreach ( aioseo()->helpers->getPublicPostTypes() as $postType ) {
if (
aioseo()->dynamicOptions->searchAppearance->postTypes->has( $postType['name'] ) &&
! aioseo()->dynamicOptions->searchAppearance->postTypes->{ $postType['name'] }->advanced->robotsMeta->default &&
aioseo()->dynamicOptions->searchAppearance->postTypes->{ $postType['name'] }->advanced->robotsMeta->noindex
) {
$noindexed[] = $postType['label'] . ' (' . $postType['name'] . ')';
}
}
foreach ( aioseo()->helpers->getPublicTaxonomies() as $taxonomy ) {
if (
aioseo()->dynamicOptions->searchAppearance->taxonomies->has( $taxonomy['name'] ) &&
! aioseo()->dynamicOptions->searchAppearance->taxonomies->{ $taxonomy['name'] }->advanced->robotsMeta->default &&
aioseo()->dynamicOptions->searchAppearance->taxonomies->{ $taxonomy['name'] }->advanced->robotsMeta->noindex
) {
$noindexed[] = $taxonomy['label'] . ' (' . $taxonomy['name'] . ')';
}
}
return $noindexed;
}
/**
* Returns a list of nofollowed content.
*
* @since 4.0.0
*
* @return array $nofollowed A list of nofollowed content.
*/
protected function nofollowed() {
$globalDefault = aioseo()->options->searchAppearance->advanced->globalRobotsMeta->default;
if (
! $globalDefault &&
aioseo()->options->searchAppearance->advanced->globalRobotsMeta->nofollow
) {
return [
__( 'Your entire site is set to globally nofollow content.', 'all-in-one-seo-pack' )
];
}
$nofollowed = [];
if (
! $globalDefault &&
aioseo()->options->searchAppearance->advanced->globalRobotsMeta->nofollowPaginated
) {
$nofollowed[] = __( 'Paginated Content', 'all-in-one-seo-pack' );
}
$archives = [
'author' => __( 'Author Archives', 'all-in-one-seo-pack' ),
'date' => __( 'Date Archives', 'all-in-one-seo-pack' ),
'search' => __( 'Search Page', 'all-in-one-seo-pack' )
];
// Archives.
foreach ( $archives as $name => $type ) {
if (
! aioseo()->options->searchAppearance->archives->{ $name }->advanced->robotsMeta->default &&
aioseo()->options->searchAppearance->archives->{ $name }->advanced->robotsMeta->nofollow
) {
$nofollowed[] = $type;
}
}
foreach ( aioseo()->helpers->getPublicPostTypes() as $postType ) {
if (
aioseo()->dynamicOptions->searchAppearance->postTypes->has( $postType['name'] ) &&
! aioseo()->dynamicOptions->searchAppearance->postTypes->{ $postType['name'] }->advanced->robotsMeta->default &&
aioseo()->dynamicOptions->searchAppearance->postTypes->{ $postType['name'] }->advanced->robotsMeta->nofollow
) {
$nofollowed[] = $postType['label'] . ' (' . $postType['name'] . ')';
}
}
foreach ( aioseo()->helpers->getPublicTaxonomies() as $taxonomy ) {
if (
aioseo()->dynamicOptions->searchAppearance->taxonomies->has( $taxonomy['name'] ) &&
! aioseo()->dynamicOptions->searchAppearance->taxonomies->{ $taxonomy['name'] }->advanced->robotsMeta->default &&
aioseo()->dynamicOptions->searchAppearance->taxonomies->{ $taxonomy['name'] }->advanced->robotsMeta->nofollow
) {
$nofollowed[] = $taxonomy['label'] . ' (' . $taxonomy['name'] . ')';
}
}
return $nofollowed;
}
/**
* Returns a debug info data field.
*
* @since 4.0.0
*
* @param string $label The field label.
* @param string $value The field value.
* @param boolean $private Whether the field shouldn't be included if the debug info is copied.
* @return array The debug info data field.
*/
private function field( $label, $value, $private = false ) {
return [
'label' => $label,
'value' => $value,
'private' => $private,
];
}
/**
* Returns the test result.
*
* @since 4.0.0
*
* @param string $name The test name.
* @param string $status The result status.
* @param string $header The test header.
* @param string $description The result description.
* @param string $actions The result actions.
* @return array The test result.
*/
protected function result( $name, $status, $header, $description, $actions = '' ) {
$color = 'blue';
switch ( $status ) {
case 'good':
break;
case 'recommended':
$color = 'orange';
break;
case 'critical':
$color = 'red';
break;
default:
break;
}
return [
'test' => $name,
'status' => $status,
'label' => $header,
'description' => $description,
'actions' => $actions,
'badge' => [
'label' => AIOSEO_PLUGIN_SHORT_NAME,
'color' => $color,
],
];
}
/**
* Returns an action link.
*
* @since 4.0.0
*
* @param string $path The path.
* @param string $anchor The anchor text.
* @return string The action link.
*/
protected function actionLink( $path, $anchor ) {
return sprintf(
'<p><a href="%1$s">%2$s</a></p>',
$path,
$anchor
);
}
} Survey.php 0000644 00000001400 15154512353 0006551 0 ustar 00 <?php
/**
* Survey helper methods.
*/
namespace Automattic\WooCommerce\Internal\Admin;
defined( 'ABSPATH' ) || exit;
/**
* Survey Class.
*/
class Survey {
/**
* Survey URL.
*/
const SURVEY_URL = 'https://automattic.survey.fm';
/**
* Get a survey's URL from a path.
*
* @param string $path Path of the survey.
* @param array $query Query arguments as key value pairs.
* @return string Full URL to survey.
*/
public static function get_url( $path, $query = array() ) {
$url = self::SURVEY_URL . $path;
$query_args = apply_filters( 'woocommerce_admin_survey_query', $query );
if ( ! empty( $query_args ) ) {
$query_string = http_build_query( $query_args );
$url = $url . '?' . $query_string;
}
return $url;
}
}
SystemStatusReport.php 0000644 00000013537 15154512353 0011156 0 ustar 00 <?php
/**
* Add additional system status report sections.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\Notes\Notes;
defined( 'ABSPATH' ) || exit;
/**
* SystemStatusReport class.
*/
class SystemStatusReport {
/**
* Class instance.
*
* @var SystemStatus instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WooCommerce.
*/
public function __construct() {
add_action( 'woocommerce_system_status_report', array( $this, 'system_status_report' ) );
}
/**
* Hooks extra necessary sections into the system status report template
*/
public function system_status_report() {
?>
<table class="wc_status_table widefat" cellspacing="0">
<thead>
<tr>
<th colspan="5" data-export-label="Admin">
<h2>
<?php esc_html_e( 'Admin', 'woocommerce' ); ?><?php echo wc_help_tip( esc_html__( 'This section shows details of WC Admin.', 'woocommerce' ) ); ?>
</h2>
</th>
</tr>
</thead>
<tbody>
<?php
$this->render_features();
$this->render_daily_cron();
$this->render_options();
$this->render_notes();
$this->render_onboarding_state();
?>
</tbody>
</table>
<?php
}
/**
* Render features rows.
*/
public function render_features() {
/**
* Filter the admin feature configs.
*
* @since 6.5.0
*/
$features = apply_filters( 'woocommerce_admin_get_feature_config', wc_admin_get_feature_config() );
$enabled_features = array_filter( $features );
$disabled_features = array_filter(
$features,
function( $feature ) {
return empty( $feature );
}
);
?>
<tr>
<td data-export-label="Enabled Features">
<?php esc_html_e( 'Enabled Features', 'woocommerce' ); ?>:
</td>
<td class="help"><?php echo wc_help_tip( esc_html__( 'Which features are enabled?', 'woocommerce' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
<td>
<?php
echo esc_html( implode( ', ', array_keys( $enabled_features ) ) )
?>
</td>
</tr>
<tr>
<td data-export-label="Disabled Features">
<?php esc_html_e( 'Disabled Features', 'woocommerce' ); ?>:
</td>
<td class="help"><?php echo wc_help_tip( esc_html__( 'Which features are disabled?', 'woocommerce' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
<td>
<?php
echo esc_html( implode( ', ', array_keys( $disabled_features ) ) )
?>
</td>
</tr>
<?php
}
/**
* Render daily cron row.
*/
public function render_daily_cron() {
$next_daily_cron = wp_next_scheduled( 'wc_admin_daily' );
?>
<tr>
<td data-export-label="Daily Cron">
<?php esc_html_e( 'Daily Cron', 'woocommerce' ); ?>:
</td>
<td class="help"><?php echo wc_help_tip( esc_html__( 'Is the daily cron job active, when does it next run?', 'woocommerce' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
<td>
<?php
if ( empty( $next_daily_cron ) ) {
echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . esc_html__( 'Not scheduled', 'woocommerce' ) . '</mark>';
} else {
echo '<mark class="yes"><span class="dashicons dashicons-yes"></span> Next scheduled: ' . esc_html( date_i18n( 'Y-m-d H:i:s P', $next_daily_cron ) ) . '</mark>';
}
?>
</td>
</tr>
<?php
}
/**
* Render option row.
*/
public function render_options() {
$woocommerce_admin_install_timestamp = get_option( 'woocommerce_admin_install_timestamp' );
$all_options_expected = is_numeric( $woocommerce_admin_install_timestamp )
&& 0 < (int) $woocommerce_admin_install_timestamp
&& is_array( get_option( 'woocommerce_onboarding_profile', array() ) );
?>
<tr>
<td data-export-label="Options">
<?php esc_html_e( 'Options', 'woocommerce' ); ?>:
</td>
<td class="help"><?php echo wc_help_tip( esc_html__( 'Do the important options return expected values?', 'woocommerce' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
<td>
<?php
if ( $all_options_expected ) {
echo '<mark class="yes"><span class="dashicons dashicons-yes"></mark>';
} else {
echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . esc_html__( 'Not all expected', 'woocommerce' ) . '</mark>';
}
?>
</td>
</tr>
<?php
}
/**
* Render the notes row.
*/
public function render_notes() {
$notes_count = Notes::get_notes_count();
?>
<tr>
<td data-export-label="Notes">
<?php esc_html_e( 'Notes', 'woocommerce' ); ?>:
</td>
<td class="help"><?php echo wc_help_tip( esc_html__( 'How many notes in the database?', 'woocommerce' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
<td>
<?php
echo esc_html( $notes_count )
?>
</td>
</tr>
<?php
}
/**
* Render the onboarding state row.
*/
public function render_onboarding_state() {
$onboarding_profile = get_option( 'woocommerce_onboarding_profile', array() );
$onboarding_state = '-';
if ( isset( $onboarding_profile['skipped'] ) && $onboarding_profile['skipped'] ) {
$onboarding_state = 'skipped';
}
if ( isset( $onboarding_profile['completed'] ) && $onboarding_profile['completed'] ) {
$onboarding_state = 'completed';
}
?>
<tr>
<td data-export-label="Onboarding">
<?php esc_html_e( 'Onboarding', 'woocommerce' ); ?>:
</td>
<td class="help"><?php echo wc_help_tip( esc_html__( 'Was onboarding completed or skipped?', 'woocommerce' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
<td>
<?php
echo esc_html( $onboarding_state )
?>
</td>
</tr>
<?php
}
}
Translations.php 0000644 00000027602 15154512353 0007751 0 ustar 00 <?php
/**
* Register the scripts, and handles items needed for managing translations within WooCommerce Admin.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Internal\Admin\Loader;
/**
* Translations Class.
*/
class Translations {
/**
* Class instance.
*
* @var Translations instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
* Hooks added here should be removed in `wc_admin_initialize` via the feature plugin.
*/
public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'potentially_load_translation_script_file' ), 15 );
// Combine JSON translation files (from chunks) when language packs are updated.
add_action( 'upgrader_process_complete', array( $this, 'combine_translation_chunk_files' ), 10, 2 );
// Handler for WooCommerce and WooCommerce Admin plugin activation.
add_action( 'woocommerce_activated_plugin', array( $this, 'potentially_generate_translation_strings' ) );
add_action( 'activated_plugin', array( $this, 'potentially_generate_translation_strings' ) );
}
/**
* Generate a filename to cache translations from JS chunks.
*
* @param string $domain Text domain.
* @param string $locale Locale being retrieved.
* @return string Filename.
*/
private function get_combined_translation_filename( $domain, $locale ) {
$filename = implode( '-', array( $domain, $locale, WC_ADMIN_APP ) ) . '.json';
return $filename;
}
/**
* Combines data from translation chunk files based on officially downloaded file format.
*
* @param array $json_i18n_filenames List of JSON chunk files.
* @return array Combined translation chunk data.
*/
private function combine_official_translation_chunks( $json_i18n_filenames ) {
// the filesystem object should be hooked up.
global $wp_filesystem;
$combined_translation_data = array();
foreach ( $json_i18n_filenames as $json_filename ) {
if ( ! $wp_filesystem->is_readable( $json_filename ) ) {
continue;
}
$file_contents = $wp_filesystem->get_contents( $json_filename );
$chunk_data = \json_decode( $file_contents, true );
if ( empty( $chunk_data ) ) {
continue;
}
$reference_file = $chunk_data['comment']['reference'];
// Only combine "app" files (not scripts registered with WP).
if (
false === strpos( $reference_file, WC_ADMIN_DIST_JS_FOLDER . 'app/index.js' ) &&
false === strpos( $reference_file, WC_ADMIN_DIST_JS_FOLDER . 'chunks/' )
) {
continue;
}
if ( empty( $combined_translation_data ) ) {
// Use the first translation file as the base structure.
$combined_translation_data = $chunk_data;
} else {
// Combine all messages from all chunk files.
$combined_translation_data['locale_data']['messages'] = array_merge(
$combined_translation_data['locale_data']['messages'],
$chunk_data['locale_data']['messages']
);
}
}
// Remove inaccurate reference comment.
unset( $combined_translation_data['comment'] );
return $combined_translation_data;
}
/**
* Combines data from translation chunk files based on user-generated file formats,
* such as wp-cli tool or Loco Translate plugin.
*
* @param array $json_i18n_filenames List of JSON chunk files.
* @return array Combined translation chunk data.
*/
private function combine_user_translation_chunks( $json_i18n_filenames ) {
// the filesystem object should be hooked up.
global $wp_filesystem;
$combined_translation_data = array();
foreach ( $json_i18n_filenames as $json_filename ) {
if ( ! $wp_filesystem->is_readable( $json_filename ) ) {
continue;
}
$file_contents = $wp_filesystem->get_contents( $json_filename );
$chunk_data = \json_decode( $file_contents, true );
if ( empty( $chunk_data ) ) {
continue;
}
$reference_file = $chunk_data['source'];
// Only combine "app" files (not scripts registered with WP).
if (
false === strpos( $reference_file, WC_ADMIN_DIST_JS_FOLDER . 'app/index.js' ) &&
false === strpos( $reference_file, WC_ADMIN_DIST_JS_FOLDER . 'chunks/' )
) {
continue;
}
if ( empty( $combined_translation_data ) ) {
// Use the first translation file as the base structure.
$combined_translation_data = $chunk_data;
} else {
// Combine all messages from all chunk files.
$combined_translation_data['locale_data']['woocommerce'] = array_merge(
$combined_translation_data['locale_data']['woocommerce'],
$chunk_data['locale_data']['woocommerce']
);
}
}
// Remove inaccurate reference comment.
unset( $combined_translation_data['source'] );
return $combined_translation_data;
}
/**
* Find and combine translation chunk files.
*
* Only targets files that aren't represented by a registered script (e.g. not passed to wp_register_script()).
*
* @param string $lang_dir Path to language files.
* @param string $domain Text domain.
* @param string $locale Locale being retrieved.
* @return array Combined translation chunk data.
*/
private function get_translation_chunk_data( $lang_dir, $domain, $locale ) {
// So long as this function is called during the 'upgrader_process_complete' action,
// the filesystem object should be hooked up.
global $wp_filesystem;
// Grab all JSON files in the current language pack.
$json_i18n_filenames = glob( $lang_dir . $domain . '-' . $locale . '-*.json' );
$combined_translation_data = array();
if ( false === $json_i18n_filenames ) {
return $combined_translation_data;
}
// Use first JSON file to determine file format. This check is required due to
// file format difference between official language files and user translated files.
$format_determine_file = reset( $json_i18n_filenames );
if ( ! $wp_filesystem->is_readable( $format_determine_file ) ) {
return $combined_translation_data;
}
$file_contents = $wp_filesystem->get_contents( $format_determine_file );
$format_determine_data = \json_decode( $file_contents, true );
if ( empty( $format_determine_data ) ) {
return $combined_translation_data;
}
if ( isset( $format_determine_data['comment'] ) ) {
return $this->combine_official_translation_chunks( $json_i18n_filenames );
} elseif ( isset( $format_determine_data['source'] ) ) {
return $this->combine_user_translation_chunks( $json_i18n_filenames );
} else {
return $combined_translation_data;
}
}
/**
* Combine and save translations for a specific locale.
*
* Note that this assumes \WP_Filesystem is already initialized with write access.
*
* @param string $language_dir Path to language files.
* @param string $plugin_domain Text domain.
* @param string $locale Locale being retrieved.
*/
private function build_and_save_translations( $language_dir, $plugin_domain, $locale ) {
global $wp_filesystem;
$translations_from_chunks = $this->get_translation_chunk_data( $language_dir, $plugin_domain, $locale );
if ( empty( $translations_from_chunks ) ) {
return;
}
$cache_filename = $this->get_combined_translation_filename( $plugin_domain, $locale );
$chunk_translations_json = wp_json_encode( $translations_from_chunks );
// Cache combined translations strings to a file.
$wp_filesystem->put_contents( $language_dir . $cache_filename, $chunk_translations_json );
}
/**
* Combine translation chunks when plugin is activated.
*
* This function combines JSON translation data auto-extracted by GlotPress
* from Webpack-generated JS chunks into a single file. This is necessary
* since the JS chunks are not known to WordPress via wp_register_script()
* and wp_set_script_translations().
*/
private function generate_translation_strings() {
$plugin_domain = explode( '/', plugin_basename( __FILE__ ) )[0];
$locale = determine_locale();
$lang_dir = WP_LANG_DIR . '/plugins/';
// Bail early if not localized.
if ( 'en_US' === $locale ) {
return;
}
if ( ! function_exists( 'get_filesystem_method' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$access_type = get_filesystem_method();
if ( 'direct' === $access_type ) {
\WP_Filesystem();
$this->build_and_save_translations( $lang_dir, $plugin_domain, $locale );
} else {
// I'm reluctant to add support for other filesystems here as it would require
// user's input on activating plugin - which I don't think is common.
return;
}
}
/**
* Loads the required translation scripts on the correct pages.
*/
public function potentially_load_translation_script_file() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
// Grab translation strings from Webpack-generated chunks.
add_filter( 'load_script_translation_file', array( $this, 'load_script_translation_file' ), 10, 3 );
}
/**
* Load translation strings from language packs for dynamic imports.
*
* @param string $file File location for the script being translated.
* @param string $handle Script handle.
* @param string $domain Text domain.
*
* @return string New file location for the script being translated.
*/
public function load_script_translation_file( $file, $handle, $domain ) {
// Make sure the main app script is being loaded.
if ( WC_ADMIN_APP !== $handle ) {
return $file;
}
// Make sure we're handing the correct domain (could be woocommerce or woocommerce-admin).
$plugin_domain = explode( '/', plugin_basename( __FILE__ ) )[0];
if ( $plugin_domain !== $domain ) {
return $file;
}
$locale = determine_locale();
$cache_filename = $this->get_combined_translation_filename( $domain, $locale );
return WP_LANG_DIR . '/plugins/' . $cache_filename;
}
/**
* Run when plugin is activated (can be WooCommerce or WooCommerce Admin).
*
* @param string $filename Activated plugin filename.
*/
public function potentially_generate_translation_strings( $filename ) {
$plugin_domain = explode( '/', plugin_basename( __FILE__ ) )[0];
$activated_plugin_domain = explode( '/', $filename )[0];
// Ensure we're only running only on activation hook that originates from our plugin.
if ( $plugin_domain === $activated_plugin_domain ) {
$this->generate_translation_strings();
}
}
/**
* Combine translation chunks when files are updated.
*
* This function combines JSON translation data auto-extracted by GlotPress
* from Webpack-generated JS chunks into a single file that can be used in
* subsequent requests. This is necessary since the JS chunks are not known
* to WordPress via wp_register_script() and wp_set_script_translations().
*
* @param Language_Pack_Upgrader $instance Upgrader instance.
* @param array $hook_extra Info about the upgraded language packs.
*/
public function combine_translation_chunk_files( $instance, $hook_extra ) {
if (
! is_a( $instance, 'Language_Pack_Upgrader' ) ||
! isset( $hook_extra['translations'] ) ||
! is_array( $hook_extra['translations'] )
) {
return;
}
// Make sure we're handing the correct domain (could be woocommerce or woocommerce-admin).
$plugin_domain = explode( '/', plugin_basename( __FILE__ ) )[0];
$locales = array();
$language_dir = WP_LANG_DIR . '/plugins/';
// Gather the locales that were updated in this operation.
foreach ( $hook_extra['translations'] as $translation ) {
if (
'plugin' === $translation['type'] &&
$plugin_domain === $translation['slug']
) {
$locales[] = $translation['language'];
}
}
// Build combined translation files for all updated locales.
foreach ( $locales as $locale ) {
// So long as this function is hooked to the 'upgrader_process_complete' action,
// WP_Filesystem should be hooked up to be able to call build_and_save_translations.
$this->build_and_save_translations( $language_dir, $plugin_domain, $locale );
}
}
}
WCAdminAssets.php 0000644 00000032121 15154512353 0007725 0 ustar 00 <?php
/**
* Register the scripts, and styles used within WooCommerce Admin.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use _WP_Dependency;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Internal\Admin\Loader;
/**
* WCAdminAssets Class.
*/
class WCAdminAssets {
/**
* Class instance.
*
* @var WCAdminAssets instance
*/
protected static $instance = null;
/**
* An array of dependencies that have been preloaded (to avoid duplicates).
*
* @var array
*/
protected $preloaded_dependencies;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
* Hooks added here should be removed in `wc_admin_initialize` via the feature plugin.
*/
public function __construct() {
Features::get_instance();
add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'inject_wc_settings_dependencies' ), 14 );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ), 15 );
}
/**
* Gets the path for the asset depending on file type.
*
* @param string $ext File extension.
* @return string Folder path of asset.
*/
public static function get_path( $ext ) {
return ( $ext === 'css' ) ? WC_ADMIN_DIST_CSS_FOLDER : WC_ADMIN_DIST_JS_FOLDER;
}
/**
* Determines if a minified JS file should be served.
*
* @param boolean $script_debug Only serve unminified files if script debug is on.
* @return boolean If js asset should use minified version.
*/
public static function should_use_minified_js_file( $script_debug ) {
// minified files are only shipped in non-core versions of wc-admin, return false if minified files are not available.
if ( ! Features::exists( 'minified-js' ) ) {
return false;
}
// Otherwise we will serve un-minified files if SCRIPT_DEBUG is on, or if anything truthy is passed in-lieu of SCRIPT_DEBUG.
return ! $script_debug;
}
/**
* Gets the URL to an asset file.
*
* @param string $file File name (without extension).
* @param string $ext File extension.
* @return string URL to asset.
*/
public static function get_url( $file, $ext ) {
$suffix = '';
// Potentially enqueue minified JavaScript.
if ( $ext === 'js' ) {
$script_debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
$suffix = self::should_use_minified_js_file( $script_debug ) ? '.min' : '';
}
return plugins_url( self::get_path( $ext ) . $file . $suffix . '.' . $ext, WC_ADMIN_PLUGIN_FILE );
}
/**
* Gets the file modified time as a cache buster if we're in dev mode, or the plugin version otherwise.
*
* @param string $ext File extension.
* @return string The cache buster value to use for the given file.
*/
public static function get_file_version( $ext ) {
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
return filemtime( WC_ADMIN_ABSPATH . self::get_path( $ext ) );
}
return WC_VERSION;
}
/**
* Gets a script asset registry filename. The asset registry lists dependencies for the given script.
*
* @param string $script_path_name Path to where the script asset registry is contained.
* @param string $file File name (without extension).
* @return string complete asset filename.
*
* @throws \Exception Throws an exception when a readable asset registry file cannot be found.
*/
public static function get_script_asset_filename( $script_path_name, $file ) {
$minification_supported = Features::exists( 'minified-js' );
$script_min_filename = $file . '.min.asset.php';
$script_nonmin_filename = $file . '.asset.php';
$script_asset_path = WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $script_path_name . '/';
// Check minification is supported first, to avoid multiple is_readable checks when minification is
// not supported.
if ( $minification_supported && is_readable( $script_asset_path . $script_min_filename ) ) {
return $script_min_filename;
} elseif ( is_readable( $script_asset_path . $script_nonmin_filename ) ) {
return $script_nonmin_filename;
} else {
// could not find an asset file, throw an error.
throw new \Exception( 'Could not find asset registry for ' . $script_path_name );
}
}
/**
* Render a preload link tag for a dependency, optionally
* checked against a provided allowlist.
*
* See: https://macarthur.me/posts/preloading-javascript-in-wordpress
*
* @param WP_Dependency $dependency The WP_Dependency being preloaded.
* @param string $type Dependency type - 'script' or 'style'.
* @param array $allowlist Optional. List of allowed dependency handles.
*/
private function maybe_output_preload_link_tag( $dependency, $type, $allowlist = array() ) {
if (
(
! empty( $allowlist ) &&
! in_array( $dependency->handle, $allowlist, true )
) ||
( ! empty( $this->preloaded_dependencies[ $type ] ) &&
in_array( $dependency->handle, $this->preloaded_dependencies[ $type ], true ) )
) {
return;
}
$this->preloaded_dependencies[ $type ][] = $dependency->handle;
$source = $dependency->ver ? add_query_arg( 'ver', $dependency->ver, $dependency->src ) : $dependency->src;
echo '<link rel="preload" href="', esc_url( $source ), '" as="', esc_attr( $type ), '" />', "\n";
}
/**
* Output a preload link tag for dependencies (and their sub dependencies)
* with an optional allowlist.
*
* See: https://macarthur.me/posts/preloading-javascript-in-wordpress
*
* @param string $type Dependency type - 'script' or 'style'.
* @param array $allowlist Optional. List of allowed dependency handles.
*/
private function output_header_preload_tags_for_type( $type, $allowlist = array() ) {
if ( $type === 'script' ) {
$dependencies_of_type = wp_scripts();
} elseif ( $type === 'style' ) {
$dependencies_of_type = wp_styles();
} else {
return;
}
foreach ( $dependencies_of_type->queue as $dependency_handle ) {
$dependency = $dependencies_of_type->query( $dependency_handle, 'registered' );
if ( $dependency === false ) {
continue;
}
// Preload the subdependencies first.
foreach ( $dependency->deps as $sub_dependency_handle ) {
$sub_dependency = $dependencies_of_type->query( $sub_dependency_handle, 'registered' );
if ( $sub_dependency ) {
$this->maybe_output_preload_link_tag( $sub_dependency, $type, $allowlist );
}
}
$this->maybe_output_preload_link_tag( $dependency, $type, $allowlist );
}
}
/**
* Output preload link tags for all enqueued stylesheets and scripts.
*
* See: https://macarthur.me/posts/preloading-javascript-in-wordpress
*/
private function output_header_preload_tags() {
$wc_admin_scripts = array(
WC_ADMIN_APP,
'wc-components',
);
$wc_admin_styles = array(
WC_ADMIN_APP,
'wc-components',
'wc-material-icons',
);
// Preload styles.
$this->output_header_preload_tags_for_type( 'style', $wc_admin_styles );
// Preload scripts.
$this->output_header_preload_tags_for_type( 'script', $wc_admin_scripts );
}
/**
* Loads the required scripts on the correct pages.
*/
public function enqueue_assets() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
wp_enqueue_script( WC_ADMIN_APP );
wp_enqueue_style( WC_ADMIN_APP );
wp_enqueue_style( 'wc-material-icons' );
wp_enqueue_style( 'wc-onboarding' );
// Preload our assets.
$this->output_header_preload_tags();
}
/**
* Registers all the necessary scripts and styles to show the admin experience.
*/
public function register_scripts() {
if ( ! function_exists( 'wp_set_script_translations' ) ) {
return;
}
$js_file_version = self::get_file_version( 'js' );
$css_file_version = self::get_file_version( 'css' );
$scripts = array(
'wc-admin-layout',
'wc-explat',
'wc-experimental',
'wc-customer-effort-score',
// NOTE: This should be removed when Gutenberg is updated and the notices package is removed from WooCommerce Admin.
'wc-notices',
'wc-number',
'wc-tracks',
'wc-date',
'wc-components',
WC_ADMIN_APP,
'wc-csv',
'wc-store-data',
'wc-currency',
'wc-navigation',
'wc-product-editor',
);
$scripts_map = array(
WC_ADMIN_APP => 'app',
'wc-csv' => 'csv-export',
'wc-store-data' => 'data',
);
$translated_scripts = array(
'wc-currency',
'wc-date',
'wc-components',
'wc-customer-effort-score',
'wc-experimental',
WC_ADMIN_APP,
);
foreach ( $scripts as $script ) {
$script_path_name = isset( $scripts_map[ $script ] ) ? $scripts_map[ $script ] : str_replace( 'wc-', '', $script );
try {
$script_assets_filename = self::get_script_asset_filename( $script_path_name, 'index' );
$script_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $script_path_name . '/' . $script_assets_filename;
global $wp_version;
if ( 'app' === $script_path_name && version_compare( $wp_version, '6.3', '<' ) ) {
// Remove wp-router dependency for WordPress versions < 6.3 because wp-router is not included in those versions. We only use wp-router in customize store pages and the feature is only available in WordPress 6.3+.
// We can remove this once our minimum support is WP 6.3.
$script_assets['dependencies'] = array_diff( $script_assets['dependencies'], array( 'wp-router' ) );
}
wp_register_script(
$script,
self::get_url( $script_path_name . '/index', 'js' ),
$script_assets ['dependencies'],
$js_file_version,
true
);
if ( in_array( $script, $translated_scripts, true ) ) {
wp_set_script_translations( $script, 'woocommerce' );
}
} catch ( \Exception $e ) {
// Avoid crashing WordPress if an asset file could not be loaded.
wc_caught_exception( $e, __CLASS__ . '::' . __FUNCTION__, $script_path_name );
}
}
wp_register_style(
'wc-admin-layout',
self::get_url( 'admin-layout/style', 'css' ),
array(),
$css_file_version
);
wp_style_add_data( 'wc-admin-layout', 'rtl', 'replace' );
wp_register_style(
'wc-components',
self::get_url( 'components/style', 'css' ),
array(),
$css_file_version
);
wp_style_add_data( 'wc-components', 'rtl', 'replace' );
wp_register_style(
'wc-product-editor',
self::get_url( 'product-editor/style', 'css' ),
array(),
$css_file_version
);
wp_style_add_data( 'wc-product-editor', 'rtl', 'replace' );
wp_register_style(
'wc-customer-effort-score',
self::get_url( 'customer-effort-score/style', 'css' ),
array(),
$css_file_version
);
wp_style_add_data( 'wc-customer-effort-score', 'rtl', 'replace' );
wp_register_style(
'wc-experimental',
self::get_url( 'experimental/style', 'css' ),
array(),
$css_file_version
);
wp_style_add_data( 'wc-experimental', 'rtl', 'replace' );
wp_localize_script(
WC_ADMIN_APP,
'wcAdminAssets',
array(
'path' => plugins_url( self::get_path( 'js' ), WC_ADMIN_PLUGIN_FILE ),
'version' => $js_file_version,
)
);
wp_register_style(
WC_ADMIN_APP,
self::get_url( 'app/style', 'css' ),
array( 'wc-components', 'wc-admin-layout', 'wc-customer-effort-score', 'wc-product-editor', 'wp-components', 'wc-experimental' ),
$css_file_version
);
wp_style_add_data( WC_ADMIN_APP, 'rtl', 'replace' );
wp_register_style(
'wc-onboarding',
self::get_url( 'onboarding/style', 'css' ),
array(),
$css_file_version
);
wp_style_add_data( 'wc-onboarding', 'rtl', 'replace' );
}
/**
* Injects wp-shared-settings as a dependency if it's present.
*/
public function inject_wc_settings_dependencies() {
if ( wp_script_is( 'wc-settings', 'registered' ) ) {
$handles_for_injection = [
'wc-admin-layout',
'wc-csv',
'wc-currency',
'wc-customer-effort-score',
'wc-navigation',
// NOTE: This should be removed when Gutenberg is updated and
// the notices package is removed from WooCommerce Admin.
'wc-notices',
'wc-number',
'wc-date',
'wc-components',
'wc-tracks',
'wc-product-editor',
];
foreach ( $handles_for_injection as $handle ) {
$script = wp_scripts()->query( $handle, 'registered' );
if ( $script instanceof _WP_Dependency ) {
$script->deps[] = 'wc-settings';
}
}
}
}
/**
* Loads a script
*
* @param string $script_path_name The script path name.
* @param string $script_name Filename of the script to load.
* @param bool $need_translation Whether the script need translations.
*/
public static function register_script( $script_path_name, $script_name, $need_translation = false ) {
$script_assets_filename = self::get_script_asset_filename( $script_path_name, $script_name );
$script_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $script_path_name . '/' . $script_assets_filename;
wp_enqueue_script(
'wc-admin-' . $script_name,
self::get_url( $script_path_name . '/' . $script_name, 'js' ),
array_merge( array( WC_ADMIN_APP ), $script_assets ['dependencies'] ),
self::get_file_version( 'js' ),
true
);
if ( $need_translation ) {
wp_set_script_translations( 'wc-admin-' . $script_name, 'woocommerce' );
}
}
}
WCAdminSharedSettings.php 0000644 00000003006 15154512353 0011412 0 ustar 00 <?php
/**
* Manages the WC Admin settings that need to be pre-loaded.
*/
namespace Automattic\WooCommerce\Internal\Admin;
defined( 'ABSPATH' ) || exit;
/**
* \Automattic\WooCommerce\Internal\Admin\WCAdminSharedSettings class.
*/
class WCAdminSharedSettings {
/**
* Settings prefix used for the window.wcSettings object.
*
* @var string
*/
private $settings_prefix = 'admin';
/**
* Class instance.
*
* @var WCAdminSharedSettings instance
*/
protected static $instance = null;
/**
* Hook into WooCommerce Blocks.
*/
protected function __construct() {
if ( did_action( 'woocommerce_blocks_loaded' ) ) {
$this->on_woocommerce_blocks_loaded();
} else {
add_action( 'woocommerce_blocks_loaded', array( $this, 'on_woocommerce_blocks_loaded' ), 10 );
}
}
/**
* Get class instance.
*
* @return object Instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Adds settings to the Blocks AssetDataRegistry when woocommerce_blocks is loaded.
*
* @return void
*/
public function on_woocommerce_blocks_loaded() {
if ( class_exists( '\Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry' ) ) {
\Automattic\WooCommerce\Blocks\Package::container()->get( \Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry::class )->add(
$this->settings_prefix,
function() {
return apply_filters( 'woocommerce_admin_shared_settings', array() );
},
true
);
}
}
}
WCAdminUser.php 0000644 00000007745 15154512353 0007417 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin;
/**
* WCAdminUser Class.
*/
class WCAdminUser {
/**
* Class instance.
*
* @var WCAdminUser instance
*/
protected static $instance = null;
/**
* Constructor.
*/
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_user_data' ) );
}
/**
* Get class instance.
*
* @return object Instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Registers WooCommerce specific user data to the WordPress user API.
*/
public function register_user_data() {
register_rest_field(
'user',
'is_super_admin',
array(
'get_callback' => function() {
return is_super_admin();
},
'schema' => null,
)
);
register_rest_field(
'user',
'woocommerce_meta',
array(
'get_callback' => array( $this, 'get_user_data_values' ),
'update_callback' => array( $this, 'update_user_data_values' ),
'schema' => null,
)
);
}
/**
* For all the registered user data fields ( Loader::get_user_data_fields ), fetch the data
* for returning via the REST API.
*
* @param WP_User $user Current user.
*/
public function get_user_data_values( $user ) {
$values = array();
foreach ( $this->get_user_data_fields() as $field ) {
$values[ $field ] = self::get_user_data_field( $user['id'], $field );
}
return $values;
}
/**
* For all the registered user data fields ( Loader::get_user_data_fields ), update the data
* for the REST API.
*
* @param array $values The new values for the meta.
* @param WP_User $user The current user.
* @param string $field_id The field id for the user meta.
*/
public function update_user_data_values( $values, $user, $field_id ) {
if ( empty( $values ) || ! is_array( $values ) || 'woocommerce_meta' !== $field_id ) {
return;
}
$fields = $this->get_user_data_fields();
$updates = array();
foreach ( $values as $field => $value ) {
if ( in_array( $field, $fields, true ) ) {
$updates[ $field ] = $value;
self::update_user_data_field( $user->ID, $field, $value );
}
}
return $updates;
}
/**
* We store some WooCommerce specific user meta attached to users endpoint,
* so that we can track certain preferences or values such as the inbox activity panel last open time.
* Additional fields can be added in the function below, and then used via wc-admin's currentUser data.
*
* @return array Fields to expose over the WP user endpoint.
*/
public function get_user_data_fields() {
/**
* Filter user data fields exposed over the WordPress user endpoint.
*
* @since 4.0.0
* @param array $fields Array of fields to expose over the WP user endpoint.
*/
return apply_filters( 'woocommerce_admin_get_user_data_fields', array( 'variable_product_tour_shown' ) );
}
/**
* Helper to update user data fields.
*
* @param int $user_id User ID.
* @param string $field Field name.
* @param mixed $value Field value.
*/
public static function update_user_data_field( $user_id, $field, $value ) {
update_user_meta( $user_id, 'woocommerce_admin_' . $field, $value );
}
/**
* Helper to retrieve user data fields.
*
* Migrates old key prefixes as well.
*
* @param int $user_id User ID.
* @param string $field Field name.
* @return mixed The user field value.
*/
public static function get_user_data_field( $user_id, $field ) {
$meta_value = get_user_meta( $user_id, 'woocommerce_admin_' . $field, true );
// Migrate old meta values (prefix changed from `wc_admin_` to `woocommerce_admin_`).
if ( '' === $meta_value ) {
$old_meta_value = get_user_meta( $user_id, 'wc_admin_' . $field, true );
if ( '' !== $old_meta_value ) {
self::update_user_data_field( $user_id, $field, $old_meta_value );
delete_user_meta( $user_id, 'wc_admin_' . $field );
$meta_value = $old_meta_value;
}
}
return $meta_value;
}
}
WCPayPromotion/Init.php 0000644 00000012223 15154512353 0011056 0 ustar 00 <?php
/**
* Handles wcpay promotion
*/
namespace Automattic\WooCommerce\Internal\Admin\WCPayPromotion;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\DataSourcePoller;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\EvaluateSuggestion;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\PaymentGatewaySuggestionsDataSourcePoller as PaymentGatewaySuggestionsDataSourcePoller;
use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
/**
* WC Pay Promotion engine.
*/
class Init {
const EXPLAT_VARIATION_PREFIX = 'woocommerce_wc_pay_promotion_payment_methods_table_';
/**
* Constructor.
*/
public function __construct() {
include_once __DIR__ . '/WCPaymentGatewayPreInstallWCPayPromotion.php';
$is_payments_page = isset( $_GET['page'] ) && $_GET['page'] === 'wc-settings' && isset( $_GET['tab'] ) && $_GET['tab'] === 'checkout'; // phpcs:ignore WordPress.Security.NonceVerification
if ( ! wp_is_json_request() && ! $is_payments_page ) {
return;
}
add_filter( 'woocommerce_payment_gateways', array( __CLASS__, 'possibly_register_pre_install_wc_pay_promotion_gateway' ) );
add_filter( 'option_woocommerce_gateway_order', [ __CLASS__, 'set_gateway_top_of_list' ] );
add_filter( 'default_option_woocommerce_gateway_order', [ __CLASS__, 'set_gateway_top_of_list' ] );
$rtl = is_rtl() ? '.rtl' : '';
wp_enqueue_style(
'wc-admin-payment-method-promotions',
WCAdminAssets::get_url( "payment-method-promotions/style{$rtl}", 'css' ),
array( 'wp-components' ),
WCAdminAssets::get_file_version( 'css' )
);
WCAdminAssets::register_script( 'wp-admin-scripts', 'payment-method-promotions', true );
}
/**
* Possibly registers the pre install wc pay promoted gateway.
*
* @param array $gateways list of gateway classes.
* @return array list of gateway classes.
*/
public static function possibly_register_pre_install_wc_pay_promotion_gateway( $gateways ) {
if ( self::can_show_promotion() && ! WCPaymentGatewayPreInstallWCPayPromotion::is_dismissed() ) {
$gateways[] = 'Automattic\WooCommerce\Internal\Admin\WCPayPromotion\WCPaymentGatewayPreInstallWCPayPromotion';
}
return $gateways;
}
/**
* Checks if promoted gateway can be registered.
*
* @return boolean if promoted gateway should be registered.
*/
public static function can_show_promotion() {
// Check if WC Pay is enabled.
if ( class_exists( '\WC_Payments' ) ) {
return false;
}
if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) === 'no' ) {
return false;
}
if ( ! apply_filters( 'woocommerce_allow_marketplace_suggestions', true ) ) {
return false;
}
$wc_pay_spec = self::get_wc_pay_promotion_spec();
if ( ! $wc_pay_spec ) {
return false;
}
return true;
}
/**
* By default, new payment gateways are put at the bottom of the list on the admin "Payments" settings screen.
* For visibility, we want WooCommerce Payments to be at the top of the list.
*
* @param array $ordering Existing ordering of the payment gateways.
*
* @return array Modified ordering.
*/
public static function set_gateway_top_of_list( $ordering ) {
$ordering = (array) $ordering;
$id = WCPaymentGatewayPreInstallWCPayPromotion::GATEWAY_ID;
// Only tweak the ordering if the list hasn't been reordered with WooCommerce Payments in it already.
if ( ! isset( $ordering[ $id ] ) || ! is_numeric( $ordering[ $id ] ) ) {
$is_empty = empty( $ordering ) || ( count( $ordering ) === 1 && $ordering[0] === false );
$ordering[ $id ] = $is_empty ? 0 : ( min( $ordering ) - 1 );
}
return $ordering;
}
/**
* Get WC Pay promotion spec.
*/
public static function get_wc_pay_promotion_spec() {
$promotions = self::get_promotions();
$wc_pay_promotion_spec = array_values(
array_filter(
$promotions,
function( $promotion ) {
return isset( $promotion->plugins ) && in_array( 'woocommerce-payments', $promotion->plugins, true );
}
)
);
return current( $wc_pay_promotion_spec );
}
/**
* Go through the specs and run them.
*/
public static function get_promotions() {
$suggestions = array();
$specs = self::get_specs();
foreach ( $specs as $spec ) {
$suggestion = EvaluateSuggestion::evaluate( $spec );
$suggestions[] = $suggestion;
}
return array_values(
array_filter(
$suggestions,
function( $suggestion ) {
return ! property_exists( $suggestion, 'is_visible' ) || $suggestion->is_visible;
}
)
);
}
/**
* Get merchant WooPay eligibility.
*/
public static function is_woopay_eligible() {
$wcpay_promotion = self::get_wc_pay_promotion_spec();
return $wcpay_promotion && 'woocommerce_payments:woopay' === $wcpay_promotion->id;
}
/**
* Delete the specs transient.
*/
public static function delete_specs_transient() {
WCPayPromotionDataSourcePoller::get_instance()->delete_specs_transient();
}
/**
* Get specs or fetch remotely if they don't exist.
*/
public static function get_specs() {
if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) === 'no' ) {
return array();
}
return WCPayPromotionDataSourcePoller::get_instance()->get_specs_from_data_sources();
}
}
WCPayPromotion/WCPayPromotionDataSourcePoller.php 0000644 00000001425 15154512353 0016200 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin\WCPayPromotion;
use Automattic\WooCommerce\Admin\DataSourcePoller;
/**
* Specs data source poller class for WooCommerce Payment Promotion.
*/
class WCPayPromotionDataSourcePoller extends DataSourcePoller {
const ID = 'payment_method_promotion';
/**
* Default data sources array.
*/
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/payment-gateway-suggestions/1.0/payment-method/promotions.json',
);
/**
* Class instance.
*
* @var Analytics instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self( self::ID, self::DATA_SOURCES );
}
return self::$instance;
}
}
WCPayPromotion/WCPaymentGatewayPreInstallWCPayPromotion.php 0000644 00000003224 15154512353 0020156 0 ustar 00 <?php
/**
* Class WCPaymentGatewayPreInstallWCPayPromotion
*
* @package WooCommerce\Admin
*/
namespace Automattic\WooCommerce\Internal\Admin\WCPayPromotion;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* A Psuedo WCPay gateway class.
*
* @extends WC_Payment_Gateway
*/
class WCPaymentGatewayPreInstallWCPayPromotion extends \WC_Payment_Gateway {
const GATEWAY_ID = 'pre_install_woocommerce_payments_promotion';
/**
* Constructor
*/
public function __construct() {
$wc_pay_spec = Init::get_wc_pay_promotion_spec();
if ( ! $wc_pay_spec ) {
return;
}
$this->id = static::GATEWAY_ID;
$this->method_title = $wc_pay_spec->title;
if ( property_exists( $wc_pay_spec, 'sub_title' ) ) {
$this->title = sprintf( '<span class="gateway-subtitle" >%s</span>', $wc_pay_spec->sub_title );
}
$this->method_description = $wc_pay_spec->content;
$this->has_fields = false;
// Get setting values.
$this->enabled = false;
// Load the settings.
$this->init_form_fields();
$this->init_settings();
}
/**
* Initialise Gateway Settings Form Fields.
*/
public function init_form_fields() {
$this->form_fields = array(
'is_dismissed' => array(
'title' => __( 'Dismiss', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Dismiss the gateway', 'woocommerce' ),
'default' => 'no',
),
);
}
/**
* Check if the promotional gateaway has been dismissed.
*
* @return bool
*/
public static function is_dismissed() {
$settings = get_option( 'woocommerce_' . self::GATEWAY_ID . '_settings', array() );
return isset( $settings['is_dismissed'] ) && 'yes' === $settings['is_dismissed'];
}
}
WcPayWelcomePage.php 0000644 00000033712 15154512353 0010423 0 ustar 00 <?php
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\WooCommercePayments;
use Automattic\WooCommerce\Admin\WCAdminHelper;
use Automattic\WooCommerce\Admin\PageController;
/**
* Class WCPayWelcomePage
*
* @package Automattic\WooCommerce\Admin\Features
*/
class WcPayWelcomePage {
const CACHE_TRANSIENT_NAME = 'wcpay_welcome_page_incentive';
const HAD_WCPAY_OPTION_NAME = 'wcpay_was_in_use';
/**
* Plugin instance.
*
* @var WcPayWelcomePage
*/
protected static $instance = null;
/**
* Main Instance.
*/
public static function instance() {
self::$instance = is_null( self::$instance ) ? new self() : self::$instance;
return self::$instance;
}
/**
* Eligible incentive for the store.
*
* @var array|null
*/
private $incentive = null;
/**
* WCPayWelcomePage constructor.
*/
public function __construct() {
add_action( 'admin_menu', [ $this, 'register_payments_welcome_page' ] );
add_filter( 'woocommerce_admin_shared_settings', [ $this, 'shared_settings' ] );
add_filter( 'woocommerce_admin_allowed_promo_notes', [ $this, 'allowed_promo_notes' ] );
add_filter( 'woocommerce_admin_woopayments_onboarding_task_badge', [ $this, 'onboarding_task_badge' ] );
}
/**
* Whether the WooPayments welcome page should be visible.
*
* @return boolean
*/
public function must_be_visible(): bool {
// The WooPayments plugin must not be active.
if ( $this->is_wcpay_active() ) {
return false;
}
// Suggestions not disabled via a setting.
if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) === 'no' ) {
return false;
}
/**
* Filter allow marketplace suggestions.
*
* User can disable all suggestions via filter.
*
* @since 3.6.0
*/
if ( ! apply_filters( 'woocommerce_allow_marketplace_suggestions', true ) ) {
return false;
}
// An incentive must be available.
if ( empty( $this->get_incentive() ) ) {
return false;
}
// Incentive not manually dismissed.
if ( $this->is_incentive_dismissed() ) {
return false;
}
return true;
}
/**
* Registers the WooPayments welcome page.
*/
public function register_payments_welcome_page() {
global $menu;
if ( ! $this->must_be_visible() ) {
return;
}
$menu_icon = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI4NTIiIGhlaWdodD0iNjg0Ij48cGF0aCBmaWxsPSIjYTJhYWIyIiBkPSJNODIgODZ2NTEyaDY4NFY4NlptMCA1OThjLTQ4IDAtODQtMzgtODQtODZWODZDLTIgMzggMzQgMCA4MiAwaDY4NGM0OCAwIDg0IDM4IDg0IDg2djUxMmMwIDQ4LTM2IDg2LTg0IDg2em0zODQtNTU2djQ0aDg2djg0SDM4MnY0NGgxMjhjMjQgMCA0MiAxOCA0MiA0MnYxMjhjMCAyNC0xOCA0Mi00MiA0MmgtNDR2NDRoLTg0di00NGgtODZ2LTg0aDE3MHYtNDRIMzM4Yy0yNCAwLTQyLTE4LTQyLTQyVjIxNGMwLTI0IDE4LTQyIDQyLTQyaDQ0di00NHoiLz48L3N2Zz4=';
$menu_data = [
'id' => 'wc-calypso-bridge-payments-welcome-page',
'title' => esc_html__( 'Payments', 'woocommerce' ),
'path' => '/wc-pay-welcome-page',
'position' => '56',
'nav_args' => [
'title' => esc_html__( 'WooPayments', 'woocommerce' ),
'is_category' => false,
'menuId' => 'plugins',
'is_top_level' => true,
],
'icon' => $menu_icon,
];
wc_admin_register_page( $menu_data );
// Registering a top level menu via wc_admin_register_page doesn't work when the new
// nav is enabled. The new nav disabled everything, except the 'WooCommerce' menu.
// We need to register this menu via add_menu_page so that it doesn't become a child of
// WooCommerce menu.
if ( get_option( 'woocommerce_navigation_enabled', 'no' ) === 'yes' ) {
$menu_with_nav_data = [
esc_html__( 'Payments', 'woocommerce' ),
esc_html__( 'Payments', 'woocommerce' ),
'view_woocommerce_reports',
'admin.php?page=wc-admin&path=/wc-pay-welcome-page',
null,
$menu_icon,
56,
];
call_user_func_array( 'add_menu_page', $menu_with_nav_data );
}
// Add badge.
$badge = ' <span class="wcpay-menu-badge awaiting-mod count-1"><span class="plugin-count">1</span></span>';
foreach ( $menu as $index => $menu_item ) {
// Only add the badge markup if not already present and the menu item is the WooPayments menu item.
if ( false === strpos( $menu_item[0], $badge )
&& ( 'wc-admin&path=/wc-pay-welcome-page' === $menu_item[2]
|| 'admin.php?page=wc-admin&path=/wc-pay-welcome-page' === $menu_item[2] )
) {
$menu[ $index ][0] .= $badge; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
// One menu item with a badge is more than enough.
break;
}
}
}
/**
* Adds shared settings for the WooPayments incentive.
*
* @param array $settings Shared settings.
* @return array
*/
public function shared_settings( $settings ): array {
// Return early if not on a wc-admin powered page.
if ( ! PageController::is_admin_page() ) {
return $settings;
}
// Return early if the incentive must not be visible.
if ( ! $this->must_be_visible() ) {
return $settings;
}
$settings['wcpayWelcomePageIncentive'] = $this->get_incentive();
return $settings;
}
/**
* Adds allowed promo notes from the WooPayments incentive.
*
* @param array $promo_notes Allowed promo notes.
* @return array
*/
public function allowed_promo_notes( $promo_notes = [] ): array {
// Return early if the incentive must not be visible.
if ( ! $this->must_be_visible() ) {
return $promo_notes;
}
// Add our incentive ID to the promo notes.
$promo_notes[] = $this->get_incentive()['id'];
return $promo_notes;
}
/**
* Adds the WooPayments incentive badge to the onboarding task.
*
* @param string $badge Current badge.
*
* @return string
*/
public function onboarding_task_badge( string $badge ): string {
// Return early if the incentive must not be visible.
if ( ! $this->must_be_visible() ) {
return $badge;
}
return $this->get_incentive()['task_badge'] ?? $badge;
}
/**
* Check if the WooPayments payment gateway is active and set up or was at some point,
* or there are orders processed with it, at some moment.
*
* @return boolean
*/
private function has_wcpay(): bool {
// First, get the stored value, if it exists.
// This way we avoid costly DB queries and API calls.
// Basically, we only want to know if WooPayments was in use in the past.
// Since the past can't be changed, neither can this value.
$had_wcpay = get_option( self::HAD_WCPAY_OPTION_NAME );
if ( false !== $had_wcpay ) {
return $had_wcpay === 'yes';
}
// We need to determine the value.
// Start with the assumption that the store didn't have WooPayments in use.
$had_wcpay = false;
// We consider the store to have WooPayments if there is meaningful account data in the WooPayments account cache.
// This implies that WooPayments was active at some point and that it was connected.
// If WooPayments is active right now, we will not get to this point since the plugin is active check is done first.
if ( $this->has_wcpay_account_data() ) {
$had_wcpay = true;
}
// If there is at least one order processed with WooPayments, we consider the store to have WooPayments.
if ( false === $had_wcpay && ! empty(
wc_get_orders(
[
'payment_method' => 'woocommerce_payments',
'return' => 'ids',
'limit' => 1,
]
)
) ) {
$had_wcpay = true;
}
// Store the value for future use.
update_option( self::HAD_WCPAY_OPTION_NAME, $had_wcpay ? 'yes' : 'no' );
return $had_wcpay;
}
/**
* Check if the WooPayments plugin is active.
*
* @return boolean
*/
private function is_wcpay_active(): bool {
return class_exists( '\WC_Payments' );
}
/**
* Check if there is meaningful data in the WooPayments account cache.
*
* @return boolean
*/
private function has_wcpay_account_data(): bool {
$account_data = get_option( 'wcpay_account_data', [] );
if ( ! empty( $account_data['data']['account_id'] ) ) {
return true;
}
return false;
}
/**
* Check if the current incentive has been manually dismissed.
*
* @return boolean
*/
private function is_incentive_dismissed(): bool {
$dismissed_incentives = get_option( 'wcpay_welcome_page_incentives_dismissed', [] );
// If there are no dismissed incentives, return early.
if ( empty( $dismissed_incentives ) ) {
return false;
}
// Return early if there is no eligible incentive.
$incentive = $this->get_incentive();
if ( empty( $incentive ) ) {
return true;
}
// Search the incentive ID in the dismissed incentives list.
if ( in_array( $incentive['id'], $dismissed_incentives, true ) ) {
return true;
}
return false;
}
/**
* Fetches and caches eligible incentive from the WooPayments API.
*
* @return array|null Array of eligible incentive or null.
*/
private function get_incentive(): ?array {
// Return in-memory cached incentive if it is set.
if ( isset( $this->incentive ) ) {
return $this->incentive;
}
// Get the cached data.
$cache = get_transient( self::CACHE_TRANSIENT_NAME );
// If the cached data is not expired and it's a WP_Error,
// it means there was an API error previously and we should not retry just yet.
if ( is_wp_error( $cache ) ) {
// Initialize the in-memory cache and return it.
$this->incentive = [];
return $this->incentive;
}
// Gather the store context data.
$store_context = [
// Store ISO-2 country code, e.g. `US`.
'country' => WC()->countries->get_base_country(),
// Store locale, e.g. `en_US`.
'locale' => get_locale(),
// WooCommerce active for duration in seconds.
'active_for' => WCAdminHelper::get_wcadmin_active_for_in_seconds(),
// Whether the store has paid orders in the last 90 days.
'has_orders' => ! empty(
wc_get_orders(
[
'status' => [ 'wc-completed', 'wc-processing' ],
'date_created' => '>=' . strtotime( '-90 days' ),
'return' => 'ids',
'limit' => 1,
]
)
),
// Whether the store has at least one payment gateway enabled.
'has_payments' => ! empty( WC()->payment_gateways()->get_available_payment_gateways() ),
'has_wcpay' => $this->has_wcpay(),
];
// Fingerprint the store context through a hash of certain entries.
$store_context_hash = $this->generate_context_hash( $store_context );
// Use the transient cached incentive if it exists, it is not expired,
// and the store context hasn't changed since we last requested from the WooPayments API (based on context hash).
if ( false !== $cache
&& ! empty( $cache['context_hash'] ) && is_string( $cache['context_hash'] )
&& hash_equals( $store_context_hash, $cache['context_hash'] ) ) {
// We have a store context hash and it matches with the current context one.
// We can use the cached incentive data.
// Store the incentive in the in-memory cache and return it.
$this->incentive = $cache['incentive'] ?? [];
return $this->incentive;
}
// By this point, we have an expired transient or the store context has changed.
// Query for incentives by calling the WooPayments API.
$url = add_query_arg(
$store_context,
'https://public-api.wordpress.com/wpcom/v2/wcpay/incentives',
);
$response = wp_remote_get(
$url,
[
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
]
);
// Return early if there is an error, waiting 6 hours before the next attempt.
if ( is_wp_error( $response ) ) {
// Store a trimmed down, lightweight error.
$error = new \WP_Error(
$response->get_error_code(),
$response->get_error_message(),
wp_remote_retrieve_response_code( $response )
);
// Store the error in the transient so we know this is due to an API error.
set_transient( self::CACHE_TRANSIENT_NAME, $error, HOUR_IN_SECONDS * 6 );
// Initialize the in-memory cache and return it.
$this->incentive = [];
return $this->incentive;
}
$cache_for = wp_remote_retrieve_header( $response, 'cache-for' );
// Initialize the in-memory cache.
$this->incentive = [];
if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
// Decode the results, falling back to an empty array.
$results = json_decode( wp_remote_retrieve_body( $response ), true ) ?? [];
// Find all `welcome_page` incentives.
$incentives = array_filter(
$results,
function( $incentive ) {
return 'welcome_page' === $incentive['type'];
}
);
// Use the first found matching incentive or empty array if none was found.
// Store incentive in the in-memory cache.
$this->incentive = empty( $incentives ) ? [] : reset( $incentives );
}
// Skip transient cache if `cache-for` header equals zero.
if ( '0' === $cache_for ) {
// If we have a transient cache that is not expired, delete it so there are no leftovers.
if ( false !== $cache ) {
delete_transient( self::CACHE_TRANSIENT_NAME );
}
return $this->incentive;
}
// Store incentive in transient cache (together with the context hash) for the given number of seconds
// or 1 day in seconds. Also attach a timestamp to the transient data so we know when we last fetched.
set_transient(
self::CACHE_TRANSIENT_NAME,
[
'incentive' => $this->incentive,
'context_hash' => $store_context_hash,
'timestamp' => time(),
],
! empty( $cache_for ) ? (int) $cache_for : DAY_IN_SECONDS
);
return $this->incentive;
}
/**
* Generate a hash from the store context data.
*
* @param array $context The store context data.
*
* @return string The context hash.
*/
private function generate_context_hash( array $context ): string {
// Include only certain entries in the context hash.
// We need only discrete, user-interaction dependent data.
// Entries like `active_for` have no place in the hash generation since they change automatically.
return md5(
wp_json_encode(
[
'country' => $context['country'] ?? '',
'locale' => $context['locale'] ?? '',
'has_orders' => $context['has_orders'] ?? false,
'has_payments' => $context['has_payments'] ?? false,
'has_wcpay' => $context['has_wcpay'] ?? false,
]
)
);
}
}
BulkEdit/BulkEditInitializer.php 0000644 00000002112 15154522323 0012665 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\BulkEdit;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use WP_Post;
defined( 'ABSPATH' ) || exit;
/**
* Class BulkEditInitializer
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\BulkEdit
*/
class BulkEditInitializer implements Service, Registerable {
/**
* Register a service.
*/
public function register(): void {
add_action( 'save_post', [ $this, 'bulk_edit_hook' ], 10, 2 );
}
/**
* Offers a way to hook into save post without causing an infinite loop
* when bulk saving info.
*
* @since 3.0.0
* @param int $post_id Post ID being saved.
* @param WP_Post $post Post object being saved.
*/
public function bulk_edit_hook( int $post_id, WP_Post $post ): void {
remove_action( 'save_post', [ $this, 'bulk_edit_hook' ] );
do_action( 'bulk_edit_save_post', $post_id, $post );
add_action( 'save_post', [ $this, 'bulk_edit_hook' ], 10, 2 );
}
}
BulkEdit/BulkEditInterface.php 0000644 00000002131 15154522323 0012303 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\BulkEdit;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Conditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use WP_Post;
defined( 'ABSPATH' ) || exit;
interface BulkEditInterface extends Service, Conditional {
public const SCREEN_COUPON = 'shop_coupon';
/**
* Function that renders view of custom bulk edit fields.
*
* @param string $column_name Column being shown.
* @param string $post_type Post type being shown.
*/
public function render_view( string $column_name, string $post_type );
/**
* The screen or screens on which to show the box (such as a post type, 'link', or 'comment').
*
* Default is the current screen.
*
* @return string
*/
public function get_screen(): string;
/**
* Handle the bulk edit submission.
*
* @param int $post_id Post ID being saved.
* @param WP_Post $post Post object being saved.
*
* @return int $post_id
*/
public function handle_submission( int $post_id, WP_Post $post ): int;
}
BulkEdit/CouponBulkEdit.php 0000644 00000010604 15154522323 0011652 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\BulkEdit;
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponMetaHandler;
use Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits\ViewHelperTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\AdminConditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\TargetAudience;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility;
use WC_Coupon;
defined( 'ABSPATH' ) || exit;
/**
* Class CouponBulkEdit
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\BulkEdit
*/
class CouponBulkEdit implements BulkEditInterface, Registerable {
use AdminConditional;
use ViewHelperTrait;
protected const VIEW_PATH = 'views/bulk-edit/shop_coupon.php';
protected const TARGET_COLUMN = 'usage';
/**
* @var CouponMetaHandler
*/
protected $meta_handler;
/**
* @var MerchantCenterService
*/
protected $merchant_center;
/**
* @var TargetAudience
*/
protected $target_audience;
/**
* CouponBulkEdit constructor.
*
* @param CouponMetaHandler $meta_handler
* @param MerchantCenterService $merchant_center
* @param TargetAudience $target_audience
*/
public function __construct( CouponMetaHandler $meta_handler, MerchantCenterService $merchant_center, TargetAudience $target_audience ) {
$this->meta_handler = $meta_handler;
$this->merchant_center = $merchant_center;
$this->target_audience = $target_audience;
}
/**
* Register a service.
*/
public function register(): void {
add_action( 'bulk_edit_custom_box', [ $this, 'render_view' ], 10, 2 );
add_action( 'bulk_edit_save_post', [ $this, 'handle_submission' ], 10, 2 );
}
/**
* The screen on which to show the bulk edit view.
*
* @return string
*/
public function get_screen(): string {
return self::SCREEN_COUPON;
}
/**
* Render the coupon bulk edit view.
*
* @param string $column_name Column being shown.
* @param string $post_type Post type being shown.
*/
public function render_view( $column_name, $post_type ) {
if ( $this->get_screen() !== $post_type || self::TARGET_COLUMN !== $column_name ) {
return;
}
if ( ! $this->merchant_center->is_setup_complete() ) {
return;
}
$target_country = $this->target_audience->get_main_target_country();
if ( ! $this->merchant_center->is_promotion_supported_country( $target_country ) ) {
return;
}
include path_join( dirname( __DIR__, 3 ), self::VIEW_PATH );
}
/**
* Handle the coupon bulk edit submission.
*
* @param int $post_id Post ID being saved.
* @param object $post Post object being saved.
*
* @return int $post_id
*/
public function handle_submission( int $post_id, $post ): int {
$request_data = $this->request_data();
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( Constants::is_true( 'DOING_AUTOSAVE' ) ) {
return $post_id;
}
// Don't save revisions and autosaves.
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) || $this->get_screen() !== $post->post_type || ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
// Check nonce.
if ( ! isset( $request_data['woocommerce_gla_bulk_edit'] ) || ! wp_verify_nonce( $request_data['woocommerce_gla_bulk_edit_nonce'], 'woocommerce_gla_bulk_edit_nonce' ) ) {
return $post_id;
}
if ( ! empty( $request_data['change_channel_visibility'] ) ) {
// Get the coupon and save.
$coupon = new WC_Coupon( $post_id );
$visibility =
ChannelVisibility::cast( sanitize_key( $request_data['change_channel_visibility'] ) );
if ( $this->meta_handler->get_visibility( $coupon ) !== $visibility ) {
$this->meta_handler->update_visibility( $coupon, $visibility );
do_action( 'woocommerce_gla_bulk_update_coupon', $post_id );
}
}
return $post_id;
}
/**
* Get the current request data ($_REQUEST superglobal).
* This method is added to ease unit testing.
*
* @return array The $_REQUEST superglobal.
*/
protected function request_data(): array {
// Nonce must be verified manually.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
return $_REQUEST;
}
}
Input/BooleanSelect.php 0000644 00000001466 15154522323 0011104 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;
defined( 'ABSPATH' ) || exit;
/**
* Class BooleanSelect
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input
*/
class BooleanSelect extends Select {
/**
* @return array
*/
public function get_options(): array {
return [
'' => __( 'Default', 'google-listings-and-ads' ),
'yes' => __( 'Yes', 'google-listings-and-ads' ),
'no' => __( 'No', 'google-listings-and-ads' ),
];
}
/**
* Return the data used for the input's view.
*
* @return array
*/
public function get_view_data(): array {
$view_data = parent::get_view_data();
if ( is_bool( $view_data['value'] ) ) {
$view_data['value'] = wc_bool_to_string( $view_data['value'] );
}
return $view_data;
}
}
Input/DateTime.php 0000644 00000004266 15154522323 0010062 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;
use DateTimeZone;
use Exception;
use WC_DateTime;
defined( 'ABSPATH' ) || exit;
/**
* Class DateTime
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input
*
* @since 1.5.0
*/
class DateTime extends Input {
/**
* DateTime constructor.
*/
public function __construct() {
parent::__construct( 'datetime', 'google-listings-and-ads/product-date-time-field' );
}
/**
* Return the data used for the input's view.
*
* @return array
*/
public function get_view_data(): array {
$view_data = parent::get_view_data();
if ( ! empty( $this->get_value() ) ) {
try {
// Display the time in site's local timezone.
$datetime = new WC_DateTime( $this->get_value(), new DateTimeZone( 'UTC' ) );
$datetime->setTimezone( new DateTimeZone( $this->get_local_tz_string() ) );
$view_data['value'] = $datetime->format( 'Y-m-d H:i:s' );
$view_data['date'] = $datetime->format( 'Y-m-d' );
$view_data['time'] = $datetime->format( 'H:i' );
} catch ( Exception $e ) {
do_action( 'woocommerce_gla_exception', $e, __METHOD__ );
$view_data['value'] = '';
$view_data['date'] = '';
$view_data['time'] = '';
}
}
return $view_data;
}
/**
* Set the form's data.
*
* @param mixed $data
*
* @return void
*/
public function set_data( $data ): void {
if ( is_array( $data ) ) {
if ( ! empty( $data['date'] ) ) {
$date = $data['date'] ?? '';
$time = $data['time'] ?? '';
$data = sprintf( '%s%s', $date, $time );
} else {
$data = '';
}
}
if ( ! empty( $data ) ) {
try {
// Store the time in UTC.
$datetime = new WC_DateTime( $data, new DateTimeZone( $this->get_local_tz_string() ) );
$datetime->setTimezone( new DateTimeZone( 'UTC' ) );
$data = (string) $datetime;
} catch ( Exception $e ) {
do_action( 'woocommerce_gla_exception', $e, __METHOD__ );
$data = '';
}
}
parent::set_data( $data );
}
/**
* Get site's local timezone string from WordPress settings.
*
* @return string
*/
protected function get_local_tz_string(): string {
return wp_timezone_string();
}
}
Input/Form.php 0000644 00000011107 15154522323 0007261 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\ValidateInterface;
defined( 'ABSPATH' ) || exit;
/**
* Class Form
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input
*/
class Form implements FormInterface {
use ValidateInterface;
/**
* @var string
*/
protected $name = '';
/**
* @var mixed
*/
protected $data;
/**
* @var FormInterface[]
*/
protected $children = [];
/**
* @var FormInterface
*/
protected $parent;
/**
* @var bool
*/
protected $is_submitted = false;
/**
* Form constructor.
*
* @param mixed $data
*/
public function __construct( $data = null ) {
$this->set_data( $data );
}
/**
* @return string
*/
public function get_name(): string {
return $this->name;
}
/**
* @param string $name
*
* @return FormInterface
*/
public function set_name( string $name ): FormInterface {
$this->name = $name;
return $this;
}
/**
* @return FormInterface[]
*/
public function get_children(): array {
return $this->children;
}
/**
* Add a child form.
*
* @param FormInterface $form
*
* @return FormInterface
*
* @throws FormException If form is already submitted.
*/
public function add( FormInterface $form ): FormInterface {
if ( $this->is_submitted ) {
throw FormException::cannot_modify_submitted();
}
$this->children[ $form->get_name() ] = $form;
$form->set_parent( $this );
return $this;
}
/**
* Remove a child with the given name from the form's children.
*
* @param string $name
*
* @return FormInterface
*
* @throws FormException If form is already submitted.
*/
public function remove( string $name ): FormInterface {
if ( $this->is_submitted ) {
throw FormException::cannot_modify_submitted();
}
if ( isset( $this->children[ $name ] ) ) {
$this->children[ $name ]->set_parent( null );
unset( $this->children[ $name ] );
}
return $this;
}
/**
* Whether the form contains a child with the given name.
*
* @param string $name
*
* @return bool
*/
public function has( string $name ): bool {
return isset( $this->children[ $name ] );
}
/**
* @param FormInterface|null $form
*
* @return void
*/
public function set_parent( ?FormInterface $form ): void {
$this->parent = $form;
}
/**
* @return FormInterface|null
*/
public function get_parent(): ?FormInterface {
return $this->parent;
}
/**
* Return the form's data.
*
* @return mixed
*/
public function get_data() {
return $this->data;
}
/**
* Set the form's data.
*
* @param mixed $data
*
* @return void
*/
public function set_data( $data ): void {
if ( is_array( $data ) && ! empty( $this->children ) ) {
$this->data = $this->map_children_data( $data );
} else {
if ( is_string( $data ) ) {
$data = trim( $data );
}
$this->data = $data;
}
}
/**
* Maps the data to each child and returns the mapped data.
*
* @param array $data
*
* @return array
*/
protected function map_children_data( array $data ): array {
$children_data = [];
foreach ( $data as $key => $datum ) {
if ( isset( $this->children[ $key ] ) ) {
$this->children[ $key ]->set_data( $datum );
$children_data[ $key ] = $this->children[ $key ]->get_data();
}
}
return $children_data;
}
/**
* Submit the form.
*
* @param array $submitted_data
*/
public function submit( array $submitted_data = [] ): void {
// todo: add form validation
if ( ! $this->is_submitted ) {
$this->is_submitted = true;
$this->set_data( $submitted_data );
}
}
/**
* Return the data used for the form's view.
*
* @return array
*/
public function get_view_data(): array {
$view_data = [
'name' => $this->get_view_name(),
'is_root' => $this->is_root(),
'children' => [],
];
foreach ( $this->get_children() as $index => $form ) {
$view_data['children'][ $index ] = $form->get_view_data();
}
return $view_data;
}
/**
* Return the name used for the form's view.
*
* @return string
*/
public function get_view_name(): string {
return $this->is_root() ? sprintf( 'gla_%s', $this->get_name() ) : sprintf( '%s[%s]', $this->get_parent()->get_view_name(), $this->get_name() );
}
/**
* Whether this is the root form (i.e. has no parents).
*
* @return bool
*/
public function is_root(): bool {
return null === $this->parent;
}
/**
* Whether the form has been already submitted.
*
* @return bool
*/
public function is_submitted(): bool {
return $this->is_submitted;
}
}
Input/FormException.php 0000644 00000001234 15154522323 0011140 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\GoogleListingsAndAdsException;
use Exception;
defined( 'ABSPATH' ) || exit;
/**
* Class FormException
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product
*/
class FormException extends Exception implements GoogleListingsAndAdsException {
/**
* Return a new instance of the exception when a submitted form is being modified.
*
* @return static
*/
public static function cannot_modify_submitted(): FormException {
return new static( 'You cannot modify a submitted form.' );
}
}
Input/FormInterface.php 0000644 00000004170 15154522323 0011104 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;
defined( 'ABSPATH' ) || exit;
/**
* Interface FormInterface
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input
*/
interface FormInterface {
/**
* Return the form's data.
*
* @return mixed
*/
public function get_data();
/**
* Set the form's data.
*
* @param mixed $data
*
* @return void
*/
public function set_data( $data ): void;
/**
* Return the form name.
*
* @return string
*/
public function get_name(): string;
/**
* Set the form's name.
*
* @param string $name
*
* @return FormInterface
*/
public function set_name( string $name ): FormInterface;
/**
* Submit the form.
*
* @param array $submitted_data
*
* @return void
*/
public function submit( array $submitted_data = [] ): void;
/**
* Return the data used for the form's view.
*
* @return array
*/
public function get_view_data(): array;
/**
* Return the name used for the form's view.
*
* @return string
*/
public function get_view_name(): string;
/**
* @return FormInterface[]
*/
public function get_children(): array;
/**
* Add a child form.
*
* @param FormInterface $form
*
* @return FormInterface
*/
public function add( FormInterface $form ): FormInterface;
/**
* Remove a child with the given name from the form's children.
*
* @param string $name
*
* @return FormInterface
*/
public function remove( string $name ): FormInterface;
/**
* Whether the form contains a child with the given name.
*
* @param string $name
*
* @return bool
*/
public function has( string $name ): bool;
/**
* @param FormInterface|null $form
*
* @return void
*/
public function set_parent( ?FormInterface $form ): void;
/**
* @return FormInterface|null
*/
public function get_parent(): ?FormInterface;
/**
* If this is the root form (i.e. has no parents)
*
* @return bool
*/
public function is_root(): bool;
/**
* Whether the form has been already submitted.
*
* @return bool
*/
public function is_submitted(): bool;
}
Input/Input.php 0000644 00000012644 15154522323 0007464 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
defined( 'ABSPATH' ) || exit;
/**
* Class Input
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input
*/
class Input extends Form implements InputInterface {
use PluginHelper;
/**
* @var string
*/
protected $id;
/**
* @var string
*/
protected $type;
/**
* @var string
*/
protected $block_name;
/**
* @var array
*/
protected $block_attributes = [];
/**
* @var string
*/
protected $label;
/**
* @var string
*/
protected $description;
/**
* @var mixed
*/
protected $value;
/**
* @var bool
*/
protected $is_readonly = false;
/**
* @var bool
*/
protected $is_hidden = false;
/**
* Input constructor.
*
* @param string $type
* @param string $block_name The name of a generic product block in WooCommerce core or a custom block in this extension.
*/
public function __construct( string $type, string $block_name ) {
$this->type = $type;
$this->block_name = $block_name;
parent::__construct();
}
/**
* @return string|null
*/
public function get_id(): ?string {
return $this->id;
}
/**
* @return string
*/
public function get_type(): string {
return $this->type;
}
/**
* @return string|null
*/
public function get_label(): ?string {
return $this->label;
}
/**
* @return string|null
*/
public function get_description(): ?string {
return $this->description;
}
/**
* @return mixed
*/
public function get_value() {
return $this->get_data();
}
/**
* @param string|null $id
*
* @return InputInterface
*/
public function set_id( ?string $id ): InputInterface {
$this->id = $id;
return $this;
}
/**
* @param string|null $label
*
* @return InputInterface
*/
public function set_label( ?string $label ): InputInterface {
$this->label = $label;
return $this;
}
/**
* @param string|null $description
*
* @return InputInterface
*/
public function set_description( ?string $description ): InputInterface {
$this->description = $description;
return $this;
}
/**
* @param mixed $value
*
* @return InputInterface
*/
public function set_value( $value ): InputInterface {
$this->set_data( $value );
return $this;
}
/**
* @return bool
*/
public function is_readonly(): bool {
return $this->is_readonly;
}
/**
* @param bool $value
*
* @return InputInterface
*/
public function set_readonly( bool $value ): InputInterface {
$this->is_readonly = $value;
return $this;
}
/**
* @param bool $value
*
* @return InputInterface
*/
public function set_hidden( bool $value ): InputInterface {
$this->is_hidden = $value;
return $this;
}
/**
* @return bool
*/
public function is_hidden(): bool {
return $this->is_hidden;
}
/**
* Return the data used for the input's view.
*
* @return array
*/
public function get_view_data(): array {
$view_data = [
'id' => $this->get_view_id(),
'type' => $this->get_type(),
'label' => $this->get_label(),
'value' => $this->get_value(),
'description' => $this->get_description(),
'desc_tip' => true,
];
if ( $this->is_readonly ) {
$view_data['custom_attributes'] = [
'readonly' => 'readonly',
];
}
return array_merge( parent::get_view_data(), $view_data );
}
/**
* Return the id used for the input's view.
*
* @return string
*/
public function get_view_id(): string {
$parent = $this->get_parent();
if ( $parent instanceof InputInterface ) {
return sprintf( '%s_%s', $parent->get_view_id(), $this->get_id() );
} elseif ( $parent instanceof FormInterface ) {
return sprintf( '%s_%s', $parent->get_view_name(), $this->get_id() );
}
return sprintf( 'gla_%s', $this->get_name() );
}
/**
* Return the name of a generic product block in WooCommerce core or a custom block in this extension.
*
* @return string
*/
public function get_block_name(): string {
return $this->block_name;
}
/**
* Add or update a block attribute used for block config.
*
* @param string $key The attribute key defined in the corresponding block.json
* @param mixed $value The attribute value defined in the corresponding block.json
*
* @return InputInterface
*/
public function set_block_attribute( string $key, $value ): InputInterface {
$this->block_attributes[ $key ] = $value;
return $this;
}
/**
* Return the attributes of block config used for the input's view within the Product Block Editor.
*
* @return array
*/
public function get_block_attributes(): array {
$meta_key = $this->prefix_meta_key( $this->get_id() );
$block_attributes = array_merge(
[
'property' => "meta_data.{$meta_key}",
'label' => $this->get_label(),
'tooltip' => $this->get_description(),
],
$this->block_attributes
);
// Set boolean disabled property only if it's needed.
if ( $this->is_readonly() ) {
$block_attributes['disabled'] = true;
}
return $block_attributes;
}
/**
* Return the config used for the input's block within the Product Block Editor.
*
* @return array
*/
public function get_block_config(): array {
$id = $this->get_id();
return [
'id' => "google-listings-and-ads-product-attributes-{$id}",
'blockName' => $this->get_block_name(),
'attributes' => $this->get_block_attributes(),
];
}
}
Input/InputInterface.php 0000644 00000004171 15154522323 0011301 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;
defined( 'ABSPATH' ) || exit;
/**
* Interface InputInterface
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input
*/
interface InputInterface extends FormInterface {
/**
* @return string|null
*/
public function get_id(): ?string;
/**
* @param string|null $id
*
* @return InputInterface
*/
public function set_id( ?string $id ): InputInterface;
/**
* @return string
*/
public function get_type(): string;
/**
* @return string|null
*/
public function get_label(): ?string;
/**
* @param string|null $label
*
* @return InputInterface
*/
public function set_label( ?string $label ): InputInterface;
/**
* @return string|null
*/
public function get_description(): ?string;
/**
* @param string|null $description
*
* @return InputInterface
*/
public function set_description( ?string $description ): InputInterface;
/**
* @return mixed
*/
public function get_value();
/**
* @param mixed $value
*
* @return InputInterface
*/
public function set_value( $value ): InputInterface;
/**
* Return the id used for the input's view.
*
* @return string
*/
public function get_view_id(): string;
/**
* Return the name of a generic product block in WooCommerce core or a custom block in this extension.
*
* @return string
*/
public function get_block_name(): string;
/**
* Add or update a block attribute used for block config.
*
* @param string $key The attribute key defined in the corresponding block.json
* @param mixed $value The attribute value defined in the corresponding block.json
*
* @return InputInterface
*/
public function set_block_attribute( string $key, $value ): InputInterface;
/**
* Return the attributes of block config used for the input's view within the Product Block Editor.
*
* @return array
*/
public function get_block_attributes(): array;
/**
* Return the block config used for the input's view within the Product Block Editor.
*
* @return array
*/
public function get_block_config(): array;
}
Input/Integer.php 0000644 00000001253 15154522323 0007754 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;
defined( 'ABSPATH' ) || exit;
/**
* Class Integer
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input
*/
class Integer extends Input {
/**
* Integer constructor.
*/
public function __construct() {
// Ideally, it should use the 'woocommerce/product-number-field' block
// but the block doesn't support integer validation. Therefore, it uses
// the text field block to work around it.
parent::__construct( 'integer', 'woocommerce/product-text-field' );
$this->set_block_attribute(
'pattern',
[
'value' => '0|[1-9]\d*',
]
);
}
}
Input/Select.php 0000644 00000002666 15154522323 0007607 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;
defined( 'ABSPATH' ) || exit;
/**
* Class Select
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input
*/
class Select extends Input {
/**
* @var array
*/
protected $options = [];
/**
* Select constructor.
*/
public function __construct() {
parent::__construct( 'select', 'google-listings-and-ads/product-select-field' );
}
/**
* @return array
*/
public function get_options(): array {
return $this->options;
}
/**
* @param array $options
*
* @return $this
*/
public function set_options( array $options ): Select {
$this->options = $options;
return $this;
}
/**
* Return the data used for the input's view.
*
* @return array
*/
public function get_view_data(): array {
$view_data = parent::get_view_data();
$view_data['options'] = $this->get_options();
// add custom class
$view_data['class'] = 'select short';
return $view_data;
}
/**
* Return the attributes of block config used for the input's view within the Product Block Editor.
*
* @return array
*/
public function get_block_attributes(): array {
$options = [];
foreach ( $this->get_options() as $key => $value ) {
$options[] = [
'label' => $value,
'value' => $key,
];
}
$this->set_block_attribute( 'options', $options );
return parent::get_block_attributes();
}
}
Input/SelectWithTextInput.php 0000644 00000010500 15154522323 0012312 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;
defined( 'ABSPATH' ) || exit;
/**
* Class Select
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input
*/
class SelectWithTextInput extends Input {
public const CUSTOM_INPUT_KEY = '_gla_custom_value';
public const SELECT_INPUT_KEY = '_gla_select';
/**
* SelectWithTextInput constructor.
*/
public function __construct() {
$select_input = ( new Select() )->set_id( self::SELECT_INPUT_KEY )
->set_name( self::SELECT_INPUT_KEY );
$this->add( $select_input );
$custom_input = ( new Text() )->set_id( self::CUSTOM_INPUT_KEY )
->set_label( __( 'Enter your value', 'google-listings-and-ads' ) )
->set_name( self::CUSTOM_INPUT_KEY );
$this->add( $custom_input );
parent::__construct( 'select-with-text-input', 'google-listings-and-ads/product-select-with-text-field' );
}
/**
* @return array
*/
public function get_options(): array {
return $this->get_select_input()->get_options();
}
/**
* @param array $options
*
* @return $this
*/
public function set_options( array $options ): SelectWithTextInput {
$this->get_select_input()->set_options( $options );
return $this;
}
/**
* @param string|null $label
*
* @return InputInterface
*/
public function set_label( ?string $label ): InputInterface {
$this->get_select_input()->set_label( $label );
return parent::set_label( $label );
}
/**
* @param string|null $description
*
* @return InputInterface
*/
public function set_description( ?string $description ): InputInterface {
$this->get_select_input()->set_description( $description );
return parent::set_description( $description );
}
/**
* @return Select
*/
protected function get_select_input(): Select {
return $this->children[ self::SELECT_INPUT_KEY ];
}
/**
* @return Text
*/
protected function get_custom_input(): Text {
return $this->children[ self::CUSTOM_INPUT_KEY ];
}
/**
* Return the data used for the input's view.
*
* @return array
*/
public function get_view_data(): array {
$view_data = parent::get_view_data();
$select_input = $view_data['children'][ self::SELECT_INPUT_KEY ];
$custom_input = $view_data['children'][ self::CUSTOM_INPUT_KEY ];
// add custom classes
$view_data['gla_wrapper_class'] = $view_data['gla_wrapper_class'] ?? '';
$view_data['gla_wrapper_class'] .= ' select-with-text-input';
$custom_input['wrapper_class'] = 'custom-input';
// add custom value option
$select_input['options'][ self::CUSTOM_INPUT_KEY ] = __( 'Enter a custom value', 'google-listings-and-ads' );
if ( $this->is_readonly ) {
$select_input['custom_attributes'] = [
'disabled' => 'disabled',
];
$custom_input['custom_attributes'] = [
'readonly' => 'readonly',
];
}
$view_data['children'][ self::CUSTOM_INPUT_KEY ] = $custom_input;
$view_data['children'][ self::SELECT_INPUT_KEY ] = $select_input;
return $view_data;
}
/**
* Set the form's data.
*
* @param mixed $data
*
* @return void
*/
public function set_data( $data ): void {
if ( empty( $data ) ) {
$this->get_select_input()->set_data( null );
$this->get_custom_input()->set_data( null );
return;
}
$select_value = is_array( $data ) ? $data[ self::SELECT_INPUT_KEY ] ?? '' : $data;
$custom_value = is_array( $data ) ? $data[ self::CUSTOM_INPUT_KEY ] ?? '' : $data;
if ( ! isset( $this->get_options()[ $select_value ] ) ) {
$this->get_select_input()->set_data( self::CUSTOM_INPUT_KEY );
$this->get_custom_input()->set_data( $custom_value );
$this->data = $custom_value;
} else {
$this->get_select_input()->set_data( $select_value );
$this->data = $select_value;
}
}
/**
* Return the attributes of block config used for the input's view within the Product Block Editor.
*
* @return array
*/
public function get_block_attributes(): array {
$options = [];
foreach ( $this->get_options() as $key => $value ) {
$options[] = [
'label' => $value,
'value' => $key,
];
}
$options[] = [
'label' => __( 'Enter a custom value', 'google-listings-and-ads' ),
'value' => self::CUSTOM_INPUT_KEY,
];
$this->set_block_attribute( 'options', $options );
$this->set_block_attribute( 'customInputValue', self::CUSTOM_INPUT_KEY );
return parent::get_block_attributes();
}
}
Input/Text.php 0000644 00000000606 15154522323 0007304 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;
defined( 'ABSPATH' ) || exit;
/**
* Class Text
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input
*/
class Text extends Input {
/**
* Text constructor.
*/
public function __construct() {
parent::__construct( 'text', 'woocommerce/product-text-field' );
}
}
MetaBox/AbstractMetaBox.php 0000644 00000007644 15154522323 0011654 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Admin;
use Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits\ViewHelperTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\View\ViewException;
use WP_Post;
defined( 'ABSPATH' ) || exit;
/**
* Class AbstractMetaBox
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox
*/
abstract class AbstractMetaBox implements MetaBoxInterface {
use ViewHelperTrait;
protected const VIEW_PATH = 'meta-box';
/**
* @var Admin
*/
protected $admin;
/**
* AbstractMetaBox constructor.
*
* @param Admin $admin
*/
protected function __construct( Admin $admin ) {
$this->admin = $admin;
}
/**
* The context within the screen where the box should display. Available contexts vary from screen to
* screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. Comments screen contexts
* include 'normal' and 'side'. Menus meta boxes (accordion sections) all use the 'side' context.
*
* Global default is 'advanced'.
*
* @return string
*/
public function get_context(): string {
return self::CONTEXT_ADVANCED;
}
/***
* The priority within the context where the box should show.
*
* Accepts 'high', 'core', 'default', or 'low'. Default 'default'.
*
* @return string
*/
public function get_priority(): string {
return self::PRIORITY_DEFAULT;
}
/**
* Data that should be set as the $args property of the box array (which is the second parameter passed to your callback).
*
* @return array
*/
public function get_callback_args(): array {
return [];
}
/**
* Returns an array of CSS classes to apply to the box.
*
* @return array
*/
public function get_classes(): array {
return [];
}
/**
* Function that fills the box with the desired content.
*
* The function should echo its output.
*
* @return callable
*/
public function get_callback(): callable {
return [ $this, 'handle_callback' ];
}
/**
* Called by WordPress when rendering the meta box.
*
* The function should echo its output.
*
* @param WP_Post $post The WordPress post object the box is loaded for.
* @param array $data Array of box data passed to the callback by WordPress.
*
* @return void
*
* @throws ViewException If the meta box view can't be rendered.
*/
public function handle_callback( WP_Post $post, array $data ): void {
$args = $data['args'] ?? [];
$context = $this->get_view_context( $post, $args );
echo wp_kses( $this->render( $context ), $this->get_allowed_html_form_tags() );
}
/**
* Render the meta box.
*
* The view templates need to be placed under 'views/meta-box' and named
* using the meta box ID specified by the `get_id` method.
*
* @param array $context Optional. Contextual information to use while
* rendering. Defaults to an empty array.
*
* @return string Rendered result.
*
* @throws ViewException If the view doesn't exist or can't be loaded.
*
* @see self::get_id To see and modify the view file name.
*/
public function render( array $context = [] ): string {
$view_path = path_join( self::VIEW_PATH, $this->get_id() );
return $this->admin->get_view( $view_path, $context );
}
/**
* Appends a prefix to the given field ID and returns it.
*
* @param string $field_id
*
* @return string
*
* @since 1.1.0
*/
protected function prefix_field_id( string $field_id ): string {
$box_id = $this->prefix_id( $this->get_id() );
return "{$box_id}_{$field_id}";
}
/**
* Returns an array of variables to be used in the view.
*
* @param WP_Post $post The WordPress post object the box is loaded for.
* @param array $args Array of data passed to the callback. Defined by `get_callback_args`.
*
* @return array
*/
abstract protected function get_view_context( WP_Post $post, array $args ): array;
}
MetaBox/ChannelVisibilityMetaBox.php 0000644 00000013006 15154522323 0013516 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Admin;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductMetaHandler;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductSyncer;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility;
use WC_Product;
use WP_Post;
defined( 'ABSPATH' ) || exit;
/**
* Class ChannelVisibilityMetaBox
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox
*/
class ChannelVisibilityMetaBox extends SubmittableMetaBox {
use PluginHelper;
/**
* @var ProductMetaHandler
*/
protected $meta_handler;
/**
* @var ProductHelper
*/
protected $product_helper;
/**
* @var MerchantCenterService
*/
protected $merchant_center;
/**
* ChannelVisibilityMetaBox constructor.
*
* @param Admin $admin
* @param ProductMetaHandler $meta_handler
* @param ProductHelper $product_helper
* @param MerchantCenterService $merchant_center
*/
public function __construct( Admin $admin, ProductMetaHandler $meta_handler, ProductHelper $product_helper, MerchantCenterService $merchant_center ) {
$this->meta_handler = $meta_handler;
$this->product_helper = $product_helper;
$this->merchant_center = $merchant_center;
parent::__construct( $admin );
}
/**
* Meta box ID (used in the 'id' attribute for the meta box).
*
* @return string
*/
public function get_id(): string {
return 'channel_visibility';
}
/**
* Title of the meta box.
*
* @return string
*/
public function get_title(): string {
return __( 'Channel visibility', 'google-listings-and-ads' );
}
/**
* The screen on which to show the box (such as a post type, 'link', or 'comment').
*
* Default is the current screen.
*
* @return string
*/
public function get_screen(): string {
return self::SCREEN_PRODUCT;
}
/**
* The context within the screen where the box should display. Available contexts vary from screen to
* screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. Comments screen contexts
* include 'normal' and 'side'. Menus meta boxes (accordion sections) all use the 'side' context.
*
* Global default is 'advanced'.
*
* @return string
*/
public function get_context(): string {
return self::CONTEXT_SIDE;
}
/**
* Returns an array of CSS classes to apply to the box.
*
* @return array
*/
public function get_classes(): array {
return [ 'gla_meta_box' ];
}
/**
* Returns an array of variables to be used in the view.
*
* @param WP_Post $post The WordPress post object the box is loaded for.
* @param array $args Array of data passed to the callback. Defined by `get_callback_args`.
*
* @return array
*/
protected function get_view_context( WP_Post $post, array $args ): array {
$product_id = absint( $post->ID );
$product = $this->product_helper->get_wc_product( $product_id );
return [
'field_id' => $this->get_visibility_field_id(),
'product_id' => $product_id,
'product' => $product,
'channel_visibility' => $this->product_helper->get_channel_visibility( $product ),
'sync_status' => $this->meta_handler->get_sync_status( $product ),
'issues' => $this->product_helper->get_validation_errors( $product ),
'is_setup_complete' => $this->merchant_center->is_setup_complete(),
'get_started_url' => $this->get_start_url(),
];
}
/**
* Register a service.
*/
public function register(): void {
add_action( 'woocommerce_new_product', [ $this, 'handle_submission' ], 10, 2 );
add_action( 'woocommerce_update_product', [ $this, 'handle_submission' ], 10, 2 );
}
/**
* @param int $product_id
* @param WC_Product $product
*/
public function handle_submission( int $product_id, WC_Product $product ) {
/**
* Array of `true` values for each product IDs already handled by this method. Used to prevent double submission.
*
* @var bool[] $already_updated
*/
static $already_updated = [];
$field_id = $this->get_visibility_field_id();
// phpcs:disable WordPress.Security.NonceVerification
// nonce is verified by self::verify_nonce
if ( ! $this->verify_nonce() || ! isset( $_POST[ $field_id ] ) || isset( $already_updated[ $product_id ] ) ) {
return;
}
// only update the value for supported product types
if ( ! in_array( $product->get_type(), ProductSyncer::get_supported_product_types(), true ) ) {
return;
}
try {
$visibility = empty( $_POST[ $field_id ] ) ?
ChannelVisibility::cast( ChannelVisibility::SYNC_AND_SHOW ) :
ChannelVisibility::cast( sanitize_key( $_POST[ $field_id ] ) );
// phpcs:enable WordPress.Security.NonceVerification
$this->meta_handler->update_visibility( $product, $visibility );
$already_updated[ $product_id ] = true;
} catch ( InvalidValue $exception ) {
// silently log the exception and do not set the product's visibility if an invalid visibility value is sent.
do_action( 'woocommerce_gla_exception', $exception, __METHOD__ );
}
}
/**
* @return string
*
* @since 1.1.0
*/
protected function get_visibility_field_id(): string {
return $this->prefix_field_id( 'visibility' );
}
}
MetaBox/CouponChannelVisibilityMetaBox.php 0000644 00000014274 15154522323 0014712 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Admin;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponMetaHandler;
use Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponSyncer;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\TargetAudience;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility;
use WC_Coupon;
use WP_Post;
defined( 'ABSPATH' ) || exit;
/**
* Class CouponChannelVisibilityMetaBox
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox
*/
class CouponChannelVisibilityMetaBox extends SubmittableMetaBox {
/**
* @var CouponMetaHandler
*/
protected $meta_handler;
/**
* @var CouponHelper
*/
protected $coupon_helper;
/**
* @var MerchantCenterService
*/
protected $merchant_center;
/**
* @var TargetAudience
*/
protected $target_audience;
/**
* CouponChannelVisibilityMetaBox constructor.
*
* @param Admin $admin
* @param CouponMetaHandler $meta_handler
* @param CouponHelper $coupon_helper
* @param MerchantCenterService $merchant_center
* @param TargetAudience $target_audience
*/
public function __construct( Admin $admin, CouponMetaHandler $meta_handler, CouponHelper $coupon_helper, MerchantCenterService $merchant_center, TargetAudience $target_audience ) {
$this->meta_handler = $meta_handler;
$this->coupon_helper = $coupon_helper;
$this->merchant_center = $merchant_center;
$this->target_audience = $target_audience;
parent::__construct( $admin );
}
/**
* Meta box ID (used in the 'id' attribute for the meta box).
*
* @return string
*/
public function get_id(): string {
return 'coupon_channel_visibility';
}
/**
* Title of the meta box.
*
* @return string
*/
public function get_title(): string {
return __( 'Channel visibility', 'google-listings-and-ads' );
}
/**
* The screen on which to show the box (such as a post type, 'link', or 'comment').
*
* Default is the current screen.
*
* @return string
*/
public function get_screen(): string {
return self::SCREEN_COUPON;
}
/**
* The context within the screen where the box should display. Available contexts vary from screen to
* screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. Comments screen contexts
* include 'normal' and 'side'. Menus meta boxes (accordion sections) all use the 'side' context.
*
* Global default is 'advanced'.
*
* @return string
*/
public function get_context(): string {
return self::CONTEXT_SIDE;
}
/**
* Returns an array of CSS classes to apply to the box.
*
* @return array
*/
public function get_classes(): array {
$shown_types = array_map(
function ( string $coupon_type ) {
return "show_if_{$coupon_type}";
},
CouponSyncer::get_supported_coupon_types()
);
$hidden_types = array_map(
function ( string $coupon_type ) {
return "hide_if_{$coupon_type}";
},
CouponSyncer::get_hidden_coupon_types()
);
return array_merge( $shown_types, $hidden_types );
}
/**
* Returns an array of variables to be used in the view.
*
* @param WP_Post $post The WordPress post object the box is loaded for.
* @param array $args Array of data passed to the callback. Defined by `get_callback_args`.
*
* @return array
*/
protected function get_view_context( WP_Post $post, array $args ): array {
$coupon_id = absint( $post->ID );
$coupon = $this->coupon_helper->get_wc_coupon( $coupon_id );
$target_country = $this->target_audience->get_main_target_country();
return [
'field_id' => $this->get_visibility_field_id(),
'coupon_id' => $coupon_id,
'coupon' => $coupon,
'channel_visibility' => $this->coupon_helper->get_channel_visibility( $coupon ),
'sync_status' => $this->meta_handler->get_sync_status( $coupon ),
'issues' => $this->coupon_helper->get_validation_errors( $coupon ),
'is_setup_complete' => $this->merchant_center->is_setup_complete(),
'is_channel_supported' => $this->merchant_center->is_promotion_supported_country( $target_country ),
'get_started_url' => $this->get_start_url(),
];
}
/**
* Register a service.
*/
public function register(): void {
add_action( 'woocommerce_new_coupon', [ $this, 'handle_submission' ], 10, 2 );
add_action( 'woocommerce_update_coupon', [ $this, 'handle_submission' ], 10, 2 );
}
/**
* @param int $coupon_id
* @param WC_Coupon $coupon
*/
public function handle_submission( int $coupon_id, WC_Coupon $coupon ) {
/**
* Array of `true` values for each coupon IDs already handled by this method. Used to prevent double submission.
*
* @var bool[] $already_updated
*/
static $already_updated = [];
$field_id = $this->get_visibility_field_id();
// phpcs:disable WordPress.Security.NonceVerification
// nonce is verified by self::verify_nonce
if ( ! $this->verify_nonce() || ! isset( $_POST[ $field_id ] ) || isset( $already_updated[ $coupon_id ] ) ) {
return;
}
// Only update the value for supported coupon types
if ( ! CouponSyncer::is_coupon_supported( $coupon ) ) {
return;
}
try {
$visibility = empty( $_POST[ $field_id ] ) ?
ChannelVisibility::cast( ChannelVisibility::DONT_SYNC_AND_SHOW ) :
ChannelVisibility::cast( sanitize_key( $_POST[ $field_id ] ) );
// phpcs:enable WordPress.Security.NonceVerification
$this->meta_handler->update_visibility( $coupon, $visibility );
$already_updated[ $coupon_id ] = true;
} catch ( InvalidValue $exception ) {
// silently log the exception and do not set the coupon's visibility if an invalid visibility value is sent.
do_action( 'woocommerce_gla_exception', $exception, __METHOD__ );
}
}
/**
* @return string
*
* @since 1.1.0
*/
protected function get_visibility_field_id(): string {
return $this->prefix_field_id( 'visibility' );
}
}
MetaBox/MetaBoxInitializer.php 0000644 00000002543 15154522323 0012365 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Admin;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\AdminConditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Conditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
defined( 'ABSPATH' ) || exit;
/**
* Class MetaBoxInitializer
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox
*/
class MetaBoxInitializer implements Service, Registerable, Conditional {
use AdminConditional;
/**
* @var Admin
*/
protected $admin;
/**
* @var MetaBoxInterface[]
*/
protected $meta_boxes;
/**
* MetaBoxInitializer constructor.
*
* @param Admin $admin
* @param MetaBoxInterface[] $meta_boxes
*/
public function __construct( Admin $admin, array $meta_boxes ) {
$this->admin = $admin;
$this->meta_boxes = $meta_boxes;
}
/**
* Register a service.
*/
public function register(): void {
add_action( 'add_meta_boxes', [ $this, 'register_meta_boxes' ] );
}
/**
* Registers the meta boxes.
*/
public function register_meta_boxes() {
array_walk( $this->meta_boxes, [ $this->admin, 'add_meta_box' ] );
}
}
MetaBox/MetaBoxInterface.php 0000644 00000004405 15154522323 0012001 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Renderable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
defined( 'ABSPATH' ) || exit;
interface MetaBoxInterface extends Renderable, Service {
public const SCREEN_PRODUCT = 'product';
public const SCREEN_COUPON = 'shop_coupon';
public const CONTEXT_NORMAL = 'normal';
public const CONTEXT_SIDE = 'side';
public const CONTEXT_ADVANCED = 'advanced';
public const PRIORITY_DEFAULT = 'default';
public const PRIORITY_LOW = 'low';
public const PRIORITY_CORE = 'core';
public const PRIORITY_HIGH = 'high';
/**
* Meta box ID (used in the 'id' attribute for the meta box).
*
* @return string
*/
public function get_id(): string;
/**
* Title of the meta box.
*
* @return string
*/
public function get_title(): string;
/**
* Function that fills the box with the desired content.
*
* The function should echo its output.
*
* @return callable
*/
public function get_callback(): callable;
/**
* The screen or screens on which to show the box (such as a post type, 'link', or 'comment').
*
* Default is the current screen.
*
* @return string
*/
public function get_screen(): string;
/**
* The context within the screen where the box should display. Available contexts vary from screen to
* screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. Comments screen contexts
* include 'normal' and 'side'. Menus meta boxes (accordion sections) all use the 'side' context.
*
* Global default is 'advanced'.
*
* @return string
*/
public function get_context(): string;
/***
* The priority within the context where the box should show.
*
* Accepts 'high', 'core', 'default', or 'low'. Default 'default'.
*
* @return string
*/
public function get_priority(): string;
/**
* Data that should be set as the $args property of the box array (which is the second parameter passed to your callback).
*
* @return array
*/
public function get_callback_args(): array;
/**
* Returns an array of CSS classes to apply to the box.
*
* @return array
*/
public function get_classes(): array;
}
MetaBox/SubmittableMetaBox.php 0000644 00000001336 15154522323 0012354 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
defined( 'ABSPATH' ) || exit;
/**
* Class SubmittableMetaBox
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox
*/
abstract class SubmittableMetaBox extends AbstractMetaBox implements Registerable {
/**
* Verifies the WooCommerce meta box nonce.
*
* @return bool True is nonce is provided and valid, false otherwise.
*/
protected function verify_nonce(): bool {
return ! empty( $_POST['woocommerce_meta_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' );
}
}
Product/Attributes/AttributesForm.php 0000644 00000017455 15154522323 0014013 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Form;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\FormException;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\InputInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Select;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\SelectWithTextInput;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input\GTINInput;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\ValidateInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\AttributeInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\WithValueOptionsInterface;
defined( 'ABSPATH' ) || exit;
/**
* Class AttributesForm
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes
*/
class AttributesForm extends Form {
use ValidateInterface;
/**
* @var string[]
*/
protected $attribute_types = [];
/**
* AttributesForm constructor.
*
* @param string[] $attribute_types The names of the attribute classes extending AttributeInterface.
* @param array $data
*/
public function __construct( array $attribute_types, array $data = [] ) {
foreach ( $attribute_types as $attribute_type ) {
$this->add_attribute( $attribute_type );
}
parent::__construct( $data );
}
/**
* Return the data used for the input's view.
*
* @return array
*/
public function get_view_data(): array {
$view_data = parent::get_view_data();
// add classes to hide/display attributes based on product type
foreach ( $view_data['children'] as $index => $input ) {
if ( ! isset( $this->attribute_types[ $index ] ) ) {
continue;
}
$attribute_type = $this->attribute_types[ $index ];
$attribute_product_types = self::get_attribute_product_types( $attribute_type );
$hidden_types = $attribute_product_types['hidden'];
$visible_types = $attribute_product_types['visible'];
$input['gla_wrapper_class'] = $input['gla_wrapper_class'] ?? '';
if ( ! empty( $visible_types ) ) {
$input['gla_wrapper_class'] .= ' show_if_' . join( ' show_if_', $visible_types );
}
if ( ! empty( $hidden_types ) ) {
$input['gla_wrapper_class'] .= ' hide_if_' . join( ' hide_if_', $hidden_types );
}
$view_data['children'][ $index ] = $input;
}
return $view_data;
}
/**
* Get the hidden and visible types of an attribute's applicable product types.
*
* @param string $attribute_type The name of an attribute class extending AttributeInterface.
*
* @return array
*/
public static function get_attribute_product_types( string $attribute_type ): array {
$attribute_id = call_user_func( [ $attribute_type, 'get_id' ] );
$applicable_product_types = call_user_func( [ $attribute_type, 'get_applicable_product_types' ] );
/**
* This filter is documented in AttributeManager::map_attribute_types
*
* @see AttributeManager::map_attribute_types
*/
$applicable_product_types = apply_filters( "woocommerce_gla_attribute_applicable_product_types_{$attribute_id}", $applicable_product_types, $attribute_type );
/**
* Filters the list of product types to hide the attribute for.
*/
$hidden_product_types = apply_filters( "woocommerce_gla_attribute_hidden_product_types_{$attribute_id}", [] );
$visible_product_types = array_diff( $applicable_product_types, $hidden_product_types );
return [
'hidden' => $hidden_product_types,
'visible' => $visible_product_types,
];
}
/**
* @param InputInterface $input
* @param AttributeInterface $attribute
*
* @return InputInterface
*/
public static function init_input( InputInterface $input, AttributeInterface $attribute ) {
$input->set_id( $attribute::get_id() )
->set_name( $attribute::get_id() );
$value_options = [];
if ( $attribute instanceof WithValueOptionsInterface ) {
$value_options = $attribute::get_value_options();
}
$value_options = apply_filters( "woocommerce_gla_product_attribute_value_options_{$attribute::get_id()}", $value_options );
if ( ! empty( $value_options ) ) {
if ( ! $input instanceof Select && ! $input instanceof SelectWithTextInput ) {
$new_input = new SelectWithTextInput();
$new_input->set_label( $input->get_label() )
->set_description( $input->get_description() );
// When GTIN uses the SelectWithTextInput field, copy the readonly/hidden attributes from the GTINInput field.
if ( $input->name === 'gtin' ) {
$gtin_input = new GTINInput();
$new_input->set_hidden( $gtin_input->is_hidden() );
$new_input->set_readonly( $gtin_input->is_readonly() );
}
return self::init_input( $new_input, $attribute );
}
// add a 'default' value option
$value_options = [ '' => __( 'Default', 'google-listings-and-ads' ) ] + $value_options;
$input->set_options( $value_options );
}
return $input;
}
/**
* Add an attribute to the form
*
* @param string $attribute_type The name of an attribute class extending AttributeInterface.
* @param string|null $input_type The name of an input class extending InputInterface to use for attribute input.
*
* @return AttributesForm
*
* @throws InvalidValue If the attribute type is invalid or an invalid input type is specified for the attribute.
* @throws FormException If form is already submitted.
*/
public function add_attribute( string $attribute_type, ?string $input_type = null ): AttributesForm {
$this->validate_interface( $attribute_type, AttributeInterface::class );
// use the attribute's default input type if none provided.
if ( empty( $input_type ) ) {
$input_type = call_user_func( [ $attribute_type, 'get_input_type' ] );
}
$this->validate_interface( $input_type, InputInterface::class );
$attribute_input = self::init_input( new $input_type(), new $attribute_type() );
if ( ! $attribute_input->is_hidden() ) {
$this->add( $attribute_input );
$attribute_id = call_user_func( [ $attribute_type, 'get_id' ] );
$this->attribute_types[ $attribute_id ] = $attribute_type;
}
return $this;
}
/**
* Remove an attribute from the form
*
* @param string $attribute_type The name of an attribute class extending AttributeInterface.
*
* @return AttributesForm
*
* @throws InvalidValue If the attribute type is invalid or an invalid input type is specified for the attribute.
* @throws FormException If form is already submitted.
*/
public function remove_attribute( string $attribute_type ): AttributesForm {
$this->validate_interface( $attribute_type, AttributeInterface::class );
$attribute_id = call_user_func( [ $attribute_type, 'get_id' ] );
unset( $this->attribute_types[ $attribute_id ] );
$this->remove( $attribute_id );
return $this;
}
/**
* Sets the input type for the given attribute.
*
* @param string $attribute_type The name of an attribute class extending AttributeInterface.
* @param string $input_type The name of an input class extending InputInterface to use for attribute input.
*
* @return $this
*
* @throws FormException If form is already submitted.
*/
public function set_attribute_input( string $attribute_type, string $input_type ): AttributesForm {
if ( $this->is_submitted ) {
throw FormException::cannot_modify_submitted();
}
$this->validate_interface( $attribute_type, AttributeInterface::class );
$this->validate_interface( $input_type, InputInterface::class );
$attribute_id = call_user_func( [ $attribute_type, 'get_id' ] );
if ( $this->has( $attribute_id ) ) {
$this->children[ $attribute_id ] = self::init_input( new $input_type(), new $attribute_type() );
}
return $this;
}
}
Product/Attributes/AttributesTab.php 0000644 00000011704 15154522323 0013605 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Admin;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\AdminConditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Conditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\AttributeManager;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductSyncer;
use WC_Product;
defined( 'ABSPATH' ) || exit;
/**
* Class AttributesTab
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes
*/
class AttributesTab implements Service, Registerable, Conditional {
use AdminConditional;
use AttributesTrait;
/**
* @var Admin
*/
protected $admin;
/**
* @var AttributeManager
*/
protected $attribute_manager;
/**
* @var MerchantCenterService
*/
protected $merchant_center;
/**
* AttributesTab constructor.
*
* @param Admin $admin
* @param AttributeManager $attribute_manager
* @param MerchantCenterService $merchant_center
*/
public function __construct( Admin $admin, AttributeManager $attribute_manager, MerchantCenterService $merchant_center ) {
$this->admin = $admin;
$this->attribute_manager = $attribute_manager;
$this->merchant_center = $merchant_center;
}
/**
* Register a service.
*/
public function register(): void {
// Register the hooks only if Merchant Center is set up.
if ( ! $this->merchant_center->is_setup_complete() ) {
return;
}
add_action(
'woocommerce_new_product',
function ( int $product_id, WC_Product $product ) {
$this->handle_update_product( $product );
},
10,
2
);
add_action(
'woocommerce_update_product',
function ( int $product_id, WC_Product $product ) {
$this->handle_update_product( $product );
},
10,
2
);
add_action(
'woocommerce_product_data_tabs',
function ( array $tabs ) {
return $this->add_tab( $tabs );
}
);
add_action(
'woocommerce_product_data_panels',
function () {
$this->render_panel();
}
);
}
/**
* Adds the Google for WooCommerce tab to the WooCommerce product data box.
*
* @param array $tabs The current product data tabs.
*
* @return array An array with product tabs with the Yoast SEO tab added.
*/
private function add_tab( array $tabs ): array {
$tabs['gla_attributes'] = [
'label' => 'Google for WooCommerce',
'class' => 'gla',
'target' => 'gla_attributes',
];
return $tabs;
}
/**
* Render the product attributes tab.
*/
private function render_panel() {
$product = wc_get_product( get_the_ID() );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $this->admin->get_view( 'attributes/tab-panel', [ 'form' => $this->get_form( $product )->get_view_data() ] );
}
/**
* Handle form submission and update the product attributes.
*
* @param WC_Product $product
*/
private function handle_update_product( WC_Product $product ) {
/**
* Array of `true` values for each product IDs already handled by this method. Used to prevent double submission.
*
* @var bool[] $already_updated
*/
static $already_updated = [];
if ( isset( $already_updated[ $product->get_id() ] ) ) {
return;
}
$form = $this->get_form( $product );
$form_view_data = $form->get_view_data();
// phpcs:disable WordPress.Security.NonceVerification
if ( empty( $_POST[ $form_view_data['name'] ] ) ) {
return;
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$submitted_data = (array) wc_clean( wp_unslash( $_POST[ $form_view_data['name'] ] ) );
// phpcs:enable WordPress.Security.NonceVerification
$form->submit( $submitted_data );
$this->update_data( $product, $form->get_data() );
$already_updated[ $product->get_id() ] = true;
}
/**
* @param WC_Product $product
*
* @return AttributesForm
*/
protected function get_form( WC_Product $product ): AttributesForm {
$attribute_types = $this->attribute_manager->get_attribute_types_for_product_types( $this->get_applicable_product_types() );
$form = new AttributesForm( $attribute_types, $this->attribute_manager->get_all_values( $product ) );
$form->set_name( 'attributes' );
return $form;
}
/**
* @param WC_Product $product
* @param array $data
*
* @return void
*/
protected function update_data( WC_Product $product, array $data ): void {
foreach ( $this->attribute_manager->get_attribute_types_for_product( $product ) as $attribute_id => $attribute_type ) {
if ( isset( $data[ $attribute_id ] ) ) {
$this->attribute_manager->update( $product, new $attribute_type( $data[ $attribute_id ] ) );
}
}
}
}
Product/Attributes/AttributesTrait.php 0000644 00000001172 15154522323 0014160 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes;
/**
* Trait AttributesTrait
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes
*/
trait AttributesTrait {
/**
* Return an array of WooCommerce product types that the Google for WooCommerce tab can be displayed for.
*
* @return array of WooCommerce product types (e.g. 'simple', 'variable', etc.)
*/
protected function get_applicable_product_types(): array {
return apply_filters( 'woocommerce_gla_attributes_tab_applicable_product_types', [ 'simple', 'variable' ] );
}
}
Product/Attributes/Input/AdultInput.php 0000644 00000001267 15154522323 0014223 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\BooleanSelect;
defined( 'ABSPATH' ) || exit;
/**
* Class Adult
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class AdultInput extends BooleanSelect {
/**
* AdultInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Adult content', 'google-listings-and-ads' ) );
$this->set_description( __( 'Whether the product contains nudity or sexually suggestive content', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/AgeGroupInput.php 0000644 00000001211 15154522323 0014650 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Select;
defined( 'ABSPATH' ) || exit;
/**
* Class AgeGroup
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class AgeGroupInput extends Select {
/**
* AgeGroupInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Age Group', 'google-listings-and-ads' ) );
$this->set_description( __( 'Target age group of the item.', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/AttributeInputInterface.php 0000644 00000001433 15154522323 0016731 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
defined( 'ABSPATH' ) || exit;
/**
* Class AttributeInputInterface
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input
*
* @since 1.5.0
*/
interface AttributeInputInterface {
/**
* Returns a name for the attribute input.
*
* @return string
*/
public static function get_name(): string;
/**
* Returns a short description for the attribute input.
*
* @return string
*/
public static function get_description(): string;
/**
* Returns the input class used for the attribute input.
*
* Must be an instance of `InputInterface`.
*
* @return string
*/
public static function get_input_type(): string;
}
Product/Attributes/Input/AvailabilityDateInput.php 0000644 00000001426 15154522323 0016357 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\DateTime;
defined( 'ABSPATH' ) || exit;
/**
* Class AvailabilityDate
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class AvailabilityDateInput extends DateTime {
/**
* AvailabilityDateInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Availability Date', 'google-listings-and-ads' ) );
$this->set_description( __( 'The date a preordered or backordered product becomes available for delivery. Required if product availability is preorder or backorder', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/BrandInput.php 0000644 00000001160 15154522323 0014170 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Text;
defined( 'ABSPATH' ) || exit;
/**
* Class Brand
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class BrandInput extends Text {
/**
* BrandInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Brand', 'google-listings-and-ads' ) );
$this->set_description( __( 'Brand of the product.', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/ColorInput.php 0000644 00000001160 15154522323 0014220 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Text;
defined( 'ABSPATH' ) || exit;
/**
* Class Color
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class ColorInput extends Text {
/**
* ColorInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Color', 'google-listings-and-ads' ) );
$this->set_description( __( 'Color of the product.', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/ConditionInput.php 0000644 00000001216 15154522323 0015072 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Select;
defined( 'ABSPATH' ) || exit;
/**
* Class Condition
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class ConditionInput extends Select {
/**
* ConditionInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Condition', 'google-listings-and-ads' ) );
$this->set_description( __( 'Condition or state of the item.', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/GTINInput.php 0000644 00000003125 15154522323 0013706 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Text;
use Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits\GTINMigrationUtilities;
defined( 'ABSPATH' ) || exit;
/**
* Class GTIN
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class GTINInput extends Text {
use GTINMigrationUtilities;
/**
* GTINInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Global Trade Item Number (GTIN)', 'google-listings-and-ads' ) );
$this->set_description( __( 'Global Trade Item Number (GTIN) for your item. These identifiers include UPC (in North America), EAN (in Europe), JAN (in Japan), and ISBN (for books)', 'google-listings-and-ads' ) );
$this->set_field_visibility();
}
/**
* Controls the inputs visibility based on the WooCommerce version and the
* initial version of Google for WooCommerce at the time of installation.
*
* @since 2.9.0
* @return void
*/
public function set_field_visibility(): void {
if ( $this->is_gtin_available_in_core() ) {
// For versions after the GTIN changes are published. Hide the GTIN field from G4W tab. Otherwise, set as readonly.
if ( $this->should_hide_gtin() ) {
$this->set_hidden( true );
} else {
$this->set_readonly( true );
$this->set_description( __( 'The Global Trade Item Number (GTIN) for your item can now be entered on the "Inventory" tab', 'google-listings-and-ads' ) );
}
}
}
}
Product/Attributes/Input/GenderInput.php 0000644 00000001221 15154522323 0014344 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Select;
defined( 'ABSPATH' ) || exit;
/**
* Class Gender
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class GenderInput extends Select {
/**
* GenderInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Gender', 'google-listings-and-ads' ) );
$this->set_description( __( 'The gender for which your product is intended.', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/IsBundleInput.php 0000644 00000001377 15154522323 0014661 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\BooleanSelect;
defined( 'ABSPATH' ) || exit;
/**
* Class IsBundle
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class IsBundleInput extends BooleanSelect {
/**
* IsBundleInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Is Bundle?', 'google-listings-and-ads' ) );
$this->set_description( __( 'Whether the item is a bundle of products. A bundle is a custom grouping of different products sold by a merchant for a single price.', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/MPNInput.php 0000644 00000001254 15154522323 0013600 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Text;
defined( 'ABSPATH' ) || exit;
/**
* Class MPN
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class MPNInput extends Text {
/**
* MPNInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Manufacturer Part Number (MPN)', 'google-listings-and-ads' ) );
$this->set_description( __( 'This code uniquely identifies the product to its manufacturer.', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/MaterialInput.php 0000644 00000001216 15154522323 0014702 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Text;
defined( 'ABSPATH' ) || exit;
/**
* Class Material
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class MaterialInput extends Text {
/**
* MaterialInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Material', 'google-listings-and-ads' ) );
$this->set_description( __( 'The material of which the item is made.', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/MultipackInput.php 0000644 00000001500 15154522323 0015071 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Integer;
defined( 'ABSPATH' ) || exit;
/**
* Class Multipack
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class MultipackInput extends Integer {
/**
* MultipackInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Multipack', 'google-listings-and-ads' ) );
$this->set_description( __( 'The number of identical products in a multipack. Use this attribute to indicate that you\'ve grouped multiple identical products for sale as one item.', 'google-listings-and-ads' ) );
$this->set_block_attribute( 'min', [ 'value' => 0 ] );
}
}
Product/Attributes/Input/PatternInput.php 0000644 00000001211 15154522323 0014554 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Text;
defined( 'ABSPATH' ) || exit;
/**
* Class Pattern
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class PatternInput extends Text {
/**
* PatternInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Pattern', 'google-listings-and-ads' ) );
$this->set_description( __( 'The item\'s pattern (e.g. polka dots).', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/SizeInput.php 0000644 00000001153 15154522323 0014056 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Text;
defined( 'ABSPATH' ) || exit;
/**
* Class Size
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class SizeInput extends Text {
/**
* SizeInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Size', 'google-listings-and-ads' ) );
$this->set_description( __( 'Size of the product.', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/SizeSystemInput.php 0000644 00000001271 15154522323 0015264 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Select;
defined( 'ABSPATH' ) || exit;
/**
* Class SizeSystem
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class SizeSystemInput extends Select {
/**
* SizeSystemInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Size system', 'google-listings-and-ads' ) );
$this->set_description( __( 'System in which the size is specified. Recommended for apparel items.', 'google-listings-and-ads' ) );
}
}
Product/Attributes/Input/SizeTypeInput.php 0000644 00000001237 15154522323 0014723 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Select;
defined( 'ABSPATH' ) || exit;
/**
* Class SizeType
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes
*
* @since 1.5.0
*/
class SizeTypeInput extends Select {
/**
* SizeTypeInput constructor.
*/
public function __construct() {
parent::__construct();
$this->set_label( __( 'Size type', 'google-listings-and-ads' ) );
$this->set_description( __( 'The cut of the item. Recommended for apparel items.', 'google-listings-and-ads' ) );
}
}
Product/Attributes/VariationsAttributes.php 0000644 00000012114 15154522323 0015212 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Admin;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Form;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\AdminConditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Conditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\AttributeManager;
use WC_Product_Variation;
use WP_Post;
defined( 'ABSPATH' ) || exit;
/**
* Class VariationsAttributes
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes
*/
class VariationsAttributes implements Service, Registerable, Conditional {
use AdminConditional;
/**
* @var Admin
*/
protected $admin;
/**
* @var AttributeManager
*/
protected $attribute_manager;
/**
* @var MerchantCenterService
*/
protected $merchant_center;
/**
* VariationsAttributes constructor.
*
* @param Admin $admin
* @param AttributeManager $attribute_manager
* @param MerchantCenterService $merchant_center
*/
public function __construct( Admin $admin, AttributeManager $attribute_manager, MerchantCenterService $merchant_center ) {
$this->admin = $admin;
$this->attribute_manager = $attribute_manager;
$this->merchant_center = $merchant_center;
}
/**
* Register a service.
*/
public function register(): void {
// Register the hooks only if Merchant Center is set up.
if ( ! $this->merchant_center->is_setup_complete() ) {
return;
}
add_action(
'woocommerce_product_after_variable_attributes',
function ( int $variation_index, array $variation_data, WP_Post $variation ) {
$this->render_attributes_form( $variation_index, $variation );
},
90,
3
);
add_action(
'woocommerce_save_product_variation',
function ( int $variation_id, int $variation_index ) {
$this->handle_save_variation( $variation_id, $variation_index );
},
10,
2
);
}
/**
* Render the attributes form for variations.
*
* @param int $variation_index Position in the loop.
* @param WP_Post $variation Post data.
*/
private function render_attributes_form( int $variation_index, WP_Post $variation ) {
/**
* @var WC_Product_Variation $product
*/
$product = wc_get_product( $variation->ID );
$data = $this->get_form( $product, $variation_index )->get_view_data();
// Do not render the form if it doesn't contain any child attributes.
$attributes = reset( $data['children'] );
if ( empty( $data['children'] ) || empty( $attributes['children'] ) ) {
return;
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $this->admin->get_view( 'attributes/variations-form', $data );
}
/**
* Handle form submission and update the product attributes.
*
* @param int $variation_id
* @param int $variation_index
*/
private function handle_save_variation( int $variation_id, int $variation_index ) {
/**
* @var WC_Product_Variation $variation
*/
$variation = wc_get_product( $variation_id );
$form = $this->get_form( $variation, $variation_index );
$form_view_data = $form->get_view_data();
// phpcs:disable WordPress.Security.NonceVerification
if ( empty( $_POST[ $form_view_data['name'] ] ) ) {
return;
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$submitted_data = (array) wc_clean( wp_unslash( $_POST[ $form_view_data['name'] ] ) );
// phpcs:enable WordPress.Security.NonceVerification
$form->submit( $submitted_data );
$form_data = $form->get_data();
if ( ! empty( $form_data[ $variation_index ] ) ) {
$this->update_data( $variation, $form_data[ $variation_index ] );
}
}
/**
* @param WC_Product_Variation $variation
* @param int $variation_index
*
* @return Form
*/
protected function get_form( WC_Product_Variation $variation, int $variation_index ): Form {
$attribute_types = $this->attribute_manager->get_attribute_types_for_product( $variation );
$attribute_form = new AttributesForm( $attribute_types );
$attribute_form->set_name( (string) $variation_index );
$form = new Form();
$form->set_name( 'variation_attributes' )
->add( $attribute_form )
->set_data( [ (string) $variation_index => $this->attribute_manager->get_all_values( $variation ) ] );
return $form;
}
/**
* @param WC_Product_Variation $variation
* @param array $data
*
* @return void
*/
protected function update_data( WC_Product_Variation $variation, array $data ): void {
foreach ( $this->attribute_manager->get_attribute_types_for_product( $variation ) as $attribute_id => $attribute_type ) {
if ( isset( $data[ $attribute_id ] ) ) {
$this->attribute_manager->update( $variation, new $attribute_type( $data[ $attribute_id ] ) );
}
}
}
}
Product/ChannelVisibilityBlock.php 0000644 00000012043 15154522323 0013272 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductSyncer;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\SyncStatus;
use WC_Data;
use WC_Product;
use WP_REST_Request as Request;
use WP_REST_Response as Response;
defined( 'ABSPATH' ) || exit;
/**
* Class ChannelVisibilityBlock
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product
*/
class ChannelVisibilityBlock implements Service, Registerable {
public const PROPERTY = 'google_listings_and_ads__channel_visibility';
/**
* @var ProductHelper
*/
protected $product_helper;
/**
* @var MerchantCenterService
*/
protected $merchant_center;
/**
* ChannelVisibilityBlock constructor.
*
* @param ProductHelper $product_helper
* @param MerchantCenterService $merchant_center
*/
public function __construct( ProductHelper $product_helper, MerchantCenterService $merchant_center ) {
$this->product_helper = $product_helper;
$this->merchant_center = $merchant_center;
}
/**
* Register hooks for querying and updating product via REST APIs.
*/
public function register(): void {
if ( ! $this->merchant_center->is_setup_complete() ) {
return;
}
// https://github.com/woocommerce/woocommerce/blob/8.6.0/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-products-v2-controller.php#L182-L192
add_filter( 'woocommerce_rest_prepare_product_object', [ $this, 'prepare_data' ], 10, 2 );
// https://github.com/woocommerce/woocommerce/blob/8.6.0/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-crud-controller.php#L200-L207
// https://github.com/woocommerce/woocommerce/blob/8.6.0/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-crud-controller.php#L247-L254
add_action( 'woocommerce_rest_insert_product_object', [ $this, 'update_data' ], 10, 2 );
}
/**
* Get channel visibility data from the given product and add it to the given response.
*
* @param Response $response Response to be added channel visibility data.
* @param WC_Product|WC_Data $product WooCommerce product to get data.
*
* @return Response
*/
public function prepare_data( Response $response, WC_Data $product ): Response {
if ( ! $product instanceof WC_Product ) {
return $response;
}
$response->data[ self::PROPERTY ] = [
'is_visible' => $product->is_visible(),
'channel_visibility' => $this->product_helper->get_channel_visibility( $product ),
'sync_status' => $this->product_helper->get_sync_status( $product ),
'issues' => $this->product_helper->get_validation_errors( $product ),
];
return $response;
}
/**
* Get channel visibility data from the given request and update it to the given product.
*
* @param WC_Product|WC_Data $product WooCommerce product to be updated.
* @param Request $request Response to get the channel visibility data.
*/
public function update_data( WC_Data $product, Request $request ): void {
if ( ! $product instanceof WC_Product || ! in_array( $product->get_type(), $this->get_visible_product_types(), true ) ) {
return;
}
$params = $request->get_params();
if ( ! isset( $params[ self::PROPERTY ] ) ) {
return;
}
$channel_visibility = $params[ self::PROPERTY ]['channel_visibility'];
if ( $channel_visibility !== $this->product_helper->get_channel_visibility( $product ) ) {
$this->product_helper->update_channel_visibility( $product, $channel_visibility );
}
}
/**
* Return the visible product types to control the hidden condition of the channel visibility block
* in the Product Block Editor.
*
* @return array
*/
public function get_visible_product_types(): array {
return array_diff( ProductSyncer::get_supported_product_types(), [ 'variation' ] );
}
/**
* Return the config used for the input's block within the Product Block Editor.
*
* @return array
*/
public function get_block_config(): array {
$options = [];
foreach ( ChannelVisibility::get_value_options() as $key => $value ) {
$options[] = [
'label' => $value,
'value' => $key,
];
}
$attributes = [
'property' => self::PROPERTY,
'options' => $options,
'valueOfSync' => ChannelVisibility::SYNC_AND_SHOW,
'valueOfDontSync' => ChannelVisibility::DONT_SYNC_AND_SHOW,
'statusOfSynced' => SyncStatus::SYNCED,
'statusOfHasErrors' => SyncStatus::HAS_ERRORS,
];
return [
'id' => 'google-listings-and-ads-product-channel-visibility',
'blockName' => 'google-listings-and-ads/product-channel-visibility',
'attributes' => $attributes,
];
}
}
ProductBlocksService.php 0000644 00000031341 15154522323 0011360 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\AttributesForm;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\AttributesTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\ChannelVisibilityBlock;
use Automattic\WooCommerce\GoogleListingsAndAds\Assets\AdminScriptWithBuiltDependenciesAsset;
use Automattic\WooCommerce\GoogleListingsAndAds\Assets\AdminStyleAsset;
use Automattic\WooCommerce\GoogleListingsAndAds\Assets\AssetsHandlerInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Conditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\AttributeManager;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductSyncer;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\BuiltScriptDependencyArray;
use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\BlockRegistry;
use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\SectionInterface;
use Automattic\WooCommerce\Admin\PageController;
defined( 'ABSPATH' ) || exit;
/**
* Class ProductBlocksService
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin
*/
class ProductBlocksService implements Service, Registerable, Conditional {
use AttributesTrait;
use PluginHelper;
/**
* @var AssetsHandlerInterface
*/
protected $assets_handler;
/**
* @var AttributeManager
*/
protected $attribute_manager;
/**
* @var MerchantCenterService
*/
protected $merchant_center;
/**
* @var ChannelVisibilityBlock
*/
protected $channel_visibility_block;
/**
* @var string[]
*/
protected const CUSTOM_BLOCKS = [
'product-onboarding-prompt',
'product-channel-visibility',
'product-date-time-field',
'product-select-field',
'product-select-with-text-field',
];
/**
* ProductBlocksService constructor.
*
* @param AssetsHandlerInterface $assets_handler
* @param ChannelVisibilityBlock $channel_visibility_block
* @param AttributeManager $attribute_manager
* @param MerchantCenterService $merchant_center
*/
public function __construct( AssetsHandlerInterface $assets_handler, ChannelVisibilityBlock $channel_visibility_block, AttributeManager $attribute_manager, MerchantCenterService $merchant_center ) {
$this->assets_handler = $assets_handler;
$this->attribute_manager = $attribute_manager;
$this->merchant_center = $merchant_center;
$this->channel_visibility_block = $channel_visibility_block;
}
/**
* Return whether this service is needed to be registered.
*
* @return bool Whether this service is needed to be registered.
*/
public static function is_needed(): bool {
// compatibility-code "WC >= 8.6" -- The Block Template API used requires at least WooCommerce 8.6
return version_compare( WC_VERSION, '8.6', '>=' );
}
/**
* Register a service.
*/
public function register(): void {
if ( PageController::is_admin_page() ) {
add_action( 'init', [ $this, 'hook_init' ] );
}
// https://github.com/woocommerce/woocommerce/blob/8.6.0/plugins/woocommerce/src/Internal/Features/ProductBlockEditor/ProductTemplates/AbstractProductFormTemplate.php#L19
$template_area = 'product-form';
// https://github.com/woocommerce/woocommerce/blob/8.6.0/plugins/woocommerce/src/Internal/Features/ProductBlockEditor/ProductTemplates/SimpleProductTemplate.php#L19
// https://github.com/woocommerce/woocommerce/blob/8.6.0/plugins/woocommerce/src/Internal/Features/ProductBlockEditor/ProductTemplates/ProductVariationTemplate.php#L19
$block_id = 'general';
add_action(
"woocommerce_block_template_area_{$template_area}_after_add_block_{$block_id}",
[ $this, 'hook_block_template' ]
);
}
/**
* Action hanlder for the 'init' hook.
*/
public function hook_init(): void {
$build_path = "{$this->get_root_dir()}/js/build";
$uri = 'js/build/blocks';
$this->register_custom_blocks( BlockRegistry::get_instance(), $build_path, $uri, self::CUSTOM_BLOCKS );
}
/**
* Action hanlder for the "woocommerce_block_template_area_{$template_area}_after_add_block_{$block_id}" hook.
*
* @param BlockInterface $block The block just added to get its root template to add this extension's group and blocks.
*/
public function hook_block_template( BlockInterface $block ): void {
/** @var Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\ProductFormTemplateInterface */
$template = $block->get_root_template();
$is_variation_template = $this->is_variation_template( $block );
// Please note that the simple, variable, grouped, and external product types
// use the same product block template 'simple-product'. Their dynamic hidden
// conditions are added below.
if ( 'simple-product' !== $template->get_id() && ! $is_variation_template ) {
return;
}
/** @var Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\GroupInterface */
$group = $template->add_group(
[
'id' => 'google-listings-and-ads-group',
'order' => 100,
'attributes' => [
'title' => __( 'Google for WooCommerce', 'google-listings-and-ads' ),
],
]
);
$visible_product_types = ProductSyncer::get_supported_product_types();
if ( $is_variation_template ) {
// The property of `editedProduct.type` doesn't exist in the variation product.
// The condition returned from `get_hide_condition` won't work, so it uses 'true' directly.
if ( ! in_array( 'variation', $visible_product_types, true ) ) {
$group->add_hide_condition( 'true' );
}
} else {
$group->add_hide_condition( $this->get_hide_condition( $visible_product_types ) );
}
if ( ! $this->merchant_center->is_setup_complete() ) {
$group->add_block(
[
'id' => 'google-listings-and-ads-product-onboarding-prompt',
'blockName' => 'google-listings-and-ads/product-onboarding-prompt',
'attributes' => [
'startUrl' => $this->get_start_url(),
],
]
);
return;
}
/** @var SectionInterface */
$channel_visibility_section = $group->add_section(
[
'id' => 'google-listings-and-ads-channel-visibility-section',
'order' => 1,
'attributes' => [
'title' => __( 'Channel visibility', 'google-listings-and-ads' ),
],
]
);
if ( ! $is_variation_template ) {
$this->add_channel_visibility_block( $channel_visibility_section );
}
// Add the hidden condition to the channel visibility section because it only has one block.
$visible_product_types = $this->channel_visibility_block->get_visible_product_types();
$channel_visibility_section->add_hide_condition( $this->get_hide_condition( $visible_product_types ) );
/** @var SectionInterface */
$product_attributes_section = $group->add_section(
[
'id' => 'google-listings-and-ads-product-attributes-section',
'order' => 2,
'attributes' => [
'title' => __( 'Product attributes', 'google-listings-and-ads' ),
],
]
);
$this->add_product_attribute_blocks( $product_attributes_section );
}
/**
* Register the custom blocks and their assets.
*
* @param BlockRegistry $block_registry BlockRegistry instance getting from Woo Core for registering custom blocks.
* @param string $build_path The absolute path to the build directory of the assets.
* @param string $uri The script URI of the custom blocks.
* @param string[] $custom_blocks The directory names of each custom block under the build path.
*/
public function register_custom_blocks( BlockRegistry $block_registry, string $build_path, string $uri, array $custom_blocks ): void {
foreach ( $custom_blocks as $custom_block ) {
$block_json_file = "{$build_path}/{$custom_block}/block.json";
if ( ! file_exists( $block_json_file ) ) {
continue;
}
$block_registry->register_block_type_from_metadata( $block_json_file );
}
$assets[] = new AdminScriptWithBuiltDependenciesAsset(
'google-listings-and-ads-product-blocks',
$uri,
"{$build_path}/blocks.asset.php",
new BuiltScriptDependencyArray(
[
'dependencies' => [],
'version' => (string) filemtime( "{$build_path}/blocks.js" ),
]
)
);
$assets[] = new AdminStyleAsset(
'google-listings-and-ads-product-blocks-css',
$uri,
[],
(string) filemtime( "{$build_path}/blocks.css" )
);
$this->assets_handler->register_many( $assets );
$this->assets_handler->enqueue_many( $assets );
}
/**
* Add the channel visibility block to the given section block.
*
* @param SectionInterface $section The section block to add the channel visibility block
*/
private function add_channel_visibility_block( SectionInterface $section ): void {
$section->add_block( $this->channel_visibility_block->get_block_config() );
}
/**
* Add product attribute blocks to the given section block.
*
* @param SectionInterface $section The section block to add product attribute blocks
*/
private function add_product_attribute_blocks( SectionInterface $section ): void {
$is_variation_template = $this->is_variation_template( $section );
$product_types = $is_variation_template ? [ 'variation' ] : $this->get_applicable_product_types();
$attribute_types = $this->attribute_manager->get_attribute_types_for_product_types( $product_types );
foreach ( $attribute_types as $attribute_type ) {
$input_type = call_user_func( [ $attribute_type, 'get_input_type' ] );
$input = AttributesForm::init_input( new $input_type(), new $attribute_type() );
// Avoid to render Inputs that are defined as hidden in the Input.
// i.e We don't render GTIN for new WC versions anymore.
if ( $input->is_hidden() ) {
continue;
}
if ( $is_variation_template ) {
// When editing a variation, its product type on the frontend side won't be changed dynamically.
// In addition, the property of `editedProduct.type` doesn't exist in the variation product.
// Therefore, instead of using the ProductTemplates API `add_hide_condition` to conditionally
// hide attributes, it doesn't add invisible attribute blocks from the beginning.
if ( $this->is_visible_for_variation( $attribute_type ) ) {
$section->add_block( $input->get_block_config() );
}
} else {
$visible_product_types = AttributesForm::get_attribute_product_types( $attribute_type )['visible'];
// When editing a simple, variable, grouped, or external product, its product type on the
// frontend side can be changed dynamically. So, it needs to use the ProductTemplates API
// `add_hide_condition` to conditionally hide attributes.
/** @var BlockInterface */
$block = $section->add_block( $input->get_block_config() );
$block->add_hide_condition( $this->get_hide_condition( $visible_product_types ) );
}
}
}
/**
* Determine if the product block template of the given block is the variation template.
*
* @param BlockInterface $block The block to be checked
*
* @return boolean
*/
private function is_variation_template( BlockInterface $block ): bool {
return 'product-variation' === $block->get_root_template()->get_id();
}
/**
* Determine if the given attribute is visible for variation product after applying related filters.
*
* @param string $attribute_type An attribute class extending AttributeInterface
*
* @return bool
*/
private function is_visible_for_variation( string $attribute_type ): bool {
$attribute_product_types = AttributesForm::get_attribute_product_types( $attribute_type );
return in_array( 'variation', $attribute_product_types['visible'], true );
}
/**
* Get the expression of the hide condition to a block based on the visible product types.
* e.g. "editedProduct.type !== 'simple' && ! editedProduct.parent_id > 0"
*
* The hide condition is a JavaScript-like expression that will be evaluated on the client to determine if the block should be hidden.
* See [@woocommerce/expression-evaluation](https://github.com/woocommerce/woocommerce/blob/trunk/packages/js/expression-evaluation/README.md) for more details.
*
* @param array $visible_product_types The visible product types to be converted to a hidden condition
*
* @return string
*/
public function get_hide_condition( array $visible_product_types ): string {
$conditions = array_map(
function ( $type ) {
return "editedProduct.type !== '{$type}'";
},
$visible_product_types
);
return implode( ' && ', $conditions ) ?: 'true';
}
}
Redirect.php 0000644 00000011655 15154522323 0007030 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Activateable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterAwareInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterAwareTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\WP;
use Automattic\WooCommerce\GoogleListingsAndAds\Menu\Dashboard;
use Automattic\WooCommerce\GoogleListingsAndAds\Menu\GetStarted;
use Automattic\WooCommerce\Admin\PageController;
/**
* Class Redirect
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin
*/
class Redirect implements Activateable, Service, Registerable, OptionsAwareInterface, MerchantCenterAwareInterface {
use MerchantCenterAwareTrait;
use OptionsAwareTrait;
protected const OPTION = OptionsInterface::REDIRECT_TO_ONBOARDING;
public const PATHS = [
'dashboard' => Dashboard::PATH,
'get_started' => GetStarted::PATH,
];
/**
* @var WP
*/
protected $wp;
/**
* Redirect constructor.
*
* @param WP $wp
*/
public function __construct( WP $wp ) {
$this->wp = $wp;
}
/**
* Register a service.
*
* @return void
*/
public function register(): void {
add_action(
'admin_init',
function () {
$this->maybe_redirect();
}
);
}
/**
* Activate a service.
*
* @return void
*/
public function activate(): void {
// Do not take any action if activated in a REST request (via wc-admin).
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return;
}
if (
// Only redirect to onboarding when activated on its own. Either with a link...
( isset( $_GET['action'] ) && 'activate' === $_GET['action'] ) // phpcs:ignore WordPress.Security.NonceVerification
// ...or with a bulk action.
|| ( isset( $_POST['checked'] ) && is_array( $_POST['checked'] ) && 1 === count( $_POST['checked'] ) ) // phpcs:ignore WordPress.Security.NonceVerification
) {
$this->options->update( self::OPTION, 'yes' );
}
}
/**
* Checks if merchant should be redirected to the onboarding page if it is not.
*
* @return void
*/
public function maybe_redirect() {
if ( $this->wp->wp_doing_ajax() ) {
return;
}
// Maybe redirect to onboarding after activation
if ( 'yes' === $this->options->get( self::OPTION ) ) {
return $this->maybe_redirect_after_activation();
}
// If setup ISNT complete then redirect from dashboard to onboarding
if ( ! $this->merchant_center->is_setup_complete() && $this->is_current_wc_admin_page( self::PATHS['dashboard'] ) ) {
return $this->redirect_to( self::PATHS['get_started'] );
}
// If setup IS complete then redirect from onboarding to dashboard
if ( $this->merchant_center->is_setup_complete() && $this->is_current_wc_admin_page( self::PATHS['get_started'] ) ) {
return $this->redirect_to( self::PATHS['dashboard'] );
}
return false;
}
/**
* Checks if merchant should be redirected to the onboarding page after extension activation.
*
* @return bool True if the redirection should have happened
*/
protected function maybe_redirect_after_activation(): bool {
// Do not redirect if setup is already complete
if ( $this->merchant_center->is_setup_complete() ) {
$this->options->update( self::OPTION, 'no' );
return false;
}
// if we are on the get started page don't redirect again
if ( $this->is_current_wc_admin_page( self::PATHS['get_started'] ) ) {
$this->options->update( self::OPTION, 'no' );
return false;
}
// Redirect if setup is not complete
$this->redirect_to( self::PATHS['get_started'] );
return true;
}
/**
* Utility function to immediately redirect to a given WC Admin path.
* Note that this function immediately ends the execution.
*
* @param string $path The WC Admin path to redirect to
*
* @return void
*/
public function redirect_to( $path ): void {
// If we are already on this path, do nothing.
if ( $this->is_current_wc_admin_page( $path ) ) {
return;
}
$params = [
'page' => PageController::PAGE_ROOT,
'path' => $path,
];
wp_safe_redirect( admin_url( add_query_arg( $params, 'admin.php' ) ) );
exit();
}
/**
* Check if the current WC Admin page matches the given path.
*
* @param string $path The path to check.
*
* @return bool
*/
public function is_current_wc_admin_page( $path ): bool {
$params = [
'page' => PageController::PAGE_ROOT,
'path' => $path,
];
return 2 === count( array_intersect_assoc( $_GET, $params ) ); // phpcs:disable WordPress.Security.NonceVerification.Recommended
}
}
ConflictingPlugins.php 0000644 00000011151 15154554337 0011071 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Models;
/**
* Checks for conflicting plugins.
*
* @since 4.0.0
*/
class ConflictingPlugins {
/**
* A list of conflicting plugin slugs.
*
* @since 4.5.1
*
* @var array
*/
protected $conflictingPluginSlugs = [
// Note: We should NOT add Jetpack here since they automatically disable their SEO module when ours is active.
'wordpress-seo',
'seo-by-rank-math',
'wp-seopress',
'autodescription',
'slim-seo',
'squirrly-seo',
'google-sitemap-generator',
'xml-sitemap-feed',
'www-xml-sitemap-generator-org',
'google-sitemap-plugin',
];
/**
* Class constructor.
*
* @since 4.0.0
*/
public function __construct() {
// We don't want to trigger our notices when not in the admin.
if ( ! is_admin() ) {
return;
}
add_action( 'init', [ $this, 'init' ], 20 );
}
/**
* Initialize the conflicting plugins check.
*
* @since 4.0.0
*
* @return void
*/
public function init() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$conflictingPlugins = $this->getAllConflictingPlugins();
$notification = Models\Notification::getNotificationByName( 'conflicting-plugins' );
if ( empty( $conflictingPlugins ) ) {
if ( ! $notification->exists() ) {
return;
}
Models\Notification::deleteNotificationByName( 'conflicting-plugins' );
return;
}
aioseo()->notices->conflictingPlugins( $conflictingPlugins );
}
/**
* Get a list of all conflicting plugins.
*
* @since 4.0.0
*
* @return array An array of conflicting plugins.
*/
public function getAllConflictingPlugins() {
$conflictingSeoPlugins = $this->getConflictingPlugins( 'seo' );
$conflictingSitemapPlugins = [];
if (
aioseo()->options->sitemap->general->enable ||
aioseo()->options->sitemap->rss->enable
) {
$conflictingSitemapPlugins = $this->getConflictingPlugins( 'sitemap' );
}
return array_merge( $conflictingSeoPlugins, $conflictingSitemapPlugins );
}
/**
* Get a list of conflicting plugins for AIOSEO.
*
* @since 4.0.0
*
* @param string $type A type to look for.
* @return array An array of conflicting plugins.
*/
public function getConflictingPlugins( $type ) {
$activePlugins = wp_get_active_and_valid_plugins();
if ( is_multisite() ) {
$activePlugins = array_merge( $activePlugins, wp_get_active_network_plugins() );
}
$conflictingPlugins = [];
switch ( $type ) {
// Note: We should NOT add Jetpack here since they automatically disable their SEO module when ours is active.
case 'seo':
$conflictingPlugins = [
'Rank Math SEO' => 'seo-by-rank-math/rank-math.php',
'Rank Math SEO Pro' => 'seo-by-rank-math-pro/rank-math-pro.php',
'SEOPress' => 'wp-seopress/seopress.php',
'The SEO Framework' => 'autodescription/autodescription.php',
'Yoast SEO' => 'wordpress-seo/wp-seo.php',
'Yoast SEO Premium' => 'wordpress-seo-premium/wp-seo-premium.php'
];
break;
case 'sitemap':
$conflictingPlugins = [
'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php',
'Google XML Sitemap Generator' => 'www-xml-sitemap-generator-org/www-xml-sitemap-generator-org.php',
'Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php',
'XML Sitemap & Google News' => 'xml-sitemap-feed/xml-sitemap.php'
];
break;
}
$activeConflictingPlugins = [];
foreach ( $activePlugins as $pluginFilePath ) {
foreach ( $conflictingPlugins as $index => $pluginPath ) {
if ( false !== strpos( $pluginFilePath, $pluginPath ) ) {
$activeConflictingPlugins[ $index ] = $pluginPath;
}
}
}
return $activeConflictingPlugins;
}
/**
* Deactivate conflicting plugins.
*
* @since 4.5.1
*
* @param array $types An array of types to look for.
* @return void
*/
public function deactivateConflictingPlugins( $types ) {
$seo = in_array( 'seo', $types, true ) ? $this->getConflictingPlugins( 'seo' ) : [];
$sitemap = in_array( 'sitemap', $types, true ) ? $this->getConflictingPlugins( 'sitemap' ) : [];
$plugins = array_merge(
$seo,
$sitemap
);
require_once ABSPATH . 'wp-admin/includes/plugin.php';
foreach ( $plugins as $pluginPath ) {
if ( is_plugin_active( $pluginPath ) ) {
deactivate_plugins( $pluginPath );
}
}
}
/**
* Get a list of conflicting plugin slugs.
*
* @since 4.5.1
*
* @return array An array of conflicting plugin slugs.
*/
public function getConflictingPluginSlugs() {
return $this->conflictingPluginSlugs;
}
} Dashboard.php 0000644 00000010721 15154554337 0007161 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class that holds our dashboard widget.
*
* @since 4.0.0
*/
class Dashboard {
/**
* Class Constructor.
*
* @since 4.0.0
*/
public function __construct() {
add_action( 'wp_dashboard_setup', [ $this, 'addDashboardWidgets' ] );
}
/**
* Registers our dashboard widgets.
*
* @since 4.2.0
*
* @return void
*/
public function addDashboardWidgets() {
// Add the SEO Setup widget.
if (
$this->canShowWidget( 'seoSetup' ) &&
apply_filters( 'aioseo_show_seo_setup', true ) &&
( aioseo()->access->isAdmin() || aioseo()->access->hasCapability( 'aioseo_setup_wizard' ) ) &&
! aioseo()->standalone->setupWizard->isCompleted()
) {
wp_add_dashboard_widget(
'aioseo-seo-setup',
// Translators: 1 - The plugin short name ("AIOSEO").
sprintf( esc_html__( '%s Setup', 'all-in-one-seo-pack' ), AIOSEO_PLUGIN_SHORT_NAME ),
[
$this,
'outputSeoSetup',
],
null,
null,
'normal',
'high'
);
}
// Add the Overview widget.
if (
$this->canShowWidget( 'seoOverview' ) &&
apply_filters( 'aioseo_show_seo_overview', true ) &&
( aioseo()->access->isAdmin() || aioseo()->access->hasCapability( 'aioseo_page_analysis' ) ) &&
aioseo()->options->advanced->truSeo
) {
wp_add_dashboard_widget(
'aioseo-overview',
// Translators: 1 - The plugin short name ("AIOSEO").
sprintf( esc_html__( '%s Overview', 'all-in-one-seo-pack' ), AIOSEO_PLUGIN_SHORT_NAME ),
[
$this,
'outputSeoOverview',
]
);
}
// Add the News widget.
if (
$this->canShowWidget( 'seoNews' ) &&
apply_filters( 'aioseo_show_seo_news', true ) &&
aioseo()->access->isAdmin()
) {
wp_add_dashboard_widget(
'aioseo-rss-feed',
esc_html__( 'SEO News', 'all-in-one-seo-pack' ),
[
$this,
'displayRssDashboardWidget',
]
);
}
}
/**
* Whether or not to show the widget.
*
* @since 4.0.0
* @version 4.2.8
*
* @param string $widget The widget to check if can show.
* @return boolean True if yes, false otherwise.
*/
protected function canShowWidget( $widget ) { // phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
return true;
}
/**
* Output the SEO Setup widget.
*
* @since 4.2.0
*
* @return void
*/
public function outputSeoSetup() {
$this->output( 'aioseo-seo-setup-app' );
}
/**
* Output the SEO Overview widget.
*
* @since 4.2.0
*
* @return void
*/
public function outputSeoOverview() {
$this->output( 'aioseo-overview-app' );
}
/**
* Output the widget wrapper for the Vue App.
*
* @since 4.2.0
*
* @param string $appId The App ID to print out.
* @return void
*/
private function output( $appId ) {
// Enqueue the scripts for the widget.
$this->enqueue();
// Opening tag.
echo '<div id="' . esc_attr( $appId ) . '">';
// Loader element.
require AIOSEO_DIR . '/app/Common/Views/parts/loader.php';
// Closing tag.
echo '</div>';
}
/**
* Enqueue the scripts and styles.
*
* @since 4.2.0
*
* @return void
*/
private function enqueue() {
aioseo()->core->assets->load( 'src/vue/standalone/dashboard-widgets/main.js', [], aioseo()->helpers->getVueData( 'dashboard' ) );
}
/**
* Display RSS Dashboard Widget
*
* @since 4.0.0
*
* @return void
*/
public function displayRssDashboardWidget() {
// Check if the user has chosen not to display this widget through screen options.
$currentScreen = aioseo()->helpers->getCurrentScreen();
if ( empty( $currentScreen->id ) ) {
return;
}
$hiddenWidgets = get_user_meta( get_current_user_id(), 'metaboxhidden_' . $currentScreen->id );
if ( $hiddenWidgets && count( $hiddenWidgets ) > 0 && is_array( $hiddenWidgets[0] ) && in_array( 'aioseo-rss-feed', $hiddenWidgets[0], true ) ) {
return;
}
$rssItems = aioseo()->helpers->fetchAioseoArticles();
if ( ! $rssItems ) {
esc_html_e( 'Temporarily unable to load feed.', 'all-in-one-seo-pack' );
return;
}
?>
<ul>
<?php
foreach ( $rssItems as $item ) {
?>
<li>
<a target="_blank" href="<?php echo esc_url( $item['url'] ); ?>" rel="noopener noreferrer">
<?php echo esc_html( $item['title'] ); ?>
</a>
<span><?php echo esc_html( $item['date'] ); ?></span>
<div>
<?php echo esc_html( wp_strip_all_tags( $item['content'] ) ) . '...'; ?>
</div>
</li>
<?php
}
?>
</ul>
<?php
}
} DeactivationSurvey.php 0000644 00000022720 15154554337 0011124 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Deactivation survey.
*
* @since 4.5.5
*/
class DeactivationSurvey {
/**
* The API URL we are calling.
*
* @since 4.5.5
*
* @var string
*/
public $apiUrl = 'https://plugin.aioseo.com/wp-json/am-deactivate-survey/v1/deactivation-data';
/**
* Name for this plugin.
*
* @since 4.5.5
*
* @var string
*/
public $name;
/**
* Unique slug for this plugin.
*
* @since 4.5.5
*
* @var string
*/
public $plugin;
/**
* Primary class constructor.
*
* @since 4.5.5
*
* @param string $name Plugin name.
* @param string $plugin Plugin slug.
*/
public function __construct( $name = '', $plugin = '' ) {
$this->name = $name;
$this->plugin = $plugin;
// Don't run deactivation survey on dev sites.
if ( aioseo()->helpers->isDev() ) {
// return;
}
add_action( 'admin_print_scripts', [ $this, 'js' ], 20 );
add_action( 'admin_print_scripts', [ $this, 'css' ] );
add_action( 'admin_footer', [ $this, 'modal' ] );
}
/**
* Returns the URL of the remote endpoint.
*
* @since 4.5.5
*
* @return string The URL.
*/
public function getApiUrl() {
if ( defined( 'AIOSEO_DEACTIVATION_SURVEY_URL' ) ) {
return AIOSEO_DEACTIVATION_SURVEY_URL;
}
return $this->apiUrl;
}
/**
* Checks if current admin screen is the plugins page.
*
* @since 4.5.5
*
* @return bool True if it is, false if not.
*/
public function isPluginPage() {
$screen = aioseo()->helpers->getCurrentScreen();
if ( empty( $screen->id ) ) {
return false;
}
return in_array( $screen->id, [ 'plugins', 'plugins-network' ], true );
}
/**
* Survey javascript.
*
* @since 4.5.5
*
* @return void
*/
public function js() {
if ( ! $this->isPluginPage() ) {
return;
}
?>
<script type="text/javascript">
window.addEventListener("load", function() {
var deactivateLink = document.querySelector('#the-list [data-slug="<?php echo esc_html( $this->plugin ); ?>"] span.deactivate a') ||
document.querySelector('#deactivate-<?php echo esc_html( $this->plugin ); ?>'),
overlay = document.querySelector('#am-deactivate-survey-<?php echo esc_html( $this->plugin ); ?>'),
form = overlay.querySelector('form'),
formOpen = false;
deactivateLink.addEventListener('click', function(event) {
event.preventDefault();
overlay.style.display = 'table';
formOpen = true;
form.querySelector('.am-deactivate-survey-option:first-of-type input[type=radio]').focus();
});
form.addEventListener('change', function(event) {
if (event.target.matches('input[type=radio]')) {
event.preventDefault();
Array.from(form.querySelectorAll('input[type=text], .error')).forEach(function(el) { el.style.display = 'none'; });
Array.from(form.querySelectorAll('.am-deactivate-survey-option')).forEach(function(el) { el.classList.remove('selected'); });
var option = event.target.closest('.am-deactivate-survey-option');
option.classList.add('selected');
var otherField = option.querySelector('input[type=text]');
if (otherField) {
otherField.style.display = 'block';
otherField.focus();
}
}
});
form.addEventListener('click', function(event) {
if (event.target.matches('.am-deactivate-survey-deactivate')) {
event.preventDefault();
window.location.href = deactivateLink.getAttribute('href');
}
});
form.addEventListener('submit', function(event) {
event.preventDefault();
if (!form.querySelector('input[type=radio]:checked')) {
if(!form.querySelector('span[class="error"]')) {
form.querySelector('.am-deactivate-survey-footer')
.insertAdjacentHTML('afterbegin', '<span class="error"><?php echo esc_js( __( 'Please select an option', 'all-in-one-seo-pack' ) ); ?></span>');
}
return;
}
var selected = form.querySelector('.selected');
var otherField = selected.querySelector('input[type=text]');
var data = {
code: selected.querySelector('input[type=radio]').value,
reason: selected.querySelector('.am-deactivate-survey-option-reason').textContent,
details: otherField ? otherField.value : '',
site: '<?php echo esc_url( home_url() ); ?>',
plugin: '<?php echo esc_html( $this->plugin ); ?>'
}
var submitSurvey = fetch('<?php echo esc_url( $this->getApiUrl() ); ?>', {
method: 'POST',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' }
});
submitSurvey.finally(function() {
window.location.href = deactivateLink.getAttribute('href');
});
});
document.addEventListener('keyup', function(event) {
if (27 === event.keyCode && formOpen) {
overlay.style.display = 'none';
formOpen = false;
deactivateLink.focus();
}
});
});
</script>
<?php
}
/**
* Survey CSS.
*
* @since 4.5.5
*
* @return void
*/
public function css() {
if ( ! $this->isPluginPage() ) {
return;
}
?>
<style type="text/css">
.am-deactivate-survey-modal {
display: none;
table-layout: fixed;
position: fixed;
z-index: 9999;
width: 100%;
height: 100%;
text-align: center;
font-size: 14px;
top: 0;
left: 0;
background: rgba(0,0,0,0.8);
}
.am-deactivate-survey-wrap {
display: table-cell;
vertical-align: middle;
}
.am-deactivate-survey {
background-color: #fff;
max-width: 550px;
margin: 0 auto;
padding: 30px;
text-align: left;
}
.am-deactivate-survey .error {
display: block;
color: red;
margin: 0 0 10px 0;
}
.am-deactivate-survey-title {
display: block;
font-size: 18px;
font-weight: 700;
text-transform: uppercase;
border-bottom: 1px solid #ddd;
padding: 0 0 18px 0;
margin: 0 0 18px 0;
}
.am-deactivate-survey-title span {
color: #999;
margin-right: 10px;
}
.am-deactivate-survey-desc {
display: block;
font-weight: 600;
margin: 0 0 18px 0;
}
.am-deactivate-survey-option {
margin: 0 0 10px 0;
}
.am-deactivate-survey-option-input {
margin-right: 10px !important;
}
.am-deactivate-survey-option-details {
display: none;
width: 90%;
margin: 10px 0 0 30px;
}
.am-deactivate-survey-footer {
margin-top: 18px;
}
.am-deactivate-survey-deactivate {
float: right;
font-size: 13px;
color: #ccc;
text-decoration: none;
padding-top: 7px;
}
</style>
<?php
}
/**
* Survey modal.
*
* @since 4.5.5
*
* @return void
*/
public function modal() {
if ( ! $this->isPluginPage() ) {
return;
}
$options = [
1 => [
'title' => esc_html__( 'I no longer need the plugin', 'all-in-one-seo-pack' ),
],
2 => [
'title' => esc_html__( 'I\'m switching to a different plugin', 'all-in-one-seo-pack' ),
'details' => esc_html__( 'Please share which plugin', 'all-in-one-seo-pack' ),
],
3 => [
'title' => esc_html__( 'I couldn\'t get the plugin to work', 'all-in-one-seo-pack' ),
],
4 => [
'title' => esc_html__( 'It\'s a temporary deactivation', 'all-in-one-seo-pack' ),
],
5 => [
'title' => esc_html__( 'Other', 'all-in-one-seo-pack' ),
'details' => esc_html__( 'Please share the reason', 'all-in-one-seo-pack' ),
],
];
?>
<div class="am-deactivate-survey-modal" id="am-deactivate-survey-<?php echo esc_html( $this->plugin ); ?>">
<div class="am-deactivate-survey-wrap">
<form class="am-deactivate-survey" method="post">
<span class="am-deactivate-survey-title"><span class="dashicons dashicons-testimonial"></span><?php echo ' ' . esc_html__( 'Quick Feedback', 'all-in-one-seo-pack' ); ?></span>
<span class="am-deactivate-survey-desc">
<?php
echo esc_html(
sprintf(
// Translators: 1 - The plugin name.
__( 'If you have a moment, please share why you are deactivating %1$s:', 'all-in-one-seo-pack' ),
$this->name
)
);
?>
</span>
<div class="am-deactivate-survey-options">
<?php foreach ( $options as $id => $option ) : ?>
<div class="am-deactivate-survey-option">
<label for="am-deactivate-survey-option-<?php echo esc_html( $this->plugin ); ?>-<?php echo intval( $id ); ?>" class="am-deactivate-survey-option-label">
<input
id="am-deactivate-survey-option-<?php echo esc_html( $this->plugin ); ?>-<?php echo intval( $id ); ?>"
class="am-deactivate-survey-option-input"
type="radio"
name="code"
value="<?php echo intval( $id ); ?>"
/>
<span class="am-deactivate-survey-option-reason"><?php echo esc_html( $option['title'] ); ?></span>
</label>
<?php if ( ! empty( $option['details'] ) ) : ?>
<input class="am-deactivate-survey-option-details" type="text" placeholder="<?php echo esc_html( $option['details'] ); ?>" />
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<div class="am-deactivate-survey-footer">
<button type="submit" class="am-deactivate-survey-submit button button-primary button-large">
<?php
echo sprintf(
// Translators: 1 - & symbol.
esc_html__( 'Submit %1$s Deactivate', 'all-in-one-seo-pack' ),
'&'
);
?>
</button>
<a href="#" class="am-deactivate-survey-deactivate">
<?php
echo sprintf(
// Translators: 1 - & symbol.
esc_html__( 'Skip %1$s Deactivate', 'all-in-one-seo-pack' ),
'&'
);
?>
</a>
</div>
</form>
</div>
</div>
<?php
}
} NetworkAdmin.php 0000644 00000003222 15154554337 0007672 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Abstract class that Pro and Lite both extend.
*
* @since 4.2.5
*/
class NetworkAdmin extends Admin {
/**
* Construct method.
*
* @since 4.2.5
*/
public function __construct() {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
if (
is_network_admin() &&
! is_plugin_active_for_network( plugin_basename( AIOSEO_FILE ) )
) {
return;
}
if ( wp_doing_ajax() || wp_doing_cron() ) {
return;
}
add_action( 'sanitize_comment_cookies', [ $this, 'init' ], 21 );
}
/**
* Initialize the admin.
*
* @since 4.2.5
*
* @return void
*/
public function init() {
add_action( 'network_admin_menu', [ $this, 'addNetworkMenu' ] );
add_action( 'init', [ $this, 'setPages' ] );
}
/**
* Add the network menu inside of WordPress.
*
* @since 4.2.5
*
* @return void
*/
public function addNetworkMenu() {
$this->addMainMenu( 'aioseo' );
foreach ( $this->pages as $slug => $page ) {
if (
'aioseo-settings' !== $slug &&
'aioseo-tools' !== $slug &&
'aioseo-about' !== $slug &&
'aioseo-feature-manager' !== $slug
) {
continue;
}
$hook = add_submenu_page(
$this->pageSlug,
! empty( $page['page_title'] ) ? $page['page_title'] : $page['menu_title'],
$page['menu_title'],
$this->getPageRequiredCapability( $slug ),
$slug,
[ $this, 'page' ]
);
add_action( "load-{$hook}", [ $this, 'hooks' ] );
}
// Remove the "dashboard" submenu page that is not needed in the network admin.
remove_submenu_page( $this->pageSlug, $this->pageSlug );
}
} Notices/ConflictingPlugins.php 0000644 00000012466 15154554337 0012507 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin\Notices;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles the Conflicting Plugins notice..
*
* @since 4.5.1
*/
class ConflictingPlugins {
/**
* Class constructor.
*
* @since 4.5.1
*/
public function __construct() {
add_action( 'wp_ajax_aioseo-dismiss-conflicting-plugins-notice', [ $this, 'dismissNotice' ] );
add_action( 'wp_ajax_aioseo-deactivate-conflicting-plugins-notice', [ $this, 'deactivateConflictingPlugins' ] );
}
/**
* Go through all the checks to see if we should show the notice.
*
* @since 4.5.1
*
* @return void
*/
public function maybeShowNotice() {
$dismissed = get_option( '_aioseo_conflicting_plugins_dismissed', true );
if ( '1' === $dismissed ) {
return;
}
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
// Only show if there are conflicting plugins.
$conflictingPlugins = aioseo()->conflictingPlugins->getAllConflictingPlugins();
if ( empty( $conflictingPlugins ) ) {
return;
}
$this->showNotice();
// Print the script to the footer.
add_action( 'admin_footer', [ $this, 'printScript' ] );
}
/**
* Renders the notice.
*
* @since 4.5.1
*
* @return void
*/
public function showNotice() {
$type = ! empty( aioseo()->conflictingPlugins->getConflictingPlugins( 'seo' ) ) ? 'SEO' : 'sitemap';
?>
<div class="notice notice-error aioseo-conflicting-plugin-notice is-dismissible">
<p>
<?php
echo wp_kses(
sprintf(
// phpcs:ignore Generic.Files.LineLength.MaxExceeded
// Translators: 1 - Type of conflicting plugin (i.e. SEO or Sitemap), 2 - Opening HTML link tag, 3 - Closing HTML link tag.
__( 'Please keep only one %1$s plugin active, otherwise, you might lose your rankings and traffic. %2$sClick here to Deactivate.%3$s', 'all-in-one-seo-pack' ), // phpcs:ignore Generic.Files.LineLength.MaxExceeded
$type,
'<a href="#" rel="noopener noreferrer" class="deactivate-conflicting-plugins">',
'</a>'
),
[
'a' => [
'href' => [],
'rel' => [],
'class' => []
],
'strong' => [],
]
);
?>
</p>
</div>
<style>
#conflicting_seo_plugins.rank-math-notice {
display: none;
}
</style>
<?php
}
/**
* Print the script for dismissing the notice.
*
* @since 4.5.1
*
* @return void
*/
public function printScript() {
// Create a nonce.
$nonce1 = wp_create_nonce( 'aioseo-dismiss-conflicting-plugins' );
$nonce2 = wp_create_nonce( 'aioseo-deactivate-conflicting-plugins' );
?>
<script>
window.addEventListener('load', function () {
var dismissBtn,
deactivateBtn
// Add an event listener to the dismiss button.
dismissBtn = document.querySelector('.aioseo-conflicting-plugin-notice .notice-dismiss')
dismissBtn.addEventListener('click', function (event) {
var httpRequest = new XMLHttpRequest(),
postData = ''
// Build the data to send in our request.
postData += '&action=aioseo-dismiss-conflicting-plugins-notice'
postData += '&nonce=<?php echo esc_html( $nonce1 ); ?>'
httpRequest.open('POST', '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>')
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
httpRequest.send(postData)
})
deactivateBtn = document.querySelector('.aioseo-conflicting-plugin-notice .deactivate-conflicting-plugins')
deactivateBtn.addEventListener('click', function (event) {
event.preventDefault()
var httpRequest = new XMLHttpRequest(),
postData = ''
// Build the data to send in our request.
postData += '&action=aioseo-deactivate-conflicting-plugins-notice'
postData += '&nonce=<?php echo esc_html( $nonce2 ); ?>'
httpRequest.open('POST', '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>')
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
httpRequest.onerror = function () {
window.location.reload()
}
httpRequest.onload = function () {
window.location.reload()
}
httpRequest.send(postData)
})
});
</script>
<?php
}
/**
* Dismiss the notice.
*
* @since 4.5.1
*
* @return string The successful response.
*/
public function dismissNotice() {
// Early exit if we're not on a aioseo-dismiss-conflicting-plugins-notice action.
if ( ! isset( $_POST['action'] ) || 'aioseo-dismiss-conflicting-plugins-notice' !== $_POST['action'] ) {
return wp_send_json_error( 'invalid-action' );
}
check_ajax_referer( 'aioseo-dismiss-conflicting-plugins', 'nonce' );
update_option( '_aioseo_conflicting_plugins_dismissed', true );
return wp_send_json_success();
}
/**
* Deactivates the conflicting plugins.
*
* @since 4.5.1
*
* @return string The successful response.
*/
public function deactivateConflictingPlugins() {
// Early exit if we're not on a aioseo-dismiss-conflicting-plugins-notice action.
if ( ! isset( $_POST['action'] ) || 'aioseo-deactivate-conflicting-plugins-notice' !== $_POST['action'] ) {
return wp_send_json_error( 'invalid-action' );
}
check_ajax_referer( 'aioseo-deactivate-conflicting-plugins', 'nonce' );
aioseo()->conflictingPlugins->deactivateConflictingPlugins( [ 'seo', 'sitemap' ] );
return wp_send_json_success();
}
} Notices/DeprecatedWordPress.php 0000644 00000011266 15154554337 0012614 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin\Notices;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WordPress Deprecated Notice.
*
* @since 4.1.2
*/
class DeprecatedWordPress {
/**
* Class Constructor.
*
* @since 4.1.2
*/
public function __construct() {
add_action( 'wp_ajax_aioseo-dismiss-deprecated-wordpress-notice', [ $this, 'dismissNotice' ] );
}
/**
* Go through all the checks to see if we should show the notice.
*
* @since 4.1.2
*
* @return void
*/
public function maybeShowNotice() {
global $wp_version; // phpcs:ignore Squiz.NamingConventions.ValidVariableName
$dismissed = get_option( '_aioseo_deprecated_wordpress_dismissed', true );
if ( '1' === $dismissed ) {
return;
}
// Show to users that interact with our pluign.
if ( ! current_user_can( 'publish_posts' ) ) {
return;
}
// Show if WordPress version is deprecated.
if ( version_compare( $wp_version, '5.4', '>=' ) ) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName
return;
}
$this->showNotice();
// Print the script to the footer.
add_action( 'admin_footer', [ $this, 'printScript' ] );
}
/**
* Actually show the review plugin.
*
* @since 4.1.2
*
* @return void
*/
public function showNotice() {
$medium = false !== strpos( AIOSEO_PHP_VERSION_DIR, 'pro' ) ? 'proplugin' : 'liteplugin';
?>
<div class="notice notice-warning aioseo-deprecated-wordpress-notice is-dismissible">
<p>
<?php
echo wp_kses(
sprintf(
// Translators: 1 - Opening HTML bold tag, 2 - Closing HTML bold tag.
__( 'Your site is running an %1$soutdated version%2$s of WordPress. We recommend using the latest version of WordPress in order to keep your site secure.', 'all-in-one-seo-pack' ), // phpcs:ignore Generic.Files.LineLength.MaxExceeded
'<strong>',
'</strong>'
),
[
'strong' => [],
]
);
?>
<br><br>
<?php
echo wp_kses(
sprintf(
// phpcs:ignore Generic.Files.LineLength.MaxExceeded
// Translators: 1 - Opening HTML bold tag, 2 - Closing HTML bold tag, 3 - The short plugin name ("AIOSEO"), 4 - The current year, 5 - Opening HTML link tag, 6 - Closing HTML link tag.
__( '%1$sNote:%2$s %3$s will be discontinuing support for WordPress versions older than version 5.7 by the end of %4$s. %5$sRead more for additional information.%6$s', 'all-in-one-seo-pack' ), // phpcs:ignore Generic.Files.LineLength.MaxExceeded
'<strong>',
'</strong>',
'AIOSEO',
gmdate( 'Y' ),
'<a href="https://aioseo.com/docs/update-wordpress/?utm_source=WordPress&utm_medium=' . $medium . '&utm_campaign=outdated-wordpress-notice" target="_blank" rel="noopener noreferrer">', // phpcs:ignore Generic.Files.LineLength.MaxExceeded
'</a>'
),
[
'a' => [
'href' => [],
'target' => [],
'rel' => [],
],
'strong' => [],
]
);
?>
</p>
</div>
<?php
// In case this is on plugin activation.
if ( isset( $_GET['activate'] ) ) { // phpcs:ignore HM.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Recommended
unset( $_GET['activate'] );
}
}
/**
* Print the script for dismissing the notice.
*
* @since 4.1.2
*
* @return void
*/
public function printScript() {
// Create a nonce.
$nonce = wp_create_nonce( 'aioseo-dismiss-deprecated-wordpress' );
?>
<script>
window.addEventListener('load', function () {
var dismissBtn
// Add an event listener to the dismiss button.
dismissBtn = document.querySelector('.aioseo-deprecated-wordpress-notice .notice-dismiss')
dismissBtn.addEventListener('click', function (event) {
var httpRequest = new XMLHttpRequest(),
postData = ''
// Build the data to send in our request.
postData += '&action=aioseo-dismiss-deprecated-wordpress-notice'
postData += '&nonce=<?php echo esc_html( $nonce ); ?>'
httpRequest.open('POST', '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>')
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
httpRequest.send(postData)
})
});
</script>
<?php
}
/**
* Dismiss the deprecated WordPress notice.
*
* @since 4.1.2
*
* @return string The successful response.
*/
public function dismissNotice() {
// Early exit if we're not on a aioseo-dismiss-deprecated-wordpress-notice action.
if ( ! isset( $_POST['action'] ) || 'aioseo-dismiss-deprecated-wordpress-notice' !== $_POST['action'] ) {
return;
}
check_ajax_referer( 'aioseo-dismiss-deprecated-wordpress', 'nonce' );
update_option( '_aioseo_deprecated_wordpress_dismissed', true );
return wp_send_json_success();
}
} Notices/Import.php 0000644 00000002375 15154554337 0010156 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin\Notices;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Plugin import notice.
*
* @since 4.0.0
*/
class Import {
/**
* Go through all the checks to see if we should show the notice.
*
* @since 4.0.0
*
* @return void
*/
public function maybeShowNotice() {
if ( ! aioseo()->importExport->isImportRunning() ) {
return;
}
$this->showNotice();
}
/**
* Register the notice so that it appears.
*
* @since 4.0.0
*
* @return void
*/
public function showNotice() {
$string1 = __( 'SEO Meta Import In Progress', 'all-in-one-seo-pack' );
// Translators: 1 - The plugin name ("All in One SEO").
$string2 = sprintf( __( '%1$s is importing your existing SEO data in the background.', 'all-in-one-seo-pack' ), AIOSEO_PLUGIN_NAME );
$string3 = __( 'This notice will automatically disappear as soon as the import has completed. Meanwhile, everything should continue to work as expected.', 'all-in-one-seo-pack' );
?>
<div class="notice notice-info aioseo-migration">
<p><strong><?php echo esc_html( $string1 ); ?></strong></p>
<p><?php echo esc_html( $string2 ); ?></p>
<p><?php echo esc_html( $string3 ); ?></p>
</div>
<style>
</style>
<?php
}
} Notices/Migration.php 0000644 00000003044 15154554337 0010627 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin\Notices;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* V3 to V4 migration notice.
*
* @since 4.0.0
*/
class Migration {
/**
* Go through all the checks to see if we should show the notice.
*
* @since 4.0.0
*
* @return void
*/
public function maybeShowNotice() {
$transientPosts = aioseo()->core->cache->get( 'v3_migration_in_progress_posts' );
$transientTerms = aioseo()->core->cache->get( 'v3_migration_in_progress_terms' );
if ( ! $transientPosts && ! $transientTerms ) {
return;
}
$this->showNotice();
}
/**
* Register the notice so that it appears.
*
* @since 4.0.0
*
* @return void
*/
public function showNotice() {
// Translators: 1 - The plugin name ("AIOSEO).
$string1 = sprintf( __( '%1$s V3->V4 Migration In Progress', 'all-in-one-seo-pack' ), AIOSEO_PLUGIN_SHORT_NAME );
// Translators: 1 - The plugin name ("All in One SEO").
$string2 = sprintf( __( '%1$s is currently upgrading your database and migrating your SEO data in the background.', 'all-in-one-seo-pack' ), AIOSEO_PLUGIN_NAME );
$string3 = __( 'This notice will automatically disappear as soon as the migration has completed. Meanwhile, everything should continue to work as expected.', 'all-in-one-seo-pack' );
?>
<div class="notice notice-info aioseo-migration">
<p><strong><?php echo esc_html( $string1 ); ?></strong></p>
<p><?php echo esc_html( $string2 ); ?></p>
<p><?php echo esc_html( $string3 ); ?></p>
</div>
<style>
</style>
<?php
}
} Notices/Notices.php 0000644 00000004706 15154554337 0010310 0 ustar 00 <?php
namespace AIOSEO\Plugin\Lite\Admin\Notices;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Admin\Notices as CommonNotices;
use AIOSEO\Plugin\Common\Models;
/**
* Lite version of the notices class.
*
* @since 4.0.0
*/
class Notices extends CommonNotices\Notices {
/**
* Initialize the internal notices.
*
* @since 4.0.0
*
* @return void
*/
protected function initInternalNotices() {
parent::initInternalNotices();
$this->wooUpsellNotice();
}
/**
* Validates the notification type.
*
* @since 4.0.0
*
* @param string $type The notification type we are targeting.
* @return boolean True if yes, false if no.
*/
public function validateType( $type ) {
$validated = parent::validateType( $type );
// Any lite notification should pass here.
if ( 'lite' === $type ) {
$validated = true;
}
return $validated;
}
/**
* Add a notice if WooCommerce is detected and not licensed or running Lite.
*
* @since 4.0.0
*
* @return void
*/
private function wooUpsellNotice() {
$notification = Models\Notification::getNotificationByName( 'woo-upsell' );
if (
! class_exists( 'WooCommerce' )
) {
if ( $notification->exists() ) {
Models\Notification::deleteNotificationByName( 'woo-upsell' );
}
return;
}
if ( $notification->exists() ) {
return;
}
Models\Notification::addNotification( [
'slug' => uniqid(),
'notification_name' => 'woo-upsell',
// Translators: 1 - "WooCommerce".
'title' => sprintf( __( 'Advanced %1$s Support', 'all-in-one-seo-pack' ), 'WooCommerce' ),
// Translators: 1 - "WooCommerce", 2 - The plugin short name ("AIOSEO").
'content' => sprintf( __( 'We have detected you are running %1$s. Upgrade to %2$s to unlock our advanced eCommerce SEO features, including SEO for Product Categories and more.', 'all-in-one-seo-pack' ), 'WooCommerce', AIOSEO_PLUGIN_SHORT_NAME . ' Pro' ), // phpcs:ignore Generic.Files.LineLength.MaxExceeded
'type' => 'info',
'level' => [ 'all' ],
// Translators: 1 - "Pro".
'button1_label' => sprintf( __( 'Upgrade to %1$s', 'all-in-one-seo-pack' ), 'Pro' ),
'button1_action' => html_entity_decode( apply_filters( 'aioseo_upgrade_link', aioseo()->helpers->utmUrl( AIOSEO_MARKETING_URL . 'lite-upgrade/', 'woo-notification-upsell', false ) ) ),
'start' => gmdate( 'Y-m-d H:i:s' )
] );
}
} Notices/WpNotices.php 0000644 00000015515 15154554337 0010617 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin\Notices;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WpNotices class.
*
* @since 4.2.3
*/
class WpNotices {
/**
* Notices array
*
* @since 4.2.3
*
* @var array
*/
private $notices = [];
/**
* The cache key.
*
* @since 4.2.3
*
* @var string
*/
private $cacheKey = 'wp_notices';
/**
* Class Constructor.
*
* @since 4.2.3
*/
public function __construct() {
add_action( 'rest_api_init', [ $this, 'registerApiField' ] );
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueueScripts' ] );
add_action( 'admin_notices', [ $this, 'adminNotices' ] );
}
/**
* Enqueue notices scripts.
*
* @since 4.2.3
*
* @return void
*/
public function enqueueScripts() {
aioseo()->core->assets->load( 'src/vue/standalone/wp-notices/main.js' );
}
/**
* Registers an API field with notices.
*
* @since 4.2.3
*
* @return void
*/
public function registerApiField() {
foreach ( aioseo()->helpers->getPublicPostTypes( true ) as $postType ) {
register_rest_field( $postType, 'aioseo_notices', [
'get_callback' => [ $this, 'apiGetNotices' ]
] );
}
}
/**
* API field callback.
*
* @since 4.2.3
*
* @return array Notices array
*/
public function apiGetNotices() {
$notices = $this->getNoticesInContext();
// Notices show only one time.
$this->removeNotices( $notices );
return $notices;
}
/**
* Get all notices.
*
* @since 4.2.3
*
* @return array Notices array
*/
public function getNotices() {
if ( empty( $this->notices ) ) {
$this->notices = (array) aioseo()->core->cache->get( $this->cacheKey );
}
return ! empty( $this->notices ) ? $this->notices : [];
}
/**
* Get all notices in the current context.
*
* @since 4.2.6
*
* @return array Notices array
*/
public function getNoticesInContext() {
$contextNotices = $this->getNotices();
foreach ( $contextNotices as $key => $notice ) {
if ( empty( $notice['allowedContexts'] ) ) {
continue;
}
$allowed = false;
foreach ( $notice['allowedContexts'] as $allowedContext ) {
if ( $this->isAllowedContext( $allowedContext ) ) {
$allowed = true;
break;
}
}
if ( ! $allowed ) {
unset( $contextNotices[ $key ] );
}
}
return $contextNotices;
}
/**
* Test if we are in the current context.
*
* @since 4.2.6
*
* @param string $context The context to test. (posts)
* @return bool Is the required context.
*/
private function isAllowedContext( $context ) {
switch ( $context ) {
case 'posts':
return aioseo()->helpers->isScreenPostList() ||
aioseo()->helpers->isScreenPostEdit() ||
aioseo()->helpers->isAjaxCronRestRequest();
}
return false;
}
/**
* Finds a notice by message.
*
* @since 4.2.3
*
* @param string $message The message string.
* @param string $type The message type.
* @return void|array The found notice.
*/
public function getNotice( $message, $type = '' ) {
$notices = $this->getNotices();
foreach ( $notices as $notice ) {
if ( $notice['options']['id'] === $this->getNoticeId( $message, $type ) ) {
return $notice;
}
}
}
/**
* Generates a notice id.
*
* @since 4.2.3
*
* @param string $message The message string.
* @param string $type The message type.
* @return string The notice id.
*/
public function getNoticeId( $message, $type = '' ) {
return md5( $message . $type );
}
/**
* Clear notices.
*
* @since 4.2.3
*
* @return void
*/
public function clearNotices() {
$this->notices = [];
$this->updateCache();
}
/**
* Remove certain notices.
*
* @since 4.2.6
*
* @param array $notices A list of notices to remove.
* @return void
*/
public function removeNotices( $notices ) {
foreach ( array_keys( $notices ) as $noticeKey ) {
unset( $this->notices[ $noticeKey ] );
}
$this->updateCache();
}
/**
* Adds a notice.
*
* @since 4.2.3
*
* @param string $message The message.
* @param string $status The message status [success, info, warning, error]
* @param array $options Options for the message. https://developer.wordpress.org/block-editor/reference-guides/data/data-core-notices/#createnotice
* @param array $allowedContexts The contexts where this notice will show.
* @return void
*/
public function addNotice( $message, $status = 'warning', $options = [], $allowedContexts = [] ) {
$type = ! empty( $options['type'] ) ? $options['type'] : '';
$foundNotice = $this->getNotice( $message, $type );
if ( empty( $message ) || ! empty( $foundNotice ) ) {
return;
}
$notice = [
'message' => $message,
'status' => $status,
'options' => wp_parse_args( $options, [
'id' => $this->getNoticeId( $message, $type ),
'isDismissible' => true
] ),
'allowedContexts' => $allowedContexts
];
$this->notices[] = $notice;
$this->updateCache();
}
/**
* Show notices on classic editor.
*
* @since 4.2.3
*
* @return void
*/
public function adminNotices() {
// Double check we're actually in the admin before outputting anything.
if ( ! is_admin() ) {
return;
}
$notices = $this->getNoticesInContext();
foreach ( $notices as $notice ) {
// Hide snackbar notices on classic editor.
if ( ! empty( $notice['options']['type'] ) && 'snackbar' === $notice['options']['type'] ) {
continue;
}
$status = ! empty( $notice['status'] ) ? $notice['status'] : 'warning';
$class = ! empty( $notice['options']['class'] ) ? $notice['options']['class'] : '';
?>
<div
class="notice notice-<?php echo esc_attr( $status ) ?> <?php echo esc_attr( $class ) ?>">
<?php echo '<p>' . $notice['message'] . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php
if ( ! empty( $notice['options']['actions'] ) ) {
foreach ( $notice['options']['actions'] as $action ) {
echo '<p>';
if ( ! empty( $action['url'] ) ) {
$class = ! empty( $action['class'] ) ? $action['class'] : '';
$target = ! empty( $action['target'] ) ? $action['target'] : '';
echo '<a
href="' . esc_attr( $action['url'] ) . '"
class="' . esc_attr( $class ) . '"
target="' . esc_attr( $target ) . '"
>';
}
echo $action['label']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ( ! empty( $action['url'] ) ) {
echo '</a>';
}
echo '</p>';
}
?>
<?php } ?>
</div>
<?php
}
// Notices show only one time.
$this->removeNotices( $notices );
}
/**
* Helper to update the cache with the current notices array.
*
* @since 4.2.6
*
* @return void
*/
private function updateCache() {
aioseo()->core->cache->update( $this->cacheKey, $this->notices );
}
} Pointers.php 0000644 00000010011 15154554337 0007065 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Models;
/**
* Handles the pointers for the admin.
*
* @since 4.8.3
*/
class Pointers {
/**
* Class constructor.
*
* @since 4.8.3
*/
public function __construct() {
if ( ! is_admin() ) {
return;
}
add_action( 'admin_init', [ $this, 'maybeDismissPointer' ] );
add_action( 'in_admin_header', [ $this, 'init' ] );
}
/**
* Initializes the pointers.
*
* @since 4.8.3
*
* @return void
*/
public function init() {
$this->registerKwRankTracker();
}
/**
* Checks if a pointer should be dismissed.
*
* @since 4.8.3
*
* @return void
*/
public function maybeDismissPointer() {
if (
! isset( $_GET['aioseo-dismiss-pointer'] ) ||
! isset( $_GET['aioseo-dismiss-pointer-nonce'] ) ||
! wp_verify_nonce( $_GET['aioseo-dismiss-pointer-nonce'], 'aioseo-dismiss-pointer' )
) {
return;
}
$pointer = sanitize_text_field( wp_unslash( $_GET['aioseo-dismiss-pointer'] ) );
update_user_meta( get_current_user_id(), "_aioseo-$pointer-dismissed", true );
}
/**
* Registers a pointer.
*
* @since 4.8.3
*
* @return void
*/
public function registerPointer( $id, $pageSlug, $args ) {
if ( get_user_meta( get_current_user_id(), "_aioseo-$id-dismissed", true ) ) {
return;
}
if ( "all-in-one-seo_page_aioseo-{$pageSlug}" === aioseo()->helpers->getCurrentScreen()->id ) {
return;
}
wp_enqueue_style( 'wp-pointer' );
wp_enqueue_script( 'wp-pointer' );
// phpcs:disable AIOSEO.Wp.I18n.NonSingularStringLiteralText, Squiz.PHP.EmbeddedPhp, Generic.WhiteSpace.ScopeIndent.IncorrectExact
?>
<script>
jQuery( document ).ready( function( $ ) {
var isClosed = false;
var pointer = $( '#toplevel_page_aioseo > a' ).pointer( {
content :
"<h3><?php esc_html_e( $args['title'], 'all-in-one-seo-pack' ); ?><\/h3>" +
"<h4><?php esc_html_e( $args['subtitle'], 'all-in-one-seo-pack' ); ?><\/h4>" +
"<p><?php esc_html_e( $args['content'], 'all-in-one-seo-pack' ); ?><\/p>" +
"<?php
echo sprintf(
'<p><a class=\"button button-primary\" href=\"%s\">%s</a></p>',
esc_attr( esc_url( $args['url'] ) ),
esc_html__( $args['button'], 'all-in-one-seo-pack' )
);
?>",
position : {
edge : <?php echo is_rtl() ? "'right'" : "'left'"; ?>,
align : 'center'
},
pointerWidth : 420,
show: function(event, el) {
el.pointer.css({'position':'fixed'});
el.pointer.addClass('aioseo-wp-pointer');
},
close : function() {
isClosed = true;
jQuery.get(
window.location.href,
{
'aioseo-dismiss-pointer' : '<?php echo esc_js( $id ); ?>',
'aioseo-dismiss-pointer-nonce' : '<?php echo esc_js( wp_create_nonce( 'aioseo-dismiss-pointer' ) ); ?>'
}
);
}
} ).pointer('open');
} );
</script>
<?php
// phpcs:enable
}
/**
* Registers the KW Rank Tracker pointer.
*
* @since 4.8.3
*
* @return void
*/
public function registerKwRankTracker() {
if (
! current_user_can( 'aioseo_search_statistics_settings' ) ||
(
is_object( aioseo()->license ) &&
aioseo()->license->hasCoreFeature( 'search-statistics', 'keyword-rank-tracker' ) &&
aioseo()->searchStatistics->api->auth->isConnected()
)
) {
return;
}
$nonce = wp_create_nonce( 'aioseo-dismiss-pointer' );
$args = [
'title' => 'NEW! Keyword Rank Tracker',
'subtitle' => 'Get insights into how your site is performing for your most important keywords',
'content' => 'Track keywords and combine them into groups to see how your site is performing for key topics in Google search results.',
'url' => admin_url( 'admin.php?aioseo-dismiss-pointer=kw-rank-tracker&aioseo-dismiss-pointer-nonce=' . $nonce . '&page=aioseo-search-statistics#/keyword-rank-tracker' ),
'button' => 'Unlock Keyword Rank Tracker'
];
$this->registerPointer( 'kw-rank-tracker', 'search-statistics', $args );
}
} PostSettings.php 0000644 00000003366 15154554337 0007747 0 ustar 00 <?php
namespace AIOSEO\Plugin\Lite\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Admin as CommonAdmin;
/**
* Abstract class that Pro and Lite both extend.
*
* @since 4.0.0
*/
class PostSettings extends CommonAdmin\PostSettings {
/**
* Holds a list of page builder integration class instances.
* This prop exists for backwards compatibility with pre-4.2.0 versions (see backwardsCompatibilityLoad() in AIOSEO.php).
*
* @since 4.4.2
*
* @var object[]
*/
public $integrations = null;
/**
* Initialize the admin.
*
* @since 4.0.0
*
* @return void
*/
public function __construct() {
parent::__construct();
}
/**
* Add upsell to terms.
*
* @since 4.0.0
*
* @return void
*/
public function init() {
if ( is_admin() ) {
// We don't call getPublicTaxonomies() here because we want to show the CTA for Product Attributes as well.
$taxonomies = get_taxonomies( [], 'objects' );
foreach ( $taxonomies as $taxObject ) {
if (
empty( $taxObject->label ) ||
! is_taxonomy_viewable( $taxObject )
) {
unset( $taxonomies[ $taxObject->name ] );
}
}
foreach ( $taxonomies as $taxonomy ) {
add_action( $taxonomy->name . '_edit_form', [ $this, 'addTaxonomyUpsell' ] );
add_action( 'after-' . $taxonomy->name . '-table', [ $this, 'addTaxonomyUpsell' ] );
}
}
}
/**
* Add Taxonomy Upsell
*
* @since 4.0.0
*
* @return void
*/
public function addTaxonomyUpsell() {
$screen = aioseo()->helpers->getCurrentScreen();
if (
! isset( $screen->parent_base ) ||
'edit' !== $screen->parent_base ||
empty( $screen->taxonomy )
) {
return;
}
include_once AIOSEO_DIR . '/app/Lite/Views/taxonomy-upsell.php';
}
} SeoAnalysis.php 0000644 00000001661 15154554337 0007527 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Models\SeoAnalyzerResult;
/**
* Handles all admin code for the SEO Analysis menu.
*
* @since 4.2.6
*/
class SeoAnalysis {
/**
* Class constructor.
*
* @since 4.2.6
*/
public function __construct() {
add_action( 'save_post', [ $this, 'bustStaticHomepageResults' ] );
}
/**
* Busts the SEO Analysis for the static homepage when it is updated.
*
* @since 4.2.6
*
* @param int $postId The post ID.
* @return void
*/
public function bustStaticHomepageResults( $postId ) {
if ( ! aioseo()->helpers->isStaticHomePage( $postId ) ) {
return;
}
aioseo()->internalOptions->internal->siteAnalysis->score = 0;
SeoAnalyzerResult::deleteByUrl( null );
aioseo()->core->cache->delete( 'analyze_site_code' );
aioseo()->core->cache->delete( 'analyze_site_body' );
}
} SlugMonitor.php 0000644 00000012421 15154554337 0007553 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Monitors changes to post slugs.
*
* @since 4.2.3
*/
class SlugMonitor {
/**
* Holds posts that have been updated.
*
* @since 4.2.3
*
* @var array
*/
private $updatedPosts = [];
/**
* Class constructor.
*
* @since 4.2.3
*/
public function __construct() {
// We can't monitor changes without permalinks enabled.
if ( ! get_option( 'permalink_structure' ) ) {
return;
}
add_action( 'pre_post_update', [ $this, 'prePostUpdate' ] );
// WP 5.6+.
if ( function_exists( 'wp_after_insert_post' ) ) {
add_action( 'wp_after_insert_post', [ $this, 'afterInsertPost' ], 11, 4 );
} else {
add_action( 'post_updated', [ $this, 'postUpdated' ], 11, 3 );
}
}
/**
* Remember the previous post permalink.
*
* @since 4.2.3
*
* @param integer $postId The post ID.
* @return void
*/
public function prePostUpdate( $postId ) {
$this->updatedPosts[ $postId ] = get_permalink( $postId );
}
/**
* Called when a post has been completely inserted ( with categories and meta ).
*
* @since 4.2.3
*
* @param integer $postId The post ID.
* @param \WP_Post $post The post object.
* @param bool $update Whether this is an existing post being updated.
* @param null|\WP_Post $postBefore The post object before changes were made.
* @return void
*/
public function afterInsertPost( $postId, $post = null, $update = false, $postBefore = null ) {
if ( ! $update ) {
return;
}
$this->postUpdated( $postId, $post, $postBefore );
}
/**
* Called when a post has been updated - check if the slug has changed.
*
* @since 4.2.3
*
* @param integer $postId The post ID.
* @param \WP_Post $post The post object.
* @param \WP_Post $postBefore The post object before changes were made.
* @return void
*/
public function postUpdated( $postId, $post = null, $postBefore = null ) {
if ( ! isset( $this->updatedPosts[ $postId ] ) ) {
return;
}
$before = aioseo()->helpers->getPermalinkPath( $this->updatedPosts[ $postId ] );
$after = aioseo()->helpers->getPermalinkPath( get_permalink( $postId ) );
if ( ! aioseo()->helpers->hasPermalinkChanged( $before, $after ) ) {
return;
}
// Can we monitor this slug?
if ( ! $this->canMonitorPost( $post, $postBefore ) ) {
return;
}
// Ask aioseo-redirects if automatic redirects is monitoring it.
if ( $this->automaticRedirect( $post->post_type, $before, $after ) ) {
return;
}
// Filter to allow users to disable the slug monitor messages.
if ( apply_filters( 'aioseo_redirects_disable_slug_monitor', false ) ) {
return;
}
$redirectUrl = $this->manualRedirectUrl( [
'url' => $before,
'target' => $after,
'type' => 301
] );
$message = __( 'The permalink for this post just changed! This could result in 404 errors for your site visitors.', 'all-in-one-seo-pack' );
// Default notice redirecting to the Redirects screen.
$action = [
'url' => $redirectUrl,
'label' => __( 'Add Redirect to improve SEO', 'all-in-one-seo-pack' ),
'target' => '_blank',
'class' => 'aioseo-redirects-slug-changed'
];
// If redirects is active we'll show add-redirect in a modal.
if ( aioseo()->addons->getLoadedAddon( 'redirects' ) ) {
// We need to remove the target here so the action keeps the url used by the add-redirect modal.
unset( $action['target'] );
}
aioseo()->wpNotices->addNotice( $message, 'warning', [ 'actions' => [ $action ] ], [ 'posts' ] );
}
/**
* Checks if this is a post we can monitor.
*
* @since 4.2.3
*
* @param \WP_Post $post The post object.
* @param \WP_Post $postBefore The post object before changes were made.
* @return boolean True if we can monitor this post.
*/
private function canMonitorPost( $post, $postBefore ) {
// Check that this is for the expected post.
if ( ! isset( $post->ID ) || ! isset( $this->updatedPosts[ $post->ID ] ) ) {
return false;
}
// Don't do anything if we're not published.
if ( 'publish' !== $post->post_status || 'publish' !== $postBefore->post_status ) {
return false;
}
// Don't do anything is the post type is not public.
if ( ! is_post_type_viewable( $post->post_type ) ) {
return false;
}
return true;
}
/**
* Tries to add a automatic redirect.
*
* @since 4.2.3
*
* @param string $postType The post type.
* @param string $before The url before.
* @param string $after The url after.
* @return bool True if an automatic redirect was added.
*/
private function automaticRedirect( $postType, $before, $after ) {
if ( ! aioseo()->addons->getLoadedAddon( 'redirects' ) ) {
return false;
}
return aioseoRedirects()->monitor->automaticRedirect( $postType, $before, $after );
}
/**
* Generates a URL for adding manual redirects.
*
* @since 4.2.3
*
* @param array $urls An array of [url, target, type, slash, case, regex].
* @return string The redirect link.
*/
public function manualRedirectUrl( $urls ) {
if ( ! aioseo()->addons->getLoadedAddon( 'redirects' ) ) {
return admin_url( 'admin.php?page=aioseo-redirects' );
}
return aioseoRedirects()->helpers->manualRedirectUrl( $urls );
}
} Usage.php 0000644 00000001177 15154554337 0006343 0 ustar 00 <?php
namespace AIOSEO\Plugin\Lite\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Admin as CommonAdmin;
/**
* Usage tracking class.
*
* @since 4.0.0
*/
class Usage extends CommonAdmin\Usage {
/**
* Class Constructor
*
* @since 4.0.0
*/
public function __construct() {
parent::__construct();
$this->enabled = apply_filters( 'aioseo_usage_tracking_enable', aioseo()->options->advanced->usageTracking );
}
/**
* Get the type for the request.
*
* @since 4.0.0
*
* @return string The install type.
*/
public function getType() {
return 'lite';
}
} WritingAssistant.php 0000644 00000005005 15154554337 0010606 0 ustar 00 <?php
namespace AIOSEO\Plugin\Common\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Models;
/**
* The Admin class.
*
* @since 4.7.4
*/
class WritingAssistant {
/**
* Class constructor.
*
* @since 4.7.4
*/
public function __construct() {
add_action( 'add_meta_boxes', [ $this, 'addMetabox' ] );
add_action( 'delete_post', [ $this, 'deletePost' ] );
}
/**
* Deletes the writing assistant post.
*
* @since 4.7.4
*
* @param int $postId The post id.
* @return void
*/
public function deletePost( $postId ) {
Models\WritingAssistantPost::getPost( $postId )->delete();
}
/**
* Adds a meta box to the page/posts screens.
*
* @since 4.7.4
*
* @return void
*/
public function addMetabox() {
if ( ! aioseo()->access->hasCapability( 'aioseo_page_writing_assistant_settings' ) ) {
return;
}
$postType = get_post_type();
if (
(
! aioseo()->options->writingAssistant->postTypes->all &&
! in_array( $postType, aioseo()->options->writingAssistant->postTypes->included, true )
) ||
! in_array( $postType, aioseo()->helpers->getPublicPostTypes( true ), true )
) {
return;
}
// Skip post types that do not support an editor.
if ( ! post_type_supports( $postType, 'editor' ) ) {
return;
}
// Ignore certain plugins.
if (
aioseo()->thirdParty->webStories->isPluginActive() &&
'web-story' === $postType
) {
return;
}
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAssets' ] );
// Translators: 1 - The plugin short name ("AIOSEO").
$aioseoMetaboxTitle = sprintf( esc_html__( '%1$s Writing Assistant', 'all-in-one-seo-pack' ), AIOSEO_PLUGIN_SHORT_NAME );
add_meta_box(
'aioseo-writing-assistant-metabox',
$aioseoMetaboxTitle,
[ $this, 'renderMetabox' ],
null,
'normal',
'low'
);
}
/**
* Render the on-page settings metabox with the Vue App wrapper.
*
* @since 4.7.4
*
* @return void
*/
public function renderMetabox() {
?>
<div id="aioseo-writing-assistant-metabox-app">
<?php aioseo()->templates->getTemplate( 'parts/loader.php' ); ?>
</div>
<?php
}
/**
* Enqueues the JS/CSS for the standalone.
*
* @since 4.7.4
*
* @return void
*/
public function enqueueAssets() {
if ( ! aioseo()->helpers->isScreenBase( 'post' ) ) {
return;
}
aioseo()->core->assets->load(
'src/vue/standalone/writing-assistant/main.js',
[],
aioseo()->writingAssistant->helpers->getStandaloneVueData(),
'aioseoWritingAssistant'
);
}
} Connect.php 0000644 00000027050 15154602726 0006662 0 ustar 00 <?php
namespace AIOSEO\Plugin\Lite\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Utils;
/**
* Connect to AIOSEO Pro Worker Service to connect with our Premium Services.
*
* @since 4.0.0
*/
class Connect {
/**
* Class constructor.
*
* @since 4.0.0
*/
public function __construct() {
add_action( 'wp_ajax_nopriv_aioseo_connect_process', [ $this, 'process' ] );
add_action( 'admin_menu', [ $this, 'addDashboardPage' ] );
add_action( 'admin_init', [ $this, 'maybeLoadConnect' ] );
}
/**
* Adds a dashboard page for our setup wizard.
*
* @since 4.0.0
*
* @return void
*/
public function addDashboardPage() {
add_dashboard_page( '', '', 'aioseo_manage_seo', 'aioseo-connect-pro', '' );
remove_submenu_page( 'index.php', 'aioseo-connect-pro' );
add_dashboard_page( '', '', 'aioseo_manage_seo', 'aioseo-connect', '' );
remove_submenu_page( 'index.php', 'aioseo-connect' );
}
/**
* Checks to see if we should load the connect page.
*
* @since 4.0.0
*
* @return void
*/
public function maybeLoadConnect() {
// Don't load the interface if doing an AJAX call.
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
// Check for connect-specific parameter.
// phpcs:disable HM.Security.ValidatedSanitizedInput.InputNotSanitized, HM.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Recommended, Generic.Files.LineLength.MaxExceeded
if ( ! isset( $_GET['page'] ) ) {
return;
}
$page = sanitize_text_field( wp_unslash( $_GET['page'] ) );
// phpcs:enable
// Check if we're on the right page and if current user is allowed to save settings.
if (
( 'aioseo-connect-pro' !== $page && 'aioseo-connect' !== $page ) ||
! current_user_can( 'aioseo_manage_seo' )
) {
return;
}
set_current_screen();
// Remove an action in the Gutenberg plugin ( not core Gutenberg ) which throws an error.
remove_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' );
if ( 'aioseo-connect-pro' === $page ) {
$this->loadConnectPro();
return;
}
$this->loadConnect();
// phpcs:enable
}
/**
* Load the Connect template.
*
* @since 4.0.0
*
* @return void
*/
private function loadConnect() {
$this->enqueueScripts();
$this->connectHeader();
$this->connectContent();
$this->connectFooter();
exit;
}
/**
* Load the Connect Pro template.
*
* @since 4.0.0
*
* @return void
*/
private function loadConnectPro() {
$this->enqueueScriptsPro();
$this->connectHeader();
$this->connectContent();
$this->connectFooter( 'pro' );
exit;
}
/**
* Enqueue's scripts for the setup wizard.
*
* @since 4.0.0
*
* @return void
*/
public function enqueueScripts() {
// We don't want any plugin adding notices to our screens. Let's clear them out here.
remove_all_actions( 'admin_notices' );
remove_all_actions( 'network_admin_notices' );
remove_all_actions( 'all_admin_notices' );
aioseo()->core->assets->load( 'src/vue/standalone/connect/main.js', [], aioseo()->helpers->getVueData() );
}
/**
* Enqueue's scripts for the setup wizard.
*
* @since 4.0.0
*
* @return void
*/
public function enqueueScriptsPro() {
// We don't want any plugin adding notices to our screens. Let's clear them out here.
remove_all_actions( 'admin_notices' );
remove_all_actions( 'network_admin_notices' );
remove_all_actions( 'all_admin_notices' );
aioseo()->core->assets->load( 'src/vue/standalone/connect-pro/main.js', [], aioseo()->helpers->getVueData() );
}
/**
* Outputs the simplified header used for the Onboarding Wizard.
*
* @since 4.0.0
*
* @return void
*/
public function connectHeader() {
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta name="viewport" content="width=device-width"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>
<?php
// Translators: 1 - The plugin name ("All in One SEO").
echo sprintf( esc_html__( '%1$s › Connect', 'all-in-one-seo-pack' ), esc_html( AIOSEO_PLUGIN_NAME ) );
?>
</title>
</head>
<body class="aioseo-connect">
<?php
}
/**
* Outputs the content of the current step.
*
* @since 4.0.0
*
* @return void
*/
public function connectContent() {
echo '<div id="aioseo-app">';
aioseo()->templates->getTemplate( 'admin/settings-page.php' );
echo '</div>';
}
/**
* Outputs the simplified footer used for the Onboarding Wizard.
*
* @since 4.0.0
*
* @return void
*/
public function connectFooter( $pro = '' ) {
?>
<?php
wp_print_scripts( 'aioseo-vendors' );
wp_print_scripts( 'aioseo-common' );
wp_print_scripts( "aioseo-connect-$pro-script" );
?>
</body>
</html>
<?php
}
/**
* Generates and returns the AIOSEO Connect URL.
*
* @since 4.0.0
*
* @return array The AIOSEO Connect URL or an error message inside an array.
*/
public function generateConnectUrl( $key, $redirect = null ) {
// Check for permissions.
if ( ! current_user_can( 'install_plugins' ) ) {
return [
'error' => esc_html__( 'You are not allowed to install plugins.', 'all-in-one-seo-pack' )
];
}
if ( empty( $key ) ) {
return [
'error' => esc_html__( 'Please enter your license key to connect.', 'all-in-one-seo-pack' ),
];
}
// Verify pro version is not installed.
$active = activate_plugin( 'all-in-one-seo-pack-pro/all_in_one_seo_pack_pro', false, false, true );
if ( ! is_wp_error( $active ) ) {
return [
'error' => esc_html__( 'Pro version is already installed.', 'all-in-one-seo-pack' )
];
}
// Just check if network is set.
$network = isset( $_POST['network'] ) ? (bool) sanitize_text_field( wp_unslash( $_POST['network'] ) ) : false; // phpcs:ignore HM.Security.ValidatedSanitizedInput.InputNotSanitized, HM.Security.NonceVerification.Missing, WordPress.Security.NonceVerification, Generic.Files.LineLength.MaxExceeded
$network = ! empty( $network );
// Generate a hash that can be compared after the user is redirected back.
$oth = hash( 'sha512', wp_rand() );
$hashedOth = hash_hmac( 'sha512', $oth, wp_salt() );
// Save the options.
aioseo()->internalOptions->internal->connect->key = $key;
aioseo()->internalOptions->internal->connect->time = time();
aioseo()->internalOptions->internal->connect->network = $network;
aioseo()->internalOptions->internal->connect->token = $oth;
$url = add_query_arg( [
'key' => $key,
'network' => $network,
'token' => $hashedOth,
'version' => aioseo()->version,
'siteurl' => admin_url(),
'homeurl' => home_url(),
'endpoint' => admin_url( 'admin-ajax.php' ),
'php' => PHP_VERSION,
'wp' => get_bloginfo( 'version' ),
'redirect' => rawurldecode( base64_encode( $redirect ? $redirect : admin_url( 'admin.php?page=aioseo-settings' ) ) ),
'v' => 1,
], defined( 'AIOSEO_UPGRADE_URL' ) ? AIOSEO_UPGRADE_URL : 'https://upgrade.aioseo.com' );
// We're storing the ID of the user who is installing Pro so that we can add capabilties for him after upgrading.
aioseo()->core->cache->update( 'connect_active_user', get_current_user_id(), 15 * MINUTE_IN_SECONDS );
return [
'url' => $url,
];
}
/**
* Process AIOSEO Connect.
*
* @since 1.0.0
*
* @return array An array containing a valid response or an error message.
*/
public function process() {
// phpcs:disable HM.Security.NonceVerification.Missing, WordPress.Security.NonceVerification
$hashedOth = ! empty( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '';
$downloadUrl = ! empty( $_POST['file'] ) ? esc_url_raw( wp_unslash( $_POST['file'] ) ) : '';
// phpcs:enable
$error = sprintf(
// Translators: 1 - The marketing site domain ("aioseo.com").
esc_html__( 'Could not install upgrade. Please download from %1$s and install manually.', 'all-in-one-seo-pack' ),
esc_html( AIOSEO_MARKETING_DOMAIN )
);
$success = esc_html__( 'Plugin installed & activated.', 'all-in-one-seo-pack' );
// Check if all required params are present.
if ( empty( $downloadUrl ) || empty( $hashedOth ) ) {
wp_send_json_error( $error );
}
$oth = aioseo()->internalOptions->internal->connect->token;
if ( empty( $oth ) ) {
wp_send_json_error( $error );
}
// Check if the stored hash matches the salted one that is sent back from the server.
if ( hash_hmac( 'sha512', $oth, wp_salt() ) !== $hashedOth ) {
wp_send_json_error( $error );
}
// Delete connect token so we don't replay.
aioseo()->internalOptions->internal->connect->token = null;
// Verify pro not activated.
if ( aioseo()->pro ) {
wp_send_json_success( $success );
}
// Check license key.
$licenseKey = aioseo()->internalOptions->internal->connect->key;
if ( ! $licenseKey ) {
wp_send_json_error( esc_html__( 'You are not licensed.', 'all-in-one-seo-pack' ) );
}
// Set the license key in a new option so we can get it when Pro is activated.
aioseo()->internalOptions->internal->connectLicenseKey = $licenseKey;
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-screen.php';
require_once ABSPATH . 'wp-admin/includes/screen.php';
// Set the current screen to avoid undefined notices.
set_current_screen( 'toplevel_page_aioseo' );
// Prepare variables.
$url = esc_url_raw(
add_query_arg(
[
'page' => 'aioseo-settings',
],
admin_url( 'admin.php' )
)
);
// Verify pro not installed.
$network = aioseo()->internalOptions->internal->connect->network;
$active = activate_plugin( 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', $url, $network, true );
if ( ! is_wp_error( $active ) ) {
aioseo()->internalOptions->internal->connect->reset();
// Because the regular activation hooks won't run, we need to add capabilities for the installing user so that he doesn't run into an error on the first request.
aioseo()->activate->addCapabilitiesOnUpgrade();
wp_send_json_success( $success );
}
$creds = request_filesystem_credentials( $url, '', false, false, null );
// Check for file system permissions.
if ( false === $creds ) {
wp_send_json_error( $error );
}
$fs = aioseo()->core->fs->noConflict();
$fs->init( $creds );
if ( ! $fs->isWpfsValid() ) {
wp_send_json_error( $error );
}
// Do not allow WordPress to search/download translations, as this will break JS output.
remove_action( 'upgrader_process_complete', [ 'Language_Pack_Upgrader', 'async_upgrade' ], 20 );
// Create the plugin upgrader with our custom skin.
$installer = new Utils\PluginUpgraderSilentAjax( new Utils\PluginUpgraderSkin() );
// Error check.
if ( ! method_exists( $installer, 'install' ) ) {
wp_send_json_error( $error );
}
$installer->install( $downloadUrl );
// Flush the cache and return the newly installed plugin basename.
wp_cache_flush();
$pluginBasename = $installer->plugin_info();
if ( ! $pluginBasename ) {
wp_send_json_error( $error );
}
// Activate the plugin silently.
$activated = activate_plugin( $pluginBasename, '', $network, true );
if ( is_wp_error( $activated ) ) {
wp_send_json_error( esc_html__( 'The Pro version installed correctly, but it needs to be activated from the Plugins page inside your WordPress admin.', 'all-in-one-seo-pack' ) );
}
aioseo()->internalOptions->internal->connect->reset();
// Because the regular activation hooks won't run, we need to add capabilities for the installing user so that he doesn't run into an error on the first request.
aioseo()->activate->addCapabilitiesOnUpgrade();
wp_send_json_success( $success );
}
}