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