wpseo-functions.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internals
  6. */
  7. if ( ! defined( 'WPSEO_VERSION' ) ) {
  8. header( 'Status: 403 Forbidden' );
  9. header( 'HTTP/1.1 403 Forbidden' );
  10. exit();
  11. }
  12. if ( ! function_exists( 'initialize_wpseo_front' ) ) {
  13. /**
  14. * Wraps frontend class.
  15. */
  16. function initialize_wpseo_front() {
  17. WPSEO_Frontend::get_instance();
  18. }
  19. }
  20. if ( ! function_exists( 'yoast_breadcrumb' ) ) {
  21. /**
  22. * Template tag for breadcrumbs.
  23. *
  24. * @param string $before What to show before the breadcrumb.
  25. * @param string $after What to show after the breadcrumb.
  26. * @param bool $display Whether to display the breadcrumb (true) or return it (false).
  27. *
  28. * @return string
  29. */
  30. function yoast_breadcrumb( $before = '', $after = '', $display = true ) {
  31. $breadcrumbs_enabled = current_theme_supports( 'yoast-seo-breadcrumbs' );
  32. if ( ! $breadcrumbs_enabled ) {
  33. $breadcrumbs_enabled = WPSEO_Options::get( 'breadcrumbs-enable', false );
  34. }
  35. if ( $breadcrumbs_enabled ) {
  36. return WPSEO_Breadcrumbs::breadcrumb( $before, $after, $display );
  37. }
  38. }
  39. }
  40. if ( ! function_exists( 'yoast_get_primary_term_id' ) ) {
  41. /**
  42. * Get the primary term ID.
  43. *
  44. * @param string $taxonomy Optional. The taxonomy to get the primary term ID for. Defaults to category.
  45. * @param null|int|WP_Post $post Optional. Post to get the primary term ID for.
  46. *
  47. * @return bool|int
  48. */
  49. function yoast_get_primary_term_id( $taxonomy = 'category', $post = null ) {
  50. $post = get_post( $post );
  51. $primary_term = new WPSEO_Primary_Term( $taxonomy, $post->ID );
  52. return $primary_term->get_primary_term();
  53. }
  54. }
  55. if ( ! function_exists( 'yoast_get_primary_term' ) ) {
  56. /**
  57. * Get the primary term name.
  58. *
  59. * @param string $taxonomy Optional. The taxonomy to get the primary term for. Defaults to category.
  60. * @param null|int|WP_Post $post Optional. Post to get the primary term for.
  61. *
  62. * @return string Name of the primary term.
  63. */
  64. function yoast_get_primary_term( $taxonomy = 'category', $post = null ) {
  65. $primary_term_id = yoast_get_primary_term_id( $taxonomy, $post );
  66. $term = get_term( $primary_term_id );
  67. if ( ! is_wp_error( $term ) && ! empty( $term ) ) {
  68. return $term->name;
  69. }
  70. return '';
  71. }
  72. }
  73. /**
  74. * Replace `%%variable_placeholders%%` with their real value based on the current requested page/post/cpt.
  75. *
  76. * @param string $string The string to replace the variables in.
  77. * @param object $args The object some of the replacement values might come from, could be a post, taxonomy or term.
  78. * @param array $omit Variables that should not be replaced by this function.
  79. *
  80. * @return string
  81. */
  82. function wpseo_replace_vars( $string, $args, $omit = array() ) {
  83. $replacer = new WPSEO_Replace_Vars();
  84. return $replacer->replace( $string, $args, $omit );
  85. }
  86. /**
  87. * Register a new variable replacement.
  88. *
  89. * This function is for use by other plugins/themes to easily add their own additional variables to replace.
  90. * This function should be called from a function on the 'wpseo_register_extra_replacements' action hook.
  91. * The use of this function is preferred over the older 'wpseo_replacements' filter as a way to add new replacements.
  92. * The 'wpseo_replacements' filter should still be used to adjust standard WPSEO replacement values.
  93. * The function can not be used to replace standard WPSEO replacement value functions and will thrown a warning
  94. * if you accidently try.
  95. * To avoid conflicts with variables registered by WPSEO and other themes/plugins, try and make the
  96. * name of your variable unique. Variable names also can not start with "%%cf_" or "%%ct_" as these are reserved
  97. * for the standard WPSEO variable variables 'cf_<custom-field-name>', 'ct_<custom-tax-name>' and
  98. * 'ct_desc_<custom-tax-name>'.
  99. * The replacement function will be passed the undelimited name (i.e. stripped of the %%) of the variable
  100. * to replace in case you need it.
  101. *
  102. * Example code:
  103. * <code>
  104. * <?php
  105. * function retrieve_var1_replacement( $var1 ) {
  106. * return 'your replacement value';
  107. * }
  108. *
  109. * function register_my_plugin_extra_replacements() {
  110. * wpseo_register_var_replacement( '%%myvar1%%', 'retrieve_var1_replacement', 'advanced', 'this is a help text for myvar1' );
  111. * wpseo_register_var_replacement( 'myvar2', array( 'class', 'method_name' ), 'basic', 'this is a help text for myvar2' );
  112. * }
  113. * add_action( 'wpseo_register_extra_replacements', 'register_my_plugin_extra_replacements' );
  114. * ?>
  115. * </code>
  116. *
  117. * @since 1.5.4
  118. *
  119. * @param string $var The name of the variable to replace, i.e. '%%var%%'
  120. * - the surrounding %% are optional, name can only contain [A-Za-z0-9_-].
  121. * @param mixed $replace_function Function or method to call to retrieve the replacement value for the variable
  122. * Uses the same format as add_filter/add_action function parameter and
  123. * should *return* the replacement value. DON'T echo it.
  124. * @param string $type Type of variable: 'basic' or 'advanced', defaults to 'advanced'.
  125. * @param string $help_text Help text to be added to the help tab for this variable.
  126. *
  127. * @return bool Whether the replacement function was succesfully registered.
  128. */
  129. function wpseo_register_var_replacement( $var, $replace_function, $type = 'advanced', $help_text = '' ) {
  130. return WPSEO_Replace_Vars::register_replacement( $var, $replace_function, $type, $help_text );
  131. }
  132. /**
  133. * WPML plugin support: Set titles for custom types / taxonomies as translatable.
  134. * It adds new keys to a wpml-config.xml file for a custom post type title, metadesc, title-ptarchive and metadesc-ptarchive fields translation.
  135. * Documentation: http://wpml.org/documentation/support/language-configuration-files/
  136. *
  137. * @global $sitepress
  138. *
  139. * @param array $config WPML configuration data to filter.
  140. *
  141. * @return array
  142. */
  143. function wpseo_wpml_config( $config ) {
  144. global $sitepress;
  145. if ( ( is_array( $config ) && isset( $config['wpml-config']['admin-texts']['key'] ) ) && ( is_array( $config['wpml-config']['admin-texts']['key'] ) && $config['wpml-config']['admin-texts']['key'] !== array() ) ) {
  146. $admin_texts = $config['wpml-config']['admin-texts']['key'];
  147. foreach ( $admin_texts as $k => $val ) {
  148. if ( $val['attr']['name'] === 'wpseo_titles' ) {
  149. $translate_cp = array_keys( $sitepress->get_translatable_documents() );
  150. if ( is_array( $translate_cp ) && $translate_cp !== array() ) {
  151. foreach ( $translate_cp as $post_type ) {
  152. $admin_texts[ $k ]['key'][]['attr']['name'] = 'title-' . $post_type;
  153. $admin_texts[ $k ]['key'][]['attr']['name'] = 'metadesc-' . $post_type;
  154. $admin_texts[ $k ]['key'][]['attr']['name'] = 'title-ptarchive-' . $post_type;
  155. $admin_texts[ $k ]['key'][]['attr']['name'] = 'metadesc-ptarchive-' . $post_type;
  156. $translate_tax = $sitepress->get_translatable_taxonomies( false, $post_type );
  157. if ( is_array( $translate_tax ) && $translate_tax !== array() ) {
  158. foreach ( $translate_tax as $taxonomy ) {
  159. $admin_texts[ $k ]['key'][]['attr']['name'] = 'title-tax-' . $taxonomy;
  160. $admin_texts[ $k ]['key'][]['attr']['name'] = 'metadesc-tax-' . $taxonomy;
  161. }
  162. }
  163. }
  164. }
  165. break;
  166. }
  167. }
  168. $config['wpml-config']['admin-texts']['key'] = $admin_texts;
  169. }
  170. return $config;
  171. }
  172. add_filter( 'icl_wpml_config_array', 'wpseo_wpml_config' );
  173. /**
  174. * Yoast SEO breadcrumb shortcode.
  175. * [wpseo_breadcrumb]
  176. *
  177. * @return string
  178. */
  179. function wpseo_shortcode_yoast_breadcrumb() {
  180. return yoast_breadcrumb( '', '', false );
  181. }
  182. add_shortcode( 'wpseo_breadcrumb', 'wpseo_shortcode_yoast_breadcrumb' );
  183. /**
  184. * Emulate PHP native ctype_digit() function for when the ctype extension would be disabled *sigh*.
  185. * Only emulates the behaviour for when the input is a string, does not handle integer input as ascii value.
  186. *
  187. * @param string $string
  188. *
  189. * @return bool
  190. */
  191. if ( ! extension_loaded( 'ctype' ) || ! function_exists( 'ctype_digit' ) ) {
  192. /**
  193. * @param string $string String input to validate.
  194. *
  195. * @return bool
  196. */
  197. function ctype_digit( $string ) {
  198. $return = false;
  199. if ( ( is_string( $string ) && $string !== '' ) && preg_match( '`^\d+$`', $string ) === 1 ) {
  200. $return = true;
  201. }
  202. return $return;
  203. }
  204. }
  205. /**
  206. * Makes sure the taxonomy meta is updated when a taxonomy term is split.
  207. *
  208. * @link https://make.wordpress.org/core/2015/02/16/taxonomy-term-splitting-in-4-2-a-developer-guide/ Article explaining the taxonomy term splitting in WP 4.2.
  209. *
  210. * @param string $old_term_id Old term id of the taxonomy term that was splitted.
  211. * @param string $new_term_id New term id of the taxonomy term that was splitted.
  212. * @param string $term_taxonomy_id Term taxonomy id for the taxonomy that was affected.
  213. * @param string $taxonomy The taxonomy that the taxonomy term was splitted for.
  214. */
  215. function wpseo_split_shared_term( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
  216. $tax_meta = get_option( 'wpseo_taxonomy_meta', array() );
  217. if ( ! empty( $tax_meta[ $taxonomy ][ $old_term_id ] ) ) {
  218. $tax_meta[ $taxonomy ][ $new_term_id ] = $tax_meta[ $taxonomy ][ $old_term_id ];
  219. unset( $tax_meta[ $taxonomy ][ $old_term_id ] );
  220. update_option( 'wpseo_taxonomy_meta', $tax_meta );
  221. }
  222. }
  223. add_action( 'split_shared_term', 'wpseo_split_shared_term', 10, 4 );
  224. /**
  225. * Get all WPSEO related capabilities.
  226. *
  227. * @since 8.3
  228. * @return array
  229. */
  230. function wpseo_get_capabilities() {
  231. if ( ! did_action( 'wpseo_register_capabilities' ) ) {
  232. do_action( 'wpseo_register_capabilities' );
  233. }
  234. return WPSEO_Capability_Manager_Factory::get()->get_capabilities();
  235. }