Created
December 21, 2017 01:55
-
-
Save ViewFromTheBox/20391ec862e4615d51b5cc6d770cb71a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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