PluginProbe
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) / trunk
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) vtrunk
9.1.2 9.1.1 9.1.0 9.0.2 9.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 All 153 releases
wp-analytify / inc / analytics-reports.php

analytics-reports.php in Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) trunk, at inc/analytics-reports.php

312 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File naming is acceptable
2 /**
3 * Analytics Reports Component for WP Analytify
4 *
5 * This file contains all analytics report generation and single post analytics functions
6 * that were previously in the main plugin file.
7 *
8 * @package WP_Analytify
9 * @since 8.0.0
10 */
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Analytics Reports Component Class
18 */
19 class Analytify_Analytics_Reports {
20
21 /**
22 * Main plugin instance
23 *
24 * @var WP_Analytify
25 */
26 private $analytify;
27
28 /**
29 * Constructor
30 *
31 * @version 7.0.5
32 * @param WP_Analytify $analytify Main plugin instance.
33 */
34 public function __construct( $analytify ) {
35 $this->analytify = $analytify;
36 $this->init_hooks();
37 }
38
39 /**
40 * Initialize hooks
41 *
42 * @return void
43 */
44 private function init_hooks() {
45 // Hooks are now managed by Analytify_Loader class.
46 // This prevents duplicate hook registration.
47 }
48
49
50 /**
51 * Show metabox under each Post type to display Analytics of single post/page in wp-admin.
52 *
53 * @return void
54 */
55 public function show_admin_single_analytics_add_metabox() {
56
57 // Return if disable post stats is on.
58 $disable_post_stats = $this->analytify && $this->analytify->settings ? $this->analytify->settings->get_option( 'enable_back_end', 'wp-analytify-admin', 'on' ) : 'on';
59 if ( 'on' !== $disable_post_stats ) {
60 return;
61 }
62
63 global $post;
64
65 if ( ! isset( $post ) ) {
66 return;
67 }
68 $display_draft_posts = apply_filters( 'analytify_filter_to_display_draft_posts', false );
69
70 // Don't show statistics on posts which are not published.
71 if ( 'publish' !== $post->post_status && ! $display_draft_posts ) {
72 return;
73 }
74
75 $post_types = $this->analytify && $this->analytify->settings ? $this->analytify->settings->get_option( 'show_analytics_post_types_back_end', 'wp-analytify-admin' ) : array();
76
77 // Don't load boxes/sections if no any post type is selected.
78 if ( ! empty( $post_types ) ) {
79 foreach ( $post_types as $post_type ) {
80 add_meta_box(
81 'pa-single-admin-analytics', // $id
82 __( 'Analytify - Stats of this Post/Page', 'wp-analytify' ), // $title.
83 array(
84 $this,
85 'show_admin_single_analytics',
86 ), // $callback
87 $post_type, // $posts
88 'normal', // $context
89 'high' // $priority
90 );
91 }
92 }
93 }
94
95
96 /**
97 * Show Analytics of single post/page in wp-admin under EDIT screen.
98 *
99 * @return void
100 */
101 public function show_admin_single_analytics() {
102 global $post;
103
104 $back_exclude_posts = false;
105 $_exclude_profile = get_option( 'wp-analytify-admin' );
106
107 if ( isset( $_exclude_profile['exclude_pages_back_end'] ) ) {
108 $back_exclude_posts = explode( ',', $_exclude_profile['exclude_pages_back_end'] );
109 }
110
111 if ( is_array( $back_exclude_posts ) ) {
112 if ( in_array( $post->ID, $back_exclude_posts, true ) ) {
113 esc_html_e( 'This post is excluded and will NOT show Analytics.', 'wp-analytify' );
114
115 return;
116 }
117 }
118
119 $url_post = '';
120 $url_post = wp_parse_url( get_permalink( $post->ID ) ? get_permalink( $post->ID ) : '' );
121
122 if ( get_the_time( 'Y', $post->ID ) < 2005 ) {
123 $start_date = '2005-01-01';
124 } else {
125 $start_date = (string) ( get_the_time( 'Y-m-d', $post->ID ) ? get_the_time( 'Y-m-d', $post->ID ) : '2005-01-01' );
126 }
127
128 $end_date = gmdate( 'Y-m-d' );
129 $is_access_level = $this->analytify && $this->analytify->settings ? $this->analytify->settings->get_option( 'show_analytics_roles_back_end', 'wp-analytify-admin' ) : array();
130
131 if ( $this->analytify && $this->analytify->pa_check_roles( $is_access_level ) ) { ?>
132
133 <div class="analytify_setting analytify_wraper">
134 <div class="analytify_select_date analytify_select_date_single_page">
135
136 <?php WPANALYTIFY_Utils::date_form( (string) $start_date, (string) $end_date, array( 'input_submit_id' => 'view_analytics' ) ); ?>
137 <?php do_action( 'after_single_view_stats_buttons' ); ?>
138 </div>
139 </div>
140
141 <div class="show-hide">
142 <?php $this->get_single_admin_analytics( $start_date, $end_date, $post->ID, 0 ); ?>
143 </div>
144
145 <?php
146 } else {
147 esc_html_e( 'You are not allowed to see stats', 'wp-analytify' );
148 }
149 }
150
151 /**
152 * Get the Analytics data for wp-admin posts/pages.
153 *
154 * @param string $start_date Start date.
155 * @param string $end_date End date.
156 * @param int $post_id Post ID.
157 * @param int $ajax AJAX flag.
158 * @return void
159 */
160 public function get_single_admin_analytics( $start_date = '', $end_date = '', $post_id = 0, $ajax = 0 ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Parameters required for backwards compatibility
161 // This method is deprecated, moved to rest.
162 }
163
164 /**
165 * Get the Analytics data from ajax() call
166 *
167 * @return void
168 */
169 public function get_ajax_single_admin_analytics() {
170 // This method is deprecated, moved to rest.
171 }
172
173 /**
174 * Set module state
175 *
176 * @return void
177 */
178 public function set_module_state() {
179 $analytify_modules = get_option( 'wp_analytify_modules' );
180 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Validation and sanitization handled below
181 $module_slug = isset( $_POST['module_slug'] ) ? sanitize_text_field( wp_unslash( $_POST['module_slug'] ) ) : '';
182 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Validation and sanitization handled below
183 $set_state = isset( $_POST['set_state'] ) ? sanitize_text_field( wp_unslash( $_POST['set_state'] ) ) : '';
184 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Validation and sanitization handled below
185 $internal_module = isset( $_POST['internal_module'] ) ? sanitize_text_field( wp_unslash( $_POST['internal_module'] ) ) : '';
186 $return = 'success';
187
188 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce verification handles security
189 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'addons' ) ) {
190 echo esc_html__( 'Failed', 'wp-analytify' );
191 wp_die();
192 }
193
194 if ( 'true' === $internal_module ) {
195 if ( ! current_user_can( 'manage_options' ) ) {
196 echo esc_html__( 'Failed', 'wp-analytify' );
197 wp_die();
198 }
199
200 // Internal module - update option.
201 if ( ! is_array( $analytify_modules ) ) {
202 $analytify_modules = array();
203 }
204
205 if ( ! isset( $analytify_modules[ $module_slug ] ) ) {
206 echo esc_html__( 'Failed', 'wp-analytify' );
207 wp_die();
208 }
209
210 if ( 'active' === $set_state && 'pixels-tracking' === $module_slug
211 && function_exists( 'wp_analytify_pro_pixels_tracking_module_file_exists' )
212 && class_exists( 'WP_Analytify_Pro', false )
213 && ! wp_analytify_pro_pixels_tracking_module_file_exists()
214 ) {
215 echo esc_html__( 'Failed', 'wp-analytify' );
216 wp_die();
217 }
218
219 if ( 'active' === $set_state ) {
220 $analytify_modules[ $module_slug ]['status'] = 'active';
221 } else {
222 $analytify_modules[ $module_slug ]['status'] = false;
223 }
224
225 update_option( 'wp_analytify_modules', $analytify_modules );
226 } else {
227 // External plugin - activate/deactivate plugin file.
228 // Restore WP core's activate_plugins gate (plugins.php enforces this).
229 if ( ! current_user_can( 'activate_plugins' ) ) {
230 echo esc_html__( 'Failed', 'wp-analytify' );
231 wp_die();
232 }
233
234 // $module_slug should be in format: plugin-folder/plugin-file.php.
235 // Check if plugin exists in installed plugins.
236 if ( ! function_exists( 'get_plugins' ) ) {
237 require_once ABSPATH . 'wp-admin/includes/plugin.php';
238 }
239
240 $all_plugins = get_plugins();
241 $plugin_found = false;
242
243 // Check if slug matches any installed plugin.
244 foreach ( $all_plugins as $plugin_file => $plugin_data ) {
245 if ( $plugin_file === $module_slug ) {
246 $plugin_found = true;
247 break;
248 }
249 }
250
251 if ( ! $plugin_found ) {
252 echo esc_html__( 'Failed', 'wp-analytify' );
253 wp_die();
254 }
255
256 if ( 'active' === $set_state ) {
257 $plugin_change_state = activate_plugin( $module_slug );
258 } else {
259 $plugin_change_state = deactivate_plugins( $module_slug );
260 }
261
262 // Error in response.
263 if ( is_wp_error( $plugin_change_state ) || ! empty( $plugin_change_state ) ) {
264 $return = 'failed';
265 }
266 }
267
268 echo esc_html( $return );
269 wp_die();
270 }
271
272 /**
273 * Add analytics stats to post row actions
274 *
275 * @param array<string, mixed> $actions Post actions.
276 * @param WP_Post $post Post object.
277 * @return array<string, mixed>
278 */
279 public function post_rows_stats( $actions, $post ) {
280 // Return if disable post stats is on.
281 $disable_post_stats = $this->analytify && $this->analytify->settings ? $this->analytify->settings->get_option( 'enable_back_end', 'wp-analytify-admin', 'on' ) : 'on';
282 if ( 'on' !== $disable_post_stats ) {
283 return $actions;
284 }
285 $display_draft_posts = apply_filters( 'analytify_filter_to_display_draft_posts', false );
286
287 if ( 'publish' === $post->post_status || true === $display_draft_posts ) {
288 $actions['post_row_stats'] = '<a href="' . admin_url( 'post.php?post=' . (string) $post->ID . '&action=edit#pa-single-admin-analytics' ) . '" title="' . esc_attr__( 'View Stats of', 'wp-analytify' ) . ' "' . esc_attr( get_the_title( $post ) ) . '"">Stats</a>';
289 }
290
291 return $actions;
292 }
293
294 /**
295 * Add analytics stats to post submit box
296 *
297 * @param WP_Post $post Post object.
298 * @return void
299 */
300 public function post_submitbox_stats_action( $post ) {
301
302 $display_draft_posts = apply_filters( 'analytify_filter_to_display_draft_posts', false );
303
304 // Check if the post is published or if the filter allows displaying draft posts.
305 if ( 'publish' === $post->post_status || $display_draft_posts ) {
306 if ( in_array( $post->post_type, $this->analytify && $this->analytify->settings ? $this->analytify->settings->get_option( 'show_analytics_post_types_back_end', 'wp-analytify-admin', array() ) : array(), true ) ) {
307 echo '<a id="view_stats_analytify" href="' . esc_url( admin_url( 'post.php?post=' . $post->ID . '&action=edit#pa-single-admin-analytics' ) ) . '" title="' . esc_attr__( 'View Stats of', 'wp-analytify' ) . ' "' . esc_attr( get_the_title( $post ) ) . '”" class="button button-primary button-large" style="float:left">' . esc_html__( 'View Stats', 'wp-analytify' ) . '</a>';
308 }
309 }
310 }
311 }
312