Theater for WordPress Code Reference
  • Package
  • Class

Packages

  • Theater
    • Abstracts
    • Events

Classes

  • Theater
  • Theater_Event
  • Theater_Event_Date
  • Theater_Event_Date_Link
  • Theater_Event_Date_List
  • Theater_Event_Field
  • Theater_Event_List
  • Theater_Item
  • Theater_List

Functions

  • Theater
  1 <?php
  2 /**
  3  * Maintains the link between events and event dates.
  4  *
  5  * @since   0.16
  6  * @package Theater/Events
  7  */
  8 class Theater_Event_Date_Link {
  9 
 10     /**
 11      * Adds the action hooks that maintain the link between events and event dates.
 12      *
 13      * @since   0.16
 14      * @static
 15      * @return void
 16      */
 17     static function init() {
 18 
 19         add_action( 'updated_post_meta', array( __CLASS__, 'sync_season' ), 20 ,4 );
 20         add_action( 'added_post_meta', array( __CLASS__, 'sync_season' ), 20 ,4 );
 21         add_action( 'set_object_terms', array( __CLASS__, 'sync_categories' ),20, 6 );
 22 
 23         add_action( 'before_delete_post',array( __CLASS__, 'delete_dates' ) );
 24         add_action( 'wp_trash_post',array( __CLASS__, 'trash_dates' ) );
 25         add_action( 'untrash_post',array( __CLASS__, 'untrash_dates' ) );
 26         
 27         add_filter( 'theater/date/field', array(__CLASS__, 'get_production_field_values'), 10, 3);
 28 
 29     }
 30 
 31     /**
 32      * Delete all connected dates of an event.
 33      *
 34      * Whenever an event is deleted (not just trashed), make sure that all connected dates are deleted as well.
 35      * Dates that are already in the trash are left alone.
 36      *
 37      * @since   0.7
 38      * @since   0.12    Added support for events with an 'auto-draft' post_status.
 39      *
 40      * @access public
 41      * @static
 42      * @param   int     $post_id    The ID of the trashed event.
 43      * @return void
 44      */
 45     static function delete_dates( $post_id ) {
 46         $post = get_post( $post_id );
 47         if ( ! empty( $post ) && $post->post_type == WPT_Production::post_type_name ) {
 48             $args = array(
 49                 'post_type' => WPT_Event::post_type_name,
 50                 'post_status' => array( 'any', 'auto-draft' ),
 51                 'meta_query' => array(
 52                     array(
 53                         'key' => WPT_Production::post_type_name,
 54                         'value' => $post_id,
 55                     ),
 56                 ),
 57             );
 58             $events = get_posts( $args );
 59             foreach ( $events as $event ) {
 60                 wp_delete_post( $event->ID );
 61             }
 62         }
 63     }
 64 
 65     static function get_production_field_values( $value, $field, $date) {
 66         
 67         if (!empty($value)) {
 68             return $value;
 69         }
 70         
 71         if ( $date->has_field($field) ) {
 72             return $value;          
 73         }
 74         
 75         if ( $event = $date->get_event() ) {
 76             $value = $event->get_field($field);       
 77         }
 78         return $value;
 79     }
 80 
 81     /**
 82      * Overwrites the categories of an event date with the categories of the event.
 83      *
 84      * Triggered if the categories of an event are set. Walks through all connected dates and
 85      * overwrites the categories of the dates with the categories of the event.
 86      *
 87      * @static
 88      * @since   0.?
 89      *
 90      * @param   int    $object_id  Object ID.
 91      * @param   array  $terms      An array of object terms.
 92      * @param   array  $tt_ids     An array of term taxonomy IDs.
 93      * @param   string $taxonomy   Taxonomy slug.
 94      * @param   bool   $append     Whether to append new terms to the old terms.
 95      * @param   array  $old_tt_ids Old array of term taxonomy IDs.
 96      * @return  void
 97      */
 98     static function sync_categories( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
 99         if ( 'category' == $taxonomy && WPT_Production::post_type_name == get_post_type( $object_id ) ) {
100             $production = new WPT_Production( $object_id );
101             foreach ( $production->events() as $event ) {
102                 wp_set_post_categories( $event->ID, $terms );
103             }
104         }
105     }
106 
107     /**
108      * Updates the season of event dates to the season of the parent event.
109      *
110      * Triggered by the updated_post_meta action.
111      *
112      * Used when:
113      * - an event is saved through the admin screen or
114      * - an event date is attached to an event.
115      *
116      * @since 0.7
117      *
118      * @access public
119      * @static
120      *
121      * @param   null|bool $check        Whether to allow updating metadata for the given type.
122      * @param   int       $object_id    Object ID.
123      * @param   string    $meta_key     Meta key.
124      * @param   mixed     $meta_value   Meta value. Must be serializable if non-scalar.
125      * @param   mixed     $prev_value   Optional. If specified, only update existing
126      *                                  metadata entries with the specified value.
127      *                                  Otherwise, update all entries.
128      * @return  void
129      */
130     static function sync_season( $meta_id, $object_id, $meta_key, $meta_value ) {
131         // An event is saved through the admin screen.
132         if ( $meta_key == WPT_Season::post_type_name ) {
133             $post = get_post( $object_id );
134             if ( $post->post_type == WPT_Production::post_type_name ) {
135 
136                 // avoid loops
137                 remove_action( 'updated_post_meta', array( __CLASS__, 'sync_season' ), 20 ,4 );
138                 remove_action( 'added_post_meta', array( __CLASS__, 'sync_season' ), 20 ,4 );
139 
140                 $args = array(
141                     'production' => $post->ID,
142                 );
143                 $events = Theater()->events->get( $args );
144                 foreach ( $events as $event ) {
145                     update_post_meta( $event->ID, WPT_Season::post_type_name, $meta_value );
146                 }
147 
148                 add_action( 'updated_post_meta', array( __CLASS__, 'sync_season' ), 20 ,4 );
149                 add_action( 'added_post_meta', array( __CLASS__, 'sync_season' ), 20 ,4 );
150             }
151         }
152 
153         // An event date is attached to an event.
154         if ( $meta_key == WPT_Production::post_type_name ) {
155             $event = new WPT_Event( $object_id );
156 
157             // avoid loops
158             remove_action( 'updated_post_meta', array( __CLASS__, 'sync_season' ), 20 ,4 );
159             remove_action( 'added_post_meta', array( __CLASS__, 'sync_season' ), 20 ,4 );
160 
161             // inherit season from production
162             if ( $season = $event->production()->season() ) {
163                 update_post_meta( $event->ID, WPT_Season::post_type_name, $season->ID );
164             }
165 
166             // inherit categories from production
167             $categories = wp_get_post_categories( $meta_value );
168             wp_set_post_categories( $event->ID, $categories );
169 
170             add_action( 'updated_post_meta', array( __CLASS__, 'sync_season' ), 20 ,4 );
171             add_action( 'added_post_meta', array( __CLASS__, 'sync_season' ), 20 ,4 );
172         }
173 
174     }
175 
176     /**
177      * Trash all connected dates of an event.
178      *
179      * Whenever an event is trashed (not deleted), make sure that all connected dates are trashed as well.
180      *
181      * @since 0.7
182      *
183      * @static
184      * @param   int     $post_id    The ID of the trashed event.
185      * @return  void
186      */
187     static function trash_dates( $post_id ) {
188         $post = get_post( $post_id );
189         if ( ! empty( $post ) && $post->post_type == WPT_Production::post_type_name ) {
190             $args = array(
191                 'status' => 'any',
192                 'production' => $post_id,
193             );
194             $events = Theater()->events->get( $args );
195             foreach ( $events as $event ) {
196                 wp_trash_post( $event->ID );
197             }
198         }
199     }
200 
201     /**
202      * Untrash all connected dates of an event.
203      *
204      * Whenever an event is untrashed, make sure that all connected dates are untrashed as well.
205      *
206      * @since 0.7
207      *
208      * @access public
209      * @static
210      * @param   int     $post_id    The ID of the trashed event.
211      * @return void
212      */
213     static function untrash_dates( $post_id ) {
214         $post = get_post( $post_id );
215         if ( ! empty( $post ) && $post->post_type == WPT_Production::post_type_name ) {
216             $args = array(
217                 'post_type' => WPT_Event::post_type_name,
218                 'post_status' => 'trash',
219                 'meta_query' => array(
220                     array(
221                         'key' => WPT_Production::post_type_name,
222                         'value' => $post_id,
223                     ),
224                 ),
225             );
226             $events = get_posts( $args );
227             foreach ( $events as $event ) {
228                 wp_untrash_post( $event->ID );
229             }
230         }
231     }
232 }
233 
Theater for WordPress Code Reference API documentation generated by ApiGen