| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Internals |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'WPSEO_VERSION' ) ) { |
| 9 |
header( 'Status: 403 Forbidden' ); |
| 10 |
header( 'HTTP/1.1 403 Forbidden' ); |
| 11 |
exit(); |
| 12 |
} |
| 13 |
|
| 14 |
if ( ! function_exists( 'yoast_breadcrumb' ) ) { |
| 15 |
/** |
| 16 |
* Template tag for breadcrumbs. |
| 17 |
* |
| 18 |
* @param string $before What to show before the breadcrumb. |
| 19 |
* @param string $after What to show after the breadcrumb. |
| 20 |
* @param bool $display Whether to display the breadcrumb (true) or return it (false). |
| 21 |
* |
| 22 |
* @return string |
| 23 |
*/ |
| 24 |
function yoast_breadcrumb( $before = '', $after = '', $display = true ) { |
| 25 |
$breadcrumbs_enabled = current_theme_supports( 'yoast-seo-breadcrumbs' ); |
| 26 |
if ( ! $breadcrumbs_enabled ) { |
| 27 |
$breadcrumbs_enabled = WPSEO_Options::get( 'breadcrumbs-enable', false ); |
| 28 |
} |
| 29 |
|
| 30 |
if ( $breadcrumbs_enabled ) { |
| 31 |
return WPSEO_Breadcrumbs::breadcrumb( $before, $after, $display ); |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
if ( ! function_exists( 'yoast_get_primary_term_id' ) ) { |
| 37 |
/** |
| 38 |
* Get the primary term ID. |
| 39 |
* |
| 40 |
* @param string $taxonomy Optional. The taxonomy to get the primary term ID for. Defaults to category. |
| 41 |
* @param int|WP_Post|null $post Optional. Post to get the primary term ID for. |
| 42 |
* |
| 43 |
* @return bool|int |
| 44 |
*/ |
| 45 |
function yoast_get_primary_term_id( $taxonomy = 'category', $post = null ) { |
| 46 |
$post = get_post( $post ); |
| 47 |
|
| 48 |
$primary_term = new WPSEO_Primary_Term( $taxonomy, $post->ID ); |
| 49 |
return $primary_term->get_primary_term(); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if ( ! function_exists( 'yoast_get_primary_term' ) ) { |
| 54 |
/** |
| 55 |
* Get the primary term name. |
| 56 |
* |
| 57 |
* @param string $taxonomy Optional. The taxonomy to get the primary term for. Defaults to category. |
| 58 |
* @param int|WP_Post|null $post Optional. Post to get the primary term for. |
| 59 |
* |
| 60 |
* @return string Name of the primary term. |
| 61 |
*/ |
| 62 |
function yoast_get_primary_term( $taxonomy = 'category', $post = null ) { |
| 63 |
$primary_term_id = yoast_get_primary_term_id( $taxonomy, $post ); |
| 64 |
|
| 65 |
$term = get_term( $primary_term_id ); |
| 66 |
if ( ! is_wp_error( $term ) && ! empty( $term ) ) { |
| 67 |
return $term->name; |
| 68 |
} |
| 69 |
|
| 70 |
return ''; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Replace `%%variable_placeholders%%` with their real value based on the current requested page/post/cpt. |
| 76 |
* |
| 77 |
* @param string $text The string to replace the variables in. |
| 78 |
* @param object $args The object some of the replacement values might come from, |
| 79 |
* could be a post, taxonomy or term. |
| 80 |
* @param array $omit Variables that should not be replaced by this function. |
| 81 |
* |
| 82 |
* @return string |
| 83 |
*/ |
| 84 |
function wpseo_replace_vars( $text, $args, $omit = [] ) { |
| 85 |
$replacer = new WPSEO_Replace_Vars(); |
| 86 |
|
| 87 |
return $replacer->replace( $text, $args, $omit ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Register a new variable replacement. |
| 92 |
* |
| 93 |
* This function is for use by other plugins/themes to easily add their own additional variables to replace. |
| 94 |
* This function should be called from a function on the 'wpseo_register_extra_replacements' action hook. |
| 95 |
* The use of this function is preferred over the older 'wpseo_replacements' filter as a way to add new replacements. |
| 96 |
* The 'wpseo_replacements' filter should still be used to adjust standard WPSEO replacement values. |
| 97 |
* The function can not be used to replace standard WPSEO replacement value functions and will thrown a warning |
| 98 |
* if you accidently try. |
| 99 |
* To avoid conflicts with variables registered by WPSEO and other themes/plugins, try and make the |
| 100 |
* name of your variable unique. Variable names also can not start with "%%cf_" or "%%ct_" as these are reserved |
| 101 |
* for the standard WPSEO variable variables 'cf_<custom-field-name>', 'ct_<custom-tax-name>' and |
| 102 |
* 'ct_desc_<custom-tax-name>'. |
| 103 |
* The replacement function will be passed the undelimited name (i.e. stripped of the %%) of the variable |
| 104 |
* to replace in case you need it. |
| 105 |
* |
| 106 |
* Example code: |
| 107 |
* <code> |
| 108 |
* <?php |
| 109 |
* function retrieve_var1_replacement( $var1 ) { |
| 110 |
* return 'your replacement value'; |
| 111 |
* } |
| 112 |
* |
| 113 |
* function register_my_plugin_extra_replacements() { |
| 114 |
* wpseo_register_var_replacement( '%%myvar1%%', 'retrieve_var1_replacement', 'advanced', 'this is a help text for myvar1' ); |
| 115 |
* wpseo_register_var_replacement( 'myvar2', array( 'class', 'method_name' ), 'basic', 'this is a help text for myvar2' ); |
| 116 |
* } |
| 117 |
* add_action( 'wpseo_register_extra_replacements', 'register_my_plugin_extra_replacements' ); |
| 118 |
* ?> |
| 119 |
* </code> |
| 120 |
* |
| 121 |
* @since 1.5.4 |
| 122 |
* |
| 123 |
* @param string $replacevar_name The name of the variable to replace, i.e. '%%var%%'. |
| 124 |
* Note: the surrounding %% are optional, name can only contain [A-Za-z0-9_-]. |
| 125 |
* @param mixed $replace_function Function or method to call to retrieve the replacement value for the variable. |
| 126 |
* Uses the same format as add_filter/add_action function parameter and |
| 127 |
* should *return* the replacement value. DON'T echo it. |
| 128 |
* @param string $type Type of variable: 'basic' or 'advanced', defaults to 'advanced'. |
| 129 |
* @param string $help_text Help text to be added to the help tab for this variable. |
| 130 |
* |
| 131 |
* @return bool Whether the replacement function was successfully registered. |
| 132 |
*/ |
| 133 |
function wpseo_register_var_replacement( $replacevar_name, $replace_function, $type = 'advanced', $help_text = '' ) { |
| 134 |
return WPSEO_Replace_Vars::register_replacement( $replacevar_name, $replace_function, $type, $help_text ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* WPML plugin support: Set titles for custom types / taxonomies as translatable. |
| 139 |
* |
| 140 |
* It adds new keys to a wpml-config.xml file for a custom post type title, metadesc, |
| 141 |
* title-ptarchive and metadesc-ptarchive fields translation. |
| 142 |
* Documentation: http://wpml.org/documentation/support/language-configuration-files/ |
| 143 |
* |
| 144 |
* @global $sitepress |
| 145 |
* |
| 146 |
* @param array $config WPML configuration data to filter. |
| 147 |
* |
| 148 |
* @return array |
| 149 |
*/ |
| 150 |
function wpseo_wpml_config( $config ) { |
| 151 |
global $sitepress; |
| 152 |
|
| 153 |
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'] !== [] ) ) { |
| 154 |
$admin_texts = $config['wpml-config']['admin-texts']['key']; |
| 155 |
foreach ( $admin_texts as $k => $val ) { |
| 156 |
if ( $val['attr']['name'] === 'wpseo_titles' ) { |
| 157 |
$translate_cp = array_keys( $sitepress->get_translatable_documents() ); |
| 158 |
if ( is_array( $translate_cp ) && $translate_cp !== [] ) { |
| 159 |
foreach ( $translate_cp as $post_type ) { |
| 160 |
$admin_texts[ $k ]['key'][]['attr']['name'] = 'title-' . $post_type; |
| 161 |
$admin_texts[ $k ]['key'][]['attr']['name'] = 'metadesc-' . $post_type; |
| 162 |
$admin_texts[ $k ]['key'][]['attr']['name'] = 'title-ptarchive-' . $post_type; |
| 163 |
$admin_texts[ $k ]['key'][]['attr']['name'] = 'metadesc-ptarchive-' . $post_type; |
| 164 |
|
| 165 |
$translate_tax = $sitepress->get_translatable_taxonomies( false, $post_type ); |
| 166 |
if ( is_array( $translate_tax ) && $translate_tax !== [] ) { |
| 167 |
foreach ( $translate_tax as $taxonomy ) { |
| 168 |
$admin_texts[ $k ]['key'][]['attr']['name'] = 'title-tax-' . $taxonomy; |
| 169 |
$admin_texts[ $k ]['key'][]['attr']['name'] = 'metadesc-tax-' . $taxonomy; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
$config['wpml-config']['admin-texts']['key'] = $admin_texts; |
| 178 |
} |
| 179 |
|
| 180 |
return $config; |
| 181 |
} |
| 182 |
|
| 183 |
add_filter( 'icl_wpml_config_array', 'wpseo_wpml_config' ); |
| 184 |
|
| 185 |
/** |
| 186 |
* Yoast SEO breadcrumb shortcode. |
| 187 |
* [wpseo_breadcrumb] |
| 188 |
* |
| 189 |
* @deprecated 14.0 |
| 190 |
* @codeCoverageIgnore |
| 191 |
* |
| 192 |
* @return string |
| 193 |
*/ |
| 194 |
function wpseo_shortcode_yoast_breadcrumb() { |
| 195 |
_deprecated_function( __FUNCTION__, 'WPSEO 14.0' ); |
| 196 |
|
| 197 |
return ''; |
| 198 |
} |
| 199 |
|
| 200 |
if ( ! function_exists( 'ctype_digit' ) ) { |
| 201 |
/** |
| 202 |
* Emulate PHP native ctype_digit() function for when the ctype extension would be disabled *sigh*. |
| 203 |
* Only emulates the behaviour for when the input is a string, does not handle integer input as ascii value. |
| 204 |
* |
| 205 |
* @param string $text String input to validate. |
| 206 |
* |
| 207 |
* @return bool |
| 208 |
*/ |
| 209 |
function ctype_digit( $text ) { |
| 210 |
$return = false; |
| 211 |
if ( ( is_string( $text ) && $text !== '' ) && preg_match( '`^\d+$`', $text ) === 1 ) { |
| 212 |
$return = true; |
| 213 |
} |
| 214 |
|
| 215 |
return $return; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Makes sure the taxonomy meta is updated when a taxonomy term is split. |
| 221 |
* |
| 222 |
* @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. |
| 223 |
* |
| 224 |
* @param string $old_term_id Old term id of the taxonomy term that was splitted. |
| 225 |
* @param string $new_term_id New term id of the taxonomy term that was splitted. |
| 226 |
* @param string $term_taxonomy_id Term taxonomy id for the taxonomy that was affected. |
| 227 |
* @param string $taxonomy The taxonomy that the taxonomy term was splitted for. |
| 228 |
*/ |
| 229 |
function wpseo_split_shared_term( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { |
| 230 |
$tax_meta = get_option( 'wpseo_taxonomy_meta', [] ); |
| 231 |
|
| 232 |
if ( ! empty( $tax_meta[ $taxonomy ][ $old_term_id ] ) ) { |
| 233 |
$tax_meta[ $taxonomy ][ $new_term_id ] = $tax_meta[ $taxonomy ][ $old_term_id ]; |
| 234 |
unset( $tax_meta[ $taxonomy ][ $old_term_id ] ); |
| 235 |
update_option( 'wpseo_taxonomy_meta', $tax_meta ); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
add_action( 'split_shared_term', 'wpseo_split_shared_term', 10, 4 ); |
| 240 |
|
| 241 |
/** |
| 242 |
* Get all WPSEO related capabilities. |
| 243 |
* |
| 244 |
* @since 8.3 |
| 245 |
* @return array |
| 246 |
*/ |
| 247 |
function wpseo_get_capabilities() { |
| 248 |
if ( ! did_action( 'wpseo_register_capabilities' ) ) { |
| 249 |
do_action( 'wpseo_register_capabilities' ); |
| 250 |
} |
| 251 |
return WPSEO_Capability_Manager_Factory::get()->get_capabilities(); |
| 252 |
} |
| 253 |
|
| 254 |
if ( ! function_exists( 'wp_get_environment_type' ) ) { |
| 255 |
/** |
| 256 |
* Retrieves the current environment type. |
| 257 |
* |
| 258 |
* The type can be set via the `WP_ENVIRONMENT_TYPE` global system variable, |
| 259 |
* or a constant of the same name. |
| 260 |
* |
| 261 |
* Possible values include 'local', 'development', 'staging', 'production'. |
| 262 |
* If not set, the type defaults to 'production'. |
| 263 |
* |
| 264 |
* Backfill for `wp_get_environment_type()` introduced in WordPress 5.5. Code |
| 265 |
* is adapted to the Yoast PHP coding standards. |
| 266 |
* |
| 267 |
* @return string The current environment type. |
| 268 |
*/ |
| 269 |
function wp_get_environment_type() { |
| 270 |
static $current_env = ''; |
| 271 |
|
| 272 |
if ( $current_env ) { |
| 273 |
return $current_env; |
| 274 |
} |
| 275 |
|
| 276 |
$wp_environments = [ |
| 277 |
'local', |
| 278 |
'development', |
| 279 |
'staging', |
| 280 |
'production', |
| 281 |
]; |
| 282 |
|
| 283 |
// Check if the environment variable has been set, if `getenv` is available on the system. |
| 284 |
if ( function_exists( 'getenv' ) ) { |
| 285 |
$has_env = getenv( 'WP_ENVIRONMENT_TYPES' ); |
| 286 |
if ( $has_env !== false ) { |
| 287 |
$wp_environments = explode( ',', $has_env ); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
// Fetch the environment types from a constant, this overrides the global system variable. |
| 292 |
if ( defined( 'WP_ENVIRONMENT_TYPES' ) ) { |
| 293 |
$wp_environments = WP_ENVIRONMENT_TYPES; |
| 294 |
} |
| 295 |
|
| 296 |
// Check if the environment variable has been set, if `getenv` is available on the system. |
| 297 |
if ( function_exists( 'getenv' ) ) { |
| 298 |
$has_env = getenv( 'WP_ENVIRONMENT_TYPE' ); |
| 299 |
if ( $has_env !== false ) { |
| 300 |
$current_env = $has_env; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
// Fetch the environment from a constant, this overrides the global system variable. |
| 305 |
if ( defined( 'WP_ENVIRONMENT_TYPE' ) ) { |
| 306 |
$current_env = WP_ENVIRONMENT_TYPE; |
| 307 |
} |
| 308 |
|
| 309 |
// Make sure the environment is an allowed one, and not accidentally set to an invalid value. |
| 310 |
if ( ! in_array( $current_env, $wp_environments, true ) ) { |
| 311 |
$current_env = 'production'; |
| 312 |
} |
| 313 |
|
| 314 |
return $current_env; |
| 315 |
} |
| 316 |
} |
| 317 |
|