WPSEO_Replace_Vars::replace
Replace %%variable_placeholders%% with their real value based on the current requested page/post/cpt/etc.
Метод класса: WPSEO_Replace_Vars{}
Хуки из метода
Возвращает
Строку.
Использование
$WPSEO_Replace_Vars = new WPSEO_Replace_Vars(); $WPSEO_Replace_Vars->replace( $text, $args, $omit );
- $text(строка) (обязательный)
- The string to replace the variables in.
- $args(массив) (обязательный)
- The object some of the replacement values might come from, could be a post, taxonomy or term.
- $omit(массив)
- Variables that should not be replaced by this function.
По умолчанию:[]
Код WPSEO_Replace_Vars::replace() WPSEO Replace Vars::replace Yoast 27.3
public function replace( $text, $args, $omit = [] ) {
$text = wp_strip_all_tags( $text );
// Let's see if we can bail super early.
if ( strpos( $text, '%%' ) === false ) {
return YoastSEO()->helpers->string->standardize_whitespace( $text );
}
$args = (array) $args;
if ( isset( $args['post_content'] ) && ! empty( $args['post_content'] ) ) {
$args['post_content'] = YoastSEO()->helpers->string->strip_shortcode( $args['post_content'] );
}
if ( isset( $args['post_excerpt'] ) && ! empty( $args['post_excerpt'] ) ) {
$args['post_excerpt'] = YoastSEO()->helpers->string->strip_shortcode( $args['post_excerpt'] );
}
$this->args = (object) wp_parse_args( $args, $this->defaults );
// Clean $omit array.
if ( is_array( $omit ) && $omit !== [] ) {
$omit = array_map( [ self::class, 'remove_var_delimiter' ], $omit );
}
$replacements = [];
if ( preg_match_all( '`%%([^%]+(%%single)?)%%?`iu', $text, $matches ) ) {
$replacements = $this->set_up_replacements( $matches, $omit );
}
/**
* Filter: 'wpseo_replacements' - Allow customization of the replacements before they are applied.
*
* @param array $replacements The replacements.
* @param array $args The object some of the replacement values might come from,
* could be a post, taxonomy or term.
*/
$replacements = apply_filters( 'wpseo_replacements', $replacements, $this->args );
// Do the actual replacements.
if ( is_array( $replacements ) && $replacements !== [] ) {
$text = str_replace(
array_keys( $replacements ),
// Make sure to exclude replacement values that are arrays e.g. coming from a custom field serialized value.
array_filter( array_values( $replacements ), 'is_scalar' ),
$text,
);
}
/**
* Filter: 'wpseo_replacements_final' - Allow overruling of whether or not to remove placeholders
* which didn't yield a replacement.
*
* @example <code>add_filter( 'wpseo_replacements_final', '__return_false' );</code>
*
* @param bool $final
*/
if ( apply_filters( 'wpseo_replacements_final', true ) === true && ( isset( $matches[1] ) && is_array( $matches[1] ) ) ) {
// Remove non-replaced variables.
$remove = array_diff( $matches[1], $omit ); // Make sure the $omit variables do not get removed.
$remove = array_map( [ self::class, 'add_var_delimiter' ], $remove );
$text = str_replace( $remove, '', $text );
}
// Undouble separators which have nothing between them, i.e. where a non-replaced variable was removed.
if ( isset( $replacements['%%sep%%'] ) && ( is_string( $replacements['%%sep%%'] ) && $replacements['%%sep%%'] !== '' ) ) {
$q_sep = preg_quote( $replacements['%%sep%%'], '`' );
$text = preg_replace( '`' . $q_sep . '(?:\s*' . $q_sep . ')*`u', $replacements['%%sep%%'], $text );
}
// Remove superfluous whitespace.
$text = YoastSEO()->helpers->string->standardize_whitespace( $text );
return $text;
}