|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Track the usage of the plugin. |
| 4 | + * |
| 5 | + * @link https://themeisle.com |
| 6 | + * @since 3.0.0 |
| 7 | + * |
| 8 | + * @package feedzy-rss-feeds |
| 9 | + * @subpackage feedzy-rss-feeds/includes/admin |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Track the usage of the plugin. |
| 14 | + * |
| 15 | + * @package feedzy-rss-feeds |
| 16 | + * @subpackage feedzy-rss-feeds/includes/admin |
| 17 | + * @author Themeisle <friends@themeisle.com> |
| 18 | + */ |
| 19 | +class Feedzy_Rss_Feeds_Usage { |
| 20 | + |
| 21 | + /** |
| 22 | + * Option name in wp_options table. |
| 23 | + */ |
| 24 | + const OPTION_NAME = 'feedzy_usage'; |
| 25 | + |
| 26 | + /** |
| 27 | + * The single instance of the class. |
| 28 | + * |
| 29 | + * @var Feedzy_Rss_Feeds_Usage|null |
| 30 | + */ |
| 31 | + private static $instance = null; |
| 32 | + |
| 33 | + /** |
| 34 | + * Default usage data structure. |
| 35 | + * |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + private $default_data = array( |
| 39 | + 'first_import_run_datetime' => null, |
| 40 | + 'imports_runs' => 0, |
| 41 | + 'first_import_created_datetime' => null, |
| 42 | + ); |
| 43 | + |
| 44 | + /** |
| 45 | + * Private constructor to prevent direct instantiation. |
| 46 | + */ |
| 47 | + private function __construct() { |
| 48 | + $this->init(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Prevent cloning of the instance. |
| 53 | + */ |
| 54 | + public function __clone() {} |
| 55 | + |
| 56 | + /** |
| 57 | + * Prevent unserialization of the instance. |
| 58 | + */ |
| 59 | + public function __wakeup() {} |
| 60 | + |
| 61 | + /** |
| 62 | + * Get the single instance of the class. |
| 63 | + * |
| 64 | + * @return Feedzy_Rss_Feeds_Usage |
| 65 | + */ |
| 66 | + public static function get_instance() { |
| 67 | + if (null === self::$instance) { |
| 68 | + self::$instance = new self(); |
| 69 | + } |
| 70 | + return self::$instance; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Initialize the usage tracking. |
| 75 | + * Creates the option if it doesn't exist. |
| 76 | + */ |
| 77 | + private function init() { |
| 78 | + if ( false === get_option(self::OPTION_NAME) ) { |
| 79 | + add_option(self::OPTION_NAME, $this->default_data); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get all usage data. |
| 85 | + * |
| 86 | + * @return array Usage data array. |
| 87 | + */ |
| 88 | + public function get_usage_data() { |
| 89 | + $data = get_option(self::OPTION_NAME, $this->default_data); |
| 90 | + return wp_parse_args($data, $this->default_data); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Update usage data. |
| 95 | + * |
| 96 | + * @param array $new_data Data to update. |
| 97 | + * @return bool True if the option was updated, false otherwise. |
| 98 | + */ |
| 99 | + private function update_usage_data($new_data) { |
| 100 | + $current_data = $this->get_usage_data(); |
| 101 | + $updated_data = array_merge($current_data, $new_data); |
| 102 | + return update_option(self::OPTION_NAME, $updated_data); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + /** |
| 108 | + * Track RSS feed import. |
| 109 | + * Sets first import timestamp if it's the first import, always increments counter. |
| 110 | + * |
| 111 | + * @return void |
| 112 | + */ |
| 113 | + public function track_rss_import() { |
| 114 | + $data = $this->get_usage_data(); |
| 115 | + |
| 116 | + if ( PHP_INT_MAX <= $data['imports_runs'] ) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + $update_data = array(); |
| 121 | + |
| 122 | + $update_data['imports_runs'] = $data['imports_runs'] + 1; |
| 123 | + |
| 124 | + if ( empty( $data['first_import_run_datetime'] ) ) { |
| 125 | + $update_data['first_import_run_datetime'] = current_time('mysql'); |
| 126 | + } |
| 127 | + |
| 128 | + $this->update_usage_data($update_data); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Track settings page creation. |
| 133 | + * Sets first settings page timestamp if it's the first page, always increments counter. |
| 134 | + * |
| 135 | + * @return void |
| 136 | + */ |
| 137 | + public function track_import_creation() { |
| 138 | + $data = $this->get_usage_data(); |
| 139 | + |
| 140 | + if ( ! empty( $data['first_import_created_datetime'] ) ) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + $this->update_usage_data( array( 'first_import_created_datetime' => current_time('mysql') ) ); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Delete the usage data option. |
| 149 | + * Useful for plugin uninstall. |
| 150 | + * |
| 151 | + * @return bool True if the option was deleted, false otherwise. |
| 152 | + */ |
| 153 | + public function delete_usage_data() { |
| 154 | + return delete_option(self::OPTION_NAME); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Get usage statistics in a formatted array. |
| 159 | + * |
| 160 | + * @return array<string, mixed> Formatted usage statistics. |
| 161 | + */ |
| 162 | + public function get_usage_stats() { |
| 163 | + $data = $this->get_usage_data(); |
| 164 | + |
| 165 | + $stats = array( |
| 166 | + 'first_import_run_datetime' => !empty($data['first_import_run_datetime']) ? |
| 167 | + $data['first_import_run_datetime'] : 'Never', |
| 168 | + 'import_runs' => $data['imports_runs'], |
| 169 | + 'first_import_created_datetime' => !empty($data['first_import_created_datetime']) ? |
| 170 | + $data['first_import_created_datetime'] : 'Never', |
| 171 | + ); |
| 172 | + |
| 173 | + // Calculate time between first import run and first import created if applicable. |
| 174 | + if (!empty($data['first_import_run_datetime']) && !empty($data['first_import_created_datetime'])) { |
| 175 | + $import_time = strtotime($data['first_import_run_datetime']); |
| 176 | + $settings_time = strtotime($data['first_import_created_datetime']); |
| 177 | + |
| 178 | + if ( $settings_time > $import_time ) { |
| 179 | + $time_diff = $settings_time - $import_time; |
| 180 | + $stats['time_between_first_import_created_and_run'] = $time_diff; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return $stats; |
| 185 | + } |
| 186 | +} |
0 commit comments