AJAX
2 years ago
Admin_Page
2 years ago
Date_Range
2 years ago
Interval
2 years ago
Menu_Bar_Stats
2 years ago
Migrations
2 years ago
Models
2 years ago
Public_API
2 years ago
Rows
2 years ago
Statistics
2 years ago
Tables
2 years ago
Utils
2 years ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
2 years ago
Chart.php
2 years ago
Chart_Geo.php
2 years 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_Manager.php
2 years ago
Email_Chart.php
2 years ago
Email_Reports.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
2 years ago
Filters.php
2 years ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.php
2 years ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
2 years ago
Independent_Analytics.php
2 years ago
Interrupt.php
2 years ago
Known_Referrers.php
2 years ago
Plugin_Conflict_Detector.php
2 years ago
Query.php
2 years ago
Quick_Stats.php
2 years ago
REST_API.php
2 years ago
Real_Time.php
2 years ago
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
2 years ago
Resource_Identifier.php
2 years ago
Settings.php
2 years ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
2 years ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
WooCommerce_Order.php
2 years ago
WooCommerce_Referrer_Meta_Box.php
2 years ago
Independent_Analytics.php
258 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP; |
| 4 | |
| 5 | use IAWP_SCOPED\IAWP\Admin_Page\Analytics_Page; |
| 6 | use IAWP_SCOPED\IAWP\Admin_Page\Campaign_Builder_Page; |
| 7 | use IAWP_SCOPED\IAWP\Admin_Page\Settings_Page; |
| 8 | use IAWP_SCOPED\IAWP\AJAX\AJAX_Manager; |
| 9 | use IAWP_SCOPED\IAWP\Menu_Bar_Stats\Menu_Bar_Stats; |
| 10 | use IAWP_SCOPED\IAWP\Migrations\Migrations; |
| 11 | use IAWP_SCOPED\IAWP\Utils\Singleton; |
| 12 | /** @internal */ |
| 13 | class Independent_Analytics |
| 14 | { |
| 15 | use Singleton; |
| 16 | public $settings; |
| 17 | public $email_reports; |
| 18 | public $cron_manager; |
| 19 | // This is where we attach functions to WP hooks |
| 20 | private function __construct() |
| 21 | { |
| 22 | $this->settings = new Settings(); |
| 23 | new REST_API(); |
| 24 | new Dashboard_Widget(); |
| 25 | new View_Counter(); |
| 26 | AJAX_Manager::getInstance(); |
| 27 | if (!Migrations::is_migrating()) { |
| 28 | new Track_Resource_Changes(); |
| 29 | Menu_Bar_Stats::register(); |
| 30 | WooCommerce_Order::initialize_order_tracker(); |
| 31 | } |
| 32 | $this->cron_manager = new Cron_Manager(); |
| 33 | if (\IAWP_SCOPED\iawp_is_pro()) { |
| 34 | $this->email_reports = new Email_Reports(); |
| 35 | new Campaign_Builder(); |
| 36 | new WooCommerce_Referrer_Meta_Box(); |
| 37 | } |
| 38 | \add_filter('admin_body_class', function ($classes) { |
| 39 | if (\get_option('iawp_dark_mode')) { |
| 40 | $classes .= ' iawp-dark-mode '; |
| 41 | } |
| 42 | return $classes; |
| 43 | }); |
| 44 | \add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts_and_styles'], 20); |
| 45 | // Called at 20 to dequeue other scripts |
| 46 | \add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts_and_styles_front_end']); |
| 47 | \add_action('admin_menu', [$this, 'add_admin_menu_pages']); |
| 48 | \add_filter('plugin_action_links_independent-analytics/iawp.php', [$this, 'plugin_action_links']); |
| 49 | \add_filter('admin_footer_text', [$this, 'ip_db_attribution'], 1, 1); |
| 50 | \add_filter('admin_head', [$this, 'style_premium_menu_item']); |
| 51 | \add_action('init', [$this, 'polylang_translations']); |
| 52 | \add_action('init', [$this, 'load_textdomain']); |
| 53 | IAWP_FS()->add_filter('connect_message_on_update', [$this, 'filter_connect_message_on_update'], 10, 6); |
| 54 | IAWP_FS()->add_filter('connect_message', [$this, 'filter_connect_message_on_update'], 10, 6); |
| 55 | IAWP_FS()->add_filter('is_submenu_visible', [$this, 'hide_freemius_sub_menus'], 10, 2); |
| 56 | IAWP_FS()->add_filter('pricing_url', [$this, 'change_freemius_pricing_url'], 10); |
| 57 | IAWP_FS()->add_filter('show_deactivation_feedback_form', function () { |
| 58 | return \false; |
| 59 | }); |
| 60 | \add_action('admin_init', [$this, 'maybe_delete_mu_plugin']); |
| 61 | } |
| 62 | /** |
| 63 | * At one point in time, there was a must-use plugin that was created. The plugin file and the |
| 64 | * option need to get cleaned up. |
| 65 | * @return void |
| 66 | */ |
| 67 | public function maybe_delete_mu_plugin() |
| 68 | { |
| 69 | $already_attempted = \get_option('iawp_attempted_to_delete_mu_plugin', '0'); |
| 70 | if ($already_attempted === '1') { |
| 71 | return; |
| 72 | } |
| 73 | if (\get_option('iawp_must_use_directory_not_writable', '0') === '1') { |
| 74 | \delete_option('iawp_must_use_directory_not_writable'); |
| 75 | } |
| 76 | $mu_plugin_file = \trailingslashit(\WPMU_PLUGIN_DIR) . 'iawp-performance-boost.php'; |
| 77 | if (\file_exists($mu_plugin_file)) { |
| 78 | \unlink($mu_plugin_file); |
| 79 | } |
| 80 | \update_option('iawp_attempted_to_delete_mu_plugin', '1'); |
| 81 | } |
| 82 | public function load_textdomain() |
| 83 | { |
| 84 | \load_plugin_textdomain('independent-analytics', \false, \IAWP_LANGUAGES_DIRECTORY); |
| 85 | } |
| 86 | public function polylang_translations() |
| 87 | { |
| 88 | if (\function_exists('IAWP_SCOPED\\pll_register_string')) { |
| 89 | pll_register_string('view_counter', 'Views:', 'Independent Analytics'); |
| 90 | } |
| 91 | } |
| 92 | public function hide_freemius_sub_menus($is_visible, $menu_id) |
| 93 | { |
| 94 | if ('pricing' === $menu_id) { |
| 95 | return \false; |
| 96 | } elseif ('support' === $menu_id && Capability_Manager::white_labeled()) { |
| 97 | return \false; |
| 98 | } else { |
| 99 | return \true; |
| 100 | } |
| 101 | } |
| 102 | public function change_freemius_pricing_url() |
| 103 | { |
| 104 | return 'https://independentwp.com/pricing/?utm_source=User+Dashboard&utm_medium=WP+Admin&utm_campaign=Upgrade+to+Pro&utm_content=Account'; |
| 105 | } |
| 106 | // The submenu item needs to be styled on all admin pages, and we only load stylesheets on our page |
| 107 | public function style_premium_menu_item() |
| 108 | { |
| 109 | if (\IAWP_SCOPED\iawp_is_free()) { |
| 110 | echo '<style>#toplevel_page_independent-analytics .wp-submenu li:nth-child(5) a { color: #F69D0A; }</style>'; |
| 111 | } |
| 112 | } |
| 113 | public function add_admin_menu_pages() |
| 114 | { |
| 115 | $title = Capability_Manager::white_labeled() ? \esc_html__('Analytics', 'independent-analytics') : 'Independent Analytics'; |
| 116 | \add_menu_page($title, \esc_html__('Analytics', 'independent-analytics'), Capability_Manager::can_view_string(), 'independent-analytics', function () { |
| 117 | $analytics_page = new Analytics_Page(); |
| 118 | $analytics_page->render(); |
| 119 | }, 'dashicons-analytics', 3); |
| 120 | if (Capability_Manager::can_edit()) { |
| 121 | \add_submenu_page('independent-analytics', \esc_html__('Settings', 'independent-analytics'), \esc_html__('Settings', 'independent-analytics'), Capability_Manager::can_view_string(), 'independent-analytics-settings', function () { |
| 122 | $settings_page = new Settings_Page(); |
| 123 | $settings_page->render(); |
| 124 | }); |
| 125 | } |
| 126 | if (\IAWP_SCOPED\iawp_is_pro()) { |
| 127 | \add_submenu_page('independent-analytics', \esc_html__('Campaign Builder', 'independent-analytics'), \esc_html__('Campaign Builder', 'independent-analytics'), Capability_Manager::can_view_string(), 'independent-analytics-campaign-builder', function () { |
| 128 | $campaign_builder_page = new Campaign_Builder_Page(); |
| 129 | $campaign_builder_page->render(); |
| 130 | }); |
| 131 | } |
| 132 | if (!Capability_Manager::white_labeled()) { |
| 133 | \add_submenu_page('independent-analytics', \esc_html__('Feedback', 'independent-analytics'), \esc_html__('Feedback', 'independent-analytics'), Capability_Manager::can_view_string(), \esc_url('https://feedback.independentwp.com/boards/feature-requests')); |
| 134 | } |
| 135 | if (\IAWP_SCOPED\iawp_is_free() && !Capability_Manager::white_labeled()) { |
| 136 | \add_submenu_page('independent-analytics', \esc_html__('Upgrade to Pro →', 'independent-analytics'), \esc_html__('Upgrade to Pro →', 'independent-analytics'), Capability_Manager::can_view_string(), \esc_url('https://independentwp.com/pricing/?utm_source=User+Dashboard&utm_medium=WP+Admin&utm_campaign=Upgrade+to+Pro&utm_content=Sidebar')); |
| 137 | } |
| 138 | } |
| 139 | public function register_scripts_and_styles() : void |
| 140 | { |
| 141 | \wp_register_style('iawp-styles', \IAWP_SCOPED\iawp_url_to('dist/styles/style.css'), [], \IAWP_VERSION); |
| 142 | \wp_register_style('iawp-dashboard-widget-styles', \IAWP_SCOPED\iawp_url_to('dist/styles/dashboard_widget.css'), [], \IAWP_VERSION); |
| 143 | \wp_register_style('iawp-freemius-notice-styles', \IAWP_SCOPED\iawp_url_to('dist/styles/freemius_notice_styles.css'), [], \IAWP_VERSION); |
| 144 | \wp_register_script('iawp-javascript', \IAWP_SCOPED\iawp_url_to('dist/js/index.js'), [], \IAWP_VERSION); |
| 145 | \wp_register_script('iawp-dashboard-widget-javascript', \IAWP_SCOPED\iawp_url_to('dist/js/dashboard_widget.js'), [], \IAWP_VERSION); |
| 146 | \wp_register_script('iawp-layout-javascript', \IAWP_SCOPED\iawp_url_to('dist/js/layout.js'), [], \IAWP_VERSION); |
| 147 | \wp_register_script('iawp-settings-javascript', \IAWP_SCOPED\iawp_url_to('dist/js/settings.js'), ['wp-color-picker'], \IAWP_VERSION); |
| 148 | if (Menu_Bar_Stats::is_option_enabled()) { |
| 149 | \wp_register_style('iawp-front-end-styles', \IAWP_SCOPED\iawp_url_to('dist/styles/menu_bar_stats.css'), [], \IAWP_VERSION); |
| 150 | } |
| 151 | if (\is_rtl()) { |
| 152 | \wp_register_style('iawp-styles-rtl', \IAWP_SCOPED\iawp_url_to('dist/styles/rtl.css'), [], \IAWP_VERSION); |
| 153 | } |
| 154 | } |
| 155 | public function register_scripts_and_styles_front_end() : void |
| 156 | { |
| 157 | if (Menu_Bar_Stats::is_option_enabled()) { |
| 158 | \wp_register_style('iawp-front-end-styles', \IAWP_SCOPED\iawp_url_to('dist/styles/menu_bar_stats.css'), [], \IAWP_VERSION); |
| 159 | } |
| 160 | } |
| 161 | public function enqueue_scripts_and_styles($hook) |
| 162 | { |
| 163 | $this->register_scripts_and_styles(); |
| 164 | $page = Env::get_page(); |
| 165 | $this->enqueue_translations(); |
| 166 | $this->enqueue_nonces(); |
| 167 | \wp_enqueue_style('iawp-freemius-notice-styles'); |
| 168 | if (\is_string($page)) { |
| 169 | \wp_enqueue_style('iawp-styles'); |
| 170 | \wp_enqueue_script('iawp-javascript'); |
| 171 | \wp_enqueue_script('iawp-layout-javascript'); |
| 172 | $this->dequeue_bad_actors(); |
| 173 | if (\is_rtl()) { |
| 174 | \wp_enqueue_style('iawp-styles-rtl'); |
| 175 | } |
| 176 | } |
| 177 | if ($page === 'independent-analytics-settings') { |
| 178 | \wp_enqueue_style('wp-color-picker'); |
| 179 | \wp_enqueue_script('iawp-settings-javascript'); |
| 180 | } elseif ($hook === 'index.php') { |
| 181 | \wp_enqueue_script('iawp-dashboard-widget-javascript'); |
| 182 | \wp_enqueue_style('iawp-dashboard-widget-styles'); |
| 183 | } |
| 184 | if (Menu_Bar_Stats::is_option_enabled()) { |
| 185 | \wp_enqueue_style('iawp-front-end-styles'); |
| 186 | } |
| 187 | } |
| 188 | public function enqueue_scripts_and_styles_front_end() |
| 189 | { |
| 190 | if (Menu_Bar_Stats::is_option_enabled()) { |
| 191 | $this->register_scripts_and_styles_front_end(); |
| 192 | \wp_enqueue_style('iawp-front-end-styles'); |
| 193 | } |
| 194 | } |
| 195 | public function enqueue_translations() |
| 196 | { |
| 197 | \wp_register_script('iawp-translations', ''); |
| 198 | \wp_enqueue_script('iawp-translations'); |
| 199 | \wp_add_inline_script('iawp-translations', 'const iawpText = ' . \json_encode(['visitors' => \__('Visitors', 'independent-analytics'), 'views' => \__('Views', 'independent-analytics'), 'sessions' => \__('Sessions', 'independent-analytics'), 'orders' => \__('Orders', 'independent-analytics'), 'netSales' => \__('Net Sales', 'independent-analytics'), 'country' => \__('country', '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'); |
| 200 | } |
| 201 | public function enqueue_nonces() |
| 202 | { |
| 203 | \wp_register_script('iawp-nonces', ''); |
| 204 | \wp_enqueue_script('iawp-nonces'); |
| 205 | \wp_add_inline_script('iawp-nonces', 'const iawpActions = ' . \json_encode(AJAX_Manager::getInstance()->get_action_signatures()), 'before'); |
| 206 | } |
| 207 | public function get_option($name, $default) |
| 208 | { |
| 209 | $option = \get_option($name, $default); |
| 210 | return $option === '' ? $default : $option; |
| 211 | } |
| 212 | public function filter_connect_message_on_update($message, $user_first_name, $product_title, $user_login, $site_link, $freemius_link) |
| 213 | { |
| 214 | // Add the heading HTML. |
| 215 | $plugin_name = 'Independent Analytics'; |
| 216 | $title = '<h3>' . \sprintf(\esc_html__('We hope you love %1$s', 'independent-analytics'), $plugin_name) . '</h3>'; |
| 217 | $html = ''; |
| 218 | // Add the introduction HTML. |
| 219 | $html .= '<p>'; |
| 220 | $html .= \sprintf(\esc_html__('Hi, %1$s! This is an invitation to help the %2$s community.', 'independent-analytics'), $user_first_name, $plugin_name); |
| 221 | $html .= '<strong>'; |
| 222 | $html .= \sprintf(\esc_html__('If you opt-in, some data about your usage of %2$s will be shared with us', 'independent-analytics'), $user_first_name, $plugin_name); |
| 223 | $html .= '</strong>'; |
| 224 | $html .= \sprintf(\esc_html__(' so we can improve %2$s. We will also share some helpful info on using the plugin so you can get the most out of your sites analytics.', 'independent-analytics'), $user_first_name, $plugin_name); |
| 225 | $html .= '</p>'; |
| 226 | $html .= '<p>'; |
| 227 | $html .= \sprintf(\esc_html__('And if you skip this, that\'s okay! %1$s will still work just fine.', 'independent-analytics'), $plugin_name); |
| 228 | $html .= '</p>'; |
| 229 | return $title . $html; |
| 230 | } |
| 231 | public function plugin_action_links($links) |
| 232 | { |
| 233 | // Create the link |
| 234 | $settings_link = '<a class="calendar-link" href="' . \esc_url(\IAWP_SCOPED\iawp_dashboard_url()) . '">' . \esc_html__('Analytics Dashboard', 'independent-analytics') . '</a>'; |
| 235 | // Add the link to the start of the array |
| 236 | \array_unshift($links, $settings_link); |
| 237 | return $links; |
| 238 | } |
| 239 | public function ip_db_attribution($text) |
| 240 | { |
| 241 | if (Env::get_tab() === 'geo') { |
| 242 | $text = $text . ' ' . \esc_html_x('Geolocation data powered by', 'Following text is a noun: DB-IP', 'independent-analytics') . ' ' . '<a href="https://db-ip.com" class="geo-message" target="_blank">DB-IP</a>.'; |
| 243 | } |
| 244 | return $text; |
| 245 | } |
| 246 | public function pagination_page_size() |
| 247 | { |
| 248 | return 50; |
| 249 | } |
| 250 | public function dequeue_bad_actors() |
| 251 | { |
| 252 | // https://wordpress.org/plugins/comment-link-remove/ |
| 253 | \wp_dequeue_style('qc_clr_admin_style_css'); |
| 254 | // https://wordpress.org/plugins/webappick-pdf-invoice-for-woocommerce/ |
| 255 | \wp_dequeue_style('woo-invoice'); |
| 256 | } |
| 257 | } |
| 258 |