Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Admin
File: WCAdminHelper.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WCAdminHelper
[2] Fix | Delete
*
[3] Fix | Delete
* Helper class for generic WCAdmin functions.
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
namespace Automattic\WooCommerce\Admin;
[7] Fix | Delete
[8] Fix | Delete
defined( 'ABSPATH' ) || exit;
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Class WCAdminHelper
[12] Fix | Delete
*/
[13] Fix | Delete
class WCAdminHelper {
[14] Fix | Delete
/**
[15] Fix | Delete
* WC Admin timestamp option name.
[16] Fix | Delete
*/
[17] Fix | Delete
const WC_ADMIN_TIMESTAMP_OPTION = 'woocommerce_admin_install_timestamp';
[18] Fix | Delete
[19] Fix | Delete
const WC_ADMIN_STORE_AGE_RANGES = array(
[20] Fix | Delete
'week-1' => array(
[21] Fix | Delete
'start' => 0,
[22] Fix | Delete
'end' => WEEK_IN_SECONDS,
[23] Fix | Delete
),
[24] Fix | Delete
'week-1-4' => array(
[25] Fix | Delete
'start' => WEEK_IN_SECONDS,
[26] Fix | Delete
'end' => WEEK_IN_SECONDS * 4,
[27] Fix | Delete
),
[28] Fix | Delete
'month-1-3' => array(
[29] Fix | Delete
'start' => MONTH_IN_SECONDS,
[30] Fix | Delete
'end' => MONTH_IN_SECONDS * 3,
[31] Fix | Delete
),
[32] Fix | Delete
'month-3-6' => array(
[33] Fix | Delete
'start' => MONTH_IN_SECONDS * 3,
[34] Fix | Delete
'end' => MONTH_IN_SECONDS * 6,
[35] Fix | Delete
),
[36] Fix | Delete
'month-6+' => array(
[37] Fix | Delete
'start' => MONTH_IN_SECONDS * 6,
[38] Fix | Delete
),
[39] Fix | Delete
);
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* Get the number of seconds that the store has been active.
[43] Fix | Delete
*
[44] Fix | Delete
* @return number Number of seconds.
[45] Fix | Delete
*/
[46] Fix | Delete
public static function get_wcadmin_active_for_in_seconds() {
[47] Fix | Delete
$install_timestamp = get_option( self::WC_ADMIN_TIMESTAMP_OPTION );
[48] Fix | Delete
[49] Fix | Delete
if ( ! is_numeric( $install_timestamp ) ) {
[50] Fix | Delete
$install_timestamp = time();
[51] Fix | Delete
update_option( self::WC_ADMIN_TIMESTAMP_OPTION, $install_timestamp );
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
return time() - $install_timestamp;
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Test how long WooCommerce Admin has been active.
[60] Fix | Delete
*
[61] Fix | Delete
* @param int $seconds Time in seconds to check.
[62] Fix | Delete
* @return bool Whether or not WooCommerce admin has been active for $seconds.
[63] Fix | Delete
*/
[64] Fix | Delete
public static function is_wc_admin_active_for( $seconds ) {
[65] Fix | Delete
$wc_admin_active_for = self::get_wcadmin_active_for_in_seconds();
[66] Fix | Delete
[67] Fix | Delete
return ( $wc_admin_active_for >= $seconds );
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
/**
[71] Fix | Delete
* Test if WooCommerce Admin has been active within a pre-defined range.
[72] Fix | Delete
*
[73] Fix | Delete
* @param string $range range available in WC_ADMIN_STORE_AGE_RANGES.
[74] Fix | Delete
* @param int $custom_start custom start in range.
[75] Fix | Delete
* @throws \InvalidArgumentException Throws exception when invalid $range is passed in.
[76] Fix | Delete
* @return bool Whether or not WooCommerce admin has been active within the range.
[77] Fix | Delete
*/
[78] Fix | Delete
public static function is_wc_admin_active_in_date_range( $range, $custom_start = null ) {
[79] Fix | Delete
if ( ! array_key_exists( $range, self::WC_ADMIN_STORE_AGE_RANGES ) ) {
[80] Fix | Delete
throw new \InvalidArgumentException(
[81] Fix | Delete
sprintf(
[82] Fix | Delete
'"%s" range is not supported, use one of: %s',
[83] Fix | Delete
$range,
[84] Fix | Delete
implode( ', ', array_keys( self::WC_ADMIN_STORE_AGE_RANGES ) )
[85] Fix | Delete
)
[86] Fix | Delete
);
[87] Fix | Delete
}
[88] Fix | Delete
$wc_admin_active_for = self::get_wcadmin_active_for_in_seconds();
[89] Fix | Delete
[90] Fix | Delete
$range_data = self::WC_ADMIN_STORE_AGE_RANGES[ $range ];
[91] Fix | Delete
$start = null !== $custom_start ? $custom_start : $range_data['start'];
[92] Fix | Delete
if ( $range_data && $wc_admin_active_for >= $start ) {
[93] Fix | Delete
return isset( $range_data['end'] ) ? $wc_admin_active_for < $range_data['end'] : true;
[94] Fix | Delete
}
[95] Fix | Delete
return false;
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
/**
[99] Fix | Delete
* Test if the site is fresh. A fresh site must meet the following requirements.
[100] Fix | Delete
*
[101] Fix | Delete
* - The current user was registered less than 1 month ago.
[102] Fix | Delete
* - fresh_site option must be 1
[103] Fix | Delete
*
[104] Fix | Delete
* @return bool
[105] Fix | Delete
*/
[106] Fix | Delete
public static function is_site_fresh() {
[107] Fix | Delete
$fresh_site = get_option( 'fresh_site' );
[108] Fix | Delete
if ( '1' !== $fresh_site ) {
[109] Fix | Delete
return false;
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
$current_userdata = get_userdata( get_current_user_id() );
[113] Fix | Delete
// Return false if we can't get user meta data for some reason.
[114] Fix | Delete
if ( ! $current_userdata ) {
[115] Fix | Delete
return false;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
$date = new \DateTime( $current_userdata->user_registered );
[119] Fix | Delete
$month_ago = new \DateTime( '-1 month' );
[120] Fix | Delete
[121] Fix | Delete
return $date > $month_ago;
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Check if the current page is a store page.
[126] Fix | Delete
*
[127] Fix | Delete
* This should only be called when WP has has set up the query, typically during or after the parse_query or template_redirect action hooks.
[128] Fix | Delete
*
[129] Fix | Delete
* @return bool
[130] Fix | Delete
*/
[131] Fix | Delete
public static function is_current_page_store_page() {
[132] Fix | Delete
// WC store pages.
[133] Fix | Delete
$store_pages = array(
[134] Fix | Delete
'shop' => wc_get_page_id( 'shop' ),
[135] Fix | Delete
'cart' => wc_get_page_id( 'cart' ),
[136] Fix | Delete
'checkout' => wc_get_page_id( 'checkout' ),
[137] Fix | Delete
'terms' => wc_terms_and_conditions_page_id(),
[138] Fix | Delete
'coming_soon' => wc_get_page_id( 'coming_soon' ),
[139] Fix | Delete
);
[140] Fix | Delete
[141] Fix | Delete
/**
[142] Fix | Delete
* Filter the store pages array to check if a URL is a store page.
[143] Fix | Delete
*
[144] Fix | Delete
* @since 8.8.0
[145] Fix | Delete
* @param array $store_pages The store pages array. The keys are the page slugs and the values are the page IDs.
[146] Fix | Delete
*/
[147] Fix | Delete
$store_pages = apply_filters( 'woocommerce_store_pages', $store_pages );
[148] Fix | Delete
[149] Fix | Delete
foreach ( $store_pages as $page_slug => $page_id ) {
[150] Fix | Delete
if ( $page_id > 0 && is_page( $page_id ) ) {
[151] Fix | Delete
return true;
[152] Fix | Delete
}
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
// Product archive page.
[156] Fix | Delete
if ( is_post_type_archive( 'product' ) ) {
[157] Fix | Delete
return true;
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
// Product page.
[161] Fix | Delete
if ( is_singular( 'product' ) ) {
[162] Fix | Delete
return true;
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
// Product taxonomy page (e.g. Product Category, Product Tag, etc.).
[166] Fix | Delete
if ( is_product_taxonomy() ) {
[167] Fix | Delete
return true;
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
global $wp;
[171] Fix | Delete
$url = self::get_url_from_wp( $wp );
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* Filter if a URL is a store page.
[175] Fix | Delete
*
[176] Fix | Delete
* @since 9.3.0
[177] Fix | Delete
* @param bool $is_store_page Whether or not the URL is a store page.
[178] Fix | Delete
* @param string $url URL to check.
[179] Fix | Delete
*/
[180] Fix | Delete
$is_store_page = apply_filters( 'woocommerce_is_extension_store_page', false, $url );
[181] Fix | Delete
[182] Fix | Delete
return filter_var( $is_store_page, FILTER_VALIDATE_BOOL );
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
/**
[186] Fix | Delete
* Test if a URL is a store page.
[187] Fix | Delete
*
[188] Fix | Delete
* @param string $url URL to check. If not provided, the current URL will be used.
[189] Fix | Delete
* @return bool Whether or not the URL is a store page.
[190] Fix | Delete
* @deprecated 9.8.0 Use is_current_page_store_page instead.
[191] Fix | Delete
*/
[192] Fix | Delete
public static function is_store_page( $url = '' ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
[193] Fix | Delete
_deprecated_function( __METHOD__, '9.8.0', 'is_current_page_store_page' );
[194] Fix | Delete
return self::is_current_page_store_page();
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
/**
[198] Fix | Delete
* Get normalized URL path.
[199] Fix | Delete
* 1. Only keep the path and query string (if any).
[200] Fix | Delete
* 2. Remove wp home path from the URL path if WP is installed in a subdirectory.
[201] Fix | Delete
* 3. Remove leading and trailing slashes.
[202] Fix | Delete
*
[203] Fix | Delete
* For example:
[204] Fix | Delete
*
[205] Fix | Delete
* - https://example.com/wordpress/shop/uncategorized/test/?add-to-cart=123 => shop/uncategorized/test/?add-to-cart=123
[206] Fix | Delete
*
[207] Fix | Delete
* @param string $url URL to normalize.
[208] Fix | Delete
*/
[209] Fix | Delete
private static function get_normalized_url_path( $url ) {
[210] Fix | Delete
$query = wp_parse_url( $url, PHP_URL_QUERY );
[211] Fix | Delete
$path = wp_parse_url( $url, PHP_URL_PATH ) . ( $query ? '?' . $query : '' );
[212] Fix | Delete
$home_path = wp_parse_url( site_url(), PHP_URL_PATH ) ?? '';
[213] Fix | Delete
$normalized_path = trim( substr( $path, strlen( $home_path ) ), '/' );
[214] Fix | Delete
return $normalized_path;
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
/**
[218] Fix | Delete
* Builds the relative URL from the WP instance.
[219] Fix | Delete
*
[220] Fix | Delete
* @internal
[221] Fix | Delete
* @link https://wordpress.stackexchange.com/a/274572
[222] Fix | Delete
* @param \WP $wp WordPress environment instance.
[223] Fix | Delete
*/
[224] Fix | Delete
private static function get_url_from_wp( \WP $wp ) {
[225] Fix | Delete
// Initialize query vars if they haven't been set.
[226] Fix | Delete
if ( empty( $wp->query_vars ) || empty( $wp->request ) ) {
[227] Fix | Delete
$wp->parse_request();
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
return home_url( add_query_arg( $wp->query_vars, $wp->request ) );
[231] Fix | Delete
}
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function