array('local','news-arab','economy','sport'));
*/
return apply_filters('yal_news_category_slugs', $defaults);
}
}
/** هل التدوينة ضمن الأقسام الإخبارية؟ (يشمل الآباء) */
if ( ! function_exists('yal_post_in_news_cats') ) {
function yal_post_in_news_cats( $post_id ) {
$news_slugs = yal_news_category_slugs();
$terms = get_the_terms( $post_id, 'category' );
if ( empty($terms) || is_wp_error($terms) ) return false;
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $news_slugs, true ) ) return true;
$anc = get_ancestors( $term->term_id, 'category' );
if ( $anc ) foreach ( $anc as $aid ) {
$a = get_term( $aid, 'category' );
if ( $a && ! is_wp_error($a) && in_array( $a->slug, $news_slugs, true ) ) return true;
}
}
return false;
}
}
/** تصنيف أساسي (Yoast Primary) */
if ( ! function_exists('yal_primary_category_term') ) {
function yal_primary_category_term( $post_id = null ){
$post_id = $post_id ?: get_the_ID();
$term = null;
if ( class_exists('WPSEO_Primary_Term') ) {
$yoast = new WPSEO_Primary_Term( 'category', $post_id );
$pid = $yoast->get_primary_term();
if ( $pid && ! is_wp_error($pid) ) {
$t = get_term( $pid, 'category' );
if ( $t && ! is_wp_error($t) ) $term = $t;
}
}
if ( ! $term ) {
$cats = get_the_category( $post_id );
if ( ! empty($cats) ) $term = $cats[0];
}
return $term;
}
}
/** نص صِرف + عدد كلمات */
if ( ! function_exists('yal_plain_text') ) {
function yal_plain_text( $html ) {
$t = wp_strip_all_tags( $html, true );
$t = html_entity_decode( $t, ENT_QUOTES, 'UTF-8' );
$t = preg_replace('/\s+/u', ' ', $t);
return trim($t);
}
}
if ( ! function_exists('yal_word_count') ) {
function yal_word_count( $text ) {
$text = yal_plain_text($text);
if ($text === '') return 0;
$parts = preg_split('/[\s\p{Z}\x{00A0}]+/u', $text, -1, PREG_SPLIT_NO_EMPTY);
return is_array($parts) ? count($parts) : 0;
}
}
/* =================== 1) أنماط وأصول: خطوط + RTL + ستايل الطفل =================== */
add_action('wp_enqueue_scripts', function () {
if ( function_exists('is_rtl') && is_rtl() ) {
wp_enqueue_style( 'tie-theme-rtl-css', get_template_directory_uri() . '/rtl.css', [], null );
}
$css = get_stylesheet_directory() . '/style.css';
$ver = file_exists($css) ? filemtime($css) : null;
wp_enqueue_style('yal-child-style', get_stylesheet_directory_uri().'/style.css', [], $ver);
}, 80);
/** تعطيل حقن خطوط خارجية بنفس الاسم (لتفادي تكرار) */
add_action('wp_enqueue_scripts', function(){
if ( wp_style_is('readex-pro-font','enqueued') ) {
wp_dequeue_style('readex-pro-font');
wp_deregister_style('readex-pro-font');
}
}, 99);
/** Preload لملفات الخط فوق الطيّة */
add_action('wp_head', function(){
if ( yal_is_amp() ) return;
$fonts = [
'/wp-content/themes/jannah-child/assets/fonts/subset-ReadexPro-Regular.woff2',
'/wp-content/themes/jannah-child/assets/fonts/subset-ReadexPro-SemiBold.woff2',
'/wp-content/themes/jannah-child/assets/fonts/subset-ReadexPro-Bold.woff2',
];
foreach ($fonts as $url){
printf('
'."\n", esc_url($url));
}
}, 3);
/** Resource Hints */
add_filter('wp_resource_hints', function( $hints, $relation_type ){
$prefetch = ['//www.googletagmanager.com','//pagead2.googlesyndication.com','//tpc.googlesyndication.com','//googleads.g.doubleclick.net'];
if ( 'dns-prefetch' === $relation_type ) {
foreach ( $prefetch as $d ){ if ( ! in_array($d, $hints, true) ) $hints[] = $d; }
}
if ( 'preconnect' === $relation_type ) {
foreach ( ['https://www.googletagmanager.com','https://pagead2.googlesyndication.com','https://tpc.googlesyndication.com','https://googleads.g.doubleclick.net'] as $u ){
if ( ! in_array($u, $hints, true) ) $hints[] = $u;
}
}
return $hints;
}, 10, 2);
/* ===== Robots: اسمح بأطول مقتطف وصور كبيرة ===== */
add_filter('wp_robots', function($robots){
if ( is_singular('post') ) {
$robots['max-image-preview'] = 'large';
$robots['max-snippet'] = -1;
$robots['max-video-preview'] = -1;
}
return $robots;
}, 99);
/* ============================ 2) تصحيحات سكيمة Yoast ============================ */
/** Publisher = NewsMediaOrganization + سياسات الثقة (روابط سليمة فقط) + Logo مؤكد */
add_filter( 'wpseo_schema_organization', function( $data ) {
if ( ! is_array($data) ) return $data;
$data['@type'] = 'NewsMediaOrganization';
// خرائط الصفحات — نتأكد أنها منشورة قبل وضع الرابط لتفادي 404 في JSON-LD
$map = [
'publishingPrinciples' => 'editorial-policy',
'correctionsPolicy' => 'corrections-policy',
'ownershipFundingInfo' => 'ownership-funding',
'ethicsPolicy' => 'ethics-policy',
];
foreach ( $map as $key => $slug ) {
$p = get_page_by_path( $slug );
if ( $p && $p->post_status === 'publish' ) {
$data[$key] = get_permalink( $p );
} else {
unset( $data[$key] );
}
}
if ( empty($data['logo']) ) {
$site_logo_id = get_theme_mod( 'custom_logo' );
if ( $site_logo_id ) {
$meta = wp_get_attachment_metadata( $site_logo_id );
$imgurl = wp_get_attachment_image_url( $site_logo_id, 'full' );
if ( $imgurl && !empty($meta['width']) && !empty($meta['height']) ) {
$data['logo'] = [
'@type' => 'ImageObject',
'url' => $imgurl,
'width' => (int) $meta['width'],
'height' => (int) $meta['height'],
];
}
}
}
return $data;
}, 20);
/** NewsArticle/Article حسب الأقسام (متوافق مع Yoast News) */
add_filter('wpseo_schema_article_type', function( $type ){
if ( ! is_singular('post') ) return $type ?: 'Article';
$post_id = get_queried_object_id();
if ( ! $post_id ) return $type ?: 'Article';
return yal_post_in_news_cats( $post_id ) ? 'NewsArticle' : 'Article';
}, 99);
/** articleSection + mentions + hardening (inLanguage/isAccessibleForFree/thumbnailUrl/wordCount) */
add_filter('wpseo_schema_article', function($data){
if ( ! is_singular('post') || ! is_array($data) ) return $data;
global $post; if ( ! $post ) return $data;
// القسم الأساسي
$pterm = yal_primary_category_term($post->ID);
if ( $pterm ) $data['articleSection'] = $pterm->name;
// لغة المحتوى + وصول مجاني
$lang = get_bloginfo('language'); // قد يُرجع ar أو ar-SA
if ( $lang ) {
if ( strpos($lang, 'ar') === 0 ) { $lang = 'ar'; }
$data['inLanguage'] = $lang;
}
$data['isAccessibleForFree'] = true;
// headline نظيف
$headline = isset($data['headline']) && $data['headline'] ? $data['headline'] : get_the_title($post);
$headline = preg_replace('/\s+/u', ' ', wp_strip_all_tags($headline));
$data['headline'] = trim($headline);
// description احتياطي
if ( empty( $data['description'] ) ) {
$excerpt = get_the_excerpt( $post );
if ( ! $excerpt ) {
$first_p = wpautop( wp_kses_post( $post->post_content ) );
$first_p = preg_replace('~.*?
]*>(.*?)
.*~is', '$1', $first_p);
$first_p = yal_plain_text( $first_p );
$words = preg_split('/\s+/u', $first_p);
$excerpt = implode(' ', array_slice($words, 0, 30));
}
$data['description'] = trim( $excerpt );
}
// mainEntityOfPage احتياطي
if ( empty( $data['mainEntityOfPage'] ) ) {
$data['mainEntityOfPage'] = [
'@type' => 'WebPage',
'@id' => get_permalink( $post ),
];
}
// speakable
$enable_speakable = apply_filters('yal_enable_speakable', (substr((string)$lang,0,2) === 'en'));
if ( $enable_speakable ) {
$data['speakable'] = [
'@type' => 'SpeakableSpecification',
'xpath' => [ '/html/head/title', '//h1', '//p[1]' ],
];
} else {
unset($data['speakable']);
}
// mentions من الوسوم (حتى 10)
$tags = get_the_tags($post->ID);
if ($tags){
$tags = array_slice($tags, 0, 10);
$data['mentions'] = array_map(function($t){
return ['@type'=>'Thing','name'=>$t->name];
}, $tags);
}
// articleBody + wordCount
$raw = get_post_field('post_content', $post, 'raw');
$body = yal_plain_text( $raw );
if ( function_exists('mb_substr') ) { $body = mb_substr( $body, 0, 4000, 'UTF-8' ); }
else { $body = substr( $body, 0, 4000 ); }
if ( $body ) {
$data['articleBody'] = $body;
$wc = yal_word_count( $raw );
if ( $wc ) $data['wordCount'] = $wc;
}
// صورة + thumbnailUrl
$img_id = get_post_thumbnail_id( $post );
if ( $img_id ) {
$meta = wp_get_attachment_metadata( $img_id );
$img_url = wp_get_attachment_image_url( $img_id, 'full' );
if ( $img_url ) {
if ( is_array($meta) && ! empty($meta['width']) && ! empty($meta['height']) ) {
$data['image'] = [
'@type' => 'ImageObject',
'url' => $img_url,
'contentUrl' => $img_url,
'width' => (int) $meta['width'],
'height' => (int) $meta['height'],
'caption' => get_the_title( $img_id ),
];
} elseif ( empty($data['image']) ) {
$data['image'] = $img_url;
}
if ( empty($data['thumbnailUrl']) ) $data['thumbnailUrl'] = $img_url;
}
}
return $data;
}, 25);
/* ======================= 3) عناصر تجربة القراءة والواجهة ======================= */
/** فتات الخبز قبل العنوان */
add_action('tie_before_single_post_title', function(){
if ( function_exists('yoast_breadcrumb') && is_singular('post') ){
yoast_breadcrumb('
','
');
}
}, 5);
/** تاريخ النشر + شارة القسم بعد العنوان */
add_action('tie_after_single_post_title', function(){
if ( ! is_singular('post') ) return;
$post = get_queried_object(); if ( ! $post ) return;
$pub_iso = get_post_time('c', true, $post);
$pub_human = get_the_time( 'j F Y — H:i', $post );
$term = yal_primary_category_term( $post->ID );
$section_html = '';
if ( $term ) {
$section_html = sprintf(
'
',
esc_url( get_term_link( $term ) ),
esc_html( $term->name )
);
}
echo '
';
echo '
نُشر في: '. esc_html($pub_human) .'
';
echo $section_html;
echo '
';
}, 12);
/** بطاقة ميتا أسفل العنوان مباشرة */
add_filter('the_content', function( $content ){
if ( ! is_singular('post') || ! in_the_loop() || ! is_main_query() ) return $content;
$post = get_queried_object(); if ( ! $post ) return $content;
$publisher = get_bloginfo('name') ?: 'اليمن الغد';
$author = get_the_author_meta( 'display_name', $post->post_author );
$upd_iso = get_post_modified_time('c', true, $post);
$upd_human = get_the_modified_time( 'j F Y — H:i', $post );
$show_update = ( $post->post_modified_gmt && $post->post_modified_gmt !== $post->post_date_gmt );
ob_start(); ?>
تم التحديث:
0, 'modestbranding'=>1, 'iv_load_policy'=>3, 'playsinline'=>1, 'enablejsapi'=>1 ];
$query = wp_parse_url( $url ? $url : $combined, PHP_URL_QUERY );
if ( $query ) {
parse_str( $query, $q );
if ( !empty($q['list']) ) $params['list'] = sanitize_text_field($q['list']);
if ( !empty($q['start']) ) $params['start'] = intval($q['start']);
if ( !empty($q['t']) && empty($params['start']) ) $params['start'] = intval($q['t']);
}
$src = 'https://www.youtube-nocookie.com/embed/' . rawurlencode($vid) . '?' . http_build_query($params);
return sprintf(
'
',
esc_url($src)
);
}
/* ==================== 3.1) شريط أقسام للجوال — بعد الهيدر ==================== */
add_action('after_setup_theme', function(){
register_nav_menus([ 'yal_mobile_topics' => __('قائمة الأقسام (الجوال)', 'yal') ]);
});
/** CSS فوري وآمن للشريط (يمنع CLS) */
add_action('wp_head', function(){ ?>
url);
$text = esc_html($m->title);
$cls = ( $current_cat_url && trailingslashit($url) === trailingslashit($current_cat_url) ) ? ' gmcat-item current-cat' : ' gmcat-item';
$items_html .= '
'. $text .' ';
}
}
}
// fallback تلقائي
if ( '' === $items_html ) {
$cats = get_categories(['orderby'=>'count','order'=>'DESC','number'=>12]);
if ( ! empty($cats) && ! is_wp_error($cats) ) {
foreach ( $cats as $c ) {
$url = esc_url( get_category_link($c->term_id) );
$text = esc_html($c->name);
$cls = ( $current_cat_url && trailingslashit($url) === trailingslashit($current_cat_url) ) ? ' gmcat-item current-cat' : ' gmcat-item';
$items_html .= '
'. $text .' ';
}
}
}
$home_item = '
'. esc_html__('الرئيسية','yal') .' ';
if ( $is_amp ) {
echo '
'. $home_item . $items_html .' ';
return;
} ?>
'."\n";
}
}, 5);
/** ⭐ تعديل سمات صورة المقال البارزة: إضافة width/height + eager + decoding */
add_filter('wp_get_attachment_image_attributes', function($attr, $attachment, $size){
if ( is_singular('post') && $attachment->ID === get_post_thumbnail_id() ){
$meta = wp_get_attachment_metadata($attachment->ID);
if (!empty($meta['width']) && !empty($meta['height'])) {
$attr['width'] = (int) $meta['width'];
$attr['height'] = (int) $meta['height'];
}
$attr['fetchpriority'] = 'high';
$attr['decoding'] = 'async';
$attr['loading'] = 'eager';
}
return $attr;
}, 10, 3);
/** ⭐ صور المحتوى: حقن width/height تلقائيًا + lazy/decoding */
add_filter('the_content', function($html){
if (!is_singular('post') || !in_the_loop() || !is_main_query()) return $html;
return preg_replace_callback('~
]+?)>~i', function($m){
$tag = $m[0];
// لا تكرر الإضافة لو موجودة أصلًا
$hasW = stripos($tag, ' width=') !== false;
$hasH = stripos($tag, ' height=') !== false;
// اجعل صور المحتوى lazy + decoding async دائمًا (غير الصورة البارزة)
if ( stripos($tag, ' loading=') === false ) $tag = preg_replace('~
' . "\n";
echo '' . "\n";
?>
ID;
if ( $cached = get_transient($cache_key) ) return $content . $cached;
$primary = yal_primary_category_term($post->ID);
$cat_id = $primary ? (int) $primary->term_id : 0;
$tag_ids = [];
$tags = get_the_tags($post->ID);
if ( $tags && !is_wp_error($tags) ){
foreach ($tags as $t){ $tag_ids[] = (int) $t->term_id; }
if ( count($tag_ids) > 8 ) $tag_ids = array_slice($tag_ids, 0, 8);
}
$tax_query = [];
if ( $cat_id ) $tax_query[] = ['taxonomy'=>'category','field'=>'term_id','terms'=>[$cat_id]];
if ( $tag_ids ) $tax_query[] = ['taxonomy'=>'post_tag','field'=>'term_id','terms'=>$tag_ids];
if ( count($tax_query) > 1 ) $tax_query['relation'] = 'OR';
$args = [
'post_type'=>'post','post_status'=>'publish','posts_per_page'=>20,
'post__not_in'=>[ $post->ID ], 'no_found_rows'=>true, 'ignore_sticky_posts'=>true,
'orderby'=>'date','order'=>'DESC','update_post_meta_cache'=>false,'update_post_term_cache'=>false,
'suppress_filters'=>true, 'date_query'=>[['after'=> date('Y-m-d', strtotime('-180 days'))]],
];
if ( $tax_query ) $args['tax_query'] = $tax_query;
$q = new WP_Query($args);
if ( !$q->have_posts() && $cat_id ){
$q = new WP_Query([
'post_type'=>'post','post_status'=>'publish','posts_per_page'=>12,'post__not_in'=>[ $post->ID ],
'cat'=>$cat_id,'no_found_rows'=>true,'ignore_sticky_posts'=>true,'orderby'=>'date','order'=>'DESC',
]);
}
if ( !$q->have_posts() ) return $content;
$cands = [];
while ( $q->have_posts() ){ $q->the_post();
$id = get_the_ID(); $score = 0;
if ( $cat_id && has_category($cat_id, $id) ) $score += 3;
if ( $tag_ids ){
$pt = wp_get_post_tags($id, ['fields'=>'ids']);
$over = count(array_intersect($tag_ids, $pt)); $score += min($over,4)*2;
}
$days = (int)(( time() - get_post_time('U',true,$id) )/DAY_IN_SECONDS);
if ( $days <= 7 ) $score += 3; elseif ( $days <= 30 ) $score += 2; elseif ( $days <= 90 ) $score += 1;
$cands[] = ['id'=>$id,'score'=>$score];
}
wp_reset_postdata();
if ( ! $cands ) return $content;
usort($cands, function($a,$b){
if ($a['score'] === $b['score']) return get_post_time('U',true,$b['id']) <=> get_post_time('U',true,$a['id']);
return $b['score'] <=> $a['score'];
});
$top = array_slice($cands, 0, 3);
ob_start(); ?>
'https://wa.me/?text='.$enc_t.'%20'.$enc_u,
'tg' => 'https://t.me/share/url?url='.$enc_u.'&text='.$enc_t,
'x' => 'https://twitter.com/intent/tweet?url='.$enc_u.'&text='.$enc_t,
'fb' => 'https://www.facebook.com/sharer/sharer.php?u='.$enc_u,
];
ob_start(); ?>
post_status !== 'publish' ) return;
if ( function_exists('yal_post_in_news_cats') && ! yal_post_in_news_cats($post_id) ) return;
if ( ( time() - get_post_time('U', true, $post_id) ) > 2 * DAY_IN_SECONDS ) return;
$k = 'yal_ping_news_' . $post_id;
if ( get_transient($k) ) return;
$newsmap = 'https://alyemenalghad.com/news-sitemap.xml';
wp_remote_get( 'https://www.google.com/ping?sitemap=' . rawurlencode($newsmap), [
'timeout' => 2, 'blocking' => false, 'user-agent'=> 'YAL-NewsPing/1.0'
]);
set_transient($k, 1, 10 * MINUTE_IN_SECONDS);
}
add_action('publish_post', 'yal_maybe_ping_news_sitemap', 10, 1);
add_action('post_updated', function($post_ID){ yal_maybe_ping_news_sitemap($post_ID); }, 10, 1);
/* ============================== 8) ⭐ تقليل الطلبات ============================== */
/** ⭐ عطّل Emoji */
add_action('init', function(){
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
add_filter('emoji_svg_url', '__return_false');
});
/** ⭐ عطّل oEmbed discovery + REST link في */
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
/** ⭐ عطّل Dashicons للزائر فقط */
add_action('wp_enqueue_scripts', function(){
if ( !is_user_logged_in() ) wp_dequeue_style('dashicons');
}, 100);
/** ⭐ خيار (فلتر) لتعطيل Gravatar نهائيًا لتقليل طلب خارجي */
add_filter('pre_option_show_avatars', function($v){
// غيّر القيمة عبر: add_filter('yal_disable_gravatar', '__return_true');
$disable = apply_filters('yal_disable_gravatar', false);
return $disable ? 0 : $v;
});