/wp-content/themes/genesis/lib/functions/head.php

https://github.com/livinglab/openlab · PHP · 267 lines · 138 code · 31 blank · 98 comment · 32 complexity · 7ee9f0c6c07ff8ffd1bcc585c6a25677 MD5 · raw file

  1. <?php
  2. /**
  3. * Genesis Framework.
  4. *
  5. * WARNING: This file is part of the core Genesis Framework. DO NOT edit this file under any circumstances.
  6. * Please do all modifications in the form of a child theme.
  7. *
  8. * @package Genesis\Header
  9. * @author StudioPress
  10. * @license GPL-2.0-or-later
  11. * @link https://my.studiopress.com/themes/genesis/
  12. */
  13. /**
  14. * Determine the meta description based on contextual criteria.
  15. *
  16. * @since 2.4.0
  17. *
  18. * @return string Meta description.
  19. */
  20. function genesis_get_seo_meta_description() {
  21. $description = '';
  22. $post_id = null;
  23. // If we're on the root page.
  24. if ( genesis_is_root_page() ) {
  25. $description = genesis_get_seo_option( 'home_description' ) ? genesis_get_seo_option( 'home_description' ) : get_bloginfo( 'description' );
  26. }
  27. // When the page is set as the Posts Page in WordPress core, use the $post_id of the page when loading SEO values.
  28. if ( is_home() && get_option( 'page_for_posts' ) && get_queried_object_id() ) {
  29. $post_id = get_option( 'page_for_posts' );
  30. }
  31. // If we're on a single post / page / attachment.
  32. if ( null !== $post_id || is_singular() ) {
  33. if ( genesis_get_custom_field( '_genesis_description', $post_id ) ) {
  34. // Description is set via custom field.
  35. $description = genesis_get_custom_field( '_genesis_description', $post_id );
  36. } elseif ( genesis_get_custom_field( '_aioseop_description', $post_id ) ) {
  37. // All-in-One SEO Pack (latest, vestigial).
  38. $description = genesis_get_custom_field( '_aioseop_description', $post_id );
  39. } elseif ( genesis_get_custom_field( '_headspace_description', $post_id ) ) {
  40. // Headspace2 (vestigial).
  41. $description = genesis_get_custom_field( '_headspace_description', $post_id );
  42. } elseif ( genesis_get_custom_field( 'thesis_description', $post_id ) ) {
  43. // Thesis (vestigial).
  44. $description = genesis_get_custom_field( 'thesis_description', $post_id );
  45. } elseif ( genesis_get_custom_field( 'description', $post_id ) ) {
  46. // All-in-One SEO Pack (old, vestigial).
  47. $description = genesis_get_custom_field( 'description', $post_id );
  48. }
  49. } elseif ( is_category() || is_tag() || is_tax() ) {
  50. $term = get_queried_object();
  51. $term_description = get_term_meta( $term->term_id, 'description', true );
  52. $description = ! empty( $term_description ) ? $term_description : '';
  53. } elseif ( is_post_type_archive() && genesis_has_post_type_archive_support() ) {
  54. $cpt_description = genesis_get_cpt_option( 'description' );
  55. $description = $cpt_description ? $cpt_description : '';
  56. } elseif ( is_author() ) {
  57. $description = get_the_author_meta( 'meta_description', (int) get_query_var( 'author' ) );
  58. }
  59. /**
  60. * Filter the content for the description meta tag.
  61. *
  62. * @since 2.4.0
  63. *
  64. * @param string $description Meta description string.
  65. */
  66. $description = apply_filters( 'genesis_get_seo_meta_description', $description );
  67. return trim( $description );
  68. }
  69. /**
  70. * Determine the meta keywords based on contextual criteria.
  71. *
  72. * @since 2.4.0
  73. *
  74. * @return string Content for keywords meta tag.
  75. */
  76. function genesis_get_seo_meta_keywords() {
  77. $keywords = '';
  78. $post_id = null;
  79. // If we're on the root page.
  80. if ( genesis_is_root_page() ) {
  81. $keywords = genesis_get_seo_option( 'home_keywords' );
  82. }
  83. // When the page is set as the Posts Page in WordPress core, use the $post_id of the page when loading SEO values.
  84. if ( is_home() && get_option( 'page_for_posts' ) && get_queried_object_id() ) {
  85. $post_id = get_option( 'page_for_posts' );
  86. }
  87. if ( null !== $post_id || is_singular() ) {
  88. if ( genesis_get_custom_field( '_genesis_keywords', $post_id ) ) {
  89. // Keywords are set via custom field.
  90. $keywords = genesis_get_custom_field( '_genesis_keywords', $post_id );
  91. } elseif ( genesis_get_custom_field( '_aioseop_keywords', $post_id ) ) {
  92. // All-in-One SEO Pack (latest, vestigial).
  93. $keywords = genesis_get_custom_field( '_aioseop_keywords', $post_id );
  94. } elseif ( genesis_get_custom_field( 'thesis_keywords', $post_id ) ) {
  95. // Thesis (vestigial).
  96. $keywords = genesis_get_custom_field( 'thesis_keywords', $post_id );
  97. } elseif ( genesis_get_custom_field( 'keywords', $post_id ) ) {
  98. // All-in-One SEO Pack (old, vestigial).
  99. $keywords = genesis_get_custom_field( 'keywords', $post_id );
  100. }
  101. } elseif ( is_category() || is_tag() || is_tax() ) {
  102. $term = get_queried_object();
  103. $keywords = get_term_meta( $term->term_id, 'keywords', true );
  104. } elseif ( is_post_type_archive() && genesis_has_post_type_archive_support() ) {
  105. $keywords = genesis_get_cpt_option( 'keywords' ) ? genesis_get_cpt_option( 'keywords' ) : '';
  106. } elseif ( is_author() ) {
  107. $keywords = get_the_author_meta( 'meta_keywords', (int) get_query_var( 'author' ) );
  108. }
  109. /**
  110. * Filter the content for the keywords meta tag.
  111. *
  112. * @since 2.4.0
  113. *
  114. * @param string $keywords Meta keywords string.
  115. */
  116. $keywords = apply_filters( 'genesis_get_seo_meta_keywords', $keywords );
  117. return trim( $keywords );
  118. }
  119. /**
  120. * Determine the `noindex`, `nofollow`, `noodp`, `noydir`, `noarchive` robots meta code for the current context.
  121. *
  122. * @since 2.4.0
  123. *
  124. * @global WP_Query $wp_query Query object.
  125. *
  126. * @return string String for `content` attribute of `robots` meta tag.
  127. */
  128. function genesis_get_robots_meta_content() {
  129. global $wp_query;
  130. $post_id = null;
  131. // Defaults.
  132. $directives = array(
  133. 'noindex' => '',
  134. 'nofollow' => '',
  135. 'noarchive' => genesis_get_seo_option( 'noarchive' ) ? 'noarchive' : '',
  136. 'noodp' => genesis_get_seo_option( 'noodp' ) ? 'noodp' : '',
  137. 'noydir' => genesis_get_seo_option( 'noydir' ) ? 'noydir' : '',
  138. );
  139. // Check root page SEO settings, set noindex, nofollow and noarchive.
  140. if ( genesis_is_root_page() ) {
  141. $directives['noindex'] = genesis_get_seo_option( 'home_noindex' ) ? 'noindex' : $directives['noindex'];
  142. $directives['nofollow'] = genesis_get_seo_option( 'home_nofollow' ) ? 'nofollow' : $directives['nofollow'];
  143. $directives['noarchive'] = genesis_get_seo_option( 'home_noarchive' ) ? 'noarchive' : $directives['noarchive'];
  144. }
  145. // When the page is set as the Posts Page in WordPress core, use the $post_id of the page when loading SEO values.
  146. if ( is_home() && get_option( 'page_for_posts' ) && get_queried_object_id() ) {
  147. $post_id = get_option( 'page_for_posts' );
  148. }
  149. if ( null !== $post_id || is_singular() ) {
  150. $directives['noindex'] = genesis_get_custom_field( '_genesis_noindex', $post_id ) ? 'noindex' : $directives['noindex'];
  151. $directives['nofollow'] = genesis_get_custom_field( '_genesis_nofollow', $post_id ) ? 'nofollow' : $directives['nofollow'];
  152. $directives['noarchive'] = genesis_get_custom_field( '_genesis_noarchive', $post_id ) ? 'noarchive' : $directives['noarchive'];
  153. } elseif ( is_category() || is_tag() || is_tax() ) {
  154. $term = $wp_query->get_queried_object();
  155. $directives['noindex'] = get_term_meta( $term->term_id, 'noindex', true ) ? 'noindex' : $directives['noindex'];
  156. $directives['nofollow'] = get_term_meta( $term->term_id, 'nofollow', true ) ? 'nofollow' : $directives['nofollow'];
  157. $directives['noarchive'] = get_term_meta( $term->term_id, 'noarchive', true ) ? 'noarchive' : $directives['noarchive'];
  158. if ( is_category() ) {
  159. $directives['noindex'] = genesis_get_seo_option( 'noindex_cat_archive' ) ? 'noindex' : $directives['noindex'];
  160. $directives['noarchive'] = genesis_get_seo_option( 'noarchive_cat_archive' ) ? 'noarchive' : $directives['noarchive'];
  161. } elseif ( is_tag() ) {
  162. $directives['noindex'] = genesis_get_seo_option( 'noindex_tag_archive' ) ? 'noindex' : $directives['noindex'];
  163. $directives['noarchive'] = genesis_get_seo_option( 'noarchive_tag_archive' ) ? 'noarchive' : $directives['noarchive'];
  164. }
  165. } elseif ( is_post_type_archive() && genesis_has_post_type_archive_support() ) {
  166. $directives['noindex'] = genesis_get_cpt_option( 'noindex' ) ? 'noindex' : $directives['noindex'];
  167. $directives['nofollow'] = genesis_get_cpt_option( 'nofollow' ) ? 'nofollow' : $directives['nofollow'];
  168. $directives['noarchive'] = genesis_get_cpt_option( 'noarchive' ) ? 'noarchive' : $directives['noarchive'];
  169. } elseif ( is_author() ) {
  170. $directives['noindex'] = get_the_author_meta( 'noindex', (int) get_query_var( 'author' ) ) ? 'noindex' : $directives['noindex'];
  171. $directives['nofollow'] = get_the_author_meta( 'nofollow', (int) get_query_var( 'author' ) ) ? 'nofollow' : $directives['nofollow'];
  172. $directives['noarchive'] = get_the_author_meta( 'noarchive', (int) get_query_var( 'author' ) ) ? 'noarchive' : $directives['noarchive'];
  173. $directives['noindex'] = genesis_get_seo_option( 'noindex_author_archive' ) ? 'noindex' : $directives['noindex'];
  174. $directives['noarchive'] = genesis_get_seo_option( 'noarchive_author_archive' ) ? 'noarchive' : $directives['noarchive'];
  175. } elseif ( is_date() ) {
  176. $directives['noindex'] = genesis_get_seo_option( 'noindex_date_archive' ) ? 'noindex' : $directives['noindex'];
  177. $directives['noarchive'] = genesis_get_seo_option( 'noarchive_date_archive' ) ? 'noarchive' : $directives['noarchive'];
  178. } elseif ( is_search() ) {
  179. $directives['noindex'] = genesis_get_seo_option( 'noindex_search_archive' ) ? 'noindex' : $directives['noindex'];
  180. $directives['noarchive'] = genesis_get_seo_option( 'noarchive_search_archive' ) ? 'noarchive' : $directives['noarchive'];
  181. }
  182. /**
  183. * Filter the array of directives for the robots meta tag.
  184. *
  185. * @since 2.4.0
  186. *
  187. * @param array $directives May contain keys for `noindex`, `nofollow`, `noodp`, `noydir`, `noarchive`.
  188. */
  189. $directives = apply_filters( 'genesis_get_robots_meta_content', $directives );
  190. // Strip empty array items.
  191. $directives = array_filter( $directives );
  192. return implode( ',', $directives );
  193. }
  194. /**
  195. * Return favicon URL.
  196. *
  197. * Falls back to Genesis theme favicon.
  198. *
  199. * URL to favicon is filtered via `genesis_favicon_url` before being echoed.
  200. *
  201. * @since 2.4.0
  202. *
  203. * @return string Path to favicon.
  204. */
  205. function genesis_get_favicon_url() {
  206. /**
  207. * Filter to allow child theme to short-circuit this function.
  208. *
  209. * @since 1.1.2
  210. *
  211. * @param bool $favicon `false`.
  212. */
  213. $pre = apply_filters( 'genesis_pre_load_favicon', false );
  214. if ( false !== $pre ) {
  215. $favicon = $pre;
  216. } elseif ( file_exists( CHILD_DIR . '/images/favicon.ico' ) ) {
  217. $favicon = CHILD_URL . '/images/favicon.ico';
  218. } elseif ( file_exists( CHILD_DIR . '/images/favicon.gif' ) ) {
  219. $favicon = CHILD_URL . '/images/favicon.gif';
  220. } elseif ( file_exists( CHILD_DIR . '/images/favicon.png' ) ) {
  221. $favicon = CHILD_URL . '/images/favicon.png';
  222. } elseif ( file_exists( CHILD_DIR . '/images/favicon.jpg' ) ) {
  223. $favicon = CHILD_URL . '/images/favicon.jpg';
  224. } else {
  225. $favicon = GENESIS_IMAGES_URL . '/favicon.ico';
  226. }
  227. /**
  228. * Filter the favicon URL.
  229. *
  230. * @since 1.0.0
  231. *
  232. * @param string $favicon Favicon URL.
  233. */
  234. $favicon = apply_filters( 'genesis_favicon_url', $favicon );
  235. return trim( $favicon );
  236. }