microsoft-clarity
Last commit date
js
2 years ago
LICENSE.txt
2 years ago
clarity.php
2 years ago
clarity_page.php
2 years ago
index.php
2 years ago
readme.txt
2 years ago
clarity.php
99 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: Microsoft Clarity |
| 4 | * Plugin URI: https://clarity.microsoft.com/ |
| 5 | * Description: With data and session replay from Clarity, you'll see how people are using your site — where they get stuck and what they love. |
| 6 | * Version: 0.9.4 |
| 7 | * Author: Microsoft |
| 8 | * Author URI: https://www.microsoft.com/en-us/ |
| 9 | * License: MIT |
| 10 | * License URI: https://docs.opensource.microsoft.com/content/releasing/license.html |
| 11 | */ |
| 12 | |
| 13 | require_once plugin_dir_path(__FILE__).'/clarity_page.php'; |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * Runs when Clarity Plugin is activated |
| 18 | **/ |
| 19 | register_activation_hook(__FILE__, 'clarity_on_activation'); |
| 20 | function clarity_on_activation() { |
| 21 | $clarity_wordpress_site_id = get_option('clarity_wordpress_site_id'); |
| 22 | // generate an identifier for this wordpress site to Clarity |
| 23 | if (empty($clarity_wordpress_site_id)) { |
| 24 | update_option( |
| 25 | 'clarity_wordpress_site_id', /* option */ |
| 26 | wp_generate_uuid4() /* value */ |
| 27 | /* autoload */ |
| 28 | ); |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Runs when Clarity Plugin is deactivated |
| 34 | **/ |
| 35 | register_deactivation_hook(__FILE__, 'clarity_on_deactivation'); |
| 36 | function clarity_on_deactivation() { |
| 37 | update_option( |
| 38 | 'clarity_project_id', /* option */ |
| 39 | '' /* value */ |
| 40 | /* autoload */ |
| 41 | ); |
| 42 | update_option( |
| 43 | 'clarity_wordpress_site_id', /* option */ |
| 44 | '' /* value */ |
| 45 | /* autoload */ |
| 46 | ); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Runs when Clarity Plugin is uninstalled |
| 52 | **/ |
| 53 | register_uninstall_hook('uninstall.php', 'clarity_on_uninstall'); |
| 54 | function clarity_on_uninstall() { |
| 55 | delete_option( |
| 56 | 'clarity_project_id' /* option */ |
| 57 | ); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * escapes the plugin id characters |
| 63 | **/ |
| 64 | function escape_value_for_script($value) { |
| 65 | return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Adds the script to run clarity |
| 70 | **/ |
| 71 | add_action('wp_head', 'clarity_add_script_to_header'); |
| 72 | function clarity_add_script_to_header(){ |
| 73 | $p_id_option = get_option( |
| 74 | 'clarity_project_id' /* option */ |
| 75 | /* default */ |
| 76 | ); |
| 77 | if (!empty($p_id_option)) { |
| 78 | ?> |
| 79 | <script type="text/javascript"> |
| 80 | (function(c,l,a,r,i,t,y){ |
| 81 | c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};t=l.createElement(r);t.async=1; |
| 82 | t.src="https://www.clarity.ms/tag/"+i+"?ref=wordpress";y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); |
| 83 | })(window, document, "clarity", "script", "<?php echo escape_value_for_script($p_id_option); ?>"); |
| 84 | </script> |
| 85 | <?php |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Adds the page link to the Microsoft Clarity block on installed plugin page |
| 91 | **/ |
| 92 | add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'clarity_page_link'); |
| 93 | function clarity_page_link($links) { |
| 94 | $url = get_admin_url() . 'admin.php?page=microsoft-clarity'; |
| 95 | $clarity_link = "<a href='$url'>" . __('Clarity Dashboard') . '</a>'; |
| 96 | array_unshift($links, $clarity_link); |
| 97 | return $links; |
| 98 | } |
| 99 |