'err_post_id_invalid']); } if (empty($keyword)) { wp_send_json_error(['code' => 'err_keyword_empty']); } $post_title = get_the_title($post_id); if (empty($post_title)) { wp_send_json_error(['code' => 'err_post_title_missing']); } if (empty($custom_prompt)) { $custom_prompt = get_post_meta($post_id, 'aicont_singlepost_seo_description_prompt_custom', true); } if (empty($custom_prompt)) { $custom_prompt = get_option('aicont_plugin_seo_meta_prompt_custom', ''); } $active_license = get_option('aicont_active_license', ''); $result = aicont_call_webtinus_meta_description([ 'keyword' => $keyword, 'post_id' => $post_id, 'post_title' => $post_title, 'prompt' => $custom_prompt, 'api_key' => $active_license ]); // بررسی اگر نتیجه wp_error باشد if (is_wp_error($result)) { wp_send_json_error(['code' => 'err_connection_failed']); } // بررسی خطا در نتیجه if (!empty($result['error_code'])) { wp_send_json_error(['code' => $result['error_code']]); } // بررسی اگر meta_description خالی باشد if (empty($result['meta_description'])) { wp_send_json_error(['code' => 'err_content_empty']); } $meta_description = sanitize_textarea_field(trim($result['meta_description'])); // بررسی اگر بعد از sanitize خالی شد if (empty($meta_description)) { wp_send_json_error(['code' => 'err_content_empty']); } // بررسی Yoast (اولویت اول) if (class_exists('WPSEO_Meta')) { aicont_save_yoast_meta_description($post_id, $meta_description); } // بررسی Rank Math (اولویت دوم) elseif (defined('RANK_MATH_VERSION') || class_exists('RankMath\Core')) { aicont_save_rankmath_meta_description($post_id, $meta_description); } // اگر هیچ کدام نبود else { wp_send_json_error(['code' => 'err_no_seo_plugin']); } wp_update_post([ 'ID' => $post_id, 'post_modified' => current_time('mysql'), 'post_modified_gmt' => current_time('mysql', 1) ]); wp_send_json_success([ 'message' => 'success_meta_generated', 'meta_description' => $meta_description ]); } /** * ذخیره توضیح متا برای Yoast */ function aicont_save_yoast_meta_description($post_id, $meta_description) { update_post_meta($post_id, '_yoast_wpseo_metadesc', $meta_description); // اطمینان از ذخیره شدن $saved = get_post_meta($post_id, '_yoast_wpseo_metadesc', true); if ($saved !== $meta_description) { wp_die('Failed to save Yoast meta description'); } } /** * ذخیره توضیح متا برای Rank Math - با استفاده از روش رسمی */ function aicont_save_rankmath_meta_description($post_id, $meta_description) { // بررسی Rank Math if (!function_exists('rank_math')) { wp_die('Rank Math is not active'); } // روش 1: استفاده از RankMath\Helper::update_post_meta if (method_exists('\RankMath\Helper', 'update_post_meta')) { \RankMath\Helper::update_post_meta('rank_math_description', $meta_description, $post_id); } // روش 2: Update via post meta مستقیم else { update_post_meta($post_id, 'rank_math_description', $meta_description); } // اطمینان از ذخیره شدن $saved = get_post_meta($post_id, 'rank_math_description', true); if ($saved !== $meta_description) { // اگر مستقیم کار نکرد، سعی کن با فیلتر do_action('rank_math/before_save_post', $post_id); update_post_meta($post_id, 'rank_math_description', $meta_description); do_action('rank_math/after_save_post', $post_id); } } /** * صدا زدن سرور Webtinus برای تولید توضیح متا */ function aicont_call_webtinus_meta_description($args) { $client_site_url = home_url('', 'https'); $server_url = 'https://webtinus.com/wp-json/aicont/v1/generate_meta_description1'; $body = wp_json_encode([ 'keyword' => $args['keyword'], 'post_id' => $args['post_id'], 'post_title' => $args['post_title'], 'prompt' => $args['prompt'], 'api_key' => $args['api_key'] ?? '', 'client_site_url' => $client_site_url, 'site_title' => get_bloginfo('name'), 'site_language' => get_locale() ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $response = wp_remote_post($server_url, [ 'headers' => [ 'Content-Type' => 'application/json', 'X-Client-Site' => $client_site_url ], 'body' => $body, 'timeout' => 90 ]); if (is_wp_error($response)) { return ['error_code' => 'err_connection_failed']; } $http_code = wp_remote_retrieve_response_code($response); if ($http_code !== 200) { return ['error_code' => 'err_server_error']; } $body_response = wp_remote_retrieve_body($response); if (empty($body_response)) { return ['error_code' => 'err_content_empty']; } $data = json_decode($body_response, true); if (!is_array($data) || json_last_error() !== JSON_ERROR_NONE) { return ['error_code' => 'err_server_error']; } if (empty($data['meta_description'])) { return ['error_code' => $data['error_code'] ?? 'err_content_empty']; } return ['meta_description' => trim($data['meta_description'])]; }