// Function to transfer SEO metadata (Yoast SEO) function wp_transfer_metadata_enhanced($source_site, $new_post_id, $source_post_id) { $log_html = ''; $url = $source_site . 'wp-json/wp/v2/posts/' . $source_post_id . '?context=view&_fields=id,title,yoast_head_json,yoast_title,yoast_metadesc,yoast_focuskw'; // title included for fallback $response = wp_remote_get($url, array('timeout' => 20)); $log_html .= "

- سئوی Yoast: درحال واکشی برای پست مبدأ با شناسه " . esc_html($source_post_id) . "...

"; if (is_wp_error($response)) { error_log('انتقال پست: واکشی متادیتای سئو از ' . $url . ' ناموفق بود. خطا: ' . $response->get_error_message()); $log_html .= '

-- واکشی ناموفق بود. خطا: ' . esc_html($response->get_error_message()) . '

'; return $log_html; } $response_code = wp_remote_retrieve_response_code($response); if ($response_code !== 200) { error_log('انتقال پست: پاسخ غیر 200 (' . $response_code . ') برای متادیتای سئو از ' . $url); $log_html .= '

-- واکشی ناموفق بود (وضعیت HTTP ' . esc_html($response_code) . ').

'; return $log_html; } $post_body = wp_remote_retrieve_body($response); $source_post_data = json_decode($post_body, true); if (empty($source_post_data) || !is_array($source_post_data)) { error_log('انتقال پست: رمزگشایی JSON برای متادیتای سئو از ' . $url . ' ناموفق بود یا اطلاعات خالی/نامعتبر است. بدنه: ' . $post_body); $log_html .= '

-- رمزگشایی متادیتای سئو ناموفق بود.

'; return $log_html; } $seo_title = ''; $meta_description = ''; $focus_keyword = ''; $focus_keyword_set_from_title = false; // Fetch SEO Title if (isset($source_post_data['yoast_title']) && is_string($source_post_data['yoast_title'])) { $potential_title = trim($source_post_data['yoast_title']); if (!empty($potential_title)) $seo_title = $potential_title; } // Fetch Meta Description if (isset($source_post_data['yoast_metadesc']) && is_string($source_post_data['yoast_metadesc'])) { $potential_desc = trim($source_post_data['yoast_metadesc']); if (!empty($potential_desc)) $meta_description = $potential_desc; } // Fallback for SEO Title and Meta Description from yoast_head_json if (isset($source_post_data['yoast_head_json']) && is_array($source_post_data['yoast_head_json'])) { if (empty($seo_title) && isset($source_post_data['yoast_head_json']['title']) && is_string($source_post_data['yoast_head_json']['title'])) { $potential_title_fallback = trim($source_post_data['yoast_head_json']['title']); if(!empty($potential_title_fallback)) $seo_title = $potential_title_fallback; } if (empty($meta_description) && isset($source_post_data['yoast_head_json']['description']) && is_string($source_post_data['yoast_head_json']['description'])) { $potential_desc_fallback = trim($source_post_data['yoast_head_json']['description']); if(!empty($potential_desc_fallback)) $meta_description = $potential_desc_fallback; } } // Attempt 1: Get Focus Keyphrase from yoast_focuskw if (isset($source_post_data['yoast_focuskw']) && is_string($source_post_data['yoast_focuskw'])) { $potential_kw = trim($source_post_data['yoast_focuskw']); if (!empty($potential_kw)) { $focus_keyword = $potential_kw; $log_html .= '

-- کلمه کلیدی کانونی از فیلد `yoast_focuskw` مبدأ دریافت شد.

'; } else { $log_html .= '

-- فیلد `yoast_focuskw` در پاسخ API مبدأ وجود داشت اما خالی بود.

'; } } else { $log_html .= '

-- فیلد `yoast_focuskw` اصلاً در پاسخ API مبدأ وجود نداشت.

'; } // Attempt 2: Fallback to post title if focus_keyword is still empty if (empty($focus_keyword)) { if (isset($source_post_data['title']['rendered']) && !empty(trim($source_post_data['title']['rendered']))) { // The title field was added to _fields in the API URL for this fallback $post_title_rendered = trim($source_post_data['title']['rendered']); $focus_keyword = sanitize_text_field($post_title_rendered); $focus_keyword_set_from_title = true; $log_html .= '

-- کلمه کلیدی کانونی: از عنوان پست ("' . esc_html(mb_strimwidth($focus_keyword, 0, 50, "...")) . '") به عنوان جایگزین استفاده شد.

'; } else { $log_html .= '

-- کلمه کلیدی کانونی: نه فیلد `yoast_focuskw` و نه عنوان پست (از `title.rendered`) برای جایگزینی یافت نشد.

'; } } // Update post meta $updated_meta_log = array(); if (!empty($seo_title)) { update_post_meta($new_post_id, '_yoast_wpseo_title', $seo_title); $updated_meta_log[] = "عنوان سئو ('" . esc_html(mb_strimwidth($seo_title, 0, 30, "...")) . "')"; } if (!empty($meta_description)) { update_post_meta($new_post_id, '_yoast_wpseo_metadesc', $meta_description); $updated_meta_log[] = "توضیحات متا ('" . esc_html(mb_strimwidth($meta_description, 0, 30, "...")) . "')"; } if (!empty($focus_keyword)) { update_post_meta($new_post_id, '_yoast_wpseo_focuskw', $focus_keyword); if ($focus_keyword_set_from_title) { $updated_meta_log[] = "کلمه کلیدی کانونی (از عنوان: '" . esc_html(mb_strimwidth($focus_keyword, 0, 30, "...")) . "')"; } else { $updated_meta_log[] = "کلمه کلیدی کانونی ('" . esc_html(mb_strimwidth($focus_keyword, 0, 30, "...")) . "')"; } } if (!empty($updated_meta_log)) { $log_html .= '

-- به‌روزرسانی شد: ' . implode('، ', $updated_meta_log) . '.

'; } elseif (empty($focus_keyword) && empty($seo_title) && empty($meta_description)) { $log_html .= '

-- اطلاعات خاصی از سئوی Yoast یافت نشد یا به‌روزرسانی نشد.

'; } return $log_html; } پیمان امیدی, نویسنده در هشتینو - صفحه 8 از 351