class-help-center.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Class WPSEO_Help_Center
  9. */
  10. class WPSEO_Help_Center {
  11. /**
  12. * The tabs in the help center.
  13. *
  14. * @var WPSEO_Option_Tab[] $tab
  15. */
  16. private $tabs;
  17. /**
  18. * Mount point in the HTML.
  19. *
  20. * @var string
  21. */
  22. private $identifier = 'yoast-help-center-container';
  23. /**
  24. * Additional help center items.
  25. *
  26. * @var array
  27. */
  28. protected $help_center_items = array();
  29. /**
  30. * Show premium support tab.
  31. *
  32. * @var bool
  33. */
  34. protected $premium_support;
  35. /**
  36. * WPSEO_Help_Center constructor.
  37. *
  38. * @param string $unused Backwards compatible argument.
  39. * @param WPSEO_Option_Tabs|WPSEO_Option_Tab $option_tabs Currently displayed tabs.
  40. * @param boolean $premium_support Show premium support tab.
  41. */
  42. public function __construct( $unused, $option_tabs, $premium_support = false ) {
  43. $this->premium_support = $premium_support;
  44. $tabs = new WPSEO_Option_Tabs( '' );
  45. if ( $option_tabs instanceof WPSEO_Option_Tabs ) {
  46. $tabs = $option_tabs;
  47. }
  48. if ( $option_tabs instanceof WPSEO_Option_Tab ) {
  49. $tabs = new WPSEO_Option_Tabs( '', $option_tabs->get_name() );
  50. $tabs->add_tab( $option_tabs );
  51. }
  52. $this->tabs = $tabs;
  53. }
  54. /**
  55. * Localize data required by the help center component.
  56. */
  57. public function localize_data() {
  58. $this->add_contact_support_item();
  59. $this->enqueue_localized_data( $this->format_data( $this->tabs->get_tabs() ) );
  60. }
  61. /**
  62. * Format the required data for localized script.
  63. *
  64. * @param WPSEO_Option_Tab[] $tabs Yoast admin pages navigational tabs.
  65. *
  66. * @return array Associative array containing data for help center component.
  67. */
  68. protected function format_data( array $tabs ) {
  69. $formatted_data = array( 'tabs' => array() );
  70. foreach ( $tabs as $tab ) {
  71. $formatted_data['tabs'][ $tab->get_name() ] = array(
  72. 'label' => $tab->get_label(),
  73. 'videoUrl' => $tab->get_video_url(),
  74. 'id' => $tab->get_name(),
  75. );
  76. }
  77. $active_tab = $this->tabs->get_active_tab();
  78. $active_tab = ( null === $active_tab ) ? $tabs[0] : $active_tab;
  79. $formatted_data['mountId'] = $this->identifier;
  80. $formatted_data['initialTab'] = $active_tab->get_name();
  81. $is_premium = WPSEO_Utils::is_yoast_seo_premium();
  82. // Will translate to either empty string or "1" in localised script.
  83. $formatted_data['isPremium'] = $is_premium;
  84. $formatted_data['pluginVersion'] = WPSEO_VERSION;
  85. // Open HelpScout on activating this tab ID.
  86. $formatted_data['shouldDisplayContactForm'] = $this->premium_support;
  87. $formatted_data['translations'] = self::get_translated_texts();
  88. $formatted_data['videoDescriptions'] = array();
  89. if ( $is_premium === false ) {
  90. $formatted_data['videoDescriptions'][] = array(
  91. 'title' => __( 'Need some help?', 'wordpress-seo' ),
  92. 'description' => __( 'Go Premium and our experts will be there for you to answer any questions you might have about the setup and use of the plugin.', 'wordpress-seo' ),
  93. 'link' => WPSEO_Shortlinker::get( 'https://yoa.st/seo-premium-vt' ),
  94. 'linkText' => __( 'Get Yoast SEO Premium now »', 'wordpress-seo' ),
  95. );
  96. $formatted_data['videoDescriptions'][] = array(
  97. 'title' => __( 'Want to be a Yoast SEO Expert?', 'wordpress-seo' ),
  98. 'description' => __( 'Follow our Yoast SEO for WordPress training and become a certified Yoast SEO Expert!', 'wordpress-seo' ),
  99. 'link' => WPSEO_Shortlinker::get( 'https://yoa.st/wordpress-training-vt' ),
  100. 'linkText' => __( 'Enroll in the Yoast SEO for WordPress training »', 'wordpress-seo' ),
  101. );
  102. }
  103. $formatted_data['contactSupportParagraphs'] = array(
  104. array(
  105. 'image' => array(
  106. 'src' => esc_url( plugin_dir_url( WPSEO_FILE ) . 'images/support-team.svg' ),
  107. 'width' => 100,
  108. 'height' => 100,
  109. 'alt' => '',
  110. ),
  111. 'content' => null,
  112. ),
  113. array(
  114. 'image' => null,
  115. 'content' => __( 'If you have a problem that you can\'t solve with our video tutorials or knowledge base, you can send a message to our support team. They can be reached 24/7.', 'wordpress-seo' ),
  116. ),
  117. array(
  118. 'image' => null,
  119. 'content' => __( 'Support requests you create here are sent directly into our support system, which is secured with 256 bit SSL, so communication is 100% secure.', 'wordpress-seo' ),
  120. ),
  121. );
  122. $formatted_data['extraTabs'] = $this->get_extra_tabs();
  123. return $formatted_data;
  124. }
  125. /**
  126. * Get additional tabs for the help center component.
  127. *
  128. * @return array Additional help center tabs.
  129. */
  130. protected function get_extra_tabs() {
  131. $help_center_items = apply_filters( 'wpseo_help_center_items', $this->help_center_items );
  132. return array_map( array( $this, 'format_helpcenter_tab' ), $help_center_items );
  133. }
  134. /**
  135. * Convert WPSEO_Help_Center_Item into help center format.
  136. *
  137. * @param WPSEO_Help_Center_Item $item The item to convert.
  138. *
  139. * @return array Formatted item.
  140. */
  141. protected function format_helpcenter_tab( WPSEO_Help_Center_Item $item ) {
  142. return array(
  143. 'identifier' => $item->get_identifier(),
  144. 'label' => $item->get_label(),
  145. 'content' => $item->get_content(),
  146. );
  147. }
  148. /**
  149. * Enqueue localized script for help center component.
  150. *
  151. * @param array $data Data to localize.
  152. */
  153. protected function enqueue_localized_data( $data ) {
  154. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'help-center', 'wpseoHelpCenterData', $data );
  155. $yoast_components_l10n = new WPSEO_Admin_Asset_Yoast_Components_L10n();
  156. $yoast_components_l10n->localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'help-center' );
  157. }
  158. /**
  159. * Outputs the help center div.
  160. */
  161. public function mount() {
  162. echo '<div id="' . esc_attr( $this->identifier ) . '">' . esc_html__( 'Loading help center.', 'wordpress-seo' ) . '</div>';
  163. }
  164. /**
  165. * Add the contact support help center item to the help center.
  166. */
  167. private function add_contact_support_item() {
  168. /* translators: %s: expands to 'Yoast SEO Premium'. */
  169. $popup_title = sprintf( __( 'Email support is a %s feature', 'wordpress-seo' ), 'Yoast SEO Premium' );
  170. $popup_content = '<p class="yoast-measure">' . __( 'Go Premium and our experts will be there for you to answer any questions you might have about the set-up and use of the plug-in!', 'wordpress-seo' ) . '</p>';
  171. /* translators: %1$s: expands to 'Yoast SEO Premium'. */
  172. $popup_content .= '<p>' . sprintf( __( 'Other benefits of %1$s for you:', 'wordpress-seo' ), 'Yoast SEO Premium' ) . '</p>';
  173. $popup_content .= '<ul class="wpseo-premium-advantages-list">';
  174. $popup_content .= '<li>' . sprintf(
  175. // We don't use strong text here, but we do use it in the "Add keyword" popup, this is just to have the same translatable strings.
  176. /* translators: %1$s expands to a 'strong' start tag, %2$s to a 'strong' end tag. */
  177. __( '%1$sNo more dead links%2$s: easy redirect manager', 'wordpress-seo' ), '', ''
  178. ) . '</li>';
  179. $popup_content .= '<li>' . __( 'Superfast internal links suggestions', 'wordpress-seo' ) . '</li>';
  180. $popup_content .= '<li>' . sprintf(
  181. // We don't use strong text here, but we do use it in the "Add keyword" popup, this is just to have the same translatable strings.
  182. /* translators: %1$s expands to a 'strong' start tag, %2$s to a 'strong' end tag. */
  183. __( '%1$sSocial media preview%2$s: Facebook &amp; Twitter', 'wordpress-seo' ), '', ''
  184. ) . '</li>';
  185. $popup_content .= '<li>' . __( '24/7 support', 'wordpress-seo' ) . '</li>';
  186. $popup_content .= '<li>' . __( 'No ads!', 'wordpress-seo' ) . '</li>';
  187. $popup_content .= '</ul>';
  188. $premium_popup = new WPSEO_Premium_Popup( 'contact-support', 'h2', $popup_title, $popup_content, WPSEO_Shortlinker::get( 'https://yoa.st/contact-support' ) );
  189. $contact_support_help_center_item = new WPSEO_Help_Center_Item(
  190. 'contact-support',
  191. __( 'Get support', 'wordpress-seo' ),
  192. array( 'content' => $premium_popup->get_premium_message( false ) ),
  193. 'dashicons-email-alt'
  194. );
  195. $this->help_center_items[] = $contact_support_help_center_item;
  196. }
  197. /**
  198. * Pass text variables to js for the help center JS module.
  199. *
  200. * %s is replaced with <code>%s</code> and replaced again in the javascript with the actual variable.
  201. *
  202. * @return array Translated text strings for the help center.
  203. */
  204. public static function get_translated_texts() {
  205. // Esc_html is not needed because React already handles HTML in the (translations of) these strings.
  206. return array(
  207. 'locale' => WPSEO_Utils::get_user_locale(),
  208. 'videoTutorial' => __( 'Video tutorial', 'wordpress-seo' ),
  209. 'knowledgeBase' => __( 'Knowledge base', 'wordpress-seo' ),
  210. 'getSupport' => __( 'Get support', 'wordpress-seo' ),
  211. 'algoliaSearcher.loadingPlaceholder' => __( 'Loading...', 'wordpress-seo' ),
  212. 'algoliaSearcher.errorMessage' => __( 'Something went wrong. Please try again later.', 'wordpress-seo' ),
  213. 'searchBar.headingText' => __( 'Search the Yoast Knowledge Base for answers to your questions:', 'wordpress-seo' ),
  214. 'searchBar.placeholderText' => __( 'Type here to search...', 'wordpress-seo' ),
  215. 'searchBar.buttonText' => __( 'Search', 'wordpress-seo' ),
  216. 'searchResultDetail.openButton' => __( 'View in KB', 'wordpress-seo' ),
  217. 'searchResultDetail.openButtonLabel' => __( 'Open the knowledge base article in a new window or read it in the iframe below', 'wordpress-seo' ),
  218. 'searchResultDetail.backButton' => __( 'Go back', 'wordpress-seo' ),
  219. 'searchResultDetail.backButtonLabel' => __( 'Go back to the search results', 'wordpress-seo' ),
  220. 'searchResultDetail.iframeTitle' => __( 'Knowledge base article', 'wordpress-seo' ),
  221. 'searchResultDetail.searchResult' => __( 'Search result', 'wordpress-seo' ),
  222. 'searchResult.noResultsText' => __( 'No results found.', 'wordpress-seo' ),
  223. 'searchResult.foundResultsText' => sprintf(
  224. /* translators: %s expands to the number of results found . */
  225. __( 'Number of results found: %s', 'wordpress-seo' ),
  226. '{ resultsCount }'
  227. ),
  228. 'searchResult.searchResultsHeading' => __( 'Search results', 'wordpress-seo' ),
  229. 'a11yNotice.opensInNewTab' => __( '(Opens in a new browser tab)', 'wordpress-seo' ),
  230. 'contactSupport.button' => __( 'New support request', 'wordpress-seo' ),
  231. 'helpCenter.buttonText' => __( 'Need help?', 'wordpress-seo' ),
  232. );
  233. }
  234. /**
  235. * Outputs the help center.
  236. *
  237. * @deprecated 5.6
  238. */
  239. public function output_help_center() {
  240. _deprecated_function( 'WPSEO_Help_Center::output_help_center', 'WPSEO 5.6.0', 'WPSEO_Help_Center::mount()' );
  241. $this->mount();
  242. }
  243. }