Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save ViewFromTheBox/41c53819c70e4a9a60854afdf7f08f0a to your computer and use it in GitHub Desktop.
Some of the basic function definitions for the WP REST API Controller
<?php
protected function prepare_item_for_database( $request ) {
global $wpdb;
$table_name = $wpdb->prefix . 'sb_teams';
if ( isset( $request[ 'team_id' ] ) ) {
$team_id = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_id' ] ) );
} elseif ( isset( $request[ 'id' ] ) ) {
$team_id = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'id' ] ) );
} else {
$team_id = '';
}
if ( isset( $request[ 'team_name' ] ) ) {
$team_name = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_name' ] ) );
} else {
$team_name = '';
}
if ( isset( $request[ 'team_location' ] ) ) {
$team_location = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_location' ] ) );
} else {
$team_location = '';
}
if ( isset( $request[ 'team_nickname' ] ) ) {
$team_nickname = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_nickname' ] ) );
} else {
$team_nickname = '';
}
if ( isset( $request[ 'team_abbreviation' ] ) ) {
$team_abbreviation = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_abbreviation' ] ) );
} else {
$team_abbreviation = '';
}
if ( isset( $request[ 'team_active' ] ) ) {
$team_active = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_active' ] ) );
} else {
$team_active = '';
}
if ( isset( $request[ 'team_city' ] ) ) {
$team_city = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_city' ] ) );
} else {
$team_city = '';
}
if ( isset( $request[ 'team_state' ] ) ) {
$team_state = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_state' ] ) );
} else {
$team_state = '';
}
if ( isset( $request[ 'team_stadium' ] ) ) {
$team_stadium = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_stadium' ] ) );
} else {
$team_stadium = '';
}
if ( isset( $request[ 'team_location_line_one' ] ) ) {
$team_location_line_one = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_location_line_one' ] ) );
} else {
$team_location_line_one = '';
}
if ( isset( $request[ 'team_location_line_two' ] ) ) {
$team_location_line_two = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_location_line_two' ] ) );
} else {
$team_location_line_two = '';
}
if ( isset( $request[ 'team_location_country' ] ) ) {
$team_location_country = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_location_country' ] ) );
} else {
$team_location_country = '';
}
if ( isset( $request[ 'team_location_zip_code' ] ) ) {
$team_location_zip_code = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_location_zip_code' ] ) );
} else {
$team_location_zip_code = '';
}
if ( isset( $request[ 'team_stadium_capacity' ] ) ) {
$team_stadium_capacity = intval( $request[ 'team_stadium_capacity' ] );
} else {
$team_stadium_capacity = '';
}
if ( isset( $request[ 'team_head_coach' ] ) ) {
$team_head_coach = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_head_coach' ] ) );
} else {
$team_head_coach = '';
}
if ( isset( $request[ 'team_division' ] ) ) {
$team_division = intval( $request[ 'team_division' ] );
} else {
$team_division = '';
}
if ( isset( $request[ 'team_primary_color' ] ) ) {
$team_primary_color = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_primary_color' ] ) );
} else {
$team_primary_color = '';
}
if ( isset( $request[ 'team_secondary_color' ] ) ) {
$team_secondary_color = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_secondary_color' ] ) );
} else {
$team_secondary_color = '';
}
if ( isset( $request[ 'team_logo' ] ) ) {
$team_logo = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_logo' ] ) );
} else {
$team_logo = '';
}
if ( isset( $request[ 'team_photo' ] ) ) {
$team_photo = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_photo' ] ) );
} else {
$team_photo = '';
}
if ( isset( $request[ 'team_slug' ] ) ) {
$team_slug = wp_filter_nohtml_kses( sanitize_text_field( $request[ 'team_slug' ] ) );
} else {
if ( isset( $request[ 'team_name' ] ) ) {
$slug = strtolower( $request[ 'team_name' ] );
$request[ 'team_slug' ] = $string = preg_replace( "/[\s_]/", "-", $slug );
$slug = '"' . $request[ 'team_slug' ] . '"';
$slug_test = $wpdb->get_results( "SELECT * FROM $table_name WHERE team_slug = $slug" );
$i = 1;
while ( $slug_test !== [] and $slug_test[ 0 ]->team_id !== $request[ 'team_id' ] ) {
$i++;
$slug = '"' . $request[ 'team_slug' ] . '-' . $i . '"';
$slug_test = $wpdb->get_results( "SELECT * FROM $table_name WHERE team_slug = $slug" );
if ( $slug_test == [] ) {
$request[ 'team_slug' ] = $request[ 'team_slug' ] . '-' . $i;
break;
}
}
$team_slug = $request[ 'team_slug' ];
} else {
$team_slug = '';
}
}
$item = array(
'team_id' => $team_id,
'team_name' => $team_name,
'team_location' => $team_location,
'team_nickname' => $team_nickname,
'team_abbreviation' => $team_abbreviation,
'team_active' => $team_active,
'team_city' => $team_city,
'team_state' => $team_state,
'team_stadium' => $team_stadium,
'team_location_line_one' => $team_location_line_one,
'team_location_line_two' => $team_location_line_two,
'team_location_country' => $team_location_country,
'team_location_zip_code' => $team_location_zip_code,
'team_stadium_capacity' => $team_stadium_capacity,
'team_head_coach' => $team_head_coach,
'team_division' => $team_division,
'team_primary_color' => $team_primary_color,
'team_secondary_color' => $team_secondary_color,
'team_logo' => $team_logo,
'team_photo' => $team_photo,
'team_slug' => $team_slug,
);
return $item;
}
/**
* Prepare the item for the REST response
*
* @param mixed $item WordPress representation of the item.
* @param WP_REST_Request $request Request object.
* @return mixed
*/
public function prepare_item_for_response( $item, $request ) {
$schema = $this->get_item_schema();
$data = array();
$data = $item;
$team = new Sports_Bench_Team( (int)$item[ 'team_id' ] );
$data[ 'team_link' ] = $team->get_permalink();
$data[ 'team_link' ] = str_replace( '&#038;', '&', $data[ 'team_link' ] );
return $data;
}
/**
* Get the query params for collections
*
* @return array
*/
public function get_collection_params() {
return array(
'team_id' => array(
'description' => 'The id(s) for the team(s) in the search.',
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
),
'team_name' => array(
'description' => 'The name(s) the team(s) in the search',
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
),
'team_location' => array(
'description' => 'The location(s) for the team(s) in the search',
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
),
'team_nickname' => array(
'description' => 'The nickname(s) for the team(s) in the search',
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
),
'team_abbreviation' => array(
'description' => 'The abbreviation(s) for the team(s) in the search',
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
),
'team_city' => array(
'description' => 'The team_city for the team(s) in the search',
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
),
'team_state' => array(
'description' => 'The state(s) for the team(s) in the search',
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
),
'team_head_coach' => array(
'description' => 'The head coach(es) for the team(s) in the search',
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
),
'team_division' => array(
'description' => 'The id(s) division(s) for the team(s) in the search',
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
),
'team_slug' => array(
'description' => 'The slug(s) for the team(s) in the search',
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
),
'team_active' => array(
'description' => 'Whether or not the team(s) are active',
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
),
);
}
/**
* Get the Entry schema, conforming to JSON Schema.
*
* @since 2.0-beta-1
* @access public
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'entry',
'type' => 'object',
'properties' => array(
'team_id' => array(
'description' => __( 'The id for the team.', 'sports-bench' ),
'type' => 'integer',
'readonly' => true,
),
),
);
return $schema;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment