@@ -14,12 +14,12 @@ discard block |
||
| 14 | 14 | * @param int | \EE_Event $event |
| 15 | 15 | * @return bool |
| 16 | 16 | */ |
| 17 | -function is_espresso_event( $event = NULL ) { |
|
| 18 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 17 | +function is_espresso_event($event = NULL) { |
|
| 18 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 19 | 19 | // extract EE_Event object from passed param regardless of what it is (within reason of course) |
| 20 | - $event = EEH_Event_View::get_event( $event ); |
|
| 20 | + $event = EEH_Event_View::get_event($event); |
|
| 21 | 21 | // do we have a valid event ? |
| 22 | - return $event instanceof EE_Event ? TRUE : FALSE; |
|
| 22 | + return $event instanceof EE_Event ? TRUE : FALSE; |
|
| 23 | 23 | } |
| 24 | 24 | return FALSE; |
| 25 | 25 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * @return bool |
| 32 | 32 | */ |
| 33 | 33 | function is_espresso_event_single() { |
| 34 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 34 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 35 | 35 | global $wp_query; |
| 36 | 36 | // return conditionals set by CPTs |
| 37 | 37 | return $wp_query instanceof WP_Query ? $wp_query->is_espresso_event_single : FALSE; |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * @return bool |
| 47 | 47 | */ |
| 48 | 48 | function is_espresso_event_archive() { |
| 49 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 49 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 50 | 50 | global $wp_query; |
| 51 | 51 | return $wp_query instanceof WP_Query ? $wp_query->is_espresso_event_archive : FALSE; |
| 52 | 52 | } |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | * @return bool |
| 61 | 61 | */ |
| 62 | 62 | function is_espresso_event_taxonomy() { |
| 63 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 63 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 64 | 64 | global $wp_query; |
| 65 | 65 | return $wp_query instanceof WP_Query ? $wp_query->is_espresso_event_taxonomy : FALSE; |
| 66 | 66 | } |
@@ -74,10 +74,10 @@ discard block |
||
| 74 | 74 | * @param int | \EE_Venue $venue |
| 75 | 75 | * @return bool |
| 76 | 76 | */ |
| 77 | -function is_espresso_venue( $venue = NULL ) { |
|
| 78 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 77 | +function is_espresso_venue($venue = NULL) { |
|
| 78 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 79 | 79 | // extract EE_Venue object from passed param regardless of what it is (within reason of course) |
| 80 | - $venue = EEH_Venue_View::get_venue( $venue, FALSE ); |
|
| 80 | + $venue = EEH_Venue_View::get_venue($venue, FALSE); |
|
| 81 | 81 | // do we have a valid event ? |
| 82 | 82 | return $venue instanceof EE_Venue ? TRUE : FALSE; |
| 83 | 83 | } |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * @return bool |
| 92 | 92 | */ |
| 93 | 93 | function is_espresso_venue_single() { |
| 94 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 94 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 95 | 95 | global $wp_query; |
| 96 | 96 | return $wp_query instanceof WP_Query ? $wp_query->is_espresso_venue_single : FALSE; |
| 97 | 97 | } |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | * @return bool |
| 106 | 106 | */ |
| 107 | 107 | function is_espresso_venue_archive() { |
| 108 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 108 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 109 | 109 | global $wp_query; |
| 110 | 110 | return $wp_query instanceof WP_Query ? $wp_query->is_espresso_venue_archive : FALSE; |
| 111 | 111 | } |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | * @return bool |
| 120 | 120 | */ |
| 121 | 121 | function is_espresso_venue_taxonomy() { |
| 122 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 122 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 123 | 123 | global $wp_query; |
| 124 | 124 | return $wp_query instanceof WP_Query ? $wp_query->is_espresso_venue_taxonomy : FALSE; |
| 125 | 125 | } |
@@ -133,12 +133,12 @@ discard block |
||
| 133 | 133 | * @param $conditional_tag |
| 134 | 134 | * @return bool |
| 135 | 135 | */ |
| 136 | -function can_use_espresso_conditionals( $conditional_tag ) { |
|
| 137 | - if ( ! did_action( 'AHEE__EE_System__initialize' )) { |
|
| 136 | +function can_use_espresso_conditionals($conditional_tag) { |
|
| 137 | + if ( ! did_action('AHEE__EE_System__initialize')) { |
|
| 138 | 138 | EE_Error::doing_it_wrong( |
| 139 | 139 | __FUNCTION__, |
| 140 | 140 | sprintf( |
| 141 | - esc_html__( 'The "%s" conditional tag can not be used until after the "init" hook has run, but works best when used within a theme\'s template files.','event_espresso'), |
|
| 141 | + esc_html__('The "%s" conditional tag can not be used until after the "init" hook has run, but works best when used within a theme\'s template files.', 'event_espresso'), |
|
| 142 | 142 | $conditional_tag |
| 143 | 143 | ), |
| 144 | 144 | '4.4.0' |
@@ -153,13 +153,13 @@ discard block |
||
| 153 | 153 | |
| 154 | 154 | /*************************** Event Queries ***************************/ |
| 155 | 155 | |
| 156 | -if ( ! function_exists( 'espresso_get_events' )) { |
|
| 156 | +if ( ! function_exists('espresso_get_events')) { |
|
| 157 | 157 | /** |
| 158 | 158 | * espresso_get_events |
| 159 | 159 | * @param array $params |
| 160 | 160 | * @return array |
| 161 | 161 | */ |
| 162 | - function espresso_get_events( $params = array() ) { |
|
| 162 | + function espresso_get_events($params = array()) { |
|
| 163 | 163 | //set default params |
| 164 | 164 | $default_espresso_events_params = array( |
| 165 | 165 | 'limit' => 10, |
@@ -170,18 +170,18 @@ discard block |
||
| 170 | 170 | 'sort' => 'ASC' |
| 171 | 171 | ); |
| 172 | 172 | // allow the defaults to be filtered |
| 173 | - $default_espresso_events_params = apply_filters( 'espresso_get_events__default_espresso_events_params', $default_espresso_events_params ); |
|
| 173 | + $default_espresso_events_params = apply_filters('espresso_get_events__default_espresso_events_params', $default_espresso_events_params); |
|
| 174 | 174 | // grab params and merge with defaults, then extract |
| 175 | - $params = array_merge( $default_espresso_events_params, $params ); |
|
| 175 | + $params = array_merge($default_espresso_events_params, $params); |
|
| 176 | 176 | // run the query |
| 177 | - $events_query = new EventEspresso\core\domain\services\wp_queries\EventListQuery( $params ); |
|
| 177 | + $events_query = new EventEspresso\core\domain\services\wp_queries\EventListQuery($params); |
|
| 178 | 178 | // assign results to a variable so we can return it |
| 179 | 179 | $events = $events_query->have_posts() ? $events_query->posts : array(); |
| 180 | 180 | // but first reset the query and postdata |
| 181 | 181 | wp_reset_query(); |
| 182 | 182 | wp_reset_postdata(); |
| 183 | 183 | EED_Events_Archive::remove_all_events_archive_filters(); |
| 184 | - unset( $events_query ); |
|
| 184 | + unset($events_query); |
|
| 185 | 185 | return $events; |
| 186 | 186 | } |
| 187 | 187 | } |
@@ -195,33 +195,33 @@ discard block |
||
| 195 | 195 | * espresso_load_ticket_selector |
| 196 | 196 | */ |
| 197 | 197 | function espresso_load_ticket_selector() { |
| 198 | - EE_Registry::instance()->load_file( EE_MODULES . 'ticket_selector', 'EED_Ticket_Selector', 'module' ); |
|
| 198 | + EE_Registry::instance()->load_file(EE_MODULES.'ticket_selector', 'EED_Ticket_Selector', 'module'); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | -if ( ! function_exists( 'espresso_ticket_selector' )) { |
|
| 201 | +if ( ! function_exists('espresso_ticket_selector')) { |
|
| 202 | 202 | /** |
| 203 | 203 | * espresso_ticket_selector |
| 204 | 204 | * @param null $event |
| 205 | 205 | */ |
| 206 | - function espresso_ticket_selector( $event = NULL ) { |
|
| 207 | - if ( ! apply_filters( 'FHEE_disable_espresso_ticket_selector', FALSE ) ) { |
|
| 206 | + function espresso_ticket_selector($event = NULL) { |
|
| 207 | + if ( ! apply_filters('FHEE_disable_espresso_ticket_selector', FALSE)) { |
|
| 208 | 208 | espresso_load_ticket_selector(); |
| 209 | 209 | \EED_Ticket_Selector::set_definitions(); |
| 210 | - echo EED_Ticket_Selector::display_ticket_selector( $event ); |
|
| 210 | + echo EED_Ticket_Selector::display_ticket_selector($event); |
|
| 211 | 211 | } |
| 212 | 212 | } |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | |
| 216 | - if ( ! function_exists( 'espresso_view_details_btn' )) { |
|
| 216 | + if ( ! function_exists('espresso_view_details_btn')) { |
|
| 217 | 217 | /** |
| 218 | 218 | * espresso_view_details_btn |
| 219 | 219 | * @param null $event |
| 220 | 220 | */ |
| 221 | - function espresso_view_details_btn( $event = NULL ) { |
|
| 222 | - if ( ! apply_filters( 'FHEE_disable_espresso_view_details_btn', FALSE ) ) { |
|
| 221 | + function espresso_view_details_btn($event = NULL) { |
|
| 222 | + if ( ! apply_filters('FHEE_disable_espresso_view_details_btn', FALSE)) { |
|
| 223 | 223 | espresso_load_ticket_selector(); |
| 224 | - echo EED_Ticket_Selector::display_ticket_selector( $event, TRUE ); |
|
| 224 | + echo EED_Ticket_Selector::display_ticket_selector($event, TRUE); |
|
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | } |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | |
| 232 | 232 | /*************************** EEH_Event_View ***************************/ |
| 233 | 233 | |
| 234 | -if ( ! function_exists( 'espresso_load_event_list_assets' )) { |
|
| 234 | +if ( ! function_exists('espresso_load_event_list_assets')) { |
|
| 235 | 235 | /** |
| 236 | 236 | * espresso_load_event_list_assets |
| 237 | 237 | * ensures that event list styles and scripts are loaded |
@@ -240,13 +240,13 @@ discard block |
||
| 240 | 240 | */ |
| 241 | 241 | function espresso_load_event_list_assets() { |
| 242 | 242 | $event_list = EED_Events_Archive::instance(); |
| 243 | - add_action( 'AHEE__EE_System__initialize_last', array( $event_list, 'load_event_list_assets' ), 10 ); |
|
| 244 | - add_filter( 'FHEE_enable_default_espresso_css', '__return_true' ); |
|
| 243 | + add_action('AHEE__EE_System__initialize_last', array($event_list, 'load_event_list_assets'), 10); |
|
| 244 | + add_filter('FHEE_enable_default_espresso_css', '__return_true'); |
|
| 245 | 245 | } |
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | |
| 249 | -if ( ! function_exists( 'espresso_event_reg_button' )) { |
|
| 249 | +if ( ! function_exists('espresso_event_reg_button')) { |
|
| 250 | 250 | /** |
| 251 | 251 | * espresso_event_reg_button |
| 252 | 252 | * returns the "Register Now" button if event is active, |
@@ -258,13 +258,13 @@ discard block |
||
| 258 | 258 | * @param bool $EVT_ID |
| 259 | 259 | * @return string |
| 260 | 260 | */ |
| 261 | - function espresso_event_reg_button( $btn_text_if_active = NULL, $btn_text_if_inactive = FALSE, $EVT_ID = FALSE ) { |
|
| 262 | - $event = EEH_Event_View::get_event( $EVT_ID ); |
|
| 263 | - if ( ! $event instanceof EE_Event ) { |
|
| 261 | + function espresso_event_reg_button($btn_text_if_active = NULL, $btn_text_if_inactive = FALSE, $EVT_ID = FALSE) { |
|
| 262 | + $event = EEH_Event_View::get_event($EVT_ID); |
|
| 263 | + if ( ! $event instanceof EE_Event) { |
|
| 264 | 264 | return; |
| 265 | 265 | } |
| 266 | 266 | $event_status = $event->get_active_status(); |
| 267 | - switch ( $event_status ) { |
|
| 267 | + switch ($event_status) { |
|
| 268 | 268 | case EE_Datetime::sold_out : |
| 269 | 269 | $btn_text = esc_html__('Sold Out', 'event_espresso'); |
| 270 | 270 | $class = 'ee-pink'; |
@@ -284,15 +284,15 @@ discard block |
||
| 284 | 284 | case EE_Datetime::upcoming : |
| 285 | 285 | case EE_Datetime::active : |
| 286 | 286 | default : |
| 287 | - $btn_text =! empty( $btn_text_if_active ) ? $btn_text_if_active : esc_html__( 'Register Now', 'event_espresso' ); |
|
| 287 | + $btn_text = ! empty($btn_text_if_active) ? $btn_text_if_active : esc_html__('Register Now', 'event_espresso'); |
|
| 288 | 288 | $class = 'ee-green'; |
| 289 | 289 | } |
| 290 | - if ( $event_status < 1 && ! empty( $btn_text_if_inactive )) { |
|
| 290 | + if ($event_status < 1 && ! empty($btn_text_if_inactive)) { |
|
| 291 | 291 | $btn_text = $btn_text_if_inactive; |
| 292 | 292 | $class = 'ee-grey'; |
| 293 | 293 | } |
| 294 | 294 | ?> |
| 295 | - <a class="ee-button ee-register-button <?php echo $class; ?>" href="<?php espresso_event_link_url( $EVT_ID ); ?>"<?php echo \EED_Events_Archive::link_target(); ?>> |
|
| 295 | + <a class="ee-button ee-register-button <?php echo $class; ?>" href="<?php espresso_event_link_url($EVT_ID); ?>"<?php echo \EED_Events_Archive::link_target(); ?>> |
|
| 296 | 296 | <?php echo $btn_text; ?> |
| 297 | 297 | </a> |
| 298 | 298 | <?php |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | |
| 302 | 302 | |
| 303 | 303 | |
| 304 | -if ( ! function_exists( 'espresso_display_ticket_selector' )) { |
|
| 304 | +if ( ! function_exists('espresso_display_ticket_selector')) { |
|
| 305 | 305 | /** |
| 306 | 306 | * espresso_display_ticket_selector |
| 307 | 307 | * whether or not to display the Ticket Selector for an event |
@@ -309,14 +309,14 @@ discard block |
||
| 309 | 309 | * @param bool $EVT_ID |
| 310 | 310 | * @return boolean |
| 311 | 311 | */ |
| 312 | - function espresso_display_ticket_selector( $EVT_ID = FALSE ) { |
|
| 313 | - return EEH_Event_View::display_ticket_selector( $EVT_ID ); |
|
| 312 | + function espresso_display_ticket_selector($EVT_ID = FALSE) { |
|
| 313 | + return EEH_Event_View::display_ticket_selector($EVT_ID); |
|
| 314 | 314 | } |
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | |
| 318 | 318 | |
| 319 | -if ( ! function_exists( 'espresso_event_status_banner' )) { |
|
| 319 | +if ( ! function_exists('espresso_event_status_banner')) { |
|
| 320 | 320 | /** |
| 321 | 321 | * espresso_event_status |
| 322 | 322 | * returns a banner showing the event status if it is sold out, expired, or inactive |
@@ -324,13 +324,13 @@ discard block |
||
| 324 | 324 | * @param bool $EVT_ID |
| 325 | 325 | * @return string |
| 326 | 326 | */ |
| 327 | - function espresso_event_status_banner( $EVT_ID = FALSE ) { |
|
| 328 | - return EEH_Event_View::event_status( $EVT_ID ); |
|
| 327 | + function espresso_event_status_banner($EVT_ID = FALSE) { |
|
| 328 | + return EEH_Event_View::event_status($EVT_ID); |
|
| 329 | 329 | } |
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | |
| 333 | -if ( ! function_exists( 'espresso_event_status' )) { |
|
| 333 | +if ( ! function_exists('espresso_event_status')) { |
|
| 334 | 334 | /** |
| 335 | 335 | * espresso_event_status |
| 336 | 336 | * returns the event status if it is sold out, expired, or inactive |
@@ -339,13 +339,13 @@ discard block |
||
| 339 | 339 | * @param bool $echo |
| 340 | 340 | * @return string |
| 341 | 341 | */ |
| 342 | - function espresso_event_status( $EVT_ID = 0, $echo = TRUE ) { |
|
| 343 | - return EEH_Event_View::event_active_status( $EVT_ID, $echo ); |
|
| 342 | + function espresso_event_status($EVT_ID = 0, $echo = TRUE) { |
|
| 343 | + return EEH_Event_View::event_active_status($EVT_ID, $echo); |
|
| 344 | 344 | } |
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | |
| 348 | -if ( ! function_exists( 'espresso_event_categories' )) { |
|
| 348 | +if ( ! function_exists('espresso_event_categories')) { |
|
| 349 | 349 | /** |
| 350 | 350 | * espresso_event_categories |
| 351 | 351 | * returns the terms associated with an event |
@@ -355,17 +355,17 @@ discard block |
||
| 355 | 355 | * @param bool $echo |
| 356 | 356 | * @return string |
| 357 | 357 | */ |
| 358 | - function espresso_event_categories( $EVT_ID = 0, $hide_uncategorized = TRUE, $echo = TRUE ) { |
|
| 359 | - if ( $echo ) { |
|
| 360 | - echo EEH_Event_View::event_categories( $EVT_ID, $hide_uncategorized ); |
|
| 358 | + function espresso_event_categories($EVT_ID = 0, $hide_uncategorized = TRUE, $echo = TRUE) { |
|
| 359 | + if ($echo) { |
|
| 360 | + echo EEH_Event_View::event_categories($EVT_ID, $hide_uncategorized); |
|
| 361 | 361 | return ''; |
| 362 | 362 | } |
| 363 | - return EEH_Event_View::event_categories( $EVT_ID, $hide_uncategorized ); |
|
| 363 | + return EEH_Event_View::event_categories($EVT_ID, $hide_uncategorized); |
|
| 364 | 364 | } |
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | |
| 368 | -if ( ! function_exists( 'espresso_event_tickets_available' )) { |
|
| 368 | +if ( ! function_exists('espresso_event_tickets_available')) { |
|
| 369 | 369 | /** |
| 370 | 370 | * espresso_event_tickets_available |
| 371 | 371 | * returns the ticket types available for purchase for an event |
@@ -375,26 +375,26 @@ discard block |
||
| 375 | 375 | * @param bool $format |
| 376 | 376 | * @return string |
| 377 | 377 | */ |
| 378 | - function espresso_event_tickets_available( $EVT_ID = 0, $echo = TRUE, $format = TRUE ) { |
|
| 379 | - $tickets = EEH_Event_View::event_tickets_available( $EVT_ID ); |
|
| 380 | - if ( is_array( $tickets ) && ! empty( $tickets )) { |
|
| 378 | + function espresso_event_tickets_available($EVT_ID = 0, $echo = TRUE, $format = TRUE) { |
|
| 379 | + $tickets = EEH_Event_View::event_tickets_available($EVT_ID); |
|
| 380 | + if (is_array($tickets) && ! empty($tickets)) { |
|
| 381 | 381 | // if formatting then $html will be a string, else it will be an array of ticket objects |
| 382 | - $html = $format ? '<ul id="ee-event-tickets-ul-' . $EVT_ID . '" class="ee-event-tickets-ul">' : array(); |
|
| 383 | - foreach ( $tickets as $ticket ) { |
|
| 384 | - if ( $ticket instanceof EE_Ticket ) { |
|
| 385 | - if ( $format ) { |
|
| 386 | - $html .= '<li id="ee-event-tickets-li-' . $ticket->ID() . '" class="ee-event-tickets-li">'; |
|
| 387 | - $html .= $ticket->name() . ' ' . EEH_Template::format_currency( $ticket->get_ticket_total_with_taxes() ); |
|
| 382 | + $html = $format ? '<ul id="ee-event-tickets-ul-'.$EVT_ID.'" class="ee-event-tickets-ul">' : array(); |
|
| 383 | + foreach ($tickets as $ticket) { |
|
| 384 | + if ($ticket instanceof EE_Ticket) { |
|
| 385 | + if ($format) { |
|
| 386 | + $html .= '<li id="ee-event-tickets-li-'.$ticket->ID().'" class="ee-event-tickets-li">'; |
|
| 387 | + $html .= $ticket->name().' '.EEH_Template::format_currency($ticket->get_ticket_total_with_taxes()); |
|
| 388 | 388 | $html .= '</li>'; |
| 389 | 389 | } else { |
| 390 | 390 | $html[] = $ticket; |
| 391 | 391 | } |
| 392 | 392 | } |
| 393 | 393 | } |
| 394 | - if ( $format ) { |
|
| 394 | + if ($format) { |
|
| 395 | 395 | $html .= '</ul>'; |
| 396 | 396 | } |
| 397 | - if ( $echo && $format ) { |
|
| 397 | + if ($echo && $format) { |
|
| 398 | 398 | echo $html; |
| 399 | 399 | return ''; |
| 400 | 400 | } |
@@ -404,7 +404,7 @@ discard block |
||
| 404 | 404 | } |
| 405 | 405 | } |
| 406 | 406 | |
| 407 | -if ( ! function_exists( 'espresso_event_date_obj' )) { |
|
| 407 | +if ( ! function_exists('espresso_event_date_obj')) { |
|
| 408 | 408 | /** |
| 409 | 409 | * espresso_event_date_obj |
| 410 | 410 | * returns the primary date object for an event |
@@ -412,13 +412,13 @@ discard block |
||
| 412 | 412 | * @param bool $EVT_ID |
| 413 | 413 | * @return object |
| 414 | 414 | */ |
| 415 | - function espresso_event_date_obj( $EVT_ID = FALSE ) { |
|
| 416 | - return EEH_Event_View::get_primary_date_obj( $EVT_ID ); |
|
| 415 | + function espresso_event_date_obj($EVT_ID = FALSE) { |
|
| 416 | + return EEH_Event_View::get_primary_date_obj($EVT_ID); |
|
| 417 | 417 | } |
| 418 | 418 | } |
| 419 | 419 | |
| 420 | 420 | |
| 421 | -if ( ! function_exists( 'espresso_event_date' )) { |
|
| 421 | +if ( ! function_exists('espresso_event_date')) { |
|
| 422 | 422 | /** |
| 423 | 423 | * espresso_event_date |
| 424 | 424 | * returns the primary date for an event |
@@ -429,22 +429,22 @@ discard block |
||
| 429 | 429 | * @param bool $echo |
| 430 | 430 | * @return string |
| 431 | 431 | */ |
| 432 | - function espresso_event_date( $date_format = '', $time_format = '', $EVT_ID = FALSE, $echo = TRUE ) { |
|
| 433 | - $date_format = ! empty( $date_format ) ? $date_format : get_option( 'date_format' ); |
|
| 434 | - $time_format = ! empty( $time_format ) ? $time_format : get_option( 'time_format' ); |
|
| 435 | - $date_format = apply_filters( 'FHEE__espresso_event_date__date_format', $date_format ); |
|
| 436 | - $time_format = apply_filters( 'FHEE__espresso_event_date__time_format', $time_format ); |
|
| 437 | - if($echo){ |
|
| 438 | - echo EEH_Event_View::the_event_date( $date_format, $time_format, $EVT_ID ); |
|
| 432 | + function espresso_event_date($date_format = '', $time_format = '', $EVT_ID = FALSE, $echo = TRUE) { |
|
| 433 | + $date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
| 434 | + $time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
| 435 | + $date_format = apply_filters('FHEE__espresso_event_date__date_format', $date_format); |
|
| 436 | + $time_format = apply_filters('FHEE__espresso_event_date__time_format', $time_format); |
|
| 437 | + if ($echo) { |
|
| 438 | + echo EEH_Event_View::the_event_date($date_format, $time_format, $EVT_ID); |
|
| 439 | 439 | return ''; |
| 440 | 440 | } |
| 441 | - return EEH_Event_View::the_event_date( $date_format, $time_format, $EVT_ID ); |
|
| 441 | + return EEH_Event_View::the_event_date($date_format, $time_format, $EVT_ID); |
|
| 442 | 442 | |
| 443 | 443 | } |
| 444 | 444 | } |
| 445 | 445 | |
| 446 | 446 | |
| 447 | -if ( ! function_exists( 'espresso_list_of_event_dates' )) { |
|
| 447 | +if ( ! function_exists('espresso_list_of_event_dates')) { |
|
| 448 | 448 | /** |
| 449 | 449 | * espresso_list_of_event_dates |
| 450 | 450 | * returns a unordered list of dates for an event |
@@ -459,40 +459,40 @@ discard block |
||
| 459 | 459 | * @param null $limit |
| 460 | 460 | * @return string |
| 461 | 461 | */ |
| 462 | - function espresso_list_of_event_dates( $EVT_ID = 0, $date_format = '', $time_format = '', $echo = TRUE, $show_expired = NULL, $format = TRUE, $add_breaks = TRUE, $limit = NULL ) { |
|
| 463 | - $date_format = ! empty( $date_format ) ? $date_format : get_option( 'date_format' ); |
|
| 464 | - $time_format = ! empty( $time_format ) ? $time_format : get_option( 'time_format' ); |
|
| 465 | - $date_format = apply_filters( 'FHEE__espresso_list_of_event_dates__date_format', $date_format ); |
|
| 466 | - $time_format = apply_filters( 'FHEE__espresso_list_of_event_dates__time_format', $time_format ); |
|
| 467 | - $datetimes = EEH_Event_View::get_all_date_obj( $EVT_ID, $show_expired, FALSE, $limit ); |
|
| 468 | - if ( ! $format ) { |
|
| 469 | - return apply_filters( 'FHEE__espresso_list_of_event_dates__datetimes', $datetimes ); |
|
| 462 | + function espresso_list_of_event_dates($EVT_ID = 0, $date_format = '', $time_format = '', $echo = TRUE, $show_expired = NULL, $format = TRUE, $add_breaks = TRUE, $limit = NULL) { |
|
| 463 | + $date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
| 464 | + $time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
| 465 | + $date_format = apply_filters('FHEE__espresso_list_of_event_dates__date_format', $date_format); |
|
| 466 | + $time_format = apply_filters('FHEE__espresso_list_of_event_dates__time_format', $time_format); |
|
| 467 | + $datetimes = EEH_Event_View::get_all_date_obj($EVT_ID, $show_expired, FALSE, $limit); |
|
| 468 | + if ( ! $format) { |
|
| 469 | + return apply_filters('FHEE__espresso_list_of_event_dates__datetimes', $datetimes); |
|
| 470 | 470 | } |
| 471 | 471 | //d( $datetimes ); |
| 472 | - if ( is_array( $datetimes ) && ! empty( $datetimes )) { |
|
| 472 | + if (is_array($datetimes) && ! empty($datetimes)) { |
|
| 473 | 473 | global $post; |
| 474 | - $html = $format ? '<ul id="ee-event-datetimes-ul-' . $post->ID . '" class="ee-event-datetimes-ul ee-clearfix">' : ''; |
|
| 475 | - foreach ( $datetimes as $datetime ) { |
|
| 476 | - if ( $datetime instanceof EE_Datetime ) { |
|
| 477 | - $html .= '<li id="ee-event-datetimes-li-' . $datetime->ID(); |
|
| 478 | - $html .= '" class="ee-event-datetimes-li ee-event-datetimes-li-' . $datetime->get_active_status() . '">'; |
|
| 474 | + $html = $format ? '<ul id="ee-event-datetimes-ul-'.$post->ID.'" class="ee-event-datetimes-ul ee-clearfix">' : ''; |
|
| 475 | + foreach ($datetimes as $datetime) { |
|
| 476 | + if ($datetime instanceof EE_Datetime) { |
|
| 477 | + $html .= '<li id="ee-event-datetimes-li-'.$datetime->ID(); |
|
| 478 | + $html .= '" class="ee-event-datetimes-li ee-event-datetimes-li-'.$datetime->get_active_status().'">'; |
|
| 479 | 479 | $datetime_name = $datetime->name(); |
| 480 | - $html .= ! empty( $datetime_name ) ? '<strong>' . $datetime_name . '</strong>' : ''; |
|
| 481 | - $html .= ! empty( $datetime_name ) && $add_breaks ? '<br />' : ''; |
|
| 482 | - $html .= '<span class="dashicons dashicons-calendar"></span><span class="ee-event-datetimes-li-daterange">' . $datetime->date_range( $date_format ) . '</span><br/>'; |
|
| 483 | - $html .= '<span class="dashicons dashicons-clock"></span><span class="ee-event-datetimes-li-timerange">' . $datetime->time_range( $time_format ) . '</span>'; |
|
| 480 | + $html .= ! empty($datetime_name) ? '<strong>'.$datetime_name.'</strong>' : ''; |
|
| 481 | + $html .= ! empty($datetime_name) && $add_breaks ? '<br />' : ''; |
|
| 482 | + $html .= '<span class="dashicons dashicons-calendar"></span><span class="ee-event-datetimes-li-daterange">'.$datetime->date_range($date_format).'</span><br/>'; |
|
| 483 | + $html .= '<span class="dashicons dashicons-clock"></span><span class="ee-event-datetimes-li-timerange">'.$datetime->time_range($time_format).'</span>'; |
|
| 484 | 484 | $datetime_description = $datetime->description(); |
| 485 | - $html .= ! empty( $datetime_description ) && $add_breaks ? '<br />' : ''; |
|
| 486 | - $html .= ! empty( $datetime_description ) ? ' - ' . $datetime_description : ''; |
|
| 487 | - $html = apply_filters( 'FHEE__espresso_list_of_event_dates__datetime_html', $html, $datetime ); |
|
| 485 | + $html .= ! empty($datetime_description) && $add_breaks ? '<br />' : ''; |
|
| 486 | + $html .= ! empty($datetime_description) ? ' - '.$datetime_description : ''; |
|
| 487 | + $html = apply_filters('FHEE__espresso_list_of_event_dates__datetime_html', $html, $datetime); |
|
| 488 | 488 | $html .= '</li>'; |
| 489 | 489 | } |
| 490 | 490 | } |
| 491 | 491 | $html .= $format ? '</ul>' : ''; |
| 492 | 492 | } else { |
| 493 | - $html = $format ? '<p><span class="dashicons dashicons-marker pink-text"></span>' . esc_html__( 'There are no upcoming dates for this event.', 'event_espresso' ) . '</p><br/>' : ''; |
|
| 493 | + $html = $format ? '<p><span class="dashicons dashicons-marker pink-text"></span>'.esc_html__('There are no upcoming dates for this event.', 'event_espresso').'</p><br/>' : ''; |
|
| 494 | 494 | } |
| 495 | - if ( $echo ) { |
|
| 495 | + if ($echo) { |
|
| 496 | 496 | echo $html; |
| 497 | 497 | return ''; |
| 498 | 498 | } |
@@ -501,7 +501,7 @@ discard block |
||
| 501 | 501 | } |
| 502 | 502 | |
| 503 | 503 | |
| 504 | -if ( ! function_exists( 'espresso_event_end_date' )) { |
|
| 504 | +if ( ! function_exists('espresso_event_end_date')) { |
|
| 505 | 505 | /** |
| 506 | 506 | * espresso_event_end_date |
| 507 | 507 | * returns the last date for an event |
@@ -512,20 +512,20 @@ discard block |
||
| 512 | 512 | * @param bool $echo |
| 513 | 513 | * @return string |
| 514 | 514 | */ |
| 515 | - function espresso_event_end_date( $date_format = '', $time_format = '', $EVT_ID = FALSE, $echo = TRUE ) { |
|
| 516 | - $date_format = ! empty( $date_format ) ? $date_format : get_option( 'date_format' ); |
|
| 517 | - $time_format = ! empty( $time_format ) ? $time_format : get_option( 'time_format' ); |
|
| 518 | - $date_format = apply_filters( 'FHEE__espresso_event_end_date__date_format', $date_format ); |
|
| 519 | - $time_format = apply_filters( 'FHEE__espresso_event_end_date__time_format', $time_format ); |
|
| 520 | - if($echo){ |
|
| 521 | - echo EEH_Event_View::the_event_end_date( $date_format, $time_format, $EVT_ID ); |
|
| 515 | + function espresso_event_end_date($date_format = '', $time_format = '', $EVT_ID = FALSE, $echo = TRUE) { |
|
| 516 | + $date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
| 517 | + $time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
| 518 | + $date_format = apply_filters('FHEE__espresso_event_end_date__date_format', $date_format); |
|
| 519 | + $time_format = apply_filters('FHEE__espresso_event_end_date__time_format', $time_format); |
|
| 520 | + if ($echo) { |
|
| 521 | + echo EEH_Event_View::the_event_end_date($date_format, $time_format, $EVT_ID); |
|
| 522 | 522 | return ''; |
| 523 | 523 | } |
| 524 | - return EEH_Event_View::the_event_end_date( $date_format, $time_format, $EVT_ID ); |
|
| 524 | + return EEH_Event_View::the_event_end_date($date_format, $time_format, $EVT_ID); |
|
| 525 | 525 | } |
| 526 | 526 | } |
| 527 | 527 | |
| 528 | -if ( ! function_exists( 'espresso_event_date_range' )) { |
|
| 528 | +if ( ! function_exists('espresso_event_date_range')) { |
|
| 529 | 529 | /** |
| 530 | 530 | * espresso_event_date_range |
| 531 | 531 | * returns the first and last chronologically ordered dates for an event (if different) |
@@ -538,32 +538,32 @@ discard block |
||
| 538 | 538 | * @param bool $echo |
| 539 | 539 | * @return string |
| 540 | 540 | */ |
| 541 | - function espresso_event_date_range( $date_format = '', $time_format = '', $single_date_format = '', $single_time_format = '', $EVT_ID = FALSE, $echo = TRUE ) { |
|
| 541 | + function espresso_event_date_range($date_format = '', $time_format = '', $single_date_format = '', $single_time_format = '', $EVT_ID = FALSE, $echo = TRUE) { |
|
| 542 | 542 | // set and filter date and time formats when a range is returned |
| 543 | - $date_format = ! empty( $date_format ) ? $date_format : get_option( 'date_format' ); |
|
| 544 | - $date_format = apply_filters( 'FHEE__espresso_event_date_range__date_format', $date_format ); |
|
| 543 | + $date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
| 544 | + $date_format = apply_filters('FHEE__espresso_event_date_range__date_format', $date_format); |
|
| 545 | 545 | // get the start and end date with NO time portion |
| 546 | - $the_event_date = EEH_Event_View::the_earliest_event_date( $date_format, '', $EVT_ID ); |
|
| 547 | - $the_event_end_date = EEH_Event_View::the_latest_event_date( $date_format, '', $EVT_ID ); |
|
| 546 | + $the_event_date = EEH_Event_View::the_earliest_event_date($date_format, '', $EVT_ID); |
|
| 547 | + $the_event_end_date = EEH_Event_View::the_latest_event_date($date_format, '', $EVT_ID); |
|
| 548 | 548 | // now we can determine if date range spans more than one day |
| 549 | - if ( $the_event_date != $the_event_end_date ) { |
|
| 550 | - $time_format = ! empty( $time_format ) ? $time_format : get_option( 'time_format' ); |
|
| 551 | - $time_format = apply_filters( 'FHEE__espresso_event_date_range__time_format', $time_format ); |
|
| 549 | + if ($the_event_date != $the_event_end_date) { |
|
| 550 | + $time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
| 551 | + $time_format = apply_filters('FHEE__espresso_event_date_range__time_format', $time_format); |
|
| 552 | 552 | $html = sprintf( |
| 553 | 553 | /* translators: 1: first event date, 2: last event date */ |
| 554 | - esc_html__( '%1$s - %2$s', 'event_espresso' ), |
|
| 555 | - EEH_Event_View::the_earliest_event_date( $date_format, $time_format, $EVT_ID ), |
|
| 556 | - EEH_Event_View::the_latest_event_date( $date_format, $time_format, $EVT_ID ) |
|
| 554 | + esc_html__('%1$s - %2$s', 'event_espresso'), |
|
| 555 | + EEH_Event_View::the_earliest_event_date($date_format, $time_format, $EVT_ID), |
|
| 556 | + EEH_Event_View::the_latest_event_date($date_format, $time_format, $EVT_ID) |
|
| 557 | 557 | ); |
| 558 | 558 | } else { |
| 559 | 559 | // set and filter date and time formats when only a single datetime is returned |
| 560 | - $single_date_format = ! empty( $single_date_format ) ? $single_date_format : get_option( 'date_format' ); |
|
| 561 | - $single_time_format = ! empty( $single_time_format ) ? $single_time_format : get_option( 'time_format' ); |
|
| 562 | - $single_date_format = apply_filters( 'FHEE__espresso_event_date_range__single_date_format', $single_date_format ); |
|
| 563 | - $single_time_format = apply_filters( 'FHEE__espresso_event_date_range__single_time_format', $single_time_format ); |
|
| 564 | - $html = EEH_Event_View::the_earliest_event_date( $single_date_format, $single_time_format, $EVT_ID ); |
|
| 560 | + $single_date_format = ! empty($single_date_format) ? $single_date_format : get_option('date_format'); |
|
| 561 | + $single_time_format = ! empty($single_time_format) ? $single_time_format : get_option('time_format'); |
|
| 562 | + $single_date_format = apply_filters('FHEE__espresso_event_date_range__single_date_format', $single_date_format); |
|
| 563 | + $single_time_format = apply_filters('FHEE__espresso_event_date_range__single_time_format', $single_time_format); |
|
| 564 | + $html = EEH_Event_View::the_earliest_event_date($single_date_format, $single_time_format, $EVT_ID); |
|
| 565 | 565 | } |
| 566 | - if ( $echo ) { |
|
| 566 | + if ($echo) { |
|
| 567 | 567 | echo $html; |
| 568 | 568 | return ''; |
| 569 | 569 | } |
@@ -571,7 +571,7 @@ discard block |
||
| 571 | 571 | } |
| 572 | 572 | } |
| 573 | 573 | |
| 574 | -if ( ! function_exists( 'espresso_next_upcoming_datetime_obj' )) { |
|
| 574 | +if ( ! function_exists('espresso_next_upcoming_datetime_obj')) { |
|
| 575 | 575 | /** |
| 576 | 576 | * espresso_next_upcoming_datetime_obj |
| 577 | 577 | * returns the next upcoming datetime object for an event |
@@ -579,12 +579,12 @@ discard block |
||
| 579 | 579 | * @param int $EVT_ID |
| 580 | 580 | * @return EE_Datetime|null |
| 581 | 581 | */ |
| 582 | - function espresso_next_upcoming_datetime_obj( $EVT_ID = 0 ) { |
|
| 583 | - return EEH_Event_View::get_next_upcoming_date_obj( $EVT_ID ); |
|
| 582 | + function espresso_next_upcoming_datetime_obj($EVT_ID = 0) { |
|
| 583 | + return EEH_Event_View::get_next_upcoming_date_obj($EVT_ID); |
|
| 584 | 584 | } |
| 585 | 585 | } |
| 586 | 586 | |
| 587 | -if ( ! function_exists( 'espresso_next_upcoming_datetime' ) ) { |
|
| 587 | +if ( ! function_exists('espresso_next_upcoming_datetime')) { |
|
| 588 | 588 | /** |
| 589 | 589 | * espresso_next_upcoming_datetime |
| 590 | 590 | * returns the start date and time for the next upcoming event. |
@@ -595,30 +595,30 @@ discard block |
||
| 595 | 595 | * @param bool $echo |
| 596 | 596 | * @return string |
| 597 | 597 | */ |
| 598 | - function espresso_next_upcoming_datetime( $date_format = '', $time_format = '', $EVT_ID = 0, $echo = true ) { |
|
| 598 | + function espresso_next_upcoming_datetime($date_format = '', $time_format = '', $EVT_ID = 0, $echo = true) { |
|
| 599 | 599 | |
| 600 | - $date_format = ! empty( $date_format ) ? $date_format : get_option( 'date_format' ); |
|
| 601 | - $date_format = apply_filters( 'FHEE__espresso_next_upcoming_datetime__date_format', $date_format ); |
|
| 600 | + $date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
| 601 | + $date_format = apply_filters('FHEE__espresso_next_upcoming_datetime__date_format', $date_format); |
|
| 602 | 602 | |
| 603 | - $time_format = ! empty( $time_format ) ? $time_format : get_option( 'time_format' ); |
|
| 604 | - $time_format = apply_filters( 'FHEE__espresso_next_upcoming_datetime__time_format', $time_format ); |
|
| 603 | + $time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
| 604 | + $time_format = apply_filters('FHEE__espresso_next_upcoming_datetime__time_format', $time_format); |
|
| 605 | 605 | |
| 606 | - $datetime_format = trim( $date_format . ' ' . $time_format); |
|
| 606 | + $datetime_format = trim($date_format.' '.$time_format); |
|
| 607 | 607 | |
| 608 | - $datetime = espresso_next_upcoming_datetime_obj( $EVT_ID ); |
|
| 608 | + $datetime = espresso_next_upcoming_datetime_obj($EVT_ID); |
|
| 609 | 609 | |
| 610 | - if( ! $datetime instanceof EE_Datetime ) { |
|
| 610 | + if ( ! $datetime instanceof EE_Datetime) { |
|
| 611 | 611 | return ''; |
| 612 | 612 | } |
| 613 | - if ( $echo ){ |
|
| 614 | - echo $datetime->get_i18n_datetime( 'DTT_EVT_start', $datetime_format ); |
|
| 613 | + if ($echo) { |
|
| 614 | + echo $datetime->get_i18n_datetime('DTT_EVT_start', $datetime_format); |
|
| 615 | 615 | return ''; |
| 616 | 616 | } |
| 617 | - return $datetime->get_i18n_datetime( 'DTT_EVT_start', $datetime_format ); |
|
| 617 | + return $datetime->get_i18n_datetime('DTT_EVT_start', $datetime_format); |
|
| 618 | 618 | } |
| 619 | 619 | } |
| 620 | 620 | |
| 621 | -if ( ! function_exists( 'espresso_event_date_as_calendar_page' )) { |
|
| 621 | +if ( ! function_exists('espresso_event_date_as_calendar_page')) { |
|
| 622 | 622 | /** |
| 623 | 623 | * espresso_event_date_as_calendar_page |
| 624 | 624 | * returns the primary date for an event, stylized to appear as the page of a calendar |
@@ -626,15 +626,15 @@ discard block |
||
| 626 | 626 | * @param bool $EVT_ID |
| 627 | 627 | * @return string |
| 628 | 628 | */ |
| 629 | - function espresso_event_date_as_calendar_page( $EVT_ID = FALSE ) { |
|
| 630 | - EEH_Event_View::event_date_as_calendar_page( $EVT_ID ); |
|
| 629 | + function espresso_event_date_as_calendar_page($EVT_ID = FALSE) { |
|
| 630 | + EEH_Event_View::event_date_as_calendar_page($EVT_ID); |
|
| 631 | 631 | } |
| 632 | 632 | } |
| 633 | 633 | |
| 634 | 634 | |
| 635 | 635 | |
| 636 | 636 | |
| 637 | -if ( ! function_exists( 'espresso_event_link_url' )) { |
|
| 637 | +if ( ! function_exists('espresso_event_link_url')) { |
|
| 638 | 638 | /** |
| 639 | 639 | * espresso_event_link_url |
| 640 | 640 | * |
@@ -642,18 +642,18 @@ discard block |
||
| 642 | 642 | * @param bool $echo |
| 643 | 643 | * @return string |
| 644 | 644 | */ |
| 645 | - function espresso_event_link_url( $EVT_ID = 0, $echo = TRUE ) { |
|
| 646 | - if ( $echo ) { |
|
| 647 | - echo EEH_Event_View::event_link_url( $EVT_ID ); |
|
| 645 | + function espresso_event_link_url($EVT_ID = 0, $echo = TRUE) { |
|
| 646 | + if ($echo) { |
|
| 647 | + echo EEH_Event_View::event_link_url($EVT_ID); |
|
| 648 | 648 | return ''; |
| 649 | 649 | } |
| 650 | - return EEH_Event_View::event_link_url( $EVT_ID ); |
|
| 650 | + return EEH_Event_View::event_link_url($EVT_ID); |
|
| 651 | 651 | } |
| 652 | 652 | } |
| 653 | 653 | |
| 654 | 654 | |
| 655 | 655 | |
| 656 | -if ( ! function_exists( 'espresso_event_has_content_or_excerpt' )) { |
|
| 656 | +if ( ! function_exists('espresso_event_has_content_or_excerpt')) { |
|
| 657 | 657 | /** |
| 658 | 658 | * espresso_event_has_content_or_excerpt |
| 659 | 659 | * |
@@ -661,15 +661,15 @@ discard block |
||
| 661 | 661 | * @param bool $EVT_ID |
| 662 | 662 | * @return boolean |
| 663 | 663 | */ |
| 664 | - function espresso_event_has_content_or_excerpt( $EVT_ID = FALSE ) { |
|
| 665 | - return EEH_Event_View::event_has_content_or_excerpt( $EVT_ID ); |
|
| 664 | + function espresso_event_has_content_or_excerpt($EVT_ID = FALSE) { |
|
| 665 | + return EEH_Event_View::event_has_content_or_excerpt($EVT_ID); |
|
| 666 | 666 | } |
| 667 | 667 | } |
| 668 | 668 | |
| 669 | 669 | |
| 670 | 670 | |
| 671 | 671 | |
| 672 | -if ( ! function_exists( 'espresso_event_content_or_excerpt' )) { |
|
| 672 | +if ( ! function_exists('espresso_event_content_or_excerpt')) { |
|
| 673 | 673 | /** |
| 674 | 674 | * espresso_event_content_or_excerpt |
| 675 | 675 | * |
@@ -678,18 +678,18 @@ discard block |
||
| 678 | 678 | * @param bool $echo |
| 679 | 679 | * @return string |
| 680 | 680 | */ |
| 681 | - function espresso_event_content_or_excerpt( $num_words = 55, $more = NULL, $echo = TRUE ) { |
|
| 682 | - if ( $echo ) { |
|
| 683 | - echo EEH_Event_View::event_content_or_excerpt( $num_words, $more ); |
|
| 681 | + function espresso_event_content_or_excerpt($num_words = 55, $more = NULL, $echo = TRUE) { |
|
| 682 | + if ($echo) { |
|
| 683 | + echo EEH_Event_View::event_content_or_excerpt($num_words, $more); |
|
| 684 | 684 | return ''; |
| 685 | 685 | } |
| 686 | - return EEH_Event_View::event_content_or_excerpt( $num_words, $more ); |
|
| 686 | + return EEH_Event_View::event_content_or_excerpt($num_words, $more); |
|
| 687 | 687 | } |
| 688 | 688 | } |
| 689 | 689 | |
| 690 | 690 | |
| 691 | 691 | |
| 692 | -if ( ! function_exists( 'espresso_event_phone' )) { |
|
| 692 | +if ( ! function_exists('espresso_event_phone')) { |
|
| 693 | 693 | /** |
| 694 | 694 | * espresso_event_phone |
| 695 | 695 | * |
@@ -697,18 +697,18 @@ discard block |
||
| 697 | 697 | * @param bool $echo |
| 698 | 698 | * @return string |
| 699 | 699 | */ |
| 700 | - function espresso_event_phone( $EVT_ID = 0, $echo = TRUE ) { |
|
| 701 | - if ( $echo ) { |
|
| 702 | - echo EEH_Event_View::event_phone( $EVT_ID ); |
|
| 700 | + function espresso_event_phone($EVT_ID = 0, $echo = TRUE) { |
|
| 701 | + if ($echo) { |
|
| 702 | + echo EEH_Event_View::event_phone($EVT_ID); |
|
| 703 | 703 | return ''; |
| 704 | 704 | } |
| 705 | - return EEH_Event_View::event_phone( $EVT_ID ); |
|
| 705 | + return EEH_Event_View::event_phone($EVT_ID); |
|
| 706 | 706 | } |
| 707 | 707 | } |
| 708 | 708 | |
| 709 | 709 | |
| 710 | 710 | |
| 711 | -if ( ! function_exists( 'espresso_edit_event_link' )) { |
|
| 711 | +if ( ! function_exists('espresso_edit_event_link')) { |
|
| 712 | 712 | /** |
| 713 | 713 | * espresso_edit_event_link |
| 714 | 714 | * returns a link to edit an event |
@@ -717,39 +717,39 @@ discard block |
||
| 717 | 717 | * @param bool $echo |
| 718 | 718 | * @return string |
| 719 | 719 | */ |
| 720 | - function espresso_edit_event_link( $EVT_ID = 0, $echo = TRUE ) { |
|
| 721 | - if ( $echo ) { |
|
| 722 | - echo EEH_Event_View::edit_event_link( $EVT_ID ); |
|
| 720 | + function espresso_edit_event_link($EVT_ID = 0, $echo = TRUE) { |
|
| 721 | + if ($echo) { |
|
| 722 | + echo EEH_Event_View::edit_event_link($EVT_ID); |
|
| 723 | 723 | return ''; |
| 724 | 724 | } |
| 725 | - return EEH_Event_View::edit_event_link( $EVT_ID ); |
|
| 725 | + return EEH_Event_View::edit_event_link($EVT_ID); |
|
| 726 | 726 | } |
| 727 | 727 | } |
| 728 | 728 | |
| 729 | 729 | |
| 730 | -if ( ! function_exists( 'espresso_organization_name' )) { |
|
| 730 | +if ( ! function_exists('espresso_organization_name')) { |
|
| 731 | 731 | /** |
| 732 | 732 | * espresso_organization_name |
| 733 | 733 | * @param bool $echo |
| 734 | 734 | * @return string |
| 735 | 735 | */ |
| 736 | 736 | function espresso_organization_name($echo = TRUE) { |
| 737 | - if($echo){ |
|
| 738 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'name' ); |
|
| 737 | + if ($echo) { |
|
| 738 | + echo EE_Registry::instance()->CFG->organization->get_pretty('name'); |
|
| 739 | 739 | return ''; |
| 740 | 740 | } |
| 741 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'name' ); |
|
| 741 | + return EE_Registry::instance()->CFG->organization->get_pretty('name'); |
|
| 742 | 742 | } |
| 743 | 743 | } |
| 744 | 744 | |
| 745 | -if ( ! function_exists( 'espresso_organization_address' )) { |
|
| 745 | +if ( ! function_exists('espresso_organization_address')) { |
|
| 746 | 746 | /** |
| 747 | 747 | * espresso_organization_address |
| 748 | 748 | * @param string $type |
| 749 | 749 | * @return string |
| 750 | 750 | */ |
| 751 | - function espresso_organization_address( $type = 'inline' ) { |
|
| 752 | - if ( EE_Registry::instance()->CFG->organization instanceof EE_Organization_Config ) { |
|
| 751 | + function espresso_organization_address($type = 'inline') { |
|
| 752 | + if (EE_Registry::instance()->CFG->organization instanceof EE_Organization_Config) { |
|
| 753 | 753 | $address = new EventEspresso\core\domain\entities\GenericAddress( |
| 754 | 754 | EE_Registry::instance()->CFG->organization->address_1, |
| 755 | 755 | EE_Registry::instance()->CFG->organization->address_2, |
@@ -758,129 +758,129 @@ discard block |
||
| 758 | 758 | EE_Registry::instance()->CFG->organization->zip, |
| 759 | 759 | EE_Registry::instance()->CFG->organization->CNT_ISO |
| 760 | 760 | ); |
| 761 | - return EEH_Address::format( $address, $type ); |
|
| 761 | + return EEH_Address::format($address, $type); |
|
| 762 | 762 | } |
| 763 | 763 | return ''; |
| 764 | 764 | } |
| 765 | 765 | } |
| 766 | 766 | |
| 767 | -if ( ! function_exists( 'espresso_organization_email' )) { |
|
| 767 | +if ( ! function_exists('espresso_organization_email')) { |
|
| 768 | 768 | /** |
| 769 | 769 | * espresso_organization_email |
| 770 | 770 | * @param bool $echo |
| 771 | 771 | * @return string |
| 772 | 772 | */ |
| 773 | - function espresso_organization_email( $echo = TRUE ) { |
|
| 774 | - if($echo){ |
|
| 775 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'email' ); |
|
| 773 | + function espresso_organization_email($echo = TRUE) { |
|
| 774 | + if ($echo) { |
|
| 775 | + echo EE_Registry::instance()->CFG->organization->get_pretty('email'); |
|
| 776 | 776 | return ''; |
| 777 | 777 | } |
| 778 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'email' ); |
|
| 778 | + return EE_Registry::instance()->CFG->organization->get_pretty('email'); |
|
| 779 | 779 | } |
| 780 | 780 | } |
| 781 | 781 | |
| 782 | -if ( ! function_exists( 'espresso_organization_logo_url' )) { |
|
| 782 | +if ( ! function_exists('espresso_organization_logo_url')) { |
|
| 783 | 783 | /** |
| 784 | 784 | * espresso_organization_logo_url |
| 785 | 785 | * @param bool $echo |
| 786 | 786 | * @return string |
| 787 | 787 | */ |
| 788 | - function espresso_organization_logo_url( $echo = TRUE ) { |
|
| 789 | - if($echo){ |
|
| 790 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'logo_url' ); |
|
| 788 | + function espresso_organization_logo_url($echo = TRUE) { |
|
| 789 | + if ($echo) { |
|
| 790 | + echo EE_Registry::instance()->CFG->organization->get_pretty('logo_url'); |
|
| 791 | 791 | return ''; |
| 792 | 792 | } |
| 793 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'logo_url' ); |
|
| 793 | + return EE_Registry::instance()->CFG->organization->get_pretty('logo_url'); |
|
| 794 | 794 | } |
| 795 | 795 | } |
| 796 | 796 | |
| 797 | -if ( ! function_exists( 'espresso_organization_facebook' )) { |
|
| 797 | +if ( ! function_exists('espresso_organization_facebook')) { |
|
| 798 | 798 | /** |
| 799 | 799 | * espresso_organization_facebook |
| 800 | 800 | * @param bool $echo |
| 801 | 801 | * @return string |
| 802 | 802 | */ |
| 803 | - function espresso_organization_facebook( $echo = TRUE ) { |
|
| 804 | - if($echo){ |
|
| 805 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'facebook' ); |
|
| 803 | + function espresso_organization_facebook($echo = TRUE) { |
|
| 804 | + if ($echo) { |
|
| 805 | + echo EE_Registry::instance()->CFG->organization->get_pretty('facebook'); |
|
| 806 | 806 | return ''; |
| 807 | 807 | } |
| 808 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'facebook' ); |
|
| 808 | + return EE_Registry::instance()->CFG->organization->get_pretty('facebook'); |
|
| 809 | 809 | } |
| 810 | 810 | } |
| 811 | 811 | |
| 812 | -if ( ! function_exists( 'espresso_organization_twitter' )) { |
|
| 812 | +if ( ! function_exists('espresso_organization_twitter')) { |
|
| 813 | 813 | /** |
| 814 | 814 | * espresso_organization_twitter |
| 815 | 815 | * @param bool $echo |
| 816 | 816 | * @return string |
| 817 | 817 | */ |
| 818 | - function espresso_organization_twitter( $echo = TRUE ) { |
|
| 819 | - if($echo){ |
|
| 820 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'twitter' ); |
|
| 818 | + function espresso_organization_twitter($echo = TRUE) { |
|
| 819 | + if ($echo) { |
|
| 820 | + echo EE_Registry::instance()->CFG->organization->get_pretty('twitter'); |
|
| 821 | 821 | return ''; |
| 822 | 822 | } |
| 823 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'twitter' ); |
|
| 823 | + return EE_Registry::instance()->CFG->organization->get_pretty('twitter'); |
|
| 824 | 824 | } |
| 825 | 825 | } |
| 826 | 826 | |
| 827 | -if ( ! function_exists( 'espresso_organization_linkedin' )) { |
|
| 827 | +if ( ! function_exists('espresso_organization_linkedin')) { |
|
| 828 | 828 | /** |
| 829 | 829 | * espresso_organization_linkedin |
| 830 | 830 | * @param bool $echo |
| 831 | 831 | * @return string |
| 832 | 832 | */ |
| 833 | - function espresso_organization_linkedin( $echo = TRUE ) { |
|
| 834 | - if($echo){ |
|
| 835 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'linkedin' ); |
|
| 833 | + function espresso_organization_linkedin($echo = TRUE) { |
|
| 834 | + if ($echo) { |
|
| 835 | + echo EE_Registry::instance()->CFG->organization->get_pretty('linkedin'); |
|
| 836 | 836 | return ''; |
| 837 | 837 | } |
| 838 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'linkedin' ); |
|
| 838 | + return EE_Registry::instance()->CFG->organization->get_pretty('linkedin'); |
|
| 839 | 839 | } |
| 840 | 840 | } |
| 841 | 841 | |
| 842 | -if ( ! function_exists( 'espresso_organization_pinterest' )) { |
|
| 842 | +if ( ! function_exists('espresso_organization_pinterest')) { |
|
| 843 | 843 | /** |
| 844 | 844 | * espresso_organization_pinterest |
| 845 | 845 | * @param bool $echo |
| 846 | 846 | * @return string |
| 847 | 847 | */ |
| 848 | - function espresso_organization_pinterest( $echo = TRUE ) { |
|
| 849 | - if($echo){ |
|
| 850 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'pinterest' ); |
|
| 848 | + function espresso_organization_pinterest($echo = TRUE) { |
|
| 849 | + if ($echo) { |
|
| 850 | + echo EE_Registry::instance()->CFG->organization->get_pretty('pinterest'); |
|
| 851 | 851 | return ''; |
| 852 | 852 | } |
| 853 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'pinterest' ); |
|
| 853 | + return EE_Registry::instance()->CFG->organization->get_pretty('pinterest'); |
|
| 854 | 854 | } |
| 855 | 855 | } |
| 856 | 856 | |
| 857 | -if ( ! function_exists( 'espresso_organization_google' )) { |
|
| 857 | +if ( ! function_exists('espresso_organization_google')) { |
|
| 858 | 858 | /** |
| 859 | 859 | * espresso_organization_google |
| 860 | 860 | * @param bool $echo |
| 861 | 861 | * @return string |
| 862 | 862 | */ |
| 863 | - function espresso_organization_google( $echo = TRUE ) { |
|
| 864 | - if($echo){ |
|
| 865 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'google' ); |
|
| 863 | + function espresso_organization_google($echo = TRUE) { |
|
| 864 | + if ($echo) { |
|
| 865 | + echo EE_Registry::instance()->CFG->organization->get_pretty('google'); |
|
| 866 | 866 | return ''; |
| 867 | 867 | } |
| 868 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'google' ); |
|
| 868 | + return EE_Registry::instance()->CFG->organization->get_pretty('google'); |
|
| 869 | 869 | } |
| 870 | 870 | } |
| 871 | 871 | |
| 872 | -if ( ! function_exists( 'espresso_organization_instagram' )) { |
|
| 872 | +if ( ! function_exists('espresso_organization_instagram')) { |
|
| 873 | 873 | /** |
| 874 | 874 | * espresso_organization_instagram |
| 875 | 875 | * @param bool $echo |
| 876 | 876 | * @return string |
| 877 | 877 | */ |
| 878 | - function espresso_organization_instagram( $echo = TRUE ) { |
|
| 879 | - if($echo){ |
|
| 880 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'instagram' ); |
|
| 878 | + function espresso_organization_instagram($echo = TRUE) { |
|
| 879 | + if ($echo) { |
|
| 880 | + echo EE_Registry::instance()->CFG->organization->get_pretty('instagram'); |
|
| 881 | 881 | return ''; |
| 882 | 882 | } |
| 883 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'instagram' ); |
|
| 883 | + return EE_Registry::instance()->CFG->organization->get_pretty('instagram'); |
|
| 884 | 884 | } |
| 885 | 885 | } |
| 886 | 886 | |
@@ -890,7 +890,7 @@ discard block |
||
| 890 | 890 | |
| 891 | 891 | |
| 892 | 892 | |
| 893 | -if ( ! function_exists( 'espresso_event_venues' )) { |
|
| 893 | +if ( ! function_exists('espresso_event_venues')) { |
|
| 894 | 894 | /** |
| 895 | 895 | * espresso_event_venues |
| 896 | 896 | * |
@@ -904,7 +904,7 @@ discard block |
||
| 904 | 904 | |
| 905 | 905 | |
| 906 | 906 | |
| 907 | -if ( ! function_exists( 'espresso_venue_id' )) { |
|
| 907 | +if ( ! function_exists('espresso_venue_id')) { |
|
| 908 | 908 | /** |
| 909 | 909 | * espresso_venue_name |
| 910 | 910 | * |
@@ -912,15 +912,15 @@ discard block |
||
| 912 | 912 | * @param int $EVT_ID |
| 913 | 913 | * @return string |
| 914 | 914 | */ |
| 915 | - function espresso_venue_id( $EVT_ID = 0 ) { |
|
| 916 | - $venue = EEH_Venue_View::get_venue( $EVT_ID ); |
|
| 915 | + function espresso_venue_id($EVT_ID = 0) { |
|
| 916 | + $venue = EEH_Venue_View::get_venue($EVT_ID); |
|
| 917 | 917 | return $venue instanceof EE_Venue ? $venue->ID() : 0; |
| 918 | 918 | } |
| 919 | 919 | } |
| 920 | 920 | |
| 921 | 921 | |
| 922 | 922 | |
| 923 | -if ( ! function_exists( 'espresso_is_venue_private' ) ) { |
|
| 923 | +if ( ! function_exists('espresso_is_venue_private')) { |
|
| 924 | 924 | /** |
| 925 | 925 | * Return whether a venue is private or not. |
| 926 | 926 | * @see EEH_Venue_View::get_venue() for more info on expected return results. |
@@ -929,45 +929,45 @@ discard block |
||
| 929 | 929 | * |
| 930 | 930 | * @return bool | null |
| 931 | 931 | */ |
| 932 | - function espresso_is_venue_private( $VNU_ID = 0 ) { |
|
| 933 | - return EEH_Venue_View::is_venue_private( $VNU_ID ); |
|
| 932 | + function espresso_is_venue_private($VNU_ID = 0) { |
|
| 933 | + return EEH_Venue_View::is_venue_private($VNU_ID); |
|
| 934 | 934 | } |
| 935 | 935 | } |
| 936 | 936 | |
| 937 | 937 | |
| 938 | 938 | |
| 939 | -if ( ! function_exists( 'espresso_venue_is_password_protected' ) ) { |
|
| 939 | +if ( ! function_exists('espresso_venue_is_password_protected')) { |
|
| 940 | 940 | /** |
| 941 | 941 | * returns true or false if a venue is password protected or not |
| 942 | 942 | * |
| 943 | 943 | * @param int $VNU_ID optional, the venue id to check. |
| 944 | 944 | * @return string |
| 945 | 945 | */ |
| 946 | - function espresso_venue_is_password_protected( $VNU_ID = 0 ) { |
|
| 947 | - EE_Registry::instance()->load_helper( 'Venue_View' ); |
|
| 948 | - return EEH_Venue_View::is_venue_password_protected( $VNU_ID ); |
|
| 946 | + function espresso_venue_is_password_protected($VNU_ID = 0) { |
|
| 947 | + EE_Registry::instance()->load_helper('Venue_View'); |
|
| 948 | + return EEH_Venue_View::is_venue_password_protected($VNU_ID); |
|
| 949 | 949 | } |
| 950 | 950 | } |
| 951 | 951 | |
| 952 | 952 | |
| 953 | 953 | |
| 954 | -if ( ! function_exists( 'espresso_password_protected_venue_form' ) ) { |
|
| 954 | +if ( ! function_exists('espresso_password_protected_venue_form')) { |
|
| 955 | 955 | /** |
| 956 | 956 | * Returns a password form if venue is password protected. |
| 957 | 957 | * |
| 958 | 958 | * @param int $VNU_ID optional, the venue id to check. |
| 959 | 959 | * @return string |
| 960 | 960 | */ |
| 961 | - function espresso_password_protected_venue_form( $VNU_ID = 0 ) { |
|
| 962 | - EE_Registry::instance()->load_helper( 'Venue_View' ); |
|
| 963 | - return EEH_Venue_View::password_protected_venue_form( $VNU_ID ); |
|
| 961 | + function espresso_password_protected_venue_form($VNU_ID = 0) { |
|
| 962 | + EE_Registry::instance()->load_helper('Venue_View'); |
|
| 963 | + return EEH_Venue_View::password_protected_venue_form($VNU_ID); |
|
| 964 | 964 | } |
| 965 | 965 | } |
| 966 | 966 | |
| 967 | 967 | |
| 968 | 968 | |
| 969 | 969 | |
| 970 | -if ( ! function_exists( 'espresso_venue_name' )) { |
|
| 970 | +if ( ! function_exists('espresso_venue_name')) { |
|
| 971 | 971 | /** |
| 972 | 972 | * espresso_venue_name |
| 973 | 973 | * |
@@ -977,19 +977,19 @@ discard block |
||
| 977 | 977 | * @param bool $echo |
| 978 | 978 | * @return string |
| 979 | 979 | */ |
| 980 | - function espresso_venue_name( $VNU_ID = 0, $link_to = 'details', $echo = TRUE ) { |
|
| 981 | - if($echo){ |
|
| 982 | - echo EEH_Venue_View::venue_name( $link_to, $VNU_ID ); |
|
| 980 | + function espresso_venue_name($VNU_ID = 0, $link_to = 'details', $echo = TRUE) { |
|
| 981 | + if ($echo) { |
|
| 982 | + echo EEH_Venue_View::venue_name($link_to, $VNU_ID); |
|
| 983 | 983 | return ''; |
| 984 | 984 | } |
| 985 | - return EEH_Venue_View::venue_name( $link_to, $VNU_ID ); |
|
| 985 | + return EEH_Venue_View::venue_name($link_to, $VNU_ID); |
|
| 986 | 986 | } |
| 987 | 987 | } |
| 988 | 988 | |
| 989 | 989 | |
| 990 | 990 | |
| 991 | 991 | |
| 992 | -if ( ! function_exists( 'espresso_venue_link' )) { |
|
| 992 | +if ( ! function_exists('espresso_venue_link')) { |
|
| 993 | 993 | /** |
| 994 | 994 | * espresso_venue_link |
| 995 | 995 | * |
@@ -998,14 +998,14 @@ discard block |
||
| 998 | 998 | * @param string $text |
| 999 | 999 | * @return string |
| 1000 | 1000 | */ |
| 1001 | - function espresso_venue_link( $VNU_ID = 0, $text = '' ) { |
|
| 1002 | - return EEH_Venue_View::venue_details_link( $VNU_ID, $text ); |
|
| 1001 | + function espresso_venue_link($VNU_ID = 0, $text = '') { |
|
| 1002 | + return EEH_Venue_View::venue_details_link($VNU_ID, $text); |
|
| 1003 | 1003 | } |
| 1004 | 1004 | } |
| 1005 | 1005 | |
| 1006 | 1006 | |
| 1007 | 1007 | |
| 1008 | -if ( ! function_exists( 'espresso_venue_description' )) { |
|
| 1008 | +if ( ! function_exists('espresso_venue_description')) { |
|
| 1009 | 1009 | /** |
| 1010 | 1010 | * espresso_venue_description |
| 1011 | 1011 | * |
@@ -1014,17 +1014,17 @@ discard block |
||
| 1014 | 1014 | * @param bool $echo |
| 1015 | 1015 | * @return string |
| 1016 | 1016 | */ |
| 1017 | - function espresso_venue_description( $VNU_ID = FALSE, $echo = TRUE ) { |
|
| 1018 | - if($echo){ |
|
| 1019 | - echo EEH_Venue_View::venue_description( $VNU_ID ); |
|
| 1017 | + function espresso_venue_description($VNU_ID = FALSE, $echo = TRUE) { |
|
| 1018 | + if ($echo) { |
|
| 1019 | + echo EEH_Venue_View::venue_description($VNU_ID); |
|
| 1020 | 1020 | return ''; |
| 1021 | 1021 | } |
| 1022 | - return EEH_Venue_View::venue_description( $VNU_ID ); |
|
| 1022 | + return EEH_Venue_View::venue_description($VNU_ID); |
|
| 1023 | 1023 | } |
| 1024 | 1024 | } |
| 1025 | 1025 | |
| 1026 | 1026 | |
| 1027 | -if ( ! function_exists( 'espresso_venue_excerpt' )) { |
|
| 1027 | +if ( ! function_exists('espresso_venue_excerpt')) { |
|
| 1028 | 1028 | /** |
| 1029 | 1029 | * espresso_venue_excerpt |
| 1030 | 1030 | * |
@@ -1033,18 +1033,18 @@ discard block |
||
| 1033 | 1033 | * @param bool $echo |
| 1034 | 1034 | * @return string |
| 1035 | 1035 | */ |
| 1036 | - function espresso_venue_excerpt( $VNU_ID = 0, $echo = TRUE ) { |
|
| 1037 | - if ( $echo ) { |
|
| 1038 | - echo EEH_Venue_View::venue_excerpt( $VNU_ID ); |
|
| 1036 | + function espresso_venue_excerpt($VNU_ID = 0, $echo = TRUE) { |
|
| 1037 | + if ($echo) { |
|
| 1038 | + echo EEH_Venue_View::venue_excerpt($VNU_ID); |
|
| 1039 | 1039 | return ''; |
| 1040 | 1040 | } |
| 1041 | - return EEH_Venue_View::venue_excerpt( $VNU_ID ); |
|
| 1041 | + return EEH_Venue_View::venue_excerpt($VNU_ID); |
|
| 1042 | 1042 | } |
| 1043 | 1043 | } |
| 1044 | 1044 | |
| 1045 | 1045 | |
| 1046 | 1046 | |
| 1047 | -if ( ! function_exists( 'espresso_venue_categories' )) { |
|
| 1047 | +if ( ! function_exists('espresso_venue_categories')) { |
|
| 1048 | 1048 | /** |
| 1049 | 1049 | * espresso_venue_categories |
| 1050 | 1050 | * returns the terms associated with a venue |
@@ -1054,17 +1054,17 @@ discard block |
||
| 1054 | 1054 | * @param bool $echo |
| 1055 | 1055 | * @return string |
| 1056 | 1056 | */ |
| 1057 | - function espresso_venue_categories( $VNU_ID = 0, $hide_uncategorized = TRUE, $echo = TRUE ) { |
|
| 1058 | - if ( $echo ) { |
|
| 1059 | - echo EEH_Venue_View::venue_categories( $VNU_ID, $hide_uncategorized ); |
|
| 1057 | + function espresso_venue_categories($VNU_ID = 0, $hide_uncategorized = TRUE, $echo = TRUE) { |
|
| 1058 | + if ($echo) { |
|
| 1059 | + echo EEH_Venue_View::venue_categories($VNU_ID, $hide_uncategorized); |
|
| 1060 | 1060 | return ''; |
| 1061 | 1061 | } |
| 1062 | - return EEH_Venue_View::venue_categories( $VNU_ID, $hide_uncategorized ); |
|
| 1062 | + return EEH_Venue_View::venue_categories($VNU_ID, $hide_uncategorized); |
|
| 1063 | 1063 | } |
| 1064 | 1064 | } |
| 1065 | 1065 | |
| 1066 | 1066 | |
| 1067 | -if ( ! function_exists( 'espresso_venue_address' )) { |
|
| 1067 | +if ( ! function_exists('espresso_venue_address')) { |
|
| 1068 | 1068 | /** |
| 1069 | 1069 | * espresso_venue_address |
| 1070 | 1070 | * returns a formatted block of html for displaying a venue's address |
@@ -1074,17 +1074,17 @@ discard block |
||
| 1074 | 1074 | * @param bool $echo |
| 1075 | 1075 | * @return string |
| 1076 | 1076 | */ |
| 1077 | - function espresso_venue_address( $type = 'multiline', $VNU_ID = 0, $echo = TRUE ) { |
|
| 1078 | - if ( $echo ) { |
|
| 1079 | - echo EEH_Venue_View::venue_address( $type, $VNU_ID ); |
|
| 1077 | + function espresso_venue_address($type = 'multiline', $VNU_ID = 0, $echo = TRUE) { |
|
| 1078 | + if ($echo) { |
|
| 1079 | + echo EEH_Venue_View::venue_address($type, $VNU_ID); |
|
| 1080 | 1080 | return ''; |
| 1081 | 1081 | } |
| 1082 | - return EEH_Venue_View::venue_address( $type, $VNU_ID ); |
|
| 1082 | + return EEH_Venue_View::venue_address($type, $VNU_ID); |
|
| 1083 | 1083 | } |
| 1084 | 1084 | } |
| 1085 | 1085 | |
| 1086 | 1086 | |
| 1087 | -if ( ! function_exists( 'espresso_venue_raw_address' )) { |
|
| 1087 | +if ( ! function_exists('espresso_venue_raw_address')) { |
|
| 1088 | 1088 | /** |
| 1089 | 1089 | * espresso_venue_address |
| 1090 | 1090 | * returns an UN-formatted string containing a venue's address |
@@ -1094,17 +1094,17 @@ discard block |
||
| 1094 | 1094 | * @param bool $echo |
| 1095 | 1095 | * @return string |
| 1096 | 1096 | */ |
| 1097 | - function espresso_venue_raw_address( $type = 'multiline', $VNU_ID = 0, $echo = TRUE ) { |
|
| 1098 | - if ( $echo ) { |
|
| 1099 | - echo EEH_Venue_View::venue_address( $type, $VNU_ID, FALSE, FALSE ); |
|
| 1097 | + function espresso_venue_raw_address($type = 'multiline', $VNU_ID = 0, $echo = TRUE) { |
|
| 1098 | + if ($echo) { |
|
| 1099 | + echo EEH_Venue_View::venue_address($type, $VNU_ID, FALSE, FALSE); |
|
| 1100 | 1100 | return ''; |
| 1101 | 1101 | } |
| 1102 | - return EEH_Venue_View::venue_address( $type, $VNU_ID, FALSE, FALSE ); |
|
| 1102 | + return EEH_Venue_View::venue_address($type, $VNU_ID, FALSE, FALSE); |
|
| 1103 | 1103 | } |
| 1104 | 1104 | } |
| 1105 | 1105 | |
| 1106 | 1106 | |
| 1107 | -if ( ! function_exists( 'espresso_venue_has_address' )) { |
|
| 1107 | +if ( ! function_exists('espresso_venue_has_address')) { |
|
| 1108 | 1108 | /** |
| 1109 | 1109 | * espresso_venue_has_address |
| 1110 | 1110 | * returns TRUE or FALSE if a Venue has address information |
@@ -1112,13 +1112,13 @@ discard block |
||
| 1112 | 1112 | * @param int $VNU_ID |
| 1113 | 1113 | * @return bool |
| 1114 | 1114 | */ |
| 1115 | - function espresso_venue_has_address( $VNU_ID = 0 ) { |
|
| 1116 | - return EEH_Venue_View::venue_has_address( $VNU_ID ); |
|
| 1115 | + function espresso_venue_has_address($VNU_ID = 0) { |
|
| 1116 | + return EEH_Venue_View::venue_has_address($VNU_ID); |
|
| 1117 | 1117 | } |
| 1118 | 1118 | } |
| 1119 | 1119 | |
| 1120 | 1120 | |
| 1121 | -if ( ! function_exists( 'espresso_venue_gmap' )) { |
|
| 1121 | +if ( ! function_exists('espresso_venue_gmap')) { |
|
| 1122 | 1122 | /** |
| 1123 | 1123 | * espresso_venue_gmap |
| 1124 | 1124 | * returns a google map for the venue address |
@@ -1129,17 +1129,17 @@ discard block |
||
| 1129 | 1129 | * @param bool $echo |
| 1130 | 1130 | * @return string |
| 1131 | 1131 | */ |
| 1132 | - function espresso_venue_gmap( $VNU_ID = 0, $map_ID = FALSE, $gmap = array(), $echo = TRUE ) { |
|
| 1133 | - if ( $echo ) { |
|
| 1134 | - echo EEH_Venue_View::venue_gmap( $VNU_ID, $map_ID, $gmap ); |
|
| 1132 | + function espresso_venue_gmap($VNU_ID = 0, $map_ID = FALSE, $gmap = array(), $echo = TRUE) { |
|
| 1133 | + if ($echo) { |
|
| 1134 | + echo EEH_Venue_View::venue_gmap($VNU_ID, $map_ID, $gmap); |
|
| 1135 | 1135 | return ''; |
| 1136 | 1136 | } |
| 1137 | - return EEH_Venue_View::venue_gmap( $VNU_ID, $map_ID, $gmap ); |
|
| 1137 | + return EEH_Venue_View::venue_gmap($VNU_ID, $map_ID, $gmap); |
|
| 1138 | 1138 | } |
| 1139 | 1139 | } |
| 1140 | 1140 | |
| 1141 | 1141 | |
| 1142 | -if ( ! function_exists( 'espresso_venue_phone' )) { |
|
| 1142 | +if ( ! function_exists('espresso_venue_phone')) { |
|
| 1143 | 1143 | /** |
| 1144 | 1144 | * espresso_venue_phone |
| 1145 | 1145 | * |
@@ -1147,18 +1147,18 @@ discard block |
||
| 1147 | 1147 | * @param bool $echo |
| 1148 | 1148 | * @return string |
| 1149 | 1149 | */ |
| 1150 | - function espresso_venue_phone( $VNU_ID = 0, $echo = TRUE ) { |
|
| 1151 | - if ( $echo ) { |
|
| 1152 | - echo EEH_Venue_View::venue_phone( $VNU_ID ); |
|
| 1150 | + function espresso_venue_phone($VNU_ID = 0, $echo = TRUE) { |
|
| 1151 | + if ($echo) { |
|
| 1152 | + echo EEH_Venue_View::venue_phone($VNU_ID); |
|
| 1153 | 1153 | return ''; |
| 1154 | 1154 | } |
| 1155 | - return EEH_Venue_View::venue_phone( $VNU_ID ); |
|
| 1155 | + return EEH_Venue_View::venue_phone($VNU_ID); |
|
| 1156 | 1156 | } |
| 1157 | 1157 | } |
| 1158 | 1158 | |
| 1159 | 1159 | |
| 1160 | 1160 | |
| 1161 | -if ( ! function_exists( 'espresso_venue_website' )) { |
|
| 1161 | +if ( ! function_exists('espresso_venue_website')) { |
|
| 1162 | 1162 | /** |
| 1163 | 1163 | * espresso_venue_website |
| 1164 | 1164 | * |
@@ -1166,18 +1166,18 @@ discard block |
||
| 1166 | 1166 | * @param bool $echo |
| 1167 | 1167 | * @return string |
| 1168 | 1168 | */ |
| 1169 | - function espresso_venue_website( $VNU_ID = 0, $echo = TRUE ) { |
|
| 1170 | - if ( $echo ) { |
|
| 1171 | - echo EEH_Venue_View::venue_website_link( $VNU_ID ); |
|
| 1169 | + function espresso_venue_website($VNU_ID = 0, $echo = TRUE) { |
|
| 1170 | + if ($echo) { |
|
| 1171 | + echo EEH_Venue_View::venue_website_link($VNU_ID); |
|
| 1172 | 1172 | return ''; |
| 1173 | 1173 | } |
| 1174 | - return EEH_Venue_View::venue_website_link( $VNU_ID ); |
|
| 1174 | + return EEH_Venue_View::venue_website_link($VNU_ID); |
|
| 1175 | 1175 | } |
| 1176 | 1176 | } |
| 1177 | 1177 | |
| 1178 | 1178 | |
| 1179 | 1179 | |
| 1180 | -if ( ! function_exists( 'espresso_edit_venue_link' )) { |
|
| 1180 | +if ( ! function_exists('espresso_edit_venue_link')) { |
|
| 1181 | 1181 | /** |
| 1182 | 1182 | * espresso_edit_venue_link |
| 1183 | 1183 | * |
@@ -1185,12 +1185,12 @@ discard block |
||
| 1185 | 1185 | * @param bool $echo |
| 1186 | 1186 | * @return string |
| 1187 | 1187 | */ |
| 1188 | - function espresso_edit_venue_link( $VNU_ID = 0, $echo = TRUE ) { |
|
| 1189 | - if($echo){ |
|
| 1190 | - echo EEH_Venue_View::edit_venue_link( $VNU_ID ); |
|
| 1188 | + function espresso_edit_venue_link($VNU_ID = 0, $echo = TRUE) { |
|
| 1189 | + if ($echo) { |
|
| 1190 | + echo EEH_Venue_View::edit_venue_link($VNU_ID); |
|
| 1191 | 1191 | return ''; |
| 1192 | 1192 | } |
| 1193 | - return EEH_Venue_View::edit_venue_link( $VNU_ID ); |
|
| 1193 | + return EEH_Venue_View::edit_venue_link($VNU_ID); |
|
| 1194 | 1194 | } |
| 1195 | 1195 | } |
| 1196 | 1196 | |
@@ -13,3348 +13,3348 @@ |
||
| 13 | 13 | abstract class EE_Base_Class |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * This is an array of the original properties and values provided during construction |
|
| 18 | - * of this model object. (keys are model field names, values are their values). |
|
| 19 | - * This list is important to remember so that when we are merging data from the db, we know |
|
| 20 | - * which values to override and which to not override. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $_props_n_values_provided_in_constructor; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Timezone |
|
| 28 | - * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
| 29 | - * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
| 30 | - * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
| 31 | - * access to it. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - protected $_timezone; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * date format |
|
| 39 | - * pattern or format for displaying dates |
|
| 40 | - * |
|
| 41 | - * @var string $_dt_frmt |
|
| 42 | - */ |
|
| 43 | - protected $_dt_frmt; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * time format |
|
| 47 | - * pattern or format for displaying time |
|
| 48 | - * |
|
| 49 | - * @var string $_tm_frmt |
|
| 50 | - */ |
|
| 51 | - protected $_tm_frmt; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * This property is for holding a cached array of object properties indexed by property name as the key. |
|
| 55 | - * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
| 56 | - * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
| 57 | - * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
| 58 | - * |
|
| 59 | - * @var array |
|
| 60 | - */ |
|
| 61 | - protected $_cached_properties = array(); |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
| 65 | - * single |
|
| 66 | - * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
| 67 | - * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
| 68 | - * all others have an array) |
|
| 69 | - * |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - protected $_model_relations = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
| 76 | - * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
| 77 | - * |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - protected $_fields = array(); |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @var boolean indicating whether or not this model object is intended to ever be saved |
|
| 84 | - * For example, we might create model objects intended to only be used for the duration |
|
| 85 | - * of this request and to be thrown away, and if they were accidentally saved |
|
| 86 | - * it would be a bug. |
|
| 87 | - */ |
|
| 88 | - protected $_allow_persist = true; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @var boolean indicating whether or not this model object's properties have changed since construction |
|
| 92 | - */ |
|
| 93 | - protected $_has_changes = false; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @var EEM_Base |
|
| 97 | - */ |
|
| 98 | - protected $_model; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose |
|
| 102 | - * for these values is for retrieval of the results, they are not further queryable and they are not persisted to |
|
| 103 | - * the db. They also do not automatically update if there are any changes to the data that produced their results. |
|
| 104 | - * The format is a simple array of field_alias => field_value. So for instance if a custom select was something |
|
| 105 | - * like, "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this |
|
| 106 | - * array as: |
|
| 107 | - * array( |
|
| 108 | - * 'Registration_Count' => 24 |
|
| 109 | - * ); |
|
| 110 | - * Note: if the custom select configuration for the query included a data type, the value will be in the data type |
|
| 111 | - * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more |
|
| 112 | - * info) |
|
| 113 | - * |
|
| 114 | - * @var array |
|
| 115 | - */ |
|
| 116 | - protected $custom_selection_results = array(); |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
| 121 | - * play nice |
|
| 122 | - * |
|
| 123 | - * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
| 124 | - * layer of the model's _fields array, (eg, EVT_ID, |
|
| 125 | - * TXN_amount, QST_name, etc) and values are their values |
|
| 126 | - * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
| 127 | - * corresponding db model or not. |
|
| 128 | - * @param string $timezone indicate what timezone you want any datetime fields to |
|
| 129 | - * be in when instantiating a EE_Base_Class object. |
|
| 130 | - * @param array $date_formats An array of date formats to set on construct where first |
|
| 131 | - * value is the date_format and second value is the time |
|
| 132 | - * format. |
|
| 133 | - * @throws InvalidArgumentException |
|
| 134 | - * @throws InvalidInterfaceException |
|
| 135 | - * @throws InvalidDataTypeException |
|
| 136 | - * @throws EE_Error |
|
| 137 | - * @throws ReflectionException |
|
| 138 | - */ |
|
| 139 | - protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
| 140 | - { |
|
| 141 | - $className = get_class($this); |
|
| 142 | - do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
| 143 | - $model = $this->get_model(); |
|
| 144 | - $model_fields = $model->field_settings(false); |
|
| 145 | - // ensure $fieldValues is an array |
|
| 146 | - $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
| 147 | - // verify client code has not passed any invalid field names |
|
| 148 | - foreach ($fieldValues as $field_name => $field_value) { |
|
| 149 | - if (! isset($model_fields[ $field_name ])) { |
|
| 150 | - throw new EE_Error( |
|
| 151 | - sprintf( |
|
| 152 | - esc_html__( |
|
| 153 | - 'Invalid field (%s) passed to constructor of %s. Allowed fields are :%s', |
|
| 154 | - 'event_espresso' |
|
| 155 | - ), |
|
| 156 | - $field_name, |
|
| 157 | - get_class($this), |
|
| 158 | - implode(', ', array_keys($model_fields)) |
|
| 159 | - ) |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 164 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
| 165 | - list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
| 166 | - } else { |
|
| 167 | - // set default formats for date and time |
|
| 168 | - $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d'); |
|
| 169 | - $this->_tm_frmt = (string) get_option('time_format', 'g:i a'); |
|
| 170 | - } |
|
| 171 | - // if db model is instantiating |
|
| 172 | - if ($bydb) { |
|
| 173 | - // client code has indicated these field values are from the database |
|
| 174 | - foreach ($model_fields as $fieldName => $field) { |
|
| 175 | - $this->set_from_db( |
|
| 176 | - $fieldName, |
|
| 177 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null |
|
| 178 | - ); |
|
| 179 | - } |
|
| 180 | - } else { |
|
| 181 | - // we're constructing a brand |
|
| 182 | - // new instance of the model object. Generally, this means we'll need to do more field validation |
|
| 183 | - foreach ($model_fields as $fieldName => $field) { |
|
| 184 | - $this->set( |
|
| 185 | - $fieldName, |
|
| 186 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null, |
|
| 187 | - true |
|
| 188 | - ); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - // remember what values were passed to this constructor |
|
| 192 | - $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
| 193 | - // remember in entity mapper |
|
| 194 | - if (! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 195 | - $model->add_to_entity_map($this); |
|
| 196 | - } |
|
| 197 | - // setup all the relations |
|
| 198 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 199 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 200 | - $this->_model_relations[ $relation_name ] = null; |
|
| 201 | - } else { |
|
| 202 | - $this->_model_relations[ $relation_name ] = array(); |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - /** |
|
| 206 | - * Action done at the end of each model object construction |
|
| 207 | - * |
|
| 208 | - * @param EE_Base_Class $this the model object just created |
|
| 209 | - */ |
|
| 210 | - do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
| 216 | - * |
|
| 217 | - * @return boolean |
|
| 218 | - */ |
|
| 219 | - public function allow_persist() |
|
| 220 | - { |
|
| 221 | - return $this->_allow_persist; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Sets whether or not this model object should be allowed to be saved to the DB. |
|
| 227 | - * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
| 228 | - * you got new information that somehow made you change your mind. |
|
| 229 | - * |
|
| 230 | - * @param boolean $allow_persist |
|
| 231 | - * @return boolean |
|
| 232 | - */ |
|
| 233 | - public function set_allow_persist($allow_persist) |
|
| 234 | - { |
|
| 235 | - return $this->_allow_persist = $allow_persist; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Gets the field's original value when this object was constructed during this request. |
|
| 241 | - * This can be helpful when determining if a model object has changed or not |
|
| 242 | - * |
|
| 243 | - * @param string $field_name |
|
| 244 | - * @return mixed|null |
|
| 245 | - * @throws ReflectionException |
|
| 246 | - * @throws InvalidArgumentException |
|
| 247 | - * @throws InvalidInterfaceException |
|
| 248 | - * @throws InvalidDataTypeException |
|
| 249 | - * @throws EE_Error |
|
| 250 | - */ |
|
| 251 | - public function get_original($field_name) |
|
| 252 | - { |
|
| 253 | - if (isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
| 254 | - && $field_settings = $this->get_model()->field_settings_for($field_name) |
|
| 255 | - ) { |
|
| 256 | - return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
| 257 | - } |
|
| 258 | - return null; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * @param EE_Base_Class $obj |
|
| 264 | - * @return string |
|
| 265 | - */ |
|
| 266 | - public function get_class($obj) |
|
| 267 | - { |
|
| 268 | - return get_class($obj); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Overrides parent because parent expects old models. |
|
| 274 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
| 275 | - * |
|
| 276 | - * @param string $field_name |
|
| 277 | - * @param mixed $field_value |
|
| 278 | - * @param bool $use_default |
|
| 279 | - * @throws InvalidArgumentException |
|
| 280 | - * @throws InvalidInterfaceException |
|
| 281 | - * @throws InvalidDataTypeException |
|
| 282 | - * @throws EE_Error |
|
| 283 | - * @throws ReflectionException |
|
| 284 | - * @throws ReflectionException |
|
| 285 | - * @throws ReflectionException |
|
| 286 | - */ |
|
| 287 | - public function set($field_name, $field_value, $use_default = false) |
|
| 288 | - { |
|
| 289 | - // if not using default and nothing has changed, and object has already been setup (has ID), |
|
| 290 | - // then don't do anything |
|
| 291 | - if (! $use_default |
|
| 292 | - && $this->_fields[ $field_name ] === $field_value |
|
| 293 | - && $this->ID() |
|
| 294 | - ) { |
|
| 295 | - return; |
|
| 296 | - } |
|
| 297 | - $model = $this->get_model(); |
|
| 298 | - $this->_has_changes = true; |
|
| 299 | - $field_obj = $model->field_settings_for($field_name); |
|
| 300 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 301 | - // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
| 302 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 303 | - $field_obj->set_timezone($this->_timezone); |
|
| 304 | - $field_obj->set_date_format($this->_dt_frmt); |
|
| 305 | - $field_obj->set_time_format($this->_tm_frmt); |
|
| 306 | - } |
|
| 307 | - $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
| 308 | - // should the value be null? |
|
| 309 | - if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
| 310 | - $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
| 311 | - /** |
|
| 312 | - * To save having to refactor all the models, if a default value is used for a |
|
| 313 | - * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
| 314 | - * object. Then let's do a set again to ensure that it becomes a DateTime |
|
| 315 | - * object. |
|
| 316 | - * |
|
| 317 | - * @since 4.6.10+ |
|
| 318 | - */ |
|
| 319 | - if ($field_obj instanceof EE_Datetime_Field |
|
| 320 | - && $this->_fields[ $field_name ] !== null |
|
| 321 | - && ! $this->_fields[ $field_name ] instanceof DateTime |
|
| 322 | - ) { |
|
| 323 | - empty($this->_fields[ $field_name ]) |
|
| 324 | - ? $this->set($field_name, time()) |
|
| 325 | - : $this->set($field_name, $this->_fields[ $field_name ]); |
|
| 326 | - } |
|
| 327 | - } else { |
|
| 328 | - $this->_fields[ $field_name ] = $holder_of_value; |
|
| 329 | - } |
|
| 330 | - // if we're not in the constructor... |
|
| 331 | - // now check if what we set was a primary key |
|
| 332 | - if (// note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
| 333 | - $this->_props_n_values_provided_in_constructor |
|
| 334 | - && $field_value |
|
| 335 | - && $field_name === $model->primary_key_name() |
|
| 336 | - ) { |
|
| 337 | - // if so, we want all this object's fields to be filled either with |
|
| 338 | - // what we've explicitly set on this model |
|
| 339 | - // or what we have in the db |
|
| 340 | - // echo "setting primary key!"; |
|
| 341 | - $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
| 342 | - $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
| 343 | - foreach ($fields_on_model as $field_obj) { |
|
| 344 | - if (! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 345 | - && $field_obj->get_name() !== $field_name |
|
| 346 | - ) { |
|
| 347 | - $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - // oh this model object has an ID? well make sure its in the entity mapper |
|
| 351 | - $model->add_to_entity_map($this); |
|
| 352 | - } |
|
| 353 | - // let's unset any cache for this field_name from the $_cached_properties property. |
|
| 354 | - $this->_clear_cached_property($field_name); |
|
| 355 | - } else { |
|
| 356 | - throw new EE_Error( |
|
| 357 | - sprintf( |
|
| 358 | - esc_html__( |
|
| 359 | - 'A valid EE_Model_Field_Base could not be found for the given field name: %s', |
|
| 360 | - 'event_espresso' |
|
| 361 | - ), |
|
| 362 | - $field_name |
|
| 363 | - ) |
|
| 364 | - ); |
|
| 365 | - } |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Set custom select values for model. |
|
| 371 | - * |
|
| 372 | - * @param array $custom_select_values |
|
| 373 | - */ |
|
| 374 | - public function setCustomSelectsValues(array $custom_select_values) |
|
| 375 | - { |
|
| 376 | - $this->custom_selection_results = $custom_select_values; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Returns the custom select value for the provided alias if its set. |
|
| 382 | - * If not set, returns null. |
|
| 383 | - * |
|
| 384 | - * @param string $alias |
|
| 385 | - * @return string|int|float|null |
|
| 386 | - */ |
|
| 387 | - public function getCustomSelect($alias) |
|
| 388 | - { |
|
| 389 | - return isset($this->custom_selection_results[ $alias ]) |
|
| 390 | - ? $this->custom_selection_results[ $alias ] |
|
| 391 | - : null; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * This sets the field value on the db column if it exists for the given $column_name or |
|
| 397 | - * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
| 398 | - * |
|
| 399 | - * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
| 400 | - * @param string $field_name Must be the exact column name. |
|
| 401 | - * @param mixed $field_value The value to set. |
|
| 402 | - * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
| 403 | - * @throws InvalidArgumentException |
|
| 404 | - * @throws InvalidInterfaceException |
|
| 405 | - * @throws InvalidDataTypeException |
|
| 406 | - * @throws EE_Error |
|
| 407 | - * @throws ReflectionException |
|
| 408 | - */ |
|
| 409 | - public function set_field_or_extra_meta($field_name, $field_value) |
|
| 410 | - { |
|
| 411 | - if ($this->get_model()->has_field($field_name)) { |
|
| 412 | - $this->set($field_name, $field_value); |
|
| 413 | - return true; |
|
| 414 | - } |
|
| 415 | - // ensure this object is saved first so that extra meta can be properly related. |
|
| 416 | - $this->save(); |
|
| 417 | - return $this->update_extra_meta($field_name, $field_value); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * This retrieves the value of the db column set on this class or if that's not present |
|
| 423 | - * it will attempt to retrieve from extra_meta if found. |
|
| 424 | - * Example Usage: |
|
| 425 | - * Via EE_Message child class: |
|
| 426 | - * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
| 427 | - * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
| 428 | - * also have additional main fields specific to the messenger. The system accommodates those extra |
|
| 429 | - * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
| 430 | - * value for those extra fields dynamically via the EE_message object. |
|
| 431 | - * |
|
| 432 | - * @param string $field_name expecting the fully qualified field name. |
|
| 433 | - * @return mixed|null value for the field if found. null if not found. |
|
| 434 | - * @throws ReflectionException |
|
| 435 | - * @throws InvalidArgumentException |
|
| 436 | - * @throws InvalidInterfaceException |
|
| 437 | - * @throws InvalidDataTypeException |
|
| 438 | - * @throws EE_Error |
|
| 439 | - */ |
|
| 440 | - public function get_field_or_extra_meta($field_name) |
|
| 441 | - { |
|
| 442 | - if ($this->get_model()->has_field($field_name)) { |
|
| 443 | - $column_value = $this->get($field_name); |
|
| 444 | - } else { |
|
| 445 | - // This isn't a column in the main table, let's see if it is in the extra meta. |
|
| 446 | - $column_value = $this->get_extra_meta($field_name, true, null); |
|
| 447 | - } |
|
| 448 | - return $column_value; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 454 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 455 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
| 456 | - * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
| 457 | - * |
|
| 458 | - * @access public |
|
| 459 | - * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
| 460 | - * @return void |
|
| 461 | - * @throws InvalidArgumentException |
|
| 462 | - * @throws InvalidInterfaceException |
|
| 463 | - * @throws InvalidDataTypeException |
|
| 464 | - * @throws EE_Error |
|
| 465 | - * @throws ReflectionException |
|
| 466 | - */ |
|
| 467 | - public function set_timezone($timezone = '') |
|
| 468 | - { |
|
| 469 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 470 | - // make sure we clear all cached properties because they won't be relevant now |
|
| 471 | - $this->_clear_cached_properties(); |
|
| 472 | - // make sure we update field settings and the date for all EE_Datetime_Fields |
|
| 473 | - $model_fields = $this->get_model()->field_settings(false); |
|
| 474 | - foreach ($model_fields as $field_name => $field_obj) { |
|
| 475 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 476 | - $field_obj->set_timezone($this->_timezone); |
|
| 477 | - if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
| 478 | - EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone)); |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * This just returns whatever is set for the current timezone. |
|
| 487 | - * |
|
| 488 | - * @access public |
|
| 489 | - * @return string timezone string |
|
| 490 | - */ |
|
| 491 | - public function get_timezone() |
|
| 492 | - { |
|
| 493 | - return $this->_timezone; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * This sets the internal date format to what is sent in to be used as the new default for the class |
|
| 499 | - * internally instead of wp set date format options |
|
| 500 | - * |
|
| 501 | - * @since 4.6 |
|
| 502 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
| 503 | - */ |
|
| 504 | - public function set_date_format($format) |
|
| 505 | - { |
|
| 506 | - $this->_dt_frmt = $format; |
|
| 507 | - // clear cached_properties because they won't be relevant now. |
|
| 508 | - $this->_clear_cached_properties(); |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - |
|
| 512 | - /** |
|
| 513 | - * This sets the internal time format string to what is sent in to be used as the new default for the |
|
| 514 | - * class internally instead of wp set time format options. |
|
| 515 | - * |
|
| 516 | - * @since 4.6 |
|
| 517 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
| 518 | - */ |
|
| 519 | - public function set_time_format($format) |
|
| 520 | - { |
|
| 521 | - $this->_tm_frmt = $format; |
|
| 522 | - // clear cached_properties because they won't be relevant now. |
|
| 523 | - $this->_clear_cached_properties(); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * This returns the current internal set format for the date and time formats. |
|
| 529 | - * |
|
| 530 | - * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
| 531 | - * where the first value is the date format and the second value is the time format. |
|
| 532 | - * @return mixed string|array |
|
| 533 | - */ |
|
| 534 | - public function get_format($full = true) |
|
| 535 | - { |
|
| 536 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * cache |
|
| 542 | - * stores the passed model object on the current model object. |
|
| 543 | - * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
| 544 | - * |
|
| 545 | - * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
| 546 | - * 'Registration' associated with this model object |
|
| 547 | - * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
| 548 | - * that could be a payment or a registration) |
|
| 549 | - * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
| 550 | - * items which will be stored in an array on this object |
|
| 551 | - * @throws ReflectionException |
|
| 552 | - * @throws InvalidArgumentException |
|
| 553 | - * @throws InvalidInterfaceException |
|
| 554 | - * @throws InvalidDataTypeException |
|
| 555 | - * @throws EE_Error |
|
| 556 | - * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
| 557 | - * related thing, no array) |
|
| 558 | - */ |
|
| 559 | - public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
| 560 | - { |
|
| 561 | - // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
| 562 | - if (! $object_to_cache instanceof EE_Base_Class) { |
|
| 563 | - return false; |
|
| 564 | - } |
|
| 565 | - // also get "how" the object is related, or throw an error |
|
| 566 | - if (! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 567 | - throw new EE_Error( |
|
| 568 | - sprintf( |
|
| 569 | - esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
| 570 | - $relationName, |
|
| 571 | - get_class($this) |
|
| 572 | - ) |
|
| 573 | - ); |
|
| 574 | - } |
|
| 575 | - // how many things are related ? |
|
| 576 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 577 | - // if it's a "belongs to" relationship, then there's only one related model object |
|
| 578 | - // eg, if this is a registration, there's only 1 attendee for it |
|
| 579 | - // so for these model objects just set it to be cached |
|
| 580 | - $this->_model_relations[ $relationName ] = $object_to_cache; |
|
| 581 | - $return = true; |
|
| 582 | - } else { |
|
| 583 | - // otherwise, this is the "many" side of a one to many relationship, |
|
| 584 | - // so we'll add the object to the array of related objects for that type. |
|
| 585 | - // eg: if this is an event, there are many registrations for that event, |
|
| 586 | - // so we cache the registrations in an array |
|
| 587 | - if (! is_array($this->_model_relations[ $relationName ])) { |
|
| 588 | - // if for some reason, the cached item is a model object, |
|
| 589 | - // then stick that in the array, otherwise start with an empty array |
|
| 590 | - $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] |
|
| 591 | - instanceof |
|
| 592 | - EE_Base_Class |
|
| 593 | - ? array($this->_model_relations[ $relationName ]) : array(); |
|
| 594 | - } |
|
| 595 | - // first check for a cache_id which is normally empty |
|
| 596 | - if (! empty($cache_id)) { |
|
| 597 | - // if the cache_id exists, then it means we are purposely trying to cache this |
|
| 598 | - // with a known key that can then be used to retrieve the object later on |
|
| 599 | - $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
| 600 | - $return = $cache_id; |
|
| 601 | - } elseif ($object_to_cache->ID()) { |
|
| 602 | - // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
| 603 | - $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
| 604 | - $return = $object_to_cache->ID(); |
|
| 605 | - } else { |
|
| 606 | - // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
| 607 | - $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
| 608 | - // move the internal pointer to the end of the array |
|
| 609 | - end($this->_model_relations[ $relationName ]); |
|
| 610 | - // and grab the key so that we can return it |
|
| 611 | - $return = key($this->_model_relations[ $relationName ]); |
|
| 612 | - } |
|
| 613 | - } |
|
| 614 | - return $return; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - |
|
| 618 | - /** |
|
| 619 | - * For adding an item to the cached_properties property. |
|
| 620 | - * |
|
| 621 | - * @access protected |
|
| 622 | - * @param string $fieldname the property item the corresponding value is for. |
|
| 623 | - * @param mixed $value The value we are caching. |
|
| 624 | - * @param string|null $cache_type |
|
| 625 | - * @return void |
|
| 626 | - * @throws ReflectionException |
|
| 627 | - * @throws InvalidArgumentException |
|
| 628 | - * @throws InvalidInterfaceException |
|
| 629 | - * @throws InvalidDataTypeException |
|
| 630 | - * @throws EE_Error |
|
| 631 | - */ |
|
| 632 | - protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
| 633 | - { |
|
| 634 | - // first make sure this property exists |
|
| 635 | - $this->get_model()->field_settings_for($fieldname); |
|
| 636 | - $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
| 637 | - $this->_cached_properties[ $fieldname ][ $cache_type ] = $value; |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - |
|
| 641 | - /** |
|
| 642 | - * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
| 643 | - * This also SETS the cache if we return the actual property! |
|
| 644 | - * |
|
| 645 | - * @param string $fieldname the name of the property we're trying to retrieve |
|
| 646 | - * @param bool $pretty |
|
| 647 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 648 | - * (in cases where the same property may be used for different outputs |
|
| 649 | - * - i.e. datetime, money etc.) |
|
| 650 | - * It can also accept certain pre-defined "schema" strings |
|
| 651 | - * to define how to output the property. |
|
| 652 | - * see the field's prepare_for_pretty_echoing for what strings can be used |
|
| 653 | - * @return mixed whatever the value for the property is we're retrieving |
|
| 654 | - * @throws ReflectionException |
|
| 655 | - * @throws InvalidArgumentException |
|
| 656 | - * @throws InvalidInterfaceException |
|
| 657 | - * @throws InvalidDataTypeException |
|
| 658 | - * @throws EE_Error |
|
| 659 | - */ |
|
| 660 | - protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 661 | - { |
|
| 662 | - // verify the field exists |
|
| 663 | - $model = $this->get_model(); |
|
| 664 | - $model->field_settings_for($fieldname); |
|
| 665 | - $cache_type = $pretty ? 'pretty' : 'standard'; |
|
| 666 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 667 | - if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) { |
|
| 668 | - return $this->_cached_properties[ $fieldname ][ $cache_type ]; |
|
| 669 | - } |
|
| 670 | - $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
|
| 671 | - $this->_set_cached_property($fieldname, $value, $cache_type); |
|
| 672 | - return $value; |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - |
|
| 676 | - /** |
|
| 677 | - * If the cache didn't fetch the needed item, this fetches it. |
|
| 678 | - * |
|
| 679 | - * @param string $fieldname |
|
| 680 | - * @param bool $pretty |
|
| 681 | - * @param string $extra_cache_ref |
|
| 682 | - * @return mixed |
|
| 683 | - * @throws InvalidArgumentException |
|
| 684 | - * @throws InvalidInterfaceException |
|
| 685 | - * @throws InvalidDataTypeException |
|
| 686 | - * @throws EE_Error |
|
| 687 | - * @throws ReflectionException |
|
| 688 | - */ |
|
| 689 | - protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 690 | - { |
|
| 691 | - $field_obj = $this->get_model()->field_settings_for($fieldname); |
|
| 692 | - // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
| 693 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 694 | - $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
| 695 | - } |
|
| 696 | - if (! isset($this->_fields[ $fieldname ])) { |
|
| 697 | - $this->_fields[ $fieldname ] = null; |
|
| 698 | - } |
|
| 699 | - $value = $pretty |
|
| 700 | - ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref) |
|
| 701 | - : $field_obj->prepare_for_get($this->_fields[ $fieldname ]); |
|
| 702 | - return $value; |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * set timezone, formats, and output for EE_Datetime_Field objects |
|
| 708 | - * |
|
| 709 | - * @param \EE_Datetime_Field $datetime_field |
|
| 710 | - * @param bool $pretty |
|
| 711 | - * @param null $date_or_time |
|
| 712 | - * @return void |
|
| 713 | - * @throws InvalidArgumentException |
|
| 714 | - * @throws InvalidInterfaceException |
|
| 715 | - * @throws InvalidDataTypeException |
|
| 716 | - * @throws EE_Error |
|
| 717 | - */ |
|
| 718 | - protected function _prepare_datetime_field( |
|
| 719 | - EE_Datetime_Field $datetime_field, |
|
| 720 | - $pretty = false, |
|
| 721 | - $date_or_time = null |
|
| 722 | - ) { |
|
| 723 | - $datetime_field->set_timezone($this->_timezone); |
|
| 724 | - $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
| 725 | - $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
| 726 | - // set the output returned |
|
| 727 | - switch ($date_or_time) { |
|
| 728 | - case 'D': |
|
| 729 | - $datetime_field->set_date_time_output('date'); |
|
| 730 | - break; |
|
| 731 | - case 'T': |
|
| 732 | - $datetime_field->set_date_time_output('time'); |
|
| 733 | - break; |
|
| 734 | - default: |
|
| 735 | - $datetime_field->set_date_time_output(); |
|
| 736 | - } |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * This just takes care of clearing out the cached_properties |
|
| 742 | - * |
|
| 743 | - * @return void |
|
| 744 | - */ |
|
| 745 | - protected function _clear_cached_properties() |
|
| 746 | - { |
|
| 747 | - $this->_cached_properties = array(); |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - |
|
| 751 | - /** |
|
| 752 | - * This just clears out ONE property if it exists in the cache |
|
| 753 | - * |
|
| 754 | - * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
| 755 | - * @return void |
|
| 756 | - */ |
|
| 757 | - protected function _clear_cached_property($property_name) |
|
| 758 | - { |
|
| 759 | - if (isset($this->_cached_properties[ $property_name ])) { |
|
| 760 | - unset($this->_cached_properties[ $property_name ]); |
|
| 761 | - } |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - |
|
| 765 | - /** |
|
| 766 | - * Ensures that this related thing is a model object. |
|
| 767 | - * |
|
| 768 | - * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
| 769 | - * @param string $model_name name of the related thing, eg 'Attendee', |
|
| 770 | - * @return EE_Base_Class |
|
| 771 | - * @throws ReflectionException |
|
| 772 | - * @throws InvalidArgumentException |
|
| 773 | - * @throws InvalidInterfaceException |
|
| 774 | - * @throws InvalidDataTypeException |
|
| 775 | - * @throws EE_Error |
|
| 776 | - */ |
|
| 777 | - protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
| 778 | - { |
|
| 779 | - $other_model_instance = self::_get_model_instance_with_name( |
|
| 780 | - self::_get_model_classname($model_name), |
|
| 781 | - $this->_timezone |
|
| 782 | - ); |
|
| 783 | - return $other_model_instance->ensure_is_obj($object_or_id); |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - |
|
| 787 | - /** |
|
| 788 | - * Forgets the cached model of the given relation Name. So the next time we request it, |
|
| 789 | - * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
| 790 | - * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
| 791 | - * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
| 792 | - * |
|
| 793 | - * @param string $relationName one of the keys in the _model_relations array on the model. |
|
| 794 | - * Eg 'Registration' |
|
| 795 | - * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
| 796 | - * if you intend to use $clear_all = TRUE, or the relation only |
|
| 797 | - * has 1 object anyways (ie, it's a BelongsToRelation) |
|
| 798 | - * @param bool $clear_all This flags clearing the entire cache relation property if |
|
| 799 | - * this is HasMany or HABTM. |
|
| 800 | - * @throws ReflectionException |
|
| 801 | - * @throws InvalidArgumentException |
|
| 802 | - * @throws InvalidInterfaceException |
|
| 803 | - * @throws InvalidDataTypeException |
|
| 804 | - * @throws EE_Error |
|
| 805 | - * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
| 806 | - * relation from all |
|
| 807 | - */ |
|
| 808 | - public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
| 809 | - { |
|
| 810 | - $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 811 | - $index_in_cache = ''; |
|
| 812 | - if (! $relationship_to_model) { |
|
| 813 | - throw new EE_Error( |
|
| 814 | - sprintf( |
|
| 815 | - esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
|
| 816 | - $relationName, |
|
| 817 | - get_class($this) |
|
| 818 | - ) |
|
| 819 | - ); |
|
| 820 | - } |
|
| 821 | - if ($clear_all) { |
|
| 822 | - $obj_removed = true; |
|
| 823 | - $this->_model_relations[ $relationName ] = null; |
|
| 824 | - } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 825 | - $obj_removed = $this->_model_relations[ $relationName ]; |
|
| 826 | - $this->_model_relations[ $relationName ] = null; |
|
| 827 | - } else { |
|
| 828 | - if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
| 829 | - && $object_to_remove_or_index_into_array->ID() |
|
| 830 | - ) { |
|
| 831 | - $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
| 832 | - if (is_array($this->_model_relations[ $relationName ]) |
|
| 833 | - && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
| 834 | - ) { |
|
| 835 | - $index_found_at = null; |
|
| 836 | - // find this object in the array even though it has a different key |
|
| 837 | - foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
| 838 | - /** @noinspection TypeUnsafeComparisonInspection */ |
|
| 839 | - if ($obj instanceof EE_Base_Class |
|
| 840 | - && ( |
|
| 841 | - $obj == $object_to_remove_or_index_into_array |
|
| 842 | - || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
| 843 | - ) |
|
| 844 | - ) { |
|
| 845 | - $index_found_at = $index; |
|
| 846 | - break; |
|
| 847 | - } |
|
| 848 | - } |
|
| 849 | - if ($index_found_at) { |
|
| 850 | - $index_in_cache = $index_found_at; |
|
| 851 | - } else { |
|
| 852 | - // it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
| 853 | - // if it wasn't in it to begin with. So we're done |
|
| 854 | - return $object_to_remove_or_index_into_array; |
|
| 855 | - } |
|
| 856 | - } |
|
| 857 | - } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
| 858 | - // so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
| 859 | - foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
| 860 | - /** @noinspection TypeUnsafeComparisonInspection */ |
|
| 861 | - if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
| 862 | - $index_in_cache = $index; |
|
| 863 | - } |
|
| 864 | - } |
|
| 865 | - } else { |
|
| 866 | - $index_in_cache = $object_to_remove_or_index_into_array; |
|
| 867 | - } |
|
| 868 | - // supposedly we've found it. But it could just be that the client code |
|
| 869 | - // provided a bad index/object |
|
| 870 | - if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
| 871 | - $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
| 872 | - unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
| 873 | - } else { |
|
| 874 | - // that thing was never cached anyways. |
|
| 875 | - $obj_removed = null; |
|
| 876 | - } |
|
| 877 | - } |
|
| 878 | - return $obj_removed; |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - |
|
| 882 | - /** |
|
| 883 | - * update_cache_after_object_save |
|
| 884 | - * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
| 885 | - * obtained after being saved to the db |
|
| 886 | - * |
|
| 887 | - * @param string $relationName - the type of object that is cached |
|
| 888 | - * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
| 889 | - * @param string $current_cache_id - the ID that was used when originally caching the object |
|
| 890 | - * @return boolean TRUE on success, FALSE on fail |
|
| 891 | - * @throws ReflectionException |
|
| 892 | - * @throws InvalidArgumentException |
|
| 893 | - * @throws InvalidInterfaceException |
|
| 894 | - * @throws InvalidDataTypeException |
|
| 895 | - * @throws EE_Error |
|
| 896 | - */ |
|
| 897 | - public function update_cache_after_object_save( |
|
| 898 | - $relationName, |
|
| 899 | - EE_Base_Class $newly_saved_object, |
|
| 900 | - $current_cache_id = '' |
|
| 901 | - ) { |
|
| 902 | - // verify that incoming object is of the correct type |
|
| 903 | - $obj_class = 'EE_' . $relationName; |
|
| 904 | - if ($newly_saved_object instanceof $obj_class) { |
|
| 905 | - /* @type EE_Base_Class $newly_saved_object */ |
|
| 906 | - // now get the type of relation |
|
| 907 | - $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 908 | - // if this is a 1:1 relationship |
|
| 909 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 910 | - // then just replace the cached object with the newly saved object |
|
| 911 | - $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
| 912 | - return true; |
|
| 913 | - // or if it's some kind of sordid feral polyamorous relationship... |
|
| 914 | - } |
|
| 915 | - if (is_array($this->_model_relations[ $relationName ]) |
|
| 916 | - && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
| 917 | - ) { |
|
| 918 | - // then remove the current cached item |
|
| 919 | - unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
| 920 | - // and cache the newly saved object using it's new ID |
|
| 921 | - $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
| 922 | - return true; |
|
| 923 | - } |
|
| 924 | - } |
|
| 925 | - return false; |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - |
|
| 929 | - /** |
|
| 930 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 931 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 932 | - * |
|
| 933 | - * @param string $relationName |
|
| 934 | - * @return EE_Base_Class |
|
| 935 | - */ |
|
| 936 | - public function get_one_from_cache($relationName) |
|
| 937 | - { |
|
| 938 | - $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
| 939 | - ? $this->_model_relations[ $relationName ] |
|
| 940 | - : null; |
|
| 941 | - if (is_array($cached_array_or_object)) { |
|
| 942 | - return array_shift($cached_array_or_object); |
|
| 943 | - } |
|
| 944 | - return $cached_array_or_object; |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - |
|
| 948 | - /** |
|
| 949 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 950 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 951 | - * |
|
| 952 | - * @param string $relationName |
|
| 953 | - * @throws ReflectionException |
|
| 954 | - * @throws InvalidArgumentException |
|
| 955 | - * @throws InvalidInterfaceException |
|
| 956 | - * @throws InvalidDataTypeException |
|
| 957 | - * @throws EE_Error |
|
| 958 | - * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
| 959 | - */ |
|
| 960 | - public function get_all_from_cache($relationName) |
|
| 961 | - { |
|
| 962 | - $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array(); |
|
| 963 | - // if the result is not an array, but exists, make it an array |
|
| 964 | - $objects = is_array($objects) ? $objects : array($objects); |
|
| 965 | - // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
| 966 | - // basically, if this model object was stored in the session, and these cached model objects |
|
| 967 | - // already have IDs, let's make sure they're in their model's entity mapper |
|
| 968 | - // otherwise we will have duplicates next time we call |
|
| 969 | - // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
| 970 | - $model = EE_Registry::instance()->load_model($relationName); |
|
| 971 | - foreach ($objects as $model_object) { |
|
| 972 | - if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
| 973 | - // ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
| 974 | - if ($model_object->ID()) { |
|
| 975 | - $model->add_to_entity_map($model_object); |
|
| 976 | - } |
|
| 977 | - } else { |
|
| 978 | - throw new EE_Error( |
|
| 979 | - sprintf( |
|
| 980 | - esc_html__( |
|
| 981 | - 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
| 982 | - 'event_espresso' |
|
| 983 | - ), |
|
| 984 | - $relationName, |
|
| 985 | - gettype($model_object) |
|
| 986 | - ) |
|
| 987 | - ); |
|
| 988 | - } |
|
| 989 | - } |
|
| 990 | - return $objects; |
|
| 991 | - } |
|
| 992 | - |
|
| 993 | - |
|
| 994 | - /** |
|
| 995 | - * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 996 | - * matching the given query conditions. |
|
| 997 | - * |
|
| 998 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 999 | - * @param int $limit How many objects to return. |
|
| 1000 | - * @param array $query_params Any additional conditions on the query. |
|
| 1001 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1002 | - * you can indicate just the columns you want returned |
|
| 1003 | - * @return array|EE_Base_Class[] |
|
| 1004 | - * @throws ReflectionException |
|
| 1005 | - * @throws InvalidArgumentException |
|
| 1006 | - * @throws InvalidInterfaceException |
|
| 1007 | - * @throws InvalidDataTypeException |
|
| 1008 | - * @throws EE_Error |
|
| 1009 | - */ |
|
| 1010 | - public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 1011 | - { |
|
| 1012 | - $model = $this->get_model(); |
|
| 1013 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1014 | - ? $model->get_primary_key_field()->get_name() |
|
| 1015 | - : $field_to_order_by; |
|
| 1016 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1017 | - if (empty($field) || empty($current_value)) { |
|
| 1018 | - return array(); |
|
| 1019 | - } |
|
| 1020 | - return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - |
|
| 1024 | - /** |
|
| 1025 | - * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 1026 | - * matching the given query conditions. |
|
| 1027 | - * |
|
| 1028 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1029 | - * @param int $limit How many objects to return. |
|
| 1030 | - * @param array $query_params Any additional conditions on the query. |
|
| 1031 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1032 | - * you can indicate just the columns you want returned |
|
| 1033 | - * @return array|EE_Base_Class[] |
|
| 1034 | - * @throws ReflectionException |
|
| 1035 | - * @throws InvalidArgumentException |
|
| 1036 | - * @throws InvalidInterfaceException |
|
| 1037 | - * @throws InvalidDataTypeException |
|
| 1038 | - * @throws EE_Error |
|
| 1039 | - */ |
|
| 1040 | - public function previous_x( |
|
| 1041 | - $field_to_order_by = null, |
|
| 1042 | - $limit = 1, |
|
| 1043 | - $query_params = array(), |
|
| 1044 | - $columns_to_select = null |
|
| 1045 | - ) { |
|
| 1046 | - $model = $this->get_model(); |
|
| 1047 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1048 | - ? $model->get_primary_key_field()->get_name() |
|
| 1049 | - : $field_to_order_by; |
|
| 1050 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1051 | - if (empty($field) || empty($current_value)) { |
|
| 1052 | - return array(); |
|
| 1053 | - } |
|
| 1054 | - return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 1055 | - } |
|
| 1056 | - |
|
| 1057 | - |
|
| 1058 | - /** |
|
| 1059 | - * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
| 1060 | - * matching the given query conditions. |
|
| 1061 | - * |
|
| 1062 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1063 | - * @param array $query_params Any additional conditions on the query. |
|
| 1064 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1065 | - * you can indicate just the columns you want returned |
|
| 1066 | - * @return array|EE_Base_Class |
|
| 1067 | - * @throws ReflectionException |
|
| 1068 | - * @throws InvalidArgumentException |
|
| 1069 | - * @throws InvalidInterfaceException |
|
| 1070 | - * @throws InvalidDataTypeException |
|
| 1071 | - * @throws EE_Error |
|
| 1072 | - */ |
|
| 1073 | - public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1074 | - { |
|
| 1075 | - $model = $this->get_model(); |
|
| 1076 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1077 | - ? $model->get_primary_key_field()->get_name() |
|
| 1078 | - : $field_to_order_by; |
|
| 1079 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1080 | - if (empty($field) || empty($current_value)) { |
|
| 1081 | - return array(); |
|
| 1082 | - } |
|
| 1083 | - return $model->next($current_value, $field, $query_params, $columns_to_select); |
|
| 1084 | - } |
|
| 1085 | - |
|
| 1086 | - |
|
| 1087 | - /** |
|
| 1088 | - * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
| 1089 | - * matching the given query conditions. |
|
| 1090 | - * |
|
| 1091 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1092 | - * @param array $query_params Any additional conditions on the query. |
|
| 1093 | - * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 1094 | - * you can indicate just the column you want returned |
|
| 1095 | - * @return array|EE_Base_Class |
|
| 1096 | - * @throws ReflectionException |
|
| 1097 | - * @throws InvalidArgumentException |
|
| 1098 | - * @throws InvalidInterfaceException |
|
| 1099 | - * @throws InvalidDataTypeException |
|
| 1100 | - * @throws EE_Error |
|
| 1101 | - */ |
|
| 1102 | - public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1103 | - { |
|
| 1104 | - $model = $this->get_model(); |
|
| 1105 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1106 | - ? $model->get_primary_key_field()->get_name() |
|
| 1107 | - : $field_to_order_by; |
|
| 1108 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1109 | - if (empty($field) || empty($current_value)) { |
|
| 1110 | - return array(); |
|
| 1111 | - } |
|
| 1112 | - return $model->previous($current_value, $field, $query_params, $columns_to_select); |
|
| 1113 | - } |
|
| 1114 | - |
|
| 1115 | - |
|
| 1116 | - /** |
|
| 1117 | - * Overrides parent because parent expects old models. |
|
| 1118 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
| 1119 | - * |
|
| 1120 | - * @param string $field_name |
|
| 1121 | - * @param mixed $field_value_from_db |
|
| 1122 | - * @throws ReflectionException |
|
| 1123 | - * @throws InvalidArgumentException |
|
| 1124 | - * @throws InvalidInterfaceException |
|
| 1125 | - * @throws InvalidDataTypeException |
|
| 1126 | - * @throws EE_Error |
|
| 1127 | - */ |
|
| 1128 | - public function set_from_db($field_name, $field_value_from_db) |
|
| 1129 | - { |
|
| 1130 | - $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 1131 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 1132 | - // you would think the DB has no NULLs for non-null label fields right? wrong! |
|
| 1133 | - // eg, a CPT model object could have an entry in the posts table, but no |
|
| 1134 | - // entry in the meta table. Meaning that all its columns in the meta table |
|
| 1135 | - // are null! yikes! so when we find one like that, use defaults for its meta columns |
|
| 1136 | - if ($field_value_from_db === null) { |
|
| 1137 | - if ($field_obj->is_nullable()) { |
|
| 1138 | - // if the field allows nulls, then let it be null |
|
| 1139 | - $field_value = null; |
|
| 1140 | - } else { |
|
| 1141 | - $field_value = $field_obj->get_default_value(); |
|
| 1142 | - } |
|
| 1143 | - } else { |
|
| 1144 | - $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
| 1145 | - } |
|
| 1146 | - $this->_fields[ $field_name ] = $field_value; |
|
| 1147 | - $this->_clear_cached_property($field_name); |
|
| 1148 | - } |
|
| 1149 | - } |
|
| 1150 | - |
|
| 1151 | - |
|
| 1152 | - /** |
|
| 1153 | - * verifies that the specified field is of the correct type |
|
| 1154 | - * |
|
| 1155 | - * @param string $field_name |
|
| 1156 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1157 | - * (in cases where the same property may be used for different outputs |
|
| 1158 | - * - i.e. datetime, money etc.) |
|
| 1159 | - * @return mixed |
|
| 1160 | - * @throws ReflectionException |
|
| 1161 | - * @throws InvalidArgumentException |
|
| 1162 | - * @throws InvalidInterfaceException |
|
| 1163 | - * @throws InvalidDataTypeException |
|
| 1164 | - * @throws EE_Error |
|
| 1165 | - */ |
|
| 1166 | - public function get($field_name, $extra_cache_ref = null) |
|
| 1167 | - { |
|
| 1168 | - return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
| 1169 | - } |
|
| 1170 | - |
|
| 1171 | - |
|
| 1172 | - /** |
|
| 1173 | - * This method simply returns the RAW unprocessed value for the given property in this class |
|
| 1174 | - * |
|
| 1175 | - * @param string $field_name A valid fieldname |
|
| 1176 | - * @return mixed Whatever the raw value stored on the property is. |
|
| 1177 | - * @throws ReflectionException |
|
| 1178 | - * @throws InvalidArgumentException |
|
| 1179 | - * @throws InvalidInterfaceException |
|
| 1180 | - * @throws InvalidDataTypeException |
|
| 1181 | - * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
| 1182 | - */ |
|
| 1183 | - public function get_raw($field_name) |
|
| 1184 | - { |
|
| 1185 | - $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1186 | - return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1187 | - ? $this->_fields[ $field_name ]->format('U') |
|
| 1188 | - : $this->_fields[ $field_name ]; |
|
| 1189 | - } |
|
| 1190 | - |
|
| 1191 | - |
|
| 1192 | - /** |
|
| 1193 | - * This is used to return the internal DateTime object used for a field that is a |
|
| 1194 | - * EE_Datetime_Field. |
|
| 1195 | - * |
|
| 1196 | - * @param string $field_name The field name retrieving the DateTime object. |
|
| 1197 | - * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
| 1198 | - * @throws EE_Error an error is set and false returned. If the field IS an |
|
| 1199 | - * EE_Datetime_Field and but the field value is null, then |
|
| 1200 | - * just null is returned (because that indicates that likely |
|
| 1201 | - * this field is nullable). |
|
| 1202 | - * @throws InvalidArgumentException |
|
| 1203 | - * @throws InvalidDataTypeException |
|
| 1204 | - * @throws InvalidInterfaceException |
|
| 1205 | - * @throws ReflectionException |
|
| 1206 | - */ |
|
| 1207 | - public function get_DateTime_object($field_name) |
|
| 1208 | - { |
|
| 1209 | - $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1210 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1211 | - EE_Error::add_error( |
|
| 1212 | - sprintf( |
|
| 1213 | - esc_html__( |
|
| 1214 | - 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
| 1215 | - 'event_espresso' |
|
| 1216 | - ), |
|
| 1217 | - $field_name |
|
| 1218 | - ), |
|
| 1219 | - __FILE__, |
|
| 1220 | - __FUNCTION__, |
|
| 1221 | - __LINE__ |
|
| 1222 | - ); |
|
| 1223 | - return false; |
|
| 1224 | - } |
|
| 1225 | - return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1226 | - ? clone $this->_fields[ $field_name ] |
|
| 1227 | - : null; |
|
| 1228 | - } |
|
| 1229 | - |
|
| 1230 | - |
|
| 1231 | - /** |
|
| 1232 | - * To be used in template to immediately echo out the value, and format it for output. |
|
| 1233 | - * Eg, should call stripslashes and whatnot before echoing |
|
| 1234 | - * |
|
| 1235 | - * @param string $field_name the name of the field as it appears in the DB |
|
| 1236 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1237 | - * (in cases where the same property may be used for different outputs |
|
| 1238 | - * - i.e. datetime, money etc.) |
|
| 1239 | - * @return void |
|
| 1240 | - * @throws ReflectionException |
|
| 1241 | - * @throws InvalidArgumentException |
|
| 1242 | - * @throws InvalidInterfaceException |
|
| 1243 | - * @throws InvalidDataTypeException |
|
| 1244 | - * @throws EE_Error |
|
| 1245 | - */ |
|
| 1246 | - public function e($field_name, $extra_cache_ref = null) |
|
| 1247 | - { |
|
| 1248 | - echo $this->get_pretty($field_name, $extra_cache_ref); |
|
| 1249 | - } |
|
| 1250 | - |
|
| 1251 | - |
|
| 1252 | - /** |
|
| 1253 | - * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
| 1254 | - * can be easily used as the value of form input. |
|
| 1255 | - * |
|
| 1256 | - * @param string $field_name |
|
| 1257 | - * @return void |
|
| 1258 | - * @throws ReflectionException |
|
| 1259 | - * @throws InvalidArgumentException |
|
| 1260 | - * @throws InvalidInterfaceException |
|
| 1261 | - * @throws InvalidDataTypeException |
|
| 1262 | - * @throws EE_Error |
|
| 1263 | - */ |
|
| 1264 | - public function f($field_name) |
|
| 1265 | - { |
|
| 1266 | - $this->e($field_name, 'form_input'); |
|
| 1267 | - } |
|
| 1268 | - |
|
| 1269 | - |
|
| 1270 | - /** |
|
| 1271 | - * Same as `f()` but just returns the value instead of echoing it |
|
| 1272 | - * |
|
| 1273 | - * @param string $field_name |
|
| 1274 | - * @return string |
|
| 1275 | - * @throws ReflectionException |
|
| 1276 | - * @throws InvalidArgumentException |
|
| 1277 | - * @throws InvalidInterfaceException |
|
| 1278 | - * @throws InvalidDataTypeException |
|
| 1279 | - * @throws EE_Error |
|
| 1280 | - */ |
|
| 1281 | - public function get_f($field_name) |
|
| 1282 | - { |
|
| 1283 | - return (string) $this->get_pretty($field_name, 'form_input'); |
|
| 1284 | - } |
|
| 1285 | - |
|
| 1286 | - |
|
| 1287 | - /** |
|
| 1288 | - * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
| 1289 | - * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
| 1290 | - * to see what options are available. |
|
| 1291 | - * |
|
| 1292 | - * @param string $field_name |
|
| 1293 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1294 | - * (in cases where the same property may be used for different outputs |
|
| 1295 | - * - i.e. datetime, money etc.) |
|
| 1296 | - * @return mixed |
|
| 1297 | - * @throws ReflectionException |
|
| 1298 | - * @throws InvalidArgumentException |
|
| 1299 | - * @throws InvalidInterfaceException |
|
| 1300 | - * @throws InvalidDataTypeException |
|
| 1301 | - * @throws EE_Error |
|
| 1302 | - */ |
|
| 1303 | - public function get_pretty($field_name, $extra_cache_ref = null) |
|
| 1304 | - { |
|
| 1305 | - return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
| 1306 | - } |
|
| 1307 | - |
|
| 1308 | - |
|
| 1309 | - /** |
|
| 1310 | - * This simply returns the datetime for the given field name |
|
| 1311 | - * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
| 1312 | - * (and the equivalent e_date, e_time, e_datetime). |
|
| 1313 | - * |
|
| 1314 | - * @access protected |
|
| 1315 | - * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
| 1316 | - * @param string $dt_frmt valid datetime format used for date |
|
| 1317 | - * (if '' then we just use the default on the field, |
|
| 1318 | - * if NULL we use the last-used format) |
|
| 1319 | - * @param string $tm_frmt Same as above except this is for time format |
|
| 1320 | - * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
| 1321 | - * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
| 1322 | - * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
| 1323 | - * if field is not a valid dtt field, or void if echoing |
|
| 1324 | - * @throws ReflectionException |
|
| 1325 | - * @throws InvalidArgumentException |
|
| 1326 | - * @throws InvalidInterfaceException |
|
| 1327 | - * @throws InvalidDataTypeException |
|
| 1328 | - * @throws EE_Error |
|
| 1329 | - */ |
|
| 1330 | - protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
| 1331 | - { |
|
| 1332 | - // clear cached property |
|
| 1333 | - $this->_clear_cached_property($field_name); |
|
| 1334 | - // reset format properties because they are used in get() |
|
| 1335 | - $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
| 1336 | - $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
| 1337 | - if ($echo) { |
|
| 1338 | - $this->e($field_name, $date_or_time); |
|
| 1339 | - return ''; |
|
| 1340 | - } |
|
| 1341 | - return $this->get($field_name, $date_or_time); |
|
| 1342 | - } |
|
| 1343 | - |
|
| 1344 | - |
|
| 1345 | - /** |
|
| 1346 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
| 1347 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1348 | - * other echoes the pretty value for dtt) |
|
| 1349 | - * |
|
| 1350 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1351 | - * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1352 | - * @return string datetime value formatted |
|
| 1353 | - * @throws ReflectionException |
|
| 1354 | - * @throws InvalidArgumentException |
|
| 1355 | - * @throws InvalidInterfaceException |
|
| 1356 | - * @throws InvalidDataTypeException |
|
| 1357 | - * @throws EE_Error |
|
| 1358 | - */ |
|
| 1359 | - public function get_date($field_name, $format = '') |
|
| 1360 | - { |
|
| 1361 | - return $this->_get_datetime($field_name, $format, null, 'D'); |
|
| 1362 | - } |
|
| 1363 | - |
|
| 1364 | - |
|
| 1365 | - /** |
|
| 1366 | - * @param $field_name |
|
| 1367 | - * @param string $format |
|
| 1368 | - * @throws ReflectionException |
|
| 1369 | - * @throws InvalidArgumentException |
|
| 1370 | - * @throws InvalidInterfaceException |
|
| 1371 | - * @throws InvalidDataTypeException |
|
| 1372 | - * @throws EE_Error |
|
| 1373 | - */ |
|
| 1374 | - public function e_date($field_name, $format = '') |
|
| 1375 | - { |
|
| 1376 | - $this->_get_datetime($field_name, $format, null, 'D', true); |
|
| 1377 | - } |
|
| 1378 | - |
|
| 1379 | - |
|
| 1380 | - /** |
|
| 1381 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
| 1382 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1383 | - * other echoes the pretty value for dtt) |
|
| 1384 | - * |
|
| 1385 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1386 | - * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
| 1387 | - * @return string datetime value formatted |
|
| 1388 | - * @throws ReflectionException |
|
| 1389 | - * @throws InvalidArgumentException |
|
| 1390 | - * @throws InvalidInterfaceException |
|
| 1391 | - * @throws InvalidDataTypeException |
|
| 1392 | - * @throws EE_Error |
|
| 1393 | - */ |
|
| 1394 | - public function get_time($field_name, $format = '') |
|
| 1395 | - { |
|
| 1396 | - return $this->_get_datetime($field_name, null, $format, 'T'); |
|
| 1397 | - } |
|
| 1398 | - |
|
| 1399 | - |
|
| 1400 | - /** |
|
| 1401 | - * @param $field_name |
|
| 1402 | - * @param string $format |
|
| 1403 | - * @throws ReflectionException |
|
| 1404 | - * @throws InvalidArgumentException |
|
| 1405 | - * @throws InvalidInterfaceException |
|
| 1406 | - * @throws InvalidDataTypeException |
|
| 1407 | - * @throws EE_Error |
|
| 1408 | - */ |
|
| 1409 | - public function e_time($field_name, $format = '') |
|
| 1410 | - { |
|
| 1411 | - $this->_get_datetime($field_name, null, $format, 'T', true); |
|
| 1412 | - } |
|
| 1413 | - |
|
| 1414 | - |
|
| 1415 | - /** |
|
| 1416 | - * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
| 1417 | - * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1418 | - * other echoes the pretty value for dtt) |
|
| 1419 | - * |
|
| 1420 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1421 | - * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1422 | - * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
| 1423 | - * @return string datetime value formatted |
|
| 1424 | - * @throws ReflectionException |
|
| 1425 | - * @throws InvalidArgumentException |
|
| 1426 | - * @throws InvalidInterfaceException |
|
| 1427 | - * @throws InvalidDataTypeException |
|
| 1428 | - * @throws EE_Error |
|
| 1429 | - */ |
|
| 1430 | - public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1431 | - { |
|
| 1432 | - return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
| 1433 | - } |
|
| 1434 | - |
|
| 1435 | - |
|
| 1436 | - /** |
|
| 1437 | - * @param string $field_name |
|
| 1438 | - * @param string $dt_frmt |
|
| 1439 | - * @param string $tm_frmt |
|
| 1440 | - * @throws ReflectionException |
|
| 1441 | - * @throws InvalidArgumentException |
|
| 1442 | - * @throws InvalidInterfaceException |
|
| 1443 | - * @throws InvalidDataTypeException |
|
| 1444 | - * @throws EE_Error |
|
| 1445 | - */ |
|
| 1446 | - public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1447 | - { |
|
| 1448 | - $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
| 1449 | - } |
|
| 1450 | - |
|
| 1451 | - |
|
| 1452 | - /** |
|
| 1453 | - * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
| 1454 | - * |
|
| 1455 | - * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
| 1456 | - * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
| 1457 | - * on the object will be used. |
|
| 1458 | - * @return string Date and time string in set locale or false if no field exists for the given |
|
| 1459 | - * @throws ReflectionException |
|
| 1460 | - * @throws InvalidArgumentException |
|
| 1461 | - * @throws InvalidInterfaceException |
|
| 1462 | - * @throws InvalidDataTypeException |
|
| 1463 | - * @throws EE_Error |
|
| 1464 | - * field name. |
|
| 1465 | - */ |
|
| 1466 | - public function get_i18n_datetime($field_name, $format = '') |
|
| 1467 | - { |
|
| 1468 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1469 | - return date_i18n( |
|
| 1470 | - $format, |
|
| 1471 | - EEH_DTT_Helper::get_timestamp_with_offset( |
|
| 1472 | - $this->get_raw($field_name), |
|
| 1473 | - $this->_timezone |
|
| 1474 | - ) |
|
| 1475 | - ); |
|
| 1476 | - } |
|
| 1477 | - |
|
| 1478 | - |
|
| 1479 | - /** |
|
| 1480 | - * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
| 1481 | - * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
| 1482 | - * thrown. |
|
| 1483 | - * |
|
| 1484 | - * @param string $field_name The field name being checked |
|
| 1485 | - * @throws ReflectionException |
|
| 1486 | - * @throws InvalidArgumentException |
|
| 1487 | - * @throws InvalidInterfaceException |
|
| 1488 | - * @throws InvalidDataTypeException |
|
| 1489 | - * @throws EE_Error |
|
| 1490 | - * @return EE_Datetime_Field |
|
| 1491 | - */ |
|
| 1492 | - protected function _get_dtt_field_settings($field_name) |
|
| 1493 | - { |
|
| 1494 | - $field = $this->get_model()->field_settings_for($field_name); |
|
| 1495 | - // check if field is dtt |
|
| 1496 | - if ($field instanceof EE_Datetime_Field) { |
|
| 1497 | - return $field; |
|
| 1498 | - } |
|
| 1499 | - throw new EE_Error( |
|
| 1500 | - sprintf( |
|
| 1501 | - esc_html__( |
|
| 1502 | - 'The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field. Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor', |
|
| 1503 | - 'event_espresso' |
|
| 1504 | - ), |
|
| 1505 | - $field_name, |
|
| 1506 | - self::_get_model_classname(get_class($this)) |
|
| 1507 | - ) |
|
| 1508 | - ); |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - |
|
| 1512 | - |
|
| 1513 | - |
|
| 1514 | - /** |
|
| 1515 | - * NOTE ABOUT BELOW: |
|
| 1516 | - * These convenience date and time setters are for setting date and time independently. In other words you might |
|
| 1517 | - * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
| 1518 | - * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
| 1519 | - * method and make sure you send the entire datetime value for setting. |
|
| 1520 | - */ |
|
| 1521 | - /** |
|
| 1522 | - * sets the time on a datetime property |
|
| 1523 | - * |
|
| 1524 | - * @access protected |
|
| 1525 | - * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1526 | - * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
| 1527 | - * @throws ReflectionException |
|
| 1528 | - * @throws InvalidArgumentException |
|
| 1529 | - * @throws InvalidInterfaceException |
|
| 1530 | - * @throws InvalidDataTypeException |
|
| 1531 | - * @throws EE_Error |
|
| 1532 | - */ |
|
| 1533 | - protected function _set_time_for($time, $fieldname) |
|
| 1534 | - { |
|
| 1535 | - $this->_set_date_time('T', $time, $fieldname); |
|
| 1536 | - } |
|
| 1537 | - |
|
| 1538 | - |
|
| 1539 | - /** |
|
| 1540 | - * sets the date on a datetime property |
|
| 1541 | - * |
|
| 1542 | - * @access protected |
|
| 1543 | - * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1544 | - * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
| 1545 | - * @throws ReflectionException |
|
| 1546 | - * @throws InvalidArgumentException |
|
| 1547 | - * @throws InvalidInterfaceException |
|
| 1548 | - * @throws InvalidDataTypeException |
|
| 1549 | - * @throws EE_Error |
|
| 1550 | - */ |
|
| 1551 | - protected function _set_date_for($date, $fieldname) |
|
| 1552 | - { |
|
| 1553 | - $this->_set_date_time('D', $date, $fieldname); |
|
| 1554 | - } |
|
| 1555 | - |
|
| 1556 | - |
|
| 1557 | - /** |
|
| 1558 | - * This takes care of setting a date or time independently on a given model object property. This method also |
|
| 1559 | - * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
| 1560 | - * |
|
| 1561 | - * @access protected |
|
| 1562 | - * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
| 1563 | - * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
| 1564 | - * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
| 1565 | - * EE_Datetime_Field property) |
|
| 1566 | - * @throws ReflectionException |
|
| 1567 | - * @throws InvalidArgumentException |
|
| 1568 | - * @throws InvalidInterfaceException |
|
| 1569 | - * @throws InvalidDataTypeException |
|
| 1570 | - * @throws EE_Error |
|
| 1571 | - */ |
|
| 1572 | - protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
| 1573 | - { |
|
| 1574 | - $field = $this->_get_dtt_field_settings($fieldname); |
|
| 1575 | - $field->set_timezone($this->_timezone); |
|
| 1576 | - $field->set_date_format($this->_dt_frmt); |
|
| 1577 | - $field->set_time_format($this->_tm_frmt); |
|
| 1578 | - switch ($what) { |
|
| 1579 | - case 'T': |
|
| 1580 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time( |
|
| 1581 | - $datetime_value, |
|
| 1582 | - $this->_fields[ $fieldname ] |
|
| 1583 | - ); |
|
| 1584 | - $this->_has_changes = true; |
|
| 1585 | - break; |
|
| 1586 | - case 'D': |
|
| 1587 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
| 1588 | - $datetime_value, |
|
| 1589 | - $this->_fields[ $fieldname ] |
|
| 1590 | - ); |
|
| 1591 | - $this->_has_changes = true; |
|
| 1592 | - break; |
|
| 1593 | - case 'B': |
|
| 1594 | - $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
| 1595 | - $this->_has_changes = true; |
|
| 1596 | - break; |
|
| 1597 | - } |
|
| 1598 | - $this->_clear_cached_property($fieldname); |
|
| 1599 | - } |
|
| 1600 | - |
|
| 1601 | - |
|
| 1602 | - /** |
|
| 1603 | - * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1604 | - * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1605 | - * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1606 | - * that could lead to some unexpected results! |
|
| 1607 | - * |
|
| 1608 | - * @access public |
|
| 1609 | - * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1610 | - * value being returned. |
|
| 1611 | - * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1612 | - * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1613 | - * @param string $prepend You can include something to prepend on the timestamp |
|
| 1614 | - * @param string $append You can include something to append on the timestamp |
|
| 1615 | - * @throws ReflectionException |
|
| 1616 | - * @throws InvalidArgumentException |
|
| 1617 | - * @throws InvalidInterfaceException |
|
| 1618 | - * @throws InvalidDataTypeException |
|
| 1619 | - * @throws EE_Error |
|
| 1620 | - * @return string timestamp |
|
| 1621 | - */ |
|
| 1622 | - public function display_in_my_timezone( |
|
| 1623 | - $field_name, |
|
| 1624 | - $callback = 'get_datetime', |
|
| 1625 | - $args = null, |
|
| 1626 | - $prepend = '', |
|
| 1627 | - $append = '' |
|
| 1628 | - ) { |
|
| 1629 | - $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1630 | - if ($timezone === $this->_timezone) { |
|
| 1631 | - return ''; |
|
| 1632 | - } |
|
| 1633 | - $original_timezone = $this->_timezone; |
|
| 1634 | - $this->set_timezone($timezone); |
|
| 1635 | - $fn = (array) $field_name; |
|
| 1636 | - $args = array_merge($fn, (array) $args); |
|
| 1637 | - if (! method_exists($this, $callback)) { |
|
| 1638 | - throw new EE_Error( |
|
| 1639 | - sprintf( |
|
| 1640 | - esc_html__( |
|
| 1641 | - 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1642 | - 'event_espresso' |
|
| 1643 | - ), |
|
| 1644 | - $callback |
|
| 1645 | - ) |
|
| 1646 | - ); |
|
| 1647 | - } |
|
| 1648 | - $args = (array) $args; |
|
| 1649 | - $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1650 | - $this->set_timezone($original_timezone); |
|
| 1651 | - return $return; |
|
| 1652 | - } |
|
| 1653 | - |
|
| 1654 | - |
|
| 1655 | - /** |
|
| 1656 | - * Deletes this model object. |
|
| 1657 | - * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1658 | - * override |
|
| 1659 | - * `EE_Base_Class::_delete` NOT this class. |
|
| 1660 | - * |
|
| 1661 | - * @return boolean | int |
|
| 1662 | - * @throws ReflectionException |
|
| 1663 | - * @throws InvalidArgumentException |
|
| 1664 | - * @throws InvalidInterfaceException |
|
| 1665 | - * @throws InvalidDataTypeException |
|
| 1666 | - * @throws EE_Error |
|
| 1667 | - */ |
|
| 1668 | - public function delete() |
|
| 1669 | - { |
|
| 1670 | - /** |
|
| 1671 | - * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1672 | - * Note: |
|
| 1673 | - * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1674 | - * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1675 | - * For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1676 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1677 | - * |
|
| 1678 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1679 | - */ |
|
| 1680 | - do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1681 | - $result = $this->_delete(); |
|
| 1682 | - /** |
|
| 1683 | - * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1684 | - * Note: |
|
| 1685 | - * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1686 | - * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1687 | - * For example `EE_Soft_Base_Class::_delete` |
|
| 1688 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1689 | - * |
|
| 1690 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1691 | - * @param boolean $result |
|
| 1692 | - */ |
|
| 1693 | - do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1694 | - return $result; |
|
| 1695 | - } |
|
| 1696 | - |
|
| 1697 | - |
|
| 1698 | - /** |
|
| 1699 | - * Calls the specific delete method for the instantiated class. |
|
| 1700 | - * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1701 | - * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1702 | - * `EE_Base_Class::delete` |
|
| 1703 | - * |
|
| 1704 | - * @return bool|int |
|
| 1705 | - * @throws ReflectionException |
|
| 1706 | - * @throws InvalidArgumentException |
|
| 1707 | - * @throws InvalidInterfaceException |
|
| 1708 | - * @throws InvalidDataTypeException |
|
| 1709 | - * @throws EE_Error |
|
| 1710 | - */ |
|
| 1711 | - protected function _delete() |
|
| 1712 | - { |
|
| 1713 | - return $this->delete_permanently(); |
|
| 1714 | - } |
|
| 1715 | - |
|
| 1716 | - |
|
| 1717 | - /** |
|
| 1718 | - * Deletes this model object permanently from db |
|
| 1719 | - * (but keep in mind related models may block the delete and return an error) |
|
| 1720 | - * |
|
| 1721 | - * @return bool | int |
|
| 1722 | - * @throws ReflectionException |
|
| 1723 | - * @throws InvalidArgumentException |
|
| 1724 | - * @throws InvalidInterfaceException |
|
| 1725 | - * @throws InvalidDataTypeException |
|
| 1726 | - * @throws EE_Error |
|
| 1727 | - */ |
|
| 1728 | - public function delete_permanently() |
|
| 1729 | - { |
|
| 1730 | - /** |
|
| 1731 | - * Called just before HARD deleting a model object |
|
| 1732 | - * |
|
| 1733 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1734 | - */ |
|
| 1735 | - do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1736 | - $model = $this->get_model(); |
|
| 1737 | - $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1738 | - $this->refresh_cache_of_related_objects(); |
|
| 1739 | - /** |
|
| 1740 | - * Called just after HARD deleting a model object |
|
| 1741 | - * |
|
| 1742 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1743 | - * @param boolean $result |
|
| 1744 | - */ |
|
| 1745 | - do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1746 | - return $result; |
|
| 1747 | - } |
|
| 1748 | - |
|
| 1749 | - |
|
| 1750 | - /** |
|
| 1751 | - * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1752 | - * related model objects |
|
| 1753 | - * |
|
| 1754 | - * @throws ReflectionException |
|
| 1755 | - * @throws InvalidArgumentException |
|
| 1756 | - * @throws InvalidInterfaceException |
|
| 1757 | - * @throws InvalidDataTypeException |
|
| 1758 | - * @throws EE_Error |
|
| 1759 | - */ |
|
| 1760 | - public function refresh_cache_of_related_objects() |
|
| 1761 | - { |
|
| 1762 | - $model = $this->get_model(); |
|
| 1763 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1764 | - if (! empty($this->_model_relations[ $relation_name ])) { |
|
| 1765 | - $related_objects = $this->_model_relations[ $relation_name ]; |
|
| 1766 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1767 | - // this relation only stores a single model object, not an array |
|
| 1768 | - // but let's make it consistent |
|
| 1769 | - $related_objects = array($related_objects); |
|
| 1770 | - } |
|
| 1771 | - foreach ($related_objects as $related_object) { |
|
| 1772 | - // only refresh their cache if they're in memory |
|
| 1773 | - if ($related_object instanceof EE_Base_Class) { |
|
| 1774 | - $related_object->clear_cache( |
|
| 1775 | - $model->get_this_model_name(), |
|
| 1776 | - $this |
|
| 1777 | - ); |
|
| 1778 | - } |
|
| 1779 | - } |
|
| 1780 | - } |
|
| 1781 | - } |
|
| 1782 | - } |
|
| 1783 | - |
|
| 1784 | - |
|
| 1785 | - /** |
|
| 1786 | - * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1787 | - * object just before saving. |
|
| 1788 | - * |
|
| 1789 | - * @access public |
|
| 1790 | - * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1791 | - * if provided during the save() method (often client code will change the fields' |
|
| 1792 | - * values before calling save) |
|
| 1793 | - * @throws InvalidArgumentException |
|
| 1794 | - * @throws InvalidInterfaceException |
|
| 1795 | - * @throws InvalidDataTypeException |
|
| 1796 | - * @throws EE_Error |
|
| 1797 | - * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1798 | - * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1799 | - * @throws ReflectionException |
|
| 1800 | - * @throws ReflectionException |
|
| 1801 | - * @throws ReflectionException |
|
| 1802 | - */ |
|
| 1803 | - public function save($set_cols_n_values = array()) |
|
| 1804 | - { |
|
| 1805 | - $model = $this->get_model(); |
|
| 1806 | - /** |
|
| 1807 | - * Filters the fields we're about to save on the model object |
|
| 1808 | - * |
|
| 1809 | - * @param array $set_cols_n_values |
|
| 1810 | - * @param EE_Base_Class $model_object |
|
| 1811 | - */ |
|
| 1812 | - $set_cols_n_values = (array) apply_filters( |
|
| 1813 | - 'FHEE__EE_Base_Class__save__set_cols_n_values', |
|
| 1814 | - $set_cols_n_values, |
|
| 1815 | - $this |
|
| 1816 | - ); |
|
| 1817 | - // set attributes as provided in $set_cols_n_values |
|
| 1818 | - foreach ($set_cols_n_values as $column => $value) { |
|
| 1819 | - $this->set($column, $value); |
|
| 1820 | - } |
|
| 1821 | - // no changes ? then don't do anything |
|
| 1822 | - if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1823 | - return 0; |
|
| 1824 | - } |
|
| 1825 | - /** |
|
| 1826 | - * Saving a model object. |
|
| 1827 | - * Before we perform a save, this action is fired. |
|
| 1828 | - * |
|
| 1829 | - * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1830 | - */ |
|
| 1831 | - do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1832 | - if (! $this->allow_persist()) { |
|
| 1833 | - return 0; |
|
| 1834 | - } |
|
| 1835 | - // now get current attribute values |
|
| 1836 | - $save_cols_n_values = $this->_fields; |
|
| 1837 | - // if the object already has an ID, update it. Otherwise, insert it |
|
| 1838 | - // also: change the assumption about values passed to the model NOT being prepare dby the model object. |
|
| 1839 | - // They have been |
|
| 1840 | - $old_assumption_concerning_value_preparation = $model |
|
| 1841 | - ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1842 | - $model->assume_values_already_prepared_by_model_object(true); |
|
| 1843 | - // does this model have an autoincrement PK? |
|
| 1844 | - if ($model->has_primary_key_field()) { |
|
| 1845 | - if ($model->get_primary_key_field()->is_auto_increment()) { |
|
| 1846 | - // ok check if it's set, if so: update; if not, insert |
|
| 1847 | - if (! empty($save_cols_n_values[ $model->primary_key_name() ])) { |
|
| 1848 | - $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1849 | - } else { |
|
| 1850 | - unset($save_cols_n_values[ $model->primary_key_name() ]); |
|
| 1851 | - $results = $model->insert($save_cols_n_values); |
|
| 1852 | - if ($results) { |
|
| 1853 | - // if successful, set the primary key |
|
| 1854 | - // but don't use the normal SET method, because it will check if |
|
| 1855 | - // an item with the same ID exists in the mapper & db, then |
|
| 1856 | - // will find it in the db (because we just added it) and THAT object |
|
| 1857 | - // will get added to the mapper before we can add this one! |
|
| 1858 | - // but if we just avoid using the SET method, all that headache can be avoided |
|
| 1859 | - $pk_field_name = $model->primary_key_name(); |
|
| 1860 | - $this->_fields[ $pk_field_name ] = $results; |
|
| 1861 | - $this->_clear_cached_property($pk_field_name); |
|
| 1862 | - $model->add_to_entity_map($this); |
|
| 1863 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1864 | - } |
|
| 1865 | - } |
|
| 1866 | - } else {// PK is NOT auto-increment |
|
| 1867 | - // so check if one like it already exists in the db |
|
| 1868 | - if ($model->exists_by_ID($this->ID())) { |
|
| 1869 | - if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1870 | - throw new EE_Error( |
|
| 1871 | - sprintf( |
|
| 1872 | - esc_html__( |
|
| 1873 | - 'Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
|
| 1874 | - 'event_espresso' |
|
| 1875 | - ), |
|
| 1876 | - get_class($this), |
|
| 1877 | - get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1878 | - get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1879 | - '<br />' |
|
| 1880 | - ) |
|
| 1881 | - ); |
|
| 1882 | - } |
|
| 1883 | - $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1884 | - } else { |
|
| 1885 | - $results = $model->insert($save_cols_n_values); |
|
| 1886 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1887 | - } |
|
| 1888 | - } |
|
| 1889 | - } else {// there is NO primary key |
|
| 1890 | - $already_in_db = false; |
|
| 1891 | - foreach ($model->unique_indexes() as $index) { |
|
| 1892 | - $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1893 | - if ($model->exists(array($uniqueness_where_params))) { |
|
| 1894 | - $already_in_db = true; |
|
| 1895 | - } |
|
| 1896 | - } |
|
| 1897 | - if ($already_in_db) { |
|
| 1898 | - $combined_pk_fields_n_values = array_intersect_key( |
|
| 1899 | - $save_cols_n_values, |
|
| 1900 | - $model->get_combined_primary_key_fields() |
|
| 1901 | - ); |
|
| 1902 | - $results = $model->update( |
|
| 1903 | - $save_cols_n_values, |
|
| 1904 | - $combined_pk_fields_n_values |
|
| 1905 | - ); |
|
| 1906 | - } else { |
|
| 1907 | - $results = $model->insert($save_cols_n_values); |
|
| 1908 | - } |
|
| 1909 | - } |
|
| 1910 | - // restore the old assumption about values being prepared by the model object |
|
| 1911 | - $model->assume_values_already_prepared_by_model_object( |
|
| 1912 | - $old_assumption_concerning_value_preparation |
|
| 1913 | - ); |
|
| 1914 | - /** |
|
| 1915 | - * After saving the model object this action is called |
|
| 1916 | - * |
|
| 1917 | - * @param EE_Base_Class $model_object which was just saved |
|
| 1918 | - * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1919 | - * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1920 | - */ |
|
| 1921 | - do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1922 | - $this->_has_changes = false; |
|
| 1923 | - return $results; |
|
| 1924 | - } |
|
| 1925 | - |
|
| 1926 | - |
|
| 1927 | - /** |
|
| 1928 | - * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1929 | - * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1930 | - * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1931 | - * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1932 | - * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the |
|
| 1933 | - * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1934 | - * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1935 | - * |
|
| 1936 | - * @return void |
|
| 1937 | - * @throws ReflectionException |
|
| 1938 | - * @throws InvalidArgumentException |
|
| 1939 | - * @throws InvalidInterfaceException |
|
| 1940 | - * @throws InvalidDataTypeException |
|
| 1941 | - * @throws EE_Error |
|
| 1942 | - */ |
|
| 1943 | - protected function _update_cached_related_model_objs_fks() |
|
| 1944 | - { |
|
| 1945 | - $model = $this->get_model(); |
|
| 1946 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1947 | - if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1948 | - foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1949 | - $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1950 | - $model->get_this_model_name() |
|
| 1951 | - ); |
|
| 1952 | - $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1953 | - if ($related_model_obj_in_cache->ID()) { |
|
| 1954 | - $related_model_obj_in_cache->save(); |
|
| 1955 | - } |
|
| 1956 | - } |
|
| 1957 | - } |
|
| 1958 | - } |
|
| 1959 | - } |
|
| 1960 | - |
|
| 1961 | - |
|
| 1962 | - /** |
|
| 1963 | - * Saves this model object and its NEW cached relations to the database. |
|
| 1964 | - * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1965 | - * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1966 | - * because otherwise, there's a potential for infinite looping of saving |
|
| 1967 | - * Saves the cached related model objects, and ensures the relation between them |
|
| 1968 | - * and this object and properly setup |
|
| 1969 | - * |
|
| 1970 | - * @return int ID of new model object on save; 0 on failure+ |
|
| 1971 | - * @throws ReflectionException |
|
| 1972 | - * @throws InvalidArgumentException |
|
| 1973 | - * @throws InvalidInterfaceException |
|
| 1974 | - * @throws InvalidDataTypeException |
|
| 1975 | - * @throws EE_Error |
|
| 1976 | - */ |
|
| 1977 | - public function save_new_cached_related_model_objs() |
|
| 1978 | - { |
|
| 1979 | - // make sure this has been saved |
|
| 1980 | - if (! $this->ID()) { |
|
| 1981 | - $id = $this->save(); |
|
| 1982 | - } else { |
|
| 1983 | - $id = $this->ID(); |
|
| 1984 | - } |
|
| 1985 | - // now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1986 | - foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1987 | - if ($this->_model_relations[ $relationName ]) { |
|
| 1988 | - // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1989 | - // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1990 | - /* @var $related_model_obj EE_Base_Class */ |
|
| 1991 | - if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1992 | - // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1993 | - // but ONLY if it DOES NOT exist in the DB |
|
| 1994 | - $related_model_obj = $this->_model_relations[ $relationName ]; |
|
| 1995 | - // if( ! $related_model_obj->ID()){ |
|
| 1996 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1997 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1998 | - // } |
|
| 1999 | - } else { |
|
| 2000 | - foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
| 2001 | - // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 2002 | - // but ONLY if it DOES NOT exist in the DB |
|
| 2003 | - // if( ! $related_model_obj->ID()){ |
|
| 2004 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 2005 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 2006 | - // } |
|
| 2007 | - } |
|
| 2008 | - } |
|
| 2009 | - } |
|
| 2010 | - } |
|
| 2011 | - return $id; |
|
| 2012 | - } |
|
| 2013 | - |
|
| 2014 | - |
|
| 2015 | - /** |
|
| 2016 | - * for getting a model while instantiated. |
|
| 2017 | - * |
|
| 2018 | - * @return EEM_Base | EEM_CPT_Base |
|
| 2019 | - * @throws ReflectionException |
|
| 2020 | - * @throws InvalidArgumentException |
|
| 2021 | - * @throws InvalidInterfaceException |
|
| 2022 | - * @throws InvalidDataTypeException |
|
| 2023 | - * @throws EE_Error |
|
| 2024 | - */ |
|
| 2025 | - public function get_model() |
|
| 2026 | - { |
|
| 2027 | - if (! $this->_model) { |
|
| 2028 | - $modelName = self::_get_model_classname(get_class($this)); |
|
| 2029 | - $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 2030 | - } else { |
|
| 2031 | - $this->_model->set_timezone($this->_timezone); |
|
| 2032 | - } |
|
| 2033 | - return $this->_model; |
|
| 2034 | - } |
|
| 2035 | - |
|
| 2036 | - |
|
| 2037 | - /** |
|
| 2038 | - * @param $props_n_values |
|
| 2039 | - * @param $classname |
|
| 2040 | - * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 2041 | - * @throws ReflectionException |
|
| 2042 | - * @throws InvalidArgumentException |
|
| 2043 | - * @throws InvalidInterfaceException |
|
| 2044 | - * @throws InvalidDataTypeException |
|
| 2045 | - * @throws EE_Error |
|
| 2046 | - */ |
|
| 2047 | - protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 2048 | - { |
|
| 2049 | - // TODO: will not work for Term_Relationships because they have no PK! |
|
| 2050 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2051 | - if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2052 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2053 | - ) { |
|
| 2054 | - $id = $props_n_values[ $primary_id_ref ]; |
|
| 2055 | - return self::_get_model($classname)->get_from_entity_map($id); |
|
| 2056 | - } |
|
| 2057 | - return false; |
|
| 2058 | - } |
|
| 2059 | - |
|
| 2060 | - |
|
| 2061 | - /** |
|
| 2062 | - * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 2063 | - * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 2064 | - * primary key for the model AND it is not null, then we check the db. If there's a an object we return it. If not |
|
| 2065 | - * we return false. |
|
| 2066 | - * |
|
| 2067 | - * @param array $props_n_values incoming array of properties and their values |
|
| 2068 | - * @param string $classname the classname of the child class |
|
| 2069 | - * @param null $timezone |
|
| 2070 | - * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 2071 | - * date_format and the second value is the time format |
|
| 2072 | - * @return mixed (EE_Base_Class|bool) |
|
| 2073 | - * @throws InvalidArgumentException |
|
| 2074 | - * @throws InvalidInterfaceException |
|
| 2075 | - * @throws InvalidDataTypeException |
|
| 2076 | - * @throws EE_Error |
|
| 2077 | - * @throws ReflectionException |
|
| 2078 | - * @throws ReflectionException |
|
| 2079 | - * @throws ReflectionException |
|
| 2080 | - */ |
|
| 2081 | - protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 2082 | - { |
|
| 2083 | - $existing = null; |
|
| 2084 | - $model = self::_get_model($classname, $timezone); |
|
| 2085 | - if ($model->has_primary_key_field()) { |
|
| 2086 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2087 | - if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2088 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2089 | - ) { |
|
| 2090 | - $existing = $model->get_one_by_ID( |
|
| 2091 | - $props_n_values[ $primary_id_ref ] |
|
| 2092 | - ); |
|
| 2093 | - } |
|
| 2094 | - } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 2095 | - // no primary key on this model, but there's still a matching item in the DB |
|
| 2096 | - $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 2097 | - self::_get_model($classname, $timezone) |
|
| 2098 | - ->get_index_primary_key_string($props_n_values) |
|
| 2099 | - ); |
|
| 2100 | - } |
|
| 2101 | - if ($existing) { |
|
| 2102 | - // set date formats if present before setting values |
|
| 2103 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
| 2104 | - $existing->set_date_format($date_formats[0]); |
|
| 2105 | - $existing->set_time_format($date_formats[1]); |
|
| 2106 | - } else { |
|
| 2107 | - // set default formats for date and time |
|
| 2108 | - $existing->set_date_format(get_option('date_format')); |
|
| 2109 | - $existing->set_time_format(get_option('time_format')); |
|
| 2110 | - } |
|
| 2111 | - foreach ($props_n_values as $property => $field_value) { |
|
| 2112 | - $existing->set($property, $field_value); |
|
| 2113 | - } |
|
| 2114 | - return $existing; |
|
| 2115 | - } |
|
| 2116 | - return false; |
|
| 2117 | - } |
|
| 2118 | - |
|
| 2119 | - |
|
| 2120 | - /** |
|
| 2121 | - * Gets the EEM_*_Model for this class |
|
| 2122 | - * |
|
| 2123 | - * @access public now, as this is more convenient |
|
| 2124 | - * @param $classname |
|
| 2125 | - * @param null $timezone |
|
| 2126 | - * @throws ReflectionException |
|
| 2127 | - * @throws InvalidArgumentException |
|
| 2128 | - * @throws InvalidInterfaceException |
|
| 2129 | - * @throws InvalidDataTypeException |
|
| 2130 | - * @throws EE_Error |
|
| 2131 | - * @return EEM_Base |
|
| 2132 | - */ |
|
| 2133 | - protected static function _get_model($classname, $timezone = null) |
|
| 2134 | - { |
|
| 2135 | - // find model for this class |
|
| 2136 | - if (! $classname) { |
|
| 2137 | - throw new EE_Error( |
|
| 2138 | - sprintf( |
|
| 2139 | - esc_html__( |
|
| 2140 | - 'What were you thinking calling _get_model(%s)?? You need to specify the class name', |
|
| 2141 | - 'event_espresso' |
|
| 2142 | - ), |
|
| 2143 | - $classname |
|
| 2144 | - ) |
|
| 2145 | - ); |
|
| 2146 | - } |
|
| 2147 | - $modelName = self::_get_model_classname($classname); |
|
| 2148 | - return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 2149 | - } |
|
| 2150 | - |
|
| 2151 | - |
|
| 2152 | - /** |
|
| 2153 | - * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 2154 | - * |
|
| 2155 | - * @param string $model_classname |
|
| 2156 | - * @param null $timezone |
|
| 2157 | - * @return EEM_Base |
|
| 2158 | - * @throws ReflectionException |
|
| 2159 | - * @throws InvalidArgumentException |
|
| 2160 | - * @throws InvalidInterfaceException |
|
| 2161 | - * @throws InvalidDataTypeException |
|
| 2162 | - * @throws EE_Error |
|
| 2163 | - */ |
|
| 2164 | - protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 2165 | - { |
|
| 2166 | - $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 2167 | - $model = EE_Registry::instance()->load_model($model_classname); |
|
| 2168 | - $model->set_timezone($timezone); |
|
| 2169 | - return $model; |
|
| 2170 | - } |
|
| 2171 | - |
|
| 2172 | - |
|
| 2173 | - /** |
|
| 2174 | - * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 2175 | - * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 2176 | - * |
|
| 2177 | - * @param null $model_name |
|
| 2178 | - * @return string like EEM_Attendee |
|
| 2179 | - */ |
|
| 2180 | - private static function _get_model_classname($model_name = null) |
|
| 2181 | - { |
|
| 2182 | - if (strpos($model_name, 'EE_') === 0) { |
|
| 2183 | - $model_classname = str_replace('EE_', 'EEM_', $model_name); |
|
| 2184 | - } else { |
|
| 2185 | - $model_classname = 'EEM_' . $model_name; |
|
| 2186 | - } |
|
| 2187 | - return $model_classname; |
|
| 2188 | - } |
|
| 2189 | - |
|
| 2190 | - |
|
| 2191 | - /** |
|
| 2192 | - * returns the name of the primary key attribute |
|
| 2193 | - * |
|
| 2194 | - * @param null $classname |
|
| 2195 | - * @throws ReflectionException |
|
| 2196 | - * @throws InvalidArgumentException |
|
| 2197 | - * @throws InvalidInterfaceException |
|
| 2198 | - * @throws InvalidDataTypeException |
|
| 2199 | - * @throws EE_Error |
|
| 2200 | - * @return string |
|
| 2201 | - */ |
|
| 2202 | - protected static function _get_primary_key_name($classname = null) |
|
| 2203 | - { |
|
| 2204 | - if (! $classname) { |
|
| 2205 | - throw new EE_Error( |
|
| 2206 | - sprintf( |
|
| 2207 | - esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
|
| 2208 | - $classname |
|
| 2209 | - ) |
|
| 2210 | - ); |
|
| 2211 | - } |
|
| 2212 | - return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 2213 | - } |
|
| 2214 | - |
|
| 2215 | - |
|
| 2216 | - /** |
|
| 2217 | - * Gets the value of the primary key. |
|
| 2218 | - * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 2219 | - * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 2220 | - * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 2221 | - * |
|
| 2222 | - * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 2223 | - * @throws ReflectionException |
|
| 2224 | - * @throws InvalidArgumentException |
|
| 2225 | - * @throws InvalidInterfaceException |
|
| 2226 | - * @throws InvalidDataTypeException |
|
| 2227 | - * @throws EE_Error |
|
| 2228 | - */ |
|
| 2229 | - public function ID() |
|
| 2230 | - { |
|
| 2231 | - $model = $this->get_model(); |
|
| 2232 | - // now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 2233 | - if ($model->has_primary_key_field()) { |
|
| 2234 | - return $this->_fields[ $model->primary_key_name() ]; |
|
| 2235 | - } |
|
| 2236 | - return $model->get_index_primary_key_string($this->_fields); |
|
| 2237 | - } |
|
| 2238 | - |
|
| 2239 | - |
|
| 2240 | - /** |
|
| 2241 | - * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 2242 | - * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 2243 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 2244 | - * |
|
| 2245 | - * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 2246 | - * @param string $relationName eg 'Events','Question',etc. |
|
| 2247 | - * an attendee to a group, you also want to specify which role they |
|
| 2248 | - * will have in that group. So you would use this parameter to |
|
| 2249 | - * specify array('role-column-name'=>'role-id') |
|
| 2250 | - * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 2251 | - * allow you to further constrict the relation to being added. |
|
| 2252 | - * However, keep in mind that the columns (keys) given must match a |
|
| 2253 | - * column on the JOIN table and currently only the HABTM models |
|
| 2254 | - * accept these additional conditions. Also remember that if an |
|
| 2255 | - * exact match isn't found for these extra cols/val pairs, then a |
|
| 2256 | - * NEW row is created in the join table. |
|
| 2257 | - * @param null $cache_id |
|
| 2258 | - * @throws ReflectionException |
|
| 2259 | - * @throws InvalidArgumentException |
|
| 2260 | - * @throws InvalidInterfaceException |
|
| 2261 | - * @throws InvalidDataTypeException |
|
| 2262 | - * @throws EE_Error |
|
| 2263 | - * @return EE_Base_Class the object the relation was added to |
|
| 2264 | - */ |
|
| 2265 | - public function _add_relation_to( |
|
| 2266 | - $otherObjectModelObjectOrID, |
|
| 2267 | - $relationName, |
|
| 2268 | - $extra_join_model_fields_n_values = array(), |
|
| 2269 | - $cache_id = null |
|
| 2270 | - ) { |
|
| 2271 | - $model = $this->get_model(); |
|
| 2272 | - // if this thing exists in the DB, save the relation to the DB |
|
| 2273 | - if ($this->ID()) { |
|
| 2274 | - $otherObject = $model->add_relationship_to( |
|
| 2275 | - $this, |
|
| 2276 | - $otherObjectModelObjectOrID, |
|
| 2277 | - $relationName, |
|
| 2278 | - $extra_join_model_fields_n_values |
|
| 2279 | - ); |
|
| 2280 | - // clear cache so future get_many_related and get_first_related() return new results. |
|
| 2281 | - $this->clear_cache($relationName, $otherObject, true); |
|
| 2282 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2283 | - $otherObject->clear_cache($model->get_this_model_name(), $this); |
|
| 2284 | - } |
|
| 2285 | - } else { |
|
| 2286 | - // this thing doesn't exist in the DB, so just cache it |
|
| 2287 | - if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2288 | - throw new EE_Error( |
|
| 2289 | - sprintf( |
|
| 2290 | - esc_html__( |
|
| 2291 | - 'Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s', |
|
| 2292 | - 'event_espresso' |
|
| 2293 | - ), |
|
| 2294 | - $otherObjectModelObjectOrID, |
|
| 2295 | - get_class($this) |
|
| 2296 | - ) |
|
| 2297 | - ); |
|
| 2298 | - } |
|
| 2299 | - $otherObject = $otherObjectModelObjectOrID; |
|
| 2300 | - $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 2301 | - } |
|
| 2302 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2303 | - // fix the reciprocal relation too |
|
| 2304 | - if ($otherObject->ID()) { |
|
| 2305 | - // its saved so assumed relations exist in the DB, so we can just |
|
| 2306 | - // clear the cache so future queries use the updated info in the DB |
|
| 2307 | - $otherObject->clear_cache( |
|
| 2308 | - $model->get_this_model_name(), |
|
| 2309 | - null, |
|
| 2310 | - true |
|
| 2311 | - ); |
|
| 2312 | - } else { |
|
| 2313 | - // it's not saved, so it caches relations like this |
|
| 2314 | - $otherObject->cache($model->get_this_model_name(), $this); |
|
| 2315 | - } |
|
| 2316 | - } |
|
| 2317 | - return $otherObject; |
|
| 2318 | - } |
|
| 2319 | - |
|
| 2320 | - |
|
| 2321 | - /** |
|
| 2322 | - * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2323 | - * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2324 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2325 | - * from the cache |
|
| 2326 | - * |
|
| 2327 | - * @param mixed $otherObjectModelObjectOrID |
|
| 2328 | - * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2329 | - * to the DB yet |
|
| 2330 | - * @param string $relationName |
|
| 2331 | - * @param array $where_query |
|
| 2332 | - * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2333 | - * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2334 | - * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2335 | - * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
|
| 2336 | - * deleted. |
|
| 2337 | - * @return EE_Base_Class the relation was removed from |
|
| 2338 | - * @throws ReflectionException |
|
| 2339 | - * @throws InvalidArgumentException |
|
| 2340 | - * @throws InvalidInterfaceException |
|
| 2341 | - * @throws InvalidDataTypeException |
|
| 2342 | - * @throws EE_Error |
|
| 2343 | - */ |
|
| 2344 | - public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2345 | - { |
|
| 2346 | - if ($this->ID()) { |
|
| 2347 | - // if this exists in the DB, save the relation change to the DB too |
|
| 2348 | - $otherObject = $this->get_model()->remove_relationship_to( |
|
| 2349 | - $this, |
|
| 2350 | - $otherObjectModelObjectOrID, |
|
| 2351 | - $relationName, |
|
| 2352 | - $where_query |
|
| 2353 | - ); |
|
| 2354 | - $this->clear_cache( |
|
| 2355 | - $relationName, |
|
| 2356 | - $otherObject |
|
| 2357 | - ); |
|
| 2358 | - } else { |
|
| 2359 | - // this doesn't exist in the DB, just remove it from the cache |
|
| 2360 | - $otherObject = $this->clear_cache( |
|
| 2361 | - $relationName, |
|
| 2362 | - $otherObjectModelObjectOrID |
|
| 2363 | - ); |
|
| 2364 | - } |
|
| 2365 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2366 | - $otherObject->clear_cache( |
|
| 2367 | - $this->get_model()->get_this_model_name(), |
|
| 2368 | - $this |
|
| 2369 | - ); |
|
| 2370 | - } |
|
| 2371 | - return $otherObject; |
|
| 2372 | - } |
|
| 2373 | - |
|
| 2374 | - |
|
| 2375 | - /** |
|
| 2376 | - * Removes ALL the related things for the $relationName. |
|
| 2377 | - * |
|
| 2378 | - * @param string $relationName |
|
| 2379 | - * @param array $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 2380 | - * @return EE_Base_Class |
|
| 2381 | - * @throws ReflectionException |
|
| 2382 | - * @throws InvalidArgumentException |
|
| 2383 | - * @throws InvalidInterfaceException |
|
| 2384 | - * @throws InvalidDataTypeException |
|
| 2385 | - * @throws EE_Error |
|
| 2386 | - */ |
|
| 2387 | - public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2388 | - { |
|
| 2389 | - if ($this->ID()) { |
|
| 2390 | - // if this exists in the DB, save the relation change to the DB too |
|
| 2391 | - $otherObjects = $this->get_model()->remove_relations( |
|
| 2392 | - $this, |
|
| 2393 | - $relationName, |
|
| 2394 | - $where_query_params |
|
| 2395 | - ); |
|
| 2396 | - $this->clear_cache( |
|
| 2397 | - $relationName, |
|
| 2398 | - null, |
|
| 2399 | - true |
|
| 2400 | - ); |
|
| 2401 | - } else { |
|
| 2402 | - // this doesn't exist in the DB, just remove it from the cache |
|
| 2403 | - $otherObjects = $this->clear_cache( |
|
| 2404 | - $relationName, |
|
| 2405 | - null, |
|
| 2406 | - true |
|
| 2407 | - ); |
|
| 2408 | - } |
|
| 2409 | - if (is_array($otherObjects)) { |
|
| 2410 | - foreach ($otherObjects as $otherObject) { |
|
| 2411 | - $otherObject->clear_cache( |
|
| 2412 | - $this->get_model()->get_this_model_name(), |
|
| 2413 | - $this |
|
| 2414 | - ); |
|
| 2415 | - } |
|
| 2416 | - } |
|
| 2417 | - return $otherObjects; |
|
| 2418 | - } |
|
| 2419 | - |
|
| 2420 | - |
|
| 2421 | - /** |
|
| 2422 | - * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2423 | - * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2424 | - * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2425 | - * because we want to get even deleted items etc. |
|
| 2426 | - * |
|
| 2427 | - * @param string $relationName key in the model's _model_relations array |
|
| 2428 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 2429 | - * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
|
| 2430 | - * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
|
| 2431 | - * results if you want IDs |
|
| 2432 | - * @throws ReflectionException |
|
| 2433 | - * @throws InvalidArgumentException |
|
| 2434 | - * @throws InvalidInterfaceException |
|
| 2435 | - * @throws InvalidDataTypeException |
|
| 2436 | - * @throws EE_Error |
|
| 2437 | - */ |
|
| 2438 | - public function get_many_related($relationName, $query_params = array()) |
|
| 2439 | - { |
|
| 2440 | - if ($this->ID()) { |
|
| 2441 | - // this exists in the DB, so get the related things from either the cache or the DB |
|
| 2442 | - // if there are query parameters, forget about caching the related model objects. |
|
| 2443 | - if ($query_params) { |
|
| 2444 | - $related_model_objects = $this->get_model()->get_all_related( |
|
| 2445 | - $this, |
|
| 2446 | - $relationName, |
|
| 2447 | - $query_params |
|
| 2448 | - ); |
|
| 2449 | - } else { |
|
| 2450 | - // did we already cache the result of this query? |
|
| 2451 | - $cached_results = $this->get_all_from_cache($relationName); |
|
| 2452 | - if (! $cached_results) { |
|
| 2453 | - $related_model_objects = $this->get_model()->get_all_related( |
|
| 2454 | - $this, |
|
| 2455 | - $relationName, |
|
| 2456 | - $query_params |
|
| 2457 | - ); |
|
| 2458 | - // if no query parameters were passed, then we got all the related model objects |
|
| 2459 | - // for that relation. We can cache them then. |
|
| 2460 | - foreach ($related_model_objects as $related_model_object) { |
|
| 2461 | - $this->cache($relationName, $related_model_object); |
|
| 2462 | - } |
|
| 2463 | - } else { |
|
| 2464 | - $related_model_objects = $cached_results; |
|
| 2465 | - } |
|
| 2466 | - } |
|
| 2467 | - } else { |
|
| 2468 | - // this doesn't exist in the DB, so just get the related things from the cache |
|
| 2469 | - $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2470 | - } |
|
| 2471 | - return $related_model_objects; |
|
| 2472 | - } |
|
| 2473 | - |
|
| 2474 | - |
|
| 2475 | - /** |
|
| 2476 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2477 | - * unless otherwise specified in the $query_params |
|
| 2478 | - * |
|
| 2479 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2480 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2481 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2482 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2483 | - * that by the setting $distinct to TRUE; |
|
| 2484 | - * @return int |
|
| 2485 | - * @throws ReflectionException |
|
| 2486 | - * @throws InvalidArgumentException |
|
| 2487 | - * @throws InvalidInterfaceException |
|
| 2488 | - * @throws InvalidDataTypeException |
|
| 2489 | - * @throws EE_Error |
|
| 2490 | - */ |
|
| 2491 | - public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2492 | - { |
|
| 2493 | - return $this->get_model()->count_related( |
|
| 2494 | - $this, |
|
| 2495 | - $relation_name, |
|
| 2496 | - $query_params, |
|
| 2497 | - $field_to_count, |
|
| 2498 | - $distinct |
|
| 2499 | - ); |
|
| 2500 | - } |
|
| 2501 | - |
|
| 2502 | - |
|
| 2503 | - /** |
|
| 2504 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2505 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2506 | - * |
|
| 2507 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2508 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2509 | - * @param string $field_to_sum name of field to count by. |
|
| 2510 | - * By default, uses primary key |
|
| 2511 | - * (which doesn't make much sense, so you should probably change it) |
|
| 2512 | - * @return int |
|
| 2513 | - * @throws ReflectionException |
|
| 2514 | - * @throws InvalidArgumentException |
|
| 2515 | - * @throws InvalidInterfaceException |
|
| 2516 | - * @throws InvalidDataTypeException |
|
| 2517 | - * @throws EE_Error |
|
| 2518 | - */ |
|
| 2519 | - public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2520 | - { |
|
| 2521 | - return $this->get_model()->sum_related( |
|
| 2522 | - $this, |
|
| 2523 | - $relation_name, |
|
| 2524 | - $query_params, |
|
| 2525 | - $field_to_sum |
|
| 2526 | - ); |
|
| 2527 | - } |
|
| 2528 | - |
|
| 2529 | - |
|
| 2530 | - /** |
|
| 2531 | - * Gets the first (ie, one) related model object of the specified type. |
|
| 2532 | - * |
|
| 2533 | - * @param string $relationName key in the model's _model_relations array |
|
| 2534 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2535 | - * @return EE_Base_Class (not an array, a single object) |
|
| 2536 | - * @throws ReflectionException |
|
| 2537 | - * @throws InvalidArgumentException |
|
| 2538 | - * @throws InvalidInterfaceException |
|
| 2539 | - * @throws InvalidDataTypeException |
|
| 2540 | - * @throws EE_Error |
|
| 2541 | - */ |
|
| 2542 | - public function get_first_related($relationName, $query_params = array()) |
|
| 2543 | - { |
|
| 2544 | - $model = $this->get_model(); |
|
| 2545 | - if ($this->ID()) {// this exists in the DB, get from the cache OR the DB |
|
| 2546 | - // if they've provided some query parameters, don't bother trying to cache the result |
|
| 2547 | - // also make sure we're not caching the result of get_first_related |
|
| 2548 | - // on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2549 | - if ($query_params |
|
| 2550 | - || ! $model->related_settings_for($relationName) |
|
| 2551 | - instanceof |
|
| 2552 | - EE_Belongs_To_Relation |
|
| 2553 | - ) { |
|
| 2554 | - $related_model_object = $model->get_first_related( |
|
| 2555 | - $this, |
|
| 2556 | - $relationName, |
|
| 2557 | - $query_params |
|
| 2558 | - ); |
|
| 2559 | - } else { |
|
| 2560 | - // first, check if we've already cached the result of this query |
|
| 2561 | - $cached_result = $this->get_one_from_cache($relationName); |
|
| 2562 | - if (! $cached_result) { |
|
| 2563 | - $related_model_object = $model->get_first_related( |
|
| 2564 | - $this, |
|
| 2565 | - $relationName, |
|
| 2566 | - $query_params |
|
| 2567 | - ); |
|
| 2568 | - $this->cache($relationName, $related_model_object); |
|
| 2569 | - } else { |
|
| 2570 | - $related_model_object = $cached_result; |
|
| 2571 | - } |
|
| 2572 | - } |
|
| 2573 | - } else { |
|
| 2574 | - $related_model_object = null; |
|
| 2575 | - // this doesn't exist in the Db, |
|
| 2576 | - // but maybe the relation is of type belongs to, and so the related thing might |
|
| 2577 | - if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2578 | - $related_model_object = $model->get_first_related( |
|
| 2579 | - $this, |
|
| 2580 | - $relationName, |
|
| 2581 | - $query_params |
|
| 2582 | - ); |
|
| 2583 | - } |
|
| 2584 | - // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
|
| 2585 | - // just get what's cached on this object |
|
| 2586 | - if (! $related_model_object) { |
|
| 2587 | - $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2588 | - } |
|
| 2589 | - } |
|
| 2590 | - return $related_model_object; |
|
| 2591 | - } |
|
| 2592 | - |
|
| 2593 | - |
|
| 2594 | - /** |
|
| 2595 | - * Does a delete on all related objects of type $relationName and removes |
|
| 2596 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2597 | - * of blocking related model objects) does nothing. If the related model objects are |
|
| 2598 | - * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2599 | - * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2600 | - * |
|
| 2601 | - * @param string $relationName |
|
| 2602 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2603 | - * @return int how many deleted |
|
| 2604 | - * @throws ReflectionException |
|
| 2605 | - * @throws InvalidArgumentException |
|
| 2606 | - * @throws InvalidInterfaceException |
|
| 2607 | - * @throws InvalidDataTypeException |
|
| 2608 | - * @throws EE_Error |
|
| 2609 | - */ |
|
| 2610 | - public function delete_related($relationName, $query_params = array()) |
|
| 2611 | - { |
|
| 2612 | - if ($this->ID()) { |
|
| 2613 | - $count = $this->get_model()->delete_related( |
|
| 2614 | - $this, |
|
| 2615 | - $relationName, |
|
| 2616 | - $query_params |
|
| 2617 | - ); |
|
| 2618 | - } else { |
|
| 2619 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2620 | - $this->clear_cache($relationName, null, true); |
|
| 2621 | - } |
|
| 2622 | - return $count; |
|
| 2623 | - } |
|
| 2624 | - |
|
| 2625 | - |
|
| 2626 | - /** |
|
| 2627 | - * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2628 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2629 | - * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2630 | - * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2631 | - * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2632 | - * |
|
| 2633 | - * @param string $relationName |
|
| 2634 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2635 | - * @return int how many deleted (including those soft deleted) |
|
| 2636 | - * @throws ReflectionException |
|
| 2637 | - * @throws InvalidArgumentException |
|
| 2638 | - * @throws InvalidInterfaceException |
|
| 2639 | - * @throws InvalidDataTypeException |
|
| 2640 | - * @throws EE_Error |
|
| 2641 | - */ |
|
| 2642 | - public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2643 | - { |
|
| 2644 | - if ($this->ID()) { |
|
| 2645 | - $count = $this->get_model()->delete_related_permanently( |
|
| 2646 | - $this, |
|
| 2647 | - $relationName, |
|
| 2648 | - $query_params |
|
| 2649 | - ); |
|
| 2650 | - } else { |
|
| 2651 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2652 | - } |
|
| 2653 | - $this->clear_cache($relationName, null, true); |
|
| 2654 | - return $count; |
|
| 2655 | - } |
|
| 2656 | - |
|
| 2657 | - |
|
| 2658 | - /** |
|
| 2659 | - * is_set |
|
| 2660 | - * Just a simple utility function children can use for checking if property exists |
|
| 2661 | - * |
|
| 2662 | - * @access public |
|
| 2663 | - * @param string $field_name property to check |
|
| 2664 | - * @return bool TRUE if existing,FALSE if not. |
|
| 2665 | - */ |
|
| 2666 | - public function is_set($field_name) |
|
| 2667 | - { |
|
| 2668 | - return isset($this->_fields[ $field_name ]); |
|
| 2669 | - } |
|
| 2670 | - |
|
| 2671 | - |
|
| 2672 | - /** |
|
| 2673 | - * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2674 | - * EE_Error exception if they don't |
|
| 2675 | - * |
|
| 2676 | - * @param mixed (string|array) $properties properties to check |
|
| 2677 | - * @throws EE_Error |
|
| 2678 | - * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2679 | - */ |
|
| 2680 | - protected function _property_exists($properties) |
|
| 2681 | - { |
|
| 2682 | - foreach ((array) $properties as $property_name) { |
|
| 2683 | - // first make sure this property exists |
|
| 2684 | - if (! $this->_fields[ $property_name ]) { |
|
| 2685 | - throw new EE_Error( |
|
| 2686 | - sprintf( |
|
| 2687 | - esc_html__( |
|
| 2688 | - 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2689 | - 'event_espresso' |
|
| 2690 | - ), |
|
| 2691 | - $property_name |
|
| 2692 | - ) |
|
| 2693 | - ); |
|
| 2694 | - } |
|
| 2695 | - } |
|
| 2696 | - return true; |
|
| 2697 | - } |
|
| 2698 | - |
|
| 2699 | - |
|
| 2700 | - /** |
|
| 2701 | - * This simply returns an array of model fields for this object |
|
| 2702 | - * |
|
| 2703 | - * @return array |
|
| 2704 | - * @throws ReflectionException |
|
| 2705 | - * @throws InvalidArgumentException |
|
| 2706 | - * @throws InvalidInterfaceException |
|
| 2707 | - * @throws InvalidDataTypeException |
|
| 2708 | - * @throws EE_Error |
|
| 2709 | - */ |
|
| 2710 | - public function model_field_array() |
|
| 2711 | - { |
|
| 2712 | - $fields = $this->get_model()->field_settings(false); |
|
| 2713 | - $properties = array(); |
|
| 2714 | - // remove prepended underscore |
|
| 2715 | - foreach ($fields as $field_name => $settings) { |
|
| 2716 | - $properties[ $field_name ] = $this->get($field_name); |
|
| 2717 | - } |
|
| 2718 | - return $properties; |
|
| 2719 | - } |
|
| 2720 | - |
|
| 2721 | - |
|
| 2722 | - /** |
|
| 2723 | - * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2724 | - * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2725 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
|
| 2726 | - * Instead of requiring a plugin to extend the EE_Base_Class |
|
| 2727 | - * (which works fine is there's only 1 plugin, but when will that happen?) |
|
| 2728 | - * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
|
| 2729 | - * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
|
| 2730 | - * and accepts 2 arguments: the object on which the function was called, |
|
| 2731 | - * and an array of the original arguments passed to the function. |
|
| 2732 | - * Whatever their callback function returns will be returned by this function. |
|
| 2733 | - * Example: in functions.php (or in a plugin): |
|
| 2734 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
|
| 2735 | - * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2736 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2737 | - * return $previousReturnValue.$returnString; |
|
| 2738 | - * } |
|
| 2739 | - * require('EE_Answer.class.php'); |
|
| 2740 | - * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2741 | - * echo $answer->my_callback('monkeys',100); |
|
| 2742 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2743 | - * |
|
| 2744 | - * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2745 | - * @param array $args array of original arguments passed to the function |
|
| 2746 | - * @throws EE_Error |
|
| 2747 | - * @return mixed whatever the plugin which calls add_filter decides |
|
| 2748 | - */ |
|
| 2749 | - public function __call($methodName, $args) |
|
| 2750 | - { |
|
| 2751 | - $className = get_class($this); |
|
| 2752 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2753 | - if (! has_filter($tagName)) { |
|
| 2754 | - throw new EE_Error( |
|
| 2755 | - sprintf( |
|
| 2756 | - esc_html__( |
|
| 2757 | - "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}", |
|
| 2758 | - 'event_espresso' |
|
| 2759 | - ), |
|
| 2760 | - $methodName, |
|
| 2761 | - $className, |
|
| 2762 | - $tagName |
|
| 2763 | - ) |
|
| 2764 | - ); |
|
| 2765 | - } |
|
| 2766 | - return apply_filters($tagName, null, $this, $args); |
|
| 2767 | - } |
|
| 2768 | - |
|
| 2769 | - |
|
| 2770 | - /** |
|
| 2771 | - * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2772 | - * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2773 | - * |
|
| 2774 | - * @param string $meta_key |
|
| 2775 | - * @param mixed $meta_value |
|
| 2776 | - * @param mixed $previous_value |
|
| 2777 | - * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2778 | - * NOTE: if the values haven't changed, returns 0 |
|
| 2779 | - * @throws InvalidArgumentException |
|
| 2780 | - * @throws InvalidInterfaceException |
|
| 2781 | - * @throws InvalidDataTypeException |
|
| 2782 | - * @throws EE_Error |
|
| 2783 | - * @throws ReflectionException |
|
| 2784 | - */ |
|
| 2785 | - public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2786 | - { |
|
| 2787 | - $query_params = array( |
|
| 2788 | - array( |
|
| 2789 | - 'EXM_key' => $meta_key, |
|
| 2790 | - 'OBJ_ID' => $this->ID(), |
|
| 2791 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2792 | - ), |
|
| 2793 | - ); |
|
| 2794 | - if ($previous_value !== null) { |
|
| 2795 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2796 | - } |
|
| 2797 | - $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2798 | - if (! $existing_rows_like_that) { |
|
| 2799 | - return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2800 | - } |
|
| 2801 | - foreach ($existing_rows_like_that as $existing_row) { |
|
| 2802 | - $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2803 | - } |
|
| 2804 | - return count($existing_rows_like_that); |
|
| 2805 | - } |
|
| 2806 | - |
|
| 2807 | - |
|
| 2808 | - /** |
|
| 2809 | - * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2810 | - * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2811 | - * extra meta row was entered, false if not |
|
| 2812 | - * |
|
| 2813 | - * @param string $meta_key |
|
| 2814 | - * @param mixed $meta_value |
|
| 2815 | - * @param boolean $unique |
|
| 2816 | - * @return boolean |
|
| 2817 | - * @throws InvalidArgumentException |
|
| 2818 | - * @throws InvalidInterfaceException |
|
| 2819 | - * @throws InvalidDataTypeException |
|
| 2820 | - * @throws EE_Error |
|
| 2821 | - * @throws ReflectionException |
|
| 2822 | - * @throws ReflectionException |
|
| 2823 | - */ |
|
| 2824 | - public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2825 | - { |
|
| 2826 | - if ($unique) { |
|
| 2827 | - $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2828 | - array( |
|
| 2829 | - array( |
|
| 2830 | - 'EXM_key' => $meta_key, |
|
| 2831 | - 'OBJ_ID' => $this->ID(), |
|
| 2832 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2833 | - ), |
|
| 2834 | - ) |
|
| 2835 | - ); |
|
| 2836 | - if ($existing_extra_meta) { |
|
| 2837 | - return false; |
|
| 2838 | - } |
|
| 2839 | - } |
|
| 2840 | - $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2841 | - array( |
|
| 2842 | - 'EXM_key' => $meta_key, |
|
| 2843 | - 'EXM_value' => $meta_value, |
|
| 2844 | - 'OBJ_ID' => $this->ID(), |
|
| 2845 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2846 | - ) |
|
| 2847 | - ); |
|
| 2848 | - $new_extra_meta->save(); |
|
| 2849 | - return true; |
|
| 2850 | - } |
|
| 2851 | - |
|
| 2852 | - |
|
| 2853 | - /** |
|
| 2854 | - * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2855 | - * is specified, only deletes extra meta records with that value. |
|
| 2856 | - * |
|
| 2857 | - * @param string $meta_key |
|
| 2858 | - * @param mixed $meta_value |
|
| 2859 | - * @return int number of extra meta rows deleted |
|
| 2860 | - * @throws InvalidArgumentException |
|
| 2861 | - * @throws InvalidInterfaceException |
|
| 2862 | - * @throws InvalidDataTypeException |
|
| 2863 | - * @throws EE_Error |
|
| 2864 | - * @throws ReflectionException |
|
| 2865 | - */ |
|
| 2866 | - public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2867 | - { |
|
| 2868 | - $query_params = array( |
|
| 2869 | - array( |
|
| 2870 | - 'EXM_key' => $meta_key, |
|
| 2871 | - 'OBJ_ID' => $this->ID(), |
|
| 2872 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2873 | - ), |
|
| 2874 | - ); |
|
| 2875 | - if ($meta_value !== null) { |
|
| 2876 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2877 | - } |
|
| 2878 | - return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2879 | - } |
|
| 2880 | - |
|
| 2881 | - |
|
| 2882 | - /** |
|
| 2883 | - * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2884 | - * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2885 | - * You can specify $default is case you haven't found the extra meta |
|
| 2886 | - * |
|
| 2887 | - * @param string $meta_key |
|
| 2888 | - * @param boolean $single |
|
| 2889 | - * @param mixed $default if we don't find anything, what should we return? |
|
| 2890 | - * @return mixed single value if $single; array if ! $single |
|
| 2891 | - * @throws ReflectionException |
|
| 2892 | - * @throws InvalidArgumentException |
|
| 2893 | - * @throws InvalidInterfaceException |
|
| 2894 | - * @throws InvalidDataTypeException |
|
| 2895 | - * @throws EE_Error |
|
| 2896 | - */ |
|
| 2897 | - public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2898 | - { |
|
| 2899 | - if ($single) { |
|
| 2900 | - $result = $this->get_first_related( |
|
| 2901 | - 'Extra_Meta', |
|
| 2902 | - array(array('EXM_key' => $meta_key)) |
|
| 2903 | - ); |
|
| 2904 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2905 | - return $result->value(); |
|
| 2906 | - } |
|
| 2907 | - } else { |
|
| 2908 | - $results = $this->get_many_related( |
|
| 2909 | - 'Extra_Meta', |
|
| 2910 | - array(array('EXM_key' => $meta_key)) |
|
| 2911 | - ); |
|
| 2912 | - if ($results) { |
|
| 2913 | - $values = array(); |
|
| 2914 | - foreach ($results as $result) { |
|
| 2915 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2916 | - $values[ $result->ID() ] = $result->value(); |
|
| 2917 | - } |
|
| 2918 | - } |
|
| 2919 | - return $values; |
|
| 2920 | - } |
|
| 2921 | - } |
|
| 2922 | - // if nothing discovered yet return default. |
|
| 2923 | - return apply_filters( |
|
| 2924 | - 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
| 2925 | - $default, |
|
| 2926 | - $meta_key, |
|
| 2927 | - $single, |
|
| 2928 | - $this |
|
| 2929 | - ); |
|
| 2930 | - } |
|
| 2931 | - |
|
| 2932 | - |
|
| 2933 | - /** |
|
| 2934 | - * Returns a simple array of all the extra meta associated with this model object. |
|
| 2935 | - * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2936 | - * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2937 | - * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2938 | - * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2939 | - * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2940 | - * finally the extra meta's value as each sub-value. (eg |
|
| 2941 | - * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2942 | - * |
|
| 2943 | - * @param boolean $one_of_each_key |
|
| 2944 | - * @return array |
|
| 2945 | - * @throws ReflectionException |
|
| 2946 | - * @throws InvalidArgumentException |
|
| 2947 | - * @throws InvalidInterfaceException |
|
| 2948 | - * @throws InvalidDataTypeException |
|
| 2949 | - * @throws EE_Error |
|
| 2950 | - */ |
|
| 2951 | - public function all_extra_meta_array($one_of_each_key = true) |
|
| 2952 | - { |
|
| 2953 | - $return_array = array(); |
|
| 2954 | - if ($one_of_each_key) { |
|
| 2955 | - $extra_meta_objs = $this->get_many_related( |
|
| 2956 | - 'Extra_Meta', |
|
| 2957 | - array('group_by' => 'EXM_key') |
|
| 2958 | - ); |
|
| 2959 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2960 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2961 | - $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
| 2962 | - } |
|
| 2963 | - } |
|
| 2964 | - } else { |
|
| 2965 | - $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2966 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2967 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2968 | - if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
| 2969 | - $return_array[ $extra_meta_obj->key() ] = array(); |
|
| 2970 | - } |
|
| 2971 | - $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
| 2972 | - } |
|
| 2973 | - } |
|
| 2974 | - } |
|
| 2975 | - return $return_array; |
|
| 2976 | - } |
|
| 2977 | - |
|
| 2978 | - |
|
| 2979 | - /** |
|
| 2980 | - * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2981 | - * |
|
| 2982 | - * @return string |
|
| 2983 | - * @throws ReflectionException |
|
| 2984 | - * @throws InvalidArgumentException |
|
| 2985 | - * @throws InvalidInterfaceException |
|
| 2986 | - * @throws InvalidDataTypeException |
|
| 2987 | - * @throws EE_Error |
|
| 2988 | - */ |
|
| 2989 | - public function name() |
|
| 2990 | - { |
|
| 2991 | - // find a field that's not a text field |
|
| 2992 | - $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2993 | - if ($field_we_can_use) { |
|
| 2994 | - return $this->get($field_we_can_use->get_name()); |
|
| 2995 | - } |
|
| 2996 | - $first_few_properties = $this->model_field_array(); |
|
| 2997 | - $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2998 | - $name_parts = array(); |
|
| 2999 | - foreach ($first_few_properties as $name => $value) { |
|
| 3000 | - $name_parts[] = "$name:$value"; |
|
| 3001 | - } |
|
| 3002 | - return implode(',', $name_parts); |
|
| 3003 | - } |
|
| 3004 | - |
|
| 3005 | - |
|
| 3006 | - /** |
|
| 3007 | - * in_entity_map |
|
| 3008 | - * Checks if this model object has been proven to already be in the entity map |
|
| 3009 | - * |
|
| 3010 | - * @return boolean |
|
| 3011 | - * @throws ReflectionException |
|
| 3012 | - * @throws InvalidArgumentException |
|
| 3013 | - * @throws InvalidInterfaceException |
|
| 3014 | - * @throws InvalidDataTypeException |
|
| 3015 | - * @throws EE_Error |
|
| 3016 | - */ |
|
| 3017 | - public function in_entity_map() |
|
| 3018 | - { |
|
| 3019 | - // well, if we looked, did we find it in the entity map? |
|
| 3020 | - return $this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this; |
|
| 3021 | - } |
|
| 3022 | - |
|
| 3023 | - |
|
| 3024 | - /** |
|
| 3025 | - * refresh_from_db |
|
| 3026 | - * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 3027 | - * |
|
| 3028 | - * @throws ReflectionException |
|
| 3029 | - * @throws InvalidArgumentException |
|
| 3030 | - * @throws InvalidInterfaceException |
|
| 3031 | - * @throws InvalidDataTypeException |
|
| 3032 | - * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 3033 | - * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 3034 | - */ |
|
| 3035 | - public function refresh_from_db() |
|
| 3036 | - { |
|
| 3037 | - if ($this->ID() && $this->in_entity_map()) { |
|
| 3038 | - $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 3039 | - } else { |
|
| 3040 | - // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 3041 | - // if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 3042 | - // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 3043 | - // absolutely nothing in it for this ID |
|
| 3044 | - if (WP_DEBUG) { |
|
| 3045 | - throw new EE_Error( |
|
| 3046 | - sprintf( |
|
| 3047 | - esc_html__( |
|
| 3048 | - 'Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
|
| 3049 | - 'event_espresso' |
|
| 3050 | - ), |
|
| 3051 | - $this->ID(), |
|
| 3052 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 3053 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 3054 | - ) |
|
| 3055 | - ); |
|
| 3056 | - } |
|
| 3057 | - } |
|
| 3058 | - } |
|
| 3059 | - |
|
| 3060 | - |
|
| 3061 | - /** |
|
| 3062 | - * Change $fields' values to $new_value_sql (which is a string of raw SQL) |
|
| 3063 | - * |
|
| 3064 | - * @since 4.9.80.p |
|
| 3065 | - * @param EE_Model_Field_Base[] $fields |
|
| 3066 | - * @param string $new_value_sql |
|
| 3067 | - * example: 'column_name=123', |
|
| 3068 | - * or 'column_name=column_name+1', |
|
| 3069 | - * or 'column_name= CASE |
|
| 3070 | - * WHEN (`column_name` + `other_column` + 5) <= `yet_another_column` |
|
| 3071 | - * THEN `column_name` + 5 |
|
| 3072 | - * ELSE `column_name` |
|
| 3073 | - * END' |
|
| 3074 | - * Also updates $field on this model object with the latest value from the database. |
|
| 3075 | - * @return bool |
|
| 3076 | - * @throws EE_Error |
|
| 3077 | - * @throws InvalidArgumentException |
|
| 3078 | - * @throws InvalidDataTypeException |
|
| 3079 | - * @throws InvalidInterfaceException |
|
| 3080 | - * @throws ReflectionException |
|
| 3081 | - */ |
|
| 3082 | - protected function updateFieldsInDB($fields, $new_value_sql) |
|
| 3083 | - { |
|
| 3084 | - // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
|
| 3085 | - // if it wasn't even there to start off. |
|
| 3086 | - if (! $this->ID()) { |
|
| 3087 | - $this->save(); |
|
| 3088 | - } |
|
| 3089 | - global $wpdb; |
|
| 3090 | - if (empty($fields)) { |
|
| 3091 | - throw new InvalidArgumentException( |
|
| 3092 | - esc_html__( |
|
| 3093 | - 'EE_Base_Class::updateFieldsInDB was passed an empty array of fields.', |
|
| 3094 | - 'event_espresso' |
|
| 3095 | - ) |
|
| 3096 | - ); |
|
| 3097 | - } |
|
| 3098 | - $first_field = reset($fields); |
|
| 3099 | - $table_alias = $first_field->get_table_alias(); |
|
| 3100 | - foreach ($fields as $field) { |
|
| 3101 | - if ($table_alias !== $field->get_table_alias()) { |
|
| 3102 | - throw new InvalidArgumentException( |
|
| 3103 | - sprintf( |
|
| 3104 | - esc_html__( |
|
| 3105 | - // @codingStandardsIgnoreStart |
|
| 3106 | - 'EE_Base_Class::updateFieldsInDB was passed fields for different tables ("%1$s" and "%2$s"), which is not supported. Instead, please call the method multiple times.', |
|
| 3107 | - // @codingStandardsIgnoreEnd |
|
| 3108 | - 'event_espresso' |
|
| 3109 | - ), |
|
| 3110 | - $table_alias, |
|
| 3111 | - $field->get_table_alias() |
|
| 3112 | - ) |
|
| 3113 | - ); |
|
| 3114 | - } |
|
| 3115 | - } |
|
| 3116 | - // Ok the fields are now known to all be for the same table. Proceed with creating the SQL to update it. |
|
| 3117 | - $table_obj = $this->get_model()->get_table_obj_by_alias($table_alias); |
|
| 3118 | - $table_pk_value = $this->ID(); |
|
| 3119 | - $table_name = $table_obj->get_table_name(); |
|
| 3120 | - if ($table_obj instanceof EE_Secondary_Table) { |
|
| 3121 | - $table_pk_field_name = $table_obj->get_fk_on_table(); |
|
| 3122 | - } else { |
|
| 3123 | - $table_pk_field_name = $table_obj->get_pk_column(); |
|
| 3124 | - } |
|
| 3125 | - |
|
| 3126 | - $query = |
|
| 3127 | - "UPDATE `{$table_name}` |
|
| 16 | + /** |
|
| 17 | + * This is an array of the original properties and values provided during construction |
|
| 18 | + * of this model object. (keys are model field names, values are their values). |
|
| 19 | + * This list is important to remember so that when we are merging data from the db, we know |
|
| 20 | + * which values to override and which to not override. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $_props_n_values_provided_in_constructor; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Timezone |
|
| 28 | + * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
| 29 | + * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
| 30 | + * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
| 31 | + * access to it. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + protected $_timezone; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * date format |
|
| 39 | + * pattern or format for displaying dates |
|
| 40 | + * |
|
| 41 | + * @var string $_dt_frmt |
|
| 42 | + */ |
|
| 43 | + protected $_dt_frmt; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * time format |
|
| 47 | + * pattern or format for displaying time |
|
| 48 | + * |
|
| 49 | + * @var string $_tm_frmt |
|
| 50 | + */ |
|
| 51 | + protected $_tm_frmt; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * This property is for holding a cached array of object properties indexed by property name as the key. |
|
| 55 | + * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
| 56 | + * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
| 57 | + * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
| 58 | + * |
|
| 59 | + * @var array |
|
| 60 | + */ |
|
| 61 | + protected $_cached_properties = array(); |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
| 65 | + * single |
|
| 66 | + * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
| 67 | + * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
| 68 | + * all others have an array) |
|
| 69 | + * |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + protected $_model_relations = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
| 76 | + * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
| 77 | + * |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + protected $_fields = array(); |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @var boolean indicating whether or not this model object is intended to ever be saved |
|
| 84 | + * For example, we might create model objects intended to only be used for the duration |
|
| 85 | + * of this request and to be thrown away, and if they were accidentally saved |
|
| 86 | + * it would be a bug. |
|
| 87 | + */ |
|
| 88 | + protected $_allow_persist = true; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @var boolean indicating whether or not this model object's properties have changed since construction |
|
| 92 | + */ |
|
| 93 | + protected $_has_changes = false; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @var EEM_Base |
|
| 97 | + */ |
|
| 98 | + protected $_model; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose |
|
| 102 | + * for these values is for retrieval of the results, they are not further queryable and they are not persisted to |
|
| 103 | + * the db. They also do not automatically update if there are any changes to the data that produced their results. |
|
| 104 | + * The format is a simple array of field_alias => field_value. So for instance if a custom select was something |
|
| 105 | + * like, "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this |
|
| 106 | + * array as: |
|
| 107 | + * array( |
|
| 108 | + * 'Registration_Count' => 24 |
|
| 109 | + * ); |
|
| 110 | + * Note: if the custom select configuration for the query included a data type, the value will be in the data type |
|
| 111 | + * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more |
|
| 112 | + * info) |
|
| 113 | + * |
|
| 114 | + * @var array |
|
| 115 | + */ |
|
| 116 | + protected $custom_selection_results = array(); |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
| 121 | + * play nice |
|
| 122 | + * |
|
| 123 | + * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
| 124 | + * layer of the model's _fields array, (eg, EVT_ID, |
|
| 125 | + * TXN_amount, QST_name, etc) and values are their values |
|
| 126 | + * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
| 127 | + * corresponding db model or not. |
|
| 128 | + * @param string $timezone indicate what timezone you want any datetime fields to |
|
| 129 | + * be in when instantiating a EE_Base_Class object. |
|
| 130 | + * @param array $date_formats An array of date formats to set on construct where first |
|
| 131 | + * value is the date_format and second value is the time |
|
| 132 | + * format. |
|
| 133 | + * @throws InvalidArgumentException |
|
| 134 | + * @throws InvalidInterfaceException |
|
| 135 | + * @throws InvalidDataTypeException |
|
| 136 | + * @throws EE_Error |
|
| 137 | + * @throws ReflectionException |
|
| 138 | + */ |
|
| 139 | + protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
| 140 | + { |
|
| 141 | + $className = get_class($this); |
|
| 142 | + do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
| 143 | + $model = $this->get_model(); |
|
| 144 | + $model_fields = $model->field_settings(false); |
|
| 145 | + // ensure $fieldValues is an array |
|
| 146 | + $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
| 147 | + // verify client code has not passed any invalid field names |
|
| 148 | + foreach ($fieldValues as $field_name => $field_value) { |
|
| 149 | + if (! isset($model_fields[ $field_name ])) { |
|
| 150 | + throw new EE_Error( |
|
| 151 | + sprintf( |
|
| 152 | + esc_html__( |
|
| 153 | + 'Invalid field (%s) passed to constructor of %s. Allowed fields are :%s', |
|
| 154 | + 'event_espresso' |
|
| 155 | + ), |
|
| 156 | + $field_name, |
|
| 157 | + get_class($this), |
|
| 158 | + implode(', ', array_keys($model_fields)) |
|
| 159 | + ) |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 164 | + if (! empty($date_formats) && is_array($date_formats)) { |
|
| 165 | + list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
| 166 | + } else { |
|
| 167 | + // set default formats for date and time |
|
| 168 | + $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d'); |
|
| 169 | + $this->_tm_frmt = (string) get_option('time_format', 'g:i a'); |
|
| 170 | + } |
|
| 171 | + // if db model is instantiating |
|
| 172 | + if ($bydb) { |
|
| 173 | + // client code has indicated these field values are from the database |
|
| 174 | + foreach ($model_fields as $fieldName => $field) { |
|
| 175 | + $this->set_from_db( |
|
| 176 | + $fieldName, |
|
| 177 | + isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null |
|
| 178 | + ); |
|
| 179 | + } |
|
| 180 | + } else { |
|
| 181 | + // we're constructing a brand |
|
| 182 | + // new instance of the model object. Generally, this means we'll need to do more field validation |
|
| 183 | + foreach ($model_fields as $fieldName => $field) { |
|
| 184 | + $this->set( |
|
| 185 | + $fieldName, |
|
| 186 | + isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null, |
|
| 187 | + true |
|
| 188 | + ); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + // remember what values were passed to this constructor |
|
| 192 | + $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
| 193 | + // remember in entity mapper |
|
| 194 | + if (! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 195 | + $model->add_to_entity_map($this); |
|
| 196 | + } |
|
| 197 | + // setup all the relations |
|
| 198 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 199 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 200 | + $this->_model_relations[ $relation_name ] = null; |
|
| 201 | + } else { |
|
| 202 | + $this->_model_relations[ $relation_name ] = array(); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + /** |
|
| 206 | + * Action done at the end of each model object construction |
|
| 207 | + * |
|
| 208 | + * @param EE_Base_Class $this the model object just created |
|
| 209 | + */ |
|
| 210 | + do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
| 216 | + * |
|
| 217 | + * @return boolean |
|
| 218 | + */ |
|
| 219 | + public function allow_persist() |
|
| 220 | + { |
|
| 221 | + return $this->_allow_persist; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Sets whether or not this model object should be allowed to be saved to the DB. |
|
| 227 | + * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
| 228 | + * you got new information that somehow made you change your mind. |
|
| 229 | + * |
|
| 230 | + * @param boolean $allow_persist |
|
| 231 | + * @return boolean |
|
| 232 | + */ |
|
| 233 | + public function set_allow_persist($allow_persist) |
|
| 234 | + { |
|
| 235 | + return $this->_allow_persist = $allow_persist; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Gets the field's original value when this object was constructed during this request. |
|
| 241 | + * This can be helpful when determining if a model object has changed or not |
|
| 242 | + * |
|
| 243 | + * @param string $field_name |
|
| 244 | + * @return mixed|null |
|
| 245 | + * @throws ReflectionException |
|
| 246 | + * @throws InvalidArgumentException |
|
| 247 | + * @throws InvalidInterfaceException |
|
| 248 | + * @throws InvalidDataTypeException |
|
| 249 | + * @throws EE_Error |
|
| 250 | + */ |
|
| 251 | + public function get_original($field_name) |
|
| 252 | + { |
|
| 253 | + if (isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
| 254 | + && $field_settings = $this->get_model()->field_settings_for($field_name) |
|
| 255 | + ) { |
|
| 256 | + return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
| 257 | + } |
|
| 258 | + return null; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * @param EE_Base_Class $obj |
|
| 264 | + * @return string |
|
| 265 | + */ |
|
| 266 | + public function get_class($obj) |
|
| 267 | + { |
|
| 268 | + return get_class($obj); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Overrides parent because parent expects old models. |
|
| 274 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
| 275 | + * |
|
| 276 | + * @param string $field_name |
|
| 277 | + * @param mixed $field_value |
|
| 278 | + * @param bool $use_default |
|
| 279 | + * @throws InvalidArgumentException |
|
| 280 | + * @throws InvalidInterfaceException |
|
| 281 | + * @throws InvalidDataTypeException |
|
| 282 | + * @throws EE_Error |
|
| 283 | + * @throws ReflectionException |
|
| 284 | + * @throws ReflectionException |
|
| 285 | + * @throws ReflectionException |
|
| 286 | + */ |
|
| 287 | + public function set($field_name, $field_value, $use_default = false) |
|
| 288 | + { |
|
| 289 | + // if not using default and nothing has changed, and object has already been setup (has ID), |
|
| 290 | + // then don't do anything |
|
| 291 | + if (! $use_default |
|
| 292 | + && $this->_fields[ $field_name ] === $field_value |
|
| 293 | + && $this->ID() |
|
| 294 | + ) { |
|
| 295 | + return; |
|
| 296 | + } |
|
| 297 | + $model = $this->get_model(); |
|
| 298 | + $this->_has_changes = true; |
|
| 299 | + $field_obj = $model->field_settings_for($field_name); |
|
| 300 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 301 | + // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
| 302 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 303 | + $field_obj->set_timezone($this->_timezone); |
|
| 304 | + $field_obj->set_date_format($this->_dt_frmt); |
|
| 305 | + $field_obj->set_time_format($this->_tm_frmt); |
|
| 306 | + } |
|
| 307 | + $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
| 308 | + // should the value be null? |
|
| 309 | + if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
| 310 | + $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
| 311 | + /** |
|
| 312 | + * To save having to refactor all the models, if a default value is used for a |
|
| 313 | + * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
| 314 | + * object. Then let's do a set again to ensure that it becomes a DateTime |
|
| 315 | + * object. |
|
| 316 | + * |
|
| 317 | + * @since 4.6.10+ |
|
| 318 | + */ |
|
| 319 | + if ($field_obj instanceof EE_Datetime_Field |
|
| 320 | + && $this->_fields[ $field_name ] !== null |
|
| 321 | + && ! $this->_fields[ $field_name ] instanceof DateTime |
|
| 322 | + ) { |
|
| 323 | + empty($this->_fields[ $field_name ]) |
|
| 324 | + ? $this->set($field_name, time()) |
|
| 325 | + : $this->set($field_name, $this->_fields[ $field_name ]); |
|
| 326 | + } |
|
| 327 | + } else { |
|
| 328 | + $this->_fields[ $field_name ] = $holder_of_value; |
|
| 329 | + } |
|
| 330 | + // if we're not in the constructor... |
|
| 331 | + // now check if what we set was a primary key |
|
| 332 | + if (// note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
| 333 | + $this->_props_n_values_provided_in_constructor |
|
| 334 | + && $field_value |
|
| 335 | + && $field_name === $model->primary_key_name() |
|
| 336 | + ) { |
|
| 337 | + // if so, we want all this object's fields to be filled either with |
|
| 338 | + // what we've explicitly set on this model |
|
| 339 | + // or what we have in the db |
|
| 340 | + // echo "setting primary key!"; |
|
| 341 | + $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
| 342 | + $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
| 343 | + foreach ($fields_on_model as $field_obj) { |
|
| 344 | + if (! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 345 | + && $field_obj->get_name() !== $field_name |
|
| 346 | + ) { |
|
| 347 | + $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + // oh this model object has an ID? well make sure its in the entity mapper |
|
| 351 | + $model->add_to_entity_map($this); |
|
| 352 | + } |
|
| 353 | + // let's unset any cache for this field_name from the $_cached_properties property. |
|
| 354 | + $this->_clear_cached_property($field_name); |
|
| 355 | + } else { |
|
| 356 | + throw new EE_Error( |
|
| 357 | + sprintf( |
|
| 358 | + esc_html__( |
|
| 359 | + 'A valid EE_Model_Field_Base could not be found for the given field name: %s', |
|
| 360 | + 'event_espresso' |
|
| 361 | + ), |
|
| 362 | + $field_name |
|
| 363 | + ) |
|
| 364 | + ); |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Set custom select values for model. |
|
| 371 | + * |
|
| 372 | + * @param array $custom_select_values |
|
| 373 | + */ |
|
| 374 | + public function setCustomSelectsValues(array $custom_select_values) |
|
| 375 | + { |
|
| 376 | + $this->custom_selection_results = $custom_select_values; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Returns the custom select value for the provided alias if its set. |
|
| 382 | + * If not set, returns null. |
|
| 383 | + * |
|
| 384 | + * @param string $alias |
|
| 385 | + * @return string|int|float|null |
|
| 386 | + */ |
|
| 387 | + public function getCustomSelect($alias) |
|
| 388 | + { |
|
| 389 | + return isset($this->custom_selection_results[ $alias ]) |
|
| 390 | + ? $this->custom_selection_results[ $alias ] |
|
| 391 | + : null; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * This sets the field value on the db column if it exists for the given $column_name or |
|
| 397 | + * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
| 398 | + * |
|
| 399 | + * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
| 400 | + * @param string $field_name Must be the exact column name. |
|
| 401 | + * @param mixed $field_value The value to set. |
|
| 402 | + * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
| 403 | + * @throws InvalidArgumentException |
|
| 404 | + * @throws InvalidInterfaceException |
|
| 405 | + * @throws InvalidDataTypeException |
|
| 406 | + * @throws EE_Error |
|
| 407 | + * @throws ReflectionException |
|
| 408 | + */ |
|
| 409 | + public function set_field_or_extra_meta($field_name, $field_value) |
|
| 410 | + { |
|
| 411 | + if ($this->get_model()->has_field($field_name)) { |
|
| 412 | + $this->set($field_name, $field_value); |
|
| 413 | + return true; |
|
| 414 | + } |
|
| 415 | + // ensure this object is saved first so that extra meta can be properly related. |
|
| 416 | + $this->save(); |
|
| 417 | + return $this->update_extra_meta($field_name, $field_value); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * This retrieves the value of the db column set on this class or if that's not present |
|
| 423 | + * it will attempt to retrieve from extra_meta if found. |
|
| 424 | + * Example Usage: |
|
| 425 | + * Via EE_Message child class: |
|
| 426 | + * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
| 427 | + * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
| 428 | + * also have additional main fields specific to the messenger. The system accommodates those extra |
|
| 429 | + * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
| 430 | + * value for those extra fields dynamically via the EE_message object. |
|
| 431 | + * |
|
| 432 | + * @param string $field_name expecting the fully qualified field name. |
|
| 433 | + * @return mixed|null value for the field if found. null if not found. |
|
| 434 | + * @throws ReflectionException |
|
| 435 | + * @throws InvalidArgumentException |
|
| 436 | + * @throws InvalidInterfaceException |
|
| 437 | + * @throws InvalidDataTypeException |
|
| 438 | + * @throws EE_Error |
|
| 439 | + */ |
|
| 440 | + public function get_field_or_extra_meta($field_name) |
|
| 441 | + { |
|
| 442 | + if ($this->get_model()->has_field($field_name)) { |
|
| 443 | + $column_value = $this->get($field_name); |
|
| 444 | + } else { |
|
| 445 | + // This isn't a column in the main table, let's see if it is in the extra meta. |
|
| 446 | + $column_value = $this->get_extra_meta($field_name, true, null); |
|
| 447 | + } |
|
| 448 | + return $column_value; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 454 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 455 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
| 456 | + * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
| 457 | + * |
|
| 458 | + * @access public |
|
| 459 | + * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
| 460 | + * @return void |
|
| 461 | + * @throws InvalidArgumentException |
|
| 462 | + * @throws InvalidInterfaceException |
|
| 463 | + * @throws InvalidDataTypeException |
|
| 464 | + * @throws EE_Error |
|
| 465 | + * @throws ReflectionException |
|
| 466 | + */ |
|
| 467 | + public function set_timezone($timezone = '') |
|
| 468 | + { |
|
| 469 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 470 | + // make sure we clear all cached properties because they won't be relevant now |
|
| 471 | + $this->_clear_cached_properties(); |
|
| 472 | + // make sure we update field settings and the date for all EE_Datetime_Fields |
|
| 473 | + $model_fields = $this->get_model()->field_settings(false); |
|
| 474 | + foreach ($model_fields as $field_name => $field_obj) { |
|
| 475 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 476 | + $field_obj->set_timezone($this->_timezone); |
|
| 477 | + if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
| 478 | + EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone)); |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * This just returns whatever is set for the current timezone. |
|
| 487 | + * |
|
| 488 | + * @access public |
|
| 489 | + * @return string timezone string |
|
| 490 | + */ |
|
| 491 | + public function get_timezone() |
|
| 492 | + { |
|
| 493 | + return $this->_timezone; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * This sets the internal date format to what is sent in to be used as the new default for the class |
|
| 499 | + * internally instead of wp set date format options |
|
| 500 | + * |
|
| 501 | + * @since 4.6 |
|
| 502 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
| 503 | + */ |
|
| 504 | + public function set_date_format($format) |
|
| 505 | + { |
|
| 506 | + $this->_dt_frmt = $format; |
|
| 507 | + // clear cached_properties because they won't be relevant now. |
|
| 508 | + $this->_clear_cached_properties(); |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * This sets the internal time format string to what is sent in to be used as the new default for the |
|
| 514 | + * class internally instead of wp set time format options. |
|
| 515 | + * |
|
| 516 | + * @since 4.6 |
|
| 517 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
| 518 | + */ |
|
| 519 | + public function set_time_format($format) |
|
| 520 | + { |
|
| 521 | + $this->_tm_frmt = $format; |
|
| 522 | + // clear cached_properties because they won't be relevant now. |
|
| 523 | + $this->_clear_cached_properties(); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * This returns the current internal set format for the date and time formats. |
|
| 529 | + * |
|
| 530 | + * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
| 531 | + * where the first value is the date format and the second value is the time format. |
|
| 532 | + * @return mixed string|array |
|
| 533 | + */ |
|
| 534 | + public function get_format($full = true) |
|
| 535 | + { |
|
| 536 | + return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * cache |
|
| 542 | + * stores the passed model object on the current model object. |
|
| 543 | + * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
| 544 | + * |
|
| 545 | + * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
| 546 | + * 'Registration' associated with this model object |
|
| 547 | + * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
| 548 | + * that could be a payment or a registration) |
|
| 549 | + * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
| 550 | + * items which will be stored in an array on this object |
|
| 551 | + * @throws ReflectionException |
|
| 552 | + * @throws InvalidArgumentException |
|
| 553 | + * @throws InvalidInterfaceException |
|
| 554 | + * @throws InvalidDataTypeException |
|
| 555 | + * @throws EE_Error |
|
| 556 | + * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
| 557 | + * related thing, no array) |
|
| 558 | + */ |
|
| 559 | + public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
| 560 | + { |
|
| 561 | + // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
| 562 | + if (! $object_to_cache instanceof EE_Base_Class) { |
|
| 563 | + return false; |
|
| 564 | + } |
|
| 565 | + // also get "how" the object is related, or throw an error |
|
| 566 | + if (! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 567 | + throw new EE_Error( |
|
| 568 | + sprintf( |
|
| 569 | + esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
| 570 | + $relationName, |
|
| 571 | + get_class($this) |
|
| 572 | + ) |
|
| 573 | + ); |
|
| 574 | + } |
|
| 575 | + // how many things are related ? |
|
| 576 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 577 | + // if it's a "belongs to" relationship, then there's only one related model object |
|
| 578 | + // eg, if this is a registration, there's only 1 attendee for it |
|
| 579 | + // so for these model objects just set it to be cached |
|
| 580 | + $this->_model_relations[ $relationName ] = $object_to_cache; |
|
| 581 | + $return = true; |
|
| 582 | + } else { |
|
| 583 | + // otherwise, this is the "many" side of a one to many relationship, |
|
| 584 | + // so we'll add the object to the array of related objects for that type. |
|
| 585 | + // eg: if this is an event, there are many registrations for that event, |
|
| 586 | + // so we cache the registrations in an array |
|
| 587 | + if (! is_array($this->_model_relations[ $relationName ])) { |
|
| 588 | + // if for some reason, the cached item is a model object, |
|
| 589 | + // then stick that in the array, otherwise start with an empty array |
|
| 590 | + $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] |
|
| 591 | + instanceof |
|
| 592 | + EE_Base_Class |
|
| 593 | + ? array($this->_model_relations[ $relationName ]) : array(); |
|
| 594 | + } |
|
| 595 | + // first check for a cache_id which is normally empty |
|
| 596 | + if (! empty($cache_id)) { |
|
| 597 | + // if the cache_id exists, then it means we are purposely trying to cache this |
|
| 598 | + // with a known key that can then be used to retrieve the object later on |
|
| 599 | + $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
| 600 | + $return = $cache_id; |
|
| 601 | + } elseif ($object_to_cache->ID()) { |
|
| 602 | + // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
| 603 | + $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
| 604 | + $return = $object_to_cache->ID(); |
|
| 605 | + } else { |
|
| 606 | + // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
| 607 | + $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
| 608 | + // move the internal pointer to the end of the array |
|
| 609 | + end($this->_model_relations[ $relationName ]); |
|
| 610 | + // and grab the key so that we can return it |
|
| 611 | + $return = key($this->_model_relations[ $relationName ]); |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | + return $return; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + |
|
| 618 | + /** |
|
| 619 | + * For adding an item to the cached_properties property. |
|
| 620 | + * |
|
| 621 | + * @access protected |
|
| 622 | + * @param string $fieldname the property item the corresponding value is for. |
|
| 623 | + * @param mixed $value The value we are caching. |
|
| 624 | + * @param string|null $cache_type |
|
| 625 | + * @return void |
|
| 626 | + * @throws ReflectionException |
|
| 627 | + * @throws InvalidArgumentException |
|
| 628 | + * @throws InvalidInterfaceException |
|
| 629 | + * @throws InvalidDataTypeException |
|
| 630 | + * @throws EE_Error |
|
| 631 | + */ |
|
| 632 | + protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
| 633 | + { |
|
| 634 | + // first make sure this property exists |
|
| 635 | + $this->get_model()->field_settings_for($fieldname); |
|
| 636 | + $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
| 637 | + $this->_cached_properties[ $fieldname ][ $cache_type ] = $value; |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + |
|
| 641 | + /** |
|
| 642 | + * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
| 643 | + * This also SETS the cache if we return the actual property! |
|
| 644 | + * |
|
| 645 | + * @param string $fieldname the name of the property we're trying to retrieve |
|
| 646 | + * @param bool $pretty |
|
| 647 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 648 | + * (in cases where the same property may be used for different outputs |
|
| 649 | + * - i.e. datetime, money etc.) |
|
| 650 | + * It can also accept certain pre-defined "schema" strings |
|
| 651 | + * to define how to output the property. |
|
| 652 | + * see the field's prepare_for_pretty_echoing for what strings can be used |
|
| 653 | + * @return mixed whatever the value for the property is we're retrieving |
|
| 654 | + * @throws ReflectionException |
|
| 655 | + * @throws InvalidArgumentException |
|
| 656 | + * @throws InvalidInterfaceException |
|
| 657 | + * @throws InvalidDataTypeException |
|
| 658 | + * @throws EE_Error |
|
| 659 | + */ |
|
| 660 | + protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 661 | + { |
|
| 662 | + // verify the field exists |
|
| 663 | + $model = $this->get_model(); |
|
| 664 | + $model->field_settings_for($fieldname); |
|
| 665 | + $cache_type = $pretty ? 'pretty' : 'standard'; |
|
| 666 | + $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 667 | + if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) { |
|
| 668 | + return $this->_cached_properties[ $fieldname ][ $cache_type ]; |
|
| 669 | + } |
|
| 670 | + $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
|
| 671 | + $this->_set_cached_property($fieldname, $value, $cache_type); |
|
| 672 | + return $value; |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + |
|
| 676 | + /** |
|
| 677 | + * If the cache didn't fetch the needed item, this fetches it. |
|
| 678 | + * |
|
| 679 | + * @param string $fieldname |
|
| 680 | + * @param bool $pretty |
|
| 681 | + * @param string $extra_cache_ref |
|
| 682 | + * @return mixed |
|
| 683 | + * @throws InvalidArgumentException |
|
| 684 | + * @throws InvalidInterfaceException |
|
| 685 | + * @throws InvalidDataTypeException |
|
| 686 | + * @throws EE_Error |
|
| 687 | + * @throws ReflectionException |
|
| 688 | + */ |
|
| 689 | + protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 690 | + { |
|
| 691 | + $field_obj = $this->get_model()->field_settings_for($fieldname); |
|
| 692 | + // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
| 693 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 694 | + $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
| 695 | + } |
|
| 696 | + if (! isset($this->_fields[ $fieldname ])) { |
|
| 697 | + $this->_fields[ $fieldname ] = null; |
|
| 698 | + } |
|
| 699 | + $value = $pretty |
|
| 700 | + ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref) |
|
| 701 | + : $field_obj->prepare_for_get($this->_fields[ $fieldname ]); |
|
| 702 | + return $value; |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * set timezone, formats, and output for EE_Datetime_Field objects |
|
| 708 | + * |
|
| 709 | + * @param \EE_Datetime_Field $datetime_field |
|
| 710 | + * @param bool $pretty |
|
| 711 | + * @param null $date_or_time |
|
| 712 | + * @return void |
|
| 713 | + * @throws InvalidArgumentException |
|
| 714 | + * @throws InvalidInterfaceException |
|
| 715 | + * @throws InvalidDataTypeException |
|
| 716 | + * @throws EE_Error |
|
| 717 | + */ |
|
| 718 | + protected function _prepare_datetime_field( |
|
| 719 | + EE_Datetime_Field $datetime_field, |
|
| 720 | + $pretty = false, |
|
| 721 | + $date_or_time = null |
|
| 722 | + ) { |
|
| 723 | + $datetime_field->set_timezone($this->_timezone); |
|
| 724 | + $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
| 725 | + $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
| 726 | + // set the output returned |
|
| 727 | + switch ($date_or_time) { |
|
| 728 | + case 'D': |
|
| 729 | + $datetime_field->set_date_time_output('date'); |
|
| 730 | + break; |
|
| 731 | + case 'T': |
|
| 732 | + $datetime_field->set_date_time_output('time'); |
|
| 733 | + break; |
|
| 734 | + default: |
|
| 735 | + $datetime_field->set_date_time_output(); |
|
| 736 | + } |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * This just takes care of clearing out the cached_properties |
|
| 742 | + * |
|
| 743 | + * @return void |
|
| 744 | + */ |
|
| 745 | + protected function _clear_cached_properties() |
|
| 746 | + { |
|
| 747 | + $this->_cached_properties = array(); |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + |
|
| 751 | + /** |
|
| 752 | + * This just clears out ONE property if it exists in the cache |
|
| 753 | + * |
|
| 754 | + * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
| 755 | + * @return void |
|
| 756 | + */ |
|
| 757 | + protected function _clear_cached_property($property_name) |
|
| 758 | + { |
|
| 759 | + if (isset($this->_cached_properties[ $property_name ])) { |
|
| 760 | + unset($this->_cached_properties[ $property_name ]); |
|
| 761 | + } |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + |
|
| 765 | + /** |
|
| 766 | + * Ensures that this related thing is a model object. |
|
| 767 | + * |
|
| 768 | + * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
| 769 | + * @param string $model_name name of the related thing, eg 'Attendee', |
|
| 770 | + * @return EE_Base_Class |
|
| 771 | + * @throws ReflectionException |
|
| 772 | + * @throws InvalidArgumentException |
|
| 773 | + * @throws InvalidInterfaceException |
|
| 774 | + * @throws InvalidDataTypeException |
|
| 775 | + * @throws EE_Error |
|
| 776 | + */ |
|
| 777 | + protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
| 778 | + { |
|
| 779 | + $other_model_instance = self::_get_model_instance_with_name( |
|
| 780 | + self::_get_model_classname($model_name), |
|
| 781 | + $this->_timezone |
|
| 782 | + ); |
|
| 783 | + return $other_model_instance->ensure_is_obj($object_or_id); |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + |
|
| 787 | + /** |
|
| 788 | + * Forgets the cached model of the given relation Name. So the next time we request it, |
|
| 789 | + * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
| 790 | + * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
| 791 | + * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
| 792 | + * |
|
| 793 | + * @param string $relationName one of the keys in the _model_relations array on the model. |
|
| 794 | + * Eg 'Registration' |
|
| 795 | + * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
| 796 | + * if you intend to use $clear_all = TRUE, or the relation only |
|
| 797 | + * has 1 object anyways (ie, it's a BelongsToRelation) |
|
| 798 | + * @param bool $clear_all This flags clearing the entire cache relation property if |
|
| 799 | + * this is HasMany or HABTM. |
|
| 800 | + * @throws ReflectionException |
|
| 801 | + * @throws InvalidArgumentException |
|
| 802 | + * @throws InvalidInterfaceException |
|
| 803 | + * @throws InvalidDataTypeException |
|
| 804 | + * @throws EE_Error |
|
| 805 | + * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
| 806 | + * relation from all |
|
| 807 | + */ |
|
| 808 | + public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
| 809 | + { |
|
| 810 | + $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 811 | + $index_in_cache = ''; |
|
| 812 | + if (! $relationship_to_model) { |
|
| 813 | + throw new EE_Error( |
|
| 814 | + sprintf( |
|
| 815 | + esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
|
| 816 | + $relationName, |
|
| 817 | + get_class($this) |
|
| 818 | + ) |
|
| 819 | + ); |
|
| 820 | + } |
|
| 821 | + if ($clear_all) { |
|
| 822 | + $obj_removed = true; |
|
| 823 | + $this->_model_relations[ $relationName ] = null; |
|
| 824 | + } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 825 | + $obj_removed = $this->_model_relations[ $relationName ]; |
|
| 826 | + $this->_model_relations[ $relationName ] = null; |
|
| 827 | + } else { |
|
| 828 | + if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
| 829 | + && $object_to_remove_or_index_into_array->ID() |
|
| 830 | + ) { |
|
| 831 | + $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
| 832 | + if (is_array($this->_model_relations[ $relationName ]) |
|
| 833 | + && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
| 834 | + ) { |
|
| 835 | + $index_found_at = null; |
|
| 836 | + // find this object in the array even though it has a different key |
|
| 837 | + foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
| 838 | + /** @noinspection TypeUnsafeComparisonInspection */ |
|
| 839 | + if ($obj instanceof EE_Base_Class |
|
| 840 | + && ( |
|
| 841 | + $obj == $object_to_remove_or_index_into_array |
|
| 842 | + || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
| 843 | + ) |
|
| 844 | + ) { |
|
| 845 | + $index_found_at = $index; |
|
| 846 | + break; |
|
| 847 | + } |
|
| 848 | + } |
|
| 849 | + if ($index_found_at) { |
|
| 850 | + $index_in_cache = $index_found_at; |
|
| 851 | + } else { |
|
| 852 | + // it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
| 853 | + // if it wasn't in it to begin with. So we're done |
|
| 854 | + return $object_to_remove_or_index_into_array; |
|
| 855 | + } |
|
| 856 | + } |
|
| 857 | + } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
| 858 | + // so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
| 859 | + foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
| 860 | + /** @noinspection TypeUnsafeComparisonInspection */ |
|
| 861 | + if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
| 862 | + $index_in_cache = $index; |
|
| 863 | + } |
|
| 864 | + } |
|
| 865 | + } else { |
|
| 866 | + $index_in_cache = $object_to_remove_or_index_into_array; |
|
| 867 | + } |
|
| 868 | + // supposedly we've found it. But it could just be that the client code |
|
| 869 | + // provided a bad index/object |
|
| 870 | + if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
| 871 | + $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
| 872 | + unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
| 873 | + } else { |
|
| 874 | + // that thing was never cached anyways. |
|
| 875 | + $obj_removed = null; |
|
| 876 | + } |
|
| 877 | + } |
|
| 878 | + return $obj_removed; |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + |
|
| 882 | + /** |
|
| 883 | + * update_cache_after_object_save |
|
| 884 | + * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
| 885 | + * obtained after being saved to the db |
|
| 886 | + * |
|
| 887 | + * @param string $relationName - the type of object that is cached |
|
| 888 | + * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
| 889 | + * @param string $current_cache_id - the ID that was used when originally caching the object |
|
| 890 | + * @return boolean TRUE on success, FALSE on fail |
|
| 891 | + * @throws ReflectionException |
|
| 892 | + * @throws InvalidArgumentException |
|
| 893 | + * @throws InvalidInterfaceException |
|
| 894 | + * @throws InvalidDataTypeException |
|
| 895 | + * @throws EE_Error |
|
| 896 | + */ |
|
| 897 | + public function update_cache_after_object_save( |
|
| 898 | + $relationName, |
|
| 899 | + EE_Base_Class $newly_saved_object, |
|
| 900 | + $current_cache_id = '' |
|
| 901 | + ) { |
|
| 902 | + // verify that incoming object is of the correct type |
|
| 903 | + $obj_class = 'EE_' . $relationName; |
|
| 904 | + if ($newly_saved_object instanceof $obj_class) { |
|
| 905 | + /* @type EE_Base_Class $newly_saved_object */ |
|
| 906 | + // now get the type of relation |
|
| 907 | + $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 908 | + // if this is a 1:1 relationship |
|
| 909 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 910 | + // then just replace the cached object with the newly saved object |
|
| 911 | + $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
| 912 | + return true; |
|
| 913 | + // or if it's some kind of sordid feral polyamorous relationship... |
|
| 914 | + } |
|
| 915 | + if (is_array($this->_model_relations[ $relationName ]) |
|
| 916 | + && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
| 917 | + ) { |
|
| 918 | + // then remove the current cached item |
|
| 919 | + unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
| 920 | + // and cache the newly saved object using it's new ID |
|
| 921 | + $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
| 922 | + return true; |
|
| 923 | + } |
|
| 924 | + } |
|
| 925 | + return false; |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + |
|
| 929 | + /** |
|
| 930 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 931 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 932 | + * |
|
| 933 | + * @param string $relationName |
|
| 934 | + * @return EE_Base_Class |
|
| 935 | + */ |
|
| 936 | + public function get_one_from_cache($relationName) |
|
| 937 | + { |
|
| 938 | + $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
| 939 | + ? $this->_model_relations[ $relationName ] |
|
| 940 | + : null; |
|
| 941 | + if (is_array($cached_array_or_object)) { |
|
| 942 | + return array_shift($cached_array_or_object); |
|
| 943 | + } |
|
| 944 | + return $cached_array_or_object; |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + |
|
| 948 | + /** |
|
| 949 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 950 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 951 | + * |
|
| 952 | + * @param string $relationName |
|
| 953 | + * @throws ReflectionException |
|
| 954 | + * @throws InvalidArgumentException |
|
| 955 | + * @throws InvalidInterfaceException |
|
| 956 | + * @throws InvalidDataTypeException |
|
| 957 | + * @throws EE_Error |
|
| 958 | + * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
| 959 | + */ |
|
| 960 | + public function get_all_from_cache($relationName) |
|
| 961 | + { |
|
| 962 | + $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array(); |
|
| 963 | + // if the result is not an array, but exists, make it an array |
|
| 964 | + $objects = is_array($objects) ? $objects : array($objects); |
|
| 965 | + // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
| 966 | + // basically, if this model object was stored in the session, and these cached model objects |
|
| 967 | + // already have IDs, let's make sure they're in their model's entity mapper |
|
| 968 | + // otherwise we will have duplicates next time we call |
|
| 969 | + // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
| 970 | + $model = EE_Registry::instance()->load_model($relationName); |
|
| 971 | + foreach ($objects as $model_object) { |
|
| 972 | + if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
| 973 | + // ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
| 974 | + if ($model_object->ID()) { |
|
| 975 | + $model->add_to_entity_map($model_object); |
|
| 976 | + } |
|
| 977 | + } else { |
|
| 978 | + throw new EE_Error( |
|
| 979 | + sprintf( |
|
| 980 | + esc_html__( |
|
| 981 | + 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
| 982 | + 'event_espresso' |
|
| 983 | + ), |
|
| 984 | + $relationName, |
|
| 985 | + gettype($model_object) |
|
| 986 | + ) |
|
| 987 | + ); |
|
| 988 | + } |
|
| 989 | + } |
|
| 990 | + return $objects; |
|
| 991 | + } |
|
| 992 | + |
|
| 993 | + |
|
| 994 | + /** |
|
| 995 | + * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 996 | + * matching the given query conditions. |
|
| 997 | + * |
|
| 998 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 999 | + * @param int $limit How many objects to return. |
|
| 1000 | + * @param array $query_params Any additional conditions on the query. |
|
| 1001 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1002 | + * you can indicate just the columns you want returned |
|
| 1003 | + * @return array|EE_Base_Class[] |
|
| 1004 | + * @throws ReflectionException |
|
| 1005 | + * @throws InvalidArgumentException |
|
| 1006 | + * @throws InvalidInterfaceException |
|
| 1007 | + * @throws InvalidDataTypeException |
|
| 1008 | + * @throws EE_Error |
|
| 1009 | + */ |
|
| 1010 | + public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 1011 | + { |
|
| 1012 | + $model = $this->get_model(); |
|
| 1013 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1014 | + ? $model->get_primary_key_field()->get_name() |
|
| 1015 | + : $field_to_order_by; |
|
| 1016 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1017 | + if (empty($field) || empty($current_value)) { |
|
| 1018 | + return array(); |
|
| 1019 | + } |
|
| 1020 | + return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + |
|
| 1024 | + /** |
|
| 1025 | + * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 1026 | + * matching the given query conditions. |
|
| 1027 | + * |
|
| 1028 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1029 | + * @param int $limit How many objects to return. |
|
| 1030 | + * @param array $query_params Any additional conditions on the query. |
|
| 1031 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1032 | + * you can indicate just the columns you want returned |
|
| 1033 | + * @return array|EE_Base_Class[] |
|
| 1034 | + * @throws ReflectionException |
|
| 1035 | + * @throws InvalidArgumentException |
|
| 1036 | + * @throws InvalidInterfaceException |
|
| 1037 | + * @throws InvalidDataTypeException |
|
| 1038 | + * @throws EE_Error |
|
| 1039 | + */ |
|
| 1040 | + public function previous_x( |
|
| 1041 | + $field_to_order_by = null, |
|
| 1042 | + $limit = 1, |
|
| 1043 | + $query_params = array(), |
|
| 1044 | + $columns_to_select = null |
|
| 1045 | + ) { |
|
| 1046 | + $model = $this->get_model(); |
|
| 1047 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1048 | + ? $model->get_primary_key_field()->get_name() |
|
| 1049 | + : $field_to_order_by; |
|
| 1050 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1051 | + if (empty($field) || empty($current_value)) { |
|
| 1052 | + return array(); |
|
| 1053 | + } |
|
| 1054 | + return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 1055 | + } |
|
| 1056 | + |
|
| 1057 | + |
|
| 1058 | + /** |
|
| 1059 | + * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
| 1060 | + * matching the given query conditions. |
|
| 1061 | + * |
|
| 1062 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1063 | + * @param array $query_params Any additional conditions on the query. |
|
| 1064 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1065 | + * you can indicate just the columns you want returned |
|
| 1066 | + * @return array|EE_Base_Class |
|
| 1067 | + * @throws ReflectionException |
|
| 1068 | + * @throws InvalidArgumentException |
|
| 1069 | + * @throws InvalidInterfaceException |
|
| 1070 | + * @throws InvalidDataTypeException |
|
| 1071 | + * @throws EE_Error |
|
| 1072 | + */ |
|
| 1073 | + public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1074 | + { |
|
| 1075 | + $model = $this->get_model(); |
|
| 1076 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1077 | + ? $model->get_primary_key_field()->get_name() |
|
| 1078 | + : $field_to_order_by; |
|
| 1079 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1080 | + if (empty($field) || empty($current_value)) { |
|
| 1081 | + return array(); |
|
| 1082 | + } |
|
| 1083 | + return $model->next($current_value, $field, $query_params, $columns_to_select); |
|
| 1084 | + } |
|
| 1085 | + |
|
| 1086 | + |
|
| 1087 | + /** |
|
| 1088 | + * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
| 1089 | + * matching the given query conditions. |
|
| 1090 | + * |
|
| 1091 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1092 | + * @param array $query_params Any additional conditions on the query. |
|
| 1093 | + * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 1094 | + * you can indicate just the column you want returned |
|
| 1095 | + * @return array|EE_Base_Class |
|
| 1096 | + * @throws ReflectionException |
|
| 1097 | + * @throws InvalidArgumentException |
|
| 1098 | + * @throws InvalidInterfaceException |
|
| 1099 | + * @throws InvalidDataTypeException |
|
| 1100 | + * @throws EE_Error |
|
| 1101 | + */ |
|
| 1102 | + public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1103 | + { |
|
| 1104 | + $model = $this->get_model(); |
|
| 1105 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1106 | + ? $model->get_primary_key_field()->get_name() |
|
| 1107 | + : $field_to_order_by; |
|
| 1108 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1109 | + if (empty($field) || empty($current_value)) { |
|
| 1110 | + return array(); |
|
| 1111 | + } |
|
| 1112 | + return $model->previous($current_value, $field, $query_params, $columns_to_select); |
|
| 1113 | + } |
|
| 1114 | + |
|
| 1115 | + |
|
| 1116 | + /** |
|
| 1117 | + * Overrides parent because parent expects old models. |
|
| 1118 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
| 1119 | + * |
|
| 1120 | + * @param string $field_name |
|
| 1121 | + * @param mixed $field_value_from_db |
|
| 1122 | + * @throws ReflectionException |
|
| 1123 | + * @throws InvalidArgumentException |
|
| 1124 | + * @throws InvalidInterfaceException |
|
| 1125 | + * @throws InvalidDataTypeException |
|
| 1126 | + * @throws EE_Error |
|
| 1127 | + */ |
|
| 1128 | + public function set_from_db($field_name, $field_value_from_db) |
|
| 1129 | + { |
|
| 1130 | + $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 1131 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 1132 | + // you would think the DB has no NULLs for non-null label fields right? wrong! |
|
| 1133 | + // eg, a CPT model object could have an entry in the posts table, but no |
|
| 1134 | + // entry in the meta table. Meaning that all its columns in the meta table |
|
| 1135 | + // are null! yikes! so when we find one like that, use defaults for its meta columns |
|
| 1136 | + if ($field_value_from_db === null) { |
|
| 1137 | + if ($field_obj->is_nullable()) { |
|
| 1138 | + // if the field allows nulls, then let it be null |
|
| 1139 | + $field_value = null; |
|
| 1140 | + } else { |
|
| 1141 | + $field_value = $field_obj->get_default_value(); |
|
| 1142 | + } |
|
| 1143 | + } else { |
|
| 1144 | + $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
| 1145 | + } |
|
| 1146 | + $this->_fields[ $field_name ] = $field_value; |
|
| 1147 | + $this->_clear_cached_property($field_name); |
|
| 1148 | + } |
|
| 1149 | + } |
|
| 1150 | + |
|
| 1151 | + |
|
| 1152 | + /** |
|
| 1153 | + * verifies that the specified field is of the correct type |
|
| 1154 | + * |
|
| 1155 | + * @param string $field_name |
|
| 1156 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1157 | + * (in cases where the same property may be used for different outputs |
|
| 1158 | + * - i.e. datetime, money etc.) |
|
| 1159 | + * @return mixed |
|
| 1160 | + * @throws ReflectionException |
|
| 1161 | + * @throws InvalidArgumentException |
|
| 1162 | + * @throws InvalidInterfaceException |
|
| 1163 | + * @throws InvalidDataTypeException |
|
| 1164 | + * @throws EE_Error |
|
| 1165 | + */ |
|
| 1166 | + public function get($field_name, $extra_cache_ref = null) |
|
| 1167 | + { |
|
| 1168 | + return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
| 1169 | + } |
|
| 1170 | + |
|
| 1171 | + |
|
| 1172 | + /** |
|
| 1173 | + * This method simply returns the RAW unprocessed value for the given property in this class |
|
| 1174 | + * |
|
| 1175 | + * @param string $field_name A valid fieldname |
|
| 1176 | + * @return mixed Whatever the raw value stored on the property is. |
|
| 1177 | + * @throws ReflectionException |
|
| 1178 | + * @throws InvalidArgumentException |
|
| 1179 | + * @throws InvalidInterfaceException |
|
| 1180 | + * @throws InvalidDataTypeException |
|
| 1181 | + * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
| 1182 | + */ |
|
| 1183 | + public function get_raw($field_name) |
|
| 1184 | + { |
|
| 1185 | + $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1186 | + return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1187 | + ? $this->_fields[ $field_name ]->format('U') |
|
| 1188 | + : $this->_fields[ $field_name ]; |
|
| 1189 | + } |
|
| 1190 | + |
|
| 1191 | + |
|
| 1192 | + /** |
|
| 1193 | + * This is used to return the internal DateTime object used for a field that is a |
|
| 1194 | + * EE_Datetime_Field. |
|
| 1195 | + * |
|
| 1196 | + * @param string $field_name The field name retrieving the DateTime object. |
|
| 1197 | + * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
| 1198 | + * @throws EE_Error an error is set and false returned. If the field IS an |
|
| 1199 | + * EE_Datetime_Field and but the field value is null, then |
|
| 1200 | + * just null is returned (because that indicates that likely |
|
| 1201 | + * this field is nullable). |
|
| 1202 | + * @throws InvalidArgumentException |
|
| 1203 | + * @throws InvalidDataTypeException |
|
| 1204 | + * @throws InvalidInterfaceException |
|
| 1205 | + * @throws ReflectionException |
|
| 1206 | + */ |
|
| 1207 | + public function get_DateTime_object($field_name) |
|
| 1208 | + { |
|
| 1209 | + $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1210 | + if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1211 | + EE_Error::add_error( |
|
| 1212 | + sprintf( |
|
| 1213 | + esc_html__( |
|
| 1214 | + 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
| 1215 | + 'event_espresso' |
|
| 1216 | + ), |
|
| 1217 | + $field_name |
|
| 1218 | + ), |
|
| 1219 | + __FILE__, |
|
| 1220 | + __FUNCTION__, |
|
| 1221 | + __LINE__ |
|
| 1222 | + ); |
|
| 1223 | + return false; |
|
| 1224 | + } |
|
| 1225 | + return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1226 | + ? clone $this->_fields[ $field_name ] |
|
| 1227 | + : null; |
|
| 1228 | + } |
|
| 1229 | + |
|
| 1230 | + |
|
| 1231 | + /** |
|
| 1232 | + * To be used in template to immediately echo out the value, and format it for output. |
|
| 1233 | + * Eg, should call stripslashes and whatnot before echoing |
|
| 1234 | + * |
|
| 1235 | + * @param string $field_name the name of the field as it appears in the DB |
|
| 1236 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1237 | + * (in cases where the same property may be used for different outputs |
|
| 1238 | + * - i.e. datetime, money etc.) |
|
| 1239 | + * @return void |
|
| 1240 | + * @throws ReflectionException |
|
| 1241 | + * @throws InvalidArgumentException |
|
| 1242 | + * @throws InvalidInterfaceException |
|
| 1243 | + * @throws InvalidDataTypeException |
|
| 1244 | + * @throws EE_Error |
|
| 1245 | + */ |
|
| 1246 | + public function e($field_name, $extra_cache_ref = null) |
|
| 1247 | + { |
|
| 1248 | + echo $this->get_pretty($field_name, $extra_cache_ref); |
|
| 1249 | + } |
|
| 1250 | + |
|
| 1251 | + |
|
| 1252 | + /** |
|
| 1253 | + * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
| 1254 | + * can be easily used as the value of form input. |
|
| 1255 | + * |
|
| 1256 | + * @param string $field_name |
|
| 1257 | + * @return void |
|
| 1258 | + * @throws ReflectionException |
|
| 1259 | + * @throws InvalidArgumentException |
|
| 1260 | + * @throws InvalidInterfaceException |
|
| 1261 | + * @throws InvalidDataTypeException |
|
| 1262 | + * @throws EE_Error |
|
| 1263 | + */ |
|
| 1264 | + public function f($field_name) |
|
| 1265 | + { |
|
| 1266 | + $this->e($field_name, 'form_input'); |
|
| 1267 | + } |
|
| 1268 | + |
|
| 1269 | + |
|
| 1270 | + /** |
|
| 1271 | + * Same as `f()` but just returns the value instead of echoing it |
|
| 1272 | + * |
|
| 1273 | + * @param string $field_name |
|
| 1274 | + * @return string |
|
| 1275 | + * @throws ReflectionException |
|
| 1276 | + * @throws InvalidArgumentException |
|
| 1277 | + * @throws InvalidInterfaceException |
|
| 1278 | + * @throws InvalidDataTypeException |
|
| 1279 | + * @throws EE_Error |
|
| 1280 | + */ |
|
| 1281 | + public function get_f($field_name) |
|
| 1282 | + { |
|
| 1283 | + return (string) $this->get_pretty($field_name, 'form_input'); |
|
| 1284 | + } |
|
| 1285 | + |
|
| 1286 | + |
|
| 1287 | + /** |
|
| 1288 | + * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
| 1289 | + * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
| 1290 | + * to see what options are available. |
|
| 1291 | + * |
|
| 1292 | + * @param string $field_name |
|
| 1293 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1294 | + * (in cases where the same property may be used for different outputs |
|
| 1295 | + * - i.e. datetime, money etc.) |
|
| 1296 | + * @return mixed |
|
| 1297 | + * @throws ReflectionException |
|
| 1298 | + * @throws InvalidArgumentException |
|
| 1299 | + * @throws InvalidInterfaceException |
|
| 1300 | + * @throws InvalidDataTypeException |
|
| 1301 | + * @throws EE_Error |
|
| 1302 | + */ |
|
| 1303 | + public function get_pretty($field_name, $extra_cache_ref = null) |
|
| 1304 | + { |
|
| 1305 | + return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
| 1306 | + } |
|
| 1307 | + |
|
| 1308 | + |
|
| 1309 | + /** |
|
| 1310 | + * This simply returns the datetime for the given field name |
|
| 1311 | + * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
| 1312 | + * (and the equivalent e_date, e_time, e_datetime). |
|
| 1313 | + * |
|
| 1314 | + * @access protected |
|
| 1315 | + * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
| 1316 | + * @param string $dt_frmt valid datetime format used for date |
|
| 1317 | + * (if '' then we just use the default on the field, |
|
| 1318 | + * if NULL we use the last-used format) |
|
| 1319 | + * @param string $tm_frmt Same as above except this is for time format |
|
| 1320 | + * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
| 1321 | + * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
| 1322 | + * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
| 1323 | + * if field is not a valid dtt field, or void if echoing |
|
| 1324 | + * @throws ReflectionException |
|
| 1325 | + * @throws InvalidArgumentException |
|
| 1326 | + * @throws InvalidInterfaceException |
|
| 1327 | + * @throws InvalidDataTypeException |
|
| 1328 | + * @throws EE_Error |
|
| 1329 | + */ |
|
| 1330 | + protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
| 1331 | + { |
|
| 1332 | + // clear cached property |
|
| 1333 | + $this->_clear_cached_property($field_name); |
|
| 1334 | + // reset format properties because they are used in get() |
|
| 1335 | + $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
| 1336 | + $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
| 1337 | + if ($echo) { |
|
| 1338 | + $this->e($field_name, $date_or_time); |
|
| 1339 | + return ''; |
|
| 1340 | + } |
|
| 1341 | + return $this->get($field_name, $date_or_time); |
|
| 1342 | + } |
|
| 1343 | + |
|
| 1344 | + |
|
| 1345 | + /** |
|
| 1346 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
| 1347 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1348 | + * other echoes the pretty value for dtt) |
|
| 1349 | + * |
|
| 1350 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1351 | + * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1352 | + * @return string datetime value formatted |
|
| 1353 | + * @throws ReflectionException |
|
| 1354 | + * @throws InvalidArgumentException |
|
| 1355 | + * @throws InvalidInterfaceException |
|
| 1356 | + * @throws InvalidDataTypeException |
|
| 1357 | + * @throws EE_Error |
|
| 1358 | + */ |
|
| 1359 | + public function get_date($field_name, $format = '') |
|
| 1360 | + { |
|
| 1361 | + return $this->_get_datetime($field_name, $format, null, 'D'); |
|
| 1362 | + } |
|
| 1363 | + |
|
| 1364 | + |
|
| 1365 | + /** |
|
| 1366 | + * @param $field_name |
|
| 1367 | + * @param string $format |
|
| 1368 | + * @throws ReflectionException |
|
| 1369 | + * @throws InvalidArgumentException |
|
| 1370 | + * @throws InvalidInterfaceException |
|
| 1371 | + * @throws InvalidDataTypeException |
|
| 1372 | + * @throws EE_Error |
|
| 1373 | + */ |
|
| 1374 | + public function e_date($field_name, $format = '') |
|
| 1375 | + { |
|
| 1376 | + $this->_get_datetime($field_name, $format, null, 'D', true); |
|
| 1377 | + } |
|
| 1378 | + |
|
| 1379 | + |
|
| 1380 | + /** |
|
| 1381 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
| 1382 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1383 | + * other echoes the pretty value for dtt) |
|
| 1384 | + * |
|
| 1385 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1386 | + * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
| 1387 | + * @return string datetime value formatted |
|
| 1388 | + * @throws ReflectionException |
|
| 1389 | + * @throws InvalidArgumentException |
|
| 1390 | + * @throws InvalidInterfaceException |
|
| 1391 | + * @throws InvalidDataTypeException |
|
| 1392 | + * @throws EE_Error |
|
| 1393 | + */ |
|
| 1394 | + public function get_time($field_name, $format = '') |
|
| 1395 | + { |
|
| 1396 | + return $this->_get_datetime($field_name, null, $format, 'T'); |
|
| 1397 | + } |
|
| 1398 | + |
|
| 1399 | + |
|
| 1400 | + /** |
|
| 1401 | + * @param $field_name |
|
| 1402 | + * @param string $format |
|
| 1403 | + * @throws ReflectionException |
|
| 1404 | + * @throws InvalidArgumentException |
|
| 1405 | + * @throws InvalidInterfaceException |
|
| 1406 | + * @throws InvalidDataTypeException |
|
| 1407 | + * @throws EE_Error |
|
| 1408 | + */ |
|
| 1409 | + public function e_time($field_name, $format = '') |
|
| 1410 | + { |
|
| 1411 | + $this->_get_datetime($field_name, null, $format, 'T', true); |
|
| 1412 | + } |
|
| 1413 | + |
|
| 1414 | + |
|
| 1415 | + /** |
|
| 1416 | + * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
| 1417 | + * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1418 | + * other echoes the pretty value for dtt) |
|
| 1419 | + * |
|
| 1420 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1421 | + * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1422 | + * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
| 1423 | + * @return string datetime value formatted |
|
| 1424 | + * @throws ReflectionException |
|
| 1425 | + * @throws InvalidArgumentException |
|
| 1426 | + * @throws InvalidInterfaceException |
|
| 1427 | + * @throws InvalidDataTypeException |
|
| 1428 | + * @throws EE_Error |
|
| 1429 | + */ |
|
| 1430 | + public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1431 | + { |
|
| 1432 | + return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
| 1433 | + } |
|
| 1434 | + |
|
| 1435 | + |
|
| 1436 | + /** |
|
| 1437 | + * @param string $field_name |
|
| 1438 | + * @param string $dt_frmt |
|
| 1439 | + * @param string $tm_frmt |
|
| 1440 | + * @throws ReflectionException |
|
| 1441 | + * @throws InvalidArgumentException |
|
| 1442 | + * @throws InvalidInterfaceException |
|
| 1443 | + * @throws InvalidDataTypeException |
|
| 1444 | + * @throws EE_Error |
|
| 1445 | + */ |
|
| 1446 | + public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1447 | + { |
|
| 1448 | + $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
| 1449 | + } |
|
| 1450 | + |
|
| 1451 | + |
|
| 1452 | + /** |
|
| 1453 | + * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
| 1454 | + * |
|
| 1455 | + * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
| 1456 | + * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
| 1457 | + * on the object will be used. |
|
| 1458 | + * @return string Date and time string in set locale or false if no field exists for the given |
|
| 1459 | + * @throws ReflectionException |
|
| 1460 | + * @throws InvalidArgumentException |
|
| 1461 | + * @throws InvalidInterfaceException |
|
| 1462 | + * @throws InvalidDataTypeException |
|
| 1463 | + * @throws EE_Error |
|
| 1464 | + * field name. |
|
| 1465 | + */ |
|
| 1466 | + public function get_i18n_datetime($field_name, $format = '') |
|
| 1467 | + { |
|
| 1468 | + $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1469 | + return date_i18n( |
|
| 1470 | + $format, |
|
| 1471 | + EEH_DTT_Helper::get_timestamp_with_offset( |
|
| 1472 | + $this->get_raw($field_name), |
|
| 1473 | + $this->_timezone |
|
| 1474 | + ) |
|
| 1475 | + ); |
|
| 1476 | + } |
|
| 1477 | + |
|
| 1478 | + |
|
| 1479 | + /** |
|
| 1480 | + * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
| 1481 | + * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
| 1482 | + * thrown. |
|
| 1483 | + * |
|
| 1484 | + * @param string $field_name The field name being checked |
|
| 1485 | + * @throws ReflectionException |
|
| 1486 | + * @throws InvalidArgumentException |
|
| 1487 | + * @throws InvalidInterfaceException |
|
| 1488 | + * @throws InvalidDataTypeException |
|
| 1489 | + * @throws EE_Error |
|
| 1490 | + * @return EE_Datetime_Field |
|
| 1491 | + */ |
|
| 1492 | + protected function _get_dtt_field_settings($field_name) |
|
| 1493 | + { |
|
| 1494 | + $field = $this->get_model()->field_settings_for($field_name); |
|
| 1495 | + // check if field is dtt |
|
| 1496 | + if ($field instanceof EE_Datetime_Field) { |
|
| 1497 | + return $field; |
|
| 1498 | + } |
|
| 1499 | + throw new EE_Error( |
|
| 1500 | + sprintf( |
|
| 1501 | + esc_html__( |
|
| 1502 | + 'The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field. Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor', |
|
| 1503 | + 'event_espresso' |
|
| 1504 | + ), |
|
| 1505 | + $field_name, |
|
| 1506 | + self::_get_model_classname(get_class($this)) |
|
| 1507 | + ) |
|
| 1508 | + ); |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + |
|
| 1512 | + |
|
| 1513 | + |
|
| 1514 | + /** |
|
| 1515 | + * NOTE ABOUT BELOW: |
|
| 1516 | + * These convenience date and time setters are for setting date and time independently. In other words you might |
|
| 1517 | + * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
| 1518 | + * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
| 1519 | + * method and make sure you send the entire datetime value for setting. |
|
| 1520 | + */ |
|
| 1521 | + /** |
|
| 1522 | + * sets the time on a datetime property |
|
| 1523 | + * |
|
| 1524 | + * @access protected |
|
| 1525 | + * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1526 | + * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
| 1527 | + * @throws ReflectionException |
|
| 1528 | + * @throws InvalidArgumentException |
|
| 1529 | + * @throws InvalidInterfaceException |
|
| 1530 | + * @throws InvalidDataTypeException |
|
| 1531 | + * @throws EE_Error |
|
| 1532 | + */ |
|
| 1533 | + protected function _set_time_for($time, $fieldname) |
|
| 1534 | + { |
|
| 1535 | + $this->_set_date_time('T', $time, $fieldname); |
|
| 1536 | + } |
|
| 1537 | + |
|
| 1538 | + |
|
| 1539 | + /** |
|
| 1540 | + * sets the date on a datetime property |
|
| 1541 | + * |
|
| 1542 | + * @access protected |
|
| 1543 | + * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1544 | + * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
| 1545 | + * @throws ReflectionException |
|
| 1546 | + * @throws InvalidArgumentException |
|
| 1547 | + * @throws InvalidInterfaceException |
|
| 1548 | + * @throws InvalidDataTypeException |
|
| 1549 | + * @throws EE_Error |
|
| 1550 | + */ |
|
| 1551 | + protected function _set_date_for($date, $fieldname) |
|
| 1552 | + { |
|
| 1553 | + $this->_set_date_time('D', $date, $fieldname); |
|
| 1554 | + } |
|
| 1555 | + |
|
| 1556 | + |
|
| 1557 | + /** |
|
| 1558 | + * This takes care of setting a date or time independently on a given model object property. This method also |
|
| 1559 | + * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
| 1560 | + * |
|
| 1561 | + * @access protected |
|
| 1562 | + * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
| 1563 | + * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
| 1564 | + * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
| 1565 | + * EE_Datetime_Field property) |
|
| 1566 | + * @throws ReflectionException |
|
| 1567 | + * @throws InvalidArgumentException |
|
| 1568 | + * @throws InvalidInterfaceException |
|
| 1569 | + * @throws InvalidDataTypeException |
|
| 1570 | + * @throws EE_Error |
|
| 1571 | + */ |
|
| 1572 | + protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
| 1573 | + { |
|
| 1574 | + $field = $this->_get_dtt_field_settings($fieldname); |
|
| 1575 | + $field->set_timezone($this->_timezone); |
|
| 1576 | + $field->set_date_format($this->_dt_frmt); |
|
| 1577 | + $field->set_time_format($this->_tm_frmt); |
|
| 1578 | + switch ($what) { |
|
| 1579 | + case 'T': |
|
| 1580 | + $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time( |
|
| 1581 | + $datetime_value, |
|
| 1582 | + $this->_fields[ $fieldname ] |
|
| 1583 | + ); |
|
| 1584 | + $this->_has_changes = true; |
|
| 1585 | + break; |
|
| 1586 | + case 'D': |
|
| 1587 | + $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
| 1588 | + $datetime_value, |
|
| 1589 | + $this->_fields[ $fieldname ] |
|
| 1590 | + ); |
|
| 1591 | + $this->_has_changes = true; |
|
| 1592 | + break; |
|
| 1593 | + case 'B': |
|
| 1594 | + $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
| 1595 | + $this->_has_changes = true; |
|
| 1596 | + break; |
|
| 1597 | + } |
|
| 1598 | + $this->_clear_cached_property($fieldname); |
|
| 1599 | + } |
|
| 1600 | + |
|
| 1601 | + |
|
| 1602 | + /** |
|
| 1603 | + * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1604 | + * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1605 | + * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1606 | + * that could lead to some unexpected results! |
|
| 1607 | + * |
|
| 1608 | + * @access public |
|
| 1609 | + * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1610 | + * value being returned. |
|
| 1611 | + * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1612 | + * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1613 | + * @param string $prepend You can include something to prepend on the timestamp |
|
| 1614 | + * @param string $append You can include something to append on the timestamp |
|
| 1615 | + * @throws ReflectionException |
|
| 1616 | + * @throws InvalidArgumentException |
|
| 1617 | + * @throws InvalidInterfaceException |
|
| 1618 | + * @throws InvalidDataTypeException |
|
| 1619 | + * @throws EE_Error |
|
| 1620 | + * @return string timestamp |
|
| 1621 | + */ |
|
| 1622 | + public function display_in_my_timezone( |
|
| 1623 | + $field_name, |
|
| 1624 | + $callback = 'get_datetime', |
|
| 1625 | + $args = null, |
|
| 1626 | + $prepend = '', |
|
| 1627 | + $append = '' |
|
| 1628 | + ) { |
|
| 1629 | + $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1630 | + if ($timezone === $this->_timezone) { |
|
| 1631 | + return ''; |
|
| 1632 | + } |
|
| 1633 | + $original_timezone = $this->_timezone; |
|
| 1634 | + $this->set_timezone($timezone); |
|
| 1635 | + $fn = (array) $field_name; |
|
| 1636 | + $args = array_merge($fn, (array) $args); |
|
| 1637 | + if (! method_exists($this, $callback)) { |
|
| 1638 | + throw new EE_Error( |
|
| 1639 | + sprintf( |
|
| 1640 | + esc_html__( |
|
| 1641 | + 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1642 | + 'event_espresso' |
|
| 1643 | + ), |
|
| 1644 | + $callback |
|
| 1645 | + ) |
|
| 1646 | + ); |
|
| 1647 | + } |
|
| 1648 | + $args = (array) $args; |
|
| 1649 | + $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1650 | + $this->set_timezone($original_timezone); |
|
| 1651 | + return $return; |
|
| 1652 | + } |
|
| 1653 | + |
|
| 1654 | + |
|
| 1655 | + /** |
|
| 1656 | + * Deletes this model object. |
|
| 1657 | + * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1658 | + * override |
|
| 1659 | + * `EE_Base_Class::_delete` NOT this class. |
|
| 1660 | + * |
|
| 1661 | + * @return boolean | int |
|
| 1662 | + * @throws ReflectionException |
|
| 1663 | + * @throws InvalidArgumentException |
|
| 1664 | + * @throws InvalidInterfaceException |
|
| 1665 | + * @throws InvalidDataTypeException |
|
| 1666 | + * @throws EE_Error |
|
| 1667 | + */ |
|
| 1668 | + public function delete() |
|
| 1669 | + { |
|
| 1670 | + /** |
|
| 1671 | + * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1672 | + * Note: |
|
| 1673 | + * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1674 | + * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1675 | + * For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1676 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1677 | + * |
|
| 1678 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1679 | + */ |
|
| 1680 | + do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1681 | + $result = $this->_delete(); |
|
| 1682 | + /** |
|
| 1683 | + * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1684 | + * Note: |
|
| 1685 | + * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1686 | + * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1687 | + * For example `EE_Soft_Base_Class::_delete` |
|
| 1688 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1689 | + * |
|
| 1690 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1691 | + * @param boolean $result |
|
| 1692 | + */ |
|
| 1693 | + do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1694 | + return $result; |
|
| 1695 | + } |
|
| 1696 | + |
|
| 1697 | + |
|
| 1698 | + /** |
|
| 1699 | + * Calls the specific delete method for the instantiated class. |
|
| 1700 | + * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1701 | + * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1702 | + * `EE_Base_Class::delete` |
|
| 1703 | + * |
|
| 1704 | + * @return bool|int |
|
| 1705 | + * @throws ReflectionException |
|
| 1706 | + * @throws InvalidArgumentException |
|
| 1707 | + * @throws InvalidInterfaceException |
|
| 1708 | + * @throws InvalidDataTypeException |
|
| 1709 | + * @throws EE_Error |
|
| 1710 | + */ |
|
| 1711 | + protected function _delete() |
|
| 1712 | + { |
|
| 1713 | + return $this->delete_permanently(); |
|
| 1714 | + } |
|
| 1715 | + |
|
| 1716 | + |
|
| 1717 | + /** |
|
| 1718 | + * Deletes this model object permanently from db |
|
| 1719 | + * (but keep in mind related models may block the delete and return an error) |
|
| 1720 | + * |
|
| 1721 | + * @return bool | int |
|
| 1722 | + * @throws ReflectionException |
|
| 1723 | + * @throws InvalidArgumentException |
|
| 1724 | + * @throws InvalidInterfaceException |
|
| 1725 | + * @throws InvalidDataTypeException |
|
| 1726 | + * @throws EE_Error |
|
| 1727 | + */ |
|
| 1728 | + public function delete_permanently() |
|
| 1729 | + { |
|
| 1730 | + /** |
|
| 1731 | + * Called just before HARD deleting a model object |
|
| 1732 | + * |
|
| 1733 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1734 | + */ |
|
| 1735 | + do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1736 | + $model = $this->get_model(); |
|
| 1737 | + $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1738 | + $this->refresh_cache_of_related_objects(); |
|
| 1739 | + /** |
|
| 1740 | + * Called just after HARD deleting a model object |
|
| 1741 | + * |
|
| 1742 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1743 | + * @param boolean $result |
|
| 1744 | + */ |
|
| 1745 | + do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1746 | + return $result; |
|
| 1747 | + } |
|
| 1748 | + |
|
| 1749 | + |
|
| 1750 | + /** |
|
| 1751 | + * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1752 | + * related model objects |
|
| 1753 | + * |
|
| 1754 | + * @throws ReflectionException |
|
| 1755 | + * @throws InvalidArgumentException |
|
| 1756 | + * @throws InvalidInterfaceException |
|
| 1757 | + * @throws InvalidDataTypeException |
|
| 1758 | + * @throws EE_Error |
|
| 1759 | + */ |
|
| 1760 | + public function refresh_cache_of_related_objects() |
|
| 1761 | + { |
|
| 1762 | + $model = $this->get_model(); |
|
| 1763 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1764 | + if (! empty($this->_model_relations[ $relation_name ])) { |
|
| 1765 | + $related_objects = $this->_model_relations[ $relation_name ]; |
|
| 1766 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1767 | + // this relation only stores a single model object, not an array |
|
| 1768 | + // but let's make it consistent |
|
| 1769 | + $related_objects = array($related_objects); |
|
| 1770 | + } |
|
| 1771 | + foreach ($related_objects as $related_object) { |
|
| 1772 | + // only refresh their cache if they're in memory |
|
| 1773 | + if ($related_object instanceof EE_Base_Class) { |
|
| 1774 | + $related_object->clear_cache( |
|
| 1775 | + $model->get_this_model_name(), |
|
| 1776 | + $this |
|
| 1777 | + ); |
|
| 1778 | + } |
|
| 1779 | + } |
|
| 1780 | + } |
|
| 1781 | + } |
|
| 1782 | + } |
|
| 1783 | + |
|
| 1784 | + |
|
| 1785 | + /** |
|
| 1786 | + * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1787 | + * object just before saving. |
|
| 1788 | + * |
|
| 1789 | + * @access public |
|
| 1790 | + * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1791 | + * if provided during the save() method (often client code will change the fields' |
|
| 1792 | + * values before calling save) |
|
| 1793 | + * @throws InvalidArgumentException |
|
| 1794 | + * @throws InvalidInterfaceException |
|
| 1795 | + * @throws InvalidDataTypeException |
|
| 1796 | + * @throws EE_Error |
|
| 1797 | + * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1798 | + * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1799 | + * @throws ReflectionException |
|
| 1800 | + * @throws ReflectionException |
|
| 1801 | + * @throws ReflectionException |
|
| 1802 | + */ |
|
| 1803 | + public function save($set_cols_n_values = array()) |
|
| 1804 | + { |
|
| 1805 | + $model = $this->get_model(); |
|
| 1806 | + /** |
|
| 1807 | + * Filters the fields we're about to save on the model object |
|
| 1808 | + * |
|
| 1809 | + * @param array $set_cols_n_values |
|
| 1810 | + * @param EE_Base_Class $model_object |
|
| 1811 | + */ |
|
| 1812 | + $set_cols_n_values = (array) apply_filters( |
|
| 1813 | + 'FHEE__EE_Base_Class__save__set_cols_n_values', |
|
| 1814 | + $set_cols_n_values, |
|
| 1815 | + $this |
|
| 1816 | + ); |
|
| 1817 | + // set attributes as provided in $set_cols_n_values |
|
| 1818 | + foreach ($set_cols_n_values as $column => $value) { |
|
| 1819 | + $this->set($column, $value); |
|
| 1820 | + } |
|
| 1821 | + // no changes ? then don't do anything |
|
| 1822 | + if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1823 | + return 0; |
|
| 1824 | + } |
|
| 1825 | + /** |
|
| 1826 | + * Saving a model object. |
|
| 1827 | + * Before we perform a save, this action is fired. |
|
| 1828 | + * |
|
| 1829 | + * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1830 | + */ |
|
| 1831 | + do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1832 | + if (! $this->allow_persist()) { |
|
| 1833 | + return 0; |
|
| 1834 | + } |
|
| 1835 | + // now get current attribute values |
|
| 1836 | + $save_cols_n_values = $this->_fields; |
|
| 1837 | + // if the object already has an ID, update it. Otherwise, insert it |
|
| 1838 | + // also: change the assumption about values passed to the model NOT being prepare dby the model object. |
|
| 1839 | + // They have been |
|
| 1840 | + $old_assumption_concerning_value_preparation = $model |
|
| 1841 | + ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1842 | + $model->assume_values_already_prepared_by_model_object(true); |
|
| 1843 | + // does this model have an autoincrement PK? |
|
| 1844 | + if ($model->has_primary_key_field()) { |
|
| 1845 | + if ($model->get_primary_key_field()->is_auto_increment()) { |
|
| 1846 | + // ok check if it's set, if so: update; if not, insert |
|
| 1847 | + if (! empty($save_cols_n_values[ $model->primary_key_name() ])) { |
|
| 1848 | + $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1849 | + } else { |
|
| 1850 | + unset($save_cols_n_values[ $model->primary_key_name() ]); |
|
| 1851 | + $results = $model->insert($save_cols_n_values); |
|
| 1852 | + if ($results) { |
|
| 1853 | + // if successful, set the primary key |
|
| 1854 | + // but don't use the normal SET method, because it will check if |
|
| 1855 | + // an item with the same ID exists in the mapper & db, then |
|
| 1856 | + // will find it in the db (because we just added it) and THAT object |
|
| 1857 | + // will get added to the mapper before we can add this one! |
|
| 1858 | + // but if we just avoid using the SET method, all that headache can be avoided |
|
| 1859 | + $pk_field_name = $model->primary_key_name(); |
|
| 1860 | + $this->_fields[ $pk_field_name ] = $results; |
|
| 1861 | + $this->_clear_cached_property($pk_field_name); |
|
| 1862 | + $model->add_to_entity_map($this); |
|
| 1863 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1864 | + } |
|
| 1865 | + } |
|
| 1866 | + } else {// PK is NOT auto-increment |
|
| 1867 | + // so check if one like it already exists in the db |
|
| 1868 | + if ($model->exists_by_ID($this->ID())) { |
|
| 1869 | + if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1870 | + throw new EE_Error( |
|
| 1871 | + sprintf( |
|
| 1872 | + esc_html__( |
|
| 1873 | + 'Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
|
| 1874 | + 'event_espresso' |
|
| 1875 | + ), |
|
| 1876 | + get_class($this), |
|
| 1877 | + get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1878 | + get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1879 | + '<br />' |
|
| 1880 | + ) |
|
| 1881 | + ); |
|
| 1882 | + } |
|
| 1883 | + $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1884 | + } else { |
|
| 1885 | + $results = $model->insert($save_cols_n_values); |
|
| 1886 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1887 | + } |
|
| 1888 | + } |
|
| 1889 | + } else {// there is NO primary key |
|
| 1890 | + $already_in_db = false; |
|
| 1891 | + foreach ($model->unique_indexes() as $index) { |
|
| 1892 | + $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1893 | + if ($model->exists(array($uniqueness_where_params))) { |
|
| 1894 | + $already_in_db = true; |
|
| 1895 | + } |
|
| 1896 | + } |
|
| 1897 | + if ($already_in_db) { |
|
| 1898 | + $combined_pk_fields_n_values = array_intersect_key( |
|
| 1899 | + $save_cols_n_values, |
|
| 1900 | + $model->get_combined_primary_key_fields() |
|
| 1901 | + ); |
|
| 1902 | + $results = $model->update( |
|
| 1903 | + $save_cols_n_values, |
|
| 1904 | + $combined_pk_fields_n_values |
|
| 1905 | + ); |
|
| 1906 | + } else { |
|
| 1907 | + $results = $model->insert($save_cols_n_values); |
|
| 1908 | + } |
|
| 1909 | + } |
|
| 1910 | + // restore the old assumption about values being prepared by the model object |
|
| 1911 | + $model->assume_values_already_prepared_by_model_object( |
|
| 1912 | + $old_assumption_concerning_value_preparation |
|
| 1913 | + ); |
|
| 1914 | + /** |
|
| 1915 | + * After saving the model object this action is called |
|
| 1916 | + * |
|
| 1917 | + * @param EE_Base_Class $model_object which was just saved |
|
| 1918 | + * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1919 | + * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1920 | + */ |
|
| 1921 | + do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1922 | + $this->_has_changes = false; |
|
| 1923 | + return $results; |
|
| 1924 | + } |
|
| 1925 | + |
|
| 1926 | + |
|
| 1927 | + /** |
|
| 1928 | + * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1929 | + * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1930 | + * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1931 | + * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1932 | + * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the |
|
| 1933 | + * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1934 | + * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1935 | + * |
|
| 1936 | + * @return void |
|
| 1937 | + * @throws ReflectionException |
|
| 1938 | + * @throws InvalidArgumentException |
|
| 1939 | + * @throws InvalidInterfaceException |
|
| 1940 | + * @throws InvalidDataTypeException |
|
| 1941 | + * @throws EE_Error |
|
| 1942 | + */ |
|
| 1943 | + protected function _update_cached_related_model_objs_fks() |
|
| 1944 | + { |
|
| 1945 | + $model = $this->get_model(); |
|
| 1946 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1947 | + if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1948 | + foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1949 | + $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1950 | + $model->get_this_model_name() |
|
| 1951 | + ); |
|
| 1952 | + $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1953 | + if ($related_model_obj_in_cache->ID()) { |
|
| 1954 | + $related_model_obj_in_cache->save(); |
|
| 1955 | + } |
|
| 1956 | + } |
|
| 1957 | + } |
|
| 1958 | + } |
|
| 1959 | + } |
|
| 1960 | + |
|
| 1961 | + |
|
| 1962 | + /** |
|
| 1963 | + * Saves this model object and its NEW cached relations to the database. |
|
| 1964 | + * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1965 | + * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1966 | + * because otherwise, there's a potential for infinite looping of saving |
|
| 1967 | + * Saves the cached related model objects, and ensures the relation between them |
|
| 1968 | + * and this object and properly setup |
|
| 1969 | + * |
|
| 1970 | + * @return int ID of new model object on save; 0 on failure+ |
|
| 1971 | + * @throws ReflectionException |
|
| 1972 | + * @throws InvalidArgumentException |
|
| 1973 | + * @throws InvalidInterfaceException |
|
| 1974 | + * @throws InvalidDataTypeException |
|
| 1975 | + * @throws EE_Error |
|
| 1976 | + */ |
|
| 1977 | + public function save_new_cached_related_model_objs() |
|
| 1978 | + { |
|
| 1979 | + // make sure this has been saved |
|
| 1980 | + if (! $this->ID()) { |
|
| 1981 | + $id = $this->save(); |
|
| 1982 | + } else { |
|
| 1983 | + $id = $this->ID(); |
|
| 1984 | + } |
|
| 1985 | + // now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1986 | + foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1987 | + if ($this->_model_relations[ $relationName ]) { |
|
| 1988 | + // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1989 | + // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1990 | + /* @var $related_model_obj EE_Base_Class */ |
|
| 1991 | + if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1992 | + // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1993 | + // but ONLY if it DOES NOT exist in the DB |
|
| 1994 | + $related_model_obj = $this->_model_relations[ $relationName ]; |
|
| 1995 | + // if( ! $related_model_obj->ID()){ |
|
| 1996 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1997 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1998 | + // } |
|
| 1999 | + } else { |
|
| 2000 | + foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
| 2001 | + // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 2002 | + // but ONLY if it DOES NOT exist in the DB |
|
| 2003 | + // if( ! $related_model_obj->ID()){ |
|
| 2004 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 2005 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 2006 | + // } |
|
| 2007 | + } |
|
| 2008 | + } |
|
| 2009 | + } |
|
| 2010 | + } |
|
| 2011 | + return $id; |
|
| 2012 | + } |
|
| 2013 | + |
|
| 2014 | + |
|
| 2015 | + /** |
|
| 2016 | + * for getting a model while instantiated. |
|
| 2017 | + * |
|
| 2018 | + * @return EEM_Base | EEM_CPT_Base |
|
| 2019 | + * @throws ReflectionException |
|
| 2020 | + * @throws InvalidArgumentException |
|
| 2021 | + * @throws InvalidInterfaceException |
|
| 2022 | + * @throws InvalidDataTypeException |
|
| 2023 | + * @throws EE_Error |
|
| 2024 | + */ |
|
| 2025 | + public function get_model() |
|
| 2026 | + { |
|
| 2027 | + if (! $this->_model) { |
|
| 2028 | + $modelName = self::_get_model_classname(get_class($this)); |
|
| 2029 | + $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 2030 | + } else { |
|
| 2031 | + $this->_model->set_timezone($this->_timezone); |
|
| 2032 | + } |
|
| 2033 | + return $this->_model; |
|
| 2034 | + } |
|
| 2035 | + |
|
| 2036 | + |
|
| 2037 | + /** |
|
| 2038 | + * @param $props_n_values |
|
| 2039 | + * @param $classname |
|
| 2040 | + * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 2041 | + * @throws ReflectionException |
|
| 2042 | + * @throws InvalidArgumentException |
|
| 2043 | + * @throws InvalidInterfaceException |
|
| 2044 | + * @throws InvalidDataTypeException |
|
| 2045 | + * @throws EE_Error |
|
| 2046 | + */ |
|
| 2047 | + protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 2048 | + { |
|
| 2049 | + // TODO: will not work for Term_Relationships because they have no PK! |
|
| 2050 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2051 | + if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2052 | + && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2053 | + ) { |
|
| 2054 | + $id = $props_n_values[ $primary_id_ref ]; |
|
| 2055 | + return self::_get_model($classname)->get_from_entity_map($id); |
|
| 2056 | + } |
|
| 2057 | + return false; |
|
| 2058 | + } |
|
| 2059 | + |
|
| 2060 | + |
|
| 2061 | + /** |
|
| 2062 | + * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 2063 | + * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 2064 | + * primary key for the model AND it is not null, then we check the db. If there's a an object we return it. If not |
|
| 2065 | + * we return false. |
|
| 2066 | + * |
|
| 2067 | + * @param array $props_n_values incoming array of properties and their values |
|
| 2068 | + * @param string $classname the classname of the child class |
|
| 2069 | + * @param null $timezone |
|
| 2070 | + * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 2071 | + * date_format and the second value is the time format |
|
| 2072 | + * @return mixed (EE_Base_Class|bool) |
|
| 2073 | + * @throws InvalidArgumentException |
|
| 2074 | + * @throws InvalidInterfaceException |
|
| 2075 | + * @throws InvalidDataTypeException |
|
| 2076 | + * @throws EE_Error |
|
| 2077 | + * @throws ReflectionException |
|
| 2078 | + * @throws ReflectionException |
|
| 2079 | + * @throws ReflectionException |
|
| 2080 | + */ |
|
| 2081 | + protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 2082 | + { |
|
| 2083 | + $existing = null; |
|
| 2084 | + $model = self::_get_model($classname, $timezone); |
|
| 2085 | + if ($model->has_primary_key_field()) { |
|
| 2086 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2087 | + if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2088 | + && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2089 | + ) { |
|
| 2090 | + $existing = $model->get_one_by_ID( |
|
| 2091 | + $props_n_values[ $primary_id_ref ] |
|
| 2092 | + ); |
|
| 2093 | + } |
|
| 2094 | + } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 2095 | + // no primary key on this model, but there's still a matching item in the DB |
|
| 2096 | + $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 2097 | + self::_get_model($classname, $timezone) |
|
| 2098 | + ->get_index_primary_key_string($props_n_values) |
|
| 2099 | + ); |
|
| 2100 | + } |
|
| 2101 | + if ($existing) { |
|
| 2102 | + // set date formats if present before setting values |
|
| 2103 | + if (! empty($date_formats) && is_array($date_formats)) { |
|
| 2104 | + $existing->set_date_format($date_formats[0]); |
|
| 2105 | + $existing->set_time_format($date_formats[1]); |
|
| 2106 | + } else { |
|
| 2107 | + // set default formats for date and time |
|
| 2108 | + $existing->set_date_format(get_option('date_format')); |
|
| 2109 | + $existing->set_time_format(get_option('time_format')); |
|
| 2110 | + } |
|
| 2111 | + foreach ($props_n_values as $property => $field_value) { |
|
| 2112 | + $existing->set($property, $field_value); |
|
| 2113 | + } |
|
| 2114 | + return $existing; |
|
| 2115 | + } |
|
| 2116 | + return false; |
|
| 2117 | + } |
|
| 2118 | + |
|
| 2119 | + |
|
| 2120 | + /** |
|
| 2121 | + * Gets the EEM_*_Model for this class |
|
| 2122 | + * |
|
| 2123 | + * @access public now, as this is more convenient |
|
| 2124 | + * @param $classname |
|
| 2125 | + * @param null $timezone |
|
| 2126 | + * @throws ReflectionException |
|
| 2127 | + * @throws InvalidArgumentException |
|
| 2128 | + * @throws InvalidInterfaceException |
|
| 2129 | + * @throws InvalidDataTypeException |
|
| 2130 | + * @throws EE_Error |
|
| 2131 | + * @return EEM_Base |
|
| 2132 | + */ |
|
| 2133 | + protected static function _get_model($classname, $timezone = null) |
|
| 2134 | + { |
|
| 2135 | + // find model for this class |
|
| 2136 | + if (! $classname) { |
|
| 2137 | + throw new EE_Error( |
|
| 2138 | + sprintf( |
|
| 2139 | + esc_html__( |
|
| 2140 | + 'What were you thinking calling _get_model(%s)?? You need to specify the class name', |
|
| 2141 | + 'event_espresso' |
|
| 2142 | + ), |
|
| 2143 | + $classname |
|
| 2144 | + ) |
|
| 2145 | + ); |
|
| 2146 | + } |
|
| 2147 | + $modelName = self::_get_model_classname($classname); |
|
| 2148 | + return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 2149 | + } |
|
| 2150 | + |
|
| 2151 | + |
|
| 2152 | + /** |
|
| 2153 | + * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 2154 | + * |
|
| 2155 | + * @param string $model_classname |
|
| 2156 | + * @param null $timezone |
|
| 2157 | + * @return EEM_Base |
|
| 2158 | + * @throws ReflectionException |
|
| 2159 | + * @throws InvalidArgumentException |
|
| 2160 | + * @throws InvalidInterfaceException |
|
| 2161 | + * @throws InvalidDataTypeException |
|
| 2162 | + * @throws EE_Error |
|
| 2163 | + */ |
|
| 2164 | + protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 2165 | + { |
|
| 2166 | + $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 2167 | + $model = EE_Registry::instance()->load_model($model_classname); |
|
| 2168 | + $model->set_timezone($timezone); |
|
| 2169 | + return $model; |
|
| 2170 | + } |
|
| 2171 | + |
|
| 2172 | + |
|
| 2173 | + /** |
|
| 2174 | + * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 2175 | + * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 2176 | + * |
|
| 2177 | + * @param null $model_name |
|
| 2178 | + * @return string like EEM_Attendee |
|
| 2179 | + */ |
|
| 2180 | + private static function _get_model_classname($model_name = null) |
|
| 2181 | + { |
|
| 2182 | + if (strpos($model_name, 'EE_') === 0) { |
|
| 2183 | + $model_classname = str_replace('EE_', 'EEM_', $model_name); |
|
| 2184 | + } else { |
|
| 2185 | + $model_classname = 'EEM_' . $model_name; |
|
| 2186 | + } |
|
| 2187 | + return $model_classname; |
|
| 2188 | + } |
|
| 2189 | + |
|
| 2190 | + |
|
| 2191 | + /** |
|
| 2192 | + * returns the name of the primary key attribute |
|
| 2193 | + * |
|
| 2194 | + * @param null $classname |
|
| 2195 | + * @throws ReflectionException |
|
| 2196 | + * @throws InvalidArgumentException |
|
| 2197 | + * @throws InvalidInterfaceException |
|
| 2198 | + * @throws InvalidDataTypeException |
|
| 2199 | + * @throws EE_Error |
|
| 2200 | + * @return string |
|
| 2201 | + */ |
|
| 2202 | + protected static function _get_primary_key_name($classname = null) |
|
| 2203 | + { |
|
| 2204 | + if (! $classname) { |
|
| 2205 | + throw new EE_Error( |
|
| 2206 | + sprintf( |
|
| 2207 | + esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
|
| 2208 | + $classname |
|
| 2209 | + ) |
|
| 2210 | + ); |
|
| 2211 | + } |
|
| 2212 | + return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 2213 | + } |
|
| 2214 | + |
|
| 2215 | + |
|
| 2216 | + /** |
|
| 2217 | + * Gets the value of the primary key. |
|
| 2218 | + * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 2219 | + * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 2220 | + * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 2221 | + * |
|
| 2222 | + * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 2223 | + * @throws ReflectionException |
|
| 2224 | + * @throws InvalidArgumentException |
|
| 2225 | + * @throws InvalidInterfaceException |
|
| 2226 | + * @throws InvalidDataTypeException |
|
| 2227 | + * @throws EE_Error |
|
| 2228 | + */ |
|
| 2229 | + public function ID() |
|
| 2230 | + { |
|
| 2231 | + $model = $this->get_model(); |
|
| 2232 | + // now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 2233 | + if ($model->has_primary_key_field()) { |
|
| 2234 | + return $this->_fields[ $model->primary_key_name() ]; |
|
| 2235 | + } |
|
| 2236 | + return $model->get_index_primary_key_string($this->_fields); |
|
| 2237 | + } |
|
| 2238 | + |
|
| 2239 | + |
|
| 2240 | + /** |
|
| 2241 | + * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 2242 | + * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 2243 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 2244 | + * |
|
| 2245 | + * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 2246 | + * @param string $relationName eg 'Events','Question',etc. |
|
| 2247 | + * an attendee to a group, you also want to specify which role they |
|
| 2248 | + * will have in that group. So you would use this parameter to |
|
| 2249 | + * specify array('role-column-name'=>'role-id') |
|
| 2250 | + * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 2251 | + * allow you to further constrict the relation to being added. |
|
| 2252 | + * However, keep in mind that the columns (keys) given must match a |
|
| 2253 | + * column on the JOIN table and currently only the HABTM models |
|
| 2254 | + * accept these additional conditions. Also remember that if an |
|
| 2255 | + * exact match isn't found for these extra cols/val pairs, then a |
|
| 2256 | + * NEW row is created in the join table. |
|
| 2257 | + * @param null $cache_id |
|
| 2258 | + * @throws ReflectionException |
|
| 2259 | + * @throws InvalidArgumentException |
|
| 2260 | + * @throws InvalidInterfaceException |
|
| 2261 | + * @throws InvalidDataTypeException |
|
| 2262 | + * @throws EE_Error |
|
| 2263 | + * @return EE_Base_Class the object the relation was added to |
|
| 2264 | + */ |
|
| 2265 | + public function _add_relation_to( |
|
| 2266 | + $otherObjectModelObjectOrID, |
|
| 2267 | + $relationName, |
|
| 2268 | + $extra_join_model_fields_n_values = array(), |
|
| 2269 | + $cache_id = null |
|
| 2270 | + ) { |
|
| 2271 | + $model = $this->get_model(); |
|
| 2272 | + // if this thing exists in the DB, save the relation to the DB |
|
| 2273 | + if ($this->ID()) { |
|
| 2274 | + $otherObject = $model->add_relationship_to( |
|
| 2275 | + $this, |
|
| 2276 | + $otherObjectModelObjectOrID, |
|
| 2277 | + $relationName, |
|
| 2278 | + $extra_join_model_fields_n_values |
|
| 2279 | + ); |
|
| 2280 | + // clear cache so future get_many_related and get_first_related() return new results. |
|
| 2281 | + $this->clear_cache($relationName, $otherObject, true); |
|
| 2282 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2283 | + $otherObject->clear_cache($model->get_this_model_name(), $this); |
|
| 2284 | + } |
|
| 2285 | + } else { |
|
| 2286 | + // this thing doesn't exist in the DB, so just cache it |
|
| 2287 | + if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2288 | + throw new EE_Error( |
|
| 2289 | + sprintf( |
|
| 2290 | + esc_html__( |
|
| 2291 | + 'Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s', |
|
| 2292 | + 'event_espresso' |
|
| 2293 | + ), |
|
| 2294 | + $otherObjectModelObjectOrID, |
|
| 2295 | + get_class($this) |
|
| 2296 | + ) |
|
| 2297 | + ); |
|
| 2298 | + } |
|
| 2299 | + $otherObject = $otherObjectModelObjectOrID; |
|
| 2300 | + $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 2301 | + } |
|
| 2302 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2303 | + // fix the reciprocal relation too |
|
| 2304 | + if ($otherObject->ID()) { |
|
| 2305 | + // its saved so assumed relations exist in the DB, so we can just |
|
| 2306 | + // clear the cache so future queries use the updated info in the DB |
|
| 2307 | + $otherObject->clear_cache( |
|
| 2308 | + $model->get_this_model_name(), |
|
| 2309 | + null, |
|
| 2310 | + true |
|
| 2311 | + ); |
|
| 2312 | + } else { |
|
| 2313 | + // it's not saved, so it caches relations like this |
|
| 2314 | + $otherObject->cache($model->get_this_model_name(), $this); |
|
| 2315 | + } |
|
| 2316 | + } |
|
| 2317 | + return $otherObject; |
|
| 2318 | + } |
|
| 2319 | + |
|
| 2320 | + |
|
| 2321 | + /** |
|
| 2322 | + * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2323 | + * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2324 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2325 | + * from the cache |
|
| 2326 | + * |
|
| 2327 | + * @param mixed $otherObjectModelObjectOrID |
|
| 2328 | + * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2329 | + * to the DB yet |
|
| 2330 | + * @param string $relationName |
|
| 2331 | + * @param array $where_query |
|
| 2332 | + * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2333 | + * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2334 | + * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2335 | + * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
|
| 2336 | + * deleted. |
|
| 2337 | + * @return EE_Base_Class the relation was removed from |
|
| 2338 | + * @throws ReflectionException |
|
| 2339 | + * @throws InvalidArgumentException |
|
| 2340 | + * @throws InvalidInterfaceException |
|
| 2341 | + * @throws InvalidDataTypeException |
|
| 2342 | + * @throws EE_Error |
|
| 2343 | + */ |
|
| 2344 | + public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2345 | + { |
|
| 2346 | + if ($this->ID()) { |
|
| 2347 | + // if this exists in the DB, save the relation change to the DB too |
|
| 2348 | + $otherObject = $this->get_model()->remove_relationship_to( |
|
| 2349 | + $this, |
|
| 2350 | + $otherObjectModelObjectOrID, |
|
| 2351 | + $relationName, |
|
| 2352 | + $where_query |
|
| 2353 | + ); |
|
| 2354 | + $this->clear_cache( |
|
| 2355 | + $relationName, |
|
| 2356 | + $otherObject |
|
| 2357 | + ); |
|
| 2358 | + } else { |
|
| 2359 | + // this doesn't exist in the DB, just remove it from the cache |
|
| 2360 | + $otherObject = $this->clear_cache( |
|
| 2361 | + $relationName, |
|
| 2362 | + $otherObjectModelObjectOrID |
|
| 2363 | + ); |
|
| 2364 | + } |
|
| 2365 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2366 | + $otherObject->clear_cache( |
|
| 2367 | + $this->get_model()->get_this_model_name(), |
|
| 2368 | + $this |
|
| 2369 | + ); |
|
| 2370 | + } |
|
| 2371 | + return $otherObject; |
|
| 2372 | + } |
|
| 2373 | + |
|
| 2374 | + |
|
| 2375 | + /** |
|
| 2376 | + * Removes ALL the related things for the $relationName. |
|
| 2377 | + * |
|
| 2378 | + * @param string $relationName |
|
| 2379 | + * @param array $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 2380 | + * @return EE_Base_Class |
|
| 2381 | + * @throws ReflectionException |
|
| 2382 | + * @throws InvalidArgumentException |
|
| 2383 | + * @throws InvalidInterfaceException |
|
| 2384 | + * @throws InvalidDataTypeException |
|
| 2385 | + * @throws EE_Error |
|
| 2386 | + */ |
|
| 2387 | + public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2388 | + { |
|
| 2389 | + if ($this->ID()) { |
|
| 2390 | + // if this exists in the DB, save the relation change to the DB too |
|
| 2391 | + $otherObjects = $this->get_model()->remove_relations( |
|
| 2392 | + $this, |
|
| 2393 | + $relationName, |
|
| 2394 | + $where_query_params |
|
| 2395 | + ); |
|
| 2396 | + $this->clear_cache( |
|
| 2397 | + $relationName, |
|
| 2398 | + null, |
|
| 2399 | + true |
|
| 2400 | + ); |
|
| 2401 | + } else { |
|
| 2402 | + // this doesn't exist in the DB, just remove it from the cache |
|
| 2403 | + $otherObjects = $this->clear_cache( |
|
| 2404 | + $relationName, |
|
| 2405 | + null, |
|
| 2406 | + true |
|
| 2407 | + ); |
|
| 2408 | + } |
|
| 2409 | + if (is_array($otherObjects)) { |
|
| 2410 | + foreach ($otherObjects as $otherObject) { |
|
| 2411 | + $otherObject->clear_cache( |
|
| 2412 | + $this->get_model()->get_this_model_name(), |
|
| 2413 | + $this |
|
| 2414 | + ); |
|
| 2415 | + } |
|
| 2416 | + } |
|
| 2417 | + return $otherObjects; |
|
| 2418 | + } |
|
| 2419 | + |
|
| 2420 | + |
|
| 2421 | + /** |
|
| 2422 | + * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2423 | + * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2424 | + * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2425 | + * because we want to get even deleted items etc. |
|
| 2426 | + * |
|
| 2427 | + * @param string $relationName key in the model's _model_relations array |
|
| 2428 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 2429 | + * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
|
| 2430 | + * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
|
| 2431 | + * results if you want IDs |
|
| 2432 | + * @throws ReflectionException |
|
| 2433 | + * @throws InvalidArgumentException |
|
| 2434 | + * @throws InvalidInterfaceException |
|
| 2435 | + * @throws InvalidDataTypeException |
|
| 2436 | + * @throws EE_Error |
|
| 2437 | + */ |
|
| 2438 | + public function get_many_related($relationName, $query_params = array()) |
|
| 2439 | + { |
|
| 2440 | + if ($this->ID()) { |
|
| 2441 | + // this exists in the DB, so get the related things from either the cache or the DB |
|
| 2442 | + // if there are query parameters, forget about caching the related model objects. |
|
| 2443 | + if ($query_params) { |
|
| 2444 | + $related_model_objects = $this->get_model()->get_all_related( |
|
| 2445 | + $this, |
|
| 2446 | + $relationName, |
|
| 2447 | + $query_params |
|
| 2448 | + ); |
|
| 2449 | + } else { |
|
| 2450 | + // did we already cache the result of this query? |
|
| 2451 | + $cached_results = $this->get_all_from_cache($relationName); |
|
| 2452 | + if (! $cached_results) { |
|
| 2453 | + $related_model_objects = $this->get_model()->get_all_related( |
|
| 2454 | + $this, |
|
| 2455 | + $relationName, |
|
| 2456 | + $query_params |
|
| 2457 | + ); |
|
| 2458 | + // if no query parameters were passed, then we got all the related model objects |
|
| 2459 | + // for that relation. We can cache them then. |
|
| 2460 | + foreach ($related_model_objects as $related_model_object) { |
|
| 2461 | + $this->cache($relationName, $related_model_object); |
|
| 2462 | + } |
|
| 2463 | + } else { |
|
| 2464 | + $related_model_objects = $cached_results; |
|
| 2465 | + } |
|
| 2466 | + } |
|
| 2467 | + } else { |
|
| 2468 | + // this doesn't exist in the DB, so just get the related things from the cache |
|
| 2469 | + $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2470 | + } |
|
| 2471 | + return $related_model_objects; |
|
| 2472 | + } |
|
| 2473 | + |
|
| 2474 | + |
|
| 2475 | + /** |
|
| 2476 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2477 | + * unless otherwise specified in the $query_params |
|
| 2478 | + * |
|
| 2479 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2480 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2481 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2482 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2483 | + * that by the setting $distinct to TRUE; |
|
| 2484 | + * @return int |
|
| 2485 | + * @throws ReflectionException |
|
| 2486 | + * @throws InvalidArgumentException |
|
| 2487 | + * @throws InvalidInterfaceException |
|
| 2488 | + * @throws InvalidDataTypeException |
|
| 2489 | + * @throws EE_Error |
|
| 2490 | + */ |
|
| 2491 | + public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2492 | + { |
|
| 2493 | + return $this->get_model()->count_related( |
|
| 2494 | + $this, |
|
| 2495 | + $relation_name, |
|
| 2496 | + $query_params, |
|
| 2497 | + $field_to_count, |
|
| 2498 | + $distinct |
|
| 2499 | + ); |
|
| 2500 | + } |
|
| 2501 | + |
|
| 2502 | + |
|
| 2503 | + /** |
|
| 2504 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2505 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2506 | + * |
|
| 2507 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2508 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2509 | + * @param string $field_to_sum name of field to count by. |
|
| 2510 | + * By default, uses primary key |
|
| 2511 | + * (which doesn't make much sense, so you should probably change it) |
|
| 2512 | + * @return int |
|
| 2513 | + * @throws ReflectionException |
|
| 2514 | + * @throws InvalidArgumentException |
|
| 2515 | + * @throws InvalidInterfaceException |
|
| 2516 | + * @throws InvalidDataTypeException |
|
| 2517 | + * @throws EE_Error |
|
| 2518 | + */ |
|
| 2519 | + public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2520 | + { |
|
| 2521 | + return $this->get_model()->sum_related( |
|
| 2522 | + $this, |
|
| 2523 | + $relation_name, |
|
| 2524 | + $query_params, |
|
| 2525 | + $field_to_sum |
|
| 2526 | + ); |
|
| 2527 | + } |
|
| 2528 | + |
|
| 2529 | + |
|
| 2530 | + /** |
|
| 2531 | + * Gets the first (ie, one) related model object of the specified type. |
|
| 2532 | + * |
|
| 2533 | + * @param string $relationName key in the model's _model_relations array |
|
| 2534 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2535 | + * @return EE_Base_Class (not an array, a single object) |
|
| 2536 | + * @throws ReflectionException |
|
| 2537 | + * @throws InvalidArgumentException |
|
| 2538 | + * @throws InvalidInterfaceException |
|
| 2539 | + * @throws InvalidDataTypeException |
|
| 2540 | + * @throws EE_Error |
|
| 2541 | + */ |
|
| 2542 | + public function get_first_related($relationName, $query_params = array()) |
|
| 2543 | + { |
|
| 2544 | + $model = $this->get_model(); |
|
| 2545 | + if ($this->ID()) {// this exists in the DB, get from the cache OR the DB |
|
| 2546 | + // if they've provided some query parameters, don't bother trying to cache the result |
|
| 2547 | + // also make sure we're not caching the result of get_first_related |
|
| 2548 | + // on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2549 | + if ($query_params |
|
| 2550 | + || ! $model->related_settings_for($relationName) |
|
| 2551 | + instanceof |
|
| 2552 | + EE_Belongs_To_Relation |
|
| 2553 | + ) { |
|
| 2554 | + $related_model_object = $model->get_first_related( |
|
| 2555 | + $this, |
|
| 2556 | + $relationName, |
|
| 2557 | + $query_params |
|
| 2558 | + ); |
|
| 2559 | + } else { |
|
| 2560 | + // first, check if we've already cached the result of this query |
|
| 2561 | + $cached_result = $this->get_one_from_cache($relationName); |
|
| 2562 | + if (! $cached_result) { |
|
| 2563 | + $related_model_object = $model->get_first_related( |
|
| 2564 | + $this, |
|
| 2565 | + $relationName, |
|
| 2566 | + $query_params |
|
| 2567 | + ); |
|
| 2568 | + $this->cache($relationName, $related_model_object); |
|
| 2569 | + } else { |
|
| 2570 | + $related_model_object = $cached_result; |
|
| 2571 | + } |
|
| 2572 | + } |
|
| 2573 | + } else { |
|
| 2574 | + $related_model_object = null; |
|
| 2575 | + // this doesn't exist in the Db, |
|
| 2576 | + // but maybe the relation is of type belongs to, and so the related thing might |
|
| 2577 | + if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2578 | + $related_model_object = $model->get_first_related( |
|
| 2579 | + $this, |
|
| 2580 | + $relationName, |
|
| 2581 | + $query_params |
|
| 2582 | + ); |
|
| 2583 | + } |
|
| 2584 | + // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
|
| 2585 | + // just get what's cached on this object |
|
| 2586 | + if (! $related_model_object) { |
|
| 2587 | + $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2588 | + } |
|
| 2589 | + } |
|
| 2590 | + return $related_model_object; |
|
| 2591 | + } |
|
| 2592 | + |
|
| 2593 | + |
|
| 2594 | + /** |
|
| 2595 | + * Does a delete on all related objects of type $relationName and removes |
|
| 2596 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2597 | + * of blocking related model objects) does nothing. If the related model objects are |
|
| 2598 | + * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2599 | + * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2600 | + * |
|
| 2601 | + * @param string $relationName |
|
| 2602 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2603 | + * @return int how many deleted |
|
| 2604 | + * @throws ReflectionException |
|
| 2605 | + * @throws InvalidArgumentException |
|
| 2606 | + * @throws InvalidInterfaceException |
|
| 2607 | + * @throws InvalidDataTypeException |
|
| 2608 | + * @throws EE_Error |
|
| 2609 | + */ |
|
| 2610 | + public function delete_related($relationName, $query_params = array()) |
|
| 2611 | + { |
|
| 2612 | + if ($this->ID()) { |
|
| 2613 | + $count = $this->get_model()->delete_related( |
|
| 2614 | + $this, |
|
| 2615 | + $relationName, |
|
| 2616 | + $query_params |
|
| 2617 | + ); |
|
| 2618 | + } else { |
|
| 2619 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2620 | + $this->clear_cache($relationName, null, true); |
|
| 2621 | + } |
|
| 2622 | + return $count; |
|
| 2623 | + } |
|
| 2624 | + |
|
| 2625 | + |
|
| 2626 | + /** |
|
| 2627 | + * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2628 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2629 | + * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2630 | + * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2631 | + * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2632 | + * |
|
| 2633 | + * @param string $relationName |
|
| 2634 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2635 | + * @return int how many deleted (including those soft deleted) |
|
| 2636 | + * @throws ReflectionException |
|
| 2637 | + * @throws InvalidArgumentException |
|
| 2638 | + * @throws InvalidInterfaceException |
|
| 2639 | + * @throws InvalidDataTypeException |
|
| 2640 | + * @throws EE_Error |
|
| 2641 | + */ |
|
| 2642 | + public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2643 | + { |
|
| 2644 | + if ($this->ID()) { |
|
| 2645 | + $count = $this->get_model()->delete_related_permanently( |
|
| 2646 | + $this, |
|
| 2647 | + $relationName, |
|
| 2648 | + $query_params |
|
| 2649 | + ); |
|
| 2650 | + } else { |
|
| 2651 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2652 | + } |
|
| 2653 | + $this->clear_cache($relationName, null, true); |
|
| 2654 | + return $count; |
|
| 2655 | + } |
|
| 2656 | + |
|
| 2657 | + |
|
| 2658 | + /** |
|
| 2659 | + * is_set |
|
| 2660 | + * Just a simple utility function children can use for checking if property exists |
|
| 2661 | + * |
|
| 2662 | + * @access public |
|
| 2663 | + * @param string $field_name property to check |
|
| 2664 | + * @return bool TRUE if existing,FALSE if not. |
|
| 2665 | + */ |
|
| 2666 | + public function is_set($field_name) |
|
| 2667 | + { |
|
| 2668 | + return isset($this->_fields[ $field_name ]); |
|
| 2669 | + } |
|
| 2670 | + |
|
| 2671 | + |
|
| 2672 | + /** |
|
| 2673 | + * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2674 | + * EE_Error exception if they don't |
|
| 2675 | + * |
|
| 2676 | + * @param mixed (string|array) $properties properties to check |
|
| 2677 | + * @throws EE_Error |
|
| 2678 | + * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2679 | + */ |
|
| 2680 | + protected function _property_exists($properties) |
|
| 2681 | + { |
|
| 2682 | + foreach ((array) $properties as $property_name) { |
|
| 2683 | + // first make sure this property exists |
|
| 2684 | + if (! $this->_fields[ $property_name ]) { |
|
| 2685 | + throw new EE_Error( |
|
| 2686 | + sprintf( |
|
| 2687 | + esc_html__( |
|
| 2688 | + 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2689 | + 'event_espresso' |
|
| 2690 | + ), |
|
| 2691 | + $property_name |
|
| 2692 | + ) |
|
| 2693 | + ); |
|
| 2694 | + } |
|
| 2695 | + } |
|
| 2696 | + return true; |
|
| 2697 | + } |
|
| 2698 | + |
|
| 2699 | + |
|
| 2700 | + /** |
|
| 2701 | + * This simply returns an array of model fields for this object |
|
| 2702 | + * |
|
| 2703 | + * @return array |
|
| 2704 | + * @throws ReflectionException |
|
| 2705 | + * @throws InvalidArgumentException |
|
| 2706 | + * @throws InvalidInterfaceException |
|
| 2707 | + * @throws InvalidDataTypeException |
|
| 2708 | + * @throws EE_Error |
|
| 2709 | + */ |
|
| 2710 | + public function model_field_array() |
|
| 2711 | + { |
|
| 2712 | + $fields = $this->get_model()->field_settings(false); |
|
| 2713 | + $properties = array(); |
|
| 2714 | + // remove prepended underscore |
|
| 2715 | + foreach ($fields as $field_name => $settings) { |
|
| 2716 | + $properties[ $field_name ] = $this->get($field_name); |
|
| 2717 | + } |
|
| 2718 | + return $properties; |
|
| 2719 | + } |
|
| 2720 | + |
|
| 2721 | + |
|
| 2722 | + /** |
|
| 2723 | + * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2724 | + * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2725 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
|
| 2726 | + * Instead of requiring a plugin to extend the EE_Base_Class |
|
| 2727 | + * (which works fine is there's only 1 plugin, but when will that happen?) |
|
| 2728 | + * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
|
| 2729 | + * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
|
| 2730 | + * and accepts 2 arguments: the object on which the function was called, |
|
| 2731 | + * and an array of the original arguments passed to the function. |
|
| 2732 | + * Whatever their callback function returns will be returned by this function. |
|
| 2733 | + * Example: in functions.php (or in a plugin): |
|
| 2734 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
|
| 2735 | + * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2736 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2737 | + * return $previousReturnValue.$returnString; |
|
| 2738 | + * } |
|
| 2739 | + * require('EE_Answer.class.php'); |
|
| 2740 | + * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2741 | + * echo $answer->my_callback('monkeys',100); |
|
| 2742 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2743 | + * |
|
| 2744 | + * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2745 | + * @param array $args array of original arguments passed to the function |
|
| 2746 | + * @throws EE_Error |
|
| 2747 | + * @return mixed whatever the plugin which calls add_filter decides |
|
| 2748 | + */ |
|
| 2749 | + public function __call($methodName, $args) |
|
| 2750 | + { |
|
| 2751 | + $className = get_class($this); |
|
| 2752 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2753 | + if (! has_filter($tagName)) { |
|
| 2754 | + throw new EE_Error( |
|
| 2755 | + sprintf( |
|
| 2756 | + esc_html__( |
|
| 2757 | + "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}", |
|
| 2758 | + 'event_espresso' |
|
| 2759 | + ), |
|
| 2760 | + $methodName, |
|
| 2761 | + $className, |
|
| 2762 | + $tagName |
|
| 2763 | + ) |
|
| 2764 | + ); |
|
| 2765 | + } |
|
| 2766 | + return apply_filters($tagName, null, $this, $args); |
|
| 2767 | + } |
|
| 2768 | + |
|
| 2769 | + |
|
| 2770 | + /** |
|
| 2771 | + * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2772 | + * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2773 | + * |
|
| 2774 | + * @param string $meta_key |
|
| 2775 | + * @param mixed $meta_value |
|
| 2776 | + * @param mixed $previous_value |
|
| 2777 | + * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2778 | + * NOTE: if the values haven't changed, returns 0 |
|
| 2779 | + * @throws InvalidArgumentException |
|
| 2780 | + * @throws InvalidInterfaceException |
|
| 2781 | + * @throws InvalidDataTypeException |
|
| 2782 | + * @throws EE_Error |
|
| 2783 | + * @throws ReflectionException |
|
| 2784 | + */ |
|
| 2785 | + public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2786 | + { |
|
| 2787 | + $query_params = array( |
|
| 2788 | + array( |
|
| 2789 | + 'EXM_key' => $meta_key, |
|
| 2790 | + 'OBJ_ID' => $this->ID(), |
|
| 2791 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2792 | + ), |
|
| 2793 | + ); |
|
| 2794 | + if ($previous_value !== null) { |
|
| 2795 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2796 | + } |
|
| 2797 | + $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2798 | + if (! $existing_rows_like_that) { |
|
| 2799 | + return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2800 | + } |
|
| 2801 | + foreach ($existing_rows_like_that as $existing_row) { |
|
| 2802 | + $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2803 | + } |
|
| 2804 | + return count($existing_rows_like_that); |
|
| 2805 | + } |
|
| 2806 | + |
|
| 2807 | + |
|
| 2808 | + /** |
|
| 2809 | + * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2810 | + * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2811 | + * extra meta row was entered, false if not |
|
| 2812 | + * |
|
| 2813 | + * @param string $meta_key |
|
| 2814 | + * @param mixed $meta_value |
|
| 2815 | + * @param boolean $unique |
|
| 2816 | + * @return boolean |
|
| 2817 | + * @throws InvalidArgumentException |
|
| 2818 | + * @throws InvalidInterfaceException |
|
| 2819 | + * @throws InvalidDataTypeException |
|
| 2820 | + * @throws EE_Error |
|
| 2821 | + * @throws ReflectionException |
|
| 2822 | + * @throws ReflectionException |
|
| 2823 | + */ |
|
| 2824 | + public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2825 | + { |
|
| 2826 | + if ($unique) { |
|
| 2827 | + $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2828 | + array( |
|
| 2829 | + array( |
|
| 2830 | + 'EXM_key' => $meta_key, |
|
| 2831 | + 'OBJ_ID' => $this->ID(), |
|
| 2832 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2833 | + ), |
|
| 2834 | + ) |
|
| 2835 | + ); |
|
| 2836 | + if ($existing_extra_meta) { |
|
| 2837 | + return false; |
|
| 2838 | + } |
|
| 2839 | + } |
|
| 2840 | + $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2841 | + array( |
|
| 2842 | + 'EXM_key' => $meta_key, |
|
| 2843 | + 'EXM_value' => $meta_value, |
|
| 2844 | + 'OBJ_ID' => $this->ID(), |
|
| 2845 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2846 | + ) |
|
| 2847 | + ); |
|
| 2848 | + $new_extra_meta->save(); |
|
| 2849 | + return true; |
|
| 2850 | + } |
|
| 2851 | + |
|
| 2852 | + |
|
| 2853 | + /** |
|
| 2854 | + * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2855 | + * is specified, only deletes extra meta records with that value. |
|
| 2856 | + * |
|
| 2857 | + * @param string $meta_key |
|
| 2858 | + * @param mixed $meta_value |
|
| 2859 | + * @return int number of extra meta rows deleted |
|
| 2860 | + * @throws InvalidArgumentException |
|
| 2861 | + * @throws InvalidInterfaceException |
|
| 2862 | + * @throws InvalidDataTypeException |
|
| 2863 | + * @throws EE_Error |
|
| 2864 | + * @throws ReflectionException |
|
| 2865 | + */ |
|
| 2866 | + public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2867 | + { |
|
| 2868 | + $query_params = array( |
|
| 2869 | + array( |
|
| 2870 | + 'EXM_key' => $meta_key, |
|
| 2871 | + 'OBJ_ID' => $this->ID(), |
|
| 2872 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2873 | + ), |
|
| 2874 | + ); |
|
| 2875 | + if ($meta_value !== null) { |
|
| 2876 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2877 | + } |
|
| 2878 | + return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2879 | + } |
|
| 2880 | + |
|
| 2881 | + |
|
| 2882 | + /** |
|
| 2883 | + * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2884 | + * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2885 | + * You can specify $default is case you haven't found the extra meta |
|
| 2886 | + * |
|
| 2887 | + * @param string $meta_key |
|
| 2888 | + * @param boolean $single |
|
| 2889 | + * @param mixed $default if we don't find anything, what should we return? |
|
| 2890 | + * @return mixed single value if $single; array if ! $single |
|
| 2891 | + * @throws ReflectionException |
|
| 2892 | + * @throws InvalidArgumentException |
|
| 2893 | + * @throws InvalidInterfaceException |
|
| 2894 | + * @throws InvalidDataTypeException |
|
| 2895 | + * @throws EE_Error |
|
| 2896 | + */ |
|
| 2897 | + public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2898 | + { |
|
| 2899 | + if ($single) { |
|
| 2900 | + $result = $this->get_first_related( |
|
| 2901 | + 'Extra_Meta', |
|
| 2902 | + array(array('EXM_key' => $meta_key)) |
|
| 2903 | + ); |
|
| 2904 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2905 | + return $result->value(); |
|
| 2906 | + } |
|
| 2907 | + } else { |
|
| 2908 | + $results = $this->get_many_related( |
|
| 2909 | + 'Extra_Meta', |
|
| 2910 | + array(array('EXM_key' => $meta_key)) |
|
| 2911 | + ); |
|
| 2912 | + if ($results) { |
|
| 2913 | + $values = array(); |
|
| 2914 | + foreach ($results as $result) { |
|
| 2915 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2916 | + $values[ $result->ID() ] = $result->value(); |
|
| 2917 | + } |
|
| 2918 | + } |
|
| 2919 | + return $values; |
|
| 2920 | + } |
|
| 2921 | + } |
|
| 2922 | + // if nothing discovered yet return default. |
|
| 2923 | + return apply_filters( |
|
| 2924 | + 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
| 2925 | + $default, |
|
| 2926 | + $meta_key, |
|
| 2927 | + $single, |
|
| 2928 | + $this |
|
| 2929 | + ); |
|
| 2930 | + } |
|
| 2931 | + |
|
| 2932 | + |
|
| 2933 | + /** |
|
| 2934 | + * Returns a simple array of all the extra meta associated with this model object. |
|
| 2935 | + * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2936 | + * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2937 | + * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2938 | + * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2939 | + * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2940 | + * finally the extra meta's value as each sub-value. (eg |
|
| 2941 | + * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2942 | + * |
|
| 2943 | + * @param boolean $one_of_each_key |
|
| 2944 | + * @return array |
|
| 2945 | + * @throws ReflectionException |
|
| 2946 | + * @throws InvalidArgumentException |
|
| 2947 | + * @throws InvalidInterfaceException |
|
| 2948 | + * @throws InvalidDataTypeException |
|
| 2949 | + * @throws EE_Error |
|
| 2950 | + */ |
|
| 2951 | + public function all_extra_meta_array($one_of_each_key = true) |
|
| 2952 | + { |
|
| 2953 | + $return_array = array(); |
|
| 2954 | + if ($one_of_each_key) { |
|
| 2955 | + $extra_meta_objs = $this->get_many_related( |
|
| 2956 | + 'Extra_Meta', |
|
| 2957 | + array('group_by' => 'EXM_key') |
|
| 2958 | + ); |
|
| 2959 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2960 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2961 | + $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
| 2962 | + } |
|
| 2963 | + } |
|
| 2964 | + } else { |
|
| 2965 | + $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2966 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2967 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2968 | + if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
| 2969 | + $return_array[ $extra_meta_obj->key() ] = array(); |
|
| 2970 | + } |
|
| 2971 | + $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
| 2972 | + } |
|
| 2973 | + } |
|
| 2974 | + } |
|
| 2975 | + return $return_array; |
|
| 2976 | + } |
|
| 2977 | + |
|
| 2978 | + |
|
| 2979 | + /** |
|
| 2980 | + * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2981 | + * |
|
| 2982 | + * @return string |
|
| 2983 | + * @throws ReflectionException |
|
| 2984 | + * @throws InvalidArgumentException |
|
| 2985 | + * @throws InvalidInterfaceException |
|
| 2986 | + * @throws InvalidDataTypeException |
|
| 2987 | + * @throws EE_Error |
|
| 2988 | + */ |
|
| 2989 | + public function name() |
|
| 2990 | + { |
|
| 2991 | + // find a field that's not a text field |
|
| 2992 | + $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2993 | + if ($field_we_can_use) { |
|
| 2994 | + return $this->get($field_we_can_use->get_name()); |
|
| 2995 | + } |
|
| 2996 | + $first_few_properties = $this->model_field_array(); |
|
| 2997 | + $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2998 | + $name_parts = array(); |
|
| 2999 | + foreach ($first_few_properties as $name => $value) { |
|
| 3000 | + $name_parts[] = "$name:$value"; |
|
| 3001 | + } |
|
| 3002 | + return implode(',', $name_parts); |
|
| 3003 | + } |
|
| 3004 | + |
|
| 3005 | + |
|
| 3006 | + /** |
|
| 3007 | + * in_entity_map |
|
| 3008 | + * Checks if this model object has been proven to already be in the entity map |
|
| 3009 | + * |
|
| 3010 | + * @return boolean |
|
| 3011 | + * @throws ReflectionException |
|
| 3012 | + * @throws InvalidArgumentException |
|
| 3013 | + * @throws InvalidInterfaceException |
|
| 3014 | + * @throws InvalidDataTypeException |
|
| 3015 | + * @throws EE_Error |
|
| 3016 | + */ |
|
| 3017 | + public function in_entity_map() |
|
| 3018 | + { |
|
| 3019 | + // well, if we looked, did we find it in the entity map? |
|
| 3020 | + return $this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this; |
|
| 3021 | + } |
|
| 3022 | + |
|
| 3023 | + |
|
| 3024 | + /** |
|
| 3025 | + * refresh_from_db |
|
| 3026 | + * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 3027 | + * |
|
| 3028 | + * @throws ReflectionException |
|
| 3029 | + * @throws InvalidArgumentException |
|
| 3030 | + * @throws InvalidInterfaceException |
|
| 3031 | + * @throws InvalidDataTypeException |
|
| 3032 | + * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 3033 | + * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 3034 | + */ |
|
| 3035 | + public function refresh_from_db() |
|
| 3036 | + { |
|
| 3037 | + if ($this->ID() && $this->in_entity_map()) { |
|
| 3038 | + $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 3039 | + } else { |
|
| 3040 | + // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 3041 | + // if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 3042 | + // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 3043 | + // absolutely nothing in it for this ID |
|
| 3044 | + if (WP_DEBUG) { |
|
| 3045 | + throw new EE_Error( |
|
| 3046 | + sprintf( |
|
| 3047 | + esc_html__( |
|
| 3048 | + 'Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
|
| 3049 | + 'event_espresso' |
|
| 3050 | + ), |
|
| 3051 | + $this->ID(), |
|
| 3052 | + get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 3053 | + get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 3054 | + ) |
|
| 3055 | + ); |
|
| 3056 | + } |
|
| 3057 | + } |
|
| 3058 | + } |
|
| 3059 | + |
|
| 3060 | + |
|
| 3061 | + /** |
|
| 3062 | + * Change $fields' values to $new_value_sql (which is a string of raw SQL) |
|
| 3063 | + * |
|
| 3064 | + * @since 4.9.80.p |
|
| 3065 | + * @param EE_Model_Field_Base[] $fields |
|
| 3066 | + * @param string $new_value_sql |
|
| 3067 | + * example: 'column_name=123', |
|
| 3068 | + * or 'column_name=column_name+1', |
|
| 3069 | + * or 'column_name= CASE |
|
| 3070 | + * WHEN (`column_name` + `other_column` + 5) <= `yet_another_column` |
|
| 3071 | + * THEN `column_name` + 5 |
|
| 3072 | + * ELSE `column_name` |
|
| 3073 | + * END' |
|
| 3074 | + * Also updates $field on this model object with the latest value from the database. |
|
| 3075 | + * @return bool |
|
| 3076 | + * @throws EE_Error |
|
| 3077 | + * @throws InvalidArgumentException |
|
| 3078 | + * @throws InvalidDataTypeException |
|
| 3079 | + * @throws InvalidInterfaceException |
|
| 3080 | + * @throws ReflectionException |
|
| 3081 | + */ |
|
| 3082 | + protected function updateFieldsInDB($fields, $new_value_sql) |
|
| 3083 | + { |
|
| 3084 | + // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
|
| 3085 | + // if it wasn't even there to start off. |
|
| 3086 | + if (! $this->ID()) { |
|
| 3087 | + $this->save(); |
|
| 3088 | + } |
|
| 3089 | + global $wpdb; |
|
| 3090 | + if (empty($fields)) { |
|
| 3091 | + throw new InvalidArgumentException( |
|
| 3092 | + esc_html__( |
|
| 3093 | + 'EE_Base_Class::updateFieldsInDB was passed an empty array of fields.', |
|
| 3094 | + 'event_espresso' |
|
| 3095 | + ) |
|
| 3096 | + ); |
|
| 3097 | + } |
|
| 3098 | + $first_field = reset($fields); |
|
| 3099 | + $table_alias = $first_field->get_table_alias(); |
|
| 3100 | + foreach ($fields as $field) { |
|
| 3101 | + if ($table_alias !== $field->get_table_alias()) { |
|
| 3102 | + throw new InvalidArgumentException( |
|
| 3103 | + sprintf( |
|
| 3104 | + esc_html__( |
|
| 3105 | + // @codingStandardsIgnoreStart |
|
| 3106 | + 'EE_Base_Class::updateFieldsInDB was passed fields for different tables ("%1$s" and "%2$s"), which is not supported. Instead, please call the method multiple times.', |
|
| 3107 | + // @codingStandardsIgnoreEnd |
|
| 3108 | + 'event_espresso' |
|
| 3109 | + ), |
|
| 3110 | + $table_alias, |
|
| 3111 | + $field->get_table_alias() |
|
| 3112 | + ) |
|
| 3113 | + ); |
|
| 3114 | + } |
|
| 3115 | + } |
|
| 3116 | + // Ok the fields are now known to all be for the same table. Proceed with creating the SQL to update it. |
|
| 3117 | + $table_obj = $this->get_model()->get_table_obj_by_alias($table_alias); |
|
| 3118 | + $table_pk_value = $this->ID(); |
|
| 3119 | + $table_name = $table_obj->get_table_name(); |
|
| 3120 | + if ($table_obj instanceof EE_Secondary_Table) { |
|
| 3121 | + $table_pk_field_name = $table_obj->get_fk_on_table(); |
|
| 3122 | + } else { |
|
| 3123 | + $table_pk_field_name = $table_obj->get_pk_column(); |
|
| 3124 | + } |
|
| 3125 | + |
|
| 3126 | + $query = |
|
| 3127 | + "UPDATE `{$table_name}` |
|
| 3128 | 3128 | SET " |
| 3129 | - . $new_value_sql |
|
| 3130 | - . $wpdb->prepare( |
|
| 3131 | - " |
|
| 3129 | + . $new_value_sql |
|
| 3130 | + . $wpdb->prepare( |
|
| 3131 | + " |
|
| 3132 | 3132 | WHERE `{$table_pk_field_name}` = %d;", |
| 3133 | - $table_pk_value |
|
| 3134 | - ); |
|
| 3135 | - $result = $wpdb->query($query); |
|
| 3136 | - foreach ($fields as $field) { |
|
| 3137 | - // If it was successful, we'd like to know the new value. |
|
| 3138 | - // If it failed, we'd also like to know the new value. |
|
| 3139 | - $new_value = $this->get_model()->get_var( |
|
| 3140 | - $this->get_model()->alter_query_params_to_restrict_by_ID( |
|
| 3141 | - $this->get_model()->get_index_primary_key_string( |
|
| 3142 | - $this->model_field_array() |
|
| 3143 | - ), |
|
| 3144 | - array( |
|
| 3145 | - 'default_where_conditions' => 'minimum', |
|
| 3146 | - ) |
|
| 3147 | - ), |
|
| 3148 | - $field->get_name() |
|
| 3149 | - ); |
|
| 3150 | - $this->set_from_db( |
|
| 3151 | - $field->get_name(), |
|
| 3152 | - $new_value |
|
| 3153 | - ); |
|
| 3154 | - } |
|
| 3155 | - return (bool) $result; |
|
| 3156 | - } |
|
| 3157 | - |
|
| 3158 | - |
|
| 3159 | - /** |
|
| 3160 | - * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()). |
|
| 3161 | - * Does not allow negative values, however. |
|
| 3162 | - * |
|
| 3163 | - * @since 4.9.80.p |
|
| 3164 | - * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them |
|
| 3165 | - * (positive or negative). One important gotcha: all these values must be |
|
| 3166 | - * on the same table (eg don't pass in one field for the posts table and |
|
| 3167 | - * another for the event meta table.) |
|
| 3168 | - * @return bool |
|
| 3169 | - * @throws EE_Error |
|
| 3170 | - * @throws InvalidArgumentException |
|
| 3171 | - * @throws InvalidDataTypeException |
|
| 3172 | - * @throws InvalidInterfaceException |
|
| 3173 | - * @throws ReflectionException |
|
| 3174 | - */ |
|
| 3175 | - public function adjustNumericFieldsInDb(array $fields_n_quantities) |
|
| 3176 | - { |
|
| 3177 | - global $wpdb; |
|
| 3178 | - if (empty($fields_n_quantities)) { |
|
| 3179 | - // No fields to update? Well sure, we updated them to that value just fine. |
|
| 3180 | - return true; |
|
| 3181 | - } |
|
| 3182 | - $fields = []; |
|
| 3183 | - $set_sql_statements = []; |
|
| 3184 | - foreach ($fields_n_quantities as $field_name => $quantity) { |
|
| 3185 | - $field = $this->get_model()->field_settings_for($field_name, true); |
|
| 3186 | - $fields[] = $field; |
|
| 3187 | - $column_name = $field->get_table_column(); |
|
| 3188 | - |
|
| 3189 | - $abs_qty = absint($quantity); |
|
| 3190 | - if ($quantity > 0) { |
|
| 3191 | - // don't let the value be negative as often these fields are unsigned |
|
| 3192 | - $set_sql_statements[] = $wpdb->prepare( |
|
| 3193 | - "`{$column_name}` = `{$column_name}` + %d", |
|
| 3194 | - $abs_qty |
|
| 3195 | - ); |
|
| 3196 | - } else { |
|
| 3197 | - $set_sql_statements[] = $wpdb->prepare( |
|
| 3198 | - "`{$column_name}` = CASE |
|
| 3133 | + $table_pk_value |
|
| 3134 | + ); |
|
| 3135 | + $result = $wpdb->query($query); |
|
| 3136 | + foreach ($fields as $field) { |
|
| 3137 | + // If it was successful, we'd like to know the new value. |
|
| 3138 | + // If it failed, we'd also like to know the new value. |
|
| 3139 | + $new_value = $this->get_model()->get_var( |
|
| 3140 | + $this->get_model()->alter_query_params_to_restrict_by_ID( |
|
| 3141 | + $this->get_model()->get_index_primary_key_string( |
|
| 3142 | + $this->model_field_array() |
|
| 3143 | + ), |
|
| 3144 | + array( |
|
| 3145 | + 'default_where_conditions' => 'minimum', |
|
| 3146 | + ) |
|
| 3147 | + ), |
|
| 3148 | + $field->get_name() |
|
| 3149 | + ); |
|
| 3150 | + $this->set_from_db( |
|
| 3151 | + $field->get_name(), |
|
| 3152 | + $new_value |
|
| 3153 | + ); |
|
| 3154 | + } |
|
| 3155 | + return (bool) $result; |
|
| 3156 | + } |
|
| 3157 | + |
|
| 3158 | + |
|
| 3159 | + /** |
|
| 3160 | + * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()). |
|
| 3161 | + * Does not allow negative values, however. |
|
| 3162 | + * |
|
| 3163 | + * @since 4.9.80.p |
|
| 3164 | + * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them |
|
| 3165 | + * (positive or negative). One important gotcha: all these values must be |
|
| 3166 | + * on the same table (eg don't pass in one field for the posts table and |
|
| 3167 | + * another for the event meta table.) |
|
| 3168 | + * @return bool |
|
| 3169 | + * @throws EE_Error |
|
| 3170 | + * @throws InvalidArgumentException |
|
| 3171 | + * @throws InvalidDataTypeException |
|
| 3172 | + * @throws InvalidInterfaceException |
|
| 3173 | + * @throws ReflectionException |
|
| 3174 | + */ |
|
| 3175 | + public function adjustNumericFieldsInDb(array $fields_n_quantities) |
|
| 3176 | + { |
|
| 3177 | + global $wpdb; |
|
| 3178 | + if (empty($fields_n_quantities)) { |
|
| 3179 | + // No fields to update? Well sure, we updated them to that value just fine. |
|
| 3180 | + return true; |
|
| 3181 | + } |
|
| 3182 | + $fields = []; |
|
| 3183 | + $set_sql_statements = []; |
|
| 3184 | + foreach ($fields_n_quantities as $field_name => $quantity) { |
|
| 3185 | + $field = $this->get_model()->field_settings_for($field_name, true); |
|
| 3186 | + $fields[] = $field; |
|
| 3187 | + $column_name = $field->get_table_column(); |
|
| 3188 | + |
|
| 3189 | + $abs_qty = absint($quantity); |
|
| 3190 | + if ($quantity > 0) { |
|
| 3191 | + // don't let the value be negative as often these fields are unsigned |
|
| 3192 | + $set_sql_statements[] = $wpdb->prepare( |
|
| 3193 | + "`{$column_name}` = `{$column_name}` + %d", |
|
| 3194 | + $abs_qty |
|
| 3195 | + ); |
|
| 3196 | + } else { |
|
| 3197 | + $set_sql_statements[] = $wpdb->prepare( |
|
| 3198 | + "`{$column_name}` = CASE |
|
| 3199 | 3199 | WHEN (`{$column_name}` >= %d) |
| 3200 | 3200 | THEN `{$column_name}` - %d |
| 3201 | 3201 | ELSE 0 |
| 3202 | 3202 | END", |
| 3203 | - $abs_qty, |
|
| 3204 | - $abs_qty |
|
| 3205 | - ); |
|
| 3206 | - } |
|
| 3207 | - } |
|
| 3208 | - return $this->updateFieldsInDB( |
|
| 3209 | - $fields, |
|
| 3210 | - implode(', ', $set_sql_statements) |
|
| 3211 | - ); |
|
| 3212 | - } |
|
| 3213 | - |
|
| 3214 | - |
|
| 3215 | - /** |
|
| 3216 | - * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of |
|
| 3217 | - * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value. |
|
| 3218 | - * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold. |
|
| 3219 | - * Returns true if the value was successfully bumped, and updates the value on this model object. |
|
| 3220 | - * Otherwise returns false. |
|
| 3221 | - * |
|
| 3222 | - * @since 4.9.80.p |
|
| 3223 | - * @param string $field_name_to_bump |
|
| 3224 | - * @param string $field_name_affecting_total |
|
| 3225 | - * @param string $limit_field_name |
|
| 3226 | - * @param int $quantity |
|
| 3227 | - * @return bool |
|
| 3228 | - * @throws EE_Error |
|
| 3229 | - * @throws InvalidArgumentException |
|
| 3230 | - * @throws InvalidDataTypeException |
|
| 3231 | - * @throws InvalidInterfaceException |
|
| 3232 | - * @throws ReflectionException |
|
| 3233 | - */ |
|
| 3234 | - public function incrementFieldConditionallyInDb($field_name_to_bump, $field_name_affecting_total, $limit_field_name, $quantity) |
|
| 3235 | - { |
|
| 3236 | - global $wpdb; |
|
| 3237 | - $field = $this->get_model()->field_settings_for($field_name_to_bump, true); |
|
| 3238 | - $column_name = $field->get_table_column(); |
|
| 3239 | - |
|
| 3240 | - $field_affecting_total = $this->get_model()->field_settings_for($field_name_affecting_total, true); |
|
| 3241 | - $column_affecting_total = $field_affecting_total->get_table_column(); |
|
| 3242 | - |
|
| 3243 | - $limiting_field = $this->get_model()->field_settings_for($limit_field_name, true); |
|
| 3244 | - $limiting_column = $limiting_field->get_table_column(); |
|
| 3245 | - return $this->updateFieldsInDB( |
|
| 3246 | - [$field], |
|
| 3247 | - $wpdb->prepare( |
|
| 3248 | - "`{$column_name}` = |
|
| 3203 | + $abs_qty, |
|
| 3204 | + $abs_qty |
|
| 3205 | + ); |
|
| 3206 | + } |
|
| 3207 | + } |
|
| 3208 | + return $this->updateFieldsInDB( |
|
| 3209 | + $fields, |
|
| 3210 | + implode(', ', $set_sql_statements) |
|
| 3211 | + ); |
|
| 3212 | + } |
|
| 3213 | + |
|
| 3214 | + |
|
| 3215 | + /** |
|
| 3216 | + * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of |
|
| 3217 | + * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value. |
|
| 3218 | + * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold. |
|
| 3219 | + * Returns true if the value was successfully bumped, and updates the value on this model object. |
|
| 3220 | + * Otherwise returns false. |
|
| 3221 | + * |
|
| 3222 | + * @since 4.9.80.p |
|
| 3223 | + * @param string $field_name_to_bump |
|
| 3224 | + * @param string $field_name_affecting_total |
|
| 3225 | + * @param string $limit_field_name |
|
| 3226 | + * @param int $quantity |
|
| 3227 | + * @return bool |
|
| 3228 | + * @throws EE_Error |
|
| 3229 | + * @throws InvalidArgumentException |
|
| 3230 | + * @throws InvalidDataTypeException |
|
| 3231 | + * @throws InvalidInterfaceException |
|
| 3232 | + * @throws ReflectionException |
|
| 3233 | + */ |
|
| 3234 | + public function incrementFieldConditionallyInDb($field_name_to_bump, $field_name_affecting_total, $limit_field_name, $quantity) |
|
| 3235 | + { |
|
| 3236 | + global $wpdb; |
|
| 3237 | + $field = $this->get_model()->field_settings_for($field_name_to_bump, true); |
|
| 3238 | + $column_name = $field->get_table_column(); |
|
| 3239 | + |
|
| 3240 | + $field_affecting_total = $this->get_model()->field_settings_for($field_name_affecting_total, true); |
|
| 3241 | + $column_affecting_total = $field_affecting_total->get_table_column(); |
|
| 3242 | + |
|
| 3243 | + $limiting_field = $this->get_model()->field_settings_for($limit_field_name, true); |
|
| 3244 | + $limiting_column = $limiting_field->get_table_column(); |
|
| 3245 | + return $this->updateFieldsInDB( |
|
| 3246 | + [$field], |
|
| 3247 | + $wpdb->prepare( |
|
| 3248 | + "`{$column_name}` = |
|
| 3249 | 3249 | CASE |
| 3250 | 3250 | WHEN ((`{$column_name}` + `{$column_affecting_total}` + %d) <= `{$limiting_column}`) OR `{$limiting_column}` = %d |
| 3251 | 3251 | THEN `{$column_name}` + %d |
| 3252 | 3252 | ELSE `{$column_name}` |
| 3253 | 3253 | END", |
| 3254 | - $quantity, |
|
| 3255 | - EE_INF_IN_DB, |
|
| 3256 | - $quantity |
|
| 3257 | - ) |
|
| 3258 | - ); |
|
| 3259 | - } |
|
| 3260 | - |
|
| 3261 | - |
|
| 3262 | - /** |
|
| 3263 | - * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 3264 | - * (probably a bad assumption they have made, oh well) |
|
| 3265 | - * |
|
| 3266 | - * @return string |
|
| 3267 | - */ |
|
| 3268 | - public function __toString() |
|
| 3269 | - { |
|
| 3270 | - try { |
|
| 3271 | - return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 3272 | - } catch (Exception $e) { |
|
| 3273 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 3274 | - return ''; |
|
| 3275 | - } |
|
| 3276 | - } |
|
| 3277 | - |
|
| 3278 | - |
|
| 3279 | - /** |
|
| 3280 | - * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 3281 | - * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 3282 | - * This means if we have made changes to those related model objects, and want to unserialize |
|
| 3283 | - * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 3284 | - * Instead, those related model objects should be directly serialized and stored. |
|
| 3285 | - * Eg, the following won't work: |
|
| 3286 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3287 | - * $att = $reg->attendee(); |
|
| 3288 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3289 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 3290 | - * //END REQUEST |
|
| 3291 | - * //START NEXT REQUEST |
|
| 3292 | - * $reg = get_option( 'my_option' ); |
|
| 3293 | - * $reg->attendee()->save(); |
|
| 3294 | - * And would need to be replace with: |
|
| 3295 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3296 | - * $att = $reg->attendee(); |
|
| 3297 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3298 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 3299 | - * //END REQUEST |
|
| 3300 | - * //START NEXT REQUEST |
|
| 3301 | - * $att = get_option( 'my_option' ); |
|
| 3302 | - * $att->save(); |
|
| 3303 | - * |
|
| 3304 | - * @return array |
|
| 3305 | - * @throws ReflectionException |
|
| 3306 | - * @throws InvalidArgumentException |
|
| 3307 | - * @throws InvalidInterfaceException |
|
| 3308 | - * @throws InvalidDataTypeException |
|
| 3309 | - * @throws EE_Error |
|
| 3310 | - */ |
|
| 3311 | - public function __sleep() |
|
| 3312 | - { |
|
| 3313 | - $model = $this->get_model(); |
|
| 3314 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 3315 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 3316 | - $classname = 'EE_' . $model->get_this_model_name(); |
|
| 3317 | - if ($this->get_one_from_cache($relation_name) instanceof $classname |
|
| 3318 | - && $this->get_one_from_cache($relation_name)->ID() |
|
| 3319 | - ) { |
|
| 3320 | - $this->clear_cache( |
|
| 3321 | - $relation_name, |
|
| 3322 | - $this->get_one_from_cache($relation_name)->ID() |
|
| 3323 | - ); |
|
| 3324 | - } |
|
| 3325 | - } |
|
| 3326 | - } |
|
| 3327 | - $this->_props_n_values_provided_in_constructor = array(); |
|
| 3328 | - $properties_to_serialize = get_object_vars($this); |
|
| 3329 | - // don't serialize the model. It's big and that risks recursion |
|
| 3330 | - unset($properties_to_serialize['_model']); |
|
| 3331 | - return array_keys($properties_to_serialize); |
|
| 3332 | - } |
|
| 3333 | - |
|
| 3334 | - |
|
| 3335 | - /** |
|
| 3336 | - * restore _props_n_values_provided_in_constructor |
|
| 3337 | - * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 3338 | - * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 3339 | - * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 3340 | - */ |
|
| 3341 | - public function __wakeup() |
|
| 3342 | - { |
|
| 3343 | - $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 3344 | - } |
|
| 3345 | - |
|
| 3346 | - |
|
| 3347 | - /** |
|
| 3348 | - * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
|
| 3349 | - * distinct with the clone host instance are also cloned. |
|
| 3350 | - */ |
|
| 3351 | - public function __clone() |
|
| 3352 | - { |
|
| 3353 | - // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
|
| 3354 | - foreach ($this->_fields as $field => $value) { |
|
| 3355 | - if ($value instanceof DateTime) { |
|
| 3356 | - $this->_fields[ $field ] = clone $value; |
|
| 3357 | - } |
|
| 3358 | - } |
|
| 3359 | - } |
|
| 3254 | + $quantity, |
|
| 3255 | + EE_INF_IN_DB, |
|
| 3256 | + $quantity |
|
| 3257 | + ) |
|
| 3258 | + ); |
|
| 3259 | + } |
|
| 3260 | + |
|
| 3261 | + |
|
| 3262 | + /** |
|
| 3263 | + * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 3264 | + * (probably a bad assumption they have made, oh well) |
|
| 3265 | + * |
|
| 3266 | + * @return string |
|
| 3267 | + */ |
|
| 3268 | + public function __toString() |
|
| 3269 | + { |
|
| 3270 | + try { |
|
| 3271 | + return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 3272 | + } catch (Exception $e) { |
|
| 3273 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 3274 | + return ''; |
|
| 3275 | + } |
|
| 3276 | + } |
|
| 3277 | + |
|
| 3278 | + |
|
| 3279 | + /** |
|
| 3280 | + * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 3281 | + * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 3282 | + * This means if we have made changes to those related model objects, and want to unserialize |
|
| 3283 | + * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 3284 | + * Instead, those related model objects should be directly serialized and stored. |
|
| 3285 | + * Eg, the following won't work: |
|
| 3286 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3287 | + * $att = $reg->attendee(); |
|
| 3288 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3289 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 3290 | + * //END REQUEST |
|
| 3291 | + * //START NEXT REQUEST |
|
| 3292 | + * $reg = get_option( 'my_option' ); |
|
| 3293 | + * $reg->attendee()->save(); |
|
| 3294 | + * And would need to be replace with: |
|
| 3295 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3296 | + * $att = $reg->attendee(); |
|
| 3297 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3298 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 3299 | + * //END REQUEST |
|
| 3300 | + * //START NEXT REQUEST |
|
| 3301 | + * $att = get_option( 'my_option' ); |
|
| 3302 | + * $att->save(); |
|
| 3303 | + * |
|
| 3304 | + * @return array |
|
| 3305 | + * @throws ReflectionException |
|
| 3306 | + * @throws InvalidArgumentException |
|
| 3307 | + * @throws InvalidInterfaceException |
|
| 3308 | + * @throws InvalidDataTypeException |
|
| 3309 | + * @throws EE_Error |
|
| 3310 | + */ |
|
| 3311 | + public function __sleep() |
|
| 3312 | + { |
|
| 3313 | + $model = $this->get_model(); |
|
| 3314 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 3315 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 3316 | + $classname = 'EE_' . $model->get_this_model_name(); |
|
| 3317 | + if ($this->get_one_from_cache($relation_name) instanceof $classname |
|
| 3318 | + && $this->get_one_from_cache($relation_name)->ID() |
|
| 3319 | + ) { |
|
| 3320 | + $this->clear_cache( |
|
| 3321 | + $relation_name, |
|
| 3322 | + $this->get_one_from_cache($relation_name)->ID() |
|
| 3323 | + ); |
|
| 3324 | + } |
|
| 3325 | + } |
|
| 3326 | + } |
|
| 3327 | + $this->_props_n_values_provided_in_constructor = array(); |
|
| 3328 | + $properties_to_serialize = get_object_vars($this); |
|
| 3329 | + // don't serialize the model. It's big and that risks recursion |
|
| 3330 | + unset($properties_to_serialize['_model']); |
|
| 3331 | + return array_keys($properties_to_serialize); |
|
| 3332 | + } |
|
| 3333 | + |
|
| 3334 | + |
|
| 3335 | + /** |
|
| 3336 | + * restore _props_n_values_provided_in_constructor |
|
| 3337 | + * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 3338 | + * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 3339 | + * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 3340 | + */ |
|
| 3341 | + public function __wakeup() |
|
| 3342 | + { |
|
| 3343 | + $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 3344 | + } |
|
| 3345 | + |
|
| 3346 | + |
|
| 3347 | + /** |
|
| 3348 | + * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
|
| 3349 | + * distinct with the clone host instance are also cloned. |
|
| 3350 | + */ |
|
| 3351 | + public function __clone() |
|
| 3352 | + { |
|
| 3353 | + // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
|
| 3354 | + foreach ($this->_fields as $field => $value) { |
|
| 3355 | + if ($value instanceof DateTime) { |
|
| 3356 | + $this->_fields[ $field ] = clone $value; |
|
| 3357 | + } |
|
| 3358 | + } |
|
| 3359 | + } |
|
| 3360 | 3360 | } |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
| 147 | 147 | // verify client code has not passed any invalid field names |
| 148 | 148 | foreach ($fieldValues as $field_name => $field_value) { |
| 149 | - if (! isset($model_fields[ $field_name ])) { |
|
| 149 | + if ( ! isset($model_fields[$field_name])) { |
|
| 150 | 150 | throw new EE_Error( |
| 151 | 151 | sprintf( |
| 152 | 152 | esc_html__( |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | } |
| 162 | 162 | } |
| 163 | 163 | $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
| 164 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
| 164 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 165 | 165 | list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
| 166 | 166 | } else { |
| 167 | 167 | // set default formats for date and time |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | foreach ($model_fields as $fieldName => $field) { |
| 175 | 175 | $this->set_from_db( |
| 176 | 176 | $fieldName, |
| 177 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null |
|
| 177 | + isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null |
|
| 178 | 178 | ); |
| 179 | 179 | } |
| 180 | 180 | } else { |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | foreach ($model_fields as $fieldName => $field) { |
| 184 | 184 | $this->set( |
| 185 | 185 | $fieldName, |
| 186 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null, |
|
| 186 | + isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null, |
|
| 187 | 187 | true |
| 188 | 188 | ); |
| 189 | 189 | } |
@@ -191,15 +191,15 @@ discard block |
||
| 191 | 191 | // remember what values were passed to this constructor |
| 192 | 192 | $this->_props_n_values_provided_in_constructor = $fieldValues; |
| 193 | 193 | // remember in entity mapper |
| 194 | - if (! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 194 | + if ( ! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 195 | 195 | $model->add_to_entity_map($this); |
| 196 | 196 | } |
| 197 | 197 | // setup all the relations |
| 198 | 198 | foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
| 199 | 199 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
| 200 | - $this->_model_relations[ $relation_name ] = null; |
|
| 200 | + $this->_model_relations[$relation_name] = null; |
|
| 201 | 201 | } else { |
| 202 | - $this->_model_relations[ $relation_name ] = array(); |
|
| 202 | + $this->_model_relations[$relation_name] = array(); |
|
| 203 | 203 | } |
| 204 | 204 | } |
| 205 | 205 | /** |
@@ -250,10 +250,10 @@ discard block |
||
| 250 | 250 | */ |
| 251 | 251 | public function get_original($field_name) |
| 252 | 252 | { |
| 253 | - if (isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
| 253 | + if (isset($this->_props_n_values_provided_in_constructor[$field_name]) |
|
| 254 | 254 | && $field_settings = $this->get_model()->field_settings_for($field_name) |
| 255 | 255 | ) { |
| 256 | - return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
| 256 | + return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]); |
|
| 257 | 257 | } |
| 258 | 258 | return null; |
| 259 | 259 | } |
@@ -288,8 +288,8 @@ discard block |
||
| 288 | 288 | { |
| 289 | 289 | // if not using default and nothing has changed, and object has already been setup (has ID), |
| 290 | 290 | // then don't do anything |
| 291 | - if (! $use_default |
|
| 292 | - && $this->_fields[ $field_name ] === $field_value |
|
| 291 | + if ( ! $use_default |
|
| 292 | + && $this->_fields[$field_name] === $field_value |
|
| 293 | 293 | && $this->ID() |
| 294 | 294 | ) { |
| 295 | 295 | return; |
@@ -307,7 +307,7 @@ discard block |
||
| 307 | 307 | $holder_of_value = $field_obj->prepare_for_set($field_value); |
| 308 | 308 | // should the value be null? |
| 309 | 309 | if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
| 310 | - $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
| 310 | + $this->_fields[$field_name] = $field_obj->get_default_value(); |
|
| 311 | 311 | /** |
| 312 | 312 | * To save having to refactor all the models, if a default value is used for a |
| 313 | 313 | * EE_Datetime_Field, and that value is not null nor is it a DateTime |
@@ -317,15 +317,15 @@ discard block |
||
| 317 | 317 | * @since 4.6.10+ |
| 318 | 318 | */ |
| 319 | 319 | if ($field_obj instanceof EE_Datetime_Field |
| 320 | - && $this->_fields[ $field_name ] !== null |
|
| 321 | - && ! $this->_fields[ $field_name ] instanceof DateTime |
|
| 320 | + && $this->_fields[$field_name] !== null |
|
| 321 | + && ! $this->_fields[$field_name] instanceof DateTime |
|
| 322 | 322 | ) { |
| 323 | - empty($this->_fields[ $field_name ]) |
|
| 323 | + empty($this->_fields[$field_name]) |
|
| 324 | 324 | ? $this->set($field_name, time()) |
| 325 | - : $this->set($field_name, $this->_fields[ $field_name ]); |
|
| 325 | + : $this->set($field_name, $this->_fields[$field_name]); |
|
| 326 | 326 | } |
| 327 | 327 | } else { |
| 328 | - $this->_fields[ $field_name ] = $holder_of_value; |
|
| 328 | + $this->_fields[$field_name] = $holder_of_value; |
|
| 329 | 329 | } |
| 330 | 330 | // if we're not in the constructor... |
| 331 | 331 | // now check if what we set was a primary key |
@@ -341,7 +341,7 @@ discard block |
||
| 341 | 341 | $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
| 342 | 342 | $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
| 343 | 343 | foreach ($fields_on_model as $field_obj) { |
| 344 | - if (! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 344 | + if ( ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 345 | 345 | && $field_obj->get_name() !== $field_name |
| 346 | 346 | ) { |
| 347 | 347 | $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
@@ -386,8 +386,8 @@ discard block |
||
| 386 | 386 | */ |
| 387 | 387 | public function getCustomSelect($alias) |
| 388 | 388 | { |
| 389 | - return isset($this->custom_selection_results[ $alias ]) |
|
| 390 | - ? $this->custom_selection_results[ $alias ] |
|
| 389 | + return isset($this->custom_selection_results[$alias]) |
|
| 390 | + ? $this->custom_selection_results[$alias] |
|
| 391 | 391 | : null; |
| 392 | 392 | } |
| 393 | 393 | |
@@ -474,8 +474,8 @@ discard block |
||
| 474 | 474 | foreach ($model_fields as $field_name => $field_obj) { |
| 475 | 475 | if ($field_obj instanceof EE_Datetime_Field) { |
| 476 | 476 | $field_obj->set_timezone($this->_timezone); |
| 477 | - if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
| 478 | - EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone)); |
|
| 477 | + if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) { |
|
| 478 | + EEH_DTT_Helper::setTimezone($this->_fields[$field_name], new DateTimeZone($this->_timezone)); |
|
| 479 | 479 | } |
| 480 | 480 | } |
| 481 | 481 | } |
@@ -533,7 +533,7 @@ discard block |
||
| 533 | 533 | */ |
| 534 | 534 | public function get_format($full = true) |
| 535 | 535 | { |
| 536 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 536 | + return $full ? $this->_dt_frmt.' '.$this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 537 | 537 | } |
| 538 | 538 | |
| 539 | 539 | |
@@ -559,11 +559,11 @@ discard block |
||
| 559 | 559 | public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
| 560 | 560 | { |
| 561 | 561 | // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
| 562 | - if (! $object_to_cache instanceof EE_Base_Class) { |
|
| 562 | + if ( ! $object_to_cache instanceof EE_Base_Class) { |
|
| 563 | 563 | return false; |
| 564 | 564 | } |
| 565 | 565 | // also get "how" the object is related, or throw an error |
| 566 | - if (! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 566 | + if ( ! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 567 | 567 | throw new EE_Error( |
| 568 | 568 | sprintf( |
| 569 | 569 | esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
@@ -577,38 +577,38 @@ discard block |
||
| 577 | 577 | // if it's a "belongs to" relationship, then there's only one related model object |
| 578 | 578 | // eg, if this is a registration, there's only 1 attendee for it |
| 579 | 579 | // so for these model objects just set it to be cached |
| 580 | - $this->_model_relations[ $relationName ] = $object_to_cache; |
|
| 580 | + $this->_model_relations[$relationName] = $object_to_cache; |
|
| 581 | 581 | $return = true; |
| 582 | 582 | } else { |
| 583 | 583 | // otherwise, this is the "many" side of a one to many relationship, |
| 584 | 584 | // so we'll add the object to the array of related objects for that type. |
| 585 | 585 | // eg: if this is an event, there are many registrations for that event, |
| 586 | 586 | // so we cache the registrations in an array |
| 587 | - if (! is_array($this->_model_relations[ $relationName ])) { |
|
| 587 | + if ( ! is_array($this->_model_relations[$relationName])) { |
|
| 588 | 588 | // if for some reason, the cached item is a model object, |
| 589 | 589 | // then stick that in the array, otherwise start with an empty array |
| 590 | - $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] |
|
| 590 | + $this->_model_relations[$relationName] = $this->_model_relations[$relationName] |
|
| 591 | 591 | instanceof |
| 592 | 592 | EE_Base_Class |
| 593 | - ? array($this->_model_relations[ $relationName ]) : array(); |
|
| 593 | + ? array($this->_model_relations[$relationName]) : array(); |
|
| 594 | 594 | } |
| 595 | 595 | // first check for a cache_id which is normally empty |
| 596 | - if (! empty($cache_id)) { |
|
| 596 | + if ( ! empty($cache_id)) { |
|
| 597 | 597 | // if the cache_id exists, then it means we are purposely trying to cache this |
| 598 | 598 | // with a known key that can then be used to retrieve the object later on |
| 599 | - $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
| 599 | + $this->_model_relations[$relationName][$cache_id] = $object_to_cache; |
|
| 600 | 600 | $return = $cache_id; |
| 601 | 601 | } elseif ($object_to_cache->ID()) { |
| 602 | 602 | // OR the cached object originally came from the db, so let's just use it's PK for an ID |
| 603 | - $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
| 603 | + $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache; |
|
| 604 | 604 | $return = $object_to_cache->ID(); |
| 605 | 605 | } else { |
| 606 | 606 | // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
| 607 | - $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
| 607 | + $this->_model_relations[$relationName][] = $object_to_cache; |
|
| 608 | 608 | // move the internal pointer to the end of the array |
| 609 | - end($this->_model_relations[ $relationName ]); |
|
| 609 | + end($this->_model_relations[$relationName]); |
|
| 610 | 610 | // and grab the key so that we can return it |
| 611 | - $return = key($this->_model_relations[ $relationName ]); |
|
| 611 | + $return = key($this->_model_relations[$relationName]); |
|
| 612 | 612 | } |
| 613 | 613 | } |
| 614 | 614 | return $return; |
@@ -634,7 +634,7 @@ discard block |
||
| 634 | 634 | // first make sure this property exists |
| 635 | 635 | $this->get_model()->field_settings_for($fieldname); |
| 636 | 636 | $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
| 637 | - $this->_cached_properties[ $fieldname ][ $cache_type ] = $value; |
|
| 637 | + $this->_cached_properties[$fieldname][$cache_type] = $value; |
|
| 638 | 638 | } |
| 639 | 639 | |
| 640 | 640 | |
@@ -663,9 +663,9 @@ discard block |
||
| 663 | 663 | $model = $this->get_model(); |
| 664 | 664 | $model->field_settings_for($fieldname); |
| 665 | 665 | $cache_type = $pretty ? 'pretty' : 'standard'; |
| 666 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 667 | - if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) { |
|
| 668 | - return $this->_cached_properties[ $fieldname ][ $cache_type ]; |
|
| 666 | + $cache_type .= ! empty($extra_cache_ref) ? '_'.$extra_cache_ref : ''; |
|
| 667 | + if (isset($this->_cached_properties[$fieldname][$cache_type])) { |
|
| 668 | + return $this->_cached_properties[$fieldname][$cache_type]; |
|
| 669 | 669 | } |
| 670 | 670 | $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
| 671 | 671 | $this->_set_cached_property($fieldname, $value, $cache_type); |
@@ -693,12 +693,12 @@ discard block |
||
| 693 | 693 | if ($field_obj instanceof EE_Datetime_Field) { |
| 694 | 694 | $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
| 695 | 695 | } |
| 696 | - if (! isset($this->_fields[ $fieldname ])) { |
|
| 697 | - $this->_fields[ $fieldname ] = null; |
|
| 696 | + if ( ! isset($this->_fields[$fieldname])) { |
|
| 697 | + $this->_fields[$fieldname] = null; |
|
| 698 | 698 | } |
| 699 | 699 | $value = $pretty |
| 700 | - ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref) |
|
| 701 | - : $field_obj->prepare_for_get($this->_fields[ $fieldname ]); |
|
| 700 | + ? $field_obj->prepare_for_pretty_echoing($this->_fields[$fieldname], $extra_cache_ref) |
|
| 701 | + : $field_obj->prepare_for_get($this->_fields[$fieldname]); |
|
| 702 | 702 | return $value; |
| 703 | 703 | } |
| 704 | 704 | |
@@ -756,8 +756,8 @@ discard block |
||
| 756 | 756 | */ |
| 757 | 757 | protected function _clear_cached_property($property_name) |
| 758 | 758 | { |
| 759 | - if (isset($this->_cached_properties[ $property_name ])) { |
|
| 760 | - unset($this->_cached_properties[ $property_name ]); |
|
| 759 | + if (isset($this->_cached_properties[$property_name])) { |
|
| 760 | + unset($this->_cached_properties[$property_name]); |
|
| 761 | 761 | } |
| 762 | 762 | } |
| 763 | 763 | |
@@ -809,7 +809,7 @@ discard block |
||
| 809 | 809 | { |
| 810 | 810 | $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
| 811 | 811 | $index_in_cache = ''; |
| 812 | - if (! $relationship_to_model) { |
|
| 812 | + if ( ! $relationship_to_model) { |
|
| 813 | 813 | throw new EE_Error( |
| 814 | 814 | sprintf( |
| 815 | 815 | esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
@@ -820,21 +820,21 @@ discard block |
||
| 820 | 820 | } |
| 821 | 821 | if ($clear_all) { |
| 822 | 822 | $obj_removed = true; |
| 823 | - $this->_model_relations[ $relationName ] = null; |
|
| 823 | + $this->_model_relations[$relationName] = null; |
|
| 824 | 824 | } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
| 825 | - $obj_removed = $this->_model_relations[ $relationName ]; |
|
| 826 | - $this->_model_relations[ $relationName ] = null; |
|
| 825 | + $obj_removed = $this->_model_relations[$relationName]; |
|
| 826 | + $this->_model_relations[$relationName] = null; |
|
| 827 | 827 | } else { |
| 828 | 828 | if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
| 829 | 829 | && $object_to_remove_or_index_into_array->ID() |
| 830 | 830 | ) { |
| 831 | 831 | $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
| 832 | - if (is_array($this->_model_relations[ $relationName ]) |
|
| 833 | - && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
| 832 | + if (is_array($this->_model_relations[$relationName]) |
|
| 833 | + && ! isset($this->_model_relations[$relationName][$index_in_cache]) |
|
| 834 | 834 | ) { |
| 835 | 835 | $index_found_at = null; |
| 836 | 836 | // find this object in the array even though it has a different key |
| 837 | - foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
| 837 | + foreach ($this->_model_relations[$relationName] as $index => $obj) { |
|
| 838 | 838 | /** @noinspection TypeUnsafeComparisonInspection */ |
| 839 | 839 | if ($obj instanceof EE_Base_Class |
| 840 | 840 | && ( |
@@ -867,9 +867,9 @@ discard block |
||
| 867 | 867 | } |
| 868 | 868 | // supposedly we've found it. But it could just be that the client code |
| 869 | 869 | // provided a bad index/object |
| 870 | - if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
| 871 | - $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
| 872 | - unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
| 870 | + if (isset($this->_model_relations[$relationName][$index_in_cache])) { |
|
| 871 | + $obj_removed = $this->_model_relations[$relationName][$index_in_cache]; |
|
| 872 | + unset($this->_model_relations[$relationName][$index_in_cache]); |
|
| 873 | 873 | } else { |
| 874 | 874 | // that thing was never cached anyways. |
| 875 | 875 | $obj_removed = null; |
@@ -900,7 +900,7 @@ discard block |
||
| 900 | 900 | $current_cache_id = '' |
| 901 | 901 | ) { |
| 902 | 902 | // verify that incoming object is of the correct type |
| 903 | - $obj_class = 'EE_' . $relationName; |
|
| 903 | + $obj_class = 'EE_'.$relationName; |
|
| 904 | 904 | if ($newly_saved_object instanceof $obj_class) { |
| 905 | 905 | /* @type EE_Base_Class $newly_saved_object */ |
| 906 | 906 | // now get the type of relation |
@@ -908,17 +908,17 @@ discard block |
||
| 908 | 908 | // if this is a 1:1 relationship |
| 909 | 909 | if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
| 910 | 910 | // then just replace the cached object with the newly saved object |
| 911 | - $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
| 911 | + $this->_model_relations[$relationName] = $newly_saved_object; |
|
| 912 | 912 | return true; |
| 913 | 913 | // or if it's some kind of sordid feral polyamorous relationship... |
| 914 | 914 | } |
| 915 | - if (is_array($this->_model_relations[ $relationName ]) |
|
| 916 | - && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
| 915 | + if (is_array($this->_model_relations[$relationName]) |
|
| 916 | + && isset($this->_model_relations[$relationName][$current_cache_id]) |
|
| 917 | 917 | ) { |
| 918 | 918 | // then remove the current cached item |
| 919 | - unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
| 919 | + unset($this->_model_relations[$relationName][$current_cache_id]); |
|
| 920 | 920 | // and cache the newly saved object using it's new ID |
| 921 | - $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
| 921 | + $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object; |
|
| 922 | 922 | return true; |
| 923 | 923 | } |
| 924 | 924 | } |
@@ -935,8 +935,8 @@ discard block |
||
| 935 | 935 | */ |
| 936 | 936 | public function get_one_from_cache($relationName) |
| 937 | 937 | { |
| 938 | - $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
| 939 | - ? $this->_model_relations[ $relationName ] |
|
| 938 | + $cached_array_or_object = isset($this->_model_relations[$relationName]) |
|
| 939 | + ? $this->_model_relations[$relationName] |
|
| 940 | 940 | : null; |
| 941 | 941 | if (is_array($cached_array_or_object)) { |
| 942 | 942 | return array_shift($cached_array_or_object); |
@@ -959,7 +959,7 @@ discard block |
||
| 959 | 959 | */ |
| 960 | 960 | public function get_all_from_cache($relationName) |
| 961 | 961 | { |
| 962 | - $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array(); |
|
| 962 | + $objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : array(); |
|
| 963 | 963 | // if the result is not an array, but exists, make it an array |
| 964 | 964 | $objects = is_array($objects) ? $objects : array($objects); |
| 965 | 965 | // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
@@ -1143,7 +1143,7 @@ discard block |
||
| 1143 | 1143 | } else { |
| 1144 | 1144 | $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
| 1145 | 1145 | } |
| 1146 | - $this->_fields[ $field_name ] = $field_value; |
|
| 1146 | + $this->_fields[$field_name] = $field_value; |
|
| 1147 | 1147 | $this->_clear_cached_property($field_name); |
| 1148 | 1148 | } |
| 1149 | 1149 | } |
@@ -1183,9 +1183,9 @@ discard block |
||
| 1183 | 1183 | public function get_raw($field_name) |
| 1184 | 1184 | { |
| 1185 | 1185 | $field_settings = $this->get_model()->field_settings_for($field_name); |
| 1186 | - return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1187 | - ? $this->_fields[ $field_name ]->format('U') |
|
| 1188 | - : $this->_fields[ $field_name ]; |
|
| 1186 | + return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime |
|
| 1187 | + ? $this->_fields[$field_name]->format('U') |
|
| 1188 | + : $this->_fields[$field_name]; |
|
| 1189 | 1189 | } |
| 1190 | 1190 | |
| 1191 | 1191 | |
@@ -1207,7 +1207,7 @@ discard block |
||
| 1207 | 1207 | public function get_DateTime_object($field_name) |
| 1208 | 1208 | { |
| 1209 | 1209 | $field_settings = $this->get_model()->field_settings_for($field_name); |
| 1210 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1210 | + if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
| 1211 | 1211 | EE_Error::add_error( |
| 1212 | 1212 | sprintf( |
| 1213 | 1213 | esc_html__( |
@@ -1222,8 +1222,8 @@ discard block |
||
| 1222 | 1222 | ); |
| 1223 | 1223 | return false; |
| 1224 | 1224 | } |
| 1225 | - return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1226 | - ? clone $this->_fields[ $field_name ] |
|
| 1225 | + return isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime |
|
| 1226 | + ? clone $this->_fields[$field_name] |
|
| 1227 | 1227 | : null; |
| 1228 | 1228 | } |
| 1229 | 1229 | |
@@ -1465,7 +1465,7 @@ discard block |
||
| 1465 | 1465 | */ |
| 1466 | 1466 | public function get_i18n_datetime($field_name, $format = '') |
| 1467 | 1467 | { |
| 1468 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1468 | + $format = empty($format) ? $this->_dt_frmt.' '.$this->_tm_frmt : $format; |
|
| 1469 | 1469 | return date_i18n( |
| 1470 | 1470 | $format, |
| 1471 | 1471 | EEH_DTT_Helper::get_timestamp_with_offset( |
@@ -1577,21 +1577,21 @@ discard block |
||
| 1577 | 1577 | $field->set_time_format($this->_tm_frmt); |
| 1578 | 1578 | switch ($what) { |
| 1579 | 1579 | case 'T': |
| 1580 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time( |
|
| 1580 | + $this->_fields[$fieldname] = $field->prepare_for_set_with_new_time( |
|
| 1581 | 1581 | $datetime_value, |
| 1582 | - $this->_fields[ $fieldname ] |
|
| 1582 | + $this->_fields[$fieldname] |
|
| 1583 | 1583 | ); |
| 1584 | 1584 | $this->_has_changes = true; |
| 1585 | 1585 | break; |
| 1586 | 1586 | case 'D': |
| 1587 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
| 1587 | + $this->_fields[$fieldname] = $field->prepare_for_set_with_new_date( |
|
| 1588 | 1588 | $datetime_value, |
| 1589 | - $this->_fields[ $fieldname ] |
|
| 1589 | + $this->_fields[$fieldname] |
|
| 1590 | 1590 | ); |
| 1591 | 1591 | $this->_has_changes = true; |
| 1592 | 1592 | break; |
| 1593 | 1593 | case 'B': |
| 1594 | - $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
| 1594 | + $this->_fields[$fieldname] = $field->prepare_for_set($datetime_value); |
|
| 1595 | 1595 | $this->_has_changes = true; |
| 1596 | 1596 | break; |
| 1597 | 1597 | } |
@@ -1634,7 +1634,7 @@ discard block |
||
| 1634 | 1634 | $this->set_timezone($timezone); |
| 1635 | 1635 | $fn = (array) $field_name; |
| 1636 | 1636 | $args = array_merge($fn, (array) $args); |
| 1637 | - if (! method_exists($this, $callback)) { |
|
| 1637 | + if ( ! method_exists($this, $callback)) { |
|
| 1638 | 1638 | throw new EE_Error( |
| 1639 | 1639 | sprintf( |
| 1640 | 1640 | esc_html__( |
@@ -1646,7 +1646,7 @@ discard block |
||
| 1646 | 1646 | ); |
| 1647 | 1647 | } |
| 1648 | 1648 | $args = (array) $args; |
| 1649 | - $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1649 | + $return = $prepend.call_user_func_array(array($this, $callback), $args).$append; |
|
| 1650 | 1650 | $this->set_timezone($original_timezone); |
| 1651 | 1651 | return $return; |
| 1652 | 1652 | } |
@@ -1761,8 +1761,8 @@ discard block |
||
| 1761 | 1761 | { |
| 1762 | 1762 | $model = $this->get_model(); |
| 1763 | 1763 | foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
| 1764 | - if (! empty($this->_model_relations[ $relation_name ])) { |
|
| 1765 | - $related_objects = $this->_model_relations[ $relation_name ]; |
|
| 1764 | + if ( ! empty($this->_model_relations[$relation_name])) { |
|
| 1765 | + $related_objects = $this->_model_relations[$relation_name]; |
|
| 1766 | 1766 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
| 1767 | 1767 | // this relation only stores a single model object, not an array |
| 1768 | 1768 | // but let's make it consistent |
@@ -1819,7 +1819,7 @@ discard block |
||
| 1819 | 1819 | $this->set($column, $value); |
| 1820 | 1820 | } |
| 1821 | 1821 | // no changes ? then don't do anything |
| 1822 | - if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1822 | + if ( ! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1823 | 1823 | return 0; |
| 1824 | 1824 | } |
| 1825 | 1825 | /** |
@@ -1829,7 +1829,7 @@ discard block |
||
| 1829 | 1829 | * @param EE_Base_Class $model_object the model object about to be saved. |
| 1830 | 1830 | */ |
| 1831 | 1831 | do_action('AHEE__EE_Base_Class__save__begin', $this); |
| 1832 | - if (! $this->allow_persist()) { |
|
| 1832 | + if ( ! $this->allow_persist()) { |
|
| 1833 | 1833 | return 0; |
| 1834 | 1834 | } |
| 1835 | 1835 | // now get current attribute values |
@@ -1844,10 +1844,10 @@ discard block |
||
| 1844 | 1844 | if ($model->has_primary_key_field()) { |
| 1845 | 1845 | if ($model->get_primary_key_field()->is_auto_increment()) { |
| 1846 | 1846 | // ok check if it's set, if so: update; if not, insert |
| 1847 | - if (! empty($save_cols_n_values[ $model->primary_key_name() ])) { |
|
| 1847 | + if ( ! empty($save_cols_n_values[$model->primary_key_name()])) { |
|
| 1848 | 1848 | $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
| 1849 | 1849 | } else { |
| 1850 | - unset($save_cols_n_values[ $model->primary_key_name() ]); |
|
| 1850 | + unset($save_cols_n_values[$model->primary_key_name()]); |
|
| 1851 | 1851 | $results = $model->insert($save_cols_n_values); |
| 1852 | 1852 | if ($results) { |
| 1853 | 1853 | // if successful, set the primary key |
@@ -1857,7 +1857,7 @@ discard block |
||
| 1857 | 1857 | // will get added to the mapper before we can add this one! |
| 1858 | 1858 | // but if we just avoid using the SET method, all that headache can be avoided |
| 1859 | 1859 | $pk_field_name = $model->primary_key_name(); |
| 1860 | - $this->_fields[ $pk_field_name ] = $results; |
|
| 1860 | + $this->_fields[$pk_field_name] = $results; |
|
| 1861 | 1861 | $this->_clear_cached_property($pk_field_name); |
| 1862 | 1862 | $model->add_to_entity_map($this); |
| 1863 | 1863 | $this->_update_cached_related_model_objs_fks(); |
@@ -1874,8 +1874,8 @@ discard block |
||
| 1874 | 1874 | 'event_espresso' |
| 1875 | 1875 | ), |
| 1876 | 1876 | get_class($this), |
| 1877 | - get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1878 | - get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1877 | + get_class($model).'::instance()->add_to_entity_map()', |
|
| 1878 | + get_class($model).'::instance()->get_one_by_ID()', |
|
| 1879 | 1879 | '<br />' |
| 1880 | 1880 | ) |
| 1881 | 1881 | ); |
@@ -1977,27 +1977,27 @@ discard block |
||
| 1977 | 1977 | public function save_new_cached_related_model_objs() |
| 1978 | 1978 | { |
| 1979 | 1979 | // make sure this has been saved |
| 1980 | - if (! $this->ID()) { |
|
| 1980 | + if ( ! $this->ID()) { |
|
| 1981 | 1981 | $id = $this->save(); |
| 1982 | 1982 | } else { |
| 1983 | 1983 | $id = $this->ID(); |
| 1984 | 1984 | } |
| 1985 | 1985 | // now save all the NEW cached model objects (ie they don't exist in the DB) |
| 1986 | 1986 | foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
| 1987 | - if ($this->_model_relations[ $relationName ]) { |
|
| 1987 | + if ($this->_model_relations[$relationName]) { |
|
| 1988 | 1988 | // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
| 1989 | 1989 | // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
| 1990 | 1990 | /* @var $related_model_obj EE_Base_Class */ |
| 1991 | 1991 | if ($relationObj instanceof EE_Belongs_To_Relation) { |
| 1992 | 1992 | // add a relation to that relation type (which saves the appropriate thing in the process) |
| 1993 | 1993 | // but ONLY if it DOES NOT exist in the DB |
| 1994 | - $related_model_obj = $this->_model_relations[ $relationName ]; |
|
| 1994 | + $related_model_obj = $this->_model_relations[$relationName]; |
|
| 1995 | 1995 | // if( ! $related_model_obj->ID()){ |
| 1996 | 1996 | $this->_add_relation_to($related_model_obj, $relationName); |
| 1997 | 1997 | $related_model_obj->save_new_cached_related_model_objs(); |
| 1998 | 1998 | // } |
| 1999 | 1999 | } else { |
| 2000 | - foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
| 2000 | + foreach ($this->_model_relations[$relationName] as $related_model_obj) { |
|
| 2001 | 2001 | // add a relation to that relation type (which saves the appropriate thing in the process) |
| 2002 | 2002 | // but ONLY if it DOES NOT exist in the DB |
| 2003 | 2003 | // if( ! $related_model_obj->ID()){ |
@@ -2024,7 +2024,7 @@ discard block |
||
| 2024 | 2024 | */ |
| 2025 | 2025 | public function get_model() |
| 2026 | 2026 | { |
| 2027 | - if (! $this->_model) { |
|
| 2027 | + if ( ! $this->_model) { |
|
| 2028 | 2028 | $modelName = self::_get_model_classname(get_class($this)); |
| 2029 | 2029 | $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
| 2030 | 2030 | } else { |
@@ -2049,9 +2049,9 @@ discard block |
||
| 2049 | 2049 | // TODO: will not work for Term_Relationships because they have no PK! |
| 2050 | 2050 | $primary_id_ref = self::_get_primary_key_name($classname); |
| 2051 | 2051 | if (array_key_exists($primary_id_ref, $props_n_values) |
| 2052 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2052 | + && ! empty($props_n_values[$primary_id_ref]) |
|
| 2053 | 2053 | ) { |
| 2054 | - $id = $props_n_values[ $primary_id_ref ]; |
|
| 2054 | + $id = $props_n_values[$primary_id_ref]; |
|
| 2055 | 2055 | return self::_get_model($classname)->get_from_entity_map($id); |
| 2056 | 2056 | } |
| 2057 | 2057 | return false; |
@@ -2085,10 +2085,10 @@ discard block |
||
| 2085 | 2085 | if ($model->has_primary_key_field()) { |
| 2086 | 2086 | $primary_id_ref = self::_get_primary_key_name($classname); |
| 2087 | 2087 | if (array_key_exists($primary_id_ref, $props_n_values) |
| 2088 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2088 | + && ! empty($props_n_values[$primary_id_ref]) |
|
| 2089 | 2089 | ) { |
| 2090 | 2090 | $existing = $model->get_one_by_ID( |
| 2091 | - $props_n_values[ $primary_id_ref ] |
|
| 2091 | + $props_n_values[$primary_id_ref] |
|
| 2092 | 2092 | ); |
| 2093 | 2093 | } |
| 2094 | 2094 | } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
@@ -2100,7 +2100,7 @@ discard block |
||
| 2100 | 2100 | } |
| 2101 | 2101 | if ($existing) { |
| 2102 | 2102 | // set date formats if present before setting values |
| 2103 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
| 2103 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 2104 | 2104 | $existing->set_date_format($date_formats[0]); |
| 2105 | 2105 | $existing->set_time_format($date_formats[1]); |
| 2106 | 2106 | } else { |
@@ -2133,7 +2133,7 @@ discard block |
||
| 2133 | 2133 | protected static function _get_model($classname, $timezone = null) |
| 2134 | 2134 | { |
| 2135 | 2135 | // find model for this class |
| 2136 | - if (! $classname) { |
|
| 2136 | + if ( ! $classname) { |
|
| 2137 | 2137 | throw new EE_Error( |
| 2138 | 2138 | sprintf( |
| 2139 | 2139 | esc_html__( |
@@ -2182,7 +2182,7 @@ discard block |
||
| 2182 | 2182 | if (strpos($model_name, 'EE_') === 0) { |
| 2183 | 2183 | $model_classname = str_replace('EE_', 'EEM_', $model_name); |
| 2184 | 2184 | } else { |
| 2185 | - $model_classname = 'EEM_' . $model_name; |
|
| 2185 | + $model_classname = 'EEM_'.$model_name; |
|
| 2186 | 2186 | } |
| 2187 | 2187 | return $model_classname; |
| 2188 | 2188 | } |
@@ -2201,7 +2201,7 @@ discard block |
||
| 2201 | 2201 | */ |
| 2202 | 2202 | protected static function _get_primary_key_name($classname = null) |
| 2203 | 2203 | { |
| 2204 | - if (! $classname) { |
|
| 2204 | + if ( ! $classname) { |
|
| 2205 | 2205 | throw new EE_Error( |
| 2206 | 2206 | sprintf( |
| 2207 | 2207 | esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
@@ -2231,7 +2231,7 @@ discard block |
||
| 2231 | 2231 | $model = $this->get_model(); |
| 2232 | 2232 | // now that we know the name of the variable, use a variable variable to get its value and return its |
| 2233 | 2233 | if ($model->has_primary_key_field()) { |
| 2234 | - return $this->_fields[ $model->primary_key_name() ]; |
|
| 2234 | + return $this->_fields[$model->primary_key_name()]; |
|
| 2235 | 2235 | } |
| 2236 | 2236 | return $model->get_index_primary_key_string($this->_fields); |
| 2237 | 2237 | } |
@@ -2284,7 +2284,7 @@ discard block |
||
| 2284 | 2284 | } |
| 2285 | 2285 | } else { |
| 2286 | 2286 | // this thing doesn't exist in the DB, so just cache it |
| 2287 | - if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2287 | + if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2288 | 2288 | throw new EE_Error( |
| 2289 | 2289 | sprintf( |
| 2290 | 2290 | esc_html__( |
@@ -2449,7 +2449,7 @@ discard block |
||
| 2449 | 2449 | } else { |
| 2450 | 2450 | // did we already cache the result of this query? |
| 2451 | 2451 | $cached_results = $this->get_all_from_cache($relationName); |
| 2452 | - if (! $cached_results) { |
|
| 2452 | + if ( ! $cached_results) { |
|
| 2453 | 2453 | $related_model_objects = $this->get_model()->get_all_related( |
| 2454 | 2454 | $this, |
| 2455 | 2455 | $relationName, |
@@ -2559,7 +2559,7 @@ discard block |
||
| 2559 | 2559 | } else { |
| 2560 | 2560 | // first, check if we've already cached the result of this query |
| 2561 | 2561 | $cached_result = $this->get_one_from_cache($relationName); |
| 2562 | - if (! $cached_result) { |
|
| 2562 | + if ( ! $cached_result) { |
|
| 2563 | 2563 | $related_model_object = $model->get_first_related( |
| 2564 | 2564 | $this, |
| 2565 | 2565 | $relationName, |
@@ -2583,7 +2583,7 @@ discard block |
||
| 2583 | 2583 | } |
| 2584 | 2584 | // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
| 2585 | 2585 | // just get what's cached on this object |
| 2586 | - if (! $related_model_object) { |
|
| 2586 | + if ( ! $related_model_object) { |
|
| 2587 | 2587 | $related_model_object = $this->get_one_from_cache($relationName); |
| 2588 | 2588 | } |
| 2589 | 2589 | } |
@@ -2665,7 +2665,7 @@ discard block |
||
| 2665 | 2665 | */ |
| 2666 | 2666 | public function is_set($field_name) |
| 2667 | 2667 | { |
| 2668 | - return isset($this->_fields[ $field_name ]); |
|
| 2668 | + return isset($this->_fields[$field_name]); |
|
| 2669 | 2669 | } |
| 2670 | 2670 | |
| 2671 | 2671 | |
@@ -2681,7 +2681,7 @@ discard block |
||
| 2681 | 2681 | { |
| 2682 | 2682 | foreach ((array) $properties as $property_name) { |
| 2683 | 2683 | // first make sure this property exists |
| 2684 | - if (! $this->_fields[ $property_name ]) { |
|
| 2684 | + if ( ! $this->_fields[$property_name]) { |
|
| 2685 | 2685 | throw new EE_Error( |
| 2686 | 2686 | sprintf( |
| 2687 | 2687 | esc_html__( |
@@ -2713,7 +2713,7 @@ discard block |
||
| 2713 | 2713 | $properties = array(); |
| 2714 | 2714 | // remove prepended underscore |
| 2715 | 2715 | foreach ($fields as $field_name => $settings) { |
| 2716 | - $properties[ $field_name ] = $this->get($field_name); |
|
| 2716 | + $properties[$field_name] = $this->get($field_name); |
|
| 2717 | 2717 | } |
| 2718 | 2718 | return $properties; |
| 2719 | 2719 | } |
@@ -2750,7 +2750,7 @@ discard block |
||
| 2750 | 2750 | { |
| 2751 | 2751 | $className = get_class($this); |
| 2752 | 2752 | $tagName = "FHEE__{$className}__{$methodName}"; |
| 2753 | - if (! has_filter($tagName)) { |
|
| 2753 | + if ( ! has_filter($tagName)) { |
|
| 2754 | 2754 | throw new EE_Error( |
| 2755 | 2755 | sprintf( |
| 2756 | 2756 | esc_html__( |
@@ -2795,7 +2795,7 @@ discard block |
||
| 2795 | 2795 | $query_params[0]['EXM_value'] = $meta_value; |
| 2796 | 2796 | } |
| 2797 | 2797 | $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
| 2798 | - if (! $existing_rows_like_that) { |
|
| 2798 | + if ( ! $existing_rows_like_that) { |
|
| 2799 | 2799 | return $this->add_extra_meta($meta_key, $meta_value); |
| 2800 | 2800 | } |
| 2801 | 2801 | foreach ($existing_rows_like_that as $existing_row) { |
@@ -2913,7 +2913,7 @@ discard block |
||
| 2913 | 2913 | $values = array(); |
| 2914 | 2914 | foreach ($results as $result) { |
| 2915 | 2915 | if ($result instanceof EE_Extra_Meta) { |
| 2916 | - $values[ $result->ID() ] = $result->value(); |
|
| 2916 | + $values[$result->ID()] = $result->value(); |
|
| 2917 | 2917 | } |
| 2918 | 2918 | } |
| 2919 | 2919 | return $values; |
@@ -2958,17 +2958,17 @@ discard block |
||
| 2958 | 2958 | ); |
| 2959 | 2959 | foreach ($extra_meta_objs as $extra_meta_obj) { |
| 2960 | 2960 | if ($extra_meta_obj instanceof EE_Extra_Meta) { |
| 2961 | - $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
| 2961 | + $return_array[$extra_meta_obj->key()] = $extra_meta_obj->value(); |
|
| 2962 | 2962 | } |
| 2963 | 2963 | } |
| 2964 | 2964 | } else { |
| 2965 | 2965 | $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
| 2966 | 2966 | foreach ($extra_meta_objs as $extra_meta_obj) { |
| 2967 | 2967 | if ($extra_meta_obj instanceof EE_Extra_Meta) { |
| 2968 | - if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
| 2969 | - $return_array[ $extra_meta_obj->key() ] = array(); |
|
| 2968 | + if ( ! isset($return_array[$extra_meta_obj->key()])) { |
|
| 2969 | + $return_array[$extra_meta_obj->key()] = array(); |
|
| 2970 | 2970 | } |
| 2971 | - $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
| 2971 | + $return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value(); |
|
| 2972 | 2972 | } |
| 2973 | 2973 | } |
| 2974 | 2974 | } |
@@ -3049,8 +3049,8 @@ discard block |
||
| 3049 | 3049 | 'event_espresso' |
| 3050 | 3050 | ), |
| 3051 | 3051 | $this->ID(), |
| 3052 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 3053 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 3052 | + get_class($this->get_model()).'::instance()->add_to_entity_map()', |
|
| 3053 | + get_class($this->get_model()).'::instance()->refresh_entity_map()' |
|
| 3054 | 3054 | ) |
| 3055 | 3055 | ); |
| 3056 | 3056 | } |
@@ -3083,7 +3083,7 @@ discard block |
||
| 3083 | 3083 | { |
| 3084 | 3084 | // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
| 3085 | 3085 | // if it wasn't even there to start off. |
| 3086 | - if (! $this->ID()) { |
|
| 3086 | + if ( ! $this->ID()) { |
|
| 3087 | 3087 | $this->save(); |
| 3088 | 3088 | } |
| 3089 | 3089 | global $wpdb; |
@@ -3313,7 +3313,7 @@ discard block |
||
| 3313 | 3313 | $model = $this->get_model(); |
| 3314 | 3314 | foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
| 3315 | 3315 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
| 3316 | - $classname = 'EE_' . $model->get_this_model_name(); |
|
| 3316 | + $classname = 'EE_'.$model->get_this_model_name(); |
|
| 3317 | 3317 | if ($this->get_one_from_cache($relation_name) instanceof $classname |
| 3318 | 3318 | && $this->get_one_from_cache($relation_name)->ID() |
| 3319 | 3319 | ) { |
@@ -3353,7 +3353,7 @@ discard block |
||
| 3353 | 3353 | // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
| 3354 | 3354 | foreach ($this->_fields as $field => $value) { |
| 3355 | 3355 | if ($value instanceof DateTime) { |
| 3356 | - $this->_fields[ $field ] = clone $value; |
|
| 3356 | + $this->_fields[$field] = clone $value; |
|
| 3357 | 3357 | } |
| 3358 | 3358 | } |
| 3359 | 3359 | } |
@@ -12,276 +12,276 @@ |
||
| 12 | 12 | { |
| 13 | 13 | |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * generates JSON-based linked data for an event |
|
| 17 | - * |
|
| 18 | - * @param EE_Event $event |
|
| 19 | - * @throws EE_Error |
|
| 20 | - */ |
|
| 21 | - public static function add_json_linked_data_for_event(EE_Event $event) |
|
| 22 | - { |
|
| 23 | - // Check we have a valid datetime for the event |
|
| 24 | - if (! $event->primary_datetime() instanceof EE_Datetime) { |
|
| 25 | - return; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - $template_args = array( |
|
| 29 | - 'event_permalink' => '', |
|
| 30 | - 'event_name' => '', |
|
| 31 | - 'event_description' => '', |
|
| 32 | - 'event_start' => '', |
|
| 33 | - 'event_end' => '', |
|
| 34 | - 'currency' => '', |
|
| 35 | - 'event_tickets' => array(), |
|
| 36 | - 'venue_name' => '', |
|
| 37 | - 'venue_url' => '', |
|
| 38 | - 'venue_locality' => '', |
|
| 39 | - 'venue_region' => '', |
|
| 40 | - 'venue_address' => '', |
|
| 41 | - 'event_image' => '', |
|
| 42 | - ); |
|
| 43 | - $template_args['event_permalink'] = $event->get_permalink(); |
|
| 44 | - $template_args['event_name'] = $event->name(); |
|
| 45 | - $template_args['event_description'] = wp_strip_all_tags($event->short_description(200)); |
|
| 46 | - // clone datetime so that date formats don't override those for the original datetime |
|
| 47 | - $primary_datetime = clone $event->primary_datetime(); |
|
| 48 | - $template_args['event_start'] = $primary_datetime->start_date(DateTime::ATOM); |
|
| 49 | - $template_args['event_end'] = $primary_datetime->end_date(DateTime::ATOM); |
|
| 50 | - unset($primary_datetime); |
|
| 51 | - $template_args['currency'] = EE_Registry::instance()->CFG->currency->code; |
|
| 52 | - foreach ($event->tickets() as $original_ticket) { |
|
| 53 | - // clone tickets so that date formats don't override those for the original ticket |
|
| 54 | - $ticket= clone $original_ticket; |
|
| 55 | - $ID = $ticket->ID(); |
|
| 56 | - $template_args['event_tickets'][ $ID ]['start_date'] = $ticket->start_date(DateTime::ATOM, null); |
|
| 57 | - $template_args['event_tickets'][ $ID ]['end_date'] = $ticket->end_date(DateTime::ATOM, null); |
|
| 58 | - $template_args['event_tickets'][ $ID ]['price'] = number_format( |
|
| 59 | - $ticket->price(), |
|
| 60 | - EE_Registry::instance()->CFG->currency->dec_plc, |
|
| 61 | - EE_Registry::instance()->CFG->currency->dec_mrk, |
|
| 62 | - '' |
|
| 63 | - ); |
|
| 64 | - switch ($ticket->ticket_status()) { |
|
| 65 | - case 'TKO': |
|
| 66 | - $availability = 'InStock'; |
|
| 67 | - break; |
|
| 68 | - case 'TKS': |
|
| 69 | - $availability = 'SoldOut'; |
|
| 70 | - break; |
|
| 71 | - default: |
|
| 72 | - $availability = null; |
|
| 73 | - break; |
|
| 74 | - } |
|
| 75 | - $template_args['event_tickets'][ $ID ]['availability'] = $availability; |
|
| 76 | - unset($ticket); |
|
| 77 | - } |
|
| 78 | - $VNU_ID = espresso_venue_id(); |
|
| 79 | - if (! empty($VNU_ID) && ! espresso_is_venue_private($VNU_ID)) { |
|
| 80 | - $venue = EEH_Venue_View::get_venue($VNU_ID); |
|
| 81 | - $template_args['venue_name'] = get_the_title($VNU_ID); |
|
| 82 | - $template_args['venue_url'] = get_permalink($VNU_ID); |
|
| 83 | - $template_args['venue_locality'] = $venue->city(); |
|
| 84 | - $template_args['venue_region'] = $venue->state_name(); |
|
| 85 | - $template_args['venue_address'] = $venue->address(); |
|
| 86 | - } |
|
| 87 | - $template_args['event_image'] = $event->feature_image_url(); |
|
| 88 | - $template_args = apply_filters( |
|
| 89 | - 'FHEE__EEH_Schema__add_json_linked_data_for_event__template_args', |
|
| 90 | - $template_args, |
|
| 91 | - $event, |
|
| 92 | - $VNU_ID |
|
| 93 | - ); |
|
| 94 | - extract($template_args, EXTR_OVERWRITE); |
|
| 95 | - include EE_TEMPLATES . 'json_linked_data_for_event.template.php'; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * location |
|
| 101 | - * The location of the event, organization or action. |
|
| 102 | - * Should include the Venue name AND schema formatted address info |
|
| 103 | - * |
|
| 104 | - * @access public |
|
| 105 | - * @param string $location |
|
| 106 | - * @return string |
|
| 107 | - */ |
|
| 108 | - public static function location($location = null) |
|
| 109 | - { |
|
| 110 | - return ! empty($location) ? '<div itemprop="location" itemscope itemtype="http://schema.org/Place">' |
|
| 111 | - . $location |
|
| 112 | - . '</div>' : ''; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * name |
|
| 119 | - * The name of the Event or Venue. |
|
| 120 | - * |
|
| 121 | - * @access public |
|
| 122 | - * @param string $name |
|
| 123 | - * @return string |
|
| 124 | - */ |
|
| 125 | - public static function name($name = null) |
|
| 126 | - { |
|
| 127 | - return ! empty($name) ? '<span itemprop="name">' . $name . '</span>' : ''; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * streetAddress |
|
| 134 | - * The street address. For example, 1600 Amphitheatre Pkwy. |
|
| 135 | - * |
|
| 136 | - * @access public |
|
| 137 | - * @param EEI_Address $obj_with_address |
|
| 138 | - * @return string |
|
| 139 | - */ |
|
| 140 | - public static function streetAddress(EEI_Address $obj_with_address = null) |
|
| 141 | - { |
|
| 142 | - return $obj_with_address->address() !== null && $obj_with_address->address() !== '' |
|
| 143 | - ? '<span itemprop="streetAddress">' . $obj_with_address->address() . '</span>' : ''; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * postOfficeBoxNumber |
|
| 150 | - * The post office box number for PO box addresses. |
|
| 151 | - * |
|
| 152 | - * @access public |
|
| 153 | - * @param EEI_Address $obj_with_address |
|
| 154 | - * @return string |
|
| 155 | - */ |
|
| 156 | - public static function postOfficeBoxNumber(EEI_Address $obj_with_address = null) |
|
| 157 | - { |
|
| 158 | - // regex check for some form of PO Box or P.O. Box, etc, etc, etc |
|
| 159 | - if (preg_match( |
|
| 160 | - "/^\s*((P(OST)?.?\s*(O(FF(ICE)?)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i", |
|
| 161 | - $obj_with_address->address2() |
|
| 162 | - ) ) { |
|
| 163 | - return $obj_with_address->address2() !== null && $obj_with_address->address2() !== '' |
|
| 164 | - ? '<span itemprop="postOfficeBoxNumber">' . $obj_with_address->address2() . '</span>' : ''; |
|
| 165 | - } else { |
|
| 166 | - return $obj_with_address->address2(); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * addressLocality |
|
| 174 | - * The locality (city, town, etc). For example, Mountain View. |
|
| 175 | - * |
|
| 176 | - * @access public |
|
| 177 | - * @param EEI_Address $obj_with_address |
|
| 178 | - * @return string |
|
| 179 | - */ |
|
| 180 | - public static function addressLocality(EEI_Address $obj_with_address = null) |
|
| 181 | - { |
|
| 182 | - return $obj_with_address->city() !== null && $obj_with_address->city() !== '' |
|
| 183 | - ? '<span itemprop="addressLocality">' . $obj_with_address->city() . '</span>' : ''; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * addressRegion |
|
| 190 | - * The region (state, province, etc). For example, CA. |
|
| 191 | - * |
|
| 192 | - * @access public |
|
| 193 | - * @param EEI_Address $obj_with_address |
|
| 194 | - * @return string |
|
| 195 | - */ |
|
| 196 | - public static function addressRegion(EEI_Address $obj_with_address = null) |
|
| 197 | - { |
|
| 198 | - $state = $obj_with_address->state_name(); |
|
| 199 | - if (! empty($state)) { |
|
| 200 | - return '<span itemprop="addressRegion">' . $state . '</span>'; |
|
| 201 | - } else { |
|
| 202 | - return ''; |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * addressCountry |
|
| 210 | - * The country. For example, USA. You can also provide the two-letter ISO 3166-1 alpha-2 country code. |
|
| 211 | - * |
|
| 212 | - * @access public |
|
| 213 | - * @param EEI_Address $obj_with_address |
|
| 214 | - * @return string |
|
| 215 | - */ |
|
| 216 | - public static function addressCountry(EEI_Address $obj_with_address = null) |
|
| 217 | - { |
|
| 218 | - $country = $obj_with_address->country_name(); |
|
| 219 | - if (! empty($country)) { |
|
| 220 | - return '<span itemprop="addressCountry">' . $country . '</span>'; |
|
| 221 | - } else { |
|
| 222 | - return ''; |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * postalCode |
|
| 230 | - * The postal code. For example, 94043. |
|
| 231 | - * |
|
| 232 | - * @access public |
|
| 233 | - * @param EEI_Address $obj_with_address |
|
| 234 | - * @return string |
|
| 235 | - */ |
|
| 236 | - public static function postalCode(EEI_Address $obj_with_address = null) |
|
| 237 | - { |
|
| 238 | - return $obj_with_address->zip() !== null && $obj_with_address->zip() !== '' ? '<span itemprop="postalCode">' |
|
| 239 | - . $obj_with_address->zip() |
|
| 240 | - . '</span>' : ''; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * telephone |
|
| 247 | - * The telephone number. |
|
| 248 | - * |
|
| 249 | - * @access public |
|
| 250 | - * @param string $phone_nmbr |
|
| 251 | - * @return string |
|
| 252 | - */ |
|
| 253 | - public static function telephone($phone_nmbr = null) |
|
| 254 | - { |
|
| 255 | - return $phone_nmbr !== null && $phone_nmbr !== '' ? '<span itemprop="telephone">' . $phone_nmbr . '</span>' |
|
| 256 | - : ''; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * URL |
|
| 263 | - * URL of the item as a clickable link |
|
| 264 | - * |
|
| 265 | - * @access public |
|
| 266 | - * @param string $url - the URL that the link will resolve to |
|
| 267 | - * @param string $text - the text that will be used for the visible link |
|
| 268 | - * @param array $attributes - array of additional link attributes in attribute_name => value pairs. ie: array( 'title' => 'click here', 'class' => 'link-class' ) |
|
| 269 | - * @return string (link) |
|
| 270 | - */ |
|
| 271 | - public static function url($url = null, $text = null, $attributes = array()) |
|
| 272 | - { |
|
| 273 | - // Check the URL includes a scheme |
|
| 274 | - $parsed_url = parse_url($url); |
|
| 275 | - if (empty($parsed_url['scheme'])) { |
|
| 276 | - $url = 'http://' . ltrim($url, '/'); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - $atts = ''; |
|
| 280 | - foreach ($attributes as $attribute => $value) { |
|
| 281 | - $atts .= ' ' . $attribute . '="' . $value . '"'; |
|
| 282 | - } |
|
| 283 | - $text = $text !== null && $text !== '' ? $text : $url; |
|
| 284 | - return $url !== null && $url !== '' ? '<a itemprop="url" href="' . $url . '"' . $atts . '>' . $text . '</a>' |
|
| 285 | - : ''; |
|
| 286 | - } |
|
| 15 | + /** |
|
| 16 | + * generates JSON-based linked data for an event |
|
| 17 | + * |
|
| 18 | + * @param EE_Event $event |
|
| 19 | + * @throws EE_Error |
|
| 20 | + */ |
|
| 21 | + public static function add_json_linked_data_for_event(EE_Event $event) |
|
| 22 | + { |
|
| 23 | + // Check we have a valid datetime for the event |
|
| 24 | + if (! $event->primary_datetime() instanceof EE_Datetime) { |
|
| 25 | + return; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + $template_args = array( |
|
| 29 | + 'event_permalink' => '', |
|
| 30 | + 'event_name' => '', |
|
| 31 | + 'event_description' => '', |
|
| 32 | + 'event_start' => '', |
|
| 33 | + 'event_end' => '', |
|
| 34 | + 'currency' => '', |
|
| 35 | + 'event_tickets' => array(), |
|
| 36 | + 'venue_name' => '', |
|
| 37 | + 'venue_url' => '', |
|
| 38 | + 'venue_locality' => '', |
|
| 39 | + 'venue_region' => '', |
|
| 40 | + 'venue_address' => '', |
|
| 41 | + 'event_image' => '', |
|
| 42 | + ); |
|
| 43 | + $template_args['event_permalink'] = $event->get_permalink(); |
|
| 44 | + $template_args['event_name'] = $event->name(); |
|
| 45 | + $template_args['event_description'] = wp_strip_all_tags($event->short_description(200)); |
|
| 46 | + // clone datetime so that date formats don't override those for the original datetime |
|
| 47 | + $primary_datetime = clone $event->primary_datetime(); |
|
| 48 | + $template_args['event_start'] = $primary_datetime->start_date(DateTime::ATOM); |
|
| 49 | + $template_args['event_end'] = $primary_datetime->end_date(DateTime::ATOM); |
|
| 50 | + unset($primary_datetime); |
|
| 51 | + $template_args['currency'] = EE_Registry::instance()->CFG->currency->code; |
|
| 52 | + foreach ($event->tickets() as $original_ticket) { |
|
| 53 | + // clone tickets so that date formats don't override those for the original ticket |
|
| 54 | + $ticket= clone $original_ticket; |
|
| 55 | + $ID = $ticket->ID(); |
|
| 56 | + $template_args['event_tickets'][ $ID ]['start_date'] = $ticket->start_date(DateTime::ATOM, null); |
|
| 57 | + $template_args['event_tickets'][ $ID ]['end_date'] = $ticket->end_date(DateTime::ATOM, null); |
|
| 58 | + $template_args['event_tickets'][ $ID ]['price'] = number_format( |
|
| 59 | + $ticket->price(), |
|
| 60 | + EE_Registry::instance()->CFG->currency->dec_plc, |
|
| 61 | + EE_Registry::instance()->CFG->currency->dec_mrk, |
|
| 62 | + '' |
|
| 63 | + ); |
|
| 64 | + switch ($ticket->ticket_status()) { |
|
| 65 | + case 'TKO': |
|
| 66 | + $availability = 'InStock'; |
|
| 67 | + break; |
|
| 68 | + case 'TKS': |
|
| 69 | + $availability = 'SoldOut'; |
|
| 70 | + break; |
|
| 71 | + default: |
|
| 72 | + $availability = null; |
|
| 73 | + break; |
|
| 74 | + } |
|
| 75 | + $template_args['event_tickets'][ $ID ]['availability'] = $availability; |
|
| 76 | + unset($ticket); |
|
| 77 | + } |
|
| 78 | + $VNU_ID = espresso_venue_id(); |
|
| 79 | + if (! empty($VNU_ID) && ! espresso_is_venue_private($VNU_ID)) { |
|
| 80 | + $venue = EEH_Venue_View::get_venue($VNU_ID); |
|
| 81 | + $template_args['venue_name'] = get_the_title($VNU_ID); |
|
| 82 | + $template_args['venue_url'] = get_permalink($VNU_ID); |
|
| 83 | + $template_args['venue_locality'] = $venue->city(); |
|
| 84 | + $template_args['venue_region'] = $venue->state_name(); |
|
| 85 | + $template_args['venue_address'] = $venue->address(); |
|
| 86 | + } |
|
| 87 | + $template_args['event_image'] = $event->feature_image_url(); |
|
| 88 | + $template_args = apply_filters( |
|
| 89 | + 'FHEE__EEH_Schema__add_json_linked_data_for_event__template_args', |
|
| 90 | + $template_args, |
|
| 91 | + $event, |
|
| 92 | + $VNU_ID |
|
| 93 | + ); |
|
| 94 | + extract($template_args, EXTR_OVERWRITE); |
|
| 95 | + include EE_TEMPLATES . 'json_linked_data_for_event.template.php'; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * location |
|
| 101 | + * The location of the event, organization or action. |
|
| 102 | + * Should include the Venue name AND schema formatted address info |
|
| 103 | + * |
|
| 104 | + * @access public |
|
| 105 | + * @param string $location |
|
| 106 | + * @return string |
|
| 107 | + */ |
|
| 108 | + public static function location($location = null) |
|
| 109 | + { |
|
| 110 | + return ! empty($location) ? '<div itemprop="location" itemscope itemtype="http://schema.org/Place">' |
|
| 111 | + . $location |
|
| 112 | + . '</div>' : ''; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * name |
|
| 119 | + * The name of the Event or Venue. |
|
| 120 | + * |
|
| 121 | + * @access public |
|
| 122 | + * @param string $name |
|
| 123 | + * @return string |
|
| 124 | + */ |
|
| 125 | + public static function name($name = null) |
|
| 126 | + { |
|
| 127 | + return ! empty($name) ? '<span itemprop="name">' . $name . '</span>' : ''; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * streetAddress |
|
| 134 | + * The street address. For example, 1600 Amphitheatre Pkwy. |
|
| 135 | + * |
|
| 136 | + * @access public |
|
| 137 | + * @param EEI_Address $obj_with_address |
|
| 138 | + * @return string |
|
| 139 | + */ |
|
| 140 | + public static function streetAddress(EEI_Address $obj_with_address = null) |
|
| 141 | + { |
|
| 142 | + return $obj_with_address->address() !== null && $obj_with_address->address() !== '' |
|
| 143 | + ? '<span itemprop="streetAddress">' . $obj_with_address->address() . '</span>' : ''; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * postOfficeBoxNumber |
|
| 150 | + * The post office box number for PO box addresses. |
|
| 151 | + * |
|
| 152 | + * @access public |
|
| 153 | + * @param EEI_Address $obj_with_address |
|
| 154 | + * @return string |
|
| 155 | + */ |
|
| 156 | + public static function postOfficeBoxNumber(EEI_Address $obj_with_address = null) |
|
| 157 | + { |
|
| 158 | + // regex check for some form of PO Box or P.O. Box, etc, etc, etc |
|
| 159 | + if (preg_match( |
|
| 160 | + "/^\s*((P(OST)?.?\s*(O(FF(ICE)?)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i", |
|
| 161 | + $obj_with_address->address2() |
|
| 162 | + ) ) { |
|
| 163 | + return $obj_with_address->address2() !== null && $obj_with_address->address2() !== '' |
|
| 164 | + ? '<span itemprop="postOfficeBoxNumber">' . $obj_with_address->address2() . '</span>' : ''; |
|
| 165 | + } else { |
|
| 166 | + return $obj_with_address->address2(); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * addressLocality |
|
| 174 | + * The locality (city, town, etc). For example, Mountain View. |
|
| 175 | + * |
|
| 176 | + * @access public |
|
| 177 | + * @param EEI_Address $obj_with_address |
|
| 178 | + * @return string |
|
| 179 | + */ |
|
| 180 | + public static function addressLocality(EEI_Address $obj_with_address = null) |
|
| 181 | + { |
|
| 182 | + return $obj_with_address->city() !== null && $obj_with_address->city() !== '' |
|
| 183 | + ? '<span itemprop="addressLocality">' . $obj_with_address->city() . '</span>' : ''; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * addressRegion |
|
| 190 | + * The region (state, province, etc). For example, CA. |
|
| 191 | + * |
|
| 192 | + * @access public |
|
| 193 | + * @param EEI_Address $obj_with_address |
|
| 194 | + * @return string |
|
| 195 | + */ |
|
| 196 | + public static function addressRegion(EEI_Address $obj_with_address = null) |
|
| 197 | + { |
|
| 198 | + $state = $obj_with_address->state_name(); |
|
| 199 | + if (! empty($state)) { |
|
| 200 | + return '<span itemprop="addressRegion">' . $state . '</span>'; |
|
| 201 | + } else { |
|
| 202 | + return ''; |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * addressCountry |
|
| 210 | + * The country. For example, USA. You can also provide the two-letter ISO 3166-1 alpha-2 country code. |
|
| 211 | + * |
|
| 212 | + * @access public |
|
| 213 | + * @param EEI_Address $obj_with_address |
|
| 214 | + * @return string |
|
| 215 | + */ |
|
| 216 | + public static function addressCountry(EEI_Address $obj_with_address = null) |
|
| 217 | + { |
|
| 218 | + $country = $obj_with_address->country_name(); |
|
| 219 | + if (! empty($country)) { |
|
| 220 | + return '<span itemprop="addressCountry">' . $country . '</span>'; |
|
| 221 | + } else { |
|
| 222 | + return ''; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * postalCode |
|
| 230 | + * The postal code. For example, 94043. |
|
| 231 | + * |
|
| 232 | + * @access public |
|
| 233 | + * @param EEI_Address $obj_with_address |
|
| 234 | + * @return string |
|
| 235 | + */ |
|
| 236 | + public static function postalCode(EEI_Address $obj_with_address = null) |
|
| 237 | + { |
|
| 238 | + return $obj_with_address->zip() !== null && $obj_with_address->zip() !== '' ? '<span itemprop="postalCode">' |
|
| 239 | + . $obj_with_address->zip() |
|
| 240 | + . '</span>' : ''; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * telephone |
|
| 247 | + * The telephone number. |
|
| 248 | + * |
|
| 249 | + * @access public |
|
| 250 | + * @param string $phone_nmbr |
|
| 251 | + * @return string |
|
| 252 | + */ |
|
| 253 | + public static function telephone($phone_nmbr = null) |
|
| 254 | + { |
|
| 255 | + return $phone_nmbr !== null && $phone_nmbr !== '' ? '<span itemprop="telephone">' . $phone_nmbr . '</span>' |
|
| 256 | + : ''; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * URL |
|
| 263 | + * URL of the item as a clickable link |
|
| 264 | + * |
|
| 265 | + * @access public |
|
| 266 | + * @param string $url - the URL that the link will resolve to |
|
| 267 | + * @param string $text - the text that will be used for the visible link |
|
| 268 | + * @param array $attributes - array of additional link attributes in attribute_name => value pairs. ie: array( 'title' => 'click here', 'class' => 'link-class' ) |
|
| 269 | + * @return string (link) |
|
| 270 | + */ |
|
| 271 | + public static function url($url = null, $text = null, $attributes = array()) |
|
| 272 | + { |
|
| 273 | + // Check the URL includes a scheme |
|
| 274 | + $parsed_url = parse_url($url); |
|
| 275 | + if (empty($parsed_url['scheme'])) { |
|
| 276 | + $url = 'http://' . ltrim($url, '/'); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + $atts = ''; |
|
| 280 | + foreach ($attributes as $attribute => $value) { |
|
| 281 | + $atts .= ' ' . $attribute . '="' . $value . '"'; |
|
| 282 | + } |
|
| 283 | + $text = $text !== null && $text !== '' ? $text : $url; |
|
| 284 | + return $url !== null && $url !== '' ? '<a itemprop="url" href="' . $url . '"' . $atts . '>' . $text . '</a>' |
|
| 285 | + : ''; |
|
| 286 | + } |
|
| 287 | 287 | } |
@@ -38,103 +38,103 @@ |
||
| 38 | 38 | * @since 4.0 |
| 39 | 39 | */ |
| 40 | 40 | if (function_exists('espresso_version')) { |
| 41 | - if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | - /** |
|
| 43 | - * espresso_duplicate_plugin_error |
|
| 44 | - * displays if more than one version of EE is activated at the same time |
|
| 45 | - */ |
|
| 46 | - function espresso_duplicate_plugin_error() |
|
| 47 | - { |
|
| 48 | - ?> |
|
| 41 | + if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | + /** |
|
| 43 | + * espresso_duplicate_plugin_error |
|
| 44 | + * displays if more than one version of EE is activated at the same time |
|
| 45 | + */ |
|
| 46 | + function espresso_duplicate_plugin_error() |
|
| 47 | + { |
|
| 48 | + ?> |
|
| 49 | 49 | <div class="error"> |
| 50 | 50 | <p> |
| 51 | 51 | <?php |
| 52 | - echo esc_html__( |
|
| 53 | - 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | - 'event_espresso' |
|
| 55 | - ); ?> |
|
| 52 | + echo esc_html__( |
|
| 53 | + 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | + 'event_espresso' |
|
| 55 | + ); ?> |
|
| 56 | 56 | </p> |
| 57 | 57 | </div> |
| 58 | 58 | <?php |
| 59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 63 | 63 | } else { |
| 64 | - define('EE_MIN_PHP_VER_REQUIRED', '5.4.0'); |
|
| 65 | - if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 66 | - /** |
|
| 67 | - * espresso_minimum_php_version_error |
|
| 68 | - * |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - function espresso_minimum_php_version_error() |
|
| 72 | - { |
|
| 73 | - ?> |
|
| 64 | + define('EE_MIN_PHP_VER_REQUIRED', '5.4.0'); |
|
| 65 | + if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 66 | + /** |
|
| 67 | + * espresso_minimum_php_version_error |
|
| 68 | + * |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + function espresso_minimum_php_version_error() |
|
| 72 | + { |
|
| 73 | + ?> |
|
| 74 | 74 | <div class="error"> |
| 75 | 75 | <p> |
| 76 | 76 | <?php |
| 77 | - printf( |
|
| 78 | - esc_html__( |
|
| 79 | - 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 80 | - 'event_espresso' |
|
| 81 | - ), |
|
| 82 | - EE_MIN_PHP_VER_REQUIRED, |
|
| 83 | - PHP_VERSION, |
|
| 84 | - '<br/>', |
|
| 85 | - '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
| 86 | - ); |
|
| 87 | - ?> |
|
| 77 | + printf( |
|
| 78 | + esc_html__( |
|
| 79 | + 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 80 | + 'event_espresso' |
|
| 81 | + ), |
|
| 82 | + EE_MIN_PHP_VER_REQUIRED, |
|
| 83 | + PHP_VERSION, |
|
| 84 | + '<br/>', |
|
| 85 | + '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
| 86 | + ); |
|
| 87 | + ?> |
|
| 88 | 88 | </p> |
| 89 | 89 | </div> |
| 90 | 90 | <?php |
| 91 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 92 | - } |
|
| 91 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 95 | - } else { |
|
| 96 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 97 | - /** |
|
| 98 | - * espresso_version |
|
| 99 | - * Returns the plugin version |
|
| 100 | - * |
|
| 101 | - * @return string |
|
| 102 | - */ |
|
| 103 | - function espresso_version() |
|
| 104 | - { |
|
| 105 | - return apply_filters('FHEE__espresso__espresso_version', '4.9.83.rc.015'); |
|
| 106 | - } |
|
| 94 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 95 | + } else { |
|
| 96 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 97 | + /** |
|
| 98 | + * espresso_version |
|
| 99 | + * Returns the plugin version |
|
| 100 | + * |
|
| 101 | + * @return string |
|
| 102 | + */ |
|
| 103 | + function espresso_version() |
|
| 104 | + { |
|
| 105 | + return apply_filters('FHEE__espresso__espresso_version', '4.9.83.rc.015'); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * espresso_plugin_activation |
|
| 110 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 111 | - */ |
|
| 112 | - function espresso_plugin_activation() |
|
| 113 | - { |
|
| 114 | - update_option('ee_espresso_activation', true); |
|
| 115 | - } |
|
| 108 | + /** |
|
| 109 | + * espresso_plugin_activation |
|
| 110 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 111 | + */ |
|
| 112 | + function espresso_plugin_activation() |
|
| 113 | + { |
|
| 114 | + update_option('ee_espresso_activation', true); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 117 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 118 | 118 | |
| 119 | - require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
| 120 | - bootstrap_espresso(); |
|
| 121 | - } |
|
| 119 | + require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
| 120 | + bootstrap_espresso(); |
|
| 121 | + } |
|
| 122 | 122 | } |
| 123 | 123 | if (! function_exists('espresso_deactivate_plugin')) { |
| 124 | - /** |
|
| 125 | - * deactivate_plugin |
|
| 126 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 127 | - * |
|
| 128 | - * @access public |
|
| 129 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - function espresso_deactivate_plugin($plugin_basename = '') |
|
| 133 | - { |
|
| 134 | - if (! function_exists('deactivate_plugins')) { |
|
| 135 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 136 | - } |
|
| 137 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
| 138 | - deactivate_plugins($plugin_basename); |
|
| 139 | - } |
|
| 124 | + /** |
|
| 125 | + * deactivate_plugin |
|
| 126 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 127 | + * |
|
| 128 | + * @access public |
|
| 129 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + function espresso_deactivate_plugin($plugin_basename = '') |
|
| 133 | + { |
|
| 134 | + if (! function_exists('deactivate_plugins')) { |
|
| 135 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 136 | + } |
|
| 137 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
| 138 | + deactivate_plugins($plugin_basename); |
|
| 139 | + } |
|
| 140 | 140 | } |
@@ -14,172 +14,172 @@ |
||
| 14 | 14 | class EE_Billing_Attendee_Info_Form extends EE_Billing_Info_Form |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * |
|
| 19 | - * @param EE_Payment_Method $payment_method |
|
| 20 | - * @param array $options_array @see EE_Form_Section_Proper::__construct() |
|
| 21 | - */ |
|
| 22 | - public function __construct(EE_Payment_Method $payment_method, $options_array = array()) |
|
| 23 | - { |
|
| 24 | - $options_array['subsections'] = array_merge( |
|
| 25 | - array( |
|
| 26 | - 'first_name' => new EE_Text_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-fname', 'html_label_text' => __('First Name', 'event_espresso') )), |
|
| 27 | - 'last_name' => new EE_Text_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-lname', 'html_label_text' => __('Last Name', 'event_espresso') )), |
|
| 28 | - 'email' => new EE_Email_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-email', 'html_label_text' => __('Email', 'event_espresso') )), |
|
| 29 | - 'address' => new EE_Text_Input(array( 'html_label_text'=> __('Address', 'event_espresso'), 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-address' )), |
|
| 30 | - 'address2' => new EE_Text_Input(array( 'html_label_text'=> __('Address 2', 'event_espresso'), 'html_class' => 'ee-billing-qstn ee-billing-qstn-address2' )), |
|
| 31 | - 'city' => new EE_Text_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-city', 'html_label_text' => __('City', 'event_espresso') )), |
|
| 32 | - 'state' => apply_filters('FHEE__EE_Billing_Attendee_Info_Form__state_field', new EE_State_Select_Input(null, array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-state', 'html_label_text' => __('State', 'event_espresso') ))), |
|
| 33 | - 'country' => apply_filters('FHEE__EE_Billing_Attendee_Info_Form__country_field', new EE_Country_Select_Input(null, array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-country', 'html_label_text' => __('Country', 'event_espresso') ))), |
|
| 34 | - 'zip' => new EE_Text_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-zip', 'html_label_text' => __('Zip', 'event_espresso') )), |
|
| 35 | - 'phone' => new EE_Text_Input(array( 'html_class' => 'ee-billing-qstn ee-billing-qstn-phone', 'html_label_text' => __('Phone', 'event_espresso') )), |
|
| 36 | - ), |
|
| 37 | - isset($options_array['subsections']) ? $options_array['subsections'] : array() |
|
| 38 | - ); |
|
| 17 | + /** |
|
| 18 | + * |
|
| 19 | + * @param EE_Payment_Method $payment_method |
|
| 20 | + * @param array $options_array @see EE_Form_Section_Proper::__construct() |
|
| 21 | + */ |
|
| 22 | + public function __construct(EE_Payment_Method $payment_method, $options_array = array()) |
|
| 23 | + { |
|
| 24 | + $options_array['subsections'] = array_merge( |
|
| 25 | + array( |
|
| 26 | + 'first_name' => new EE_Text_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-fname', 'html_label_text' => __('First Name', 'event_espresso') )), |
|
| 27 | + 'last_name' => new EE_Text_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-lname', 'html_label_text' => __('Last Name', 'event_espresso') )), |
|
| 28 | + 'email' => new EE_Email_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-email', 'html_label_text' => __('Email', 'event_espresso') )), |
|
| 29 | + 'address' => new EE_Text_Input(array( 'html_label_text'=> __('Address', 'event_espresso'), 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-address' )), |
|
| 30 | + 'address2' => new EE_Text_Input(array( 'html_label_text'=> __('Address 2', 'event_espresso'), 'html_class' => 'ee-billing-qstn ee-billing-qstn-address2' )), |
|
| 31 | + 'city' => new EE_Text_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-city', 'html_label_text' => __('City', 'event_espresso') )), |
|
| 32 | + 'state' => apply_filters('FHEE__EE_Billing_Attendee_Info_Form__state_field', new EE_State_Select_Input(null, array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-state', 'html_label_text' => __('State', 'event_espresso') ))), |
|
| 33 | + 'country' => apply_filters('FHEE__EE_Billing_Attendee_Info_Form__country_field', new EE_Country_Select_Input(null, array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-country', 'html_label_text' => __('Country', 'event_espresso') ))), |
|
| 34 | + 'zip' => new EE_Text_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-zip', 'html_label_text' => __('Zip', 'event_espresso') )), |
|
| 35 | + 'phone' => new EE_Text_Input(array( 'html_class' => 'ee-billing-qstn ee-billing-qstn-phone', 'html_label_text' => __('Phone', 'event_espresso') )), |
|
| 36 | + ), |
|
| 37 | + isset($options_array['subsections']) ? $options_array['subsections'] : array() |
|
| 38 | + ); |
|
| 39 | 39 | |
| 40 | - parent::__construct($payment_method, $options_array); |
|
| 41 | - } |
|
| 40 | + parent::__construct($payment_method, $options_array); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Sets the defaults for the billing form according to the attendee's details |
|
| 45 | - * @param EE_Attendee $attendee |
|
| 46 | - */ |
|
| 47 | - public function populate_from_attendee($attendee) |
|
| 48 | - { |
|
| 49 | - $attendee = EEM_Attendee::instance()->ensure_is_obj($attendee); |
|
| 43 | + /** |
|
| 44 | + * Sets the defaults for the billing form according to the attendee's details |
|
| 45 | + * @param EE_Attendee $attendee |
|
| 46 | + */ |
|
| 47 | + public function populate_from_attendee($attendee) |
|
| 48 | + { |
|
| 49 | + $attendee = EEM_Attendee::instance()->ensure_is_obj($attendee); |
|
| 50 | 50 | |
| 51 | - /** @var $attendee EE_Attendee */ |
|
| 52 | - $this->populate_defaults( |
|
| 53 | - apply_filters( |
|
| 54 | - 'FHEE__EE_Billing_Attendee_Info_Form__populate_from_attendee', |
|
| 55 | - array( |
|
| 56 | - 'first_name'=>$attendee->fname(), |
|
| 57 | - 'last_name'=>$attendee->lname(), |
|
| 58 | - 'email'=>$attendee->email(), |
|
| 59 | - 'address'=>$attendee->address(), |
|
| 60 | - 'address2'=>$attendee->address2(), |
|
| 61 | - 'city'=>$attendee->city(), |
|
| 62 | - 'state'=> $this->getAttendeeStateValueForForm($attendee), |
|
| 63 | - 'country'=> $attendee->country_ID(), |
|
| 64 | - 'zip'=>$attendee->zip(), |
|
| 65 | - 'phone'=>$attendee->phone(), |
|
| 66 | - ), |
|
| 67 | - $attendee, |
|
| 68 | - $this |
|
| 69 | - ) |
|
| 70 | - ); |
|
| 71 | - } |
|
| 51 | + /** @var $attendee EE_Attendee */ |
|
| 52 | + $this->populate_defaults( |
|
| 53 | + apply_filters( |
|
| 54 | + 'FHEE__EE_Billing_Attendee_Info_Form__populate_from_attendee', |
|
| 55 | + array( |
|
| 56 | + 'first_name'=>$attendee->fname(), |
|
| 57 | + 'last_name'=>$attendee->lname(), |
|
| 58 | + 'email'=>$attendee->email(), |
|
| 59 | + 'address'=>$attendee->address(), |
|
| 60 | + 'address2'=>$attendee->address2(), |
|
| 61 | + 'city'=>$attendee->city(), |
|
| 62 | + 'state'=> $this->getAttendeeStateValueForForm($attendee), |
|
| 63 | + 'country'=> $attendee->country_ID(), |
|
| 64 | + 'zip'=>$attendee->zip(), |
|
| 65 | + 'phone'=>$attendee->phone(), |
|
| 66 | + ), |
|
| 67 | + $attendee, |
|
| 68 | + $this |
|
| 69 | + ) |
|
| 70 | + ); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Gets the default value to use for the billing form's state value. |
|
| 75 | - * @since $VID:$ |
|
| 76 | - * @param EE_Attendee $attendee |
|
| 77 | - * @return string |
|
| 78 | - * @throws EE_Error2 |
|
| 79 | - */ |
|
| 80 | - protected function getAttendeeStateValueForForm(EE_Attendee $attendee) |
|
| 81 | - { |
|
| 82 | - // If the state input was removed, just return a blank string. |
|
| 83 | - if (! $this->has_subsection('state')) { |
|
| 84 | - return ''; |
|
| 85 | - } |
|
| 86 | - $state_input = $this->get_input('state', false); |
|
| 87 | - if ($state_input instanceof EE_State_Select_Input) { |
|
| 88 | - $state_field_to_use = $state_input->valueFieldName(); |
|
| 89 | - } else { |
|
| 90 | - $state_field_to_use = 'STA_ID'; |
|
| 91 | - } |
|
| 92 | - switch ($state_field_to_use) { |
|
| 93 | - case 'STA_abbrev': |
|
| 94 | - $state_value = $attendee->state_abbrev(); |
|
| 95 | - break; |
|
| 96 | - case 'STA_name': |
|
| 97 | - $state_value = $attendee->state_name(); |
|
| 98 | - break; |
|
| 99 | - default: |
|
| 100 | - $state_value = $attendee->state_ID(); |
|
| 101 | - } |
|
| 102 | - return $state_value; |
|
| 103 | - } |
|
| 73 | + /** |
|
| 74 | + * Gets the default value to use for the billing form's state value. |
|
| 75 | + * @since $VID:$ |
|
| 76 | + * @param EE_Attendee $attendee |
|
| 77 | + * @return string |
|
| 78 | + * @throws EE_Error2 |
|
| 79 | + */ |
|
| 80 | + protected function getAttendeeStateValueForForm(EE_Attendee $attendee) |
|
| 81 | + { |
|
| 82 | + // If the state input was removed, just return a blank string. |
|
| 83 | + if (! $this->has_subsection('state')) { |
|
| 84 | + return ''; |
|
| 85 | + } |
|
| 86 | + $state_input = $this->get_input('state', false); |
|
| 87 | + if ($state_input instanceof EE_State_Select_Input) { |
|
| 88 | + $state_field_to_use = $state_input->valueFieldName(); |
|
| 89 | + } else { |
|
| 90 | + $state_field_to_use = 'STA_ID'; |
|
| 91 | + } |
|
| 92 | + switch ($state_field_to_use) { |
|
| 93 | + case 'STA_abbrev': |
|
| 94 | + $state_value = $attendee->state_abbrev(); |
|
| 95 | + break; |
|
| 96 | + case 'STA_name': |
|
| 97 | + $state_value = $attendee->state_name(); |
|
| 98 | + break; |
|
| 99 | + default: |
|
| 100 | + $state_value = $attendee->state_ID(); |
|
| 101 | + } |
|
| 102 | + return $state_value; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | 105 | |
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * copy_billing_form_data_to_attendee |
|
| 109 | - * copies info from the billing form to the attendee's details |
|
| 110 | - * @param \EE_Attendee $attendee - the attendee object to copy details to |
|
| 111 | - * @return \EE_Attendee |
|
| 112 | - */ |
|
| 113 | - public function copy_billing_form_data_to_attendee(EE_Attendee $attendee) |
|
| 114 | - { |
|
| 115 | - // grab billing form data |
|
| 116 | - $data = $this->valid_data(); |
|
| 117 | - // copy first_name |
|
| 118 | - if (! empty($data['first_name'])) { |
|
| 119 | - $attendee->set_fname($data['first_name']); |
|
| 120 | - } |
|
| 121 | - // copy last_name |
|
| 122 | - if (! empty($data['last_name'])) { |
|
| 123 | - $attendee->set_lname($data['last_name']); |
|
| 124 | - } |
|
| 125 | - // copy email |
|
| 126 | - if (! empty($data['email'])) { |
|
| 127 | - $attendee->set_email($data['email']); |
|
| 128 | - } |
|
| 129 | - // copy address |
|
| 130 | - if (! empty($data['address'])) { |
|
| 131 | - $attendee->set_address($data['address']); |
|
| 132 | - } |
|
| 133 | - // copy address2 |
|
| 134 | - if (! empty($data['address2'])) { |
|
| 135 | - $attendee->set_address2($data['address2']); |
|
| 136 | - } |
|
| 137 | - // copy city |
|
| 138 | - if (! empty($data['city'])) { |
|
| 139 | - $attendee->set_city($data['city']); |
|
| 140 | - } |
|
| 141 | - // copy state |
|
| 142 | - if (! empty($data['state'])) { |
|
| 143 | - $attendee->set_state($data['state']); |
|
| 144 | - } |
|
| 145 | - // copy country |
|
| 146 | - if (! empty($data['country'])) { |
|
| 147 | - $attendee->set_country($data['country']); |
|
| 148 | - } |
|
| 149 | - // copy zip |
|
| 150 | - if (! empty($data['zip'])) { |
|
| 151 | - $attendee->set_zip($data['zip']); |
|
| 152 | - } |
|
| 153 | - // copy phone |
|
| 154 | - if (! empty($data['phone'])) { |
|
| 155 | - $attendee->set_phone($data['phone']); |
|
| 156 | - } |
|
| 157 | - return $attendee; |
|
| 158 | - } |
|
| 107 | + /** |
|
| 108 | + * copy_billing_form_data_to_attendee |
|
| 109 | + * copies info from the billing form to the attendee's details |
|
| 110 | + * @param \EE_Attendee $attendee - the attendee object to copy details to |
|
| 111 | + * @return \EE_Attendee |
|
| 112 | + */ |
|
| 113 | + public function copy_billing_form_data_to_attendee(EE_Attendee $attendee) |
|
| 114 | + { |
|
| 115 | + // grab billing form data |
|
| 116 | + $data = $this->valid_data(); |
|
| 117 | + // copy first_name |
|
| 118 | + if (! empty($data['first_name'])) { |
|
| 119 | + $attendee->set_fname($data['first_name']); |
|
| 120 | + } |
|
| 121 | + // copy last_name |
|
| 122 | + if (! empty($data['last_name'])) { |
|
| 123 | + $attendee->set_lname($data['last_name']); |
|
| 124 | + } |
|
| 125 | + // copy email |
|
| 126 | + if (! empty($data['email'])) { |
|
| 127 | + $attendee->set_email($data['email']); |
|
| 128 | + } |
|
| 129 | + // copy address |
|
| 130 | + if (! empty($data['address'])) { |
|
| 131 | + $attendee->set_address($data['address']); |
|
| 132 | + } |
|
| 133 | + // copy address2 |
|
| 134 | + if (! empty($data['address2'])) { |
|
| 135 | + $attendee->set_address2($data['address2']); |
|
| 136 | + } |
|
| 137 | + // copy city |
|
| 138 | + if (! empty($data['city'])) { |
|
| 139 | + $attendee->set_city($data['city']); |
|
| 140 | + } |
|
| 141 | + // copy state |
|
| 142 | + if (! empty($data['state'])) { |
|
| 143 | + $attendee->set_state($data['state']); |
|
| 144 | + } |
|
| 145 | + // copy country |
|
| 146 | + if (! empty($data['country'])) { |
|
| 147 | + $attendee->set_country($data['country']); |
|
| 148 | + } |
|
| 149 | + // copy zip |
|
| 150 | + if (! empty($data['zip'])) { |
|
| 151 | + $attendee->set_zip($data['zip']); |
|
| 152 | + } |
|
| 153 | + // copy phone |
|
| 154 | + if (! empty($data['phone'])) { |
|
| 155 | + $attendee->set_phone($data['phone']); |
|
| 156 | + } |
|
| 157 | + return $attendee; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * create_attendee_from_billing_form_data |
|
| 163 | - * uses info from the billing form to create a new attendee |
|
| 164 | - * @return \EE_Attendee |
|
| 165 | - */ |
|
| 166 | - public function create_attendee_from_billing_form_data() |
|
| 167 | - { |
|
| 168 | - // grab billing form data |
|
| 169 | - $data = $this->valid_data(); |
|
| 170 | - return EE_Attendee::new_instance(array( |
|
| 171 | - 'ATT_fname' => ! empty($data['first_name']) ? $data['first_name'] : '', |
|
| 172 | - 'ATT_lname' => ! empty($data['last_name']) ? $data['last_name'] : '', |
|
| 173 | - 'ATT_email' => ! empty($data['email']) ? $data['email'] : '', |
|
| 174 | - 'ATT_address' => ! empty($data['address']) ? $data['address'] : '', |
|
| 175 | - 'ATT_address2' => ! empty($data['address2']) ? $data['address2'] : '', |
|
| 176 | - 'ATT_city' => ! empty($data['city']) ? $data['city'] : '', |
|
| 177 | - 'STA_ID' => ! empty($data['state']) ? $data['state'] : '', |
|
| 178 | - 'CNT_ISO' => ! empty($data['country']) ? $data['country'] : '', |
|
| 179 | - 'ATT_zip' => ! empty($data['zip']) ? $data['zip'] : '', |
|
| 180 | - 'ATT_phone' => ! empty($data['phone']) ? $data['phone'] : '', |
|
| 181 | - )); |
|
| 182 | - } |
|
| 161 | + /** |
|
| 162 | + * create_attendee_from_billing_form_data |
|
| 163 | + * uses info from the billing form to create a new attendee |
|
| 164 | + * @return \EE_Attendee |
|
| 165 | + */ |
|
| 166 | + public function create_attendee_from_billing_form_data() |
|
| 167 | + { |
|
| 168 | + // grab billing form data |
|
| 169 | + $data = $this->valid_data(); |
|
| 170 | + return EE_Attendee::new_instance(array( |
|
| 171 | + 'ATT_fname' => ! empty($data['first_name']) ? $data['first_name'] : '', |
|
| 172 | + 'ATT_lname' => ! empty($data['last_name']) ? $data['last_name'] : '', |
|
| 173 | + 'ATT_email' => ! empty($data['email']) ? $data['email'] : '', |
|
| 174 | + 'ATT_address' => ! empty($data['address']) ? $data['address'] : '', |
|
| 175 | + 'ATT_address2' => ! empty($data['address2']) ? $data['address2'] : '', |
|
| 176 | + 'ATT_city' => ! empty($data['city']) ? $data['city'] : '', |
|
| 177 | + 'STA_ID' => ! empty($data['state']) ? $data['state'] : '', |
|
| 178 | + 'CNT_ISO' => ! empty($data['country']) ? $data['country'] : '', |
|
| 179 | + 'ATT_zip' => ! empty($data['zip']) ? $data['zip'] : '', |
|
| 180 | + 'ATT_phone' => ! empty($data['phone']) ? $data['phone'] : '', |
|
| 181 | + )); |
|
| 182 | + } |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | // End of file EE_Billing_Attendee_Info_Form.form.php |
@@ -23,16 +23,16 @@ discard block |
||
| 23 | 23 | { |
| 24 | 24 | $options_array['subsections'] = array_merge( |
| 25 | 25 | array( |
| 26 | - 'first_name' => new EE_Text_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-fname', 'html_label_text' => __('First Name', 'event_espresso') )), |
|
| 27 | - 'last_name' => new EE_Text_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-lname', 'html_label_text' => __('Last Name', 'event_espresso') )), |
|
| 28 | - 'email' => new EE_Email_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-email', 'html_label_text' => __('Email', 'event_espresso') )), |
|
| 29 | - 'address' => new EE_Text_Input(array( 'html_label_text'=> __('Address', 'event_espresso'), 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-address' )), |
|
| 30 | - 'address2' => new EE_Text_Input(array( 'html_label_text'=> __('Address 2', 'event_espresso'), 'html_class' => 'ee-billing-qstn ee-billing-qstn-address2' )), |
|
| 31 | - 'city' => new EE_Text_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-city', 'html_label_text' => __('City', 'event_espresso') )), |
|
| 32 | - 'state' => apply_filters('FHEE__EE_Billing_Attendee_Info_Form__state_field', new EE_State_Select_Input(null, array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-state', 'html_label_text' => __('State', 'event_espresso') ))), |
|
| 33 | - 'country' => apply_filters('FHEE__EE_Billing_Attendee_Info_Form__country_field', new EE_Country_Select_Input(null, array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-country', 'html_label_text' => __('Country', 'event_espresso') ))), |
|
| 34 | - 'zip' => new EE_Text_Input(array( 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-zip', 'html_label_text' => __('Zip', 'event_espresso') )), |
|
| 35 | - 'phone' => new EE_Text_Input(array( 'html_class' => 'ee-billing-qstn ee-billing-qstn-phone', 'html_label_text' => __('Phone', 'event_espresso') )), |
|
| 26 | + 'first_name' => new EE_Text_Input(array('required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-fname', 'html_label_text' => __('First Name', 'event_espresso'))), |
|
| 27 | + 'last_name' => new EE_Text_Input(array('required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-lname', 'html_label_text' => __('Last Name', 'event_espresso'))), |
|
| 28 | + 'email' => new EE_Email_Input(array('required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-email', 'html_label_text' => __('Email', 'event_espresso'))), |
|
| 29 | + 'address' => new EE_Text_Input(array('html_label_text'=> __('Address', 'event_espresso'), 'required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-address')), |
|
| 30 | + 'address2' => new EE_Text_Input(array('html_label_text'=> __('Address 2', 'event_espresso'), 'html_class' => 'ee-billing-qstn ee-billing-qstn-address2')), |
|
| 31 | + 'city' => new EE_Text_Input(array('required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-city', 'html_label_text' => __('City', 'event_espresso'))), |
|
| 32 | + 'state' => apply_filters('FHEE__EE_Billing_Attendee_Info_Form__state_field', new EE_State_Select_Input(null, array('required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-state', 'html_label_text' => __('State', 'event_espresso')))), |
|
| 33 | + 'country' => apply_filters('FHEE__EE_Billing_Attendee_Info_Form__country_field', new EE_Country_Select_Input(null, array('required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-country', 'html_label_text' => __('Country', 'event_espresso')))), |
|
| 34 | + 'zip' => new EE_Text_Input(array('required'=>true, 'html_class' => 'ee-billing-qstn ee-billing-qstn-zip', 'html_label_text' => __('Zip', 'event_espresso'))), |
|
| 35 | + 'phone' => new EE_Text_Input(array('html_class' => 'ee-billing-qstn ee-billing-qstn-phone', 'html_label_text' => __('Phone', 'event_espresso'))), |
|
| 36 | 36 | ), |
| 37 | 37 | isset($options_array['subsections']) ? $options_array['subsections'] : array() |
| 38 | 38 | ); |
@@ -80,12 +80,12 @@ discard block |
||
| 80 | 80 | protected function getAttendeeStateValueForForm(EE_Attendee $attendee) |
| 81 | 81 | { |
| 82 | 82 | // If the state input was removed, just return a blank string. |
| 83 | - if (! $this->has_subsection('state')) { |
|
| 83 | + if ( ! $this->has_subsection('state')) { |
|
| 84 | 84 | return ''; |
| 85 | 85 | } |
| 86 | - $state_input = $this->get_input('state', false); |
|
| 86 | + $state_input = $this->get_input('state', false); |
|
| 87 | 87 | if ($state_input instanceof EE_State_Select_Input) { |
| 88 | - $state_field_to_use = $state_input->valueFieldName(); |
|
| 88 | + $state_field_to_use = $state_input->valueFieldName(); |
|
| 89 | 89 | } else { |
| 90 | 90 | $state_field_to_use = 'STA_ID'; |
| 91 | 91 | } |
@@ -115,43 +115,43 @@ discard block |
||
| 115 | 115 | // grab billing form data |
| 116 | 116 | $data = $this->valid_data(); |
| 117 | 117 | // copy first_name |
| 118 | - if (! empty($data['first_name'])) { |
|
| 118 | + if ( ! empty($data['first_name'])) { |
|
| 119 | 119 | $attendee->set_fname($data['first_name']); |
| 120 | 120 | } |
| 121 | 121 | // copy last_name |
| 122 | - if (! empty($data['last_name'])) { |
|
| 122 | + if ( ! empty($data['last_name'])) { |
|
| 123 | 123 | $attendee->set_lname($data['last_name']); |
| 124 | 124 | } |
| 125 | 125 | // copy email |
| 126 | - if (! empty($data['email'])) { |
|
| 126 | + if ( ! empty($data['email'])) { |
|
| 127 | 127 | $attendee->set_email($data['email']); |
| 128 | 128 | } |
| 129 | 129 | // copy address |
| 130 | - if (! empty($data['address'])) { |
|
| 130 | + if ( ! empty($data['address'])) { |
|
| 131 | 131 | $attendee->set_address($data['address']); |
| 132 | 132 | } |
| 133 | 133 | // copy address2 |
| 134 | - if (! empty($data['address2'])) { |
|
| 134 | + if ( ! empty($data['address2'])) { |
|
| 135 | 135 | $attendee->set_address2($data['address2']); |
| 136 | 136 | } |
| 137 | 137 | // copy city |
| 138 | - if (! empty($data['city'])) { |
|
| 138 | + if ( ! empty($data['city'])) { |
|
| 139 | 139 | $attendee->set_city($data['city']); |
| 140 | 140 | } |
| 141 | 141 | // copy state |
| 142 | - if (! empty($data['state'])) { |
|
| 142 | + if ( ! empty($data['state'])) { |
|
| 143 | 143 | $attendee->set_state($data['state']); |
| 144 | 144 | } |
| 145 | 145 | // copy country |
| 146 | - if (! empty($data['country'])) { |
|
| 146 | + if ( ! empty($data['country'])) { |
|
| 147 | 147 | $attendee->set_country($data['country']); |
| 148 | 148 | } |
| 149 | 149 | // copy zip |
| 150 | - if (! empty($data['zip'])) { |
|
| 150 | + if ( ! empty($data['zip'])) { |
|
| 151 | 151 | $attendee->set_zip($data['zip']); |
| 152 | 152 | } |
| 153 | 153 | // copy phone |
| 154 | - if (! empty($data['phone'])) { |
|
| 154 | + if ( ! empty($data['phone'])) { |
|
| 155 | 155 | $attendee->set_phone($data['phone']); |
| 156 | 156 | } |
| 157 | 157 | return $attendee; |