jetpack-seo.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. }
  41. private function get_authors() {
  42. global $wp_query;
  43. $authors = array();
  44. foreach ( $wp_query->posts as $post ) {
  45. $authors[] = get_the_author_meta( 'display_name', (int) $post->post_author );
  46. }
  47. $authors = array_unique( $authors );
  48. return $authors;
  49. }
  50. public function set_custom_og_tags( $tags ) {
  51. $custom_title = Jetpack_SEO_Titles::get_custom_title();
  52. if ( ! empty( $custom_title ) ) {
  53. $tags['og:title'] = $custom_title;
  54. }
  55. $post_custom_description = Jetpack_SEO_Posts::get_post_custom_description( get_post() );
  56. $front_page_meta = Jetpack_SEO_Utils::get_front_page_meta_description();
  57. if ( is_front_page() && ! empty( $front_page_meta ) ) {
  58. $tags['og:description'] = $front_page_meta;
  59. } else {
  60. if ( ! empty( $post_custom_description ) ) {
  61. $tags['og:description'] = $post_custom_description;
  62. }
  63. }
  64. return $tags;
  65. }
  66. public function meta_tags() {
  67. global $wp_query;
  68. $period = '';
  69. $template = '';
  70. $meta = array();
  71. /**
  72. * Can be used to specify a list of themes that set their own meta tags.
  73. *
  74. * If current site is using one of the themes listed as conflicting, inserting Jetpack SEO
  75. * meta tags will be prevented.
  76. *
  77. * @module seo-tools
  78. *
  79. * @since 4.4.0
  80. *
  81. * @param array List of conflicted theme names. Defaults to empty array.
  82. */
  83. $conflicted_themes = apply_filters( 'jetpack_seo_meta_tags_conflicted_themes', array() );
  84. if ( isset( $conflicted_themes[ get_option( 'template' ) ] ) ) {
  85. return;
  86. }
  87. $front_page_meta = Jetpack_SEO_Utils::get_front_page_meta_description();
  88. $description = $front_page_meta ? $front_page_meta : get_bloginfo( 'description' );
  89. $meta['description'] = trim( $description );
  90. // Try to target things if we're on a "specific" page of any kind.
  91. if ( is_singular() ) {
  92. // Business users can overwrite the description.
  93. if ( ! ( is_front_page() && Jetpack_SEO_Utils::get_front_page_meta_description() ) ) {
  94. $description = Jetpack_SEO_Posts::get_post_description( get_post() );
  95. if ( $description ) {
  96. $description = wp_trim_words( strip_shortcodes( wp_kses( $description, array() ) ) );
  97. $meta['description'] = $description;
  98. }
  99. }
  100. } elseif ( is_author() ) {
  101. $obj = get_queried_object();
  102. $meta['description'] = sprintf(
  103. _x( 'Read all of the posts by %1$s on %2$s', 'Read all of the posts by Author Name on Blog Title', 'jetpack' ),
  104. $obj->display_name,
  105. get_bloginfo( 'title' )
  106. );
  107. } elseif ( is_tag() || is_category() || is_tax() ) {
  108. $obj = get_queried_object();
  109. $description = get_term_field( 'description', $obj->term_id, $obj->taxonomy, 'raw' );
  110. if ( ! is_wp_error( $description ) && '' != $description ) {
  111. $meta['description'] = wp_trim_words( $description );
  112. } else {
  113. $authors = $this->get_authors();
  114. $meta['description'] = wp_sprintf(
  115. _x( 'Posts about %1$s written by %2$l', 'Posts about Category written by John and Bob', 'jetpack' ),
  116. single_term_title( '', false ),
  117. $authors
  118. );
  119. }
  120. } elseif ( is_date() ) {
  121. if ( is_year() ) {
  122. $period = get_query_var( 'year' );
  123. $template = _nx(
  124. '%1$s post published by %2$l in the year %3$s', // singular
  125. '%1$s posts published by %2$l in the year %3$s', // plural
  126. count( $wp_query->posts ), // number
  127. '10 posts published by John in the year 2012', // context
  128. 'jetpack'
  129. );
  130. } elseif ( is_month() ) {
  131. $period = date( 'F Y', mktime( 0, 0, 0, get_query_var( 'monthnum' ), 1, get_query_var( 'year' ) ) );
  132. $template = _nx(
  133. '%1$s post published by %2$l during %3$s', // singular
  134. '%1$s posts published by %2$l during %3$s', // plural
  135. count( $wp_query->posts ), // number
  136. '10 posts publishes by John during May 2012', // context
  137. 'jetpack'
  138. );
  139. } elseif ( is_day() ) {
  140. $period = date(
  141. 'F j, Y',
  142. mktime( 0, 0, 0, get_query_var( 'monthnum' ), get_query_var( 'day' ), get_query_var( 'year' ) )
  143. );
  144. $template = _nx(
  145. '%1$s post published by %2$l on %3$s', // singular
  146. '%1$s posts published by %2$l on %3$s', // plural
  147. count( $wp_query->posts ), // number
  148. '10 posts published by John on May 30, 2012', // context
  149. 'jetpack'
  150. );
  151. }
  152. $authors = $this->get_authors();
  153. $meta['description'] = wp_sprintf( $template, count( $wp_query->posts ), $authors, $period );
  154. }
  155. /**
  156. * Can be used to edit the default SEO tools meta tags.
  157. *
  158. * @module seo-tools
  159. *
  160. * @since 4.4.0
  161. *
  162. * @param array Array that consists of meta name and meta content pairs.
  163. */
  164. $meta = apply_filters( 'jetpack_seo_meta_tags', $meta );
  165. // Output them
  166. foreach ( $meta as $name => $content ) {
  167. if ( ! empty( $content ) ) {
  168. echo '<meta name="' . esc_attr( $name ) . '" content="' . esc_attr( $content ) . '" />' . "\n";
  169. }
  170. }
  171. }
  172. }