public static function resend() { if (!current_user_can('manage_options')) { wp_die( esc_html__('ليس لديك صلاحية لإعادة إرسال رسالة التقييم.', 'hjwa'), esc_html__('غير مسموح', 'hjwa'), ['response' => 403] ); } $order_id = isset($_GET['order_id']) ? absint($_GET['order_id']) : 0; $nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : ''; if ( !$order_id || !wp_verify_nonce( $nonce, 'hjwa_resend_product_review_' . $order_id ) ) { self::redirect_with_resend_error( 'طلب إعادة الإرسال غير صالح أو انتهت صلاحية الرابط.' ); } if (!function_exists('wc_get_order')) { self::redirect_with_resend_error( 'WooCommerce غير متاح حاليًا.' ); } $order = wc_get_order($order_id); if (!$order) { self::redirect_with_resend_error( 'لم يتم العثور على الطلب المطلوب.' ); } if (get_post_meta($order_id, '_hjwa_review_submitted', true)) { self::redirect_with_resend_error( 'تم تقييم هذا الطلب بالفعل، لذلك لن يتم إرسال طلب تقييم جديد.' ); } if (!class_exists('HJWA_Product_Review_Sender')) { self::redirect_with_resend_error( 'كلاس HJWA_Product_Review_Sender غير موجود أو لم يتم تحميله.' ); } $lock_key = '_hjwa_review_manual_resend_lock'; $locked = get_post_meta($order_id, $lock_key, true); if ($locked) { self::redirect_with_resend_error( 'توجد عملية إرسال جارية لهذا الطلب. حاول مرة أخرى بعد قليل.' ); } update_post_meta( $order_id, $lock_key, current_time('timestamp') ); delete_post_meta($order_id, '_hjwa_review_last_error'); delete_post_meta($order_id, '_hjwa_review_sent'); delete_post_meta($order_id, '_hjwa_review_sent_at'); delete_post_meta($order_id, '_hjwa_review_wa_message_id'); try { $result = HJWA_Product_Review_Sender::send($order_id); } catch (Throwable $e) { delete_post_meta($order_id, $lock_key); update_post_meta( $order_id, '_hjwa_review_last_error', $e->getMessage() ); self::redirect_with_resend_error( 'حدث خطأ أثناء إعادة الإرسال: ' . $e->getMessage() ); } delete_post_meta($order_id, $lock_key); if (is_wp_error($result)) { update_post_meta( $order_id, '_hjwa_review_last_error', $result->get_error_message() ); self::redirect_with_resend_error( $result->get_error_message() ); } if ($result === false || $result === null) { update_post_meta( $order_id, '_hjwa_review_last_error', 'فشل الإرسال ولم يرجع النظام نتيجة صالحة.' ); self::redirect_with_resend_error( 'فشل إعادة إرسال رسالة التقييم.' ); } if ( is_array($result) && isset($result['success']) && !$result['success'] ) { $message = !empty($result['message']) ? sanitize_text_field((string) $result['message']) : 'فشل إعادة إرسال رسالة التقييم.'; update_post_meta( $order_id, '_hjwa_review_last_error', $message ); self::redirect_with_resend_error($message); } delete_post_meta($order_id, '_hjwa_review_last_error'); if (method_exists($order, 'add_order_note')) { $order->add_order_note( 'تمت إعادة إرسال طلب تقييم المنتج يدويًا عبر واتساب.' ); } wp_safe_redirect( add_query_arg( [ 'page' => 'hjwa-product-review', 'pr_resend_ok' => 1, ], admin_url('admin.php') ) ); exit; } private static function redirect_with_error($message) { $message = sanitize_text_field((string) $message); wp_safe_redirect( add_query_arg( [ 'page' => 'hjwa-product-review', 'pr_test_error' => rawurlencode($message), ], admin_url('admin.php') ) ); exit; } private static function redirect_with_resend_error($message) { $message = sanitize_text_field((string) $message); wp_safe_redirect( add_query_arg( [ 'page' => 'hjwa-product-review', 'pr_resend_error' => rawurlencode($message), ], admin_url('admin.php') ) ); exit; } private static function render_template_select_fallback( $field_name, $current_value, array $templates ) { $field_name = sanitize_key($field_name); $current_value = sanitize_text_field($current_value); if ( class_exists('HJWA_Admin') && method_exists('HJWA_Admin', 'render_template_select') ) { HJWA_Admin::render_template_select( $field_name, $current_value, $templates ); return; } echo ''; } private static function get_stats() { $stats = [ 'pending' => 0, 'sent' => 0, 'submitted' => 0, 'failed' => 0, 'cancelled' => 0, 'response_rate' => 0, 'average_rating'=> 0, ]; if (!function_exists('wc_get_orders')) { return $stats; } $orders = wc_get_orders([ 'limit' => 500, 'orderby' => 'date', 'order' => 'DESC', 'return' => 'objects', ]); if (empty($orders)) { return $stats; } $ratings_sum = 0; $ratings_count = 0; foreach ($orders as $order) { if (!$order instanceof WC_Order) { continue; } $order_id = $order->get_id(); $scheduled = (bool) $order->get_meta( '_hjwa_review_scheduled', true ); $sent = (bool) $order->get_meta( '_hjwa_review_sent', true ); $submitted = (bool) $order->get_meta( '_hjwa_review_submitted', true ); $last_error = trim( (string) $order->get_meta( '_hjwa_review_last_error', true ) ); $rating = absint( $order->get_meta( '_hjwa_review_rating', true ) ); $status = $order->get_status(); if ($submitted) { $stats['submitted']++; if ($rating >= 1 && $rating <= 5) { $ratings_sum += $rating; $ratings_count++; } continue; } if ($last_error !== '') { $stats['failed']++; continue; } if ( in_array( $status, ['cancelled', 'failed', 'refunded'], true ) && $scheduled ) { $stats['cancelled']++; continue; } if ($sent) { $stats['sent']++; continue; } if ($scheduled) { $stats['pending']++; } } $total_sent = $stats['sent'] + $stats['submitted']; if ($total_sent > 0) { $stats['response_rate'] = round( ($stats['submitted'] / $total_sent) * 100, 1 ); } if ($ratings_count > 0) { $stats['average_rating'] = round( $ratings_sum / $ratings_count, 2 ); } return $stats; } private static function render_stats_cards($stats) { $cards = [ [ 'label' => 'في الانتظار', 'value' => (int) ($stats['pending'] ?? 0), 'icon' => '⏳', 'class' => 'pending', ], [ 'label' => 'تم الإرسال', 'value' => (int) ($stats['sent'] ?? 0), 'icon' => '📨', 'class' => 'sent', ], [ 'label' => 'تم التقييم', 'value' => (int) ($stats['submitted'] ?? 0), 'icon' => '⭐', 'class' => 'submitted', ], [ 'label' => 'فشل الإرسال', 'value' => (int) ($stats['failed'] ?? 0), 'icon' => '⚠️', 'class' => 'failed', ], [ 'label' => 'طلبات ملغاة', 'value' => (int) ($stats['cancelled'] ?? 0), 'icon' => '🚫', 'class' => 'cancelled', ], [ 'label' => 'نسبة الاستجابة', 'value' => number_format( (float) ($stats['response_rate'] ?? 0), 1 ) . '%', 'icon' => '📈', 'class' => 'response', ], [ 'label' => 'متوسط التقييم', 'value' => number_format( (float) ($stats['average_rating'] ?? 0), 2 ) . ' / 5', 'icon' => '🏆', 'class' => 'rating', ], ]; echo ''; echo '
'; foreach ($cards as $card) { echo '
'; echo '
' . esc_html($card['icon']) . '
'; echo '
' . esc_html($card['label']) . '
'; echo '
' . esc_html($card['value']) . '
'; echo '
'; } echo '
'; } private static function get_tracking_rows() { $rows = []; if (!function_exists('wc_get_orders')) { return $rows; } $orders = wc_get_orders([ 'limit' => 150, 'orderby' => 'date', 'order' => 'DESC', 'return' => 'objects', ]); if (empty($orders)) { return $rows; } foreach ($orders as $order) { if (!$order instanceof WC_Order) { continue; } $order_id = $order->get_id(); $scheduled = (bool) $order->get_meta( '_hjwa_review_scheduled', true ); $scheduled_at = absint( $order->get_meta( '_hjwa_review_scheduled_at', true ) ); $completed_at = absint( $order->get_meta( '_hjwa_review_completed_at', true ) ); $sent = (bool) $order->get_meta( '_hjwa_review_sent', true ); $sent_at = absint( $order->get_meta( '_hjwa_review_sent_at', true ) ); $submitted = (bool) $order->get_meta( '_hjwa_review_submitted', true ); $submitted_at = absint( $order->get_meta( '_hjwa_review_submitted_at', true ) ); $rating = absint( $order->get_meta( '_hjwa_review_rating', true ) ); $last_error = trim( (string) $order->get_meta( '_hjwa_review_last_error', true ) ); if ( !$scheduled && !$sent && !$submitted && $last_error === '' ) { continue; } $customer = trim( (string) $order->get_formatted_billing_full_name() ); if ($customer === '') { $customer = trim( (string) $order->get_billing_first_name() ); } if ($customer === '') { $customer = '—'; } $rows[] = [ 'order_id' => $order_id, 'order_url' => self::get_order_edit_url($order), 'customer' => $customer, 'phone' => (string) $order->get_billing_phone(), 'products' => self::get_products_text($order), 'completed_at' => self::format_ts($completed_at), 'scheduled_at' => self::format_ts($scheduled_at), 'status_badge' => self::build_status_badge( $order, $scheduled, $sent, $submitted, $last_error ), 'sent_at' => self::format_ts($sent_at), 'submitted_at' => self::format_ts($submitted_at), 'last_error' => $last_error !== '' ? $last_error : '—', 'rating' => self::format_rating($rating), 'actions' => self::build_row_actions( $order_id, $submitted, $sent ), ]; } return $rows; } private static function build_row_actions( $order_id, $submitted, $sent ) { $actions = []; $order_id = absint($order_id); if (!$submitted) { $url = wp_nonce_url( add_query_arg( [ 'action' => 'hjwa_resend_product_review', 'order_id' => $order_id, ], admin_url('admin-post.php') ), 'hjwa_resend_product_review_' . $order_id ); $label = $sent ? 'إعادة الإرسال' : 'إرسال الآن'; $actions[] = '' . esc_html($label) . ''; } else { $actions[] = '' . '✓ تم التقييم' . ''; } return implode(' ', $actions); } private static function get_products_text($order) { $names = []; foreach ($order->get_items('line_item') as $item) { if (!$item instanceof WC_Order_Item_Product) { continue; } $name = trim((string) $item->get_name()); if ($name === '') { continue; } $quantity = max(1, (int) $item->get_quantity()); if ($quantity > 1) { $name .= ' × ' . $quantity; } $names[] = $name; } $names = array_values(array_unique($names)); return !empty($names) ? implode('، ', $names) : '—'; } private static function build_status_badge( $order, $scheduled, $sent, $submitted, $last_error ) { $status = $order instanceof WC_Order ? $order->get_status() : ''; if ($submitted) { return self::badge( 'تم التقييم', '#d1fae5', '#065f46' ); } if ($last_error !== '') { return self::badge( 'فشل الإرسال', '#fee2e2', '#991b1b' ); } if ( in_array( $status, ['cancelled', 'failed', 'refunded'], true ) && $scheduled ) { return self::badge( 'تم الإلغاء', '#f1f5f9', '#475569' ); } if ($sent) { return self::badge( 'تم الإرسال', '#dbeafe', '#1d4ed8' ); } if ($scheduled) { return self::badge( 'في الانتظار', '#fef3c7', '#92400e' ); } return self::badge( 'غير معروف', '#f1f5f9', '#475569' ); } private static function badge( $label, $background, $color ) { return '' . esc_html($label) . ''; } private static function format_rating($rating) { $rating = absint($rating); if ($rating < 1 || $rating > 5) { return '—'; } return str_repeat('★', $rating) . str_repeat('☆', 5 - $rating) . ' (' . $rating . '/5)'; } private static function get_order_edit_url($order) { if (!$order instanceof WC_Order) { return admin_url('admin.php?page=wc-orders'); } if (method_exists($order, 'get_edit_order_url')) { $url = $order->get_edit_order_url(); if (!empty($url)) { return $url; } } return admin_url( 'post.php?post=' . absint($order->get_id()) . '&action=edit' ); } private static function format_ts($timestamp) { $timestamp = absint($timestamp); if ($timestamp <= 0) { return '—'; } return wp_date( 'Y-m-d h:i A', $timestamp, wp_timezone() ); } } https://hayahjoy.com/page-sitemap.xml 2026-03-13T22:34:57+00:00 https://hayahjoy.com/product-sitemap.xml 2026-07-07T20:15:32+00:00 https://hayahjoy.com/product_cat-sitemap.xml 2026-07-07T20:15:32+00:00