Ei kuvausta

jetpack-seo-utils.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Class containing utility static methods that other SEO tools are relying on.
  4. */
  5. class Jetpack_SEO_Utils {
  6. /**
  7. * Site option name used to store front page meta description.
  8. */
  9. const FRONT_PAGE_META_OPTION = 'advanced_seo_front_page_description';
  10. /**
  11. * The LEGACY_META_OPTION is used to support legacy usage on WPcom simple sites (free or paid).
  12. * For WPorg JP sites, the JP seo-tools features were made free for all sites (free or paid).
  13. */
  14. const LEGACY_META_OPTION = 'seo_meta_description';
  15. /**
  16. * Used to check whether SEO tools are enabled for given site.
  17. *
  18. * @param int $site_id Optional. Defaults to current blog id if not given.
  19. *
  20. * @return bool True if SEO tools are enabled, false otherwise.
  21. */
  22. public static function is_enabled_jetpack_seo( $site_id = 0 ) {
  23. /**
  24. * Can be used by SEO plugin authors to disable the conflicting output of SEO Tools.
  25. *
  26. * @module seo-tools
  27. *
  28. * @since 5.0.0
  29. *
  30. * @param bool True if SEO Tools should be disabled, false otherwise.
  31. */
  32. if ( apply_filters( 'jetpack_disable_seo_tools', false ) ) {
  33. return false;
  34. }
  35. if ( function_exists( 'has_any_blog_stickers' ) ) {
  36. // For WPCOM simple sites.
  37. if ( empty( $site_id ) ) {
  38. $site_id = get_current_blog_id();
  39. }
  40. return has_any_blog_stickers( array( 'business-plan', 'ecommerce-plan' ), $site_id );
  41. }
  42. // For all Jetpack sites.
  43. return true;
  44. }
  45. /**
  46. * Checks if this option was set while it was freely available to all WPcom simple sites.
  47. *
  48. * @return bool True if we should enable legacy usage, false otherwise.
  49. */
  50. public static function has_legacy_front_page_meta() {
  51. return ! self::is_enabled_jetpack_seo() && get_option( self::LEGACY_META_OPTION );
  52. }
  53. /**
  54. * Returns front page meta description for current site.
  55. *
  56. * @return string Front page meta description string or empty string.
  57. */
  58. public static function get_front_page_meta_description() {
  59. if ( self::is_enabled_jetpack_seo() ) {
  60. $front_page_meta = get_option( self::FRONT_PAGE_META_OPTION );
  61. return $front_page_meta ? $front_page_meta : get_option( self::LEGACY_META_OPTION, '' );
  62. }
  63. // Support legacy usage for WPcom simple sites.
  64. return get_option( self::LEGACY_META_OPTION, '' );
  65. }
  66. /**
  67. * Sanitizes the custom front page meta description input.
  68. *
  69. * @param string $value Front page meta string.
  70. *
  71. * @return string The sanitized string.
  72. */
  73. public static function sanitize_front_page_meta_description( $value ) {
  74. return wp_strip_all_tags( $value );
  75. }
  76. /**
  77. * Updates the site option value for front page meta description.
  78. *
  79. * @param string $value New value for front page meta description.
  80. *
  81. * @return string Saved value, or empty string if no update was performed.
  82. */
  83. public static function update_front_page_meta_description( $value ) {
  84. $front_page_description = self::sanitize_front_page_meta_description( $value );
  85. /**
  86. * Can be used to limit the length of front page meta description.
  87. *
  88. * @module seo-tools
  89. *
  90. * @since 4.4.0
  91. *
  92. * @param int Maximum length of front page meta description. Defaults to 300.
  93. */
  94. $description_max_length = apply_filters( 'jetpack_seo_front_page_description_max_length', 300 );
  95. if ( function_exists( 'mb_substr' ) ) {
  96. $front_page_description = mb_substr( $front_page_description, 0, $description_max_length );
  97. } else {
  98. $front_page_description = substr( $front_page_description, 0, $description_max_length );
  99. }
  100. $can_set_meta = self::is_enabled_jetpack_seo();
  101. $legacy_meta_option = get_option( self::LEGACY_META_OPTION );
  102. $has_old_meta = ! empty( $legacy_meta_option );
  103. $option_name = self::has_legacy_front_page_meta() ? self::LEGACY_META_OPTION : self::FRONT_PAGE_META_OPTION;
  104. $did_update = update_option( $option_name, $front_page_description );
  105. if ( $did_update && $has_old_meta && $can_set_meta ) {
  106. // Delete legacy option if user has switched to Business or eCommerce plan and updated the front page meta description.
  107. delete_option( self::LEGACY_META_OPTION );
  108. }
  109. if ( $did_update ) {
  110. return $front_page_description;
  111. }
  112. return '';
  113. }
  114. }