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