2019-11-15 23:26:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class containing utility static methods that other SEO tools are relying on.
|
|
|
|
|
*/
|
|
|
|
|
class Jetpack_SEO_Utils {
|
|
|
|
|
/**
|
|
|
|
|
* Site option name used to store front page meta description.
|
|
|
|
|
*/
|
|
|
|
|
const FRONT_PAGE_META_OPTION = 'advanced_seo_front_page_description';
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-27 08:32:47 +02:00
|
|
|
* Initially setting the front page meta description was for all sites, then the feature was grouped to a paid plan.
|
|
|
|
|
* The LEGACY_META_OPTION was added at that time to support legacy usage. Later on, a decision was made to have
|
|
|
|
|
* the JP seo-tools features for all JP sites (paid plan or not).
|
2019-11-15 23:26:29 +01:00
|
|
|
*/
|
2020-09-15 14:30:05 +02:00
|
|
|
const LEGACY_META_OPTION = 'seo_meta_description';
|
2019-11-15 23:26:29 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used to check whether SEO tools are enabled for given site.
|
|
|
|
|
*
|
|
|
|
|
* @return bool True if SEO tools are enabled, false otherwise.
|
|
|
|
|
*/
|
2021-04-27 08:32:47 +02:00
|
|
|
public static function is_enabled_jetpack_seo() {
|
2019-11-15 23:26:29 +01:00
|
|
|
/**
|
|
|
|
|
* Can be used by SEO plugin authors to disable the conflicting output of SEO Tools.
|
|
|
|
|
*
|
|
|
|
|
* @module seo-tools
|
|
|
|
|
*
|
|
|
|
|
* @since 5.0.0
|
|
|
|
|
*
|
|
|
|
|
* @param bool True if SEO Tools should be disabled, false otherwise.
|
|
|
|
|
*/
|
|
|
|
|
if ( apply_filters( 'jetpack_disable_seo_tools', false ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns front page meta description for current site.
|
|
|
|
|
*
|
|
|
|
|
* @return string Front page meta description string or empty string.
|
|
|
|
|
*/
|
|
|
|
|
public static function get_front_page_meta_description() {
|
2021-04-27 08:32:47 +02:00
|
|
|
$front_page_meta = get_option( self::FRONT_PAGE_META_OPTION );
|
|
|
|
|
|
|
|
|
|
if ( empty( $front_page_meta ) ) {
|
|
|
|
|
$legacy_meta_option = get_option( self::LEGACY_META_OPTION );
|
|
|
|
|
if ( ! empty( $legacy_meta_option ) ) {
|
|
|
|
|
return self::update_front_page_meta_description( $legacy_meta_option, true );
|
|
|
|
|
}
|
2019-11-15 23:26:29 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-27 08:32:47 +02:00
|
|
|
return $front_page_meta;
|
2019-11-15 23:26:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the site option value for front page meta description.
|
|
|
|
|
*
|
2021-04-27 08:32:47 +02:00
|
|
|
* @param string $value New value for front page meta description.
|
|
|
|
|
* @param bool $delete_legacy_meta_option Delete the LEGACY_META_OPTION if true.
|
2019-11-15 23:26:29 +01:00
|
|
|
*
|
|
|
|
|
* @return string Saved value, or empty string if no update was performed.
|
|
|
|
|
*/
|
2021-04-27 08:32:47 +02:00
|
|
|
public static function update_front_page_meta_description( $value, $delete_legacy_meta_option = false ) {
|
2019-11-15 23:26:29 +01:00
|
|
|
$front_page_description = sanitize_text_field( $value );
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-27 08:32:47 +02:00
|
|
|
* Can be used to limit the length of front page meta description.
|
2019-11-15 23:26:29 +01:00
|
|
|
*
|
|
|
|
|
* @module seo-tools
|
|
|
|
|
*
|
|
|
|
|
* @since 4.4.0
|
|
|
|
|
*
|
|
|
|
|
* @param int Maximum length of front page meta description. Defaults to 300.
|
|
|
|
|
*/
|
|
|
|
|
$description_max_length = apply_filters( 'jetpack_seo_front_page_description_max_length', 300 );
|
|
|
|
|
|
|
|
|
|
if ( function_exists( 'mb_substr' ) ) {
|
|
|
|
|
$front_page_description = mb_substr( $front_page_description, 0, $description_max_length );
|
|
|
|
|
} else {
|
|
|
|
|
$front_page_description = substr( $front_page_description, 0, $description_max_length );
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 08:32:47 +02:00
|
|
|
$did_update = update_option( self::FRONT_PAGE_META_OPTION, $front_page_description );
|
2019-11-15 23:26:29 +01:00
|
|
|
|
2021-04-27 08:32:47 +02:00
|
|
|
if ( $delete_legacy_meta_option && $did_update ) {
|
2019-11-15 23:26:29 +01:00
|
|
|
delete_option( 'seo_meta_description' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $did_update ) {
|
|
|
|
|
return $front_page_description;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|