Aucune description

jetpack-seo-posts.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /**
  53. * Registers the self::DESCRIPTION_META_KEY post_meta for use in the REST API.
  54. */
  55. public static function register_post_meta() {
  56. $args = array(
  57. 'type' => 'string',
  58. 'description' => __( 'Custom post description to be used in HTML <meta /> tag.', 'jetpack' ),
  59. 'single' => true,
  60. 'default' => '',
  61. 'show_in_rest' => array(
  62. 'name' => self::DESCRIPTION_META_KEY
  63. ),
  64. );
  65. register_meta( 'post', self::DESCRIPTION_META_KEY, $args );
  66. }
  67. /**
  68. * Register the Advanced SEO Gutenberg extension
  69. */
  70. public static function register_gutenberg_extension() {
  71. if ( Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) {
  72. Jetpack_Gutenberg::set_extension_available( 'jetpack-seo' );
  73. } else {
  74. Jetpack_Gutenberg::set_extension_unavailable( 'jetpack-seo', 'jetpack_seo_disabled' );
  75. }
  76. }
  77. }