Automattic\WooCommerce\Admin\API
OnboardingTasks{} │ WC 1.0
Onboarding Tasks Controller.
Hooks from the class
Usage
$OnboardingTasks = new OnboardingTasks(); // use class methods
Methods
- public create_pages_permission_check( $request )
- public static create_product_from_template( $request )
- public create_products_permission_check( $request )
- public static get_header_mappings( $file )
- public get_tasks_permission_check( $request )
- public hide_task_list_permission_check( $request )
- public static import_sample_products()
- public static import_sample_products_from_csv( $csv_file )
- public register_routes()
- public static sanitize_special_column_name_regex( $value )
- public snooze_task_permissions_check( $request )
- private static get_homepage_cover_block( $image )
OnboardingTasks{} OnboardingTasks{} code WC 10.6.2
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 millisecond 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 ) || ! is_array( $import['imported'] ) || 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( ProductStatus::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 = wc_get_page_permalink( '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 -->';
}