Нет описания

jetpack-seo.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * An SEO expert walks into a bar, bars, pub, public house, Irish pub, drinks, beer, wine, liquor, Grey Goose, Cristal...
  4. */
  5. class Jetpack_SEO {
  6. public function __construct() {
  7. add_action( 'init', array( $this, 'init' ) );
  8. }
  9. public function init() {
  10. /**
  11. * Can be used to prevent SEO tools from inserting custom meta tags.
  12. *
  13. * @module seo-tools
  14. *
  15. * @since 4.4.0
  16. *
  17. * @param bool true Should Jetpack's SEO Meta Tags be enabled. Defaults to true.
  18. */
  19. if ( apply_filters( 'jetpack_seo_meta_tags_enabled', true ) ) {
  20. add_action( 'wp_head', array( $this, 'meta_tags' ) );
  21. // Add support for editing page excerpts in pages, regardless of theme support.
  22. add_post_type_support( 'page', 'excerpt' );
  23. }
  24. /**
  25. * Can be used to prevent SEO tools form modifying site titles.
  26. *
  27. * @module seo-tools
  28. *
  29. * @since 4.4.0
  30. *
  31. * @param bool true Should Jetpack SEO modify site titles. Defaults to true.
  32. */
  33. if ( apply_filters( 'jetpack_seo_custom_titles', true ) ) {
  34. // Overwrite page title with custom SEO meta title for themes that support title-tag.
  35. add_filter( 'pre_get_document_title', array( 'Jetpack_SEO_Titles', 'get_custom_title' ) );
  36. // Add overwrite support for themes that don't support title-tag.
  37. add_filter( 'wp_title', array( 'Jetpack_SEO_Titles', 'get_custom_title' ) );
  38. }
  39. add_filter( 'jetpack_open_graph_tags', array( $this, 'set_custom_og_tags' ) );
  40. Jetpack_SEO_Posts::register_post_meta();
  41. Jetpack_SEO_Posts::register_gutenberg_extension();
  42. }
  43. private function get_authors() {
  44. global $wp_query;
  45. $authors = array();
  46. foreach ( $wp_query->posts as $post ) {
  47. $authors[] = get_the_author_meta( 'display_name', (int) $post->post_author );
  48. }
  49. $authors = array_unique( $authors );
  50. return $authors;
  51. }
  52. public function set_custom_og_tags( $tags ) {
  53. $custom_title = Jetpack_SEO_Titles::get_custom_title();
  54. if ( ! empty( $custom_title ) ) {
  55. $tags['og:title'] = $custom_title;
  56. }
  57. $post_custom_description = Jetpack_SEO_Posts::get_post_custom_description( get_post() );
  58. $front_page_meta = Jetpack_SEO_Utils::get_front_page_meta_description();
  59. if ( is_front_page() && ! empty( $front_page_meta ) ) {
  60. $tags['og:description'] = $front_page_meta;
  61. } else {
  62. if ( ! empty( $post_custom_description ) ) {
  63. $tags['og:description'] = $post_custom_description;
  64. }
  65. }
  66. return $tags;
  67. }
  68. public function meta_tags() {
  69. global $wp_query;
  70. $period = '';
  71. $template = '';
  72. $meta = array();
  73. /**
  74. * Can be used to specify a list of themes that set their own meta tags.
  75. *
  76. * If current site is using one of the themes listed as conflicting, inserting Jetpack SEO
  77. * meta tags will be prevented.
  78. *
  79. * @module seo-tools
  80. *
  81. * @since 4.4.0
  82. *
  83. * @param array List of conflicted theme names. Defaults to empty array.
  84. */
  85. $conflicted_themes = apply_filters( 'jetpack_seo_meta_tags_conflicted_themes', array() );
  86. if ( isset( $conflicted_themes[ get_option( 'template' ) ] ) ) {
  87. return;
  88. }
  89. $front_page_meta = Jetpack_SEO_Utils::get_front_page_meta_description();
  90. $description = $front_page_meta ? $front_page_meta : get_bloginfo( 'description' );
  91. $meta['description'] = trim( $description );
  92. // Try to target things if we're on a "specific" page of any kind.
  93. if ( is_singular() ) {
  94. if ( ! ( is_front_page() && Jetpack_SEO_Utils::get_front_page_meta_description() ) ) {
  95. $description = Jetpack_SEO_Posts::get_post_description( get_post() );
  96. if ( $description ) {
  97. $description = wp_trim_words(
  98. strip_shortcodes(
  99. wp_strip_all_tags( $description, true )
  100. )
  101. );
  102. $meta['description'] = $description;
  103. }
  104. }
  105. } elseif ( is_author() ) {
  106. $obj = get_queried_object();
  107. $meta['description'] = sprintf(
  108. /* translators: first property is an user's display name, the second is the site's title. */
  109. _x( 'Read all of the posts by %1$s on %2$s', 'Read all of the posts by Author Name on Blog Title', 'jetpack' ),
  110. isset( $obj->display_name ) ? $obj->display_name : __( 'the author', 'jetpack' ),
  111. get_bloginfo( 'title' )
  112. );
  113. } elseif ( is_tag() || is_category() || is_tax() ) {
  114. $obj = get_queried_object();
  115. $description = '';
  116. if ( isset( $obj->term_id ) && isset( $obj->taxonomy ) ) {
  117. $description = get_term_field( 'description', $obj->term_id, $obj->taxonomy, 'raw' );
  118. }
  119. if ( ! is_wp_error( $description ) && $description ) {
  120. $meta['description'] = wp_trim_words( $description );
  121. } else {
  122. $authors = $this->get_authors();
  123. $meta['description'] = wp_sprintf(
  124. _x( 'Posts about %1$s written by %2$l', 'Posts about Category written by John and Bob', 'jetpack' ),
  125. single_term_title( '', false ),
  126. $authors
  127. );
  128. }
  129. } elseif ( is_date() ) {
  130. if ( is_year() ) {
  131. $period = get_query_var( 'year' );
  132. $template = _nx(
  133. '%1$s post published by %2$l in the year %3$s', // singular
  134. '%1$s posts published by %2$l in the year %3$s', // plural
  135. count( $wp_query->posts ), // number
  136. '10 posts published by John in the year 2012', // context
  137. 'jetpack'
  138. );
  139. } elseif ( is_month() ) {
  140. $period = date( 'F Y', mktime( 0, 0, 0, get_query_var( 'monthnum' ), 1, get_query_var( 'year' ) ) );
  141. $template = _nx(
  142. '%1$s post published by %2$l during %3$s', // singular
  143. '%1$s posts published by %2$l during %3$s', // plural
  144. count( $wp_query->posts ), // number
  145. '10 posts publishes by John during May 2012', // context
  146. 'jetpack'
  147. );
  148. } elseif ( is_day() ) {
  149. $period = date(
  150. 'F j, Y',
  151. mktime( 0, 0, 0, get_query_var( 'monthnum' ), get_query_var( 'day' ), get_query_var( 'year' ) )
  152. );
  153. $template = _nx(
  154. '%1$s post published by %2$l on %3$s', // singular
  155. '%1$s posts published by %2$l on %3$s', // plural
  156. count( $wp_query->posts ), // number
  157. '10 posts published by John on May 30, 2012', // context
  158. 'jetpack'
  159. );
  160. }
  161. $authors = $this->get_authors();
  162. $meta['description'] = wp_sprintf( $template, count( $wp_query->posts ), $authors, $period );
  163. }
  164. /**
  165. * Can be used to edit the default SEO tools meta tags.
  166. *
  167. * @module seo-tools
  168. *
  169. * @since 4.4.0
  170. *
  171. * @param array Array that consists of meta name and meta content pairs.
  172. */
  173. $meta = apply_filters( 'jetpack_seo_meta_tags', $meta );
  174. // Output them
  175. foreach ( $meta as $name => $content ) {
  176. if ( ! empty( $content ) ) {
  177. echo '<meta name="' . esc_attr( $name ) . '" content="' . esc_attr( $content ) . '" />' . "\n";
  178. }
  179. }
  180. }
  181. }