Skip to content

Instantly share code, notes, and snippets.

@ViewFromTheBox
Last active December 21, 2017 01:52
Show Gist options
  • Select an option

  • Save ViewFromTheBox/f9a390bc4b82fa84bd33bb562307b61a to your computer and use it in GitHub Desktop.

Select an option

Save ViewFromTheBox/f9a390bc4b82fa84bd33bb562307b61a to your computer and use it in GitHub Desktop.
<?php
class Sports_Bench_Team_REST_Controller extends WP_REST_Controller {
/**
* Get a collection of items
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$params = $request->get_params();
$items = sports_bench_rest_get_teams( $params ); //do a query, call another class, etc
$data = array();
foreach( $items as $item ) {
$itemdata = $this->prepare_item_for_response( $item, $request );
$data[] = $this->prepare_response_for_collection( $itemdata );
}
return new WP_REST_Response( $data, 200 );
}
/**
* Get one item from the collection
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
//get parameters from request
$params = $request->get_params();
$item = sports_bench_rest_get_team( $params[ 'id' ] );//do a query, call another class, etc
$data = $this->prepare_item_for_response( $item, $request );
//return a response or error based on some conditional
if ( 1 == 1 ) {
return new WP_REST_Response( $data, 200 );
}else{
return new WP_Error( 'code', __( 'message', 'text-domain' ) );
}
}
}
/**
* Takes the REST URL and returns an array of the results
*
* @param array $params
*
* @return array, array of the SQL results
*
* @since 1.1
*/
function sports_bench_rest_get_teams( $params ) {
$response = '';
if ( ( isset( $params[ 'team_id' ] ) && $params[ 'team_id' ] != null ) or ( isset( $params[ 'team_name' ] ) && $params[ 'team_name' ] != null ) or ( isset( $params[ 'team_location' ] ) && $params[ 'team_location' ] != null ) or ( isset( $params[ 'team_nickname' ] ) && $params[ 'team_nickname' ] != null ) or ( isset( $params[ 'team_abbreviation' ] ) && $params[ 'team_abbreviation' ] != null ) or ( isset( $params[ 'team_active' ] ) && $params[ 'team_active' ] != null ) or ( isset( $params[ 'team_city' ] ) && $params[ 'team_city' ] != null ) or ( isset( $params[ 'team_state' ] ) && $params[ 'team_state' ] != null ) or ( isset( $params[ 'team_head_coach' ] ) && $params[ 'team_head_coach' ] != null ) or ( isset( $params[ 'team_division' ] ) && $params[ 'team_division' ] != null ) or ( isset( $params[ 'team_slug' ] ) && $params[ 'team_slug' ] != null ) ) {
$and = false;
$search = '';
if ( $params[ 'team_id' ] != null ) {
$search .= 'team_id in (' . $params[ 'team_id' ] . ')';
$and = true;
} if ( isset( $params[ 'team_name' ] ) && $params[ 'team_name' ] != null ) {
if ( $and == true ) {
$prefix = ' AND ';
} else {
$prefix = '';
}
$search .= $prefix . 'team_name in ( "' . $params[ 'team_name' ] . '" )';
$and = true;
} if ( isset( $params[ 'team_location' ] ) && $params[ 'team_location' ] != null ) {
if ( $and == true ) {
$prefix = ' AND ';
} else {
$prefix = '';
}
$search .= $prefix . 'team_location in ( "' . $params[ 'team_location' ] . '" )';
$and = true;
} if ( isset( $params[ 'team_nickname' ] ) && $params[ 'team_nickname' ] != null ) {
if ( $and == true ) {
$prefix = ' AND ';
} else {
$prefix = '';
}
$search .= $prefix . 'team_nickname in ( "' . $params[ 'team_nickname' ] . '" )';
$and = true;
} if ( isset( $params[ 'team_abbreviation' ] ) && $params[ 'team_abbreviation' ] != null ) {
if ( $and == true ) {
$prefix = ' AND ';
} else {
$prefix = '';
}
$search .= $prefix . 'team_abbreviation in ( "' . $params[ 'team_abbreviation' ] . '" )';
$and = true;
} if ( isset( $params[ 'team_active' ] ) && $params[ 'team_active' ] != null ) {
if ( $and == true ) {
$prefix = ' AND ';
} else {
$prefix = '';
}
$search .= $prefix . 'team_active LIKE "' . $params[ 'team_active' ] . '"';
$and = true;
} if ( isset( $params[ 'team_city' ] ) && $params[ 'team_city' ] != null ) {
if ( $and == true ) {
$prefix = ' AND ';
} else {
$prefix = '';
}
$search .= $prefix . 'team_city in ( "' . $params[ 'team_city' ] . '" )';
$and = true;
} if ( isset( $params[ 'team_state' ] ) && $params[ 'team_state' ] != null ) {
if ( $and == true ) {
$prefix = ' AND ';
} else {
$prefix = '';
}
$search .= $prefix . 'team_state in ( "' . $params[ 'team_state' ] . '" )';
$and = true;
} if ( isset( $params[ 'team_division' ] ) && $params[ 'team_division' ] != null ) {
if ( $and == true ) {
$prefix = ' AND ';
} else {
$prefix = '';
}
$search .= $prefix . 'team_division in ( "' . $params[ 'team_division' ] . '" )';
$and = true;
} if ( isset( $params[ 'team_slug' ] ) && $params[ 'team_slug' ] != null ) {
if ( $and == true ) {
$prefix = ' AND ';
} else {
$prefix = '';
}
$search .= $prefix . 'team_slug in ( "' . $params[ 'team_slug' ] . '" )';
}
global $wpdb;
$table = $wpdb->prefix . 'sb_teams';
$querystr = "SELECT * FROM $table WHERE $search;";
$teams = $wpdb->get_results( $querystr );
$team_list = [];
foreach( $teams as $team ) {
$team = new Sports_Bench_Team( (int) $team->team_id );
$return_team = array (
'team_id' => $team->team_id,
'team_name' => $team->team_name,
'team_location' => $team->team_location,
'team_nickname' => $team->team_nickname,
'team_abbreviation' => $team->team_abbreviation,
'team_active' => $team->team_active,
'team_city' => $team->team_city,
'team_state' => $team->team_state,
'team_stadium' => $team->team_stadium,
'team_stadium_capacity' => $team->team_stadium_capacity,
'team_location_line_one' => $team->team_location_line_one,
'team_location_line_two' => $team->team_location_line_two,
'team_location_country' => $team->team_location_country,
'team_location_zip_code' => $team->team_location_zip_code,
'team_head_coach' => $team->team_head_coach,
'team_logo' => $team->team_logo,
'team_photo' => $team->team_photo,
'team_division' => $team->team_division,
'team_primary_color' => $team->team_primary_color,
'team_secondary_color' => $team->team_secondary_color,
'team_slug' => $team->team_slug,
);
array_push( $team_list, $return_team);
}
$response = $team_list;
} else {
$teams = sports_bench_get_teams();
$team_list = [];
foreach ( $teams as $key => $label ) {
$the_team = new Sports_Bench_Team( (int) $key );
$team_info = array (
'team_id' => $the_team->team_id,
'team_name' => $the_team->team_name,
'team_location' => $the_team->team_location,
'team_nickname' => $the_team->team_nickname,
'team_abbreviation' => $the_team->team_abbreviation,
'team_city' => $the_team->team_city,
'team_state' => $the_team->team_state,
'team_location_line_one' => $the_team->team_location_line_one,
'team_location_line_two' => $the_team->team_location_line_two,
'team_location_country' => $the_team->team_location_country,
'team_location_zip_code' => $the_team->team_location_zip_code,
'team_stadium' => $the_team->team_stadium,
'team_stadium_capacity' => $the_team->team_stadium_capacity,
'team_head_coach' => $the_team->team_head_coach,
'team_logo' => $the_team->team_logo,
'team_photo' => $the_team->team_photo,
'team_division' => $the_team->team_division,
'team_primary_color' => $the_team->team_primary_color,
'team_secondary_color' => $the_team->team_secondary_color,
'team_slug' => $the_team->team_slug,
'team_active' => $the_team->team_active,
);
array_push( $team_list, $team_info );
}
$response = $team_list;
}
//print_r( $response );
return $response;
}
/**
* Returns an array of information for a team
*
* @param int $team_id
*
* @return array, information for a team
*
* @since 1.4
*/
function sports_bench_rest_get_team( $team_id ) {
$the_team = new Sports_Bench_Team( (int) $team_id );
$team_info = array (
'team_id' => $the_team->team_id,
'team_name' => $the_team->team_name,
'team_location' => $the_team->team_location,
'team_nickname' => $the_team->team_nickname,
'team_abbreviation' => $the_team->team_abbreviation,
'team_city' => $the_team->team_city,
'team_state' => $the_team->team_state,
'team_location_line_one' => $the_team->team_location_line_one,
'team_location_line_two' => $the_team->team_location_line_two,
'team_location_country' => $the_team->team_location_country,
'team_location_zip_code' => $the_team->team_location_zip_code,
'team_stadium' => $the_team->team_stadium,
'team_stadium_capacity' => $the_team->team_stadium_capacity,
'team_head_coach' => $the_team->team_head_coach,
'team_logo' => $the_team->team_logo,
'team_photo' => $the_team->team_photo,
'team_division' => $the_team->team_division,
'team_primary_color' => $the_team->team_primary_color,
'team_secondary_color' => $the_team->team_secondary_color,
'team_slug' => $the_team->team_slug,
'team_active' => $the_team->team_active,
);
return $team_info;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment