This commit is contained in:
KhaiNguyen
2020-02-13 10:39:37 +07:00
commit 59401cb805
12867 changed files with 4646216 additions and 0 deletions

View File

@@ -0,0 +1,265 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Formatter
*/
/**
* This class forces needed methods for the metabox localization.
*/
class WPSEO_Metabox_Formatter {
/**
* Object that provides formatted values.
*
* @var WPSEO_Metabox_Formatter_Interface
*/
private $formatter;
/**
* Setting the formatter property.
*
* @param WPSEO_Metabox_Formatter_Interface $formatter Object that provides the formatted values.
*/
public function __construct( WPSEO_Metabox_Formatter_Interface $formatter ) {
$this->formatter = $formatter;
}
/**
* Returns the values.
*
* @return array
*/
public function get_values() {
$defaults = $this->get_defaults();
$values = $this->formatter->get_values();
return ( $values + $defaults );
}
/**
* Returns array with all the values always needed by a scraper object.
*
* @return array Default settings for the metabox.
*/
private function get_defaults() {
$analysis_seo = new WPSEO_Metabox_Analysis_SEO();
$analysis_readability = new WPSEO_Metabox_Analysis_Readability();
return [
'language' => WPSEO_Language_Utils::get_site_language_name(),
'settings_link' => $this->get_settings_link(),
'search_url' => '',
'post_edit_url' => '',
'base_url' => '',
'contentTab' => __( 'Readability', 'wordpress-seo' ),
'keywordTab' => __( 'Keyphrase:', 'wordpress-seo' ),
'removeKeyword' => __( 'Remove keyphrase', 'wordpress-seo' ),
'contentLocale' => get_locale(),
'userLocale' => WPSEO_Language_Utils::get_user_locale(),
'translations' => $this->get_translations(),
'keyword_usage' => [],
'title_template' => '',
'metadesc_template' => '',
'contentAnalysisActive' => $analysis_readability->is_enabled() ? 1 : 0,
'keywordAnalysisActive' => $analysis_seo->is_enabled() ? 1 : 0,
'cornerstoneActive' => WPSEO_Options::get( 'enable_cornerstone_content', false ) ? 1 : 0,
'intl' => $this->get_content_analysis_component_translations(),
'isRtl' => is_rtl(),
'isPremium' => WPSEO_Utils::is_yoast_seo_premium(),
'addKeywordUpsell' => $this->get_add_keyword_upsell_translations(),
'wordFormRecognitionActive' => ( WPSEO_Language_Utils::get_language( get_locale() ) === 'en' ),
'siteIconUrl' => get_site_icon_url(),
/**
* Filter to determine if the markers should be enabled or not.
*
* @param bool $showMarkers Should the markers being enabled. Default = true.
*/
'show_markers' => apply_filters( 'wpseo_enable_assessment_markers', true ),
'publish_box' => [
'labels' => [
'content' => [
'na' => sprintf(
/* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the readability score. */
__( '%1$sReadability%2$s: %3$s', 'wordpress-seo' ),
'<a href="#yoast-readability-analysis-collapsible-metabox">',
'</a>',
'<strong>' . __( 'Not available', 'wordpress-seo' ) . '</strong>'
),
'bad' => sprintf(
/* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the readability score. */
__( '%1$sReadability%2$s: %3$s', 'wordpress-seo' ),
'<a href="#yoast-readability-analysis-collapsible-metabox">',
'</a>',
'<strong>' . __( 'Needs improvement', 'wordpress-seo' ) . '</strong>'
),
'ok' => sprintf(
/* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the readability score. */
__( '%1$sReadability%2$s: %3$s', 'wordpress-seo' ),
'<a href="#yoast-readability-analysis-collapsible-metabox">',
'</a>',
'<strong>' . __( 'OK', 'wordpress-seo' ) . '</strong>'
),
'good' => sprintf(
/* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the readability score. */
__( '%1$sReadability%2$s: %3$s', 'wordpress-seo' ),
'<a href="#yoast-readability-analysis-collapsible-metabox">',
'</a>',
'<strong>' . __( 'Good', 'wordpress-seo' ) . '</strong>'
),
],
'keyword' => [
'na' => sprintf(
/* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the SEO score. */
__( '%1$sSEO%2$s: %3$s', 'wordpress-seo' ),
'<a href="#yoast-seo-analysis-collapsible-metabox">',
'</a>',
'<strong>' . __( 'Not available', 'wordpress-seo' ) . '</strong>'
),
'bad' => sprintf(
/* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the SEO score. */
__( '%1$sSEO%2$s: %3$s', 'wordpress-seo' ),
'<a href="#yoast-seo-analysis-collapsible-metabox">',
'</a>',
'<strong>' . __( 'Needs improvement', 'wordpress-seo' ) . '</strong>'
),
'ok' => sprintf(
/* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the SEO score. */
__( '%1$sSEO%2$s: %3$s', 'wordpress-seo' ),
'<a href="#yoast-seo-analysis-collapsible-metabox">',
'</a>',
'<strong>' . __( 'OK', 'wordpress-seo' ) . '</strong>'
),
'good' => sprintf(
/* translators: %1$s expands to the opening anchor tag, %2$s to the closing anchor tag, %3$s to the SEO score. */
__( '%1$sSEO%2$s: %3$s', 'wordpress-seo' ),
'<a href="#yoast-seo-analysis-collapsible-metabox">',
'</a>',
'<strong>' . __( 'Good', 'wordpress-seo' ) . '</strong>'
),
],
],
],
'markdownEnabled' => $this->is_markdown_enabled(),
'analysisHeadingTitle' => __( 'Analysis', 'wordpress-seo' ),
];
}
/**
* Returns a link to the settings page, if the user has the right capabilities.
* Returns an empty string otherwise.
*
* @return string The settings link.
*/
private function get_settings_link() {
if ( current_user_can( 'manage_options' ) ) {
return admin_url( 'options-general.php' );
}
return '';
}
/**
* Returns required yoast-component translations.
*
* @return array
*/
private function get_content_analysis_component_translations() {
// Esc_html is not needed because React already handles HTML in the (translations of) these strings.
return [
'locale' => WPSEO_Language_Utils::get_user_locale(),
'content-analysis.language-notice-link' => __( 'Change language', 'wordpress-seo' ),
'content-analysis.errors' => __( 'Errors', 'wordpress-seo' ),
'content-analysis.problems' => __( 'Problems', 'wordpress-seo' ),
'content-analysis.improvements' => __( 'Improvements', 'wordpress-seo' ),
'content-analysis.considerations' => __( 'Considerations', 'wordpress-seo' ),
'content-analysis.good' => __( 'Good results', 'wordpress-seo' ),
'content-analysis.language-notice' => __( 'Your site language is set to {language}.', 'wordpress-seo' ),
'content-analysis.language-notice-contact-admin' => __( 'Your site language is set to {language}. If this is not correct, contact your site administrator.', 'wordpress-seo' ),
'content-analysis.highlight' => __( 'Highlight this result in the text', 'wordpress-seo' ),
'content-analysis.nohighlight' => __( 'Remove highlight from the text', 'wordpress-seo' ),
'content-analysis.disabledButton' => __( 'Marks are disabled in current view', 'wordpress-seo' ),
'a11yNotice.opensInNewTab' => __( '(Opens in a new browser tab)', 'wordpress-seo' ),
];
}
/**
* Returns the translations for the Add Keyword modal.
*
* These strings are not escaped because they're meant to be used with React
* which already takes care of that. If used in PHP, they should be escaped.
*
* @return array Translated text strings for the Add Keyword modal.
*/
public function get_add_keyword_upsell_translations() {
return [
'title' => __( 'Would you like to add more than one keyphrase?', 'wordpress-seo' ),
'intro' => sprintf(
/* translators: %s expands to a 'Yoast SEO Premium' text linked to the yoast.com website. */
__( 'Great news: you can, with %s!', 'wordpress-seo' ),
'{{link}}Yoast SEO Premium{{/link}}'
),
'link' => WPSEO_Shortlinker::get( 'https://yoa.st/pe-premium-page' ),
'other' => sprintf(
/* translators: %s expands to 'Yoast SEO Premium'. */
__( 'Other benefits of %s for you:', 'wordpress-seo' ),
'Yoast SEO Premium'
),
'buylink' => WPSEO_Shortlinker::get( 'https://yoa.st/add-keywords-popup' ),
'buy' => sprintf(
/* translators: %s expands to 'Yoast SEO Premium'. */
__( 'Get %s', 'wordpress-seo' ),
'Yoast SEO Premium'
),
'small' => __( '1 year free support and updates included!', 'wordpress-seo' ),
'a11yNotice.opensInNewTab' => __( '(Opens in a new browser tab)', 'wordpress-seo' ),
];
}
/**
* Returns Jed compatible YoastSEO.js translations.
*
* @return array
*/
private function get_translations() {
$locale = WPSEO_Language_Utils::get_user_locale();
$file = plugin_dir_path( WPSEO_FILE ) . 'languages/wordpress-seo-' . $locale . '.json';
if ( file_exists( $file ) ) {
$file = file_get_contents( $file );
if ( is_string( $file ) && $file !== '' ) {
return json_decode( $file, true );
}
}
return [];
}
/**
* Checks if Jetpack's markdown module is enabled.
* Can be extended to work with other plugins that parse markdown in the content.
*
* @return boolean
*/
private function is_markdown_enabled() {
$is_markdown = false;
if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'get_active_modules' ) ) {
$active_modules = Jetpack::get_active_modules();
// First at all, check if Jetpack's markdown module is active.
$is_markdown = in_array( 'markdown', $active_modules, true );
}
/**
* Filters whether markdown support is active in the readability- and seo-analysis.
*
* @since 11.3
*
* @param array $is_markdown Is markdown support for Yoast SEO active.
*/
return apply_filters( 'wpseo_is_markdown_enabled', $is_markdown );
}
}

View File

@@ -0,0 +1,249 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Formatter
*/
/**
* This class provides data for the post metabox by return its values for localization.
*/
class WPSEO_Post_Metabox_Formatter implements WPSEO_Metabox_Formatter_Interface {
/**
* Holds the WordPress Post.
*
* @var WP_Post
*/
private $post;
/**
* The permalink to follow.
*
* @var string
*/
private $permalink;
/**
* The date helper.
*
* @var WPSEO_Date_Helper
*/
protected $date;
/**
* Constructor.
*
* @param WP_Post|array $post Post object.
* @param array $options Title options to use.
* @param string $structure The permalink to follow.
*/
public function __construct( $post, array $options, $structure ) {
$this->post = $post;
$this->permalink = $structure;
$this->date = new WPSEO_Date_Helper();
}
/**
* Returns the translated values.
*
* @return array
*/
public function get_values() {
$values = [
'search_url' => $this->search_url(),
'post_edit_url' => $this->edit_url(),
'base_url' => $this->base_url_for_js(),
'metaDescriptionDate' => '',
];
if ( $this->post instanceof WP_Post ) {
$values_to_set = [
'keyword_usage' => $this->get_focus_keyword_usage(),
'title_template' => $this->get_title_template(),
'metadesc_template' => $this->get_metadesc_template(),
'metaDescriptionDate' => $this->get_metadesc_date(),
'social_preview_image_url' => $this->get_image_url(),
];
$values = ( $values_to_set + $values );
}
return $values;
}
/**
* Gets the image URL for the post's social preview.
*
* @return string|null The image URL for the social preview.
*/
protected function get_image_url() {
$post_id = $this->post->ID;
if ( has_post_thumbnail( $post_id ) ) {
$featured_image_info = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'thumbnail' );
return $featured_image_info[0];
}
return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id );
}
/**
* Returns the url to search for keyword for the post.
*
* @return string
*/
private function search_url() {
return admin_url( 'edit.php?seo_kw_filter={keyword}' );
}
/**
* Returns the url to edit the taxonomy.
*
* @return string
*/
private function edit_url() {
return admin_url( 'post.php?post={id}&action=edit' );
}
/**
* Returns a base URL for use in the JS, takes permalink structure into account.
*
* @return string
*/
private function base_url_for_js() {
global $pagenow;
// The default base is the home_url.
$base_url = home_url( '/', null );
if ( $pagenow === 'post-new.php' ) {
return $base_url;
}
// If %postname% is the last tag, just strip it and use that as a base.
if ( preg_match( '#%postname%/?$#', $this->permalink ) === 1 ) {
$base_url = preg_replace( '#%postname%/?$#', '', $this->permalink );
}
return $base_url;
}
/**
* Counts the number of given keywords used for other posts other than the given post_id.
*
* @return array The keyword and the associated posts that use it.
*/
private function get_focus_keyword_usage() {
$keyword = WPSEO_Meta::get_value( 'focuskw', $this->post->ID );
$usage = [ $keyword => $this->get_keyword_usage_for_current_post( $keyword ) ];
if ( WPSEO_Utils::is_yoast_seo_premium() ) {
return $this->get_premium_keywords( $usage );
}
return $usage;
}
/**
* Retrieves the additional keywords from Premium, that are associated with the post.
*
* @param array $usage The original keyword usage for the main keyword.
*
* @return array The keyword usage, including the additional keywords.
*/
protected function get_premium_keywords( $usage ) {
$additional_keywords = json_decode( WPSEO_Meta::get_value( 'focuskeywords', $this->post->ID ), true );
if ( empty( $additional_keywords ) ) {
return $usage;
}
foreach ( $additional_keywords as $additional_keyword ) {
$keyword = $additional_keyword['keyword'];
$usage[ $keyword ] = $this->get_keyword_usage_for_current_post( $keyword );
}
return $usage;
}
/**
* Gets the keyword usage for the current post and the specified keyword.
*
* @param string $keyword The keyword to check the usage of.
*
* @return array The post IDs which use the passed keyword.
*/
protected function get_keyword_usage_for_current_post( $keyword ) {
return WPSEO_Meta::keyword_usage( $keyword, $this->post->ID );
}
/**
* Retrieves the title template.
*
* @return string The title template.
*/
private function get_title_template() {
$title = $this->get_template( 'title' );
if ( $title === '' ) {
return '%%title%% %%sep%% %%sitename%%';
}
return $title;
}
/**
* Retrieves the metadesc template.
*
* @return string
*/
private function get_metadesc_template() {
return $this->get_template( 'metadesc' );
}
/**
* Retrieves a template.
*
* @param String $template_option_name The name of the option in which the template you want to get is saved.
*
* @return string
*/
private function get_template( $template_option_name ) {
$needed_option = $template_option_name . '-' . $this->post->post_type;
if ( WPSEO_Options::get( $needed_option, '' ) !== '' ) {
return WPSEO_Options::get( $needed_option );
}
return '';
}
/**
* Determines the date to be displayed in the snippet preview.
*
* @return string
*/
private function get_metadesc_date() {
$date = '';
if ( $this->is_show_date_enabled() ) {
$date = $this->date->format_translated( $this->post->post_date, 'M j, Y' );
}
return $date;
}
/**
* Returns whether or not showing the date in the snippet preview is enabled.
*
* @return bool
*/
private function is_show_date_enabled() {
$key = sprintf( 'showdate-%s', $this->post->post_type );
return WPSEO_Options::get( $key, true );
}
}

View File

@@ -0,0 +1,160 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Formatter
*/
/**
* This class provides data for the term metabox by return its values for localization.
*/
class WPSEO_Term_Metabox_Formatter implements WPSEO_Metabox_Formatter_Interface {
/**
* The term the metabox formatter is for.
*
* @var WP_Term|stdClass
*/
private $term;
/**
* The term's taxonomy.
*
* @var stdClass
*/
private $taxonomy;
/**
* Array with the WPSEO_Titles options.
*
* @var array
*/
protected $options;
/**
* WPSEO_Taxonomy_Scraper constructor.
*
* @param stdClass $taxonomy Taxonomy.
* @param WP_Term|stdClass $term Term.
*/
public function __construct( $taxonomy, $term ) {
$this->taxonomy = $taxonomy;
$this->term = $term;
}
/**
* Returns the translated values.
*
* @return array
*/
public function get_values() {
$values = [];
// Todo: a column needs to be added on the termpages to add a filter for the keyword, so this can be used in the focus keyphrase doubles.
if ( is_object( $this->term ) && property_exists( $this->term, 'taxonomy' ) ) {
$values = [
'search_url' => $this->search_url(),
'post_edit_url' => $this->edit_url(),
'base_url' => $this->base_url_for_js(),
'taxonomy' => $this->term->taxonomy,
'keyword_usage' => $this->get_focus_keyword_usage(),
'title_template' => $this->get_title_template(),
'metadesc_template' => $this->get_metadesc_template(),
'social_preview_image_url' => $this->get_image_url(),
];
}
return $values;
}
/**
* Gets the image URL for the term's social preview.
*
* @return string|null The image URL for the social preview.
*/
protected function get_image_url() {
return WPSEO_Image_Utils::get_first_content_image_for_term( $this->term->term_id );
}
/**
* Returns the url to search for keyword for the taxonomy.
*
* @return string
*/
private function search_url() {
return admin_url( 'edit-tags.php?taxonomy=' . $this->term->taxonomy . '&seo_kw_filter={keyword}' );
}
/**
* Returns the url to edit the taxonomy.
*
* @return string
*/
private function edit_url() {
global $wp_version;
$script_filename = version_compare( $wp_version, '4.5', '<' ) ? 'edit-tags' : 'term';
return admin_url( $script_filename . '.php?action=edit&taxonomy=' . $this->term->taxonomy . '&tag_ID={id}' );
}
/**
* Returns a base URL for use in the JS, takes permalink structure into account.
*
* @return string
*/
private function base_url_for_js() {
$base_url = home_url( '/', null );
if ( ! WPSEO_Options::get( 'stripcategorybase', false ) ) {
$base_url = trailingslashit( $base_url . $this->taxonomy->rewrite['slug'] );
}
return $base_url;
}
/**
* Counting the number of given keyword used for other term than given term_id.
*
* @return array
*/
private function get_focus_keyword_usage() {
$focuskw = WPSEO_Taxonomy_Meta::get_term_meta( $this->term, $this->term->taxonomy, 'focuskw' );
return WPSEO_Taxonomy_Meta::get_keyword_usage( $focuskw, $this->term->term_id, $this->term->taxonomy );
}
/**
* Retrieves the title template.
*
* @return string The title template.
*/
private function get_title_template() {
$title = $this->get_template( 'title' );
if ( $title === '' ) {
return '%%title%% %%sep%% %%sitename%%';
}
return $title;
}
/**
* Retrieves the metadesc template.
*
* @return string
*/
private function get_metadesc_template() {
return $this->get_template( 'metadesc' );
}
/**
* Retrieves a template.
*
* @param String $template_option_name The name of the option in which the template you want to get is saved.
*
* @return string
*/
private function get_template( $template_option_name ) {
$needed_option = $template_option_name . '-tax-' . $this->term->taxonomy;
return WPSEO_Options::get( $needed_option, '' );
}
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Formatter
*/
/**
* Interface to force get_values.
*/
interface WPSEO_Metabox_Formatter_Interface {
/**
* Returns formatter values.
*
* @return array
*/
public function get_values();
}