/** * @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); }
/** * Retrieve the body from the post. * * @param object $post The post object. * * @return string The post content. */ function get_body($post) { // This filter allows plugins to add their content to the content to be analyzed. $post_content = apply_filters('wpseo_pre_analysis_post_content', $post->post_content, $post); // Strip shortcodes, for obvious reasons, if plugins think their content should be in the analysis, they should // hook into the above filter. $post_content = wpseo_strip_shortcode($post_content); if (trim($post_content) == '') { return ''; } $htmdata3 = preg_replace('`<(?:\\x20*script|script).*?(?:/>|/script>)`', '', $post_content); if ($htmdata3 == null) { $htmdata3 = $post_content; } else { unset($post_content); } $htmdata4 = preg_replace('`<!--.*?-->`', '', $htmdata3); if ($htmdata4 == null) { $htmdata4 = $htmdata3; } else { unset($htmdata3); } $htmdata5 = preg_replace('`<(?:\\x20*style|style).*?(?:/>|/style>)`', '', $htmdata4); if ($htmdata5 == null) { $htmdata5 = $htmdata4; } else { unset($htmdata4); } return $htmdata5; }
/** * Retrieve the body from the post. * * @param object $post The post object. * @return string The post content. */ function get_body($post) { // Strip shortcodes, for obvious reasons $origHtml = wpseo_strip_shortcode($post->post_content); if (trim($origHtml) == '') { return ''; } $htmdata2 = preg_replace("/\n|\r/", " ", $origHtml); if ($htmdata2 == null) { $htmdata2 = $origHtml; } else { unset($origHtml); } $htmdata3 = preg_replace("/<( *script|script).*?(\\/>|\\/script>)/", "", $htmdata2); if ($htmdata3 == null) { $htmdata3 = $htmdata2; } else { unset($htmdata2); } $htmdata4 = preg_replace("/<!--.*?-->/", "", $htmdata3); if ($htmdata4 == null) { $htmdata4 = $htmdata3; } else { unset($htmdata3); } $htmdata5 = preg_replace("/<( *style|style).*?(\\/>|\\/style>)/", "", $htmdata4); if ($htmdata5 == null) { $htmdata5 = $htmdata4; } else { unset($htmdata4); } return $htmdata5; }
/** * @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 ); }
/** * Replace `%%variable_placeholders%%` with their real value based on the current requested page/post/cpt/etc * * @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 */ public function replace($string, $args, $omit = array()) { $string = strip_tags($string); // Let's see if we can bail super early. if (strpos($string, '%%') === false) { return wpseo_standardize_whitespace($string); } $args = (array) $args; if (isset($args['post_content']) && !empty($args['post_content'])) { $args['post_content'] = wpseo_strip_shortcode($args['post_content']); } if (isset($args['post_excerpt']) && !empty($args['post_excerpt'])) { $args['post_excerpt'] = wpseo_strip_shortcode($args['post_excerpt']); } $this->args = (object) wp_parse_args($args, $this->defaults); // Clean $omit array if (is_array($omit) && $omit !== array()) { $omit = array_map(array(__CLASS__, 'remove_var_delimiter'), $omit); } $replacements = array(); if (preg_match_all('`%%([^%]+(%%single)?)%%?`iu', $string, $matches)) { $replacements = $this->set_up_replacements($matches, $omit); } /** * Filter: 'wpseo_replacements' - Allow customization of the replacements before they are applied * * @api array $replacements The replacements */ $replacements = apply_filters('wpseo_replacements', $replacements); // Do the actual replacements if (is_array($replacements) && $replacements !== array()) { $string = str_replace(array_keys($replacements), array_values($replacements), $string); } /** * 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> * * @api 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(array(__CLASS__, 'add_var_delimiter'), $remove); $string = str_replace($remove, '', $string); } // 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%%'], '`'); $string = preg_replace('`' . $q_sep . '(?:\\s*' . $q_sep . ')*`u', $replacements['%%sep%%'], $string); } // Remove superfluous whitespace $string = wpseo_standardize_whitespace($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); }