/**
 * @param string $string the string to replace the variables in.
 * @param array  $args   the object some of the replacement values might come from, could be a post, taxonomy or term.
 * @param array  $omit   variables that should not be replaced by this function.
 * @return string
 */
function wpseo_replace_vars( $string, $args, $omit = array() ) {

	$args = (array) $args;

	$string = strip_tags( $string );

	// Let's see if we can bail super early.
	if ( strpos( $string, '%%' ) === false ) {
		return trim( preg_replace( '`\s+`u', ' ', $string ) );
	}

	global $sep;
	if ( ! isset( $sep ) || empty( $sep ) ) {
		$sep = '-';
	}

	$simple_replacements = array(
		'%%sep%%'          => $sep,
		'%%sitename%%'     => get_bloginfo( 'name' ),
		'%%sitedesc%%'     => get_bloginfo( 'description' ),
		'%%currenttime%%'  => date( get_option( 'time_format' ) ),
		'%%currentdate%%'  => date( get_option( 'date_format' ) ),
		'%%currentday%%'   => date( 'j' ),
		'%%currentmonth%%' => date( 'F' ),
		'%%currentyear%%'  => date( 'Y' ),
	);

	foreach ( $simple_replacements as $var => $repl ) {
		$string = str_replace( $var, $repl, $string );
	}

	// Let's see if we can bail early.
	if ( strpos( $string, '%%' ) === false ) {
		return trim( preg_replace( '`\s+`u', ' ', $string ) );
	}

	global $wp_query;

	$defaults = array(
		'ID'            => '',
		'name'          => '',
		'post_author'   => '',
		'post_content'  => '',
		'post_date'     => '',
		'post_excerpt'  => '',
		'post_modified' => '',
		'post_title'    => '',
		'taxonomy'      => '',
		'term_id'       => '',
		'term404'		=> '',
	);

	if ( isset( $args['post_content'] ) ) {
		$args['post_content'] = wpseo_strip_shortcode( $args['post_content'] );
	}
	if ( isset( $args['post_excerpt'] ) ) {
		$args['post_excerpt'] = wpseo_strip_shortcode( $args['post_excerpt'] );
	}

	$r = (object) wp_parse_args( $args, $defaults );

	$max_num_pages = 1;
	if ( ! is_singular() ) {
		$pagenum = get_query_var( 'paged' );
		if ( $pagenum === 0 ) {
			$pagenum = 1;
		}

		if ( isset( $wp_query->max_num_pages ) && $wp_query->max_num_pages != '' && $wp_query->max_num_pages != 0 ) {
			$max_num_pages = $wp_query->max_num_pages;
		}
	}
	else {
		global $post;
		$pagenum       = get_query_var( 'page' );
		$max_num_pages = ( isset( $post->post_content ) ) ? substr_count( $post->post_content, '<!--nextpage-->' ) : 1;
		if ( $max_num_pages >= 1 ) {
			$max_num_pages++;
		}
	}

	// Let's do date first as it's a bit more work to get right.
	if ( $r->post_date != '' ) {
		$date = mysql2date( get_option( 'date_format' ), $r->post_date );
	}
	else {
		if ( get_query_var( 'day' ) && get_query_var( 'day' ) != '' ) {
			$date = get_the_date();
		}
		else {
			if ( single_month_title( ' ', false ) && single_month_title( ' ', false ) != '' ) {
				$date = single_month_title( ' ', false );
			}
			elseif ( get_query_var( 'year' ) != '' ) {
				$date = get_query_var( 'year' );
			}
			else {
				$date = '';
			}
		}
	}

	$replacements = array(
		'%%date%%'         => $date,
		'%%searchphrase%%' => esc_html( get_query_var( 's' ) ),
		'%%page%%'         => ( $max_num_pages > 1 && $pagenum > 1 ) ? sprintf( $sep . ' ' . __( 'Page %d of %d', 'wordpress-seo' ), $pagenum, $max_num_pages ) : '',
		'%%pagetotal%%'    => $max_num_pages,
		'%%pagenumber%%'   => $pagenum,
		'%%term404%%'	   => sanitize_text_field( str_replace( '-', ' ', $r->term404 ) ),
	);

	if ( isset( $r->ID ) ) {
		$replacements = array_merge(
			$replacements, array(
				'%%caption%%'      => $r->post_excerpt,
				'%%category%%'     => wpseo_get_terms( $r->ID, 'category' ),
				'%%excerpt%%'      => ( ! empty( $r->post_excerpt ) ) ? strip_tags( $r->post_excerpt ) : wp_html_excerpt( strip_shortcodes( $r->post_content ),155 ),
				'%%excerpt_only%%' => strip_tags( $r->post_excerpt ),
				'%%focuskw%%'      => WPSEO_Meta::get_value( 'focuskw', $r->ID ),
				'%%id%%'           => $r->ID,
				'%%modified%%'     => mysql2date( get_option( 'date_format' ), $r->post_modified ),
				'%%name%%'         => get_the_author_meta( 'display_name', ! empty( $r->post_author ) ? $r->post_author : get_query_var( 'author' ) ),
				'%%tag%%'          => wpseo_get_terms( $r->ID, 'post_tag' ),
				'%%title%%'        => stripslashes( $r->post_title ),
				'%%userid%%'       => ! empty( $r->post_author ) ? $r->post_author : get_query_var( 'author' ),
			)
		);
	}

	if ( ! empty( $r->taxonomy ) ) {
		$replacements = array_merge(
			$replacements, array(
				'%%category_description%%' => trim( strip_tags( get_term_field( 'description', $r->term_id, $r->taxonomy ) ) ),
				'%%tag_description%%'      => trim( strip_tags( get_term_field( 'description', $r->term_id, $r->taxonomy ) ) ),
				'%%term_description%%'     => trim( strip_tags( get_term_field( 'description', $r->term_id, $r->taxonomy ) ) ),
				'%%term_title%%'           => $r->name,
			)
		);
	}

	/**
	* Filter: 'wpseo_replacements' - Allow customization of the replacements before they are applied
	*
	* @api array $replacements The replacements
	*/
	$replacements = apply_filters( 'wpseo_replacements', $replacements );

	foreach ( $replacements as $var => $repl ) {
		if ( ! in_array( $var, $omit ) ) {
			$string = str_replace( $var, $repl, $string );
		}
	}

	if ( strpos( $string, '%%' ) === false ) {
		$string = preg_replace( '`\s+`u', ' ', $string );
		return trim( $string );
	}

	if ( isset( $wp_query->query_vars['post_type'] ) && preg_match_all( '`%%pt_([^%]+)%%`u', $string, $matches, PREG_SET_ORDER ) ) {
		$post_type = $wp_query->query_vars['post_type'];

		if ( is_array( $post_type ) ) {
			$post_type = reset( $post_type );
		}

		$pt        = get_post_type_object( $post_type );
		$pt_plural = $pt_singular = $pt->name;
		if ( isset( $pt->labels->singular_name ) ) {
			$pt_singular = $pt->labels->singular_name;
		}
		if ( isset( $pt->labels->name ) ) {
			$pt_plural = $pt->labels->name;
		}
		$string = str_replace( '%%pt_single%%', $pt_singular, $string );
		$string = str_replace( '%%pt_plural%%', $pt_plural, $string );
	}

	if ( ( is_singular() || is_admin() ) && preg_match_all( '`%%cf_([^%]+)%%`u', $string, $matches, PREG_SET_ORDER ) ) {
		global $post;
		if( is_object( $post ) && isset( $post->ID ) ) {
			foreach ( $matches as $match ) {
				$string = str_replace( $match[0], get_post_meta( $post->ID, $match[1], true ), $string );
			}
		}
	}

	if ( ( is_singular() || is_admin() ) && false !== strpos( $string, '%%parent_title%%' ) ) {
		global $post;
		if ( isset( $post->post_parent ) && 0 != $post->post_parent ) {
			$parent_title = get_the_title( $post->post_parent );
			$string = str_replace( '%%parent_title%%', $parent_title, $string );
		}
	}

	if ( preg_match_all( '`%%ct_desc_([^%]+)?%%`u', $string, $matches, PREG_SET_ORDER ) ) {
		global $post;
		foreach ( $matches as $match ) {
			$terms = get_the_terms( $post->ID, $match[1] );
			if ( is_array( $terms ) && $terms !== array() ) {
				$term   = current( $terms );
				$string = str_replace( $match[0], get_term_field( 'description', $term->term_id, $match[1] ), $string );
			}
			else {
				// Make sure that the variable is removed ?
				$string = str_replace( $match[0], '', $string );

				/* Check for WP_Error object (=invalid taxonomy entered) and if it's an error,
				 notify in admin dashboard */
				if ( is_wp_error( $terms ) && is_admin() ) {
					add_action( 'admin_notices', 'wpseo_invalid_custom_taxonomy' );
				}
			}
		}
	}

	if ( preg_match_all( '`%%ct_([^%]+)%%(single%%)?`u', $string, $matches, PREG_SET_ORDER ) ) {
		foreach ( $matches as $match ) {
			$single = false;
			if ( isset( $match[2] ) && $match[2] == 'single%%' ) {
				$single = true;
			}
			$ct_terms = wpseo_get_terms( $r->ID, $match[1], $single );

			$string = str_replace( $match[0], $ct_terms, $string );
		}
	}

	$string = preg_replace( '`\s+`u', ' ', $string );
	return trim( $string );
}
示例#2
0
/**
 * @param string $string the string to replace the variables in.
 * @param array  $args   the object some of the replacement values might come from, could be a post, taxonomy or term.
 * @param array  $omit   variables that should not be replaced by this function.
 * @return string
 */
