jetpack-seo-posts.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Class containing utility static methods for managing SEO custom descriptions for Posts and Pages.
  4. */
  5. class Jetpack_SEO_Posts {
  6. /**
  7. * Key of the post meta value that will be used to store post custom description.
  8. */
  9. const DESCRIPTION_META_KEY = 'advanced_seo_description';
  10. /**
  11. * Build meta description for post SEO.
  12. *
  13. * @param WP_Post $post Source of data for custom description.
  14. *
  15. * @return string Post description or empty string.
  16. */
  17. public static function get_post_description( $post ) {
  18. if ( empty( $post ) ) {
  19. return '';
  20. }
  21. if ( post_password_required() || ! is_singular() ) {
  22. return '';
  23. }
  24. // Business users can overwrite the description
  25. $custom_description = self::get_post_custom_description( $post );
  26. if ( ! empty( $custom_description ) ) {
  27. return $custom_description;
  28. }
  29. if ( ! empty( $post->post_excerpt ) ) {
  30. return $post->post_excerpt;
  31. }
  32. return $post->post_content;
  33. }
  34. /**
  35. * Returns post's custom meta description if it is set, and if
  36. * SEO tools are enabled for current blog.
  37. *
  38. * @param WP_Post $post Source of data for custom description
  39. *
  40. * @return string Custom description or empty string
  41. */
  42. public static function get_post_custom_description( $post ) {
  43. if ( empty( $post ) ) {
  44. return '';
  45. }
  46. $custom_description = get_post_meta( $post->ID, self::DESCRIPTION_META_KEY, true );
  47. if ( empty( $custom_description ) || ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) {
  48. return '';
  49. }
  50. return $custom_description;
  51. }
  52. }