[ 'code' => 'ar', 'label' => 'MSA - Modern Standard Arabic' ],
'saudi' => [ 'code' => 'ar-SA', 'label' => 'Saudi Najdi Arabic' ],
'egyptian' => [ 'code' => 'ar-EG', 'label' => 'Egyptian Arabic (Masri)' ],
'kuwaiti' => [ 'code' => 'ar-KW', 'label' => 'Kuwaiti Arabic' ],
'lebanese' => [ 'code' => 'ar-LB', 'label' => 'Lebanese Arabic' ],
'uae' => [ 'code' => 'ar-AE', 'label' => 'Emirati Arabic (UAE)' ],
'iraqi' => [ 'code' => 'ar-IQ', 'label' => 'Iraqi Arabic' ],
'jordanian' => [ 'code' => 'ar-JO', 'label' => 'Jordanian Arabic' ],
'moroccan' => [ 'code' => 'ar-MA', 'label' => 'Moroccan Arabic (Darija)' ],
];
public static function dialect_regions() {
return self::DIALECT_REGIONS;
}
public static function init() {
add_action( 'wp_head', [ __CLASS__, 'output_hreflang' ], 2 );
}
public static function output_hreflang() {
$settings = get_option( 'awbseo_settings', AWBSEO_Core::default_settings() );
if ( empty( $settings['hreflang_enabled'] ) ) return;
$tags = self::get_tags_for_current_url();
foreach ( $tags as $tag ) {
printf(
'' . "\n",
esc_attr( $tag['lang'] ),
esc_url( $tag['url'] )
);
}
}
private static function get_tags_for_current_url() {
// WPML integration.
if ( function_exists( 'icl_get_languages' ) ) {
return self::from_wpml();
}
// Polylang integration.
if ( function_exists( 'pll_the_languages' ) ) {
return self::from_polylang();
}
// Manual pairs from settings.
return self::from_settings();
}
private static function from_wpml() {
$languages = icl_get_languages( 'skip_missing=0' );
$tags = [];
foreach ( $languages as $lang ) {
$tags[] = [
'lang' => $lang['language_code'],
'url' => $lang['url'],
];
}
// Add x-default pointing to default language.
$default = apply_filters( 'wpml_default_language', null );
if ( $default && isset( $languages[ $default ] ) ) {
$tags[] = [ 'lang' => 'x-default', 'url' => $languages[ $default ]['url'] ];
}
return $tags;
}
private static function from_polylang() {
$languages = pll_the_languages( [ 'raw' => 1 ] );
$tags = [];
foreach ( (array) $languages as $lang ) {
$tags[] = [
'lang' => $lang['slug'],
'url' => $lang['url'],
];
}
return $tags;
}
private static function from_settings() {
$settings = get_option( 'awbseo_settings', [] );
$pairs = $settings['hreflang_pairs'] ?? [];
$tags = [];
foreach ( $pairs as $pair ) {
if ( empty( $pair['lang'] ) || empty( $pair['url'] ) ) continue;
$tags[] = [
'lang' => sanitize_text_field( $pair['lang'] ),
'url' => esc_url_raw( $pair['url'] ),
];
}
return $tags;
}
/**
* Validate the configured manual hreflang pairs. Checks two things
* Google's own hreflang guidelines call out as the most common reasons
* a hreflang set gets silently ignored:
*
* 1. Every target URL actually resolves (HTTP 200).
* 2. Every pair is reciprocal - each page in the set links back to
* every other page in the set (and ideally to itself). A one-way
* hreflang link is treated by Google as untrustworthy and dropped.
*
* Returns an array of issues, each
* [ 'lang' => .., 'url' => .., 'type' => .., 'message' => .. ]
* with 'type' one of: unreachable, bad_status, not_reciprocal,
* no_self_reference. An empty array means everything checked out.
*/
public static function audit_pairs() {
$settings = get_option( 'awbseo_settings', [] );
$pairs = array_values( array_filter(
$settings['hreflang_pairs'] ?? [],
function ( $p ) { return ! empty( $p['lang'] ) && ! empty( $p['url'] ); }
) );
$issues = [];
foreach ( $pairs as $pair ) {
$url = $pair['url'];
$lang = $pair['lang'];
$response = wp_remote_get( $url, [ 'timeout' => 8, 'sslverify' => false ] );
if ( is_wp_error( $response ) ) {
$issues[] = [
'lang' => $lang,
'url' => $url,
'type' => 'unreachable',
'message' => "Could not reach {$url}: " . $response->get_error_message(),
];
continue;
}
$code = (int) wp_remote_retrieve_response_code( $response );
if ( 200 !== $code ) {
$issues[] = [
'lang' => $lang,
'url' => $url,
'type' => 'bad_status',
'message' => "{$url} returned HTTP {$code} instead of 200 - Google will ignore this hreflang entry.",
];
continue;
}
$found = self::extract_hreflang_from_html( wp_remote_retrieve_body( $response ) );
// Every OTHER configured language must be reciprocated on this page.
foreach ( $pairs as $other ) {
if ( $other['lang'] === $lang ) continue;
if ( ! self::pairs_contain( $found, $other['lang'], $other['url'] ) ) {
$issues[] = [
'lang' => $lang,
'url' => $url,
'type' => 'not_reciprocal',
'message' => "{$url} (hreflang=\"{$lang}\") doesn't link back to your {$other['lang']} version ({$other['url']}). Hreflang pairs must be reciprocal or Google ignores them.",
];
}
}
// Best practice: a page should also declare hreflang for itself.
if ( ! self::pairs_contain( $found, $lang, $url ) ) {
$issues[] = [
'lang' => $lang,
'url' => $url,
'type' => 'no_self_reference',
'message' => "{$url} doesn't include a self-referencing hreflang=\"{$lang}\" tag pointing to itself, which Google's guidelines recommend.",
];
}
}
return $issues;
}
/**
* Parse every tag out
* of a raw HTML string. Used to check what a target page actually
* declares, independent of what we expect it to declare.
*/
private static function extract_hreflang_from_html( $html ) {
$found = [];
if ( ! preg_match_all( '/]*rel=["\']alternate["\'][^>]*>/i', (string) $html, $tags ) ) {
return $found;
}
foreach ( $tags[0] as $tag ) {
if ( ! preg_match( '/hreflang=["\']([^"\']+)["\']/i', $tag, $lang_m ) ) continue;
if ( ! preg_match( '/href=["\']([^"\']+)["\']/i', $tag, $url_m ) ) continue;
$found[] = [ 'lang' => $lang_m[1], 'url' => html_entity_decode( $url_m[1] ) ];
}
return $found;
}
/**
* True if $haystack (a list of ['lang'=>..,'url'=>..] pairs, typically
* from extract_hreflang_from_html()) already contains a pair matching
* $lang/$url. Language codes compare case-insensitively; URLs ignore a
* trailing slash so "https://x.com/ar" and "https://x.com/ar/" count
* as the same target.
*/
private static function pairs_contain( array $haystack, $lang, $url ) {
foreach ( $haystack as $pair ) {
if ( 0 === strcasecmp( $pair['lang'], $lang )
&& untrailingslashit( $pair['url'] ) === untrailingslashit( $url ) ) {
return true;
}
}
return false;
}
}