}
if ( $postType['hierarchical'] ) {
$context[ $postType['name'] . 'Title' ][] = 'parent_title';
}
}
// Taxonomies including from CPT's.
foreach ( aioseo()->helpers->getPublicTaxonomies() as $taxonomy ) {
$context[ $taxonomy['name'] . 'Title' ] = $context['taxonomyTitle'];
$context[ $taxonomy['name'] . 'Description' ] = $context['taxonomyDescription'];
}
return $context;
}
/**
* Replace the tags in the string provided.
*
* @since 4.0.0
*
* @param string $string The string to look for tags in.
* @param int $id The page or post ID.
* @return string The string with tags replaced.
*/
public function replaceTags( $string, $id = 0 ) {
if ( ! $string || ! preg_match( '/#/', $string ) ) {
return $string;
}
foreach ( $this->tags as $tag ) {
if ( 'custom_field' === $tag['id'] || 'tax_name' === $tag['id'] ) {
continue;
}
$tagId = $this->denotationChar . $tag['id'];
// Pattern explained: Exact match of tag, not followed by any additional letter, number or underscore.
// This allows us to have tags like: #post_link and #post_link_alt
// and it will always replace the correct one.
$pattern = "/$tagId(?![a-zA-Z0-9_])/im";
if ( preg_match( $pattern, $string ) ) {
$tagValue = $this->getTagValue( $tag, $id );
$string = preg_replace( $pattern, '%|%' . aioseo()->helpers->escapeRegexReplacement( $tagValue ), $string );
}
}
$string = $this->parseTaxonomyNames( $string, $id );
// Custom fields are parsed separately.
$string = $this->parseCustomFields( $string, $id );
return preg_replace( '/%\|%/im', '', $string );
}
/**
* Get the value of the tag to replace.
*
* @since 4.0.0
*
* @param string $tag The tag to look for.
* @param int $id The post ID.
* @param bool $sampleData Whether or not to fill empty values with sample data.
* @return mixed The value of the tag.
*/
public function getTagValue( $tag, $id, $sampleData = false ) {
$author = new \WP_User();
$post = aioseo()->helpers->getPost( $id );
$postId = null;
$category = null;
if ( $post ) {
$author = new \WP_User( $post->post_author );
$postId = empty( $id ) ? $post->ID : $id;
$category = get_the_category( $postId );
} elseif ( is_author() && is_a( get_queried_object(), 'WP_User' ) ) {
$author = get_queried_object();
}
switch ( $tag['id'] ) {
case 'alt_tag':
return empty( $id )
? ( $sampleData ? __( 'A sample alt tag for your image', 'all-in-one-seo-pack' ) : '' )
: get_post_meta( $id, '_wp_attachment_image_alt', true );
case 'archive_date':
$date = null;
if ( is_year() ) {
$date = get_the_date( 'Y' );
}
if ( is_month() ) {
$date = get_the_date( 'F, Y' );
}
if ( is_day() ) {
$date = get_the_date();
}
if ( $sampleData ) {
$date = $this->formatDateAsI18n( date_i18n( 'U' ) );
}
if ( ! empty( $date ) ) {
return $date;
}
break;
case 'archive_title':
$title = is_post_type_archive() ? post_type_archive_title( '', false ) : get_the_archive_title();
return $sampleData ? __( 'Sample Archive Title', 'all-in-one-seo-pack' ) : wp_strip_all_tags( $title );
case 'author_bio':
$bio = get_the_author_meta( 'description', $author->ID );
return empty( $bio ) && $sampleData ? __( 'Sample author biography', 'all-in-one-seo-pack' ) : $bio;
case 'author_first_name':
$name = $author->first_name;
return empty( $name ) && $sampleData ? wp_get_current_user()->first_name : $author->first_name;
case 'author_last_name':
$name = $author->last_name;
return empty( $name ) && $sampleData ? wp_get_current_user()->last_name : $author->last_name;
case 'author_link':
return '' . esc_html( $author->display_name ) . '';
case 'author_link_alt':
return '' . esc_url( get_author_posts_url( $author->ID ) ) . '';
case 'author_name':
$name = $author->display_name;
return empty( $name ) && $sampleData ? wp_get_current_user()->display_name : $author->display_name;
case 'author_url':
$authorUrl = get_author_posts_url( $author->ID );
return ! empty( $authorUrl ) ? $authorUrl : '';
case 'attachment_caption':
$caption = wp_get_attachment_caption( $postId );
return empty( $caption ) && $sampleData ? __( 'Sample caption for media.', 'all-in-one-seo-pack' ) : $caption;
case 'attachment_description':
$description = ! empty( $post->post_content ) ? $post->post_content : '';
return empty( $description ) && $sampleData ? __( 'Sample description for media.', 'all-in-one-seo-pack' ) : $description;
case 'categories':
if ( ! is_object( $post ) || 'post' !== $post->post_type ) {
return ! is_object( $post ) && $sampleData ? __( 'Sample Category 1, Sample Category 2', 'all-in-one-seo-pack' ) : '';
}
$categories = get_the_terms( $post->ID, 'category' );
$names = [];
if ( ! is_array( $categories ) ) {
return '';
}
foreach ( $categories as $category ) {
$names[] = $category->name;
}
return implode( ', ', $names );
case 'category_link':
return '' . ( $category ? $category[0]->name : '' ) . '';
case 'category_link_alt':
return '' . esc_url( get_category_link( $category ) ) . '';
case 'current_date':
return $this->formatDateAsI18n( date_i18n( 'U' ) );
case 'current_day':
return date_i18n( 'd' );
case 'current_month':
return date_i18n( 'F' );
case 'current_year':
return date_i18n( 'Y' );
case 'custom_field':
return $sampleData ? __( 'Sample Custom Field Value', 'all-in-one-seo-pack' ) : '';
case 'featured_image':
if ( ! has_post_thumbnail( $postId ) ) {
return $sampleData ? __( 'Sample featured image', 'all-in-one-seo-pack' ) : '';
}
$imageId = get_post_thumbnail_id( $postId );
$image = (array) wp_get_attachment_image_src( $imageId, 'full' );
$image = isset( $image[0] ) ? '' : '';
return $sampleData ? __( 'Sample featured image', 'all-in-one-seo-pack' ) : $image;
case 'page_number':
return aioseo()->helpers->getPageNumber();
case 'parent_title':
if ( ! is_object( $post ) || ! $post->post_parent ) {
return ! is_object( $post ) && $sampleData ? __( 'Sample Parent', 'all-in-one-seo-pack' ) : '';
}
$parent = get_post( $post->post_parent );
return $parent ? $parent->post_title : '';
case 'permalink':
return aioseo()->helpers->getUrl();
case 'post_date':
$date = $this->formatDateAsI18n( get_the_date( 'U' ) );
return empty( $date ) && $sampleData ? $this->formatDateAsI18n( date_i18n( 'U' ) ) : $date;
case 'post_day':
$day = get_the_date( 'd', $post );
return empty( $day ) && $sampleData ? date_i18n( 'd' ) : $day;
case 'post_excerpt_only':
return empty( $postId ) ? ( $sampleData ? __( 'Sample excerpt from a page/post.', 'all-in-one-seo-pack' ) : '' ) : $post->post_excerpt;
case 'post_excerpt':
if ( empty( $postId ) ) {
return $sampleData ? __( 'Sample excerpt from a page/post.', 'all-in-one-seo-pack' ) : '';
}
if ( $post->post_excerpt ) {
return $post->post_excerpt;
}
// Fall through if the post doesn't have an excerpt set. In that case getDescriptionFromContent() will generate it for us.
case 'post_content':
return empty( $postId ) ? ( $sampleData ? __( 'An example of content from your page/post.', 'all-in-one-seo-pack' ) : '' ) : aioseo()->helpers->getDescriptionFromContent( $post );
case 'post_link':
return '' . esc_html( get_the_title( $post ) ) . '';
case 'post_link_alt':
return '' . esc_url( get_permalink( $post ) ) . '';
case 'post_month':
$month = get_the_date( 'F', $post );
return empty( $month ) && $sampleData ? date_i18n( 'F' ) : $month;
case 'post_title':
$title = esc_html( get_the_title( $post ) );
return empty( $title ) && $sampleData ? __( 'Sample Post', 'all-in-one-seo-pack' ) : $title;
case 'post_year':
$year = get_the_date( 'Y', $post );
return empty( $year ) && $sampleData ? date_i18n( 'Y' ) : $year;
case 'search_term':
$search = get_search_query();
return empty( $search ) && $sampleData ? __( 'Example search string', 'all-in-one-seo-pack' ) : esc_attr( stripslashes( $search ) );
case 'separator_sa':
return aioseo()->helpers->decodeHtmlEntities( aioseo()->options->searchAppearance->global->separator );
case 'site_link':
case 'blog_link':
return '' . esc_html( get_bloginfo( 'name' ) ) . '';
case 'site_link_alt':
return '' . esc_url( get_bloginfo( 'url' ) ) . '';
case 'tag':
return single_term_title( '', false );
case 'tax_name':
return $sampleData ? __( 'Sample Taxonomy Name Value', 'all-in-one-seo-pack' ) : '';
case 'tax_parent_name':
$termObject = get_term( $id );
$parentTermObject = ! empty( $termObject->parent ) ? get_term( $termObject->parent ) : '';
$name = is_a( $parentTermObject, 'WP_Term' ) && ! empty( $parentTermObject->name ) ? $parentTermObject->name : '';
return $sampleData ? __( 'Sample Parent Term Name', 'all-in-one-seo-pack' ) : $name;
case 'taxonomy_description':
$description = term_description();
return empty( $description ) && $sampleData ? __( 'Sample taxonomy description', 'all-in-one-seo-pack' ) : $description;
case 'taxonomy_title':
case 'category':
$title = $this->getTaxonomyTitle( $postId );
return ! $title && $sampleData ? __( 'Sample Taxonomy Title', 'all-in-one-seo-pack' ) : $title;
case 'site_description':
case 'blog_description':
case 'tagline':
return aioseo()->helpers->decodeHtmlEntities( get_bloginfo( 'description' ) );
case 'site_title':
case 'blog_title':
return aioseo()->helpers->decodeHtmlEntities( get_bloginfo( 'name' ) );
default:
return '';
}
}
/**
* Get the category title.
*
* @since 4.0.0
*
* @param integer $postId The post ID if set.
* @return string The category title.
*/
private function getTaxonomyTitle( $postId = null ) {
$isWcActive = aioseo()->helpers->isWooCommerceActive();
$title = '';
if ( $isWcActive && is_product_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
} elseif ( is_author() ) {
$title = get_the_author();
} elseif ( is_tax() ) {
$title = single_term_title( '', false );
} elseif ( is_post_type_archive() ) {
$title = post_type_archive_title( '', false );
} elseif ( is_archive() ) {
$title = get_the_archive_title();
}
if ( $postId ) {
$currentScreen = aioseo()->helpers->getCurrentScreen();
$isProduct = $isWcActive && ( is_product() || 'product' === ( $currentScreen->post_type ?? '' ) );
$post = aioseo()->helpers->getPost( $postId );
$postTaxonomies = get_object_taxonomies( $post, 'objects' );
$postTerms = [];
foreach ( $postTaxonomies as $taxonomySlug => $taxonomy ) {
if ( ! $taxonomy->hierarchical ) {
continue;
}
$taxonomySlug = $isProduct ? 'product_cat' : $taxonomySlug;
$primaryTerm = aioseo()->standalone->primaryTerm->getPrimaryTerm( $postId, $taxonomySlug );
if ( $primaryTerm ) {
$postTerms[] = get_term( $primaryTerm, $taxonomySlug );
break;
}
$postTaxonomyTerms = get_the_terms( $postId, $taxonomySlug );
if ( is_array( $postTaxonomyTerms ) ) {
$postTerms = array_merge( $postTerms, $postTaxonomyTerms );
break;
}
}
$title = $postTerms ? $postTerms[0]->name : '';
}
return wp_strip_all_tags( (string) $title );
}
/**
* Formatted Date
*
* Get formatted date based on WP options.
*
* @since 4.0.0
*
* @param null|int $date Date in UNIX timestamp format. Otherwise, current time.
* @return string Date internationalized.
*/
public function formatDateAsI18n( $date = null ) {
if ( ! $date ) {
$date = time();
}
$format = get_option( 'date_format' );
$formattedDate = date_i18n( $format, $date );
return apply_filters(
'aioseo_format_date',
$formattedDate,
[
$date,
$format
]
);
}
/**
* Parses custom taxonomy tags by replacing them with the name of the first assigned term of the given taxonomy.
*
* @since 4.0.0
*
* @param string $string The string to parse.
* @return mixed The new title.
*/
private function parseTaxonomyNames( $string, $id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$pattern = '/' . $this->denotationChar . 'tax_name-([a-zA-Z0-9_-]+)/im';
$string = preg_replace_callback( $pattern, [ $this, 'replaceTaxonomyName' ], $string );
$pattern = '/' . $this->denotationChar . 'tax_name(?![a-zA-Z0-9_-])/im';
return preg_replace( $pattern, '', $string );
}
/**
* Adds support for using #custom_field-[custom_field_title] for using
* custom fields / Advanced Custom Fields in titles / descriptions etc.
*
* @since 4.0.0
*
* @param string $string The string to parse customs fields out of.
* @param int $postId The page or post ID.
* @return mixed The new title.
*/
public function parseCustomFields( $string, $postId = 0 ) {
$pattern = '/' . $this->denotationChar . 'custom_field-([a-zA-Z0-9_-]+)/im';
$matches = [];
preg_match_all( $pattern, $string, $matches, PREG_SET_ORDER );
$string = $this->replaceCustomField( $string, $matches, $postId );
$pattern = '/' . $this->denotationChar . 'custom_field(?![a-zA-Z0-9_-])/im';
return preg_replace( $pattern, '', $string );
}
/**
* Add context to our internal context.
*
* @since 4.0.0
*
* @param array $context A context array to append.
* @return void
*/
public function addContext( $context ) {
$this->context = array_merge( $this->context, $context );
}
/**
* Add tags to our internal tags.
*
* @since 4.0.0
*
* @param array $tags A tags array to append.
* @return void
*/
public function addTags( $tags ) {
$this->tags = array_merge( $this->tags, $tags );
}
/**
* Replaces a taxonomy name tag with its respective value.
*
* @since 4.0.0
*
* @param array $matches The matches.
* @return string The replaced matches.
*/
private function replaceTaxonomyName( $matches ) {
$termName = '';
$post = aioseo()->helpers->getPost();
if ( ! empty( $matches[1] ) && $post ) {
$taxonomy = get_taxonomy( $matches[1] );
if ( ! $taxonomy ) {
return '';
}
$term = aioseo()->standalone->primaryTerm->getPrimaryTerm( $post->ID, $taxonomy->name );
if ( ! $term ) {
$terms = get_the_terms( $post->ID, $taxonomy->name );
if ( ! $terms || is_wp_error( $terms ) ) {
return '';
}
$term = array_shift( $terms );
}
$termName = $term->name;
}
return '%|%' . $termName;
}
/**
* (ACF) Custom Field Replace.
*
* @since 4.0.0
*
* @param string $string The string to parse customs fields out of.
* @param array $matches Array of matched values.
* @param int $postId The page or post ID.
* @return bool|string New title/text.
*/
private function replaceCustomField( $string, $matches, $postId ) {
if ( empty( $matches ) ) {
return $string;
}
foreach ( $matches as $match ) {
$str = '';
if ( ! empty( $match[1] ) ) {
if ( function_exists( 'get_field' ) ) {
$str = get_field( $match[1], get_queried_object() ?? $postId );
}
if ( empty( $str ) ) {
global $post;
if ( ! empty( $post ) ) {
$str = get_post_meta( $post->ID, $match[1], true );
}
}
} else {
$str = $match[0];
}
$str = wp_strip_all_tags( $str );
$string = str_replace( $match[0], '%|%' . $str, $string );
}
return $string;
}
/**
* Get the default tags for the current post.
*
* @since 4.0.0
*
* @param integer $postId The Post ID.
* @return array An array of tags.
*/
public function getDefaultPostTags( $postId ) {
$post = get_post( $postId );
$title = aioseo()->meta->title->getTitle( $post, true );
$description = aioseo()->meta->description->getDescription( $post, true );
return [
'title' => empty( $title ) ? '' : $title,
'description' => empty( $description ) ? '' : $description
];
}
}