/wp-content/plugins/wordpress-seo/admin/formatter/class-metabox-formatter.php

https://bitbucket.org/carloskikea/helpet · PHP · 208 lines · 129 code · 17 blank · 62 comment · 7 complexity · c4e3c8404079adf6293da323a28fbac9 MD5 · raw file

  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. 'enterFocusKeyword' => __( 'Enter your focus keyword', 'wordpress-seo' ),
  50. 'removeKeyword' => __( 'Remove keyword', 'wordpress-seo' ),
  51. 'contentLocale' => get_locale(),
  52. 'userLocale' => WPSEO_Utils::get_user_locale(),
  53. 'translations' => $this->get_translations(),
  54. 'keyword_usage' => array(),
  55. 'title_template' => '',
  56. 'metadesc_template' => '',
  57. 'contentAnalysisActive' => $analysis_readability->is_enabled() ? 1 : 0,
  58. 'keywordAnalysisActive' => $analysis_seo->is_enabled() ? 1 : 0,
  59. 'intl' => $this->get_content_analysis_component_translations(),
  60. 'reactSnippetPreview' => defined( 'YOAST_FEATURE_SNIPPET_PREVIEW' ) && YOAST_FEATURE_SNIPPET_PREVIEW,
  61. /**
  62. * Filter to determine if the markers should be enabled or not.
  63. *
  64. * @param bool $showMarkers Should the markers being enabled. Default = true.
  65. */
  66. 'show_markers' => apply_filters( 'wpseo_enable_assessment_markers', true ),
  67. 'publish_box' => array(
  68. 'labels' => array(
  69. 'content' => array(
  70. 'na' => sprintf(
  71. /* translators: %1$s expands to an opening strong tag, %2$s expands to a closing strong tag. */
  72. __( 'Readability: %1$sNot available%2$s', 'wordpress-seo' ),
  73. '<strong>',
  74. '</strong>'
  75. ),
  76. 'bad' => sprintf(
  77. /* translators: %1$s expands to an opening strong tag, %2$s expands to a closing strong tag. */
  78. __( 'Readability: %1$sNeeds improvement%2$s', 'wordpress-seo' ),
  79. '<strong>',
  80. '</strong>'
  81. ),
  82. 'ok' => sprintf(
  83. /* translators: %1$s expands to an opening strong tag, %2$s expands to a closing strong tag. */
  84. __( 'Readability: %1$sOK%2$s', 'wordpress-seo' ),
  85. '<strong>',
  86. '</strong>'
  87. ),
  88. 'good' => sprintf(
  89. /* translators: %1$s expands to an opening strong tag, %2$s expands to a closing strong tag. */
  90. __( 'Readability: %1$sGood%2$s', 'wordpress-seo' ),
  91. '<strong>',
  92. '</strong>'
  93. ),
  94. ),
  95. 'keyword' => array(
  96. 'na' => sprintf(
  97. /* translators: %1$s expands to an opening strong tag, %2$s expands to a closing strong tag. */
  98. __( 'SEO: %1$sNot available%2$s', 'wordpress-seo' ),
  99. '<strong>',
  100. '</strong>'
  101. ),
  102. 'bad' => sprintf(
  103. /* translators: %1$s expands to an opening strong tag, %2$s expands to a closing strong tag. */
  104. __( 'SEO: %1$sNeeds improvement%2$s', 'wordpress-seo' ),
  105. '<strong>',
  106. '</strong>'
  107. ),
  108. 'ok' => sprintf(
  109. /* translators: %1$s expands to an opening strong tag, %2$s expands to a closing strong tag. */
  110. __( 'SEO: %1$sOK%2$s', 'wordpress-seo' ),
  111. '<strong>',
  112. '</strong>'
  113. ),
  114. 'good' => sprintf(
  115. /* translators: %1$s expands to an opening strong tag, %2$s expands to a closing strong tag. */
  116. __( 'SEO: %1$sGood%2$s', 'wordpress-seo' ),
  117. '<strong>',
  118. '</strong>'
  119. ),
  120. ),
  121. ),
  122. ),
  123. 'markdownEnabled' => $this->is_markdown_enabled(),
  124. 'analysisHeadingTitle' => __( 'Analysis', 'wordpress-seo' ),
  125. );
  126. }
  127. /**
  128. * Returns a link to the settings page, if the user has the right capabilities.
  129. * Returns an empty string otherwise.
  130. *
  131. * @return string The settings link.
  132. */
  133. private function get_settings_link() {
  134. if ( current_user_can( 'manage_options' ) ) {
  135. return admin_url( 'options-general.php' );
  136. }
  137. return '';
  138. }
  139. /**
  140. * Returns required yoast-component translations.
  141. *
  142. * @return array
  143. */
  144. private function get_content_analysis_component_translations() {
  145. // Esc_html is not needed because React already handles HTML in the (translations of) these strings.
  146. return array(
  147. 'locale' => WPSEO_Utils::get_user_locale(),
  148. 'content-analysis.language-notice-link' => __( 'Change language', 'wordpress-seo' ),
  149. 'content-analysis.errors' => __( 'Errors', 'wordpress-seo' ),
  150. 'content-analysis.problems' => __( 'Problems', 'wordpress-seo' ),
  151. 'content-analysis.improvements' => __( 'Improvements', 'wordpress-seo' ),
  152. 'content-analysis.considerations' => __( 'Considerations', 'wordpress-seo' ),
  153. 'content-analysis.good' => __( 'Good results', 'wordpress-seo' ),
  154. 'content-analysis.language-notice' => __( 'Your site language is set to {language}.', 'wordpress-seo' ),
  155. 'content-analysis.language-notice-contact-admin' => __( 'Your site language is set to {language}. If this is not correct, contact your site administrator.', 'wordpress-seo' ),
  156. 'content-analysis.highlight' => __( 'Highlight this result in the text', 'wordpress-seo' ),
  157. 'content-analysis.nohighlight' => __( 'Remove highlight from the text', 'wordpress-seo' ),
  158. 'content-analysis.disabledButton' => __( 'Marks are disabled in current view', 'wordpress-seo' ),
  159. 'a11yNotice.opensInNewTab' => __( '(Opens in a new browser tab)', 'wordpress-seo' ),
  160. );
  161. }
  162. /**
  163. * Returns Jed compatible YoastSEO.js translations.
  164. *
  165. * @return array
  166. */
  167. private function get_translations() {
  168. $locale = WPSEO_Utils::get_user_locale();
  169. $file = plugin_dir_path( WPSEO_FILE ) . 'languages/wordpress-seo-' . $locale . '.json';
  170. if ( file_exists( $file ) ) {
  171. $file = file_get_contents( $file );
  172. if ( is_string( $file ) && $file !== '' ) {
  173. return json_decode( $file, true );
  174. }
  175. }
  176. return array();
  177. }
  178. /**
  179. * Checks if Jetpack's markdown module is enabled.
  180. * Can be extended to work with other plugins that parse markdown in the content.
  181. *
  182. * @return boolean
  183. */
  184. private function is_markdown_enabled() {
  185. if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'get_active_modules' ) ) {
  186. $active_modules = Jetpack::get_active_modules();
  187. return in_array( 'markdown', $active_modules, true );
  188. }
  189. return false;
  190. }
  191. }