/var/www/html_us/wp-content/plugins/woocommerce/src/Admin/WCAdminHelper.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
 * WCAdminHelper
 *
 * Helper class for generic WCAdmin functions.
 */

namespace Automattic\WooCommerce\Admin;

defined'ABSPATH' ) || exit;

/**
 * Class WCAdminHelper
 */
class WCAdminHelper {
    
/**
     * WC Admin timestamp option name.
     */
    
const WC_ADMIN_TIMESTAMP_OPTION 'woocommerce_admin_install_timestamp';

    const 
WC_ADMIN_STORE_AGE_RANGES = array(
        
'week-1'    => array(
            
'start' => 0,
            
'end'   => WEEK_IN_SECONDS,
        ),
        
'week-1-4'  => array(
            
'start' => WEEK_IN_SECONDS,
            
'end'   => WEEK_IN_SECONDS 4,
        ),
        
'month-1-3' => array(
            
'start' => MONTH_IN_SECONDS,
            
'end'   => MONTH_IN_SECONDS 3,
        ),
        
'month-3-6' => array(
            
'start' => MONTH_IN_SECONDS 3,
            
'end'   => MONTH_IN_SECONDS 6,
        ),
        
'month-6+'  => array(
            
'start' => MONTH_IN_SECONDS 6,
        ),
    );

    
/**
     * Get the number of seconds that the store has been active.
     *
     * @return number Number of seconds.
     */
    
public static function get_wcadmin_active_for_in_seconds() {
        
$install_timestamp get_optionself::WC_ADMIN_TIMESTAMP_OPTION );

        if ( ! 
is_numeric$install_timestamp ) ) {
            
$install_timestamp time();
            
update_optionself::WC_ADMIN_TIMESTAMP_OPTION$install_timestamp );
        }

        return 
time() - $install_timestamp;
    }


    
/**
     * Test how long WooCommerce Admin has been active.
     *
     * @param int $seconds Time in seconds to check.
     * @return bool Whether or not WooCommerce admin has been active for $seconds.
     */
    
public static function is_wc_admin_active_for$seconds ) {
        
$wc_admin_active_for self::get_wcadmin_active_for_in_seconds();

        return ( 
$wc_admin_active_for >= $seconds );
    }

    
/**
     * Test if WooCommerce Admin has been active within a pre-defined range.
     *
     * @param string $range range available in WC_ADMIN_STORE_AGE_RANGES.
     * @param int    $custom_start custom start in range.
     * @throws \InvalidArgumentException Throws exception when invalid $range is passed in.
     * @return bool Whether or not WooCommerce admin has been active within the range.
     */
    
public static function is_wc_admin_active_in_date_range$range$custom_start null ) {
        if ( ! 
array_key_exists$rangeself::WC_ADMIN_STORE_AGE_RANGES ) ) {
            throw new 
\InvalidArgumentException(
                
sprintf(
                    
'"%s" range is not supported, use one of: %s',
                    
$range,
                    
implode', 'array_keysself::WC_ADMIN_STORE_AGE_RANGES ) )
                )
            );
        }
        
$wc_admin_active_for self::get_wcadmin_active_for_in_seconds();

        
$range_data self::WC_ADMIN_STORE_AGE_RANGES$range ];
        
$start      null !== $custom_start $custom_start $range_data['start'];
        if ( 
$range_data && $wc_admin_active_for >= $start ) {
            return isset( 
$range_data['end'] ) ? $wc_admin_active_for $range_data['end'] : true;
        }
        return 
false;
    }

    
/**
     * Test if the site is fresh. A fresh site must meet the following requirements.
     *
     * - The current user was registered less than 1 month ago.
     * - fresh_site option must be 1
     *
     * @return bool
     */
    
public static function is_site_fresh() {
        
$fresh_site get_option'fresh_site' );
        if ( 
'1' !== $fresh_site ) {
            return 
false;
        }

        
$current_userdata get_userdataget_current_user_id() );
        
// Return false if we can't get user meta data for some reason.
        
if ( ! $current_userdata ) {
            return 
false;
        }

        
$date      = new \DateTime$current_userdata->user_registered );
        
$month_ago = new \DateTime'-1 month' );

        return 
$date $month_ago;
    }

    
/**
     * Test if a URL is a store page. This function ignores the domain and protocol of the URL and only checks the path and query string.
     *
     * Store pages are defined as:
     *
     * - Shop
     * - Cart
     * - Checkout
     * - Privacy Policy
     * - Terms and Conditions
     *
     * Additionally, the following autogenerated pages should be included:
     * - Product pages
     * - Product Category pages
     * - Product Tag pages
     *
     * @param string $url URL to check. If not provided, the current URL will be used.
     * @return bool Whether or not the URL is a store page.
     */
    
public static function is_store_page$url '' ) {
        
$url $url $url esc_url_rawwp_unslash$_SERVER['REQUEST_URI'] ?? '' ) );

        if ( ! 
$url ) {
            return 
false;
        }
        
$normalized_path self::get_normalized_url_path$url );

        
$params = array(
            
'post_type' => 'product',
        );

        
$query_string wp_parse_url$urlPHP_URL_QUERY );
        if ( 
$query_string ) {
            
parse_str$query_string$url_params );
            foreach ( 
$params as $key => $param ) {
                if ( isset( 
$url_params$key ] ) && $url_params$key ] === $param ) {
                    return 
true;
                }
            }
        }

        
// WC store pages.
        
$store_pages = array(
            
'shop'        => wc_get_page_id'shop' ),
            
'cart'        => wc_get_page_id'cart' ),
            
'checkout'    => wc_get_page_id'checkout' ),
            
'terms'       => wc_terms_and_conditions_page_id(),
            
