PluginProbe ʕ •ᴥ•ʔ
Microsoft Clarity / 0.10.9
Microsoft Clarity v0.10.9
0.10.26 0.10.25 0.10.24 0.8.0 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 trunk 0.10.0 0.10.1 0.10.10 0.10.11 0.10.12 0.10.13 0.10.14 0.10.15 0.10.16 0.10.17 0.10.18 0.10.19 0.10.2 0.10.20 0.10.21 0.10.22 0.10.23 0.10.3 0.10.4 0.10.5 0.10.6 0.10.7 0.10.8 0.10.9 0.2 0.4 0.5 0.6 0.6.1 0.7 0.7.1 0.7.2 0.7.3 0.7.4 0.7.5
microsoft-clarity / clarity-page.php
microsoft-clarity Last commit date
js 2 years ago LICENSE.txt 5 years ago clarity-hooks.php 1 year ago clarity-page.php 1 year ago clarity-server-analytics.php 7 months ago clarity.php 7 months ago index.php 5 years ago readme.txt 7 months ago
clarity-page.php
233 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 **/
93 add_action('admin_init', 'clarity_register_settings');
94 function clarity_register_settings() {
95 register_setting(
96 'clarity_settings_fields', /* $option_group */
97 'clarity_project_id' /* option_name */
98 /* args */
99 );
100 register_setting(
101 'general', /* $option_group */
102 'clarity_wordpress_site_id' /* option_name */
103 /* args */
104 );
105 }
106
107 /**
108 * Notice for when wordpress admins did not finish intalling Clarity
109 * did not integrate a project
110 */
111 add_action('admin_notices', 'setup_clarity_notice__info');
112 function setup_clarity_notice__info() {
113 global $pagenow;
114 $url = get_admin_url() . 'admin.php?page=microsoft-clarity';
115
116 $learnMoreUrl = 'https://wordpress.org/plugins/microsoft-clarity/';
117
118 $clarity_project_id_option = get_option(
119 'clarity_project_id', /* option */
120 clarity_project_id_default_value() /* default */
121 );
122 $pageQPExists = isset($_GET['page']);
123 if($pageQPExists) {
124 $pageQP = $_GET['page'];
125 } else {
126 $pageQP = "";
127 }
128
129
130 if(empty($clarity_project_id_option) && $pageQP !== "microsoft-clarity" && current_user_can("manage_options")) {
131 echo
132 '<div class="notice notice-info is-dismissible">
133 <p style="font-weight:700">
134 Unlock User Insights with Microsoft Clarity!
135 </p>
136 <p style="font-weight:500">
137 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.
138 </p>
139 <p>
140 <a class="button-primary" href="'. $url .'">
141 Setup Clarity
142 </a>
143 <a class="button-primary" style="margin-left:10px" href="'. $learnMoreUrl .'">
144 Learn more
145 </a>
146 </p>
147 </div>';
148 }
149 }
150
151 /**
152 * Add js function to listen to message on all admin pages
153 * These message contain changes to integrated Clarity project
154 * remove - change - add new
155 */
156 add_action('admin_enqueue_scripts', 'add_event_listeners');
157 function add_event_listeners($hook) {
158 $pageQPExists = isset($_GET['page']);
159 if($pageQPExists) {
160 $pageQP = $_GET['page'];
161 } else {
162 $pageQP = "";
163 }
164
165 if($pageQP !== "microsoft-clarity") {
166 return;
167 }
168
169 if(!current_user_can("edit_posts")) {
170 return;
171 }
172
173 wp_register_script(
174 'window_listeners_js', /* handle */
175 plugins_url('js\add_window_listeners.js', __FILE__ ), /* src */
176 array(), /* deps */
177 false, /* ver */
178 false /* in_footer */
179 );
180 wp_enqueue_script(
181 'window_listeners_js' /* handle */
182 /* src */
183 /* deps */
184 /* ver */
185 /* in_footer */
186 );
187 }
188
189 /**
190 * Add callback triggered when a new message is received
191 * Edits the clarity project id option respectively
192 */
193 add_action('wp_ajax_edit_clarity_project_id', "edit_clarity_project_id");
194 function edit_clarity_project_id() {
195 $new_value = $_POST['new_value'];
196 $nonce = $_POST['nonce'];
197 if(!wp_verify_nonce($nonce, "wp_ajax_edit_clarity_project_id")) {
198 die(
199 json_encode(
200 array(
201 'success' => false,
202 'message' => 'Invalid nonce.',
203 )
204 )
205 );
206 }
207 // only admins are allowed to edit the Clarity project id
208 if (!current_user_can('manage_options')) {
209 die(
210 json_encode(
211 array(
212 'success' => false,
213 'message' => 'User must be WordPress admin.'
214 )
215 )
216 );
217 } else {
218 update_option(
219 'clarity_project_id', /* option */
220 $new_value /* value */
221 /* autoload */
222 );
223 die(
224 json_encode(
225 array(
226 'success' => true,
227 'message' => 'Clarity project updated successfully.'
228 )
229 )
230 );
231 }
232 }
233