jetpack-seo-utils.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * Old version of option name that was previously used under Free plan.
  12. */
  13. const GRANDFATHERED_META_OPTION = 'seo_meta_description';
  14. /**
  15. * Used to check whether SEO tools are enabled for given site.
  16. *
  17. * @param int $site_id Optional. Defaults to current blog id if not given.
  18. *
  19. * @return bool True if SEO tools are enabled, false otherwise.
  20. */
  21. public static function is_enabled_jetpack_seo( $site_id = 0 ) {
  22. /**
  23. * Can be used by SEO plugin authors to disable the conflicting output of SEO Tools.
  24. *
  25. * @module seo-tools
  26. *
  27. * @since 5.0.0
  28. *
  29. * @param bool True if SEO Tools should be disabled, false otherwise.
  30. */
  31. if ( apply_filters( 'jetpack_disable_seo_tools', false ) ) {
  32. return false;
  33. }
  34. if ( function_exists( 'has_blog_sticker' ) ) {
  35. // For WPCOM sites
  36. if ( empty( $site_id ) ) {
  37. $site_id = get_current_blog_id();
  38. }
  39. return has_blog_sticker( 'business-plan', $site_id );
  40. }
  41. // For all Jetpack sites
  42. return true;
  43. }
  44. /**
  45. * Checks if this option was set while it was still available under free plan.
  46. *
  47. * @return bool True if we should enable grandfathering, false otherwise.
  48. */
  49. public static function has_grandfathered_front_page_meta() {
  50. return ! self::is_enabled_jetpack_seo() && get_option( self::GRANDFATHERED_META_OPTION );
  51. }
  52. /**
  53. * Returns front page meta description for current site.
  54. *
  55. * Since we allowed non-business users to set Front page meta description for some time,
  56. * before bundling it with other SEO tools features that require a business plan,
  57. * we are supporting grandfathering here.
  58. *
  59. * @return string Front page meta description string or empty string.
  60. */
  61. public static function get_front_page_meta_description() {
  62. if ( self::is_enabled_jetpack_seo() ) {
  63. $front_page_meta = get_option( self::FRONT_PAGE_META_OPTION );
  64. return $front_page_meta ? $front_page_meta : get_option( self::GRANDFATHERED_META_OPTION, '' );
  65. }
  66. // Support grandfathering for non-business users.
  67. return get_option( self::GRANDFATHERED_META_OPTION, '' );
  68. }
  69. /**
  70. * Updates the site option value for front page meta description.
  71. *
  72. * We are taking care to update the correct option, in case the value is grandfathered for current site.
  73. *
  74. * @param $value string New value for front page meta description.
  75. *
  76. * @return string Saved value, or empty string if no update was performed.
  77. */
  78. public static function update_front_page_meta_description( $value ) {
  79. $front_page_description = sanitize_text_field( $value );
  80. /**
  81. * Can be used to limit the lenght of front page meta description.
  82. *
  83. * @module seo-tools
  84. *
  85. * @since 4.4.0
  86. *
  87. * @param int Maximum length of front page meta description. Defaults to 300.
  88. */
  89. $description_max_length = apply_filters( 'jetpack_seo_front_page_description_max_length', 300 );
  90. if ( function_exists( 'mb_substr' ) ) {
  91. $front_page_description = mb_substr( $front_page_description, 0, $description_max_length );
  92. } else {
  93. $front_page_description = substr( $front_page_description, 0, $description_max_length );
  94. }
  95. $can_set_meta = self::is_enabled_jetpack_seo();
  96. $grandfathered_meta_option = get_option( self::GRANDFATHERED_META_OPTION );
  97. $has_old_meta = ! empty( $grandfathered_meta_option );
  98. $option_name = self::has_grandfathered_front_page_meta() ? self::GRANDFATHERED_META_OPTION : self::FRONT_PAGE_META_OPTION;
  99. $did_update = update_option( $option_name, $front_page_description );
  100. if ( $did_update && $has_old_meta && $can_set_meta ) {
  101. // Delete grandfathered option if user has switched to Business plan and updated meta description.
  102. delete_option( 'seo_meta_description' );
  103. }
  104. if ( $did_update ) {
  105. return $front_page_description;
  106. }
  107. return '';
  108. }
  109. }