'coming_soon' => wc_get_page_id'coming_soon' ),
        );

        
/**
         * Filter the store pages array to check if a URL is a store page.
         *
         * @since 8.8.0
         * @param array $store_pages The store pages array. The keys are the page slugs and the values are the page IDs.
         */
        
$store_pages apply_filters'woocommerce_store_pages'$store_pages );

        
// If the shop page is not set, we will still show the product archive page.
        // Therefore, we need to check if the URL is a product archive page when the shop page is not set.
        
if ( $store_pages['shop'] <= ) {
            
$product_post_archive_link get_post_type_archive_link'product' );

            if ( 
is_string$product_post_archive_link ) &&
                
=== strpos$normalized_pathself::get_normalized_url_path$product_post_archive_link ) )
            ) {
                return 
true;
            }
        }

        
$url_path wp_parse_url$normalized_pathPHP_URL_PATH );

        foreach ( 
$store_pages as $page => $page_id ) {
            if ( 
>= $page_id ) {
                continue;
            }

            
$permalink get_permalink$page_id );

            if ( ! 
$permalink ) {
                continue;
            }

            
$normalized_permalink self::get_normalized_url_path$permalink );

            
// Compare the URL path with the permalink path.
            // If the URL path is null, it means the provided URL doesn't have any paths.
            // In that case, compare the normalized permalink with the normalized URL.
            // Otherwise, compare the normalized permalink path with the URL path.
            
if ( null === $url_path && $normalized_permalink === $normalized_path ) {
                return 
true;
            } elseif ( 
$normalized_permalink === $url_path ) {
                return 
true;
            }
        }

        
// Check if the URL matches any of the WooCommerce permalink structures.
        
if ( $url_path ) {
            
$permalink_structure wc_get_permalink_structure();
            
$permalink_keys      = array(
                
'category_base',
                
'tag_base',
                
'product_base',
            );

            foreach ( 
$permalink_keys as $key ) {
                if ( ! isset( 
$permalink_structure$key ] ) || ! is_string$permalink_structure$key ] ) ) {
                    continue;
                }

                
/**
                 * Check if the URL path starts with a URL segment matching the permalink base.
                 *
                 * Match examples:
                 *    1. product_base = 'product'
                 *       $url_path: '/product/blue-t-shirt'
                 *    2. category_base = 'product-category'
                 *       $url_path: '/product-category/clothing/'
                 *
                 * Non-match examples:
                 *    1. product_base = 'product'
                 *       $url_path: '/products' (URL segment partial match)
                 */
                
if ( === strncmp$url_pathtrim$permalink_structure$key ], '/' ), strlentrim$permalink_structure$key ], '/' ) ) ) ) {
                    
$base_length strlentrim$permalink_structure$key ], '/' ) );
                    
$next_char   substr$url_path$base_length);

                    
// If the next character is a slash, we're confident we're on a valid product page.
                    
if ( '/' === $next_char ) {
                        return 
true;
                    }
                }

                
/**
                 * If the permalink structure contains placeholders, we need to check if the URL matches the structure using regex.
                 *
                 * Match examples:
                 *    1. product_base = 'product/%product_cat%'
                 *       $url_path: '/product/t-shirts/blue-t-shirt'
                 *       $url_path: '/product/electronics/smartphones/iphone-12'
                 *    2. product_base = 'shop/%product_cat%/%product%'
                 *       $url_path: '/shop/clothing/mens/summer-shirt'
                 *    3. product_base = '%product_cat%'
                 *       $url_path: '/electronics/laptops/macbook-pro'
                 *
                 * Non-match examples:
                 *    1. product_base = 'product/%product_cat%'
                 *       $url_path: '/blog/top-10-products'
                 *       $url_path: '/product-reviews/best-sellers'
                 *    2. product_base = 'shop/%product_cat%/%product%'
                 *       $url_path: '/shop/new-arrivals'
                 */
                
if ( strpos$permalink_structure$key ], '%' ) !== false ) {
                    global 
$wp_rewrite;
                    
$rules $wp_rewrite->generate_rewrite_rule$permalink_structure$key ] );
                    if ( 
is_array$rules ) && ! empty( $rules ) ) {
                        
// rule key is the regex pattern.
                        
$rule array_keys$rules )[0];
                        
$rule '#^' str_replace'?$'''$rule ) . '#';

                        if ( 
preg_match$rule$normalized_path ) ) {
                            return 
true;
                        }
                    }
                }
            }
        }

        
/**
         * Filter if a URL is a store page.
         *
         * @since 9.3.0
         * @param bool   $is_store_page Whether or not the URL is a store page.
         * @param string $url           URL to check.
         */
        
$is_store_page apply_filters'woocommerce_is_extension_store_page'false$url );

        return 
filter_var$is_store_pageFILTER_VALIDATE_BOOL );
    }

    
/**
     * Get normalized URL path.
     * 1. Only keep the path and query string (if any).
     * 2. Remove wp home path from the URL path if WP is installed in a subdirectory.
     * 3. Remove leading and trailing slashes.
     *
     * For example:
     *
     * - https://example.com/wordpress/shop/uncategorized/test/?add-to-cart=123 => shop/uncategorized/test/?add-to-cart=123
     *
     * @param string $url URL to normalize.
     */
    
private static function get_normalized_url_path$url ) {
        
$query           wp_parse_url$urlPHP_URL_QUERY );
        
$path            wp_parse_url$urlPHP_URL_PATH ) . ( $query '?' $query '' );
        
$home_path       wp_parse_urlsite_url(), PHP_URL_PATH ) ?? '';
        
$normalized_path trimsubstr$pathstrlen$home_path ) ), '/' );
        return 
$normalized_path;
    }
}