Add API function to get and update main results
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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';
|
||||
}
|
||||
|
||||
@@ -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 ) ) {
|
||||
|
||||
Reference in New Issue
Block a user