kollapsminoriteten/wp-content/plugins/jetpack/modules/seo-tools/class-jetpack-seo-utils.php

149 lines
4.4 KiB
PHP
Raw Normal View History

2019-11-15 23:26:29 +01:00
<?php
2022-04-02 10:26:41 +02:00
/**
* Class containing utility static methods that other SEO tools are relying on.
*
* @package automattic/jetpack
*/
2019-11-15 23:26:29 +01:00
/**
* 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-07-23 11:58:50 +02:00
* The LEGACY_META_OPTION is used to support legacy usage on WPcom simple sites (free or paid).
* For WPorg JP sites, the JP seo-tools features were made free for all sites (free or paid).
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.
*/
2022-06-16 14:01: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;
}
2024-04-17 11:32:24 +02:00
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
return wpcom_site_has_feature( 'advanced-seo', get_current_blog_id() );
}
return true;
2019-11-15 23:26:29 +01:00
}
2021-07-23 11:58:50 +02:00
/**
* Checks if this option was set while it was freely available to all WPcom simple sites.
*
* @return bool True if we should enable legacy usage, false otherwise.
*/
public static function has_legacy_front_page_meta() {
return ! self::is_enabled_jetpack_seo() && get_option( self::LEGACY_META_OPTION );
}
2019-11-15 23:26:29 +01:00
/**
* 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-07-23 11:58:50 +02:00
if ( self::is_enabled_jetpack_seo() ) {
$front_page_meta = get_option( self::FRONT_PAGE_META_OPTION );
return $front_page_meta ? $front_page_meta : get_option( self::LEGACY_META_OPTION, '' );
2019-11-15 23:26:29 +01:00
}
2021-07-23 11:58:50 +02:00
// Support legacy usage for WPcom simple sites.
return get_option( self::LEGACY_META_OPTION, '' );
}
/**
* Sanitizes the custom front page meta description input.
*
* @param string $value Front page meta string.
*
* @return string The sanitized string.
*/
public static function sanitize_front_page_meta_description( $value ) {
return wp_strip_all_tags( $value );
2019-11-15 23:26:29 +01:00
}
/**
* Updates the site option value for front page meta description.
*
2021-07-23 11:58:50 +02:00
* @param string $value New value for front page meta description.
2019-11-15 23:26:29 +01:00
*
* @return string Saved value, or empty string if no update was performed.
*/
2021-07-23 11:58:50 +02:00
public static function update_front_page_meta_description( $value ) {
$front_page_description = self::sanitize_front_page_meta_description( $value );
2019-11-15 23:26:29 +01:00
/**
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-07-23 11:58:50 +02:00
$can_set_meta = self::is_enabled_jetpack_seo();
$legacy_meta_option = get_option( self::LEGACY_META_OPTION );
$has_old_meta = ! empty( $legacy_meta_option );
$option_name = self::has_legacy_front_page_meta() ? self::LEGACY_META_OPTION : self::FRONT_PAGE_META_OPTION;
$did_update = update_option( $option_name, $front_page_description );
2019-11-15 23:26:29 +01:00
2021-07-23 11:58:50 +02:00
if ( $did_update && $has_old_meta && $can_set_meta ) {
// Delete legacy option if user has switched to Business or eCommerce plan and updated the front page meta description.
delete_option( self::LEGACY_META_OPTION );
2019-11-15 23:26:29 +01:00
}
if ( $did_update ) {
return $front_page_description;
}
return '';
}
2025-08-27 08:44:30 +02:00
/**
* Remove content within wp:query blocks.
*
* @uses jetpack_og_remove_query_blocks
*
* @since 14.9
*
* @param string $content Post content.
*
* @return string Post content stripped from wp:query blocks.
*/
public static function remove_query_blocks( $content ) {
if ( ! function_exists( 'jetpack_og_remove_query_blocks' ) ) {
return $content;
}
return jetpack_og_remove_query_blocks( $content );
}
2019-11-15 23:26:29 +01:00
}