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