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 / class-analytify-loader.php

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

257 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Analytify Loader
4 *
5 * Loads all modular components and initializes them
6 *
7 * @package WP_ANALYTIFY
8 * @since 8.0.0
9 */
10
11 // Exit if accessed directly.
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Analytify Loader Class
18 *
19 * Responsible for loading and initializing all modular components
20 */
21 class Analytify_Loader {
22
23 /**
24 * Main plugin instance
25 *
26 * @var WP_Analytify
27 */
28 private $analytify;
29
30 /**
31 * Component instances
32 *
33 * @var array<string, mixed>
34 */
35 private $components = array();
36
37 /**
38 * Constructor
39 *
40 * @version 7.0.5
41 * @param WP_Analytify $analytify Main plugin instance.
42 */
43 public function __construct( $analytify ) {
44 $this->analytify = $analytify;
45 $this->load_components();
46 $this->init_components();
47 }
48
49 /**
50 * Load all component files
51 *
52 * @version 7.0.5
53 * @return void
54 */
55 private function load_components() {
56 $component_files = array(
57 'admin-notices',
58 'analytics-accounts',
59 'analytics-reports',
60 'profile-management',
61 'utils',
62 'gdpr-compliance',
63 'module-manager',
64 'scripts-styles',
65 'promotions',
66 'analytify-admin-ui',
67 'ga4-property-management',
68 'plugin-lifecycle',
69 'class-analytify-license-sticky-notice',
70 );
71
72 foreach ( $component_files as $component ) {
73 $file_path = ( defined( 'WP_ANALYTIFY_PLUGIN_DIR' ) ? WP_ANALYTIFY_PLUGIN_DIR : dirname( __DIR__ ) ) . '/inc/' . $component . '.php';
74 if ( file_exists( $file_path ) ) {
75 require_once $file_path;
76 }
77 }
78 }
79
80 /**
81 * Initialize all components
82 *
83 * @version 7.0.5
84 * @return void
85 */
86 private function init_components() {
87 // Initialize components that need the main plugin instance.
88 $this->components['admin_notices'] = new Analytify_Admin_Notices( $this->analytify );
89 $this->components['analytics_accounts'] = new Analytify_Analytics_Accounts( $this->analytify );
90 $this->components['analytics_reports'] = new Analytify_Analytics_Reports( $this->analytify );
91 $this->components['profile_management'] = new Analytify_Profile_Management( $this->analytify );
92 $this->components['gdpr_compliance'] = new Analytify_GDPR_Compliance( $this->analytify );
93 $this->components['module_manager'] = new Analytify_Module_Manager( $this->analytify );
94 $this->components['scripts_styles'] = new Analytify_Scripts_Styles( $this->analytify );
95 $this->components['promotions'] = new Analytify_Promotions( $this->analytify );
96 $this->components['ga4_property_management'] = new Analytify_GA4_Property_Management( $this->analytify );
97 $this->components['plugin_lifecycle'] = new Analytify_Plugin_Lifecycle( $this->analytify );
98
99 if ( class_exists( 'Analytify_License_Sticky_Notice', false ) ) {
100 $this->components['license_sticky_notice'] = new Analytify_License_Sticky_Notice();
101 $this->components['license_sticky_notice']->init();
102 }
103
104 // Set up component hooks - register admin notices and scripts immediately.
105 $this->setup_admin_notice_hooks();
106 $this->setup_scripts_hooks();
107 add_action( 'init', array( $this, 'setup_other_component_hooks' ), 20 );
108
109 do_action( 'analytify_components_loaded', $this->components );
110 }
111
112 /**
113 * Set up admin notice hooks immediately
114 *
115 * @version 8.1.1
116 * @return void
117 */
118 private function setup_admin_notice_hooks() {
119 // Admin notices hooks - register immediately.
120 if ( isset( $this->components['admin_notices'] ) ) {
121 $admin_notices = $this->components['admin_notices'];
122
123 // Add admin notice hooks with proper priority.
124 add_action( 'admin_notices', array( $admin_notices, 'pro_update_notice' ), 10 );
125 add_action( 'admin_notices', array( $admin_notices, 'analytify_admin_notice' ), 10 );
126 add_action( 'admin_notices', array( $admin_notices, 'addons_ga4_update_notice' ), 10 );
127 add_action( 'admin_notices', array( $admin_notices, 'analytify_measurement_protocol_secret_notice' ), 10 );
128 add_action( 'admin_notices', array( $admin_notices, 'analytify_cache_clear_notice' ), 10 );
129
130 // AJAX handlers.
131 add_action( 'wp_ajax_analytify_dismiss_rank_math_notice', array( $admin_notices, 'analytify_dismiss_rank_math_notice' ) );
132 }
133 }
134
135 /**
136 * Set up scripts and styles hooks immediately (CRITICAL - must not be delayed)
137 *
138 * @version 7.0.5
139 * @return void
140 */
141 private function setup_scripts_hooks() {
142 // Scripts & Styles hooks - MUST be registered immediately to prevent layout destruction.
143 if ( isset( $this->components['scripts_styles'] ) ) {
144 $scripts_styles = $this->components['scripts_styles'];
145
146 // Admin scripts and styles - CRITICAL for plugin layout.
147 add_action( 'admin_enqueue_scripts', array( $scripts_styles, 'admin_scripts' ) );
148 add_action( 'admin_enqueue_scripts', array( $scripts_styles, 'admin_styles' ) );
149 add_action( 'wp_enqueue_scripts', array( $scripts_styles, 'front_styles' ) );
150 add_action( 'wp_enqueue_scripts', array( $scripts_styles, 'front_scripts' ) );
151 add_action( 'admin_head', array( $scripts_styles, 'add_dashboard_inline_styles' ) );
152 add_action( 'admin_head', array( $scripts_styles, 'add_dashboard_inline_scripts' ) );
153 }
154 }
155
156 /**
157 * Set up other component hooks after WordPress is loaded
158 *
159 * @version 7.0.5
160 * @return void
161 */
162 public function setup_other_component_hooks() {
163 // Promotions hooks.
164 if ( isset( $this->components['promotions'] ) ) {
165 $promotions = $this->components['promotions'];
166
167 // Promotional notices and messages.
168 add_action( 'admin_init', array( $promotions, 'analytify_review_notice' ) );
169 add_action( 'admin_init', array( $promotions, 'analytify_buy_pro_notice' ) );
170 add_action( 'admin_init', array( $promotions, 'dismiss_notices' ) );
171 add_action( 'admin_notices', array( $promotions, 'gtag_move_to_notice' ) );
172 add_action( 'admin_notices', array( $promotions, 'profile_warning' ) );
173 add_filter( 'plugin_row_meta', array( $promotions, 'add_rating_icon' ), 10, 2 );
174 }
175
176 // Analytics reports hooks.
177 if ( isset( $this->components['analytics_reports'] ) ) {
178 $analytics_reports = $this->components['analytics_reports'];
179
180 // Add meta boxes for single post analytics.
181 add_action( 'add_meta_boxes', array( $analytics_reports, 'show_admin_single_analytics_add_metabox' ) );
182
183 // AJAX handlers.
184 add_action( 'wp_ajax_get_ajax_single_admin_analytics', array( $analytics_reports, 'get_ajax_single_admin_analytics' ) );
185 add_action( 'wp_ajax_set_module_state', array( $analytics_reports, 'set_module_state' ) );
186
187 // Post row stats.
188 add_filter( 'post_row_actions', array( $analytics_reports, 'post_rows_stats' ), 10, 2 );
189 add_filter( 'page_row_actions', array( $analytics_reports, 'post_rows_stats' ), 10, 2 );
190 add_action( 'post_submitbox_minor_actions', array( $analytics_reports, 'post_submitbox_stats_action' ), 10, 1 );
191 }
192
193 // Profile management hooks.
194 if ( isset( $this->components['profile_management'] ) ) {
195 $profile_management = $this->components['profile_management'];
196
197 // Profile management hooks.
198 add_action( 'update_option_wp-analytify-profile', array( $profile_management, 'update_profiles_list_summary' ), 10, 2 );
199 add_action( 'update_option_wp-analytify-advanced', array( $profile_management, 'update_selected_profiles' ), 10, 2 );
200 add_action( 'admin_init', array( $profile_management, 'update_profile_list_summary_on_update' ), 1 );
201 }
202
203 // GDPR: {@see Analytify_GDPR_Compliance} registers init + add_meta_boxes in its constructor.
204
205 // Module manager hooks.
206 if ( isset( $this->components['module_manager'] ) ) {
207 $module_manager = $this->components['module_manager'];
208
209 // Module management hooks.
210 add_action( 'admin_init', array( $module_manager, 'modules_fallback_page' ) );
211 }
212 }
213
214 /**
215 * Get a specific component instance
216 *
217 * @version 7.0.5
218 * @param string $component_name Component name.
219 * @return object|null Component instance or null if not found
220 */
221 public function get_component( $component_name ) {
222 return isset( $this->components[ $component_name ] ) ? $this->components[ $component_name ] : null;
223 }
224
225 /**
226 * Get all component instances
227 *
228 * @version 7.0.5
229 * @return array<string, mixed> All component instances.
230 */
231 public function get_all_components() {
232 return $this->components;
233 }
234
235 /**
236 * Check if a component is loaded
237 *
238 * @version 7.0.5
239 * @param string $component_name Component name.
240 * @return bool Whether component is loaded.
241 */
242 public function is_component_loaded( $component_name ) {
243 return isset( $this->components[ $component_name ] );
244 }
245
246 /**
247 * Check if a component is loaded (alias for is_component_loaded)
248 *
249 * @version 7.0.5
250 * @param string $component_name Component name.
251 * @return bool Whether component is loaded.
252 */
253 public function has_component( $component_name ) {
254 return $this->is_component_loaded( $component_name );
255 }
256 }
257