Jetpack_SEO_Utils

Class containing utility static methods that other SEO tools are relying on.

Defined (1)

The class is defined in the following location(s).

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