Skip to content

Instantly share code, notes, and snippets.

@ViewFromTheBox
Created December 21, 2017 01:55
Show Gist options
  • Select an option

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

Select an option

Save ViewFromTheBox/20391ec862e4615d51b5cc6d770cb71a to your computer and use it in GitHub Desktop.
<?php
class Sports_Bench_Team_REST_Controller extends WP_REST_Controller {
/**
* Update one item from the collection
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
$item = $this->prepare_item_for_database( $request );
if ( function_exists( 'sports_bench_rest_update_team') ) {
$data = sports_bench_rest_update_team( $item );
if ( is_array( $data ) ) {
return new WP_REST_Response( $data, 200 );
} else {
return $data;
}
}
return new WP_Error( 'cant-update', __( 'message', 'text-domain'), array( 'status' => 500 ) );
}
}
function sports_bench_rest_update_team( $item ) {
global $wpdb;
$table_name = $wpdb->prefix . 'sb_teams';
$team_id = $item[ 'team_id' ];
$slug_test = $wpdb->get_results( "SELECT * FROM $table_name WHERE team_id = $team_id" );
if ( is_array( $slug_test ) ) {
$result = $wpdb->update( $table_name, $item, array ( 'team_id' => $item[ 'team_id' ] ) );
if ( $result ) {
return $item;
} else {
return new WP_Error( 'error_team_update', __( 'There was an error updating the team. Please check your data and try again.', 'sports-bench' ), array ( 'status' => 500 ) );
}
} else {
return new WP_Error( 'error_team_update', __( 'This team does not exist. Try adding the team first.', 'sports-bench' ), array ( 'status' => 500 ) );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment