class-metabox-formatter.php 8.7 KB

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