class-inline-help.php
2 years ago
gridicon-help.svg
5 years ago
inline-help-template.php
2 years ago
inline-help.css
5 years ago
class-inline-help.php
116 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Inline Help. |
| 4 | * |
| 5 | * Handles providing a LiveChat icon within WPAdmin until such time |
| 6 | * as the full live chat experience can be run in a non-Calypso environment. |
| 7 | * |
| 8 | * @package automattic/jetpack |
| 9 | */ |
| 10 | |
| 11 | namespace Automattic\Jetpack\Dashboard_Customizations; |
| 12 | |
| 13 | /** |
| 14 | * Class Inline_Help. |
| 15 | */ |
| 16 | class Inline_Help { |
| 17 | |
| 18 | /** |
| 19 | * Inline_Help constructor. |
| 20 | */ |
| 21 | public function __construct() { |
| 22 | add_action( 'current_screen', array( $this, 'register_actions' ) ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Registers actions. |
| 27 | * |
| 28 | * @param object $current_screen Current screen object. |
| 29 | * @return void |
| 30 | */ |
| 31 | public function register_actions( $current_screen ) { |
| 32 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 33 | // Do not inject the FAB icon on embedded screens since the parent window may already contain a FAB icon. |
| 34 | $is_framed = ! empty( $_GET['frame-nonce'] ); |
| 35 | |
| 36 | // Do not inject the FAB icon on Yoast screens to avoid overlap with the Yoast help icon. |
| 37 | $is_yoast = ! empty( $current_screen->base ) && str_contains( $current_screen->base, '_page_wpseo_' ); |
| 38 | |
| 39 | if ( $is_framed || $is_yoast ) { |
| 40 | return; |
| 41 | } |
| 42 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 43 | |
| 44 | add_action( 'admin_footer', array( $this, 'add_fab_icon' ) ); |
| 45 | |
| 46 | add_action( 'admin_enqueue_scripts', array( $this, 'add_fab_styles' ) ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Outputs "FAB" icon markup and SVG. |
| 51 | * |
| 52 | * @return void|string the HTML markup for the FAB or early exit. |
| 53 | */ |
| 54 | public function add_fab_icon() { |
| 55 | |
| 56 | if ( wp_doing_ajax() ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | $svg_allowed = array( |
| 61 | 'svg' => array( |
| 62 | 'id' => true, |
| 63 | 'class' => true, |
| 64 | 'aria-hidden' => true, |
| 65 | 'aria-labelledby' => true, |
| 66 | 'role' => true, |
| 67 | 'xmlns' => true, |
| 68 | 'width' => true, |
| 69 | 'height' => true, |
| 70 | 'viewbox' => true, // <= Must be lower case! |
| 71 | ), |
| 72 | 'g' => array( 'fill' => true ), |
| 73 | 'title' => array( 'title' => true ), |
| 74 | 'path' => array( |
| 75 | 'd' => true, |
| 76 | 'fill' => true, |
| 77 | ), |
| 78 | ); |
| 79 | |
| 80 | $gridicon_help = file_get_contents( __DIR__ . '/gridicon-help.svg', true ); |
| 81 | |
| 82 | // Add tracking data to link to be picked up by Calypso for GA and Tracks usage. |
| 83 | $tracking_href = add_query_arg( |
| 84 | array( |
| 85 | 'utm_source' => 'wp_admin', |
| 86 | 'utm_medium' => 'other', |
| 87 | 'utm_content' => 'jetpack_masterbar_inline_help_click', |
| 88 | 'flags' => 'a8c-analytics.on', |
| 89 | ), |
| 90 | 'https://wordpress.com/help' |
| 91 | ); |
| 92 | |
| 93 | // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 94 | // We trust that output in the template has been escaped. |
| 95 | echo load_template( |
| 96 | __DIR__ . '/inline-help-template.php', |
| 97 | true, |
| 98 | array( |
| 99 | 'href' => $tracking_href, |
| 100 | 'icon' => $gridicon_help, |
| 101 | 'svg_allowed' => $svg_allowed, |
| 102 | ) |
| 103 | ); |
| 104 | // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Enqueues FAB CSS styles. |
| 109 | * |
| 110 | * @return void |
| 111 | */ |
| 112 | public function add_fab_styles() { |
| 113 | wp_enqueue_style( 'a8c-faux-inline-help', plugins_url( 'inline-help.css', __FILE__ ), array(), JETPACK__VERSION ); |
| 114 | } |
| 115 | } |
| 116 |