jetpack-seo-titles.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /*
  3. * Each title format is an array of arrays containing two values:
  4. * - type
  5. * - value
  6. *
  7. * Possible values for type are: 'token' and 'string'.
  8. * Possible values for 'value' are: any string in case that 'type' is set
  9. * to 'string', or allowed token values for page type in case that 'type'
  10. * is set to 'token'.
  11. *
  12. * Examples of valid formats:
  13. *
  14. * [
  15. * 'front_page' => [
  16. * [ 'type' => 'string', 'value' => 'Front page title and site name:'],
  17. * [ 'type' => 'token', 'value' => 'site_name']
  18. * ],
  19. * 'posts' => [
  20. * [ 'type' => 'token', 'value' => 'site_name' ],
  21. * [ 'type' => 'string', 'value' => ' | ' ],
  22. * [ 'type' => 'token', 'value' => 'post_title' ]
  23. * ],
  24. * 'pages' => [],
  25. * 'groups' => [],
  26. * 'archives' => []
  27. * ]
  28. * Custom title for given page type is created by concatenating all of the array 'value' parts.
  29. * Tokens are replaced with their corresponding values for current site.
  30. * Empty array signals that we are not overriding the default title for particular page type.
  31. */
  32. /**
  33. * Class containing utility static methods for managing SEO custom title formats.
  34. */
  35. class Jetpack_SEO_Titles {
  36. /**
  37. * Site option name used to store custom title formats.
  38. */
  39. const TITLE_FORMATS_OPTION = 'advanced_seo_title_formats';
  40. /**
  41. * Retrieves custom title formats from site option.
  42. *
  43. * @return array Array of custom title formats, or empty array.
  44. */
  45. public static function get_custom_title_formats() {
  46. if( Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) {
  47. return get_option( self::TITLE_FORMATS_OPTION, array() );
  48. }
  49. return array();
  50. }
  51. /**
  52. * Returns tokens that are currently supported for each page type.
  53. *
  54. * @return array Array of allowed token strings.
  55. */
  56. public static function get_allowed_tokens() {
  57. return array(
  58. 'front_page' => array( 'site_name', 'tagline' ),
  59. 'posts' => array( 'site_name', 'tagline', 'post_title' ),
  60. 'pages' => array( 'site_name', 'tagline', 'page_title' ),
  61. 'groups' => array( 'site_name', 'tagline', 'group_title' ),
  62. 'archives' => array( 'site_name', 'tagline', 'date' ),
  63. );
  64. }
  65. /**
  66. * Used to modify the default title with custom SEO title.
  67. *
  68. * @param string $default_title Default title for current page.
  69. *
  70. * @return string Custom title with replaced tokens or default title.
  71. */
  72. public static function get_custom_title( $default_title = '' ) {
  73. // Don't filter title for unsupported themes.
  74. if ( self::is_conflicted_theme() ) {
  75. return $default_title;
  76. }
  77. $page_type = self::get_page_type();
  78. // Keep default title if invalid page type is supplied.
  79. if ( empty( $page_type ) ) {
  80. return $default_title;
  81. }
  82. $title_formats = self::get_custom_title_formats();
  83. // Keep default title if user has not defined custom title for this page type.
  84. if ( empty( $title_formats[ $page_type ] ) ) {
  85. return $default_title;
  86. }
  87. if ( ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) {
  88. return $default_title;
  89. }
  90. $custom_title = '';
  91. $format_array = $title_formats[ $page_type ];
  92. foreach ( $format_array as $item ) {
  93. if ( 'token' == $item['type'] ) {
  94. $custom_title .= self::get_token_value( $item['value'] );
  95. } else {
  96. $custom_title .= $item['value'];
  97. }
  98. }
  99. return esc_html( $custom_title );
  100. }
  101. /**
  102. * Returns string value for given token.
  103. *
  104. * @param string $token_name The token name value that should be replaced.
  105. *
  106. * @return string Token replacement for current site, or empty string for unknown token name.
  107. */
  108. public static function get_token_value( $token_name ) {
  109. switch ( $token_name ) {
  110. case 'site_name':
  111. return get_bloginfo( 'name' );
  112. case 'tagline':
  113. return get_bloginfo( 'description' );
  114. case 'post_title':
  115. case 'page_title':
  116. return get_the_title();
  117. case 'group_title':
  118. return single_tag_title( '', false );
  119. case 'date':
  120. return self::get_date_for_title();
  121. default:
  122. return '';
  123. }
  124. }
  125. /**
  126. * Returns page type for current page. We need this helper in order to determine what
  127. * user defined title format should be used for custom title.
  128. *
  129. * @return string|bool Type of current page or false if unsupported.
  130. */
  131. public static function get_page_type() {
  132. if ( is_front_page() ) {
  133. return 'front_page';
  134. }
  135. if ( is_category() || is_tag() ) {
  136. return 'groups';
  137. }
  138. if ( is_archive() && ! is_author() ) {
  139. return 'archives';
  140. }
  141. if ( is_page() ) {
  142. return 'pages';
  143. }
  144. if ( is_singular() ) {
  145. return 'posts';
  146. }
  147. return false;
  148. }
  149. /**
  150. * Returns the value that should be used as a replacement for the date token,
  151. * depending on the archive path specified.
  152. *
  153. * @return string Token replacement for a given date, or empty string if no date is specified.
  154. */
  155. public static function get_date_for_title() {
  156. // If archive year, month, and day are specified.
  157. if ( is_day() ) {
  158. return get_the_date();
  159. }
  160. // If archive year, and month are specified.
  161. if ( is_month() ) {
  162. return trim( single_month_title( ' ', false ) );
  163. }
  164. // Only archive year is specified.
  165. if ( is_year() ) {
  166. return get_query_var( 'year' );
  167. }
  168. return '';
  169. }
  170. /**
  171. * Checks if current theme is defining custom title that won't work nicely
  172. * with our custom SEO title override.
  173. *
  174. * @return bool True if current theme sets custom title, false otherwise.
  175. */
  176. public static function is_conflicted_theme() {
  177. /**
  178. * Can be used to specify a list of themes that use their own custom title format.
  179. *
  180. * If current site is using one of the themes listed as conflicting,
  181. * Jetpack SEO custom title formats will be disabled.
  182. *
  183. * @module seo-tools
  184. *
  185. * @since 4.4.0
  186. *
  187. * @param array List of conflicted theme names. Defaults to empty array.
  188. */
  189. $conflicted_themes = apply_filters( 'jetpack_seo_custom_title_conflicted_themes', array() );
  190. return isset( $conflicted_themes[ get_option( 'template' ) ] );
  191. }
  192. /**
  193. * Checks if a given format conforms to predefined SEO title templates.
  194. *
  195. * Every format type and token must be whitelisted.
  196. * @see get_allowed_tokens()
  197. *
  198. * @param array $title_formats Template of SEO title to check.
  199. *
  200. * @return bool True if the formats are valid, false otherwise.
  201. */
  202. public static function are_valid_title_formats( $title_formats ) {
  203. $allowed_tokens = self::get_allowed_tokens();
  204. if ( ! is_array( $title_formats ) ) {
  205. return false;
  206. }
  207. foreach ( $title_formats as $format_type => $format_array ) {
  208. if ( ! in_array( $format_type, array_keys( $allowed_tokens ) ) ) {
  209. return false;
  210. }
  211. if ( '' === $format_array ) {
  212. continue;
  213. }
  214. if ( ! is_array( $format_array ) ) {
  215. return false;
  216. }
  217. foreach ( $format_array as $item ) {
  218. if ( empty( $item['type'] ) || empty( $item['value'] ) ) {
  219. return false;
  220. }
  221. if ( 'token' == $item['type'] ) {
  222. if ( ! in_array( $item['value'], $allowed_tokens[ $format_type ] ) ) {
  223. return false;
  224. }
  225. }
  226. }
  227. }
  228. return true;
  229. }
  230. /**
  231. * Combines the previous values of title formats, stored as array in site options,
  232. * with the new values that are provided.
  233. *
  234. * @param array $new_formats Array containing new title formats.
  235. *
  236. * @return array $result Array of updated title formats, or empty array if no update was performed.
  237. */
  238. public static function update_title_formats( $new_formats ) {
  239. // Empty array signals that custom title shouldn't be used.
  240. $empty_formats = array(
  241. 'front_page' => array(),
  242. 'posts' => array(),
  243. 'pages' => array(),
  244. 'groups' => array(),
  245. 'archives' => array(),
  246. );
  247. $previous_formats = self::get_custom_title_formats();
  248. $result = array_merge( $empty_formats, $previous_formats, $new_formats );
  249. if ( update_option( self::TITLE_FORMATS_OPTION, $result ) ) {
  250. return $result;
  251. }
  252. return array();
  253. }
  254. }