AJAX
1 year ago
Admin_Page
1 year ago
Custom_WordPress_Columns
2 years ago
Data_Pruning
1 year ago
Date_Picker
1 year ago
Date_Range
1 year ago
Ecommerce
1 year ago
Email_Reports
1 year ago
Filter_Lists
2 years ago
Form_Submissions
1 year ago
Interval
2 years ago
Menu_Bar_Stats
1 year ago
Migrations
1 year ago
Models
1 year ago
Public_API
2 years ago
Rows
1 year ago
Statistics
1 year ago
Tables
1 year ago
Utils
1 year ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
1 year ago
Chart.php
1 year ago
Chart_Geo.php
1 year ago
Cron_Job.php
1 year ago
Cron_Job_Autoloader.php
1 year ago
Cron_Manager.php
2 years ago
Current_Traffic_Finder.php
2 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
2 years ago
Database.php
2 years ago
Database_Manager.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
1 year ago
Filters.php
1 year ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.php
1 year ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
1 year ago
Independent_Analytics.php
1 year ago
Interrupt.php
1 year ago
Known_Referrers.php
2 years ago
Patch.php
1 year ago
Plugin_Conflict_Detector.php
1 year ago
Plugin_Group.php
1 year ago
Plugin_Group_Option.php
2 years ago
Query.php
1 year ago
Query_Taps.php
1 year ago
Quick_Stats.php
2 years ago
REST_API.php
1 year ago
Real_Time.php
1 year ago
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
1 year ago
Resource_Identifier.php
1 year ago
Settings.php
1 year ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
1 year ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
Independent_Analytics.php
340 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWP\Admin_Page\Analytics_Page; |
| 6 | use IAWP\Admin_Page\Campaign_Builder_Page; |
| 7 | use IAWP\Admin_Page\Settings_Page; |
| 8 | use IAWP\Admin_Page\Support_Page; |
| 9 | use IAWP\Admin_Page\Updates_Page; |
| 10 | use IAWP\AJAX\AJAX_Manager; |
| 11 | use IAWP\Data_Pruning\Pruner; |
| 12 | use IAWP\Ecommerce\SureCart_Order; |
| 13 | use IAWP\Ecommerce\WooCommerce_Order; |
| 14 | use IAWP\Ecommerce\WooCommerce_Referrer_Meta_Box; |
| 15 | use IAWP\Email_Reports\Email_Reports; |
| 16 | use IAWP\Form_Submissions\Form; |
| 17 | use IAWP\Form_Submissions\Submission_Listener; |
| 18 | use IAWP\Menu_Bar_Stats\Menu_Bar_Stats; |
| 19 | use IAWP\Migrations\Migrations; |
| 20 | use IAWP\Utils\Plugin; |
| 21 | use IAWP\Utils\Singleton; |
| 22 | use IAWPSCOPED\Proper\Timezone; |
| 23 | /** @internal */ |
| 24 | class Independent_Analytics |
| 25 | { |
| 26 | use Singleton; |
| 27 | public $settings; |
| 28 | public $email_reports; |
| 29 | public $cron_manager; |
| 30 | private $is_woocommerce_support_enabled; |
| 31 | private $is_surecart_support_enabled; |
| 32 | private $is_form_submission_support_enabled; |
| 33 | // This is where we attach functions to WP hooks |
| 34 | private function __construct() |
| 35 | { |
| 36 | $this->settings = new \IAWP\Settings(); |
| 37 | new \IAWP\REST_API(); |
| 38 | new \IAWP\Dashboard_Widget(); |
| 39 | new \IAWP\View_Counter(); |
| 40 | new Submission_Listener(); |
| 41 | Pruner::register_hook(); |
| 42 | AJAX_Manager::getInstance(); |
| 43 | if (!Migrations::is_migrating()) { |
| 44 | new \IAWP\Track_Resource_Changes(); |
| 45 | Menu_Bar_Stats::register(); |
| 46 | WooCommerce_Order::register_hooks(); |
| 47 | SureCart_Order::register_hooks(); |
| 48 | } |
| 49 | $this->cron_manager = new \IAWP\Cron_Manager(); |
| 50 | \IAWP\Cron_Job_Autoloader::register_handler(); |
| 51 | if (\IAWPSCOPED\iawp_is_pro()) { |
| 52 | $this->email_reports = new Email_Reports(); |
| 53 | new \IAWP\Campaign_Builder(); |
| 54 | new WooCommerce_Referrer_Meta_Box(); |
| 55 | } |
| 56 | \add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts_and_styles'], 110); |
| 57 | // Called at 110 to dequeue other scripts |
| 58 | \add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts_and_styles_front_end']); |
| 59 | \add_action('admin_menu', [$this, 'add_admin_menu_pages']); |
| 60 | \add_action('admin_init', [$this, 'remove_freemius_pricing_menu']); |
| 61 | \add_filter('plugin_action_links_independent-analytics/iawp.php', [$this, 'plugin_action_links']); |
| 62 | \add_action('init', [$this, 'polylang_translations']); |
| 63 | \add_action('init', [$this, 'load_textdomain']); |
| 64 | \IAWP_FS()->add_filter('pricing_url', [$this, 'change_freemius_pricing_url'], 10); |
| 65 | \IAWP_FS()->add_filter('show_deactivation_feedback_form', function () { |
| 66 | return \false; |
| 67 | }); |
| 68 | \add_action('admin_init', [$this, 'maybe_delete_mu_plugin']); |
| 69 | \add_action('admin_body_class', [$this, 'add_body_class']); |
| 70 | } |
| 71 | public function add_body_class($classes) |
| 72 | { |
| 73 | if (\get_option('iawp_dark_mode')) { |
| 74 | $classes .= ' iawp-dark-mode '; |
| 75 | } |
| 76 | $page = \IAWP\Env::get_page(); |
| 77 | if (\is_string($page)) { |
| 78 | $classes .= " {$page} "; |
| 79 | } |
| 80 | return $classes; |
| 81 | } |
| 82 | /** |
| 83 | * At one point in time, there was a must-use plugin that was created. The plugin file and the |
| 84 | * option need to get cleaned up. |
| 85 | * @return void |
| 86 | */ |
| 87 | public function maybe_delete_mu_plugin() |
| 88 | { |
| 89 | $already_attempted = \get_option('iawp_attempted_to_delete_mu_plugin', '0'); |
| 90 | if ($already_attempted === '1') { |
| 91 | return; |
| 92 | } |
| 93 | if (\get_option('iawp_must_use_directory_not_writable', '0') === '1') { |
| 94 | \delete_option('iawp_must_use_directory_not_writable'); |
| 95 | } |
| 96 | $mu_plugin_file = \trailingslashit(\WPMU_PLUGIN_DIR) . 'iawp-performance-boost.php'; |
| 97 | if (\file_exists($mu_plugin_file)) { |
| 98 | \unlink($mu_plugin_file); |
| 99 | } |
| 100 | \update_option('iawp_attempted_to_delete_mu_plugin', '1', \true); |
| 101 | } |
| 102 | public function load_textdomain() |
| 103 | { |
| 104 | \load_plugin_textdomain('independent-analytics', \false, \IAWP_LANGUAGES_DIRECTORY); |
| 105 | } |
| 106 | public function polylang_translations() |
| 107 | { |
| 108 | if (\function_exists('IAWPSCOPED\\pll_register_string')) { |
| 109 | pll_register_string('view_counter', 'Views:', 'Independent Analytics'); |
| 110 | } |
| 111 | } |
| 112 | // Changes the URL for the "Upgrade" tab in the Account menu |
| 113 | public function change_freemius_pricing_url() |
| 114 | { |
| 115 | return 'https://independentwp.com/pricing/?utm_source=User+Dashboard&utm_medium=WP+Admin&utm_campaign=Upgrade+to+Pro&utm_content=Account'; |
| 116 | } |
| 117 | public function add_admin_menu_pages() |
| 118 | { |
| 119 | $title = \IAWP\Capability_Manager::show_white_labeled_ui() ? \esc_html__('Analytics', 'independent-analytics') : 'Independent Analytics'; |
| 120 | \add_menu_page($title, \esc_html__('Analytics', 'independent-analytics'), \IAWP\Capability_Manager::menu_page_capability_string(), 'independent-analytics', function () { |
| 121 | $analytics_page = new Analytics_Page(); |
| 122 | $analytics_page->render(); |
| 123 | }, 'dashicons-analytics', 3); |
| 124 | if (\IAWP\Capability_Manager::can_edit()) { |
| 125 | \add_submenu_page('independent-analytics', \esc_html__('Settings', 'independent-analytics'), \esc_html__('Settings', 'independent-analytics'), \IAWP\Capability_Manager::menu_page_capability_string(), 'independent-analytics-settings', function () { |
| 126 | $settings_page = new Settings_Page(); |
| 127 | $settings_page->render(\false); |
| 128 | }); |
| 129 | } |
| 130 | if (\IAWPSCOPED\iawp_is_pro()) { |
| 131 | \add_submenu_page('independent-analytics', \esc_html__('Campaign Builder', 'independent-analytics'), \esc_html__('Campaign Builder', 'independent-analytics'), \IAWP\Capability_Manager::menu_page_capability_string(), 'independent-analytics-campaign-builder', function () { |
| 132 | $campaign_builder_page = new Campaign_Builder_Page(); |
| 133 | $campaign_builder_page->render(\false); |
| 134 | }); |
| 135 | } |
| 136 | if (\IAWP\Capability_Manager::show_branded_ui()) { |
| 137 | \add_submenu_page('independent-analytics', \esc_html__('Help & Support', 'independent-analytics'), \esc_html__('Help & Support', 'independent-analytics'), \IAWP\Capability_Manager::menu_page_capability_string(), 'independent-analytics-support-center', function () { |
| 138 | $support_page = new Support_Page(); |
| 139 | $support_page->render(\false); |
| 140 | }); |
| 141 | } |
| 142 | if (\IAWP\Capability_Manager::show_branded_ui()) { |
| 143 | $menu_html = '<span class="menu-name">' . \esc_html__('Changelog', 'independent-analytics') . '</span>'; |
| 144 | $menu_html = $this->changelog_viewed_since_update() ? $menu_html . ' <span class="menu-counter">' . \esc_html__('New', 'independent-analytics') . '</span>' : $menu_html; |
| 145 | \add_submenu_page('independent-analytics', \esc_html__('Changelog', 'independent-analytics'), $menu_html, \IAWP\Capability_Manager::menu_page_capability_string(), 'independent-analytics-updates', function () { |
| 146 | $updates_page = new Updates_Page(); |
| 147 | $updates_page->render(\false); |
| 148 | }); |
| 149 | } |
| 150 | if (\IAWPSCOPED\iawp_is_free() && \IAWP\Capability_Manager::show_branded_ui()) { |
| 151 | \add_submenu_page('independent-analytics', \esc_html__('Upgrade to Pro →', 'independent-analytics'), '<span style="color: #F69D0A;">' . \esc_html__('Upgrade to Pro →', 'independent-analytics') . '</span>', \IAWP\Capability_Manager::menu_page_capability_string(), \esc_url('https://independentwp.com/pricing/?utm_source=User+Dashboard&utm_medium=WP+Admin&utm_campaign=Upgrade+to+Pro&utm_content=Sidebar')); |
| 152 | } |
| 153 | } |
| 154 | // The menu link is removed in the SDK setup, but this makes it completely inaccessible |
| 155 | public function remove_freemius_pricing_menu() |
| 156 | { |
| 157 | \remove_submenu_page('independent-analytics', 'independent-analytics-pricing'); |
| 158 | } |
| 159 | public function register_scripts_and_styles() : void |
| 160 | { |
| 161 | \wp_register_style('iawp-styles', \IAWPSCOPED\iawp_url_to('dist/styles/style.css'), [], \IAWP_VERSION); |
| 162 | \wp_register_style('iawp-dashboard-widget-styles', \IAWPSCOPED\iawp_url_to('dist/styles/dashboard_widget.css'), [], \IAWP_VERSION); |
| 163 | \wp_register_style('iawp-freemius-notice-styles', \IAWPSCOPED\iawp_url_to('dist/styles/freemius_notice_styles.css'), [], \IAWP_VERSION); |
| 164 | \wp_register_style('iawp-posts-menu-styles', \IAWPSCOPED\iawp_url_to('dist/styles/posts_menu.css'), [], \IAWP_VERSION); |
| 165 | \wp_register_script('iawp-javascript', \IAWPSCOPED\iawp_url_to('dist/js/index.js'), [], \IAWP_VERSION); |
| 166 | \wp_register_script('iawp-dashboard-widget-javascript', \IAWPSCOPED\iawp_url_to('dist/js/dashboard_widget.js'), [], \IAWP_VERSION); |
| 167 | \wp_register_script('iawp-layout-javascript', \IAWPSCOPED\iawp_url_to('dist/js/layout.js'), [], \IAWP_VERSION); |
| 168 | \wp_register_script('iawp-settings-javascript', \IAWPSCOPED\iawp_url_to('dist/js/settings.js'), ['wp-color-picker'], \IAWP_VERSION); |
| 169 | if (Menu_Bar_Stats::is_option_enabled()) { |
| 170 | \wp_register_style('iawp-front-end-styles', \IAWPSCOPED\iawp_url_to('dist/styles/menu_bar_stats.css'), [], \IAWP_VERSION); |
| 171 | } |
| 172 | if (\is_rtl()) { |
| 173 | \wp_register_style('iawp-styles-rtl', \IAWPSCOPED\iawp_url_to('dist/styles/rtl.css'), [], \IAWP_VERSION); |
| 174 | } |
| 175 | } |
| 176 | public function register_scripts_and_styles_front_end() : void |
| 177 | { |
| 178 | if (Menu_Bar_Stats::is_option_enabled()) { |
| 179 | \wp_register_style('iawp-front-end-styles', \IAWPSCOPED\iawp_url_to('dist/styles/menu_bar_stats.css'), [], \IAWP_VERSION); |
| 180 | } |
| 181 | } |
| 182 | public function enqueue_scripts_and_styles($hook) |
| 183 | { |
| 184 | $this->register_scripts_and_styles(); |
| 185 | $page = \IAWP\Env::get_page(); |
| 186 | $this->enqueue_translations(); |
| 187 | $this->enqueue_nonces(); |
| 188 | \wp_enqueue_style('iawp-freemius-notice-styles'); |
| 189 | if (\is_string($page)) { |
| 190 | \wp_enqueue_style('iawp-styles'); |
| 191 | \wp_enqueue_script('iawp-javascript'); |
| 192 | \wp_enqueue_script('iawp-layout-javascript'); |
| 193 | $this->dequeue_bad_actors(); |
| 194 | $this->maybe_override_adminify_styles(); |
| 195 | if (\is_rtl()) { |
| 196 | \wp_enqueue_style('iawp-styles-rtl'); |
| 197 | } |
| 198 | } |
| 199 | if ($page === 'independent-analytics-settings') { |
| 200 | \wp_enqueue_style('wp-color-picker'); |
| 201 | \wp_enqueue_script('iawp-settings-javascript'); |
| 202 | } elseif ($hook === 'index.php') { |
| 203 | \wp_enqueue_script('iawp-dashboard-widget-javascript'); |
| 204 | \wp_enqueue_style('iawp-dashboard-widget-styles'); |
| 205 | } elseif ($hook === 'edit.php') { |
| 206 | \wp_enqueue_style('iawp-posts-menu-styles'); |
| 207 | } |
| 208 | if (Menu_Bar_Stats::is_option_enabled()) { |
| 209 | \wp_enqueue_style('iawp-front-end-styles'); |
| 210 | } |
| 211 | } |
| 212 | public function enqueue_scripts_and_styles_front_end() |
| 213 | { |
| 214 | if (Menu_Bar_Stats::is_option_enabled()) { |
| 215 | $this->register_scripts_and_styles_front_end(); |
| 216 | \wp_enqueue_style('iawp-front-end-styles'); |
| 217 | } |
| 218 | } |
| 219 | public function enqueue_translations() |
| 220 | { |
| 221 | \wp_register_script('iawp-translations', ''); |
| 222 | \wp_enqueue_script('iawp-translations'); |
| 223 | \wp_add_inline_script('iawp-translations', 'const iawpText = ' . \json_encode(['views' => \__('Views', 'independent-analytics'), 'exactDates' => \__('Apply Exact Dates', 'independent-analytics'), 'relativeDates' => \__('Apply Relative Dates', 'independent-analytics'), 'copied' => \__('Copied', 'independent-analytics'), 'exportingPages' => \__('Exporting Pages...', 'independent-analytics'), 'exportPages' => \__('Export Pages', 'independent-analytics'), 'exportingReferrers' => \__('Exporting Referrers...', 'independent-analytics'), 'exportReferrers' => \__('Export Referrers', 'independent-analytics'), 'exportingGeolocations' => \__('Exporting Geolocations...', 'independent-analytics'), 'exportGeolocations' => \__('Export Geolocations', 'independent-analytics'), 'exportingDevices' => \__('Exporting Devices...', 'independent-analytics'), 'exportDevices' => \__('Export Devices', 'independent-analytics'), 'exportingCampaigns' => \__('Exporting Campaigns...', 'independent-analytics'), 'exportCampaigns' => \__('Export Campaigns', 'independent-analytics'), 'invalidReportArchive' => \__('This report archive is invalid. Please export your reports and try again.', 'independent-analytics'), 'openMobileMenu' => \__('Open menu', 'independent-analytics'), 'closeMobileMenu' => \__('Close menu', 'independent-analytics')]), 'before'); |
| 224 | } |
| 225 | public function enqueue_nonces() |
| 226 | { |
| 227 | \wp_register_script('iawp-nonces', ''); |
| 228 | \wp_enqueue_script('iawp-nonces'); |
| 229 | \wp_add_inline_script('iawp-nonces', 'const iawpActions = ' . \json_encode(AJAX_Manager::getInstance()->get_action_signatures()), 'before'); |
| 230 | } |
| 231 | public function get_option($name, $default) |
| 232 | { |
| 233 | $option = \get_option($name, $default); |
| 234 | return $option === '' ? $default : $option; |
| 235 | } |
| 236 | public function date_i18n(string $format, \DateTime $date) : string |
| 237 | { |
| 238 | return \date_i18n($format, $date->setTimezone(Timezone::site_timezone())->getTimestamp() + Timezone::site_offset_in_seconds()); |
| 239 | } |
| 240 | public function plugin_action_links($links) |
| 241 | { |
| 242 | // Create the link |
| 243 | $settings_link = '<a class="calendar-link" href="' . \esc_url(\IAWPSCOPED\iawp_dashboard_url()) . '">' . \esc_html__('Analytics Dashboard', 'independent-analytics') . '</a>'; |
| 244 | // Add the link to the start of the array |
| 245 | \array_unshift($links, $settings_link); |
| 246 | return $links; |
| 247 | } |
| 248 | public function pagination_page_size() |
| 249 | { |
| 250 | return 50; |
| 251 | } |
| 252 | public function dequeue_bad_actors() |
| 253 | { |
| 254 | // https://wordpress.org/plugins/comment-link-remove/ |
| 255 | \wp_dequeue_style('qc_clr_admin_style_css'); |
| 256 | // https://wordpress.org/plugins/webappick-pdf-invoice-for-woocommerce/ |
| 257 | \wp_dequeue_style('woo-invoice'); |
| 258 | // https://wordpress.org/plugins/wp-media-files-name-rename/ |
| 259 | \wp_dequeue_style('wpcmp_bootstrap_css'); |
| 260 | // https://wordpress.org/plugins/morepuzzles/ |
| 261 | \wp_dequeue_style('bscss'); |
| 262 | \wp_dequeue_style('mypluginstyle'); |
| 263 | } |
| 264 | public function maybe_override_adminify_styles() |
| 265 | { |
| 266 | if (\is_plugin_active('adminify/adminify.php')) { |
| 267 | $settings = \get_option('_wpadminify'); |
| 268 | if ($settings) { |
| 269 | if (\array_key_exists('admin_ui', $settings)) { |
| 270 | if ($settings['admin_ui']) { |
| 271 | \wp_register_style('iawp-adminify-styles', \IAWPSCOPED\iawp_url_to('dist/styles/adminify.css'), [], \IAWP_VERSION); |
| 272 | \wp_enqueue_style('iawp-adminify-styles'); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | public function changelog_viewed_since_update() : bool |
| 279 | { |
| 280 | if (\number_format(\floatval(\IAWP_VERSION), 1) > \floatval($this->get_option('iawp_last_update_viewed', '0'))) { |
| 281 | return \true; |
| 282 | } |
| 283 | return \false; |
| 284 | } |
| 285 | public function is_form_submission_support_enabled() : bool |
| 286 | { |
| 287 | if (!\is_bool($this->is_form_submission_support_enabled)) { |
| 288 | $this->is_form_submission_support_enabled = \IAWPSCOPED\iawp_is_pro() && Form::has_active_form_plugin() && \IAWP\Capability_Manager::can_view_all_analytics(); |
| 289 | } |
| 290 | return $this->is_form_submission_support_enabled; |
| 291 | } |
| 292 | public function is_woocommerce_support_enabled() : bool |
| 293 | { |
| 294 | if (!\is_bool($this->is_woocommerce_support_enabled)) { |
| 295 | $this->is_woocommerce_support_enabled = $this->actually_check_if_woocommerce_support_is_enabled(); |
| 296 | } |
| 297 | return $this->is_woocommerce_support_enabled; |
| 298 | } |
| 299 | public function is_surecart_support_enabled() : bool |
| 300 | { |
| 301 | if (!\is_bool($this->is_surecart_support_enabled)) { |
| 302 | $this->is_surecart_support_enabled = $this->actually_check_if_surecart_support_is_enabled(); |
| 303 | } |
| 304 | return $this->is_surecart_support_enabled; |
| 305 | } |
| 306 | public function is_ecommerce_support_enabled() : bool |
| 307 | { |
| 308 | return $this->is_woocommerce_support_enabled() || $this->is_surecart_support_enabled(); |
| 309 | } |
| 310 | private function actually_check_if_woocommerce_support_is_enabled() : bool |
| 311 | { |
| 312 | global $wpdb; |
| 313 | if (\IAWPSCOPED\iawp_is_free()) { |
| 314 | return \false; |
| 315 | } |
| 316 | if (\IAWP\Capability_Manager::can_only_view_authored_analytics()) { |
| 317 | return \false; |
| 318 | } |
| 319 | if (!\is_plugin_active('woocommerce/woocommerce.php')) { |
| 320 | return \false; |
| 321 | } |
| 322 | $table_name = $wpdb->prefix . 'wc_order_stats'; |
| 323 | $order_stats_table = $wpdb->get_row($wpdb->prepare(' |
| 324 | SELECT * FROM INFORMATION_SCHEMA.TABLES |
| 325 | WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s |
| 326 | ', $wpdb->dbname, $table_name)); |
| 327 | if (\is_null($order_stats_table)) { |
| 328 | return \false; |
| 329 | } |
| 330 | return \true; |
| 331 | } |
| 332 | private function actually_check_if_surecart_support_is_enabled() : bool |
| 333 | { |
| 334 | if (\IAWPSCOPED\iawp_is_free()) { |
| 335 | return \false; |
| 336 | } |
| 337 | return \is_plugin_active('surecart/surecart.php'); |
| 338 | } |
| 339 | } |
| 340 |