wpseo-functions.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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,
  78. * could be a post, taxonomy or term.
  79. * @param array $omit Variables that should not be replaced by this function.
  80. *
  81. * @return string
  82. */
  83. function wpseo_replace_vars( $string, $args, $omit = [] ) {
  84. $replacer = new WPSEO_Replace_Vars();
  85. return $replacer->replace( $string, $args, $omit );
  86. }
  87. /**
  88. * Register a new variable replacement.
  89. *
  90. * This function is for use by other plugins/themes to easily add their own additional variables to replace.
  91. * This function should be called from a function on the 'wpseo_register_extra_replacements' action hook.
  92. * The use of this function is preferred over the older 'wpseo_replacements' filter as a way to add new replacements.
  93. * The 'wpseo_replacements' filter should still be used to adjust standard WPSEO replacement values.
  94. * The function can not be used to replace standard WPSEO replacement value functions and will thrown a warning
  95. * if you accidently try.
  96. * To avoid conflicts with variables registered by WPSEO and other themes/plugins, try and make the
  97. * name of your variable unique. Variable names also can not start with "%%cf_" or "%%ct_" as these are reserved
  98. * for the standard WPSEO variable variables 'cf_<custom-field-name>', 'ct_<custom-tax-name>' and
  99. * 'ct_desc_<custom-tax-name>'.
  100. * The replacement function will be passed the undelimited name (i.e. stripped of the %%) of the variable
  101. * to replace in case you need it.
  102. *
  103. * Example code:
  104. * <code>
  105. * <?php
  106. * function retrieve_var1_replacement( $var1 ) {
  107. * return 'your replacement value';
  108. * }
  109. *
  110. * function register_my_plugin_extra_replacements() {
  111. * wpseo_register_var_replacement( '%%myvar1%%', 'retrieve_var1_replacement', 'advanced', 'this is a help text for myvar1' );
  112. * wpseo_register_var_replacement( 'myvar2', array( 'class', 'method_name' ), 'basic', 'this is a help text for myvar2' );
  113. * }
  114. * add_action( 'wpseo_register_extra_replacements', 'register_my_plugin_extra_replacements' );
  115. * ?>
  116. * </code>
  117. *
  118. * @since 1.5.4
  119. *
  120. * @param string $var The name of the variable to replace, i.e. '%%var%%'.
  121. * Note: the surrounding %% are optional, name can only contain [A-Za-z0-9_-].
  122. * @param mixed $replace_function Function or method to call to retrieve the replacement value for the variable.
  123. * Uses the same format as add_filter/add_action function parameter and
  124. * should *return* the replacement value. DON'T echo it.
  125. * @param string $type Type of variable: 'basic' or 'advanced', defaults to 'advanced'.
  126. * @param string $help_text Help text to be added to the help tab for this variable.
  127. *
  128. * @return bool Whether the replacement function was successfully registered.
  129. */
  130. function wpseo_register_var_replacement( $var, $replace_function, $type = 'advanced', $help_text = '' ) {
  131. return WPSEO_Replace_Vars::register_replacement( $var, $replace_function, $type, $help_text );
  132. }
  133. /**
  134. * WPML plugin support: Set titles for custom types / taxonomies as translatable.
  135. *
  136. * It adds new keys to a wpml-config.xml file for a custom post type title, metadesc,
  137. * title-ptarchive and metadesc-ptarchive fields translation.
  138. * Documentation: http://wpml.org/documentation/support/language-configuration-files/
  139. *
  140. * @global $sitepress
  141. *
  142. * @param array $config WPML configuration data to filter.
  143. *
  144. * @return array
  145. */
  146. function wpseo_wpml_config( $config ) {
  147. global $sitepress;
  148. 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'] !== [] ) ) {
  149. $admin_texts = $config['wpml-config']['admin-texts']['key'];
  150. foreach ( $admin_texts as $k => $val ) {
  151. if ( $val['attr']['name'] === 'wpseo_titles' ) {
  152. $translate_cp = array_keys( $sitepress->get_translatable_documents() );
  153. if ( is_array( $translate_cp ) && $translate_cp !== [] ) {
  154. foreach ( $translate_cp as $post_type ) {
  155. $admin_texts[ $k ]['key'][]['attr']['name'] = 'title-' . $post_type;
  156. $admin_texts[ $k ]['key'][]['attr']['name'] = 'metadesc-' . $post_type;
  157. $admin_texts[ $k ]['key'][]['attr']['name'] = 'title-ptarchive-' . $post_type;
  158. $admin_texts[ $k ]['key'][]['attr']['name'] = 'metadesc-ptarchive-' . $post_type;
  159. $translate_tax = $sitepress->get_translatable_taxonomies( false, $post_type );
  160. if ( is_array( $translate_tax ) && $translate_tax !== [] ) {
  161. foreach ( $translate_tax as $taxonomy ) {
  162. $admin_texts[ $k ]['key'][]['attr']['name'] = 'title-tax-' . $taxonomy;
  163. $admin_texts[ $k ]['key'][]['attr']['name'] = 'metadesc-tax-' . $taxonomy;
  164. }
  165. }
  166. }
  167. }
  168. break;
  169. }
  170. }
  171. $config['wpml-config']['admin-texts']['key'] = $admin_texts;
  172. }
  173. return $config;
  174. }
  175. add_filter( 'icl_wpml_config_array', 'wpseo_wpml_config' );
  176. /**
  177. * Yoast SEO breadcrumb shortcode.
  178. * [wpseo_breadcrumb]
  179. *
  180. * @return string
  181. */
  182. function wpseo_shortcode_yoast_breadcrumb() {
  183. return yoast_breadcrumb( '', '', false );
  184. }
  185. add_shortcode( 'wpseo_breadcrumb', 'wpseo_shortcode_yoast_breadcrumb' );
  186. if ( ! extension_loaded( 'ctype' ) || ! function_exists( 'ctype_digit' ) ) {
  187. /**
  188. * Emulate PHP native ctype_digit() function for when the ctype extension would be disabled *sigh*.
  189. * Only emulates the behaviour for when the input is a string, does not handle integer input as ascii value.
  190. *
  191. * @param string $string String input to validate.
  192. *
  193. * @return bool
  194. */
  195. function ctype_digit( $string ) {
  196. $return = false;
  197. if ( ( is_string( $string ) && $string !== '' ) && preg_match( '`^\d+$`', $string ) === 1 ) {
  198. $return = true;
  199. }
  200. return $return;
  201. }
  202. }
  203. /**
  204. * Makes sure the taxonomy meta is updated when a taxonomy term is split.
  205. *
  206. * @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.
  207. *
  208. * @param string $old_term_id Old term id of the taxonomy term that was splitted.
  209. * @param string $new_term_id New term id of the taxonomy term that was splitted.
  210. * @param string $term_taxonomy_id Term taxonomy id for the taxonomy that was affected.
  211. * @param string $taxonomy The taxonomy that the taxonomy term was splitted for.
  212. */
  213. function wpseo_split_shared_term( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
  214. $tax_meta = get_option( 'wpseo_taxonomy_meta', [] );
  215. if ( ! empty( $tax_meta[ $taxonomy ][ $old_term_id ] ) ) {
  216. $tax_meta[ $taxonomy ][ $new_term_id ] = $tax_meta[ $taxonomy ][ $old_term_id ];
  217. unset( $tax_meta[ $taxonomy ][ $old_term_id ] );
  218. update_option( 'wpseo_taxonomy_meta', $tax_meta );
  219. }
  220. }
  221. add_action( 'split_shared_term', 'wpseo_split_shared_term', 10, 4 );
  222. /**
  223. * Get all WPSEO related capabilities.
  224. *
  225. * @since 8.3
  226. * @return array
  227. */
  228. function wpseo_get_capabilities() {
  229. if ( ! did_action( 'wpseo_register_capabilities' ) ) {
  230. do_action( 'wpseo_register_capabilities' );
  231. }
  232. return WPSEO_Capability_Manager_Factory::get()->get_capabilities();
  233. }