API
2 years ago
Composer
4 years ago
DateTimeProvider
4 years ago
Features
2 years ago
Marketing
3 years ago
Notes
3 years ago
Overrides
3 years ago
PluginsInstallLoggers
2 years ago
PluginsProvider
4 years ago
RemoteInboxNotifications
2 years ago
Schedulers
4 years ago
DataSourcePoller.php
3 years ago
DeprecatedClassFacade.php
4 years ago
FeaturePlugin.php
4 years ago
Loader.php
4 years ago
PageController.php
3 years ago
PluginsHelper.php
2 years ago
PluginsInstaller.php
4 years ago
ReportCSVEmail.php
4 years ago
ReportCSVExporter.php
3 years ago
ReportExporter.php
3 years ago
ReportsSync.php
3 years ago
WCAdminHelper.php
4 years ago
WCAdminHelper.php
100 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 |