Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Admin/API
File: MarketingCampaigns.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* REST API MarketingCampaigns Controller
[2] Fix | Delete
*
[3] Fix | Delete
* Handles requests to /marketing/campaigns.
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
namespace Automattic\WooCommerce\Admin\API;
[7] Fix | Delete
[8] Fix | Delete
use Automattic\WooCommerce\Admin\Marketing\MarketingCampaign;
[9] Fix | Delete
use Automattic\WooCommerce\Admin\Marketing\MarketingChannels as MarketingChannelsService;
[10] Fix | Delete
use Automattic\WooCommerce\Admin\Marketing\Price;
[11] Fix | Delete
use WC_REST_Controller;
[12] Fix | Delete
use WP_Error;
[13] Fix | Delete
use WP_REST_Request;
[14] Fix | Delete
use WP_REST_Response;
[15] Fix | Delete
[16] Fix | Delete
defined( 'ABSPATH' ) || exit;
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* MarketingCampaigns Controller.
[20] Fix | Delete
*
[21] Fix | Delete
* @internal
[22] Fix | Delete
* @extends WC_REST_Controller
[23] Fix | Delete
* @since x.x.x
[24] Fix | Delete
*/
[25] Fix | Delete
class MarketingCampaigns extends WC_REST_Controller {
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Endpoint namespace.
[29] Fix | Delete
*
[30] Fix | Delete
* @var string
[31] Fix | Delete
*/
[32] Fix | Delete
protected $namespace = 'wc-admin';
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* Route base.
[36] Fix | Delete
*
[37] Fix | Delete
* @var string
[38] Fix | Delete
*/
[39] Fix | Delete
protected $rest_base = 'marketing/campaigns';
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* Register routes.
[43] Fix | Delete
*/
[44] Fix | Delete
public function register_routes() {
[45] Fix | Delete
register_rest_route(
[46] Fix | Delete
$this->namespace,
[47] Fix | Delete
'/' . $this->rest_base,
[48] Fix | Delete
array(
[49] Fix | Delete
array(
[50] Fix | Delete
'methods' => \WP_REST_Server::READABLE,
[51] Fix | Delete
'callback' => array( $this, 'get_items' ),
[52] Fix | Delete
'permission_callback' => array( $this, 'get_items_permissions_check' ),
[53] Fix | Delete
'args' => $this->get_collection_params(),
[54] Fix | Delete
),
[55] Fix | Delete
'schema' => array( $this, 'get_public_item_schema' ),
[56] Fix | Delete
)
[57] Fix | Delete
);
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Check whether a given request has permission to view marketing campaigns.
[62] Fix | Delete
*
[63] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[64] Fix | Delete
*
[65] Fix | Delete
* @return WP_Error|boolean
[66] Fix | Delete
*/
[67] Fix | Delete
public function get_items_permissions_check( $request ) {
[68] Fix | Delete
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
[69] Fix | Delete
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
return true;
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Returns an aggregated array of marketing campaigns for all active marketing channels.
[78] Fix | Delete
*
[79] Fix | Delete
* @param WP_REST_Request $request Request data.
[80] Fix | Delete
*
[81] Fix | Delete
* @return WP_Error|WP_REST_Response
[82] Fix | Delete
*/
[83] Fix | Delete
public function get_items( $request ) {
[84] Fix | Delete
/**
[85] Fix | Delete
* MarketingChannels class.
[86] Fix | Delete
*
[87] Fix | Delete
* @var MarketingChannelsService $marketing_channels_service
[88] Fix | Delete
*/
[89] Fix | Delete
$marketing_channels_service = wc_get_container()->get( MarketingChannelsService::class );
[90] Fix | Delete
[91] Fix | Delete
// Aggregate the campaigns from all registered marketing channels.
[92] Fix | Delete
$responses = array();
[93] Fix | Delete
foreach ( $marketing_channels_service->get_registered_channels() as $channel ) {
[94] Fix | Delete
foreach ( $channel->get_campaigns() as $campaign ) {
[95] Fix | Delete
$response = $this->prepare_item_for_response( $campaign, $request );
[96] Fix | Delete
$responses[] = $this->prepare_response_for_collection( $response );
[97] Fix | Delete
}
[98] Fix | Delete
}
[99] Fix | Delete
[100] Fix | Delete
// Pagination.
[101] Fix | Delete
$page = $request['page'];
[102] Fix | Delete
$items_per_page = $request['per_page'];
[103] Fix | Delete
$offset = ( $page - 1 ) * $items_per_page;
[104] Fix | Delete
$paginated_results = array_slice( $responses, $offset, $items_per_page );
[105] Fix | Delete
[106] Fix | Delete
$response = rest_ensure_response( $paginated_results );
[107] Fix | Delete
[108] Fix | Delete
$total_campaigns = count( $responses );
[109] Fix | Delete
$max_pages = ceil( $total_campaigns / $items_per_page );
[110] Fix | Delete
$response->header( 'X-WP-Total', $total_campaigns );
[111] Fix | Delete
$response->header( 'X-WP-TotalPages', (int) $max_pages );
[112] Fix | Delete
[113] Fix | Delete
// Add previous and next page links to response header.
[114] Fix | Delete
$request_params = $request->get_query_params();
[115] Fix | Delete
$base = add_query_arg( urlencode_deep( $request_params ), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
[116] Fix | Delete
if ( $page > 1 ) {
[117] Fix | Delete
$prev_page = $page - 1;
[118] Fix | Delete
if ( $prev_page > $max_pages ) {
[119] Fix | Delete
$prev_page = $max_pages;
[120] Fix | Delete
}
[121] Fix | Delete
$prev_link = add_query_arg( 'page', $prev_page, $base );
[122] Fix | Delete
$response->link_header( 'prev', $prev_link );
[123] Fix | Delete
}
[124] Fix | Delete
if ( $max_pages > $page ) {
[125] Fix | Delete
$next_page = $page + 1;
[126] Fix | Delete
$next_link = add_query_arg( 'page', $next_page, $base );
[127] Fix | Delete
$response->link_header( 'next', $next_link );
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
return $response;
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
/**
[134] Fix | Delete
* Get formatted price based on Price type.
[135] Fix | Delete
*
[136] Fix | Delete
* This uses plugins/woocommerce/i18n/currency-info.php and plugins/woocommerce/i18n/locale-info.php to get option object based on $price->currency.
[137] Fix | Delete
*
[138] Fix | Delete
* Example:
[139] Fix | Delete
*
[140] Fix | Delete
* - When $price->currency is 'USD' and $price->value is '1000', it should return '$1000.00'.
[141] Fix | Delete
* - When $price->currency is 'JPY' and $price->value is '1000', it should return 'Â¥1,000'.
[142] Fix | Delete
* - When $price->currency is 'AED' and $price->value is '1000', it should return '5.000,00 د.إ'.
[143] Fix | Delete
*
[144] Fix | Delete
* @param Price $price Price object.
[145] Fix | Delete
* @return String formatted price.
[146] Fix | Delete
*/
[147] Fix | Delete
private function get_formatted_price( $price ) {
[148] Fix | Delete
// Get $num_decimals to be passed to wc_price.
[149] Fix | Delete
$locale_info_all = include WC()->plugin_path() . '/i18n/locale-info.php';
[150] Fix | Delete
$locale_index = array_search( $price->get_currency(), array_column( $locale_info_all, 'currency_code' ), true );
[151] Fix | Delete
$locale = array_values( $locale_info_all )[ $locale_index ];
[152] Fix | Delete
$num_decimals = $locale['num_decimals'];
[153] Fix | Delete
[154] Fix | Delete
// Get $currency_info based on user locale or default locale.
[155] Fix | Delete
$currency_locales = $locale['locales'];
[156] Fix | Delete
$user_locale = get_user_locale();
[157] Fix | Delete
$currency_info = $currency_locales[ $user_locale ] ?? $currency_locales['default'];
[158] Fix | Delete
[159] Fix | Delete
// Get $price_format to be passed to wc_price.
[160] Fix | Delete
$currency_pos = $currency_info['currency_pos'];
[161] Fix | Delete
$currency_formats = array(
[162] Fix | Delete
'left' => '%1$s%2$s',
[163] Fix | Delete
'right' => '%2$s%1$s',
[164] Fix | Delete
'left_space' => '%1$s&nbsp;%2$s',
[165] Fix | Delete
'right_space' => '%2$s&nbsp;%1$s',
[166] Fix | Delete
);
[167] Fix | Delete
$price_format = $currency_formats[ $currency_pos ] ?? $currency_formats['left'];
[168] Fix | Delete
[169] Fix | Delete
$price_value = wc_format_decimal( $price->get_value() );
[170] Fix | Delete
$price_formatted = wc_price(
[171] Fix | Delete
$price_value,
[172] Fix | Delete
array(
[173] Fix | Delete
'currency' => $price->get_currency(),
[174] Fix | Delete
'decimal_separator' => $currency_info['decimal_sep'],
[175] Fix | Delete
'thousand_separator' => $currency_info['thousand_sep'],
[176] Fix | Delete
'decimals' => $num_decimals,
[177] Fix | Delete
'price_format' => $price_format,
[178] Fix | Delete
)
[179] Fix | Delete
);
[180] Fix | Delete
[181] Fix | Delete
return html_entity_decode( wp_strip_all_tags( $price_formatted ) );
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
/**
[185] Fix | Delete
* Prepares the item for the REST response.
[186] Fix | Delete
*
[187] Fix | Delete
* @param MarketingCampaign $item WordPress representation of the item.
[188] Fix | Delete
* @param WP_REST_Request $request Request object.
[189] Fix | Delete
*
[190] Fix | Delete
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
[191] Fix | Delete
*/
[192] Fix | Delete
public function prepare_item_for_response( $item, $request ) {
[193] Fix | Delete
$data = array(
[194] Fix | Delete
'id' => $item->get_id(),
[195] Fix | Delete
'channel' => $item->get_type()->get_channel()->get_slug(),
[196] Fix | Delete
'title' => $item->get_title(),
[197] Fix | Delete
'manage_url' => $item->get_manage_url(),
[198] Fix | Delete
);
[199] Fix | Delete
[200] Fix | Delete
if ( $item->get_cost() instanceof Price ) {
[201] Fix | Delete
$data['cost'] = array(
[202] Fix | Delete
'value' => wc_format_decimal( $item->get_cost()->get_value() ),
[203] Fix | Delete
'currency' => $item->get_cost()->get_currency(),
[204] Fix | Delete
'formatted' => $this->get_formatted_price( $item->get_cost() ),
[205] Fix | Delete
);
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
if ( $item->get_sales() instanceof Price ) {
[209] Fix | Delete
$data['sales'] = array(
[210] Fix | Delete
'value' => wc_format_decimal( $item->get_sales()->get_value() ),
[211] Fix | Delete
'currency' => $item->get_sales()->get_currency(),
[212] Fix | Delete
'formatted' => $this->get_formatted_price( $item->get_sales() ),
[213] Fix | Delete
);
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
$context = $request['context'] ?? 'view';
[217] Fix | Delete
$data = $this->add_additional_fields_to_object( $data, $request );
[218] Fix | Delete
$data = $this->filter_response_by_context( $data, $context );
[219] Fix | Delete
[220] Fix | Delete
return rest_ensure_response( $data );
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
/**
[224] Fix | Delete
* Retrieves the item's schema, conforming to JSON Schema.
[225] Fix | Delete
*
[226] Fix | Delete
* @return array Item schema data.
[227] Fix | Delete
*/
[228] Fix | Delete
public function get_item_schema() {
[229] Fix | Delete
$schema = array(
[230] Fix | Delete
'$schema' => 'http://json-schema.org/draft-04/schema#',
[231] Fix | Delete
'title' => 'marketing_campaign',
[232] Fix | Delete
'type' => 'object',
[233] Fix | Delete
'properties' => array(
[234] Fix | Delete
'id' => array(
[235] Fix | Delete
'description' => __( 'The unique identifier for the marketing campaign.', 'woocommerce' ),
[236] Fix | Delete
'type' => 'string',
[237] Fix | Delete
'context' => array( 'view' ),
[238] Fix | Delete
'readonly' => true,
[239] Fix | Delete
),
[240] Fix | Delete
'channel' => array(
[241] Fix | Delete
'description' => __( 'The unique identifier for the marketing channel that this campaign belongs to.', 'woocommerce' ),
[242] Fix | Delete
'type' => 'string',
[243] Fix | Delete
'context' => array( 'view' ),
[244] Fix | Delete
'readonly' => true,
[245] Fix | Delete
),
[246] Fix | Delete
'title' => array(
[247] Fix | Delete
'description' => __( 'Title of the marketing campaign.', 'woocommerce' ),
[248] Fix | Delete
'type' => 'string',
[249] Fix | Delete
'context' => array( 'view' ),
[250] Fix | Delete
'readonly' => true,
[251] Fix | Delete
),
[252] Fix | Delete
'manage_url' => array(
[253] Fix | Delete
'description' => __( 'URL to the campaign management page.', 'woocommerce' ),
[254] Fix | Delete
'type' => 'string',
[255] Fix | Delete
'context' => array( 'view' ),
[256] Fix | Delete
'readonly' => true,
[257] Fix | Delete
),
[258] Fix | Delete
'cost' => array(
[259] Fix | Delete
'description' => __( 'Cost of the marketing campaign.', 'woocommerce' ),
[260] Fix | Delete
'context' => array( 'view' ),
[261] Fix | Delete
'readonly' => true,
[262] Fix | Delete
'type' => 'object',
[263] Fix | Delete
'properties' => array(
[264] Fix | Delete
'value' => array(
[265] Fix | Delete
'type' => 'string',
[266] Fix | Delete
'context' => array( 'view' ),
[267] Fix | Delete
'readonly' => true,
[268] Fix | Delete
),
[269] Fix | Delete
'currency' => array(
[270] Fix | Delete
'type' => 'string',
[271] Fix | Delete
'context' => array( 'view' ),
[272] Fix | Delete
'readonly' => true,
[273] Fix | Delete
),
[274] Fix | Delete
),
[275] Fix | Delete
),
[276] Fix | Delete
'sales' => array(
[277] Fix | Delete
'description' => __( 'Sales of the marketing campaign.', 'woocommerce' ),
[278] Fix | Delete
'context' => array( 'view' ),
[279] Fix | Delete
'readonly' => true,
[280] Fix | Delete
'type' => 'object',
[281] Fix | Delete
'properties' => array(
[282] Fix | Delete
'value' => array(
[283] Fix | Delete
'type' => 'string',
[284] Fix | Delete
'context' => array( 'view' ),
[285] Fix | Delete
'readonly' => true,
[286] Fix | Delete
),
[287] Fix | Delete
'currency' => array(
[288] Fix | Delete
'type' => 'string',
[289] Fix | Delete
'context' => array( 'view' ),
[290] Fix | Delete
'readonly' => true,
[291] Fix | Delete
),
[292] Fix | Delete
),
[293] Fix | Delete
),
[294] Fix | Delete
),
[295] Fix | Delete
);
[296] Fix | Delete
[297] Fix | Delete
return $this->add_additional_fields_schema( $schema );
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
/**
[301] Fix | Delete
* Retrieves the query params for the collections.
[302] Fix | Delete
*
[303] Fix | Delete
* @return array Query parameters for the collection.
[304] Fix | Delete
*/
[305] Fix | Delete
public function get_collection_params() {
[306] Fix | Delete
$params = parent::get_collection_params();
[307] Fix | Delete
unset( $params['search'] );
[308] Fix | Delete
[309] Fix | Delete
return $params;
[310] Fix | Delete
}
[311] Fix | Delete
}
[312] Fix | Delete
[313] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function