microsoft-clarity
Last commit date
js
4 months ago
LICENSE.txt
5 years ago
clarity-collect-batch.php
3 months ago
clarity-collect-storage.php
3 months ago
clarity-hooks.php
4 months ago
clarity-page.php
4 months ago
clarity-server-analytics.php
4 months ago
clarity.php
3 months ago
index.php
5 years ago
readme.txt
3 months ago
clarity-hooks.php
192 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Includes Clarity custom hooks available for use by other plugins. |
| 5 | * |
| 6 | * @package Clarity |
| 7 | * @since 0.10.0 |
| 8 | */ |
| 9 | |
| 10 | add_filter('clrt_integrate_with_clarity', 'clrt_integrate_with_clarity_handler'); |
| 11 | |
| 12 | /** |
| 13 | * Integrates the user's WordPress site with Clarity and invites the site users to the created project. |
| 14 | * |
| 15 | * @since 0.10.0 |
| 16 | * |
| 17 | * @param string $referrer This value is sent from the external plugin calling the hook. |
| 18 | * @return bool|WP_Error True on success or 'WP_Error' in case of failure. |
| 19 | */ |
| 20 | function clrt_integrate_with_clarity_handler($referrer) |
| 21 | { |
| 22 | // Ensure the current user has admin privileges. |
| 23 | if (! current_user_can('install_plugins')) { |
| 24 | return new WP_Error('invalid_user_role', 'This hook requires admin privileges'); |
| 25 | } |
| 26 | |
| 27 | $is_integrated = get_option('clarity_project_id'); |
| 28 | |
| 29 | if ($is_integrated) { |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | $project_id = clrt_get_clarity_wordpress_site_id(); |
| 34 | $url = get_site_url(); |
| 35 | $site_name = wp_parse_url($url, PHP_URL_HOST); |
| 36 | $users = clrt_get_mapped_users(); |
| 37 | $integrations = clrt_get_integrations(); |
| 38 | |
| 39 | $body = wp_json_encode( |
| 40 | array( |
| 41 | 'projectId' => $project_id, |
| 42 | 'url' => $url, |
| 43 | 'siteName' => $site_name, |
| 44 | 'users' => $users, |
| 45 | 'integrations' => $integrations, |
| 46 | ) |
| 47 | ); |
| 48 | |
| 49 | $api_url = "https://clarity.azure-api.net/provision?referrer=$referrer"; |
| 50 | |
| 51 | $args = array( |
| 52 | 'body' => $body, |
| 53 | 'headers' => array( |
| 54 | 'Content-Type' => 'application/json', |
| 55 | ), |
| 56 | ); |
| 57 | |
| 58 | $result = wp_remote_post($api_url, $args); |
| 59 | |
| 60 | $error_codes = array(400, 403, 429, 500); |
| 61 | if (is_wp_error($result) || in_array($result['response']['code'], $error_codes, true)) { |
| 62 | return new WP_Error('request_error', 'An error occurred when sending the request'); |
| 63 | } else { |
| 64 | update_option('clarity_project_id', $result['body']); |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Gets the WordPress site ID from the options table or creates a new one if it doesn't exist. |
| 71 | * |
| 72 | * @since 0.10.0 |
| 73 | * |
| 74 | * @return string The WordPress site ID. |
| 75 | */ |
| 76 | function clrt_get_clarity_wordpress_site_id() |
| 77 | { |
| 78 | $id = get_option('clarity_wordpress_site_id'); |
| 79 | |
| 80 | if (! $id) { |
| 81 | $id = wp_generate_uuid4(); |
| 82 | update_option('clarity_wordpress_site_id', $id); |
| 83 | } |
| 84 | |
| 85 | return $id; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Extracts the WordPress site users and maps them to the format required by the Clarity api. |
| 90 | * |
| 91 | * @since 0.10.0 |
| 92 | * |
| 93 | * @return array[] An array of mapped users data. |
| 94 | */ |
| 95 | function clrt_get_mapped_users() |
| 96 | { |
| 97 | define('MAX_NUMBER_OF_USERS', 10); // Max number allowed by the Clarity API. |
| 98 | |
| 99 | $requesting_user = wp_get_current_user(); |
| 100 | |
| 101 | $requesting_user_id = array($requesting_user->ID); |
| 102 | |
| 103 | $users[] = clrt_map_user($requesting_user, true); |
| 104 | |
| 105 | if (is_multisite()) { |
| 106 | $super_admins = get_users( |
| 107 | array( |
| 108 | 'exclude' => $requesting_user_id, |
| 109 | 'capability' => 'manage_network', |
| 110 | ) |
| 111 | ); |
| 112 | $super_admins = array_map('clrt_map_user', $super_admins); |
| 113 | |
| 114 | $users = array_merge($users, $super_admins); |
| 115 | } else { |
| 116 | $admins = get_users( |
| 117 | array( |
| 118 | 'exclude' => $requesting_user_id, |
| 119 | 'role' => 'administrator', |
| 120 | ) |
| 121 | ); |
| 122 | $admins = array_map('clrt_map_user', $admins); |
| 123 | |
| 124 | $editors = get_users( |
| 125 | array( |
| 126 | 'exclude' => $requesting_user_id, |
| 127 | 'role' => 'editor', |
| 128 | ) |
| 129 | ); |
| 130 | $editors = array_map('clrt_map_user', $editors); |
| 131 | |
| 132 | $users = array_merge($users, $admins, $editors); |
| 133 | } |
| 134 | |
| 135 | $users = array_slice($users, 0, MAX_NUMBER_OF_USERS); |
| 136 | |
| 137 | return $users; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Maps a WordPress user to a Clarity user. |
| 142 | * |
| 143 | * @since 0.10.0 |
| 144 | * |
| 145 | * @param WP_User $user The WordPress user object. |
| 146 | * @param bool $is_requesting_user Indicates whether the user is the one calling the integration hook. |
| 147 | * @return array An associative array containing the mapped user data. |
| 148 | */ |
| 149 | function clrt_map_user($user, $is_requesting_user = false) |
| 150 | { |
| 151 | return array( |
| 152 | 'email' => $user->user_email, |
| 153 | 'role' => clrt_map_user_role($user, $is_requesting_user), |
| 154 | 'provider' => 'codeInvite', |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Maps a WordPress user role to a Clarity role. |
| 160 | * |
| 161 | * @since 0.10.0 |
| 162 | * |
| 163 | * @param WP_User $user The WordPress user object. |
| 164 | * @param bool $is_requesting_user Indicates whether the user is the one calling the integration hook. |
| 165 | * @return string The mapped user role. |
| 166 | */ |
| 167 | function clrt_map_user_role($user, $is_requesting_user) |
| 168 | { |
| 169 | if ($is_requesting_user) { |
| 170 | return 'requestingAdmin'; |
| 171 | } |
| 172 | |
| 173 | return in_array('editor', $user->roles, true) ? 'member' : 'admin'; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns a list of integrations that contains only WordPress integration data. |
| 178 | * |
| 179 | * @since 0.10.0 |
| 180 | * |
| 181 | * @return array[] An array containing WordPress integration data. |
| 182 | */ |
| 183 | function clrt_get_integrations() |
| 184 | { |
| 185 | return array( |
| 186 | array( |
| 187 | 'id' => clrt_get_clarity_wordpress_site_id(), |
| 188 | 'integrationType' => 'Wordpress', |
| 189 | ), |
| 190 | ); |
| 191 | } |
| 192 |