class-metabox-formatter.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Formatter
  6. */
  7. /**
  8. * This class forces needed methods for the metabox localization.
  9. */
  10. class WPSEO_Metabox_Formatter {
  11. /**
  12. * Object that provides formatted values.
  13. *
  14. * @var WPSEO_Metabox_Formatter_Interface
  15. */
  16. private $formatter;
  17. /**
  18. * Setting the formatter property.
  19. *
  20. * @param WPSEO_Metabox_Formatter_Interface $formatter Object that provides the formatted values.
  21. */
  22. public function __construct( WPSEO_Metabox_Formatter_Interface $formatter ) {
  23. $this->formatter = $formatter;
  24. }
  25. /**
  26. * Returns the values.
  27. *
  28. * @return array
  29. */
  30. public function get_values() {
  31. $defaults = $this->get_defaults();
  32. $values = $this->formatter->get_values();
  33. return ( $values + $defaults );
  34. }
  35. /**
  36. * Returns array with all the values always needed by a scraper object.
  37. *
  38. * @return array Default settings for the metabox.
  39. */
  40. private function get_defaults() {
  41. $analysis_seo = new WPSEO_Metabox_Analysis_SEO();
  42. $analysis_readability = new WPSEO_Metabox_Analysis_Readability();
  43. return [
  44. 'language' => WPSEO_Language_Utils::get_site_language_name(),
  45. 'settings_link' => $this->get_settings_link(),
  46. 'search_url' => '',
  47. 'post_edit_url' => '',
  48. 'base_url' => '',
  49. 'contentTab' => __( 'Readability', 'wordpress-seo' ),
  50. 'keywordTab' => __( 'Keyphrase:', 'wordpress-seo' ),
  51. 'removeKeyword' => __( 'Remove keyphrase', 'wordpress-seo' ),
  52. 'contentLocale' => get_locale(),
  53. 'userLocale' => WPSEO_Language_Utils::get_user_locale(),
  54. 'translations' => $this->get_translations(),
  55. 'keyword_usage' => [],
  56. 'title_template' => '',
  57. 'metadesc_template' => '',
  58. 'contentAnalysisActive' => $analysis_readability->is_enabled() ? 1 : 0,
  59. 'keywordAnalysisActive' => $analysis_seo->is_enabled() ? 1 : 0,
  60. 'cornerstoneActive' => WPSEO_Options::get( 'enable_cornerstone_content', false ) ? 1 : 0,
  61. 'intl' => $this->get_content_analysis_component_translations(),
  62. 'isRtl' => is_rtl(),
  63. 'isPremium' => WPSEO_Utils::is_yoast_seo_premium(),
  64. 'addKeywordUpsell' => $this->get_add_keyword_upsell_translations(),
  65. 'wordFormRecognitionActive' => ( WPSEO_Language_Utils::get_language( get_locale() ) === 'en' ),
  66. 'siteIconUrl' => get_site_icon_url(),
  67. /**
  68. * Filter to determine if the markers should be enabled or not.
  69. *
  70. * @param bool $showMarkers Should the markers being enabled. Default = true.
  71. */
  72. 'show_markers' => apply_filters( 'wpseo_enable_assessment_markers', true ),
  73. 'publish_box' => [
  74. 'labels' => [
  75. 'content' => [
  76. 'na' => sprintf(
  77. /* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the readability score. */
  78. __( '%1$sReadability%2$s: %3$s', 'wordpress-seo' ),
  79. '<a href="#yoast-readability-analysis-collapsible-metabox">',
  80. '</a>',
  81. '<strong>' . __( 'Not available', 'wordpress-seo' ) . '</strong>'
  82. ),
  83. 'bad' => sprintf(
  84. /* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the readability score. */
  85. __( '%1$sReadability%2$s: %3$s', 'wordpress-seo' ),
  86. '<a href="#yoast-readability-analysis-collapsible-metabox">',
  87. '</a>',
  88. '<strong>' . __( 'Needs improvement', 'wordpress-seo' ) . '</strong>'
  89. ),
  90. 'ok' => sprintf(
  91. /* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the readability score. */
  92. __( '%1$sReadability%2$s: %3$s', 'wordpress-seo' ),
  93. '<a href="#yoast-readability-analysis-collapsible-metabox">',
  94. '</a>',
  95. '<strong>' . __( 'OK', 'wordpress-seo' ) . '</strong>'
  96. ),
  97. 'good' => sprintf(
  98. /* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the readability score. */
  99. __( '%1$sReadability%2$s: %3$s', 'wordpress-seo' ),
  100. '<a href="#yoast-readability-analysis-collapsible-metabox">',
  101. '</a>',
  102. '<strong>' . __( 'Good', 'wordpress-seo' ) . '</strong>'
  103. ),
  104. ],
  105. 'keyword' => [
  106. 'na' => sprintf(
  107. /* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the SEO score. */
  108. __( '%1$sSEO%2$s: %3$s', 'wordpress-seo' ),
  109. '<a href="#yoast-seo-analysis-collapsible-metabox">',
  110. '</a>',
  111. '<strong>' . __( 'Not available', 'wordpress-seo' ) . '</strong>'
  112. ),
  113. 'bad' => sprintf(
  114. /* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the SEO score. */
  115. __( '%1$sSEO%2$s: %3$s', 'wordpress-seo' ),
  116. '<a href="#yoast-seo-analysis-collapsible-metabox">',
  117. '</a>',
  118. '<strong>' . __( 'Needs improvement', 'wordpress-seo' ) . '</strong>'
  119. ),
  120. 'ok' => sprintf(
  121. /* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the SEO score. */
  122. __( '%1$sSEO%2$s: %3$s', 'wordpress-seo' ),
  123. '<a href="#yoast-seo-analysis-collapsible-metabox">',
  124. '</a>',
  125. '<strong>' . __( 'OK', 'wordpress-seo' ) . '</strong>'
  126. ),
  127. 'good' => sprintf(
  128. /* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the SEO score. */
  129. __( '%1$sSEO%2$s: %3$s', 'wordpress-seo' ),
  130. '<a href="#yoast-seo-analysis-collapsible-metabox">',
  131. '</a>',
  132. '<strong>' . __( 'Good', 'wordpress-seo' ) . '</strong>'
  133. ),
  134. ],
  135. ],
  136. ],
  137. 'markdownEnabled' => $this->is_markdown_enabled(),
  138. 'analysisHeadingTitle' => __( 'Analysis', 'wordpress-seo' ),
  139. ];
  140. }
  141. /**
  142. * Returns a link to the settings page, if the user has the right capabilities.
  143. * Returns an empty string otherwise.
  144. *
  145. * @return string The settings link.
  146. */
  147. private function get_settings_link() {
  148. if ( current_user_can( 'manage_options' ) ) {
  149. return admin_url( 'options-general.php' );
  150. }
  151. return '';
  152. }
  153. /**
  154. * Returns required yoast-component translations.
  155. *
  156. * @return array
  157. */
  158. private function get_content_analysis_component_translations() {
  159. // Esc_html is not needed because React already handles HTML in the (translations of) these strings.
  160. return [
  161. 'locale' => WPSEO_Language_Utils::get_user_locale(),
  162. 'content-analysis.language-notice-link' => __( 'Change language', 'wordpress-seo' ),
  163. 'content-analysis.errors' => __( 'Errors', 'wordpress-seo' ),
  164. 'content-analysis.problems' => __( 'Problems', 'wordpress-seo' ),
  165. 'content-analysis.improvements' => __( 'Improvements', 'wordpress-seo' ),
  166. 'content-analysis.considerations' => __( 'Considerations', 'wordpress-seo' ),
  167. 'content-analysis.good' => __( 'Good results', 'wordpress-seo' ),
  168. 'content-analysis.language-notice' => __( 'Your site language is set to {language}.', 'wordpress-seo' ),
  169. 'content-analysis.language-notice-contact-admin' => __( 'Your site language is set to {language}. If this is not correct, contact your site administrator.', 'wordpress-seo' ),
  170. 'content-analysis.highlight' => __( 'Highlight this result in the text', 'wordpress-seo' ),
  171. 'content-analysis.nohighlight' => __( 'Remove highlight from the text', 'wordpress-seo' ),
  172. 'content-analysis.disabledButton' => __( 'Marks are disabled in current view', 'wordpress-seo' ),
  173. 'a11yNotice.opensInNewTab' => __( '(Opens in a new browser tab)', 'wordpress-seo' ),
  174. ];
  175. }
  176. /**
  177. * Returns the translations for the Add Keyword modal.
  178. *
  179. * These strings are not escaped because they're meant to be used with React
  180. * which already takes care of that. If used in PHP, they should be escaped.
  181. *
  182. * @return array Translated text strings for the Add Keyword modal.
  183. */
  184. public function get_add_keyword_upsell_translations() {
  185. return [
  186. 'title' => __( 'Would you like to add more than one keyphrase?', 'wordpress-seo' ),
  187. 'intro' => sprintf(
  188. /* translators: %s expands to a 'Yoast SEO Premium' text linked to the yoast.com website. */
  189. __( 'Great news: you can, with %s!', 'wordpress-seo' ),
  190. '{{link}}Yoast SEO Premium{{/link}}'
  191. ),
  192. 'link' => WPSEO_Shortlinker::get( 'https://yoa.st/pe-premium-page' ),
  193. 'other' => sprintf(
  194. /* translators: %s expands to 'Yoast SEO Premium'. */
  195. __( 'Other benefits of %s for you:', 'wordpress-seo' ),
  196. 'Yoast SEO Premium'
  197. ),
  198. 'buylink' => WPSEO_Shortlinker::get( 'https://yoa.st/add-keywords-popup' ),
  199. 'buy' => sprintf(
  200. /* translators: %s expands to 'Yoast SEO Premium'. */
  201. __( 'Get %s', 'wordpress-seo' ),
  202. 'Yoast SEO Premium'
  203. ),
  204. 'small' => __( '1 year free support and updates included!', 'wordpress-seo' ),
  205. 'a11yNotice.opensInNewTab' => __( '(Opens in a new browser tab)', 'wordpress-seo' ),
  206. ];
  207. }
  208. /**
  209. * Returns Jed compatible YoastSEO.js translations.
  210. *
  211. * @return array
  212. */
  213. private function get_translations() {
  214. $locale = WPSEO_Language_Utils::get_user_locale();
  215. $file = plugin_dir_path( WPSEO_FILE ) . 'languages/wordpress-seo-' . $locale . '.json';
  216. if ( file_exists( $file ) ) {
  217. $file = file_get_contents( $file );
  218. if ( is_string( $file ) && $file !== '' ) {
  219. return json_decode( $file, true );
  220. }
  221. }
  222. return [];
  223. }
  224. /**
  225. * Checks if Jetpack's markdown module is enabled.
  226. * Can be extended to work with other plugins that parse markdown in the content.
  227. *
  228. * @return boolean
  229. */
  230. private function is_markdown_enabled() {
  231. $is_markdown = false;
  232. if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'get_active_modules' ) ) {
  233. $active_modules = Jetpack::get_active_modules();
  234. // First at all, check if Jetpack's markdown module is active.
  235. $is_markdown = in_array( 'markdown', $active_modules, true );
  236. }
  237. /**
  238. * Filters whether markdown support is active in the readability- and seo-analysis.
  239. *
  240. * @since 11.3
  241. *
  242. * @param array $is_markdown Is markdown support for Yoast SEO active.
  243. */
  244. return apply_filters( 'wpseo_is_markdown_enabled', $is_markdown );
  245. }
  246. }