@@ -40,9 +40,8 @@ public static function get_the_date( $the_date = '', $d = '', $post = null ) {
4040 */
4141 public static function hook () {
4242 add_action ( 'save_post_wpfc_sermon ' , array ( get_class (), 'maybe_update_date ' ), 10 , 3 );
43- add_action ( 'save_post_wpfc_sermon ' , array ( get_class (), 'save_series_date ' ), 20 , 3 );
44- add_action ( 'save_post_wpfc_sermon ' , array ( get_class (), 'update_series_date ' ), 30 );
45- add_action ( 'pre_post_update ' , array ( get_class (), 'get_original_series ' ) );
43+ add_action ( 'save_post_wpfc_sermon ' , array ( get_class (), 'save_terms_dates ' ), 20 , 3 );
44+ add_action ( 'pre_post_update ' , array ( get_class (), 'get_original_terms ' ) );
4645 add_action ( 'pre_post_update ' , array ( get_class (), 'get_original_date ' ) );
4746 add_filter ( 'cmb2_override_sermon_date_meta_remove ' , '__return_true ' );
4847 add_filter ( 'cmb2_override_sermon_date_meta_save ' , '__return_true ' );
@@ -62,11 +61,46 @@ public static function hook() {
6261 *
6362 * @param int $post_ID Post ID.
6463 *
65- * @since 2.8
64+ * @since 2.8
65+ *
66+ * @deprecated 2.15.11 - in favor of SM_Dates_WP::get_original_terms()
67+ * @see SM_Dates_WP::get_original_terms()
6668 */
6769 public static function get_original_series ( $ post_ID ) {
68- if ( get_post_type ( $ post_ID ) === 'wpfc_sermon ' ) {
69- $ GLOBALS ['sm_original_series ' ] = wp_get_object_terms ( $ post_ID , 'wpfc_sermon_series ' );
70+ self ::get_original_terms ( $ post_ID );
71+ }
72+
73+ /**
74+ * Used to save terms dates that were there before sermon update, for later comparison.
75+ *
76+ * @param int $post_ID Post ID.
77+ *
78+ * @since 2.15.11
79+ */
80+ public static function get_original_terms ( $ post_ID ) {
81+ if ( get_post_type ( $ post_ID ) !== 'wpfc_sermon ' ) {
82+ return ;
83+ }
84+
85+ $ data = array ();
86+
87+ foreach ( sm_get_taxonomies () as $ taxonomy ) {
88+ // Create an empty taxonomy.
89+ $ data [ $ taxonomy ] = array ();
90+
91+ // Get taxonomy terms.
92+ $ terms = wp_get_object_terms ( $ post_ID , $ taxonomy );
93+
94+ // Fill out the terms, if any.
95+ foreach ( $ terms as $ term ) {
96+ $ data [ $ taxonomy ][] = $ term ->term_id ;
97+ }
98+
99+ // New format. taxonomy => array(...terms).
100+ $ GLOBALS ['sm_original_terms ' ] = $ data ;
101+
102+ // Back-compat.
103+ $ GLOBALS [ 'sm_original_ ' . $ taxonomy ] = $ terms ;
70104 }
71105 }
72106
@@ -77,99 +111,133 @@ public static function get_original_series( $post_ID ) {
77111 * @param WP_Post $post Post object.
78112 * @param bool $update Whether this is an existing post being updated or not.
79113 *
80- * @since 2.8
114+ * @since 2.8
115+ *
116+ * @deprecated 2.15.11 - in favor of SM_Dates_WP::save_terms_dates()
117+ * @see SM_Dates_WP::save_terms_dates()
81118 */
82119 public static function save_series_date ( $ post_ID , $ post , $ update ) {
120+ self ::save_terms_dates ( $ post_ID , $ post , $ update );
121+ }
122+
123+ /**
124+ * Saves sermon date as term meta for all terms for that sermon, used for ordering.
125+ *
126+ * @param int $post_ID Post ID.
127+ * @param WP_Post $post Post object.
128+ * @param bool $update Whether this is an existing post being updated or not.
129+ *
130+ * @since 2.15.11
131+ */
132+ public static function save_terms_dates ( $ post_ID , $ post , $ update ) {
83133 if ( ! isset ( $ _POST ['tax_input ' ] ) ) {
84134 return ;
85135 }
86136
87- $ series = $ _POST [ ' tax_input ' ][ ' wpfc_sermon_series ' ];
88- $ orig_series = $ GLOBALS [ ' sm_original_series ' ] ;
137+ $ original_terms = $ GLOBALS [ ' sm_original_terms ' ];
138+ $ updated_terms = isset ( $ _POST [ ' tax_input ' ] ) ? $ _POST [ ' tax_input ' ] : null ;
89139
90- if ( $ update ) {
91- foreach ( $ orig_series as $ term ) {
92- delete_term_meta ( $ term -> term_id , ' sermon_date ' );
93- }
140+ $ updated_terms += array_fill_keys ( sm_get_taxonomies (), array () );
141+
142+ if ( ! $ updated_terms ) {
143+ return ;
94144 }
95145
96- if ( ! empty ( $ series ) ) {
97- foreach ( $ orig_series as $ term_id ) {
98- update_term_meta ( $ term_id , 'sermon_date_ ' . $ post_ID , get_post_meta ( $ post_ID , 'sermon_date ' , true ) );
146+ foreach ( sm_get_taxonomies () as $ taxonomy ) {
147+ $ new_terms = $ updated_terms [ $ taxonomy ];
148+ $ orig_terms = $ original_terms [ $ taxonomy ];
149+
150+ // Remove the date of the current sermon from removed terms.
151+ foreach ( $ orig_terms as $ term ) {
152+ if ( ! in_array ( $ term , $ new_terms ) ) {
153+ delete_term_meta ( $ term , 'sermon_date_ ' . $ post_ID );
154+ }
155+ }
156+
157+ // Add the date of the current sermon to its terms.
158+ if ( ! empty ( $ new_terms ) ) {
159+ foreach ( $ new_terms as $ term ) {
160+ update_term_meta ( $ term , 'sermon_date_ ' . $ post_ID , get_post_meta ( $ post_ID , 'sermon_date ' , true ) );
161+ }
99162 }
163+
164+ // Update the main date.
165+ self ::update_term_dates ( $ taxonomy , $ orig_terms + $ new_terms );
100166 }
101167 }
102168
103169 /**
104- * Left here for backwards-compatibility reasons.
105- * Does exactly the same as - self::update_term_dates();
170+ * Loops through taxonomies and terms and sets latest available sermon date.
106171 *
107- * @since 2.8
108- * @deprecated
109- */
110- public static function update_series_date () {
111- self ::update_term_dates ();
112- }
113-
114- /**
115- * Loops through all terms and sets latest available sermon date.
172+ * @param string $taxonomy The taxonomy to update. Default all.
173+ * @param array|string $terms The term(s) to update. Default all.
116174 *
117175 * @since 2.13.0 - extended to all terms
176+ * @since 2.15.11 - added parameters
118177 */
119- public static function update_term_dates () {
120- foreach (
121- get_terms ( array (
122- 'taxonomy ' => array (
123- 'wpfc_sermon_series ' ,
124- 'wpfc_preacher ' ,
125- 'wpfc_sermon_topics ' ,
126- 'wpfc_bible_book ' ,
127- 'wpfc_service_type ' ,
128- ),
129- 'hide_empty ' => true ,
130- ) ) as $ term
131- ) {
132- $ term_meta = get_term_meta ( $ term ->term_id );
133-
134- if ( empty ( $ term_meta ['sermon_date ' ] ) ) {
178+ public static function update_term_dates ( $ taxonomy = '' , $ terms = array () ) {
179+ $ taxonomies = $ taxonomy ? array ( $ taxonomy ) : sm_get_taxonomies ();
180+
181+ foreach ( $ taxonomies as $ taxonomy ) {
182+ $ the_terms = ! empty ( $ terms ) ? (array ) $ terms : null ;
183+
184+ if ( null === $ the_terms ) {
185+ $ get_terms = get_terms (
186+ array (
187+ 'taxonomy ' => $ taxonomy ,
188+ 'hide_empty ' => false ,
189+ )
190+ );
191+
192+ foreach ( $ get_terms as $ term ) {
193+ $ the_terms [] = $ term ->term_id ;
194+ }
195+ }
196+
197+ // Save the most recent sermon date to the term.
198+ foreach ( $ the_terms as $ term ) {
199+ $ meta = get_term_meta ( $ term );
135200 $ dates = array ();
136- foreach ( $ term_meta as $ name => $ value ) {
137- if ( strpos ( $ name , 'sermon_date_ ' ) !== false ) {
138- $ dates [] = $ value [0 ];
201+
202+ // Gather all of the dates.
203+ foreach ( $ meta as $ meta_key => $ meta_value ) {
204+ if ( substr ( $ meta_key , 0 , 12 ) !== 'sermon_date_ ' ) {
205+ continue ;
139206 }
140- }
141207
142- if ( ! empty ( $ dates ) ) {
143- arsort ( $ dates );
144- $ date = $ dates [0 ];
145- } else {
146- $ query = new WP_Query ( array (
147- 'post_type ' => 'wpfc_sermon ' ,
148- 'posts_per_page ' => 1 ,
149- 'meta_key ' => 'sermon_date ' ,
150- 'meta_value_num ' => time (),
151- 'meta_compare ' => '<= ' ,
152- 'orderby ' => 'meta_value_num ' ,
153- 'tax_query ' => array (
154- array (
155- 'taxonomy ' => $ term ->taxonomy ,
156- 'field ' => 'term_id ' ,
157- 'terms ' => $ term ->term_id ,
158- ),
159- ),
160- ) );
161- if ( $ query ->have_posts () ) {
162- $ date = get_post_meta ( $ query ->posts [0 ]->ID , 'sermon_date ' , true );
163- } else {
164- $ date = 0 ;
208+ $ sermon_date = intval ( $ meta_value [0 ] );
209+
210+ if ( $ sermon_date ) {
211+ $ dates [] = $ sermon_date ;
165212 }
166213 }
167214
168- update_term_meta ( $ term ->term_id , 'sermon_date ' , $ date );
215+ // If we can't find a date, remove the existing.
216+ if ( empty ( $ dates ) ) {
217+ delete_term_meta ( $ term , 'sermon_date ' );
218+ continue ;
219+ }
220+
221+ // Sort the dates by newest first (DESC).
222+ rsort ( $ dates );
223+
224+ // Update the date.
225+ update_term_meta ( $ term , 'sermon_date ' , $ dates [0 ] );
169226 }
170227 }
171228 }
172229
230+ /**
231+ * Left here for backwards-compatibility reasons.
232+ * Does exactly the same as - self::update_term_dates();
233+ *
234+ * @since 2.8
235+ * @deprecated 2.13.0
236+ */
237+ public static function update_series_date () {
238+ self ::update_term_dates ();
239+ }
240+
173241 /**
174242 * Used to save date that was there before sermon update, for later comparison.
175243 *
0 commit comments