function wpseo_replace_vars($string, $args, $omit = array())
{
    $args = (array) $args;
    $string = strip_tags($string);
    // Let's see if we can bail super early.
    if (strpos($string, '%%') === false) {
        return trim(preg_replace('/\\s+/u', ' ', $string));
    }
    global $sep;
    if (!isset($sep) || empty($sep)) {
        $sep = '-';
    }
    $simple_replacements = array('%%sep%%' => $sep, '%%sitename%%' => get_bloginfo('name'), '%%sitedesc%%' => get_bloginfo('description'), '%%currenttime%%' => date('H:i'), '%%currentdate%%' => date('M jS Y'), '%%currentmonth%%' => date('F'), '%%currentyear%%' => date('Y'));
    foreach ($simple_replacements as $var => $repl) {
        $string = str_replace($var, $repl, $string);
    }
    // Let's see if we can bail early.
    if (strpos($string, '%%') === false) {
        return trim(preg_replace('/\\s+/u', ' ', $string));
    }
    global $wp_query;
    $defaults = array('ID' => '', 'name' => '', 'post_author' => '', 'post_content' => '', 'post_date' => '', 'post_excerpt' => '', 'post_modified' => '', 'post_title' => '', 'taxonomy' => '', 'term_id' => '');
    if (isset($args['post_content'])) {
        $args['post_content'] = wpseo_strip_shortcode($args['post_content']);
    }
    if (isset($args['post_excerpt'])) {
        $args['post_excerpt'] = wpseo_strip_shortcode($args['post_excerpt']);
    }
    $r = (object) wp_parse_args($args, $defaults);
    $max_num_pages = 1;
    if (!is_single()) {
        $pagenum = get_query_var('paged');
        if ($pagenum === 0) {
            $pagenum = 1;
        }
        if (isset($wp_query->max_num_pages) && $wp_query->max_num_pages != '' && $wp_query->max_num_pages != 0) {
            $max_num_pages = $wp_query->max_num_pages;
        }
    } else {
        global $post;
        $pagenum = get_query_var('page');
        $max_num_pages = isset($post->post_content) ? substr_count($post->post_content, '<!--nextpage-->') : 1;
        if ($max_num_pages >= 1) {
            $max_num_pages++;
        }
    }
    // Let's do date first as it's a bit more work to get right.
    if ($r->post_date != '') {
        $date = mysql2date(get_option('date_format'), $r->post_date);
    } else {
        if (get_query_var('day') && get_query_var('day') != '') {
            $date = get_the_date();
        } else {
            if (single_month_title(' ', false) && single_month_title(' ', false) != '') {
                $date = single_month_title(' ', false);
            } else {
                if (get_query_var('year') != '') {
                    $date = get_query_var('year');
                } else {
                    $date = '';
                }
            }
        }
    }
    $replacements = array('%%date%%' => $date, '%%searchphrase%%' => esc_html(get_query_var('s')), '%%page%%' => $max_num_pages > 1 && $pagenum > 1 ? sprintf($sep . ' ' . __('Page %d of %d', 'wordpress-seo'), $pagenum, $max_num_pages) : '', '%%pagetotal%%' => $max_num_pages, '%%pagenumber%%' => $pagenum);
    if (isset($r->ID)) {
        $replacements = array_merge($replacements, array('%%caption%%' => $r->post_excerpt, '%%category%%' => wpseo_get_terms($r->ID, 'category'), '%%excerpt%%' => !empty($r->post_excerpt) ? strip_tags($r->post_excerpt) : utf8_encode(substr(strip_shortcodes(strip_tags(utf8_decode($r->post_content))), 0, 155)), '%%excerpt_only%%' => strip_tags($r->post_excerpt), '%%focuskw%%' => wpseo_get_value('focuskw', $r->ID), '%%id%%' => $r->ID, '%%modified%%' => mysql2date(get_option('date_format'), $r->post_modified), '%%name%%' => get_the_author_meta('display_name', !empty($r->post_author) ? $r->post_author : get_query_var('author')), '%%tag%%' => wpseo_get_terms($r->ID, 'post_tag'), '%%title%%' => stripslashes($r->post_title), '%%userid%%' => !empty($r->post_author) ? $r->post_author : get_query_var('author')));
    }
    if (!empty($r->taxonomy)) {
        $replacements = array_merge($replacements, array('%%category_description%%' => trim(strip_tags(get_term_field('description', $r->term_id, $r->taxonomy))), '%%tag_description%%' => trim(strip_tags(get_term_field('description', $r->term_id, $r->taxonomy))), '%%term_description%%' => trim(strip_tags(get_term_field('description', $r->term_id, $r->taxonomy))), '%%term_title%%' => $r->name));
    }
    foreach ($replacements as $var => $repl) {
        if (!in_array($var, $omit)) {
            $string = str_replace($var, $repl, $string);
        }
    }
    if (strpos($string, '%%') === false) {
        $string = preg_replace('/\\s+/u', ' ', $string);
        return trim($string);
    }
    if (isset($wp_query->query_vars['post_type']) && preg_match_all('/%%pt_([^%]+)%%/u', $string, $matches, PREG_SET_ORDER)) {
        $pt = get_post_type_object($wp_query->query_vars['post_type']);
        $pt_plural = $pt_singular = $pt->name;
        if (isset($pt->labels->singular_name)) {
            $pt_singular = $pt->labels->singular_name;
        }
        if (isset($pt->labels->name)) {
            $pt_plural = $pt->labels->name;
        }
        $string = str_replace('%%pt_single%%', $pt_singular, $string);
        $string = str_replace('%%pt_plural%%', $pt_plural, $string);
    }
    if (preg_match_all('/%%cf_([^%]+)%%/u', $string, $matches, PREG_SET_ORDER)) {
        global $post;
        foreach ($matches as $match) {
            $string = str_replace($match[0], get_post_meta($post->ID, $match[1], true), $string);
        }
    }
    if (preg_match_all('/%%ct_desc_([^%]+)?%%/u', $string, $matches, PREG_SET_ORDER)) {
        global $post;
        foreach ($matches as $match) {
            $terms = get_the_terms($post->ID, $match[1]);
            $string = str_replace($match[0], get_term_field('description', $terms[0]->term_id, $match[1]), $string);
        }
    }
    if (preg_match_all('/%%ct_([^%]+)%%(single%%)?/u', $string, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $match) {
            $single = false;
            if (isset($match[2]) && $match[2] == 'single%%') {
                $single = true;
            }
            $ct_terms = wpseo_get_terms($r->ID, $match[1], $single);
            $string = str_replace($match[0], $ct_terms, $string);
        }
    }
    $string = preg_replace('/\\s+/u', ' ', $string);
    return trim($string);
}
function wpseo_replace_vars($string, $args, $omit = array())
{
    $args = (array) $args;
    $string = strip_tags($string);
    // Let's see if we can bail super early.
    if (strpos($string, '%%') === false) {
        return trim(preg_replace('/\\s+/', ' ', $string));
    }
    $simple_replacements = array('%%sitename%%' => get_bloginfo('name'), '%%sitedesc%%' => get_bloginfo('description'), '%%currenttime%%' => date('H:i'), '%%currentdate%%' => date('M jS Y'), '%%currentmonth%%' => date('F'), '%%currentyear%%' => date('Y'));
    foreach ($simple_replacements as $var => $repl) {
        $string = str_replace($var, $repl, $string);
    }
    // Let's see if we can bail early.
    if (strpos($string, '%%') === false) {
        return trim(preg_replace('/\\s+/', ' ', $string));
    }
    global $wp_query;
    $defaults = array('ID' => '', 'name' => '', 'post_author' => '', 'post_content' => '', 'post_date' => '', 'post_content' => '', 'post_excerpt' => '', 'post_modified' => '', 'post_title' => '', 'taxonomy' => '', 'term_id' => '');
    $pagenum = get_query_var('paged');
    if ($pagenum === 0) {
        if ($wp_query->max_num_pages > 1) {
            $pagenum = 1;
        } else {
            $pagenum = '';
        }
    }
    if (isset($args['post_content'])) {
        $args['post_content'] = wpseo_strip_shortcode($args['post_content']);
    }
    if (isset($args['post_excerpt'])) {
        $args['post_excerpt'] = wpseo_strip_shortcode($args['post_excerpt']);
    }
    $r = (object) wp_parse_args($args, $defaults);
    // Only global $post on single's, otherwise some expressions will return wrong results.
    if (is_singular() || is_front_page() && 'posts' != get_option('show_on_front')) {
        global $post;
    }
    // Let's do date first as it's a bit more work to get right.
    if ($r->post_date != '') {
        $date = mysql2date(get_option('date_format'), $r->post_date);
    } else {
        if (get_query_var('day') && get_query_var('day') != '') {
            $date = get_the_date();
        } else {
            if (single_month_title(' ', false) && single_month_title(' ', false) != '') {
                $date = single_month_title(' ', false);
            } else {
                if (get_query_var('year') != '') {
                    $date = get_query_var('year');
                } else {
                    $date = '';
                }
            }
        }
    }
    $replacements = array('%%date%%' => $date, '%%title%%' => stripslashes($r->post_title), '%%excerpt%%' => !empty($r->post_excerpt) ? strip_tags($r->post_excerpt) : substr(strip_shortcodes(strip_tags($r->post_content)), 0, 155), '%%excerpt_only%%' => strip_tags($r->post_excerpt), '%%category%%' => wpseo_get_terms($r->ID, 'category'), '%%category_description%%' => !empty($r->taxonomy) ? trim(strip_tags(get_term_field('description', $r->term_id, $r->taxonomy))) : '', '%%tag_description%%' => !empty($r->taxonomy) ? trim(strip_tags(get_term_field('description', $r->term_id, $r->taxonomy))) : '', '%%term_description%%' => !empty($r->taxonomy) ? trim(strip_tags(get_term_field('description', $r->term_id, $r->taxonomy))) : '', '%%term_title%%' => $r->name, '%%focuskw%%' => wpseo_get_value('focuskw', $r->ID), '%%tag%%' => wpseo_get_terms($r->ID, 'post_tag'), '%%modified%%' => mysql2date(get_option('date_format'), $r->post_modified), '%%id%%' => $r->ID, '%%name%%' => get_the_author_meta('display_name', !empty($r->post_author) ? $r->post_author : get_query_var('author')), '%%userid%%' => !empty($r->post_author) ? $r->post_author : get_query_var('author'), '%%searchphrase%%' => esc_html(get_query_var('s')), '%%page%%' => get_query_var('paged') != 0 ? 'Page ' . get_query_var('paged') . ' of ' . $wp_query->max_num_pages : '', '%%pagetotal%%' => $wp_query->max_num_pages > 1 ? $wp_query->max_num_pages : '', '%%pagenumber%%' => $pagenum, '%%caption%%' => $r->post_excerpt);
    foreach ($replacements as $var => $repl) {
        if (!in_array($var, $omit)) {
            $string = str_replace($var, $repl, $string);
        }
    }
    if (strpos($string, '%%') === false) {
        $string = preg_replace('/\\s\\s+/', ' ', $string);
        return trim($string);
    }
    if (preg_match_all('/%%cf_([^%]+)%%/', $string, $matches, PREG_SET_ORDER)) {
        global $post;
        foreach ($matches as $match) {
            $string = str_replace($match[0], get_post_meta($post->ID, $match[1], true), $string);
        }
    }
    $string = preg_replace('/\\s\\s+/', ' ', $string);
    return trim($string);
}