| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helper Class |
| 4 |
* |
| 5 |
* @package CTC |
| 6 |
* @since 5.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace CTC; |
| 10 |
|
| 11 |
/** |
| 12 |
* Helper Class |
| 13 |
* |
| 14 |
* @since 5.0.0 |
| 15 |
*/ |
| 16 |
class Helper { |
| 17 |
|
| 18 |
/** |
| 19 |
* Check if the plugin is Pro version |
| 20 |
* |
| 21 |
* Uses Freemius or can be filtered by Pro add-on plugin. |
| 22 |
* |
| 23 |
* @since 5.0.0 |
| 24 |
* @return bool |
| 25 |
*/ |
| 26 |
public static function is_pro() { |
| 27 |
/** |
| 28 |
* Filter to enable Pro features. |
| 29 |
* |
| 30 |
* Pro add-on plugin can hook into this to enable Pro features. |
| 31 |
* |
| 32 |
* @since 5.0.0 |
| 33 |
* @param bool $is_pro Whether Pro is active. Default false. |
| 34 |
*/ |
| 35 |
return apply_filters( 'ctc/is_pro', function_exists( 'ctc_fs' ) && ctc_fs()->is__premium_only() ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Minify CSS: strip comments and collapse whitespace. |
| 40 |
* |
| 41 |
* @since 5.0.2 |
| 42 |
* @param string $css Raw CSS. |
| 43 |
* @return string Minified CSS. |
| 44 |
*/ |
| 45 |
public static function minify_css( $css ) { |
| 46 |
$css = (string) preg_replace( '/\/\*[\s\S]*?\*\//', '', $css ); |
| 47 |
$css = (string) preg_replace( '/\s+/', ' ', $css ); |
| 48 |
return trim( $css ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Current date/time in MySQL format (UTC). |
| 53 |
* |
| 54 |
* @since 5.3.0 |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
public static function mysql_now() { |
| 58 |
return current_time( 'mysql', true ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Date relative to now (or a given timestamp) in MySQL format (UTC). |
| 63 |
* |
| 64 |
* @since 5.3.0 |
| 65 |
* @param string $relative Relative time e.g. '-30 days', '-24 hours'. |
| 66 |
* @param int|null $from_timestamp Optional base timestamp (UTC); default current timestamp. |
| 67 |
* @return string MySQL datetime (Y-m-d H:i:s). |
| 68 |
*/ |
| 69 |
public static function mysql_date_ago( $relative, $from_timestamp = null ) { |
| 70 |
if ( null === $from_timestamp ) { |
| 71 |
$from_timestamp = time(); |
| 72 |
} |
| 73 |
return gmdate( 'Y-m-d H:i:s', strtotime( $relative, $from_timestamp ) ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Epoch date for "all time" analytics (very old start date). |
| 78 |
* |
| 79 |
* @since 5.3.0 |
| 80 |
* @return string MySQL datetime. |
| 81 |
*/ |
| 82 |
public static function mysql_epoch() { |
| 83 |
return '1970-01-01 00:00:00'; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Map analytics period key to number of days. |
| 88 |
* |
| 89 |
* @since 5.3.0 |
| 90 |
* @param string $period One of '24h', '7d', '30d', '60d', '90d', '180d'. |
| 91 |
* @return int Number of days; default 7. |
| 92 |
*/ |
| 93 |
public static function analytics_period_to_days( $period ) { |
| 94 |
$map = [ |
| 95 |
'24h' => 1, |
| 96 |
'7d' => 7, |
| 97 |
'30d' => 30, |
| 98 |
'60d' => 60, |
| 99 |
'90d' => 90, |
| 100 |
'180d' => 180, |
| 101 |
]; |
| 102 |
return isset( $map[ $period ] ) ? $map[ $period ] : 7; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get analytics date range (from/to) in MySQL datetime format. |
| 107 |
* |
| 108 |
* Use when custom from/to are provided, or derive from period. |
| 109 |
* |
| 110 |
* @since 5.3.0 |
| 111 |
* @param string $period Period key ('24h', '7d', '30d', '90d'). |
| 112 |
* @param string|null $from Optional custom start date (Y-m-d or MySQL datetime). |
| 113 |
* @param string|null $to Optional custom end date (Y-m-d or MySQL datetime). |
| 114 |
* @return array{from: string, to: string} Keys 'from' and 'to' as MySQL datetime strings. |
| 115 |
*/ |
| 116 |
public static function analytics_date_range( $period, $from = null, $to = null ) { |
| 117 |
if ( $from && $to ) { |
| 118 |
return [ |
| 119 |
'from' => sanitize_text_field( $from ), |
| 120 |
'to' => sanitize_text_field( $to ), |
| 121 |
]; |
| 122 |
} |
| 123 |
$days = self::analytics_period_to_days( $period ); |
| 124 |
$start_date = self::mysql_date_ago( "-{$days} days" ); |
| 125 |
$end_date = self::mysql_now(); |
| 126 |
return [ |
| 127 |
'from' => $start_date, |
| 128 |
'to' => $end_date, |
| 129 |
]; |
| 130 |
} |
| 131 |
} |
| 132 |
|