jetpack-seo-posts.php
6 years ago
jetpack-seo-titles.php
6 years ago
jetpack-seo-utils.php
7 years ago
jetpack-seo.php
9 years ago
jetpack-seo-posts.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class containing utility static methods for managing SEO custom descriptions for Posts and Pages. |
| 5 | */ |
| 6 | class Jetpack_SEO_Posts { |
| 7 | /** |
| 8 | * Key of the post meta value that will be used to store post custom description. |
| 9 | */ |
| 10 | const DESCRIPTION_META_KEY = 'advanced_seo_description'; |
| 11 | |
| 12 | /** |
| 13 | * Build meta description for post SEO. |
| 14 | * |
| 15 | * @param WP_Post $post Source of data for custom description. |
| 16 | * |
| 17 | * @return string Post description or empty string. |
| 18 | */ |
| 19 | public static function get_post_description( $post ) { |
| 20 | if ( empty( $post ) ) { |
| 21 | return ''; |
| 22 | } |
| 23 | |
| 24 | if ( post_password_required() || ! is_singular() ) { |
| 25 | return ''; |
| 26 | } |
| 27 | |
| 28 | // Business users can overwrite the description |
| 29 | $custom_description = self::get_post_custom_description( $post ); |
| 30 | |
| 31 | if ( ! empty( $custom_description ) ) { |
| 32 | return $custom_description; |
| 33 | } |
| 34 | |
| 35 | if ( ! empty( $post->post_excerpt ) ) { |
| 36 | return $post->post_excerpt; |
| 37 | } |
| 38 | |
| 39 | return $post->post_content; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns post's custom meta description if it is set, and if |
| 44 | * SEO tools are enabled for current blog. |
| 45 | * |
| 46 | * @param WP_Post $post Source of data for custom description |
| 47 | * |
| 48 | * @return string Custom description or empty string |
| 49 | */ |
| 50 | public static function get_post_custom_description( $post ) { |
| 51 | if ( empty( $post ) ) { |
| 52 | return ''; |
| 53 | } |
| 54 | |
| 55 | $custom_description = get_post_meta( $post->ID, self::DESCRIPTION_META_KEY, true ); |
| 56 | |
| 57 | if ( empty( $custom_description ) || ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) { |
| 58 | return ''; |
| 59 | } |
| 60 | |
| 61 | return $custom_description; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Registers the self::DESCRIPTION_META_KEY post_meta for use in the REST API. |
| 66 | */ |
| 67 | public static function register_post_meta() { |
| 68 | $args = array( |
| 69 | 'type' => 'string', |
| 70 | 'description' => __( 'Custom post description to be used in HTML <meta /> tag.', 'jetpack' ), |
| 71 | 'single' => true, |
| 72 | 'default' => '', |
| 73 | 'show_in_rest' => array( |
| 74 | 'name' => self::DESCRIPTION_META_KEY |
| 75 | ), |
| 76 | ); |
| 77 | |
| 78 | register_meta( 'post', self::DESCRIPTION_META_KEY, $args ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Register the Advanced SEO Gutenberg extension |
| 83 | */ |
| 84 | public static function register_gutenberg_extension() { |
| 85 | if ( Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) { |
| 86 | Jetpack_Gutenberg::set_extension_available( 'jetpack-seo' ); |
| 87 | } else { |
| 88 | Jetpack_Gutenberg::set_extension_unavailable( 'jetpack-seo', 'jetpack_seo_disabled' ); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 |