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 ]; } }