Created
January 28, 2017 16:25
-
-
Save Idealien/f304939bc3af036ef4c563975c72a741 to your computer and use it in GitHub Desktop.
This file contains 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 | |
add_action( 'rest_api_init', 'rest_register_game_meta' ); | |
function rest_register_game_meta() { | |
//For generic fields that should never allow front-end updates | |
$restArgsDisplay = array( | |
'get_callback' => 'rest_get_game_meta', | |
'update_callback' => null, | |
'schema' => null, | |
); | |
//For generic fields that can be update in all cases. | |
$restArgsEditGameMeta = array( | |
'get_callback' => 'rest_get_game_meta', | |
'update_callback' => 'rest_update_game_meta', | |
'schema' => null, | |
); | |
register_rest_field( 'game', 'game_date', $restArgsDisplay ); | |
register_rest_field( 'game', 'game_location', $restArgsDisplay ); | |
register_rest_field( 'game', 'game_center_score', $restArgsEditGameMeta ); | |
register_rest_field( 'game', 'game_center_assists', $restArgsEditGameMeta ); | |
register_rest_field( 'game', 'game_shootguard_score', $restArgsEditGameMeta ); | |
register_rest_field( 'game', 'game_shootguard_assists', $restArgsEditGameMeta ); | |
register_rest_field( 'game', 'game_pointguard_score', $restArgsEditGameMeta ); | |
register_rest_field( 'game', 'game_pointguard_assists', $restArgsEditGameMeta ); | |
register_rest_field( 'game', 'game_powerforward_score', $restArgsEditGameMeta ); | |
register_rest_field( 'game', 'game_powerforward_assists', $restArgsEditGameMeta ); | |
register_rest_field( 'game', 'game_smallforward_score', $restArgsEditGameMeta ); | |
register_rest_field( 'game', 'game_smallforward_assists', $restArgsEditGameMeta ); | |
} | |
//Used in register_post_meta callbacks to retrieve meta data | |
function rest_get_game_meta( $object, $field_name, $request ) { | |
return get_post_meta( $object[ 'id' ], $field_name ); | |
} | |
//Ensure score / assist are sent in right format and resource before storing to meta field | |
function rest_update_game_meta( $value, $object, $field_name ) { | |
//Validate basic format | |
if ( ! is_string( $value ) ) { | |
return new WP_Error( 'rest_count_invalid', esc_html__( 'The score/assist provided does not match format criteria', 'game' ), array( 'status' => 406 ) );; | |
} | |
//Validate access level | |
if ( current_user_can('volunteer') ) { | |
//Additional logic to confirm volunteer is assigned to this game, otherwise | |
return new WP_Error( 'rest_access_invalid', esc_html__( 'Resource is not assigned to edit requested data', 'game' ), array( 'status' => 403 ) ); | |
} | |
//Validate score / assist is not LBJ level | |
$number = (int) abs( strip_tags( $value ) ); | |
if( $number < 0 || $number > 150 ) { | |
return new WP_Error( 'rest_count_invalid', esc_html__( 'The count provided does not match format criteria', 'game' ), array( 'status' => 406 ) ); | |
} | |
return update_post_meta( $object->ID, $field_name, $number ); | |
} | |
//Default example update_post_meta that has only basic field validation | |
function rest_update_any_meta( $value, $object, $field_name ) { | |
if ( ! $value || ! is_string( $value ) ) { | |
return; | |
} | |
return update_post_meta( $object->ID, $field_name, strip_tags( $value ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment