From 26edb65bea22ad62e5c30601f6f89e3d7d48328c Mon Sep 17 00:00:00 2001
From: Brian Miyaji
Date: Wed, 29 Oct 2014 19:17:46 +1100
Subject: [PATCH] Automatically select venue based on home team
---
.../class-sp-meta-box-event-details.php | 62 ++++++++++++-------
includes/sp-core-functions.php | 21 ++++++-
2 files changed, 56 insertions(+), 27 deletions(-)
diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-details.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-details.php
index 5800a9da..d2207dd3 100644
--- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-details.php
+++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-details.php
@@ -29,27 +29,34 @@ class SP_Meta_Box_Event_Details {
- $post_type ): $obj = get_taxonomy( $taxonomy ); if ( $obj ): ?>
-
-
labels->singular_name; ?>
-
- $taxonomy,
- 'name' => $taxonomy,
- 'class' => 'sp-has-dummy',
- 'selected' => sp_get_the_term_id( $post->ID, $taxonomy, 0 ),
- 'values' => 'term_id',
- 'show_option_none' => __( '-- Not set --', 'sportspress' ),
- );
- if ( ! sp_dropdown_taxonomies( $args ) ):
- sp_taxonomy_adder( $taxonomy, $post_type, $obj->labels->add_new_item );
- endif;
- ?>
-
-
-
$post_type ) {
+ $obj = get_taxonomy( $taxonomy ); if ( $obj ) {
+ ?>
+
+
labels->singular_name; ?>
+
+ $taxonomy,
+ 'name' => $taxonomy,
+ 'class' => 'sp-has-dummy',
+ 'selected' => sp_get_the_term_id_or_meta( $post->ID, $taxonomy ),
+ 'values' => 'term_id',
+ 'show_option_none' => __( '-- Not set --', 'sportspress' ),
+ );
+ if ( in_array( $taxonomy, apply_filters( 'sportspress_event_auto_taxonomies', array( 'sp_venue' ) ) ) ) {
+ $args['show_option_all'] = __( '(Auto)', 'sportspress' );
+ }
+ if ( ! sp_dropdown_taxonomies( $args ) ) {
+ sp_taxonomy_adder( $taxonomy, $post_type, $obj->labels->add_new_item );
+ }
+ ?>
+
+
+ null, 'sp_season' => null, 'sp_venue' => 'sp_event' ) );
- foreach ( $taxonomies as $taxonomy => $post_type ):
- wp_set_post_terms( $post_id, sp_array_value( $_POST, $taxonomy, -1 ), $taxonomy );
- endforeach;
+ foreach ( $taxonomies as $taxonomy => $post_type ) {
+ $value = sp_array_value( $_POST, $taxonomy, -1 );
+ if ( 0 == $value) {
+ $teams = sp_array_value( $_POST, 'sp_team', array() );
+ $team = reset( $teams );
+ $value = sp_get_the_term_id( $team, $taxonomy );
+ }
+ wp_set_post_terms( $post_id, $value, $taxonomy );
+ update_post_meta( $post_id, $taxonomy, $value );
+ }
}
-}
\ No newline at end of file
+}
diff --git a/includes/sp-core-functions.php b/includes/sp-core-functions.php
index 8f425d82..58ef5434 100644
--- a/includes/sp-core-functions.php
+++ b/includes/sp-core-functions.php
@@ -298,7 +298,7 @@ if ( !function_exists( 'sp_numbers_to_words' ) ) {
}
if ( !function_exists( 'sp_get_the_term_id' ) ) {
- function sp_get_the_term_id( $post_id, $taxonomy, $index ) {
+ function sp_get_the_term_id( $post_id, $taxonomy ) {
$terms = get_the_terms( $post_id, $taxonomy );
if ( is_array( $terms ) && sizeof( $terms ) > 0 ):
$term = reset( $terms );
@@ -312,6 +312,21 @@ if ( !function_exists( 'sp_get_the_term_id' ) ) {
}
}
+if ( !function_exists( 'sp_get_the_term_id_or_meta' ) ) {
+ function sp_get_the_term_id_or_meta( $post_id, $taxonomy ) {
+ $terms = get_the_terms( $post_id, $taxonomy );
+ if ( is_array( $terms ) && sizeof( $terms ) > 0 ):
+ $term = reset( $terms );
+ if ( is_object( $term ) && property_exists( $term, 'term_id' ) )
+ return $term->term_id;
+ else
+ return 0;
+ else:
+ return get_post_meta( $post_id, $taxonomy, true );
+ endif;
+ }
+}
+
if ( !function_exists( 'sp_get_url' ) ) {
function sp_get_url( $post_id ) {
$url = get_post_meta( $post_id, 'sp_url', true );
@@ -502,10 +517,10 @@ if ( !function_exists( 'sp_dropdown_taxonomies' ) ) {
echo '';
endif;
if ( $args['show_option_all'] ):
- printf( '', $args['show_option_all'] );
+ printf( '', $args['show_option_all'] );
endif;
if ( $args['show_option_none'] ):
- printf( '', $args['show_option_none'] );
+ printf( '', $args['show_option_none'] );
endif;
endif;