Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../includes/admin
File: class-wc-admin-addons.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Addons Page
[2] Fix | Delete
*
[3] Fix | Delete
* @package WooCommerce\Admin
[4] Fix | Delete
* @version 2.5.0
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
use Automattic\Jetpack\Constants;
[8] Fix | Delete
use Automattic\WooCommerce\Admin\RemoteInboxNotifications as PromotionRuleEngine;
[9] Fix | Delete
use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\RuleEvaluator;
[10] Fix | Delete
[11] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[12] Fix | Delete
exit;
[13] Fix | Delete
}
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* WC_Admin_Addons Class.
[17] Fix | Delete
*/
[18] Fix | Delete
class WC_Admin_Addons {
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Fetch featured products from WCCOM's the Featured 3.0 Endpoint and cache the data for a day.
[22] Fix | Delete
*
[23] Fix | Delete
* @return array|WP_Error
[24] Fix | Delete
*/
[25] Fix | Delete
public static function fetch_featured() {
[26] Fix | Delete
$transient_name = 'wc_addons_featured';
[27] Fix | Delete
// Important: WCCOM Extensions API v4.0 is used.
[28] Fix | Delete
$url = 'https://woocommerce.com/wp-json/wccom-extensions/4.0/featured';
[29] Fix | Delete
$locale = get_user_locale();
[30] Fix | Delete
$featured = self::get_locale_data_from_transient( $transient_name, $locale );
[31] Fix | Delete
[32] Fix | Delete
if ( false === $featured ) {
[33] Fix | Delete
$fetch_options = array(
[34] Fix | Delete
'auth' => true,
[35] Fix | Delete
'locale' => true,
[36] Fix | Delete
'country' => true,
[37] Fix | Delete
);
[38] Fix | Delete
$raw_featured = self::fetch( $url, $fetch_options );
[39] Fix | Delete
[40] Fix | Delete
$featured = self::process_api_response( $raw_featured, 'featured' );
[41] Fix | Delete
[42] Fix | Delete
if ( ! is_wp_error( $featured ) && $featured ) {
[43] Fix | Delete
self::set_locale_data_in_transient( $transient_name, $featured, $locale, DAY_IN_SECONDS );
[44] Fix | Delete
}
[45] Fix | Delete
}
[46] Fix | Delete
[47] Fix | Delete
return $featured;
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* Fetch markup and other info for the preview of a product.
[52] Fix | Delete
*
[53] Fix | Delete
* @param int $product_id The ID of the product to fetch preview for.
[54] Fix | Delete
* @return array|WP_Error Preview data or error object.
[55] Fix | Delete
*/
[56] Fix | Delete
public static function fetch_product_preview( int $product_id ) {
[57] Fix | Delete
$url = 'https://woocommerce.com/wp-json/wccom-extensions/1.0/product-previews?product_id=' . $product_id;
[58] Fix | Delete
[59] Fix | Delete
$fetch_options = array(
[60] Fix | Delete
'locale' => true,
[61] Fix | Delete
);
[62] Fix | Delete
[63] Fix | Delete
$raw_preview = self::fetch( $url, $fetch_options );
[64] Fix | Delete
[65] Fix | Delete
return self::process_api_response( $raw_preview, 'product preview', true );
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Check if the error is due to an SSL error
[70] Fix | Delete
*
[71] Fix | Delete
* @param string $error_message Error message.
[72] Fix | Delete
*
[73] Fix | Delete
* @return bool True if SSL error, false otherwise
[74] Fix | Delete
*/
[75] Fix | Delete
public static function is_ssl_error( $error_message ) {
[76] Fix | Delete
return false !== stripos( $error_message, 'cURL error 35' );
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
/**
[80] Fix | Delete
* Get sections for the addons screen
[81] Fix | Delete
*
[82] Fix | Delete
* @return array of objects
[83] Fix | Delete
*/
[84] Fix | Delete
public static function get_sections() {
[85] Fix | Delete
$locale = get_user_locale();
[86] Fix | Delete
$addon_sections = self::get_locale_data_from_transient( 'wc_addons_sections', $locale );
[87] Fix | Delete
if ( false === ( $addon_sections ) ) {
[88] Fix | Delete
$parameter_string = '?' . http_build_query( array( 'locale' => get_user_locale() ) );
[89] Fix | Delete
$raw_sections = wp_safe_remote_get(
[90] Fix | Delete
'https://woocommerce.com/wp-json/wccom-extensions/1.0/categories' . $parameter_string,
[91] Fix | Delete
array(
[92] Fix | Delete
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
[93] Fix | Delete
)
[94] Fix | Delete
);
[95] Fix | Delete
if ( ! is_wp_error( $raw_sections ) ) {
[96] Fix | Delete
$addon_sections = json_decode( wp_remote_retrieve_body( $raw_sections ) );
[97] Fix | Delete
if ( $addon_sections ) {
[98] Fix | Delete
self::set_locale_data_in_transient( 'wc_addons_sections', $addon_sections, $locale, WEEK_IN_SECONDS );
[99] Fix | Delete
}
[100] Fix | Delete
}
[101] Fix | Delete
}
[102] Fix | Delete
return apply_filters( 'woocommerce_addons_sections', $addon_sections );
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Get section for the addons screen.
[107] Fix | Delete
*
[108] Fix | Delete
* @param string $section_id Required section ID.
[109] Fix | Delete
*
[110] Fix | Delete
* @return object|bool
[111] Fix | Delete
*/
[112] Fix | Delete
public static function get_section( $section_id ) {
[113] Fix | Delete
$sections = self::get_sections();
[114] Fix | Delete
if ( isset( $sections[ $section_id ] ) ) {
[115] Fix | Delete
return $sections[ $section_id ];
[116] Fix | Delete
}
[117] Fix | Delete
return false;
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
/**
[121] Fix | Delete
* Returns in-app-purchase URL params.
[122] Fix | Delete
*/
[123] Fix | Delete
public static function get_in_app_purchase_url_params() {
[124] Fix | Delete
// Get url (from path onward) for the current page,
[125] Fix | Delete
// so WCCOM "back" link returns user to where they were.
[126] Fix | Delete
$back_admin_path = add_query_arg( array() );
[127] Fix | Delete
return array(
[128] Fix | Delete
'wccom-site' => site_url(),
[129] Fix | Delete
'wccom-back' => rawurlencode( $back_admin_path ),
[130] Fix | Delete
'wccom-woo-version' => Constants::get_constant( 'WC_VERSION' ),
[131] Fix | Delete
'wccom-connect-nonce' => wp_create_nonce( 'connect' ),
[132] Fix | Delete
);
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
/**
[136] Fix | Delete
* Add in-app-purchase URL params to link.
[137] Fix | Delete
*
[138] Fix | Delete
* Adds various url parameters to a url to support a streamlined
[139] Fix | Delete
* flow for obtaining and setting up WooCommerce extensons.
[140] Fix | Delete
*
[141] Fix | Delete
* @param string $url Destination URL.
[142] Fix | Delete
*/
[143] Fix | Delete
public static function add_in_app_purchase_url_params( $url ) {
[144] Fix | Delete
return add_query_arg(
[145] Fix | Delete
self::get_in_app_purchase_url_params(),
[146] Fix | Delete
$url
[147] Fix | Delete
);
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
/**
[151] Fix | Delete
* Outputs a button.
[152] Fix | Delete
*
[153] Fix | Delete
* @param string $url Destination URL.
[154] Fix | Delete
* @param string $text Button label text.
[155] Fix | Delete
* @param string $style Button style class.
[156] Fix | Delete
* @param string $plugin The plugin the button is promoting.
[157] Fix | Delete
*/
[158] Fix | Delete
public static function output_button( $url, $text, $style, $plugin = '' ) {
[159] Fix | Delete
$style = __( 'Free', 'woocommerce' ) === $text ? 'addons-button-outline-purple' : $style;
[160] Fix | Delete
$style = is_plugin_active( $plugin ) ? 'addons-button-installed' : $style;
[161] Fix | Delete
$text = is_plugin_active( $plugin ) ? __( 'Installed', 'woocommerce' ) : $text;
[162] Fix | Delete
$url = self::add_in_app_purchase_url_params( $url );
[163] Fix | Delete
?>
[164] Fix | Delete
<a
[165] Fix | Delete
class="addons-button <?php echo esc_attr( $style ); ?>"
[166] Fix | Delete
href="<?php echo esc_url( $url ); ?>">
[167] Fix | Delete
<?php echo esc_html( $text ); ?>
[168] Fix | Delete
</a>
[169] Fix | Delete
<?php
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* Process requests to legacy marketplace menu and redirect to correct in-app pages.
[174] Fix | Delete
*
[175] Fix | Delete
* @return void
[176] Fix | Delete
*/
[177] Fix | Delete
public static function handle_legacy_marketplace_redirects() {
[178] Fix | Delete
$section = isset( $_GET['section'] ) ? sanitize_text_field( wp_unslash( $_GET['section'] ) ) : '_featured';
[179] Fix | Delete
$search = isset( $_GET['search'] ) ? sanitize_text_field( wp_unslash( $_GET['search'] ) ) : '';
[180] Fix | Delete
[181] Fix | Delete
if ( 'helper' === $section ) {
[182] Fix | Delete
$url = admin_url( 'admin.php?page=wc-admin&tab=my-subscriptions&path=%2Fextensions' );
[183] Fix | Delete
[184] Fix | Delete
if ( isset( $_GET['connect'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
[185] Fix | Delete
$url .= '&connect';
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
wp_safe_redirect( $url );
[189] Fix | Delete
exit();
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
if ( 'search' === $section || ! empty( $search ) ) {
[193] Fix | Delete
wp_safe_redirect( admin_url( 'admin.php?page=wc-admin&term=' . $search . '&tab=search&path=%2Fextensions' ) );
[194] Fix | Delete
exit();
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
$sections = self::get_sections();
[198] Fix | Delete
$allowed_sections = array_map( fn( $section_object ) => $section_object->slug, $sections );
[199] Fix | Delete
// Validate if the category is supported.
[200] Fix | Delete
$section = in_array( $section, $allowed_sections, true ) ? $section : '_featured';
[201] Fix | Delete
[202] Fix | Delete
if ( '_featured' === $section ) {
[203] Fix | Delete
wp_safe_redirect( admin_url( 'admin.php?page=wc-admin&path=%2Fextensions' ) );
[204] Fix | Delete
exit();
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
wp_safe_redirect( admin_url( 'admin.php?page=wc-admin&tab=extensions&path=%2Fextensions&category=' . $section ) );
[208] Fix | Delete
exit();
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
/**
[212] Fix | Delete
* We're displaying page=wc-addons and page=wc-addons&section=helper as two separate pages.
[213] Fix | Delete
* When we're on those pages, add body classes to distinguishe them.
[214] Fix | Delete
*
[215] Fix | Delete
* @param string $admin_body_class Unfiltered body class.
[216] Fix | Delete
*
[217] Fix | Delete
* @return string Body class with added class for Marketplace or My Subscriptions page.
[218] Fix | Delete
*/
[219] Fix | Delete
public static function filter_admin_body_classes( string $admin_body_class = '' ): string {
[220] Fix | Delete
if ( isset( $_GET['section'] ) && 'helper' === $_GET['section'] ) {
[221] Fix | Delete
return " $admin_body_class woocommerce-page-wc-subscriptions ";
[222] Fix | Delete
}
[223] Fix | Delete
[224] Fix | Delete
return " $admin_body_class woocommerce-page-wc-marketplace ";
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
/**
[228] Fix | Delete
* Take an action object and return the URL based on properties of the action.
[229] Fix | Delete
*
[230] Fix | Delete
* @param object $action Action object.
[231] Fix | Delete
* @return string URL.
[232] Fix | Delete
*/
[233] Fix | Delete
public static function get_action_url( $action ): string {
[234] Fix | Delete
if ( ! isset( $action->url ) ) {
[235] Fix | Delete
return '';
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
if ( isset( $action->url_is_admin_query ) && $action->url_is_admin_query ) {
[239] Fix | Delete
return wc_admin_url( $action->url );
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
if ( isset( $action->url_is_admin_nonce_query ) && $action->url_is_admin_nonce_query ) {
[243] Fix | Delete
if ( empty( $action->nonce ) ) {
[244] Fix | Delete
return '';
[245] Fix | Delete
}
[246] Fix | Delete
return wp_nonce_url(
[247] Fix | Delete
admin_url( $action->url ),
[248] Fix | Delete
$action->nonce
[249] Fix | Delete
);
[250] Fix | Delete
}
[251] Fix | Delete
[252] Fix | Delete
return $action->url;
[253] Fix | Delete
}
[254] Fix | Delete
[255] Fix | Delete
/**
[256] Fix | Delete
* Retrieves the locale data from a transient.
[257] Fix | Delete
*
[258] Fix | Delete
* Transient value is an array of locale data in the following format:
[259] Fix | Delete
* array(
[260] Fix | Delete
* 'en_US' => ...,
[261] Fix | Delete
* 'fr_FR' => ...,
[262] Fix | Delete
* )
[263] Fix | Delete
*
[264] Fix | Delete
* If the transient does not exist, does not have a value, or has expired,
[265] Fix | Delete
* then the return value will be false.
[266] Fix | Delete
*
[267] Fix | Delete
* @param string $transient Transient name. Expected to not be SQL-escaped.
[268] Fix | Delete
* @param string $locale Locale to retrieve.
[269] Fix | Delete
* @return mixed Value of transient.
[270] Fix | Delete
*/
[271] Fix | Delete
private static function get_locale_data_from_transient( $transient, $locale ) {
[272] Fix | Delete
$transient_value = get_transient( $transient );
[273] Fix | Delete
$transient_value = is_array( $transient_value ) ? $transient_value : array();
[274] Fix | Delete
return $transient_value[ $locale ] ?? false;
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
/**
[278] Fix | Delete
* Sets the locale data in a transient.
[279] Fix | Delete
*
[280] Fix | Delete
* Transient value is an array of locale data in the following format:
[281] Fix | Delete
* array(
[282] Fix | Delete
* 'en_US' => ...,
[283] Fix | Delete
* 'fr_FR' => ...,
[284] Fix | Delete
* )
[285] Fix | Delete
*
[286] Fix | Delete
* @param string $transient Transient name. Expected to not be SQL-escaped.
[287] Fix | Delete
* Must be 172 characters or fewer in length.
[288] Fix | Delete
* @param mixed $value Transient value. Must be serializable if non-scalar.
[289] Fix | Delete
* Expected to not be SQL-escaped.
[290] Fix | Delete
* @param string $locale Locale to set.
[291] Fix | Delete
* @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
[292] Fix | Delete
* @return bool True if the value was set, false otherwise.
[293] Fix | Delete
*/
[294] Fix | Delete
private static function set_locale_data_in_transient( $transient, $value, $locale, $expiration = 0 ) {
[295] Fix | Delete
$transient_value = get_transient( $transient );
[296] Fix | Delete
$transient_value = is_array( $transient_value ) ? $transient_value : array();
[297] Fix | Delete
$transient_value[ $locale ] = $value;
[298] Fix | Delete
return set_transient( $transient, $transient_value, $expiration );
[299] Fix | Delete
}
[300] Fix | Delete
[301] Fix | Delete
/**
[302] Fix | Delete
* Process API response from WooCommerce.com endpoints.
[303] Fix | Delete
*
[304] Fix | Delete
* @param array|WP_Error $response The response from the API request.
[305] Fix | Delete
* @param string $context Context for error messages (e.g. 'featured', 'product-preview').
[306] Fix | Delete
* @param bool $associative Whether to decode the JSON as an associative array.
[307] Fix | Delete
*
[308] Fix | Delete
* @return array|WP_Error Processed API data or WP_Error on failure.
[309] Fix | Delete
*/
[310] Fix | Delete
private static function process_api_response( $response, $context = 'api', $associative = false ) {
[311] Fix | Delete
if ( is_wp_error( $response ) ) {
[312] Fix | Delete
/**
[313] Fix | Delete
* Hook fired when there is a connection error with WooCommerce.com.
[314] Fix | Delete
*
[315] Fix | Delete
* @since 6.1.0
[316] Fix | Delete
* @param string $error_message The error message.
[317] Fix | Delete
*/
[318] Fix | Delete
do_action( 'woocommerce_page_wc_addons_connection_error', $response->get_error_message() );
[319] Fix | Delete
[320] Fix | Delete
$message = self::is_ssl_error( $response->get_error_message() )
[321] Fix | Delete
? __(
[322] Fix | Delete
'We encountered an SSL error. Please ensure your site supports TLS version 1.2 or above.',
[323] Fix | Delete
'woocommerce'
[324] Fix | Delete
)
[325] Fix | Delete
: $response->get_error_message();
[326] Fix | Delete
[327] Fix | Delete
return new WP_Error( 'wc-addons-connection-error', $message );
[328] Fix | Delete
}
[329] Fix | Delete
[330] Fix | Delete
$response_code = (int) wp_remote_retrieve_response_code( $response );
[331] Fix | Delete
if ( 200 !== $response_code ) {
[332] Fix | Delete
/**
[333] Fix | Delete
* Hook fired when there is a connection error with WooCommerce.com.
[334] Fix | Delete
*
[335] Fix | Delete
* @since 6.1.0
[336] Fix | Delete
* @param int $response_code The HTTP response code.
[337] Fix | Delete
*/
[338] Fix | Delete
do_action( 'woocommerce_page_wc_addons_connection_error', $response_code );
[339] Fix | Delete
[340] Fix | Delete
$message = sprintf(
[341] Fix | Delete
/* translators: 1: Context (e.g. 'featured', 'product-preview') 2: HTTP error code */
[342] Fix | Delete
__( 'Our request to the %1$s API got error code %2$d.', 'woocommerce' ),
[343] Fix | Delete
$context,
[344] Fix | Delete
$response_code
[345] Fix | Delete
);
[346] Fix | Delete
[347] Fix | Delete
return new WP_Error( 'wc-addons-connection-error', $message );
[348] Fix | Delete
}
[349] Fix | Delete
[350] Fix | Delete
$data = json_decode( wp_remote_retrieve_body( $response ), $associative );
[351] Fix | Delete
if ( empty( $data ) || ! is_array( $data ) ) {
[352] Fix | Delete
/**
[353] Fix | Delete
* Hook fired when there is a connection error with WooCommerce.com.
[354] Fix | Delete
*
[355] Fix | Delete
* @since 6.1.0
[356] Fix | Delete
* @param string $error_message The error message.
[357] Fix | Delete
*/
[358] Fix | Delete
do_action( 'woocommerce_page_wc_addons_connection_error', 'Empty or malformed response' );
[359] Fix | Delete
[360] Fix | Delete
$message = sprintf(
[361] Fix | Delete
/* translators: %s: Context (e.g. 'featured', 'product-preview') */
[362] Fix | Delete
__( 'Our request to the %s API got a malformed response.', 'woocommerce' ),
[363] Fix | Delete
$context
[364] Fix | Delete
);
[365] Fix | Delete
[366] Fix | Delete
return new WP_Error( 'wc-addons-connection-error', $message );
[367] Fix | Delete
}
[368] Fix | Delete
[369] Fix | Delete
return $data;
[370] Fix | Delete
}
[371] Fix | Delete
[372] Fix | Delete
/**
[373] Fix | Delete
* Make wp_safe_remote_get request to WooCommerce.com endpoint.
[374] Fix | Delete
* Optionally pass user auth token, locale or country.
[375] Fix | Delete
*
[376] Fix | Delete
* @param string $url URL to request.
[377] Fix | Delete
* @param ?array $options Options for the request. For example, to pass auth token, locale and country,
[378] Fix | Delete
* pass array( 'auth' => true, 'locale' => true, 'country' => true, ).
[379] Fix | Delete
*
[380] Fix | Delete
* @return array|WP_Error
[381] Fix | Delete
*/
[382] Fix | Delete
public static function fetch( $url, $options = array() ) {
[383] Fix | Delete
$headers = array();
[384] Fix | Delete
[385] Fix | Delete
if ( isset( $options['auth'] ) && $options['auth'] ) {
[386] Fix | Delete
$auth = WC_Helper_Options::get( 'auth' );
[387] Fix | Delete
[388] Fix | Delete
if ( isset( $auth['access_token'] ) && ! empty( $auth['access_token'] ) ) {
[389] Fix | Delete
$headers['Authorization'] = 'Bearer ' . $auth['access_token'];
[390] Fix | Delete
}
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
$parameters = array();
[394] Fix | Delete
[395] Fix | Delete
if ( isset( $options['locale'] ) && $options['locale'] ) {
[396] Fix | Delete
$parameters['locale'] = get_user_locale();
[397] Fix | Delete
}
[398] Fix | Delete
[399] Fix | Delete
if ( isset( $options['country'] ) && $options['country'] ) {
[400] Fix | Delete
$country = WC()->countries->get_base_country();
[401] Fix | Delete
if ( ! empty( $country ) ) {
[402] Fix | Delete
$parameters['country'] = $country;
[403] Fix | Delete
}
[404] Fix | Delete
}
[405] Fix | Delete
[406] Fix | Delete
// Check if URL already has query parameters.
[407] Fix | Delete
$connector = strpos( $url, '?' ) !== false ? '&' : '?';
[408] Fix | Delete
$query_string = ! empty( $parameters ) ? $connector . http_build_query( $parameters ) : '';
[409] Fix | Delete
[410] Fix | Delete
return wp_safe_remote_get(
[411] Fix | Delete
$url . $query_string,
[412] Fix | Delete
array(
[413] Fix | Delete
'headers' => $headers,
[414] Fix | Delete
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
[415] Fix | Delete
)
[416] Fix | Delete
);
[417] Fix | Delete
}
[418] Fix | Delete
}
[419] Fix | Delete
[420] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function