Admin_Page.php
11 months ago
Analytics_Page.php
11 months ago
Campaign_Builder_Page.php
2 years ago
Click_Tracking_Page.php
1 year ago
Debug_Page.php
1 year ago
Integrations_Pages.php
1 year ago
Settings_Page.php
2 years ago
Support_Page.php
1 year ago
Updates_Page.php
1 year ago
Analytics_Page.php
238 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Admin_Page; |
| 4 | |
| 5 | use IAWP\Capability_Manager; |
| 6 | use IAWP\Chart; |
| 7 | use IAWP\Dashboard_Options; |
| 8 | use IAWP\Database; |
| 9 | use IAWP\Env; |
| 10 | use IAWP\Examiner\Header; |
| 11 | use IAWP\Map; |
| 12 | use IAWP\Map_Data; |
| 13 | use IAWP\Overview\Overview; |
| 14 | use IAWP\Plugin_Conflict_Detector; |
| 15 | use IAWP\Quick_Stats; |
| 16 | use IAWP\Real_Time; |
| 17 | use IAWP\Report; |
| 18 | use IAWP\Report_Finder; |
| 19 | use IAWP\Tables\Table; |
| 20 | use IAWP\Utils\Request; |
| 21 | use IAWP\Utils\Security; |
| 22 | use IAWPSCOPED\Illuminate\Support\Arr; |
| 23 | use IAWPSCOPED\Illuminate\Support\Collection; |
| 24 | /** @internal */ |
| 25 | class Analytics_Page extends \IAWP\Admin_Page\Admin_Page |
| 26 | { |
| 27 | protected function render_page() |
| 28 | { |
| 29 | $options = Dashboard_Options::getInstance(); |
| 30 | $date_rage = $options->get_date_range(); |
| 31 | $tab = (new Env())->get_tab(); |
| 32 | $report = Report_Finder::new()->fetch_current_report(); |
| 33 | $is_showing_skeleton_ui = $report instanceof Report && $report->has_filters(); |
| 34 | // Real-time is its own thing |
| 35 | if ($tab === 'real-time') { |
| 36 | $real_time = new Real_Time(); |
| 37 | $real_time->render_real_time_analytics(); |
| 38 | return; |
| 39 | } |
| 40 | // Overview is its own thing |
| 41 | if ($tab === 'overview') { |
| 42 | $overview = new Overview(); |
| 43 | echo $overview->get_report_html(); |
| 44 | $this->notices(); |
| 45 | return; |
| 46 | } |
| 47 | $table_class = Env::get_table($tab); |
| 48 | $table = new $table_class($options->group()); |
| 49 | $sort_configuration = $table->sanitize_sort_parameters($options->sort_column(), $options->sort_direction()); |
| 50 | $hide_unfiltered_statistics = \false; |
| 51 | $rows = null; |
| 52 | $examiner_model = null; |
| 53 | if ($options->is_examiner()) { |
| 54 | $rows_class = $table->group()->rows_class(); |
| 55 | $rows = new $rows_class($options->get_date_range(), null, null, $sort_configuration); |
| 56 | $id = Request::query_int('examiner'); |
| 57 | $rows->limit_to($id); |
| 58 | $examiner_model = $rows->rows()[0]; |
| 59 | $hide_unfiltered_statistics = \true; |
| 60 | } |
| 61 | $statistics_class = $table->group()->statistics_class(); |
| 62 | $statistics = new $statistics_class($date_rage, $rows, $options->chart_interval()); |
| 63 | $stats = new Quick_Stats($statistics, \false, $is_showing_skeleton_ui, $hide_unfiltered_statistics); |
| 64 | // Never show the map when loading the geo examiner. It'll only ever show a single country anyway. |
| 65 | if ($tab === 'geo' && !$options->is_examiner()) { |
| 66 | $table_data_class = $table->group()->rows_class(); |
| 67 | $geo_data = new $table_data_class($date_rage); |
| 68 | $map_data = new Map_Data($geo_data->rows()); |
| 69 | $chart = new Map($map_data->get_country_data(), null, $is_showing_skeleton_ui); |
| 70 | } else { |
| 71 | $chart = new Chart($statistics, \false, $is_showing_skeleton_ui); |
| 72 | } |
| 73 | $this->interface($table, $stats, $chart, $examiner_model); |
| 74 | } |
| 75 | private function interface(Table $table, $stats, $chart, $examiner_model = null) |
| 76 | { |
| 77 | $options = Dashboard_Options::getInstance(); |
| 78 | $sort_configuration = $table->sanitize_sort_parameters($options->sort_column(), $options->sort_direction()); |
| 79 | $header = \IAWPSCOPED\iawp_blade()->run('partials.report-header', ['report' => Report_Finder::new()->fetch_current_report(), 'can_edit' => Capability_Manager::can_edit()]); |
| 80 | $examiner_tabs = ''; |
| 81 | if ($examiner_model) { |
| 82 | $header = Header::html($table, $examiner_model); |
| 83 | $available_tabs = Collection::make($this->tables())->filter(function (array $table) use($examiner_model) { |
| 84 | return $table['table_type'] !== $examiner_model->table_type(); |
| 85 | })->values()->all(); |
| 86 | $current = Arr::first($available_tabs); |
| 87 | $examiner_tabs = \IAWPSCOPED\iawp_blade()->run('examiner.table-tabs', ['tables' => $available_tabs, 'active' => $current['table_type']]); |
| 88 | $table_class = Env::get_table($current['table_type']); |
| 89 | $table = new $table_class(); |
| 90 | } |
| 91 | if ($options->is_examiner()) { |
| 92 | ?> |
| 93 | <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> |
| 94 | <?php |
| 95 | } |
| 96 | ?> |
| 97 | <div data-controller="report" |
| 98 | data-report-is-examiner-value="<?php |
| 99 | echo $options->is_examiner() ? '1' : '0'; |
| 100 | ?>" |
| 101 | data-report-name-value="<?php |
| 102 | echo Security::string($options->report_name()); |
| 103 | ?>" |
| 104 | data-report-report-name-value="<?php |
| 105 | echo Security::string($table->group()->singular()); |
| 106 | ?>" |
| 107 | data-report-relative-range-id-value="<?php |
| 108 | echo Security::attr($options->relative_range_id()); |
| 109 | ?>" |
| 110 | data-report-exact-start-value="<?php |
| 111 | echo Security::attr($options->start()); |
| 112 | ?>" |
| 113 | data-report-exact-end-value="<?php |
| 114 | echo Security::attr($options->end()); |
| 115 | ?>" |
| 116 | data-report-group-value="<?php |
| 117 | echo Security::attr($table->group()->id()); |
| 118 | ?>" |
| 119 | data-report-filters-value="<?php |
| 120 | echo \esc_attr(Security::json_encode($options->filters())); |
| 121 | ?>" |
| 122 | data-report-chart-interval-value="<?php |
| 123 | echo Security::attr($options->chart_interval()->id()); |
| 124 | ?>" |
| 125 | data-report-sort-column-value="<?php |
| 126 | echo Security::attr($sort_configuration->column()); |
| 127 | ?>" |
| 128 | data-report-sort-direction-value="<?php |
| 129 | echo Security::attr($sort_configuration->direction()); |
| 130 | ?>" |
| 131 | data-report-columns-value="<?php |
| 132 | echo \esc_attr(Security::json_encode($table->visible_column_ids())); |
| 133 | ?>" |
| 134 | data-report-quick-stats-value="<?php |
| 135 | echo \esc_attr(Security::json_encode($options->visible_quick_stats())); |
| 136 | ?>" |
| 137 | data-report-primary-chart-metric-id-value="<?php |
| 138 | echo \esc_attr($options->primary_chart_metric_id()); |
| 139 | ?>" |
| 140 | data-report-secondary-chart-metric-id-value="<?php |
| 141 | echo \esc_attr($options->secondary_chart_metric_id()); |
| 142 | ?>" |
| 143 | > |
| 144 | <div id="report-header-container" class="report-header-container"> |
| 145 | <?php |
| 146 | echo $header; |
| 147 | ?> |
| 148 | <?php |
| 149 | $table->output_report_toolbar(); |
| 150 | ?> |
| 151 | <div class="modal-background"></div> |
| 152 | </div> |
| 153 | <?php |
| 154 | echo $stats->get_html(); |
| 155 | ?> |
| 156 | <?php |
| 157 | echo $chart->get_html(); |
| 158 | ?> |
| 159 | <?php |
| 160 | echo $examiner_tabs; |
| 161 | ?> |
| 162 | <?php |
| 163 | echo $table->get_table_toolbar_markup(); |
| 164 | ?> |
| 165 | <?php |
| 166 | echo $table->get_table_markup($sort_configuration->column(), $sort_configuration->direction()); |
| 167 | ?> |
| 168 | </div> |
| 169 | <?php |
| 170 | if (!$options->is_examiner()) { |
| 171 | ?> |
| 172 | <div id="iawp-examiner-modal" aria-hidden="true" class="mm micromodal-slide" data-controller="examiner"> |
| 173 | <div tabindex="-1" class="mm__overlay mm__overlay--full-screen" data-action="click->examiner#close:self" > |
| 174 | <div role="dialog" aria-modal="true" class="mm__container examiner examiner--loading"> |
| 175 | <div data-examiner-target="content" class="examiner-content"></div> |
| 176 | <?php |
| 177 | echo \IAWPSCOPED\iawp_blade()->run('examiner.skeleton'); |
| 178 | ?> |
| 179 | </div> |
| 180 | </div> |
| 181 | </div> |
| 182 | <?php |
| 183 | } |
| 184 | ?> |
| 185 | <?php |
| 186 | if (Env::get_tab() === 'geo') { |
| 187 | echo '<div class="geo-ip-attribution">'; |
| 188 | echo \esc_html_x('Geolocation data powered by', 'Following text is a noun: DB-IP', 'independent-analytics') . ' ' . '<a href="https://db-ip.com" target="_blank">DB-IP</a>.'; |
| 189 | echo '</div>'; |
| 190 | } |
| 191 | $this->notices(); |
| 192 | } |
| 193 | private function notices() |
| 194 | { |
| 195 | $plugin_conflict_detector = new Plugin_Conflict_Detector(); |
| 196 | $requires_logged_in_tracking = $plugin_conflict_detector->plugin_requiring_logged_in_tracking(); |
| 197 | $show_logged_in_tracking_notice = $requires_logged_in_tracking && !\get_option('iawp_track_authenticated_users') && !\get_option('iawp_need_clear_cache') && !\get_option('iawp_logged_in_tracking_notice'); |
| 198 | ?><div class="iawp-notices"><?php |
| 199 | if (Capability_Manager::can_edit()) { |
| 200 | if ($plugin_conflict_detector->has_conflict()) { |
| 201 | echo \IAWPSCOPED\iawp_blade()->run('notices.notice', ['notice_text' => $plugin_conflict_detector->get_error(), 'button_text' => \false, 'id' => 'plugin-conflict', 'notice' => 'iawp-error', 'plugin' => $plugin_conflict_detector->get_plugin(), 'url' => 'https://independentwp.com/knowledgebase/tracking/secure-rest-api/']); |
| 202 | } elseif (\IAWPSCOPED\iawp_is_pro() && \is_plugin_active('better-wp-security/better-wp-security.php')) { |
| 203 | $settings = \get_option('itsec-storage'); |
| 204 | if (\array_key_exists('system-tweaks', $settings)) { |
| 205 | if (\array_key_exists('plugins_php', $settings['system-tweaks'])) { |
| 206 | if ($settings['system-tweaks']['plugins_php']) { |
| 207 | echo \IAWPSCOPED\iawp_blade()->run('notices.notice', ['notice_text' => \__('The "Solid Security" plugin is disabling PHP execution in the plugins folder, and this is preventing click tracking from working. Please visit the Security > Settings page, click on the Advanced section, click on System Tweaks Settings, uncheck the "Disable PHP Plugins" option, and then save.', 'independent-analytics'), 'button_text' => \false, 'id' => 'plugin-conflict', 'notice' => 'iawp-error', 'url' => 'https://independentwp.com/knowledgebase/click-tracking/allow-php-execution-plugins-folder/']); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | if (\get_option('iawp_need_clear_cache')) { |
| 213 | echo \IAWPSCOPED\iawp_blade()->run('notices.notice', ['notice_text' => \__('Please clear your cache to ensure tracking works properly.', 'independent-analytics'), 'button_text' => \__('I\'ve cleared the cache', 'independent-analytics'), 'id' => 'iawp_need_clear_cache', 'notice' => 'iawp-warning', 'url' => 'https://independentwp.com/knowledgebase/common-questions/views-not-recording/']); |
| 214 | } |
| 215 | if ($show_logged_in_tracking_notice) { |
| 216 | echo \IAWPSCOPED\iawp_blade()->run('notices.notice', ['notice_text' => '<strong>' . \sprintf(\_x('%s compatibility:', 'Variable is the name of a plugin', 'independent-analytics'), $requires_logged_in_tracking) . '</strong> ' . \__('We recommend you enable tracking for logged-in visitors.', 'independent-analytics'), 'button_text' => \__('Dismiss', 'independent-analytics'), 'id' => 'enable-logged-in-tracking', 'notice' => 'iawp-warning', 'url' => 'https://independentwp.com/knowledgebase/tracking/how-to-track-logged-in-visitors/']); |
| 217 | } |
| 218 | if (\IAWPSCOPED\iawp_db_version() > 0 && !Database::has_correct_database_privileges()) { |
| 219 | echo \IAWPSCOPED\iawp_blade()->run('notices.notice', ['notice_text' => \__('Your site is missing the following critical database permissions:', 'independent-analytics') . ' ' . \implode(', ', Database::missing_database_privileges()) . '. ' . \__('There is no issue at this time, but you will need to enable the missing permissions before updating the plugin to a newer version to ensure an error is avoided. Please click this link to read our tutorial:', 'independent-analytics'), 'button_text' => \false, 'id' => 'missing-permissions', 'notice' => 'iawp-error', 'url' => 'https://independentwp.com/knowledgebase/common-questions/missing-database-permissions/']); |
| 220 | } |
| 221 | if (Env::get_tab() === 'clicks') { |
| 222 | if (!\get_option('iawp_clicks_sync_notice')) { |
| 223 | echo \IAWPSCOPED\iawp_blade()->run('notices.notice', ['notice_text' => \__('Click data syncs every 60 seconds. Please allow for this delay when testing clicks on new links.', 'independent-analytics'), 'button_text' => \__('Dismiss', 'independent-analytics'), 'id' => 'iawp_clicks_sync_notice', 'notice' => 'iawp-warning', 'url' => 'https://independentwp.com/knowledgebase/click-tracking/click-tracking-update-process/']); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | ?> |
| 228 | </div><?php |
| 229 | if (\get_option('iawp_show_gsg') === '1' && !\get_option('iawp_need_clear_cache') && !$show_logged_in_tracking_notice && !$plugin_conflict_detector->has_conflict() && (Env::get_tab() !== 'clicks' || Env::get_tab() === 'clicks' && \get_option('iawp_clicks_sync_notice'))) { |
| 230 | echo \IAWPSCOPED\iawp_blade()->run('notices.getting-started'); |
| 231 | } |
| 232 | } |
| 233 | private function tables() : array |
| 234 | { |
| 235 | return [['table_type' => 'views', 'name' => \__('Pages', 'independent-analytics')], ['table_type' => 'referrers', 'name' => \__('Referrers', 'independent-analytics')], ['table_type' => 'geo', 'name' => \__('Geographic', 'independent-analytics')], ['table_type' => 'devices', 'name' => \__('Devices', 'independent-analytics')], ['table_type' => 'campaigns', 'name' => \__('Campaigns', 'independent-analytics')], ['table_type' => 'clicks', 'name' => \__('Clicks', 'independent-analytics')]]; |
| 236 | } |
| 237 | } |
| 238 |