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