various improvements to make release 1 complete
This commit is contained in:
@@ -84,18 +84,10 @@ class Swi_Foot_REST {
|
||||
)
|
||||
));
|
||||
|
||||
// Also allow team_id as a path parameter: /wp-json/swi-foot/v1/matches/123
|
||||
register_rest_route('swi-foot/v1', '/matches/(?P<team_id>\d+)', array(
|
||||
register_rest_route('swi-foot/v1', '/block-status/(?P<match_id>\d+)/(?P<endpoint>[a-z_]+)', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array($this, 'get_matches_for_team'),
|
||||
'permission_callback' => '__return_true',
|
||||
'args' => array(
|
||||
'team_id' => array(
|
||||
'validate_callback' => function($param, $request, $key) {
|
||||
return is_numeric($param);
|
||||
}
|
||||
)
|
||||
)
|
||||
'callback' => array($this, 'check_block_status'),
|
||||
'permission_callback' => '__return_true'
|
||||
));
|
||||
|
||||
// Single match details: /wp-json/swi-foot/v1/match/<match_id>
|
||||
@@ -129,9 +121,9 @@ class Swi_Foot_REST {
|
||||
)
|
||||
));
|
||||
|
||||
register_rest_route('swi-foot/v1', '/commons-ids', array(
|
||||
register_rest_route('swi-foot/v1', '/common-ids', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array($this, 'get_commons_ids'),
|
||||
'callback' => array($this, 'get_common_ids'),
|
||||
'permission_callback' => '__return_true'
|
||||
));
|
||||
|
||||
@@ -210,7 +202,7 @@ class Swi_Foot_REST {
|
||||
return rest_ensure_response($normalized);
|
||||
}
|
||||
|
||||
public function get_commons_ids($request) {
|
||||
public function get_common_ids($request) {
|
||||
$api = new Swi_Foot_API();
|
||||
$commons = $api->get_commons_ids();
|
||||
if (is_wp_error($commons)) {
|
||||
@@ -370,12 +362,77 @@ class Swi_Foot_REST {
|
||||
public function admin_test_connection($request) {
|
||||
$api = new Swi_Foot_API();
|
||||
if (method_exists($api, 'test_connection')) {
|
||||
$ok = $api->test_connection();
|
||||
$result = $api->test_connection();
|
||||
} else {
|
||||
$ok = false;
|
||||
$result = array(
|
||||
'success' => false,
|
||||
'error' => 'test_connection method not found',
|
||||
'details' => 'Plugin may be incorrectly installed'
|
||||
);
|
||||
}
|
||||
|
||||
// Return response with full error details
|
||||
if (is_array($result) && isset($result['success'])) {
|
||||
if ($result['success']) {
|
||||
return rest_ensure_response(array(
|
||||
'success' => true,
|
||||
'message' => isset($result['details']) ? $result['details'] : 'Connection successful!'
|
||||
));
|
||||
} else {
|
||||
return new WP_REST_Response(array(
|
||||
'success' => false,
|
||||
'error' => isset($result['error']) ? $result['error'] : 'Connection failed',
|
||||
'details' => isset($result['details']) ? $result['details'] : 'Unknown error'
|
||||
), 400);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback for unexpected response format
|
||||
return new WP_REST_Response(array(
|
||||
'success' => false,
|
||||
'error' => 'Unexpected response from API test',
|
||||
'details' => 'Unable to determine connection status'
|
||||
), 400);
|
||||
}
|
||||
|
||||
public function check_block_status($request) {
|
||||
$match_id = $request->get_param('match_id');
|
||||
$endpoint = $request->get_param('endpoint');
|
||||
|
||||
if (empty($match_id) || empty($endpoint)) {
|
||||
return rest_ensure_response(array('status' => 'error', 'message' => 'Missing parameters'));
|
||||
}
|
||||
|
||||
$api = new Swi_Foot_API();
|
||||
|
||||
// Map endpoint names to API methods
|
||||
$method_map = array(
|
||||
'roster' => 'get_match_players',
|
||||
'bench' => 'get_match_bench',
|
||||
'referees' => 'get_match_referees',
|
||||
'events' => 'get_match_events'
|
||||
);
|
||||
|
||||
if (!isset($method_map[$endpoint])) {
|
||||
return rest_ensure_response(array('status' => 'error', 'message' => __('Unknown endpoint', 'swi_foot_matchdata')));
|
||||
}
|
||||
|
||||
$method = $method_map[$endpoint];
|
||||
$result = $api->$method($match_id);
|
||||
|
||||
// Determine status based on result
|
||||
if (is_wp_error($result)) {
|
||||
$code = $result->get_error_code();
|
||||
if ($code === 'data_not_available') {
|
||||
return rest_ensure_response(array('status' => 'warning', 'message' => __('Data not yet available', 'swi_foot_matchdata')));
|
||||
} else {
|
||||
return rest_ensure_response(array('status' => 'error', 'message' => __('No data available', 'swi_foot_matchdata')));
|
||||
}
|
||||
} elseif (empty($result)) {
|
||||
return rest_ensure_response(array('status' => 'error', 'message' => __('No data available', 'swi_foot_matchdata')));
|
||||
} else {
|
||||
return rest_ensure_response(array('status' => 'success', 'message' => __('Data available', 'swi_foot_matchdata')));
|
||||
}
|
||||
if ($ok) return rest_ensure_response(array('success' => true));
|
||||
return new WP_REST_Response(array('error' => 'Connection failed'), 400);
|
||||
}
|
||||
|
||||
public function admin_delete_finished_match($request) {
|
||||
|
||||
Reference in New Issue
Block a user