Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 346 |
|
0.00% |
0 / 21 |
CRAP | |
0.00% |
0 / 1 |
| Help_Center | |
0.00% |
0 / 345 |
|
0.00% |
0 / 21 |
13572 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
42 | |||
| calypso_preferences_update | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
42 | |||
| determine_iso_639_locale | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| jetpack_remove_core_help_tab | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| is_proxied | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| init | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| is_next_steps_tutorial_enabled | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| enqueue_script | |
0.00% |
0 / 102 |
|
0.00% |
0 / 1 |
600 | |||
| is_menu_panel_enabled | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
90 | |||
| get_current_site | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
30 | |||
| register_rest_api | |
0.00% |
0 / 45 |
|
0.00% |
0 / 1 |
2 | |||
| is_admin_bar | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| is_block_editor | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| is_wc_admin_home_page | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| is_jetpack_disconnected | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| is_loading_on_frontend | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
20 | |||
| get_help_center_url | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
| get_assets_json | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| enqueue_customizer_scripts | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
| add_help_center_container | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| enqueue_wp_admin_scripts | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
650 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Help center |
| 4 | * |
| 5 | * @package automattic/jetpack-mu-wpcom |
| 6 | */ |
| 7 | |
| 8 | namespace A8C\FSE; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 12 | use Automattic\Jetpack\Status\Host; |
| 13 | |
| 14 | /** |
| 15 | * Class Help_Center |
| 16 | */ |
| 17 | class Help_Center { |
| 18 | /** |
| 19 | * Class instance. |
| 20 | * |
| 21 | * @var Help_Center |
| 22 | */ |
| 23 | private static $instance = null; |
| 24 | |
| 25 | /** |
| 26 | * Whether the current site is a support site. |
| 27 | * |
| 28 | * @var bool |
| 29 | */ |
| 30 | private $is_support_site = false; |
| 31 | |
| 32 | /** |
| 33 | * The purchases of the current site. |
| 34 | * |
| 35 | * @var array |
| 36 | */ |
| 37 | private $purchases = array(); |
| 38 | |
| 39 | /** |
| 40 | * Help_Center constructor. |
| 41 | */ |
| 42 | public function __construct() { |
| 43 | if ( function_exists( 'wpcom_get_site_purchases' ) ) { |
| 44 | $this->purchases = wp_list_filter( wpcom_get_site_purchases(), array( 'product_type' => 'bundle' ) ); |
| 45 | } |
| 46 | |
| 47 | $blog_id = get_current_blog_id(); |
| 48 | $this->is_support_site = ( defined( 'WPCOM_SUPPORT_BLOG_IDS' ) && in_array( $blog_id, (array) WPCOM_SUPPORT_BLOG_IDS, true ) ) || ( defined( 'WPCOM_FORUM_BLOG_IDS' ) && in_array( $blog_id, (array) WPCOM_FORUM_BLOG_IDS, true ) ); |
| 49 | |
| 50 | // Always register REST API endpoints. |
| 51 | add_action( 'rest_api_init', array( $this, 'register_rest_api' ) ); |
| 52 | add_filter( 'calypso_preferences_update', array( $this, 'calypso_preferences_update' ) ); |
| 53 | |
| 54 | // Handle customizer separately. |
| 55 | if ( is_customize_preview() ) { |
| 56 | add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_customizer_scripts' ) ); |
| 57 | add_action( 'customize_controls_print_footer_scripts', array( $this, 'add_help_center_container' ) ); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_wp_admin_scripts' ), 100 ); |
| 62 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_wp_admin_scripts' ), 100 ); |
| 63 | add_action( 'next_admin_init', array( $this, 'enqueue_wp_admin_scripts' ), 1000 ); |
| 64 | add_filter( 'in_admin_header', array( $this, 'jetpack_remove_core_help_tab' ) ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Update the calypso preferences. |
| 69 | * |
| 70 | * @param \stdClass $preferences The preferences. |
| 71 | * @return \stdClass The preferences. |
| 72 | */ |
| 73 | public function calypso_preferences_update( $preferences ) { |
| 74 | // Check if help_center_router_history exists and is a valid array structure |
| 75 | if ( ! isset( $preferences->help_center_router_history ) || |
| 76 | ! is_array( $preferences->help_center_router_history ) ) { |
| 77 | return $preferences; |
| 78 | } |
| 79 | |
| 80 | $router_history = $preferences->help_center_router_history; |
| 81 | |
| 82 | // Check if entries exist and is an array |
| 83 | if ( ! isset( $router_history['entries'] ) || |
| 84 | ! is_array( $router_history['entries'] ) ) { |
| 85 | return $preferences; |
| 86 | } |
| 87 | |
| 88 | $entries = $router_history['entries']; |
| 89 | |
| 90 | // Limit entries to 50 to prevent spamming entries in the router history. |
| 91 | if ( count( $entries ) > 50 ) { |
| 92 | // Keep only the last 49 entries and add the root entry at the beginning. |
| 93 | $entries = array_slice( $entries, -49 ); |
| 94 | // Keep the start at root so the back button always works. |
| 95 | array_unshift( |
| 96 | $entries, |
| 97 | array( |
| 98 | 'pathname' => '/', |
| 99 | 'search' => '', |
| 100 | 'hash' => '', |
| 101 | 'key' => 'default', |
| 102 | 'state' => null, |
| 103 | ) |
| 104 | ); |
| 105 | |
| 106 | // Update the preferences object directly |
| 107 | $preferences->help_center_router_history['entries'] = $entries; |
| 108 | $preferences->help_center_router_history['index'] = 49; |
| 109 | } |
| 110 | |
| 111 | return $preferences; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns ISO 639 conforming locale string of the current user. |
| 116 | * |
| 117 | * @return string ISO 639 locale string e.g. "en" |
| 118 | */ |
| 119 | private static function determine_iso_639_locale() { |
| 120 | $language = get_user_locale(); |
| 121 | $language = strtolower( $language ); |
| 122 | |
| 123 | if ( in_array( $language, array( 'pt_br', 'pt-br', 'zh_tw', 'zh-tw', 'zh_cn', 'zh-cn' ), true ) ) { |
| 124 | $language = str_replace( '_', '-', $language ); |
| 125 | } else { |
| 126 | $language = preg_replace( '/([-_].*)$/i', '', $language ); |
| 127 | } |
| 128 | |
| 129 | if ( empty( $language ) ) { |
| 130 | return 'en'; |
| 131 | } |
| 132 | |
| 133 | return $language; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * We prefer to use the Help Center instead of the Help tab. |
| 138 | */ |
| 139 | public function jetpack_remove_core_help_tab() { |
| 140 | ?> |
| 141 | <style>#contextual-help-link-wrap { display: none; }</style> |
| 142 | <?php |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns whether the current request is coming from the a8c proxy. |
| 147 | */ |
| 148 | private static function is_proxied() { |
| 149 | return isset( $_SERVER['A8C_PROXIED_REQUEST'] ) |
| 150 | ? sanitize_text_field( wp_unslash( $_SERVER['A8C_PROXIED_REQUEST'] ) ) |
| 151 | : defined( 'A8C_PROXIED_REQUEST' ) && A8C_PROXIED_REQUEST; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Creates instance. |
| 156 | * |
| 157 | * @return void |
| 158 | */ |
| 159 | public static function init() { |
| 160 | $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; |
| 161 | |
| 162 | if ( str_contains( $request_uri, 'wp-content/plugins/gutenberg-core' ) || str_contains( $request_uri, 'preview=true' ) ) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | if ( self::$instance === null ) { |
| 167 | self::$instance = new self(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Acts as a feature flag, returning a boolean for whether we should show the next steps tutorial UI. |
| 173 | * |
| 174 | * @return boolean |
| 175 | */ |
| 176 | public static function is_next_steps_tutorial_enabled() { |
| 177 | return apply_filters( |
| 178 | 'help_center_should_enable_next_steps_tutorial', |
| 179 | false |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Enqueue Help Center assets. |
| 185 | * |
| 186 | * @param string $variant The variant of the asset file to get. |
| 187 | * @param array $dependencies The asset file to get. |
| 188 | * @param string $version The version of the asset file to get. |
| 189 | */ |
| 190 | public function enqueue_script( $variant, $dependencies, $version ) { |
| 191 | $script_dependencies = $dependencies ?? array(); |
| 192 | |
| 193 | if ( $variant === 'wp-admin' || $variant === 'wp-admin-disconnected' ) { |
| 194 | add_action( |
| 195 | 'admin_bar_menu', |
| 196 | function ( $wp_admin_bar ) { |
| 197 | $wp_admin_bar->add_menu( |
| 198 | array( |
| 199 | 'id' => 'help-center', |
| 200 | 'title' => '<span title="' . __( 'Help Center', 'jetpack-mu-wpcom' ) . '"><svg id="help-center-icon" class="ab-icon" width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> |
| 201 | <path fill="currentColor" fill-rule="evenodd" clip-rule="evenodd" d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm-1 16v-2h2v2h-2zm2-3v-1.141A3.991 3.991 0 0016 10a4 4 0 00-8 0h2c0-1.103.897-2 2-2s2 .897 2 2-.897 2-2 2a1 1 0 00-1 1v2h2z" /> |
| 202 | </svg> |
| 203 | <svg id="help-center-icon-with-notification" class="ab-icon" width="24" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 204 | <path d="M12 2C6.477 2 2 6.477 2 12C2 17.523 6.477 22 12 22C17.523 22 22 17.523 22 12C22 6.477 17.523 2 12 2ZM13 18H11V16H13V18ZM13 13.859V15H11V13C11 12.448 11.448 12 12 12C13.103 12 14 11.103 14 10C14 8.897 13.103 8 12 8C10.897 8 10 8.897 10 10H8C8 7.791 9.791 6 12 6C14.209 6 16 7.791 16 10C16 11.862 14.722 13.413 13 13.859Z" fill="currentColor"/> |
| 205 | <circle cx="20" cy="3.5" r="4.3" fill="#e65054" stroke="#1d2327" stroke-width="2"/> |
| 206 | </svg> |
| 207 | </span>', |
| 208 | 'parent' => 'top-secondary', |
| 209 | 'href' => $this->get_help_center_url(), |
| 210 | 'meta' => array( |
| 211 | 'html' => '<div id="help-center-masterbar" />', |
| 212 | 'class' => 'menupop', |
| 213 | 'target' => '_blank', |
| 214 | ), |
| 215 | ) |
| 216 | ); |
| 217 | }, |
| 218 | // Add the help center icon to the admin bar after the reader icon. |
| 219 | 12 |
| 220 | ); |
| 221 | |
| 222 | if ( is_user_logged_in() && $variant === 'wp-admin' && $this->is_menu_panel_enabled() ) { |
| 223 | // Initialize the help center menu panel |
| 224 | require_once __DIR__ . '/class-help-center-menu-panel.php'; |
| 225 | Help_Center_Menu_Panel::init(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if ( $variant !== 'wp-admin-disconnected' && $variant !== 'gutenberg-disconnected' ) { |
| 230 | $locale = self::determine_iso_639_locale(); |
| 231 | |
| 232 | if ( 'en' !== $locale ) { |
| 233 | // Load translations directly from widgets.wp.com. |
| 234 | wp_enqueue_script( |
| 235 | 'help-center-translations', |
| 236 | 'https://widgets.wp.com/help-center/languages/' . $locale . '-v1.js', |
| 237 | array( 'wp-i18n' ), |
| 238 | $version, |
| 239 | true |
| 240 | ); |
| 241 | |
| 242 | $script_dependencies[] = 'help-center-translations'; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // If the user is not connected, the Help Center icon will link to the support page. |
| 247 | // The disconnected version is significantly smaller than the connected version. |
| 248 | wp_enqueue_script( |
| 249 | 'help-center', |
| 250 | 'https://widgets.wp.com/help-center/help-center-' . $variant . '.min.js', |
| 251 | $script_dependencies, |
| 252 | $version, |
| 253 | true |
| 254 | ); |
| 255 | |
| 256 | wp_enqueue_style( |
| 257 | 'help-center-' . $variant . '-style', |
| 258 | 'https://widgets.wp.com/help-center/help-center-' . $variant . ( is_rtl() ? '.rtl.css' : '.css' ), |
| 259 | array(), |
| 260 | $version |
| 261 | ); |
| 262 | |
| 263 | // This information is only needed for the connected version of the help center. |
| 264 | if ( $variant !== 'wp-admin-disconnected' && $variant !== 'gutenberg-disconnected' ) { |
| 265 | // Adds feature flags for development. |
| 266 | wp_add_inline_script( |
| 267 | 'help-center', |
| 268 | 'const helpCenterFeatureFlags = ' . wp_json_encode( |
| 269 | array( |
| 270 | 'loadNextStepsTutorial' => self::is_next_steps_tutorial_enabled(), |
| 271 | ), |
| 272 | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP |
| 273 | ), |
| 274 | 'before' |
| 275 | ); |
| 276 | |
| 277 | $user_id = get_current_user_id(); |
| 278 | $user_data = get_userdata( $user_id ); |
| 279 | $username = $user_data ? $user_data->user_login : null; |
| 280 | $user_email = $user_data ? $user_data->user_email : null; |
| 281 | $display_name = $user_data ? $user_data->display_name : null; |
| 282 | $avatar_url = $user_data ? ( function_exists( 'wpcom_get_avatar_url' ) ? wpcom_get_avatar_url( $user_email, 64, '', true )[0] : get_avatar_url( $user_id ) ) : null; |
| 283 | $is_commerce_garden = defined( 'IS_COMMERCE_GARDEN' ); |
| 284 | |
| 285 | wp_add_inline_script( |
| 286 | 'help-center', |
| 287 | 'const helpCenterData = ' . wp_json_encode( |
| 288 | array( |
| 289 | 'isProxied' => boolval( self::is_proxied() ), |
| 290 | 'isSU' => defined( 'WPCOM_SUPPORT_SESSION' ) && WPCOM_SUPPORT_SESSION, |
| 291 | 'isSSP' => isset( $_COOKIE['ssp'] ), |
| 292 | 'sectionName' => $this->is_support_site ? 'wp.com/support' : $variant, |
| 293 | 'isCommerceGarden' => $is_commerce_garden, |
| 294 | 'currentUser' => array( |
| 295 | 'ID' => $user_id, |
| 296 | 'username' => $username, |
| 297 | 'display_name' => $display_name, |
| 298 | 'avatar_URL' => $avatar_url, |
| 299 | 'email' => $user_email, |
| 300 | ), |
| 301 | 'site' => $this->get_current_site(), |
| 302 | 'locale' => self::determine_iso_639_locale(), |
| 303 | ), |
| 304 | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP |
| 305 | ), |
| 306 | 'before' |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | $should_enqueue_wp_components = ! is_admin() || ( is_customize_preview() && ( new Host() )->is_wpcom_simple() ); |
| 311 | |
| 312 | if ( $should_enqueue_wp_components ) { |
| 313 | $stylesheet = is_rtl() ? 'build/components/style-rtl.css' : 'build/components/style.css'; |
| 314 | $stylesheet_url = plugins_url( 'gutenberg/' . $stylesheet ); |
| 315 | |
| 316 | if ( function_exists( 'gutenberg_url' ) ) { |
| 317 | // @phan-suppress-next-line PhanUndeclaredFunction |
| 318 | $stylesheet_url = gutenberg_url( $stylesheet ); |
| 319 | } |
| 320 | // Enqueue wp-component styles because they're not enqueued in wp-admin outside of the editor. |
| 321 | wp_enqueue_style( |
| 322 | 'wp-components', |
| 323 | $stylesheet_url, |
| 324 | array( 'dashicons' ), |
| 325 | $version |
| 326 | ); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Returns whether the menu panel experiment is enabled for the current user. |
| 332 | * |
| 333 | * @return boolean True if the menu panel experiment variation is enabled, false otherwise. |
| 334 | */ |
| 335 | private function is_menu_panel_enabled() { |
| 336 | $experiment_name = 'calypso_help_center_menu_popover_increase_exposure'; |
| 337 | $experiment_variation = 'menu_popover'; |
| 338 | $user_id = get_current_user_id(); |
| 339 | $cache_key = 'help-center-menu-panel-enabled-' . $user_id . '-' . $experiment_name; |
| 340 | |
| 341 | // Check cache first. |
| 342 | $cached_result = get_transient( $cache_key ); |
| 343 | if ( false !== $cached_result ) { |
| 344 | return (bool) $cached_result; |
| 345 | } |
| 346 | |
| 347 | $result = false; |
| 348 | |
| 349 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 350 | $result = $experiment_variation === \ExPlat\assign_current_user( $experiment_name ); |
| 351 | } elseif ( ( new Connection_Manager() )->is_user_connected() ) { |
| 352 | $request_path = '/experiments/0.1.0/assignments/calypso'; |
| 353 | $response = Client::wpcom_json_api_request_as_user( |
| 354 | add_query_arg( array( 'experiment_name' => $experiment_name ), $request_path ), |
| 355 | 'v2' |
| 356 | ); |
| 357 | |
| 358 | if ( ! is_wp_error( $response ) ) { |
| 359 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 360 | if ( 200 === $response_code ) { |
| 361 | $data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 362 | if ( isset( $data['variations'] ) && isset( $data['variations'][ $experiment_name ] ) ) { |
| 363 | $variation = $data['variations'][ $experiment_name ]; |
| 364 | $result = $experiment_variation === $variation; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Cache the result for 1 hour. |
| 371 | set_transient( $cache_key, $result ? 1 : 0, HOUR_IN_SECONDS ); |
| 372 | |
| 373 | return $result; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Get current site details. |
| 378 | */ |
| 379 | public function get_current_site() { |
| 380 | if ( $this->is_support_site ) { |
| 381 | return null; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Atomic sites have the WP.com blog ID stored as a Jetpack option. This code deliberately |
| 386 | * doesn't use `Jetpack_Options::get_option` so it works even when Jetpack has not been loaded. |
| 387 | */ |
| 388 | $jetpack_options = get_option( 'jetpack_options' ); |
| 389 | if ( is_array( $jetpack_options ) && isset( $jetpack_options['id'] ) ) { |
| 390 | $site = (int) $jetpack_options['id']; |
| 391 | } else { |
| 392 | $site = get_current_blog_id(); |
| 393 | } |
| 394 | |
| 395 | $logo_id = get_option( 'site_logo' ); |
| 396 | $bundles = $this->purchases; |
| 397 | $plan = array_pop( $bundles ); |
| 398 | |
| 399 | $return_data = array( |
| 400 | 'ID' => $site, |
| 401 | 'name' => get_bloginfo( 'name' ), |
| 402 | 'URL' => get_bloginfo( 'url' ), |
| 403 | 'plan' => array( |
| 404 | 'product_slug' => $plan->product_slug ?? null, |
| 405 | ), |
| 406 | 'is_wpcom_atomic' => defined( 'IS_ATOMIC' ) && IS_ATOMIC, |
| 407 | 'jetpack' => true === apply_filters( 'is_jetpack_site', false, $site ), |
| 408 | 'logo' => array( |
| 409 | 'id' => $logo_id, |
| 410 | 'sizes' => array(), |
| 411 | 'url' => wp_get_attachment_image_src( $logo_id, 'thumbnail' )[0] ?? '', |
| 412 | ), |
| 413 | 'options' => array( |
| 414 | 'launchpad_screen' => get_option( 'launchpad_screen' ), |
| 415 | 'site_intent' => get_option( 'site_intent' ), |
| 416 | 'admin_url' => get_admin_url(), |
| 417 | ), |
| 418 | ); |
| 419 | |
| 420 | return $return_data; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Register the Help Center endpoints. |
| 425 | */ |
| 426 | public function register_rest_api() { |
| 427 | require_once __DIR__ . '/class-wp-rest-help-center-authenticate.php'; |
| 428 | $controller = new WP_REST_Help_Center_Authenticate(); |
| 429 | $controller->register_rest_route(); |
| 430 | |
| 431 | require_once __DIR__ . '/class-wp-rest-help-center-sibyl.php'; |
| 432 | $controller = new WP_REST_Help_Center_Sibyl(); |
| 433 | $controller->register_rest_route(); |
| 434 | |
| 435 | require_once __DIR__ . '/class-wp-rest-help-center-support-status.php'; |
| 436 | $controller = new WP_REST_Help_Center_Support_Status(); |
| 437 | $controller->register_rest_route(); |
| 438 | |
| 439 | require_once __DIR__ . '/class-wp-rest-help-center-search.php'; |
| 440 | $controller = new WP_REST_Help_Center_Search(); |
| 441 | $controller->register_rest_route(); |
| 442 | |
| 443 | require_once __DIR__ . '/class-wp-rest-help-center-jetpack-search-ai.php'; |
| 444 | $controller = new WP_REST_Help_Center_Jetpack_Search_AI(); |
| 445 | $controller->register_rest_route(); |
| 446 | |
| 447 | require_once __DIR__ . '/class-wp-rest-help-center-fetch-post.php'; |
| 448 | $controller = new WP_REST_Help_Center_Fetch_Post(); |
| 449 | $controller->register_rest_route(); |
| 450 | |
| 451 | require_once __DIR__ . '/class-wp-rest-help-center-ticket.php'; |
| 452 | $controller = new WP_REST_Help_Center_Ticket(); |
| 453 | $controller->register_rest_route(); |
| 454 | |
| 455 | require_once __DIR__ . '/class-wp-rest-help-center-forum.php'; |
| 456 | $controller = new WP_REST_Help_Center_Forum(); |
| 457 | $controller->register_rest_route(); |
| 458 | |
| 459 | require_once __DIR__ . '/class-wp-rest-help-center-support-activity.php'; |
| 460 | $controller = new WP_REST_Help_Center_Support_Activity(); |
| 461 | $controller->register_rest_route(); |
| 462 | |
| 463 | require_once __DIR__ . '/class-wp-rest-help-center-support-interactions.php'; |
| 464 | $controller = new WP_REST_Help_Center_Support_Interactions(); |
| 465 | $controller->register_rest_route(); |
| 466 | |
| 467 | require_once __DIR__ . '/class-wp-rest-help-center-user-fields.php'; |
| 468 | $controller = new WP_REST_Help_Center_User_Fields(); |
| 469 | $controller->register_rest_route(); |
| 470 | |
| 471 | require_once __DIR__ . '/class-wp-rest-help-center-odie.php'; |
| 472 | $controller = new WP_REST_Help_Center_Odie(); |
| 473 | $controller->register_rest_route(); |
| 474 | |
| 475 | require_once __DIR__ . '/class-wp-rest-help-center-persisted-open-state.php'; |
| 476 | $controller = new WP_REST_Help_Center_Persisted_Open_State(); |
| 477 | $controller->register_rest_route(); |
| 478 | |
| 479 | require_once __DIR__ . '/class-wp-rest-help-center-email-support-enabled.php'; |
| 480 | $controller = new WP_REST_Help_Center_Email_Support_Enabled(); |
| 481 | $controller->register_rest_route(); |
| 482 | |
| 483 | require_once __DIR__ . '/class-wp-rest-help-center-ticket-csat.php'; |
| 484 | $controller = new WP_REST_Help_Center_Ticket_CSAT(); |
| 485 | $controller->register_rest_route(); |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Returns true if the admin bar is set. |
| 490 | */ |
| 491 | public function is_admin_bar() { |
| 492 | global $wp_admin_bar; |
| 493 | return is_object( $wp_admin_bar ); |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Returns true if the current screen if the block editor. |
| 498 | */ |
| 499 | public function is_block_editor() { |
| 500 | global $current_screen; |
| 501 | |
| 502 | if ( ! $current_screen ) { |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | // widgets screen does have the block editor but also no Gutenberg top bar. |
| 507 | return $current_screen->is_block_editor() && $current_screen->id !== 'widgets'; |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Returns true if the current screen is the woo commerce admin home page. |
| 512 | */ |
| 513 | private function is_wc_admin_home_page() { |
| 514 | global $current_screen; |
| 515 | return $current_screen && $current_screen->id === 'woocommerce_page_wc-admin'; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Returns true if the current user is connected through Jetpack |
| 520 | */ |
| 521 | public function is_jetpack_disconnected() { |
| 522 | $user_id = get_current_user_id(); |
| 523 | $blog_id = get_current_blog_id(); |
| 524 | |
| 525 | if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) { |
| 526 | return ! ( new Connection_Manager( 'jetpack' ) )->is_user_connected( $user_id ); |
| 527 | } |
| 528 | |
| 529 | if ( true === apply_filters( 'is_jetpack_site', false, $blog_id ) ) { |
| 530 | return ! ( new Connection_Manager( 'jetpack' ) )->is_user_connected( $user_id ); |
| 531 | } |
| 532 | |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Returns true if... |
| 538 | * 1. The current user can edit posts. |
| 539 | * 2. The current user is a member of the blog. |
| 540 | * 3. The current request is not in the admin. |
| 541 | * 4. The current request is not in the block editor. |
| 542 | * |
| 543 | * @return bool True if the this is being loaded on the frontend. |
| 544 | */ |
| 545 | public function is_loading_on_frontend() { |
| 546 | $can_edit_posts = current_user_can( 'edit_posts' ) && is_user_member_of_blog(); |
| 547 | |
| 548 | return ! is_admin() && ! $this->is_block_editor() && $can_edit_posts; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Returns the URL for the Help Center redirect. |
| 553 | * Used for the Help Center when disconnected. |
| 554 | */ |
| 555 | public function get_help_center_url() { |
| 556 | $help_url = 'https://wordpress.com/help?help-center=home'; |
| 557 | |
| 558 | if ( $this->is_jetpack_disconnected() || ( $this->is_loading_on_frontend() && ! $this->is_support_site ) ) { |
| 559 | return $help_url; |
| 560 | } |
| 561 | |
| 562 | return false; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Get the asset via file-system on wpcom and via network on Atomic sites. |
| 567 | * |
| 568 | * @param string $filepath The URL to download the asset file from. |
| 569 | */ |
| 570 | private static function get_assets_json( $filepath ) { |
| 571 | $accessible_directly = file_exists( ABSPATH . '/' . $filepath ); |
| 572 | if ( $accessible_directly ) { |
| 573 | return json_decode( file_get_contents( ABSPATH . $filepath ), true ); |
| 574 | } |
| 575 | $request = wp_remote_get( 'https://' . $filepath ); |
| 576 | if ( is_wp_error( $request ) ) { |
| 577 | return null; |
| 578 | } |
| 579 | return json_decode( wp_remote_retrieve_body( $request ), true ); |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Enqueue Help Center assets for the customizer. |
| 584 | */ |
| 585 | public function enqueue_customizer_scripts() { |
| 586 | if ( $this->is_jetpack_disconnected() ) { |
| 587 | $variant = 'wp-admin-disconnected'; |
| 588 | } else { |
| 589 | $variant = 'customizer'; |
| 590 | } |
| 591 | |
| 592 | $cache_key = 'help-center-asset-' . $variant . '.asset.json'; |
| 593 | $asset_file = get_transient( $cache_key ); |
| 594 | |
| 595 | if ( ! $asset_file ) { |
| 596 | $asset_file = self::get_assets_json( 'widgets.wp.com/help-center/help-center-' . $variant . '.asset.json' ); |
| 597 | if ( ! $asset_file ) { |
| 598 | return; |
| 599 | } |
| 600 | set_transient( $cache_key, $asset_file, HOUR_IN_SECONDS ); |
| 601 | } |
| 602 | |
| 603 | // When the request is proxied, use a random cache buster as the version for easier debugging. |
| 604 | $version = self::is_proxied() ? wp_rand() : $asset_file['version']; |
| 605 | |
| 606 | $this->enqueue_script( $variant, $asset_file['dependencies'], $version ); |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * Add Help Center container div in customizer. |
| 611 | */ |
| 612 | public function add_help_center_container() { |
| 613 | ?> |
| 614 | <div id="help-center-customizer"></div> |
| 615 | <?php |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Add icon to WP-ADMIN admin bar. |
| 620 | */ |
| 621 | public function enqueue_wp_admin_scripts() { |
| 622 | if ( $this->is_wc_admin_home_page() ) { |
| 623 | return; |
| 624 | } |
| 625 | $is_next_admin = (bool) did_action( 'next_admin_init' ); |
| 626 | |
| 627 | require_once ABSPATH . 'wp-admin/includes/screen.php'; |
| 628 | |
| 629 | $can_edit_posts = current_user_can( 'edit_posts' ) && is_user_member_of_blog(); |
| 630 | $is_p2 = str_contains( get_stylesheet(), 'pub/p2' ) || function_exists( '\WPForTeams\is_wpforteams_site' ) && is_wpforteams_site( get_current_blog_id() ); |
| 631 | |
| 632 | // We will show the help center icon in the admin bar when; |
| 633 | // 1. On wp-admin |
| 634 | // 2. On the front end of the site if the current user can edit posts |
| 635 | // 3. On the front end of the site and the theme is not P2 |
| 636 | // 4. If it is the frontend we show the disconnected version of the help center. |
| 637 | if ( ! is_admin() && ( ! $can_edit_posts || $is_p2 ) && ! $this->is_support_site ) { |
| 638 | return; |
| 639 | } |
| 640 | |
| 641 | // Do not load Help Center for logged-out users if we are not on support sites and the experiment variation is the treatment. |
| 642 | if ( ! is_user_logged_in() ) { |
| 643 | if ( ! $this->is_support_site ) { |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | $experiment_variation = null; |
| 648 | if ( function_exists( '\ExPlat\assign_maybe_anon_user' ) ) { |
| 649 | $experiment_variation = \ExPlat\assign_maybe_anon_user( 'wpcom_ai_on_logged_out_support_pages_v2' ); |
| 650 | } else { |
| 651 | log2logstash( |
| 652 | array( |
| 653 | 'feature' => 'help-center', |
| 654 | 'message' => 'ExPlat\assign_maybe_anon_user function is unavailable', |
| 655 | ) |
| 656 | ); |
| 657 | } |
| 658 | if ( $experiment_variation !== 'treatment' ) { |
| 659 | return; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | if ( $is_next_admin ) { |
| 664 | $variant = 'ciab-admin' . ( $this->is_jetpack_disconnected() ? '-disconnected' : '' ); |
| 665 | } elseif ( $this->is_support_site ) { |
| 666 | if ( ! is_user_logged_in() ) { |
| 667 | $variant = 'logged-out'; |
| 668 | } else { |
| 669 | $variant = 'wp-admin' . ( $this->is_jetpack_disconnected() ? '-disconnected' : '' ); |
| 670 | } |
| 671 | } elseif ( $this->is_loading_on_frontend() ) { |
| 672 | $variant = 'wp-admin-disconnected'; |
| 673 | } elseif ( $this->is_block_editor() ) { |
| 674 | $variant = 'gutenberg' . ( $this->is_jetpack_disconnected() ? '-disconnected' : '' ); |
| 675 | } else { |
| 676 | $variant = 'wp-admin' . ( $this->is_jetpack_disconnected() ? '-disconnected' : '' ); |
| 677 | } |
| 678 | |
| 679 | $cache_key = 'help-center-asset-' . $variant . '.asset.json'; |
| 680 | $asset_file = get_transient( $cache_key ); |
| 681 | |
| 682 | if ( ! $asset_file ) { |
| 683 | $asset_file = self::get_assets_json( 'widgets.wp.com/help-center/help-center-' . $variant . '.asset.json' ); |
| 684 | if ( ! $asset_file ) { |
| 685 | return; |
| 686 | } |
| 687 | set_transient( $cache_key, $asset_file, HOUR_IN_SECONDS ); |
| 688 | } |
| 689 | |
| 690 | // When the request is proxied, use a random cache buster as the version for easier debugging. |
| 691 | $version = self::is_proxied() ? wp_rand() : $asset_file['version']; |
| 692 | |
| 693 | $this->enqueue_script( $variant, $asset_file['dependencies'], $version ); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | add_action( 'init', array( __NAMESPACE__ . '\Help_Center', 'init' ) ); |