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