Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Admin/API
File: Marketing.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* REST API Marketing Controller
[2] Fix | Delete
*
[3] Fix | Delete
* Handles requests to /marketing.
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
namespace Automattic\WooCommerce\Admin\API;
[7] Fix | Delete
[8] Fix | Delete
use Automattic\WooCommerce\Admin\PluginsHelper;
[9] Fix | Delete
use Automattic\WooCommerce\Internal\Admin\Marketing\MarketingSpecs;
[10] Fix | Delete
use Automattic\WooCommerce\Admin\Features\MarketingRecommendations\Init as MarketingRecommendationsInit;
[11] Fix | Delete
[12] Fix | Delete
defined( 'ABSPATH' ) || exit;
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* Marketing Controller.
[16] Fix | Delete
*
[17] Fix | Delete
* @internal
[18] Fix | Delete
* @extends WC_REST_Data_Controller
[19] Fix | Delete
*/
[20] Fix | Delete
class Marketing extends \WC_REST_Data_Controller {
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* Endpoint namespace.
[24] Fix | Delete
*
[25] Fix | Delete
* @var string
[26] Fix | Delete
*/
[27] Fix | Delete
protected $namespace = 'wc-admin';
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* Route base.
[31] Fix | Delete
*
[32] Fix | Delete
* @var string
[33] Fix | Delete
*/
[34] Fix | Delete
protected $rest_base = 'marketing';
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* Register routes.
[38] Fix | Delete
*/
[39] Fix | Delete
public function register_routes() {
[40] Fix | Delete
register_rest_route(
[41] Fix | Delete
$this->namespace,
[42] Fix | Delete
'/' . $this->rest_base . '/recommended',
[43] Fix | Delete
array(
[44] Fix | Delete
array(
[45] Fix | Delete
'methods' => \WP_REST_Server::READABLE,
[46] Fix | Delete
'callback' => array( $this, 'get_recommended_plugins' ),
[47] Fix | Delete
'permission_callback' => array( $this, 'get_recommended_plugins_permissions_check' ),
[48] Fix | Delete
'args' => array(
[49] Fix | Delete
'per_page' => $this->get_collection_params()['per_page'],
[50] Fix | Delete
'category' => array(
[51] Fix | Delete
'type' => 'string',
[52] Fix | Delete
'validate_callback' => 'rest_validate_request_arg',
[53] Fix | Delete
'sanitize_callback' => 'sanitize_title_with_dashes',
[54] Fix | Delete
),
[55] Fix | Delete
),
[56] Fix | Delete
),
[57] Fix | Delete
'schema' => array( $this, 'get_public_item_schema' ),
[58] Fix | Delete
)
[59] Fix | Delete
);
[60] Fix | Delete
[61] Fix | Delete
register_rest_route(
[62] Fix | Delete
$this->namespace,
[63] Fix | Delete
'/' . $this->rest_base . '/knowledge-base',
[64] Fix | Delete
array(
[65] Fix | Delete
array(
[66] Fix | Delete
'methods' => \WP_REST_Server::READABLE,
[67] Fix | Delete
'callback' => array( $this, 'get_knowledge_base_posts' ),
[68] Fix | Delete
'permission_callback' => array( $this, 'get_items_permissions_check' ),
[69] Fix | Delete
'args' => array(
[70] Fix | Delete
'category' => array(
[71] Fix | Delete
'type' => 'string',
[72] Fix | Delete
'validate_callback' => 'rest_validate_request_arg',
[73] Fix | Delete
'sanitize_callback' => 'sanitize_title_with_dashes',
[74] Fix | Delete
),
[75] Fix | Delete
),
[76] Fix | Delete
),
[77] Fix | Delete
'schema' => array( $this, 'get_public_item_schema' ),
[78] Fix | Delete
)
[79] Fix | Delete
);
[80] Fix | Delete
[81] Fix | Delete
register_rest_route(
[82] Fix | Delete
$this->namespace,
[83] Fix | Delete
'/' . $this->rest_base . '/misc-recommendations',
[84] Fix | Delete
array(
[85] Fix | Delete
array(
[86] Fix | Delete
'methods' => \WP_REST_Server::READABLE,
[87] Fix | Delete
'callback' => array( $this, 'get_misc_recommendations' ),
[88] Fix | Delete
'permission_callback' => array( $this, 'get_recommended_plugins_permissions_check' ),
[89] Fix | Delete
),
[90] Fix | Delete
'schema' => array( $this, 'get_public_item_schema' ),
[91] Fix | Delete
)
[92] Fix | Delete
);
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Check whether a given request has permission to install plugins.
[97] Fix | Delete
*
[98] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[99] Fix | Delete
* @return WP_Error|boolean
[100] Fix | Delete
*/
[101] Fix | Delete
public function get_recommended_plugins_permissions_check( $request ) {
[102] Fix | Delete
if ( ! current_user_can( 'install_plugins' ) ) {
[103] Fix | Delete
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage plugins.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
return true;
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* Return installed marketing extensions data.
[112] Fix | Delete
*
[113] Fix | Delete
* @param \WP_REST_Request $request Request data.
[114] Fix | Delete
*
[115] Fix | Delete
* @return \WP_Error|\WP_REST_Response
[116] Fix | Delete
*/
[117] Fix | Delete
public function get_recommended_plugins( $request ) {
[118] Fix | Delete
// Default to marketing category (if no category set).
[119] Fix | Delete
$category = ( ! empty( $request->get_param( 'category' ) ) ) ? $request->get_param( 'category' ) : 'marketing';
[120] Fix | Delete
$all_plugins = MarketingRecommendationsInit::get_recommended_plugins();
[121] Fix | Delete
$valid_plugins = [];
[122] Fix | Delete
$per_page = $request->get_param( 'per_page' );
[123] Fix | Delete
[124] Fix | Delete
foreach ( $all_plugins as $plugin ) {
[125] Fix | Delete
[126] Fix | Delete
// default to marketing if 'categories' is empty on the plugin object (support for legacy api while testing).
[127] Fix | Delete
$plugin_categories = ( ! empty( $plugin['categories'] ) ) ? $plugin['categories'] : [ 'marketing' ];
[128] Fix | Delete
[129] Fix | Delete
if ( ! PluginsHelper::is_plugin_installed( $plugin['plugin'] ) && in_array( $category, $plugin_categories, true ) ) {
[130] Fix | Delete
$valid_plugins[] = $plugin;
[131] Fix | Delete
}
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
return rest_ensure_response( array_slice( $valid_plugins, 0, $per_page ) );
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Return installed marketing extensions data.
[139] Fix | Delete
*
[140] Fix | Delete
* @param \WP_REST_Request $request Request data.
[141] Fix | Delete
*
[142] Fix | Delete
* @return \WP_Error|\WP_REST_Response
[143] Fix | Delete
*/
[144] Fix | Delete
public function get_knowledge_base_posts( $request ) {
[145] Fix | Delete
/**
[146] Fix | Delete
* MarketingSpecs class.
[147] Fix | Delete
*
[148] Fix | Delete
* @var MarketingSpecs $marketing_specs
[149] Fix | Delete
*/
[150] Fix | Delete
$marketing_specs = wc_get_container()->get( MarketingSpecs::class );
[151] Fix | Delete
[152] Fix | Delete
$category = $request->get_param( 'category' );
[153] Fix | Delete
return rest_ensure_response( $marketing_specs->get_knowledge_base_posts( $category ) );
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
/**
[157] Fix | Delete
* Return misc recommendations.
[158] Fix | Delete
*
[159] Fix | Delete
* @param \WP_REST_Request $request Request data.
[160] Fix | Delete
*
[161] Fix | Delete
* @since 9.5.0
[162] Fix | Delete
*
[163] Fix | Delete
* @return \WP_Error|\WP_REST_Response
[164] Fix | Delete
*/
[165] Fix | Delete
public function get_misc_recommendations( $request ) {
[166] Fix | Delete
$misc_recommendations = MarketingRecommendationsInit::get_misc_recommendations();
[167] Fix | Delete
[168] Fix | Delete
return rest_ensure_response( $misc_recommendations );
[169] Fix | Delete
}
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function