PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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