| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File naming is acceptable |
| 2 |
/** |
| 3 |
* Scripts & Styles Component for WP Analytify |
| 4 |
* |
| 5 |
* This file contains all script and style enqueuing 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 |
* Scripts & Styles Component Class |
| 18 |
*/ |
| 19 |
class Analytify_Scripts_Styles { |
| 20 |
|
| 21 |
/** |
| 22 |
* Main plugin instance |
| 23 |
* |
| 24 |
* @var WP_Analytify |
| 25 |
*/ |
| 26 |
private $analytify; |
| 27 |
|
| 28 |
/** |
| 29 |
* Main plugin file path |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
private $plugin_file; |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor |
| 37 |
* |
| 38 |
* @param WP_Analytify $analytify Main plugin instance. |
| 39 |
*/ |
| 40 |
public function __construct( $analytify ) { |
| 41 |
$this->analytify = $analytify; |
| 42 |
$this->plugin_file = WP_ANALYTIFY_PLUGIN_DIR . '/wp-analytify.php'; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Loading admin styles CSS for the plugin. |
| 47 |
* |
| 48 |
* @param string $page loaded page name. |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function admin_styles( $page ) { |
| 52 |
wp_enqueue_style( 'dashicons' ); |
| 53 |
wp_enqueue_style( 'admin-bar-style', plugins_url( 'assets/css/admin_bar_styles.css', $this->plugin_file ), array(), ANALYTIFY_VERSION ); |
| 54 |
|
| 55 |
// For Settings only. |
| 56 |
if ( 'analytify_page_analytify-settings' === $page || 'analytify_page_analytify-campaigns' === $page ) { |
| 57 |
wp_enqueue_style( 'jquery_tooltip', '//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css', array(), ANALYTIFY_VERSION ); |
| 58 |
} |
| 59 |
|
| 60 |
// For Single Page/Post Stats. |
| 61 |
if ( 'analytify_page_analytify-settings' === $page || 'post.php' === $page || 'post-new.php' === $page ) { |
| 62 |
wp_enqueue_style( 'chosen', plugins_url( 'assets/css/chosen.min.css', $this->plugin_file ), array(), ANALYTIFY_VERSION ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Version is set |
| 63 |
} |
| 64 |
|
| 65 |
if ( false !== strpos( $page, 'analytify' ) || 'post.php' === $page || 'post-new.php' === $page || 'index.php' === $page ) { |
| 66 |
wp_enqueue_style( 'wp-analytify-style', plugins_url( 'assets/css/wp-analytify-style.css', $this->plugin_file ), array(), ANALYTIFY_VERSION ); |
| 67 |
wp_enqueue_style( 'wp-analytify-default-style', plugins_url( 'assets/css/styles.css', $this->plugin_file ), array(), ANALYTIFY_VERSION ); |
| 68 |
|
| 69 |
$conditional_style = ''; |
| 70 |
|
| 71 |
// Filter dashboard header animation. |
| 72 |
if ( apply_filters( 'analytify_dashboard_head_animate', true ) ) { |
| 73 |
$conditional_style .= ' |
| 74 |
.wpanalytify .graph { |
| 75 |
-webkit-animation: graph_animation 130s linear infinite; |
| 76 |
-moz-animation: graph_animation 130s linear infinite; |
| 77 |
-o-animation: graph_animation 130s linear infinite; |
| 78 |
animation: graph_animation 130s linear infinite; |
| 79 |
} |
| 80 |
.wpanalytify .graph:after { |
| 81 |
-webkit-animation: graph_animation 250s linear infinite; |
| 82 |
-moz-animation: graph_animation 250s linear infinite; |
| 83 |
-o-animation: graph_animation 250s linear infinite; |
| 84 |
animation: graph_animation 250s linear infinite; |
| 85 |
}'; |
| 86 |
} |
| 87 |
|
| 88 |
// Add conditional style. |
| 89 |
wp_add_inline_style( 'wp-analytify-default-style', $conditional_style ); |
| 90 |
} |
| 91 |
|
| 92 |
wp_enqueue_style( 'wp-analytify-utils-style', plugins_url( 'assets/css/utils.css', $this->plugin_file ), array(), ANALYTIFY_VERSION ); |
| 93 |
// For WP Pointer. |
| 94 |
if ( 1 !== (int) get_option( 'show_tracking_pointer_1' ) ) { |
| 95 |
wp_enqueue_style( 'wp-pointer' ); |
| 96 |
} |
| 97 |
|
| 98 |
// Hide unnamed menu item (promo page) from Analytify submenu. |
| 99 |
$hide_submenu_style = ' |
| 100 |
#toplevel_page_analytify-dashboard .wp-submenu li a[href*="analytify-promo"] { |
| 101 |
display: none !important; |
| 102 |
}'; |
| 103 |
wp_add_inline_style( 'wp-analytify-utils-style', $hide_submenu_style ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Loading admin scripts JS for the plugin. |
| 108 |
* |
| 109 |
* @param string $page Current page. |
| 110 |
* @version 9.0.0 |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function admin_scripts( $page ) { |
| 114 |
|
| 115 |
wp_enqueue_script( 'wp-analytify-script-js', plugins_url( 'assets/js/wp-analytify.js', $this->plugin_file ), array( 'jquery' ), ANALYTIFY_VERSION, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter -- Script loaded in header intentionally |
| 116 |
|
| 117 |
wp_localize_script( |
| 118 |
'wp-analytify-script-js', |
| 119 |
'wp_analytify_script', |
| 120 |
array( |
| 121 |
'url' => esc_url_raw( rest_url( 'wp-analytify/v1/get_report/' ) ), |
| 122 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 123 |
'delimiter' => WPANALYTIFY_Utils::get_delimiter(), |
| 124 |
'no_stats_message' => __( 'No activity during this period.', 'wp-analytify' ), |
| 125 |
'error_message' => __( 'Something went wrong. Please try again later single.', 'wp-analytify' ), |
| 126 |
) |
| 127 |
); |
| 128 |
|
| 129 |
global $post_type; |
| 130 |
|
| 131 |
// For main page. |
| 132 |
$allowed_pages = array( |
| 133 |
'index.php', |
| 134 |
'toplevel_page_analytify-dashboard', |
| 135 |
'analytify_page_analytify-woocommerce', |
| 136 |
'analytify_page_analytify-lifterlms', |
| 137 |
'analytify_page_analytify-learndash', |
| 138 |
'analytify_page_analytify-pmpro', |
| 139 |
'analytify_page_edd-dashboard', |
| 140 |
'analytify_page_analytify-campaigns', |
| 141 |
'analytify_page_analytify-goals', |
| 142 |
'analytify_page_analytify-forms', |
| 143 |
'analytify_page_analytify-dimensions', |
| 144 |
'analytify_page_analytify-authors', |
| 145 |
'analytify_page_analytify-events', |
| 146 |
'analytify_page_analytify-promo', |
| 147 |
); |
| 148 |
if ( in_array( $page, $allowed_pages, true ) || in_array( $post_type, $this->analytify->settings->get_option( 'show_analytics_post_types_back_end', 'wp-analytify-admin', array() ), true ) ) { |
| 149 |
// Using WP's internal moment-js, after 4.2.1. |
| 150 |
|
| 151 |
/** |
| 152 |
* Filter to force moment js to use the same timezone as the one set within WordPress. |
| 153 |
* Default is false. |
| 154 |
* |
| 155 |
* Filter was remove after 4.2.1 |
| 156 |
* |
| 157 |
* Example use: add_filter( 'analytify_set_moment_timezone_to_match_wp', '__return_true' ); |
| 158 |
*/ |
| 159 |
|
| 160 |
/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found -- Kept for reference |
| 161 |
// phpcs:disable |
| 162 |
$apply_timezone_match = apply_filters( 'analytify_set_moment_timezone_to_match_wp', false ); |
| 163 |
$timezone = $apply_timezone_match ? WPANALYTIFY_Utils::timezone() : false; |
| 164 |
|
| 165 |
if ( $timezone ) { |
| 166 |
wp_enqueue_script( 'moment-timezone-with-data', plugins_url( 'assets/js/moment-timezone-with-data.min.js', __FILE__ ), array( 'jquery', 'moment' ), '0.5.34' ); |
| 167 |
} |
| 168 |
|
| 169 |
wp_localize_script( 'moment', 'moment_analytify', array( 'timezone' => $timezone ) ); |
| 170 |
// phpcs:enable |
| 171 |
*/ |
| 172 |
|
| 173 |
wp_enqueue_script( 'pikaday-js', plugins_url( 'assets/js/pikaday.js', $this->plugin_file ), array( 'moment' ), ANALYTIFY_VERSION, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter -- Script loaded in header intentionally |
| 174 |
|
| 175 |
$analytify_dashboard_js_deps = array( 'pikaday-js' ); |
| 176 |
// jsPDF 4.2.1+ (patched HTML-in-new-window / output options); bundled under assets (see package.json). |
| 177 |
if ( 'toplevel_page_analytify-dashboard' === $page ) { |
| 178 |
wp_enqueue_script( |
| 179 |
'analytify-jspdf', |
| 180 |
plugins_url( 'assets/js/jspdf.umd.min.js', $this->plugin_file ), |
| 181 |
array(), |
| 182 |
'4.2.1', |
| 183 |
false |
| 184 |
); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter -- PDF export; must load before dashboard JS |
| 185 |
wp_enqueue_script( |
| 186 |
'analytify-html2canvas', |
| 187 |
plugins_url( 'assets/js/html2canvas.min.js', $this->plugin_file ), |
| 188 |
array(), |
| 189 |
'1.4.1', |
| 190 |
false |
| 191 |
); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter -- PDF export; vendored (same as jsPDF) |
| 192 |
$analytify_dashboard_js_deps[] = 'analytify-jspdf'; |
| 193 |
$analytify_dashboard_js_deps[] = 'analytify-html2canvas'; |
| 194 |
} |
| 195 |
|
| 196 |
wp_enqueue_script( 'analytify-dashboard-js', plugins_url( 'assets/js/wp-analytify-dashboard.js', $this->plugin_file ), $analytify_dashboard_js_deps, ANALYTIFY_VERSION, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter -- Script loaded in header intentionally |
| 197 |
|
| 198 |
wp_localize_script( |
| 199 |
'analytify-dashboard-js', |
| 200 |
'analytify_dashboard', |
| 201 |
array( |
| 202 |
'i18n' => array( |
| 203 |
'previousMonth' => __( 'Previous Month', 'wp-analytify' ), |
| 204 |
'nextMonth' => __( 'Next Month', 'wp-analytify' ), |
| 205 |
'months' => array( |
| 206 |
__( 'January', 'wp-analytify' ), |
| 207 |
__( 'February', 'wp-analytify' ), |
| 208 |
__( 'March', 'wp-analytify' ), |
| 209 |
__( 'April', 'wp-analytify' ), |
| 210 |
__( 'May', 'wp-analytify' ), |
| 211 |
__( 'June', 'wp-analytify' ), |
| 212 |
__( 'July', 'wp-analytify' ), |
| 213 |
__( 'August', 'wp-analytify' ), |
| 214 |
__( 'September', 'wp-analytify' ), |
| 215 |
__( 'October', 'wp-analytify' ), |
| 216 |
__( 'November', 'wp-analytify' ), |
| 217 |
__( 'December', 'wp-analytify' ), |
| 218 |
), |
| 219 |
'weekdays' => array( |
| 220 |
__( 'Sunday', 'wp-analytify' ), |
| 221 |
__( 'Monday', 'wp-analytify' ), |
| 222 |
__( 'Tuesday', 'wp-analytify' ), |
| 223 |
__( 'Wednesday', 'wp-analytify' ), |
| 224 |
__( 'Thursday', 'wp-analytify' ), |
| 225 |
__( 'Friday', 'wp-analytify' ), |
| 226 |
__( 'Saturday', 'wp-analytify' ), |
| 227 |
), |
| 228 |
'weekdaysShort' => array( |
| 229 |
__( 'Sun', 'wp-analytify' ), |
| 230 |
__( 'Mon', 'wp-analytify' ), |
| 231 |
__( 'Tue', 'wp-analytify' ), |
| 232 |
__( 'Wed', 'wp-analytify' ), |
| 233 |
__( 'Thu', 'wp-analytify' ), |
| 234 |
__( 'Fri', 'wp-analytify' ), |
| 235 |
__( 'Sat', 'wp-analytify' ), |
| 236 |
), |
| 237 |
), |
| 238 |
) |
| 239 |
); |
| 240 |
} |
| 241 |
|
| 242 |
// For dashboard only. |
| 243 |
$analytify_chart_pages = array( 'toplevel_page_analytify-dashboard', 'analytify_page_analytify-woocommerce', 'analytify_page_edd-dashboard', 'analytify_page_analytify-pmpro', 'analytify_page_analytify-learndash', 'analytify_page_analytify-lifterlms', 'analytify_page_analytify-campaigns' ); |
| 244 |
|
| 245 |
if ( in_array( $page, $analytify_chart_pages, true ) ) { |
| 246 |
// Enqueue the main JavaScript file. |
| 247 |
wp_enqueue_script( 'echarts-js', plugins_url( 'assets/js/echarts.min.js', $this->plugin_file ), array(), ANALYTIFY_VERSION, true ); |
| 248 |
wp_enqueue_script( 'echarts-world-js', 'https://cdn.jsdelivr.net/npm/echarts-maps@1.1.0/world.min.js', array(), ANALYTIFY_VERSION, true ); |
| 249 |
} |
| 250 |
|
| 251 |
// Main dashboard file that handles AJAX calls for core, also generates the template. |
| 252 |
if ( 'toplevel_page_analytify-dashboard' === $page ) { |
| 253 |
/** |
| 254 |
* Tells the script to load the data via an ajax request on date change. |
| 255 |
* Only load data via ajax if the Pro version is 5.0.0 or higher. |
| 256 |
*/ |
| 257 |
$load_via_ajax = true; |
| 258 |
if ( defined( 'ANALYTIFY_PRO_VERSION' ) && 0 > version_compare( ANALYTIFY_PRO_VERSION, '5.0.0' ) ) { |
| 259 |
$load_via_ajax = false; |
| 260 |
} |
| 261 |
|
| 262 |
wp_enqueue_style( 'analytify-dashboard-core', plugins_url( 'assets/css/common-dashboard.css', $this->plugin_file ), array(), ANALYTIFY_VERSION ); |
| 263 |
// Code added by jawad for fixing. |
| 264 |
$rest_url = esc_url_raw( get_rest_url() ); |
| 265 |
$api_url = $rest_url . 'wp-analytify/v1/get_report/'; |
| 266 |
|
| 267 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading URL parameters for display purposes |
| 268 |
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 269 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading URL parameters for display purposes |
| 270 |
$show = isset( $_GET['show'] ) ? sanitize_text_field( wp_unslash( $_GET['show'] ) ) : ''; |
| 271 |
if ( class_exists( 'WP_Analytify_Pro_Base' ) && version_compare( ANALYTIFY_PRO_VERSION, '5.0.0' ) >= 0 && ! empty( $page ) && empty( $show ) && 'analytify-dashboard' === $page ) { |
| 272 |
wp_enqueue_script( 'analytify-stats-core', plugins_url( 'assets/js/stats-core.js', $this->plugin_file ), array( 'jquery', 'echarts-js', 'analytify-comp-chart' ), ANALYTIFY_VERSION, true ); |
| 273 |
|
| 274 |
} else { |
| 275 |
wp_enqueue_script( 'analytify-stats-core', plugins_url( 'assets/js/stats-core.js', $this->plugin_file ), array( 'jquery', 'echarts-js' ), ANALYTIFY_VERSION, true ); |
| 276 |
} |
| 277 |
// Localize the GeoJSON file URL to use in JavaScript. |
| 278 |
wp_localize_script( |
| 279 |
'analytify-stats-core', |
| 280 |
'geoJsonData', |
| 281 |
array( |
| 282 |
'geoJsonUrl' => plugins_url( 'assets/js/geo.json', $this->plugin_file ), |
| 283 |
) |
| 284 |
); |
| 285 |
|
| 286 |
wp_localize_script( |
| 287 |
'analytify-stats-core', |
| 288 |
'analytify_stats_core', |
| 289 |
array( |
| 290 |
'url' => $api_url, |
| 291 |
'delimiter' => WPANALYTIFY_Utils::get_delimiter(), |
| 292 |
'ga_mode' => WPANALYTIFY_Utils::get_ga_mode(), |
| 293 |
'ga4_report_url' => WP_ANALYTIFY_FUNCTIONS::get_ga_report_url( WPANALYTIFY_Utils::get_reporting_property() ), |
| 294 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 295 |
'load_via_ajax' => $load_via_ajax, |
| 296 |
'dist_js_url' => plugins_url( 'assets/js/', $this->plugin_file ), |
| 297 |
'no_stats_message' => __( 'No activity during this period.', 'wp-analytify' ), |
| 298 |
'error_message' => __( 'Something went wrong. Please try again.', 'wp-analytify' ), |
| 299 |
) |
| 300 |
); |
| 301 |
} |
| 302 |
|
| 303 |
// For Settings only. |
| 304 |
if ( 'analytify_page_analytify-settings' === $page ) { |
| 305 |
wp_enqueue_script( 'chosen-js', plugins_url( 'assets/js/chosen.jquery.min.js', $this->plugin_file ), array( 'jquery' ), ANALYTIFY_VERSION, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter -- Script loaded in header intentionally |
| 306 |
wp_enqueue_script( 'analytify-settings-js', plugins_url( 'assets/js/wp-analytify-settings.js', $this->plugin_file ), array( 'jquery-ui-tooltip', 'jquery', 'chosen-js' ), ANALYTIFY_VERSION, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter -- Script loaded in header intentionally |
| 307 |
wp_localize_script( |
| 308 |
'analytify-settings-js', |
| 309 |
'analytify_settings', |
| 310 |
array( |
| 311 |
'is_hide_profile' => $this->analytify->settings->get_option( 'hide_profiles_list', 'wp-analytify-profile', 'off' ), |
| 312 |
'is_authenticate' => (bool) get_option( 'pa_google_token' ), |
| 313 |
'ga_mode' => WPANALYTIFY_Utils::get_ga_mode(), |
| 314 |
'copy_failed_message' => __( 'Failed to copy to clipboard. Please copy manually.', 'wp-analytify' ), |
| 315 |
'copy_success_message' => __( 'Copied!', 'wp-analytify' ), |
| 316 |
'no_diagnostic_message' => __( 'No diagnostic information available to copy. Please load the diagnostic log first.', 'wp-analytify' ), |
| 317 |
) |
| 318 |
); |
| 319 |
|
| 320 |
// Localize wpanalytify_data for settings script. |
| 321 |
// This must be done here to ensure the data is available when the script loads. |
| 322 |
$nonces_pre = array( |
| 323 |
'check_license' => wp_create_nonce( 'check-license' ), |
| 324 |
'activate_license' => wp_create_nonce( 'activate-license' ), |
| 325 |
'clear_log' => wp_create_nonce( 'clear-log' ), |
| 326 |
'fetch_log' => wp_create_nonce( 'fetch-log' ), |
| 327 |
'import_export' => wp_create_nonce( 'import-export' ), |
| 328 |
'analytify_rated' => wp_create_nonce( 'analytify-rated' ), |
| 329 |
'reactivate_license' => wp_create_nonce( 'reactivate-license' ), |
| 330 |
'single_post_stats' => wp_create_nonce( 'analytify-get-single-stats' ), |
| 331 |
'send_single_post_email' => wp_create_nonce( 'analytify-single-post-email' ), |
| 332 |
); |
| 333 |
|
| 334 |
$nonces = apply_filters( 'wpanalytify_nonces', $nonces_pre ); |
| 335 |
|
| 336 |
$data_pre = array( |
| 337 |
'this_url' => esc_html( addslashes( home_url() ) ), |
| 338 |
'is_multisite' => esc_html( is_multisite() ? 'true' : 'false' ), |
| 339 |
'nonces' => $nonces, |
| 340 |
); |
| 341 |
|
| 342 |
$data = apply_filters( 'wpanalytify_data', $data_pre ); |
| 343 |
|
| 344 |
// Backup the complete data before localization. |
| 345 |
// This is needed because the Pro plugin's filter uses array_merge which replaces |
| 346 |
// the entire nonces array instead of merging it, and other scripts may overwrite |
| 347 |
// wpanalytify_data after this script loads. |
| 348 |
// |
| 349 |
// Security Note: Nonces are meant to be in JavaScript (this is how WordPress AJAX works). |
| 350 |
// The security comes from server-side verification via check_ajax_referer() which: |
| 351 |
// - Verifies the nonce is valid for the action |
| 352 |
// - Checks the nonce is tied to the current user session |
| 353 |
// - Validates the HTTP referer |
| 354 |
// - Requires manage_options capability |
| 355 |
// The backup variable is just a copy of the same nonce already exposed in JavaScript. |
| 356 |
$settings_data_backup = $data; |
| 357 |
$backup_json = wp_json_encode( $settings_data_backup ); |
| 358 |
|
| 359 |
wp_localize_script( 'analytify-settings-js', 'wpanalytify_data', $data ); |
| 360 |
|
| 361 |
// Add backup variable BEFORE the settings script loads. |
| 362 |
// This backup is used as a fallback if wpanalytify_data gets overwritten. |
| 363 |
wp_add_inline_script( |
| 364 |
'analytify-settings-js', |
| 365 |
'var wpanalytify_data_settings_backup = ' . $backup_json . ';', |
| 366 |
'before' |
| 367 |
); |
| 368 |
|
| 369 |
// Add restore script that runs immediately and on jQuery ready. |
| 370 |
// This ensures wpanalytify_data is restored if it gets overwritten by other scripts. |
| 371 |
add_action( |
| 372 |
'admin_footer', |
| 373 |
function () use ( $backup_json ) { |
| 374 |
$restore_script = ' |
| 375 |
(function() { |
| 376 |
var backup = ' . $backup_json . '; |
| 377 |
function restoreIfNeeded() { |
| 378 |
if (typeof wpanalytify_data === "undefined" || !wpanalytify_data.nonces || !wpanalytify_data.nonces.fetch_log) { |
| 379 |
wpanalytify_data = backup; |
| 380 |
return true; |
| 381 |
} |
| 382 |
return false; |
| 383 |
} |
| 384 |
// Restore immediately when this script runs. |
| 385 |
restoreIfNeeded(); |
| 386 |
// Also restore when DOM is ready (in case something overwrites it). |
| 387 |
if (document.readyState === "loading") { |
| 388 |
document.addEventListener("DOMContentLoaded", restoreIfNeeded); |
| 389 |
} |
| 390 |
// Also restore when jQuery is ready (in case something overwrites it). |
| 391 |
if (typeof jQuery !== "undefined") { |
| 392 |
jQuery(document).ready(restoreIfNeeded); |
| 393 |
} |
| 394 |
})(); |
| 395 |
'; |
| 396 |
|
| 397 |
// Output as standalone script to ensure it runs after all other scripts. |
| 398 |
echo '<script type="text/javascript">' . $restore_script . '</script>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- JSON is already escaped, script is intentional |
| 399 |
}, |
| 400 |
999 |
| 401 |
); |
| 402 |
} |
| 403 |
|
| 404 |
// Addons page script - Basic localization here, full localization with slugs happens in page-addons.php. |
| 405 |
if ( 'analytify_page_analytify-addons' === $page ) { |
| 406 |
wp_enqueue_script( 'analytify-addons-js', plugins_url( 'assets/js/wp-analytify-addons.js', $this->plugin_file ), array( 'jquery' ), ANALYTIFY_VERSION, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter -- Script loaded in header intentionally. |
| 407 |
// Basic localization - page-addons.php will override with full data including allowed_slugs. |
| 408 |
wp_localize_script( |
| 409 |
'analytify-addons-js', |
| 410 |
'analytify_addons', |
| 411 |
array( |
| 412 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 413 |
'nonce' => wp_create_nonce( 'addons' ), |
| 414 |
'allowed_slugs' => array(), // Will be populated by page-addons.php. |
| 415 |
) |
| 416 |
); |
| 417 |
} |
| 418 |
|
| 419 |
// For Single Page/Post Stats. |
| 420 |
if ( 'post.php' === $page || 'post-new.php' === $page ) { |
| 421 |
wp_enqueue_script( 'chosen-js', plugins_url( 'assets/js/chosen.jquery.min.js', $this->plugin_file ), array( 'jquery' ), ANALYTIFY_VERSION, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter -- Script loaded in header intentionally |
| 422 |
} |
| 423 |
|
| 424 |
if ( 1 !== (int) get_option( 'show_tracking_pointer_1' ) ) { |
| 425 |
wp_enqueue_script( 'wp-pointer' ); |
| 426 |
} |
| 427 |
|
| 428 |
wp_localize_script( |
| 429 |
'wp-analytify-script-js', |
| 430 |
'wpanalytify_strings', |
| 431 |
array( |
| 432 |
'enter_license_key' => __( 'Please enter your license key.', 'wp-analytify' ), |
| 433 |
'register_license_problem' => __( 'A problem occurred when trying to register the license, please try again.', 'wp-analytify' ), |
| 434 |
'license_check_problem' => __( 'A problem occurred when trying to check the license, please try again.', 'wp-analytify' ), |
| 435 |
'license_registered' => __( 'Your license has been activated. You will now receive automatic updates and access to email support.', 'wp-analytify' ), |
| 436 |
) |
| 437 |
); |
| 438 |
|
| 439 |
$nonces = apply_filters( |
| 440 |
'wpanalytify_nonces', |
| 441 |
array( |
| 442 |
'check_license' => wp_create_nonce( 'check-license' ), |
| 443 |
'activate_license' => wp_create_nonce( 'activate-license' ), |
| 444 |
'clear_log' => wp_create_nonce( 'clear-log' ), |
| 445 |
'fetch_log' => wp_create_nonce( 'fetch-log' ), |
| 446 |
'import_export' => wp_create_nonce( 'import-export' ), |
| 447 |
'analytify_rated' => wp_create_nonce( 'analytify-rated' ), |
| 448 |
'reactivate_license' => wp_create_nonce( 'reactivate-license' ), |
| 449 |
'single_post_stats' => wp_create_nonce( 'analytify-get-single-stats' ), |
| 450 |
'send_single_post_email' => wp_create_nonce( 'analytify-single-post-email' ), |
| 451 |
) |
| 452 |
); |
| 453 |
|
| 454 |
$data = apply_filters( |
| 455 |
'wpanalytify_data', |
| 456 |
array( |
| 457 |
'this_url' => esc_html( addslashes( home_url() ) ), |
| 458 |
'is_multisite' => esc_html( is_multisite() ? 'true' : 'false' ), |
| 459 |
'nonces' => $nonces, |
| 460 |
) |
| 461 |
); |
| 462 |
|
| 463 |
// Fix: Only localize wpanalytify_data to wp-analytify-script-js if NOT on settings page. |
| 464 |
// Reason: wp-analytify-script-js doesn't use wpanalytify_data, but analytify-settings-js does. |
| 465 |
// If we localize to both scripts with the same variable name, the second one overwrites the first. |
| 466 |
// By skipping localization to wp-analytify-script-js on the settings page, we prevent the overwrite. |
| 467 |
if ( 'analytify_page_analytify-settings' !== $page ) { |
| 468 |
wp_localize_script( 'wp-analytify-script-js', 'wpanalytify_data', $data ); |
| 469 |
} |
| 470 |
|
| 471 |
// Also ensure ajaxurl is available for settings script if it's loaded. |
| 472 |
if ( 'analytify_page_analytify-settings' === $page ) { |
| 473 |
add_action( |
| 474 |
'admin_footer', |
| 475 |
function () { |
| 476 |
// Ensure ajaxurl is available if not already set. |
| 477 |
if ( ! wp_script_is( 'analytify-settings-js', 'done' ) ) { |
| 478 |
wp_add_inline_script( |
| 479 |
'analytify-settings-js', |
| 480 |
'if (typeof ajaxurl === "undefined") { var ajaxurl = "' . esc_js( admin_url( 'admin-ajax.php' ) ) . '"; }', |
| 481 |
'before' |
| 482 |
); |
| 483 |
} |
| 484 |
}, |
| 485 |
20 |
| 486 |
); |
| 487 |
} |
| 488 |
|
| 489 |
// Print JS at footer. |
| 490 |
} |
| 491 |
|
| 492 |
/** |
| 493 |
* Loading frontend scripts JS for the plugin. |
| 494 |
* |
| 495 |
* @return void |
| 496 |
*/ |
| 497 |
public function front_scripts() { |
| 498 |
if ( 'on' === $this->analytify->settings->get_option( 'disable_front_end', 'wp-analytify-front', 'off' ) ) { |
| 499 |
return; |
| 500 |
} |
| 501 |
|
| 502 |
// Only enqueue if file exists to prevent 404 errors. |
| 503 |
$front_js_path = plugin_dir_path( $this->plugin_file ) . 'assets/js/front-analytics.js'; |
| 504 |
if ( file_exists( $front_js_path ) ) { |
| 505 |
wp_enqueue_script( 'wp-analytify-front-js', plugins_url( 'assets/js/front-analytics.js', $this->plugin_file ), array( 'jquery' ), ANALYTIFY_VERSION, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter -- Script loaded in header intentionally |
| 506 |
|
| 507 |
wp_localize_script( |
| 508 |
'wp-analytify-front-js', |
| 509 |
'wp_analytify_front', |
| 510 |
array( |
| 511 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 512 |
'nonce' => wp_create_nonce( 'wp_analytify_front_nonce' ), |
| 513 |
) |
| 514 |
); |
| 515 |
} |
| 516 |
|
| 517 |
// Enqueue scroll depth script if enabled. |
| 518 |
if ( 'on' === $this->analytify->settings->get_option( 'depth_percentage', 'wp-analytify-advanced', 'off' ) ) { |
| 519 |
wp_enqueue_script( 'wp-analytify-scrolldepth', plugins_url( 'assets/js/scrolldepth.js', $this->plugin_file ), array( 'jquery' ), ANALYTIFY_VERSION, true ); |
| 520 |
|
| 521 |
$ga_mode = class_exists( 'WPANALYTIFY_Utils' ) && method_exists( 'WPANALYTIFY_Utils', 'get_ga_mode' ) ? WPANALYTIFY_Utils::get_ga_mode() : 'ga3'; |
| 522 |
$tracking_mode = defined( 'WP_ANALYTIFY_TRACKING_MODE' ) ? WP_ANALYTIFY_TRACKING_MODE : 'gtag'; |
| 523 |
|
| 524 |
// Only enqueue scroll depth for GA4 + gtag to avoid UA incompatibility. |
| 525 |
if ( 'ga4' === $ga_mode && 'gtag' === $tracking_mode ) { |
| 526 |
wp_enqueue_script( 'wp-analytify-scrolldepth', plugins_url( 'assets/js/scrolldepth.js', $this->plugin_file ), array( 'jquery' ), ANALYTIFY_VERSION, true ); |
| 527 |
wp_localize_script( |
| 528 |
'wp-analytify-scrolldepth', |
| 529 |
'analytifyScroll', |
| 530 |
array( |
| 531 |
'tracking_mode' => $tracking_mode, |
| 532 |
'ga4_tracking' => true, |
| 533 |
'permalink' => get_permalink(), |
| 534 |
) |
| 535 |
); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
// Enqueue video tracking script if enabled. |
| 540 |
if ( 'on' === $this->analytify->settings->get_option( 'video_tracking', 'wp-analytify-advanced', 'off' ) ) { |
| 541 |
$ga_mode = class_exists( 'WPANALYTIFY_Utils' ) && method_exists( 'WPANALYTIFY_Utils', 'get_ga_mode' ) ? WPANALYTIFY_Utils::get_ga_mode() : 'ga3'; |
| 542 |
$tracking_mode = defined( 'WP_ANALYTIFY_TRACKING_MODE' ) ? WP_ANALYTIFY_TRACKING_MODE : 'gtag'; |
| 543 |
|
| 544 |
// Only enqueue video tracking for GA4 + gtag to avoid UA incompatibility. |
| 545 |
if ( 'ga4' === $ga_mode && 'gtag' === $tracking_mode ) { |
| 546 |
$video_tracking_path = plugin_dir_path( $this->plugin_file ) . 'assets/js/video_tracking.js'; |
| 547 |
if ( file_exists( $video_tracking_path ) ) { |
| 548 |
wp_enqueue_script( 'wp-analytify-video-tracking', plugins_url( 'assets/js/video_tracking.js', $this->plugin_file ), array( 'jquery' ), ANALYTIFY_VERSION, true ); |
| 549 |
wp_localize_script( |
| 550 |
'wp-analytify-video-tracking', |
| 551 |
'analytifyVideo', |
| 552 |
array( |
| 553 |
'tracking_mode' => $tracking_mode, |
| 554 |
'ga4_tracking' => true, |
| 555 |
'permalink' => get_permalink(), |
| 556 |
) |
| 557 |
); |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
/** |
| 564 |
* Loading frontend styles CSS for the plugin. |
| 565 |
* |
| 566 |
* @return void |
| 567 |
*/ |
| 568 |
public function front_styles() { |
| 569 |
if ( 'on' === $this->analytify->settings->get_option( 'disable_front_end', 'wp-analytify-front', 'off' ) ) { |
| 570 |
return; |
| 571 |
} |
| 572 |
|
| 573 |
// Only enqueue if file exists to prevent 404 errors. |
| 574 |
$front_css_path = plugin_dir_path( $this->plugin_file ) . 'assets/css/front-analytics.css'; |
| 575 |
if ( file_exists( $front_css_path ) ) { |
| 576 |
wp_enqueue_style( 'wp-analytify-front-style', plugins_url( 'assets/css/front-analytics.css', $this->plugin_file ), array(), ANALYTIFY_VERSION ); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
/** |
| 581 |
* Add dashboard inline styles |
| 582 |
* |
| 583 |
* @return void |
| 584 |
*/ |
| 585 |
public function add_dashboard_inline_styles() { |
| 586 |
$custom_css = $this->analytify->settings->get_option( 'custom_css_code', 'wp-analytify-advanced' ); |
| 587 |
if ( ! empty( $custom_css ) ) { |
| 588 |
echo '<style type="text/css">' . wp_kses( $custom_css, array( 'style' => array() ) ) . '</style>'; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* Add dashboard inline scripts |
| 594 |
* |
| 595 |
* @return void |
| 596 |
*/ |
| 597 |
public function add_dashboard_inline_scripts() { |
| 598 |
$custom_js = $this->analytify->settings->get_option( 'custom_js_code', 'wp-analytify-advanced' ); |
| 599 |
if ( ! empty( $custom_js ) ) { |
| 600 |
echo '<script type="text/javascript">' . wp_kses( $custom_js, array( 'script' => array() ) ) . '</script>'; |
| 601 |
} |
| 602 |
|
| 603 |
// Move Analytify "refresh stats" notice below the Dashboard H1 (match native placement). |
| 604 |
$screen = get_current_screen(); |
| 605 |
if ( $screen && 'dashboard' === $screen->id ) { |
| 606 |
?> |
| 607 |
<script type="text/javascript"> |
| 608 |
(function($){ |
| 609 |
$(function(){ |
| 610 |
var $wrap = $('.wrap').first(); |
| 611 |
var $title = $wrap.find('h1').first(); |
| 612 |
var $notice = $('.wp-analytify-notification.wp-analytify-refresh-stats').first(); |
| 613 |
|
| 614 |
if ($wrap.length && $title.length && $notice.length) { |
| 615 |
$notice.insertAfter($title); |
| 616 |
} |
| 617 |
}); |
| 618 |
})(jQuery); |
| 619 |
</script> |
| 620 |
<?php |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|