prefix . 'aieo_seo_head_cache';
}
public static function cache_key( $object_type, $object_id, $lang ) {
return 'aieo_seo_head:' . $object_type . ':' . (int) $object_id . ':' . $lang;
}
public static function current_lang() {
if ( defined( 'ICL_LANGUAGE_CODE' ) && ICL_LANGUAGE_CODE ) {
return (string) ICL_LANGUAGE_CODE;
}
if ( function_exists( 'pll_current_language' ) ) {
$pll = pll_current_language();
if ( $pll ) {
return (string) $pll;
}
}
return '';
}
public static function object_lang( $object_type, $object_id ) {
$object_type = (string) $object_type;
if ( '' === $object_type || ! has_filter( 'wpml_element_language_code' ) || ! post_type_exists( $object_type ) ) {
return '';
}
$lang = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => (int) $object_id, 'element_type' => $object_type ) );
return is_string( $lang ) ? $lang : '';
}
public static function get( $object_type, $object_id, $lang = null ) {
$object_type = (string) $object_type;
$object_id = (int) $object_id;
if ( '' === $object_type || $object_id <= 0 ) {
return null;
}
if ( null === $lang ) {
$lang = self::object_lang( $object_type, $object_id );
if ( '' === $lang ) {
$lang = self::current_lang();
}
}
$key = self::cache_key( $object_type, $object_id, $lang );
$cached = wp_cache_get( $key, self::CACHE_GROUP );
if ( false !== $cached ) {
return is_array( $cached ) ? self::unexpired( $cached, $object_type, $object_id ) : null;
}
global $wpdb;
$t = self::table();
$quiet = $wpdb->suppress_errors( true );
$row = $wpdb->get_row(
$wpdb->prepare(
self::in_unified()
? "SELECT title, canonical, robots, head_html, prices_until, updated FROM `{$t}` WHERE object_type = %s AND object_id = %d AND lang = %s"
: "SELECT title, canonical, robots, head_html, prices_until FROM `{$t}` WHERE object_type = %s AND object_id = %d AND lang = %s",
$object_type,
$object_id,
$lang
),
ARRAY_A
);
if ( null === $row && '' !== (string) $wpdb->last_error ) {
$row = $wpdb->get_row( $wpdb->prepare( "SELECT title, canonical, robots, head_html FROM `{$t}` WHERE object_type = %s AND object_id = %d AND lang = %s", $object_type, $object_id, $lang ), ARRAY_A );
}
$wpdb->suppress_errors( $quiet );
wp_cache_set( $key, is_array( $row ) ? $row : '', self::CACHE_GROUP, self::CACHE_TTL );
return is_array( $row ) ? self::unexpired( $row, $object_type, $object_id ) : null;
}
private static function in_unified() {
return class_exists( 'AIEO_DMM_Price_Filter' ) && AIEO_DMM_Price_Filter::resolves();
}
private static $queued = array();
private static function unexpired( array $row, $object_type, $object_id ) {
$until = isset( $row['prices_until'] ) ? (string) $row['prices_until'] : '';
$expired = '' !== $until && $until <= gmdate( 'Y-m-d H:i:s' );
if ( ! $expired && 'product' === $object_type && array_key_exists( 'updated', $row ) && self::in_unified() ) {
$entered = class_exists( 'AIEO_DMM_Offer_Facts' ) && method_exists( 'AIEO_DMM_Offer_Facts', 'switches' ) ? (string) AIEO_DMM_Offer_Facts::switches()['to_unified'] : '';
$expired = '' !== $entered && (string) $row['updated'] < $entered;
}
if ( ! $expired ) {
return $row;
}
$row['prices_expired'] = true;
$queue = $object_type . ':' . (int) $object_id;
if ( ! isset( self::$queued[ $queue ] ) && class_exists( 'AIEO_SEO_Head_Builder', false ) && method_exists( 'AIEO_SEO_Head_Builder', 'expired' ) ) {
self::$queued[ $queue ] = true;
AIEO_SEO_Head_Builder::expired( $object_type, (int) $object_id );
}
return $row;
}
public static function register() {
static $done = false;
if ( $done ) {
return;
}
$done = true;
if ( is_admin() ) {
return;
}
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return;
}
add_action( 'wp', array( __CLASS__, 'on_wp' ) );
}
public static function on_wp() {
if ( is_admin() || is_feed() || is_embed() ) {
return;
}
if ( self::competitor_active() ) {
return;
}
if ( ! self::engine_enabled() ) {
self::queue_social();
return;
}
add_filter( 'rooster/seo/provider_active', '__return_true' );
add_action( 'wp_head', array( __CLASS__, 'emit_fallback_description' ), 1 );
if ( ( is_date() && self::archive_noindex( 'noindex_date' ) )
|| ( is_author() && self::archive_noindex( 'noindex_author' ) ) ) {
add_filter( 'wp_robots', array( __CLASS__, 'filter_robots_noindex_follow' ), 99 );
}
$resolved = self::resolve_current_object();
if ( ! $resolved ) {
self::maybe_paginated_robots_fallback();
self::queue_social();
return;
}
list( $object_type, $object_id ) = $resolved;
$row = self::get( $object_type, $object_id );
if ( ! $row ) {
self::maybe_paginated_robots_fallback();
self::queue_social();
return;
}
self::$current = $row;
if ( isset( $row['title'] ) && '' !== (string) $row['title'] ) {
add_filter( 'pre_get_document_title', array( __CLASS__, 'filter_title' ), 99 );
add_filter( 'document_title_parts', array( __CLASS__, 'filter_title_parts' ), 99 );
if ( ! current_theme_supports( 'title-tag' ) ) {
add_filter( 'wp_title', array( __CLASS__, 'filter_wp_title' ), 99 );
}
}
if ( isset( $row['robots'] ) && '' !== (string) $row['robots'] ) {
add_filter( 'wp_robots', array( __CLASS__, 'filter_robots' ), 99 );
} else {
self::maybe_paginated_robots_fallback();
}
if ( isset( $row['canonical'] ) && '' !== (string) $row['canonical'] ) {
remove_action( 'wp_head', 'rel_canonical' );
}
if ( isset( $row['head_html'] ) && '' !== (string) $row['head_html'] ) {
add_action( 'wp_head', array( __CLASS__, 'emit_head' ), 1 );
} else {
self::queue_social();
}
add_filter( 'woocommerce_structured_data_type_for_page', array( __CLASS__, 'filter_wc_structured_data' ), 99 );
}
private static function competitor_active() {
return defined( 'AIOSEO_VERSION' ) || class_exists( 'AIOSEO\\Plugin\\Common\\Main', false )
|| defined( 'WPSEO_VERSION' ) || class_exists( 'WPSEO_Options', false )
|| defined( 'RANK_MATH_VERSION' ) || class_exists( 'RankMath', false )
|| defined( 'SEOPRESS_VERSION' ) || defined( 'THE_SEO_FRAMEWORK_VERSION' )
|| defined( 'SLIM_SEO_VER' ) || defined( 'SQUIRRLY_SEO_VERSION' );
}
private static function engine_enabled() {
$opt = get_option( 'aieo_dmm_settings' );
return is_array( $opt ) && isset( $opt['seo']['enabled'] ) && ! empty( $opt['seo']['enabled'] );
}
private static function resolve_current_object() {
if ( is_singular() ) {
$id = (int) get_queried_object_id();
if ( $id <= 0 ) {
return null;
}
return array( (string) get_post_type( $id ), $id );
}
if ( is_category() || is_tag() || is_tax() ) {
$term = get_queried_object();
if ( $term instanceof WP_Term ) {
return array( (string) $term->taxonomy, (int) $term->term_id );
}
}
return null;
}
public static function emit_fallback_description() {
$html = is_array( self::$current ) ? (string) ( self::$current['head_html'] ?? '' ) : '';
if ( '' !== $html && preg_match( '/]*name=["\']description["\']/i', $html ) ) {
return;
}
$text = self::fallback_description();
if ( '' !== $text ) {
echo '' . "\n";
}
}
public static function fallback_description() {
$text = '';
$site = (string) get_bloginfo( 'name', 'display' );
if ( is_singular() ) {
$post = get_queried_object();
if ( $post instanceof WP_Post ) {
$text = '' !== $post->post_excerpt ? $post->post_excerpt : wp_trim_words( wp_strip_all_tags( strip_shortcodes( excerpt_remove_blocks( (string) $post->post_content ) ) ), 30, 'ā¦' );
if ( '' === trim( $text ) && is_front_page() ) {
$text = (string) get_bloginfo( 'description', 'display' );
}
}
} elseif ( is_category() || is_tag() || is_tax() ) {
$term = get_queried_object();
if ( $term instanceof WP_Term ) {
$text = '' !== $term->description ? $term->description : sprintf( __( '%1$s at %2$s', 'ai-eshop-optimizer' ), $term->name, $site );
}
} elseif ( is_post_type_archive() ) {
$type = get_queried_object();
$text = $type instanceof WP_Post_Type && '' !== (string) $type->description ? (string) $type->description : sprintf( __( '%1$s at %2$s', 'ai-eshop-optimizer' ), (string) post_type_archive_title( '', false ), $site );
} elseif ( is_search() ) {
$text = sprintf( __( 'Search results for ā%sā', 'ai-eshop-optimizer' ), get_search_query() );
} elseif ( is_404() ) {
$text = __( 'The page you were looking for is not here. Try a search, or head back to the start.', 'ai-eshop-optimizer' );
} elseif ( is_home() || is_front_page() ) {
$text = (string) get_bloginfo( 'description', 'display' );
}
$text = trim( wp_strip_all_tags( (string) $text ) );
if ( '' === $text ) {
$text = (string) get_bloginfo( 'description', 'display' );
}
if ( '' === trim( $text ) ) {
$text = $site;
}
$text = (string) preg_replace( '/\s+/', ' ', $text );
if ( mb_strlen( $text ) > 160 ) {
$text = rtrim( mb_substr( $text, 0, 157 ) ) . 'ā¦';
}
$text = (string) apply_filters( 'aieo_seo_fallback_description', $text );
return (string) apply_filters( 'rooster/seo/description', $text );
}
public static function filter_title( $title ) {
return ( self::$current && ! empty( self::$current['title'] ) )
? (string) self::$current['title']
: $title;
}
public static function filter_title_parts( $parts ) {
if ( self::$current && ! empty( self::$current['title'] ) ) {
return array( 'title' => (string) self::$current['title'] );
}
return $parts;
}
public static function filter_wp_title( $title ) {
if ( ! self::$current || empty( self::$current['title'] ) ) {
return $title;
}
return esc_html( html_entity_decode( (string) self::$current['title'], ENT_QUOTES, 'UTF-8' ) );
}
public static function filter_robots( $robots ) {
if ( ! self::$current || empty( self::$current['robots'] ) ) {
return $robots;
}
$out = array();
foreach ( explode( ',', (string) self::$current['robots'] ) as $token ) {
$token = trim( $token );
if ( '' === $token ) {
continue;
}
if ( false !== strpos( $token, ':' ) ) {
list( $k, $v ) = array_map( 'trim', explode( ':', $token, 2 ) );
if ( '' !== $k ) {
$out[ $k ] = $v;
}
} else {
$out[ $token ] = true;
}
}
if ( is_paged() ) {
$pag = self::paginated_robots();
if ( $pag['noindex'] ) {
unset( $out['index'] );
$out['noindex'] = true;
}
if ( $pag['nofollow'] ) {
unset( $out['follow'] );
$out['nofollow'] = true;
}
}
return $out;
}
private static function paginated_robots() {
$opt = get_option( 'aieo_dmm_settings' );
$g = ( is_array( $opt ) && isset( $opt['seo']['robots_global'] ) && is_array( $opt['seo']['robots_global'] ) )
? $opt['seo']['robots_global'] : null;
if ( null === $g ) {
return array( 'noindex' => true, 'nofollow' => false );
}
return array(
'noindex' => ! empty( $g['noindex_paginated'] ),
'nofollow' => ! empty( $g['nofollow_paginated'] ),
);
}
private static function archive_noindex( $key ) {
$opt = get_option( 'aieo_dmm_settings' );
return is_array( $opt ) && ! empty( $opt['seo']['robots_global'][ $key ] );
}
private static function maybe_paginated_robots_fallback() {
if ( ! is_paged() ) {
return;
}
$pag = self::paginated_robots();
if ( $pag['noindex'] || $pag['nofollow'] ) {
add_filter( 'wp_robots', array( __CLASS__, 'filter_robots_paginated_fallback' ), 99 );
}
}
public static function filter_robots_paginated_fallback( $robots ) {
if ( ! is_array( $robots ) ) {
$robots = array();
}
$pag = self::paginated_robots();
if ( $pag['noindex'] ) {
unset( $robots['index'] );
$robots['noindex'] = true;
}
if ( $pag['nofollow'] ) {
unset( $robots['follow'] );
$robots['nofollow'] = true;
}
return $robots;
}
public static function filter_robots_noindex_follow( $robots ) {
if ( ! is_array( $robots ) ) {
$robots = array();
}
unset( $robots['index'] );
$robots['noindex'] = true;
$robots['follow'] = true;
return $robots;
}
public static function emit_head() {
if ( ! self::$current || empty( self::$current['head_html'] ) ) {
return;
}
$html = (string) self::$current['head_html'];
$n = self::current_page_num();
if ( $n > 1 && ! empty( self::$current['canonical'] ) && self::canonical_is_own_url( (string) self::$current['canonical'] ) ) {
$paged = self::paged_url( (string) self::$current['canonical'], $n );
if ( '' !== $paged ) {
$html = (string) preg_replace_callback(
'##',
static function () use ( $paged ) {
return '';
},
$html,
1
);
}
}
$social = self::social_enabled();
if ( ! $social ) {
$html = (string) preg_replace( '#^\n?#m', '', $html );
}
if ( false !== strpos( $html, '"offers"' ) && class_exists( 'AIEO_Catalogue_Mode' ) && AIEO_Catalogue_Mode::prices_off( is_singular( 'product' ) ? (int) get_queried_object_id() : null ) ) {
$html = self::without_offers( $html );
}
if ( ! empty( self::$current['prices_expired'] ) && false !== strpos( $html, '"offers"' ) ) {
$html = self::without_offers( $html );
}
echo "\n", $html, "\n";
if ( $social && is_singular( 'product' ) ) {
echo self::meta_html( self::product_price_tags( get_queried_object() ) );
}
}
private static function queue_social() {
if ( self::social_enabled() ) {
add_action( 'wp_head', array( __CLASS__, 'emit_social' ), 2 );
}
}
public static function social_enabled() {
$opt = get_option( 'aieo_dmm_settings' );
return ! ( is_array( $opt ) && isset( $opt['seo'] ) && is_array( $opt['seo'] ) && array_key_exists( 'social_tags', $opt['seo'] ) && ! $opt['seo']['social_tags'] );
}
public static function emit_social() {
if ( has_action( 'wp_head', 'jetpack_og_tags' ) ) {
return;
}
echo self::meta_html( self::social_tags() );
}
public static function social_tags() {
if ( is_404() || is_search() || is_author() || is_date() || is_feed() || is_embed() ) {
return array();
}
$post = null;
$type = 'website';
if ( is_singular() ) {
$post = get_queried_object();
if ( ! $post instanceof WP_Post || post_password_required( $post ) ) {
return array();
}
$url = (string) wp_get_canonical_url( $post );
if ( 'product' === $post->post_type ) {
$type = 'product';
} elseif ( 'page' !== $post->post_type && ! is_front_page() ) {
$type = 'article';
}
} elseif ( is_front_page() ) {
$url = home_url( '/' );
} elseif ( is_home() ) {
$blog = (int) get_option( 'page_for_posts' );
$url = $blog > 0 ? (string) get_permalink( $blog ) : home_url( '/' );
} elseif ( is_category() || is_tag() || is_tax() ) {
$term = get_queried_object();
$link = $term instanceof WP_Term ? get_term_link( $term ) : '';
$url = is_string( $link ) ? $link : '';
} elseif ( is_post_type_archive() ) {
$pt = get_query_var( 'post_type' );
$pt = is_array( $pt ) ? (string) reset( $pt ) : (string) $pt;
$url = (string) get_post_type_archive_link( $pt );
} else {
return array();
}
$tags = array(
'og:locale' => self::og_locale( get_locale() ),
'og:type' => $type,
'og:title' => self::plain( wp_get_document_title() ),
'og:description' => self::plain( self::fallback_description() ),
'og:url' => $url,
'og:site_name' => self::plain( get_bloginfo( 'name' ) ),
);
$image = $post instanceof WP_Post ? self::featured_image( $post ) : array();
if ( ! $image ) {
$image = self::default_image();
}
if ( $image ) {
$tags['og:image'] = $image[0];
if ( $image[1] > 0 && $image[2] > 0 ) {
$tags['og:image:width'] = (string) $image[1];
$tags['og:image:height'] = (string) $image[2];
}
}
if ( 'product' === $type ) {
$tags += self::product_price_tags( $post );
}
$tags['twitter:card'] = isset( $tags['og:image'] ) ? 'summary_large_image' : 'summary';
$opt = get_option( 'aieo_dmm_settings' );
$handle = is_array( $opt ) && isset( $opt['seo']['twitter_user'] ) ? ltrim( trim( (string) $opt['seo']['twitter_user'] ), '@' ) : '';
if ( '' !== $handle ) {
$tags['twitter:site'] = '@' . $handle;
}
$tags = apply_filters( 'aieo_seo_social_tags', $tags, $post );
$out = array();
foreach ( is_array( $tags ) ? $tags : array() as $name => $content ) {
if ( is_string( $name ) && '' !== $name && is_scalar( $content ) && '' !== trim( (string) $content ) ) {
$out[ $name ] = (string) $content;
}
}
return $out;
}
public static function without_offers( $html ) {
$strip = static function ( array $node ) use ( &$strip ) {
unset( $node['offers'] );
foreach ( $node as $key => $value ) {
if ( is_array( $value ) ) {
$node[ $key ] = $strip( $value );
}
}
return $node;
};
return (string) preg_replace_callback(
'##s',
static function ( $m ) use ( $strip ) {
if ( false === strpos( $m[1], '"offers"' ) ) {
return $m[0];
}
$graph = json_decode( $m[1], true );
$json = is_array( $graph ) ? wp_json_encode( $strip( $graph ), JSON_HEX_TAG | JSON_HEX_AMP | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) : false;
return is_string( $json ) ? '' : '';
},
(string) $html
);
}
public static function product_price_tags( $product ) {
if ( ! function_exists( 'wc_get_product' ) || ! function_exists( 'get_woocommerce_currency' ) ) {
return array();
}
if ( ! is_object( $product ) || ! method_exists( $product, 'get_price' ) ) {
$product = wc_get_product( $product );
}
if ( ! self::price_shown( $product ) ) {
return array();
}
$amount = function_exists( 'wc_get_price_to_display' ) ? wc_get_price_to_display( $product ) : $product->get_price();
$currency = (string) get_woocommerce_currency();
if ( ! is_numeric( $amount ) || '' === $currency ) {
return array();
}
return array(
'product:price:amount' => (string) wc_format_decimal( $amount, wc_get_price_decimals() ),
'product:price:currency' => $currency,
);
}
public static function price_shown( $product ) {
if ( ! is_object( $product ) || ! method_exists( $product, 'get_price' ) || '' === (string) $product->get_price() ) {
return false;
}
if ( class_exists( 'AIEO_Catalogue_Mode' ) && AIEO_Catalogue_Mode::prices_off( $product ) ) {
return false;
}
return ! method_exists( $product, 'get_price_html' ) || '' !== trim( wp_strip_all_tags( (string) $product->get_price_html() ) );
}
private static function featured_image( $post ) {
$id = (int) get_post_thumbnail_id( $post );
if ( $id <= 0 ) {
return array();
}
$src = wp_get_attachment_image_src( $id, 'large' );
if ( ! is_array( $src ) || empty( $src[0] ) ) {
return array();
}
return array( (string) $src[0], (int) $src[1], (int) $src[2] );
}
private static function default_image() {
$opt = get_option( 'aieo_dmm_settings' );
$seo = is_array( $opt ) && isset( $opt['seo'] ) && is_array( $opt['seo'] ) ? $opt['seo'] : array();
$url = isset( $seo['og_default_image'] ) ? trim( (string) $seo['og_default_image'] ) : '';
if ( '' === $url ) {
return array();
}
return array( $url, (int) ( $seo['og_default_image_width'] ?? 0 ), (int) ( $seo['og_default_image_height'] ?? 0 ) );
}
public static function og_locale( $locale ) {
$parts = explode( '_', (string) preg_replace( '/[^A-Za-z_]/', '', str_replace( '-', '_', (string) $locale ) ) );
$lang = strtolower( (string) $parts[0] );
if ( '' === $lang ) {
return 'en_US';
}
if ( isset( $parts[1] ) && 2 === strlen( $parts[1] ) ) {
return $lang . '_' . strtoupper( $parts[1] );
}
$map = array(
'el' => 'el_GR', 'en' => 'en_US', 'ja' => 'ja_JP', 'ar' => 'ar_AR', 'he' => 'he_IL', 'fi' => 'fi_FI',
'hr' => 'hr_HR', 'ca' => 'ca_ES', 'eu' => 'eu_ES', 'gl' => 'gl_ES', 'cy' => 'cy_GB', 'et' => 'et_EE',
'lt' => 'lt_LT', 'uk' => 'uk_UA', 'th' => 'th_TH', 'vi' => 'vi_VN', 'sq' => 'sq_AL', 'az' => 'az_AZ',
'hy' => 'hy_AM', 'ka' => 'ka_GE', 'kk' => 'kk_KZ', 'ko' => 'ko_KR', 'sl' => 'sl_SI', 'da' => 'da_DK',
);
return $map[ $lang ] ?? $lang . '_' . strtoupper( $lang );
}
private static function plain( $text ) {
return trim( html_entity_decode( (string) $text, ENT_QUOTES, 'UTF-8' ) );
}
public static function meta_html( $tags ) {
$out = '';
foreach ( (array) $tags as $name => $content ) {
$attr = 0 === strpos( (string) $name, 'twitter:' ) ? 'name' : 'property';
$value = in_array( $name, array( 'og:url', 'og:image' ), true ) ? esc_url( (string) $content ) : esc_attr( (string) $content );
if ( '' !== $value ) {
$out .= '' . "\n";
}
}
return $out;
}
private static function current_page_num() {
return is_singular()
? max( 1, (int) get_query_var( 'page' ) )
: max( 1, (int) get_query_var( 'paged' ) );
}
private static function canonical_is_own_url( $canonical ) {
$own = '';
if ( is_singular() ) {
$own = (string) get_permalink( get_queried_object_id() );
} else {
$term = get_queried_object();
if ( $term instanceof WP_Term ) {
$link = get_term_link( $term );
$own = is_wp_error( $link ) ? '' : (string) $link;
}
}
return '' !== $own && untrailingslashit( $canonical ) === untrailingslashit( $own );
}
private static function paged_url( $canonical, $n ) {
$canonical = trim( (string) $canonical );
$n = (int) $n;
if ( '' === $canonical || $n <= 1 ) {
return '';
}
if ( false !== strpos( $canonical, '?' ) ) {
return add_query_arg( is_singular() ? 'page' : 'paged', $n, $canonical );
}
$base = trailingslashit( $canonical );
if ( is_singular() && ! is_front_page() ) {
return user_trailingslashit( $base . $n, 'single_paged' );
}
global $wp_rewrite;
$pag_base = ( is_object( $wp_rewrite ) && ! empty( $wp_rewrite->pagination_base ) ) ? (string) $wp_rewrite->pagination_base : 'page';
return user_trailingslashit( $base . $pag_base . '/' . $n, 'paged' );
}
public static function filter_wc_structured_data( $types ) {
if ( ! is_array( $types ) ) {
return $types;
}
return array_values( array_diff( $types, array( 'product', 'website', 'breadcrumblist' ) ) );
}
}
}