microsoft-clarity
Last commit date
js
2 years ago
LICENSE.txt
5 years ago
clarity-hooks.php
1 year ago
clarity-page.php
6 months ago
clarity-server-analytics.php
7 months ago
clarity.php
6 months ago
index.php
5 years ago
readme.txt
6 months ago
clarity-page.php
343 lines
| 1 | <?php |
| 2 | /******************************************************************************* |
| 3 | * File with Clarity page |
| 4 | *******************************************************************************/ |
| 5 | |
| 6 | function generate_wordpress_id_option_if_empty() { |
| 7 | $clarity_wp_site = get_option('clarity_wordpress_site_id'); |
| 8 | if (empty($clarity_wp_site)) { |
| 9 | update_option('clarity_wordpress_site_id', wp_generate_uuid4()); |
| 10 | }; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * generate a guid identifier for this wordpress site |
| 15 | * runs in the callback of register_activation_hook, rerunning here for existing plugin which updated |
| 16 | **/ |
| 17 | function refresh_wordpress_id_option() { |
| 18 | update_option('clarity_wordpress_site_id', wp_generate_uuid4()); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Displays the embedded iframe in Clarity settings |
| 23 | **/ |
| 24 | function clarity_section_iframe_callback() { |
| 25 | $nonce = wp_create_nonce('wp_ajax_edit_clarity_project_id'); |
| 26 | |
| 27 | $clarity_project_id_option = get_option( |
| 28 | 'clarity_project_id', /* option */ |
| 29 | clarity_project_id_default_value() /* default */ |
| 30 | ); |
| 31 | $clarity_wp_site = get_option( |
| 32 | 'clarity_wordpress_site_id' /* option */ |
| 33 | /* default */ |
| 34 | ); |
| 35 | |
| 36 | $site_url = home_url(); |
| 37 | |
| 38 | $clarity_domain = "https://clarity.microsoft.com/embed"; |
| 39 | |
| 40 | $query_params = "?nonce=$nonce&integration=Wordpress&wpsite=$clarity_wp_site&siteurl=$site_url"; |
| 41 | |
| 42 | // set a QP if user is admin |
| 43 | if(current_user_can('manage_options')) { |
| 44 | $query_params = $query_params . "&WPAdmin=1"; |
| 45 | } |
| 46 | |
| 47 | // initially set iframe src to the new users path |
| 48 | $iframe_src = $clarity_domain.$query_params; |
| 49 | |
| 50 | // clarity project exist |
| 51 | if(!empty($clarity_project_id_option)) |
| 52 | { |
| 53 | $iframe_src = $iframe_src."&project=".$clarity_project_id_option; |
| 54 | } |
| 55 | |
| 56 | ?> |
| 57 | <div style="width:100%;height:100vh;padding-right:15px;margin-top:0px;box-sizing:border-box;"> |
| 58 | <iframe sandbox="allow-modals allow-forms allow-scripts allow-same-origin allow-popups allow-storage-access-by-user-activation" src="<?php echo $iframe_src ?>" width="100%" height="100%" title="Microsoft Clarity" /> |
| 59 | </div> |
| 60 | <?php |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * clarity project id default value is empty string |
| 65 | **/ |
| 66 | function clarity_project_id_default_value() { |
| 67 | return ''; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Generates a menu page |
| 72 | **/ |
| 73 | |
| 74 | add_action('admin_menu', 'clarity_page_generation'); |
| 75 | function clarity_page_generation() { |
| 76 | add_menu_page( |
| 77 | 'microsoft-clarity', /* $page_title */ |
| 78 | 'Clarity', /* menu_title */ |
| 79 | 'edit_posts', /* capability */ |
| 80 | 'microsoft-clarity', /* menu_slug */ |
| 81 | 'clarity_section_iframe_callback', /* callback */ |
| 82 | 'https://claritystatic.blob.core.windows.net/images/logo.svg', /* icon_url */ |
| 83 | 99 /* position */ |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Register Plugin settings |
| 89 | * clarity_project_id: option for currently integrated Clarity project id |
| 90 | * clarity_wordpress_site_id: a guid generated by the Clarity plugin to uniquely identify this wordpress site |
| 91 | * clarity_section_iframe_callback: part of the settings page in which the iframe is embedded |
| 92 | * is_latest_plugin_version: option for checking if the plugin is latest version |
| 93 | **/ |
| 94 | add_action('admin_init', 'clarity_register_settings'); |
| 95 | function clarity_register_settings() { |
| 96 | register_setting( |
| 97 | 'clarity_settings_fields', /* $option_group */ |
| 98 | 'clarity_project_id' /* option_name */ |
| 99 | /* args */ |
| 100 | ); |
| 101 | register_setting( |
| 102 | 'general', /* $option_group */ |
| 103 | 'clarity_wordpress_site_id' /* option_name */ |
| 104 | /* args */ |
| 105 | ); |
| 106 | register_setting( |
| 107 | 'general', /* $option_group */ |
| 108 | 'is_latest_plugin_version' /* option_name */ |
| 109 | /* args */ |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Notice for when wordpress admins did not finish intalling Clarity |
| 115 | * did not integrate a project |
| 116 | */ |
| 117 | add_action('admin_notices', 'setup_clarity_notice__info'); |
| 118 | function setup_clarity_notice__info() { |
| 119 | global $pagenow; |
| 120 | $url = get_admin_url() . 'admin.php?page=microsoft-clarity'; |
| 121 | |
| 122 | $learnMoreUrl = 'https://wordpress.org/plugins/microsoft-clarity/'; |
| 123 | |
| 124 | $clarity_project_id_option = get_option( |
| 125 | 'clarity_project_id', /* option */ |
| 126 | clarity_project_id_default_value() /* default */ |
| 127 | ); |
| 128 | $pageQPExists = isset($_GET['page']); |
| 129 | if($pageQPExists) { |
| 130 | $pageQP = $_GET['page']; |
| 131 | } else { |
| 132 | $pageQP = ""; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | if(empty($clarity_project_id_option) && $pageQP !== "microsoft-clarity" && current_user_can("manage_options")) { |
| 137 | echo |
| 138 | '<div class="notice notice-info is-dismissible"> |
| 139 | <p style="font-weight:700"> |
| 140 | Unlock User Insights with Microsoft Clarity! |
| 141 | </p> |
| 142 | <p style="font-weight:500"> |
| 143 | Almost there! Start tracking user behavior on your site with Microsoft Clarity. See exactly where on your site users click, scroll, and get stuck. It takes just a few moments to set up. |
| 144 | </p> |
| 145 | <p> |
| 146 | <a class="button-primary" href="'. $url .'"> |
| 147 | Setup Clarity |
| 148 | </a> |
| 149 | <a class="button-primary" style="margin-left:10px" href="'. $learnMoreUrl .'"> |
| 150 | Learn more |
| 151 | </a> |
| 152 | </p> |
| 153 | </div>'; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Add js function to listen to message on all admin pages |
| 159 | * These message contain changes to integrated Clarity project |
| 160 | * remove - change - add new |
| 161 | */ |
| 162 | add_action('admin_enqueue_scripts', 'add_event_listeners'); |
| 163 | function add_event_listeners($hook) { |
| 164 | $pageQPExists = isset($_GET['page']); |
| 165 | if($pageQPExists) { |
| 166 | $pageQP = $_GET['page']; |
| 167 | } else { |
| 168 | $pageQP = ""; |
| 169 | } |
| 170 | |
| 171 | if($pageQP !== "microsoft-clarity") { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | if(!current_user_can("edit_posts")) { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | wp_register_script( |
| 180 | 'window_listeners_js', /* handle */ |
| 181 | plugins_url('js\add_window_listeners.js', __FILE__ ), /* src */ |
| 182 | array(), /* deps */ |
| 183 | false, /* ver */ |
| 184 | false /* in_footer */ |
| 185 | ); |
| 186 | wp_enqueue_script( |
| 187 | 'window_listeners_js' /* handle */ |
| 188 | /* src */ |
| 189 | /* deps */ |
| 190 | /* ver */ |
| 191 | /* in_footer */ |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Add callback triggered when a new message is received |
| 197 | * Edits the clarity project id option respectively |
| 198 | */ |
| 199 | add_action('wp_ajax_edit_clarity_project_id', "edit_clarity_project_id"); |
| 200 | function edit_clarity_project_id() { |
| 201 | $new_value = $_POST['new_value']; |
| 202 | $nonce = $_POST['nonce']; |
| 203 | if(!wp_verify_nonce($nonce, "wp_ajax_edit_clarity_project_id")) { |
| 204 | die( |
| 205 | json_encode( |
| 206 | array( |
| 207 | 'success' => false, |
| 208 | 'message' => 'Invalid nonce.', |
| 209 | ) |
| 210 | ) |
| 211 | ); |
| 212 | } |
| 213 | // only admins are allowed to edit the Clarity project id |
| 214 | if (!current_user_can('manage_options')) { |
| 215 | die( |
| 216 | json_encode( |
| 217 | array( |
| 218 | 'success' => false, |
| 219 | 'message' => 'User must be WordPress admin.' |
| 220 | ) |
| 221 | ) |
| 222 | ); |
| 223 | } else { |
| 224 | update_option( |
| 225 | 'clarity_project_id', /* option */ |
| 226 | $new_value /* value */ |
| 227 | /* autoload */ |
| 228 | ); |
| 229 | die( |
| 230 | json_encode( |
| 231 | array( |
| 232 | 'success' => true, |
| 233 | 'message' => 'Clarity project updated successfully.' |
| 234 | ) |
| 235 | ) |
| 236 | ); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Displays an admin notice if the plugin version installed is not the latest |
| 242 | */ |
| 243 | add_action( 'admin_notices', 'plugin_update_notice' ); |
| 244 | function plugin_update_notice() { |
| 245 | // Only show the notice to users who can update plugins |
| 246 | if ( ! current_user_can( 'update_plugins' ) ) { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | if ( get_option( 'is_latest_plugin_version' ) == '1' ) { |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | $plugin_slug = 'microsoft-clarity/clarity.php'; |
| 255 | $update_url = wp_nonce_url( |
| 256 | add_query_arg( |
| 257 | array( |
| 258 | 'action' => 'trigger_plugin_update', |
| 259 | 'plugin' => urlencode( $plugin_slug ), |
| 260 | ), |
| 261 | admin_url( 'admin.php' ) |
| 262 | ), |
| 263 | 'plugin_update_nonce' |
| 264 | ); |
| 265 | |
| 266 | ?> |
| 267 | <div class="notice notice-warning is-dismissible"> |
| 268 | <p style="font-weight:700"> |
| 269 | <?php _e( 'A new version of Microsoft Clarity is available.', 'text-domain' ); ?> |
| 270 | </p> |
| 271 | <p> |
| 272 | <a href="<?php echo esc_url( $update_url ); ?>" class="button button-primary"> |
| 273 | <?php _e( 'Update Now', 'text-domain' ); ?> |
| 274 | </a> |
| 275 | </p> |
| 276 | </div> |
| 277 | <?php |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Updates the plugin to the latest version programmatically |
| 282 | * The upgrade function deactives the plugin by default before the upgrade, hence the need to reactivate it |
| 283 | */ |
| 284 | add_action( 'admin_action_trigger_plugin_update', 'plugin_perform_update' ); |
| 285 | function plugin_perform_update() { |
| 286 | if ( ! current_user_can( 'update_plugins' ) ) { |
| 287 | wp_die( __( 'You do not have sufficient permissions to update plugins.', 'text-domain' ) ); |
| 288 | } |
| 289 | |
| 290 | if ( ! isset( $_GET['plugin'] ) || ! isset( $_GET['_wpnonce'] ) ) { |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | $plugin_slug = sanitize_text_field( urldecode( $_GET['plugin'] ) ); |
| 295 | |
| 296 | if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'plugin_update_nonce' ) ) { |
| 297 | wp_die( __( 'Nonce verification failed.', 'text-domain' ) ); |
| 298 | } |
| 299 | |
| 300 | include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
| 301 | include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 302 | |
| 303 | // Create a custom skin to handle output and redirection |
| 304 | $upgrader_skin = new Automatic_Upgrader_Skin(); |
| 305 | $upgrader = new Plugin_Upgrader( $upgrader_skin ); |
| 306 | |
| 307 | // Perform the update |
| 308 | $updated = $upgrader->upgrade( $plugin_slug ); |
| 309 | |
| 310 | if ( is_wp_error( $updated ) || ! $updated ) { |
| 311 | // Handle error: redirect back to admin page with an error notice |
| 312 | $redirect_url = add_query_arg( 'plugin_update_error', '1', admin_url( 'plugins.php' ) ); |
| 313 | wp_redirect( esc_url( $redirect_url ) ); |
| 314 | exit; |
| 315 | } else { |
| 316 | // Success: redirect back to the plugin page with a success notice |
| 317 | activate_plugin( $plugin_slug ); |
| 318 | $redirect_url = add_query_arg( 'plugin_updated', '1', admin_url( 'admin.php?page=microsoft-clarity' ) ); |
| 319 | wp_redirect( esc_url( $redirect_url ) ); |
| 320 | exit; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Display an admin notice with the status of the plugin update |
| 326 | */ |
| 327 | add_action( 'admin_notices', 'plugin_admin_notices' ); |
| 328 | function plugin_admin_notices() { |
| 329 | if ( isset( $_GET['plugin_updated'] ) && '1' === $_GET['plugin_updated'] ) { |
| 330 | echo |
| 331 | '<div class="notice notice-success is-dismissible"> |
| 332 | <p><strong>Microsoft Clarity plugin has been updated successfully.</strong></p> |
| 333 | </div>'; |
| 334 | |
| 335 | } else if ( isset( $_GET['plugin_update_error'] ) && '1' === $_GET['plugin_update_error'] ) { |
| 336 | echo |
| 337 | '<div class="notice notice-error is-dismissible"> |
| 338 | <p><strong>Microsoft Clarity plugin update failed.</strong></p> |
| 339 | </div>'; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 |