| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File naming is acceptable |
| 2 |
/** |
| 3 |
* Admin Notices Component for WP Analytify |
| 4 |
* |
| 5 |
* This file contains all admin notices and promotion-related functions |
| 6 |
* that were previously in the main plugin file. |
| 7 |
* |
| 8 |
* @package WP_Analytify |
| 9 |
* @version 8.1.1 |
| 10 |
* @since 8.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Admin Notices Component Class |
| 19 |
*/ |
| 20 |
class Analytify_Admin_Notices { |
| 21 |
|
| 22 |
/** |
| 23 |
* Main plugin instance |
| 24 |
* |
| 25 |
* @var WP_Analytify|null |
| 26 |
*/ |
| 27 |
private $analytify; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor |
| 31 |
* |
| 32 |
* @version 7.0.5 |
| 33 |
* @param WP_Analytify|null $analytify Main plugin instance. |
| 34 |
*/ |
| 35 |
public function __construct( $analytify = null ) { |
| 36 |
$this->analytify = $analytify; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Resolve the plugin instance. |
| 41 |
* |
| 42 |
* Returns the injected instance when available, otherwise falls back |
| 43 |
* to the global $wp_analytify set by the bootstrap file. |
| 44 |
* |
| 45 |
* @since 8.1.1 |
| 46 |
* @return WP_Analytify|null |
| 47 |
*/ |
| 48 |
private function analytify_get_plugin_instance() { |
| 49 |
return $this->analytify ? $this->analytify : $GLOBALS['wp_analytify']; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Display pro update notice |
| 54 |
* |
| 55 |
* @version 7.0.5 |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function pro_update_notice() { |
| 59 |
if ( defined( 'ANALYTIFY_PRO_VERSION' ) && version_compare( ANALYTIFY_PRO_VERSION, '6.0.0', '<' ) ) { |
| 60 |
$class = 'wp-analytify-danger'; |
| 61 |
$message = sprintf( // translators: Update notice. |
| 62 |
esc_html__( '%1$sNote:%2$s Please update to the latest Analytify Pro version to manage all modules/add-ons from %3$s here %4$s.', 'wp-analytify' ), |
| 63 |
'<b>', |
| 64 |
'</b>', |
| 65 |
'<a href="' . esc_url( admin_url( 'admin.php?page=analytify-addons' ) ) . '">', |
| 66 |
'</a>' |
| 67 |
); |
| 68 |
analytify_notice( $message, $class ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Display GA4 update notice |
| 74 |
* |
| 75 |
* @version 8.1.1 |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
public function addons_ga4_update_notice() { |
| 79 |
$analytify = $this->analytify_get_plugin_instance(); |
| 80 |
|
| 81 |
if ( $analytify && ! $this->analytify_is_ga4_enabled() ) { |
| 82 |
$class = 'wp-analytify-danger'; |
| 83 |
$message = sprintf( // translators: GA4 update notice. |
| 84 |
esc_html__( '%1$sAttention:%2$s Switch to GA4 (Google Analytics 4), Your current version of Google Analytics (UA) is outdated and no longer tracks data. %3$sFollow the guide%4$s.', 'wp-analytify' ), |
| 85 |
'<b>', |
| 86 |
'</b>', |
| 87 |
'<a href="https://analytify.io/doc/switch-to-ga4/?utm_source=plugin-notices" target="_blank">', |
| 88 |
'</a>' |
| 89 |
); |
| 90 |
analytify_notice( $message, $class ); |
| 91 |
} |
| 92 |
|
| 93 |
if ( $this->analytify_is_analytify_screen() ) { |
| 94 |
|
| 95 |
$addons_update_todo = WPANALYTIFY_Utils::get_addons_to_upgmdate(); |
| 96 |
if ( ! empty( $addons_update_todo ) ) { |
| 97 |
$class = 'wp-analytify-danger'; |
| 98 |
$message = sprintf( // translators: Update addons. |
| 99 |
esc_html__( '%1$sNotice:%2$s Please update the following plugins to make them work with the Analytify 5.0.0 smoothly. %3$s', 'wp-analytify' ), |
| 100 |
'<b>', |
| 101 |
'</b>', |
| 102 |
'<br>' . implode( '<br>', $addons_update_todo ) |
| 103 |
); |
| 104 |
analytify_notice( $message, $class ); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Display Measurement Protocol Secret missing notice |
| 111 |
* |
| 112 |
* @since 8.1.1 |
| 113 |
* @version 8.1.2 |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
public function analytify_measurement_protocol_secret_notice() { |
| 117 |
|
| 118 |
// Only show to users who can manage options. |
| 119 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
// Only show notice if GA4 mode is enabled. |
| 124 |
if ( ! $this->analytify_is_ga4_enabled() ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
// Only show on Analytify pages. |
| 129 |
if ( ! $this->analytify_is_analytify_screen() ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
// Check if a profile is selected first. |
| 134 |
$profile_settings = get_option( 'wp-analytify-profile' ); |
| 135 |
if ( empty( $profile_settings ) || ! isset( $profile_settings['profile_for_dashboard'] ) ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
$analytify = $this->analytify_get_plugin_instance(); |
| 140 |
|
| 141 |
// Check if Measurement Protocol Secret is missing (must be a non-empty string; API can mistakenly store full response array). |
| 142 |
$mp_secret = ''; |
| 143 |
if ( $analytify && isset( $analytify->settings ) ) { |
| 144 |
$raw = $analytify->settings->get_option( |
| 145 |
'measurement_protocol_secret', |
| 146 |
'wp-analytify-advanced', |
| 147 |
'' |
| 148 |
); |
| 149 |
if ( is_string( $raw ) ) { |
| 150 |
$mp_secret = $raw; |
| 151 |
} elseif ( is_array( $raw ) && isset( $raw['secretValue'] ) && is_string( $raw['secretValue'] ) ) { |
| 152 |
$mp_secret = $raw['secretValue']; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// Show notice if secret is empty. |
| 157 |
if ( '' === $mp_secret ) { |
| 158 |
$class = 'wp-analytify-danger'; |
| 159 |
|
| 160 |
$message = sprintf( |
| 161 |
/* translators: 1: Opening <b> tag, 2: Closing </b> tag, 3: Opening <a> tag with URL, 4: Closing </a> tag. */ |
| 162 |
esc_html__( |
| 163 |
'%1$sWarning:%2$s Measurement Protocol Secret is missing, so server-side event tracking for WooCommerce, Forms, and other integrations is disabled. %3$sConfigure it here%4$s, complete the GA4 User Data Collection Acknowledgement, and reauthenticate Google Analytics in Analytify settings.', |
| 164 |
'wp-analytify' |
| 165 |
), |
| 166 |
'<b>', |
| 167 |
'</b>', |
| 168 |
'<a href="' . esc_url( admin_url( 'admin.php?page=analytify-settings' ) ) . '#wp-analytify-advanced">', |
| 169 |
'</a>' |
| 170 |
); |
| 171 |
|
| 172 |
analytify_notice( $message, $class ); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* Display cache clear notice |
| 179 |
* |
| 180 |
* @version 7.0.5 |
| 181 |
* @return void |
| 182 |
*/ |
| 183 |
public function analytify_cache_clear_notice() { |
| 184 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading URL parameter for display purposes |
| 185 |
if ( ! isset( $_GET['analytify-cache'] ) ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
$notice_message = esc_html__( 'Analytify statistics refreshed', 'wp-analytify' ); |
| 190 |
$class = 'wp-analytify-success wp-analytify-refresh-stats'; |
| 191 |
|
| 192 |
analytify_notice( $notice_message, $class ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Dismiss the Rank Math notice |
| 197 |
* |
| 198 |
* @version 7.0.5 |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
public function analytify_dismiss_rank_math_notice() { |
| 202 |
// Check user capability. |
| 203 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 204 |
wp_send_json_error( __( 'You do not have permission to perform this action.', 'wp-analytify' ) ); |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
// Verify nonce for security. |
| 209 |
if ( ! check_ajax_referer( 'analytify_dismiss_rank_math_notice', 'nonce' ) ) { |
| 210 |
wp_send_json_error( __( 'Security check failed.', 'wp-analytify' ) ); |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
update_option( 'analytify_show_rank_math_notice', false ); |
| 215 |
wp_send_json_success(); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Display admin notice |
| 220 |
* |
| 221 |
* @version 7.0.5 |
| 222 |
* @return void |
| 223 |
*/ |
| 224 |
public function analytify_admin_notice() { |
| 225 |
// Check if current page is analytify dashboard. |
| 226 |
if ( WPANALYTIFY_Utils::is_current_page( 'analytify-dashboard' ) ) { |
| 227 |
// Check if the notice should be displayed by fetching the option. |
| 228 |
$show_notice = get_option( 'analytify_show_rank_math_notice', true ); |
| 229 |
|
| 230 |
if ( $show_notice && is_plugin_active( 'seo-by-rank-math-pro/rank-math-pro.php' ) ) { |
| 231 |
add_option( 'analytify_show_rank_math_notice', true ); |
| 232 |
$rank_math_analytics_options = get_option( 'rank_math_google_analytic_options' ); |
| 233 |
|
| 234 |
if ( is_array( $rank_math_analytics_options ) && isset( $rank_math_analytics_options['local_ga_js'] ) && $rank_math_analytics_options['local_ga_js'] ) { |
| 235 |
$screen = get_current_screen(); |
| 236 |
// Check if the current page is related to Rank Math or Analytify. |
| 237 |
if ( $screen && ( strpos( $screen->id, 'analytify' ) !== false ) ) { |
| 238 |
echo '<div id="message" class="notice notice-warning is-dismissible analytify-rank-math-notice"> |
| 239 |
<p>' . wp_kses( |
| 240 |
sprintf( // translators: Rank match notice. |
| 241 |
__( 'Kindly note that Rank Math Self-Hosted Analytics JS File Feature is available in %1$s Analytify %2$s as well. We recommend using Analytify for this functionality.', 'wp-analytify' ), |
| 242 |
'<a style="text-decoration:none" href="' . esc_url( menu_page_url( 'analytify-settings', false ) ) . '#wp-analytify-advanced">', |
| 243 |
'</a>' |
| 244 |
), |
| 245 |
array( |
| 246 |
'a' => array( |
| 247 |
'href' => array(), |
| 248 |
'style' => array(), |
| 249 |
), |
| 250 |
) |
| 251 |
) . '</p></div>'; ?> |
| 252 |
<script type="text/javascript"> |
| 253 |
(function($) { |
| 254 |
$(document).on('click', '.analytify-rank-math-notice .notice-dismiss', function() { |
| 255 |
$.post(ajaxurl, { |
| 256 |
action: 'analytify_dismiss_rank_math_notice', |
| 257 |
nonce: '<?php echo esc_js( wp_create_nonce( 'analytify_dismiss_rank_math_notice' ) ); ?>' |
| 258 |
}); |
| 259 |
}); |
| 260 |
})(jQuery); |
| 261 |
</script> |
| 262 |
<?php |
| 263 |
} elseif ( $screen && strpos( $screen->id, 'rank-math' ) !== false ) { |
| 264 |
echo '<div id="message" class="rank-math-notice notice is-dismissible"> |
| 265 |
<p>' . wp_kses( |
| 266 |
sprintf( // translators: Rank match notice. |
| 267 |
__( 'Kindly note that Rank Math Self-Hosted Analytics JS File Feature is available in %1$s Analytify %2$s as well. We recommend using Analytify for this functionality.', 'wp-analytify' ), |
| 268 |
'<a style="text-decoration:none" href="' . esc_url( menu_page_url( 'analytify-settings', false ) ) . '#wp-analytify-advanced">', |
| 269 |
'</a>' |
| 270 |
), |
| 271 |
array( |
| 272 |
'a' => array( |
| 273 |
'href' => array(), |
| 274 |
'style' => array(), |
| 275 |
), |
| 276 |
) |
| 277 |
) . '</p></div>'; |
| 278 |
?> |
| 279 |
<script type="text/javascript"> |
| 280 |
(function($) { |
| 281 |
$(document).on('click', '.rank-math-notice .notice-dismiss', function() { |
| 282 |
$.post(ajaxurl, { |
| 283 |
action: 'analytify_dismiss_rank_math_notice', |
| 284 |
nonce: '<?php echo esc_js( wp_create_nonce( 'analytify_dismiss_rank_math_notice' ) ); ?>' |
| 285 |
}); |
| 286 |
}); |
| 287 |
})(jQuery); |
| 288 |
</script> |
| 289 |
<?php |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Check if GA4 mode is enabled. |
| 298 |
* |
| 299 |
* @since 8.1.1 |
| 300 |
* @version 9.1.0 |
| 301 |
* @return bool |
| 302 |
*/ |
| 303 |
private function analytify_is_ga4_enabled() { |
| 304 |
if ( class_exists( 'WPANALYTIFY_Utils' ) && method_exists( 'WPANALYTIFY_Utils', 'get_ga_mode' ) ) { |
| 305 |
return 'ga4' === WPANALYTIFY_Utils::get_ga_mode(); |
| 306 |
} |
| 307 |
|
| 308 |
return (bool) get_option( 'analytify_ga4_mode', false ); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Check if current screen is Analytify page. |
| 313 |
* |
| 314 |
* @since 8.1.1 |
| 315 |
* @version 8.1.2 |
| 316 |
* @return bool |
| 317 |
*/ |
| 318 |
private function analytify_is_analytify_screen() { |
| 319 |
$analytify_pages = array( |
| 320 |
'toplevel_page_analytify-dashboard', |
| 321 |
'analytify_page_analytify-settings', |
| 322 |
'analytify_page_analytify-goals', |
| 323 |
'analytify_page_analytify-woocommerce', |
| 324 |
'analytify_page_analytify-authors', |
| 325 |
'edd-dashboard', |
| 326 |
'dashboard', |
| 327 |
'analytify_page_analytify-dimensions', |
| 328 |
'analytify_page_analytify-campaigns', |
| 329 |
'analytify_page_analytify-addons', |
| 330 |
); |
| 331 |
|
| 332 |
$current_screen = get_current_screen() ? get_current_screen()->base : ''; |
| 333 |
return in_array( $current_screen, $analytify_pages, true ); |
| 334 |
} |
| 335 |
} |
| 336 |
|