* REST API Onboarding Profile Controller
* Handles requests to /onboarding/profile
declare( strict_types=1 );
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.
* @extends WC_REST_Data_Controller
class OnboardingProfile extends \WC_REST_Data_Controller {
protected $namespace = 'wc-admin';
protected $rest_base = 'onboarding/profile';
public function register_routes() {
'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' ),
'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.
'/' . $this->rest_base . '/experimental_get_email_prefill',
'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' ),
'/' . $this->rest_base . '/progress',
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_profile_progress' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'/' . $this->rest_base . '/progress/core-profiler/complete',
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'core_profiler_step_complete' ),
'permission_callback' => array( $this, 'update_items_permissions_check' ),
'description' => __( 'The Core Profiler step to mark as complete.', 'woocommerce' ),
'/' . $this->rest_base . '/update-store-currency-and-measurement-units',
'callback' => array( $this, 'update_store_currency_and_measurement_units' ),
'permission_callback' => array( $this, 'update_items_permissions_check' ),
'description' => __( 'Country code.', 'woocommerce' ),
'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() ) );
* 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 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();
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 );
* @param array $industries List of industries.
protected function filter_industries( $industries ) {
* Filter the list of industries.
* @param array $industries List of industries.
'woocommerce_admin_onboarding_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 );
* Fires when onboarding profile data is updated via the REST API.
* @param array $onboarding_data Previous onboarding data.
* @param array $query_args New data being set.
do_action( 'woocommerce_onboarding_profile_data_updated', $onboarding_data, $query_args );
'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 ) {
// 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 );
* Mark a core profiler step as complete.
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
public function core_profiler_step_complete( $request ) {
$json = $request->get_json_params();
$onboarding_progress = (array) get_option( Profile::PROGRESS_OPTION, array() );
if ( ! isset( $onboarding_progress['core_profiler_completed_steps'] ) ) {
$onboarding_progress['core_profiler_completed_steps'] = array();
$onboarding_progress['core_profiler_completed_steps'][ $step ] = array(
'completed_at' => gmdate( 'Y-m-d\TH:i:s\Z' ),
update_option( Profile::PROGRESS_OPTION, $onboarding_progress );
* Fires when a core profiler step is completed.
* @param string $step The completed step name.
do_action( 'woocommerce_core_profiler_step_complete', $step );
'results' => $onboarding_progress,
$response = rest_ensure_response( $response_data );
* Get the onboarding profile progress.
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
public function get_profile_progress( $request ) {
$onboarding_progress = (array) get_option( Profile::PROGRESS_OPTION, array() );
return rest_ensure_response( $onboarding_progress );
* Update store's currency and measurement units.
* Requires 'country' code to be passed in the request.
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
public function update_store_currency_and_measurement_units( WP_REST_Request $request ) {
$country_code = $request->get_param( 'country_code' );
$locale_info = include WC()->plugin_path() . '/i18n/locale-info.php';
if ( empty( $country_code ) || ! isset( $locale_info[ $country_code ] ) ) {
'woocommerce_rest_invalid_country_code',
__( 'Invalid country code.', 'woocommerce' ),
$country_info = $locale_info[ $country_code ];
$currency_settings = array(
'woocommerce_currency' => $country_info['currency_code'],
'woocommerce_currency_pos' => $country_info['currency_pos'],
'woocommerce_price_thousand_sep' => $country_info['thousand_sep'],
'woocommerce_price_decimal_sep' => $country_info['decimal_sep'],
'woocommerce_price_num_decimals' => $country_info['num_decimals'],
'woocommerce_weight_unit' => $country_info['weight_unit'],
'woocommerce_dimension_unit' => $country_info['dimension_unit'],
foreach ( $currency_settings as $key => $value ) {
update_option( $key, $value );
return new WP_REST_Response( array(), 204 );
* @param array $params The params sent in the request.
protected function prepare_objects_query( $params ) {
$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
* @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 );
* 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.
public static function get_profile_properties() {
'description' => __( 'Whether or not the profile was completed.', 'woocommerce' ),
'context' => array( 'view' ),
'validate_callback' => 'rest_validate_request_arg',
'description' => __( 'Whether or not the profile was skipped.', 'woocommerce' ),
'context' => array( 'view' ),
'validate_callback' => 'rest_validate_request_arg',
'description' => __( 'Industry.', 'woocommerce' ),
'context' => array( 'view' ),
'validate_callback' => 'rest_validate_request_arg',
'business_extensions' => array(
'description' => __( 'Extra business extensions to install.', 'woocommerce' ),
'context' => array( 'view' ),
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'is_agree_marketing' => array(
'description' => __( 'Whether or not this store agreed to receiving marketing contents from WooCommerce.com.', 'woocommerce' ),
'context' => array( 'view' ),
'validate_callback' => 'rest_validate_request_arg',
'description' => __( 'Store email address.', 'woocommerce' ),
'context' => array( 'view' ),
'validate_callback' => array( __CLASS__, 'rest_validate_marketing_email' ),
'is_store_country_set' => array(
'description' => __( 'Whether or not this store country is set via onboarding profiler.', 'woocommerce' ),
'context' => array( 'view' ),
'validate_callback' => 'rest_validate_request_arg',
'is_plugins_page_skipped' => array(
'description' => __( 'Whether or not plugins step in core profiler was skipped.', 'woocommerce' ),
'context' => array( 'view' ),
'validate_callback' => 'rest_validate_request_arg',