WC_Helper_Subscriptions_API::activate
Activate a WooCommerce.com product. This activates the plugin/theme on the site.
Метод класса: WC_Helper_Subscriptions_API{}
Хуков нет.
Возвращает
null. Ничего (null).
Использование
$result = WC_Helper_Subscriptions_API::activate( $request );
- $request(WP_REST_Request) (обязательный)
- Request object.
Код WC_Helper_Subscriptions_API::activate() WC Helper Subscriptions API::activate WC 11.1.0
public static function activate( $request ) {
$product_key = $request->get_param( 'product_key' );
$subscription = WC_Helper::get_subscription( $product_key );
if ( ! is_array( $subscription ) ) {
wp_send_json_error(
array(
'message' => __( 'We couldn\'t find a subscription for this product.', 'woocommerce' ),
),
400
);
}
if ( true !== $subscription['local']['installed'] || ! isset( $subscription['local']['active'] ) ) {
wp_send_json_error(
array(
'message' => __( 'This product is not installed.', 'woocommerce' ),
),
400
);
}
if ( true === $subscription['local']['active'] ) {
wp_send_json_success(
array(
'message' => __( 'This product is already active.', 'woocommerce' ),
),
);
}
if ( ! in_array( $subscription['product_type'] ?? null, array( 'plugin', 'theme' ), true ) ) {
wp_send_json_error(
array(
'message' => __( 'This product type is not supported.', 'woocommerce' ),
),
400
);
}
if ( 'plugin' === $subscription['product_type'] ) {
// manage_woocommerce (checked by get_permission() above) doesn't imply activate_plugins.
if ( ! current_user_can( 'activate_plugins' ) ) {
wp_send_json_error(
array(
'message' => __( 'You do not have permission to activate plugins.', 'woocommerce' ),
),
403
);
}
$success = activate_plugin( $subscription['local']['path'] );
if ( is_wp_error( $success ) ) {
wp_send_json_error(
array(
'message' => __( 'There was an error activating this plugin.', 'woocommerce' ),
),
400
);
}
} elseif ( 'theme' === $subscription['product_type'] ) {
if ( ! current_user_can( 'switch_themes' ) ) {
wp_send_json_error(
array(
'message' => __( 'You do not have permission to switch themes.', 'woocommerce' ),
),
403
);
}
switch_theme( $subscription['local']['slug'] );
$theme = wp_get_theme();
if ( $subscription['local']['slug'] !== $theme->get_stylesheet() ) {
wp_send_json_error(
array(
'message' => __( 'There was an error activating this theme.', 'woocommerce' ),
),
400
);
}
}
wp_send_json_success(
array(
'message' => __( 'This product has been activated.', 'woocommerce' ),
),
);
}