From 2531e1a77e881c3f812598433106106429c37e90 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Wed, 11 Mar 2015 18:26:52 +1100 Subject: [PATCH] Add API function to get and update main results --- assets/js/admin/sportspress-admin.js | 10 ++- includes/admin/class-sp-admin-ajax.php | 86 ++------------------------ includes/class-sp-event.php | 80 ++++++++++++++++++++++++ includes/sp-api-functions.php | 20 +++++- 4 files changed, 106 insertions(+), 90 deletions(-) diff --git a/assets/js/admin/sportspress-admin.js b/assets/js/admin/sportspress-admin.js index 10486e15..eb81d3c5 100644 --- a/assets/js/admin/sportspress-admin.js +++ b/assets/js/admin/sportspress-admin.js @@ -588,13 +588,11 @@ jQuery(document).ready(function($){ // Save inline results $("#the-list").on("click", ".sp-inline-edit-save .save", function(){ $column = $(this).closest(".column-sp_team"); - results = []; + results = {}; $column.find(".sp-edit-result").each(function() { - team = {}; - team.id = $(this).data("team"); - team.key = $(this).data("key"); - team.result = $(this).val(); - results.push( team ); + id = $(this).data("team"); + result = $(this).val(); + results[id] = result; }); $.post( ajaxurl, { action: "sp-save-inline-results", diff --git a/includes/admin/class-sp-admin-ajax.php b/includes/admin/class-sp-admin-ajax.php index 62f591e2..ec34436c 100644 --- a/includes/admin/class-sp-admin-ajax.php +++ b/includes/admin/class-sp-admin-ajax.php @@ -59,92 +59,14 @@ class SP_Admin_AJAX { function save_inline_results() { check_ajax_referer( 'sp-save-inline-results', 'nonce' ); - $post_id = sp_array_value( $_POST, 'post_id' ); + $id = sp_array_value( $_POST, 'post_id' ); $results = sp_array_value( $_POST, 'results' ); - $main_result = get_option( 'sportspress_primary_result', null ); - if ( ! $post_id || ! is_array( $results ) ) { - // Return error + if ( sp_update_main_results ( $id, $results ) ) { + wp_send_json_success(); + } else { wp_send_json_error(); } - - // Get current results meta - $meta = get_post_meta( $post_id, 'sp_results', true ); - - $primary_results = array(); - foreach ( $results as $result ) { - $id = sp_array_value( $result, 'id' ); - $key = sp_array_value( $result, 'key' ); - - $primary_results[ $id ] = sp_array_value( $result, 'result', null ); - - if ( ! $id || ! $key ) continue; - - $meta[ $id ][ $key ] = sp_array_value( $result, 'result' ); - } - - arsort( $primary_results ); - - if ( count( $primary_results ) && ! in_array( null, $primary_results ) ) { - if ( count( array_unique( $primary_results ) ) === 1 ) { - $args = array( - 'post_type' => 'sp_outcome', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'meta_key' => 'sp_condition', - 'meta_value' => '=', - ); - $outcomes = get_posts( $args ); - foreach ( $meta as $team => $team_results ) { - if ( $outcomes ) { - $meta[ $team ][ 'outcome' ] = array(); - foreach ( $outcomes as $outcome ) { - $meta[ $team ][ 'outcome' ][] = $outcome->post_name; - } - } - } - } else { - reset( $primary_results ); - $max = key( $primary_results ); - $args = array( - 'post_type' => 'sp_outcome', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'meta_key' => 'sp_condition', - 'meta_value' => '>', - ); - $outcomes = get_posts( $args ); - if ( $outcomes ) { - $meta[ $max ][ 'outcome' ] = array(); - foreach ( $outcomes as $outcome ) { - $meta[ $max ][ 'outcome' ][] = $outcome->post_name; - } - } - - end( $primary_results ); - $min = key( $primary_results ); - $args = array( - 'post_type' => 'sp_outcome', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'meta_key' => 'sp_condition', - 'meta_value' => '<', - ); - $outcomes = get_posts( $args ); - if ( $outcomes ) { - $meta[ $min ][ 'outcome' ] = array(); - foreach ( $outcomes as $outcome ) { - $meta[ $min ][ 'outcome' ][] = $outcome->post_name; - } - } - } - } - - // Update results - update_post_meta( $post_id, 'sp_results', $meta ); - - // Return success - wp_send_json_success(); } } diff --git a/includes/class-sp-event.php b/includes/class-sp-event.php index df8cc6f9..5f20e804 100644 --- a/includes/class-sp-event.php +++ b/includes/class-sp-event.php @@ -236,6 +236,86 @@ class SP_Event extends SP_Custom_Post{ return null; } + public function update_main_results( $results ) { + $main_result = sp_get_main_result_option(); + + if ( ! $this->ID || ! is_array( $results ) || null === $main_result ) { + return false; + } + + // Get current results meta + $meta = get_post_meta( $this->ID, 'sp_results', true ); + + $primary_results = array(); + foreach ( $results as $id => $result ) { + $primary_results[ $id ] = $result; + + if ( ! $id ) continue; + + $meta[ $id ][ $main_result ] = $result; + } + + arsort( $primary_results ); + + if ( count( $primary_results ) && ! in_array( null, $primary_results ) ) { + if ( count( array_unique( $primary_results ) ) === 1 ) { + $args = array( + 'post_type' => 'sp_outcome', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'meta_key' => 'sp_condition', + 'meta_value' => '=', + ); + $outcomes = get_posts( $args ); + foreach ( $meta as $team => $team_results ) { + if ( $outcomes ) { + $meta[ $team ][ 'outcome' ] = array(); + foreach ( $outcomes as $outcome ) { + $meta[ $team ][ 'outcome' ][] = $outcome->post_name; + } + } + } + } else { + reset( $primary_results ); + $max = key( $primary_results ); + $args = array( + 'post_type' => 'sp_outcome', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'meta_key' => 'sp_condition', + 'meta_value' => '>', + ); + $outcomes = get_posts( $args ); + if ( $outcomes ) { + $meta[ $max ][ 'outcome' ] = array(); + foreach ( $outcomes as $outcome ) { + $meta[ $max ][ 'outcome' ][] = $outcome->post_name; + } + } + + end( $primary_results ); + $min = key( $primary_results ); + $args = array( + 'post_type' => 'sp_outcome', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'meta_key' => 'sp_condition', + 'meta_value' => '<', + ); + $outcomes = get_posts( $args ); + if ( $outcomes ) { + $meta[ $min ][ 'outcome' ] = array(); + foreach ( $outcomes as $outcome ) { + $meta[ $min ][ 'outcome' ][] = $outcome->post_name; + } + } + } + } + + // Update results + update_post_meta( $this->ID, 'sp_results', $meta ); + } + public function lineup_filter( $v ) { return sp_array_value( $v, 'status', 'lineup' ) == 'lineup'; } diff --git a/includes/sp-api-functions.php b/includes/sp-api-functions.php index 21f0da51..1a72cb5b 100644 --- a/includes/sp-api-functions.php +++ b/includes/sp-api-functions.php @@ -16,8 +16,9 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly * General functions */ -function sp_get_time( $post = 0 ) { - return get_post_time( get_option( 'time_format' ), false, $post, true ); +function sp_get_time( $post = 0, $format = null ) { + if ( null == $format ) $format = get_option( 'time_format' ); + return get_post_time( $format, false, $post, true ); } function sp_the_time( $post = 0 ) { @@ -42,6 +43,16 @@ function sp_get_teams( $post = 0 ) { return get_post_meta( $post, 'sp_team' ); } +function sp_get_main_result_option() { + $main_result = get_option( 'sportspress_primary_result', null ); + if ( $main_result ) return $main_result; + $results = get_posts( array( 'post_type' => 'sp_result', 'posts_per_page' => 1, 'orderby' => 'menu_order', 'order' => 'DESC' ) ); + if ( ! $results ) return null; + $result = reset( $results ); + $slug = $result->post_name; + return $slug; +} + function sp_get_main_results( $post = 0 ) { $event = new SP_Event( $post ); return $event->main_results(); @@ -52,6 +63,11 @@ function sp_the_main_results( $post = 0, $delimiter = '-' ) { echo implode( $delimiter, $results ); } +function sp_update_main_results( $post = 0, $results = array() ) { + $event = new SP_Event( $post ); + return $event->update_main_results ( $results ); +} + function sp_get_main_results_or_time( $post = 0 ) { $results = sp_get_main_results( $post ); if ( sizeof( $results ) ) {