From e2aeaf4fc9c89ec7baa4baa9fc0f9599b3b2244f Mon Sep 17 00:00:00 2001 From: Kef Date: Mon, 22 Apr 2019 10:41:20 +0200 Subject: [PATCH] To address #339 Remove array control on metrics data and add an autoconversion of PHP Object in PHP array. For json request, i use the variable instead of label in metrics. The variable names are lower case, without accent, without space (more stable than a label in a site). --- includes/api/class-sp-rest-api.php | 6 +++++- includes/class-sp-player.php | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/includes/api/class-sp-rest-api.php b/includes/api/class-sp-rest-api.php index 577a1839..1eb8a0a0 100644 --- a/includes/api/class-sp-rest-api.php +++ b/includes/api/class-sp-rest-api.php @@ -430,7 +430,6 @@ class SP_REST_API { 'update_callback' => 'SP_REST_API::update_post_meta_array', 'schema' => array( 'description' => __( 'Metrics', 'sportspress' ), - 'type' => 'array', 'context' => array( 'view', 'edit' ), 'arg_options' => array( 'sanitize_callback' => 'rest_sanitize_request_arg', @@ -564,6 +563,11 @@ class SP_REST_API { * @return bool|int */ public static function update_post_meta_array( $value, $object, $field_name ) { + // Convert PHP object to array + if ( is_object( $value ) ) { + $value = (array) $value; + } + if ( ! is_array( $value ) ) return false; $type = $object->post_type; diff --git a/includes/class-sp-player.php b/includes/class-sp-player.php index 53c35571..141ce7d0 100644 --- a/includes/class-sp-player.php +++ b/includes/class-sp-player.php @@ -96,7 +96,7 @@ class SP_Player extends SP_Custom_Post { $metric = sp_array_value( $metrics, $key, null ); if ( $metric == null ) continue; - $data[ $value ] = sp_array_value( $metrics, $key, ' ' ); + $data[ $key ] = sp_array_value( $metrics, $key, ' ' ); endforeach; return $data; }