/* ===== /games (مبسطة وموثوقة) ===== */ function flx_api_games( WP_REST_Request $r ) { global $wpdb; $p = $wpdb->prefix; if ( ! flx_tbl_exists( "{$p}anwpfl_matches" ) ) { return new WP_Error( 'no_table', 'Table anwpfl_matches not found', ['status'=>500] ); } list($page,$per_page,$offset) = flx_pagination_args($r); $where = []; $params = []; // الفلاتر الأساسية if ( $comp = absint( $r->get_param('comp_id') ) ) { $where[] = "m.competition_id = %d"; $params[] = $comp; } if ( $team = absint( $r->get_param('team_id') ) ) { $where[] = "(m.home_club = %d OR m.away_club = %d)"; $params[] = $team; $params[] = $team; } // finished: 0/1 if ( null !== $r->get_param('finished') ) { $finished = (int) $r->get_param('finished'); $where[] = "m.finished = %d"; $params[] = $finished; } $where_sql = $where ? 'WHERE ' . implode(' AND ', $where) : ''; // العدّ $sql_count = "SELECT COUNT(*) FROM {$p}anwpfl_matches m $where_sql"; if ($params) { $total = (int) $wpdb->get_var( $wpdb->prepare( $sql_count, $params ) ); } else { $total = (int) $wpdb->get_var( $sql_count ); } // البيانات $sql = "SELECT m.* FROM {$p}anwpfl_matches m $where_sql ORDER BY m.kickoff DESC LIMIT %d OFFSET %d"; $params_pag = $params; $params_pag[] = $per_page; $params_pag[] = $offset; $rows = $wpdb->get_results( $wpdb->prepare( $sql, $params_pag ) ); // ⚠️ إرجاع البيانات كما هي بدون إثراء (للتجربة) return flx_json_response( $rows, [ 'page' => $page, 'per_page' => $per_page, 'total' => $total, 'total_pages' => (int) ceil($total / max(1, $per_page)), 'note' => 'بيانات خام من جدول المباريات' ]); } /* ===== /games/facets (مبسطة) ===== */ add_action( 'rest_api_init', function(){ register_rest_route( 'fl', '/games/facets', [ 'methods' => 'GET', 'permission_callback' => '__return_true', 'callback' => function( WP_REST_Request $r ){ global $wpdb; $p = $wpdb->prefix; if ( ! flx_tbl_exists( "{$p}anwpfl_matches" ) ) { return new WP_Error( 'no_table', 'Table anwpfl_matches not found', ['status'=>500] ); } // البيانات الأساسية فقط $comps = $wpdb->get_col( "SELECT DISTINCT competition_id FROM {$p}anwpfl_matches WHERE competition_id IS NOT NULL AND competition_id != '' ORDER BY competition_id ASC LIMIT 50" ); $seasons = $wpdb->get_col( "SELECT DISTINCT season_id FROM {$p}anwpfl_matches WHERE season_id IS NOT NULL AND season_id != '' ORDER BY season_id DESC LIMIT 20" ); $homes = $wpdb->get_col( "SELECT DISTINCT home_club FROM {$p}anwpfl_matches WHERE home_club IS NOT NULL AND home_club != '' ORDER BY home_club ASC LIMIT 50" ); $aways = $wpdb->get_col( "SELECT DISTINCT away_club FROM {$p}anwpfl_matches WHERE away_club IS NOT NULL AND away_club != '' ORDER BY away_club ASC LIMIT 50" ); return flx_json_response( [ 'competition_ids' => array_map('intval', $comps), 'season_ids' => array_map('intval', $seasons), 'home_club_ids' => array_map('intval', $homes), 'away_club_ids' => array_map('intval', $aways), 'note' => 'معرفات فقط - تحتاج لإثراء بالأسماء' ]); } ]); }); /* ===== /games/raw (بيانات خام للتجربة) ===== */ add_action( 'rest_api_init', function(){ register_rest_route( 'fl', '/games/raw', [ 'methods' => 'GET', 'permission_callback' => '__return_true', 'callback' => function( WP_REST_Request $r ){ global $wpdb; $p = $wpdb->prefix; if ( ! flx_tbl_exists( "{$p}anwpfl_matches" ) ) { return new WP_Error( 'no_table', 'Table anwpfl_matches not found', ['status'=>500] ); } // أول 10 مباريات فقط للتجربة $matches = $wpdb->get_results( "SELECT * FROM {$p}anwpfl_matches ORDER BY match_id DESC LIMIT 10" ); return [ 'success' => true, 'count' => count($matches), 'data' => $matches, 'note' => 'أول 10 مباريات للفحص' ]; } ]); }); /* ===== الدوال المساعدة ===== */ function flx_tbl_exists( $table ) { global $wpdb; return $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table ) ) === $table; } function flx_pagination_args( WP_REST_Request $r ) { $page = max( 1, (int) $r->get_param('page') ); $per_page = max( 1, min( 100, (int) $r->get_param('per_page') ?: 20 ) ); $offset = ( $page - 1 ) * $per_page; return [ $page, $per_page, $offset ]; } function flx_json_response( $data, $extra = [] ) { return new WP_REST_Response( array_merge( [ 'success' => true, 'data' => $data ], $extra ) ); }