at path:
ROOT
/
wp-content
/
plugins
/
woocommerce
/
includes
/
wc-rest-functions.php
run:
R
W
Run
abstracts
DIR
2026-04-15 05:42:42
R
W
Run
admin
DIR
2026-04-15 05:42:43
R
W
Run
blocks
DIR
2026-04-15 05:42:43
R
W
Run
cli
DIR
2026-04-15 05:42:43
R
W
Run
customizer
DIR
2026-04-15 05:42:43
R
W
Run
data-stores
DIR
2026-04-15 05:42:43
R
W
Run
emails
DIR
2026-04-15 05:42:43
R
W
Run
export
DIR
2026-04-15 05:42:43
R
W
Run
gateways
DIR
2026-04-15 05:42:43
R
W
Run
import
DIR
2026-04-15 05:42:43
R
W
Run
integrations
DIR
2026-04-15 05:42:37
R
W
Run
interfaces
DIR
2026-04-15 05:42:43
R
W
Run
legacy
DIR
2026-04-15 05:42:43
R
W
Run
libraries
DIR
2026-04-15 05:42:43
R
W
Run
log-handlers
DIR
2026-04-15 05:42:43
R
W
Run
payment-tokens
DIR
2026-04-15 05:42:43
R
W
Run
product-usage
DIR
2026-04-15 05:42:43
R
W
Run
queue
DIR
2026-04-15 05:42:43
R
W
Run
react-admin
DIR
2026-04-15 05:42:43
R
W
Run
rest-api
DIR
2026-04-15 05:42:43
R
W
Run
shipping
DIR
2026-04-15 05:42:37
R
W
Run
shortcodes
DIR
2026-04-15 05:42:43
R
W
Run
theme-support
DIR
2026-04-15 05:42:43
R
W
Run
tracks
DIR
2026-04-15 05:42:43
R
W
Run
traits
DIR
2026-04-15 05:42:43
R
W
Run
walkers
DIR
2026-04-15 05:42:43
R
W
Run
wccom-site
DIR
2026-04-15 05:42:43
R
W
Run
widgets
DIR
2026-04-15 05:42:43
R
W
Run
class-wc-ajax.php
131.44 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-auth.php
12.69 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-autoloader.php
5.27 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-background-emailer.php
4.58 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-background-updater.php
3.45 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-brands-brand-settings-manager.php
1.78 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-brands-coupons.php
6.89 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-brands.php
34.8 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-breadcrumb.php
10.41 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-cache-helper.php
12.69 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-cart-fees.php
3.37 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-cart-session.php
25.37 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-cart-totals.php
28.48 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-cart.php
75.57 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-checkout.php
50.15 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-cli.php
3.34 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-comments.php
23.08 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-countries.php
50.67 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-coupon.php
43.87 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-customer-download-log.php
3.37 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-customer-download.php
10.34 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-customer.php
33.2 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-data-exception.php
1.29 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-data-store.php
6.59 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-datetime.php
2.26 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-deprecated-action-hooks.php
6.59 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-deprecated-filter-hooks.php
7.34 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-discounts.php
36.64 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-download-handler.php
28.37 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-emails.php
39.3 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-embed.php
4.24 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-form-handler.php
48.17 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-frontend-scripts.php
34.36 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-geo-ip.php
30.43 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-geolite-integration.php
1.99 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-geolocation.php
11.34 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-https.php
4.33 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-install.php
116.23 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-integrations.php
1.28 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-log-levels.php
3.9 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-logger.php
9.41 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-meta-data.php
2.21 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-order-factory.php
8.74 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-order-item-coupon.php
4.08 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-order-item-fee.php
9.99 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-order-item-meta.php
5.8 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-order-item-product.php
17.54 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-order-item-shipping.php
9.58 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-order-item-tax.php
6.49 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-order-item.php
21.39 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-order-query.php
2.55 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-order-refund.php
5.99 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-order.php
77.56 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-payment-gateways.php
14.26 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-payment-tokens.php
6.24 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-post-data.php
38.69 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-post-types.php
32.89 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-privacy-background-process.php
1.79 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-privacy-erasers.php
13.61 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-privacy-exporters.php
14.69 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-privacy.php
17.22 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-product-attribute.php
7.87 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-product-download.php
13.18 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-product-external.php
4.98 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-product-factory.php
4.59 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-product-grouped.php
6.81 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-product-query.php
2.27 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-product-simple.php
2.7 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-product-variable.php
23.76 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-product-variation.php
20.18 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-query.php
33.5 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-rate-limiter.php
4 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-regenerate-images-request.php
7.74 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-regenerate-images.php
15.44 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-register-wp-admin-settings.php
5.05 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-rest-authentication.php
21.55 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-rest-exception.php
276 By
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-session-handler.php
24.57 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-shipping-rate.php
9.34 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-shipping-zone.php
13.08 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-shipping-zones.php
5 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-shipping.php
13.06 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-shortcodes.php
18.82 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-structured-data.php
24.42 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-tax.php
39.74 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-template-loader.php
20.42 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-tracker.php
51.5 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-validation.php
5.79 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-wc-webhook.php
30.08 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
class-woocommerce.php
62.32 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-account-functions.php
14.15 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-attribute-functions.php
21.85 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-brands-functions.php
4.17 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-cart-functions.php
20.81 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-conditional-functions.php
15.53 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-core-functions.php
78.71 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-coupon-functions.php
5.56 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-deprecated-functions.php
39.78 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-formatting-functions.php
49.9 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-interactivity-api-functions.php
2.17 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-notice-functions.php
8.49 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-order-functions.php
44.01 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-order-item-functions.php
5.03 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-order-step-logger-functions.php
5.97 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-page-functions.php
9.43 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-product-functions.php
67.38 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-rest-functions.php
13.93 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-stock-functions.php
17.43 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-template-functions.php
142.07 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-template-hooks.php
12.84 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-term-functions.php
24.57 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-update-functions.php
102.38 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-user-functions.php
35.35 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-webhook-functions.php
5.77 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
wc-widget-functions.php
2.01 KB
2026-04-15 05:42:43
R
W
Run
Delete
Rename
error_log
up
📄
wc-rest-functions.php
Save
<?php /** * WooCommerce REST Functions * * Functions for REST specific things. * * @package WooCommerce\Functions * @version 2.6.0 */ use Automattic\WooCommerce\Internal\Utilities\Users; defined( 'ABSPATH' ) || exit; /** * Parses and formats a date for ISO8601/RFC3339. * * Required WP 4.4 or later. * See https://developer.wordpress.org/reference/functions/mysql_to_rfc3339/ * * @since 2.6.0 * @param string|null|WC_DateTime $date Date. * @param bool $utc Send false to get local/offset time. * @return string|null ISO8601/RFC3339 formatted datetime. */ function wc_rest_prepare_date_response( $date, $utc = true ) { if ( is_numeric( $date ) ) { $date = new WC_DateTime( "@$date", new DateTimeZone( 'UTC' ) ); $date->setTimezone( new DateTimeZone( wc_timezone_string() ) ); } elseif ( is_string( $date ) ) { $date = new WC_DateTime( $date, new DateTimeZone( 'UTC' ) ); $date->setTimezone( new DateTimeZone( wc_timezone_string() ) ); } if ( ! is_a( $date, 'WC_DateTime' ) ) { return null; } // Get timestamp before changing timezone to UTC. return gmdate( 'Y-m-d\TH:i:s', $utc ? $date->getTimestamp() : $date->getOffsetTimestamp() ); } /** * Returns image mime types users are allowed to upload via the API. * * @since 2.6.4 * @return array */ function wc_rest_allowed_image_mime_types() { return apply_filters( 'woocommerce_rest_allowed_image_mime_types', array( 'jpg|jpeg|jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'bmp' => 'image/bmp', 'tiff|tif' => 'image/tiff', 'ico' => 'image/x-icon', 'webp' => 'image/webp', ) ); } /** * Upload image from URL. * * @since 2.6.0 * @param string $image_url Image URL. * @return array|WP_Error Attachment data or error message. */ function wc_rest_upload_image_from_url( $image_url ) { $parsed_url = wp_parse_url( $image_url ); // Check parsed URL. if ( ! $parsed_url || ! is_array( $parsed_url ) ) { /* translators: %s: image URL */ return new WP_Error( 'woocommerce_rest_invalid_image_url', sprintf( __( 'Invalid URL %s.', 'woocommerce' ), $image_url ), array( 'status' => 400 ) ); } // Ensure url is valid. $image_url = esc_url_raw( $image_url ); // download_url function is part of wp-admin. if ( ! function_exists( 'download_url' ) ) { include_once ABSPATH . 'wp-admin/includes/file.php'; } $file_array = array(); $file_array['name'] = basename( current( explode( '?', $image_url ) ) ); // Download file to temp location. $file_array['tmp_name'] = download_url( $image_url ); // If error storing temporarily, return the error. if ( is_wp_error( $file_array['tmp_name'] ) ) { return new WP_Error( 'woocommerce_rest_invalid_remote_image_url', /* translators: %s: image URL */ sprintf( __( 'Error getting remote image %s.', 'woocommerce' ), $image_url ) . ' ' /* translators: %s: error message */ . sprintf( __( 'Error: %s', 'woocommerce' ), $file_array['tmp_name']->get_error_message() ), array( 'status' => 400 ) ); } // Do the validation and storage stuff. $file = wp_handle_sideload( $file_array, array( 'test_form' => false, 'mimes' => wc_rest_allowed_image_mime_types(), ), current_time( 'Y/m' ) ); if ( isset( $file['error'] ) ) { @unlink( $file_array['tmp_name'] ); // @codingStandardsIgnoreLine. /* translators: %s: error message */ return new WP_Error( 'woocommerce_rest_invalid_image', sprintf( __( 'Invalid image: %s', 'woocommerce' ), $file['error'] ), array( 'status' => 400 ) ); } do_action( 'woocommerce_rest_api_uploaded_image_from_url', $file, $image_url ); return $file; } /** * Set uploaded image as attachment. * * @since 2.6.0 * @param array $upload Upload information from wp_upload_bits. * @param int $id Post ID. Default to 0. * @return int Attachment ID */ function wc_rest_set_uploaded_image_as_attachment( $upload, $id = 0 ) { $info = wp_check_filetype( $upload['file'] ); $title = ''; $content = ''; if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { include_once ABSPATH . 'wp-admin/includes/image.php'; } $image_meta = @wp_read_image_metadata( $upload['file'] ); if ( $image_meta ) { if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) { $title = wc_clean( $image_meta['title'] ); } if ( trim( $image_meta['caption'] ) ) { $content = wc_clean( $image_meta['caption'] ); } } $attachment = array( 'post_mime_type' => $info['type'], 'guid' => $upload['url'], 'post_parent' => $id, 'post_title' => $title ? $title : basename( $upload['file'] ), 'post_content' => $content, ); $attachment_id = wp_insert_attachment( $attachment, $upload['file'], $id ); if ( ! is_wp_error( $attachment_id ) ) { @wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $upload['file'] ) ); } return $attachment_id; } /** * Validate reports request arguments. * * @since 2.6.0 * @param mixed $value Value to validate. * @param WP_REST_Request $request Request instance. * @param string $param Param to validate. * @return WP_Error|boolean */ function wc_rest_validate_reports_request_arg( $value, $request, $param ) { $attributes = $request->get_attributes(); if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { return true; } $args = $attributes['args'][ $param ]; if ( 'string' === $args['type'] && ! is_string( $value ) ) { /* translators: 1: param 2: type */ return new WP_Error( 'woocommerce_rest_invalid_param', sprintf( __( '%1$s is not of type %2$s', 'woocommerce' ), $param, 'string' ) ); } if ( 'date' === $args['format'] ) { $regex = '#^\d{4}-\d{2}-\d{2}$#'; if ( ! preg_match( $regex, $value, $matches ) ) { return new WP_Error( 'woocommerce_rest_invalid_date', __( 'The date you provided is invalid.', 'woocommerce' ) ); } } return true; } /** * Encodes a value according to RFC 3986. * Supports multidimensional arrays. * * @since 2.6.0 * @param string|array $value The value to encode. * @return string|array Encoded values. */ function wc_rest_urlencode_rfc3986( $value ) { if ( is_array( $value ) ) { return array_map( 'wc_rest_urlencode_rfc3986', $value ); } return str_replace( array( '+', '%7E' ), array( ' ', '~' ), rawurlencode( $value ) ); } /** * Check permissions of posts on REST API. * * @since 2.6.0 * @param string $post_type Post type. * @param string $context Request context. * @param int $object_id Post ID. * @return bool */ function wc_rest_check_post_permissions( $post_type, $context = 'read', $object_id = 0 ) { $contexts = array( 'read' => 'read_private_posts', 'create' => 'publish_posts', 'edit' => 'edit_post', 'delete' => 'delete_post', 'batch' => 'edit_others_posts', ); if ( 'revision' === $post_type ) { $permission = false; } else { $cap = $contexts[ $context ]; $post_type_object = get_post_type_object( $post_type ); $permission = false; if ( $post_type_object instanceof WP_Post_Type ) { $permission = current_user_can( $post_type_object->cap->$cap, $object_id ); } } return apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, $object_id, $post_type ); } /** * Check permissions of users on REST API. * * @since 2.6.0 * @since 9.4.0 Became multisite aware. The function now considers whether the user belongs to the current site. * * @param string $context Request context. * @param int $object_id User ID. * @return bool */ function wc_rest_check_user_permissions( $context = 'read', $object_id = 0 ) { $contexts = array( 'read' => 'list_users', 'create' => 'create_customers', 'edit' => 'edit_users', 'delete' => 'delete_users', 'batch' => 'promote_users', ); // Check to allow shop_managers to manage only customers. if ( in_array( $context, array( 'edit', 'delete' ), true ) && wc_current_user_has_role( 'shop_manager' ) ) { $permission = false; $user_data = get_userdata( $object_id ); $shop_manager_editable_roles = apply_filters( 'woocommerce_shop_manager_editable_roles', array( 'customer' ) ); if ( isset( $user_data->roles ) ) { $can_manage_users = array_intersect( $user_data->roles, array_unique( $shop_manager_editable_roles ) ); // Check if Shop Manager can edit customer or with the is same shop manager. if ( 0 < count( $can_manage_users ) || intval( $object_id ) === intval( get_current_user_id() ) ) { $permission = current_user_can( $contexts[ $context ], $object_id ); } } } else { $permission = current_user_can( $contexts[ $context ], $object_id ); } // Possibly revoke $permission if the user is 'out of bounds' from a multisite-network perspective. if ( $permission && ! Users::get_user_in_current_site( $object_id ) ) { $permission = false; } /** * Provides an opportunity to override the permission check made before acting on an object in relation to * REST API requests. * * @since 2.6.0 * * @param bool $permission If we have permission to act on this object. * @param string $context Describes the operation being performed: 'read', 'edit', 'delete', etc. * @param int $object_id Object ID. This could be a user ID, order ID, post ID, etc. * @param string $object_type Type of object ('user', 'shop_order', etc) for which checks are being made. */ return apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, $object_id, 'user' ); } /** * Check permissions of product terms on REST API. * * @since 2.6.0 * @param string $taxonomy Taxonomy. * @param string $context Request context. * @param int $object_id Post ID. * @return bool */ function wc_rest_check_product_term_permissions( $taxonomy, $context = 'read', $object_id = 0 ) { $contexts = array( 'read' => 'manage_terms', 'create' => 'edit_terms', 'edit' => 'edit_terms', 'delete' => 'delete_terms', 'batch' => 'edit_terms', ); $cap = $contexts[ $context ]; $taxonomy_object = get_taxonomy( $taxonomy ); $permission = current_user_can( $taxonomy_object->cap->$cap, $object_id ); return apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, $object_id, $taxonomy ); } /** * Check manager permissions on REST API. * * @since 2.6.0 * @param string $object Object. * @param string $context Request context. * @return bool */ function wc_rest_check_manager_permissions( $object, $context = 'read' ) { $objects = array( 'reports' => 'view_woocommerce_reports', 'settings' => 'manage_woocommerce', 'system_status' => 'manage_woocommerce', 'attributes' => 'manage_product_terms', 'shipping_methods' => 'manage_woocommerce', 'payment_gateways' => 'manage_woocommerce', 'webhooks' => 'manage_woocommerce', ); $permission = current_user_can( $objects[ $object ] ); return apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, 0, $object ); } /** * Check product reviews permissions on REST API. * * @since 3.5.0 * @param string $context Request context. * @param string $object_id Object ID. * @return bool */ function wc_rest_check_product_reviews_permissions( $context = 'read', $object_id = 0 ) { $permission = false; $contexts = array( 'read' => 'moderate_comments', 'create' => 'edit_products', 'edit' => 'edit_products', 'delete' => 'edit_products', 'batch' => 'edit_products', ); if ( $object_id > 0 ) { $object = get_comment( $object_id ); if ( ! is_a( $object, 'WP_Comment' ) || get_comment_type( $object ) !== 'review' ) { return false; } } if ( isset( $contexts[ $context ] ) ) { $permission = current_user_can( $contexts[ $context ], $object_id ); } return apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, $object_id, 'product_review' ); } /** * Returns true if the current REST request is from the product editor. * * @since 8.9.0 * @return bool */ function wc_rest_is_from_product_editor() { return isset( $_SERVER['HTTP_X_WC_FROM_PRODUCT_EDITOR'] ) && '1' === $_SERVER['HTTP_X_WC_FROM_PRODUCT_EDITOR']; } /** * Check if a REST namespace should be loaded. Useful to maintain site performance even when lots of REST namespaces are registered. * * @since 9.2.0. * * @param string $ns The namespace to check. * @param string $rest_route (Optional) The REST route being checked. * * @return bool True if the namespace should be loaded, false otherwise. */ function wc_rest_should_load_namespace( string $ns, string $rest_route = '' ): bool { if ( '' === $rest_route ) { $rest_route = $GLOBALS['wp']->query_vars['rest_route'] ?? ''; } if ( '' === $rest_route ) { return true; } $rest_route = trailingslashit( ltrim( $rest_route, '/' ) ); $ns = trailingslashit( $ns ); /** * Known namespaces that we know are safe to not load if the request is not for them. Namespaces not in this namespace should always be loaded, because we don't know if they won't be making another internal REST request to an unloaded namespace. */ $known_namespaces = array( 'wc/v1', 'wc/v2', 'wc/v3', 'wc/v4', 'wc-telemetry', 'wc-admin', 'wc-analytics', 'wc/store', 'wc/private', ); $known_namespace_request = false; foreach ( $known_namespaces as $known_namespace ) { if ( str_starts_with( $rest_route, $known_namespace ) ) { $known_namespace_request = true; break; } } if ( ! $known_namespace_request ) { return true; } /** * Filters whether a namespace should be loaded. * * @param bool $should_load True if the namespace should be loaded, false otherwise. * @param string $ns The namespace to check. * @param string $rest_route The REST route being checked. * @param array $known_namespaces Known namespaces that we know are safe to not load if the request is not for them. * * @since 9.4 */ return apply_filters( 'wc_rest_should_load_namespace', str_starts_with( $rest_route, $ns ), $ns, $rest_route, $known_namespaces ); }