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_page.php
229 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('clarity_project_id', clarity_project_id_default_value()); |
| 28 | if (empty($clarity_project_id_option)) { |
| 29 | refresh_wordpress_id_option(); |
| 30 | } else { |
| 31 | generate_wordpress_id_option_if_empty(); |
| 32 | } |
| 33 | $clarity_project_id_option = get_option( |
| 34 | 'clarity_project_id', /* option */ |
| 35 | clarity_project_id_default_value() /* default */ |
| 36 | ); |
| 37 | $clarity_wp_site = get_option( |
| 38 | 'clarity_wordpress_site_id' /* option */ |
| 39 | /* default */ |
| 40 | ); |
| 41 | |
| 42 | $clarity_domain = "https://clarity.microsoft.com/embed"; |
| 43 | |
| 44 | $query_params = "?nonce=$nonce&integration=Wordpress&wpsite=$clarity_wp_site"; |
| 45 | |
| 46 | // set a QP if user is admin |
| 47 | if(current_user_can('manage_options')) { |
| 48 | $query_params = $query_params . "&WPAdmin=1"; |
| 49 | } |
| 50 | |
| 51 | // initially set iframe src to the new users path |
| 52 | $iframe_src = $clarity_domain.$query_params; |
| 53 | |
| 54 | // clarity project exist |
| 55 | if(!empty($clarity_project_id_option)) |
| 56 | { |
| 57 | $iframe_src = $iframe_src."&project=".$clarity_project_id_option; |
| 58 | } |
| 59 | |
| 60 | ?> |
| 61 | <div style="width:100%;padding-right:15px;margin-top:20px;box-sizing:border-box;"> |
| 62 | <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="1300px" title="Microsoft Clarity" /> |
| 63 | </div> |
| 64 | <?php |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * clarity project id default value is empty string |
| 69 | **/ |
| 70 | function clarity_project_id_default_value() { |
| 71 | return ''; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Generates a menu page |
| 76 | **/ |
| 77 | |
| 78 | add_action('admin_menu', 'clarity_page_generation'); |
| 79 | function clarity_page_generation() { |
| 80 | add_menu_page( |
| 81 | 'microsoft-clarity', /* $page_title */ |
| 82 | 'Clarity', /* menu_title */ |
| 83 | 'edit_posts', /* capability */ |
| 84 | 'microsoft-clarity', /* menu_slug */ |
| 85 | 'clarity_section_iframe_callback', /* callback */ |
| 86 | 'https://claritystatic.blob.core.windows.net/images/logo.svg', /* icon_url */ |
| 87 | 99 /* position */ |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Register Plugin settings |
| 93 | * clarity_project_id: option for currently integrated Clarity project id |
| 94 | * clarity_wordpress_site_id: a guid generated by the Clarity plugin to uniquely identify this wordpress site |
| 95 | * clarity_section_iframe_callback: part of the settings page in which the iframe is embedded |
| 96 | **/ |
| 97 | add_action('admin_init', 'clarity_register_settings'); |
| 98 | function clarity_register_settings() { |
| 99 | register_setting( |
| 100 | 'clarity_settings_fields', /* $option_group */ |
| 101 | 'clarity_project_id' /* option_name */ |
| 102 | /* args */ |
| 103 | ); |
| 104 | register_setting( |
| 105 | 'general', /* $option_group */ |
| 106 | 'clarity_wordpress_site_id' /* option_name */ |
| 107 | /* args */ |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Notice for when wordpress admins did not finish intalling Clarity |
| 113 | * did not integrate a project |
| 114 | */ |
| 115 | add_action('admin_notices', 'setup_clarity_notice__info'); |
| 116 | function setup_clarity_notice__info() { |
| 117 | global $pagenow; |
| 118 | $url = get_admin_url() . 'admin.php?page=microsoft-clarity'; |
| 119 | |
| 120 | $clarity_project_id_option = get_option( |
| 121 | 'clarity_project_id', /* option */ |
| 122 | clarity_project_id_default_value() /* default */ |
| 123 | ); |
| 124 | $pageQPExists = isset($_GET['page']); |
| 125 | if($pageQPExists) { |
| 126 | $pageQP = $_GET['page']; |
| 127 | } else { |
| 128 | $pageQP = ""; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | if(empty($clarity_project_id_option) && $pageQP !== "microsoft-clarity" && current_user_can("manage_options")) { |
| 133 | echo |
| 134 | '<div class="notice notice-info is-dismissible"> |
| 135 | <p style="font-weight:700"> |
| 136 | Please setup Clarity to start understanding user behavior on your site. |
| 137 | </p> |
| 138 | <p> |
| 139 | <a class="button-primary" href="'. $url .'"> |
| 140 | Setup Clarity |
| 141 | </a> |
| 142 | </p> |
| 143 | </div>'; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Add js function to listen to message on all admin pages |
| 149 | * These message contain changes to integrated Clarity project |
| 150 | * remove - change - add new |
| 151 | */ |
| 152 | add_action('admin_enqueue_scripts', 'add_event_listeners'); |
| 153 | function add_event_listeners($hook) { |
| 154 | $pageQPExists = isset($_GET['page']); |
| 155 | if($pageQPExists) { |
| 156 | $pageQP = $_GET['page']; |
| 157 | } else { |
| 158 | $pageQP = ""; |
| 159 | } |
| 160 | |
| 161 | if($pageQP !== "microsoft-clarity") { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | if(!current_user_can("edit_posts")) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | wp_register_script( |
| 170 | 'window_listeners_js', /* handle */ |
| 171 | plugins_url('js\add_window_listeners.js', __FILE__ ), /* src */ |
| 172 | array(), /* deps */ |
| 173 | false, /* ver */ |
| 174 | false /* in_footer */ |
| 175 | ); |
| 176 | wp_enqueue_script( |
| 177 | 'window_listeners_js' /* handle */ |
| 178 | /* src */ |
| 179 | /* deps */ |
| 180 | /* ver */ |
| 181 | /* in_footer */ |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Add callback triggered when a new message is received |
| 187 | * Edits the clarity project id option respectively |
| 188 | */ |
| 189 | add_action('wp_ajax_edit_clarity_project_id', "edit_clarity_project_id"); |
| 190 | function edit_clarity_project_id() { |
| 191 | $new_value = $_POST['new_value']; |
| 192 | $nonce = $_POST['nonce']; |
| 193 | if(!wp_verify_nonce($nonce, "wp_ajax_edit_clarity_project_id")) { |
| 194 | die( |
| 195 | json_encode( |
| 196 | array( |
| 197 | 'success' => false, |
| 198 | 'message' => 'Invalid nonce.', |
| 199 | ) |
| 200 | ) |
| 201 | ); |
| 202 | } |
| 203 | // only admins are allowed to edit the Clarity project id |
| 204 | if (!current_user_can('manage_options')) { |
| 205 | die( |
| 206 | json_encode( |
| 207 | array( |
| 208 | 'success' => false, |
| 209 | 'message' => 'User must be WordPress admin.' |
| 210 | ) |
| 211 | ) |
| 212 | ); |
| 213 | } else { |
| 214 | update_option( |
| 215 | 'clarity_project_id', /* option */ |
| 216 | $new_value /* value */ |
| 217 | /* autoload */ |
| 218 | ); |
| 219 | die( |
| 220 | json_encode( |
| 221 | array( |
| 222 | 'success' => true, |
| 223 | 'message' => 'Clarity project updated successfully.' |
| 224 | ) |
| 225 | ) |
| 226 | ); |
| 227 | } |
| 228 | } |
| 229 |