microsoft-clarity
Last commit date
admin
4 years ago
LICENSE.txt
5 years ago
clarity.php
4 years ago
index.php
5 years ago
readme.txt
4 years ago
clarity.php
100 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.7 |
| 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 | /** |
| 14 | * Require files only if is admin area |
| 15 | * @param void |
| 16 | * @return HTML |
| 17 | **/ |
| 18 | if(is_admin()) { |
| 19 | require_once plugin_dir_path(__FILE__).'/admin/settings_callbacks.php'; |
| 20 | require_once plugin_dir_path(__FILE__).'/admin/settings_page.php'; |
| 21 | } |
| 22 | |
| 23 | function clarity_plugin_settings_link($links) { |
| 24 | $url = get_admin_url() . 'options-general.php?page=clarity_settings'; |
| 25 | $settings_link = '<a href="' . $url . '">' . __('Settings', 'textdomain') . '</a>'; |
| 26 | array_unshift($links, $settings_link); |
| 27 | return $links; |
| 28 | } |
| 29 | |
| 30 | function clarity_after_setup_theme() { |
| 31 | add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'clarity_plugin_settings_link'); |
| 32 | } |
| 33 | add_action ('after_setup_theme', 'clarity_after_setup_theme'); |
| 34 | |
| 35 | /** |
| 36 | * Add origins for CORS |
| 37 | * @param void |
| 38 | * @return HTML |
| 39 | **/ |
| 40 | add_filter('allowed_http_origins', 'clarity_add_origins'); |
| 41 | function clarity_add_origins($origins) { |
| 42 | $origins[] = 'https://www.clarity.ms'; |
| 43 | return $origins; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Runs when Clarity Plugin is activated |
| 48 | * @param void |
| 49 | * @return void |
| 50 | **/ |
| 51 | register_activation_hook(__FILE__, 'clarity_on_activation'); |
| 52 | function clarity_on_activation() { |
| 53 | add_option("display_clarity_notice", true); |
| 54 | update_option("display_clarity_notice", true); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Runs when Clarity Plugin is deactivated |
| 59 | * @param void |
| 60 | * @return void |
| 61 | **/ |
| 62 | register_deactivation_hook( __FILE__, 'clarity_on_deactivation'); |
| 63 | function clarity_on_deactivation() { |
| 64 | remove_menu_page( 'clarity_settings' ); |
| 65 | update_option("display_clarity_notice", false); |
| 66 | update_option('clarity_project_id', ''); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | register_uninstall_hook('uninstall.php', 'clarity_on_uninstallation'); |
| 71 | function clarity_on_uninstallation() { |
| 72 | delete_option("display_clarity_notice"); |
| 73 | delete_option('clarity_project_id'); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | function escape_value_for_script($value) { |
| 78 | return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Adds the script to run clarity |
| 83 | * @param void |
| 84 | * @return void |
| 85 | **/ |
| 86 | add_action('wp_head', 'clarity_add_script_to_header'); |
| 87 | function clarity_add_script_to_header(){ |
| 88 | $p_id_option = get_option('clarity_project_id'); |
| 89 | if ($p_id_option) { |
| 90 | ?> |
| 91 | <script type="text/javascript"> |
| 92 | (function(c,l,a,r,i,t,y){ |
| 93 | c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};t=l.createElement(r);t.async=1; |
| 94 | t.src="https://www.clarity.ms/tag/"+i+"?ref=wordpress";y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); |
| 95 | })(window, document, "clarity", "script", "<?php echo escape_value_for_script($p_id_option); ?>"); |
| 96 | </script> |
| 97 | <?php |
| 98 | } |
| 99 | } |
| 100 |