@@ -26,160 +26,160 @@ |
||
| 26 | 26 | |
| 27 | 27 | class EE_DMS_4_1_0_checkins extends EE_Data_Migration_Script_Stage_Table |
| 28 | 28 | { |
| 29 | - private $_new_table; |
|
| 30 | - public function __construct() |
|
| 31 | - { |
|
| 32 | - global $wpdb; |
|
| 33 | - $this->_pretty_name = esc_html__('Checkins', 'event_espresso'); |
|
| 34 | - $this->_old_table = $wpdb->prefix."events_attendee"; |
|
| 35 | - $this->select_expression = 'att.*, e.event_status'; |
|
| 36 | - $this->_extra_where_sql = 'AS att |
|
| 29 | + private $_new_table; |
|
| 30 | + public function __construct() |
|
| 31 | + { |
|
| 32 | + global $wpdb; |
|
| 33 | + $this->_pretty_name = esc_html__('Checkins', 'event_espresso'); |
|
| 34 | + $this->_old_table = $wpdb->prefix."events_attendee"; |
|
| 35 | + $this->select_expression = 'att.*, e.event_status'; |
|
| 36 | + $this->_extra_where_sql = 'AS att |
|
| 37 | 37 | INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id=e.id |
| 38 | 38 | WHERE e.event_status!="D"'; |
| 39 | - $this->_new_table = $wpdb->prefix."esp_checkin"; |
|
| 40 | - parent::__construct(); |
|
| 41 | - } |
|
| 42 | - protected function _migrate_old_row($old_row) |
|
| 43 | - { |
|
| 44 | - global $wpdb; |
|
| 45 | - $new_reg_table = $wpdb->prefix."esp_registration"; |
|
| 39 | + $this->_new_table = $wpdb->prefix."esp_checkin"; |
|
| 40 | + parent::__construct(); |
|
| 41 | + } |
|
| 42 | + protected function _migrate_old_row($old_row) |
|
| 43 | + { |
|
| 44 | + global $wpdb; |
|
| 45 | + $new_reg_table = $wpdb->prefix."esp_registration"; |
|
| 46 | 46 | |
| 47 | - $num_to_checkin_at_this_time = max(array(intval($old_row['checked_in_quantity']),intval($old_row['checked_in']))) ; |
|
| 47 | + $num_to_checkin_at_this_time = max(array(intval($old_row['checked_in_quantity']),intval($old_row['checked_in']))) ; |
|
| 48 | 48 | |
| 49 | - $new_registrations_for_attendee = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $new_reg_table); |
|
| 50 | - if (! $new_registrations_for_attendee) { |
|
| 51 | - $new_registrations_for_attendee = array(); |
|
| 52 | - } |
|
| 53 | - $new_datetime = $this->_try_to_find_datetime($old_row); |
|
| 49 | + $new_registrations_for_attendee = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $new_reg_table); |
|
| 50 | + if (! $new_registrations_for_attendee) { |
|
| 51 | + $new_registrations_for_attendee = array(); |
|
| 52 | + } |
|
| 53 | + $new_datetime = $this->_try_to_find_datetime($old_row); |
|
| 54 | 54 | |
| 55 | - // make sure registrations array is numerically indexed starting at 0 (it probably already is) |
|
| 56 | - $new_registrations_for_attendee = array_values($new_registrations_for_attendee); |
|
| 57 | - $new_checkin_ids = array(); |
|
| 58 | - for ($i = 0; $i<abs($num_to_checkin_at_this_time); $i++) { |
|
| 59 | - $new_reg_id = $new_registrations_for_attendee[ $i ]; |
|
| 60 | - if (! $new_reg_id) { |
|
| 61 | - $this->add_error(sprintf( |
|
| 62 | - esc_html__( |
|
| 63 | - /* translators: %1$s database row represented in JSON, %2$s number of registrations to check-in |
|
| 55 | + // make sure registrations array is numerically indexed starting at 0 (it probably already is) |
|
| 56 | + $new_registrations_for_attendee = array_values($new_registrations_for_attendee); |
|
| 57 | + $new_checkin_ids = array(); |
|
| 58 | + for ($i = 0; $i<abs($num_to_checkin_at_this_time); $i++) { |
|
| 59 | + $new_reg_id = $new_registrations_for_attendee[ $i ]; |
|
| 60 | + if (! $new_reg_id) { |
|
| 61 | + $this->add_error(sprintf( |
|
| 62 | + esc_html__( |
|
| 63 | + /* translators: %1$s database row represented in JSON, %2$s number of registrations to check-in |
|
| 64 | 64 | * %3$s number of registrations for the attendee, %4$s new registration rows represented in JSON |
| 65 | 65 | */ |
| 66 | - // @codingStandardsIgnoreStart |
|
| 67 | - 'It appears we wanted to check-in more registrations than actually exist. The old attendee record (%1$s) indicated we should check-in %2$d registrations, but there are only %3$d registrations for that attendee (%4$s)', |
|
| 68 | - // @codingStandardsIgnoreEnd |
|
| 69 | - 'event_espresso' |
|
| 70 | - ), |
|
| 71 | - $this->_json_encode($old_row), |
|
| 72 | - abs($num_to_checkin_at_this_time), |
|
| 73 | - count($new_registrations_for_attendee), |
|
| 74 | - $this->_json_encode($new_registrations_for_attendee) |
|
| 75 | - )); |
|
| 76 | - break; |
|
| 77 | - } |
|
| 78 | - $existing_checkin_record = $wpdb->get_var( |
|
| 79 | - $wpdb->prepare( |
|
| 80 | - "SELECT CHK_ID FROM $this->_new_table WHERE REG_ID = %d ORDER BY CHK_ID DESC LIMIT 1", |
|
| 81 | - $new_reg_id |
|
| 82 | - ) |
|
| 83 | - ); |
|
| 84 | - if (! $existing_checkin_record) { |
|
| 85 | - $new_id = $this->_insert_checkin_record($new_reg_id, $new_datetime); |
|
| 86 | - if ($new_id) { |
|
| 87 | - $new_checkin_ids[]= $new_id; |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - if ($new_checkin_ids) { |
|
| 92 | - $this->get_migration_script()->set_mapping( |
|
| 93 | - $this->_old_table, |
|
| 94 | - $old_row['id'], |
|
| 95 | - $this->_new_table, |
|
| 96 | - $new_checkin_ids |
|
| 97 | - ); |
|
| 98 | - } |
|
| 99 | - } |
|
| 66 | + // @codingStandardsIgnoreStart |
|
| 67 | + 'It appears we wanted to check-in more registrations than actually exist. The old attendee record (%1$s) indicated we should check-in %2$d registrations, but there are only %3$d registrations for that attendee (%4$s)', |
|
| 68 | + // @codingStandardsIgnoreEnd |
|
| 69 | + 'event_espresso' |
|
| 70 | + ), |
|
| 71 | + $this->_json_encode($old_row), |
|
| 72 | + abs($num_to_checkin_at_this_time), |
|
| 73 | + count($new_registrations_for_attendee), |
|
| 74 | + $this->_json_encode($new_registrations_for_attendee) |
|
| 75 | + )); |
|
| 76 | + break; |
|
| 77 | + } |
|
| 78 | + $existing_checkin_record = $wpdb->get_var( |
|
| 79 | + $wpdb->prepare( |
|
| 80 | + "SELECT CHK_ID FROM $this->_new_table WHERE REG_ID = %d ORDER BY CHK_ID DESC LIMIT 1", |
|
| 81 | + $new_reg_id |
|
| 82 | + ) |
|
| 83 | + ); |
|
| 84 | + if (! $existing_checkin_record) { |
|
| 85 | + $new_id = $this->_insert_checkin_record($new_reg_id, $new_datetime); |
|
| 86 | + if ($new_id) { |
|
| 87 | + $new_checkin_ids[]= $new_id; |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + if ($new_checkin_ids) { |
|
| 92 | + $this->get_migration_script()->set_mapping( |
|
| 93 | + $this->_old_table, |
|
| 94 | + $old_row['id'], |
|
| 95 | + $this->_new_table, |
|
| 96 | + $new_checkin_ids |
|
| 97 | + ); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Tries to find the new datetime the Check-in was for, based on the attendee row |
|
| 104 | - * (because we know the attendee was for an event as a specific time, and we know |
|
| 105 | - * the event's OLD ID...) |
|
| 106 | - * @global type $wpdb |
|
| 107 | - * @param array $old_attendee_row |
|
| 108 | - * @return array row of datetime from DB |
|
| 109 | - */ |
|
| 110 | - private function _try_to_find_datetime($old_attendee) |
|
| 111 | - { |
|
| 112 | - global $wpdb; |
|
| 102 | + /** |
|
| 103 | + * Tries to find the new datetime the Check-in was for, based on the attendee row |
|
| 104 | + * (because we know the attendee was for an event as a specific time, and we know |
|
| 105 | + * the event's OLD ID...) |
|
| 106 | + * @global type $wpdb |
|
| 107 | + * @param array $old_attendee_row |
|
| 108 | + * @return array row of datetime from DB |
|
| 109 | + */ |
|
| 110 | + private function _try_to_find_datetime($old_attendee) |
|
| 111 | + { |
|
| 112 | + global $wpdb; |
|
| 113 | 113 | |
| 114 | - $new_event_id = $this->get_migration_script()->get_mapping_new_pk($wpdb->prefix."events_detail", $old_attendee['event_id'], $wpdb->posts); |
|
| 115 | - if (! $new_event_id) { |
|
| 116 | - $this->add_error( |
|
| 117 | - sprintf( |
|
| 118 | - esc_html__( |
|
| 119 | - /* translators: 1: original event ID, 2: original attendee database row */ |
|
| 120 | - // @codingStandardsIgnoreStart |
|
| 121 | - 'Could not find new event ID with old event ID %1$d, on attendee row %2$s; and because of that couldn\'t find the correct datetime for Check-in', |
|
| 122 | - // @codingStandardsIgnoreEnd |
|
| 123 | - 'event_espresso' |
|
| 124 | - ), |
|
| 125 | - $old_attendee['event_id'], |
|
| 126 | - $this->_json_encode($old_attendee) |
|
| 127 | - ) |
|
| 128 | - ); |
|
| 129 | - return 0; |
|
| 130 | - } |
|
| 131 | - $old_att_start_date = $old_attendee['start_date']; |
|
| 132 | - $old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']); |
|
| 133 | - $old_att_datetime = $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, "$old_att_start_date $old_att_start_time:00"); |
|
| 114 | + $new_event_id = $this->get_migration_script()->get_mapping_new_pk($wpdb->prefix."events_detail", $old_attendee['event_id'], $wpdb->posts); |
|
| 115 | + if (! $new_event_id) { |
|
| 116 | + $this->add_error( |
|
| 117 | + sprintf( |
|
| 118 | + esc_html__( |
|
| 119 | + /* translators: 1: original event ID, 2: original attendee database row */ |
|
| 120 | + // @codingStandardsIgnoreStart |
|
| 121 | + 'Could not find new event ID with old event ID %1$d, on attendee row %2$s; and because of that couldn\'t find the correct datetime for Check-in', |
|
| 122 | + // @codingStandardsIgnoreEnd |
|
| 123 | + 'event_espresso' |
|
| 124 | + ), |
|
| 125 | + $old_attendee['event_id'], |
|
| 126 | + $this->_json_encode($old_attendee) |
|
| 127 | + ) |
|
| 128 | + ); |
|
| 129 | + return 0; |
|
| 130 | + } |
|
| 131 | + $old_att_start_date = $old_attendee['start_date']; |
|
| 132 | + $old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']); |
|
| 133 | + $old_att_datetime = $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, "$old_att_start_date $old_att_start_time:00"); |
|
| 134 | 134 | |
| 135 | - $datetime_table = $wpdb->prefix."esp_datetime"; |
|
| 136 | - // add all conditions to an array from which we can SHIFT conditions off in order to widen our search |
|
| 137 | - // the most important condition should be last, as it will be array_shift'ed off last |
|
| 138 | - $conditions = array( |
|
| 139 | - $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match? |
|
| 140 | - $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match? |
|
| 141 | - ); |
|
| 142 | - // start running queries, widening search each time by removing a condition |
|
| 143 | - $datetime_found = null; |
|
| 144 | - do { |
|
| 145 | - $full_query = "SELECT * FROM $datetime_table WHERE ".implode(" AND ", $conditions)." LIMIT 1"; |
|
| 146 | - $datetime_found = $wpdb->get_row($full_query, ARRAY_A); |
|
| 147 | - array_shift($conditions); |
|
| 148 | - } while (! $datetime_found && $conditions); |
|
| 149 | - return $datetime_found; |
|
| 150 | - } |
|
| 135 | + $datetime_table = $wpdb->prefix."esp_datetime"; |
|
| 136 | + // add all conditions to an array from which we can SHIFT conditions off in order to widen our search |
|
| 137 | + // the most important condition should be last, as it will be array_shift'ed off last |
|
| 138 | + $conditions = array( |
|
| 139 | + $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match? |
|
| 140 | + $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match? |
|
| 141 | + ); |
|
| 142 | + // start running queries, widening search each time by removing a condition |
|
| 143 | + $datetime_found = null; |
|
| 144 | + do { |
|
| 145 | + $full_query = "SELECT * FROM $datetime_table WHERE ".implode(" AND ", $conditions)." LIMIT 1"; |
|
| 146 | + $datetime_found = $wpdb->get_row($full_query, ARRAY_A); |
|
| 147 | + array_shift($conditions); |
|
| 148 | + } while (! $datetime_found && $conditions); |
|
| 149 | + return $datetime_found; |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * Adds a new Check-in/checkout record according for $new_reg_id,$new_datetime_id,$checking_in, and $timestmap |
|
| 154 | - * @param int $new_reg_id |
|
| 155 | - * @param int $new_datetime_id |
|
| 156 | - * @param string $timestamp mysql datetime |
|
| 157 | - * @return int new Check-in id |
|
| 158 | - */ |
|
| 159 | - private function _insert_checkin_record($new_reg_id, $new_datetime) |
|
| 160 | - { |
|
| 161 | - global $wpdb; |
|
| 152 | + /** |
|
| 153 | + * Adds a new Check-in/checkout record according for $new_reg_id,$new_datetime_id,$checking_in, and $timestmap |
|
| 154 | + * @param int $new_reg_id |
|
| 155 | + * @param int $new_datetime_id |
|
| 156 | + * @param string $timestamp mysql datetime |
|
| 157 | + * @return int new Check-in id |
|
| 158 | + */ |
|
| 159 | + private function _insert_checkin_record($new_reg_id, $new_datetime) |
|
| 160 | + { |
|
| 161 | + global $wpdb; |
|
| 162 | 162 | |
| 163 | 163 | |
| 164 | - // ok we can actually do what we set out to do: add a checkin/checkout record |
|
| 165 | - $cols_n_values = array( |
|
| 166 | - 'REG_ID'=>$new_reg_id, |
|
| 167 | - 'DTT_ID'=>$new_datetime['DTT_ID'], |
|
| 168 | - 'CHK_in'=>true, |
|
| 169 | - 'CHK_timestamp'=>$new_datetime['DTT_EVT_start'] |
|
| 170 | - ); |
|
| 171 | - $datatypes = array( |
|
| 172 | - '%d',// REG_ID |
|
| 173 | - '%d',// DTT_ID |
|
| 174 | - '%d',// CHK_in |
|
| 175 | - '%s',// CHK_timestamp |
|
| 176 | - ); |
|
| 177 | - $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes); |
|
| 178 | - if (! $success) { |
|
| 179 | - $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_checkin, $this->_new_table, $cols_n_values, $datatypes)); |
|
| 180 | - return 0; |
|
| 181 | - } |
|
| 182 | - $new_id = $wpdb->insert_id; |
|
| 183 | - return $new_id; |
|
| 184 | - } |
|
| 164 | + // ok we can actually do what we set out to do: add a checkin/checkout record |
|
| 165 | + $cols_n_values = array( |
|
| 166 | + 'REG_ID'=>$new_reg_id, |
|
| 167 | + 'DTT_ID'=>$new_datetime['DTT_ID'], |
|
| 168 | + 'CHK_in'=>true, |
|
| 169 | + 'CHK_timestamp'=>$new_datetime['DTT_EVT_start'] |
|
| 170 | + ); |
|
| 171 | + $datatypes = array( |
|
| 172 | + '%d',// REG_ID |
|
| 173 | + '%d',// DTT_ID |
|
| 174 | + '%d',// CHK_in |
|
| 175 | + '%s',// CHK_timestamp |
|
| 176 | + ); |
|
| 177 | + $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes); |
|
| 178 | + if (! $success) { |
|
| 179 | + $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_checkin, $this->_new_table, $cols_n_values, $datatypes)); |
|
| 180 | + return 0; |
|
| 181 | + } |
|
| 182 | + $new_id = $wpdb->insert_id; |
|
| 183 | + return $new_id; |
|
| 184 | + } |
|
| 185 | 185 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | $this->_old_table = $wpdb->prefix."events_attendee"; |
| 35 | 35 | $this->select_expression = 'att.*, e.event_status'; |
| 36 | 36 | $this->_extra_where_sql = 'AS att |
| 37 | - INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id=e.id |
|
| 37 | + INNER JOIN ' . $wpdb->prefix.'events_detail AS e ON att.event_id=e.id |
|
| 38 | 38 | WHERE e.event_status!="D"'; |
| 39 | 39 | $this->_new_table = $wpdb->prefix."esp_checkin"; |
| 40 | 40 | parent::__construct(); |
@@ -44,10 +44,10 @@ discard block |
||
| 44 | 44 | global $wpdb; |
| 45 | 45 | $new_reg_table = $wpdb->prefix."esp_registration"; |
| 46 | 46 | |
| 47 | - $num_to_checkin_at_this_time = max(array(intval($old_row['checked_in_quantity']),intval($old_row['checked_in']))) ; |
|
| 47 | + $num_to_checkin_at_this_time = max(array(intval($old_row['checked_in_quantity']), intval($old_row['checked_in']))); |
|
| 48 | 48 | |
| 49 | 49 | $new_registrations_for_attendee = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $new_reg_table); |
| 50 | - if (! $new_registrations_for_attendee) { |
|
| 50 | + if ( ! $new_registrations_for_attendee) { |
|
| 51 | 51 | $new_registrations_for_attendee = array(); |
| 52 | 52 | } |
| 53 | 53 | $new_datetime = $this->_try_to_find_datetime($old_row); |
@@ -55,9 +55,9 @@ discard block |
||
| 55 | 55 | // make sure registrations array is numerically indexed starting at 0 (it probably already is) |
| 56 | 56 | $new_registrations_for_attendee = array_values($new_registrations_for_attendee); |
| 57 | 57 | $new_checkin_ids = array(); |
| 58 | - for ($i = 0; $i<abs($num_to_checkin_at_this_time); $i++) { |
|
| 59 | - $new_reg_id = $new_registrations_for_attendee[ $i ]; |
|
| 60 | - if (! $new_reg_id) { |
|
| 58 | + for ($i = 0; $i < abs($num_to_checkin_at_this_time); $i++) { |
|
| 59 | + $new_reg_id = $new_registrations_for_attendee[$i]; |
|
| 60 | + if ( ! $new_reg_id) { |
|
| 61 | 61 | $this->add_error(sprintf( |
| 62 | 62 | esc_html__( |
| 63 | 63 | /* translators: %1$s database row represented in JSON, %2$s number of registrations to check-in |
@@ -81,10 +81,10 @@ discard block |
||
| 81 | 81 | $new_reg_id |
| 82 | 82 | ) |
| 83 | 83 | ); |
| 84 | - if (! $existing_checkin_record) { |
|
| 84 | + if ( ! $existing_checkin_record) { |
|
| 85 | 85 | $new_id = $this->_insert_checkin_record($new_reg_id, $new_datetime); |
| 86 | 86 | if ($new_id) { |
| 87 | - $new_checkin_ids[]= $new_id; |
|
| 87 | + $new_checkin_ids[] = $new_id; |
|
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | } |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | global $wpdb; |
| 113 | 113 | |
| 114 | 114 | $new_event_id = $this->get_migration_script()->get_mapping_new_pk($wpdb->prefix."events_detail", $old_attendee['event_id'], $wpdb->posts); |
| 115 | - if (! $new_event_id) { |
|
| 115 | + if ( ! $new_event_id) { |
|
| 116 | 116 | $this->add_error( |
| 117 | 117 | sprintf( |
| 118 | 118 | esc_html__( |
@@ -136,8 +136,8 @@ discard block |
||
| 136 | 136 | // add all conditions to an array from which we can SHIFT conditions off in order to widen our search |
| 137 | 137 | // the most important condition should be last, as it will be array_shift'ed off last |
| 138 | 138 | $conditions = array( |
| 139 | - $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match? |
|
| 140 | - $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match? |
|
| 139 | + $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime), // times match? |
|
| 140 | + $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id), // events match? |
|
| 141 | 141 | ); |
| 142 | 142 | // start running queries, widening search each time by removing a condition |
| 143 | 143 | $datetime_found = null; |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | $full_query = "SELECT * FROM $datetime_table WHERE ".implode(" AND ", $conditions)." LIMIT 1"; |
| 146 | 146 | $datetime_found = $wpdb->get_row($full_query, ARRAY_A); |
| 147 | 147 | array_shift($conditions); |
| 148 | - } while (! $datetime_found && $conditions); |
|
| 148 | + }while ( ! $datetime_found && $conditions); |
|
| 149 | 149 | return $datetime_found; |
| 150 | 150 | } |
| 151 | 151 | |
@@ -169,13 +169,13 @@ discard block |
||
| 169 | 169 | 'CHK_timestamp'=>$new_datetime['DTT_EVT_start'] |
| 170 | 170 | ); |
| 171 | 171 | $datatypes = array( |
| 172 | - '%d',// REG_ID |
|
| 173 | - '%d',// DTT_ID |
|
| 174 | - '%d',// CHK_in |
|
| 175 | - '%s',// CHK_timestamp |
|
| 172 | + '%d', // REG_ID |
|
| 173 | + '%d', // DTT_ID |
|
| 174 | + '%d', // CHK_in |
|
| 175 | + '%s', // CHK_timestamp |
|
| 176 | 176 | ); |
| 177 | 177 | $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes); |
| 178 | - if (! $success) { |
|
| 178 | + if ( ! $success) { |
|
| 179 | 179 | $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_checkin, $this->_new_table, $cols_n_values, $datatypes)); |
| 180 | 180 | return 0; |
| 181 | 181 | } |
@@ -38,172 +38,172 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class EE_DMS_4_1_0_line_items extends EE_Data_Migration_Script_Stage_Table |
| 40 | 40 | { |
| 41 | - private $_new_line_table; |
|
| 42 | - private $_new_transaction_table; |
|
| 43 | - private $_new_reg_table; |
|
| 44 | - public function __construct() |
|
| 45 | - { |
|
| 46 | - global $wpdb; |
|
| 47 | - $this->_pretty_name = __("Line Items", "event_espresso"); |
|
| 48 | - $this->_old_table = $wpdb->prefix."events_attendee"; |
|
| 49 | - $this->select_expression = 'att.*, e.event_status'; |
|
| 50 | - $this->_extra_where_sql = ' AS att |
|
| 41 | + private $_new_line_table; |
|
| 42 | + private $_new_transaction_table; |
|
| 43 | + private $_new_reg_table; |
|
| 44 | + public function __construct() |
|
| 45 | + { |
|
| 46 | + global $wpdb; |
|
| 47 | + $this->_pretty_name = __("Line Items", "event_espresso"); |
|
| 48 | + $this->_old_table = $wpdb->prefix."events_attendee"; |
|
| 49 | + $this->select_expression = 'att.*, e.event_status'; |
|
| 50 | + $this->_extra_where_sql = ' AS att |
|
| 51 | 51 | INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id=e.id |
| 52 | 52 | WHERE e.event_status!="D"'; |
| 53 | - $this->_new_transaction_table = $wpdb->prefix."esp_transaction"; |
|
| 54 | - $this->_new_line_table = $wpdb->prefix."esp_line_item"; |
|
| 55 | - $this->_new_reg_table = $wpdb->prefix."esp_registration"; |
|
| 56 | - parent::__construct(); |
|
| 57 | - } |
|
| 53 | + $this->_new_transaction_table = $wpdb->prefix."esp_transaction"; |
|
| 54 | + $this->_new_line_table = $wpdb->prefix."esp_line_item"; |
|
| 55 | + $this->_new_reg_table = $wpdb->prefix."esp_registration"; |
|
| 56 | + parent::__construct(); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - protected function _migrate_old_row($old_row) |
|
| 60 | - { |
|
| 61 | - // insert line items if its a primary id |
|
| 62 | - if (intval($old_row['is_primary'])) { |
|
| 63 | - $txn_id = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $this->_new_transaction_table); |
|
| 64 | - if (! $txn_id) { |
|
| 65 | - $this->add_error(sprintf(__("Could not find the transaction for the 3.1 attendee %d from row %s", "event_espresso"), $old_row['id'], $this->_json_encode($old_row))); |
|
| 66 | - return; |
|
| 67 | - } |
|
| 68 | - $txn = $this->_get_txn($txn_id); |
|
| 69 | - $new_line_items = $this->_insert_new_line_items($txn, $old_row); |
|
| 70 | - $this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_line_table, $new_line_items); |
|
| 71 | - } |
|
| 72 | - } |
|
| 59 | + protected function _migrate_old_row($old_row) |
|
| 60 | + { |
|
| 61 | + // insert line items if its a primary id |
|
| 62 | + if (intval($old_row['is_primary'])) { |
|
| 63 | + $txn_id = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $this->_new_transaction_table); |
|
| 64 | + if (! $txn_id) { |
|
| 65 | + $this->add_error(sprintf(__("Could not find the transaction for the 3.1 attendee %d from row %s", "event_espresso"), $old_row['id'], $this->_json_encode($old_row))); |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | + $txn = $this->_get_txn($txn_id); |
|
| 69 | + $new_line_items = $this->_insert_new_line_items($txn, $old_row); |
|
| 70 | + $this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_line_table, $new_line_items); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - private function _get_txn($txn_id) |
|
| 75 | - { |
|
| 76 | - global $wpdb; |
|
| 77 | - $txn = $wpdb->get_row($wpdb->prepare("SELECT * FROM $this->_new_transaction_table WHERE TXN_ID=%d", $txn_id), ARRAY_A); |
|
| 78 | - return $txn; |
|
| 79 | - } |
|
| 74 | + private function _get_txn($txn_id) |
|
| 75 | + { |
|
| 76 | + global $wpdb; |
|
| 77 | + $txn = $wpdb->get_row($wpdb->prepare("SELECT * FROM $this->_new_transaction_table WHERE TXN_ID=%d", $txn_id), ARRAY_A); |
|
| 78 | + return $txn; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * In 4.1, we'd normally need more info than just the registrations to make the line items. Ie, we'd need |
|
| 83 | - * the transaction, and tax prices at the time of registration. (And probably promotions and other price factors). |
|
| 84 | - * But seeing how these are REGs created from 3.1 attendee data, which have |
|
| 85 | - * @param array $transaction |
|
| 86 | - * @return array new line item ids |
|
| 87 | - */ |
|
| 88 | - private function _insert_new_line_items($transaction, $old_attendee) |
|
| 89 | - { |
|
| 90 | - global $wpdb; |
|
| 91 | - $regs_on_this_transaction = $wpdb->get_results($wpdb->prepare("SELECT * FROM ".$this->_new_reg_table." WHERE TXN_ID=%d", $transaction['TXN_ID']), ARRAY_A); |
|
| 92 | - $new_line_item_ids = array(); |
|
| 93 | - // create a totla line item |
|
| 94 | - $total_line_item_id = $this->_insert_new_line_item(array( |
|
| 95 | - 'LIN_code'=>'total', |
|
| 96 | - 'TXN_ID'=>$transaction['TXN_ID'], |
|
| 97 | - 'LIN_name'=> __("Total", "event_espresso"), |
|
| 98 | - 'LIN_total'=>$transaction['TXN_total'], |
|
| 99 | - 'LIN_type'=>'total', |
|
| 100 | - 'OBJ_ID'=>$transaction['TXN_ID'], |
|
| 101 | - 'OBJ_type'=>'Transaction' |
|
| 102 | - ), $old_attendee); |
|
| 103 | - $new_line_item_ids[] = $total_line_item_id; |
|
| 104 | - // create a subtotal line item |
|
| 105 | - $reg_total = 0; |
|
| 106 | - foreach ($regs_on_this_transaction as $new_reg) { |
|
| 107 | - $reg_total += floatval($new_reg['REG_final_price']); |
|
| 108 | - } |
|
| 109 | - $subtotal_line_item_id = $this->_insert_new_line_item(array( |
|
| 110 | - 'LIN_code'=>'sub-total', |
|
| 111 | - 'TXN_ID'=>$transaction['TXN_ID'], |
|
| 112 | - 'LIN_name'=> __("Subtotal", "event_espresso"), |
|
| 113 | - 'LIN_total'=>$reg_total, |
|
| 114 | - 'LIN_parent'=>$total_line_item_id, |
|
| 115 | - 'LIN_type'=>'sub-total', |
|
| 116 | - ), $old_attendee); |
|
| 117 | - $new_line_item_ids[] = $subtotal_line_item_id; |
|
| 118 | - // group REGs by TKT_ID |
|
| 119 | - $regs_by_tkt = array(); |
|
| 120 | - foreach ($regs_on_this_transaction as $new_reg) { |
|
| 121 | - $regs_by_tkt[ $new_reg['TKT_ID'] ][] = $new_reg; |
|
| 122 | - } |
|
| 81 | + /** |
|
| 82 | + * In 4.1, we'd normally need more info than just the registrations to make the line items. Ie, we'd need |
|
| 83 | + * the transaction, and tax prices at the time of registration. (And probably promotions and other price factors). |
|
| 84 | + * But seeing how these are REGs created from 3.1 attendee data, which have |
|
| 85 | + * @param array $transaction |
|
| 86 | + * @return array new line item ids |
|
| 87 | + */ |
|
| 88 | + private function _insert_new_line_items($transaction, $old_attendee) |
|
| 89 | + { |
|
| 90 | + global $wpdb; |
|
| 91 | + $regs_on_this_transaction = $wpdb->get_results($wpdb->prepare("SELECT * FROM ".$this->_new_reg_table." WHERE TXN_ID=%d", $transaction['TXN_ID']), ARRAY_A); |
|
| 92 | + $new_line_item_ids = array(); |
|
| 93 | + // create a totla line item |
|
| 94 | + $total_line_item_id = $this->_insert_new_line_item(array( |
|
| 95 | + 'LIN_code'=>'total', |
|
| 96 | + 'TXN_ID'=>$transaction['TXN_ID'], |
|
| 97 | + 'LIN_name'=> __("Total", "event_espresso"), |
|
| 98 | + 'LIN_total'=>$transaction['TXN_total'], |
|
| 99 | + 'LIN_type'=>'total', |
|
| 100 | + 'OBJ_ID'=>$transaction['TXN_ID'], |
|
| 101 | + 'OBJ_type'=>'Transaction' |
|
| 102 | + ), $old_attendee); |
|
| 103 | + $new_line_item_ids[] = $total_line_item_id; |
|
| 104 | + // create a subtotal line item |
|
| 105 | + $reg_total = 0; |
|
| 106 | + foreach ($regs_on_this_transaction as $new_reg) { |
|
| 107 | + $reg_total += floatval($new_reg['REG_final_price']); |
|
| 108 | + } |
|
| 109 | + $subtotal_line_item_id = $this->_insert_new_line_item(array( |
|
| 110 | + 'LIN_code'=>'sub-total', |
|
| 111 | + 'TXN_ID'=>$transaction['TXN_ID'], |
|
| 112 | + 'LIN_name'=> __("Subtotal", "event_espresso"), |
|
| 113 | + 'LIN_total'=>$reg_total, |
|
| 114 | + 'LIN_parent'=>$total_line_item_id, |
|
| 115 | + 'LIN_type'=>'sub-total', |
|
| 116 | + ), $old_attendee); |
|
| 117 | + $new_line_item_ids[] = $subtotal_line_item_id; |
|
| 118 | + // group REGs by TKT_ID |
|
| 119 | + $regs_by_tkt = array(); |
|
| 120 | + foreach ($regs_on_this_transaction as $new_reg) { |
|
| 121 | + $regs_by_tkt[ $new_reg['TKT_ID'] ][] = $new_reg; |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - // create individual line items |
|
| 124 | + // create individual line items |
|
| 125 | 125 | |
| 126 | - foreach ($regs_by_tkt as $ticket_id => $regs) { |
|
| 127 | - $count = count($regs); |
|
| 128 | - $line_total = 0; |
|
| 129 | - foreach ($regs as $new_reg) { |
|
| 130 | - $line_total += $new_reg['REG_final_price']; |
|
| 131 | - } |
|
| 132 | - $a_reg = reset($regs); |
|
| 133 | - $new_ticket = $this->_get_new_ticket_row($a_reg['TKT_ID']); |
|
| 134 | - $reg_line_item_id = $this->_insert_new_line_item(array( |
|
| 135 | - 'LIN_code'=> md5('Ticket' . $ticket_id . time()), |
|
| 136 | - 'TXN_ID'=>$transaction['TXN_ID'], |
|
| 137 | - 'LIN_name'=>$new_ticket['TKT_name'], |
|
| 138 | - 'LIN_unit_price'=>$a_reg['REG_final_price'], |
|
| 139 | - 'LIN_is_taxable'=>false, |
|
| 140 | - 'LIN_total'=>$line_total, |
|
| 141 | - 'LIN_quantity'=>$count, |
|
| 142 | - 'LIN_parent'=>$subtotal_line_item_id, |
|
| 143 | - 'OBJ_ID'=>$ticket_id, |
|
| 144 | - 'OBJ_type'=>'Ticket' |
|
| 145 | - ), $old_attendee); |
|
| 146 | - $new_line_item_ids[] = $reg_line_item_id; |
|
| 147 | - } |
|
| 126 | + foreach ($regs_by_tkt as $ticket_id => $regs) { |
|
| 127 | + $count = count($regs); |
|
| 128 | + $line_total = 0; |
|
| 129 | + foreach ($regs as $new_reg) { |
|
| 130 | + $line_total += $new_reg['REG_final_price']; |
|
| 131 | + } |
|
| 132 | + $a_reg = reset($regs); |
|
| 133 | + $new_ticket = $this->_get_new_ticket_row($a_reg['TKT_ID']); |
|
| 134 | + $reg_line_item_id = $this->_insert_new_line_item(array( |
|
| 135 | + 'LIN_code'=> md5('Ticket' . $ticket_id . time()), |
|
| 136 | + 'TXN_ID'=>$transaction['TXN_ID'], |
|
| 137 | + 'LIN_name'=>$new_ticket['TKT_name'], |
|
| 138 | + 'LIN_unit_price'=>$a_reg['REG_final_price'], |
|
| 139 | + 'LIN_is_taxable'=>false, |
|
| 140 | + 'LIN_total'=>$line_total, |
|
| 141 | + 'LIN_quantity'=>$count, |
|
| 142 | + 'LIN_parent'=>$subtotal_line_item_id, |
|
| 143 | + 'OBJ_ID'=>$ticket_id, |
|
| 144 | + 'OBJ_type'=>'Ticket' |
|
| 145 | + ), $old_attendee); |
|
| 146 | + $new_line_item_ids[] = $reg_line_item_id; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | 149 | |
| 150 | 150 | |
| 151 | - return $new_line_item_ids; |
|
| 152 | - } |
|
| 153 | - /** |
|
| 154 | - * Gets the full ticket by ID |
|
| 155 | - * @global type $wpdb |
|
| 156 | - * @param type $new_ticket_id |
|
| 157 | - * @return array |
|
| 158 | - */ |
|
| 159 | - private function _get_new_ticket_row($new_ticket_id) |
|
| 160 | - { |
|
| 161 | - global $wpdb; |
|
| 162 | - $ticket_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM ".$wpdb->prefix."esp_ticket WHERE TKT_ID=%d", $new_ticket_id), ARRAY_A); |
|
| 163 | - return $ticket_row; |
|
| 164 | - } |
|
| 151 | + return $new_line_item_ids; |
|
| 152 | + } |
|
| 153 | + /** |
|
| 154 | + * Gets the full ticket by ID |
|
| 155 | + * @global type $wpdb |
|
| 156 | + * @param type $new_ticket_id |
|
| 157 | + * @return array |
|
| 158 | + */ |
|
| 159 | + private function _get_new_ticket_row($new_ticket_id) |
|
| 160 | + { |
|
| 161 | + global $wpdb; |
|
| 162 | + $ticket_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM ".$wpdb->prefix."esp_ticket WHERE TKT_ID=%d", $new_ticket_id), ARRAY_A); |
|
| 163 | + return $ticket_row; |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - private function _insert_new_line_item($cols_n_values, $old_attendee) |
|
| 167 | - { |
|
| 168 | - global $wpdb; |
|
| 169 | - $default_cols_n_values = array( |
|
| 170 | - 'LIN_code'=>'', |
|
| 171 | - 'TXN_ID'=>0, |
|
| 172 | - 'LIN_name'=>'', |
|
| 173 | - 'LIN_desc'=>'', |
|
| 174 | - 'LIN_unit_price'=>0, |
|
| 175 | - 'LIN_percent'=>0, |
|
| 176 | - 'LIN_is_taxable'=>false, |
|
| 177 | - 'LIN_order'=>0, |
|
| 178 | - 'LIN_total'=>0, |
|
| 179 | - 'LIN_quantity'=>null, |
|
| 180 | - 'LIN_parent'=>0, |
|
| 181 | - 'LIN_type'=>'line-item', |
|
| 182 | - 'OBJ_ID'=>null, |
|
| 183 | - 'OBJ_type'=>null |
|
| 184 | - ); |
|
| 185 | - $cols_n_values = array_merge($default_cols_n_values, $cols_n_values); |
|
| 186 | - $datatypes = array( |
|
| 187 | - '%s',// LIN_code |
|
| 188 | - '%d',// TXN_ID |
|
| 189 | - '%s',// LIN_name |
|
| 190 | - '%s',// LIN_desc |
|
| 191 | - '%f',// LIN_unit_price |
|
| 192 | - '%f',// LIN_percent |
|
| 193 | - '%d',// LIN_is_taxable |
|
| 194 | - '%d',// LIN_order |
|
| 195 | - '%f',// LIN_total |
|
| 196 | - '%d',// LIN_quantity |
|
| 197 | - '%d',// LIN_parent |
|
| 198 | - '%s',// LIN_type |
|
| 199 | - '%d',// OBJ_ID |
|
| 200 | - '%s',// OBJ_type |
|
| 201 | - ); |
|
| 202 | - $success = $wpdb->insert($this->_new_line_table, $cols_n_values, $datatypes); |
|
| 203 | - if (! $success) { |
|
| 204 | - $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_reg_table, $cols_n_values, $datatypes)); |
|
| 205 | - return 0; |
|
| 206 | - } |
|
| 207 | - return $wpdb->insert_id; |
|
| 208 | - } |
|
| 166 | + private function _insert_new_line_item($cols_n_values, $old_attendee) |
|
| 167 | + { |
|
| 168 | + global $wpdb; |
|
| 169 | + $default_cols_n_values = array( |
|
| 170 | + 'LIN_code'=>'', |
|
| 171 | + 'TXN_ID'=>0, |
|
| 172 | + 'LIN_name'=>'', |
|
| 173 | + 'LIN_desc'=>'', |
|
| 174 | + 'LIN_unit_price'=>0, |
|
| 175 | + 'LIN_percent'=>0, |
|
| 176 | + 'LIN_is_taxable'=>false, |
|
| 177 | + 'LIN_order'=>0, |
|
| 178 | + 'LIN_total'=>0, |
|
| 179 | + 'LIN_quantity'=>null, |
|
| 180 | + 'LIN_parent'=>0, |
|
| 181 | + 'LIN_type'=>'line-item', |
|
| 182 | + 'OBJ_ID'=>null, |
|
| 183 | + 'OBJ_type'=>null |
|
| 184 | + ); |
|
| 185 | + $cols_n_values = array_merge($default_cols_n_values, $cols_n_values); |
|
| 186 | + $datatypes = array( |
|
| 187 | + '%s',// LIN_code |
|
| 188 | + '%d',// TXN_ID |
|
| 189 | + '%s',// LIN_name |
|
| 190 | + '%s',// LIN_desc |
|
| 191 | + '%f',// LIN_unit_price |
|
| 192 | + '%f',// LIN_percent |
|
| 193 | + '%d',// LIN_is_taxable |
|
| 194 | + '%d',// LIN_order |
|
| 195 | + '%f',// LIN_total |
|
| 196 | + '%d',// LIN_quantity |
|
| 197 | + '%d',// LIN_parent |
|
| 198 | + '%s',// LIN_type |
|
| 199 | + '%d',// OBJ_ID |
|
| 200 | + '%s',// OBJ_type |
|
| 201 | + ); |
|
| 202 | + $success = $wpdb->insert($this->_new_line_table, $cols_n_values, $datatypes); |
|
| 203 | + if (! $success) { |
|
| 204 | + $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_reg_table, $cols_n_values, $datatypes)); |
|
| 205 | + return 0; |
|
| 206 | + } |
|
| 207 | + return $wpdb->insert_id; |
|
| 208 | + } |
|
| 209 | 209 | } |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | $this->_old_table = $wpdb->prefix."events_attendee"; |
| 49 | 49 | $this->select_expression = 'att.*, e.event_status'; |
| 50 | 50 | $this->_extra_where_sql = ' AS att |
| 51 | - INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id=e.id |
|
| 51 | + INNER JOIN ' . $wpdb->prefix.'events_detail AS e ON att.event_id=e.id |
|
| 52 | 52 | WHERE e.event_status!="D"'; |
| 53 | 53 | $this->_new_transaction_table = $wpdb->prefix."esp_transaction"; |
| 54 | 54 | $this->_new_line_table = $wpdb->prefix."esp_line_item"; |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | // insert line items if its a primary id |
| 62 | 62 | if (intval($old_row['is_primary'])) { |
| 63 | 63 | $txn_id = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $this->_new_transaction_table); |
| 64 | - if (! $txn_id) { |
|
| 64 | + if ( ! $txn_id) { |
|
| 65 | 65 | $this->add_error(sprintf(__("Could not find the transaction for the 3.1 attendee %d from row %s", "event_espresso"), $old_row['id'], $this->_json_encode($old_row))); |
| 66 | 66 | return; |
| 67 | 67 | } |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | // group REGs by TKT_ID |
| 119 | 119 | $regs_by_tkt = array(); |
| 120 | 120 | foreach ($regs_on_this_transaction as $new_reg) { |
| 121 | - $regs_by_tkt[ $new_reg['TKT_ID'] ][] = $new_reg; |
|
| 121 | + $regs_by_tkt[$new_reg['TKT_ID']][] = $new_reg; |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | // create individual line items |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | $a_reg = reset($regs); |
| 133 | 133 | $new_ticket = $this->_get_new_ticket_row($a_reg['TKT_ID']); |
| 134 | 134 | $reg_line_item_id = $this->_insert_new_line_item(array( |
| 135 | - 'LIN_code'=> md5('Ticket' . $ticket_id . time()), |
|
| 135 | + 'LIN_code'=> md5('Ticket'.$ticket_id.time()), |
|
| 136 | 136 | 'TXN_ID'=>$transaction['TXN_ID'], |
| 137 | 137 | 'LIN_name'=>$new_ticket['TKT_name'], |
| 138 | 138 | 'LIN_unit_price'=>$a_reg['REG_final_price'], |
@@ -184,23 +184,23 @@ discard block |
||
| 184 | 184 | ); |
| 185 | 185 | $cols_n_values = array_merge($default_cols_n_values, $cols_n_values); |
| 186 | 186 | $datatypes = array( |
| 187 | - '%s',// LIN_code |
|
| 188 | - '%d',// TXN_ID |
|
| 189 | - '%s',// LIN_name |
|
| 190 | - '%s',// LIN_desc |
|
| 191 | - '%f',// LIN_unit_price |
|
| 192 | - '%f',// LIN_percent |
|
| 193 | - '%d',// LIN_is_taxable |
|
| 194 | - '%d',// LIN_order |
|
| 195 | - '%f',// LIN_total |
|
| 196 | - '%d',// LIN_quantity |
|
| 197 | - '%d',// LIN_parent |
|
| 198 | - '%s',// LIN_type |
|
| 199 | - '%d',// OBJ_ID |
|
| 200 | - '%s',// OBJ_type |
|
| 187 | + '%s', // LIN_code |
|
| 188 | + '%d', // TXN_ID |
|
| 189 | + '%s', // LIN_name |
|
| 190 | + '%s', // LIN_desc |
|
| 191 | + '%f', // LIN_unit_price |
|
| 192 | + '%f', // LIN_percent |
|
| 193 | + '%d', // LIN_is_taxable |
|
| 194 | + '%d', // LIN_order |
|
| 195 | + '%f', // LIN_total |
|
| 196 | + '%d', // LIN_quantity |
|
| 197 | + '%d', // LIN_parent |
|
| 198 | + '%s', // LIN_type |
|
| 199 | + '%d', // OBJ_ID |
|
| 200 | + '%s', // OBJ_type |
|
| 201 | 201 | ); |
| 202 | 202 | $success = $wpdb->insert($this->_new_line_table, $cols_n_values, $datatypes); |
| 203 | - if (! $success) { |
|
| 203 | + if ( ! $success) { |
|
| 204 | 204 | $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_reg_table, $cols_n_values, $datatypes)); |
| 205 | 205 | return 0; |
| 206 | 206 | } |
@@ -23,595 +23,595 @@ |
||
| 23 | 23 | class Registry |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var AssetCollection $assets |
|
| 30 | - */ |
|
| 31 | - protected $assets; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var I18nRegistry |
|
| 35 | - */ |
|
| 36 | - private $i18n_registry; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
|
| 40 | - * |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - protected $jsdata = array(); |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
|
| 47 | - * page source. |
|
| 48 | - * |
|
| 49 | - * @var array |
|
| 50 | - */ |
|
| 51 | - private $script_handles_with_data = array(); |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Holds the manifest data obtained from registered manifest files. |
|
| 56 | - * Manifests are maps of asset chunk name to actual built asset file names. |
|
| 57 | - * Shape of this array is: |
|
| 58 | - * array( |
|
| 59 | - * 'some_namespace_slug' => array( |
|
| 60 | - * 'some_chunk_name' => array( |
|
| 61 | - * 'js' => 'filename.js' |
|
| 62 | - * 'css' => 'filename.js' |
|
| 63 | - * ), |
|
| 64 | - * 'url_base' => 'https://baseurl.com/to/assets |
|
| 65 | - * ) |
|
| 66 | - * ) |
|
| 67 | - * |
|
| 68 | - * @var array |
|
| 69 | - */ |
|
| 70 | - private $manifest_data = array(); |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Registry constructor. |
|
| 75 | - * Hooking into WP actions for script registry. |
|
| 76 | - * |
|
| 77 | - * @param AssetCollection $assets |
|
| 78 | - * @param I18nRegistry $i18n_registry |
|
| 79 | - */ |
|
| 80 | - public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
|
| 81 | - { |
|
| 82 | - $this->assets = $assets; |
|
| 83 | - $this->i18n_registry = $i18n_registry; |
|
| 84 | - add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 85 | - add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 86 | - add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 87 | - add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 88 | - add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 89 | - add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 90 | - add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 91 | - add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
|
| 97 | - * translation handling. |
|
| 98 | - * |
|
| 99 | - * @return I18nRegistry |
|
| 100 | - */ |
|
| 101 | - public function getI18nRegistry() |
|
| 102 | - { |
|
| 103 | - return $this->i18n_registry; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Callback for the wp_enqueue_scripts actions used to register assets. |
|
| 109 | - * |
|
| 110 | - * @since 4.9.62.p |
|
| 111 | - * @throws Exception |
|
| 112 | - */ |
|
| 113 | - public function registerScriptsAndStyles() |
|
| 114 | - { |
|
| 115 | - try { |
|
| 116 | - $this->registerScripts($this->assets->getJavascriptAssets()); |
|
| 117 | - $this->registerStyles($this->assets->getStylesheetAssets()); |
|
| 118 | - } catch (Exception $exception) { |
|
| 119 | - new ExceptionStackTraceDisplay($exception); |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Registers JS assets with WP core |
|
| 126 | - * |
|
| 127 | - * @since 4.9.62.p |
|
| 128 | - * @param JavascriptAsset[] $scripts |
|
| 129 | - * @throws AssetRegistrationException |
|
| 130 | - * @throws InvalidDataTypeException |
|
| 131 | - */ |
|
| 132 | - public function registerScripts(array $scripts) |
|
| 133 | - { |
|
| 134 | - foreach ($scripts as $script) { |
|
| 135 | - // skip to next script if this has already been done |
|
| 136 | - if ($script->isRegistered()) { |
|
| 137 | - continue; |
|
| 138 | - } |
|
| 139 | - do_action( |
|
| 140 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script', |
|
| 141 | - $script |
|
| 142 | - ); |
|
| 143 | - $registered = wp_register_script( |
|
| 144 | - $script->handle(), |
|
| 145 | - $script->source(), |
|
| 146 | - $script->dependencies(), |
|
| 147 | - $script->version(), |
|
| 148 | - $script->loadInFooter() |
|
| 149 | - ); |
|
| 150 | - if (! $registered && $this->debug()) { |
|
| 151 | - throw new AssetRegistrationException($script->handle()); |
|
| 152 | - } |
|
| 153 | - $script->setRegistered($registered); |
|
| 154 | - if ($script->requiresTranslation()) { |
|
| 155 | - $this->registerTranslation($script->handle()); |
|
| 156 | - } |
|
| 157 | - do_action( |
|
| 158 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__after_script', |
|
| 159 | - $script |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Registers CSS assets with WP core |
|
| 167 | - * |
|
| 168 | - * @since 4.9.62.p |
|
| 169 | - * @param StylesheetAsset[] $styles |
|
| 170 | - * @throws InvalidDataTypeException |
|
| 171 | - */ |
|
| 172 | - public function registerStyles(array $styles) |
|
| 173 | - { |
|
| 174 | - foreach ($styles as $style) { |
|
| 175 | - // skip to next style if this has already been done |
|
| 176 | - if ($style->isRegistered()) { |
|
| 177 | - continue; |
|
| 178 | - } |
|
| 179 | - do_action( |
|
| 180 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style', |
|
| 181 | - $style |
|
| 182 | - ); |
|
| 183 | - wp_register_style( |
|
| 184 | - $style->handle(), |
|
| 185 | - $style->source(), |
|
| 186 | - $style->dependencies(), |
|
| 187 | - $style->version(), |
|
| 188 | - $style->media() |
|
| 189 | - ); |
|
| 190 | - $style->setRegistered(); |
|
| 191 | - do_action( |
|
| 192 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style', |
|
| 193 | - $style |
|
| 194 | - ); |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Call back for the script print in frontend and backend. |
|
| 201 | - * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
|
| 202 | - * |
|
| 203 | - * @since 4.9.31.rc.015 |
|
| 204 | - */ |
|
| 205 | - public function enqueueData() |
|
| 206 | - { |
|
| 207 | - $this->removeAlreadyRegisteredDataForScriptHandles(); |
|
| 208 | - wp_add_inline_script( |
|
| 209 | - 'eejs-core', |
|
| 210 | - 'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
|
| 211 | - 'before' |
|
| 212 | - ); |
|
| 213 | - $scripts = $this->assets->getJavascriptAssetsWithData(); |
|
| 214 | - foreach ($scripts as $script) { |
|
| 215 | - $this->addRegisteredScriptHandlesWithData($script->handle()); |
|
| 216 | - if ($script->hasInlineDataCallback()) { |
|
| 217 | - $localize = $script->inlineDataCallback(); |
|
| 218 | - $localize(); |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Used to add data to eejs.data object. |
|
| 226 | - * Note: Overriding existing data is not allowed. |
|
| 227 | - * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
|
| 228 | - * If the data you add is something like this: |
|
| 229 | - * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
|
| 230 | - * It will be exposed in the page source as: |
|
| 231 | - * eejs.data.my_plugin_data.foo == gar |
|
| 232 | - * |
|
| 233 | - * @param string $key Key used to access your data |
|
| 234 | - * @param string|array $value Value to attach to key |
|
| 235 | - * @throws InvalidArgumentException |
|
| 236 | - */ |
|
| 237 | - public function addData($key, $value) |
|
| 238 | - { |
|
| 239 | - if ($this->verifyDataNotExisting($key)) { |
|
| 240 | - $this->jsdata[ $key ] = $value; |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Similar to addData except this allows for users to push values to an existing key where the values on key are |
|
| 247 | - * elements in an array. |
|
| 248 | - * |
|
| 249 | - * When you use this method, the value you include will be merged with the array on $key. |
|
| 250 | - * So if the $key was 'test' and you added a value of ['my_data'] then it would be represented in the javascript |
|
| 251 | - * object like this, eejs.data.test = [ my_data, |
|
| 252 | - * ] |
|
| 253 | - * If there has already been a scalar value attached to the data object given key (via addData for instance), then |
|
| 254 | - * this will throw an exception. |
|
| 255 | - * |
|
| 256 | - * Caution: Only add data using this method if you are okay with the potential for additional data added on the same |
|
| 257 | - * key potentially overriding the existing data on merge (specifically with associative arrays). |
|
| 258 | - * |
|
| 259 | - * @param string $key Key to attach data to. |
|
| 260 | - * @param string|array $value Value being registered. |
|
| 261 | - * @throws InvalidArgumentException |
|
| 262 | - */ |
|
| 263 | - public function pushData($key, $value) |
|
| 264 | - { |
|
| 265 | - if (isset($this->jsdata[ $key ]) |
|
| 266 | - && ! is_array($this->jsdata[ $key ]) |
|
| 267 | - ) { |
|
| 268 | - if (! $this->debug()) { |
|
| 269 | - return; |
|
| 270 | - } |
|
| 271 | - throw new InvalidArgumentException( |
|
| 272 | - sprintf( |
|
| 273 | - __( |
|
| 274 | - 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
|
| 26 | + const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var AssetCollection $assets |
|
| 30 | + */ |
|
| 31 | + protected $assets; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var I18nRegistry |
|
| 35 | + */ |
|
| 36 | + private $i18n_registry; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
|
| 40 | + * |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + protected $jsdata = array(); |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
|
| 47 | + * page source. |
|
| 48 | + * |
|
| 49 | + * @var array |
|
| 50 | + */ |
|
| 51 | + private $script_handles_with_data = array(); |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Holds the manifest data obtained from registered manifest files. |
|
| 56 | + * Manifests are maps of asset chunk name to actual built asset file names. |
|
| 57 | + * Shape of this array is: |
|
| 58 | + * array( |
|
| 59 | + * 'some_namespace_slug' => array( |
|
| 60 | + * 'some_chunk_name' => array( |
|
| 61 | + * 'js' => 'filename.js' |
|
| 62 | + * 'css' => 'filename.js' |
|
| 63 | + * ), |
|
| 64 | + * 'url_base' => 'https://baseurl.com/to/assets |
|
| 65 | + * ) |
|
| 66 | + * ) |
|
| 67 | + * |
|
| 68 | + * @var array |
|
| 69 | + */ |
|
| 70 | + private $manifest_data = array(); |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Registry constructor. |
|
| 75 | + * Hooking into WP actions for script registry. |
|
| 76 | + * |
|
| 77 | + * @param AssetCollection $assets |
|
| 78 | + * @param I18nRegistry $i18n_registry |
|
| 79 | + */ |
|
| 80 | + public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
|
| 81 | + { |
|
| 82 | + $this->assets = $assets; |
|
| 83 | + $this->i18n_registry = $i18n_registry; |
|
| 84 | + add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 85 | + add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 86 | + add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 87 | + add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 88 | + add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 89 | + add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 90 | + add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 91 | + add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
|
| 97 | + * translation handling. |
|
| 98 | + * |
|
| 99 | + * @return I18nRegistry |
|
| 100 | + */ |
|
| 101 | + public function getI18nRegistry() |
|
| 102 | + { |
|
| 103 | + return $this->i18n_registry; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Callback for the wp_enqueue_scripts actions used to register assets. |
|
| 109 | + * |
|
| 110 | + * @since 4.9.62.p |
|
| 111 | + * @throws Exception |
|
| 112 | + */ |
|
| 113 | + public function registerScriptsAndStyles() |
|
| 114 | + { |
|
| 115 | + try { |
|
| 116 | + $this->registerScripts($this->assets->getJavascriptAssets()); |
|
| 117 | + $this->registerStyles($this->assets->getStylesheetAssets()); |
|
| 118 | + } catch (Exception $exception) { |
|
| 119 | + new ExceptionStackTraceDisplay($exception); |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Registers JS assets with WP core |
|
| 126 | + * |
|
| 127 | + * @since 4.9.62.p |
|
| 128 | + * @param JavascriptAsset[] $scripts |
|
| 129 | + * @throws AssetRegistrationException |
|
| 130 | + * @throws InvalidDataTypeException |
|
| 131 | + */ |
|
| 132 | + public function registerScripts(array $scripts) |
|
| 133 | + { |
|
| 134 | + foreach ($scripts as $script) { |
|
| 135 | + // skip to next script if this has already been done |
|
| 136 | + if ($script->isRegistered()) { |
|
| 137 | + continue; |
|
| 138 | + } |
|
| 139 | + do_action( |
|
| 140 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script', |
|
| 141 | + $script |
|
| 142 | + ); |
|
| 143 | + $registered = wp_register_script( |
|
| 144 | + $script->handle(), |
|
| 145 | + $script->source(), |
|
| 146 | + $script->dependencies(), |
|
| 147 | + $script->version(), |
|
| 148 | + $script->loadInFooter() |
|
| 149 | + ); |
|
| 150 | + if (! $registered && $this->debug()) { |
|
| 151 | + throw new AssetRegistrationException($script->handle()); |
|
| 152 | + } |
|
| 153 | + $script->setRegistered($registered); |
|
| 154 | + if ($script->requiresTranslation()) { |
|
| 155 | + $this->registerTranslation($script->handle()); |
|
| 156 | + } |
|
| 157 | + do_action( |
|
| 158 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__after_script', |
|
| 159 | + $script |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Registers CSS assets with WP core |
|
| 167 | + * |
|
| 168 | + * @since 4.9.62.p |
|
| 169 | + * @param StylesheetAsset[] $styles |
|
| 170 | + * @throws InvalidDataTypeException |
|
| 171 | + */ |
|
| 172 | + public function registerStyles(array $styles) |
|
| 173 | + { |
|
| 174 | + foreach ($styles as $style) { |
|
| 175 | + // skip to next style if this has already been done |
|
| 176 | + if ($style->isRegistered()) { |
|
| 177 | + continue; |
|
| 178 | + } |
|
| 179 | + do_action( |
|
| 180 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style', |
|
| 181 | + $style |
|
| 182 | + ); |
|
| 183 | + wp_register_style( |
|
| 184 | + $style->handle(), |
|
| 185 | + $style->source(), |
|
| 186 | + $style->dependencies(), |
|
| 187 | + $style->version(), |
|
| 188 | + $style->media() |
|
| 189 | + ); |
|
| 190 | + $style->setRegistered(); |
|
| 191 | + do_action( |
|
| 192 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style', |
|
| 193 | + $style |
|
| 194 | + ); |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Call back for the script print in frontend and backend. |
|
| 201 | + * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
|
| 202 | + * |
|
| 203 | + * @since 4.9.31.rc.015 |
|
| 204 | + */ |
|
| 205 | + public function enqueueData() |
|
| 206 | + { |
|
| 207 | + $this->removeAlreadyRegisteredDataForScriptHandles(); |
|
| 208 | + wp_add_inline_script( |
|
| 209 | + 'eejs-core', |
|
| 210 | + 'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
|
| 211 | + 'before' |
|
| 212 | + ); |
|
| 213 | + $scripts = $this->assets->getJavascriptAssetsWithData(); |
|
| 214 | + foreach ($scripts as $script) { |
|
| 215 | + $this->addRegisteredScriptHandlesWithData($script->handle()); |
|
| 216 | + if ($script->hasInlineDataCallback()) { |
|
| 217 | + $localize = $script->inlineDataCallback(); |
|
| 218 | + $localize(); |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Used to add data to eejs.data object. |
|
| 226 | + * Note: Overriding existing data is not allowed. |
|
| 227 | + * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
|
| 228 | + * If the data you add is something like this: |
|
| 229 | + * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
|
| 230 | + * It will be exposed in the page source as: |
|
| 231 | + * eejs.data.my_plugin_data.foo == gar |
|
| 232 | + * |
|
| 233 | + * @param string $key Key used to access your data |
|
| 234 | + * @param string|array $value Value to attach to key |
|
| 235 | + * @throws InvalidArgumentException |
|
| 236 | + */ |
|
| 237 | + public function addData($key, $value) |
|
| 238 | + { |
|
| 239 | + if ($this->verifyDataNotExisting($key)) { |
|
| 240 | + $this->jsdata[ $key ] = $value; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Similar to addData except this allows for users to push values to an existing key where the values on key are |
|
| 247 | + * elements in an array. |
|
| 248 | + * |
|
| 249 | + * When you use this method, the value you include will be merged with the array on $key. |
|
| 250 | + * So if the $key was 'test' and you added a value of ['my_data'] then it would be represented in the javascript |
|
| 251 | + * object like this, eejs.data.test = [ my_data, |
|
| 252 | + * ] |
|
| 253 | + * If there has already been a scalar value attached to the data object given key (via addData for instance), then |
|
| 254 | + * this will throw an exception. |
|
| 255 | + * |
|
| 256 | + * Caution: Only add data using this method if you are okay with the potential for additional data added on the same |
|
| 257 | + * key potentially overriding the existing data on merge (specifically with associative arrays). |
|
| 258 | + * |
|
| 259 | + * @param string $key Key to attach data to. |
|
| 260 | + * @param string|array $value Value being registered. |
|
| 261 | + * @throws InvalidArgumentException |
|
| 262 | + */ |
|
| 263 | + public function pushData($key, $value) |
|
| 264 | + { |
|
| 265 | + if (isset($this->jsdata[ $key ]) |
|
| 266 | + && ! is_array($this->jsdata[ $key ]) |
|
| 267 | + ) { |
|
| 268 | + if (! $this->debug()) { |
|
| 269 | + return; |
|
| 270 | + } |
|
| 271 | + throw new InvalidArgumentException( |
|
| 272 | + sprintf( |
|
| 273 | + __( |
|
| 274 | + 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
|
| 275 | 275 | push values to this data element when it is an array.', |
| 276 | - 'event_espresso' |
|
| 277 | - ), |
|
| 278 | - $key, |
|
| 279 | - __METHOD__ |
|
| 280 | - ) |
|
| 281 | - ); |
|
| 282 | - } |
|
| 283 | - if ( ! isset( $this->jsdata[ $key ] ) ) { |
|
| 284 | - $this->jsdata[ $key ] = is_array($value) ? $value : [$value]; |
|
| 285 | - } else { |
|
| 286 | - $this->jsdata[ $key ] = array_merge( $this->jsdata[$key], (array) $value); |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Used to set content used by javascript for a template. |
|
| 293 | - * Note: Overrides of existing registered templates are not allowed. |
|
| 294 | - * |
|
| 295 | - * @param string $template_reference |
|
| 296 | - * @param string $template_content |
|
| 297 | - * @throws InvalidArgumentException |
|
| 298 | - */ |
|
| 299 | - public function addTemplate($template_reference, $template_content) |
|
| 300 | - { |
|
| 301 | - if (! isset($this->jsdata['templates'])) { |
|
| 302 | - $this->jsdata['templates'] = array(); |
|
| 303 | - } |
|
| 304 | - //no overrides allowed. |
|
| 305 | - if (isset($this->jsdata['templates'][ $template_reference ])) { |
|
| 306 | - if (! $this->debug()) { |
|
| 307 | - return; |
|
| 308 | - } |
|
| 309 | - throw new InvalidArgumentException( |
|
| 310 | - sprintf( |
|
| 311 | - __( |
|
| 312 | - 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
|
| 313 | - 'event_espresso' |
|
| 314 | - ), |
|
| 315 | - $template_reference |
|
| 316 | - ) |
|
| 317 | - ); |
|
| 318 | - } |
|
| 319 | - $this->jsdata['templates'][ $template_reference ] = $template_content; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Retrieve the template content already registered for the given reference. |
|
| 325 | - * |
|
| 326 | - * @param string $template_reference |
|
| 327 | - * @return string |
|
| 328 | - */ |
|
| 329 | - public function getTemplate($template_reference) |
|
| 330 | - { |
|
| 331 | - return isset($this->jsdata['templates'][ $template_reference ]) |
|
| 332 | - ? $this->jsdata['templates'][ $template_reference ] |
|
| 333 | - : ''; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Retrieve registered data. |
|
| 339 | - * |
|
| 340 | - * @param string $key Name of key to attach data to. |
|
| 341 | - * @return mixed If there is no for the given key, then false is returned. |
|
| 342 | - */ |
|
| 343 | - public function getData($key) |
|
| 344 | - { |
|
| 345 | - return isset($this->jsdata[ $key ]) |
|
| 346 | - ? $this->jsdata[ $key ] |
|
| 347 | - : false; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * Verifies whether the given data exists already on the jsdata array. |
|
| 353 | - * Overriding data is not allowed. |
|
| 354 | - * |
|
| 355 | - * @param string $key Index for data. |
|
| 356 | - * @return bool If valid then return true. |
|
| 357 | - * @throws InvalidArgumentException if data already exists. |
|
| 358 | - */ |
|
| 359 | - protected function verifyDataNotExisting($key) |
|
| 360 | - { |
|
| 361 | - if (isset($this->jsdata[ $key ])) { |
|
| 362 | - if (! $this->debug()) { |
|
| 363 | - return false; |
|
| 364 | - } |
|
| 365 | - if (is_array($this->jsdata[ $key ])) { |
|
| 366 | - throw new InvalidArgumentException( |
|
| 367 | - sprintf( |
|
| 368 | - __( |
|
| 369 | - 'The value for %1$s already exists in the Registry::eejs object. |
|
| 276 | + 'event_espresso' |
|
| 277 | + ), |
|
| 278 | + $key, |
|
| 279 | + __METHOD__ |
|
| 280 | + ) |
|
| 281 | + ); |
|
| 282 | + } |
|
| 283 | + if ( ! isset( $this->jsdata[ $key ] ) ) { |
|
| 284 | + $this->jsdata[ $key ] = is_array($value) ? $value : [$value]; |
|
| 285 | + } else { |
|
| 286 | + $this->jsdata[ $key ] = array_merge( $this->jsdata[$key], (array) $value); |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Used to set content used by javascript for a template. |
|
| 293 | + * Note: Overrides of existing registered templates are not allowed. |
|
| 294 | + * |
|
| 295 | + * @param string $template_reference |
|
| 296 | + * @param string $template_content |
|
| 297 | + * @throws InvalidArgumentException |
|
| 298 | + */ |
|
| 299 | + public function addTemplate($template_reference, $template_content) |
|
| 300 | + { |
|
| 301 | + if (! isset($this->jsdata['templates'])) { |
|
| 302 | + $this->jsdata['templates'] = array(); |
|
| 303 | + } |
|
| 304 | + //no overrides allowed. |
|
| 305 | + if (isset($this->jsdata['templates'][ $template_reference ])) { |
|
| 306 | + if (! $this->debug()) { |
|
| 307 | + return; |
|
| 308 | + } |
|
| 309 | + throw new InvalidArgumentException( |
|
| 310 | + sprintf( |
|
| 311 | + __( |
|
| 312 | + 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
|
| 313 | + 'event_espresso' |
|
| 314 | + ), |
|
| 315 | + $template_reference |
|
| 316 | + ) |
|
| 317 | + ); |
|
| 318 | + } |
|
| 319 | + $this->jsdata['templates'][ $template_reference ] = $template_content; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Retrieve the template content already registered for the given reference. |
|
| 325 | + * |
|
| 326 | + * @param string $template_reference |
|
| 327 | + * @return string |
|
| 328 | + */ |
|
| 329 | + public function getTemplate($template_reference) |
|
| 330 | + { |
|
| 331 | + return isset($this->jsdata['templates'][ $template_reference ]) |
|
| 332 | + ? $this->jsdata['templates'][ $template_reference ] |
|
| 333 | + : ''; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Retrieve registered data. |
|
| 339 | + * |
|
| 340 | + * @param string $key Name of key to attach data to. |
|
| 341 | + * @return mixed If there is no for the given key, then false is returned. |
|
| 342 | + */ |
|
| 343 | + public function getData($key) |
|
| 344 | + { |
|
| 345 | + return isset($this->jsdata[ $key ]) |
|
| 346 | + ? $this->jsdata[ $key ] |
|
| 347 | + : false; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * Verifies whether the given data exists already on the jsdata array. |
|
| 353 | + * Overriding data is not allowed. |
|
| 354 | + * |
|
| 355 | + * @param string $key Index for data. |
|
| 356 | + * @return bool If valid then return true. |
|
| 357 | + * @throws InvalidArgumentException if data already exists. |
|
| 358 | + */ |
|
| 359 | + protected function verifyDataNotExisting($key) |
|
| 360 | + { |
|
| 361 | + if (isset($this->jsdata[ $key ])) { |
|
| 362 | + if (! $this->debug()) { |
|
| 363 | + return false; |
|
| 364 | + } |
|
| 365 | + if (is_array($this->jsdata[ $key ])) { |
|
| 366 | + throw new InvalidArgumentException( |
|
| 367 | + sprintf( |
|
| 368 | + __( |
|
| 369 | + 'The value for %1$s already exists in the Registry::eejs object. |
|
| 370 | 370 | Overrides are not allowed. Since the value of this data is an array, you may want to use the |
| 371 | 371 | %2$s method to push your value to the array.', |
| 372 | - 'event_espresso' |
|
| 373 | - ), |
|
| 374 | - $key, |
|
| 375 | - 'pushData()' |
|
| 376 | - ) |
|
| 377 | - ); |
|
| 378 | - } |
|
| 379 | - throw new InvalidArgumentException( |
|
| 380 | - sprintf( |
|
| 381 | - __( |
|
| 382 | - 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
|
| 372 | + 'event_espresso' |
|
| 373 | + ), |
|
| 374 | + $key, |
|
| 375 | + 'pushData()' |
|
| 376 | + ) |
|
| 377 | + ); |
|
| 378 | + } |
|
| 379 | + throw new InvalidArgumentException( |
|
| 380 | + sprintf( |
|
| 381 | + __( |
|
| 382 | + 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
|
| 383 | 383 | allowed. Consider attaching your value to a different key', |
| 384 | - 'event_espresso' |
|
| 385 | - ), |
|
| 386 | - $key |
|
| 387 | - ) |
|
| 388 | - ); |
|
| 389 | - } |
|
| 390 | - return true; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * Get the actual asset path for asset manifests. |
|
| 396 | - * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
|
| 397 | - * |
|
| 398 | - * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
|
| 399 | - * asset file location. |
|
| 400 | - * @param string $chunk_name |
|
| 401 | - * @param string $asset_type |
|
| 402 | - * @return string |
|
| 403 | - * @since 4.9.59.p |
|
| 404 | - */ |
|
| 405 | - public function getAssetUrl($namespace, $chunk_name, $asset_type) |
|
| 406 | - { |
|
| 407 | - $url = isset( |
|
| 408 | - $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ], |
|
| 409 | - $this->manifest_data[ $namespace ]['url_base'] |
|
| 410 | - ) |
|
| 411 | - ? $this->manifest_data[ $namespace ]['url_base'] |
|
| 412 | - . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ] |
|
| 413 | - : $chunk_name; |
|
| 414 | - |
|
| 415 | - return apply_filters( |
|
| 416 | - 'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl', |
|
| 417 | - $url, |
|
| 418 | - $namespace, |
|
| 419 | - $chunk_name, |
|
| 420 | - $asset_type |
|
| 421 | - ); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Return the url to a js file for the given namespace and chunk name. |
|
| 428 | - * |
|
| 429 | - * @param string $namespace |
|
| 430 | - * @param string $chunk_name |
|
| 431 | - * @return string |
|
| 432 | - */ |
|
| 433 | - public function getJsUrl($namespace, $chunk_name) |
|
| 434 | - { |
|
| 435 | - return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS); |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * Return the url to a css file for the given namespace and chunk name. |
|
| 441 | - * |
|
| 442 | - * @param string $namespace |
|
| 443 | - * @param string $chunk_name |
|
| 444 | - * @return string |
|
| 445 | - */ |
|
| 446 | - public function getCssUrl($namespace, $chunk_name) |
|
| 447 | - { |
|
| 448 | - return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * @since 4.9.62.p |
|
| 454 | - * @throws InvalidArgumentException |
|
| 455 | - * @throws InvalidFilePathException |
|
| 456 | - */ |
|
| 457 | - public function registerManifestFiles() |
|
| 458 | - { |
|
| 459 | - $manifest_files = $this->assets->getManifestFiles(); |
|
| 460 | - foreach ($manifest_files as $manifest_file) { |
|
| 461 | - $this->registerManifestFile( |
|
| 462 | - $manifest_file->assetNamespace(), |
|
| 463 | - $manifest_file->urlBase(), |
|
| 464 | - $manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST |
|
| 465 | - ); |
|
| 466 | - } |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * Used to register a js/css manifest file with the registered_manifest_files property. |
|
| 472 | - * |
|
| 473 | - * @param string $namespace Provided to associate the manifest file with a specific namespace. |
|
| 474 | - * @param string $url_base The url base for the manifest file location. |
|
| 475 | - * @param string $manifest_file The absolute path to the manifest file. |
|
| 476 | - * @throws InvalidArgumentException |
|
| 477 | - * @throws InvalidFilePathException |
|
| 478 | - * @since 4.9.59.p |
|
| 479 | - */ |
|
| 480 | - public function registerManifestFile($namespace, $url_base, $manifest_file) |
|
| 481 | - { |
|
| 482 | - if (isset($this->manifest_data[ $namespace ])) { |
|
| 483 | - if (! $this->debug()) { |
|
| 484 | - return; |
|
| 485 | - } |
|
| 486 | - throw new InvalidArgumentException( |
|
| 487 | - sprintf( |
|
| 488 | - esc_html__( |
|
| 489 | - 'The namespace for this manifest file has already been registered, choose a namespace other than %s', |
|
| 490 | - 'event_espresso' |
|
| 491 | - ), |
|
| 492 | - $namespace |
|
| 493 | - ) |
|
| 494 | - ); |
|
| 495 | - } |
|
| 496 | - if (filter_var($url_base, FILTER_VALIDATE_URL) === false) { |
|
| 497 | - if (is_admin()) { |
|
| 498 | - EE_Error::add_error( |
|
| 499 | - sprintf( |
|
| 500 | - esc_html__( |
|
| 501 | - 'The url given for %1$s assets is invalid. The url provided was: "%2$s". This usually happens when another plugin or theme on a site is using the "%3$s" filter or has an invalid url set for the "%4$s" constant', |
|
| 502 | - 'event_espresso' |
|
| 503 | - ), |
|
| 504 | - 'Event Espresso', |
|
| 505 | - $url_base, |
|
| 506 | - 'plugins_url', |
|
| 507 | - 'WP_PLUGIN_URL' |
|
| 508 | - ), |
|
| 509 | - __FILE__, |
|
| 510 | - __FUNCTION__, |
|
| 511 | - __LINE__ |
|
| 512 | - ); |
|
| 513 | - } |
|
| 514 | - return; |
|
| 515 | - } |
|
| 516 | - $this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file); |
|
| 517 | - if (! isset($this->manifest_data[ $namespace ]['url_base'])) { |
|
| 518 | - $this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base); |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Decodes json from the provided manifest file. |
|
| 525 | - * |
|
| 526 | - * @since 4.9.59.p |
|
| 527 | - * @param string $manifest_file Path to manifest file. |
|
| 528 | - * @return array |
|
| 529 | - * @throws InvalidFilePathException |
|
| 530 | - */ |
|
| 531 | - private function decodeManifestFile($manifest_file) |
|
| 532 | - { |
|
| 533 | - if (! file_exists($manifest_file)) { |
|
| 534 | - throw new InvalidFilePathException($manifest_file); |
|
| 535 | - } |
|
| 536 | - return json_decode(file_get_contents($manifest_file), true); |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * This is used to set registered script handles that have data. |
|
| 542 | - * |
|
| 543 | - * @param string $script_handle |
|
| 544 | - */ |
|
| 545 | - private function addRegisteredScriptHandlesWithData($script_handle) |
|
| 546 | - { |
|
| 547 | - $this->script_handles_with_data[ $script_handle ] = $script_handle; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - |
|
| 551 | - /**i |
|
| 384 | + 'event_espresso' |
|
| 385 | + ), |
|
| 386 | + $key |
|
| 387 | + ) |
|
| 388 | + ); |
|
| 389 | + } |
|
| 390 | + return true; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * Get the actual asset path for asset manifests. |
|
| 396 | + * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
|
| 397 | + * |
|
| 398 | + * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
|
| 399 | + * asset file location. |
|
| 400 | + * @param string $chunk_name |
|
| 401 | + * @param string $asset_type |
|
| 402 | + * @return string |
|
| 403 | + * @since 4.9.59.p |
|
| 404 | + */ |
|
| 405 | + public function getAssetUrl($namespace, $chunk_name, $asset_type) |
|
| 406 | + { |
|
| 407 | + $url = isset( |
|
| 408 | + $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ], |
|
| 409 | + $this->manifest_data[ $namespace ]['url_base'] |
|
| 410 | + ) |
|
| 411 | + ? $this->manifest_data[ $namespace ]['url_base'] |
|
| 412 | + . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ] |
|
| 413 | + : $chunk_name; |
|
| 414 | + |
|
| 415 | + return apply_filters( |
|
| 416 | + 'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl', |
|
| 417 | + $url, |
|
| 418 | + $namespace, |
|
| 419 | + $chunk_name, |
|
| 420 | + $asset_type |
|
| 421 | + ); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Return the url to a js file for the given namespace and chunk name. |
|
| 428 | + * |
|
| 429 | + * @param string $namespace |
|
| 430 | + * @param string $chunk_name |
|
| 431 | + * @return string |
|
| 432 | + */ |
|
| 433 | + public function getJsUrl($namespace, $chunk_name) |
|
| 434 | + { |
|
| 435 | + return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS); |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * Return the url to a css file for the given namespace and chunk name. |
|
| 441 | + * |
|
| 442 | + * @param string $namespace |
|
| 443 | + * @param string $chunk_name |
|
| 444 | + * @return string |
|
| 445 | + */ |
|
| 446 | + public function getCssUrl($namespace, $chunk_name) |
|
| 447 | + { |
|
| 448 | + return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * @since 4.9.62.p |
|
| 454 | + * @throws InvalidArgumentException |
|
| 455 | + * @throws InvalidFilePathException |
|
| 456 | + */ |
|
| 457 | + public function registerManifestFiles() |
|
| 458 | + { |
|
| 459 | + $manifest_files = $this->assets->getManifestFiles(); |
|
| 460 | + foreach ($manifest_files as $manifest_file) { |
|
| 461 | + $this->registerManifestFile( |
|
| 462 | + $manifest_file->assetNamespace(), |
|
| 463 | + $manifest_file->urlBase(), |
|
| 464 | + $manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST |
|
| 465 | + ); |
|
| 466 | + } |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * Used to register a js/css manifest file with the registered_manifest_files property. |
|
| 472 | + * |
|
| 473 | + * @param string $namespace Provided to associate the manifest file with a specific namespace. |
|
| 474 | + * @param string $url_base The url base for the manifest file location. |
|
| 475 | + * @param string $manifest_file The absolute path to the manifest file. |
|
| 476 | + * @throws InvalidArgumentException |
|
| 477 | + * @throws InvalidFilePathException |
|
| 478 | + * @since 4.9.59.p |
|
| 479 | + */ |
|
| 480 | + public function registerManifestFile($namespace, $url_base, $manifest_file) |
|
| 481 | + { |
|
| 482 | + if (isset($this->manifest_data[ $namespace ])) { |
|
| 483 | + if (! $this->debug()) { |
|
| 484 | + return; |
|
| 485 | + } |
|
| 486 | + throw new InvalidArgumentException( |
|
| 487 | + sprintf( |
|
| 488 | + esc_html__( |
|
| 489 | + 'The namespace for this manifest file has already been registered, choose a namespace other than %s', |
|
| 490 | + 'event_espresso' |
|
| 491 | + ), |
|
| 492 | + $namespace |
|
| 493 | + ) |
|
| 494 | + ); |
|
| 495 | + } |
|
| 496 | + if (filter_var($url_base, FILTER_VALIDATE_URL) === false) { |
|
| 497 | + if (is_admin()) { |
|
| 498 | + EE_Error::add_error( |
|
| 499 | + sprintf( |
|
| 500 | + esc_html__( |
|
| 501 | + 'The url given for %1$s assets is invalid. The url provided was: "%2$s". This usually happens when another plugin or theme on a site is using the "%3$s" filter or has an invalid url set for the "%4$s" constant', |
|
| 502 | + 'event_espresso' |
|
| 503 | + ), |
|
| 504 | + 'Event Espresso', |
|
| 505 | + $url_base, |
|
| 506 | + 'plugins_url', |
|
| 507 | + 'WP_PLUGIN_URL' |
|
| 508 | + ), |
|
| 509 | + __FILE__, |
|
| 510 | + __FUNCTION__, |
|
| 511 | + __LINE__ |
|
| 512 | + ); |
|
| 513 | + } |
|
| 514 | + return; |
|
| 515 | + } |
|
| 516 | + $this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file); |
|
| 517 | + if (! isset($this->manifest_data[ $namespace ]['url_base'])) { |
|
| 518 | + $this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base); |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Decodes json from the provided manifest file. |
|
| 525 | + * |
|
| 526 | + * @since 4.9.59.p |
|
| 527 | + * @param string $manifest_file Path to manifest file. |
|
| 528 | + * @return array |
|
| 529 | + * @throws InvalidFilePathException |
|
| 530 | + */ |
|
| 531 | + private function decodeManifestFile($manifest_file) |
|
| 532 | + { |
|
| 533 | + if (! file_exists($manifest_file)) { |
|
| 534 | + throw new InvalidFilePathException($manifest_file); |
|
| 535 | + } |
|
| 536 | + return json_decode(file_get_contents($manifest_file), true); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * This is used to set registered script handles that have data. |
|
| 542 | + * |
|
| 543 | + * @param string $script_handle |
|
| 544 | + */ |
|
| 545 | + private function addRegisteredScriptHandlesWithData($script_handle) |
|
| 546 | + { |
|
| 547 | + $this->script_handles_with_data[ $script_handle ] = $script_handle; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + |
|
| 551 | + /**i |
|
| 552 | 552 | * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
| 553 | 553 | * Dependency stored in WP_Scripts if its set. |
| 554 | 554 | */ |
| 555 | - private function removeAlreadyRegisteredDataForScriptHandles() |
|
| 556 | - { |
|
| 557 | - if (empty($this->script_handles_with_data)) { |
|
| 558 | - return; |
|
| 559 | - } |
|
| 560 | - foreach ($this->script_handles_with_data as $script_handle) { |
|
| 561 | - $this->removeAlreadyRegisteredDataForScriptHandle($script_handle); |
|
| 562 | - } |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * Removes any data dependency registered in WP_Scripts if its set. |
|
| 568 | - * |
|
| 569 | - * @param string $script_handle |
|
| 570 | - */ |
|
| 571 | - private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
|
| 572 | - { |
|
| 573 | - if (isset($this->script_handles_with_data[ $script_handle ])) { |
|
| 574 | - global $wp_scripts; |
|
| 575 | - $unset_handle = false; |
|
| 576 | - if ($wp_scripts->get_data($script_handle, 'data')) { |
|
| 577 | - unset($wp_scripts->registered[ $script_handle ]->extra['data']); |
|
| 578 | - $unset_handle = true; |
|
| 579 | - } |
|
| 580 | - //deal with inline_scripts |
|
| 581 | - if ($wp_scripts->get_data($script_handle, 'before')) { |
|
| 582 | - unset($wp_scripts->registered[ $script_handle ]->extra['before']); |
|
| 583 | - $unset_handle = true; |
|
| 584 | - } |
|
| 585 | - if ($wp_scripts->get_data($script_handle, 'after')) { |
|
| 586 | - unset($wp_scripts->registered[ $script_handle ]->extra['after']); |
|
| 587 | - } |
|
| 588 | - if ($unset_handle) { |
|
| 589 | - unset($this->script_handles_with_data[ $script_handle ]); |
|
| 590 | - } |
|
| 591 | - } |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * register translations for a registered script |
|
| 597 | - * |
|
| 598 | - * @param string $handle |
|
| 599 | - */ |
|
| 600 | - public function registerTranslation($handle) |
|
| 601 | - { |
|
| 602 | - $this->i18n_registry->registerScriptI18n($handle); |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * @since 4.9.63.p |
|
| 608 | - * @return bool |
|
| 609 | - */ |
|
| 610 | - private function debug() |
|
| 611 | - { |
|
| 612 | - return apply_filters( |
|
| 613 | - 'FHEE__EventEspresso_core_services_assets_Registry__debug', |
|
| 614 | - defined('EE_DEBUG') && EE_DEBUG |
|
| 615 | - ); |
|
| 616 | - } |
|
| 555 | + private function removeAlreadyRegisteredDataForScriptHandles() |
|
| 556 | + { |
|
| 557 | + if (empty($this->script_handles_with_data)) { |
|
| 558 | + return; |
|
| 559 | + } |
|
| 560 | + foreach ($this->script_handles_with_data as $script_handle) { |
|
| 561 | + $this->removeAlreadyRegisteredDataForScriptHandle($script_handle); |
|
| 562 | + } |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * Removes any data dependency registered in WP_Scripts if its set. |
|
| 568 | + * |
|
| 569 | + * @param string $script_handle |
|
| 570 | + */ |
|
| 571 | + private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
|
| 572 | + { |
|
| 573 | + if (isset($this->script_handles_with_data[ $script_handle ])) { |
|
| 574 | + global $wp_scripts; |
|
| 575 | + $unset_handle = false; |
|
| 576 | + if ($wp_scripts->get_data($script_handle, 'data')) { |
|
| 577 | + unset($wp_scripts->registered[ $script_handle ]->extra['data']); |
|
| 578 | + $unset_handle = true; |
|
| 579 | + } |
|
| 580 | + //deal with inline_scripts |
|
| 581 | + if ($wp_scripts->get_data($script_handle, 'before')) { |
|
| 582 | + unset($wp_scripts->registered[ $script_handle ]->extra['before']); |
|
| 583 | + $unset_handle = true; |
|
| 584 | + } |
|
| 585 | + if ($wp_scripts->get_data($script_handle, 'after')) { |
|
| 586 | + unset($wp_scripts->registered[ $script_handle ]->extra['after']); |
|
| 587 | + } |
|
| 588 | + if ($unset_handle) { |
|
| 589 | + unset($this->script_handles_with_data[ $script_handle ]); |
|
| 590 | + } |
|
| 591 | + } |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * register translations for a registered script |
|
| 597 | + * |
|
| 598 | + * @param string $handle |
|
| 599 | + */ |
|
| 600 | + public function registerTranslation($handle) |
|
| 601 | + { |
|
| 602 | + $this->i18n_registry->registerScriptI18n($handle); |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * @since 4.9.63.p |
|
| 608 | + * @return bool |
|
| 609 | + */ |
|
| 610 | + private function debug() |
|
| 611 | + { |
|
| 612 | + return apply_filters( |
|
| 613 | + 'FHEE__EventEspresso_core_services_assets_Registry__debug', |
|
| 614 | + defined('EE_DEBUG') && EE_DEBUG |
|
| 615 | + ); |
|
| 616 | + } |
|
| 617 | 617 | } |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | $script->version(), |
| 148 | 148 | $script->loadInFooter() |
| 149 | 149 | ); |
| 150 | - if (! $registered && $this->debug()) { |
|
| 150 | + if ( ! $registered && $this->debug()) { |
|
| 151 | 151 | throw new AssetRegistrationException($script->handle()); |
| 152 | 152 | } |
| 153 | 153 | $script->setRegistered($registered); |
@@ -207,7 +207,7 @@ discard block |
||
| 207 | 207 | $this->removeAlreadyRegisteredDataForScriptHandles(); |
| 208 | 208 | wp_add_inline_script( |
| 209 | 209 | 'eejs-core', |
| 210 | - 'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
|
| 210 | + 'var eejsdata='.wp_json_encode(array('data' => $this->jsdata)), |
|
| 211 | 211 | 'before' |
| 212 | 212 | ); |
| 213 | 213 | $scripts = $this->assets->getJavascriptAssetsWithData(); |
@@ -237,7 +237,7 @@ discard block |
||
| 237 | 237 | public function addData($key, $value) |
| 238 | 238 | { |
| 239 | 239 | if ($this->verifyDataNotExisting($key)) { |
| 240 | - $this->jsdata[ $key ] = $value; |
|
| 240 | + $this->jsdata[$key] = $value; |
|
| 241 | 241 | } |
| 242 | 242 | } |
| 243 | 243 | |
@@ -262,10 +262,10 @@ discard block |
||
| 262 | 262 | */ |
| 263 | 263 | public function pushData($key, $value) |
| 264 | 264 | { |
| 265 | - if (isset($this->jsdata[ $key ]) |
|
| 266 | - && ! is_array($this->jsdata[ $key ]) |
|
| 265 | + if (isset($this->jsdata[$key]) |
|
| 266 | + && ! is_array($this->jsdata[$key]) |
|
| 267 | 267 | ) { |
| 268 | - if (! $this->debug()) { |
|
| 268 | + if ( ! $this->debug()) { |
|
| 269 | 269 | return; |
| 270 | 270 | } |
| 271 | 271 | throw new InvalidArgumentException( |
@@ -280,10 +280,10 @@ discard block |
||
| 280 | 280 | ) |
| 281 | 281 | ); |
| 282 | 282 | } |
| 283 | - if ( ! isset( $this->jsdata[ $key ] ) ) { |
|
| 284 | - $this->jsdata[ $key ] = is_array($value) ? $value : [$value]; |
|
| 283 | + if ( ! isset($this->jsdata[$key])) { |
|
| 284 | + $this->jsdata[$key] = is_array($value) ? $value : [$value]; |
|
| 285 | 285 | } else { |
| 286 | - $this->jsdata[ $key ] = array_merge( $this->jsdata[$key], (array) $value); |
|
| 286 | + $this->jsdata[$key] = array_merge($this->jsdata[$key], (array) $value); |
|
| 287 | 287 | } |
| 288 | 288 | } |
| 289 | 289 | |
@@ -298,12 +298,12 @@ discard block |
||
| 298 | 298 | */ |
| 299 | 299 | public function addTemplate($template_reference, $template_content) |
| 300 | 300 | { |
| 301 | - if (! isset($this->jsdata['templates'])) { |
|
| 301 | + if ( ! isset($this->jsdata['templates'])) { |
|
| 302 | 302 | $this->jsdata['templates'] = array(); |
| 303 | 303 | } |
| 304 | 304 | //no overrides allowed. |
| 305 | - if (isset($this->jsdata['templates'][ $template_reference ])) { |
|
| 306 | - if (! $this->debug()) { |
|
| 305 | + if (isset($this->jsdata['templates'][$template_reference])) { |
|
| 306 | + if ( ! $this->debug()) { |
|
| 307 | 307 | return; |
| 308 | 308 | } |
| 309 | 309 | throw new InvalidArgumentException( |
@@ -316,7 +316,7 @@ discard block |
||
| 316 | 316 | ) |
| 317 | 317 | ); |
| 318 | 318 | } |
| 319 | - $this->jsdata['templates'][ $template_reference ] = $template_content; |
|
| 319 | + $this->jsdata['templates'][$template_reference] = $template_content; |
|
| 320 | 320 | } |
| 321 | 321 | |
| 322 | 322 | |
@@ -328,8 +328,8 @@ discard block |
||
| 328 | 328 | */ |
| 329 | 329 | public function getTemplate($template_reference) |
| 330 | 330 | { |
| 331 | - return isset($this->jsdata['templates'][ $template_reference ]) |
|
| 332 | - ? $this->jsdata['templates'][ $template_reference ] |
|
| 331 | + return isset($this->jsdata['templates'][$template_reference]) |
|
| 332 | + ? $this->jsdata['templates'][$template_reference] |
|
| 333 | 333 | : ''; |
| 334 | 334 | } |
| 335 | 335 | |
@@ -342,8 +342,8 @@ discard block |
||
| 342 | 342 | */ |
| 343 | 343 | public function getData($key) |
| 344 | 344 | { |
| 345 | - return isset($this->jsdata[ $key ]) |
|
| 346 | - ? $this->jsdata[ $key ] |
|
| 345 | + return isset($this->jsdata[$key]) |
|
| 346 | + ? $this->jsdata[$key] |
|
| 347 | 347 | : false; |
| 348 | 348 | } |
| 349 | 349 | |
@@ -358,11 +358,11 @@ discard block |
||
| 358 | 358 | */ |
| 359 | 359 | protected function verifyDataNotExisting($key) |
| 360 | 360 | { |
| 361 | - if (isset($this->jsdata[ $key ])) { |
|
| 362 | - if (! $this->debug()) { |
|
| 361 | + if (isset($this->jsdata[$key])) { |
|
| 362 | + if ( ! $this->debug()) { |
|
| 363 | 363 | return false; |
| 364 | 364 | } |
| 365 | - if (is_array($this->jsdata[ $key ])) { |
|
| 365 | + if (is_array($this->jsdata[$key])) { |
|
| 366 | 366 | throw new InvalidArgumentException( |
| 367 | 367 | sprintf( |
| 368 | 368 | __( |
@@ -405,11 +405,11 @@ discard block |
||
| 405 | 405 | public function getAssetUrl($namespace, $chunk_name, $asset_type) |
| 406 | 406 | { |
| 407 | 407 | $url = isset( |
| 408 | - $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ], |
|
| 409 | - $this->manifest_data[ $namespace ]['url_base'] |
|
| 408 | + $this->manifest_data[$namespace][$chunk_name.'.'.$asset_type], |
|
| 409 | + $this->manifest_data[$namespace]['url_base'] |
|
| 410 | 410 | ) |
| 411 | - ? $this->manifest_data[ $namespace ]['url_base'] |
|
| 412 | - . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ] |
|
| 411 | + ? $this->manifest_data[$namespace]['url_base'] |
|
| 412 | + . $this->manifest_data[$namespace][$chunk_name.'.'.$asset_type] |
|
| 413 | 413 | : $chunk_name; |
| 414 | 414 | |
| 415 | 415 | return apply_filters( |
@@ -461,7 +461,7 @@ discard block |
||
| 461 | 461 | $this->registerManifestFile( |
| 462 | 462 | $manifest_file->assetNamespace(), |
| 463 | 463 | $manifest_file->urlBase(), |
| 464 | - $manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST |
|
| 464 | + $manifest_file->filepath().Registry::FILE_NAME_BUILD_MANIFEST |
|
| 465 | 465 | ); |
| 466 | 466 | } |
| 467 | 467 | } |
@@ -479,8 +479,8 @@ discard block |
||
| 479 | 479 | */ |
| 480 | 480 | public function registerManifestFile($namespace, $url_base, $manifest_file) |
| 481 | 481 | { |
| 482 | - if (isset($this->manifest_data[ $namespace ])) { |
|
| 483 | - if (! $this->debug()) { |
|
| 482 | + if (isset($this->manifest_data[$namespace])) { |
|
| 483 | + if ( ! $this->debug()) { |
|
| 484 | 484 | return; |
| 485 | 485 | } |
| 486 | 486 | throw new InvalidArgumentException( |
@@ -513,9 +513,9 @@ discard block |
||
| 513 | 513 | } |
| 514 | 514 | return; |
| 515 | 515 | } |
| 516 | - $this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file); |
|
| 517 | - if (! isset($this->manifest_data[ $namespace ]['url_base'])) { |
|
| 518 | - $this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base); |
|
| 516 | + $this->manifest_data[$namespace] = $this->decodeManifestFile($manifest_file); |
|
| 517 | + if ( ! isset($this->manifest_data[$namespace]['url_base'])) { |
|
| 518 | + $this->manifest_data[$namespace]['url_base'] = trailingslashit($url_base); |
|
| 519 | 519 | } |
| 520 | 520 | } |
| 521 | 521 | |
@@ -530,7 +530,7 @@ discard block |
||
| 530 | 530 | */ |
| 531 | 531 | private function decodeManifestFile($manifest_file) |
| 532 | 532 | { |
| 533 | - if (! file_exists($manifest_file)) { |
|
| 533 | + if ( ! file_exists($manifest_file)) { |
|
| 534 | 534 | throw new InvalidFilePathException($manifest_file); |
| 535 | 535 | } |
| 536 | 536 | return json_decode(file_get_contents($manifest_file), true); |
@@ -544,7 +544,7 @@ discard block |
||
| 544 | 544 | */ |
| 545 | 545 | private function addRegisteredScriptHandlesWithData($script_handle) |
| 546 | 546 | { |
| 547 | - $this->script_handles_with_data[ $script_handle ] = $script_handle; |
|
| 547 | + $this->script_handles_with_data[$script_handle] = $script_handle; |
|
| 548 | 548 | } |
| 549 | 549 | |
| 550 | 550 | |
@@ -570,23 +570,23 @@ discard block |
||
| 570 | 570 | */ |
| 571 | 571 | private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
| 572 | 572 | { |
| 573 | - if (isset($this->script_handles_with_data[ $script_handle ])) { |
|
| 573 | + if (isset($this->script_handles_with_data[$script_handle])) { |
|
| 574 | 574 | global $wp_scripts; |
| 575 | 575 | $unset_handle = false; |
| 576 | 576 | if ($wp_scripts->get_data($script_handle, 'data')) { |
| 577 | - unset($wp_scripts->registered[ $script_handle ]->extra['data']); |
|
| 577 | + unset($wp_scripts->registered[$script_handle]->extra['data']); |
|
| 578 | 578 | $unset_handle = true; |
| 579 | 579 | } |
| 580 | 580 | //deal with inline_scripts |
| 581 | 581 | if ($wp_scripts->get_data($script_handle, 'before')) { |
| 582 | - unset($wp_scripts->registered[ $script_handle ]->extra['before']); |
|
| 582 | + unset($wp_scripts->registered[$script_handle]->extra['before']); |
|
| 583 | 583 | $unset_handle = true; |
| 584 | 584 | } |
| 585 | 585 | if ($wp_scripts->get_data($script_handle, 'after')) { |
| 586 | - unset($wp_scripts->registered[ $script_handle ]->extra['after']); |
|
| 586 | + unset($wp_scripts->registered[$script_handle]->extra['after']); |
|
| 587 | 587 | } |
| 588 | 588 | if ($unset_handle) { |
| 589 | - unset($this->script_handles_with_data[ $script_handle ]); |
|
| 589 | + unset($this->script_handles_with_data[$script_handle]); |
|
| 590 | 590 | } |
| 591 | 591 | } |
| 592 | 592 | } |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | public function ticket_status($display = false, $remaining = null) |
| 130 | 130 | { |
| 131 | 131 | $remaining = is_bool($remaining) ? $remaining : $this->is_remaining(); |
| 132 | - if (! $remaining) { |
|
| 132 | + if ( ! $remaining) { |
|
| 133 | 133 | return $display ? EEH_Template::pretty_status(EE_Ticket::sold_out, false, 'sentence') : EE_Ticket::sold_out; |
| 134 | 134 | } |
| 135 | 135 | if ($this->get('TKT_deleted')) { |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | : ''; |
| 248 | 248 | $last_date = $this->last_datetime() instanceof EE_Datetime ? $this->last_datetime()->get_i18n_datetime('DTT_EVT_end', $dt_frmt) : ''; |
| 249 | 249 | |
| 250 | - return $first_date && $last_date ? $first_date . $conjunction . $last_date : ''; |
|
| 250 | + return $first_date && $last_date ? $first_date.$conjunction.$last_date : ''; |
|
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | */ |
| 275 | 275 | public function datetimes($query_params = array()) |
| 276 | 276 | { |
| 277 | - if (! isset($query_params['order_by'])) { |
|
| 277 | + if ( ! isset($query_params['order_by'])) { |
|
| 278 | 278 | $query_params['order_by']['DTT_order'] = 'ASC'; |
| 279 | 279 | } |
| 280 | 280 | return $this->get_many_related('Datetime', $query_params); |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | if (empty($tickets_sold['datetime'])) { |
| 320 | 320 | return $total; |
| 321 | 321 | } |
| 322 | - if (! empty($dtt_id) && ! isset($tickets_sold['datetime'][ $dtt_id ])) { |
|
| 322 | + if ( ! empty($dtt_id) && ! isset($tickets_sold['datetime'][$dtt_id])) { |
|
| 323 | 323 | EE_Error::add_error( |
| 324 | 324 | __( |
| 325 | 325 | 'You\'ve requested the amount of tickets sold for a given ticket and datetime, however there are no records for the datetime id you included. Are you SURE that is a datetime related to this ticket?', |
@@ -331,7 +331,7 @@ discard block |
||
| 331 | 331 | ); |
| 332 | 332 | return $total; |
| 333 | 333 | } |
| 334 | - return empty($dtt_id) ? $tickets_sold['datetime'] : $tickets_sold['datetime'][ $dtt_id ]; |
|
| 334 | + return empty($dtt_id) ? $tickets_sold['datetime'] : $tickets_sold['datetime'][$dtt_id]; |
|
| 335 | 335 | break; |
| 336 | 336 | default: |
| 337 | 337 | return $total; |
@@ -349,9 +349,9 @@ discard block |
||
| 349 | 349 | { |
| 350 | 350 | $datetimes = $this->get_many_related('Datetime'); |
| 351 | 351 | $tickets_sold = array(); |
| 352 | - if (! empty($datetimes)) { |
|
| 352 | + if ( ! empty($datetimes)) { |
|
| 353 | 353 | foreach ($datetimes as $datetime) { |
| 354 | - $tickets_sold['datetime'][ $datetime->ID() ] = $datetime->get('DTT_sold'); |
|
| 354 | + $tickets_sold['datetime'][$datetime->ID()] = $datetime->get('DTT_sold'); |
|
| 355 | 355 | } |
| 356 | 356 | } |
| 357 | 357 | // Tickets sold |
@@ -924,7 +924,7 @@ discard block |
||
| 924 | 924 | 'TKT_qty', |
| 925 | 925 | $qty |
| 926 | 926 | ); |
| 927 | - if (! $success) { |
|
| 927 | + if ( ! $success) { |
|
| 928 | 928 | // The datetimes were successfully bumped, but not the |
| 929 | 929 | // ticket. So we need to manually rollback the datetimes. |
| 930 | 930 | $this->decreaseReservedForDatetimes($qty); |
@@ -1373,7 +1373,7 @@ discard block |
||
| 1373 | 1373 | foreach ($this->datetimes() as $datetime) { |
| 1374 | 1374 | $times[] = $datetime->start_date_and_time(); |
| 1375 | 1375 | } |
| 1376 | - return $this->name() . ' @ ' . implode(', ', $times) . ' for ' . $this->pretty_price(); |
|
| 1376 | + return $this->name().' @ '.implode(', ', $times).' for '.$this->pretty_price(); |
|
| 1377 | 1377 | } |
| 1378 | 1378 | |
| 1379 | 1379 | |
@@ -1472,7 +1472,7 @@ discard block |
||
| 1472 | 1472 | { |
| 1473 | 1473 | // get one datetime to use for getting the event |
| 1474 | 1474 | $datetime = $this->first_datetime(); |
| 1475 | - if (! $datetime instanceof \EE_Datetime) { |
|
| 1475 | + if ( ! $datetime instanceof \EE_Datetime) { |
|
| 1476 | 1476 | throw new UnexpectedEntityException( |
| 1477 | 1477 | $datetime, |
| 1478 | 1478 | 'EE_Datetime', |
@@ -1483,7 +1483,7 @@ discard block |
||
| 1483 | 1483 | ); |
| 1484 | 1484 | } |
| 1485 | 1485 | $event = $datetime->event(); |
| 1486 | - if (! $event instanceof \EE_Event) { |
|
| 1486 | + if ( ! $event instanceof \EE_Event) { |
|
| 1487 | 1487 | throw new UnexpectedEntityException( |
| 1488 | 1488 | $event, |
| 1489 | 1489 | 'EE_Event', |
@@ -14,1732 +14,1732 @@ |
||
| 14 | 14 | class EE_Ticket extends EE_Soft_Delete_Base_Class implements EEI_Line_Item_Object, EEI_Event_Relation, EEI_Has_Icon |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The following constants are used by the ticket_status() method to indicate whether a ticket is on sale or not. |
|
| 19 | - */ |
|
| 20 | - const sold_out = 'TKS'; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * |
|
| 24 | - */ |
|
| 25 | - const expired = 'TKE'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * |
|
| 29 | - */ |
|
| 30 | - const archived = 'TKA'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * |
|
| 34 | - */ |
|
| 35 | - const pending = 'TKP'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * |
|
| 39 | - */ |
|
| 40 | - const onsale = 'TKO'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * extra meta key for tracking ticket reservations |
|
| 44 | - * |
|
| 45 | - * @type string |
|
| 46 | - */ |
|
| 47 | - const META_KEY_TICKET_RESERVATIONS = 'ticket_reservations'; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * cached result from method of the same name |
|
| 51 | - * |
|
| 52 | - * @var float $_ticket_total_with_taxes |
|
| 53 | - */ |
|
| 54 | - private $_ticket_total_with_taxes; |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param array $props_n_values incoming values |
|
| 59 | - * @param string $timezone incoming timezone (if not set the timezone set for the website will be |
|
| 60 | - * used.) |
|
| 61 | - * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 62 | - * date_format and the second value is the time format |
|
| 63 | - * @return EE_Ticket |
|
| 64 | - * @throws EE_Error |
|
| 65 | - */ |
|
| 66 | - public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array()) |
|
| 67 | - { |
|
| 68 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats); |
|
| 69 | - return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @param array $props_n_values incoming values from the database |
|
| 75 | - * @param string $timezone incoming timezone as set by the model. If not set the timezone for |
|
| 76 | - * the website will be used. |
|
| 77 | - * @return EE_Ticket |
|
| 78 | - * @throws EE_Error |
|
| 79 | - */ |
|
| 80 | - public static function new_instance_from_db($props_n_values = array(), $timezone = null) |
|
| 81 | - { |
|
| 82 | - return new self($props_n_values, true, $timezone); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @return bool |
|
| 88 | - * @throws EE_Error |
|
| 89 | - */ |
|
| 90 | - public function parent() |
|
| 91 | - { |
|
| 92 | - return $this->get('TKT_parent'); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * return if a ticket has quantities available for purchase |
|
| 98 | - * |
|
| 99 | - * @param int $DTT_ID the primary key for a particular datetime |
|
| 100 | - * @return boolean |
|
| 101 | - * @throws EE_Error |
|
| 102 | - */ |
|
| 103 | - public function available($DTT_ID = 0) |
|
| 104 | - { |
|
| 105 | - // are we checking availability for a particular datetime ? |
|
| 106 | - if ($DTT_ID) { |
|
| 107 | - // get that datetime object |
|
| 108 | - $datetime = $this->get_first_related('Datetime', array(array('DTT_ID' => $DTT_ID))); |
|
| 109 | - // if ticket sales for this datetime have exceeded the reg limit... |
|
| 110 | - if ($datetime instanceof EE_Datetime && $datetime->sold_out()) { |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - // datetime is still open for registration, but is this ticket sold out ? |
|
| 115 | - return $this->qty() < 1 || $this->qty() > $this->sold() ? true : false; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Using the start date and end date this method calculates whether the ticket is On Sale, Pending, or Expired |
|
| 121 | - * |
|
| 122 | - * @param bool $display true = we'll return a localized string, otherwise we just return the value of the |
|
| 123 | - * relevant status const |
|
| 124 | - * @param bool | null $remaining if it is already known that tickets are available, then simply pass a bool to save |
|
| 125 | - * further processing |
|
| 126 | - * @return mixed status int if the display string isn't requested |
|
| 127 | - * @throws EE_Error |
|
| 128 | - */ |
|
| 129 | - public function ticket_status($display = false, $remaining = null) |
|
| 130 | - { |
|
| 131 | - $remaining = is_bool($remaining) ? $remaining : $this->is_remaining(); |
|
| 132 | - if (! $remaining) { |
|
| 133 | - return $display ? EEH_Template::pretty_status(EE_Ticket::sold_out, false, 'sentence') : EE_Ticket::sold_out; |
|
| 134 | - } |
|
| 135 | - if ($this->get('TKT_deleted')) { |
|
| 136 | - return $display ? EEH_Template::pretty_status(EE_Ticket::archived, false, 'sentence') : EE_Ticket::archived; |
|
| 137 | - } |
|
| 138 | - if ($this->is_expired()) { |
|
| 139 | - return $display ? EEH_Template::pretty_status(EE_Ticket::expired, false, 'sentence') : EE_Ticket::expired; |
|
| 140 | - } |
|
| 141 | - if ($this->is_pending()) { |
|
| 142 | - return $display ? EEH_Template::pretty_status(EE_Ticket::pending, false, 'sentence') : EE_Ticket::pending; |
|
| 143 | - } |
|
| 144 | - if ($this->is_on_sale()) { |
|
| 145 | - return $display ? EEH_Template::pretty_status(EE_Ticket::onsale, false, 'sentence') : EE_Ticket::onsale; |
|
| 146 | - } |
|
| 147 | - return ''; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * The purpose of this method is to simply return a boolean for whether there are any tickets remaining for sale |
|
| 153 | - * considering ALL the factors used for figuring that out. |
|
| 154 | - * |
|
| 155 | - * @access public |
|
| 156 | - * @param int $DTT_ID if an int above 0 is included here then we get a specific dtt. |
|
| 157 | - * @return boolean true = tickets remaining, false not. |
|
| 158 | - * @throws EE_Error |
|
| 159 | - */ |
|
| 160 | - public function is_remaining($DTT_ID = 0) |
|
| 161 | - { |
|
| 162 | - $num_remaining = $this->remaining($DTT_ID); |
|
| 163 | - if ($num_remaining === 0) { |
|
| 164 | - return false; |
|
| 165 | - } |
|
| 166 | - if ($num_remaining > 0 && $num_remaining < $this->min()) { |
|
| 167 | - return false; |
|
| 168 | - } |
|
| 169 | - return true; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * return the total number of tickets available for purchase |
|
| 175 | - * |
|
| 176 | - * @param int $DTT_ID the primary key for a particular datetime. |
|
| 177 | - * set to 0 for all related datetimes |
|
| 178 | - * @return int |
|
| 179 | - * @throws EE_Error |
|
| 180 | - */ |
|
| 181 | - public function remaining($DTT_ID = 0) |
|
| 182 | - { |
|
| 183 | - return $this->real_quantity_on_ticket('saleable', $DTT_ID); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Gets min |
|
| 189 | - * |
|
| 190 | - * @return int |
|
| 191 | - * @throws EE_Error |
|
| 192 | - */ |
|
| 193 | - public function min() |
|
| 194 | - { |
|
| 195 | - return $this->get('TKT_min'); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * return if a ticket is no longer available cause its available dates have expired. |
|
| 201 | - * |
|
| 202 | - * @return boolean |
|
| 203 | - * @throws EE_Error |
|
| 204 | - */ |
|
| 205 | - public function is_expired() |
|
| 206 | - { |
|
| 207 | - return ($this->get_raw('TKT_end_date') < time()); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Return if a ticket is yet to go on sale or not |
|
| 213 | - * |
|
| 214 | - * @return boolean |
|
| 215 | - * @throws EE_Error |
|
| 216 | - */ |
|
| 217 | - public function is_pending() |
|
| 218 | - { |
|
| 219 | - return ($this->get_raw('TKT_start_date') > time()); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Return if a ticket is on sale or not |
|
| 225 | - * |
|
| 226 | - * @return boolean |
|
| 227 | - * @throws EE_Error |
|
| 228 | - */ |
|
| 229 | - public function is_on_sale() |
|
| 230 | - { |
|
| 231 | - return ($this->get_raw('TKT_start_date') < time() && $this->get_raw('TKT_end_date') > time()); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * This returns the chronologically last datetime that this ticket is associated with |
|
| 237 | - * |
|
| 238 | - * @param string $dt_frmt |
|
| 239 | - * @param string $conjunction - conjunction junction what's your function ? this string joins the start date with |
|
| 240 | - * the end date ie: Jan 01 "to" Dec 31 |
|
| 241 | - * @return string |
|
| 242 | - * @throws EE_Error |
|
| 243 | - */ |
|
| 244 | - public function date_range($dt_frmt = '', $conjunction = ' - ') |
|
| 245 | - { |
|
| 246 | - $dt_frmt = ! empty($dt_frmt) ? $dt_frmt : $this->_dt_frmt; |
|
| 247 | - $first_date = $this->first_datetime() instanceof EE_Datetime ? $this->first_datetime()->get_i18n_datetime('DTT_EVT_start', $dt_frmt) |
|
| 248 | - : ''; |
|
| 249 | - $last_date = $this->last_datetime() instanceof EE_Datetime ? $this->last_datetime()->get_i18n_datetime('DTT_EVT_end', $dt_frmt) : ''; |
|
| 250 | - |
|
| 251 | - return $first_date && $last_date ? $first_date . $conjunction . $last_date : ''; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * This returns the chronologically first datetime that this ticket is associated with |
|
| 257 | - * |
|
| 258 | - * @return EE_Datetime |
|
| 259 | - * @throws EE_Error |
|
| 260 | - */ |
|
| 261 | - public function first_datetime() |
|
| 262 | - { |
|
| 263 | - $datetimes = $this->datetimes(array('limit' => 1)); |
|
| 264 | - return reset($datetimes); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Gets all the datetimes this ticket can be used for attending. |
|
| 270 | - * Unless otherwise specified, orders datetimes by start date. |
|
| 271 | - * |
|
| 272 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 273 | - * @return EE_Datetime[]|EE_Base_Class[] |
|
| 274 | - * @throws EE_Error |
|
| 275 | - */ |
|
| 276 | - public function datetimes($query_params = array()) |
|
| 277 | - { |
|
| 278 | - if (! isset($query_params['order_by'])) { |
|
| 279 | - $query_params['order_by']['DTT_order'] = 'ASC'; |
|
| 280 | - } |
|
| 281 | - return $this->get_many_related('Datetime', $query_params); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * This returns the chronologically last datetime that this ticket is associated with |
|
| 287 | - * |
|
| 288 | - * @return EE_Datetime |
|
| 289 | - * @throws EE_Error |
|
| 290 | - */ |
|
| 291 | - public function last_datetime() |
|
| 292 | - { |
|
| 293 | - $datetimes = $this->datetimes(array('limit' => 1, 'order_by' => array('DTT_EVT_start' => 'DESC'))); |
|
| 294 | - return end($datetimes); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * This returns the total tickets sold depending on the given parameters. |
|
| 300 | - * |
|
| 301 | - * @param string $what Can be one of two options: 'ticket', 'datetime'. |
|
| 302 | - * 'ticket' = total ticket sales for all datetimes this ticket is related to |
|
| 303 | - * 'datetime' = total ticket sales for a specified datetime (required $dtt_id) |
|
| 304 | - * 'datetime' = total ticket sales in the datetime_ticket table. |
|
| 305 | - * If $dtt_id is not given then we return an array of sales indexed by datetime. |
|
| 306 | - * If $dtt_id IS given then we return the tickets sold for that given datetime. |
|
| 307 | - * @param int $dtt_id [optional] include the dtt_id with $what = 'datetime'. |
|
| 308 | - * @return mixed (array|int) how many tickets have sold |
|
| 309 | - * @throws EE_Error |
|
| 310 | - */ |
|
| 311 | - public function tickets_sold($what = 'ticket', $dtt_id = null) |
|
| 312 | - { |
|
| 313 | - $total = 0; |
|
| 314 | - $tickets_sold = $this->_all_tickets_sold(); |
|
| 315 | - switch ($what) { |
|
| 316 | - case 'ticket': |
|
| 317 | - return $tickets_sold['ticket']; |
|
| 318 | - break; |
|
| 319 | - case 'datetime': |
|
| 320 | - if (empty($tickets_sold['datetime'])) { |
|
| 321 | - return $total; |
|
| 322 | - } |
|
| 323 | - if (! empty($dtt_id) && ! isset($tickets_sold['datetime'][ $dtt_id ])) { |
|
| 324 | - EE_Error::add_error( |
|
| 325 | - __( |
|
| 326 | - 'You\'ve requested the amount of tickets sold for a given ticket and datetime, however there are no records for the datetime id you included. Are you SURE that is a datetime related to this ticket?', |
|
| 327 | - 'event_espresso' |
|
| 328 | - ), |
|
| 329 | - __FILE__, |
|
| 330 | - __FUNCTION__, |
|
| 331 | - __LINE__ |
|
| 332 | - ); |
|
| 333 | - return $total; |
|
| 334 | - } |
|
| 335 | - return empty($dtt_id) ? $tickets_sold['datetime'] : $tickets_sold['datetime'][ $dtt_id ]; |
|
| 336 | - break; |
|
| 337 | - default: |
|
| 338 | - return $total; |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * This returns an array indexed by datetime_id for tickets sold with this ticket. |
|
| 345 | - * |
|
| 346 | - * @return EE_Ticket[] |
|
| 347 | - * @throws EE_Error |
|
| 348 | - */ |
|
| 349 | - protected function _all_tickets_sold() |
|
| 350 | - { |
|
| 351 | - $datetimes = $this->get_many_related('Datetime'); |
|
| 352 | - $tickets_sold = array(); |
|
| 353 | - if (! empty($datetimes)) { |
|
| 354 | - foreach ($datetimes as $datetime) { |
|
| 355 | - $tickets_sold['datetime'][ $datetime->ID() ] = $datetime->get('DTT_sold'); |
|
| 356 | - } |
|
| 357 | - } |
|
| 358 | - // Tickets sold |
|
| 359 | - $tickets_sold['ticket'] = $this->sold(); |
|
| 360 | - return $tickets_sold; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * This returns the base price object for the ticket. |
|
| 366 | - * |
|
| 367 | - * @param bool $return_array whether to return as an array indexed by price id or just the object. |
|
| 368 | - * @return EE_Price|EE_Base_Class|EE_Price[]|EE_Base_Class[] |
|
| 369 | - * @throws EE_Error |
|
| 370 | - */ |
|
| 371 | - public function base_price($return_array = false) |
|
| 372 | - { |
|
| 373 | - $_where = array('Price_Type.PBT_ID' => EEM_Price_Type::base_type_base_price); |
|
| 374 | - return $return_array |
|
| 375 | - ? $this->get_many_related('Price', array($_where)) |
|
| 376 | - : $this->get_first_related('Price', array($_where)); |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * This returns ONLY the price modifiers for the ticket (i.e. no taxes or base price) |
|
| 382 | - * |
|
| 383 | - * @access public |
|
| 384 | - * @return EE_Price[] |
|
| 385 | - * @throws EE_Error |
|
| 386 | - */ |
|
| 387 | - public function price_modifiers() |
|
| 388 | - { |
|
| 389 | - $query_params = array( |
|
| 390 | - 0 => array( |
|
| 391 | - 'Price_Type.PBT_ID' => array( |
|
| 392 | - 'NOT IN', |
|
| 393 | - array(EEM_Price_Type::base_type_base_price, EEM_Price_Type::base_type_tax), |
|
| 394 | - ), |
|
| 395 | - ), |
|
| 396 | - ); |
|
| 397 | - return $this->prices($query_params); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * Gets all the prices that combine to form the final price of this ticket |
|
| 403 | - * |
|
| 404 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 405 | - * @return EE_Price[]|EE_Base_Class[] |
|
| 406 | - * @throws EE_Error |
|
| 407 | - */ |
|
| 408 | - public function prices($query_params = array()) |
|
| 409 | - { |
|
| 410 | - return $this->get_many_related('Price', $query_params); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Gets all the ticket applicabilities (ie, relations between datetimes and tickets) |
|
| 416 | - * |
|
| 417 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 418 | - * @return EE_Datetime_Ticket|EE_Base_Class[] |
|
| 419 | - * @throws EE_Error |
|
| 420 | - */ |
|
| 421 | - public function datetime_tickets($query_params = array()) |
|
| 422 | - { |
|
| 423 | - return $this->get_many_related('Datetime_Ticket', $query_params); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * Gets all the datetimes from the db ordered by DTT_order |
|
| 429 | - * |
|
| 430 | - * @param boolean $show_expired |
|
| 431 | - * @param boolean $show_deleted |
|
| 432 | - * @return EE_Datetime[] |
|
| 433 | - * @throws EE_Error |
|
| 434 | - */ |
|
| 435 | - public function datetimes_ordered($show_expired = true, $show_deleted = false) |
|
| 436 | - { |
|
| 437 | - return EEM_Datetime::instance($this->_timezone)->get_datetimes_for_ticket_ordered_by_DTT_order( |
|
| 438 | - $this->ID(), |
|
| 439 | - $show_expired, |
|
| 440 | - $show_deleted |
|
| 441 | - ); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Gets ID |
|
| 447 | - * |
|
| 448 | - * @return string |
|
| 449 | - * @throws EE_Error |
|
| 450 | - */ |
|
| 451 | - public function ID() |
|
| 452 | - { |
|
| 453 | - return $this->get('TKT_ID'); |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * get the author of the ticket. |
|
| 459 | - * |
|
| 460 | - * @since 4.5.0 |
|
| 461 | - * @return int |
|
| 462 | - * @throws EE_Error |
|
| 463 | - */ |
|
| 464 | - public function wp_user() |
|
| 465 | - { |
|
| 466 | - return $this->get('TKT_wp_user'); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * Gets the template for the ticket |
|
| 472 | - * |
|
| 473 | - * @return EE_Ticket_Template|EE_Base_Class |
|
| 474 | - * @throws EE_Error |
|
| 475 | - */ |
|
| 476 | - public function template() |
|
| 477 | - { |
|
| 478 | - return $this->get_first_related('Ticket_Template'); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Simply returns an array of EE_Price objects that are taxes. |
|
| 484 | - * |
|
| 485 | - * @return EE_Price[] |
|
| 486 | - * @throws EE_Error |
|
| 487 | - */ |
|
| 488 | - public function get_ticket_taxes_for_admin() |
|
| 489 | - { |
|
| 490 | - return EE_Taxes::get_taxes_for_admin(); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * @return float |
|
| 496 | - * @throws EE_Error |
|
| 497 | - */ |
|
| 498 | - public function ticket_price() |
|
| 499 | - { |
|
| 500 | - return $this->get('TKT_price'); |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * @return mixed |
|
| 506 | - * @throws EE_Error |
|
| 507 | - */ |
|
| 508 | - public function pretty_price() |
|
| 509 | - { |
|
| 510 | - return $this->get_pretty('TKT_price'); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * @return bool |
|
| 516 | - * @throws EE_Error |
|
| 517 | - */ |
|
| 518 | - public function is_free() |
|
| 519 | - { |
|
| 520 | - return $this->get_ticket_total_with_taxes() === (float) 0; |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - |
|
| 524 | - /** |
|
| 525 | - * get_ticket_total_with_taxes |
|
| 526 | - * |
|
| 527 | - * @param bool $no_cache |
|
| 528 | - * @return float |
|
| 529 | - * @throws EE_Error |
|
| 530 | - */ |
|
| 531 | - public function get_ticket_total_with_taxes($no_cache = false) |
|
| 532 | - { |
|
| 533 | - if ($this->_ticket_total_with_taxes === null || $no_cache) { |
|
| 534 | - $this->_ticket_total_with_taxes = $this->get_ticket_subtotal() + $this->get_ticket_taxes_total_for_admin(); |
|
| 535 | - } |
|
| 536 | - return (float) $this->_ticket_total_with_taxes; |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - |
|
| 540 | - public function ensure_TKT_Price_correct() |
|
| 541 | - { |
|
| 542 | - $this->set('TKT_price', EE_Taxes::get_subtotal_for_admin($this)); |
|
| 543 | - $this->save(); |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * @return float |
|
| 549 | - * @throws EE_Error |
|
| 550 | - */ |
|
| 551 | - public function get_ticket_subtotal() |
|
| 552 | - { |
|
| 553 | - return EE_Taxes::get_subtotal_for_admin($this); |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * Returns the total taxes applied to this ticket |
|
| 559 | - * |
|
| 560 | - * @return float |
|
| 561 | - * @throws EE_Error |
|
| 562 | - */ |
|
| 563 | - public function get_ticket_taxes_total_for_admin() |
|
| 564 | - { |
|
| 565 | - return EE_Taxes::get_total_taxes_for_admin($this); |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Sets name |
|
| 571 | - * |
|
| 572 | - * @param string $name |
|
| 573 | - * @throws EE_Error |
|
| 574 | - */ |
|
| 575 | - public function set_name($name) |
|
| 576 | - { |
|
| 577 | - $this->set('TKT_name', $name); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * Gets description |
|
| 583 | - * |
|
| 584 | - * @return string |
|
| 585 | - * @throws EE_Error |
|
| 586 | - */ |
|
| 587 | - public function description() |
|
| 588 | - { |
|
| 589 | - return $this->get('TKT_description'); |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * Sets description |
|
| 595 | - * |
|
| 596 | - * @param string $description |
|
| 597 | - * @throws EE_Error |
|
| 598 | - */ |
|
| 599 | - public function set_description($description) |
|
| 600 | - { |
|
| 601 | - $this->set('TKT_description', $description); |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * Gets start_date |
|
| 607 | - * |
|
| 608 | - * @param string $dt_frmt |
|
| 609 | - * @param string $tm_frmt |
|
| 610 | - * @return string |
|
| 611 | - * @throws EE_Error |
|
| 612 | - */ |
|
| 613 | - public function start_date($dt_frmt = '', $tm_frmt = '') |
|
| 614 | - { |
|
| 615 | - return $this->_get_datetime('TKT_start_date', $dt_frmt, $tm_frmt); |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - |
|
| 619 | - /** |
|
| 620 | - * Sets start_date |
|
| 621 | - * |
|
| 622 | - * @param string $start_date |
|
| 623 | - * @return void |
|
| 624 | - * @throws EE_Error |
|
| 625 | - */ |
|
| 626 | - public function set_start_date($start_date) |
|
| 627 | - { |
|
| 628 | - $this->_set_date_time('B', $start_date, 'TKT_start_date'); |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * Gets end_date |
|
| 634 | - * |
|
| 635 | - * @param string $dt_frmt |
|
| 636 | - * @param string $tm_frmt |
|
| 637 | - * @return string |
|
| 638 | - * @throws EE_Error |
|
| 639 | - */ |
|
| 640 | - public function end_date($dt_frmt = '', $tm_frmt = '') |
|
| 641 | - { |
|
| 642 | - return $this->_get_datetime('TKT_end_date', $dt_frmt, $tm_frmt); |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - |
|
| 646 | - /** |
|
| 647 | - * Sets end_date |
|
| 648 | - * |
|
| 649 | - * @param string $end_date |
|
| 650 | - * @return void |
|
| 651 | - * @throws EE_Error |
|
| 652 | - */ |
|
| 653 | - public function set_end_date($end_date) |
|
| 654 | - { |
|
| 655 | - $this->_set_date_time('B', $end_date, 'TKT_end_date'); |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - |
|
| 659 | - /** |
|
| 660 | - * Sets sell until time |
|
| 661 | - * |
|
| 662 | - * @since 4.5.0 |
|
| 663 | - * @param string $time a string representation of the sell until time (ex 9am or 7:30pm) |
|
| 664 | - * @throws EE_Error |
|
| 665 | - */ |
|
| 666 | - public function set_end_time($time) |
|
| 667 | - { |
|
| 668 | - $this->_set_time_for($time, 'TKT_end_date'); |
|
| 669 | - } |
|
| 670 | - |
|
| 671 | - |
|
| 672 | - /** |
|
| 673 | - * Sets min |
|
| 674 | - * |
|
| 675 | - * @param int $min |
|
| 676 | - * @return void |
|
| 677 | - * @throws EE_Error |
|
| 678 | - */ |
|
| 679 | - public function set_min($min) |
|
| 680 | - { |
|
| 681 | - $this->set('TKT_min', $min); |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - |
|
| 685 | - /** |
|
| 686 | - * Gets max |
|
| 687 | - * |
|
| 688 | - * @return int |
|
| 689 | - * @throws EE_Error |
|
| 690 | - */ |
|
| 691 | - public function max() |
|
| 692 | - { |
|
| 693 | - return $this->get('TKT_max'); |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - |
|
| 697 | - /** |
|
| 698 | - * Sets max |
|
| 699 | - * |
|
| 700 | - * @param int $max |
|
| 701 | - * @return void |
|
| 702 | - * @throws EE_Error |
|
| 703 | - */ |
|
| 704 | - public function set_max($max) |
|
| 705 | - { |
|
| 706 | - $this->set('TKT_max', $max); |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * Sets price |
|
| 712 | - * |
|
| 713 | - * @param float $price |
|
| 714 | - * @return void |
|
| 715 | - * @throws EE_Error |
|
| 716 | - */ |
|
| 717 | - public function set_price($price) |
|
| 718 | - { |
|
| 719 | - $this->set('TKT_price', $price); |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - |
|
| 723 | - /** |
|
| 724 | - * Gets sold |
|
| 725 | - * |
|
| 726 | - * @return int |
|
| 727 | - * @throws EE_Error |
|
| 728 | - */ |
|
| 729 | - public function sold() |
|
| 730 | - { |
|
| 731 | - return $this->get_raw('TKT_sold'); |
|
| 732 | - } |
|
| 733 | - |
|
| 734 | - |
|
| 735 | - /** |
|
| 736 | - * Sets sold |
|
| 737 | - * |
|
| 738 | - * @param int $sold |
|
| 739 | - * @return void |
|
| 740 | - * @throws EE_Error |
|
| 741 | - */ |
|
| 742 | - public function set_sold($sold) |
|
| 743 | - { |
|
| 744 | - // sold can not go below zero |
|
| 745 | - $sold = max(0, $sold); |
|
| 746 | - $this->set('TKT_sold', $sold); |
|
| 747 | - } |
|
| 748 | - |
|
| 749 | - |
|
| 750 | - /** |
|
| 751 | - * Increments sold by amount passed by $qty AND decrements the reserved count on both this ticket and its |
|
| 752 | - * associated datetimes. |
|
| 753 | - * |
|
| 754 | - * @since 4.9.80.p |
|
| 755 | - * @param int $qty |
|
| 756 | - * @return boolean |
|
| 757 | - * @throws EE_Error |
|
| 758 | - * @throws InvalidArgumentException |
|
| 759 | - * @throws InvalidDataTypeException |
|
| 760 | - * @throws InvalidInterfaceException |
|
| 761 | - * @throws ReflectionException |
|
| 762 | - */ |
|
| 763 | - public function increaseSold($qty = 1) |
|
| 764 | - { |
|
| 765 | - $qty = absint($qty); |
|
| 766 | - // increment sold and decrement reserved datetime quantities simultaneously |
|
| 767 | - // don't worry about failures, because they must have already had a spot reserved |
|
| 768 | - $this->increaseSoldForDatetimes($qty); |
|
| 769 | - // Increment and decrement ticket quantities simultaneously |
|
| 770 | - $success = $this->adjustNumericFieldsInDb( |
|
| 771 | - [ |
|
| 772 | - 'TKT_reserved' => $qty * -1, |
|
| 773 | - 'TKT_sold' => $qty |
|
| 774 | - ] |
|
| 775 | - ); |
|
| 776 | - do_action( |
|
| 777 | - 'AHEE__EE_Ticket__increase_sold', |
|
| 778 | - $this, |
|
| 779 | - $qty, |
|
| 780 | - $this->sold(), |
|
| 781 | - $success |
|
| 782 | - ); |
|
| 783 | - return $success; |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * On each datetime related to this ticket, increases its sold count and decreases its reserved count by $qty. |
|
| 788 | - * |
|
| 789 | - * @since 4.9.80.p |
|
| 790 | - * @param int $qty positive or negative. Positive means to increase sold counts (and decrease reserved counts), |
|
| 791 | - * Negative means to decreases old counts (and increase reserved counts). |
|
| 792 | - * @param EE_Datetime[] $datetimes |
|
| 793 | - * @throws EE_Error |
|
| 794 | - * @throws InvalidArgumentException |
|
| 795 | - * @throws InvalidDataTypeException |
|
| 796 | - * @throws InvalidInterfaceException |
|
| 797 | - * @throws ReflectionException |
|
| 798 | - */ |
|
| 799 | - protected function increaseSoldForDatetimes($qty, array $datetimes = []) |
|
| 800 | - { |
|
| 801 | - $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes(); |
|
| 802 | - foreach ($datetimes as $datetime) { |
|
| 803 | - $datetime->increaseSold($qty); |
|
| 804 | - } |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - |
|
| 808 | - |
|
| 809 | - /** |
|
| 810 | - * Decrements (subtracts) sold by amount passed by $qty on both the ticket and its related datetimes directly in the |
|
| 811 | - * DB and then updates the model objects. |
|
| 812 | - * Does not affect the reserved counts. |
|
| 813 | - * |
|
| 814 | - * @since 4.9.80.p |
|
| 815 | - * @param int $qty |
|
| 816 | - * @return boolean |
|
| 817 | - * @throws EE_Error |
|
| 818 | - * @throws InvalidArgumentException |
|
| 819 | - * @throws InvalidDataTypeException |
|
| 820 | - * @throws InvalidInterfaceException |
|
| 821 | - * @throws ReflectionException |
|
| 822 | - */ |
|
| 823 | - public function decreaseSold($qty = 1) |
|
| 824 | - { |
|
| 825 | - $qty = absint($qty); |
|
| 826 | - $this->decreaseSoldForDatetimes($qty); |
|
| 827 | - $success = $this->adjustNumericFieldsInDb( |
|
| 828 | - [ |
|
| 829 | - 'TKT_sold' => $qty * -1 |
|
| 830 | - ] |
|
| 831 | - ); |
|
| 832 | - do_action( |
|
| 833 | - 'AHEE__EE_Ticket__decrease_sold', |
|
| 834 | - $this, |
|
| 835 | - $qty, |
|
| 836 | - $this->sold(), |
|
| 837 | - $success |
|
| 838 | - ); |
|
| 839 | - return $success; |
|
| 840 | - } |
|
| 841 | - |
|
| 842 | - |
|
| 843 | - /** |
|
| 844 | - * Decreases sold on related datetimes |
|
| 845 | - * |
|
| 846 | - * @since 4.9.80.p |
|
| 847 | - * @param int $qty |
|
| 848 | - * @param EE_Datetime[] $datetimes |
|
| 849 | - * @return void |
|
| 850 | - * @throws EE_Error |
|
| 851 | - * @throws InvalidArgumentException |
|
| 852 | - * @throws InvalidDataTypeException |
|
| 853 | - * @throws InvalidInterfaceException |
|
| 854 | - * @throws ReflectionException |
|
| 855 | - */ |
|
| 856 | - protected function decreaseSoldForDatetimes($qty = 1, array $datetimes = []) |
|
| 857 | - { |
|
| 858 | - $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes(); |
|
| 859 | - if (is_array($datetimes)) { |
|
| 860 | - foreach ($datetimes as $datetime) { |
|
| 861 | - if ($datetime instanceof EE_Datetime) { |
|
| 862 | - $datetime->decreaseSold($qty); |
|
| 863 | - } |
|
| 864 | - } |
|
| 865 | - } |
|
| 866 | - } |
|
| 867 | - |
|
| 868 | - |
|
| 869 | - /** |
|
| 870 | - * Gets qty of reserved tickets |
|
| 871 | - * |
|
| 872 | - * @return int |
|
| 873 | - * @throws EE_Error |
|
| 874 | - */ |
|
| 875 | - public function reserved() |
|
| 876 | - { |
|
| 877 | - return $this->get_raw('TKT_reserved'); |
|
| 878 | - } |
|
| 879 | - |
|
| 880 | - |
|
| 881 | - /** |
|
| 882 | - * Sets reserved |
|
| 883 | - * |
|
| 884 | - * @param int $reserved |
|
| 885 | - * @return void |
|
| 886 | - * @throws EE_Error |
|
| 887 | - */ |
|
| 888 | - public function set_reserved($reserved) |
|
| 889 | - { |
|
| 890 | - // reserved can not go below zero |
|
| 891 | - $reserved = max(0, (int) $reserved); |
|
| 892 | - $this->set('TKT_reserved', $reserved); |
|
| 893 | - } |
|
| 894 | - |
|
| 895 | - |
|
| 896 | - /** |
|
| 897 | - * Increments reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 898 | - * |
|
| 899 | - * @since 4.9.80.p |
|
| 900 | - * @param int $qty |
|
| 901 | - * @param string $source |
|
| 902 | - * @return bool whether we successfully reserved the ticket or not. |
|
| 903 | - * @throws EE_Error |
|
| 904 | - * @throws InvalidArgumentException |
|
| 905 | - * @throws ReflectionException |
|
| 906 | - * @throws InvalidDataTypeException |
|
| 907 | - * @throws InvalidInterfaceException |
|
| 908 | - */ |
|
| 909 | - public function increaseReserved($qty = 1, $source = 'unknown') |
|
| 910 | - { |
|
| 911 | - $qty = absint($qty); |
|
| 912 | - do_action( |
|
| 913 | - 'AHEE__EE_Ticket__increase_reserved__begin', |
|
| 914 | - $this, |
|
| 915 | - $qty, |
|
| 916 | - $source |
|
| 917 | - ); |
|
| 918 | - $this->add_extra_meta(EE_Ticket::META_KEY_TICKET_RESERVATIONS, "{$qty} from {$source}"); |
|
| 919 | - $success = false; |
|
| 920 | - $datetimes_adjusted_successfully = $this->increaseReservedForDatetimes($qty); |
|
| 921 | - if ($datetimes_adjusted_successfully) { |
|
| 922 | - $success = $this->incrementFieldConditionallyInDb( |
|
| 923 | - 'TKT_reserved', |
|
| 924 | - 'TKT_sold', |
|
| 925 | - 'TKT_qty', |
|
| 926 | - $qty |
|
| 927 | - ); |
|
| 928 | - if (! $success) { |
|
| 929 | - // The datetimes were successfully bumped, but not the |
|
| 930 | - // ticket. So we need to manually rollback the datetimes. |
|
| 931 | - $this->decreaseReservedForDatetimes($qty); |
|
| 932 | - } |
|
| 933 | - } |
|
| 934 | - do_action( |
|
| 935 | - 'AHEE__EE_Ticket__increase_reserved', |
|
| 936 | - $this, |
|
| 937 | - $qty, |
|
| 938 | - $this->reserved(), |
|
| 939 | - $success |
|
| 940 | - ); |
|
| 941 | - return $success; |
|
| 942 | - } |
|
| 943 | - |
|
| 944 | - |
|
| 945 | - /** |
|
| 946 | - * Increases reserved counts on related datetimes |
|
| 947 | - * |
|
| 948 | - * @since 4.9.80.p |
|
| 949 | - * @param int $qty |
|
| 950 | - * @param EE_Datetime[] $datetimes |
|
| 951 | - * @return boolean indicating success |
|
| 952 | - * @throws EE_Error |
|
| 953 | - * @throws InvalidArgumentException |
|
| 954 | - * @throws InvalidDataTypeException |
|
| 955 | - * @throws InvalidInterfaceException |
|
| 956 | - * @throws ReflectionException |
|
| 957 | - */ |
|
| 958 | - protected function increaseReservedForDatetimes($qty = 1, array $datetimes = []) |
|
| 959 | - { |
|
| 960 | - $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes(); |
|
| 961 | - $datetimes_updated = []; |
|
| 962 | - $limit_exceeded = false; |
|
| 963 | - if (is_array($datetimes)) { |
|
| 964 | - foreach ($datetimes as $datetime) { |
|
| 965 | - if ($datetime instanceof EE_Datetime) { |
|
| 966 | - if ($datetime->increaseReserved($qty)) { |
|
| 967 | - $datetimes_updated[] = $datetime; |
|
| 968 | - } else { |
|
| 969 | - $limit_exceeded = true; |
|
| 970 | - break; |
|
| 971 | - } |
|
| 972 | - } |
|
| 973 | - } |
|
| 974 | - // If somewhere along the way we detected a datetime whose |
|
| 975 | - // limit was exceeded, do a manual rollback. |
|
| 976 | - if ($limit_exceeded) { |
|
| 977 | - $this->decreaseReservedForDatetimes($qty, $datetimes_updated); |
|
| 978 | - return false; |
|
| 979 | - } |
|
| 980 | - } |
|
| 981 | - return true; |
|
| 982 | - } |
|
| 983 | - |
|
| 984 | - |
|
| 985 | - /** |
|
| 986 | - * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 987 | - * |
|
| 988 | - * @since 4.9.80.p |
|
| 989 | - * @param int $qty |
|
| 990 | - * @param bool $adjust_datetimes |
|
| 991 | - * @param string $source |
|
| 992 | - * @return boolean |
|
| 993 | - * @throws EE_Error |
|
| 994 | - * @throws InvalidArgumentException |
|
| 995 | - * @throws ReflectionException |
|
| 996 | - * @throws InvalidDataTypeException |
|
| 997 | - * @throws InvalidInterfaceException |
|
| 998 | - */ |
|
| 999 | - public function decreaseReserved($qty = 1, $adjust_datetimes = true, $source = 'unknown') |
|
| 1000 | - { |
|
| 1001 | - $qty = absint($qty); |
|
| 1002 | - $this->add_extra_meta(EE_Ticket::META_KEY_TICKET_RESERVATIONS, "-{$qty} from {$source}"); |
|
| 1003 | - if ($adjust_datetimes) { |
|
| 1004 | - $this->decreaseReservedForDatetimes($qty); |
|
| 1005 | - } |
|
| 1006 | - $success = $this->adjustNumericFieldsInDb( |
|
| 1007 | - [ |
|
| 1008 | - 'TKT_reserved' => $qty * -1 |
|
| 1009 | - ] |
|
| 1010 | - ); |
|
| 1011 | - do_action( |
|
| 1012 | - 'AHEE__EE_Ticket__decrease_reserved', |
|
| 1013 | - $this, |
|
| 1014 | - $qty, |
|
| 1015 | - $this->reserved(), |
|
| 1016 | - $success |
|
| 1017 | - ); |
|
| 1018 | - return $success; |
|
| 1019 | - } |
|
| 1020 | - |
|
| 1021 | - |
|
| 1022 | - /** |
|
| 1023 | - * Decreases the reserved count on the specified datetimes. |
|
| 1024 | - * |
|
| 1025 | - * @since 4.9.80.p |
|
| 1026 | - * @param int $qty |
|
| 1027 | - * @param EE_Datetime[] $datetimes |
|
| 1028 | - * @throws EE_Error |
|
| 1029 | - * @throws InvalidArgumentException |
|
| 1030 | - * @throws ReflectionException |
|
| 1031 | - * @throws InvalidDataTypeException |
|
| 1032 | - * @throws InvalidInterfaceException |
|
| 1033 | - */ |
|
| 1034 | - protected function decreaseReservedForDatetimes($qty = 1, array $datetimes = []) |
|
| 1035 | - { |
|
| 1036 | - $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes(); |
|
| 1037 | - foreach ($datetimes as $datetime) { |
|
| 1038 | - if ($datetime instanceof EE_Datetime) { |
|
| 1039 | - $datetime->decreaseReserved($qty); |
|
| 1040 | - } |
|
| 1041 | - } |
|
| 1042 | - } |
|
| 1043 | - |
|
| 1044 | - |
|
| 1045 | - /** |
|
| 1046 | - * Gets ticket quantity |
|
| 1047 | - * |
|
| 1048 | - * @param string $context ticket quantity is somewhat subjective depending on the exact information sought |
|
| 1049 | - * therefore $context can be one of three values: '', 'reg_limit', or 'saleable' |
|
| 1050 | - * '' (default) quantity is the actual db value for TKT_qty, unaffected by other objects |
|
| 1051 | - * REG LIMIT: caps qty based on DTT_reg_limit for ALL related datetimes |
|
| 1052 | - * SALEABLE: also considers datetime sold and returns zero if ANY DTT is sold out, and |
|
| 1053 | - * is therefore the truest measure of tickets that can be purchased at the moment |
|
| 1054 | - * @return int |
|
| 1055 | - * @throws EE_Error |
|
| 1056 | - */ |
|
| 1057 | - public function qty($context = '') |
|
| 1058 | - { |
|
| 1059 | - switch ($context) { |
|
| 1060 | - case 'reg_limit': |
|
| 1061 | - return $this->real_quantity_on_ticket(); |
|
| 1062 | - case 'saleable': |
|
| 1063 | - return $this->real_quantity_on_ticket('saleable'); |
|
| 1064 | - default: |
|
| 1065 | - return $this->get_raw('TKT_qty'); |
|
| 1066 | - } |
|
| 1067 | - } |
|
| 1068 | - |
|
| 1069 | - |
|
| 1070 | - /** |
|
| 1071 | - * Gets ticket quantity |
|
| 1072 | - * |
|
| 1073 | - * @param string $context ticket quantity is somewhat subjective depending on the exact information sought |
|
| 1074 | - * therefore $context can be one of two values: 'reg_limit', or 'saleable' |
|
| 1075 | - * REG LIMIT: caps qty based on DTT_reg_limit for ALL related datetimes |
|
| 1076 | - * SALEABLE: also considers datetime sold and returns zero if ANY DTT is sold out, and |
|
| 1077 | - * is therefore the truest measure of tickets that can be purchased at the moment |
|
| 1078 | - * @param int $DTT_ID the primary key for a particular datetime. |
|
| 1079 | - * set to 0 for all related datetimes |
|
| 1080 | - * @return int |
|
| 1081 | - * @throws EE_Error |
|
| 1082 | - */ |
|
| 1083 | - public function real_quantity_on_ticket($context = 'reg_limit', $DTT_ID = 0) |
|
| 1084 | - { |
|
| 1085 | - $raw = $this->get_raw('TKT_qty'); |
|
| 1086 | - // return immediately if it's zero |
|
| 1087 | - if ($raw === 0) { |
|
| 1088 | - return $raw; |
|
| 1089 | - } |
|
| 1090 | - // echo "\n\n<br />Ticket: " . $this->name() . '<br />'; |
|
| 1091 | - // ensure qty doesn't exceed raw value for THIS ticket |
|
| 1092 | - $qty = min(EE_INF, $raw); |
|
| 1093 | - // echo "\n . qty: " . $qty . '<br />'; |
|
| 1094 | - // calculate this ticket's total sales and reservations |
|
| 1095 | - $sold_and_reserved_for_this_ticket = $this->sold() + $this->reserved(); |
|
| 1096 | - // echo "\n . sold: " . $this->sold() . '<br />'; |
|
| 1097 | - // echo "\n . reserved: " . $this->reserved() . '<br />'; |
|
| 1098 | - // echo "\n . sold_and_reserved_for_this_ticket: " . $sold_and_reserved_for_this_ticket . '<br />'; |
|
| 1099 | - // first we need to calculate the maximum number of tickets available for the datetime |
|
| 1100 | - // do we want data for one datetime or all of them ? |
|
| 1101 | - $query_params = $DTT_ID ? array(array('DTT_ID' => $DTT_ID)) : array(); |
|
| 1102 | - $datetimes = $this->datetimes($query_params); |
|
| 1103 | - if (is_array($datetimes) && ! empty($datetimes)) { |
|
| 1104 | - foreach ($datetimes as $datetime) { |
|
| 1105 | - if ($datetime instanceof EE_Datetime) { |
|
| 1106 | - $datetime->refresh_from_db(); |
|
| 1107 | - // echo "\n . . datetime name: " . $datetime->name() . '<br />'; |
|
| 1108 | - // echo "\n . . datetime ID: " . $datetime->ID() . '<br />'; |
|
| 1109 | - // initialize with no restrictions for each datetime |
|
| 1110 | - // but adjust datetime qty based on datetime reg limit |
|
| 1111 | - $datetime_qty = min(EE_INF, $datetime->reg_limit()); |
|
| 1112 | - // echo "\n . . . datetime reg_limit: " . $datetime->reg_limit() . '<br />'; |
|
| 1113 | - // echo "\n . . . datetime_qty: " . $datetime_qty . '<br />'; |
|
| 1114 | - // if we want the actual saleable amount, then we need to consider OTHER ticket sales |
|
| 1115 | - // and reservations for this datetime, that do NOT include sales and reservations |
|
| 1116 | - // for this ticket (so we add $this->sold() and $this->reserved() back in) |
|
| 1117 | - if ($context === 'saleable') { |
|
| 1118 | - $datetime_qty = max( |
|
| 1119 | - $datetime_qty - $datetime->sold_and_reserved() + $sold_and_reserved_for_this_ticket, |
|
| 1120 | - 0 |
|
| 1121 | - ); |
|
| 1122 | - // echo "\n . . . datetime sold: " . $datetime->sold() . '<br />'; |
|
| 1123 | - // echo "\n . . . datetime reserved: " . $datetime->reserved() . '<br />'; |
|
| 1124 | - // echo "\n . . . datetime sold_and_reserved: " . $datetime->sold_and_reserved() . '<br />'; |
|
| 1125 | - // echo "\n . . . datetime_qty: " . $datetime_qty . '<br />'; |
|
| 1126 | - $datetime_qty = ! $datetime->sold_out() ? $datetime_qty : 0; |
|
| 1127 | - // echo "\n . . . datetime_qty: " . $datetime_qty . '<br />'; |
|
| 1128 | - } |
|
| 1129 | - $qty = min($datetime_qty, $qty); |
|
| 1130 | - // echo "\n . . qty: " . $qty . '<br />'; |
|
| 1131 | - } |
|
| 1132 | - } |
|
| 1133 | - } |
|
| 1134 | - // NOW that we know the maximum number of tickets available for the datetime |
|
| 1135 | - // we can finally factor in the details for this specific ticket |
|
| 1136 | - if ($qty > 0 && $context === 'saleable') { |
|
| 1137 | - // and subtract the sales for THIS ticket |
|
| 1138 | - $qty = max($qty - $sold_and_reserved_for_this_ticket, 0); |
|
| 1139 | - // echo "\n . qty: " . $qty . '<br />'; |
|
| 1140 | - } |
|
| 1141 | - // echo "\nFINAL QTY: " . $qty . "<br /><br />"; |
|
| 1142 | - return $qty; |
|
| 1143 | - } |
|
| 1144 | - |
|
| 1145 | - |
|
| 1146 | - /** |
|
| 1147 | - * Sets qty - IMPORTANT!!! Does NOT allow QTY to be set higher than the lowest reg limit of any related datetimes |
|
| 1148 | - * |
|
| 1149 | - * @param int $qty |
|
| 1150 | - * @return void |
|
| 1151 | - * @throws EE_Error |
|
| 1152 | - */ |
|
| 1153 | - public function set_qty($qty) |
|
| 1154 | - { |
|
| 1155 | - $datetimes = $this->datetimes(); |
|
| 1156 | - foreach ($datetimes as $datetime) { |
|
| 1157 | - if ($datetime instanceof EE_Datetime) { |
|
| 1158 | - $qty = min($qty, $datetime->reg_limit()); |
|
| 1159 | - } |
|
| 1160 | - } |
|
| 1161 | - $this->set('TKT_qty', $qty); |
|
| 1162 | - } |
|
| 1163 | - |
|
| 1164 | - |
|
| 1165 | - /** |
|
| 1166 | - * Gets uses |
|
| 1167 | - * |
|
| 1168 | - * @return int |
|
| 1169 | - * @throws EE_Error |
|
| 1170 | - */ |
|
| 1171 | - public function uses() |
|
| 1172 | - { |
|
| 1173 | - return $this->get('TKT_uses'); |
|
| 1174 | - } |
|
| 1175 | - |
|
| 1176 | - |
|
| 1177 | - /** |
|
| 1178 | - * Sets uses |
|
| 1179 | - * |
|
| 1180 | - * @param int $uses |
|
| 1181 | - * @return void |
|
| 1182 | - * @throws EE_Error |
|
| 1183 | - */ |
|
| 1184 | - public function set_uses($uses) |
|
| 1185 | - { |
|
| 1186 | - $this->set('TKT_uses', $uses); |
|
| 1187 | - } |
|
| 1188 | - |
|
| 1189 | - |
|
| 1190 | - /** |
|
| 1191 | - * returns whether ticket is required or not. |
|
| 1192 | - * |
|
| 1193 | - * @return boolean |
|
| 1194 | - * @throws EE_Error |
|
| 1195 | - */ |
|
| 1196 | - public function required() |
|
| 1197 | - { |
|
| 1198 | - return $this->get('TKT_required'); |
|
| 1199 | - } |
|
| 1200 | - |
|
| 1201 | - |
|
| 1202 | - /** |
|
| 1203 | - * sets the TKT_required property |
|
| 1204 | - * |
|
| 1205 | - * @param boolean $required |
|
| 1206 | - * @return void |
|
| 1207 | - * @throws EE_Error |
|
| 1208 | - */ |
|
| 1209 | - public function set_required($required) |
|
| 1210 | - { |
|
| 1211 | - $this->set('TKT_required', $required); |
|
| 1212 | - } |
|
| 1213 | - |
|
| 1214 | - |
|
| 1215 | - /** |
|
| 1216 | - * Gets taxable |
|
| 1217 | - * |
|
| 1218 | - * @return boolean |
|
| 1219 | - * @throws EE_Error |
|
| 1220 | - */ |
|
| 1221 | - public function taxable() |
|
| 1222 | - { |
|
| 1223 | - return $this->get('TKT_taxable'); |
|
| 1224 | - } |
|
| 1225 | - |
|
| 1226 | - |
|
| 1227 | - /** |
|
| 1228 | - * Sets taxable |
|
| 1229 | - * |
|
| 1230 | - * @param boolean $taxable |
|
| 1231 | - * @return void |
|
| 1232 | - * @throws EE_Error |
|
| 1233 | - */ |
|
| 1234 | - public function set_taxable($taxable) |
|
| 1235 | - { |
|
| 1236 | - $this->set('TKT_taxable', $taxable); |
|
| 1237 | - } |
|
| 1238 | - |
|
| 1239 | - |
|
| 1240 | - /** |
|
| 1241 | - * Gets is_default |
|
| 1242 | - * |
|
| 1243 | - * @return boolean |
|
| 1244 | - * @throws EE_Error |
|
| 1245 | - */ |
|
| 1246 | - public function is_default() |
|
| 1247 | - { |
|
| 1248 | - return $this->get('TKT_is_default'); |
|
| 1249 | - } |
|
| 1250 | - |
|
| 1251 | - |
|
| 1252 | - /** |
|
| 1253 | - * Sets is_default |
|
| 1254 | - * |
|
| 1255 | - * @param boolean $is_default |
|
| 1256 | - * @return void |
|
| 1257 | - * @throws EE_Error |
|
| 1258 | - */ |
|
| 1259 | - public function set_is_default($is_default) |
|
| 1260 | - { |
|
| 1261 | - $this->set('TKT_is_default', $is_default); |
|
| 1262 | - } |
|
| 1263 | - |
|
| 1264 | - |
|
| 1265 | - /** |
|
| 1266 | - * Gets order |
|
| 1267 | - * |
|
| 1268 | - * @return int |
|
| 1269 | - * @throws EE_Error |
|
| 1270 | - */ |
|
| 1271 | - public function order() |
|
| 1272 | - { |
|
| 1273 | - return $this->get('TKT_order'); |
|
| 1274 | - } |
|
| 1275 | - |
|
| 1276 | - |
|
| 1277 | - /** |
|
| 1278 | - * Sets order |
|
| 1279 | - * |
|
| 1280 | - * @param int $order |
|
| 1281 | - * @return void |
|
| 1282 | - * @throws EE_Error |
|
| 1283 | - */ |
|
| 1284 | - public function set_order($order) |
|
| 1285 | - { |
|
| 1286 | - $this->set('TKT_order', $order); |
|
| 1287 | - } |
|
| 1288 | - |
|
| 1289 | - |
|
| 1290 | - /** |
|
| 1291 | - * Gets row |
|
| 1292 | - * |
|
| 1293 | - * @return int |
|
| 1294 | - * @throws EE_Error |
|
| 1295 | - */ |
|
| 1296 | - public function row() |
|
| 1297 | - { |
|
| 1298 | - return $this->get('TKT_row'); |
|
| 1299 | - } |
|
| 1300 | - |
|
| 1301 | - |
|
| 1302 | - /** |
|
| 1303 | - * Sets row |
|
| 1304 | - * |
|
| 1305 | - * @param int $row |
|
| 1306 | - * @return void |
|
| 1307 | - * @throws EE_Error |
|
| 1308 | - */ |
|
| 1309 | - public function set_row($row) |
|
| 1310 | - { |
|
| 1311 | - $this->set('TKT_row', $row); |
|
| 1312 | - } |
|
| 1313 | - |
|
| 1314 | - |
|
| 1315 | - /** |
|
| 1316 | - * Gets deleted |
|
| 1317 | - * |
|
| 1318 | - * @return boolean |
|
| 1319 | - * @throws EE_Error |
|
| 1320 | - */ |
|
| 1321 | - public function deleted() |
|
| 1322 | - { |
|
| 1323 | - return $this->get('TKT_deleted'); |
|
| 1324 | - } |
|
| 1325 | - |
|
| 1326 | - |
|
| 1327 | - /** |
|
| 1328 | - * Sets deleted |
|
| 1329 | - * |
|
| 1330 | - * @param boolean $deleted |
|
| 1331 | - * @return void |
|
| 1332 | - * @throws EE_Error |
|
| 1333 | - */ |
|
| 1334 | - public function set_deleted($deleted) |
|
| 1335 | - { |
|
| 1336 | - $this->set('TKT_deleted', $deleted); |
|
| 1337 | - } |
|
| 1338 | - |
|
| 1339 | - |
|
| 1340 | - /** |
|
| 1341 | - * Gets parent |
|
| 1342 | - * |
|
| 1343 | - * @return int |
|
| 1344 | - * @throws EE_Error |
|
| 1345 | - */ |
|
| 1346 | - public function parent_ID() |
|
| 1347 | - { |
|
| 1348 | - return $this->get('TKT_parent'); |
|
| 1349 | - } |
|
| 1350 | - |
|
| 1351 | - |
|
| 1352 | - /** |
|
| 1353 | - * Sets parent |
|
| 1354 | - * |
|
| 1355 | - * @param int $parent |
|
| 1356 | - * @return void |
|
| 1357 | - * @throws EE_Error |
|
| 1358 | - */ |
|
| 1359 | - public function set_parent_ID($parent) |
|
| 1360 | - { |
|
| 1361 | - $this->set('TKT_parent', $parent); |
|
| 1362 | - } |
|
| 1363 | - |
|
| 1364 | - |
|
| 1365 | - /** |
|
| 1366 | - * Gets a string which is handy for showing in gateways etc that describes the ticket. |
|
| 1367 | - * |
|
| 1368 | - * @return string |
|
| 1369 | - * @throws EE_Error |
|
| 1370 | - */ |
|
| 1371 | - public function name_and_info() |
|
| 1372 | - { |
|
| 1373 | - $times = array(); |
|
| 1374 | - foreach ($this->datetimes() as $datetime) { |
|
| 1375 | - $times[] = $datetime->start_date_and_time(); |
|
| 1376 | - } |
|
| 1377 | - return $this->name() . ' @ ' . implode(', ', $times) . ' for ' . $this->pretty_price(); |
|
| 1378 | - } |
|
| 1379 | - |
|
| 1380 | - |
|
| 1381 | - /** |
|
| 1382 | - * Gets name |
|
| 1383 | - * |
|
| 1384 | - * @return string |
|
| 1385 | - * @throws EE_Error |
|
| 1386 | - */ |
|
| 1387 | - public function name() |
|
| 1388 | - { |
|
| 1389 | - return $this->get('TKT_name'); |
|
| 1390 | - } |
|
| 1391 | - |
|
| 1392 | - |
|
| 1393 | - /** |
|
| 1394 | - * Gets price |
|
| 1395 | - * |
|
| 1396 | - * @return float |
|
| 1397 | - * @throws EE_Error |
|
| 1398 | - */ |
|
| 1399 | - public function price() |
|
| 1400 | - { |
|
| 1401 | - return $this->get('TKT_price'); |
|
| 1402 | - } |
|
| 1403 | - |
|
| 1404 | - |
|
| 1405 | - /** |
|
| 1406 | - * Gets all the registrations for this ticket |
|
| 1407 | - * |
|
| 1408 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1409 | - * @return EE_Registration[]|EE_Base_Class[] |
|
| 1410 | - * @throws EE_Error |
|
| 1411 | - */ |
|
| 1412 | - public function registrations($query_params = array()) |
|
| 1413 | - { |
|
| 1414 | - return $this->get_many_related('Registration', $query_params); |
|
| 1415 | - } |
|
| 1416 | - |
|
| 1417 | - |
|
| 1418 | - /** |
|
| 1419 | - * Updates the TKT_sold attribute (and saves) based on the number of APPROVED registrations for this ticket. |
|
| 1420 | - * |
|
| 1421 | - * @return int |
|
| 1422 | - * @throws EE_Error |
|
| 1423 | - */ |
|
| 1424 | - public function update_tickets_sold() |
|
| 1425 | - { |
|
| 1426 | - $count_regs_for_this_ticket = $this->count_registrations( |
|
| 1427 | - array( |
|
| 1428 | - array( |
|
| 1429 | - 'STS_ID' => EEM_Registration::status_id_approved, |
|
| 1430 | - 'REG_deleted' => 0, |
|
| 1431 | - ), |
|
| 1432 | - ) |
|
| 1433 | - ); |
|
| 1434 | - $this->set_sold($count_regs_for_this_ticket); |
|
| 1435 | - $this->save(); |
|
| 1436 | - return $count_regs_for_this_ticket; |
|
| 1437 | - } |
|
| 1438 | - |
|
| 1439 | - |
|
| 1440 | - /** |
|
| 1441 | - * Counts the registrations for this ticket |
|
| 1442 | - * |
|
| 1443 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1444 | - * @return int |
|
| 1445 | - */ |
|
| 1446 | - public function count_registrations($query_params = array()) |
|
| 1447 | - { |
|
| 1448 | - return $this->count_related('Registration', $query_params); |
|
| 1449 | - } |
|
| 1450 | - |
|
| 1451 | - |
|
| 1452 | - /** |
|
| 1453 | - * Implementation for EEI_Has_Icon interface method. |
|
| 1454 | - * |
|
| 1455 | - * @see EEI_Visual_Representation for comments |
|
| 1456 | - * @return string |
|
| 1457 | - */ |
|
| 1458 | - public function get_icon() |
|
| 1459 | - { |
|
| 1460 | - return '<span class="dashicons dashicons-tickets-alt"></span>'; |
|
| 1461 | - } |
|
| 1462 | - |
|
| 1463 | - |
|
| 1464 | - /** |
|
| 1465 | - * Implementation of the EEI_Event_Relation interface method |
|
| 1466 | - * |
|
| 1467 | - * @see EEI_Event_Relation for comments |
|
| 1468 | - * @return EE_Event |
|
| 1469 | - * @throws EE_Error |
|
| 1470 | - * @throws UnexpectedEntityException |
|
| 1471 | - */ |
|
| 1472 | - public function get_related_event() |
|
| 1473 | - { |
|
| 1474 | - // get one datetime to use for getting the event |
|
| 1475 | - $datetime = $this->first_datetime(); |
|
| 1476 | - if (! $datetime instanceof \EE_Datetime) { |
|
| 1477 | - throw new UnexpectedEntityException( |
|
| 1478 | - $datetime, |
|
| 1479 | - 'EE_Datetime', |
|
| 1480 | - sprintf( |
|
| 1481 | - __('The ticket (%s) is not associated with any valid datetimes.', 'event_espresso'), |
|
| 1482 | - $this->name() |
|
| 1483 | - ) |
|
| 1484 | - ); |
|
| 1485 | - } |
|
| 1486 | - $event = $datetime->event(); |
|
| 1487 | - if (! $event instanceof \EE_Event) { |
|
| 1488 | - throw new UnexpectedEntityException( |
|
| 1489 | - $event, |
|
| 1490 | - 'EE_Event', |
|
| 1491 | - sprintf( |
|
| 1492 | - __('The ticket (%s) is not associated with a valid event.', 'event_espresso'), |
|
| 1493 | - $this->name() |
|
| 1494 | - ) |
|
| 1495 | - ); |
|
| 1496 | - } |
|
| 1497 | - return $event; |
|
| 1498 | - } |
|
| 1499 | - |
|
| 1500 | - |
|
| 1501 | - /** |
|
| 1502 | - * Implementation of the EEI_Event_Relation interface method |
|
| 1503 | - * |
|
| 1504 | - * @see EEI_Event_Relation for comments |
|
| 1505 | - * @return string |
|
| 1506 | - * @throws UnexpectedEntityException |
|
| 1507 | - * @throws EE_Error |
|
| 1508 | - */ |
|
| 1509 | - public function get_event_name() |
|
| 1510 | - { |
|
| 1511 | - $event = $this->get_related_event(); |
|
| 1512 | - return $event instanceof EE_Event ? $event->name() : ''; |
|
| 1513 | - } |
|
| 1514 | - |
|
| 1515 | - |
|
| 1516 | - /** |
|
| 1517 | - * Implementation of the EEI_Event_Relation interface method |
|
| 1518 | - * |
|
| 1519 | - * @see EEI_Event_Relation for comments |
|
| 1520 | - * @return int |
|
| 1521 | - * @throws UnexpectedEntityException |
|
| 1522 | - * @throws EE_Error |
|
| 1523 | - */ |
|
| 1524 | - public function get_event_ID() |
|
| 1525 | - { |
|
| 1526 | - $event = $this->get_related_event(); |
|
| 1527 | - return $event instanceof EE_Event ? $event->ID() : 0; |
|
| 1528 | - } |
|
| 1529 | - |
|
| 1530 | - |
|
| 1531 | - /** |
|
| 1532 | - * This simply returns whether a ticket can be permanently deleted or not. |
|
| 1533 | - * The criteria for determining this is whether the ticket has any related registrations. |
|
| 1534 | - * If there are none then it can be permanently deleted. |
|
| 1535 | - * |
|
| 1536 | - * @return bool |
|
| 1537 | - */ |
|
| 1538 | - public function is_permanently_deleteable() |
|
| 1539 | - { |
|
| 1540 | - return $this->count_registrations() === 0; |
|
| 1541 | - } |
|
| 1542 | - |
|
| 1543 | - |
|
| 1544 | - /******************************************************************* |
|
| 17 | + /** |
|
| 18 | + * The following constants are used by the ticket_status() method to indicate whether a ticket is on sale or not. |
|
| 19 | + */ |
|
| 20 | + const sold_out = 'TKS'; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * |
|
| 24 | + */ |
|
| 25 | + const expired = 'TKE'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * |
|
| 29 | + */ |
|
| 30 | + const archived = 'TKA'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * |
|
| 34 | + */ |
|
| 35 | + const pending = 'TKP'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * |
|
| 39 | + */ |
|
| 40 | + const onsale = 'TKO'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * extra meta key for tracking ticket reservations |
|
| 44 | + * |
|
| 45 | + * @type string |
|
| 46 | + */ |
|
| 47 | + const META_KEY_TICKET_RESERVATIONS = 'ticket_reservations'; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * cached result from method of the same name |
|
| 51 | + * |
|
| 52 | + * @var float $_ticket_total_with_taxes |
|
| 53 | + */ |
|
| 54 | + private $_ticket_total_with_taxes; |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param array $props_n_values incoming values |
|
| 59 | + * @param string $timezone incoming timezone (if not set the timezone set for the website will be |
|
| 60 | + * used.) |
|
| 61 | + * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 62 | + * date_format and the second value is the time format |
|
| 63 | + * @return EE_Ticket |
|
| 64 | + * @throws EE_Error |
|
| 65 | + */ |
|
| 66 | + public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array()) |
|
| 67 | + { |
|
| 68 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats); |
|
| 69 | + return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @param array $props_n_values incoming values from the database |
|
| 75 | + * @param string $timezone incoming timezone as set by the model. If not set the timezone for |
|
| 76 | + * the website will be used. |
|
| 77 | + * @return EE_Ticket |
|
| 78 | + * @throws EE_Error |
|
| 79 | + */ |
|
| 80 | + public static function new_instance_from_db($props_n_values = array(), $timezone = null) |
|
| 81 | + { |
|
| 82 | + return new self($props_n_values, true, $timezone); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @return bool |
|
| 88 | + * @throws EE_Error |
|
| 89 | + */ |
|
| 90 | + public function parent() |
|
| 91 | + { |
|
| 92 | + return $this->get('TKT_parent'); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * return if a ticket has quantities available for purchase |
|
| 98 | + * |
|
| 99 | + * @param int $DTT_ID the primary key for a particular datetime |
|
| 100 | + * @return boolean |
|
| 101 | + * @throws EE_Error |
|
| 102 | + */ |
|
| 103 | + public function available($DTT_ID = 0) |
|
| 104 | + { |
|
| 105 | + // are we checking availability for a particular datetime ? |
|
| 106 | + if ($DTT_ID) { |
|
| 107 | + // get that datetime object |
|
| 108 | + $datetime = $this->get_first_related('Datetime', array(array('DTT_ID' => $DTT_ID))); |
|
| 109 | + // if ticket sales for this datetime have exceeded the reg limit... |
|
| 110 | + if ($datetime instanceof EE_Datetime && $datetime->sold_out()) { |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + // datetime is still open for registration, but is this ticket sold out ? |
|
| 115 | + return $this->qty() < 1 || $this->qty() > $this->sold() ? true : false; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Using the start date and end date this method calculates whether the ticket is On Sale, Pending, or Expired |
|
| 121 | + * |
|
| 122 | + * @param bool $display true = we'll return a localized string, otherwise we just return the value of the |
|
| 123 | + * relevant status const |
|
| 124 | + * @param bool | null $remaining if it is already known that tickets are available, then simply pass a bool to save |
|
| 125 | + * further processing |
|
| 126 | + * @return mixed status int if the display string isn't requested |
|
| 127 | + * @throws EE_Error |
|
| 128 | + */ |
|
| 129 | + public function ticket_status($display = false, $remaining = null) |
|
| 130 | + { |
|
| 131 | + $remaining = is_bool($remaining) ? $remaining : $this->is_remaining(); |
|
| 132 | + if (! $remaining) { |
|
| 133 | + return $display ? EEH_Template::pretty_status(EE_Ticket::sold_out, false, 'sentence') : EE_Ticket::sold_out; |
|
| 134 | + } |
|
| 135 | + if ($this->get('TKT_deleted')) { |
|
| 136 | + return $display ? EEH_Template::pretty_status(EE_Ticket::archived, false, 'sentence') : EE_Ticket::archived; |
|
| 137 | + } |
|
| 138 | + if ($this->is_expired()) { |
|
| 139 | + return $display ? EEH_Template::pretty_status(EE_Ticket::expired, false, 'sentence') : EE_Ticket::expired; |
|
| 140 | + } |
|
| 141 | + if ($this->is_pending()) { |
|
| 142 | + return $display ? EEH_Template::pretty_status(EE_Ticket::pending, false, 'sentence') : EE_Ticket::pending; |
|
| 143 | + } |
|
| 144 | + if ($this->is_on_sale()) { |
|
| 145 | + return $display ? EEH_Template::pretty_status(EE_Ticket::onsale, false, 'sentence') : EE_Ticket::onsale; |
|
| 146 | + } |
|
| 147 | + return ''; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * The purpose of this method is to simply return a boolean for whether there are any tickets remaining for sale |
|
| 153 | + * considering ALL the factors used for figuring that out. |
|
| 154 | + * |
|
| 155 | + * @access public |
|
| 156 | + * @param int $DTT_ID if an int above 0 is included here then we get a specific dtt. |
|
| 157 | + * @return boolean true = tickets remaining, false not. |
|
| 158 | + * @throws EE_Error |
|
| 159 | + */ |
|
| 160 | + public function is_remaining($DTT_ID = 0) |
|
| 161 | + { |
|
| 162 | + $num_remaining = $this->remaining($DTT_ID); |
|
| 163 | + if ($num_remaining === 0) { |
|
| 164 | + return false; |
|
| 165 | + } |
|
| 166 | + if ($num_remaining > 0 && $num_remaining < $this->min()) { |
|
| 167 | + return false; |
|
| 168 | + } |
|
| 169 | + return true; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * return the total number of tickets available for purchase |
|
| 175 | + * |
|
| 176 | + * @param int $DTT_ID the primary key for a particular datetime. |
|
| 177 | + * set to 0 for all related datetimes |
|
| 178 | + * @return int |
|
| 179 | + * @throws EE_Error |
|
| 180 | + */ |
|
| 181 | + public function remaining($DTT_ID = 0) |
|
| 182 | + { |
|
| 183 | + return $this->real_quantity_on_ticket('saleable', $DTT_ID); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Gets min |
|
| 189 | + * |
|
| 190 | + * @return int |
|
| 191 | + * @throws EE_Error |
|
| 192 | + */ |
|
| 193 | + public function min() |
|
| 194 | + { |
|
| 195 | + return $this->get('TKT_min'); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * return if a ticket is no longer available cause its available dates have expired. |
|
| 201 | + * |
|
| 202 | + * @return boolean |
|
| 203 | + * @throws EE_Error |
|
| 204 | + */ |
|
| 205 | + public function is_expired() |
|
| 206 | + { |
|
| 207 | + return ($this->get_raw('TKT_end_date') < time()); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Return if a ticket is yet to go on sale or not |
|
| 213 | + * |
|
| 214 | + * @return boolean |
|
| 215 | + * @throws EE_Error |
|
| 216 | + */ |
|
| 217 | + public function is_pending() |
|
| 218 | + { |
|
| 219 | + return ($this->get_raw('TKT_start_date') > time()); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Return if a ticket is on sale or not |
|
| 225 | + * |
|
| 226 | + * @return boolean |
|
| 227 | + * @throws EE_Error |
|
| 228 | + */ |
|
| 229 | + public function is_on_sale() |
|
| 230 | + { |
|
| 231 | + return ($this->get_raw('TKT_start_date') < time() && $this->get_raw('TKT_end_date') > time()); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * This returns the chronologically last datetime that this ticket is associated with |
|
| 237 | + * |
|
| 238 | + * @param string $dt_frmt |
|
| 239 | + * @param string $conjunction - conjunction junction what's your function ? this string joins the start date with |
|
| 240 | + * the end date ie: Jan 01 "to" Dec 31 |
|
| 241 | + * @return string |
|
| 242 | + * @throws EE_Error |
|
| 243 | + */ |
|
| 244 | + public function date_range($dt_frmt = '', $conjunction = ' - ') |
|
| 245 | + { |
|
| 246 | + $dt_frmt = ! empty($dt_frmt) ? $dt_frmt : $this->_dt_frmt; |
|
| 247 | + $first_date = $this->first_datetime() instanceof EE_Datetime ? $this->first_datetime()->get_i18n_datetime('DTT_EVT_start', $dt_frmt) |
|
| 248 | + : ''; |
|
| 249 | + $last_date = $this->last_datetime() instanceof EE_Datetime ? $this->last_datetime()->get_i18n_datetime('DTT_EVT_end', $dt_frmt) : ''; |
|
| 250 | + |
|
| 251 | + return $first_date && $last_date ? $first_date . $conjunction . $last_date : ''; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * This returns the chronologically first datetime that this ticket is associated with |
|
| 257 | + * |
|
| 258 | + * @return EE_Datetime |
|
| 259 | + * @throws EE_Error |
|
| 260 | + */ |
|
| 261 | + public function first_datetime() |
|
| 262 | + { |
|
| 263 | + $datetimes = $this->datetimes(array('limit' => 1)); |
|
| 264 | + return reset($datetimes); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Gets all the datetimes this ticket can be used for attending. |
|
| 270 | + * Unless otherwise specified, orders datetimes by start date. |
|
| 271 | + * |
|
| 272 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 273 | + * @return EE_Datetime[]|EE_Base_Class[] |
|
| 274 | + * @throws EE_Error |
|
| 275 | + */ |
|
| 276 | + public function datetimes($query_params = array()) |
|
| 277 | + { |
|
| 278 | + if (! isset($query_params['order_by'])) { |
|
| 279 | + $query_params['order_by']['DTT_order'] = 'ASC'; |
|
| 280 | + } |
|
| 281 | + return $this->get_many_related('Datetime', $query_params); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * This returns the chronologically last datetime that this ticket is associated with |
|
| 287 | + * |
|
| 288 | + * @return EE_Datetime |
|
| 289 | + * @throws EE_Error |
|
| 290 | + */ |
|
| 291 | + public function last_datetime() |
|
| 292 | + { |
|
| 293 | + $datetimes = $this->datetimes(array('limit' => 1, 'order_by' => array('DTT_EVT_start' => 'DESC'))); |
|
| 294 | + return end($datetimes); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * This returns the total tickets sold depending on the given parameters. |
|
| 300 | + * |
|
| 301 | + * @param string $what Can be one of two options: 'ticket', 'datetime'. |
|
| 302 | + * 'ticket' = total ticket sales for all datetimes this ticket is related to |
|
| 303 | + * 'datetime' = total ticket sales for a specified datetime (required $dtt_id) |
|
| 304 | + * 'datetime' = total ticket sales in the datetime_ticket table. |
|
| 305 | + * If $dtt_id is not given then we return an array of sales indexed by datetime. |
|
| 306 | + * If $dtt_id IS given then we return the tickets sold for that given datetime. |
|
| 307 | + * @param int $dtt_id [optional] include the dtt_id with $what = 'datetime'. |
|
| 308 | + * @return mixed (array|int) how many tickets have sold |
|
| 309 | + * @throws EE_Error |
|
| 310 | + */ |
|
| 311 | + public function tickets_sold($what = 'ticket', $dtt_id = null) |
|
| 312 | + { |
|
| 313 | + $total = 0; |
|
| 314 | + $tickets_sold = $this->_all_tickets_sold(); |
|
| 315 | + switch ($what) { |
|
| 316 | + case 'ticket': |
|
| 317 | + return $tickets_sold['ticket']; |
|
| 318 | + break; |
|
| 319 | + case 'datetime': |
|
| 320 | + if (empty($tickets_sold['datetime'])) { |
|
| 321 | + return $total; |
|
| 322 | + } |
|
| 323 | + if (! empty($dtt_id) && ! isset($tickets_sold['datetime'][ $dtt_id ])) { |
|
| 324 | + EE_Error::add_error( |
|
| 325 | + __( |
|
| 326 | + 'You\'ve requested the amount of tickets sold for a given ticket and datetime, however there are no records for the datetime id you included. Are you SURE that is a datetime related to this ticket?', |
|
| 327 | + 'event_espresso' |
|
| 328 | + ), |
|
| 329 | + __FILE__, |
|
| 330 | + __FUNCTION__, |
|
| 331 | + __LINE__ |
|
| 332 | + ); |
|
| 333 | + return $total; |
|
| 334 | + } |
|
| 335 | + return empty($dtt_id) ? $tickets_sold['datetime'] : $tickets_sold['datetime'][ $dtt_id ]; |
|
| 336 | + break; |
|
| 337 | + default: |
|
| 338 | + return $total; |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * This returns an array indexed by datetime_id for tickets sold with this ticket. |
|
| 345 | + * |
|
| 346 | + * @return EE_Ticket[] |
|
| 347 | + * @throws EE_Error |
|
| 348 | + */ |
|
| 349 | + protected function _all_tickets_sold() |
|
| 350 | + { |
|
| 351 | + $datetimes = $this->get_many_related('Datetime'); |
|
| 352 | + $tickets_sold = array(); |
|
| 353 | + if (! empty($datetimes)) { |
|
| 354 | + foreach ($datetimes as $datetime) { |
|
| 355 | + $tickets_sold['datetime'][ $datetime->ID() ] = $datetime->get('DTT_sold'); |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + // Tickets sold |
|
| 359 | + $tickets_sold['ticket'] = $this->sold(); |
|
| 360 | + return $tickets_sold; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * This returns the base price object for the ticket. |
|
| 366 | + * |
|
| 367 | + * @param bool $return_array whether to return as an array indexed by price id or just the object. |
|
| 368 | + * @return EE_Price|EE_Base_Class|EE_Price[]|EE_Base_Class[] |
|
| 369 | + * @throws EE_Error |
|
| 370 | + */ |
|
| 371 | + public function base_price($return_array = false) |
|
| 372 | + { |
|
| 373 | + $_where = array('Price_Type.PBT_ID' => EEM_Price_Type::base_type_base_price); |
|
| 374 | + return $return_array |
|
| 375 | + ? $this->get_many_related('Price', array($_where)) |
|
| 376 | + : $this->get_first_related('Price', array($_where)); |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * This returns ONLY the price modifiers for the ticket (i.e. no taxes or base price) |
|
| 382 | + * |
|
| 383 | + * @access public |
|
| 384 | + * @return EE_Price[] |
|
| 385 | + * @throws EE_Error |
|
| 386 | + */ |
|
| 387 | + public function price_modifiers() |
|
| 388 | + { |
|
| 389 | + $query_params = array( |
|
| 390 | + 0 => array( |
|
| 391 | + 'Price_Type.PBT_ID' => array( |
|
| 392 | + 'NOT IN', |
|
| 393 | + array(EEM_Price_Type::base_type_base_price, EEM_Price_Type::base_type_tax), |
|
| 394 | + ), |
|
| 395 | + ), |
|
| 396 | + ); |
|
| 397 | + return $this->prices($query_params); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * Gets all the prices that combine to form the final price of this ticket |
|
| 403 | + * |
|
| 404 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 405 | + * @return EE_Price[]|EE_Base_Class[] |
|
| 406 | + * @throws EE_Error |
|
| 407 | + */ |
|
| 408 | + public function prices($query_params = array()) |
|
| 409 | + { |
|
| 410 | + return $this->get_many_related('Price', $query_params); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Gets all the ticket applicabilities (ie, relations between datetimes and tickets) |
|
| 416 | + * |
|
| 417 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 418 | + * @return EE_Datetime_Ticket|EE_Base_Class[] |
|
| 419 | + * @throws EE_Error |
|
| 420 | + */ |
|
| 421 | + public function datetime_tickets($query_params = array()) |
|
| 422 | + { |
|
| 423 | + return $this->get_many_related('Datetime_Ticket', $query_params); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * Gets all the datetimes from the db ordered by DTT_order |
|
| 429 | + * |
|
| 430 | + * @param boolean $show_expired |
|
| 431 | + * @param boolean $show_deleted |
|
| 432 | + * @return EE_Datetime[] |
|
| 433 | + * @throws EE_Error |
|
| 434 | + */ |
|
| 435 | + public function datetimes_ordered($show_expired = true, $show_deleted = false) |
|
| 436 | + { |
|
| 437 | + return EEM_Datetime::instance($this->_timezone)->get_datetimes_for_ticket_ordered_by_DTT_order( |
|
| 438 | + $this->ID(), |
|
| 439 | + $show_expired, |
|
| 440 | + $show_deleted |
|
| 441 | + ); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Gets ID |
|
| 447 | + * |
|
| 448 | + * @return string |
|
| 449 | + * @throws EE_Error |
|
| 450 | + */ |
|
| 451 | + public function ID() |
|
| 452 | + { |
|
| 453 | + return $this->get('TKT_ID'); |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * get the author of the ticket. |
|
| 459 | + * |
|
| 460 | + * @since 4.5.0 |
|
| 461 | + * @return int |
|
| 462 | + * @throws EE_Error |
|
| 463 | + */ |
|
| 464 | + public function wp_user() |
|
| 465 | + { |
|
| 466 | + return $this->get('TKT_wp_user'); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * Gets the template for the ticket |
|
| 472 | + * |
|
| 473 | + * @return EE_Ticket_Template|EE_Base_Class |
|
| 474 | + * @throws EE_Error |
|
| 475 | + */ |
|
| 476 | + public function template() |
|
| 477 | + { |
|
| 478 | + return $this->get_first_related('Ticket_Template'); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Simply returns an array of EE_Price objects that are taxes. |
|
| 484 | + * |
|
| 485 | + * @return EE_Price[] |
|
| 486 | + * @throws EE_Error |
|
| 487 | + */ |
|
| 488 | + public function get_ticket_taxes_for_admin() |
|
| 489 | + { |
|
| 490 | + return EE_Taxes::get_taxes_for_admin(); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * @return float |
|
| 496 | + * @throws EE_Error |
|
| 497 | + */ |
|
| 498 | + public function ticket_price() |
|
| 499 | + { |
|
| 500 | + return $this->get('TKT_price'); |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * @return mixed |
|
| 506 | + * @throws EE_Error |
|
| 507 | + */ |
|
| 508 | + public function pretty_price() |
|
| 509 | + { |
|
| 510 | + return $this->get_pretty('TKT_price'); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * @return bool |
|
| 516 | + * @throws EE_Error |
|
| 517 | + */ |
|
| 518 | + public function is_free() |
|
| 519 | + { |
|
| 520 | + return $this->get_ticket_total_with_taxes() === (float) 0; |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + |
|
| 524 | + /** |
|
| 525 | + * get_ticket_total_with_taxes |
|
| 526 | + * |
|
| 527 | + * @param bool $no_cache |
|
| 528 | + * @return float |
|
| 529 | + * @throws EE_Error |
|
| 530 | + */ |
|
| 531 | + public function get_ticket_total_with_taxes($no_cache = false) |
|
| 532 | + { |
|
| 533 | + if ($this->_ticket_total_with_taxes === null || $no_cache) { |
|
| 534 | + $this->_ticket_total_with_taxes = $this->get_ticket_subtotal() + $this->get_ticket_taxes_total_for_admin(); |
|
| 535 | + } |
|
| 536 | + return (float) $this->_ticket_total_with_taxes; |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + |
|
| 540 | + public function ensure_TKT_Price_correct() |
|
| 541 | + { |
|
| 542 | + $this->set('TKT_price', EE_Taxes::get_subtotal_for_admin($this)); |
|
| 543 | + $this->save(); |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + |
|
| 547 | + /** |
|
| 548 | + * @return float |
|
| 549 | + * @throws EE_Error |
|
| 550 | + */ |
|
| 551 | + public function get_ticket_subtotal() |
|
| 552 | + { |
|
| 553 | + return EE_Taxes::get_subtotal_for_admin($this); |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * Returns the total taxes applied to this ticket |
|
| 559 | + * |
|
| 560 | + * @return float |
|
| 561 | + * @throws EE_Error |
|
| 562 | + */ |
|
| 563 | + public function get_ticket_taxes_total_for_admin() |
|
| 564 | + { |
|
| 565 | + return EE_Taxes::get_total_taxes_for_admin($this); |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Sets name |
|
| 571 | + * |
|
| 572 | + * @param string $name |
|
| 573 | + * @throws EE_Error |
|
| 574 | + */ |
|
| 575 | + public function set_name($name) |
|
| 576 | + { |
|
| 577 | + $this->set('TKT_name', $name); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * Gets description |
|
| 583 | + * |
|
| 584 | + * @return string |
|
| 585 | + * @throws EE_Error |
|
| 586 | + */ |
|
| 587 | + public function description() |
|
| 588 | + { |
|
| 589 | + return $this->get('TKT_description'); |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * Sets description |
|
| 595 | + * |
|
| 596 | + * @param string $description |
|
| 597 | + * @throws EE_Error |
|
| 598 | + */ |
|
| 599 | + public function set_description($description) |
|
| 600 | + { |
|
| 601 | + $this->set('TKT_description', $description); |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + |
|
| 605 | + /** |
|
| 606 | + * Gets start_date |
|
| 607 | + * |
|
| 608 | + * @param string $dt_frmt |
|
| 609 | + * @param string $tm_frmt |
|
| 610 | + * @return string |
|
| 611 | + * @throws EE_Error |
|
| 612 | + */ |
|
| 613 | + public function start_date($dt_frmt = '', $tm_frmt = '') |
|
| 614 | + { |
|
| 615 | + return $this->_get_datetime('TKT_start_date', $dt_frmt, $tm_frmt); |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + |
|
| 619 | + /** |
|
| 620 | + * Sets start_date |
|
| 621 | + * |
|
| 622 | + * @param string $start_date |
|
| 623 | + * @return void |
|
| 624 | + * @throws EE_Error |
|
| 625 | + */ |
|
| 626 | + public function set_start_date($start_date) |
|
| 627 | + { |
|
| 628 | + $this->_set_date_time('B', $start_date, 'TKT_start_date'); |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * Gets end_date |
|
| 634 | + * |
|
| 635 | + * @param string $dt_frmt |
|
| 636 | + * @param string $tm_frmt |
|
| 637 | + * @return string |
|
| 638 | + * @throws EE_Error |
|
| 639 | + */ |
|
| 640 | + public function end_date($dt_frmt = '', $tm_frmt = '') |
|
| 641 | + { |
|
| 642 | + return $this->_get_datetime('TKT_end_date', $dt_frmt, $tm_frmt); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + |
|
| 646 | + /** |
|
| 647 | + * Sets end_date |
|
| 648 | + * |
|
| 649 | + * @param string $end_date |
|
| 650 | + * @return void |
|
| 651 | + * @throws EE_Error |
|
| 652 | + */ |
|
| 653 | + public function set_end_date($end_date) |
|
| 654 | + { |
|
| 655 | + $this->_set_date_time('B', $end_date, 'TKT_end_date'); |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + |
|
| 659 | + /** |
|
| 660 | + * Sets sell until time |
|
| 661 | + * |
|
| 662 | + * @since 4.5.0 |
|
| 663 | + * @param string $time a string representation of the sell until time (ex 9am or 7:30pm) |
|
| 664 | + * @throws EE_Error |
|
| 665 | + */ |
|
| 666 | + public function set_end_time($time) |
|
| 667 | + { |
|
| 668 | + $this->_set_time_for($time, 'TKT_end_date'); |
|
| 669 | + } |
|
| 670 | + |
|
| 671 | + |
|
| 672 | + /** |
|
| 673 | + * Sets min |
|
| 674 | + * |
|
| 675 | + * @param int $min |
|
| 676 | + * @return void |
|
| 677 | + * @throws EE_Error |
|
| 678 | + */ |
|
| 679 | + public function set_min($min) |
|
| 680 | + { |
|
| 681 | + $this->set('TKT_min', $min); |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + |
|
| 685 | + /** |
|
| 686 | + * Gets max |
|
| 687 | + * |
|
| 688 | + * @return int |
|
| 689 | + * @throws EE_Error |
|
| 690 | + */ |
|
| 691 | + public function max() |
|
| 692 | + { |
|
| 693 | + return $this->get('TKT_max'); |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + |
|
| 697 | + /** |
|
| 698 | + * Sets max |
|
| 699 | + * |
|
| 700 | + * @param int $max |
|
| 701 | + * @return void |
|
| 702 | + * @throws EE_Error |
|
| 703 | + */ |
|
| 704 | + public function set_max($max) |
|
| 705 | + { |
|
| 706 | + $this->set('TKT_max', $max); |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * Sets price |
|
| 712 | + * |
|
| 713 | + * @param float $price |
|
| 714 | + * @return void |
|
| 715 | + * @throws EE_Error |
|
| 716 | + */ |
|
| 717 | + public function set_price($price) |
|
| 718 | + { |
|
| 719 | + $this->set('TKT_price', $price); |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + |
|
| 723 | + /** |
|
| 724 | + * Gets sold |
|
| 725 | + * |
|
| 726 | + * @return int |
|
| 727 | + * @throws EE_Error |
|
| 728 | + */ |
|
| 729 | + public function sold() |
|
| 730 | + { |
|
| 731 | + return $this->get_raw('TKT_sold'); |
|
| 732 | + } |
|
| 733 | + |
|
| 734 | + |
|
| 735 | + /** |
|
| 736 | + * Sets sold |
|
| 737 | + * |
|
| 738 | + * @param int $sold |
|
| 739 | + * @return void |
|
| 740 | + * @throws EE_Error |
|
| 741 | + */ |
|
| 742 | + public function set_sold($sold) |
|
| 743 | + { |
|
| 744 | + // sold can not go below zero |
|
| 745 | + $sold = max(0, $sold); |
|
| 746 | + $this->set('TKT_sold', $sold); |
|
| 747 | + } |
|
| 748 | + |
|
| 749 | + |
|
| 750 | + /** |
|
| 751 | + * Increments sold by amount passed by $qty AND decrements the reserved count on both this ticket and its |
|
| 752 | + * associated datetimes. |
|
| 753 | + * |
|
| 754 | + * @since 4.9.80.p |
|
| 755 | + * @param int $qty |
|
| 756 | + * @return boolean |
|
| 757 | + * @throws EE_Error |
|
| 758 | + * @throws InvalidArgumentException |
|
| 759 | + * @throws InvalidDataTypeException |
|
| 760 | + * @throws InvalidInterfaceException |
|
| 761 | + * @throws ReflectionException |
|
| 762 | + */ |
|
| 763 | + public function increaseSold($qty = 1) |
|
| 764 | + { |
|
| 765 | + $qty = absint($qty); |
|
| 766 | + // increment sold and decrement reserved datetime quantities simultaneously |
|
| 767 | + // don't worry about failures, because they must have already had a spot reserved |
|
| 768 | + $this->increaseSoldForDatetimes($qty); |
|
| 769 | + // Increment and decrement ticket quantities simultaneously |
|
| 770 | + $success = $this->adjustNumericFieldsInDb( |
|
| 771 | + [ |
|
| 772 | + 'TKT_reserved' => $qty * -1, |
|
| 773 | + 'TKT_sold' => $qty |
|
| 774 | + ] |
|
| 775 | + ); |
|
| 776 | + do_action( |
|
| 777 | + 'AHEE__EE_Ticket__increase_sold', |
|
| 778 | + $this, |
|
| 779 | + $qty, |
|
| 780 | + $this->sold(), |
|
| 781 | + $success |
|
| 782 | + ); |
|
| 783 | + return $success; |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * On each datetime related to this ticket, increases its sold count and decreases its reserved count by $qty. |
|
| 788 | + * |
|
| 789 | + * @since 4.9.80.p |
|
| 790 | + * @param int $qty positive or negative. Positive means to increase sold counts (and decrease reserved counts), |
|
| 791 | + * Negative means to decreases old counts (and increase reserved counts). |
|
| 792 | + * @param EE_Datetime[] $datetimes |
|
| 793 | + * @throws EE_Error |
|
| 794 | + * @throws InvalidArgumentException |
|
| 795 | + * @throws InvalidDataTypeException |
|
| 796 | + * @throws InvalidInterfaceException |
|
| 797 | + * @throws ReflectionException |
|
| 798 | + */ |
|
| 799 | + protected function increaseSoldForDatetimes($qty, array $datetimes = []) |
|
| 800 | + { |
|
| 801 | + $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes(); |
|
| 802 | + foreach ($datetimes as $datetime) { |
|
| 803 | + $datetime->increaseSold($qty); |
|
| 804 | + } |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + |
|
| 808 | + |
|
| 809 | + /** |
|
| 810 | + * Decrements (subtracts) sold by amount passed by $qty on both the ticket and its related datetimes directly in the |
|
| 811 | + * DB and then updates the model objects. |
|
| 812 | + * Does not affect the reserved counts. |
|
| 813 | + * |
|
| 814 | + * @since 4.9.80.p |
|
| 815 | + * @param int $qty |
|
| 816 | + * @return boolean |
|
| 817 | + * @throws EE_Error |
|
| 818 | + * @throws InvalidArgumentException |
|
| 819 | + * @throws InvalidDataTypeException |
|
| 820 | + * @throws InvalidInterfaceException |
|
| 821 | + * @throws ReflectionException |
|
| 822 | + */ |
|
| 823 | + public function decreaseSold($qty = 1) |
|
| 824 | + { |
|
| 825 | + $qty = absint($qty); |
|
| 826 | + $this->decreaseSoldForDatetimes($qty); |
|
| 827 | + $success = $this->adjustNumericFieldsInDb( |
|
| 828 | + [ |
|
| 829 | + 'TKT_sold' => $qty * -1 |
|
| 830 | + ] |
|
| 831 | + ); |
|
| 832 | + do_action( |
|
| 833 | + 'AHEE__EE_Ticket__decrease_sold', |
|
| 834 | + $this, |
|
| 835 | + $qty, |
|
| 836 | + $this->sold(), |
|
| 837 | + $success |
|
| 838 | + ); |
|
| 839 | + return $success; |
|
| 840 | + } |
|
| 841 | + |
|
| 842 | + |
|
| 843 | + /** |
|
| 844 | + * Decreases sold on related datetimes |
|
| 845 | + * |
|
| 846 | + * @since 4.9.80.p |
|
| 847 | + * @param int $qty |
|
| 848 | + * @param EE_Datetime[] $datetimes |
|
| 849 | + * @return void |
|
| 850 | + * @throws EE_Error |
|
| 851 | + * @throws InvalidArgumentException |
|
| 852 | + * @throws InvalidDataTypeException |
|
| 853 | + * @throws InvalidInterfaceException |
|
| 854 | + * @throws ReflectionException |
|
| 855 | + */ |
|
| 856 | + protected function decreaseSoldForDatetimes($qty = 1, array $datetimes = []) |
|
| 857 | + { |
|
| 858 | + $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes(); |
|
| 859 | + if (is_array($datetimes)) { |
|
| 860 | + foreach ($datetimes as $datetime) { |
|
| 861 | + if ($datetime instanceof EE_Datetime) { |
|
| 862 | + $datetime->decreaseSold($qty); |
|
| 863 | + } |
|
| 864 | + } |
|
| 865 | + } |
|
| 866 | + } |
|
| 867 | + |
|
| 868 | + |
|
| 869 | + /** |
|
| 870 | + * Gets qty of reserved tickets |
|
| 871 | + * |
|
| 872 | + * @return int |
|
| 873 | + * @throws EE_Error |
|
| 874 | + */ |
|
| 875 | + public function reserved() |
|
| 876 | + { |
|
| 877 | + return $this->get_raw('TKT_reserved'); |
|
| 878 | + } |
|
| 879 | + |
|
| 880 | + |
|
| 881 | + /** |
|
| 882 | + * Sets reserved |
|
| 883 | + * |
|
| 884 | + * @param int $reserved |
|
| 885 | + * @return void |
|
| 886 | + * @throws EE_Error |
|
| 887 | + */ |
|
| 888 | + public function set_reserved($reserved) |
|
| 889 | + { |
|
| 890 | + // reserved can not go below zero |
|
| 891 | + $reserved = max(0, (int) $reserved); |
|
| 892 | + $this->set('TKT_reserved', $reserved); |
|
| 893 | + } |
|
| 894 | + |
|
| 895 | + |
|
| 896 | + /** |
|
| 897 | + * Increments reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 898 | + * |
|
| 899 | + * @since 4.9.80.p |
|
| 900 | + * @param int $qty |
|
| 901 | + * @param string $source |
|
| 902 | + * @return bool whether we successfully reserved the ticket or not. |
|
| 903 | + * @throws EE_Error |
|
| 904 | + * @throws InvalidArgumentException |
|
| 905 | + * @throws ReflectionException |
|
| 906 | + * @throws InvalidDataTypeException |
|
| 907 | + * @throws InvalidInterfaceException |
|
| 908 | + */ |
|
| 909 | + public function increaseReserved($qty = 1, $source = 'unknown') |
|
| 910 | + { |
|
| 911 | + $qty = absint($qty); |
|
| 912 | + do_action( |
|
| 913 | + 'AHEE__EE_Ticket__increase_reserved__begin', |
|
| 914 | + $this, |
|
| 915 | + $qty, |
|
| 916 | + $source |
|
| 917 | + ); |
|
| 918 | + $this->add_extra_meta(EE_Ticket::META_KEY_TICKET_RESERVATIONS, "{$qty} from {$source}"); |
|
| 919 | + $success = false; |
|
| 920 | + $datetimes_adjusted_successfully = $this->increaseReservedForDatetimes($qty); |
|
| 921 | + if ($datetimes_adjusted_successfully) { |
|
| 922 | + $success = $this->incrementFieldConditionallyInDb( |
|
| 923 | + 'TKT_reserved', |
|
| 924 | + 'TKT_sold', |
|
| 925 | + 'TKT_qty', |
|
| 926 | + $qty |
|
| 927 | + ); |
|
| 928 | + if (! $success) { |
|
| 929 | + // The datetimes were successfully bumped, but not the |
|
| 930 | + // ticket. So we need to manually rollback the datetimes. |
|
| 931 | + $this->decreaseReservedForDatetimes($qty); |
|
| 932 | + } |
|
| 933 | + } |
|
| 934 | + do_action( |
|
| 935 | + 'AHEE__EE_Ticket__increase_reserved', |
|
| 936 | + $this, |
|
| 937 | + $qty, |
|
| 938 | + $this->reserved(), |
|
| 939 | + $success |
|
| 940 | + ); |
|
| 941 | + return $success; |
|
| 942 | + } |
|
| 943 | + |
|
| 944 | + |
|
| 945 | + /** |
|
| 946 | + * Increases reserved counts on related datetimes |
|
| 947 | + * |
|
| 948 | + * @since 4.9.80.p |
|
| 949 | + * @param int $qty |
|
| 950 | + * @param EE_Datetime[] $datetimes |
|
| 951 | + * @return boolean indicating success |
|
| 952 | + * @throws EE_Error |
|
| 953 | + * @throws InvalidArgumentException |
|
| 954 | + * @throws InvalidDataTypeException |
|
| 955 | + * @throws InvalidInterfaceException |
|
| 956 | + * @throws ReflectionException |
|
| 957 | + */ |
|
| 958 | + protected function increaseReservedForDatetimes($qty = 1, array $datetimes = []) |
|
| 959 | + { |
|
| 960 | + $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes(); |
|
| 961 | + $datetimes_updated = []; |
|
| 962 | + $limit_exceeded = false; |
|
| 963 | + if (is_array($datetimes)) { |
|
| 964 | + foreach ($datetimes as $datetime) { |
|
| 965 | + if ($datetime instanceof EE_Datetime) { |
|
| 966 | + if ($datetime->increaseReserved($qty)) { |
|
| 967 | + $datetimes_updated[] = $datetime; |
|
| 968 | + } else { |
|
| 969 | + $limit_exceeded = true; |
|
| 970 | + break; |
|
| 971 | + } |
|
| 972 | + } |
|
| 973 | + } |
|
| 974 | + // If somewhere along the way we detected a datetime whose |
|
| 975 | + // limit was exceeded, do a manual rollback. |
|
| 976 | + if ($limit_exceeded) { |
|
| 977 | + $this->decreaseReservedForDatetimes($qty, $datetimes_updated); |
|
| 978 | + return false; |
|
| 979 | + } |
|
| 980 | + } |
|
| 981 | + return true; |
|
| 982 | + } |
|
| 983 | + |
|
| 984 | + |
|
| 985 | + /** |
|
| 986 | + * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 987 | + * |
|
| 988 | + * @since 4.9.80.p |
|
| 989 | + * @param int $qty |
|
| 990 | + * @param bool $adjust_datetimes |
|
| 991 | + * @param string $source |
|
| 992 | + * @return boolean |
|
| 993 | + * @throws EE_Error |
|
| 994 | + * @throws InvalidArgumentException |
|
| 995 | + * @throws ReflectionException |
|
| 996 | + * @throws InvalidDataTypeException |
|
| 997 | + * @throws InvalidInterfaceException |
|
| 998 | + */ |
|
| 999 | + public function decreaseReserved($qty = 1, $adjust_datetimes = true, $source = 'unknown') |
|
| 1000 | + { |
|
| 1001 | + $qty = absint($qty); |
|
| 1002 | + $this->add_extra_meta(EE_Ticket::META_KEY_TICKET_RESERVATIONS, "-{$qty} from {$source}"); |
|
| 1003 | + if ($adjust_datetimes) { |
|
| 1004 | + $this->decreaseReservedForDatetimes($qty); |
|
| 1005 | + } |
|
| 1006 | + $success = $this->adjustNumericFieldsInDb( |
|
| 1007 | + [ |
|
| 1008 | + 'TKT_reserved' => $qty * -1 |
|
| 1009 | + ] |
|
| 1010 | + ); |
|
| 1011 | + do_action( |
|
| 1012 | + 'AHEE__EE_Ticket__decrease_reserved', |
|
| 1013 | + $this, |
|
| 1014 | + $qty, |
|
| 1015 | + $this->reserved(), |
|
| 1016 | + $success |
|
| 1017 | + ); |
|
| 1018 | + return $success; |
|
| 1019 | + } |
|
| 1020 | + |
|
| 1021 | + |
|
| 1022 | + /** |
|
| 1023 | + * Decreases the reserved count on the specified datetimes. |
|
| 1024 | + * |
|
| 1025 | + * @since 4.9.80.p |
|
| 1026 | + * @param int $qty |
|
| 1027 | + * @param EE_Datetime[] $datetimes |
|
| 1028 | + * @throws EE_Error |
|
| 1029 | + * @throws InvalidArgumentException |
|
| 1030 | + * @throws ReflectionException |
|
| 1031 | + * @throws InvalidDataTypeException |
|
| 1032 | + * @throws InvalidInterfaceException |
|
| 1033 | + */ |
|
| 1034 | + protected function decreaseReservedForDatetimes($qty = 1, array $datetimes = []) |
|
| 1035 | + { |
|
| 1036 | + $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes(); |
|
| 1037 | + foreach ($datetimes as $datetime) { |
|
| 1038 | + if ($datetime instanceof EE_Datetime) { |
|
| 1039 | + $datetime->decreaseReserved($qty); |
|
| 1040 | + } |
|
| 1041 | + } |
|
| 1042 | + } |
|
| 1043 | + |
|
| 1044 | + |
|
| 1045 | + /** |
|
| 1046 | + * Gets ticket quantity |
|
| 1047 | + * |
|
| 1048 | + * @param string $context ticket quantity is somewhat subjective depending on the exact information sought |
|
| 1049 | + * therefore $context can be one of three values: '', 'reg_limit', or 'saleable' |
|
| 1050 | + * '' (default) quantity is the actual db value for TKT_qty, unaffected by other objects |
|
| 1051 | + * REG LIMIT: caps qty based on DTT_reg_limit for ALL related datetimes |
|
| 1052 | + * SALEABLE: also considers datetime sold and returns zero if ANY DTT is sold out, and |
|
| 1053 | + * is therefore the truest measure of tickets that can be purchased at the moment |
|
| 1054 | + * @return int |
|
| 1055 | + * @throws EE_Error |
|
| 1056 | + */ |
|
| 1057 | + public function qty($context = '') |
|
| 1058 | + { |
|
| 1059 | + switch ($context) { |
|
| 1060 | + case 'reg_limit': |
|
| 1061 | + return $this->real_quantity_on_ticket(); |
|
| 1062 | + case 'saleable': |
|
| 1063 | + return $this->real_quantity_on_ticket('saleable'); |
|
| 1064 | + default: |
|
| 1065 | + return $this->get_raw('TKT_qty'); |
|
| 1066 | + } |
|
| 1067 | + } |
|
| 1068 | + |
|
| 1069 | + |
|
| 1070 | + /** |
|
| 1071 | + * Gets ticket quantity |
|
| 1072 | + * |
|
| 1073 | + * @param string $context ticket quantity is somewhat subjective depending on the exact information sought |
|
| 1074 | + * therefore $context can be one of two values: 'reg_limit', or 'saleable' |
|
| 1075 | + * REG LIMIT: caps qty based on DTT_reg_limit for ALL related datetimes |
|
| 1076 | + * SALEABLE: also considers datetime sold and returns zero if ANY DTT is sold out, and |
|
| 1077 | + * is therefore the truest measure of tickets that can be purchased at the moment |
|
| 1078 | + * @param int $DTT_ID the primary key for a particular datetime. |
|
| 1079 | + * set to 0 for all related datetimes |
|
| 1080 | + * @return int |
|
| 1081 | + * @throws EE_Error |
|
| 1082 | + */ |
|
| 1083 | + public function real_quantity_on_ticket($context = 'reg_limit', $DTT_ID = 0) |
|
| 1084 | + { |
|
| 1085 | + $raw = $this->get_raw('TKT_qty'); |
|
| 1086 | + // return immediately if it's zero |
|
| 1087 | + if ($raw === 0) { |
|
| 1088 | + return $raw; |
|
| 1089 | + } |
|
| 1090 | + // echo "\n\n<br />Ticket: " . $this->name() . '<br />'; |
|
| 1091 | + // ensure qty doesn't exceed raw value for THIS ticket |
|
| 1092 | + $qty = min(EE_INF, $raw); |
|
| 1093 | + // echo "\n . qty: " . $qty . '<br />'; |
|
| 1094 | + // calculate this ticket's total sales and reservations |
|
| 1095 | + $sold_and_reserved_for_this_ticket = $this->sold() + $this->reserved(); |
|
| 1096 | + // echo "\n . sold: " . $this->sold() . '<br />'; |
|
| 1097 | + // echo "\n . reserved: " . $this->reserved() . '<br />'; |
|
| 1098 | + // echo "\n . sold_and_reserved_for_this_ticket: " . $sold_and_reserved_for_this_ticket . '<br />'; |
|
| 1099 | + // first we need to calculate the maximum number of tickets available for the datetime |
|
| 1100 | + // do we want data for one datetime or all of them ? |
|
| 1101 | + $query_params = $DTT_ID ? array(array('DTT_ID' => $DTT_ID)) : array(); |
|
| 1102 | + $datetimes = $this->datetimes($query_params); |
|
| 1103 | + if (is_array($datetimes) && ! empty($datetimes)) { |
|
| 1104 | + foreach ($datetimes as $datetime) { |
|
| 1105 | + if ($datetime instanceof EE_Datetime) { |
|
| 1106 | + $datetime->refresh_from_db(); |
|
| 1107 | + // echo "\n . . datetime name: " . $datetime->name() . '<br />'; |
|
| 1108 | + // echo "\n . . datetime ID: " . $datetime->ID() . '<br />'; |
|
| 1109 | + // initialize with no restrictions for each datetime |
|
| 1110 | + // but adjust datetime qty based on datetime reg limit |
|
| 1111 | + $datetime_qty = min(EE_INF, $datetime->reg_limit()); |
|
| 1112 | + // echo "\n . . . datetime reg_limit: " . $datetime->reg_limit() . '<br />'; |
|
| 1113 | + // echo "\n . . . datetime_qty: " . $datetime_qty . '<br />'; |
|
| 1114 | + // if we want the actual saleable amount, then we need to consider OTHER ticket sales |
|
| 1115 | + // and reservations for this datetime, that do NOT include sales and reservations |
|
| 1116 | + // for this ticket (so we add $this->sold() and $this->reserved() back in) |
|
| 1117 | + if ($context === 'saleable') { |
|
| 1118 | + $datetime_qty = max( |
|
| 1119 | + $datetime_qty - $datetime->sold_and_reserved() + $sold_and_reserved_for_this_ticket, |
|
| 1120 | + 0 |
|
| 1121 | + ); |
|
| 1122 | + // echo "\n . . . datetime sold: " . $datetime->sold() . '<br />'; |
|
| 1123 | + // echo "\n . . . datetime reserved: " . $datetime->reserved() . '<br />'; |
|
| 1124 | + // echo "\n . . . datetime sold_and_reserved: " . $datetime->sold_and_reserved() . '<br />'; |
|
| 1125 | + // echo "\n . . . datetime_qty: " . $datetime_qty . '<br />'; |
|
| 1126 | + $datetime_qty = ! $datetime->sold_out() ? $datetime_qty : 0; |
|
| 1127 | + // echo "\n . . . datetime_qty: " . $datetime_qty . '<br />'; |
|
| 1128 | + } |
|
| 1129 | + $qty = min($datetime_qty, $qty); |
|
| 1130 | + // echo "\n . . qty: " . $qty . '<br />'; |
|
| 1131 | + } |
|
| 1132 | + } |
|
| 1133 | + } |
|
| 1134 | + // NOW that we know the maximum number of tickets available for the datetime |
|
| 1135 | + // we can finally factor in the details for this specific ticket |
|
| 1136 | + if ($qty > 0 && $context === 'saleable') { |
|
| 1137 | + // and subtract the sales for THIS ticket |
|
| 1138 | + $qty = max($qty - $sold_and_reserved_for_this_ticket, 0); |
|
| 1139 | + // echo "\n . qty: " . $qty . '<br />'; |
|
| 1140 | + } |
|
| 1141 | + // echo "\nFINAL QTY: " . $qty . "<br /><br />"; |
|
| 1142 | + return $qty; |
|
| 1143 | + } |
|
| 1144 | + |
|
| 1145 | + |
|
| 1146 | + /** |
|
| 1147 | + * Sets qty - IMPORTANT!!! Does NOT allow QTY to be set higher than the lowest reg limit of any related datetimes |
|
| 1148 | + * |
|
| 1149 | + * @param int $qty |
|
| 1150 | + * @return void |
|
| 1151 | + * @throws EE_Error |
|
| 1152 | + */ |
|
| 1153 | + public function set_qty($qty) |
|
| 1154 | + { |
|
| 1155 | + $datetimes = $this->datetimes(); |
|
| 1156 | + foreach ($datetimes as $datetime) { |
|
| 1157 | + if ($datetime instanceof EE_Datetime) { |
|
| 1158 | + $qty = min($qty, $datetime->reg_limit()); |
|
| 1159 | + } |
|
| 1160 | + } |
|
| 1161 | + $this->set('TKT_qty', $qty); |
|
| 1162 | + } |
|
| 1163 | + |
|
| 1164 | + |
|
| 1165 | + /** |
|
| 1166 | + * Gets uses |
|
| 1167 | + * |
|
| 1168 | + * @return int |
|
| 1169 | + * @throws EE_Error |
|
| 1170 | + */ |
|
| 1171 | + public function uses() |
|
| 1172 | + { |
|
| 1173 | + return $this->get('TKT_uses'); |
|
| 1174 | + } |
|
| 1175 | + |
|
| 1176 | + |
|
| 1177 | + /** |
|
| 1178 | + * Sets uses |
|
| 1179 | + * |
|
| 1180 | + * @param int $uses |
|
| 1181 | + * @return void |
|
| 1182 | + * @throws EE_Error |
|
| 1183 | + */ |
|
| 1184 | + public function set_uses($uses) |
|
| 1185 | + { |
|
| 1186 | + $this->set('TKT_uses', $uses); |
|
| 1187 | + } |
|
| 1188 | + |
|
| 1189 | + |
|
| 1190 | + /** |
|
| 1191 | + * returns whether ticket is required or not. |
|
| 1192 | + * |
|
| 1193 | + * @return boolean |
|
| 1194 | + * @throws EE_Error |
|
| 1195 | + */ |
|
| 1196 | + public function required() |
|
| 1197 | + { |
|
| 1198 | + return $this->get('TKT_required'); |
|
| 1199 | + } |
|
| 1200 | + |
|
| 1201 | + |
|
| 1202 | + /** |
|
| 1203 | + * sets the TKT_required property |
|
| 1204 | + * |
|
| 1205 | + * @param boolean $required |
|
| 1206 | + * @return void |
|
| 1207 | + * @throws EE_Error |
|
| 1208 | + */ |
|
| 1209 | + public function set_required($required) |
|
| 1210 | + { |
|
| 1211 | + $this->set('TKT_required', $required); |
|
| 1212 | + } |
|
| 1213 | + |
|
| 1214 | + |
|
| 1215 | + /** |
|
| 1216 | + * Gets taxable |
|
| 1217 | + * |
|
| 1218 | + * @return boolean |
|
| 1219 | + * @throws EE_Error |
|
| 1220 | + */ |
|
| 1221 | + public function taxable() |
|
| 1222 | + { |
|
| 1223 | + return $this->get('TKT_taxable'); |
|
| 1224 | + } |
|
| 1225 | + |
|
| 1226 | + |
|
| 1227 | + /** |
|
| 1228 | + * Sets taxable |
|
| 1229 | + * |
|
| 1230 | + * @param boolean $taxable |
|
| 1231 | + * @return void |
|
| 1232 | + * @throws EE_Error |
|
| 1233 | + */ |
|
| 1234 | + public function set_taxable($taxable) |
|
| 1235 | + { |
|
| 1236 | + $this->set('TKT_taxable', $taxable); |
|
| 1237 | + } |
|
| 1238 | + |
|
| 1239 | + |
|
| 1240 | + /** |
|
| 1241 | + * Gets is_default |
|
| 1242 | + * |
|
| 1243 | + * @return boolean |
|
| 1244 | + * @throws EE_Error |
|
| 1245 | + */ |
|
| 1246 | + public function is_default() |
|
| 1247 | + { |
|
| 1248 | + return $this->get('TKT_is_default'); |
|
| 1249 | + } |
|
| 1250 | + |
|
| 1251 | + |
|
| 1252 | + /** |
|
| 1253 | + * Sets is_default |
|
| 1254 | + * |
|
| 1255 | + * @param boolean $is_default |
|
| 1256 | + * @return void |
|
| 1257 | + * @throws EE_Error |
|
| 1258 | + */ |
|
| 1259 | + public function set_is_default($is_default) |
|
| 1260 | + { |
|
| 1261 | + $this->set('TKT_is_default', $is_default); |
|
| 1262 | + } |
|
| 1263 | + |
|
| 1264 | + |
|
| 1265 | + /** |
|
| 1266 | + * Gets order |
|
| 1267 | + * |
|
| 1268 | + * @return int |
|
| 1269 | + * @throws EE_Error |
|
| 1270 | + */ |
|
| 1271 | + public function order() |
|
| 1272 | + { |
|
| 1273 | + return $this->get('TKT_order'); |
|
| 1274 | + } |
|
| 1275 | + |
|
| 1276 | + |
|
| 1277 | + /** |
|
| 1278 | + * Sets order |
|
| 1279 | + * |
|
| 1280 | + * @param int $order |
|
| 1281 | + * @return void |
|
| 1282 | + * @throws EE_Error |
|
| 1283 | + */ |
|
| 1284 | + public function set_order($order) |
|
| 1285 | + { |
|
| 1286 | + $this->set('TKT_order', $order); |
|
| 1287 | + } |
|
| 1288 | + |
|
| 1289 | + |
|
| 1290 | + /** |
|
| 1291 | + * Gets row |
|
| 1292 | + * |
|
| 1293 | + * @return int |
|
| 1294 | + * @throws EE_Error |
|
| 1295 | + */ |
|
| 1296 | + public function row() |
|
| 1297 | + { |
|
| 1298 | + return $this->get('TKT_row'); |
|
| 1299 | + } |
|
| 1300 | + |
|
| 1301 | + |
|
| 1302 | + /** |
|
| 1303 | + * Sets row |
|
| 1304 | + * |
|
| 1305 | + * @param int $row |
|
| 1306 | + * @return void |
|
| 1307 | + * @throws EE_Error |
|
| 1308 | + */ |
|
| 1309 | + public function set_row($row) |
|
| 1310 | + { |
|
| 1311 | + $this->set('TKT_row', $row); |
|
| 1312 | + } |
|
| 1313 | + |
|
| 1314 | + |
|
| 1315 | + /** |
|
| 1316 | + * Gets deleted |
|
| 1317 | + * |
|
| 1318 | + * @return boolean |
|
| 1319 | + * @throws EE_Error |
|
| 1320 | + */ |
|
| 1321 | + public function deleted() |
|
| 1322 | + { |
|
| 1323 | + return $this->get('TKT_deleted'); |
|
| 1324 | + } |
|
| 1325 | + |
|
| 1326 | + |
|
| 1327 | + /** |
|
| 1328 | + * Sets deleted |
|
| 1329 | + * |
|
| 1330 | + * @param boolean $deleted |
|
| 1331 | + * @return void |
|
| 1332 | + * @throws EE_Error |
|
| 1333 | + */ |
|
| 1334 | + public function set_deleted($deleted) |
|
| 1335 | + { |
|
| 1336 | + $this->set('TKT_deleted', $deleted); |
|
| 1337 | + } |
|
| 1338 | + |
|
| 1339 | + |
|
| 1340 | + /** |
|
| 1341 | + * Gets parent |
|
| 1342 | + * |
|
| 1343 | + * @return int |
|
| 1344 | + * @throws EE_Error |
|
| 1345 | + */ |
|
| 1346 | + public function parent_ID() |
|
| 1347 | + { |
|
| 1348 | + return $this->get('TKT_parent'); |
|
| 1349 | + } |
|
| 1350 | + |
|
| 1351 | + |
|
| 1352 | + /** |
|
| 1353 | + * Sets parent |
|
| 1354 | + * |
|
| 1355 | + * @param int $parent |
|
| 1356 | + * @return void |
|
| 1357 | + * @throws EE_Error |
|
| 1358 | + */ |
|
| 1359 | + public function set_parent_ID($parent) |
|
| 1360 | + { |
|
| 1361 | + $this->set('TKT_parent', $parent); |
|
| 1362 | + } |
|
| 1363 | + |
|
| 1364 | + |
|
| 1365 | + /** |
|
| 1366 | + * Gets a string which is handy for showing in gateways etc that describes the ticket. |
|
| 1367 | + * |
|
| 1368 | + * @return string |
|
| 1369 | + * @throws EE_Error |
|
| 1370 | + */ |
|
| 1371 | + public function name_and_info() |
|
| 1372 | + { |
|
| 1373 | + $times = array(); |
|
| 1374 | + foreach ($this->datetimes() as $datetime) { |
|
| 1375 | + $times[] = $datetime->start_date_and_time(); |
|
| 1376 | + } |
|
| 1377 | + return $this->name() . ' @ ' . implode(', ', $times) . ' for ' . $this->pretty_price(); |
|
| 1378 | + } |
|
| 1379 | + |
|
| 1380 | + |
|
| 1381 | + /** |
|
| 1382 | + * Gets name |
|
| 1383 | + * |
|
| 1384 | + * @return string |
|
| 1385 | + * @throws EE_Error |
|
| 1386 | + */ |
|
| 1387 | + public function name() |
|
| 1388 | + { |
|
| 1389 | + return $this->get('TKT_name'); |
|
| 1390 | + } |
|
| 1391 | + |
|
| 1392 | + |
|
| 1393 | + /** |
|
| 1394 | + * Gets price |
|
| 1395 | + * |
|
| 1396 | + * @return float |
|
| 1397 | + * @throws EE_Error |
|
| 1398 | + */ |
|
| 1399 | + public function price() |
|
| 1400 | + { |
|
| 1401 | + return $this->get('TKT_price'); |
|
| 1402 | + } |
|
| 1403 | + |
|
| 1404 | + |
|
| 1405 | + /** |
|
| 1406 | + * Gets all the registrations for this ticket |
|
| 1407 | + * |
|
| 1408 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1409 | + * @return EE_Registration[]|EE_Base_Class[] |
|
| 1410 | + * @throws EE_Error |
|
| 1411 | + */ |
|
| 1412 | + public function registrations($query_params = array()) |
|
| 1413 | + { |
|
| 1414 | + return $this->get_many_related('Registration', $query_params); |
|
| 1415 | + } |
|
| 1416 | + |
|
| 1417 | + |
|
| 1418 | + /** |
|
| 1419 | + * Updates the TKT_sold attribute (and saves) based on the number of APPROVED registrations for this ticket. |
|
| 1420 | + * |
|
| 1421 | + * @return int |
|
| 1422 | + * @throws EE_Error |
|
| 1423 | + */ |
|
| 1424 | + public function update_tickets_sold() |
|
| 1425 | + { |
|
| 1426 | + $count_regs_for_this_ticket = $this->count_registrations( |
|
| 1427 | + array( |
|
| 1428 | + array( |
|
| 1429 | + 'STS_ID' => EEM_Registration::status_id_approved, |
|
| 1430 | + 'REG_deleted' => 0, |
|
| 1431 | + ), |
|
| 1432 | + ) |
|
| 1433 | + ); |
|
| 1434 | + $this->set_sold($count_regs_for_this_ticket); |
|
| 1435 | + $this->save(); |
|
| 1436 | + return $count_regs_for_this_ticket; |
|
| 1437 | + } |
|
| 1438 | + |
|
| 1439 | + |
|
| 1440 | + /** |
|
| 1441 | + * Counts the registrations for this ticket |
|
| 1442 | + * |
|
| 1443 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1444 | + * @return int |
|
| 1445 | + */ |
|
| 1446 | + public function count_registrations($query_params = array()) |
|
| 1447 | + { |
|
| 1448 | + return $this->count_related('Registration', $query_params); |
|
| 1449 | + } |
|
| 1450 | + |
|
| 1451 | + |
|
| 1452 | + /** |
|
| 1453 | + * Implementation for EEI_Has_Icon interface method. |
|
| 1454 | + * |
|
| 1455 | + * @see EEI_Visual_Representation for comments |
|
| 1456 | + * @return string |
|
| 1457 | + */ |
|
| 1458 | + public function get_icon() |
|
| 1459 | + { |
|
| 1460 | + return '<span class="dashicons dashicons-tickets-alt"></span>'; |
|
| 1461 | + } |
|
| 1462 | + |
|
| 1463 | + |
|
| 1464 | + /** |
|
| 1465 | + * Implementation of the EEI_Event_Relation interface method |
|
| 1466 | + * |
|
| 1467 | + * @see EEI_Event_Relation for comments |
|
| 1468 | + * @return EE_Event |
|
| 1469 | + * @throws EE_Error |
|
| 1470 | + * @throws UnexpectedEntityException |
|
| 1471 | + */ |
|
| 1472 | + public function get_related_event() |
|
| 1473 | + { |
|
| 1474 | + // get one datetime to use for getting the event |
|
| 1475 | + $datetime = $this->first_datetime(); |
|
| 1476 | + if (! $datetime instanceof \EE_Datetime) { |
|
| 1477 | + throw new UnexpectedEntityException( |
|
| 1478 | + $datetime, |
|
| 1479 | + 'EE_Datetime', |
|
| 1480 | + sprintf( |
|
| 1481 | + __('The ticket (%s) is not associated with any valid datetimes.', 'event_espresso'), |
|
| 1482 | + $this->name() |
|
| 1483 | + ) |
|
| 1484 | + ); |
|
| 1485 | + } |
|
| 1486 | + $event = $datetime->event(); |
|
| 1487 | + if (! $event instanceof \EE_Event) { |
|
| 1488 | + throw new UnexpectedEntityException( |
|
| 1489 | + $event, |
|
| 1490 | + 'EE_Event', |
|
| 1491 | + sprintf( |
|
| 1492 | + __('The ticket (%s) is not associated with a valid event.', 'event_espresso'), |
|
| 1493 | + $this->name() |
|
| 1494 | + ) |
|
| 1495 | + ); |
|
| 1496 | + } |
|
| 1497 | + return $event; |
|
| 1498 | + } |
|
| 1499 | + |
|
| 1500 | + |
|
| 1501 | + /** |
|
| 1502 | + * Implementation of the EEI_Event_Relation interface method |
|
| 1503 | + * |
|
| 1504 | + * @see EEI_Event_Relation for comments |
|
| 1505 | + * @return string |
|
| 1506 | + * @throws UnexpectedEntityException |
|
| 1507 | + * @throws EE_Error |
|
| 1508 | + */ |
|
| 1509 | + public function get_event_name() |
|
| 1510 | + { |
|
| 1511 | + $event = $this->get_related_event(); |
|
| 1512 | + return $event instanceof EE_Event ? $event->name() : ''; |
|
| 1513 | + } |
|
| 1514 | + |
|
| 1515 | + |
|
| 1516 | + /** |
|
| 1517 | + * Implementation of the EEI_Event_Relation interface method |
|
| 1518 | + * |
|
| 1519 | + * @see EEI_Event_Relation for comments |
|
| 1520 | + * @return int |
|
| 1521 | + * @throws UnexpectedEntityException |
|
| 1522 | + * @throws EE_Error |
|
| 1523 | + */ |
|
| 1524 | + public function get_event_ID() |
|
| 1525 | + { |
|
| 1526 | + $event = $this->get_related_event(); |
|
| 1527 | + return $event instanceof EE_Event ? $event->ID() : 0; |
|
| 1528 | + } |
|
| 1529 | + |
|
| 1530 | + |
|
| 1531 | + /** |
|
| 1532 | + * This simply returns whether a ticket can be permanently deleted or not. |
|
| 1533 | + * The criteria for determining this is whether the ticket has any related registrations. |
|
| 1534 | + * If there are none then it can be permanently deleted. |
|
| 1535 | + * |
|
| 1536 | + * @return bool |
|
| 1537 | + */ |
|
| 1538 | + public function is_permanently_deleteable() |
|
| 1539 | + { |
|
| 1540 | + return $this->count_registrations() === 0; |
|
| 1541 | + } |
|
| 1542 | + |
|
| 1543 | + |
|
| 1544 | + /******************************************************************* |
|
| 1545 | 1545 | *********************** DEPRECATED METHODS ********************** |
| 1546 | 1546 | *******************************************************************/ |
| 1547 | 1547 | |
| 1548 | 1548 | |
| 1549 | - /** |
|
| 1550 | - * Increments sold by amount passed by $qty AND decrements the reserved count on both this ticket and its |
|
| 1551 | - * associated datetimes. |
|
| 1552 | - * |
|
| 1553 | - * @deprecated 4.9.80.p |
|
| 1554 | - * @param int $qty |
|
| 1555 | - * @return void |
|
| 1556 | - * @throws EE_Error |
|
| 1557 | - * @throws InvalidArgumentException |
|
| 1558 | - * @throws InvalidDataTypeException |
|
| 1559 | - * @throws InvalidInterfaceException |
|
| 1560 | - * @throws ReflectionException |
|
| 1561 | - */ |
|
| 1562 | - public function increase_sold($qty = 1) |
|
| 1563 | - { |
|
| 1564 | - EE_Error::doing_it_wrong( |
|
| 1565 | - __FUNCTION__, |
|
| 1566 | - esc_html__('Please use EE_Ticket::increaseSold() instead', 'event_espresso'), |
|
| 1567 | - '4.9.80.p', |
|
| 1568 | - '5.0.0.p' |
|
| 1569 | - ); |
|
| 1570 | - $this->increaseSold($qty); |
|
| 1571 | - } |
|
| 1572 | - |
|
| 1573 | - |
|
| 1574 | - /** |
|
| 1575 | - * On each datetime related to this ticket, increases its sold count and decreases its reserved count by $qty. |
|
| 1576 | - * |
|
| 1577 | - * @deprecated 4.9.80.p |
|
| 1578 | - * @param int $qty positive or negative. Positive means to increase sold counts (and decrease reserved counts), |
|
| 1579 | - * Negative means to decreases old counts (and increase reserved counts). |
|
| 1580 | - * @throws EE_Error |
|
| 1581 | - * @throws InvalidArgumentException |
|
| 1582 | - * @throws InvalidDataTypeException |
|
| 1583 | - * @throws InvalidInterfaceException |
|
| 1584 | - * @throws ReflectionException |
|
| 1585 | - */ |
|
| 1586 | - protected function _increase_sold_for_datetimes($qty) |
|
| 1587 | - { |
|
| 1588 | - EE_Error::doing_it_wrong( |
|
| 1589 | - __FUNCTION__, |
|
| 1590 | - esc_html__('Please use EE_Ticket::increaseSoldForDatetimes() instead', 'event_espresso'), |
|
| 1591 | - '4.9.80.p', |
|
| 1592 | - '5.0.0.p' |
|
| 1593 | - ); |
|
| 1594 | - $this->increaseSoldForDatetimes($qty); |
|
| 1595 | - } |
|
| 1596 | - |
|
| 1597 | - |
|
| 1598 | - /** |
|
| 1599 | - * Decrements (subtracts) sold by amount passed by $qty on both the ticket and its related datetimes directly in the |
|
| 1600 | - * DB and then updates the model objects. |
|
| 1601 | - * Does not affect the reserved counts. |
|
| 1602 | - * |
|
| 1603 | - * @deprecated 4.9.80.p |
|
| 1604 | - * @param int $qty |
|
| 1605 | - * @return void |
|
| 1606 | - * @throws EE_Error |
|
| 1607 | - * @throws InvalidArgumentException |
|
| 1608 | - * @throws InvalidDataTypeException |
|
| 1609 | - * @throws InvalidInterfaceException |
|
| 1610 | - * @throws ReflectionException |
|
| 1611 | - */ |
|
| 1612 | - public function decrease_sold($qty = 1) |
|
| 1613 | - { |
|
| 1614 | - EE_Error::doing_it_wrong( |
|
| 1615 | - __FUNCTION__, |
|
| 1616 | - esc_html__('Please use EE_Ticket::decreaseSold() instead', 'event_espresso'), |
|
| 1617 | - '4.9.80.p', |
|
| 1618 | - '5.0.0.p' |
|
| 1619 | - ); |
|
| 1620 | - $this->decreaseSold($qty); |
|
| 1621 | - } |
|
| 1622 | - |
|
| 1623 | - |
|
| 1624 | - /** |
|
| 1625 | - * Decreases sold on related datetimes |
|
| 1626 | - * |
|
| 1627 | - * @deprecated 4.9.80.p |
|
| 1628 | - * @param int $qty |
|
| 1629 | - * @return void |
|
| 1630 | - * @throws EE_Error |
|
| 1631 | - * @throws InvalidArgumentException |
|
| 1632 | - * @throws InvalidDataTypeException |
|
| 1633 | - * @throws InvalidInterfaceException |
|
| 1634 | - * @throws ReflectionException |
|
| 1635 | - */ |
|
| 1636 | - protected function _decrease_sold_for_datetimes($qty = 1) |
|
| 1637 | - { |
|
| 1638 | - EE_Error::doing_it_wrong( |
|
| 1639 | - __FUNCTION__, |
|
| 1640 | - esc_html__('Please use EE_Ticket::decreaseSoldForDatetimes() instead', 'event_espresso'), |
|
| 1641 | - '4.9.80.p', |
|
| 1642 | - '5.0.0.p' |
|
| 1643 | - ); |
|
| 1644 | - $this->decreaseSoldForDatetimes($qty); |
|
| 1645 | - } |
|
| 1646 | - |
|
| 1647 | - |
|
| 1648 | - /** |
|
| 1649 | - * Increments reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 1650 | - * |
|
| 1651 | - * @deprecated 4.9.80.p |
|
| 1652 | - * @param int $qty |
|
| 1653 | - * @param string $source |
|
| 1654 | - * @return bool whether we successfully reserved the ticket or not. |
|
| 1655 | - * @throws EE_Error |
|
| 1656 | - * @throws InvalidArgumentException |
|
| 1657 | - * @throws ReflectionException |
|
| 1658 | - * @throws InvalidDataTypeException |
|
| 1659 | - * @throws InvalidInterfaceException |
|
| 1660 | - */ |
|
| 1661 | - public function increase_reserved($qty = 1, $source = 'unknown') |
|
| 1662 | - { |
|
| 1663 | - EE_Error::doing_it_wrong( |
|
| 1664 | - __FUNCTION__, |
|
| 1665 | - esc_html__('Please use EE_Ticket::increaseReserved() instead', 'event_espresso'), |
|
| 1666 | - '4.9.80.p', |
|
| 1667 | - '5.0.0.p' |
|
| 1668 | - ); |
|
| 1669 | - return $this->increaseReserved($qty); |
|
| 1670 | - } |
|
| 1671 | - |
|
| 1672 | - |
|
| 1673 | - /** |
|
| 1674 | - * Increases sold on related datetimes |
|
| 1675 | - * |
|
| 1676 | - * @deprecated 4.9.80.p |
|
| 1677 | - * @param int $qty |
|
| 1678 | - * @return boolean indicating success |
|
| 1679 | - * @throws EE_Error |
|
| 1680 | - * @throws InvalidArgumentException |
|
| 1681 | - * @throws InvalidDataTypeException |
|
| 1682 | - * @throws InvalidInterfaceException |
|
| 1683 | - * @throws ReflectionException |
|
| 1684 | - */ |
|
| 1685 | - protected function _increase_reserved_for_datetimes($qty = 1) |
|
| 1686 | - { |
|
| 1687 | - EE_Error::doing_it_wrong( |
|
| 1688 | - __FUNCTION__, |
|
| 1689 | - esc_html__('Please use EE_Ticket::increaseReservedForDatetimes() instead', 'event_espresso'), |
|
| 1690 | - '4.9.80.p', |
|
| 1691 | - '5.0.0.p' |
|
| 1692 | - ); |
|
| 1693 | - return $this->increaseReservedForDatetimes($qty); |
|
| 1694 | - } |
|
| 1695 | - |
|
| 1696 | - |
|
| 1697 | - /** |
|
| 1698 | - * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 1699 | - * |
|
| 1700 | - * @deprecated 4.9.80.p |
|
| 1701 | - * @param int $qty |
|
| 1702 | - * @param bool $adjust_datetimes |
|
| 1703 | - * @param string $source |
|
| 1704 | - * @return void |
|
| 1705 | - * @throws EE_Error |
|
| 1706 | - * @throws InvalidArgumentException |
|
| 1707 | - * @throws ReflectionException |
|
| 1708 | - * @throws InvalidDataTypeException |
|
| 1709 | - * @throws InvalidInterfaceException |
|
| 1710 | - */ |
|
| 1711 | - public function decrease_reserved($qty = 1, $adjust_datetimes = true, $source = 'unknown') |
|
| 1712 | - { |
|
| 1713 | - EE_Error::doing_it_wrong( |
|
| 1714 | - __FUNCTION__, |
|
| 1715 | - esc_html__('Please use EE_Ticket::decreaseReserved() instead', 'event_espresso'), |
|
| 1716 | - '4.9.80.p', |
|
| 1717 | - '5.0.0.p' |
|
| 1718 | - ); |
|
| 1719 | - $this->decreaseReserved($qty); |
|
| 1720 | - } |
|
| 1721 | - |
|
| 1722 | - |
|
| 1723 | - /** |
|
| 1724 | - * Decreases reserved on related datetimes |
|
| 1725 | - * |
|
| 1726 | - * @deprecated 4.9.80.p |
|
| 1727 | - * @param int $qty |
|
| 1728 | - * @return void |
|
| 1729 | - * @throws EE_Error |
|
| 1730 | - * @throws InvalidArgumentException |
|
| 1731 | - * @throws ReflectionException |
|
| 1732 | - * @throws InvalidDataTypeException |
|
| 1733 | - * @throws InvalidInterfaceException |
|
| 1734 | - */ |
|
| 1735 | - protected function _decrease_reserved_for_datetimes($qty = 1) |
|
| 1736 | - { |
|
| 1737 | - EE_Error::doing_it_wrong( |
|
| 1738 | - __FUNCTION__, |
|
| 1739 | - esc_html__('Please use EE_Ticket::decreaseReservedForDatetimes() instead', 'event_espresso'), |
|
| 1740 | - '4.9.80.p', |
|
| 1741 | - '5.0.0.p' |
|
| 1742 | - ); |
|
| 1743 | - $this->decreaseReservedForDatetimes($qty); |
|
| 1744 | - } |
|
| 1549 | + /** |
|
| 1550 | + * Increments sold by amount passed by $qty AND decrements the reserved count on both this ticket and its |
|
| 1551 | + * associated datetimes. |
|
| 1552 | + * |
|
| 1553 | + * @deprecated 4.9.80.p |
|
| 1554 | + * @param int $qty |
|
| 1555 | + * @return void |
|
| 1556 | + * @throws EE_Error |
|
| 1557 | + * @throws InvalidArgumentException |
|
| 1558 | + * @throws InvalidDataTypeException |
|
| 1559 | + * @throws InvalidInterfaceException |
|
| 1560 | + * @throws ReflectionException |
|
| 1561 | + */ |
|
| 1562 | + public function increase_sold($qty = 1) |
|
| 1563 | + { |
|
| 1564 | + EE_Error::doing_it_wrong( |
|
| 1565 | + __FUNCTION__, |
|
| 1566 | + esc_html__('Please use EE_Ticket::increaseSold() instead', 'event_espresso'), |
|
| 1567 | + '4.9.80.p', |
|
| 1568 | + '5.0.0.p' |
|
| 1569 | + ); |
|
| 1570 | + $this->increaseSold($qty); |
|
| 1571 | + } |
|
| 1572 | + |
|
| 1573 | + |
|
| 1574 | + /** |
|
| 1575 | + * On each datetime related to this ticket, increases its sold count and decreases its reserved count by $qty. |
|
| 1576 | + * |
|
| 1577 | + * @deprecated 4.9.80.p |
|
| 1578 | + * @param int $qty positive or negative. Positive means to increase sold counts (and decrease reserved counts), |
|
| 1579 | + * Negative means to decreases old counts (and increase reserved counts). |
|
| 1580 | + * @throws EE_Error |
|
| 1581 | + * @throws InvalidArgumentException |
|
| 1582 | + * @throws InvalidDataTypeException |
|
| 1583 | + * @throws InvalidInterfaceException |
|
| 1584 | + * @throws ReflectionException |
|
| 1585 | + */ |
|
| 1586 | + protected function _increase_sold_for_datetimes($qty) |
|
| 1587 | + { |
|
| 1588 | + EE_Error::doing_it_wrong( |
|
| 1589 | + __FUNCTION__, |
|
| 1590 | + esc_html__('Please use EE_Ticket::increaseSoldForDatetimes() instead', 'event_espresso'), |
|
| 1591 | + '4.9.80.p', |
|
| 1592 | + '5.0.0.p' |
|
| 1593 | + ); |
|
| 1594 | + $this->increaseSoldForDatetimes($qty); |
|
| 1595 | + } |
|
| 1596 | + |
|
| 1597 | + |
|
| 1598 | + /** |
|
| 1599 | + * Decrements (subtracts) sold by amount passed by $qty on both the ticket and its related datetimes directly in the |
|
| 1600 | + * DB and then updates the model objects. |
|
| 1601 | + * Does not affect the reserved counts. |
|
| 1602 | + * |
|
| 1603 | + * @deprecated 4.9.80.p |
|
| 1604 | + * @param int $qty |
|
| 1605 | + * @return void |
|
| 1606 | + * @throws EE_Error |
|
| 1607 | + * @throws InvalidArgumentException |
|
| 1608 | + * @throws InvalidDataTypeException |
|
| 1609 | + * @throws InvalidInterfaceException |
|
| 1610 | + * @throws ReflectionException |
|
| 1611 | + */ |
|
| 1612 | + public function decrease_sold($qty = 1) |
|
| 1613 | + { |
|
| 1614 | + EE_Error::doing_it_wrong( |
|
| 1615 | + __FUNCTION__, |
|
| 1616 | + esc_html__('Please use EE_Ticket::decreaseSold() instead', 'event_espresso'), |
|
| 1617 | + '4.9.80.p', |
|
| 1618 | + '5.0.0.p' |
|
| 1619 | + ); |
|
| 1620 | + $this->decreaseSold($qty); |
|
| 1621 | + } |
|
| 1622 | + |
|
| 1623 | + |
|
| 1624 | + /** |
|
| 1625 | + * Decreases sold on related datetimes |
|
| 1626 | + * |
|
| 1627 | + * @deprecated 4.9.80.p |
|
| 1628 | + * @param int $qty |
|
| 1629 | + * @return void |
|
| 1630 | + * @throws EE_Error |
|
| 1631 | + * @throws InvalidArgumentException |
|
| 1632 | + * @throws InvalidDataTypeException |
|
| 1633 | + * @throws InvalidInterfaceException |
|
| 1634 | + * @throws ReflectionException |
|
| 1635 | + */ |
|
| 1636 | + protected function _decrease_sold_for_datetimes($qty = 1) |
|
| 1637 | + { |
|
| 1638 | + EE_Error::doing_it_wrong( |
|
| 1639 | + __FUNCTION__, |
|
| 1640 | + esc_html__('Please use EE_Ticket::decreaseSoldForDatetimes() instead', 'event_espresso'), |
|
| 1641 | + '4.9.80.p', |
|
| 1642 | + '5.0.0.p' |
|
| 1643 | + ); |
|
| 1644 | + $this->decreaseSoldForDatetimes($qty); |
|
| 1645 | + } |
|
| 1646 | + |
|
| 1647 | + |
|
| 1648 | + /** |
|
| 1649 | + * Increments reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 1650 | + * |
|
| 1651 | + * @deprecated 4.9.80.p |
|
| 1652 | + * @param int $qty |
|
| 1653 | + * @param string $source |
|
| 1654 | + * @return bool whether we successfully reserved the ticket or not. |
|
| 1655 | + * @throws EE_Error |
|
| 1656 | + * @throws InvalidArgumentException |
|
| 1657 | + * @throws ReflectionException |
|
| 1658 | + * @throws InvalidDataTypeException |
|
| 1659 | + * @throws InvalidInterfaceException |
|
| 1660 | + */ |
|
| 1661 | + public function increase_reserved($qty = 1, $source = 'unknown') |
|
| 1662 | + { |
|
| 1663 | + EE_Error::doing_it_wrong( |
|
| 1664 | + __FUNCTION__, |
|
| 1665 | + esc_html__('Please use EE_Ticket::increaseReserved() instead', 'event_espresso'), |
|
| 1666 | + '4.9.80.p', |
|
| 1667 | + '5.0.0.p' |
|
| 1668 | + ); |
|
| 1669 | + return $this->increaseReserved($qty); |
|
| 1670 | + } |
|
| 1671 | + |
|
| 1672 | + |
|
| 1673 | + /** |
|
| 1674 | + * Increases sold on related datetimes |
|
| 1675 | + * |
|
| 1676 | + * @deprecated 4.9.80.p |
|
| 1677 | + * @param int $qty |
|
| 1678 | + * @return boolean indicating success |
|
| 1679 | + * @throws EE_Error |
|
| 1680 | + * @throws InvalidArgumentException |
|
| 1681 | + * @throws InvalidDataTypeException |
|
| 1682 | + * @throws InvalidInterfaceException |
|
| 1683 | + * @throws ReflectionException |
|
| 1684 | + */ |
|
| 1685 | + protected function _increase_reserved_for_datetimes($qty = 1) |
|
| 1686 | + { |
|
| 1687 | + EE_Error::doing_it_wrong( |
|
| 1688 | + __FUNCTION__, |
|
| 1689 | + esc_html__('Please use EE_Ticket::increaseReservedForDatetimes() instead', 'event_espresso'), |
|
| 1690 | + '4.9.80.p', |
|
| 1691 | + '5.0.0.p' |
|
| 1692 | + ); |
|
| 1693 | + return $this->increaseReservedForDatetimes($qty); |
|
| 1694 | + } |
|
| 1695 | + |
|
| 1696 | + |
|
| 1697 | + /** |
|
| 1698 | + * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 1699 | + * |
|
| 1700 | + * @deprecated 4.9.80.p |
|
| 1701 | + * @param int $qty |
|
| 1702 | + * @param bool $adjust_datetimes |
|
| 1703 | + * @param string $source |
|
| 1704 | + * @return void |
|
| 1705 | + * @throws EE_Error |
|
| 1706 | + * @throws InvalidArgumentException |
|
| 1707 | + * @throws ReflectionException |
|
| 1708 | + * @throws InvalidDataTypeException |
|
| 1709 | + * @throws InvalidInterfaceException |
|
| 1710 | + */ |
|
| 1711 | + public function decrease_reserved($qty = 1, $adjust_datetimes = true, $source = 'unknown') |
|
| 1712 | + { |
|
| 1713 | + EE_Error::doing_it_wrong( |
|
| 1714 | + __FUNCTION__, |
|
| 1715 | + esc_html__('Please use EE_Ticket::decreaseReserved() instead', 'event_espresso'), |
|
| 1716 | + '4.9.80.p', |
|
| 1717 | + '5.0.0.p' |
|
| 1718 | + ); |
|
| 1719 | + $this->decreaseReserved($qty); |
|
| 1720 | + } |
|
| 1721 | + |
|
| 1722 | + |
|
| 1723 | + /** |
|
| 1724 | + * Decreases reserved on related datetimes |
|
| 1725 | + * |
|
| 1726 | + * @deprecated 4.9.80.p |
|
| 1727 | + * @param int $qty |
|
| 1728 | + * @return void |
|
| 1729 | + * @throws EE_Error |
|
| 1730 | + * @throws InvalidArgumentException |
|
| 1731 | + * @throws ReflectionException |
|
| 1732 | + * @throws InvalidDataTypeException |
|
| 1733 | + * @throws InvalidInterfaceException |
|
| 1734 | + */ |
|
| 1735 | + protected function _decrease_reserved_for_datetimes($qty = 1) |
|
| 1736 | + { |
|
| 1737 | + EE_Error::doing_it_wrong( |
|
| 1738 | + __FUNCTION__, |
|
| 1739 | + esc_html__('Please use EE_Ticket::decreaseReservedForDatetimes() instead', 'event_espresso'), |
|
| 1740 | + '4.9.80.p', |
|
| 1741 | + '5.0.0.p' |
|
| 1742 | + ); |
|
| 1743 | + $this->decreaseReservedForDatetimes($qty); |
|
| 1744 | + } |
|
| 1745 | 1745 | } |
@@ -14,981 +14,981 @@ discard block |
||
| 14 | 14 | final class EE_Capabilities extends EE_Base |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * the name of the wp option used to store caps previously initialized |
|
| 19 | - */ |
|
| 20 | - const option_name = 'ee_caps_initialized'; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * instance of EE_Capabilities object |
|
| 24 | - * |
|
| 25 | - * @var EE_Capabilities |
|
| 26 | - */ |
|
| 27 | - private static $_instance; |
|
| 28 | - |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * This is a map of caps that correspond to a default WP_Role. |
|
| 32 | - * Array is indexed by Role and values are ee capabilities. |
|
| 33 | - * |
|
| 34 | - * @since 4.5.0 |
|
| 35 | - * |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - private $capabilities_map = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * This used to hold an array of EE_Meta_Capability_Map objects |
|
| 42 | - * that define the granular capabilities mapped to for a user depending on context. |
|
| 43 | - * |
|
| 44 | - * @var EE_Meta_Capability_Map[] |
|
| 45 | - */ |
|
| 46 | - private $_meta_caps = array(); |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The internal $capabilities_map needs to be initialized before it can be used. |
|
| 50 | - * This flag tracks whether that has happened or not. |
|
| 51 | - * But for this to work, we need three states to indicate: |
|
| 52 | - * initialization has not occurred at all |
|
| 53 | - * initialization has started but is not complete |
|
| 54 | - * initialization is complete |
|
| 55 | - * The reason this is needed is because the addCaps() method |
|
| 56 | - * normally requires the $capabilities_map to be initialized, |
|
| 57 | - * but is also used during the initialization process. |
|
| 58 | - * So: |
|
| 59 | - * If initialized === null, init_caps() will be called before any other methods will run. |
|
| 60 | - * If initialized === false, then init_caps() is in the process of running it's logic. |
|
| 61 | - * If initialized === true, then init_caps() has completed the initialization process. |
|
| 62 | - * |
|
| 63 | - * @var boolean|null $initialized |
|
| 64 | - */ |
|
| 65 | - private $initialized; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @var boolean $reset |
|
| 69 | - */ |
|
| 70 | - private $reset = false; |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * singleton method used to instantiate class object |
|
| 75 | - * |
|
| 76 | - * @since 4.5.0 |
|
| 77 | - * |
|
| 78 | - * @return EE_Capabilities |
|
| 79 | - */ |
|
| 80 | - public static function instance() |
|
| 81 | - { |
|
| 82 | - // check if instantiated, and if not do so. |
|
| 83 | - if (! self::$_instance instanceof EE_Capabilities) { |
|
| 84 | - self::$_instance = new self(); |
|
| 85 | - } |
|
| 86 | - return self::$_instance; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * private constructor |
|
| 92 | - * |
|
| 93 | - * @since 4.5.0 |
|
| 94 | - */ |
|
| 95 | - private function __construct() |
|
| 96 | - { |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * This delays the initialization of the capabilities class until EE_System core is loaded and ready. |
|
| 102 | - * |
|
| 103 | - * @param bool $reset allows for resetting the default capabilities saved on roles. Note that this doesn't |
|
| 104 | - * actually REMOVE any capabilities from existing roles, it just resaves defaults roles and |
|
| 105 | - * ensures that they are up to date. |
|
| 106 | - * |
|
| 107 | - * @since 4.5.0 |
|
| 108 | - * @return bool |
|
| 109 | - * @throws EE_Error |
|
| 110 | - */ |
|
| 111 | - public function init_caps($reset = false) |
|
| 112 | - { |
|
| 113 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 114 | - return false; |
|
| 115 | - } |
|
| 116 | - $this->reset = filter_var($reset, FILTER_VALIDATE_BOOLEAN); |
|
| 117 | - // if reset, then completely delete the cache option and clear the $capabilities_map property. |
|
| 118 | - if ($this->reset) { |
|
| 119 | - $this->initialized = null; |
|
| 120 | - $this->capabilities_map = array(); |
|
| 121 | - delete_option(self::option_name); |
|
| 122 | - } |
|
| 123 | - if ($this->initialized === null) { |
|
| 124 | - $this->initialized = false; |
|
| 125 | - do_action( |
|
| 126 | - 'AHEE__EE_Capabilities__init_caps__before_initialization', |
|
| 127 | - $this->reset |
|
| 128 | - ); |
|
| 129 | - $this->addCaps($this->_init_caps_map()); |
|
| 130 | - $this->_set_meta_caps(); |
|
| 131 | - do_action( |
|
| 132 | - 'AHEE__EE_Capabilities__init_caps__after_initialization', |
|
| 133 | - $this->capabilities_map |
|
| 134 | - ); |
|
| 135 | - $this->initialized = true; |
|
| 136 | - } |
|
| 137 | - // reset $this->reset so that it's not stuck on true if init_caps() gets called again |
|
| 138 | - $this->reset = false; |
|
| 139 | - return true; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * This sets the meta caps property. |
|
| 145 | - * |
|
| 146 | - * @since 4.5.0 |
|
| 147 | - * @return void |
|
| 148 | - * @throws EE_Error |
|
| 149 | - */ |
|
| 150 | - private function _set_meta_caps() |
|
| 151 | - { |
|
| 152 | - // get default meta caps and filter the returned array |
|
| 153 | - $this->_meta_caps = apply_filters( |
|
| 154 | - 'FHEE__EE_Capabilities___set_meta_caps__meta_caps', |
|
| 155 | - $this->_get_default_meta_caps_array() |
|
| 156 | - ); |
|
| 157 | - // add filter for map_meta_caps but only if models can query. |
|
| 158 | - if (! has_filter('map_meta_cap', array($this, 'map_meta_caps'))) { |
|
| 159 | - add_filter('map_meta_cap', array($this, 'map_meta_caps'), 10, 4); |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * This builds and returns the default meta_caps array only once. |
|
| 166 | - * |
|
| 167 | - * @since 4.8.28.rc.012 |
|
| 168 | - * @return array |
|
| 169 | - * @throws EE_Error |
|
| 170 | - */ |
|
| 171 | - private function _get_default_meta_caps_array() |
|
| 172 | - { |
|
| 173 | - static $default_meta_caps = array(); |
|
| 174 | - // make sure we're only ever initializing the default _meta_caps array once if it's empty. |
|
| 175 | - if (empty($default_meta_caps)) { |
|
| 176 | - $default_meta_caps = array( |
|
| 177 | - // edits |
|
| 178 | - new EE_Meta_Capability_Map_Edit( |
|
| 179 | - 'ee_edit_event', |
|
| 180 | - array('Event', 'ee_edit_published_events', 'ee_edit_others_events', 'ee_edit_private_events') |
|
| 181 | - ), |
|
| 182 | - new EE_Meta_Capability_Map_Edit( |
|
| 183 | - 'ee_edit_venue', |
|
| 184 | - array('Venue', 'ee_edit_published_venues', 'ee_edit_others_venues', 'ee_edit_private_venues') |
|
| 185 | - ), |
|
| 186 | - new EE_Meta_Capability_Map_Edit( |
|
| 187 | - 'ee_edit_registration', |
|
| 188 | - array('Registration', '', 'ee_edit_others_registrations', '') |
|
| 189 | - ), |
|
| 190 | - new EE_Meta_Capability_Map_Edit( |
|
| 191 | - 'ee_edit_checkin', |
|
| 192 | - array('Registration', '', 'ee_edit_others_checkins', '') |
|
| 193 | - ), |
|
| 194 | - new EE_Meta_Capability_Map_Messages_Cap( |
|
| 195 | - 'ee_edit_message', |
|
| 196 | - array('Message_Template_Group', '', 'ee_edit_others_messages', 'ee_edit_global_messages') |
|
| 197 | - ), |
|
| 198 | - new EE_Meta_Capability_Map_Edit( |
|
| 199 | - 'ee_edit_default_ticket', |
|
| 200 | - array('Ticket', '', 'ee_edit_others_default_tickets', '') |
|
| 201 | - ), |
|
| 202 | - new EE_Meta_Capability_Map_Registration_Form_Cap( |
|
| 203 | - 'ee_edit_question', |
|
| 204 | - array('Question', '', '', 'ee_edit_system_questions') |
|
| 205 | - ), |
|
| 206 | - new EE_Meta_Capability_Map_Registration_Form_Cap( |
|
| 207 | - 'ee_edit_question_group', |
|
| 208 | - array('Question_Group', '', '', 'ee_edit_system_question_groups') |
|
| 209 | - ), |
|
| 210 | - new EE_Meta_Capability_Map_Edit( |
|
| 211 | - 'ee_edit_payment_method', |
|
| 212 | - array('Payment_Method', '', 'ee_edit_others_payment_methods', '') |
|
| 213 | - ), |
|
| 214 | - // reads |
|
| 215 | - new EE_Meta_Capability_Map_Read( |
|
| 216 | - 'ee_read_event', |
|
| 217 | - array('Event', '', 'ee_read_others_events', 'ee_read_private_events') |
|
| 218 | - ), |
|
| 219 | - new EE_Meta_Capability_Map_Read( |
|
| 220 | - 'ee_read_venue', |
|
| 221 | - array('Venue', '', 'ee_read_others_venues', 'ee_read_private_venues') |
|
| 222 | - ), |
|
| 223 | - new EE_Meta_Capability_Map_Read( |
|
| 224 | - 'ee_read_registration', |
|
| 225 | - array('Registration', '', 'ee_read_others_registrations', '') |
|
| 226 | - ), |
|
| 227 | - new EE_Meta_Capability_Map_Read( |
|
| 228 | - 'ee_read_checkin', |
|
| 229 | - array('Registration', '', 'ee_read_others_checkins', '') |
|
| 230 | - ), |
|
| 231 | - new EE_Meta_Capability_Map_Messages_Cap( |
|
| 232 | - 'ee_read_message', |
|
| 233 | - array('Message_Template_Group', '', 'ee_read_others_messages', 'ee_read_global_messages') |
|
| 234 | - ), |
|
| 235 | - new EE_Meta_Capability_Map_Read( |
|
| 236 | - 'ee_read_default_ticket', |
|
| 237 | - array('Ticket', '', 'ee_read_others_default_tickets', '') |
|
| 238 | - ), |
|
| 239 | - new EE_Meta_Capability_Map_Read( |
|
| 240 | - 'ee_read_payment_method', |
|
| 241 | - array('Payment_Method', '', 'ee_read_others_payment_methods', '') |
|
| 242 | - ), |
|
| 243 | - // deletes |
|
| 244 | - new EE_Meta_Capability_Map_Delete( |
|
| 245 | - 'ee_delete_event', |
|
| 246 | - array( |
|
| 247 | - 'Event', |
|
| 248 | - 'ee_delete_published_events', |
|
| 249 | - 'ee_delete_others_events', |
|
| 250 | - 'ee_delete_private_events', |
|
| 251 | - ) |
|
| 252 | - ), |
|
| 253 | - new EE_Meta_Capability_Map_Delete( |
|
| 254 | - 'ee_delete_venue', |
|
| 255 | - array( |
|
| 256 | - 'Venue', |
|
| 257 | - 'ee_delete_published_venues', |
|
| 258 | - 'ee_delete_others_venues', |
|
| 259 | - 'ee_delete_private_venues', |
|
| 260 | - ) |
|
| 261 | - ), |
|
| 262 | - new EE_Meta_Capability_Map_Delete( |
|
| 263 | - 'ee_delete_registration', |
|
| 264 | - array('Registration', '', 'ee_delete_others_registrations', '') |
|
| 265 | - ), |
|
| 266 | - new EE_Meta_Capability_Map_Delete( |
|
| 267 | - 'ee_delete_checkin', |
|
| 268 | - array('Registration', '', 'ee_delete_others_checkins', '') |
|
| 269 | - ), |
|
| 270 | - new EE_Meta_Capability_Map_Messages_Cap( |
|
| 271 | - 'ee_delete_message', |
|
| 272 | - array('Message_Template_Group', '', 'ee_delete_others_messages', 'ee_delete_global_messages') |
|
| 273 | - ), |
|
| 274 | - new EE_Meta_Capability_Map_Delete( |
|
| 275 | - 'ee_delete_default_ticket', |
|
| 276 | - array('Ticket', '', 'ee_delete_others_default_tickets', '') |
|
| 277 | - ), |
|
| 278 | - new EE_Meta_Capability_Map_Registration_Form_Cap( |
|
| 279 | - 'ee_delete_question', |
|
| 280 | - array('Question', '', '', 'delete_system_questions') |
|
| 281 | - ), |
|
| 282 | - new EE_Meta_Capability_Map_Registration_Form_Cap( |
|
| 283 | - 'ee_delete_question_group', |
|
| 284 | - array('Question_Group', '', '', 'delete_system_question_groups') |
|
| 285 | - ), |
|
| 286 | - new EE_Meta_Capability_Map_Delete( |
|
| 287 | - 'ee_delete_payment_method', |
|
| 288 | - array('Payment_Method', '', 'ee_delete_others_payment_methods', '') |
|
| 289 | - ), |
|
| 290 | - ); |
|
| 291 | - } |
|
| 292 | - return $default_meta_caps; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 298 | - * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 299 | - * |
|
| 300 | - * The actual logic is carried out by implementer classes in their definition of _map_meta_caps. |
|
| 301 | - * |
|
| 302 | - * @since 4.5.0 |
|
| 303 | - * @see wp-includes/capabilities.php |
|
| 304 | - * |
|
| 305 | - * @param array $caps actual users capabilities |
|
| 306 | - * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 307 | - * @param int $user_id The user id |
|
| 308 | - * @param array $args Adds context to the cap. Typically the object ID. |
|
| 309 | - * @return array actual users capabilities |
|
| 310 | - * @throws EE_Error |
|
| 311 | - */ |
|
| 312 | - public function map_meta_caps($caps, $cap, $user_id, $args) |
|
| 313 | - { |
|
| 314 | - if (did_action('AHEE__EE_System__load_espresso_addons__complete')) { |
|
| 315 | - // loop through our _meta_caps array |
|
| 316 | - foreach ($this->_meta_caps as $meta_map) { |
|
| 317 | - if (! $meta_map instanceof EE_Meta_Capability_Map) { |
|
| 318 | - continue; |
|
| 319 | - } |
|
| 320 | - // don't load models if there is no object ID in the args |
|
| 321 | - if (! empty($args[0])) { |
|
| 322 | - $meta_map->ensure_is_model(); |
|
| 323 | - } |
|
| 324 | - $caps = $meta_map->map_meta_caps($caps, $cap, $user_id, $args); |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - return $caps; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * This sets up and returns the initial capabilities map for Event Espresso |
|
| 333 | - * Note this array is filtered. |
|
| 334 | - * It is assumed that all available EE capabilities are assigned to the administrator role. |
|
| 335 | - * |
|
| 336 | - * @since 4.5.0 |
|
| 337 | - * |
|
| 338 | - * @return array |
|
| 339 | - */ |
|
| 340 | - private function _init_caps_map() |
|
| 341 | - { |
|
| 342 | - return apply_filters( |
|
| 343 | - 'FHEE__EE_Capabilities__init_caps_map__caps', |
|
| 344 | - array( |
|
| 345 | - 'administrator' => array( |
|
| 346 | - // basic access |
|
| 347 | - 'ee_read_ee', |
|
| 348 | - // gateways |
|
| 349 | - /** |
|
| 350 | - * note that with payment method capabilities, although we've implemented |
|
| 351 | - * capability mapping which will be used for accessing payment methods owned by |
|
| 352 | - * other users. This is not fully implemented yet in the payment method ui. |
|
| 353 | - * Currently only the "plural" caps are in active use. |
|
| 354 | - * (Specific payment method caps are in use as well). |
|
| 355 | - **/ |
|
| 356 | - 'ee_manage_gateways', |
|
| 357 | - 'ee_read_payment_methods', |
|
| 358 | - 'ee_read_others_payment_methods', |
|
| 359 | - 'ee_edit_payment_methods', |
|
| 360 | - 'ee_edit_others_payment_methods', |
|
| 361 | - 'ee_delete_payment_methods', |
|
| 362 | - // events |
|
| 363 | - 'ee_publish_events', |
|
| 364 | - 'ee_read_private_events', |
|
| 365 | - 'ee_read_others_events', |
|
| 366 | - 'ee_read_events', |
|
| 367 | - 'ee_edit_events', |
|
| 368 | - 'ee_edit_published_events', |
|
| 369 | - 'ee_edit_others_events', |
|
| 370 | - 'ee_edit_private_events', |
|
| 371 | - 'ee_delete_published_events', |
|
| 372 | - 'ee_delete_private_events', |
|
| 373 | - 'ee_delete_events', |
|
| 374 | - 'ee_delete_others_events', |
|
| 375 | - // event categories |
|
| 376 | - 'ee_manage_event_categories', |
|
| 377 | - 'ee_edit_event_category', |
|
| 378 | - 'ee_delete_event_category', |
|
| 379 | - 'ee_assign_event_category', |
|
| 380 | - // venues |
|
| 381 | - 'ee_publish_venues', |
|
| 382 | - 'ee_read_venues', |
|
| 383 | - 'ee_read_others_venues', |
|
| 384 | - 'ee_read_private_venues', |
|
| 385 | - 'ee_edit_venues', |
|
| 386 | - 'ee_edit_others_venues', |
|
| 387 | - 'ee_edit_published_venues', |
|
| 388 | - 'ee_edit_private_venues', |
|
| 389 | - 'ee_delete_venues', |
|
| 390 | - 'ee_delete_others_venues', |
|
| 391 | - 'ee_delete_private_venues', |
|
| 392 | - 'ee_delete_published_venues', |
|
| 393 | - // venue categories |
|
| 394 | - 'ee_manage_venue_categories', |
|
| 395 | - 'ee_edit_venue_category', |
|
| 396 | - 'ee_delete_venue_category', |
|
| 397 | - 'ee_assign_venue_category', |
|
| 398 | - // contacts |
|
| 399 | - 'ee_read_contacts', |
|
| 400 | - 'ee_edit_contacts', |
|
| 401 | - 'ee_delete_contacts', |
|
| 402 | - // registrations |
|
| 403 | - 'ee_read_registrations', |
|
| 404 | - 'ee_read_others_registrations', |
|
| 405 | - 'ee_edit_registrations', |
|
| 406 | - 'ee_edit_others_registrations', |
|
| 407 | - 'ee_delete_registrations', |
|
| 408 | - 'ee_delete_others_registrations', |
|
| 409 | - // checkins |
|
| 410 | - 'ee_read_others_checkins', |
|
| 411 | - 'ee_read_checkins', |
|
| 412 | - 'ee_edit_checkins', |
|
| 413 | - 'ee_edit_others_checkins', |
|
| 414 | - 'ee_delete_checkins', |
|
| 415 | - 'ee_delete_others_checkins', |
|
| 416 | - // transactions && payments |
|
| 417 | - 'ee_read_transaction', |
|
| 418 | - 'ee_read_transactions', |
|
| 419 | - 'ee_edit_payments', |
|
| 420 | - 'ee_delete_payments', |
|
| 421 | - // messages |
|
| 422 | - 'ee_read_messages', |
|
| 423 | - 'ee_read_others_messages', |
|
| 424 | - 'ee_read_global_messages', |
|
| 425 | - 'ee_edit_global_messages', |
|
| 426 | - 'ee_edit_messages', |
|
| 427 | - 'ee_edit_others_messages', |
|
| 428 | - 'ee_delete_messages', |
|
| 429 | - 'ee_delete_others_messages', |
|
| 430 | - 'ee_delete_global_messages', |
|
| 431 | - 'ee_send_message', |
|
| 432 | - // tickets |
|
| 433 | - 'ee_read_default_tickets', |
|
| 434 | - 'ee_read_others_default_tickets', |
|
| 435 | - 'ee_edit_default_tickets', |
|
| 436 | - 'ee_edit_others_default_tickets', |
|
| 437 | - 'ee_delete_default_tickets', |
|
| 438 | - 'ee_delete_others_default_tickets', |
|
| 439 | - // prices |
|
| 440 | - 'ee_edit_default_price', |
|
| 441 | - 'ee_edit_default_prices', |
|
| 442 | - 'ee_delete_default_price', |
|
| 443 | - 'ee_delete_default_prices', |
|
| 444 | - 'ee_edit_default_price_type', |
|
| 445 | - 'ee_edit_default_price_types', |
|
| 446 | - 'ee_delete_default_price_type', |
|
| 447 | - 'ee_delete_default_price_types', |
|
| 448 | - 'ee_read_default_prices', |
|
| 449 | - 'ee_read_default_price_types', |
|
| 450 | - // registration form |
|
| 451 | - 'ee_edit_questions', |
|
| 452 | - 'ee_edit_system_questions', |
|
| 453 | - 'ee_read_questions', |
|
| 454 | - 'ee_delete_questions', |
|
| 455 | - 'ee_edit_question_groups', |
|
| 456 | - 'ee_read_question_groups', |
|
| 457 | - 'ee_edit_system_question_groups', |
|
| 458 | - 'ee_delete_question_groups', |
|
| 459 | - // event_type taxonomy |
|
| 460 | - 'ee_assign_event_type', |
|
| 461 | - 'ee_manage_event_types', |
|
| 462 | - 'ee_edit_event_type', |
|
| 463 | - 'ee_delete_event_type', |
|
| 464 | - ), |
|
| 465 | - 'ee_events_administrator' => array( |
|
| 466 | - // core wp caps |
|
| 467 | - 'read', |
|
| 468 | - 'read_private_pages', |
|
| 469 | - 'read_private_posts', |
|
| 470 | - 'edit_users', |
|
| 471 | - 'edit_posts', |
|
| 472 | - 'edit_pages', |
|
| 473 | - 'edit_published_posts', |
|
| 474 | - 'edit_published_pages', |
|
| 475 | - 'edit_private_pages', |
|
| 476 | - 'edit_private_posts', |
|
| 477 | - 'edit_others_posts', |
|
| 478 | - 'edit_others_pages', |
|
| 479 | - 'publish_posts', |
|
| 480 | - 'publish_pages', |
|
| 481 | - 'delete_posts', |
|
| 482 | - 'delete_pages', |
|
| 483 | - 'delete_private_pages', |
|
| 484 | - 'delete_private_posts', |
|
| 485 | - 'delete_published_pages', |
|
| 486 | - 'delete_published_posts', |
|
| 487 | - 'delete_others_posts', |
|
| 488 | - 'delete_others_pages', |
|
| 489 | - 'manage_categories', |
|
| 490 | - 'manage_links', |
|
| 491 | - 'moderate_comments', |
|
| 492 | - 'unfiltered_html', |
|
| 493 | - 'upload_files', |
|
| 494 | - 'export', |
|
| 495 | - 'import', |
|
| 496 | - 'list_users', |
|
| 497 | - 'level_1', // required if user with this role shows up in author dropdowns |
|
| 498 | - // basic ee access |
|
| 499 | - 'ee_read_ee', |
|
| 500 | - // events |
|
| 501 | - 'ee_publish_events', |
|
| 502 | - 'ee_read_private_events', |
|
| 503 | - 'ee_read_others_events', |
|
| 504 | - 'ee_read_event', |
|
| 505 | - 'ee_read_events', |
|
| 506 | - 'ee_edit_event', |
|
| 507 | - 'ee_edit_events', |
|
| 508 | - 'ee_edit_published_events', |
|
| 509 | - 'ee_edit_others_events', |
|
| 510 | - 'ee_edit_private_events', |
|
| 511 | - 'ee_delete_published_events', |
|
| 512 | - 'ee_delete_private_events', |
|
| 513 | - 'ee_delete_event', |
|
| 514 | - 'ee_delete_events', |
|
| 515 | - 'ee_delete_others_events', |
|
| 516 | - // event categories |
|
| 517 | - 'ee_manage_event_categories', |
|
| 518 | - 'ee_edit_event_category', |
|
| 519 | - 'ee_delete_event_category', |
|
| 520 | - 'ee_assign_event_category', |
|
| 521 | - // venues |
|
| 522 | - 'ee_publish_venues', |
|
| 523 | - 'ee_read_venue', |
|
| 524 | - 'ee_read_venues', |
|
| 525 | - 'ee_read_others_venues', |
|
| 526 | - 'ee_read_private_venues', |
|
| 527 | - 'ee_edit_venue', |
|
| 528 | - 'ee_edit_venues', |
|
| 529 | - 'ee_edit_others_venues', |
|
| 530 | - 'ee_edit_published_venues', |
|
| 531 | - 'ee_edit_private_venues', |
|
| 532 | - 'ee_delete_venue', |
|
| 533 | - 'ee_delete_venues', |
|
| 534 | - 'ee_delete_others_venues', |
|
| 535 | - 'ee_delete_private_venues', |
|
| 536 | - 'ee_delete_published_venues', |
|
| 537 | - // venue categories |
|
| 538 | - 'ee_manage_venue_categories', |
|
| 539 | - 'ee_edit_venue_category', |
|
| 540 | - 'ee_delete_venue_category', |
|
| 541 | - 'ee_assign_venue_category', |
|
| 542 | - // contacts |
|
| 543 | - 'ee_read_contacts', |
|
| 544 | - 'ee_edit_contacts', |
|
| 545 | - 'ee_delete_contacts', |
|
| 546 | - // registrations |
|
| 547 | - 'ee_read_registrations', |
|
| 548 | - 'ee_read_others_registrations', |
|
| 549 | - 'ee_edit_registration', |
|
| 550 | - 'ee_edit_registrations', |
|
| 551 | - 'ee_edit_others_registrations', |
|
| 552 | - 'ee_delete_registration', |
|
| 553 | - 'ee_delete_registrations', |
|
| 554 | - 'ee_delete_others_registrations', |
|
| 555 | - // checkins |
|
| 556 | - 'ee_read_others_checkins', |
|
| 557 | - 'ee_read_checkins', |
|
| 558 | - 'ee_edit_checkins', |
|
| 559 | - 'ee_edit_others_checkins', |
|
| 560 | - 'ee_delete_checkins', |
|
| 561 | - 'ee_delete_others_checkins', |
|
| 562 | - // transactions && payments |
|
| 563 | - 'ee_read_transaction', |
|
| 564 | - 'ee_read_transactions', |
|
| 565 | - 'ee_edit_payments', |
|
| 566 | - 'ee_delete_payments', |
|
| 567 | - // messages |
|
| 568 | - 'ee_read_messages', |
|
| 569 | - 'ee_read_others_messages', |
|
| 570 | - 'ee_read_global_messages', |
|
| 571 | - 'ee_edit_global_messages', |
|
| 572 | - 'ee_edit_messages', |
|
| 573 | - 'ee_edit_others_messages', |
|
| 574 | - 'ee_delete_messages', |
|
| 575 | - 'ee_delete_others_messages', |
|
| 576 | - 'ee_delete_global_messages', |
|
| 577 | - 'ee_send_message', |
|
| 578 | - // tickets |
|
| 579 | - 'ee_read_default_tickets', |
|
| 580 | - 'ee_read_others_default_tickets', |
|
| 581 | - 'ee_edit_default_tickets', |
|
| 582 | - 'ee_edit_others_default_tickets', |
|
| 583 | - 'ee_delete_default_tickets', |
|
| 584 | - 'ee_delete_others_default_tickets', |
|
| 585 | - // prices |
|
| 586 | - 'ee_edit_default_price', |
|
| 587 | - 'ee_edit_default_prices', |
|
| 588 | - 'ee_delete_default_price', |
|
| 589 | - 'ee_delete_default_prices', |
|
| 590 | - 'ee_edit_default_price_type', |
|
| 591 | - 'ee_edit_default_price_types', |
|
| 592 | - 'ee_delete_default_price_type', |
|
| 593 | - 'ee_delete_default_price_types', |
|
| 594 | - 'ee_read_default_prices', |
|
| 595 | - 'ee_read_default_price_types', |
|
| 596 | - // registration form |
|
| 597 | - 'ee_edit_questions', |
|
| 598 | - 'ee_edit_system_questions', |
|
| 599 | - 'ee_read_questions', |
|
| 600 | - 'ee_delete_questions', |
|
| 601 | - 'ee_edit_question_groups', |
|
| 602 | - 'ee_read_question_groups', |
|
| 603 | - 'ee_edit_system_question_groups', |
|
| 604 | - 'ee_delete_question_groups', |
|
| 605 | - // event_type taxonomy |
|
| 606 | - 'ee_assign_event_type', |
|
| 607 | - 'ee_manage_event_types', |
|
| 608 | - 'ee_edit_event_type', |
|
| 609 | - 'ee_delete_event_type', |
|
| 610 | - ), |
|
| 611 | - ) |
|
| 612 | - ); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - |
|
| 616 | - /** |
|
| 617 | - * @return bool |
|
| 618 | - * @throws EE_Error |
|
| 619 | - */ |
|
| 620 | - private function setupCapabilitiesMap() |
|
| 621 | - { |
|
| 622 | - // if the initialization process hasn't even started, then we need to call init_caps() |
|
| 623 | - if ($this->initialized === null) { |
|
| 624 | - return $this->init_caps(); |
|
| 625 | - } |
|
| 626 | - // unless resetting, get caps from db if we haven't already |
|
| 627 | - $this->capabilities_map = $this->reset || ! empty($this->capabilities_map) |
|
| 628 | - ? $this->capabilities_map |
|
| 629 | - : get_option(self::option_name, array()); |
|
| 630 | - return true; |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - |
|
| 634 | - /** |
|
| 635 | - * @param bool $update |
|
| 636 | - * @return bool |
|
| 637 | - */ |
|
| 638 | - private function updateCapabilitiesMap($update = true) |
|
| 639 | - { |
|
| 640 | - return $update ? update_option(self::option_name, $this->capabilities_map) : false; |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - |
|
| 644 | - /** |
|
| 645 | - * Adds capabilities to roles. |
|
| 646 | - * |
|
| 647 | - * @since 4.9.42 |
|
| 648 | - * @param array $capabilities_to_add array of capabilities to add, indexed by roles. |
|
| 649 | - * Note that this should ONLY be called on activation hook |
|
| 650 | - * otherwise the caps will be added on every request. |
|
| 651 | - * @return bool |
|
| 652 | - * @throws \EE_Error |
|
| 653 | - */ |
|
| 654 | - public function addCaps(array $capabilities_to_add) |
|
| 655 | - { |
|
| 656 | - // don't do anything if the capabilities map can not be initialized |
|
| 657 | - if (! $this->setupCapabilitiesMap()) { |
|
| 658 | - return false; |
|
| 659 | - } |
|
| 660 | - // and filter the array so others can get in on the fun during resets |
|
| 661 | - $capabilities_to_add = apply_filters( |
|
| 662 | - 'FHEE__EE_Capabilities__addCaps__capabilities_to_add', |
|
| 663 | - $capabilities_to_add, |
|
| 664 | - $this->reset, |
|
| 665 | - $this->capabilities_map |
|
| 666 | - ); |
|
| 667 | - $update_capabilities_map = false; |
|
| 668 | - // if not reset, see what caps are new for each role. if they're new, add them. |
|
| 669 | - foreach ($capabilities_to_add as $role => $caps_for_role) { |
|
| 670 | - if (is_array($caps_for_role)) { |
|
| 671 | - foreach ($caps_for_role as $cap) { |
|
| 672 | - if (! $this->capHasBeenAddedToRole($role, $cap) |
|
| 673 | - && $this->add_cap_to_role($role, $cap, true, false) |
|
| 674 | - ) { |
|
| 675 | - $update_capabilities_map = true; |
|
| 676 | - } |
|
| 677 | - } |
|
| 678 | - } |
|
| 679 | - } |
|
| 680 | - // now let's just save the cap that has been set but only if there's been a change. |
|
| 681 | - $updated = $this->updateCapabilitiesMap($update_capabilities_map); |
|
| 682 | - $this->flushWpUser($updated); |
|
| 683 | - do_action('AHEE__EE_Capabilities__addCaps__complete', $this->capabilities_map, $updated); |
|
| 684 | - return $updated; |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - |
|
| 688 | - /** |
|
| 689 | - * Loops through the capabilities map and removes the role caps specified by the incoming array |
|
| 690 | - * |
|
| 691 | - * @param array $caps_map map of capabilities to be removed (indexed by roles) |
|
| 692 | - * @return bool |
|
| 693 | - * @throws \EE_Error |
|
| 694 | - */ |
|
| 695 | - public function removeCaps($caps_map) |
|
| 696 | - { |
|
| 697 | - // don't do anything if the capabilities map can not be initialized |
|
| 698 | - if (! $this->setupCapabilitiesMap()) { |
|
| 699 | - return false; |
|
| 700 | - } |
|
| 701 | - $update_capabilities_map = false; |
|
| 702 | - foreach ($caps_map as $role => $caps_for_role) { |
|
| 703 | - if (is_array($caps_for_role)) { |
|
| 704 | - foreach ($caps_for_role as $cap) { |
|
| 705 | - if ($this->capHasBeenAddedToRole($role, $cap) |
|
| 706 | - && $this->remove_cap_from_role($role, $cap, false) |
|
| 707 | - ) { |
|
| 708 | - $update_capabilities_map = true; |
|
| 709 | - } |
|
| 710 | - } |
|
| 711 | - } |
|
| 712 | - } |
|
| 713 | - // maybe resave the caps |
|
| 714 | - $updated = $this->updateCapabilitiesMap($update_capabilities_map); |
|
| 715 | - $this->flushWpUser($updated); |
|
| 716 | - return $updated; |
|
| 717 | - } |
|
| 718 | - |
|
| 719 | - |
|
| 720 | - /** |
|
| 721 | - * This ensures that the WP User object cached on the $current_user global in WP has the latest capabilities from |
|
| 722 | - * the roles on that user. |
|
| 723 | - * |
|
| 724 | - * @param bool $flush Default is to flush the WP_User object. If false, then this method effectively does nothing. |
|
| 725 | - */ |
|
| 726 | - private function flushWpUser($flush = true) |
|
| 727 | - { |
|
| 728 | - if ($flush) { |
|
| 729 | - $user = wp_get_current_user(); |
|
| 730 | - if ($user instanceof WP_User) { |
|
| 731 | - $user->get_role_caps(); |
|
| 732 | - } |
|
| 733 | - } |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - |
|
| 737 | - /** |
|
| 738 | - * This method sets a capability on a role. Note this should only be done on activation, or if you have something |
|
| 739 | - * specific to prevent the cap from being added on every page load (adding caps are persistent to the db). Note. |
|
| 740 | - * this is a wrapper for $wp_role->add_cap() |
|
| 741 | - * |
|
| 742 | - * @see wp-includes/capabilities.php |
|
| 743 | - * @since 4.5.0 |
|
| 744 | - * @param string|WP_Role $role A WordPress role the capability is being added to |
|
| 745 | - * @param string $cap The capability being added to the role |
|
| 746 | - * @param bool $grant Whether to grant access to this cap on this role. |
|
| 747 | - * @param bool $update_capabilities_map |
|
| 748 | - * @return bool |
|
| 749 | - * @throws \EE_Error |
|
| 750 | - */ |
|
| 751 | - public function add_cap_to_role($role, $cap, $grant = true, $update_capabilities_map = true) |
|
| 752 | - { |
|
| 753 | - // capture incoming value for $role because we may need it to create a new WP_Role |
|
| 754 | - $orig_role = $role; |
|
| 755 | - $role = $role instanceof WP_Role ? $role : get_role($role); |
|
| 756 | - // if the role isn't available then we create it. |
|
| 757 | - if (! $role instanceof WP_Role) { |
|
| 758 | - // if a plugin wants to create a specific role name then they should create the role before |
|
| 759 | - // EE_Capabilities does. Otherwise this function will create the role name from the slug: |
|
| 760 | - // - removes any `ee_` namespacing from the start of the slug. |
|
| 761 | - // - replaces `_` with ` ` (empty space). |
|
| 762 | - // - sentence case on the resulting string. |
|
| 763 | - $role_label = ucwords(str_replace(array('ee_', '_'), array('', ' '), $orig_role)); |
|
| 764 | - $role = add_role($orig_role, $role_label); |
|
| 765 | - } |
|
| 766 | - if ($role instanceof WP_Role) { |
|
| 767 | - // don't do anything if the capabilities map can not be initialized |
|
| 768 | - if (! $this->setupCapabilitiesMap()) { |
|
| 769 | - return false; |
|
| 770 | - } |
|
| 771 | - if (! $this->capHasBeenAddedToRole($role->name, $cap)) { |
|
| 772 | - $role->add_cap($cap, $grant); |
|
| 773 | - $this->capabilities_map[ $role->name ][] = $cap; |
|
| 774 | - $this->updateCapabilitiesMap($update_capabilities_map); |
|
| 775 | - return true; |
|
| 776 | - } |
|
| 777 | - } |
|
| 778 | - return false; |
|
| 779 | - } |
|
| 780 | - |
|
| 781 | - |
|
| 782 | - /** |
|
| 783 | - * Functions similarly to add_cap_to_role except removes cap from given role. |
|
| 784 | - * Wrapper for $wp_role->remove_cap() |
|
| 785 | - * |
|
| 786 | - * @see wp-includes/capabilities.php |
|
| 787 | - * @since 4.5.0 |
|
| 788 | - * @param string|WP_Role $role A WordPress role the capability is being removed from. |
|
| 789 | - * @param string $cap The capability being removed |
|
| 790 | - * @param bool $update_capabilities_map |
|
| 791 | - * @return bool |
|
| 792 | - * @throws \EE_Error |
|
| 793 | - */ |
|
| 794 | - public function remove_cap_from_role($role, $cap, $update_capabilities_map = true) |
|
| 795 | - { |
|
| 796 | - // don't do anything if the capabilities map can not be initialized |
|
| 797 | - if (! $this->setupCapabilitiesMap()) { |
|
| 798 | - return false; |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - $role = $role instanceof WP_Role ? $role : get_role($role); |
|
| 802 | - if ($role instanceof WP_Role && $index = $this->capHasBeenAddedToRole($role->name, $cap, true)) { |
|
| 803 | - $role->remove_cap($cap); |
|
| 804 | - unset($this->capabilities_map[ $role->name ][ $index ]); |
|
| 805 | - $this->updateCapabilitiesMap($update_capabilities_map); |
|
| 806 | - return true; |
|
| 807 | - } |
|
| 808 | - return false; |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - |
|
| 812 | - /** |
|
| 813 | - * @param string $role_name |
|
| 814 | - * @param string $cap |
|
| 815 | - * @param bool $get_index |
|
| 816 | - * @return bool|mixed |
|
| 817 | - */ |
|
| 818 | - private function capHasBeenAddedToRole($role_name = '', $cap = '', $get_index = false) |
|
| 819 | - { |
|
| 820 | - if (isset($this->capabilities_map[ $role_name ]) |
|
| 821 | - && ($index = array_search($cap, $this->capabilities_map[ $role_name ], true)) !== false |
|
| 822 | - ) { |
|
| 823 | - return $get_index ? $index : true; |
|
| 824 | - } |
|
| 825 | - return false; |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - |
|
| 829 | - /** |
|
| 830 | - * Wrapper for the native WP current_user_can() method. |
|
| 831 | - * This is provided as a handy method for a couple things: |
|
| 832 | - * 1. Using the context string it allows for targeted filtering by addons for a specific check (without having to |
|
| 833 | - * write those filters wherever current_user_can is called). |
|
| 834 | - * 2. Explicit passing of $id from a given context ( useful in the cases of map_meta_cap filters ) |
|
| 835 | - * |
|
| 836 | - * @since 4.5.0 |
|
| 837 | - * |
|
| 838 | - * @param string $cap The cap being checked. |
|
| 839 | - * @param string $context The context where the current_user_can is being called from. |
|
| 840 | - * @param int $id Optional. Id for item where current_user_can is being called from (used in map_meta_cap() |
|
| 841 | - * filters. |
|
| 842 | - * |
|
| 843 | - * @return bool Whether user can or not. |
|
| 844 | - */ |
|
| 845 | - public function current_user_can($cap, $context, $id = 0) |
|
| 846 | - { |
|
| 847 | - // apply filters (both a global on just the cap, and context specific. Global overrides context specific) |
|
| 848 | - $filtered_cap = apply_filters('FHEE__EE_Capabilities__current_user_can__cap__' . $context, $cap, $id); |
|
| 849 | - $filtered_cap = apply_filters( |
|
| 850 | - 'FHEE__EE_Capabilities__current_user_can__cap', |
|
| 851 | - $filtered_cap, |
|
| 852 | - $context, |
|
| 853 | - $cap, |
|
| 854 | - $id |
|
| 855 | - ); |
|
| 856 | - return ! empty($id) |
|
| 857 | - ? current_user_can($filtered_cap, $id) |
|
| 858 | - : current_user_can($filtered_cap); |
|
| 859 | - } |
|
| 860 | - |
|
| 861 | - |
|
| 862 | - /** |
|
| 863 | - * This is a wrapper for the WP user_can() function and follows the same style as the other wrappers in this class. |
|
| 864 | - * |
|
| 865 | - * @param int|WP_User $user Either the user_id or a WP_User object |
|
| 866 | - * @param string $cap The capability string being checked |
|
| 867 | - * @param string $context The context where the user_can is being called from (used in filters). |
|
| 868 | - * @param int $id Optional. Id for item where user_can is being called from ( used in map_meta_cap() |
|
| 869 | - * filters) |
|
| 870 | - * |
|
| 871 | - * @return bool Whether user can or not. |
|
| 872 | - */ |
|
| 873 | - public function user_can($user, $cap, $context, $id = 0) |
|
| 874 | - { |
|
| 875 | - // apply filters (both a global on just the cap, and context specific. Global overrides context specific) |
|
| 876 | - $filtered_cap = apply_filters('FHEE__EE_Capabilities__user_can__cap__' . $context, $cap, $user, $id); |
|
| 877 | - $filtered_cap = apply_filters( |
|
| 878 | - 'FHEE__EE_Capabilities__user_can__cap', |
|
| 879 | - $filtered_cap, |
|
| 880 | - $context, |
|
| 881 | - $cap, |
|
| 882 | - $user, |
|
| 883 | - $id |
|
| 884 | - ); |
|
| 885 | - return ! empty($id) |
|
| 886 | - ? user_can($user, $filtered_cap, $id) |
|
| 887 | - : user_can($user, $filtered_cap); |
|
| 888 | - } |
|
| 889 | - |
|
| 890 | - |
|
| 891 | - /** |
|
| 892 | - * Wrapper for the native WP current_user_can_for_blog() method. |
|
| 893 | - * This is provided as a handy method for a couple things: |
|
| 894 | - * 1. Using the context string it allows for targeted filtering by addons for a specific check (without having to |
|
| 895 | - * write those filters wherever current_user_can is called). |
|
| 896 | - * 2. Explicit passing of $id from a given context ( useful in the cases of map_meta_cap filters ) |
|
| 897 | - * |
|
| 898 | - * @since 4.5.0 |
|
| 899 | - * |
|
| 900 | - * @param int $blog_id The blog id that is being checked for. |
|
| 901 | - * @param string $cap The cap being checked. |
|
| 902 | - * @param string $context The context where the current_user_can is being called from. |
|
| 903 | - * @param int $id Optional. Id for item where current_user_can is being called from (used in map_meta_cap() |
|
| 904 | - * filters. |
|
| 905 | - * |
|
| 906 | - * @return bool Whether user can or not. |
|
| 907 | - */ |
|
| 908 | - public function current_user_can_for_blog($blog_id, $cap, $context, $id = 0) |
|
| 909 | - { |
|
| 910 | - $user_can = ! empty($id) |
|
| 911 | - ? current_user_can_for_blog($blog_id, $cap, $id) |
|
| 912 | - : current_user_can($blog_id, $cap); |
|
| 913 | - // apply filters (both a global on just the cap, and context specific. Global overrides context specific) |
|
| 914 | - $user_can = apply_filters( |
|
| 915 | - 'FHEE__EE_Capabilities__current_user_can_for_blog__user_can__' . $context, |
|
| 916 | - $user_can, |
|
| 917 | - $blog_id, |
|
| 918 | - $cap, |
|
| 919 | - $id |
|
| 920 | - ); |
|
| 921 | - $user_can = apply_filters( |
|
| 922 | - 'FHEE__EE_Capabilities__current_user_can_for_blog__user_can', |
|
| 923 | - $user_can, |
|
| 924 | - $context, |
|
| 925 | - $blog_id, |
|
| 926 | - $cap, |
|
| 927 | - $id |
|
| 928 | - ); |
|
| 929 | - return $user_can; |
|
| 930 | - } |
|
| 931 | - |
|
| 932 | - |
|
| 933 | - /** |
|
| 934 | - * This helper method just returns an array of registered EE capabilities. |
|
| 935 | - * |
|
| 936 | - * @since 4.5.0 |
|
| 937 | - * @param string $role If empty then the entire role/capability map is returned. |
|
| 938 | - * Otherwise just the capabilities for the given role are returned. |
|
| 939 | - * @return array |
|
| 940 | - * @throws EE_Error |
|
| 941 | - */ |
|
| 942 | - public function get_ee_capabilities($role = 'administrator') |
|
| 943 | - { |
|
| 944 | - if (! $this->initialized) { |
|
| 945 | - $this->init_caps(); |
|
| 946 | - } |
|
| 947 | - if (empty($role)) { |
|
| 948 | - return $this->capabilities_map; |
|
| 949 | - } |
|
| 950 | - return isset($this->capabilities_map[ $role ]) |
|
| 951 | - ? $this->capabilities_map[ $role ] |
|
| 952 | - : array(); |
|
| 953 | - } |
|
| 954 | - |
|
| 955 | - |
|
| 956 | - /** |
|
| 957 | - * @deprecated 4.9.42 |
|
| 958 | - * @param bool $reset If you need to reset Event Espresso's capabilities, |
|
| 959 | - * then please use the init_caps() method with the "$reset" parameter set to "true" |
|
| 960 | - * @param array $caps_map Optional. |
|
| 961 | - * Can be used to send a custom map of roles and capabilities for setting them up. |
|
| 962 | - * Note that this should ONLY be called on activation hook or some other one-time |
|
| 963 | - * task otherwise the caps will be added on every request. |
|
| 964 | - * @return void |
|
| 965 | - * @throws EE_Error |
|
| 966 | - */ |
|
| 967 | - public function init_role_caps($reset = false, $caps_map = array()) |
|
| 968 | - { |
|
| 969 | - // If this method is called directly and reset is set as 'true', |
|
| 970 | - // then display a doing it wrong notice, because we want resets to go through init_caps() |
|
| 971 | - // to guarantee that everything is set up correctly. |
|
| 972 | - // This prevents the capabilities map getting reset incorrectly by direct calls to this method. |
|
| 973 | - if ($reset) { |
|
| 974 | - EE_Error::doing_it_wrong( |
|
| 975 | - __METHOD__, |
|
| 976 | - sprintf( |
|
| 977 | - esc_html__( |
|
| 978 | - 'The "%1$s" parameter for the "%2$s" method is deprecated. If you need to reset Event Espresso\'s capabilities, then please use the "%3$s" method with the "%1$s" parameter set to "%4$s".', |
|
| 979 | - 'event_espresso' |
|
| 980 | - ), |
|
| 981 | - '$reset', |
|
| 982 | - __METHOD__ . '()', |
|
| 983 | - 'EE_Capabilities::init_caps()', |
|
| 984 | - 'true' |
|
| 985 | - ), |
|
| 986 | - '4.9.42', |
|
| 987 | - '5.0.0' |
|
| 988 | - ); |
|
| 989 | - } |
|
| 990 | - $this->addCaps($caps_map); |
|
| 991 | - } |
|
| 17 | + /** |
|
| 18 | + * the name of the wp option used to store caps previously initialized |
|
| 19 | + */ |
|
| 20 | + const option_name = 'ee_caps_initialized'; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * instance of EE_Capabilities object |
|
| 24 | + * |
|
| 25 | + * @var EE_Capabilities |
|
| 26 | + */ |
|
| 27 | + private static $_instance; |
|
| 28 | + |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * This is a map of caps that correspond to a default WP_Role. |
|
| 32 | + * Array is indexed by Role and values are ee capabilities. |
|
| 33 | + * |
|
| 34 | + * @since 4.5.0 |
|
| 35 | + * |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + private $capabilities_map = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * This used to hold an array of EE_Meta_Capability_Map objects |
|
| 42 | + * that define the granular capabilities mapped to for a user depending on context. |
|
| 43 | + * |
|
| 44 | + * @var EE_Meta_Capability_Map[] |
|
| 45 | + */ |
|
| 46 | + private $_meta_caps = array(); |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The internal $capabilities_map needs to be initialized before it can be used. |
|
| 50 | + * This flag tracks whether that has happened or not. |
|
| 51 | + * But for this to work, we need three states to indicate: |
|
| 52 | + * initialization has not occurred at all |
|
| 53 | + * initialization has started but is not complete |
|
| 54 | + * initialization is complete |
|
| 55 | + * The reason this is needed is because the addCaps() method |
|
| 56 | + * normally requires the $capabilities_map to be initialized, |
|
| 57 | + * but is also used during the initialization process. |
|
| 58 | + * So: |
|
| 59 | + * If initialized === null, init_caps() will be called before any other methods will run. |
|
| 60 | + * If initialized === false, then init_caps() is in the process of running it's logic. |
|
| 61 | + * If initialized === true, then init_caps() has completed the initialization process. |
|
| 62 | + * |
|
| 63 | + * @var boolean|null $initialized |
|
| 64 | + */ |
|
| 65 | + private $initialized; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @var boolean $reset |
|
| 69 | + */ |
|
| 70 | + private $reset = false; |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * singleton method used to instantiate class object |
|
| 75 | + * |
|
| 76 | + * @since 4.5.0 |
|
| 77 | + * |
|
| 78 | + * @return EE_Capabilities |
|
| 79 | + */ |
|
| 80 | + public static function instance() |
|
| 81 | + { |
|
| 82 | + // check if instantiated, and if not do so. |
|
| 83 | + if (! self::$_instance instanceof EE_Capabilities) { |
|
| 84 | + self::$_instance = new self(); |
|
| 85 | + } |
|
| 86 | + return self::$_instance; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * private constructor |
|
| 92 | + * |
|
| 93 | + * @since 4.5.0 |
|
| 94 | + */ |
|
| 95 | + private function __construct() |
|
| 96 | + { |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * This delays the initialization of the capabilities class until EE_System core is loaded and ready. |
|
| 102 | + * |
|
| 103 | + * @param bool $reset allows for resetting the default capabilities saved on roles. Note that this doesn't |
|
| 104 | + * actually REMOVE any capabilities from existing roles, it just resaves defaults roles and |
|
| 105 | + * ensures that they are up to date. |
|
| 106 | + * |
|
| 107 | + * @since 4.5.0 |
|
| 108 | + * @return bool |
|
| 109 | + * @throws EE_Error |
|
| 110 | + */ |
|
| 111 | + public function init_caps($reset = false) |
|
| 112 | + { |
|
| 113 | + if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 114 | + return false; |
|
| 115 | + } |
|
| 116 | + $this->reset = filter_var($reset, FILTER_VALIDATE_BOOLEAN); |
|
| 117 | + // if reset, then completely delete the cache option and clear the $capabilities_map property. |
|
| 118 | + if ($this->reset) { |
|
| 119 | + $this->initialized = null; |
|
| 120 | + $this->capabilities_map = array(); |
|
| 121 | + delete_option(self::option_name); |
|
| 122 | + } |
|
| 123 | + if ($this->initialized === null) { |
|
| 124 | + $this->initialized = false; |
|
| 125 | + do_action( |
|
| 126 | + 'AHEE__EE_Capabilities__init_caps__before_initialization', |
|
| 127 | + $this->reset |
|
| 128 | + ); |
|
| 129 | + $this->addCaps($this->_init_caps_map()); |
|
| 130 | + $this->_set_meta_caps(); |
|
| 131 | + do_action( |
|
| 132 | + 'AHEE__EE_Capabilities__init_caps__after_initialization', |
|
| 133 | + $this->capabilities_map |
|
| 134 | + ); |
|
| 135 | + $this->initialized = true; |
|
| 136 | + } |
|
| 137 | + // reset $this->reset so that it's not stuck on true if init_caps() gets called again |
|
| 138 | + $this->reset = false; |
|
| 139 | + return true; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * This sets the meta caps property. |
|
| 145 | + * |
|
| 146 | + * @since 4.5.0 |
|
| 147 | + * @return void |
|
| 148 | + * @throws EE_Error |
|
| 149 | + */ |
|
| 150 | + private function _set_meta_caps() |
|
| 151 | + { |
|
| 152 | + // get default meta caps and filter the returned array |
|
| 153 | + $this->_meta_caps = apply_filters( |
|
| 154 | + 'FHEE__EE_Capabilities___set_meta_caps__meta_caps', |
|
| 155 | + $this->_get_default_meta_caps_array() |
|
| 156 | + ); |
|
| 157 | + // add filter for map_meta_caps but only if models can query. |
|
| 158 | + if (! has_filter('map_meta_cap', array($this, 'map_meta_caps'))) { |
|
| 159 | + add_filter('map_meta_cap', array($this, 'map_meta_caps'), 10, 4); |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * This builds and returns the default meta_caps array only once. |
|
| 166 | + * |
|
| 167 | + * @since 4.8.28.rc.012 |
|
| 168 | + * @return array |
|
| 169 | + * @throws EE_Error |
|
| 170 | + */ |
|
| 171 | + private function _get_default_meta_caps_array() |
|
| 172 | + { |
|
| 173 | + static $default_meta_caps = array(); |
|
| 174 | + // make sure we're only ever initializing the default _meta_caps array once if it's empty. |
|
| 175 | + if (empty($default_meta_caps)) { |
|
| 176 | + $default_meta_caps = array( |
|
| 177 | + // edits |
|
| 178 | + new EE_Meta_Capability_Map_Edit( |
|
| 179 | + 'ee_edit_event', |
|
| 180 | + array('Event', 'ee_edit_published_events', 'ee_edit_others_events', 'ee_edit_private_events') |
|
| 181 | + ), |
|
| 182 | + new EE_Meta_Capability_Map_Edit( |
|
| 183 | + 'ee_edit_venue', |
|
| 184 | + array('Venue', 'ee_edit_published_venues', 'ee_edit_others_venues', 'ee_edit_private_venues') |
|
| 185 | + ), |
|
| 186 | + new EE_Meta_Capability_Map_Edit( |
|
| 187 | + 'ee_edit_registration', |
|
| 188 | + array('Registration', '', 'ee_edit_others_registrations', '') |
|
| 189 | + ), |
|
| 190 | + new EE_Meta_Capability_Map_Edit( |
|
| 191 | + 'ee_edit_checkin', |
|
| 192 | + array('Registration', '', 'ee_edit_others_checkins', '') |
|
| 193 | + ), |
|
| 194 | + new EE_Meta_Capability_Map_Messages_Cap( |
|
| 195 | + 'ee_edit_message', |
|
| 196 | + array('Message_Template_Group', '', 'ee_edit_others_messages', 'ee_edit_global_messages') |
|
| 197 | + ), |
|
| 198 | + new EE_Meta_Capability_Map_Edit( |
|
| 199 | + 'ee_edit_default_ticket', |
|
| 200 | + array('Ticket', '', 'ee_edit_others_default_tickets', '') |
|
| 201 | + ), |
|
| 202 | + new EE_Meta_Capability_Map_Registration_Form_Cap( |
|
| 203 | + 'ee_edit_question', |
|
| 204 | + array('Question', '', '', 'ee_edit_system_questions') |
|
| 205 | + ), |
|
| 206 | + new EE_Meta_Capability_Map_Registration_Form_Cap( |
|
| 207 | + 'ee_edit_question_group', |
|
| 208 | + array('Question_Group', '', '', 'ee_edit_system_question_groups') |
|
| 209 | + ), |
|
| 210 | + new EE_Meta_Capability_Map_Edit( |
|
| 211 | + 'ee_edit_payment_method', |
|
| 212 | + array('Payment_Method', '', 'ee_edit_others_payment_methods', '') |
|
| 213 | + ), |
|
| 214 | + // reads |
|
| 215 | + new EE_Meta_Capability_Map_Read( |
|
| 216 | + 'ee_read_event', |
|
| 217 | + array('Event', '', 'ee_read_others_events', 'ee_read_private_events') |
|
| 218 | + ), |
|
| 219 | + new EE_Meta_Capability_Map_Read( |
|
| 220 | + 'ee_read_venue', |
|
| 221 | + array('Venue', '', 'ee_read_others_venues', 'ee_read_private_venues') |
|
| 222 | + ), |
|
| 223 | + new EE_Meta_Capability_Map_Read( |
|
| 224 | + 'ee_read_registration', |
|
| 225 | + array('Registration', '', 'ee_read_others_registrations', '') |
|
| 226 | + ), |
|
| 227 | + new EE_Meta_Capability_Map_Read( |
|
| 228 | + 'ee_read_checkin', |
|
| 229 | + array('Registration', '', 'ee_read_others_checkins', '') |
|
| 230 | + ), |
|
| 231 | + new EE_Meta_Capability_Map_Messages_Cap( |
|
| 232 | + 'ee_read_message', |
|
| 233 | + array('Message_Template_Group', '', 'ee_read_others_messages', 'ee_read_global_messages') |
|
| 234 | + ), |
|
| 235 | + new EE_Meta_Capability_Map_Read( |
|
| 236 | + 'ee_read_default_ticket', |
|
| 237 | + array('Ticket', '', 'ee_read_others_default_tickets', '') |
|
| 238 | + ), |
|
| 239 | + new EE_Meta_Capability_Map_Read( |
|
| 240 | + 'ee_read_payment_method', |
|
| 241 | + array('Payment_Method', '', 'ee_read_others_payment_methods', '') |
|
| 242 | + ), |
|
| 243 | + // deletes |
|
| 244 | + new EE_Meta_Capability_Map_Delete( |
|
| 245 | + 'ee_delete_event', |
|
| 246 | + array( |
|
| 247 | + 'Event', |
|
| 248 | + 'ee_delete_published_events', |
|
| 249 | + 'ee_delete_others_events', |
|
| 250 | + 'ee_delete_private_events', |
|
| 251 | + ) |
|
| 252 | + ), |
|
| 253 | + new EE_Meta_Capability_Map_Delete( |
|
| 254 | + 'ee_delete_venue', |
|
| 255 | + array( |
|
| 256 | + 'Venue', |
|
| 257 | + 'ee_delete_published_venues', |
|
| 258 | + 'ee_delete_others_venues', |
|
| 259 | + 'ee_delete_private_venues', |
|
| 260 | + ) |
|
| 261 | + ), |
|
| 262 | + new EE_Meta_Capability_Map_Delete( |
|
| 263 | + 'ee_delete_registration', |
|
| 264 | + array('Registration', '', 'ee_delete_others_registrations', '') |
|
| 265 | + ), |
|
| 266 | + new EE_Meta_Capability_Map_Delete( |
|
| 267 | + 'ee_delete_checkin', |
|
| 268 | + array('Registration', '', 'ee_delete_others_checkins', '') |
|
| 269 | + ), |
|
| 270 | + new EE_Meta_Capability_Map_Messages_Cap( |
|
| 271 | + 'ee_delete_message', |
|
| 272 | + array('Message_Template_Group', '', 'ee_delete_others_messages', 'ee_delete_global_messages') |
|
| 273 | + ), |
|
| 274 | + new EE_Meta_Capability_Map_Delete( |
|
| 275 | + 'ee_delete_default_ticket', |
|
| 276 | + array('Ticket', '', 'ee_delete_others_default_tickets', '') |
|
| 277 | + ), |
|
| 278 | + new EE_Meta_Capability_Map_Registration_Form_Cap( |
|
| 279 | + 'ee_delete_question', |
|
| 280 | + array('Question', '', '', 'delete_system_questions') |
|
| 281 | + ), |
|
| 282 | + new EE_Meta_Capability_Map_Registration_Form_Cap( |
|
| 283 | + 'ee_delete_question_group', |
|
| 284 | + array('Question_Group', '', '', 'delete_system_question_groups') |
|
| 285 | + ), |
|
| 286 | + new EE_Meta_Capability_Map_Delete( |
|
| 287 | + 'ee_delete_payment_method', |
|
| 288 | + array('Payment_Method', '', 'ee_delete_others_payment_methods', '') |
|
| 289 | + ), |
|
| 290 | + ); |
|
| 291 | + } |
|
| 292 | + return $default_meta_caps; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 298 | + * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 299 | + * |
|
| 300 | + * The actual logic is carried out by implementer classes in their definition of _map_meta_caps. |
|
| 301 | + * |
|
| 302 | + * @since 4.5.0 |
|
| 303 | + * @see wp-includes/capabilities.php |
|
| 304 | + * |
|
| 305 | + * @param array $caps actual users capabilities |
|
| 306 | + * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 307 | + * @param int $user_id The user id |
|
| 308 | + * @param array $args Adds context to the cap. Typically the object ID. |
|
| 309 | + * @return array actual users capabilities |
|
| 310 | + * @throws EE_Error |
|
| 311 | + */ |
|
| 312 | + public function map_meta_caps($caps, $cap, $user_id, $args) |
|
| 313 | + { |
|
| 314 | + if (did_action('AHEE__EE_System__load_espresso_addons__complete')) { |
|
| 315 | + // loop through our _meta_caps array |
|
| 316 | + foreach ($this->_meta_caps as $meta_map) { |
|
| 317 | + if (! $meta_map instanceof EE_Meta_Capability_Map) { |
|
| 318 | + continue; |
|
| 319 | + } |
|
| 320 | + // don't load models if there is no object ID in the args |
|
| 321 | + if (! empty($args[0])) { |
|
| 322 | + $meta_map->ensure_is_model(); |
|
| 323 | + } |
|
| 324 | + $caps = $meta_map->map_meta_caps($caps, $cap, $user_id, $args); |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + return $caps; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * This sets up and returns the initial capabilities map for Event Espresso |
|
| 333 | + * Note this array is filtered. |
|
| 334 | + * It is assumed that all available EE capabilities are assigned to the administrator role. |
|
| 335 | + * |
|
| 336 | + * @since 4.5.0 |
|
| 337 | + * |
|
| 338 | + * @return array |
|
| 339 | + */ |
|
| 340 | + private function _init_caps_map() |
|
| 341 | + { |
|
| 342 | + return apply_filters( |
|
| 343 | + 'FHEE__EE_Capabilities__init_caps_map__caps', |
|
| 344 | + array( |
|
| 345 | + 'administrator' => array( |
|
| 346 | + // basic access |
|
| 347 | + 'ee_read_ee', |
|
| 348 | + // gateways |
|
| 349 | + /** |
|
| 350 | + * note that with payment method capabilities, although we've implemented |
|
| 351 | + * capability mapping which will be used for accessing payment methods owned by |
|
| 352 | + * other users. This is not fully implemented yet in the payment method ui. |
|
| 353 | + * Currently only the "plural" caps are in active use. |
|
| 354 | + * (Specific payment method caps are in use as well). |
|
| 355 | + **/ |
|
| 356 | + 'ee_manage_gateways', |
|
| 357 | + 'ee_read_payment_methods', |
|
| 358 | + 'ee_read_others_payment_methods', |
|
| 359 | + 'ee_edit_payment_methods', |
|
| 360 | + 'ee_edit_others_payment_methods', |
|
| 361 | + 'ee_delete_payment_methods', |
|
| 362 | + // events |
|
| 363 | + 'ee_publish_events', |
|
| 364 | + 'ee_read_private_events', |
|
| 365 | + 'ee_read_others_events', |
|
| 366 | + 'ee_read_events', |
|
| 367 | + 'ee_edit_events', |
|
| 368 | + 'ee_edit_published_events', |
|
| 369 | + 'ee_edit_others_events', |
|
| 370 | + 'ee_edit_private_events', |
|
| 371 | + 'ee_delete_published_events', |
|
| 372 | + 'ee_delete_private_events', |
|
| 373 | + 'ee_delete_events', |
|
| 374 | + 'ee_delete_others_events', |
|
| 375 | + // event categories |
|
| 376 | + 'ee_manage_event_categories', |
|
| 377 | + 'ee_edit_event_category', |
|
| 378 | + 'ee_delete_event_category', |
|
| 379 | + 'ee_assign_event_category', |
|
| 380 | + // venues |
|
| 381 | + 'ee_publish_venues', |
|
| 382 | + 'ee_read_venues', |
|
| 383 | + 'ee_read_others_venues', |
|
| 384 | + 'ee_read_private_venues', |
|
| 385 | + 'ee_edit_venues', |
|
| 386 | + 'ee_edit_others_venues', |
|
| 387 | + 'ee_edit_published_venues', |
|
| 388 | + 'ee_edit_private_venues', |
|
| 389 | + 'ee_delete_venues', |
|
| 390 | + 'ee_delete_others_venues', |
|
| 391 | + 'ee_delete_private_venues', |
|
| 392 | + 'ee_delete_published_venues', |
|
| 393 | + // venue categories |
|
| 394 | + 'ee_manage_venue_categories', |
|
| 395 | + 'ee_edit_venue_category', |
|
| 396 | + 'ee_delete_venue_category', |
|
| 397 | + 'ee_assign_venue_category', |
|
| 398 | + // contacts |
|
| 399 | + 'ee_read_contacts', |
|
| 400 | + 'ee_edit_contacts', |
|
| 401 | + 'ee_delete_contacts', |
|
| 402 | + // registrations |
|
| 403 | + 'ee_read_registrations', |
|
| 404 | + 'ee_read_others_registrations', |
|
| 405 | + 'ee_edit_registrations', |
|
| 406 | + 'ee_edit_others_registrations', |
|
| 407 | + 'ee_delete_registrations', |
|
| 408 | + 'ee_delete_others_registrations', |
|
| 409 | + // checkins |
|
| 410 | + 'ee_read_others_checkins', |
|
| 411 | + 'ee_read_checkins', |
|
| 412 | + 'ee_edit_checkins', |
|
| 413 | + 'ee_edit_others_checkins', |
|
| 414 | + 'ee_delete_checkins', |
|
| 415 | + 'ee_delete_others_checkins', |
|
| 416 | + // transactions && payments |
|
| 417 | + 'ee_read_transaction', |
|
| 418 | + 'ee_read_transactions', |
|
| 419 | + 'ee_edit_payments', |
|
| 420 | + 'ee_delete_payments', |
|
| 421 | + // messages |
|
| 422 | + 'ee_read_messages', |
|
| 423 | + 'ee_read_others_messages', |
|
| 424 | + 'ee_read_global_messages', |
|
| 425 | + 'ee_edit_global_messages', |
|
| 426 | + 'ee_edit_messages', |
|
| 427 | + 'ee_edit_others_messages', |
|
| 428 | + 'ee_delete_messages', |
|
| 429 | + 'ee_delete_others_messages', |
|
| 430 | + 'ee_delete_global_messages', |
|
| 431 | + 'ee_send_message', |
|
| 432 | + // tickets |
|
| 433 | + 'ee_read_default_tickets', |
|
| 434 | + 'ee_read_others_default_tickets', |
|
| 435 | + 'ee_edit_default_tickets', |
|
| 436 | + 'ee_edit_others_default_tickets', |
|
| 437 | + 'ee_delete_default_tickets', |
|
| 438 | + 'ee_delete_others_default_tickets', |
|
| 439 | + // prices |
|
| 440 | + 'ee_edit_default_price', |
|
| 441 | + 'ee_edit_default_prices', |
|
| 442 | + 'ee_delete_default_price', |
|
| 443 | + 'ee_delete_default_prices', |
|
| 444 | + 'ee_edit_default_price_type', |
|
| 445 | + 'ee_edit_default_price_types', |
|
| 446 | + 'ee_delete_default_price_type', |
|
| 447 | + 'ee_delete_default_price_types', |
|
| 448 | + 'ee_read_default_prices', |
|
| 449 | + 'ee_read_default_price_types', |
|
| 450 | + // registration form |
|
| 451 | + 'ee_edit_questions', |
|
| 452 | + 'ee_edit_system_questions', |
|
| 453 | + 'ee_read_questions', |
|
| 454 | + 'ee_delete_questions', |
|
| 455 | + 'ee_edit_question_groups', |
|
| 456 | + 'ee_read_question_groups', |
|
| 457 | + 'ee_edit_system_question_groups', |
|
| 458 | + 'ee_delete_question_groups', |
|
| 459 | + // event_type taxonomy |
|
| 460 | + 'ee_assign_event_type', |
|
| 461 | + 'ee_manage_event_types', |
|
| 462 | + 'ee_edit_event_type', |
|
| 463 | + 'ee_delete_event_type', |
|
| 464 | + ), |
|
| 465 | + 'ee_events_administrator' => array( |
|
| 466 | + // core wp caps |
|
| 467 | + 'read', |
|
| 468 | + 'read_private_pages', |
|
| 469 | + 'read_private_posts', |
|
| 470 | + 'edit_users', |
|
| 471 | + 'edit_posts', |
|
| 472 | + 'edit_pages', |
|
| 473 | + 'edit_published_posts', |
|
| 474 | + 'edit_published_pages', |
|
| 475 | + 'edit_private_pages', |
|
| 476 | + 'edit_private_posts', |
|
| 477 | + 'edit_others_posts', |
|
| 478 | + 'edit_others_pages', |
|
| 479 | + 'publish_posts', |
|
| 480 | + 'publish_pages', |
|
| 481 | + 'delete_posts', |
|
| 482 | + 'delete_pages', |
|
| 483 | + 'delete_private_pages', |
|
| 484 | + 'delete_private_posts', |
|
| 485 | + 'delete_published_pages', |
|
| 486 | + 'delete_published_posts', |
|
| 487 | + 'delete_others_posts', |
|
| 488 | + 'delete_others_pages', |
|
| 489 | + 'manage_categories', |
|
| 490 | + 'manage_links', |
|
| 491 | + 'moderate_comments', |
|
| 492 | + 'unfiltered_html', |
|
| 493 | + 'upload_files', |
|
| 494 | + 'export', |
|
| 495 | + 'import', |
|
| 496 | + 'list_users', |
|
| 497 | + 'level_1', // required if user with this role shows up in author dropdowns |
|
| 498 | + // basic ee access |
|
| 499 | + 'ee_read_ee', |
|
| 500 | + // events |
|
| 501 | + 'ee_publish_events', |
|
| 502 | + 'ee_read_private_events', |
|
| 503 | + 'ee_read_others_events', |
|
| 504 | + 'ee_read_event', |
|
| 505 | + 'ee_read_events', |
|
| 506 | + 'ee_edit_event', |
|
| 507 | + 'ee_edit_events', |
|
| 508 | + 'ee_edit_published_events', |
|
| 509 | + 'ee_edit_others_events', |
|
| 510 | + 'ee_edit_private_events', |
|
| 511 | + 'ee_delete_published_events', |
|
| 512 | + 'ee_delete_private_events', |
|
| 513 | + 'ee_delete_event', |
|
| 514 | + 'ee_delete_events', |
|
| 515 | + 'ee_delete_others_events', |
|
| 516 | + // event categories |
|
| 517 | + 'ee_manage_event_categories', |
|
| 518 | + 'ee_edit_event_category', |
|
| 519 | + 'ee_delete_event_category', |
|
| 520 | + 'ee_assign_event_category', |
|
| 521 | + // venues |
|
| 522 | + 'ee_publish_venues', |
|
| 523 | + 'ee_read_venue', |
|
| 524 | + 'ee_read_venues', |
|
| 525 | + 'ee_read_others_venues', |
|
| 526 | + 'ee_read_private_venues', |
|
| 527 | + 'ee_edit_venue', |
|
| 528 | + 'ee_edit_venues', |
|
| 529 | + 'ee_edit_others_venues', |
|
| 530 | + 'ee_edit_published_venues', |
|
| 531 | + 'ee_edit_private_venues', |
|
| 532 | + 'ee_delete_venue', |
|
| 533 | + 'ee_delete_venues', |
|
| 534 | + 'ee_delete_others_venues', |
|
| 535 | + 'ee_delete_private_venues', |
|
| 536 | + 'ee_delete_published_venues', |
|
| 537 | + // venue categories |
|
| 538 | + 'ee_manage_venue_categories', |
|
| 539 | + 'ee_edit_venue_category', |
|
| 540 | + 'ee_delete_venue_category', |
|
| 541 | + 'ee_assign_venue_category', |
|
| 542 | + // contacts |
|
| 543 | + 'ee_read_contacts', |
|
| 544 | + 'ee_edit_contacts', |
|
| 545 | + 'ee_delete_contacts', |
|
| 546 | + // registrations |
|
| 547 | + 'ee_read_registrations', |
|
| 548 | + 'ee_read_others_registrations', |
|
| 549 | + 'ee_edit_registration', |
|
| 550 | + 'ee_edit_registrations', |
|
| 551 | + 'ee_edit_others_registrations', |
|
| 552 | + 'ee_delete_registration', |
|
| 553 | + 'ee_delete_registrations', |
|
| 554 | + 'ee_delete_others_registrations', |
|
| 555 | + // checkins |
|
| 556 | + 'ee_read_others_checkins', |
|
| 557 | + 'ee_read_checkins', |
|
| 558 | + 'ee_edit_checkins', |
|
| 559 | + 'ee_edit_others_checkins', |
|
| 560 | + 'ee_delete_checkins', |
|
| 561 | + 'ee_delete_others_checkins', |
|
| 562 | + // transactions && payments |
|
| 563 | + 'ee_read_transaction', |
|
| 564 | + 'ee_read_transactions', |
|
| 565 | + 'ee_edit_payments', |
|
| 566 | + 'ee_delete_payments', |
|
| 567 | + // messages |
|
| 568 | + 'ee_read_messages', |
|
| 569 | + 'ee_read_others_messages', |
|
| 570 | + 'ee_read_global_messages', |
|
| 571 | + 'ee_edit_global_messages', |
|
| 572 | + 'ee_edit_messages', |
|
| 573 | + 'ee_edit_others_messages', |
|
| 574 | + 'ee_delete_messages', |
|
| 575 | + 'ee_delete_others_messages', |
|
| 576 | + 'ee_delete_global_messages', |
|
| 577 | + 'ee_send_message', |
|
| 578 | + // tickets |
|
| 579 | + 'ee_read_default_tickets', |
|
| 580 | + 'ee_read_others_default_tickets', |
|
| 581 | + 'ee_edit_default_tickets', |
|
| 582 | + 'ee_edit_others_default_tickets', |
|
| 583 | + 'ee_delete_default_tickets', |
|
| 584 | + 'ee_delete_others_default_tickets', |
|
| 585 | + // prices |
|
| 586 | + 'ee_edit_default_price', |
|
| 587 | + 'ee_edit_default_prices', |
|
| 588 | + 'ee_delete_default_price', |
|
| 589 | + 'ee_delete_default_prices', |
|
| 590 | + 'ee_edit_default_price_type', |
|
| 591 | + 'ee_edit_default_price_types', |
|
| 592 | + 'ee_delete_default_price_type', |
|
| 593 | + 'ee_delete_default_price_types', |
|
| 594 | + 'ee_read_default_prices', |
|
| 595 | + 'ee_read_default_price_types', |
|
| 596 | + // registration form |
|
| 597 | + 'ee_edit_questions', |
|
| 598 | + 'ee_edit_system_questions', |
|
| 599 | + 'ee_read_questions', |
|
| 600 | + 'ee_delete_questions', |
|
| 601 | + 'ee_edit_question_groups', |
|
| 602 | + 'ee_read_question_groups', |
|
| 603 | + 'ee_edit_system_question_groups', |
|
| 604 | + 'ee_delete_question_groups', |
|
| 605 | + // event_type taxonomy |
|
| 606 | + 'ee_assign_event_type', |
|
| 607 | + 'ee_manage_event_types', |
|
| 608 | + 'ee_edit_event_type', |
|
| 609 | + 'ee_delete_event_type', |
|
| 610 | + ), |
|
| 611 | + ) |
|
| 612 | + ); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + |
|
| 616 | + /** |
|
| 617 | + * @return bool |
|
| 618 | + * @throws EE_Error |
|
| 619 | + */ |
|
| 620 | + private function setupCapabilitiesMap() |
|
| 621 | + { |
|
| 622 | + // if the initialization process hasn't even started, then we need to call init_caps() |
|
| 623 | + if ($this->initialized === null) { |
|
| 624 | + return $this->init_caps(); |
|
| 625 | + } |
|
| 626 | + // unless resetting, get caps from db if we haven't already |
|
| 627 | + $this->capabilities_map = $this->reset || ! empty($this->capabilities_map) |
|
| 628 | + ? $this->capabilities_map |
|
| 629 | + : get_option(self::option_name, array()); |
|
| 630 | + return true; |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + |
|
| 634 | + /** |
|
| 635 | + * @param bool $update |
|
| 636 | + * @return bool |
|
| 637 | + */ |
|
| 638 | + private function updateCapabilitiesMap($update = true) |
|
| 639 | + { |
|
| 640 | + return $update ? update_option(self::option_name, $this->capabilities_map) : false; |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + |
|
| 644 | + /** |
|
| 645 | + * Adds capabilities to roles. |
|
| 646 | + * |
|
| 647 | + * @since 4.9.42 |
|
| 648 | + * @param array $capabilities_to_add array of capabilities to add, indexed by roles. |
|
| 649 | + * Note that this should ONLY be called on activation hook |
|
| 650 | + * otherwise the caps will be added on every request. |
|
| 651 | + * @return bool |
|
| 652 | + * @throws \EE_Error |
|
| 653 | + */ |
|
| 654 | + public function addCaps(array $capabilities_to_add) |
|
| 655 | + { |
|
| 656 | + // don't do anything if the capabilities map can not be initialized |
|
| 657 | + if (! $this->setupCapabilitiesMap()) { |
|
| 658 | + return false; |
|
| 659 | + } |
|
| 660 | + // and filter the array so others can get in on the fun during resets |
|
| 661 | + $capabilities_to_add = apply_filters( |
|
| 662 | + 'FHEE__EE_Capabilities__addCaps__capabilities_to_add', |
|
| 663 | + $capabilities_to_add, |
|
| 664 | + $this->reset, |
|
| 665 | + $this->capabilities_map |
|
| 666 | + ); |
|
| 667 | + $update_capabilities_map = false; |
|
| 668 | + // if not reset, see what caps are new for each role. if they're new, add them. |
|
| 669 | + foreach ($capabilities_to_add as $role => $caps_for_role) { |
|
| 670 | + if (is_array($caps_for_role)) { |
|
| 671 | + foreach ($caps_for_role as $cap) { |
|
| 672 | + if (! $this->capHasBeenAddedToRole($role, $cap) |
|
| 673 | + && $this->add_cap_to_role($role, $cap, true, false) |
|
| 674 | + ) { |
|
| 675 | + $update_capabilities_map = true; |
|
| 676 | + } |
|
| 677 | + } |
|
| 678 | + } |
|
| 679 | + } |
|
| 680 | + // now let's just save the cap that has been set but only if there's been a change. |
|
| 681 | + $updated = $this->updateCapabilitiesMap($update_capabilities_map); |
|
| 682 | + $this->flushWpUser($updated); |
|
| 683 | + do_action('AHEE__EE_Capabilities__addCaps__complete', $this->capabilities_map, $updated); |
|
| 684 | + return $updated; |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + |
|
| 688 | + /** |
|
| 689 | + * Loops through the capabilities map and removes the role caps specified by the incoming array |
|
| 690 | + * |
|
| 691 | + * @param array $caps_map map of capabilities to be removed (indexed by roles) |
|
| 692 | + * @return bool |
|
| 693 | + * @throws \EE_Error |
|
| 694 | + */ |
|
| 695 | + public function removeCaps($caps_map) |
|
| 696 | + { |
|
| 697 | + // don't do anything if the capabilities map can not be initialized |
|
| 698 | + if (! $this->setupCapabilitiesMap()) { |
|
| 699 | + return false; |
|
| 700 | + } |
|
| 701 | + $update_capabilities_map = false; |
|
| 702 | + foreach ($caps_map as $role => $caps_for_role) { |
|
| 703 | + if (is_array($caps_for_role)) { |
|
| 704 | + foreach ($caps_for_role as $cap) { |
|
| 705 | + if ($this->capHasBeenAddedToRole($role, $cap) |
|
| 706 | + && $this->remove_cap_from_role($role, $cap, false) |
|
| 707 | + ) { |
|
| 708 | + $update_capabilities_map = true; |
|
| 709 | + } |
|
| 710 | + } |
|
| 711 | + } |
|
| 712 | + } |
|
| 713 | + // maybe resave the caps |
|
| 714 | + $updated = $this->updateCapabilitiesMap($update_capabilities_map); |
|
| 715 | + $this->flushWpUser($updated); |
|
| 716 | + return $updated; |
|
| 717 | + } |
|
| 718 | + |
|
| 719 | + |
|
| 720 | + /** |
|
| 721 | + * This ensures that the WP User object cached on the $current_user global in WP has the latest capabilities from |
|
| 722 | + * the roles on that user. |
|
| 723 | + * |
|
| 724 | + * @param bool $flush Default is to flush the WP_User object. If false, then this method effectively does nothing. |
|
| 725 | + */ |
|
| 726 | + private function flushWpUser($flush = true) |
|
| 727 | + { |
|
| 728 | + if ($flush) { |
|
| 729 | + $user = wp_get_current_user(); |
|
| 730 | + if ($user instanceof WP_User) { |
|
| 731 | + $user->get_role_caps(); |
|
| 732 | + } |
|
| 733 | + } |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + |
|
| 737 | + /** |
|
| 738 | + * This method sets a capability on a role. Note this should only be done on activation, or if you have something |
|
| 739 | + * specific to prevent the cap from being added on every page load (adding caps are persistent to the db). Note. |
|
| 740 | + * this is a wrapper for $wp_role->add_cap() |
|
| 741 | + * |
|
| 742 | + * @see wp-includes/capabilities.php |
|
| 743 | + * @since 4.5.0 |
|
| 744 | + * @param string|WP_Role $role A WordPress role the capability is being added to |
|
| 745 | + * @param string $cap The capability being added to the role |
|
| 746 | + * @param bool $grant Whether to grant access to this cap on this role. |
|
| 747 | + * @param bool $update_capabilities_map |
|
| 748 | + * @return bool |
|
| 749 | + * @throws \EE_Error |
|
| 750 | + */ |
|
| 751 | + public function add_cap_to_role($role, $cap, $grant = true, $update_capabilities_map = true) |
|
| 752 | + { |
|
| 753 | + // capture incoming value for $role because we may need it to create a new WP_Role |
|
| 754 | + $orig_role = $role; |
|
| 755 | + $role = $role instanceof WP_Role ? $role : get_role($role); |
|
| 756 | + // if the role isn't available then we create it. |
|
| 757 | + if (! $role instanceof WP_Role) { |
|
| 758 | + // if a plugin wants to create a specific role name then they should create the role before |
|
| 759 | + // EE_Capabilities does. Otherwise this function will create the role name from the slug: |
|
| 760 | + // - removes any `ee_` namespacing from the start of the slug. |
|
| 761 | + // - replaces `_` with ` ` (empty space). |
|
| 762 | + // - sentence case on the resulting string. |
|
| 763 | + $role_label = ucwords(str_replace(array('ee_', '_'), array('', ' '), $orig_role)); |
|
| 764 | + $role = add_role($orig_role, $role_label); |
|
| 765 | + } |
|
| 766 | + if ($role instanceof WP_Role) { |
|
| 767 | + // don't do anything if the capabilities map can not be initialized |
|
| 768 | + if (! $this->setupCapabilitiesMap()) { |
|
| 769 | + return false; |
|
| 770 | + } |
|
| 771 | + if (! $this->capHasBeenAddedToRole($role->name, $cap)) { |
|
| 772 | + $role->add_cap($cap, $grant); |
|
| 773 | + $this->capabilities_map[ $role->name ][] = $cap; |
|
| 774 | + $this->updateCapabilitiesMap($update_capabilities_map); |
|
| 775 | + return true; |
|
| 776 | + } |
|
| 777 | + } |
|
| 778 | + return false; |
|
| 779 | + } |
|
| 780 | + |
|
| 781 | + |
|
| 782 | + /** |
|
| 783 | + * Functions similarly to add_cap_to_role except removes cap from given role. |
|
| 784 | + * Wrapper for $wp_role->remove_cap() |
|
| 785 | + * |
|
| 786 | + * @see wp-includes/capabilities.php |
|
| 787 | + * @since 4.5.0 |
|
| 788 | + * @param string|WP_Role $role A WordPress role the capability is being removed from. |
|
| 789 | + * @param string $cap The capability being removed |
|
| 790 | + * @param bool $update_capabilities_map |
|
| 791 | + * @return bool |
|
| 792 | + * @throws \EE_Error |
|
| 793 | + */ |
|
| 794 | + public function remove_cap_from_role($role, $cap, $update_capabilities_map = true) |
|
| 795 | + { |
|
| 796 | + // don't do anything if the capabilities map can not be initialized |
|
| 797 | + if (! $this->setupCapabilitiesMap()) { |
|
| 798 | + return false; |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + $role = $role instanceof WP_Role ? $role : get_role($role); |
|
| 802 | + if ($role instanceof WP_Role && $index = $this->capHasBeenAddedToRole($role->name, $cap, true)) { |
|
| 803 | + $role->remove_cap($cap); |
|
| 804 | + unset($this->capabilities_map[ $role->name ][ $index ]); |
|
| 805 | + $this->updateCapabilitiesMap($update_capabilities_map); |
|
| 806 | + return true; |
|
| 807 | + } |
|
| 808 | + return false; |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + |
|
| 812 | + /** |
|
| 813 | + * @param string $role_name |
|
| 814 | + * @param string $cap |
|
| 815 | + * @param bool $get_index |
|
| 816 | + * @return bool|mixed |
|
| 817 | + */ |
|
| 818 | + private function capHasBeenAddedToRole($role_name = '', $cap = '', $get_index = false) |
|
| 819 | + { |
|
| 820 | + if (isset($this->capabilities_map[ $role_name ]) |
|
| 821 | + && ($index = array_search($cap, $this->capabilities_map[ $role_name ], true)) !== false |
|
| 822 | + ) { |
|
| 823 | + return $get_index ? $index : true; |
|
| 824 | + } |
|
| 825 | + return false; |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + |
|
| 829 | + /** |
|
| 830 | + * Wrapper for the native WP current_user_can() method. |
|
| 831 | + * This is provided as a handy method for a couple things: |
|
| 832 | + * 1. Using the context string it allows for targeted filtering by addons for a specific check (without having to |
|
| 833 | + * write those filters wherever current_user_can is called). |
|
| 834 | + * 2. Explicit passing of $id from a given context ( useful in the cases of map_meta_cap filters ) |
|
| 835 | + * |
|
| 836 | + * @since 4.5.0 |
|
| 837 | + * |
|
| 838 | + * @param string $cap The cap being checked. |
|
| 839 | + * @param string $context The context where the current_user_can is being called from. |
|
| 840 | + * @param int $id Optional. Id for item where current_user_can is being called from (used in map_meta_cap() |
|
| 841 | + * filters. |
|
| 842 | + * |
|
| 843 | + * @return bool Whether user can or not. |
|
| 844 | + */ |
|
| 845 | + public function current_user_can($cap, $context, $id = 0) |
|
| 846 | + { |
|
| 847 | + // apply filters (both a global on just the cap, and context specific. Global overrides context specific) |
|
| 848 | + $filtered_cap = apply_filters('FHEE__EE_Capabilities__current_user_can__cap__' . $context, $cap, $id); |
|
| 849 | + $filtered_cap = apply_filters( |
|
| 850 | + 'FHEE__EE_Capabilities__current_user_can__cap', |
|
| 851 | + $filtered_cap, |
|
| 852 | + $context, |
|
| 853 | + $cap, |
|
| 854 | + $id |
|
| 855 | + ); |
|
| 856 | + return ! empty($id) |
|
| 857 | + ? current_user_can($filtered_cap, $id) |
|
| 858 | + : current_user_can($filtered_cap); |
|
| 859 | + } |
|
| 860 | + |
|
| 861 | + |
|
| 862 | + /** |
|
| 863 | + * This is a wrapper for the WP user_can() function and follows the same style as the other wrappers in this class. |
|
| 864 | + * |
|
| 865 | + * @param int|WP_User $user Either the user_id or a WP_User object |
|
| 866 | + * @param string $cap The capability string being checked |
|
| 867 | + * @param string $context The context where the user_can is being called from (used in filters). |
|
| 868 | + * @param int $id Optional. Id for item where user_can is being called from ( used in map_meta_cap() |
|
| 869 | + * filters) |
|
| 870 | + * |
|
| 871 | + * @return bool Whether user can or not. |
|
| 872 | + */ |
|
| 873 | + public function user_can($user, $cap, $context, $id = 0) |
|
| 874 | + { |
|
| 875 | + // apply filters (both a global on just the cap, and context specific. Global overrides context specific) |
|
| 876 | + $filtered_cap = apply_filters('FHEE__EE_Capabilities__user_can__cap__' . $context, $cap, $user, $id); |
|
| 877 | + $filtered_cap = apply_filters( |
|
| 878 | + 'FHEE__EE_Capabilities__user_can__cap', |
|
| 879 | + $filtered_cap, |
|
| 880 | + $context, |
|
| 881 | + $cap, |
|
| 882 | + $user, |
|
| 883 | + $id |
|
| 884 | + ); |
|
| 885 | + return ! empty($id) |
|
| 886 | + ? user_can($user, $filtered_cap, $id) |
|
| 887 | + : user_can($user, $filtered_cap); |
|
| 888 | + } |
|
| 889 | + |
|
| 890 | + |
|
| 891 | + /** |
|
| 892 | + * Wrapper for the native WP current_user_can_for_blog() method. |
|
| 893 | + * This is provided as a handy method for a couple things: |
|
| 894 | + * 1. Using the context string it allows for targeted filtering by addons for a specific check (without having to |
|
| 895 | + * write those filters wherever current_user_can is called). |
|
| 896 | + * 2. Explicit passing of $id from a given context ( useful in the cases of map_meta_cap filters ) |
|
| 897 | + * |
|
| 898 | + * @since 4.5.0 |
|
| 899 | + * |
|
| 900 | + * @param int $blog_id The blog id that is being checked for. |
|
| 901 | + * @param string $cap The cap being checked. |
|
| 902 | + * @param string $context The context where the current_user_can is being called from. |
|
| 903 | + * @param int $id Optional. Id for item where current_user_can is being called from (used in map_meta_cap() |
|
| 904 | + * filters. |
|
| 905 | + * |
|
| 906 | + * @return bool Whether user can or not. |
|
| 907 | + */ |
|
| 908 | + public function current_user_can_for_blog($blog_id, $cap, $context, $id = 0) |
|
| 909 | + { |
|
| 910 | + $user_can = ! empty($id) |
|
| 911 | + ? current_user_can_for_blog($blog_id, $cap, $id) |
|
| 912 | + : current_user_can($blog_id, $cap); |
|
| 913 | + // apply filters (both a global on just the cap, and context specific. Global overrides context specific) |
|
| 914 | + $user_can = apply_filters( |
|
| 915 | + 'FHEE__EE_Capabilities__current_user_can_for_blog__user_can__' . $context, |
|
| 916 | + $user_can, |
|
| 917 | + $blog_id, |
|
| 918 | + $cap, |
|
| 919 | + $id |
|
| 920 | + ); |
|
| 921 | + $user_can = apply_filters( |
|
| 922 | + 'FHEE__EE_Capabilities__current_user_can_for_blog__user_can', |
|
| 923 | + $user_can, |
|
| 924 | + $context, |
|
| 925 | + $blog_id, |
|
| 926 | + $cap, |
|
| 927 | + $id |
|
| 928 | + ); |
|
| 929 | + return $user_can; |
|
| 930 | + } |
|
| 931 | + |
|
| 932 | + |
|
| 933 | + /** |
|
| 934 | + * This helper method just returns an array of registered EE capabilities. |
|
| 935 | + * |
|
| 936 | + * @since 4.5.0 |
|
| 937 | + * @param string $role If empty then the entire role/capability map is returned. |
|
| 938 | + * Otherwise just the capabilities for the given role are returned. |
|
| 939 | + * @return array |
|
| 940 | + * @throws EE_Error |
|
| 941 | + */ |
|
| 942 | + public function get_ee_capabilities($role = 'administrator') |
|
| 943 | + { |
|
| 944 | + if (! $this->initialized) { |
|
| 945 | + $this->init_caps(); |
|
| 946 | + } |
|
| 947 | + if (empty($role)) { |
|
| 948 | + return $this->capabilities_map; |
|
| 949 | + } |
|
| 950 | + return isset($this->capabilities_map[ $role ]) |
|
| 951 | + ? $this->capabilities_map[ $role ] |
|
| 952 | + : array(); |
|
| 953 | + } |
|
| 954 | + |
|
| 955 | + |
|
| 956 | + /** |
|
| 957 | + * @deprecated 4.9.42 |
|
| 958 | + * @param bool $reset If you need to reset Event Espresso's capabilities, |
|
| 959 | + * then please use the init_caps() method with the "$reset" parameter set to "true" |
|
| 960 | + * @param array $caps_map Optional. |
|
| 961 | + * Can be used to send a custom map of roles and capabilities for setting them up. |
|
| 962 | + * Note that this should ONLY be called on activation hook or some other one-time |
|
| 963 | + * task otherwise the caps will be added on every request. |
|
| 964 | + * @return void |
|
| 965 | + * @throws EE_Error |
|
| 966 | + */ |
|
| 967 | + public function init_role_caps($reset = false, $caps_map = array()) |
|
| 968 | + { |
|
| 969 | + // If this method is called directly and reset is set as 'true', |
|
| 970 | + // then display a doing it wrong notice, because we want resets to go through init_caps() |
|
| 971 | + // to guarantee that everything is set up correctly. |
|
| 972 | + // This prevents the capabilities map getting reset incorrectly by direct calls to this method. |
|
| 973 | + if ($reset) { |
|
| 974 | + EE_Error::doing_it_wrong( |
|
| 975 | + __METHOD__, |
|
| 976 | + sprintf( |
|
| 977 | + esc_html__( |
|
| 978 | + 'The "%1$s" parameter for the "%2$s" method is deprecated. If you need to reset Event Espresso\'s capabilities, then please use the "%3$s" method with the "%1$s" parameter set to "%4$s".', |
|
| 979 | + 'event_espresso' |
|
| 980 | + ), |
|
| 981 | + '$reset', |
|
| 982 | + __METHOD__ . '()', |
|
| 983 | + 'EE_Capabilities::init_caps()', |
|
| 984 | + 'true' |
|
| 985 | + ), |
|
| 986 | + '4.9.42', |
|
| 987 | + '5.0.0' |
|
| 988 | + ); |
|
| 989 | + } |
|
| 990 | + $this->addCaps($caps_map); |
|
| 991 | + } |
|
| 992 | 992 | } |
| 993 | 993 | |
| 994 | 994 | |
@@ -1005,142 +1005,142 @@ discard block |
||
| 1005 | 1005 | abstract class EE_Meta_Capability_Map |
| 1006 | 1006 | { |
| 1007 | 1007 | |
| 1008 | - public $meta_cap; |
|
| 1009 | - |
|
| 1010 | - /** |
|
| 1011 | - * @var EEM_Base |
|
| 1012 | - */ |
|
| 1013 | - protected $_model; |
|
| 1014 | - |
|
| 1015 | - protected $_model_name; |
|
| 1016 | - |
|
| 1017 | - public $published_cap = ''; |
|
| 1018 | - |
|
| 1019 | - public $others_cap = ''; |
|
| 1020 | - |
|
| 1021 | - public $private_cap = ''; |
|
| 1022 | - |
|
| 1023 | - |
|
| 1024 | - /** |
|
| 1025 | - * constructor. |
|
| 1026 | - * Receives the setup arguments for the map. |
|
| 1027 | - * |
|
| 1028 | - * @since 4.5.0 |
|
| 1029 | - * |
|
| 1030 | - * @param string $meta_cap What meta capability is this mapping. |
|
| 1031 | - * @param array $map_values array { |
|
| 1032 | - * //array of values that MUST match a count of 4. It's okay to send an empty string for |
|
| 1033 | - * capabilities that don't get mapped to. |
|
| 1034 | - * |
|
| 1035 | - * @type $map_values [0] string A string representing the model name. Required. String's |
|
| 1036 | - * should always be used when Menu Maps are registered via the |
|
| 1037 | - * plugin API as models are not allowed to be instantiated when |
|
| 1038 | - * in maintenance mode 2 (migrations). |
|
| 1039 | - * @type $map_values [1] string represents the capability used for published. Optional. |
|
| 1040 | - * @type $map_values [2] string represents the capability used for "others". Optional. |
|
| 1041 | - * @type $map_values [3] string represents the capability used for private. Optional. |
|
| 1042 | - * } |
|
| 1043 | - * @throws EE_Error |
|
| 1044 | - */ |
|
| 1045 | - public function __construct($meta_cap, $map_values) |
|
| 1046 | - { |
|
| 1047 | - $this->meta_cap = $meta_cap; |
|
| 1048 | - // verify there are four args in the $map_values array; |
|
| 1049 | - if (count($map_values) !== 4) { |
|
| 1050 | - throw new EE_Error( |
|
| 1051 | - sprintf( |
|
| 1052 | - __( |
|
| 1053 | - 'Incoming $map_values array should have a count of four values in it. This is what was given: %s', |
|
| 1054 | - 'event_espresso' |
|
| 1055 | - ), |
|
| 1056 | - '<br>' . print_r($map_values, true) |
|
| 1057 | - ) |
|
| 1058 | - ); |
|
| 1059 | - } |
|
| 1060 | - // set properties |
|
| 1061 | - $this->_model = null; |
|
| 1062 | - $this->_model_name = $map_values[0]; |
|
| 1063 | - $this->published_cap = (string) $map_values[1]; |
|
| 1064 | - $this->others_cap = (string) $map_values[2]; |
|
| 1065 | - $this->private_cap = (string) $map_values[3]; |
|
| 1066 | - } |
|
| 1067 | - |
|
| 1068 | - /** |
|
| 1069 | - * Makes it so this object stops filtering caps |
|
| 1070 | - */ |
|
| 1071 | - public function remove_filters() |
|
| 1072 | - { |
|
| 1073 | - remove_filter('map_meta_cap', array($this, 'map_meta_caps'), 10); |
|
| 1074 | - } |
|
| 1075 | - |
|
| 1076 | - |
|
| 1077 | - /** |
|
| 1078 | - * This method ensures that the $model property is converted from the model name string to a proper EEM_Base class |
|
| 1079 | - * |
|
| 1080 | - * @since 4.5.0 |
|
| 1081 | - * @throws EE_Error |
|
| 1082 | - * |
|
| 1083 | - * @return void |
|
| 1084 | - */ |
|
| 1085 | - public function ensure_is_model() |
|
| 1086 | - { |
|
| 1087 | - // is it already instantiated? |
|
| 1088 | - if ($this->_model instanceof EEM_Base) { |
|
| 1089 | - return; |
|
| 1090 | - } |
|
| 1091 | - // ensure model name is string |
|
| 1092 | - $this->_model_name = (string) $this->_model_name; |
|
| 1093 | - // error proof if the name has EEM in it |
|
| 1094 | - $this->_model_name = str_replace('EEM', '', $this->_model_name); |
|
| 1095 | - $this->_model = EE_Registry::instance()->load_model($this->_model_name); |
|
| 1096 | - if (! $this->_model instanceof EEM_Base) { |
|
| 1097 | - throw new EE_Error( |
|
| 1098 | - sprintf( |
|
| 1099 | - __( |
|
| 1100 | - 'This string passed in to %s to represent a EEM_Base model class was not able to be used to instantiate the class. Please ensure that the string is a match for the EEM_Base model name (not including the EEM_ part). This was given: %s', |
|
| 1101 | - 'event_espresso' |
|
| 1102 | - ), |
|
| 1103 | - get_class($this), |
|
| 1104 | - $this->_model |
|
| 1105 | - ) |
|
| 1106 | - ); |
|
| 1107 | - } |
|
| 1108 | - } |
|
| 1109 | - |
|
| 1110 | - |
|
| 1111 | - /** |
|
| 1112 | - * |
|
| 1113 | - * @see EE_Meta_Capability_Map::_map_meta_caps() for docs on params. |
|
| 1114 | - * @since 4.6.x |
|
| 1115 | - * |
|
| 1116 | - * @param $caps |
|
| 1117 | - * @param $cap |
|
| 1118 | - * @param $user_id |
|
| 1119 | - * @param $args |
|
| 1120 | - * |
|
| 1121 | - * @return array |
|
| 1122 | - */ |
|
| 1123 | - public function map_meta_caps($caps, $cap, $user_id, $args) |
|
| 1124 | - { |
|
| 1125 | - return $this->_map_meta_caps($caps, $cap, $user_id, $args); |
|
| 1126 | - } |
|
| 1127 | - |
|
| 1128 | - |
|
| 1129 | - /** |
|
| 1130 | - * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 1131 | - * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 1132 | - * |
|
| 1133 | - * @since 4.5.0 |
|
| 1134 | - * @see wp-includes/capabilities.php |
|
| 1135 | - * |
|
| 1136 | - * @param array $caps actual users capabilities |
|
| 1137 | - * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 1138 | - * @param int $user_id The user id |
|
| 1139 | - * @param array $args Adds context to the cap. Typically the object ID. |
|
| 1140 | - * |
|
| 1141 | - * @return array actual users capabilities |
|
| 1142 | - */ |
|
| 1143 | - abstract protected function _map_meta_caps($caps, $cap, $user_id, $args); |
|
| 1008 | + public $meta_cap; |
|
| 1009 | + |
|
| 1010 | + /** |
|
| 1011 | + * @var EEM_Base |
|
| 1012 | + */ |
|
| 1013 | + protected $_model; |
|
| 1014 | + |
|
| 1015 | + protected $_model_name; |
|
| 1016 | + |
|
| 1017 | + public $published_cap = ''; |
|
| 1018 | + |
|
| 1019 | + public $others_cap = ''; |
|
| 1020 | + |
|
| 1021 | + public $private_cap = ''; |
|
| 1022 | + |
|
| 1023 | + |
|
| 1024 | + /** |
|
| 1025 | + * constructor. |
|
| 1026 | + * Receives the setup arguments for the map. |
|
| 1027 | + * |
|
| 1028 | + * @since 4.5.0 |
|
| 1029 | + * |
|
| 1030 | + * @param string $meta_cap What meta capability is this mapping. |
|
| 1031 | + * @param array $map_values array { |
|
| 1032 | + * //array of values that MUST match a count of 4. It's okay to send an empty string for |
|
| 1033 | + * capabilities that don't get mapped to. |
|
| 1034 | + * |
|
| 1035 | + * @type $map_values [0] string A string representing the model name. Required. String's |
|
| 1036 | + * should always be used when Menu Maps are registered via the |
|
| 1037 | + * plugin API as models are not allowed to be instantiated when |
|
| 1038 | + * in maintenance mode 2 (migrations). |
|
| 1039 | + * @type $map_values [1] string represents the capability used for published. Optional. |
|
| 1040 | + * @type $map_values [2] string represents the capability used for "others". Optional. |
|
| 1041 | + * @type $map_values [3] string represents the capability used for private. Optional. |
|
| 1042 | + * } |
|
| 1043 | + * @throws EE_Error |
|
| 1044 | + */ |
|
| 1045 | + public function __construct($meta_cap, $map_values) |
|
| 1046 | + { |
|
| 1047 | + $this->meta_cap = $meta_cap; |
|
| 1048 | + // verify there are four args in the $map_values array; |
|
| 1049 | + if (count($map_values) !== 4) { |
|
| 1050 | + throw new EE_Error( |
|
| 1051 | + sprintf( |
|
| 1052 | + __( |
|
| 1053 | + 'Incoming $map_values array should have a count of four values in it. This is what was given: %s', |
|
| 1054 | + 'event_espresso' |
|
| 1055 | + ), |
|
| 1056 | + '<br>' . print_r($map_values, true) |
|
| 1057 | + ) |
|
| 1058 | + ); |
|
| 1059 | + } |
|
| 1060 | + // set properties |
|
| 1061 | + $this->_model = null; |
|
| 1062 | + $this->_model_name = $map_values[0]; |
|
| 1063 | + $this->published_cap = (string) $map_values[1]; |
|
| 1064 | + $this->others_cap = (string) $map_values[2]; |
|
| 1065 | + $this->private_cap = (string) $map_values[3]; |
|
| 1066 | + } |
|
| 1067 | + |
|
| 1068 | + /** |
|
| 1069 | + * Makes it so this object stops filtering caps |
|
| 1070 | + */ |
|
| 1071 | + public function remove_filters() |
|
| 1072 | + { |
|
| 1073 | + remove_filter('map_meta_cap', array($this, 'map_meta_caps'), 10); |
|
| 1074 | + } |
|
| 1075 | + |
|
| 1076 | + |
|
| 1077 | + /** |
|
| 1078 | + * This method ensures that the $model property is converted from the model name string to a proper EEM_Base class |
|
| 1079 | + * |
|
| 1080 | + * @since 4.5.0 |
|
| 1081 | + * @throws EE_Error |
|
| 1082 | + * |
|
| 1083 | + * @return void |
|
| 1084 | + */ |
|
| 1085 | + public function ensure_is_model() |
|
| 1086 | + { |
|
| 1087 | + // is it already instantiated? |
|
| 1088 | + if ($this->_model instanceof EEM_Base) { |
|
| 1089 | + return; |
|
| 1090 | + } |
|
| 1091 | + // ensure model name is string |
|
| 1092 | + $this->_model_name = (string) $this->_model_name; |
|
| 1093 | + // error proof if the name has EEM in it |
|
| 1094 | + $this->_model_name = str_replace('EEM', '', $this->_model_name); |
|
| 1095 | + $this->_model = EE_Registry::instance()->load_model($this->_model_name); |
|
| 1096 | + if (! $this->_model instanceof EEM_Base) { |
|
| 1097 | + throw new EE_Error( |
|
| 1098 | + sprintf( |
|
| 1099 | + __( |
|
| 1100 | + 'This string passed in to %s to represent a EEM_Base model class was not able to be used to instantiate the class. Please ensure that the string is a match for the EEM_Base model name (not including the EEM_ part). This was given: %s', |
|
| 1101 | + 'event_espresso' |
|
| 1102 | + ), |
|
| 1103 | + get_class($this), |
|
| 1104 | + $this->_model |
|
| 1105 | + ) |
|
| 1106 | + ); |
|
| 1107 | + } |
|
| 1108 | + } |
|
| 1109 | + |
|
| 1110 | + |
|
| 1111 | + /** |
|
| 1112 | + * |
|
| 1113 | + * @see EE_Meta_Capability_Map::_map_meta_caps() for docs on params. |
|
| 1114 | + * @since 4.6.x |
|
| 1115 | + * |
|
| 1116 | + * @param $caps |
|
| 1117 | + * @param $cap |
|
| 1118 | + * @param $user_id |
|
| 1119 | + * @param $args |
|
| 1120 | + * |
|
| 1121 | + * @return array |
|
| 1122 | + */ |
|
| 1123 | + public function map_meta_caps($caps, $cap, $user_id, $args) |
|
| 1124 | + { |
|
| 1125 | + return $this->_map_meta_caps($caps, $cap, $user_id, $args); |
|
| 1126 | + } |
|
| 1127 | + |
|
| 1128 | + |
|
| 1129 | + /** |
|
| 1130 | + * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 1131 | + * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 1132 | + * |
|
| 1133 | + * @since 4.5.0 |
|
| 1134 | + * @see wp-includes/capabilities.php |
|
| 1135 | + * |
|
| 1136 | + * @param array $caps actual users capabilities |
|
| 1137 | + * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 1138 | + * @param int $user_id The user id |
|
| 1139 | + * @param array $args Adds context to the cap. Typically the object ID. |
|
| 1140 | + * |
|
| 1141 | + * @return array actual users capabilities |
|
| 1142 | + */ |
|
| 1143 | + abstract protected function _map_meta_caps($caps, $cap, $user_id, $args); |
|
| 1144 | 1144 | } |
| 1145 | 1145 | |
| 1146 | 1146 | |
@@ -1156,81 +1156,81 @@ discard block |
||
| 1156 | 1156 | class EE_Meta_Capability_Map_Edit extends EE_Meta_Capability_Map |
| 1157 | 1157 | { |
| 1158 | 1158 | |
| 1159 | - /** |
|
| 1160 | - * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 1161 | - * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 1162 | - * |
|
| 1163 | - * @since 4.5.0 |
|
| 1164 | - * @see wp-includes/capabilities.php |
|
| 1165 | - * |
|
| 1166 | - * @param array $caps actual users capabilities |
|
| 1167 | - * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 1168 | - * @param int $user_id The user id |
|
| 1169 | - * @param array $args Adds context to the cap. Typically the object ID. |
|
| 1170 | - * |
|
| 1171 | - * @return array actual users capabilities |
|
| 1172 | - */ |
|
| 1173 | - protected function _map_meta_caps($caps, $cap, $user_id, $args) |
|
| 1174 | - { |
|
| 1175 | - // only process if we're checking our mapped_cap |
|
| 1176 | - if ($cap !== $this->meta_cap) { |
|
| 1177 | - return $caps; |
|
| 1178 | - } |
|
| 1179 | - |
|
| 1180 | - // okay it is a meta cap so let's first remove that cap from the $caps array. |
|
| 1181 | - if (($key = array_search($cap, $caps)) !== false) { |
|
| 1182 | - unset($caps[ $key ]); |
|
| 1183 | - } |
|
| 1184 | - |
|
| 1185 | - // cast $user_id to int for later explicit comparisons |
|
| 1186 | - $user_id = (int) $user_id; |
|
| 1187 | - |
|
| 1188 | - /** @var EE_Base_Class $obj */ |
|
| 1189 | - $obj = ! empty($args[0]) ? $this->_model->get_one_by_ID($args[0]) : null; |
|
| 1190 | - // if no obj then let's just do cap |
|
| 1191 | - if (! $obj instanceof EE_Base_Class) { |
|
| 1192 | - $caps[] = 'do_not_allow'; |
|
| 1193 | - return $caps; |
|
| 1194 | - } |
|
| 1195 | - $caps[] = $cap . 's'; |
|
| 1196 | - if ($obj instanceof EE_CPT_Base) { |
|
| 1197 | - // if the item author is set and the user is the author... |
|
| 1198 | - if ($obj->wp_user() && $user_id === $obj->wp_user()) { |
|
| 1199 | - // if obj is published... |
|
| 1200 | - if ($obj->status() === 'publish') { |
|
| 1201 | - $caps[] = $this->published_cap; |
|
| 1202 | - } |
|
| 1203 | - } else { |
|
| 1204 | - // the user is trying to edit someone else's obj |
|
| 1205 | - if (! empty($this->others_cap)) { |
|
| 1206 | - $caps[] = $this->others_cap; |
|
| 1207 | - } |
|
| 1208 | - if (! empty($this->published_cap) && $obj->status() === 'publish') { |
|
| 1209 | - $caps[] = $this->published_cap; |
|
| 1210 | - } elseif (! empty($this->private_cap) && $obj->status() === 'private') { |
|
| 1211 | - $caps[] = $this->private_cap; |
|
| 1212 | - } |
|
| 1213 | - } |
|
| 1214 | - } else { |
|
| 1215 | - // not a cpt object so handled differently |
|
| 1216 | - $has_cap = false; |
|
| 1217 | - try { |
|
| 1218 | - $has_cap = method_exists($obj, 'wp_user') |
|
| 1219 | - && $obj->wp_user() |
|
| 1220 | - && $obj->wp_user() === $user_id; |
|
| 1221 | - } catch (Exception $e) { |
|
| 1222 | - if (WP_DEBUG) { |
|
| 1223 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1224 | - } |
|
| 1225 | - } |
|
| 1226 | - if (! $has_cap) { |
|
| 1227 | - if (! empty($this->others_cap)) { |
|
| 1228 | - $caps[] = $this->others_cap; |
|
| 1229 | - } |
|
| 1230 | - } |
|
| 1231 | - } |
|
| 1232 | - return $caps; |
|
| 1233 | - } |
|
| 1159 | + /** |
|
| 1160 | + * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 1161 | + * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 1162 | + * |
|
| 1163 | + * @since 4.5.0 |
|
| 1164 | + * @see wp-includes/capabilities.php |
|
| 1165 | + * |
|
| 1166 | + * @param array $caps actual users capabilities |
|
| 1167 | + * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 1168 | + * @param int $user_id The user id |
|
| 1169 | + * @param array $args Adds context to the cap. Typically the object ID. |
|
| 1170 | + * |
|
| 1171 | + * @return array actual users capabilities |
|
| 1172 | + */ |
|
| 1173 | + protected function _map_meta_caps($caps, $cap, $user_id, $args) |
|
| 1174 | + { |
|
| 1175 | + // only process if we're checking our mapped_cap |
|
| 1176 | + if ($cap !== $this->meta_cap) { |
|
| 1177 | + return $caps; |
|
| 1178 | + } |
|
| 1179 | + |
|
| 1180 | + // okay it is a meta cap so let's first remove that cap from the $caps array. |
|
| 1181 | + if (($key = array_search($cap, $caps)) !== false) { |
|
| 1182 | + unset($caps[ $key ]); |
|
| 1183 | + } |
|
| 1184 | + |
|
| 1185 | + // cast $user_id to int for later explicit comparisons |
|
| 1186 | + $user_id = (int) $user_id; |
|
| 1187 | + |
|
| 1188 | + /** @var EE_Base_Class $obj */ |
|
| 1189 | + $obj = ! empty($args[0]) ? $this->_model->get_one_by_ID($args[0]) : null; |
|
| 1190 | + // if no obj then let's just do cap |
|
| 1191 | + if (! $obj instanceof EE_Base_Class) { |
|
| 1192 | + $caps[] = 'do_not_allow'; |
|
| 1193 | + return $caps; |
|
| 1194 | + } |
|
| 1195 | + $caps[] = $cap . 's'; |
|
| 1196 | + if ($obj instanceof EE_CPT_Base) { |
|
| 1197 | + // if the item author is set and the user is the author... |
|
| 1198 | + if ($obj->wp_user() && $user_id === $obj->wp_user()) { |
|
| 1199 | + // if obj is published... |
|
| 1200 | + if ($obj->status() === 'publish') { |
|
| 1201 | + $caps[] = $this->published_cap; |
|
| 1202 | + } |
|
| 1203 | + } else { |
|
| 1204 | + // the user is trying to edit someone else's obj |
|
| 1205 | + if (! empty($this->others_cap)) { |
|
| 1206 | + $caps[] = $this->others_cap; |
|
| 1207 | + } |
|
| 1208 | + if (! empty($this->published_cap) && $obj->status() === 'publish') { |
|
| 1209 | + $caps[] = $this->published_cap; |
|
| 1210 | + } elseif (! empty($this->private_cap) && $obj->status() === 'private') { |
|
| 1211 | + $caps[] = $this->private_cap; |
|
| 1212 | + } |
|
| 1213 | + } |
|
| 1214 | + } else { |
|
| 1215 | + // not a cpt object so handled differently |
|
| 1216 | + $has_cap = false; |
|
| 1217 | + try { |
|
| 1218 | + $has_cap = method_exists($obj, 'wp_user') |
|
| 1219 | + && $obj->wp_user() |
|
| 1220 | + && $obj->wp_user() === $user_id; |
|
| 1221 | + } catch (Exception $e) { |
|
| 1222 | + if (WP_DEBUG) { |
|
| 1223 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1224 | + } |
|
| 1225 | + } |
|
| 1226 | + if (! $has_cap) { |
|
| 1227 | + if (! empty($this->others_cap)) { |
|
| 1228 | + $caps[] = $this->others_cap; |
|
| 1229 | + } |
|
| 1230 | + } |
|
| 1231 | + } |
|
| 1232 | + return $caps; |
|
| 1233 | + } |
|
| 1234 | 1234 | } |
| 1235 | 1235 | |
| 1236 | 1236 | |
@@ -1247,24 +1247,24 @@ discard block |
||
| 1247 | 1247 | class EE_Meta_Capability_Map_Delete extends EE_Meta_Capability_Map_Edit |
| 1248 | 1248 | { |
| 1249 | 1249 | |
| 1250 | - /** |
|
| 1251 | - * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 1252 | - * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 1253 | - * |
|
| 1254 | - * @since 4.5.0 |
|
| 1255 | - * @see wp-includes/capabilities.php |
|
| 1256 | - * |
|
| 1257 | - * @param array $caps actual users capabilities |
|
| 1258 | - * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 1259 | - * @param int $user_id The user id |
|
| 1260 | - * @param array $args Adds context to the cap. Typically the object ID. |
|
| 1261 | - * |
|
| 1262 | - * @return array actual users capabilities |
|
| 1263 | - */ |
|
| 1264 | - protected function _map_meta_caps($caps, $cap, $user_id, $args) |
|
| 1265 | - { |
|
| 1266 | - return parent::_map_meta_caps($caps, $cap, $user_id, $args); |
|
| 1267 | - } |
|
| 1250 | + /** |
|
| 1251 | + * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 1252 | + * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 1253 | + * |
|
| 1254 | + * @since 4.5.0 |
|
| 1255 | + * @see wp-includes/capabilities.php |
|
| 1256 | + * |
|
| 1257 | + * @param array $caps actual users capabilities |
|
| 1258 | + * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 1259 | + * @param int $user_id The user id |
|
| 1260 | + * @param array $args Adds context to the cap. Typically the object ID. |
|
| 1261 | + * |
|
| 1262 | + * @return array actual users capabilities |
|
| 1263 | + */ |
|
| 1264 | + protected function _map_meta_caps($caps, $cap, $user_id, $args) |
|
| 1265 | + { |
|
| 1266 | + return parent::_map_meta_caps($caps, $cap, $user_id, $args); |
|
| 1267 | + } |
|
| 1268 | 1268 | } |
| 1269 | 1269 | |
| 1270 | 1270 | |
@@ -1280,85 +1280,85 @@ discard block |
||
| 1280 | 1280 | class EE_Meta_Capability_Map_Read extends EE_Meta_Capability_Map |
| 1281 | 1281 | { |
| 1282 | 1282 | |
| 1283 | - /** |
|
| 1284 | - * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 1285 | - * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 1286 | - * |
|
| 1287 | - * @since 4.5.0 |
|
| 1288 | - * @see wp-includes/capabilities.php |
|
| 1289 | - * |
|
| 1290 | - * @param array $caps actual users capabilities |
|
| 1291 | - * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 1292 | - * @param int $user_id The user id |
|
| 1293 | - * @param array $args Adds context to the cap. Typically the object ID. |
|
| 1294 | - * |
|
| 1295 | - * @return array actual users capabilities |
|
| 1296 | - */ |
|
| 1297 | - protected function _map_meta_caps($caps, $cap, $user_id, $args) |
|
| 1298 | - { |
|
| 1299 | - // only process if we're checking our mapped cap; |
|
| 1300 | - if ($cap !== $this->meta_cap) { |
|
| 1301 | - return $caps; |
|
| 1302 | - } |
|
| 1303 | - |
|
| 1304 | - // okay it is a meta cap so let's first remove that cap from the $caps array. |
|
| 1305 | - if (($key = array_search($cap, $caps)) !== false) { |
|
| 1306 | - unset($caps[ $key ]); |
|
| 1307 | - } |
|
| 1308 | - |
|
| 1309 | - // cast $user_id to int for later explicit comparisons |
|
| 1310 | - $user_id = (int) $user_id; |
|
| 1311 | - |
|
| 1312 | - $obj = ! empty($args[0]) ? $this->_model->get_one_by_ID($args[0]) : null; |
|
| 1313 | - // if no obj then let's just do cap |
|
| 1314 | - if (! $obj instanceof EE_Base_Class) { |
|
| 1315 | - $caps[] = 'do_not_allow'; |
|
| 1316 | - return $caps; |
|
| 1317 | - } |
|
| 1318 | - |
|
| 1319 | - $caps[] = $cap . 's'; |
|
| 1320 | - if ($obj instanceof EE_CPT_Base) { |
|
| 1321 | - $status_obj = get_post_status_object($obj->status()); |
|
| 1322 | - if ($status_obj->public) { |
|
| 1323 | - return $caps; |
|
| 1324 | - } |
|
| 1325 | - // if the item author is set and the user is not the author... |
|
| 1326 | - if ($obj->wp_user() && $obj->wp_user() !== $user_id) { |
|
| 1327 | - if (! empty($this->others_cap)) { |
|
| 1328 | - $caps[] = $this->others_cap; |
|
| 1329 | - } |
|
| 1330 | - } |
|
| 1331 | - // yes this means that if users created the private post, they are able to see it regardless of private cap. |
|
| 1332 | - if ($status_obj->private |
|
| 1333 | - && ! empty($this->private_cap) |
|
| 1334 | - && $obj->wp_user() !== $user_id |
|
| 1335 | - ) { |
|
| 1336 | - // the user is trying to view a private object for an object they don't own. |
|
| 1337 | - $caps[] = $this->private_cap; |
|
| 1338 | - } |
|
| 1339 | - } else { |
|
| 1340 | - // not a cpt object so handled differently |
|
| 1341 | - $has_cap = false; |
|
| 1342 | - try { |
|
| 1343 | - $has_cap = method_exists($obj, 'wp_user') |
|
| 1344 | - && $obj->wp_user() |
|
| 1345 | - && $obj->wp_user() === $user_id; |
|
| 1346 | - } catch (Exception $e) { |
|
| 1347 | - if (WP_DEBUG) { |
|
| 1348 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1349 | - } |
|
| 1350 | - } |
|
| 1351 | - if (! $has_cap) { |
|
| 1352 | - if (! empty($this->private_cap)) { |
|
| 1353 | - $caps[] = $this->private_cap; |
|
| 1354 | - } |
|
| 1355 | - if (! empty($this->others_cap)) { |
|
| 1356 | - $caps[] = $this->others_cap; |
|
| 1357 | - } |
|
| 1358 | - } |
|
| 1359 | - } |
|
| 1360 | - return $caps; |
|
| 1361 | - } |
|
| 1283 | + /** |
|
| 1284 | + * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 1285 | + * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 1286 | + * |
|
| 1287 | + * @since 4.5.0 |
|
| 1288 | + * @see wp-includes/capabilities.php |
|
| 1289 | + * |
|
| 1290 | + * @param array $caps actual users capabilities |
|
| 1291 | + * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 1292 | + * @param int $user_id The user id |
|
| 1293 | + * @param array $args Adds context to the cap. Typically the object ID. |
|
| 1294 | + * |
|
| 1295 | + * @return array actual users capabilities |
|
| 1296 | + */ |
|
| 1297 | + protected function _map_meta_caps($caps, $cap, $user_id, $args) |
|
| 1298 | + { |
|
| 1299 | + // only process if we're checking our mapped cap; |
|
| 1300 | + if ($cap !== $this->meta_cap) { |
|
| 1301 | + return $caps; |
|
| 1302 | + } |
|
| 1303 | + |
|
| 1304 | + // okay it is a meta cap so let's first remove that cap from the $caps array. |
|
| 1305 | + if (($key = array_search($cap, $caps)) !== false) { |
|
| 1306 | + unset($caps[ $key ]); |
|
| 1307 | + } |
|
| 1308 | + |
|
| 1309 | + // cast $user_id to int for later explicit comparisons |
|
| 1310 | + $user_id = (int) $user_id; |
|
| 1311 | + |
|
| 1312 | + $obj = ! empty($args[0]) ? $this->_model->get_one_by_ID($args[0]) : null; |
|
| 1313 | + // if no obj then let's just do cap |
|
| 1314 | + if (! $obj instanceof EE_Base_Class) { |
|
| 1315 | + $caps[] = 'do_not_allow'; |
|
| 1316 | + return $caps; |
|
| 1317 | + } |
|
| 1318 | + |
|
| 1319 | + $caps[] = $cap . 's'; |
|
| 1320 | + if ($obj instanceof EE_CPT_Base) { |
|
| 1321 | + $status_obj = get_post_status_object($obj->status()); |
|
| 1322 | + if ($status_obj->public) { |
|
| 1323 | + return $caps; |
|
| 1324 | + } |
|
| 1325 | + // if the item author is set and the user is not the author... |
|
| 1326 | + if ($obj->wp_user() && $obj->wp_user() !== $user_id) { |
|
| 1327 | + if (! empty($this->others_cap)) { |
|
| 1328 | + $caps[] = $this->others_cap; |
|
| 1329 | + } |
|
| 1330 | + } |
|
| 1331 | + // yes this means that if users created the private post, they are able to see it regardless of private cap. |
|
| 1332 | + if ($status_obj->private |
|
| 1333 | + && ! empty($this->private_cap) |
|
| 1334 | + && $obj->wp_user() !== $user_id |
|
| 1335 | + ) { |
|
| 1336 | + // the user is trying to view a private object for an object they don't own. |
|
| 1337 | + $caps[] = $this->private_cap; |
|
| 1338 | + } |
|
| 1339 | + } else { |
|
| 1340 | + // not a cpt object so handled differently |
|
| 1341 | + $has_cap = false; |
|
| 1342 | + try { |
|
| 1343 | + $has_cap = method_exists($obj, 'wp_user') |
|
| 1344 | + && $obj->wp_user() |
|
| 1345 | + && $obj->wp_user() === $user_id; |
|
| 1346 | + } catch (Exception $e) { |
|
| 1347 | + if (WP_DEBUG) { |
|
| 1348 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1349 | + } |
|
| 1350 | + } |
|
| 1351 | + if (! $has_cap) { |
|
| 1352 | + if (! empty($this->private_cap)) { |
|
| 1353 | + $caps[] = $this->private_cap; |
|
| 1354 | + } |
|
| 1355 | + if (! empty($this->others_cap)) { |
|
| 1356 | + $caps[] = $this->others_cap; |
|
| 1357 | + } |
|
| 1358 | + } |
|
| 1359 | + } |
|
| 1360 | + return $caps; |
|
| 1361 | + } |
|
| 1362 | 1362 | } |
| 1363 | 1363 | |
| 1364 | 1364 | |
@@ -1375,56 +1375,56 @@ discard block |
||
| 1375 | 1375 | class EE_Meta_Capability_Map_Messages_Cap extends EE_Meta_Capability_Map |
| 1376 | 1376 | { |
| 1377 | 1377 | |
| 1378 | - /** |
|
| 1379 | - * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 1380 | - * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 1381 | - * |
|
| 1382 | - * @since 4.5.0 |
|
| 1383 | - * @see wp-includes/capabilities.php |
|
| 1384 | - * |
|
| 1385 | - * @param array $caps actual users capabilities |
|
| 1386 | - * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 1387 | - * @param int $user_id The user id |
|
| 1388 | - * @param array $args Adds context to the cap. Typically the object ID. |
|
| 1389 | - * |
|
| 1390 | - * @return array actual users capabilities |
|
| 1391 | - */ |
|
| 1392 | - protected function _map_meta_caps($caps, $cap, $user_id, $args) |
|
| 1393 | - { |
|
| 1394 | - // only process if we're checking our mapped_cap |
|
| 1395 | - if ($cap !== $this->meta_cap) { |
|
| 1396 | - return $caps; |
|
| 1397 | - } |
|
| 1398 | - |
|
| 1399 | - // okay it is a meta cap so let's first remove that cap from the $caps array. |
|
| 1400 | - if (($key = array_search($cap, $caps)) !== false) { |
|
| 1401 | - unset($caps[ $key ]); |
|
| 1402 | - } |
|
| 1403 | - |
|
| 1404 | - // cast $user_id to int for later explicit comparisons |
|
| 1405 | - $user_id = (int) $user_id; |
|
| 1406 | - |
|
| 1407 | - $obj = ! empty($args[0]) ? $this->_model->get_one_by_ID($args[0]) : null; |
|
| 1408 | - // if no obj then let's just do cap |
|
| 1409 | - if (! $obj instanceof EE_Message_Template_Group) { |
|
| 1410 | - $caps[] = 'do_not_allow'; |
|
| 1411 | - return $caps; |
|
| 1412 | - } |
|
| 1413 | - $caps[] = $cap . 's'; |
|
| 1414 | - $is_global = $obj->is_global(); |
|
| 1415 | - if ($obj->wp_user() && $obj->wp_user() === $user_id) { |
|
| 1416 | - if ($is_global) { |
|
| 1417 | - $caps[] = $this->private_cap; |
|
| 1418 | - } |
|
| 1419 | - } else { |
|
| 1420 | - if ($is_global) { |
|
| 1421 | - $caps[] = $this->private_cap; |
|
| 1422 | - } else { |
|
| 1423 | - $caps[] = $this->others_cap; |
|
| 1424 | - } |
|
| 1425 | - } |
|
| 1426 | - return $caps; |
|
| 1427 | - } |
|
| 1378 | + /** |
|
| 1379 | + * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 1380 | + * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 1381 | + * |
|
| 1382 | + * @since 4.5.0 |
|
| 1383 | + * @see wp-includes/capabilities.php |
|
| 1384 | + * |
|
| 1385 | + * @param array $caps actual users capabilities |
|
| 1386 | + * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 1387 | + * @param int $user_id The user id |
|
| 1388 | + * @param array $args Adds context to the cap. Typically the object ID. |
|
| 1389 | + * |
|
| 1390 | + * @return array actual users capabilities |
|
| 1391 | + */ |
|
| 1392 | + protected function _map_meta_caps($caps, $cap, $user_id, $args) |
|
| 1393 | + { |
|
| 1394 | + // only process if we're checking our mapped_cap |
|
| 1395 | + if ($cap !== $this->meta_cap) { |
|
| 1396 | + return $caps; |
|
| 1397 | + } |
|
| 1398 | + |
|
| 1399 | + // okay it is a meta cap so let's first remove that cap from the $caps array. |
|
| 1400 | + if (($key = array_search($cap, $caps)) !== false) { |
|
| 1401 | + unset($caps[ $key ]); |
|
| 1402 | + } |
|
| 1403 | + |
|
| 1404 | + // cast $user_id to int for later explicit comparisons |
|
| 1405 | + $user_id = (int) $user_id; |
|
| 1406 | + |
|
| 1407 | + $obj = ! empty($args[0]) ? $this->_model->get_one_by_ID($args[0]) : null; |
|
| 1408 | + // if no obj then let's just do cap |
|
| 1409 | + if (! $obj instanceof EE_Message_Template_Group) { |
|
| 1410 | + $caps[] = 'do_not_allow'; |
|
| 1411 | + return $caps; |
|
| 1412 | + } |
|
| 1413 | + $caps[] = $cap . 's'; |
|
| 1414 | + $is_global = $obj->is_global(); |
|
| 1415 | + if ($obj->wp_user() && $obj->wp_user() === $user_id) { |
|
| 1416 | + if ($is_global) { |
|
| 1417 | + $caps[] = $this->private_cap; |
|
| 1418 | + } |
|
| 1419 | + } else { |
|
| 1420 | + if ($is_global) { |
|
| 1421 | + $caps[] = $this->private_cap; |
|
| 1422 | + } else { |
|
| 1423 | + $caps[] = $this->others_cap; |
|
| 1424 | + } |
|
| 1425 | + } |
|
| 1426 | + return $caps; |
|
| 1427 | + } |
|
| 1428 | 1428 | } |
| 1429 | 1429 | |
| 1430 | 1430 | |
@@ -1441,40 +1441,40 @@ discard block |
||
| 1441 | 1441 | class EE_Meta_Capability_Map_Registration_Form_Cap extends EE_Meta_Capability_Map |
| 1442 | 1442 | { |
| 1443 | 1443 | |
| 1444 | - /** |
|
| 1445 | - * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 1446 | - * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 1447 | - * |
|
| 1448 | - * @since 4.5.0 |
|
| 1449 | - * @see wp-includes/capabilities.php |
|
| 1450 | - * @param array $caps actual users capabilities |
|
| 1451 | - * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 1452 | - * @param int $user_id The user id |
|
| 1453 | - * @param array $args Adds context to the cap. Typically the object ID. |
|
| 1454 | - * @return array actual users capabilities |
|
| 1455 | - */ |
|
| 1456 | - protected function _map_meta_caps($caps, $cap, $user_id, $args) |
|
| 1457 | - { |
|
| 1458 | - // only process if we're checking our mapped_cap |
|
| 1459 | - if ($cap !== $this->meta_cap) { |
|
| 1460 | - return $caps; |
|
| 1461 | - } |
|
| 1462 | - // okay it is a meta cap so let's first remove that cap from the $caps array. |
|
| 1463 | - if (($key = array_search($cap, $caps)) !== false) { |
|
| 1464 | - unset($caps[ $key ]); |
|
| 1465 | - } |
|
| 1466 | - $obj = ! empty($args[0]) ? $this->_model->get_one_by_ID($args[0]) : null; |
|
| 1467 | - // if no obj then let's just do cap |
|
| 1468 | - if (! $obj instanceof EE_Base_Class) { |
|
| 1469 | - $caps[] = 'do_not_allow'; |
|
| 1470 | - return $caps; |
|
| 1471 | - } |
|
| 1472 | - $caps[] = $cap . 's'; |
|
| 1473 | - $is_system = $obj instanceof EE_Question_Group ? $obj->system_group() : false; |
|
| 1474 | - $is_system = $obj instanceof EE_Question ? $obj->is_system_question() : $is_system; |
|
| 1475 | - if ($is_system) { |
|
| 1476 | - $caps[] = $this->private_cap; |
|
| 1477 | - } |
|
| 1478 | - return $caps; |
|
| 1479 | - } |
|
| 1444 | + /** |
|
| 1445 | + * This is the callback for the wp map_meta_caps() function which allows for ensuring certain caps that act as a |
|
| 1446 | + * "meta" for other caps ( i.e. ee_edit_event is a meta for ee_edit_others_events ) work as expected. |
|
| 1447 | + * |
|
| 1448 | + * @since 4.5.0 |
|
| 1449 | + * @see wp-includes/capabilities.php |
|
| 1450 | + * @param array $caps actual users capabilities |
|
| 1451 | + * @param string $cap initial capability name that is being checked (the "map" key) |
|
| 1452 | + * @param int $user_id The user id |
|
| 1453 | + * @param array $args Adds context to the cap. Typically the object ID. |
|
| 1454 | + * @return array actual users capabilities |
|
| 1455 | + */ |
|
| 1456 | + protected function _map_meta_caps($caps, $cap, $user_id, $args) |
|
| 1457 | + { |
|
| 1458 | + // only process if we're checking our mapped_cap |
|
| 1459 | + if ($cap !== $this->meta_cap) { |
|
| 1460 | + return $caps; |
|
| 1461 | + } |
|
| 1462 | + // okay it is a meta cap so let's first remove that cap from the $caps array. |
|
| 1463 | + if (($key = array_search($cap, $caps)) !== false) { |
|
| 1464 | + unset($caps[ $key ]); |
|
| 1465 | + } |
|
| 1466 | + $obj = ! empty($args[0]) ? $this->_model->get_one_by_ID($args[0]) : null; |
|
| 1467 | + // if no obj then let's just do cap |
|
| 1468 | + if (! $obj instanceof EE_Base_Class) { |
|
| 1469 | + $caps[] = 'do_not_allow'; |
|
| 1470 | + return $caps; |
|
| 1471 | + } |
|
| 1472 | + $caps[] = $cap . 's'; |
|
| 1473 | + $is_system = $obj instanceof EE_Question_Group ? $obj->system_group() : false; |
|
| 1474 | + $is_system = $obj instanceof EE_Question ? $obj->is_system_question() : $is_system; |
|
| 1475 | + if ($is_system) { |
|
| 1476 | + $caps[] = $this->private_cap; |
|
| 1477 | + } |
|
| 1478 | + return $caps; |
|
| 1479 | + } |
|
| 1480 | 1480 | } |
@@ -29,639 +29,639 @@ |
||
| 29 | 29 | abstract class FormHandler implements FormHandlerInterface |
| 30 | 30 | { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * will add opening and closing HTML form tags as well as a submit button |
|
| 34 | - */ |
|
| 35 | - const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * will add opening and closing HTML form tags but NOT a submit button |
|
| 39 | - */ |
|
| 40 | - const ADD_FORM_TAGS_ONLY = 'add_form_tags_only'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * will NOT add opening and closing HTML form tags but will add a submit button |
|
| 44 | - */ |
|
| 45 | - const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only'; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * will NOT add opening and closing HTML form tags NOR a submit button |
|
| 49 | - */ |
|
| 50 | - const DO_NOT_SETUP_FORM = 'do_not_setup_form'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * if set to false, then this form has no displayable content, |
|
| 54 | - * and will only be used for processing data sent passed via GET or POST |
|
| 55 | - * defaults to true ( ie: form has displayable content ) |
|
| 56 | - * |
|
| 57 | - * @var boolean $displayable |
|
| 58 | - */ |
|
| 59 | - private $displayable = true; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var string $form_name |
|
| 63 | - */ |
|
| 64 | - private $form_name; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var string $admin_name |
|
| 68 | - */ |
|
| 69 | - private $admin_name; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var string $slug |
|
| 73 | - */ |
|
| 74 | - private $slug; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var string $submit_btn_text |
|
| 78 | - */ |
|
| 79 | - private $submit_btn_text; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @var string $form_action |
|
| 83 | - */ |
|
| 84 | - private $form_action; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * form params in key value pairs |
|
| 88 | - * can be added to form action URL or as hidden inputs |
|
| 89 | - * |
|
| 90 | - * @var array $form_args |
|
| 91 | - */ |
|
| 92 | - private $form_args = array(); |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * value of one of the string constant above |
|
| 96 | - * |
|
| 97 | - * @var string $form_config |
|
| 98 | - */ |
|
| 99 | - private $form_config; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * whether or not the form was determined to be invalid |
|
| 103 | - * |
|
| 104 | - * @var boolean $form_has_errors |
|
| 105 | - */ |
|
| 106 | - private $form_has_errors; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * the absolute top level form section being used on the page |
|
| 110 | - * |
|
| 111 | - * @var EE_Form_Section_Proper $form |
|
| 112 | - */ |
|
| 113 | - private $form; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @var EE_Registry $registry |
|
| 117 | - */ |
|
| 118 | - protected $registry; |
|
| 119 | - |
|
| 120 | - // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
| 121 | - /** |
|
| 122 | - * Form constructor. |
|
| 123 | - * |
|
| 124 | - * @param string $form_name |
|
| 125 | - * @param string $admin_name |
|
| 126 | - * @param string $slug |
|
| 127 | - * @param string $form_action |
|
| 128 | - * @param string $form_config |
|
| 129 | - * @param EE_Registry $registry |
|
| 130 | - * @throws InvalidDataTypeException |
|
| 131 | - * @throws DomainException |
|
| 132 | - * @throws InvalidArgumentException |
|
| 133 | - */ |
|
| 134 | - public function __construct( |
|
| 135 | - $form_name, |
|
| 136 | - $admin_name, |
|
| 137 | - $slug, |
|
| 138 | - $form_action = '', |
|
| 139 | - $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 140 | - EE_Registry $registry |
|
| 141 | - ) { |
|
| 142 | - $this->setFormName($form_name); |
|
| 143 | - $this->setAdminName($admin_name); |
|
| 144 | - $this->setSlug($slug); |
|
| 145 | - $this->setFormAction($form_action); |
|
| 146 | - $this->setFormConfig($form_config); |
|
| 147 | - $this->setSubmitBtnText(esc_html__('Submit', 'event_espresso')); |
|
| 148 | - $this->registry = $registry; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @return array |
|
| 154 | - */ |
|
| 155 | - public static function getFormConfigConstants() |
|
| 156 | - { |
|
| 157 | - return array( |
|
| 158 | - FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 159 | - FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 160 | - FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 161 | - FormHandler::DO_NOT_SETUP_FORM, |
|
| 162 | - ); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param bool $for_display |
|
| 168 | - * @return EE_Form_Section_Proper |
|
| 169 | - * @throws EE_Error |
|
| 170 | - * @throws LogicException |
|
| 171 | - */ |
|
| 172 | - public function form($for_display = false) |
|
| 173 | - { |
|
| 174 | - if (! $this->formIsValid()) { |
|
| 175 | - return null; |
|
| 176 | - } |
|
| 177 | - if ($for_display) { |
|
| 178 | - $form_config = $this->formConfig(); |
|
| 179 | - if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 180 | - || $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY |
|
| 181 | - ) { |
|
| 182 | - $this->appendSubmitButton(); |
|
| 183 | - $this->clearFormButtonFloats(); |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - return $this->form; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * @return boolean |
|
| 192 | - * @throws LogicException |
|
| 193 | - */ |
|
| 194 | - public function formIsValid() |
|
| 195 | - { |
|
| 196 | - if ($this->form instanceof EE_Form_Section_Proper) { |
|
| 197 | - return true; |
|
| 198 | - } |
|
| 199 | - $form = apply_filters( |
|
| 200 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object', |
|
| 201 | - $this->generate(), |
|
| 202 | - $this |
|
| 203 | - ); |
|
| 204 | - if ($this->verifyForm($form)) { |
|
| 205 | - $this->setForm($form); |
|
| 206 | - } |
|
| 207 | - return true; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * @param EE_Form_Section_Proper|null $form |
|
| 213 | - * @return bool |
|
| 214 | - * @throws LogicException |
|
| 215 | - */ |
|
| 216 | - public function verifyForm(EE_Form_Section_Proper $form = null) |
|
| 217 | - { |
|
| 218 | - $form = $form !== null ? $form : $this->form; |
|
| 219 | - if ($form instanceof EE_Form_Section_Proper) { |
|
| 220 | - return true; |
|
| 221 | - } |
|
| 222 | - throw new LogicException( |
|
| 223 | - sprintf( |
|
| 224 | - esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'), |
|
| 225 | - $this->form_name, |
|
| 226 | - var_export($form, true) |
|
| 227 | - ) |
|
| 228 | - ); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * @param EE_Form_Section_Proper $form |
|
| 234 | - */ |
|
| 235 | - public function setForm(EE_Form_Section_Proper $form) |
|
| 236 | - { |
|
| 237 | - $this->form = $form; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * @return boolean |
|
| 243 | - */ |
|
| 244 | - public function displayable() |
|
| 245 | - { |
|
| 246 | - return $this->displayable; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param boolean $displayable |
|
| 252 | - */ |
|
| 253 | - public function setDisplayable($displayable = false) |
|
| 254 | - { |
|
| 255 | - $this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * a public name for the form that can be displayed on the frontend of a site |
|
| 261 | - * |
|
| 262 | - * @return string |
|
| 263 | - */ |
|
| 264 | - public function formName() |
|
| 265 | - { |
|
| 266 | - return $this->form_name; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * @param string $form_name |
|
| 272 | - * @throws InvalidDataTypeException |
|
| 273 | - */ |
|
| 274 | - public function setFormName($form_name) |
|
| 275 | - { |
|
| 276 | - if (! is_string($form_name)) { |
|
| 277 | - throw new InvalidDataTypeException('$form_name', $form_name, 'string'); |
|
| 278 | - } |
|
| 279 | - $this->form_name = $form_name; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * a public name for the form that can be displayed, but only in the admin |
|
| 285 | - * |
|
| 286 | - * @return string |
|
| 287 | - */ |
|
| 288 | - public function adminName() |
|
| 289 | - { |
|
| 290 | - return $this->admin_name; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * @param string $admin_name |
|
| 296 | - * @throws InvalidDataTypeException |
|
| 297 | - */ |
|
| 298 | - public function setAdminName($admin_name) |
|
| 299 | - { |
|
| 300 | - if (! is_string($admin_name)) { |
|
| 301 | - throw new InvalidDataTypeException('$admin_name', $admin_name, 'string'); |
|
| 302 | - } |
|
| 303 | - $this->admin_name = $admin_name; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * a URL friendly string that can be used for identifying the form |
|
| 309 | - * |
|
| 310 | - * @return string |
|
| 311 | - */ |
|
| 312 | - public function slug() |
|
| 313 | - { |
|
| 314 | - return $this->slug; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * @param string $slug |
|
| 320 | - * @throws InvalidDataTypeException |
|
| 321 | - */ |
|
| 322 | - public function setSlug($slug) |
|
| 323 | - { |
|
| 324 | - if (! is_string($slug)) { |
|
| 325 | - throw new InvalidDataTypeException('$slug', $slug, 'string'); |
|
| 326 | - } |
|
| 327 | - $this->slug = $slug; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * @return string |
|
| 333 | - */ |
|
| 334 | - public function submitBtnText() |
|
| 335 | - { |
|
| 336 | - return $this->submit_btn_text; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * @param string $submit_btn_text |
|
| 342 | - * @throws InvalidDataTypeException |
|
| 343 | - * @throws InvalidArgumentException |
|
| 344 | - */ |
|
| 345 | - public function setSubmitBtnText($submit_btn_text) |
|
| 346 | - { |
|
| 347 | - if (! is_string($submit_btn_text)) { |
|
| 348 | - throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string'); |
|
| 349 | - } |
|
| 350 | - if (empty($submit_btn_text)) { |
|
| 351 | - throw new InvalidArgumentException( |
|
| 352 | - esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso') |
|
| 353 | - ); |
|
| 354 | - } |
|
| 355 | - $this->submit_btn_text = $submit_btn_text; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * @return string |
|
| 361 | - */ |
|
| 362 | - public function formAction() |
|
| 363 | - { |
|
| 364 | - return ! empty($this->form_args) |
|
| 365 | - ? add_query_arg($this->form_args, $this->form_action) |
|
| 366 | - : $this->form_action; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * @param string $form_action |
|
| 372 | - * @throws InvalidDataTypeException |
|
| 373 | - */ |
|
| 374 | - public function setFormAction($form_action) |
|
| 375 | - { |
|
| 376 | - if (! is_string($form_action)) { |
|
| 377 | - throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
| 378 | - } |
|
| 379 | - $this->form_action = $form_action; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * @param array $form_args |
|
| 385 | - * @throws InvalidDataTypeException |
|
| 386 | - * @throws InvalidArgumentException |
|
| 387 | - */ |
|
| 388 | - public function addFormActionArgs($form_args = array()) |
|
| 389 | - { |
|
| 390 | - if (is_object($form_args)) { |
|
| 391 | - throw new InvalidDataTypeException( |
|
| 392 | - '$form_args', |
|
| 393 | - $form_args, |
|
| 394 | - 'anything other than an object was expected.' |
|
| 395 | - ); |
|
| 396 | - } |
|
| 397 | - if (empty($form_args)) { |
|
| 398 | - throw new InvalidArgumentException( |
|
| 399 | - esc_html__('The redirect arguments can not be an empty array.', 'event_espresso') |
|
| 400 | - ); |
|
| 401 | - } |
|
| 402 | - $this->form_args = array_merge($this->form_args, $form_args); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * @return string |
|
| 408 | - */ |
|
| 409 | - public function formConfig() |
|
| 410 | - { |
|
| 411 | - return $this->form_config; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * @param string $form_config |
|
| 417 | - * @throws DomainException |
|
| 418 | - */ |
|
| 419 | - public function setFormConfig($form_config) |
|
| 420 | - { |
|
| 421 | - if (! in_array( |
|
| 422 | - $form_config, |
|
| 423 | - array( |
|
| 424 | - FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 425 | - FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 426 | - FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 427 | - FormHandler::DO_NOT_SETUP_FORM, |
|
| 428 | - ), |
|
| 429 | - true |
|
| 430 | - ) |
|
| 431 | - ) { |
|
| 432 | - throw new DomainException( |
|
| 433 | - sprintf( |
|
| 434 | - esc_html__( |
|
| 435 | - '"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form', |
|
| 436 | - 'event_espresso' |
|
| 437 | - ), |
|
| 438 | - $form_config |
|
| 439 | - ) |
|
| 440 | - ); |
|
| 441 | - } |
|
| 442 | - $this->form_config = $form_config; |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * called after the form is instantiated |
|
| 448 | - * and used for performing any logic that needs to occur early |
|
| 449 | - * before any of the other methods are called. |
|
| 450 | - * returns true if everything is ok to proceed, |
|
| 451 | - * and false if no further form logic should be implemented |
|
| 452 | - * |
|
| 453 | - * @return boolean |
|
| 454 | - */ |
|
| 455 | - public function initialize() |
|
| 456 | - { |
|
| 457 | - $this->form_has_errors = EE_Error::has_error(true); |
|
| 458 | - return true; |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * used for setting up css and js |
|
| 464 | - * |
|
| 465 | - * @return void |
|
| 466 | - * @throws LogicException |
|
| 467 | - * @throws EE_Error |
|
| 468 | - */ |
|
| 469 | - public function enqueueStylesAndScripts() |
|
| 470 | - { |
|
| 471 | - $this->form()->enqueue_js(); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * creates and returns the actual form |
|
| 477 | - * |
|
| 478 | - * @return EE_Form_Section_Proper |
|
| 479 | - */ |
|
| 480 | - abstract public function generate(); |
|
| 481 | - |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * creates and returns an EE_Submit_Input labeled "Submit" |
|
| 485 | - * |
|
| 486 | - * @param string $text |
|
| 487 | - * @return EE_Submit_Input |
|
| 488 | - */ |
|
| 489 | - public function generateSubmitButton($text = '') |
|
| 490 | - { |
|
| 491 | - $text = ! empty($text) ? $text : $this->submitBtnText(); |
|
| 492 | - return new EE_Submit_Input( |
|
| 493 | - array( |
|
| 494 | - 'html_name' => 'ee-form-submit-' . $this->slug(), |
|
| 495 | - 'html_id' => 'ee-form-submit-' . $this->slug(), |
|
| 496 | - 'html_class' => 'ee-form-submit', |
|
| 497 | - 'html_label' => ' ', |
|
| 498 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 499 | - 'default' => $text, |
|
| 500 | - ) |
|
| 501 | - ); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * calls generateSubmitButton() and appends it onto the form along with a float clearing div |
|
| 507 | - * |
|
| 508 | - * @param string $text |
|
| 509 | - * @return void |
|
| 510 | - * @throws EE_Error |
|
| 511 | - */ |
|
| 512 | - public function appendSubmitButton($text = '') |
|
| 513 | - { |
|
| 514 | - if ($this->form->subsection_exists($this->slug() . '-submit-btn')) { |
|
| 515 | - return; |
|
| 516 | - } |
|
| 517 | - $this->form->add_subsections( |
|
| 518 | - array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)), |
|
| 519 | - null, |
|
| 520 | - false |
|
| 521 | - ); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - |
|
| 525 | - /** |
|
| 526 | - * creates and returns an EE_Submit_Input labeled "Cancel" |
|
| 527 | - * |
|
| 528 | - * @param string $text |
|
| 529 | - * @return EE_Submit_Input |
|
| 530 | - */ |
|
| 531 | - public function generateCancelButton($text = '') |
|
| 532 | - { |
|
| 533 | - $cancel_button = new EE_Submit_Input( |
|
| 534 | - array( |
|
| 535 | - 'html_name' => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!! |
|
| 536 | - 'html_id' => 'ee-cancel-form-' . $this->slug(), |
|
| 537 | - 'html_class' => 'ee-cancel-form', |
|
| 538 | - 'html_label' => ' ', |
|
| 539 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 540 | - 'default' => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'), |
|
| 541 | - ) |
|
| 542 | - ); |
|
| 543 | - $cancel_button->set_button_css_attributes(false); |
|
| 544 | - return $cancel_button; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - |
|
| 548 | - /** |
|
| 549 | - * appends a float clearing div onto end of form |
|
| 550 | - * |
|
| 551 | - * @return void |
|
| 552 | - * @throws EE_Error |
|
| 553 | - */ |
|
| 554 | - public function clearFormButtonFloats() |
|
| 555 | - { |
|
| 556 | - $this->form->add_subsections( |
|
| 557 | - array( |
|
| 558 | - 'clear-submit-btn-float' => new EE_Form_Section_HTML( |
|
| 559 | - EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx() |
|
| 560 | - ), |
|
| 561 | - ), |
|
| 562 | - null, |
|
| 563 | - false |
|
| 564 | - ); |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - |
|
| 568 | - /** |
|
| 569 | - * takes the generated form and displays it along with ony other non-form HTML that may be required |
|
| 570 | - * returns a string of HTML that can be directly echoed in a template |
|
| 571 | - * |
|
| 572 | - * @return string |
|
| 573 | - * @throws \InvalidArgumentException |
|
| 574 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 575 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 576 | - * @throws LogicException |
|
| 577 | - * @throws EE_Error |
|
| 578 | - */ |
|
| 579 | - public function display() |
|
| 580 | - { |
|
| 581 | - $form_html = apply_filters( |
|
| 582 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form', |
|
| 583 | - '' |
|
| 584 | - ); |
|
| 585 | - $form_config = $this->formConfig(); |
|
| 586 | - if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 587 | - || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 588 | - ) { |
|
| 589 | - $additional_props = $this->requiresMultipartEnctype() |
|
| 590 | - ? 'enctype="multipart/form-data"' |
|
| 591 | - : ''; |
|
| 592 | - $form_html .= $this->form()->form_open( |
|
| 593 | - $this->formAction(), |
|
| 594 | - 'POST', |
|
| 595 | - $additional_props |
|
| 596 | - ); |
|
| 597 | - } |
|
| 598 | - $form_html .= $this->form(true)->get_html(); |
|
| 599 | - if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 600 | - || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 601 | - ) { |
|
| 602 | - $form_html .= $this->form()->form_close(); |
|
| 603 | - } |
|
| 604 | - $form_html .= apply_filters( |
|
| 605 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form', |
|
| 606 | - '' |
|
| 607 | - ); |
|
| 608 | - return $form_html; |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * Determines if this form needs "enctype='multipart/form-data'" or not. |
|
| 613 | - * @since 4.9.80.p |
|
| 614 | - * @return bool |
|
| 615 | - * @throws EE_Error |
|
| 616 | - */ |
|
| 617 | - public function requiresMultipartEnctype() |
|
| 618 | - { |
|
| 619 | - foreach ($this->form()->inputs_in_subsections() as $input) { |
|
| 620 | - if ($input instanceof EE_File_Input) { |
|
| 621 | - return true; |
|
| 622 | - } |
|
| 623 | - } |
|
| 624 | - return false; |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * handles processing the form submission |
|
| 630 | - * returns true or false depending on whether the form was processed successfully or not |
|
| 631 | - * |
|
| 632 | - * @param array $submitted_form_data |
|
| 633 | - * @return array |
|
| 634 | - * @throws \InvalidArgumentException |
|
| 635 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 636 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 637 | - * @throws EE_Error |
|
| 638 | - * @throws LogicException |
|
| 639 | - * @throws InvalidFormSubmissionException |
|
| 640 | - */ |
|
| 641 | - public function process($submitted_form_data = array()) |
|
| 642 | - { |
|
| 643 | - if (! $this->form()->was_submitted($submitted_form_data)) { |
|
| 644 | - throw new InvalidFormSubmissionException($this->form_name); |
|
| 645 | - } |
|
| 646 | - $this->form(true)->receive_form_submission($submitted_form_data); |
|
| 647 | - if (! $this->form()->is_valid()) { |
|
| 648 | - throw new InvalidFormSubmissionException( |
|
| 649 | - $this->form_name, |
|
| 650 | - sprintf( |
|
| 651 | - esc_html__( |
|
| 652 | - 'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s', |
|
| 653 | - 'event_espresso' |
|
| 654 | - ), |
|
| 655 | - $this->form_name, |
|
| 656 | - '<br />', |
|
| 657 | - implode('<br />', $this->form()->get_validation_errors_accumulated()) |
|
| 658 | - ) |
|
| 659 | - ); |
|
| 660 | - } |
|
| 661 | - return apply_filters( |
|
| 662 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data', |
|
| 663 | - $this->form()->valid_data(), |
|
| 664 | - $this |
|
| 665 | - ); |
|
| 666 | - } |
|
| 32 | + /** |
|
| 33 | + * will add opening and closing HTML form tags as well as a submit button |
|
| 34 | + */ |
|
| 35 | + const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * will add opening and closing HTML form tags but NOT a submit button |
|
| 39 | + */ |
|
| 40 | + const ADD_FORM_TAGS_ONLY = 'add_form_tags_only'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * will NOT add opening and closing HTML form tags but will add a submit button |
|
| 44 | + */ |
|
| 45 | + const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only'; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * will NOT add opening and closing HTML form tags NOR a submit button |
|
| 49 | + */ |
|
| 50 | + const DO_NOT_SETUP_FORM = 'do_not_setup_form'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * if set to false, then this form has no displayable content, |
|
| 54 | + * and will only be used for processing data sent passed via GET or POST |
|
| 55 | + * defaults to true ( ie: form has displayable content ) |
|
| 56 | + * |
|
| 57 | + * @var boolean $displayable |
|
| 58 | + */ |
|
| 59 | + private $displayable = true; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var string $form_name |
|
| 63 | + */ |
|
| 64 | + private $form_name; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var string $admin_name |
|
| 68 | + */ |
|
| 69 | + private $admin_name; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var string $slug |
|
| 73 | + */ |
|
| 74 | + private $slug; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var string $submit_btn_text |
|
| 78 | + */ |
|
| 79 | + private $submit_btn_text; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @var string $form_action |
|
| 83 | + */ |
|
| 84 | + private $form_action; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * form params in key value pairs |
|
| 88 | + * can be added to form action URL or as hidden inputs |
|
| 89 | + * |
|
| 90 | + * @var array $form_args |
|
| 91 | + */ |
|
| 92 | + private $form_args = array(); |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * value of one of the string constant above |
|
| 96 | + * |
|
| 97 | + * @var string $form_config |
|
| 98 | + */ |
|
| 99 | + private $form_config; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * whether or not the form was determined to be invalid |
|
| 103 | + * |
|
| 104 | + * @var boolean $form_has_errors |
|
| 105 | + */ |
|
| 106 | + private $form_has_errors; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * the absolute top level form section being used on the page |
|
| 110 | + * |
|
| 111 | + * @var EE_Form_Section_Proper $form |
|
| 112 | + */ |
|
| 113 | + private $form; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @var EE_Registry $registry |
|
| 117 | + */ |
|
| 118 | + protected $registry; |
|
| 119 | + |
|
| 120 | + // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
| 121 | + /** |
|
| 122 | + * Form constructor. |
|
| 123 | + * |
|
| 124 | + * @param string $form_name |
|
| 125 | + * @param string $admin_name |
|
| 126 | + * @param string $slug |
|
| 127 | + * @param string $form_action |
|
| 128 | + * @param string $form_config |
|
| 129 | + * @param EE_Registry $registry |
|
| 130 | + * @throws InvalidDataTypeException |
|
| 131 | + * @throws DomainException |
|
| 132 | + * @throws InvalidArgumentException |
|
| 133 | + */ |
|
| 134 | + public function __construct( |
|
| 135 | + $form_name, |
|
| 136 | + $admin_name, |
|
| 137 | + $slug, |
|
| 138 | + $form_action = '', |
|
| 139 | + $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 140 | + EE_Registry $registry |
|
| 141 | + ) { |
|
| 142 | + $this->setFormName($form_name); |
|
| 143 | + $this->setAdminName($admin_name); |
|
| 144 | + $this->setSlug($slug); |
|
| 145 | + $this->setFormAction($form_action); |
|
| 146 | + $this->setFormConfig($form_config); |
|
| 147 | + $this->setSubmitBtnText(esc_html__('Submit', 'event_espresso')); |
|
| 148 | + $this->registry = $registry; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @return array |
|
| 154 | + */ |
|
| 155 | + public static function getFormConfigConstants() |
|
| 156 | + { |
|
| 157 | + return array( |
|
| 158 | + FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 159 | + FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 160 | + FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 161 | + FormHandler::DO_NOT_SETUP_FORM, |
|
| 162 | + ); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param bool $for_display |
|
| 168 | + * @return EE_Form_Section_Proper |
|
| 169 | + * @throws EE_Error |
|
| 170 | + * @throws LogicException |
|
| 171 | + */ |
|
| 172 | + public function form($for_display = false) |
|
| 173 | + { |
|
| 174 | + if (! $this->formIsValid()) { |
|
| 175 | + return null; |
|
| 176 | + } |
|
| 177 | + if ($for_display) { |
|
| 178 | + $form_config = $this->formConfig(); |
|
| 179 | + if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 180 | + || $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY |
|
| 181 | + ) { |
|
| 182 | + $this->appendSubmitButton(); |
|
| 183 | + $this->clearFormButtonFloats(); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + return $this->form; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * @return boolean |
|
| 192 | + * @throws LogicException |
|
| 193 | + */ |
|
| 194 | + public function formIsValid() |
|
| 195 | + { |
|
| 196 | + if ($this->form instanceof EE_Form_Section_Proper) { |
|
| 197 | + return true; |
|
| 198 | + } |
|
| 199 | + $form = apply_filters( |
|
| 200 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object', |
|
| 201 | + $this->generate(), |
|
| 202 | + $this |
|
| 203 | + ); |
|
| 204 | + if ($this->verifyForm($form)) { |
|
| 205 | + $this->setForm($form); |
|
| 206 | + } |
|
| 207 | + return true; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * @param EE_Form_Section_Proper|null $form |
|
| 213 | + * @return bool |
|
| 214 | + * @throws LogicException |
|
| 215 | + */ |
|
| 216 | + public function verifyForm(EE_Form_Section_Proper $form = null) |
|
| 217 | + { |
|
| 218 | + $form = $form !== null ? $form : $this->form; |
|
| 219 | + if ($form instanceof EE_Form_Section_Proper) { |
|
| 220 | + return true; |
|
| 221 | + } |
|
| 222 | + throw new LogicException( |
|
| 223 | + sprintf( |
|
| 224 | + esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'), |
|
| 225 | + $this->form_name, |
|
| 226 | + var_export($form, true) |
|
| 227 | + ) |
|
| 228 | + ); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * @param EE_Form_Section_Proper $form |
|
| 234 | + */ |
|
| 235 | + public function setForm(EE_Form_Section_Proper $form) |
|
| 236 | + { |
|
| 237 | + $this->form = $form; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * @return boolean |
|
| 243 | + */ |
|
| 244 | + public function displayable() |
|
| 245 | + { |
|
| 246 | + return $this->displayable; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param boolean $displayable |
|
| 252 | + */ |
|
| 253 | + public function setDisplayable($displayable = false) |
|
| 254 | + { |
|
| 255 | + $this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * a public name for the form that can be displayed on the frontend of a site |
|
| 261 | + * |
|
| 262 | + * @return string |
|
| 263 | + */ |
|
| 264 | + public function formName() |
|
| 265 | + { |
|
| 266 | + return $this->form_name; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * @param string $form_name |
|
| 272 | + * @throws InvalidDataTypeException |
|
| 273 | + */ |
|
| 274 | + public function setFormName($form_name) |
|
| 275 | + { |
|
| 276 | + if (! is_string($form_name)) { |
|
| 277 | + throw new InvalidDataTypeException('$form_name', $form_name, 'string'); |
|
| 278 | + } |
|
| 279 | + $this->form_name = $form_name; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * a public name for the form that can be displayed, but only in the admin |
|
| 285 | + * |
|
| 286 | + * @return string |
|
| 287 | + */ |
|
| 288 | + public function adminName() |
|
| 289 | + { |
|
| 290 | + return $this->admin_name; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * @param string $admin_name |
|
| 296 | + * @throws InvalidDataTypeException |
|
| 297 | + */ |
|
| 298 | + public function setAdminName($admin_name) |
|
| 299 | + { |
|
| 300 | + if (! is_string($admin_name)) { |
|
| 301 | + throw new InvalidDataTypeException('$admin_name', $admin_name, 'string'); |
|
| 302 | + } |
|
| 303 | + $this->admin_name = $admin_name; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * a URL friendly string that can be used for identifying the form |
|
| 309 | + * |
|
| 310 | + * @return string |
|
| 311 | + */ |
|
| 312 | + public function slug() |
|
| 313 | + { |
|
| 314 | + return $this->slug; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * @param string $slug |
|
| 320 | + * @throws InvalidDataTypeException |
|
| 321 | + */ |
|
| 322 | + public function setSlug($slug) |
|
| 323 | + { |
|
| 324 | + if (! is_string($slug)) { |
|
| 325 | + throw new InvalidDataTypeException('$slug', $slug, 'string'); |
|
| 326 | + } |
|
| 327 | + $this->slug = $slug; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * @return string |
|
| 333 | + */ |
|
| 334 | + public function submitBtnText() |
|
| 335 | + { |
|
| 336 | + return $this->submit_btn_text; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * @param string $submit_btn_text |
|
| 342 | + * @throws InvalidDataTypeException |
|
| 343 | + * @throws InvalidArgumentException |
|
| 344 | + */ |
|
| 345 | + public function setSubmitBtnText($submit_btn_text) |
|
| 346 | + { |
|
| 347 | + if (! is_string($submit_btn_text)) { |
|
| 348 | + throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string'); |
|
| 349 | + } |
|
| 350 | + if (empty($submit_btn_text)) { |
|
| 351 | + throw new InvalidArgumentException( |
|
| 352 | + esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso') |
|
| 353 | + ); |
|
| 354 | + } |
|
| 355 | + $this->submit_btn_text = $submit_btn_text; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * @return string |
|
| 361 | + */ |
|
| 362 | + public function formAction() |
|
| 363 | + { |
|
| 364 | + return ! empty($this->form_args) |
|
| 365 | + ? add_query_arg($this->form_args, $this->form_action) |
|
| 366 | + : $this->form_action; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * @param string $form_action |
|
| 372 | + * @throws InvalidDataTypeException |
|
| 373 | + */ |
|
| 374 | + public function setFormAction($form_action) |
|
| 375 | + { |
|
| 376 | + if (! is_string($form_action)) { |
|
| 377 | + throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
| 378 | + } |
|
| 379 | + $this->form_action = $form_action; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * @param array $form_args |
|
| 385 | + * @throws InvalidDataTypeException |
|
| 386 | + * @throws InvalidArgumentException |
|
| 387 | + */ |
|
| 388 | + public function addFormActionArgs($form_args = array()) |
|
| 389 | + { |
|
| 390 | + if (is_object($form_args)) { |
|
| 391 | + throw new InvalidDataTypeException( |
|
| 392 | + '$form_args', |
|
| 393 | + $form_args, |
|
| 394 | + 'anything other than an object was expected.' |
|
| 395 | + ); |
|
| 396 | + } |
|
| 397 | + if (empty($form_args)) { |
|
| 398 | + throw new InvalidArgumentException( |
|
| 399 | + esc_html__('The redirect arguments can not be an empty array.', 'event_espresso') |
|
| 400 | + ); |
|
| 401 | + } |
|
| 402 | + $this->form_args = array_merge($this->form_args, $form_args); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * @return string |
|
| 408 | + */ |
|
| 409 | + public function formConfig() |
|
| 410 | + { |
|
| 411 | + return $this->form_config; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * @param string $form_config |
|
| 417 | + * @throws DomainException |
|
| 418 | + */ |
|
| 419 | + public function setFormConfig($form_config) |
|
| 420 | + { |
|
| 421 | + if (! in_array( |
|
| 422 | + $form_config, |
|
| 423 | + array( |
|
| 424 | + FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 425 | + FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 426 | + FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 427 | + FormHandler::DO_NOT_SETUP_FORM, |
|
| 428 | + ), |
|
| 429 | + true |
|
| 430 | + ) |
|
| 431 | + ) { |
|
| 432 | + throw new DomainException( |
|
| 433 | + sprintf( |
|
| 434 | + esc_html__( |
|
| 435 | + '"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form', |
|
| 436 | + 'event_espresso' |
|
| 437 | + ), |
|
| 438 | + $form_config |
|
| 439 | + ) |
|
| 440 | + ); |
|
| 441 | + } |
|
| 442 | + $this->form_config = $form_config; |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * called after the form is instantiated |
|
| 448 | + * and used for performing any logic that needs to occur early |
|
| 449 | + * before any of the other methods are called. |
|
| 450 | + * returns true if everything is ok to proceed, |
|
| 451 | + * and false if no further form logic should be implemented |
|
| 452 | + * |
|
| 453 | + * @return boolean |
|
| 454 | + */ |
|
| 455 | + public function initialize() |
|
| 456 | + { |
|
| 457 | + $this->form_has_errors = EE_Error::has_error(true); |
|
| 458 | + return true; |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * used for setting up css and js |
|
| 464 | + * |
|
| 465 | + * @return void |
|
| 466 | + * @throws LogicException |
|
| 467 | + * @throws EE_Error |
|
| 468 | + */ |
|
| 469 | + public function enqueueStylesAndScripts() |
|
| 470 | + { |
|
| 471 | + $this->form()->enqueue_js(); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * creates and returns the actual form |
|
| 477 | + * |
|
| 478 | + * @return EE_Form_Section_Proper |
|
| 479 | + */ |
|
| 480 | + abstract public function generate(); |
|
| 481 | + |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * creates and returns an EE_Submit_Input labeled "Submit" |
|
| 485 | + * |
|
| 486 | + * @param string $text |
|
| 487 | + * @return EE_Submit_Input |
|
| 488 | + */ |
|
| 489 | + public function generateSubmitButton($text = '') |
|
| 490 | + { |
|
| 491 | + $text = ! empty($text) ? $text : $this->submitBtnText(); |
|
| 492 | + return new EE_Submit_Input( |
|
| 493 | + array( |
|
| 494 | + 'html_name' => 'ee-form-submit-' . $this->slug(), |
|
| 495 | + 'html_id' => 'ee-form-submit-' . $this->slug(), |
|
| 496 | + 'html_class' => 'ee-form-submit', |
|
| 497 | + 'html_label' => ' ', |
|
| 498 | + 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 499 | + 'default' => $text, |
|
| 500 | + ) |
|
| 501 | + ); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * calls generateSubmitButton() and appends it onto the form along with a float clearing div |
|
| 507 | + * |
|
| 508 | + * @param string $text |
|
| 509 | + * @return void |
|
| 510 | + * @throws EE_Error |
|
| 511 | + */ |
|
| 512 | + public function appendSubmitButton($text = '') |
|
| 513 | + { |
|
| 514 | + if ($this->form->subsection_exists($this->slug() . '-submit-btn')) { |
|
| 515 | + return; |
|
| 516 | + } |
|
| 517 | + $this->form->add_subsections( |
|
| 518 | + array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)), |
|
| 519 | + null, |
|
| 520 | + false |
|
| 521 | + ); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + |
|
| 525 | + /** |
|
| 526 | + * creates and returns an EE_Submit_Input labeled "Cancel" |
|
| 527 | + * |
|
| 528 | + * @param string $text |
|
| 529 | + * @return EE_Submit_Input |
|
| 530 | + */ |
|
| 531 | + public function generateCancelButton($text = '') |
|
| 532 | + { |
|
| 533 | + $cancel_button = new EE_Submit_Input( |
|
| 534 | + array( |
|
| 535 | + 'html_name' => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!! |
|
| 536 | + 'html_id' => 'ee-cancel-form-' . $this->slug(), |
|
| 537 | + 'html_class' => 'ee-cancel-form', |
|
| 538 | + 'html_label' => ' ', |
|
| 539 | + 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 540 | + 'default' => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'), |
|
| 541 | + ) |
|
| 542 | + ); |
|
| 543 | + $cancel_button->set_button_css_attributes(false); |
|
| 544 | + return $cancel_button; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * appends a float clearing div onto end of form |
|
| 550 | + * |
|
| 551 | + * @return void |
|
| 552 | + * @throws EE_Error |
|
| 553 | + */ |
|
| 554 | + public function clearFormButtonFloats() |
|
| 555 | + { |
|
| 556 | + $this->form->add_subsections( |
|
| 557 | + array( |
|
| 558 | + 'clear-submit-btn-float' => new EE_Form_Section_HTML( |
|
| 559 | + EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx() |
|
| 560 | + ), |
|
| 561 | + ), |
|
| 562 | + null, |
|
| 563 | + false |
|
| 564 | + ); |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + |
|
| 568 | + /** |
|
| 569 | + * takes the generated form and displays it along with ony other non-form HTML that may be required |
|
| 570 | + * returns a string of HTML that can be directly echoed in a template |
|
| 571 | + * |
|
| 572 | + * @return string |
|
| 573 | + * @throws \InvalidArgumentException |
|
| 574 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 575 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 576 | + * @throws LogicException |
|
| 577 | + * @throws EE_Error |
|
| 578 | + */ |
|
| 579 | + public function display() |
|
| 580 | + { |
|
| 581 | + $form_html = apply_filters( |
|
| 582 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form', |
|
| 583 | + '' |
|
| 584 | + ); |
|
| 585 | + $form_config = $this->formConfig(); |
|
| 586 | + if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 587 | + || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 588 | + ) { |
|
| 589 | + $additional_props = $this->requiresMultipartEnctype() |
|
| 590 | + ? 'enctype="multipart/form-data"' |
|
| 591 | + : ''; |
|
| 592 | + $form_html .= $this->form()->form_open( |
|
| 593 | + $this->formAction(), |
|
| 594 | + 'POST', |
|
| 595 | + $additional_props |
|
| 596 | + ); |
|
| 597 | + } |
|
| 598 | + $form_html .= $this->form(true)->get_html(); |
|
| 599 | + if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 600 | + || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 601 | + ) { |
|
| 602 | + $form_html .= $this->form()->form_close(); |
|
| 603 | + } |
|
| 604 | + $form_html .= apply_filters( |
|
| 605 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form', |
|
| 606 | + '' |
|
| 607 | + ); |
|
| 608 | + return $form_html; |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Determines if this form needs "enctype='multipart/form-data'" or not. |
|
| 613 | + * @since 4.9.80.p |
|
| 614 | + * @return bool |
|
| 615 | + * @throws EE_Error |
|
| 616 | + */ |
|
| 617 | + public function requiresMultipartEnctype() |
|
| 618 | + { |
|
| 619 | + foreach ($this->form()->inputs_in_subsections() as $input) { |
|
| 620 | + if ($input instanceof EE_File_Input) { |
|
| 621 | + return true; |
|
| 622 | + } |
|
| 623 | + } |
|
| 624 | + return false; |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * handles processing the form submission |
|
| 630 | + * returns true or false depending on whether the form was processed successfully or not |
|
| 631 | + * |
|
| 632 | + * @param array $submitted_form_data |
|
| 633 | + * @return array |
|
| 634 | + * @throws \InvalidArgumentException |
|
| 635 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 636 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 637 | + * @throws EE_Error |
|
| 638 | + * @throws LogicException |
|
| 639 | + * @throws InvalidFormSubmissionException |
|
| 640 | + */ |
|
| 641 | + public function process($submitted_form_data = array()) |
|
| 642 | + { |
|
| 643 | + if (! $this->form()->was_submitted($submitted_form_data)) { |
|
| 644 | + throw new InvalidFormSubmissionException($this->form_name); |
|
| 645 | + } |
|
| 646 | + $this->form(true)->receive_form_submission($submitted_form_data); |
|
| 647 | + if (! $this->form()->is_valid()) { |
|
| 648 | + throw new InvalidFormSubmissionException( |
|
| 649 | + $this->form_name, |
|
| 650 | + sprintf( |
|
| 651 | + esc_html__( |
|
| 652 | + 'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s', |
|
| 653 | + 'event_espresso' |
|
| 654 | + ), |
|
| 655 | + $this->form_name, |
|
| 656 | + '<br />', |
|
| 657 | + implode('<br />', $this->form()->get_validation_errors_accumulated()) |
|
| 658 | + ) |
|
| 659 | + ); |
|
| 660 | + } |
|
| 661 | + return apply_filters( |
|
| 662 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data', |
|
| 663 | + $this->form()->valid_data(), |
|
| 664 | + $this |
|
| 665 | + ); |
|
| 666 | + } |
|
| 667 | 667 | } |
@@ -13,3345 +13,3345 @@ |
||
| 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 | - break; |
|
| 1585 | - case 'D': |
|
| 1586 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
| 1587 | - $datetime_value, |
|
| 1588 | - $this->_fields[ $fieldname ] |
|
| 1589 | - ); |
|
| 1590 | - break; |
|
| 1591 | - case 'B': |
|
| 1592 | - $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
| 1593 | - break; |
|
| 1594 | - } |
|
| 1595 | - $this->_clear_cached_property($fieldname); |
|
| 1596 | - } |
|
| 1597 | - |
|
| 1598 | - |
|
| 1599 | - /** |
|
| 1600 | - * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1601 | - * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1602 | - * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1603 | - * that could lead to some unexpected results! |
|
| 1604 | - * |
|
| 1605 | - * @access public |
|
| 1606 | - * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1607 | - * value being returned. |
|
| 1608 | - * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1609 | - * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1610 | - * @param string $prepend You can include something to prepend on the timestamp |
|
| 1611 | - * @param string $append You can include something to append on the timestamp |
|
| 1612 | - * @throws ReflectionException |
|
| 1613 | - * @throws InvalidArgumentException |
|
| 1614 | - * @throws InvalidInterfaceException |
|
| 1615 | - * @throws InvalidDataTypeException |
|
| 1616 | - * @throws EE_Error |
|
| 1617 | - * @return string timestamp |
|
| 1618 | - */ |
|
| 1619 | - public function display_in_my_timezone( |
|
| 1620 | - $field_name, |
|
| 1621 | - $callback = 'get_datetime', |
|
| 1622 | - $args = null, |
|
| 1623 | - $prepend = '', |
|
| 1624 | - $append = '' |
|
| 1625 | - ) { |
|
| 1626 | - $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1627 | - if ($timezone === $this->_timezone) { |
|
| 1628 | - return ''; |
|
| 1629 | - } |
|
| 1630 | - $original_timezone = $this->_timezone; |
|
| 1631 | - $this->set_timezone($timezone); |
|
| 1632 | - $fn = (array) $field_name; |
|
| 1633 | - $args = array_merge($fn, (array) $args); |
|
| 1634 | - if (! method_exists($this, $callback)) { |
|
| 1635 | - throw new EE_Error( |
|
| 1636 | - sprintf( |
|
| 1637 | - esc_html__( |
|
| 1638 | - 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1639 | - 'event_espresso' |
|
| 1640 | - ), |
|
| 1641 | - $callback |
|
| 1642 | - ) |
|
| 1643 | - ); |
|
| 1644 | - } |
|
| 1645 | - $args = (array) $args; |
|
| 1646 | - $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1647 | - $this->set_timezone($original_timezone); |
|
| 1648 | - return $return; |
|
| 1649 | - } |
|
| 1650 | - |
|
| 1651 | - |
|
| 1652 | - /** |
|
| 1653 | - * Deletes this model object. |
|
| 1654 | - * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1655 | - * override |
|
| 1656 | - * `EE_Base_Class::_delete` NOT this class. |
|
| 1657 | - * |
|
| 1658 | - * @return boolean | int |
|
| 1659 | - * @throws ReflectionException |
|
| 1660 | - * @throws InvalidArgumentException |
|
| 1661 | - * @throws InvalidInterfaceException |
|
| 1662 | - * @throws InvalidDataTypeException |
|
| 1663 | - * @throws EE_Error |
|
| 1664 | - */ |
|
| 1665 | - public function delete() |
|
| 1666 | - { |
|
| 1667 | - /** |
|
| 1668 | - * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1669 | - * Note: |
|
| 1670 | - * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1671 | - * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1672 | - * For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1673 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1674 | - * |
|
| 1675 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1676 | - */ |
|
| 1677 | - do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1678 | - $result = $this->_delete(); |
|
| 1679 | - /** |
|
| 1680 | - * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1681 | - * Note: |
|
| 1682 | - * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1683 | - * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1684 | - * For example `EE_Soft_Base_Class::_delete` |
|
| 1685 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1686 | - * |
|
| 1687 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1688 | - * @param boolean $result |
|
| 1689 | - */ |
|
| 1690 | - do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1691 | - return $result; |
|
| 1692 | - } |
|
| 1693 | - |
|
| 1694 | - |
|
| 1695 | - /** |
|
| 1696 | - * Calls the specific delete method for the instantiated class. |
|
| 1697 | - * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1698 | - * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1699 | - * `EE_Base_Class::delete` |
|
| 1700 | - * |
|
| 1701 | - * @return bool|int |
|
| 1702 | - * @throws ReflectionException |
|
| 1703 | - * @throws InvalidArgumentException |
|
| 1704 | - * @throws InvalidInterfaceException |
|
| 1705 | - * @throws InvalidDataTypeException |
|
| 1706 | - * @throws EE_Error |
|
| 1707 | - */ |
|
| 1708 | - protected function _delete() |
|
| 1709 | - { |
|
| 1710 | - return $this->delete_permanently(); |
|
| 1711 | - } |
|
| 1712 | - |
|
| 1713 | - |
|
| 1714 | - /** |
|
| 1715 | - * Deletes this model object permanently from db |
|
| 1716 | - * (but keep in mind related models may block the delete and return an error) |
|
| 1717 | - * |
|
| 1718 | - * @return bool | int |
|
| 1719 | - * @throws ReflectionException |
|
| 1720 | - * @throws InvalidArgumentException |
|
| 1721 | - * @throws InvalidInterfaceException |
|
| 1722 | - * @throws InvalidDataTypeException |
|
| 1723 | - * @throws EE_Error |
|
| 1724 | - */ |
|
| 1725 | - public function delete_permanently() |
|
| 1726 | - { |
|
| 1727 | - /** |
|
| 1728 | - * Called just before HARD deleting a model object |
|
| 1729 | - * |
|
| 1730 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1731 | - */ |
|
| 1732 | - do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1733 | - $model = $this->get_model(); |
|
| 1734 | - $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1735 | - $this->refresh_cache_of_related_objects(); |
|
| 1736 | - /** |
|
| 1737 | - * Called just after HARD deleting a model object |
|
| 1738 | - * |
|
| 1739 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1740 | - * @param boolean $result |
|
| 1741 | - */ |
|
| 1742 | - do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1743 | - return $result; |
|
| 1744 | - } |
|
| 1745 | - |
|
| 1746 | - |
|
| 1747 | - /** |
|
| 1748 | - * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1749 | - * related model objects |
|
| 1750 | - * |
|
| 1751 | - * @throws ReflectionException |
|
| 1752 | - * @throws InvalidArgumentException |
|
| 1753 | - * @throws InvalidInterfaceException |
|
| 1754 | - * @throws InvalidDataTypeException |
|
| 1755 | - * @throws EE_Error |
|
| 1756 | - */ |
|
| 1757 | - public function refresh_cache_of_related_objects() |
|
| 1758 | - { |
|
| 1759 | - $model = $this->get_model(); |
|
| 1760 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1761 | - if (! empty($this->_model_relations[ $relation_name ])) { |
|
| 1762 | - $related_objects = $this->_model_relations[ $relation_name ]; |
|
| 1763 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1764 | - // this relation only stores a single model object, not an array |
|
| 1765 | - // but let's make it consistent |
|
| 1766 | - $related_objects = array($related_objects); |
|
| 1767 | - } |
|
| 1768 | - foreach ($related_objects as $related_object) { |
|
| 1769 | - // only refresh their cache if they're in memory |
|
| 1770 | - if ($related_object instanceof EE_Base_Class) { |
|
| 1771 | - $related_object->clear_cache( |
|
| 1772 | - $model->get_this_model_name(), |
|
| 1773 | - $this |
|
| 1774 | - ); |
|
| 1775 | - } |
|
| 1776 | - } |
|
| 1777 | - } |
|
| 1778 | - } |
|
| 1779 | - } |
|
| 1780 | - |
|
| 1781 | - |
|
| 1782 | - /** |
|
| 1783 | - * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1784 | - * object just before saving. |
|
| 1785 | - * |
|
| 1786 | - * @access public |
|
| 1787 | - * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1788 | - * if provided during the save() method (often client code will change the fields' |
|
| 1789 | - * values before calling save) |
|
| 1790 | - * @throws InvalidArgumentException |
|
| 1791 | - * @throws InvalidInterfaceException |
|
| 1792 | - * @throws InvalidDataTypeException |
|
| 1793 | - * @throws EE_Error |
|
| 1794 | - * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1795 | - * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1796 | - * @throws ReflectionException |
|
| 1797 | - * @throws ReflectionException |
|
| 1798 | - * @throws ReflectionException |
|
| 1799 | - */ |
|
| 1800 | - public function save($set_cols_n_values = array()) |
|
| 1801 | - { |
|
| 1802 | - $model = $this->get_model(); |
|
| 1803 | - /** |
|
| 1804 | - * Filters the fields we're about to save on the model object |
|
| 1805 | - * |
|
| 1806 | - * @param array $set_cols_n_values |
|
| 1807 | - * @param EE_Base_Class $model_object |
|
| 1808 | - */ |
|
| 1809 | - $set_cols_n_values = (array) apply_filters( |
|
| 1810 | - 'FHEE__EE_Base_Class__save__set_cols_n_values', |
|
| 1811 | - $set_cols_n_values, |
|
| 1812 | - $this |
|
| 1813 | - ); |
|
| 1814 | - // set attributes as provided in $set_cols_n_values |
|
| 1815 | - foreach ($set_cols_n_values as $column => $value) { |
|
| 1816 | - $this->set($column, $value); |
|
| 1817 | - } |
|
| 1818 | - // no changes ? then don't do anything |
|
| 1819 | - if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1820 | - return 0; |
|
| 1821 | - } |
|
| 1822 | - /** |
|
| 1823 | - * Saving a model object. |
|
| 1824 | - * Before we perform a save, this action is fired. |
|
| 1825 | - * |
|
| 1826 | - * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1827 | - */ |
|
| 1828 | - do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1829 | - if (! $this->allow_persist()) { |
|
| 1830 | - return 0; |
|
| 1831 | - } |
|
| 1832 | - // now get current attribute values |
|
| 1833 | - $save_cols_n_values = $this->_fields; |
|
| 1834 | - // if the object already has an ID, update it. Otherwise, insert it |
|
| 1835 | - // also: change the assumption about values passed to the model NOT being prepare dby the model object. |
|
| 1836 | - // They have been |
|
| 1837 | - $old_assumption_concerning_value_preparation = $model |
|
| 1838 | - ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1839 | - $model->assume_values_already_prepared_by_model_object(true); |
|
| 1840 | - // does this model have an autoincrement PK? |
|
| 1841 | - if ($model->has_primary_key_field()) { |
|
| 1842 | - if ($model->get_primary_key_field()->is_auto_increment()) { |
|
| 1843 | - // ok check if it's set, if so: update; if not, insert |
|
| 1844 | - if (! empty($save_cols_n_values[ $model->primary_key_name() ])) { |
|
| 1845 | - $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1846 | - } else { |
|
| 1847 | - unset($save_cols_n_values[ $model->primary_key_name() ]); |
|
| 1848 | - $results = $model->insert($save_cols_n_values); |
|
| 1849 | - if ($results) { |
|
| 1850 | - // if successful, set the primary key |
|
| 1851 | - // but don't use the normal SET method, because it will check if |
|
| 1852 | - // an item with the same ID exists in the mapper & db, then |
|
| 1853 | - // will find it in the db (because we just added it) and THAT object |
|
| 1854 | - // will get added to the mapper before we can add this one! |
|
| 1855 | - // but if we just avoid using the SET method, all that headache can be avoided |
|
| 1856 | - $pk_field_name = $model->primary_key_name(); |
|
| 1857 | - $this->_fields[ $pk_field_name ] = $results; |
|
| 1858 | - $this->_clear_cached_property($pk_field_name); |
|
| 1859 | - $model->add_to_entity_map($this); |
|
| 1860 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1861 | - } |
|
| 1862 | - } |
|
| 1863 | - } else {// PK is NOT auto-increment |
|
| 1864 | - // so check if one like it already exists in the db |
|
| 1865 | - if ($model->exists_by_ID($this->ID())) { |
|
| 1866 | - if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1867 | - throw new EE_Error( |
|
| 1868 | - sprintf( |
|
| 1869 | - esc_html__( |
|
| 1870 | - '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', |
|
| 1871 | - 'event_espresso' |
|
| 1872 | - ), |
|
| 1873 | - get_class($this), |
|
| 1874 | - get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1875 | - get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1876 | - '<br />' |
|
| 1877 | - ) |
|
| 1878 | - ); |
|
| 1879 | - } |
|
| 1880 | - $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1881 | - } else { |
|
| 1882 | - $results = $model->insert($save_cols_n_values); |
|
| 1883 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1884 | - } |
|
| 1885 | - } |
|
| 1886 | - } else {// there is NO primary key |
|
| 1887 | - $already_in_db = false; |
|
| 1888 | - foreach ($model->unique_indexes() as $index) { |
|
| 1889 | - $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1890 | - if ($model->exists(array($uniqueness_where_params))) { |
|
| 1891 | - $already_in_db = true; |
|
| 1892 | - } |
|
| 1893 | - } |
|
| 1894 | - if ($already_in_db) { |
|
| 1895 | - $combined_pk_fields_n_values = array_intersect_key( |
|
| 1896 | - $save_cols_n_values, |
|
| 1897 | - $model->get_combined_primary_key_fields() |
|
| 1898 | - ); |
|
| 1899 | - $results = $model->update( |
|
| 1900 | - $save_cols_n_values, |
|
| 1901 | - $combined_pk_fields_n_values |
|
| 1902 | - ); |
|
| 1903 | - } else { |
|
| 1904 | - $results = $model->insert($save_cols_n_values); |
|
| 1905 | - } |
|
| 1906 | - } |
|
| 1907 | - // restore the old assumption about values being prepared by the model object |
|
| 1908 | - $model->assume_values_already_prepared_by_model_object( |
|
| 1909 | - $old_assumption_concerning_value_preparation |
|
| 1910 | - ); |
|
| 1911 | - /** |
|
| 1912 | - * After saving the model object this action is called |
|
| 1913 | - * |
|
| 1914 | - * @param EE_Base_Class $model_object which was just saved |
|
| 1915 | - * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1916 | - * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1917 | - */ |
|
| 1918 | - do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1919 | - $this->_has_changes = false; |
|
| 1920 | - return $results; |
|
| 1921 | - } |
|
| 1922 | - |
|
| 1923 | - |
|
| 1924 | - /** |
|
| 1925 | - * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1926 | - * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1927 | - * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1928 | - * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1929 | - * 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 |
|
| 1930 | - * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1931 | - * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1932 | - * |
|
| 1933 | - * @return void |
|
| 1934 | - * @throws ReflectionException |
|
| 1935 | - * @throws InvalidArgumentException |
|
| 1936 | - * @throws InvalidInterfaceException |
|
| 1937 | - * @throws InvalidDataTypeException |
|
| 1938 | - * @throws EE_Error |
|
| 1939 | - */ |
|
| 1940 | - protected function _update_cached_related_model_objs_fks() |
|
| 1941 | - { |
|
| 1942 | - $model = $this->get_model(); |
|
| 1943 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1944 | - if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1945 | - foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1946 | - $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1947 | - $model->get_this_model_name() |
|
| 1948 | - ); |
|
| 1949 | - $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1950 | - if ($related_model_obj_in_cache->ID()) { |
|
| 1951 | - $related_model_obj_in_cache->save(); |
|
| 1952 | - } |
|
| 1953 | - } |
|
| 1954 | - } |
|
| 1955 | - } |
|
| 1956 | - } |
|
| 1957 | - |
|
| 1958 | - |
|
| 1959 | - /** |
|
| 1960 | - * Saves this model object and its NEW cached relations to the database. |
|
| 1961 | - * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1962 | - * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1963 | - * because otherwise, there's a potential for infinite looping of saving |
|
| 1964 | - * Saves the cached related model objects, and ensures the relation between them |
|
| 1965 | - * and this object and properly setup |
|
| 1966 | - * |
|
| 1967 | - * @return int ID of new model object on save; 0 on failure+ |
|
| 1968 | - * @throws ReflectionException |
|
| 1969 | - * @throws InvalidArgumentException |
|
| 1970 | - * @throws InvalidInterfaceException |
|
| 1971 | - * @throws InvalidDataTypeException |
|
| 1972 | - * @throws EE_Error |
|
| 1973 | - */ |
|
| 1974 | - public function save_new_cached_related_model_objs() |
|
| 1975 | - { |
|
| 1976 | - // make sure this has been saved |
|
| 1977 | - if (! $this->ID()) { |
|
| 1978 | - $id = $this->save(); |
|
| 1979 | - } else { |
|
| 1980 | - $id = $this->ID(); |
|
| 1981 | - } |
|
| 1982 | - // now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1983 | - foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1984 | - if ($this->_model_relations[ $relationName ]) { |
|
| 1985 | - // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1986 | - // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1987 | - /* @var $related_model_obj EE_Base_Class */ |
|
| 1988 | - if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1989 | - // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1990 | - // but ONLY if it DOES NOT exist in the DB |
|
| 1991 | - $related_model_obj = $this->_model_relations[ $relationName ]; |
|
| 1992 | - // if( ! $related_model_obj->ID()){ |
|
| 1993 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1994 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1995 | - // } |
|
| 1996 | - } else { |
|
| 1997 | - foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
| 1998 | - // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1999 | - // but ONLY if it DOES NOT exist in the DB |
|
| 2000 | - // if( ! $related_model_obj->ID()){ |
|
| 2001 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 2002 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 2003 | - // } |
|
| 2004 | - } |
|
| 2005 | - } |
|
| 2006 | - } |
|
| 2007 | - } |
|
| 2008 | - return $id; |
|
| 2009 | - } |
|
| 2010 | - |
|
| 2011 | - |
|
| 2012 | - /** |
|
| 2013 | - * for getting a model while instantiated. |
|
| 2014 | - * |
|
| 2015 | - * @return EEM_Base | EEM_CPT_Base |
|
| 2016 | - * @throws ReflectionException |
|
| 2017 | - * @throws InvalidArgumentException |
|
| 2018 | - * @throws InvalidInterfaceException |
|
| 2019 | - * @throws InvalidDataTypeException |
|
| 2020 | - * @throws EE_Error |
|
| 2021 | - */ |
|
| 2022 | - public function get_model() |
|
| 2023 | - { |
|
| 2024 | - if (! $this->_model) { |
|
| 2025 | - $modelName = self::_get_model_classname(get_class($this)); |
|
| 2026 | - $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 2027 | - } else { |
|
| 2028 | - $this->_model->set_timezone($this->_timezone); |
|
| 2029 | - } |
|
| 2030 | - return $this->_model; |
|
| 2031 | - } |
|
| 2032 | - |
|
| 2033 | - |
|
| 2034 | - /** |
|
| 2035 | - * @param $props_n_values |
|
| 2036 | - * @param $classname |
|
| 2037 | - * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 2038 | - * @throws ReflectionException |
|
| 2039 | - * @throws InvalidArgumentException |
|
| 2040 | - * @throws InvalidInterfaceException |
|
| 2041 | - * @throws InvalidDataTypeException |
|
| 2042 | - * @throws EE_Error |
|
| 2043 | - */ |
|
| 2044 | - protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 2045 | - { |
|
| 2046 | - // TODO: will not work for Term_Relationships because they have no PK! |
|
| 2047 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2048 | - if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2049 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2050 | - ) { |
|
| 2051 | - $id = $props_n_values[ $primary_id_ref ]; |
|
| 2052 | - return self::_get_model($classname)->get_from_entity_map($id); |
|
| 2053 | - } |
|
| 2054 | - return false; |
|
| 2055 | - } |
|
| 2056 | - |
|
| 2057 | - |
|
| 2058 | - /** |
|
| 2059 | - * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 2060 | - * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 2061 | - * 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 |
|
| 2062 | - * we return false. |
|
| 2063 | - * |
|
| 2064 | - * @param array $props_n_values incoming array of properties and their values |
|
| 2065 | - * @param string $classname the classname of the child class |
|
| 2066 | - * @param null $timezone |
|
| 2067 | - * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 2068 | - * date_format and the second value is the time format |
|
| 2069 | - * @return mixed (EE_Base_Class|bool) |
|
| 2070 | - * @throws InvalidArgumentException |
|
| 2071 | - * @throws InvalidInterfaceException |
|
| 2072 | - * @throws InvalidDataTypeException |
|
| 2073 | - * @throws EE_Error |
|
| 2074 | - * @throws ReflectionException |
|
| 2075 | - * @throws ReflectionException |
|
| 2076 | - * @throws ReflectionException |
|
| 2077 | - */ |
|
| 2078 | - protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 2079 | - { |
|
| 2080 | - $existing = null; |
|
| 2081 | - $model = self::_get_model($classname, $timezone); |
|
| 2082 | - if ($model->has_primary_key_field()) { |
|
| 2083 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2084 | - if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2085 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2086 | - ) { |
|
| 2087 | - $existing = $model->get_one_by_ID( |
|
| 2088 | - $props_n_values[ $primary_id_ref ] |
|
| 2089 | - ); |
|
| 2090 | - } |
|
| 2091 | - } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 2092 | - // no primary key on this model, but there's still a matching item in the DB |
|
| 2093 | - $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 2094 | - self::_get_model($classname, $timezone) |
|
| 2095 | - ->get_index_primary_key_string($props_n_values) |
|
| 2096 | - ); |
|
| 2097 | - } |
|
| 2098 | - if ($existing) { |
|
| 2099 | - // set date formats if present before setting values |
|
| 2100 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
| 2101 | - $existing->set_date_format($date_formats[0]); |
|
| 2102 | - $existing->set_time_format($date_formats[1]); |
|
| 2103 | - } else { |
|
| 2104 | - // set default formats for date and time |
|
| 2105 | - $existing->set_date_format(get_option('date_format')); |
|
| 2106 | - $existing->set_time_format(get_option('time_format')); |
|
| 2107 | - } |
|
| 2108 | - foreach ($props_n_values as $property => $field_value) { |
|
| 2109 | - $existing->set($property, $field_value); |
|
| 2110 | - } |
|
| 2111 | - return $existing; |
|
| 2112 | - } |
|
| 2113 | - return false; |
|
| 2114 | - } |
|
| 2115 | - |
|
| 2116 | - |
|
| 2117 | - /** |
|
| 2118 | - * Gets the EEM_*_Model for this class |
|
| 2119 | - * |
|
| 2120 | - * @access public now, as this is more convenient |
|
| 2121 | - * @param $classname |
|
| 2122 | - * @param null $timezone |
|
| 2123 | - * @throws ReflectionException |
|
| 2124 | - * @throws InvalidArgumentException |
|
| 2125 | - * @throws InvalidInterfaceException |
|
| 2126 | - * @throws InvalidDataTypeException |
|
| 2127 | - * @throws EE_Error |
|
| 2128 | - * @return EEM_Base |
|
| 2129 | - */ |
|
| 2130 | - protected static function _get_model($classname, $timezone = null) |
|
| 2131 | - { |
|
| 2132 | - // find model for this class |
|
| 2133 | - if (! $classname) { |
|
| 2134 | - throw new EE_Error( |
|
| 2135 | - sprintf( |
|
| 2136 | - esc_html__( |
|
| 2137 | - 'What were you thinking calling _get_model(%s)?? You need to specify the class name', |
|
| 2138 | - 'event_espresso' |
|
| 2139 | - ), |
|
| 2140 | - $classname |
|
| 2141 | - ) |
|
| 2142 | - ); |
|
| 2143 | - } |
|
| 2144 | - $modelName = self::_get_model_classname($classname); |
|
| 2145 | - return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 2146 | - } |
|
| 2147 | - |
|
| 2148 | - |
|
| 2149 | - /** |
|
| 2150 | - * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 2151 | - * |
|
| 2152 | - * @param string $model_classname |
|
| 2153 | - * @param null $timezone |
|
| 2154 | - * @return EEM_Base |
|
| 2155 | - * @throws ReflectionException |
|
| 2156 | - * @throws InvalidArgumentException |
|
| 2157 | - * @throws InvalidInterfaceException |
|
| 2158 | - * @throws InvalidDataTypeException |
|
| 2159 | - * @throws EE_Error |
|
| 2160 | - */ |
|
| 2161 | - protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 2162 | - { |
|
| 2163 | - $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 2164 | - $model = EE_Registry::instance()->load_model($model_classname); |
|
| 2165 | - $model->set_timezone($timezone); |
|
| 2166 | - return $model; |
|
| 2167 | - } |
|
| 2168 | - |
|
| 2169 | - |
|
| 2170 | - /** |
|
| 2171 | - * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 2172 | - * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 2173 | - * |
|
| 2174 | - * @param null $model_name |
|
| 2175 | - * @return string like EEM_Attendee |
|
| 2176 | - */ |
|
| 2177 | - private static function _get_model_classname($model_name = null) |
|
| 2178 | - { |
|
| 2179 | - if (strpos($model_name, 'EE_') === 0) { |
|
| 2180 | - $model_classname = str_replace('EE_', 'EEM_', $model_name); |
|
| 2181 | - } else { |
|
| 2182 | - $model_classname = 'EEM_' . $model_name; |
|
| 2183 | - } |
|
| 2184 | - return $model_classname; |
|
| 2185 | - } |
|
| 2186 | - |
|
| 2187 | - |
|
| 2188 | - /** |
|
| 2189 | - * returns the name of the primary key attribute |
|
| 2190 | - * |
|
| 2191 | - * @param null $classname |
|
| 2192 | - * @throws ReflectionException |
|
| 2193 | - * @throws InvalidArgumentException |
|
| 2194 | - * @throws InvalidInterfaceException |
|
| 2195 | - * @throws InvalidDataTypeException |
|
| 2196 | - * @throws EE_Error |
|
| 2197 | - * @return string |
|
| 2198 | - */ |
|
| 2199 | - protected static function _get_primary_key_name($classname = null) |
|
| 2200 | - { |
|
| 2201 | - if (! $classname) { |
|
| 2202 | - throw new EE_Error( |
|
| 2203 | - sprintf( |
|
| 2204 | - esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
|
| 2205 | - $classname |
|
| 2206 | - ) |
|
| 2207 | - ); |
|
| 2208 | - } |
|
| 2209 | - return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 2210 | - } |
|
| 2211 | - |
|
| 2212 | - |
|
| 2213 | - /** |
|
| 2214 | - * Gets the value of the primary key. |
|
| 2215 | - * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 2216 | - * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 2217 | - * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 2218 | - * |
|
| 2219 | - * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 2220 | - * @throws ReflectionException |
|
| 2221 | - * @throws InvalidArgumentException |
|
| 2222 | - * @throws InvalidInterfaceException |
|
| 2223 | - * @throws InvalidDataTypeException |
|
| 2224 | - * @throws EE_Error |
|
| 2225 | - */ |
|
| 2226 | - public function ID() |
|
| 2227 | - { |
|
| 2228 | - $model = $this->get_model(); |
|
| 2229 | - // now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 2230 | - if ($model->has_primary_key_field()) { |
|
| 2231 | - return $this->_fields[ $model->primary_key_name() ]; |
|
| 2232 | - } |
|
| 2233 | - return $model->get_index_primary_key_string($this->_fields); |
|
| 2234 | - } |
|
| 2235 | - |
|
| 2236 | - |
|
| 2237 | - /** |
|
| 2238 | - * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 2239 | - * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 2240 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 2241 | - * |
|
| 2242 | - * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 2243 | - * @param string $relationName eg 'Events','Question',etc. |
|
| 2244 | - * an attendee to a group, you also want to specify which role they |
|
| 2245 | - * will have in that group. So you would use this parameter to |
|
| 2246 | - * specify array('role-column-name'=>'role-id') |
|
| 2247 | - * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 2248 | - * allow you to further constrict the relation to being added. |
|
| 2249 | - * However, keep in mind that the columns (keys) given must match a |
|
| 2250 | - * column on the JOIN table and currently only the HABTM models |
|
| 2251 | - * accept these additional conditions. Also remember that if an |
|
| 2252 | - * exact match isn't found for these extra cols/val pairs, then a |
|
| 2253 | - * NEW row is created in the join table. |
|
| 2254 | - * @param null $cache_id |
|
| 2255 | - * @throws ReflectionException |
|
| 2256 | - * @throws InvalidArgumentException |
|
| 2257 | - * @throws InvalidInterfaceException |
|
| 2258 | - * @throws InvalidDataTypeException |
|
| 2259 | - * @throws EE_Error |
|
| 2260 | - * @return EE_Base_Class the object the relation was added to |
|
| 2261 | - */ |
|
| 2262 | - public function _add_relation_to( |
|
| 2263 | - $otherObjectModelObjectOrID, |
|
| 2264 | - $relationName, |
|
| 2265 | - $extra_join_model_fields_n_values = array(), |
|
| 2266 | - $cache_id = null |
|
| 2267 | - ) { |
|
| 2268 | - $model = $this->get_model(); |
|
| 2269 | - // if this thing exists in the DB, save the relation to the DB |
|
| 2270 | - if ($this->ID()) { |
|
| 2271 | - $otherObject = $model->add_relationship_to( |
|
| 2272 | - $this, |
|
| 2273 | - $otherObjectModelObjectOrID, |
|
| 2274 | - $relationName, |
|
| 2275 | - $extra_join_model_fields_n_values |
|
| 2276 | - ); |
|
| 2277 | - // clear cache so future get_many_related and get_first_related() return new results. |
|
| 2278 | - $this->clear_cache($relationName, $otherObject, true); |
|
| 2279 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2280 | - $otherObject->clear_cache($model->get_this_model_name(), $this); |
|
| 2281 | - } |
|
| 2282 | - } else { |
|
| 2283 | - // this thing doesn't exist in the DB, so just cache it |
|
| 2284 | - if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2285 | - throw new EE_Error( |
|
| 2286 | - sprintf( |
|
| 2287 | - esc_html__( |
|
| 2288 | - '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', |
|
| 2289 | - 'event_espresso' |
|
| 2290 | - ), |
|
| 2291 | - $otherObjectModelObjectOrID, |
|
| 2292 | - get_class($this) |
|
| 2293 | - ) |
|
| 2294 | - ); |
|
| 2295 | - } |
|
| 2296 | - $otherObject = $otherObjectModelObjectOrID; |
|
| 2297 | - $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 2298 | - } |
|
| 2299 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2300 | - // fix the reciprocal relation too |
|
| 2301 | - if ($otherObject->ID()) { |
|
| 2302 | - // its saved so assumed relations exist in the DB, so we can just |
|
| 2303 | - // clear the cache so future queries use the updated info in the DB |
|
| 2304 | - $otherObject->clear_cache( |
|
| 2305 | - $model->get_this_model_name(), |
|
| 2306 | - null, |
|
| 2307 | - true |
|
| 2308 | - ); |
|
| 2309 | - } else { |
|
| 2310 | - // it's not saved, so it caches relations like this |
|
| 2311 | - $otherObject->cache($model->get_this_model_name(), $this); |
|
| 2312 | - } |
|
| 2313 | - } |
|
| 2314 | - return $otherObject; |
|
| 2315 | - } |
|
| 2316 | - |
|
| 2317 | - |
|
| 2318 | - /** |
|
| 2319 | - * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2320 | - * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2321 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2322 | - * from the cache |
|
| 2323 | - * |
|
| 2324 | - * @param mixed $otherObjectModelObjectOrID |
|
| 2325 | - * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2326 | - * to the DB yet |
|
| 2327 | - * @param string $relationName |
|
| 2328 | - * @param array $where_query |
|
| 2329 | - * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2330 | - * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2331 | - * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2332 | - * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
|
| 2333 | - * deleted. |
|
| 2334 | - * @return EE_Base_Class the relation was removed from |
|
| 2335 | - * @throws ReflectionException |
|
| 2336 | - * @throws InvalidArgumentException |
|
| 2337 | - * @throws InvalidInterfaceException |
|
| 2338 | - * @throws InvalidDataTypeException |
|
| 2339 | - * @throws EE_Error |
|
| 2340 | - */ |
|
| 2341 | - public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2342 | - { |
|
| 2343 | - if ($this->ID()) { |
|
| 2344 | - // if this exists in the DB, save the relation change to the DB too |
|
| 2345 | - $otherObject = $this->get_model()->remove_relationship_to( |
|
| 2346 | - $this, |
|
| 2347 | - $otherObjectModelObjectOrID, |
|
| 2348 | - $relationName, |
|
| 2349 | - $where_query |
|
| 2350 | - ); |
|
| 2351 | - $this->clear_cache( |
|
| 2352 | - $relationName, |
|
| 2353 | - $otherObject |
|
| 2354 | - ); |
|
| 2355 | - } else { |
|
| 2356 | - // this doesn't exist in the DB, just remove it from the cache |
|
| 2357 | - $otherObject = $this->clear_cache( |
|
| 2358 | - $relationName, |
|
| 2359 | - $otherObjectModelObjectOrID |
|
| 2360 | - ); |
|
| 2361 | - } |
|
| 2362 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2363 | - $otherObject->clear_cache( |
|
| 2364 | - $this->get_model()->get_this_model_name(), |
|
| 2365 | - $this |
|
| 2366 | - ); |
|
| 2367 | - } |
|
| 2368 | - return $otherObject; |
|
| 2369 | - } |
|
| 2370 | - |
|
| 2371 | - |
|
| 2372 | - /** |
|
| 2373 | - * Removes ALL the related things for the $relationName. |
|
| 2374 | - * |
|
| 2375 | - * @param string $relationName |
|
| 2376 | - * @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 |
|
| 2377 | - * @return EE_Base_Class |
|
| 2378 | - * @throws ReflectionException |
|
| 2379 | - * @throws InvalidArgumentException |
|
| 2380 | - * @throws InvalidInterfaceException |
|
| 2381 | - * @throws InvalidDataTypeException |
|
| 2382 | - * @throws EE_Error |
|
| 2383 | - */ |
|
| 2384 | - public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2385 | - { |
|
| 2386 | - if ($this->ID()) { |
|
| 2387 | - // if this exists in the DB, save the relation change to the DB too |
|
| 2388 | - $otherObjects = $this->get_model()->remove_relations( |
|
| 2389 | - $this, |
|
| 2390 | - $relationName, |
|
| 2391 | - $where_query_params |
|
| 2392 | - ); |
|
| 2393 | - $this->clear_cache( |
|
| 2394 | - $relationName, |
|
| 2395 | - null, |
|
| 2396 | - true |
|
| 2397 | - ); |
|
| 2398 | - } else { |
|
| 2399 | - // this doesn't exist in the DB, just remove it from the cache |
|
| 2400 | - $otherObjects = $this->clear_cache( |
|
| 2401 | - $relationName, |
|
| 2402 | - null, |
|
| 2403 | - true |
|
| 2404 | - ); |
|
| 2405 | - } |
|
| 2406 | - if (is_array($otherObjects)) { |
|
| 2407 | - foreach ($otherObjects as $otherObject) { |
|
| 2408 | - $otherObject->clear_cache( |
|
| 2409 | - $this->get_model()->get_this_model_name(), |
|
| 2410 | - $this |
|
| 2411 | - ); |
|
| 2412 | - } |
|
| 2413 | - } |
|
| 2414 | - return $otherObjects; |
|
| 2415 | - } |
|
| 2416 | - |
|
| 2417 | - |
|
| 2418 | - /** |
|
| 2419 | - * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2420 | - * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2421 | - * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2422 | - * because we want to get even deleted items etc. |
|
| 2423 | - * |
|
| 2424 | - * @param string $relationName key in the model's _model_relations array |
|
| 2425 | - * @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 |
|
| 2426 | - * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
|
| 2427 | - * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
|
| 2428 | - * results if you want IDs |
|
| 2429 | - * @throws ReflectionException |
|
| 2430 | - * @throws InvalidArgumentException |
|
| 2431 | - * @throws InvalidInterfaceException |
|
| 2432 | - * @throws InvalidDataTypeException |
|
| 2433 | - * @throws EE_Error |
|
| 2434 | - */ |
|
| 2435 | - public function get_many_related($relationName, $query_params = array()) |
|
| 2436 | - { |
|
| 2437 | - if ($this->ID()) { |
|
| 2438 | - // this exists in the DB, so get the related things from either the cache or the DB |
|
| 2439 | - // if there are query parameters, forget about caching the related model objects. |
|
| 2440 | - if ($query_params) { |
|
| 2441 | - $related_model_objects = $this->get_model()->get_all_related( |
|
| 2442 | - $this, |
|
| 2443 | - $relationName, |
|
| 2444 | - $query_params |
|
| 2445 | - ); |
|
| 2446 | - } else { |
|
| 2447 | - // did we already cache the result of this query? |
|
| 2448 | - $cached_results = $this->get_all_from_cache($relationName); |
|
| 2449 | - if (! $cached_results) { |
|
| 2450 | - $related_model_objects = $this->get_model()->get_all_related( |
|
| 2451 | - $this, |
|
| 2452 | - $relationName, |
|
| 2453 | - $query_params |
|
| 2454 | - ); |
|
| 2455 | - // if no query parameters were passed, then we got all the related model objects |
|
| 2456 | - // for that relation. We can cache them then. |
|
| 2457 | - foreach ($related_model_objects as $related_model_object) { |
|
| 2458 | - $this->cache($relationName, $related_model_object); |
|
| 2459 | - } |
|
| 2460 | - } else { |
|
| 2461 | - $related_model_objects = $cached_results; |
|
| 2462 | - } |
|
| 2463 | - } |
|
| 2464 | - } else { |
|
| 2465 | - // this doesn't exist in the DB, so just get the related things from the cache |
|
| 2466 | - $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2467 | - } |
|
| 2468 | - return $related_model_objects; |
|
| 2469 | - } |
|
| 2470 | - |
|
| 2471 | - |
|
| 2472 | - /** |
|
| 2473 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2474 | - * unless otherwise specified in the $query_params |
|
| 2475 | - * |
|
| 2476 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2477 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2478 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2479 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2480 | - * that by the setting $distinct to TRUE; |
|
| 2481 | - * @return int |
|
| 2482 | - * @throws ReflectionException |
|
| 2483 | - * @throws InvalidArgumentException |
|
| 2484 | - * @throws InvalidInterfaceException |
|
| 2485 | - * @throws InvalidDataTypeException |
|
| 2486 | - * @throws EE_Error |
|
| 2487 | - */ |
|
| 2488 | - public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2489 | - { |
|
| 2490 | - return $this->get_model()->count_related( |
|
| 2491 | - $this, |
|
| 2492 | - $relation_name, |
|
| 2493 | - $query_params, |
|
| 2494 | - $field_to_count, |
|
| 2495 | - $distinct |
|
| 2496 | - ); |
|
| 2497 | - } |
|
| 2498 | - |
|
| 2499 | - |
|
| 2500 | - /** |
|
| 2501 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2502 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2503 | - * |
|
| 2504 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2505 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2506 | - * @param string $field_to_sum name of field to count by. |
|
| 2507 | - * By default, uses primary key |
|
| 2508 | - * (which doesn't make much sense, so you should probably change it) |
|
| 2509 | - * @return int |
|
| 2510 | - * @throws ReflectionException |
|
| 2511 | - * @throws InvalidArgumentException |
|
| 2512 | - * @throws InvalidInterfaceException |
|
| 2513 | - * @throws InvalidDataTypeException |
|
| 2514 | - * @throws EE_Error |
|
| 2515 | - */ |
|
| 2516 | - public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2517 | - { |
|
| 2518 | - return $this->get_model()->sum_related( |
|
| 2519 | - $this, |
|
| 2520 | - $relation_name, |
|
| 2521 | - $query_params, |
|
| 2522 | - $field_to_sum |
|
| 2523 | - ); |
|
| 2524 | - } |
|
| 2525 | - |
|
| 2526 | - |
|
| 2527 | - /** |
|
| 2528 | - * Gets the first (ie, one) related model object of the specified type. |
|
| 2529 | - * |
|
| 2530 | - * @param string $relationName key in the model's _model_relations array |
|
| 2531 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2532 | - * @return EE_Base_Class (not an array, a single object) |
|
| 2533 | - * @throws ReflectionException |
|
| 2534 | - * @throws InvalidArgumentException |
|
| 2535 | - * @throws InvalidInterfaceException |
|
| 2536 | - * @throws InvalidDataTypeException |
|
| 2537 | - * @throws EE_Error |
|
| 2538 | - */ |
|
| 2539 | - public function get_first_related($relationName, $query_params = array()) |
|
| 2540 | - { |
|
| 2541 | - $model = $this->get_model(); |
|
| 2542 | - if ($this->ID()) {// this exists in the DB, get from the cache OR the DB |
|
| 2543 | - // if they've provided some query parameters, don't bother trying to cache the result |
|
| 2544 | - // also make sure we're not caching the result of get_first_related |
|
| 2545 | - // on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2546 | - if ($query_params |
|
| 2547 | - || ! $model->related_settings_for($relationName) |
|
| 2548 | - instanceof |
|
| 2549 | - EE_Belongs_To_Relation |
|
| 2550 | - ) { |
|
| 2551 | - $related_model_object = $model->get_first_related( |
|
| 2552 | - $this, |
|
| 2553 | - $relationName, |
|
| 2554 | - $query_params |
|
| 2555 | - ); |
|
| 2556 | - } else { |
|
| 2557 | - // first, check if we've already cached the result of this query |
|
| 2558 | - $cached_result = $this->get_one_from_cache($relationName); |
|
| 2559 | - if (! $cached_result) { |
|
| 2560 | - $related_model_object = $model->get_first_related( |
|
| 2561 | - $this, |
|
| 2562 | - $relationName, |
|
| 2563 | - $query_params |
|
| 2564 | - ); |
|
| 2565 | - $this->cache($relationName, $related_model_object); |
|
| 2566 | - } else { |
|
| 2567 | - $related_model_object = $cached_result; |
|
| 2568 | - } |
|
| 2569 | - } |
|
| 2570 | - } else { |
|
| 2571 | - $related_model_object = null; |
|
| 2572 | - // this doesn't exist in the Db, |
|
| 2573 | - // but maybe the relation is of type belongs to, and so the related thing might |
|
| 2574 | - if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2575 | - $related_model_object = $model->get_first_related( |
|
| 2576 | - $this, |
|
| 2577 | - $relationName, |
|
| 2578 | - $query_params |
|
| 2579 | - ); |
|
| 2580 | - } |
|
| 2581 | - // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
|
| 2582 | - // just get what's cached on this object |
|
| 2583 | - if (! $related_model_object) { |
|
| 2584 | - $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2585 | - } |
|
| 2586 | - } |
|
| 2587 | - return $related_model_object; |
|
| 2588 | - } |
|
| 2589 | - |
|
| 2590 | - |
|
| 2591 | - /** |
|
| 2592 | - * Does a delete on all related objects of type $relationName and removes |
|
| 2593 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2594 | - * of blocking related model objects) does nothing. If the related model objects are |
|
| 2595 | - * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2596 | - * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2597 | - * |
|
| 2598 | - * @param string $relationName |
|
| 2599 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2600 | - * @return int how many deleted |
|
| 2601 | - * @throws ReflectionException |
|
| 2602 | - * @throws InvalidArgumentException |
|
| 2603 | - * @throws InvalidInterfaceException |
|
| 2604 | - * @throws InvalidDataTypeException |
|
| 2605 | - * @throws EE_Error |
|
| 2606 | - */ |
|
| 2607 | - public function delete_related($relationName, $query_params = array()) |
|
| 2608 | - { |
|
| 2609 | - if ($this->ID()) { |
|
| 2610 | - $count = $this->get_model()->delete_related( |
|
| 2611 | - $this, |
|
| 2612 | - $relationName, |
|
| 2613 | - $query_params |
|
| 2614 | - ); |
|
| 2615 | - } else { |
|
| 2616 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2617 | - $this->clear_cache($relationName, null, true); |
|
| 2618 | - } |
|
| 2619 | - return $count; |
|
| 2620 | - } |
|
| 2621 | - |
|
| 2622 | - |
|
| 2623 | - /** |
|
| 2624 | - * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2625 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2626 | - * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2627 | - * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2628 | - * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2629 | - * |
|
| 2630 | - * @param string $relationName |
|
| 2631 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2632 | - * @return int how many deleted (including those soft deleted) |
|
| 2633 | - * @throws ReflectionException |
|
| 2634 | - * @throws InvalidArgumentException |
|
| 2635 | - * @throws InvalidInterfaceException |
|
| 2636 | - * @throws InvalidDataTypeException |
|
| 2637 | - * @throws EE_Error |
|
| 2638 | - */ |
|
| 2639 | - public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2640 | - { |
|
| 2641 | - if ($this->ID()) { |
|
| 2642 | - $count = $this->get_model()->delete_related_permanently( |
|
| 2643 | - $this, |
|
| 2644 | - $relationName, |
|
| 2645 | - $query_params |
|
| 2646 | - ); |
|
| 2647 | - } else { |
|
| 2648 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2649 | - } |
|
| 2650 | - $this->clear_cache($relationName, null, true); |
|
| 2651 | - return $count; |
|
| 2652 | - } |
|
| 2653 | - |
|
| 2654 | - |
|
| 2655 | - /** |
|
| 2656 | - * is_set |
|
| 2657 | - * Just a simple utility function children can use for checking if property exists |
|
| 2658 | - * |
|
| 2659 | - * @access public |
|
| 2660 | - * @param string $field_name property to check |
|
| 2661 | - * @return bool TRUE if existing,FALSE if not. |
|
| 2662 | - */ |
|
| 2663 | - public function is_set($field_name) |
|
| 2664 | - { |
|
| 2665 | - return isset($this->_fields[ $field_name ]); |
|
| 2666 | - } |
|
| 2667 | - |
|
| 2668 | - |
|
| 2669 | - /** |
|
| 2670 | - * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2671 | - * EE_Error exception if they don't |
|
| 2672 | - * |
|
| 2673 | - * @param mixed (string|array) $properties properties to check |
|
| 2674 | - * @throws EE_Error |
|
| 2675 | - * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2676 | - */ |
|
| 2677 | - protected function _property_exists($properties) |
|
| 2678 | - { |
|
| 2679 | - foreach ((array) $properties as $property_name) { |
|
| 2680 | - // first make sure this property exists |
|
| 2681 | - if (! $this->_fields[ $property_name ]) { |
|
| 2682 | - throw new EE_Error( |
|
| 2683 | - sprintf( |
|
| 2684 | - esc_html__( |
|
| 2685 | - 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2686 | - 'event_espresso' |
|
| 2687 | - ), |
|
| 2688 | - $property_name |
|
| 2689 | - ) |
|
| 2690 | - ); |
|
| 2691 | - } |
|
| 2692 | - } |
|
| 2693 | - return true; |
|
| 2694 | - } |
|
| 2695 | - |
|
| 2696 | - |
|
| 2697 | - /** |
|
| 2698 | - * This simply returns an array of model fields for this object |
|
| 2699 | - * |
|
| 2700 | - * @return array |
|
| 2701 | - * @throws ReflectionException |
|
| 2702 | - * @throws InvalidArgumentException |
|
| 2703 | - * @throws InvalidInterfaceException |
|
| 2704 | - * @throws InvalidDataTypeException |
|
| 2705 | - * @throws EE_Error |
|
| 2706 | - */ |
|
| 2707 | - public function model_field_array() |
|
| 2708 | - { |
|
| 2709 | - $fields = $this->get_model()->field_settings(false); |
|
| 2710 | - $properties = array(); |
|
| 2711 | - // remove prepended underscore |
|
| 2712 | - foreach ($fields as $field_name => $settings) { |
|
| 2713 | - $properties[ $field_name ] = $this->get($field_name); |
|
| 2714 | - } |
|
| 2715 | - return $properties; |
|
| 2716 | - } |
|
| 2717 | - |
|
| 2718 | - |
|
| 2719 | - /** |
|
| 2720 | - * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2721 | - * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2722 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
|
| 2723 | - * Instead of requiring a plugin to extend the EE_Base_Class |
|
| 2724 | - * (which works fine is there's only 1 plugin, but when will that happen?) |
|
| 2725 | - * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
|
| 2726 | - * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
|
| 2727 | - * and accepts 2 arguments: the object on which the function was called, |
|
| 2728 | - * and an array of the original arguments passed to the function. |
|
| 2729 | - * Whatever their callback function returns will be returned by this function. |
|
| 2730 | - * Example: in functions.php (or in a plugin): |
|
| 2731 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
|
| 2732 | - * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2733 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2734 | - * return $previousReturnValue.$returnString; |
|
| 2735 | - * } |
|
| 2736 | - * require('EE_Answer.class.php'); |
|
| 2737 | - * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2738 | - * echo $answer->my_callback('monkeys',100); |
|
| 2739 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2740 | - * |
|
| 2741 | - * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2742 | - * @param array $args array of original arguments passed to the function |
|
| 2743 | - * @throws EE_Error |
|
| 2744 | - * @return mixed whatever the plugin which calls add_filter decides |
|
| 2745 | - */ |
|
| 2746 | - public function __call($methodName, $args) |
|
| 2747 | - { |
|
| 2748 | - $className = get_class($this); |
|
| 2749 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2750 | - if (! has_filter($tagName)) { |
|
| 2751 | - throw new EE_Error( |
|
| 2752 | - sprintf( |
|
| 2753 | - esc_html__( |
|
| 2754 | - "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;}", |
|
| 2755 | - 'event_espresso' |
|
| 2756 | - ), |
|
| 2757 | - $methodName, |
|
| 2758 | - $className, |
|
| 2759 | - $tagName |
|
| 2760 | - ) |
|
| 2761 | - ); |
|
| 2762 | - } |
|
| 2763 | - return apply_filters($tagName, null, $this, $args); |
|
| 2764 | - } |
|
| 2765 | - |
|
| 2766 | - |
|
| 2767 | - /** |
|
| 2768 | - * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2769 | - * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2770 | - * |
|
| 2771 | - * @param string $meta_key |
|
| 2772 | - * @param mixed $meta_value |
|
| 2773 | - * @param mixed $previous_value |
|
| 2774 | - * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2775 | - * NOTE: if the values haven't changed, returns 0 |
|
| 2776 | - * @throws InvalidArgumentException |
|
| 2777 | - * @throws InvalidInterfaceException |
|
| 2778 | - * @throws InvalidDataTypeException |
|
| 2779 | - * @throws EE_Error |
|
| 2780 | - * @throws ReflectionException |
|
| 2781 | - */ |
|
| 2782 | - public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2783 | - { |
|
| 2784 | - $query_params = array( |
|
| 2785 | - array( |
|
| 2786 | - 'EXM_key' => $meta_key, |
|
| 2787 | - 'OBJ_ID' => $this->ID(), |
|
| 2788 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2789 | - ), |
|
| 2790 | - ); |
|
| 2791 | - if ($previous_value !== null) { |
|
| 2792 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2793 | - } |
|
| 2794 | - $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2795 | - if (! $existing_rows_like_that) { |
|
| 2796 | - return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2797 | - } |
|
| 2798 | - foreach ($existing_rows_like_that as $existing_row) { |
|
| 2799 | - $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2800 | - } |
|
| 2801 | - return count($existing_rows_like_that); |
|
| 2802 | - } |
|
| 2803 | - |
|
| 2804 | - |
|
| 2805 | - /** |
|
| 2806 | - * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2807 | - * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2808 | - * extra meta row was entered, false if not |
|
| 2809 | - * |
|
| 2810 | - * @param string $meta_key |
|
| 2811 | - * @param mixed $meta_value |
|
| 2812 | - * @param boolean $unique |
|
| 2813 | - * @return boolean |
|
| 2814 | - * @throws InvalidArgumentException |
|
| 2815 | - * @throws InvalidInterfaceException |
|
| 2816 | - * @throws InvalidDataTypeException |
|
| 2817 | - * @throws EE_Error |
|
| 2818 | - * @throws ReflectionException |
|
| 2819 | - * @throws ReflectionException |
|
| 2820 | - */ |
|
| 2821 | - public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2822 | - { |
|
| 2823 | - if ($unique) { |
|
| 2824 | - $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2825 | - array( |
|
| 2826 | - array( |
|
| 2827 | - 'EXM_key' => $meta_key, |
|
| 2828 | - 'OBJ_ID' => $this->ID(), |
|
| 2829 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2830 | - ), |
|
| 2831 | - ) |
|
| 2832 | - ); |
|
| 2833 | - if ($existing_extra_meta) { |
|
| 2834 | - return false; |
|
| 2835 | - } |
|
| 2836 | - } |
|
| 2837 | - $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2838 | - array( |
|
| 2839 | - 'EXM_key' => $meta_key, |
|
| 2840 | - 'EXM_value' => $meta_value, |
|
| 2841 | - 'OBJ_ID' => $this->ID(), |
|
| 2842 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2843 | - ) |
|
| 2844 | - ); |
|
| 2845 | - $new_extra_meta->save(); |
|
| 2846 | - return true; |
|
| 2847 | - } |
|
| 2848 | - |
|
| 2849 | - |
|
| 2850 | - /** |
|
| 2851 | - * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2852 | - * is specified, only deletes extra meta records with that value. |
|
| 2853 | - * |
|
| 2854 | - * @param string $meta_key |
|
| 2855 | - * @param mixed $meta_value |
|
| 2856 | - * @return int number of extra meta rows deleted |
|
| 2857 | - * @throws InvalidArgumentException |
|
| 2858 | - * @throws InvalidInterfaceException |
|
| 2859 | - * @throws InvalidDataTypeException |
|
| 2860 | - * @throws EE_Error |
|
| 2861 | - * @throws ReflectionException |
|
| 2862 | - */ |
|
| 2863 | - public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2864 | - { |
|
| 2865 | - $query_params = array( |
|
| 2866 | - array( |
|
| 2867 | - 'EXM_key' => $meta_key, |
|
| 2868 | - 'OBJ_ID' => $this->ID(), |
|
| 2869 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2870 | - ), |
|
| 2871 | - ); |
|
| 2872 | - if ($meta_value !== null) { |
|
| 2873 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2874 | - } |
|
| 2875 | - return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2876 | - } |
|
| 2877 | - |
|
| 2878 | - |
|
| 2879 | - /** |
|
| 2880 | - * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2881 | - * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2882 | - * You can specify $default is case you haven't found the extra meta |
|
| 2883 | - * |
|
| 2884 | - * @param string $meta_key |
|
| 2885 | - * @param boolean $single |
|
| 2886 | - * @param mixed $default if we don't find anything, what should we return? |
|
| 2887 | - * @return mixed single value if $single; array if ! $single |
|
| 2888 | - * @throws ReflectionException |
|
| 2889 | - * @throws InvalidArgumentException |
|
| 2890 | - * @throws InvalidInterfaceException |
|
| 2891 | - * @throws InvalidDataTypeException |
|
| 2892 | - * @throws EE_Error |
|
| 2893 | - */ |
|
| 2894 | - public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2895 | - { |
|
| 2896 | - if ($single) { |
|
| 2897 | - $result = $this->get_first_related( |
|
| 2898 | - 'Extra_Meta', |
|
| 2899 | - array(array('EXM_key' => $meta_key)) |
|
| 2900 | - ); |
|
| 2901 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2902 | - return $result->value(); |
|
| 2903 | - } |
|
| 2904 | - } else { |
|
| 2905 | - $results = $this->get_many_related( |
|
| 2906 | - 'Extra_Meta', |
|
| 2907 | - array(array('EXM_key' => $meta_key)) |
|
| 2908 | - ); |
|
| 2909 | - if ($results) { |
|
| 2910 | - $values = array(); |
|
| 2911 | - foreach ($results as $result) { |
|
| 2912 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2913 | - $values[ $result->ID() ] = $result->value(); |
|
| 2914 | - } |
|
| 2915 | - } |
|
| 2916 | - return $values; |
|
| 2917 | - } |
|
| 2918 | - } |
|
| 2919 | - // if nothing discovered yet return default. |
|
| 2920 | - return apply_filters( |
|
| 2921 | - 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
| 2922 | - $default, |
|
| 2923 | - $meta_key, |
|
| 2924 | - $single, |
|
| 2925 | - $this |
|
| 2926 | - ); |
|
| 2927 | - } |
|
| 2928 | - |
|
| 2929 | - |
|
| 2930 | - /** |
|
| 2931 | - * Returns a simple array of all the extra meta associated with this model object. |
|
| 2932 | - * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2933 | - * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2934 | - * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2935 | - * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2936 | - * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2937 | - * finally the extra meta's value as each sub-value. (eg |
|
| 2938 | - * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2939 | - * |
|
| 2940 | - * @param boolean $one_of_each_key |
|
| 2941 | - * @return array |
|
| 2942 | - * @throws ReflectionException |
|
| 2943 | - * @throws InvalidArgumentException |
|
| 2944 | - * @throws InvalidInterfaceException |
|
| 2945 | - * @throws InvalidDataTypeException |
|
| 2946 | - * @throws EE_Error |
|
| 2947 | - */ |
|
| 2948 | - public function all_extra_meta_array($one_of_each_key = true) |
|
| 2949 | - { |
|
| 2950 | - $return_array = array(); |
|
| 2951 | - if ($one_of_each_key) { |
|
| 2952 | - $extra_meta_objs = $this->get_many_related( |
|
| 2953 | - 'Extra_Meta', |
|
| 2954 | - array('group_by' => 'EXM_key') |
|
| 2955 | - ); |
|
| 2956 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2957 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2958 | - $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
| 2959 | - } |
|
| 2960 | - } |
|
| 2961 | - } else { |
|
| 2962 | - $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2963 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2964 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2965 | - if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
| 2966 | - $return_array[ $extra_meta_obj->key() ] = array(); |
|
| 2967 | - } |
|
| 2968 | - $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
| 2969 | - } |
|
| 2970 | - } |
|
| 2971 | - } |
|
| 2972 | - return $return_array; |
|
| 2973 | - } |
|
| 2974 | - |
|
| 2975 | - |
|
| 2976 | - /** |
|
| 2977 | - * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2978 | - * |
|
| 2979 | - * @return string |
|
| 2980 | - * @throws ReflectionException |
|
| 2981 | - * @throws InvalidArgumentException |
|
| 2982 | - * @throws InvalidInterfaceException |
|
| 2983 | - * @throws InvalidDataTypeException |
|
| 2984 | - * @throws EE_Error |
|
| 2985 | - */ |
|
| 2986 | - public function name() |
|
| 2987 | - { |
|
| 2988 | - // find a field that's not a text field |
|
| 2989 | - $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2990 | - if ($field_we_can_use) { |
|
| 2991 | - return $this->get($field_we_can_use->get_name()); |
|
| 2992 | - } |
|
| 2993 | - $first_few_properties = $this->model_field_array(); |
|
| 2994 | - $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2995 | - $name_parts = array(); |
|
| 2996 | - foreach ($first_few_properties as $name => $value) { |
|
| 2997 | - $name_parts[] = "$name:$value"; |
|
| 2998 | - } |
|
| 2999 | - return implode(',', $name_parts); |
|
| 3000 | - } |
|
| 3001 | - |
|
| 3002 | - |
|
| 3003 | - /** |
|
| 3004 | - * in_entity_map |
|
| 3005 | - * Checks if this model object has been proven to already be in the entity map |
|
| 3006 | - * |
|
| 3007 | - * @return boolean |
|
| 3008 | - * @throws ReflectionException |
|
| 3009 | - * @throws InvalidArgumentException |
|
| 3010 | - * @throws InvalidInterfaceException |
|
| 3011 | - * @throws InvalidDataTypeException |
|
| 3012 | - * @throws EE_Error |
|
| 3013 | - */ |
|
| 3014 | - public function in_entity_map() |
|
| 3015 | - { |
|
| 3016 | - // well, if we looked, did we find it in the entity map? |
|
| 3017 | - return $this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this; |
|
| 3018 | - } |
|
| 3019 | - |
|
| 3020 | - |
|
| 3021 | - /** |
|
| 3022 | - * refresh_from_db |
|
| 3023 | - * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 3024 | - * |
|
| 3025 | - * @throws ReflectionException |
|
| 3026 | - * @throws InvalidArgumentException |
|
| 3027 | - * @throws InvalidInterfaceException |
|
| 3028 | - * @throws InvalidDataTypeException |
|
| 3029 | - * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 3030 | - * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 3031 | - */ |
|
| 3032 | - public function refresh_from_db() |
|
| 3033 | - { |
|
| 3034 | - if ($this->ID() && $this->in_entity_map()) { |
|
| 3035 | - $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 3036 | - } else { |
|
| 3037 | - // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 3038 | - // if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 3039 | - // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 3040 | - // absolutely nothing in it for this ID |
|
| 3041 | - if (WP_DEBUG) { |
|
| 3042 | - throw new EE_Error( |
|
| 3043 | - sprintf( |
|
| 3044 | - esc_html__( |
|
| 3045 | - '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.', |
|
| 3046 | - 'event_espresso' |
|
| 3047 | - ), |
|
| 3048 | - $this->ID(), |
|
| 3049 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 3050 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 3051 | - ) |
|
| 3052 | - ); |
|
| 3053 | - } |
|
| 3054 | - } |
|
| 3055 | - } |
|
| 3056 | - |
|
| 3057 | - |
|
| 3058 | - /** |
|
| 3059 | - * Change $fields' values to $new_value_sql (which is a string of raw SQL) |
|
| 3060 | - * |
|
| 3061 | - * @since 4.9.80.p |
|
| 3062 | - * @param EE_Model_Field_Base[] $fields |
|
| 3063 | - * @param string $new_value_sql |
|
| 3064 | - * example: 'column_name=123', |
|
| 3065 | - * or 'column_name=column_name+1', |
|
| 3066 | - * or 'column_name= CASE |
|
| 3067 | - * WHEN (`column_name` + `other_column` + 5) <= `yet_another_column` |
|
| 3068 | - * THEN `column_name` + 5 |
|
| 3069 | - * ELSE `column_name` |
|
| 3070 | - * END' |
|
| 3071 | - * Also updates $field on this model object with the latest value from the database. |
|
| 3072 | - * @return bool |
|
| 3073 | - * @throws EE_Error |
|
| 3074 | - * @throws InvalidArgumentException |
|
| 3075 | - * @throws InvalidDataTypeException |
|
| 3076 | - * @throws InvalidInterfaceException |
|
| 3077 | - * @throws ReflectionException |
|
| 3078 | - */ |
|
| 3079 | - protected function updateFieldsInDB($fields, $new_value_sql) |
|
| 3080 | - { |
|
| 3081 | - // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
|
| 3082 | - // if it wasn't even there to start off. |
|
| 3083 | - if (! $this->ID()) { |
|
| 3084 | - $this->save(); |
|
| 3085 | - } |
|
| 3086 | - global $wpdb; |
|
| 3087 | - if (empty($fields)) { |
|
| 3088 | - throw new InvalidArgumentException( |
|
| 3089 | - esc_html__( |
|
| 3090 | - 'EE_Base_Class::updateFieldsInDB was passed an empty array of fields.', |
|
| 3091 | - 'event_espresso' |
|
| 3092 | - ) |
|
| 3093 | - ); |
|
| 3094 | - } |
|
| 3095 | - $first_field = reset($fields); |
|
| 3096 | - $table_alias = $first_field->get_table_alias(); |
|
| 3097 | - foreach ($fields as $field) { |
|
| 3098 | - if ($table_alias !== $field->get_table_alias()) { |
|
| 3099 | - throw new InvalidArgumentException( |
|
| 3100 | - sprintf( |
|
| 3101 | - esc_html__( |
|
| 3102 | - // @codingStandardsIgnoreStart |
|
| 3103 | - '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.', |
|
| 3104 | - // @codingStandardsIgnoreEnd |
|
| 3105 | - 'event_espresso' |
|
| 3106 | - ), |
|
| 3107 | - $table_alias, |
|
| 3108 | - $field->get_table_alias() |
|
| 3109 | - ) |
|
| 3110 | - ); |
|
| 3111 | - } |
|
| 3112 | - } |
|
| 3113 | - // Ok the fields are now known to all be for the same table. Proceed with creating the SQL to update it. |
|
| 3114 | - $table_obj = $this->get_model()->get_table_obj_by_alias($table_alias); |
|
| 3115 | - $table_pk_value = $this->ID(); |
|
| 3116 | - $table_name = $table_obj->get_table_name(); |
|
| 3117 | - if ($table_obj instanceof EE_Secondary_Table) { |
|
| 3118 | - $table_pk_field_name = $table_obj->get_fk_on_table(); |
|
| 3119 | - } else { |
|
| 3120 | - $table_pk_field_name = $table_obj->get_pk_column(); |
|
| 3121 | - } |
|
| 3122 | - |
|
| 3123 | - $query = |
|
| 3124 | - "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 | + break; |
|
| 1585 | + case 'D': |
|
| 1586 | + $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
| 1587 | + $datetime_value, |
|
| 1588 | + $this->_fields[ $fieldname ] |
|
| 1589 | + ); |
|
| 1590 | + break; |
|
| 1591 | + case 'B': |
|
| 1592 | + $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
| 1593 | + break; |
|
| 1594 | + } |
|
| 1595 | + $this->_clear_cached_property($fieldname); |
|
| 1596 | + } |
|
| 1597 | + |
|
| 1598 | + |
|
| 1599 | + /** |
|
| 1600 | + * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1601 | + * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1602 | + * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1603 | + * that could lead to some unexpected results! |
|
| 1604 | + * |
|
| 1605 | + * @access public |
|
| 1606 | + * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1607 | + * value being returned. |
|
| 1608 | + * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1609 | + * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1610 | + * @param string $prepend You can include something to prepend on the timestamp |
|
| 1611 | + * @param string $append You can include something to append on the timestamp |
|
| 1612 | + * @throws ReflectionException |
|
| 1613 | + * @throws InvalidArgumentException |
|
| 1614 | + * @throws InvalidInterfaceException |
|
| 1615 | + * @throws InvalidDataTypeException |
|
| 1616 | + * @throws EE_Error |
|
| 1617 | + * @return string timestamp |
|
| 1618 | + */ |
|
| 1619 | + public function display_in_my_timezone( |
|
| 1620 | + $field_name, |
|
| 1621 | + $callback = 'get_datetime', |
|
| 1622 | + $args = null, |
|
| 1623 | + $prepend = '', |
|
| 1624 | + $append = '' |
|
| 1625 | + ) { |
|
| 1626 | + $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1627 | + if ($timezone === $this->_timezone) { |
|
| 1628 | + return ''; |
|
| 1629 | + } |
|
| 1630 | + $original_timezone = $this->_timezone; |
|
| 1631 | + $this->set_timezone($timezone); |
|
| 1632 | + $fn = (array) $field_name; |
|
| 1633 | + $args = array_merge($fn, (array) $args); |
|
| 1634 | + if (! method_exists($this, $callback)) { |
|
| 1635 | + throw new EE_Error( |
|
| 1636 | + sprintf( |
|
| 1637 | + esc_html__( |
|
| 1638 | + 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1639 | + 'event_espresso' |
|
| 1640 | + ), |
|
| 1641 | + $callback |
|
| 1642 | + ) |
|
| 1643 | + ); |
|
| 1644 | + } |
|
| 1645 | + $args = (array) $args; |
|
| 1646 | + $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1647 | + $this->set_timezone($original_timezone); |
|
| 1648 | + return $return; |
|
| 1649 | + } |
|
| 1650 | + |
|
| 1651 | + |
|
| 1652 | + /** |
|
| 1653 | + * Deletes this model object. |
|
| 1654 | + * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1655 | + * override |
|
| 1656 | + * `EE_Base_Class::_delete` NOT this class. |
|
| 1657 | + * |
|
| 1658 | + * @return boolean | int |
|
| 1659 | + * @throws ReflectionException |
|
| 1660 | + * @throws InvalidArgumentException |
|
| 1661 | + * @throws InvalidInterfaceException |
|
| 1662 | + * @throws InvalidDataTypeException |
|
| 1663 | + * @throws EE_Error |
|
| 1664 | + */ |
|
| 1665 | + public function delete() |
|
| 1666 | + { |
|
| 1667 | + /** |
|
| 1668 | + * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1669 | + * Note: |
|
| 1670 | + * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1671 | + * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1672 | + * For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1673 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1674 | + * |
|
| 1675 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1676 | + */ |
|
| 1677 | + do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1678 | + $result = $this->_delete(); |
|
| 1679 | + /** |
|
| 1680 | + * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1681 | + * Note: |
|
| 1682 | + * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1683 | + * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1684 | + * For example `EE_Soft_Base_Class::_delete` |
|
| 1685 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1686 | + * |
|
| 1687 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1688 | + * @param boolean $result |
|
| 1689 | + */ |
|
| 1690 | + do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1691 | + return $result; |
|
| 1692 | + } |
|
| 1693 | + |
|
| 1694 | + |
|
| 1695 | + /** |
|
| 1696 | + * Calls the specific delete method for the instantiated class. |
|
| 1697 | + * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1698 | + * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1699 | + * `EE_Base_Class::delete` |
|
| 1700 | + * |
|
| 1701 | + * @return bool|int |
|
| 1702 | + * @throws ReflectionException |
|
| 1703 | + * @throws InvalidArgumentException |
|
| 1704 | + * @throws InvalidInterfaceException |
|
| 1705 | + * @throws InvalidDataTypeException |
|
| 1706 | + * @throws EE_Error |
|
| 1707 | + */ |
|
| 1708 | + protected function _delete() |
|
| 1709 | + { |
|
| 1710 | + return $this->delete_permanently(); |
|
| 1711 | + } |
|
| 1712 | + |
|
| 1713 | + |
|
| 1714 | + /** |
|
| 1715 | + * Deletes this model object permanently from db |
|
| 1716 | + * (but keep in mind related models may block the delete and return an error) |
|
| 1717 | + * |
|
| 1718 | + * @return bool | int |
|
| 1719 | + * @throws ReflectionException |
|
| 1720 | + * @throws InvalidArgumentException |
|
| 1721 | + * @throws InvalidInterfaceException |
|
| 1722 | + * @throws InvalidDataTypeException |
|
| 1723 | + * @throws EE_Error |
|
| 1724 | + */ |
|
| 1725 | + public function delete_permanently() |
|
| 1726 | + { |
|
| 1727 | + /** |
|
| 1728 | + * Called just before HARD deleting a model object |
|
| 1729 | + * |
|
| 1730 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1731 | + */ |
|
| 1732 | + do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1733 | + $model = $this->get_model(); |
|
| 1734 | + $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1735 | + $this->refresh_cache_of_related_objects(); |
|
| 1736 | + /** |
|
| 1737 | + * Called just after HARD deleting a model object |
|
| 1738 | + * |
|
| 1739 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1740 | + * @param boolean $result |
|
| 1741 | + */ |
|
| 1742 | + do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1743 | + return $result; |
|
| 1744 | + } |
|
| 1745 | + |
|
| 1746 | + |
|
| 1747 | + /** |
|
| 1748 | + * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1749 | + * related model objects |
|
| 1750 | + * |
|
| 1751 | + * @throws ReflectionException |
|
| 1752 | + * @throws InvalidArgumentException |
|
| 1753 | + * @throws InvalidInterfaceException |
|
| 1754 | + * @throws InvalidDataTypeException |
|
| 1755 | + * @throws EE_Error |
|
| 1756 | + */ |
|
| 1757 | + public function refresh_cache_of_related_objects() |
|
| 1758 | + { |
|
| 1759 | + $model = $this->get_model(); |
|
| 1760 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1761 | + if (! empty($this->_model_relations[ $relation_name ])) { |
|
| 1762 | + $related_objects = $this->_model_relations[ $relation_name ]; |
|
| 1763 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1764 | + // this relation only stores a single model object, not an array |
|
| 1765 | + // but let's make it consistent |
|
| 1766 | + $related_objects = array($related_objects); |
|
| 1767 | + } |
|
| 1768 | + foreach ($related_objects as $related_object) { |
|
| 1769 | + // only refresh their cache if they're in memory |
|
| 1770 | + if ($related_object instanceof EE_Base_Class) { |
|
| 1771 | + $related_object->clear_cache( |
|
| 1772 | + $model->get_this_model_name(), |
|
| 1773 | + $this |
|
| 1774 | + ); |
|
| 1775 | + } |
|
| 1776 | + } |
|
| 1777 | + } |
|
| 1778 | + } |
|
| 1779 | + } |
|
| 1780 | + |
|
| 1781 | + |
|
| 1782 | + /** |
|
| 1783 | + * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1784 | + * object just before saving. |
|
| 1785 | + * |
|
| 1786 | + * @access public |
|
| 1787 | + * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1788 | + * if provided during the save() method (often client code will change the fields' |
|
| 1789 | + * values before calling save) |
|
| 1790 | + * @throws InvalidArgumentException |
|
| 1791 | + * @throws InvalidInterfaceException |
|
| 1792 | + * @throws InvalidDataTypeException |
|
| 1793 | + * @throws EE_Error |
|
| 1794 | + * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1795 | + * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1796 | + * @throws ReflectionException |
|
| 1797 | + * @throws ReflectionException |
|
| 1798 | + * @throws ReflectionException |
|
| 1799 | + */ |
|
| 1800 | + public function save($set_cols_n_values = array()) |
|
| 1801 | + { |
|
| 1802 | + $model = $this->get_model(); |
|
| 1803 | + /** |
|
| 1804 | + * Filters the fields we're about to save on the model object |
|
| 1805 | + * |
|
| 1806 | + * @param array $set_cols_n_values |
|
| 1807 | + * @param EE_Base_Class $model_object |
|
| 1808 | + */ |
|
| 1809 | + $set_cols_n_values = (array) apply_filters( |
|
| 1810 | + 'FHEE__EE_Base_Class__save__set_cols_n_values', |
|
| 1811 | + $set_cols_n_values, |
|
| 1812 | + $this |
|
| 1813 | + ); |
|
| 1814 | + // set attributes as provided in $set_cols_n_values |
|
| 1815 | + foreach ($set_cols_n_values as $column => $value) { |
|
| 1816 | + $this->set($column, $value); |
|
| 1817 | + } |
|
| 1818 | + // no changes ? then don't do anything |
|
| 1819 | + if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1820 | + return 0; |
|
| 1821 | + } |
|
| 1822 | + /** |
|
| 1823 | + * Saving a model object. |
|
| 1824 | + * Before we perform a save, this action is fired. |
|
| 1825 | + * |
|
| 1826 | + * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1827 | + */ |
|
| 1828 | + do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1829 | + if (! $this->allow_persist()) { |
|
| 1830 | + return 0; |
|
| 1831 | + } |
|
| 1832 | + // now get current attribute values |
|
| 1833 | + $save_cols_n_values = $this->_fields; |
|
| 1834 | + // if the object already has an ID, update it. Otherwise, insert it |
|
| 1835 | + // also: change the assumption about values passed to the model NOT being prepare dby the model object. |
|
| 1836 | + // They have been |
|
| 1837 | + $old_assumption_concerning_value_preparation = $model |
|
| 1838 | + ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1839 | + $model->assume_values_already_prepared_by_model_object(true); |
|
| 1840 | + // does this model have an autoincrement PK? |
|
| 1841 | + if ($model->has_primary_key_field()) { |
|
| 1842 | + if ($model->get_primary_key_field()->is_auto_increment()) { |
|
| 1843 | + // ok check if it's set, if so: update; if not, insert |
|
| 1844 | + if (! empty($save_cols_n_values[ $model->primary_key_name() ])) { |
|
| 1845 | + $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1846 | + } else { |
|
| 1847 | + unset($save_cols_n_values[ $model->primary_key_name() ]); |
|
| 1848 | + $results = $model->insert($save_cols_n_values); |
|
| 1849 | + if ($results) { |
|
| 1850 | + // if successful, set the primary key |
|
| 1851 | + // but don't use the normal SET method, because it will check if |
|
| 1852 | + // an item with the same ID exists in the mapper & db, then |
|
| 1853 | + // will find it in the db (because we just added it) and THAT object |
|
| 1854 | + // will get added to the mapper before we can add this one! |
|
| 1855 | + // but if we just avoid using the SET method, all that headache can be avoided |
|
| 1856 | + $pk_field_name = $model->primary_key_name(); |
|
| 1857 | + $this->_fields[ $pk_field_name ] = $results; |
|
| 1858 | + $this->_clear_cached_property($pk_field_name); |
|
| 1859 | + $model->add_to_entity_map($this); |
|
| 1860 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1861 | + } |
|
| 1862 | + } |
|
| 1863 | + } else {// PK is NOT auto-increment |
|
| 1864 | + // so check if one like it already exists in the db |
|
| 1865 | + if ($model->exists_by_ID($this->ID())) { |
|
| 1866 | + if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1867 | + throw new EE_Error( |
|
| 1868 | + sprintf( |
|
| 1869 | + esc_html__( |
|
| 1870 | + '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', |
|
| 1871 | + 'event_espresso' |
|
| 1872 | + ), |
|
| 1873 | + get_class($this), |
|
| 1874 | + get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1875 | + get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1876 | + '<br />' |
|
| 1877 | + ) |
|
| 1878 | + ); |
|
| 1879 | + } |
|
| 1880 | + $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1881 | + } else { |
|
| 1882 | + $results = $model->insert($save_cols_n_values); |
|
| 1883 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1884 | + } |
|
| 1885 | + } |
|
| 1886 | + } else {// there is NO primary key |
|
| 1887 | + $already_in_db = false; |
|
| 1888 | + foreach ($model->unique_indexes() as $index) { |
|
| 1889 | + $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1890 | + if ($model->exists(array($uniqueness_where_params))) { |
|
| 1891 | + $already_in_db = true; |
|
| 1892 | + } |
|
| 1893 | + } |
|
| 1894 | + if ($already_in_db) { |
|
| 1895 | + $combined_pk_fields_n_values = array_intersect_key( |
|
| 1896 | + $save_cols_n_values, |
|
| 1897 | + $model->get_combined_primary_key_fields() |
|
| 1898 | + ); |
|
| 1899 | + $results = $model->update( |
|
| 1900 | + $save_cols_n_values, |
|
| 1901 | + $combined_pk_fields_n_values |
|
| 1902 | + ); |
|
| 1903 | + } else { |
|
| 1904 | + $results = $model->insert($save_cols_n_values); |
|
| 1905 | + } |
|
| 1906 | + } |
|
| 1907 | + // restore the old assumption about values being prepared by the model object |
|
| 1908 | + $model->assume_values_already_prepared_by_model_object( |
|
| 1909 | + $old_assumption_concerning_value_preparation |
|
| 1910 | + ); |
|
| 1911 | + /** |
|
| 1912 | + * After saving the model object this action is called |
|
| 1913 | + * |
|
| 1914 | + * @param EE_Base_Class $model_object which was just saved |
|
| 1915 | + * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1916 | + * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1917 | + */ |
|
| 1918 | + do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1919 | + $this->_has_changes = false; |
|
| 1920 | + return $results; |
|
| 1921 | + } |
|
| 1922 | + |
|
| 1923 | + |
|
| 1924 | + /** |
|
| 1925 | + * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1926 | + * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1927 | + * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1928 | + * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1929 | + * 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 |
|
| 1930 | + * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1931 | + * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1932 | + * |
|
| 1933 | + * @return void |
|
| 1934 | + * @throws ReflectionException |
|
| 1935 | + * @throws InvalidArgumentException |
|
| 1936 | + * @throws InvalidInterfaceException |
|
| 1937 | + * @throws InvalidDataTypeException |
|
| 1938 | + * @throws EE_Error |
|
| 1939 | + */ |
|
| 1940 | + protected function _update_cached_related_model_objs_fks() |
|
| 1941 | + { |
|
| 1942 | + $model = $this->get_model(); |
|
| 1943 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1944 | + if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1945 | + foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1946 | + $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1947 | + $model->get_this_model_name() |
|
| 1948 | + ); |
|
| 1949 | + $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1950 | + if ($related_model_obj_in_cache->ID()) { |
|
| 1951 | + $related_model_obj_in_cache->save(); |
|
| 1952 | + } |
|
| 1953 | + } |
|
| 1954 | + } |
|
| 1955 | + } |
|
| 1956 | + } |
|
| 1957 | + |
|
| 1958 | + |
|
| 1959 | + /** |
|
| 1960 | + * Saves this model object and its NEW cached relations to the database. |
|
| 1961 | + * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1962 | + * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1963 | + * because otherwise, there's a potential for infinite looping of saving |
|
| 1964 | + * Saves the cached related model objects, and ensures the relation between them |
|
| 1965 | + * and this object and properly setup |
|
| 1966 | + * |
|
| 1967 | + * @return int ID of new model object on save; 0 on failure+ |
|
| 1968 | + * @throws ReflectionException |
|
| 1969 | + * @throws InvalidArgumentException |
|
| 1970 | + * @throws InvalidInterfaceException |
|
| 1971 | + * @throws InvalidDataTypeException |
|
| 1972 | + * @throws EE_Error |
|
| 1973 | + */ |
|
| 1974 | + public function save_new_cached_related_model_objs() |
|
| 1975 | + { |
|
| 1976 | + // make sure this has been saved |
|
| 1977 | + if (! $this->ID()) { |
|
| 1978 | + $id = $this->save(); |
|
| 1979 | + } else { |
|
| 1980 | + $id = $this->ID(); |
|
| 1981 | + } |
|
| 1982 | + // now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1983 | + foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1984 | + if ($this->_model_relations[ $relationName ]) { |
|
| 1985 | + // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1986 | + // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1987 | + /* @var $related_model_obj EE_Base_Class */ |
|
| 1988 | + if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1989 | + // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1990 | + // but ONLY if it DOES NOT exist in the DB |
|
| 1991 | + $related_model_obj = $this->_model_relations[ $relationName ]; |
|
| 1992 | + // if( ! $related_model_obj->ID()){ |
|
| 1993 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1994 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1995 | + // } |
|
| 1996 | + } else { |
|
| 1997 | + foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
| 1998 | + // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1999 | + // but ONLY if it DOES NOT exist in the DB |
|
| 2000 | + // if( ! $related_model_obj->ID()){ |
|
| 2001 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 2002 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 2003 | + // } |
|
| 2004 | + } |
|
| 2005 | + } |
|
| 2006 | + } |
|
| 2007 | + } |
|
| 2008 | + return $id; |
|
| 2009 | + } |
|
| 2010 | + |
|
| 2011 | + |
|
| 2012 | + /** |
|
| 2013 | + * for getting a model while instantiated. |
|
| 2014 | + * |
|
| 2015 | + * @return EEM_Base | EEM_CPT_Base |
|
| 2016 | + * @throws ReflectionException |
|
| 2017 | + * @throws InvalidArgumentException |
|
| 2018 | + * @throws InvalidInterfaceException |
|
| 2019 | + * @throws InvalidDataTypeException |
|
| 2020 | + * @throws EE_Error |
|
| 2021 | + */ |
|
| 2022 | + public function get_model() |
|
| 2023 | + { |
|
| 2024 | + if (! $this->_model) { |
|
| 2025 | + $modelName = self::_get_model_classname(get_class($this)); |
|
| 2026 | + $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 2027 | + } else { |
|
| 2028 | + $this->_model->set_timezone($this->_timezone); |
|
| 2029 | + } |
|
| 2030 | + return $this->_model; |
|
| 2031 | + } |
|
| 2032 | + |
|
| 2033 | + |
|
| 2034 | + /** |
|
| 2035 | + * @param $props_n_values |
|
| 2036 | + * @param $classname |
|
| 2037 | + * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 2038 | + * @throws ReflectionException |
|
| 2039 | + * @throws InvalidArgumentException |
|
| 2040 | + * @throws InvalidInterfaceException |
|
| 2041 | + * @throws InvalidDataTypeException |
|
| 2042 | + * @throws EE_Error |
|
| 2043 | + */ |
|
| 2044 | + protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 2045 | + { |
|
| 2046 | + // TODO: will not work for Term_Relationships because they have no PK! |
|
| 2047 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2048 | + if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2049 | + && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2050 | + ) { |
|
| 2051 | + $id = $props_n_values[ $primary_id_ref ]; |
|
| 2052 | + return self::_get_model($classname)->get_from_entity_map($id); |
|
| 2053 | + } |
|
| 2054 | + return false; |
|
| 2055 | + } |
|
| 2056 | + |
|
| 2057 | + |
|
| 2058 | + /** |
|
| 2059 | + * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 2060 | + * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 2061 | + * 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 |
|
| 2062 | + * we return false. |
|
| 2063 | + * |
|
| 2064 | + * @param array $props_n_values incoming array of properties and their values |
|
| 2065 | + * @param string $classname the classname of the child class |
|
| 2066 | + * @param null $timezone |
|
| 2067 | + * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 2068 | + * date_format and the second value is the time format |
|
| 2069 | + * @return mixed (EE_Base_Class|bool) |
|
| 2070 | + * @throws InvalidArgumentException |
|
| 2071 | + * @throws InvalidInterfaceException |
|
| 2072 | + * @throws InvalidDataTypeException |
|
| 2073 | + * @throws EE_Error |
|
| 2074 | + * @throws ReflectionException |
|
| 2075 | + * @throws ReflectionException |
|
| 2076 | + * @throws ReflectionException |
|
| 2077 | + */ |
|
| 2078 | + protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 2079 | + { |
|
| 2080 | + $existing = null; |
|
| 2081 | + $model = self::_get_model($classname, $timezone); |
|
| 2082 | + if ($model->has_primary_key_field()) { |
|
| 2083 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2084 | + if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2085 | + && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2086 | + ) { |
|
| 2087 | + $existing = $model->get_one_by_ID( |
|
| 2088 | + $props_n_values[ $primary_id_ref ] |
|
| 2089 | + ); |
|
| 2090 | + } |
|
| 2091 | + } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 2092 | + // no primary key on this model, but there's still a matching item in the DB |
|
| 2093 | + $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 2094 | + self::_get_model($classname, $timezone) |
|
| 2095 | + ->get_index_primary_key_string($props_n_values) |
|
| 2096 | + ); |
|
| 2097 | + } |
|
| 2098 | + if ($existing) { |
|
| 2099 | + // set date formats if present before setting values |
|
| 2100 | + if (! empty($date_formats) && is_array($date_formats)) { |
|
| 2101 | + $existing->set_date_format($date_formats[0]); |
|
| 2102 | + $existing->set_time_format($date_formats[1]); |
|
| 2103 | + } else { |
|
| 2104 | + // set default formats for date and time |
|
| 2105 | + $existing->set_date_format(get_option('date_format')); |
|
| 2106 | + $existing->set_time_format(get_option('time_format')); |
|
| 2107 | + } |
|
| 2108 | + foreach ($props_n_values as $property => $field_value) { |
|
| 2109 | + $existing->set($property, $field_value); |
|
| 2110 | + } |
|
| 2111 | + return $existing; |
|
| 2112 | + } |
|
| 2113 | + return false; |
|
| 2114 | + } |
|
| 2115 | + |
|
| 2116 | + |
|
| 2117 | + /** |
|
| 2118 | + * Gets the EEM_*_Model for this class |
|
| 2119 | + * |
|
| 2120 | + * @access public now, as this is more convenient |
|
| 2121 | + * @param $classname |
|
| 2122 | + * @param null $timezone |
|
| 2123 | + * @throws ReflectionException |
|
| 2124 | + * @throws InvalidArgumentException |
|
| 2125 | + * @throws InvalidInterfaceException |
|
| 2126 | + * @throws InvalidDataTypeException |
|
| 2127 | + * @throws EE_Error |
|
| 2128 | + * @return EEM_Base |
|
| 2129 | + */ |
|
| 2130 | + protected static function _get_model($classname, $timezone = null) |
|
| 2131 | + { |
|
| 2132 | + // find model for this class |
|
| 2133 | + if (! $classname) { |
|
| 2134 | + throw new EE_Error( |
|
| 2135 | + sprintf( |
|
| 2136 | + esc_html__( |
|
| 2137 | + 'What were you thinking calling _get_model(%s)?? You need to specify the class name', |
|
| 2138 | + 'event_espresso' |
|
| 2139 | + ), |
|
| 2140 | + $classname |
|
| 2141 | + ) |
|
| 2142 | + ); |
|
| 2143 | + } |
|
| 2144 | + $modelName = self::_get_model_classname($classname); |
|
| 2145 | + return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 2146 | + } |
|
| 2147 | + |
|
| 2148 | + |
|
| 2149 | + /** |
|
| 2150 | + * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 2151 | + * |
|
| 2152 | + * @param string $model_classname |
|
| 2153 | + * @param null $timezone |
|
| 2154 | + * @return EEM_Base |
|
| 2155 | + * @throws ReflectionException |
|
| 2156 | + * @throws InvalidArgumentException |
|
| 2157 | + * @throws InvalidInterfaceException |
|
| 2158 | + * @throws InvalidDataTypeException |
|
| 2159 | + * @throws EE_Error |
|
| 2160 | + */ |
|
| 2161 | + protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 2162 | + { |
|
| 2163 | + $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 2164 | + $model = EE_Registry::instance()->load_model($model_classname); |
|
| 2165 | + $model->set_timezone($timezone); |
|
| 2166 | + return $model; |
|
| 2167 | + } |
|
| 2168 | + |
|
| 2169 | + |
|
| 2170 | + /** |
|
| 2171 | + * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 2172 | + * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 2173 | + * |
|
| 2174 | + * @param null $model_name |
|
| 2175 | + * @return string like EEM_Attendee |
|
| 2176 | + */ |
|
| 2177 | + private static function _get_model_classname($model_name = null) |
|
| 2178 | + { |
|
| 2179 | + if (strpos($model_name, 'EE_') === 0) { |
|
| 2180 | + $model_classname = str_replace('EE_', 'EEM_', $model_name); |
|
| 2181 | + } else { |
|
| 2182 | + $model_classname = 'EEM_' . $model_name; |
|
| 2183 | + } |
|
| 2184 | + return $model_classname; |
|
| 2185 | + } |
|
| 2186 | + |
|
| 2187 | + |
|
| 2188 | + /** |
|
| 2189 | + * returns the name of the primary key attribute |
|
| 2190 | + * |
|
| 2191 | + * @param null $classname |
|
| 2192 | + * @throws ReflectionException |
|
| 2193 | + * @throws InvalidArgumentException |
|
| 2194 | + * @throws InvalidInterfaceException |
|
| 2195 | + * @throws InvalidDataTypeException |
|
| 2196 | + * @throws EE_Error |
|
| 2197 | + * @return string |
|
| 2198 | + */ |
|
| 2199 | + protected static function _get_primary_key_name($classname = null) |
|
| 2200 | + { |
|
| 2201 | + if (! $classname) { |
|
| 2202 | + throw new EE_Error( |
|
| 2203 | + sprintf( |
|
| 2204 | + esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
|
| 2205 | + $classname |
|
| 2206 | + ) |
|
| 2207 | + ); |
|
| 2208 | + } |
|
| 2209 | + return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 2210 | + } |
|
| 2211 | + |
|
| 2212 | + |
|
| 2213 | + /** |
|
| 2214 | + * Gets the value of the primary key. |
|
| 2215 | + * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 2216 | + * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 2217 | + * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 2218 | + * |
|
| 2219 | + * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 2220 | + * @throws ReflectionException |
|
| 2221 | + * @throws InvalidArgumentException |
|
| 2222 | + * @throws InvalidInterfaceException |
|
| 2223 | + * @throws InvalidDataTypeException |
|
| 2224 | + * @throws EE_Error |
|
| 2225 | + */ |
|
| 2226 | + public function ID() |
|
| 2227 | + { |
|
| 2228 | + $model = $this->get_model(); |
|
| 2229 | + // now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 2230 | + if ($model->has_primary_key_field()) { |
|
| 2231 | + return $this->_fields[ $model->primary_key_name() ]; |
|
| 2232 | + } |
|
| 2233 | + return $model->get_index_primary_key_string($this->_fields); |
|
| 2234 | + } |
|
| 2235 | + |
|
| 2236 | + |
|
| 2237 | + /** |
|
| 2238 | + * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 2239 | + * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 2240 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 2241 | + * |
|
| 2242 | + * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 2243 | + * @param string $relationName eg 'Events','Question',etc. |
|
| 2244 | + * an attendee to a group, you also want to specify which role they |
|
| 2245 | + * will have in that group. So you would use this parameter to |
|
| 2246 | + * specify array('role-column-name'=>'role-id') |
|
| 2247 | + * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 2248 | + * allow you to further constrict the relation to being added. |
|
| 2249 | + * However, keep in mind that the columns (keys) given must match a |
|
| 2250 | + * column on the JOIN table and currently only the HABTM models |
|
| 2251 | + * accept these additional conditions. Also remember that if an |
|
| 2252 | + * exact match isn't found for these extra cols/val pairs, then a |
|
| 2253 | + * NEW row is created in the join table. |
|
| 2254 | + * @param null $cache_id |
|
| 2255 | + * @throws ReflectionException |
|
| 2256 | + * @throws InvalidArgumentException |
|
| 2257 | + * @throws InvalidInterfaceException |
|
| 2258 | + * @throws InvalidDataTypeException |
|
| 2259 | + * @throws EE_Error |
|
| 2260 | + * @return EE_Base_Class the object the relation was added to |
|
| 2261 | + */ |
|
| 2262 | + public function _add_relation_to( |
|
| 2263 | + $otherObjectModelObjectOrID, |
|
| 2264 | + $relationName, |
|
| 2265 | + $extra_join_model_fields_n_values = array(), |
|
| 2266 | + $cache_id = null |
|
| 2267 | + ) { |
|
| 2268 | + $model = $this->get_model(); |
|
| 2269 | + // if this thing exists in the DB, save the relation to the DB |
|
| 2270 | + if ($this->ID()) { |
|
| 2271 | + $otherObject = $model->add_relationship_to( |
|
| 2272 | + $this, |
|
| 2273 | + $otherObjectModelObjectOrID, |
|
| 2274 | + $relationName, |
|
| 2275 | + $extra_join_model_fields_n_values |
|
| 2276 | + ); |
|
| 2277 | + // clear cache so future get_many_related and get_first_related() return new results. |
|
| 2278 | + $this->clear_cache($relationName, $otherObject, true); |
|
| 2279 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2280 | + $otherObject->clear_cache($model->get_this_model_name(), $this); |
|
| 2281 | + } |
|
| 2282 | + } else { |
|
| 2283 | + // this thing doesn't exist in the DB, so just cache it |
|
| 2284 | + if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2285 | + throw new EE_Error( |
|
| 2286 | + sprintf( |
|
| 2287 | + esc_html__( |
|
| 2288 | + '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', |
|
| 2289 | + 'event_espresso' |
|
| 2290 | + ), |
|
| 2291 | + $otherObjectModelObjectOrID, |
|
| 2292 | + get_class($this) |
|
| 2293 | + ) |
|
| 2294 | + ); |
|
| 2295 | + } |
|
| 2296 | + $otherObject = $otherObjectModelObjectOrID; |
|
| 2297 | + $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 2298 | + } |
|
| 2299 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2300 | + // fix the reciprocal relation too |
|
| 2301 | + if ($otherObject->ID()) { |
|
| 2302 | + // its saved so assumed relations exist in the DB, so we can just |
|
| 2303 | + // clear the cache so future queries use the updated info in the DB |
|
| 2304 | + $otherObject->clear_cache( |
|
| 2305 | + $model->get_this_model_name(), |
|
| 2306 | + null, |
|
| 2307 | + true |
|
| 2308 | + ); |
|
| 2309 | + } else { |
|
| 2310 | + // it's not saved, so it caches relations like this |
|
| 2311 | + $otherObject->cache($model->get_this_model_name(), $this); |
|
| 2312 | + } |
|
| 2313 | + } |
|
| 2314 | + return $otherObject; |
|
| 2315 | + } |
|
| 2316 | + |
|
| 2317 | + |
|
| 2318 | + /** |
|
| 2319 | + * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2320 | + * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2321 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2322 | + * from the cache |
|
| 2323 | + * |
|
| 2324 | + * @param mixed $otherObjectModelObjectOrID |
|
| 2325 | + * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2326 | + * to the DB yet |
|
| 2327 | + * @param string $relationName |
|
| 2328 | + * @param array $where_query |
|
| 2329 | + * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2330 | + * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2331 | + * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2332 | + * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
|
| 2333 | + * deleted. |
|
| 2334 | + * @return EE_Base_Class the relation was removed from |
|
| 2335 | + * @throws ReflectionException |
|
| 2336 | + * @throws InvalidArgumentException |
|
| 2337 | + * @throws InvalidInterfaceException |
|
| 2338 | + * @throws InvalidDataTypeException |
|
| 2339 | + * @throws EE_Error |
|
| 2340 | + */ |
|
| 2341 | + public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2342 | + { |
|
| 2343 | + if ($this->ID()) { |
|
| 2344 | + // if this exists in the DB, save the relation change to the DB too |
|
| 2345 | + $otherObject = $this->get_model()->remove_relationship_to( |
|
| 2346 | + $this, |
|
| 2347 | + $otherObjectModelObjectOrID, |
|
| 2348 | + $relationName, |
|
| 2349 | + $where_query |
|
| 2350 | + ); |
|
| 2351 | + $this->clear_cache( |
|
| 2352 | + $relationName, |
|
| 2353 | + $otherObject |
|
| 2354 | + ); |
|
| 2355 | + } else { |
|
| 2356 | + // this doesn't exist in the DB, just remove it from the cache |
|
| 2357 | + $otherObject = $this->clear_cache( |
|
| 2358 | + $relationName, |
|
| 2359 | + $otherObjectModelObjectOrID |
|
| 2360 | + ); |
|
| 2361 | + } |
|
| 2362 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2363 | + $otherObject->clear_cache( |
|
| 2364 | + $this->get_model()->get_this_model_name(), |
|
| 2365 | + $this |
|
| 2366 | + ); |
|
| 2367 | + } |
|
| 2368 | + return $otherObject; |
|
| 2369 | + } |
|
| 2370 | + |
|
| 2371 | + |
|
| 2372 | + /** |
|
| 2373 | + * Removes ALL the related things for the $relationName. |
|
| 2374 | + * |
|
| 2375 | + * @param string $relationName |
|
| 2376 | + * @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 |
|
| 2377 | + * @return EE_Base_Class |
|
| 2378 | + * @throws ReflectionException |
|
| 2379 | + * @throws InvalidArgumentException |
|
| 2380 | + * @throws InvalidInterfaceException |
|
| 2381 | + * @throws InvalidDataTypeException |
|
| 2382 | + * @throws EE_Error |
|
| 2383 | + */ |
|
| 2384 | + public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2385 | + { |
|
| 2386 | + if ($this->ID()) { |
|
| 2387 | + // if this exists in the DB, save the relation change to the DB too |
|
| 2388 | + $otherObjects = $this->get_model()->remove_relations( |
|
| 2389 | + $this, |
|
| 2390 | + $relationName, |
|
| 2391 | + $where_query_params |
|
| 2392 | + ); |
|
| 2393 | + $this->clear_cache( |
|
| 2394 | + $relationName, |
|
| 2395 | + null, |
|
| 2396 | + true |
|
| 2397 | + ); |
|
| 2398 | + } else { |
|
| 2399 | + // this doesn't exist in the DB, just remove it from the cache |
|
| 2400 | + $otherObjects = $this->clear_cache( |
|
| 2401 | + $relationName, |
|
| 2402 | + null, |
|
| 2403 | + true |
|
| 2404 | + ); |
|
| 2405 | + } |
|
| 2406 | + if (is_array($otherObjects)) { |
|
| 2407 | + foreach ($otherObjects as $otherObject) { |
|
| 2408 | + $otherObject->clear_cache( |
|
| 2409 | + $this->get_model()->get_this_model_name(), |
|
| 2410 | + $this |
|
| 2411 | + ); |
|
| 2412 | + } |
|
| 2413 | + } |
|
| 2414 | + return $otherObjects; |
|
| 2415 | + } |
|
| 2416 | + |
|
| 2417 | + |
|
| 2418 | + /** |
|
| 2419 | + * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2420 | + * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2421 | + * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2422 | + * because we want to get even deleted items etc. |
|
| 2423 | + * |
|
| 2424 | + * @param string $relationName key in the model's _model_relations array |
|
| 2425 | + * @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 |
|
| 2426 | + * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
|
| 2427 | + * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
|
| 2428 | + * results if you want IDs |
|
| 2429 | + * @throws ReflectionException |
|
| 2430 | + * @throws InvalidArgumentException |
|
| 2431 | + * @throws InvalidInterfaceException |
|
| 2432 | + * @throws InvalidDataTypeException |
|
| 2433 | + * @throws EE_Error |
|
| 2434 | + */ |
|
| 2435 | + public function get_many_related($relationName, $query_params = array()) |
|
| 2436 | + { |
|
| 2437 | + if ($this->ID()) { |
|
| 2438 | + // this exists in the DB, so get the related things from either the cache or the DB |
|
| 2439 | + // if there are query parameters, forget about caching the related model objects. |
|
| 2440 | + if ($query_params) { |
|
| 2441 | + $related_model_objects = $this->get_model()->get_all_related( |
|
| 2442 | + $this, |
|
| 2443 | + $relationName, |
|
| 2444 | + $query_params |
|
| 2445 | + ); |
|
| 2446 | + } else { |
|
| 2447 | + // did we already cache the result of this query? |
|
| 2448 | + $cached_results = $this->get_all_from_cache($relationName); |
|
| 2449 | + if (! $cached_results) { |
|
| 2450 | + $related_model_objects = $this->get_model()->get_all_related( |
|
| 2451 | + $this, |
|
| 2452 | + $relationName, |
|
| 2453 | + $query_params |
|
| 2454 | + ); |
|
| 2455 | + // if no query parameters were passed, then we got all the related model objects |
|
| 2456 | + // for that relation. We can cache them then. |
|
| 2457 | + foreach ($related_model_objects as $related_model_object) { |
|
| 2458 | + $this->cache($relationName, $related_model_object); |
|
| 2459 | + } |
|
| 2460 | + } else { |
|
| 2461 | + $related_model_objects = $cached_results; |
|
| 2462 | + } |
|
| 2463 | + } |
|
| 2464 | + } else { |
|
| 2465 | + // this doesn't exist in the DB, so just get the related things from the cache |
|
| 2466 | + $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2467 | + } |
|
| 2468 | + return $related_model_objects; |
|
| 2469 | + } |
|
| 2470 | + |
|
| 2471 | + |
|
| 2472 | + /** |
|
| 2473 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2474 | + * unless otherwise specified in the $query_params |
|
| 2475 | + * |
|
| 2476 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2477 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2478 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2479 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2480 | + * that by the setting $distinct to TRUE; |
|
| 2481 | + * @return int |
|
| 2482 | + * @throws ReflectionException |
|
| 2483 | + * @throws InvalidArgumentException |
|
| 2484 | + * @throws InvalidInterfaceException |
|
| 2485 | + * @throws InvalidDataTypeException |
|
| 2486 | + * @throws EE_Error |
|
| 2487 | + */ |
|
| 2488 | + public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2489 | + { |
|
| 2490 | + return $this->get_model()->count_related( |
|
| 2491 | + $this, |
|
| 2492 | + $relation_name, |
|
| 2493 | + $query_params, |
|
| 2494 | + $field_to_count, |
|
| 2495 | + $distinct |
|
| 2496 | + ); |
|
| 2497 | + } |
|
| 2498 | + |
|
| 2499 | + |
|
| 2500 | + /** |
|
| 2501 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2502 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2503 | + * |
|
| 2504 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2505 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2506 | + * @param string $field_to_sum name of field to count by. |
|
| 2507 | + * By default, uses primary key |
|
| 2508 | + * (which doesn't make much sense, so you should probably change it) |
|
| 2509 | + * @return int |
|
| 2510 | + * @throws ReflectionException |
|
| 2511 | + * @throws InvalidArgumentException |
|
| 2512 | + * @throws InvalidInterfaceException |
|
| 2513 | + * @throws InvalidDataTypeException |
|
| 2514 | + * @throws EE_Error |
|
| 2515 | + */ |
|
| 2516 | + public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2517 | + { |
|
| 2518 | + return $this->get_model()->sum_related( |
|
| 2519 | + $this, |
|
| 2520 | + $relation_name, |
|
| 2521 | + $query_params, |
|
| 2522 | + $field_to_sum |
|
| 2523 | + ); |
|
| 2524 | + } |
|
| 2525 | + |
|
| 2526 | + |
|
| 2527 | + /** |
|
| 2528 | + * Gets the first (ie, one) related model object of the specified type. |
|
| 2529 | + * |
|
| 2530 | + * @param string $relationName key in the model's _model_relations array |
|
| 2531 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2532 | + * @return EE_Base_Class (not an array, a single object) |
|
| 2533 | + * @throws ReflectionException |
|
| 2534 | + * @throws InvalidArgumentException |
|
| 2535 | + * @throws InvalidInterfaceException |
|
| 2536 | + * @throws InvalidDataTypeException |
|
| 2537 | + * @throws EE_Error |
|
| 2538 | + */ |
|
| 2539 | + public function get_first_related($relationName, $query_params = array()) |
|
| 2540 | + { |
|
| 2541 | + $model = $this->get_model(); |
|
| 2542 | + if ($this->ID()) {// this exists in the DB, get from the cache OR the DB |
|
| 2543 | + // if they've provided some query parameters, don't bother trying to cache the result |
|
| 2544 | + // also make sure we're not caching the result of get_first_related |
|
| 2545 | + // on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2546 | + if ($query_params |
|
| 2547 | + || ! $model->related_settings_for($relationName) |
|
| 2548 | + instanceof |
|
| 2549 | + EE_Belongs_To_Relation |
|
| 2550 | + ) { |
|
| 2551 | + $related_model_object = $model->get_first_related( |
|
| 2552 | + $this, |
|
| 2553 | + $relationName, |
|
| 2554 | + $query_params |
|
| 2555 | + ); |
|
| 2556 | + } else { |
|
| 2557 | + // first, check if we've already cached the result of this query |
|
| 2558 | + $cached_result = $this->get_one_from_cache($relationName); |
|
| 2559 | + if (! $cached_result) { |
|
| 2560 | + $related_model_object = $model->get_first_related( |
|
| 2561 | + $this, |
|
| 2562 | + $relationName, |
|
| 2563 | + $query_params |
|
| 2564 | + ); |
|
| 2565 | + $this->cache($relationName, $related_model_object); |
|
| 2566 | + } else { |
|
| 2567 | + $related_model_object = $cached_result; |
|
| 2568 | + } |
|
| 2569 | + } |
|
| 2570 | + } else { |
|
| 2571 | + $related_model_object = null; |
|
| 2572 | + // this doesn't exist in the Db, |
|
| 2573 | + // but maybe the relation is of type belongs to, and so the related thing might |
|
| 2574 | + if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2575 | + $related_model_object = $model->get_first_related( |
|
| 2576 | + $this, |
|
| 2577 | + $relationName, |
|
| 2578 | + $query_params |
|
| 2579 | + ); |
|
| 2580 | + } |
|
| 2581 | + // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
|
| 2582 | + // just get what's cached on this object |
|
| 2583 | + if (! $related_model_object) { |
|
| 2584 | + $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2585 | + } |
|
| 2586 | + } |
|
| 2587 | + return $related_model_object; |
|
| 2588 | + } |
|
| 2589 | + |
|
| 2590 | + |
|
| 2591 | + /** |
|
| 2592 | + * Does a delete on all related objects of type $relationName and removes |
|
| 2593 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2594 | + * of blocking related model objects) does nothing. If the related model objects are |
|
| 2595 | + * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2596 | + * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2597 | + * |
|
| 2598 | + * @param string $relationName |
|
| 2599 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2600 | + * @return int how many deleted |
|
| 2601 | + * @throws ReflectionException |
|
| 2602 | + * @throws InvalidArgumentException |
|
| 2603 | + * @throws InvalidInterfaceException |
|
| 2604 | + * @throws InvalidDataTypeException |
|
| 2605 | + * @throws EE_Error |
|
| 2606 | + */ |
|
| 2607 | + public function delete_related($relationName, $query_params = array()) |
|
| 2608 | + { |
|
| 2609 | + if ($this->ID()) { |
|
| 2610 | + $count = $this->get_model()->delete_related( |
|
| 2611 | + $this, |
|
| 2612 | + $relationName, |
|
| 2613 | + $query_params |
|
| 2614 | + ); |
|
| 2615 | + } else { |
|
| 2616 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2617 | + $this->clear_cache($relationName, null, true); |
|
| 2618 | + } |
|
| 2619 | + return $count; |
|
| 2620 | + } |
|
| 2621 | + |
|
| 2622 | + |
|
| 2623 | + /** |
|
| 2624 | + * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2625 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2626 | + * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2627 | + * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2628 | + * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2629 | + * |
|
| 2630 | + * @param string $relationName |
|
| 2631 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2632 | + * @return int how many deleted (including those soft deleted) |
|
| 2633 | + * @throws ReflectionException |
|
| 2634 | + * @throws InvalidArgumentException |
|
| 2635 | + * @throws InvalidInterfaceException |
|
| 2636 | + * @throws InvalidDataTypeException |
|
| 2637 | + * @throws EE_Error |
|
| 2638 | + */ |
|
| 2639 | + public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2640 | + { |
|
| 2641 | + if ($this->ID()) { |
|
| 2642 | + $count = $this->get_model()->delete_related_permanently( |
|
| 2643 | + $this, |
|
| 2644 | + $relationName, |
|
| 2645 | + $query_params |
|
| 2646 | + ); |
|
| 2647 | + } else { |
|
| 2648 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2649 | + } |
|
| 2650 | + $this->clear_cache($relationName, null, true); |
|
| 2651 | + return $count; |
|
| 2652 | + } |
|
| 2653 | + |
|
| 2654 | + |
|
| 2655 | + /** |
|
| 2656 | + * is_set |
|
| 2657 | + * Just a simple utility function children can use for checking if property exists |
|
| 2658 | + * |
|
| 2659 | + * @access public |
|
| 2660 | + * @param string $field_name property to check |
|
| 2661 | + * @return bool TRUE if existing,FALSE if not. |
|
| 2662 | + */ |
|
| 2663 | + public function is_set($field_name) |
|
| 2664 | + { |
|
| 2665 | + return isset($this->_fields[ $field_name ]); |
|
| 2666 | + } |
|
| 2667 | + |
|
| 2668 | + |
|
| 2669 | + /** |
|
| 2670 | + * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2671 | + * EE_Error exception if they don't |
|
| 2672 | + * |
|
| 2673 | + * @param mixed (string|array) $properties properties to check |
|
| 2674 | + * @throws EE_Error |
|
| 2675 | + * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2676 | + */ |
|
| 2677 | + protected function _property_exists($properties) |
|
| 2678 | + { |
|
| 2679 | + foreach ((array) $properties as $property_name) { |
|
| 2680 | + // first make sure this property exists |
|
| 2681 | + if (! $this->_fields[ $property_name ]) { |
|
| 2682 | + throw new EE_Error( |
|
| 2683 | + sprintf( |
|
| 2684 | + esc_html__( |
|
| 2685 | + 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2686 | + 'event_espresso' |
|
| 2687 | + ), |
|
| 2688 | + $property_name |
|
| 2689 | + ) |
|
| 2690 | + ); |
|
| 2691 | + } |
|
| 2692 | + } |
|
| 2693 | + return true; |
|
| 2694 | + } |
|
| 2695 | + |
|
| 2696 | + |
|
| 2697 | + /** |
|
| 2698 | + * This simply returns an array of model fields for this object |
|
| 2699 | + * |
|
| 2700 | + * @return array |
|
| 2701 | + * @throws ReflectionException |
|
| 2702 | + * @throws InvalidArgumentException |
|
| 2703 | + * @throws InvalidInterfaceException |
|
| 2704 | + * @throws InvalidDataTypeException |
|
| 2705 | + * @throws EE_Error |
|
| 2706 | + */ |
|
| 2707 | + public function model_field_array() |
|
| 2708 | + { |
|
| 2709 | + $fields = $this->get_model()->field_settings(false); |
|
| 2710 | + $properties = array(); |
|
| 2711 | + // remove prepended underscore |
|
| 2712 | + foreach ($fields as $field_name => $settings) { |
|
| 2713 | + $properties[ $field_name ] = $this->get($field_name); |
|
| 2714 | + } |
|
| 2715 | + return $properties; |
|
| 2716 | + } |
|
| 2717 | + |
|
| 2718 | + |
|
| 2719 | + /** |
|
| 2720 | + * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2721 | + * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2722 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
|
| 2723 | + * Instead of requiring a plugin to extend the EE_Base_Class |
|
| 2724 | + * (which works fine is there's only 1 plugin, but when will that happen?) |
|
| 2725 | + * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
|
| 2726 | + * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
|
| 2727 | + * and accepts 2 arguments: the object on which the function was called, |
|
| 2728 | + * and an array of the original arguments passed to the function. |
|
| 2729 | + * Whatever their callback function returns will be returned by this function. |
|
| 2730 | + * Example: in functions.php (or in a plugin): |
|
| 2731 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
|
| 2732 | + * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2733 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2734 | + * return $previousReturnValue.$returnString; |
|
| 2735 | + * } |
|
| 2736 | + * require('EE_Answer.class.php'); |
|
| 2737 | + * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2738 | + * echo $answer->my_callback('monkeys',100); |
|
| 2739 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2740 | + * |
|
| 2741 | + * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2742 | + * @param array $args array of original arguments passed to the function |
|
| 2743 | + * @throws EE_Error |
|
| 2744 | + * @return mixed whatever the plugin which calls add_filter decides |
|
| 2745 | + */ |
|
| 2746 | + public function __call($methodName, $args) |
|
| 2747 | + { |
|
| 2748 | + $className = get_class($this); |
|
| 2749 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2750 | + if (! has_filter($tagName)) { |
|
| 2751 | + throw new EE_Error( |
|
| 2752 | + sprintf( |
|
| 2753 | + esc_html__( |
|
| 2754 | + "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;}", |
|
| 2755 | + 'event_espresso' |
|
| 2756 | + ), |
|
| 2757 | + $methodName, |
|
| 2758 | + $className, |
|
| 2759 | + $tagName |
|
| 2760 | + ) |
|
| 2761 | + ); |
|
| 2762 | + } |
|
| 2763 | + return apply_filters($tagName, null, $this, $args); |
|
| 2764 | + } |
|
| 2765 | + |
|
| 2766 | + |
|
| 2767 | + /** |
|
| 2768 | + * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2769 | + * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2770 | + * |
|
| 2771 | + * @param string $meta_key |
|
| 2772 | + * @param mixed $meta_value |
|
| 2773 | + * @param mixed $previous_value |
|
| 2774 | + * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2775 | + * NOTE: if the values haven't changed, returns 0 |
|
| 2776 | + * @throws InvalidArgumentException |
|
| 2777 | + * @throws InvalidInterfaceException |
|
| 2778 | + * @throws InvalidDataTypeException |
|
| 2779 | + * @throws EE_Error |
|
| 2780 | + * @throws ReflectionException |
|
| 2781 | + */ |
|
| 2782 | + public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2783 | + { |
|
| 2784 | + $query_params = array( |
|
| 2785 | + array( |
|
| 2786 | + 'EXM_key' => $meta_key, |
|
| 2787 | + 'OBJ_ID' => $this->ID(), |
|
| 2788 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2789 | + ), |
|
| 2790 | + ); |
|
| 2791 | + if ($previous_value !== null) { |
|
| 2792 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2793 | + } |
|
| 2794 | + $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2795 | + if (! $existing_rows_like_that) { |
|
| 2796 | + return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2797 | + } |
|
| 2798 | + foreach ($existing_rows_like_that as $existing_row) { |
|
| 2799 | + $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2800 | + } |
|
| 2801 | + return count($existing_rows_like_that); |
|
| 2802 | + } |
|
| 2803 | + |
|
| 2804 | + |
|
| 2805 | + /** |
|
| 2806 | + * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2807 | + * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2808 | + * extra meta row was entered, false if not |
|
| 2809 | + * |
|
| 2810 | + * @param string $meta_key |
|
| 2811 | + * @param mixed $meta_value |
|
| 2812 | + * @param boolean $unique |
|
| 2813 | + * @return boolean |
|
| 2814 | + * @throws InvalidArgumentException |
|
| 2815 | + * @throws InvalidInterfaceException |
|
| 2816 | + * @throws InvalidDataTypeException |
|
| 2817 | + * @throws EE_Error |
|
| 2818 | + * @throws ReflectionException |
|
| 2819 | + * @throws ReflectionException |
|
| 2820 | + */ |
|
| 2821 | + public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2822 | + { |
|
| 2823 | + if ($unique) { |
|
| 2824 | + $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2825 | + array( |
|
| 2826 | + array( |
|
| 2827 | + 'EXM_key' => $meta_key, |
|
| 2828 | + 'OBJ_ID' => $this->ID(), |
|
| 2829 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2830 | + ), |
|
| 2831 | + ) |
|
| 2832 | + ); |
|
| 2833 | + if ($existing_extra_meta) { |
|
| 2834 | + return false; |
|
| 2835 | + } |
|
| 2836 | + } |
|
| 2837 | + $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2838 | + array( |
|
| 2839 | + 'EXM_key' => $meta_key, |
|
| 2840 | + 'EXM_value' => $meta_value, |
|
| 2841 | + 'OBJ_ID' => $this->ID(), |
|
| 2842 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2843 | + ) |
|
| 2844 | + ); |
|
| 2845 | + $new_extra_meta->save(); |
|
| 2846 | + return true; |
|
| 2847 | + } |
|
| 2848 | + |
|
| 2849 | + |
|
| 2850 | + /** |
|
| 2851 | + * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2852 | + * is specified, only deletes extra meta records with that value. |
|
| 2853 | + * |
|
| 2854 | + * @param string $meta_key |
|
| 2855 | + * @param mixed $meta_value |
|
| 2856 | + * @return int number of extra meta rows deleted |
|
| 2857 | + * @throws InvalidArgumentException |
|
| 2858 | + * @throws InvalidInterfaceException |
|
| 2859 | + * @throws InvalidDataTypeException |
|
| 2860 | + * @throws EE_Error |
|
| 2861 | + * @throws ReflectionException |
|
| 2862 | + */ |
|
| 2863 | + public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2864 | + { |
|
| 2865 | + $query_params = array( |
|
| 2866 | + array( |
|
| 2867 | + 'EXM_key' => $meta_key, |
|
| 2868 | + 'OBJ_ID' => $this->ID(), |
|
| 2869 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2870 | + ), |
|
| 2871 | + ); |
|
| 2872 | + if ($meta_value !== null) { |
|
| 2873 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2874 | + } |
|
| 2875 | + return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2876 | + } |
|
| 2877 | + |
|
| 2878 | + |
|
| 2879 | + /** |
|
| 2880 | + * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2881 | + * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2882 | + * You can specify $default is case you haven't found the extra meta |
|
| 2883 | + * |
|
| 2884 | + * @param string $meta_key |
|
| 2885 | + * @param boolean $single |
|
| 2886 | + * @param mixed $default if we don't find anything, what should we return? |
|
| 2887 | + * @return mixed single value if $single; array if ! $single |
|
| 2888 | + * @throws ReflectionException |
|
| 2889 | + * @throws InvalidArgumentException |
|
| 2890 | + * @throws InvalidInterfaceException |
|
| 2891 | + * @throws InvalidDataTypeException |
|
| 2892 | + * @throws EE_Error |
|
| 2893 | + */ |
|
| 2894 | + public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2895 | + { |
|
| 2896 | + if ($single) { |
|
| 2897 | + $result = $this->get_first_related( |
|
| 2898 | + 'Extra_Meta', |
|
| 2899 | + array(array('EXM_key' => $meta_key)) |
|
| 2900 | + ); |
|
| 2901 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2902 | + return $result->value(); |
|
| 2903 | + } |
|
| 2904 | + } else { |
|
| 2905 | + $results = $this->get_many_related( |
|
| 2906 | + 'Extra_Meta', |
|
| 2907 | + array(array('EXM_key' => $meta_key)) |
|
| 2908 | + ); |
|
| 2909 | + if ($results) { |
|
| 2910 | + $values = array(); |
|
| 2911 | + foreach ($results as $result) { |
|
| 2912 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2913 | + $values[ $result->ID() ] = $result->value(); |
|
| 2914 | + } |
|
| 2915 | + } |
|
| 2916 | + return $values; |
|
| 2917 | + } |
|
| 2918 | + } |
|
| 2919 | + // if nothing discovered yet return default. |
|
| 2920 | + return apply_filters( |
|
| 2921 | + 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
| 2922 | + $default, |
|
| 2923 | + $meta_key, |
|
| 2924 | + $single, |
|
| 2925 | + $this |
|
| 2926 | + ); |
|
| 2927 | + } |
|
| 2928 | + |
|
| 2929 | + |
|
| 2930 | + /** |
|
| 2931 | + * Returns a simple array of all the extra meta associated with this model object. |
|
| 2932 | + * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2933 | + * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2934 | + * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2935 | + * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2936 | + * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2937 | + * finally the extra meta's value as each sub-value. (eg |
|
| 2938 | + * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2939 | + * |
|
| 2940 | + * @param boolean $one_of_each_key |
|
| 2941 | + * @return array |
|
| 2942 | + * @throws ReflectionException |
|
| 2943 | + * @throws InvalidArgumentException |
|
| 2944 | + * @throws InvalidInterfaceException |
|
| 2945 | + * @throws InvalidDataTypeException |
|
| 2946 | + * @throws EE_Error |
|
| 2947 | + */ |
|
| 2948 | + public function all_extra_meta_array($one_of_each_key = true) |
|
| 2949 | + { |
|
| 2950 | + $return_array = array(); |
|
| 2951 | + if ($one_of_each_key) { |
|
| 2952 | + $extra_meta_objs = $this->get_many_related( |
|
| 2953 | + 'Extra_Meta', |
|
| 2954 | + array('group_by' => 'EXM_key') |
|
| 2955 | + ); |
|
| 2956 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2957 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2958 | + $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
| 2959 | + } |
|
| 2960 | + } |
|
| 2961 | + } else { |
|
| 2962 | + $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2963 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2964 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2965 | + if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
| 2966 | + $return_array[ $extra_meta_obj->key() ] = array(); |
|
| 2967 | + } |
|
| 2968 | + $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
| 2969 | + } |
|
| 2970 | + } |
|
| 2971 | + } |
|
| 2972 | + return $return_array; |
|
| 2973 | + } |
|
| 2974 | + |
|
| 2975 | + |
|
| 2976 | + /** |
|
| 2977 | + * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2978 | + * |
|
| 2979 | + * @return string |
|
| 2980 | + * @throws ReflectionException |
|
| 2981 | + * @throws InvalidArgumentException |
|
| 2982 | + * @throws InvalidInterfaceException |
|
| 2983 | + * @throws InvalidDataTypeException |
|
| 2984 | + * @throws EE_Error |
|
| 2985 | + */ |
|
| 2986 | + public function name() |
|
| 2987 | + { |
|
| 2988 | + // find a field that's not a text field |
|
| 2989 | + $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2990 | + if ($field_we_can_use) { |
|
| 2991 | + return $this->get($field_we_can_use->get_name()); |
|
| 2992 | + } |
|
| 2993 | + $first_few_properties = $this->model_field_array(); |
|
| 2994 | + $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2995 | + $name_parts = array(); |
|
| 2996 | + foreach ($first_few_properties as $name => $value) { |
|
| 2997 | + $name_parts[] = "$name:$value"; |
|
| 2998 | + } |
|
| 2999 | + return implode(',', $name_parts); |
|
| 3000 | + } |
|
| 3001 | + |
|
| 3002 | + |
|
| 3003 | + /** |
|
| 3004 | + * in_entity_map |
|
| 3005 | + * Checks if this model object has been proven to already be in the entity map |
|
| 3006 | + * |
|
| 3007 | + * @return boolean |
|
| 3008 | + * @throws ReflectionException |
|
| 3009 | + * @throws InvalidArgumentException |
|
| 3010 | + * @throws InvalidInterfaceException |
|
| 3011 | + * @throws InvalidDataTypeException |
|
| 3012 | + * @throws EE_Error |
|
| 3013 | + */ |
|
| 3014 | + public function in_entity_map() |
|
| 3015 | + { |
|
| 3016 | + // well, if we looked, did we find it in the entity map? |
|
| 3017 | + return $this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this; |
|
| 3018 | + } |
|
| 3019 | + |
|
| 3020 | + |
|
| 3021 | + /** |
|
| 3022 | + * refresh_from_db |
|
| 3023 | + * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 3024 | + * |
|
| 3025 | + * @throws ReflectionException |
|
| 3026 | + * @throws InvalidArgumentException |
|
| 3027 | + * @throws InvalidInterfaceException |
|
| 3028 | + * @throws InvalidDataTypeException |
|
| 3029 | + * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 3030 | + * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 3031 | + */ |
|
| 3032 | + public function refresh_from_db() |
|
| 3033 | + { |
|
| 3034 | + if ($this->ID() && $this->in_entity_map()) { |
|
| 3035 | + $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 3036 | + } else { |
|
| 3037 | + // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 3038 | + // if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 3039 | + // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 3040 | + // absolutely nothing in it for this ID |
|
| 3041 | + if (WP_DEBUG) { |
|
| 3042 | + throw new EE_Error( |
|
| 3043 | + sprintf( |
|
| 3044 | + esc_html__( |
|
| 3045 | + '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.', |
|
| 3046 | + 'event_espresso' |
|
| 3047 | + ), |
|
| 3048 | + $this->ID(), |
|
| 3049 | + get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 3050 | + get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 3051 | + ) |
|
| 3052 | + ); |
|
| 3053 | + } |
|
| 3054 | + } |
|
| 3055 | + } |
|
| 3056 | + |
|
| 3057 | + |
|
| 3058 | + /** |
|
| 3059 | + * Change $fields' values to $new_value_sql (which is a string of raw SQL) |
|
| 3060 | + * |
|
| 3061 | + * @since 4.9.80.p |
|
| 3062 | + * @param EE_Model_Field_Base[] $fields |
|
| 3063 | + * @param string $new_value_sql |
|
| 3064 | + * example: 'column_name=123', |
|
| 3065 | + * or 'column_name=column_name+1', |
|
| 3066 | + * or 'column_name= CASE |
|
| 3067 | + * WHEN (`column_name` + `other_column` + 5) <= `yet_another_column` |
|
| 3068 | + * THEN `column_name` + 5 |
|
| 3069 | + * ELSE `column_name` |
|
| 3070 | + * END' |
|
| 3071 | + * Also updates $field on this model object with the latest value from the database. |
|
| 3072 | + * @return bool |
|
| 3073 | + * @throws EE_Error |
|
| 3074 | + * @throws InvalidArgumentException |
|
| 3075 | + * @throws InvalidDataTypeException |
|
| 3076 | + * @throws InvalidInterfaceException |
|
| 3077 | + * @throws ReflectionException |
|
| 3078 | + */ |
|
| 3079 | + protected function updateFieldsInDB($fields, $new_value_sql) |
|
| 3080 | + { |
|
| 3081 | + // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
|
| 3082 | + // if it wasn't even there to start off. |
|
| 3083 | + if (! $this->ID()) { |
|
| 3084 | + $this->save(); |
|
| 3085 | + } |
|
| 3086 | + global $wpdb; |
|
| 3087 | + if (empty($fields)) { |
|
| 3088 | + throw new InvalidArgumentException( |
|
| 3089 | + esc_html__( |
|
| 3090 | + 'EE_Base_Class::updateFieldsInDB was passed an empty array of fields.', |
|
| 3091 | + 'event_espresso' |
|
| 3092 | + ) |
|
| 3093 | + ); |
|
| 3094 | + } |
|
| 3095 | + $first_field = reset($fields); |
|
| 3096 | + $table_alias = $first_field->get_table_alias(); |
|
| 3097 | + foreach ($fields as $field) { |
|
| 3098 | + if ($table_alias !== $field->get_table_alias()) { |
|
| 3099 | + throw new InvalidArgumentException( |
|
| 3100 | + sprintf( |
|
| 3101 | + esc_html__( |
|
| 3102 | + // @codingStandardsIgnoreStart |
|
| 3103 | + '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.', |
|
| 3104 | + // @codingStandardsIgnoreEnd |
|
| 3105 | + 'event_espresso' |
|
| 3106 | + ), |
|
| 3107 | + $table_alias, |
|
| 3108 | + $field->get_table_alias() |
|
| 3109 | + ) |
|
| 3110 | + ); |
|
| 3111 | + } |
|
| 3112 | + } |
|
| 3113 | + // Ok the fields are now known to all be for the same table. Proceed with creating the SQL to update it. |
|
| 3114 | + $table_obj = $this->get_model()->get_table_obj_by_alias($table_alias); |
|
| 3115 | + $table_pk_value = $this->ID(); |
|
| 3116 | + $table_name = $table_obj->get_table_name(); |
|
| 3117 | + if ($table_obj instanceof EE_Secondary_Table) { |
|
| 3118 | + $table_pk_field_name = $table_obj->get_fk_on_table(); |
|
| 3119 | + } else { |
|
| 3120 | + $table_pk_field_name = $table_obj->get_pk_column(); |
|
| 3121 | + } |
|
| 3122 | + |
|
| 3123 | + $query = |
|
| 3124 | + "UPDATE `{$table_name}` |
|
| 3125 | 3125 | SET " |
| 3126 | - . $new_value_sql |
|
| 3127 | - . $wpdb->prepare( |
|
| 3128 | - " |
|
| 3126 | + . $new_value_sql |
|
| 3127 | + . $wpdb->prepare( |
|
| 3128 | + " |
|
| 3129 | 3129 | WHERE `{$table_pk_field_name}` = %d;", |
| 3130 | - $table_pk_value |
|
| 3131 | - ); |
|
| 3132 | - $result = $wpdb->query($query); |
|
| 3133 | - foreach ($fields as $field) { |
|
| 3134 | - // If it was successful, we'd like to know the new value. |
|
| 3135 | - // If it failed, we'd also like to know the new value. |
|
| 3136 | - $new_value = $this->get_model()->get_var( |
|
| 3137 | - $this->get_model()->alter_query_params_to_restrict_by_ID( |
|
| 3138 | - $this->get_model()->get_index_primary_key_string( |
|
| 3139 | - $this->model_field_array() |
|
| 3140 | - ), |
|
| 3141 | - array( |
|
| 3142 | - 'default_where_conditions' => 'minimum', |
|
| 3143 | - ) |
|
| 3144 | - ), |
|
| 3145 | - $field->get_name() |
|
| 3146 | - ); |
|
| 3147 | - $this->set_from_db( |
|
| 3148 | - $field->get_name(), |
|
| 3149 | - $new_value |
|
| 3150 | - ); |
|
| 3151 | - } |
|
| 3152 | - return (bool) $result; |
|
| 3153 | - } |
|
| 3154 | - |
|
| 3155 | - |
|
| 3156 | - /** |
|
| 3157 | - * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()). |
|
| 3158 | - * Does not allow negative values, however. |
|
| 3159 | - * |
|
| 3160 | - * @since 4.9.80.p |
|
| 3161 | - * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them |
|
| 3162 | - * (positive or negative). One important gotcha: all these values must be |
|
| 3163 | - * on the same table (eg don't pass in one field for the posts table and |
|
| 3164 | - * another for the event meta table.) |
|
| 3165 | - * @return bool |
|
| 3166 | - * @throws EE_Error |
|
| 3167 | - * @throws InvalidArgumentException |
|
| 3168 | - * @throws InvalidDataTypeException |
|
| 3169 | - * @throws InvalidInterfaceException |
|
| 3170 | - * @throws ReflectionException |
|
| 3171 | - */ |
|
| 3172 | - public function adjustNumericFieldsInDb(array $fields_n_quantities) |
|
| 3173 | - { |
|
| 3174 | - global $wpdb; |
|
| 3175 | - if (empty($fields_n_quantities)) { |
|
| 3176 | - // No fields to update? Well sure, we updated them to that value just fine. |
|
| 3177 | - return true; |
|
| 3178 | - } |
|
| 3179 | - $fields = []; |
|
| 3180 | - $set_sql_statements = []; |
|
| 3181 | - foreach ($fields_n_quantities as $field_name => $quantity) { |
|
| 3182 | - $field = $this->get_model()->field_settings_for($field_name, true); |
|
| 3183 | - $fields[] = $field; |
|
| 3184 | - $column_name = $field->get_table_column(); |
|
| 3185 | - |
|
| 3186 | - $abs_qty = absint($quantity); |
|
| 3187 | - if ($quantity > 0) { |
|
| 3188 | - // don't let the value be negative as often these fields are unsigned |
|
| 3189 | - $set_sql_statements[] = $wpdb->prepare( |
|
| 3190 | - "`{$column_name}` = `{$column_name}` + %d", |
|
| 3191 | - $abs_qty |
|
| 3192 | - ); |
|
| 3193 | - } else { |
|
| 3194 | - $set_sql_statements[] = $wpdb->prepare( |
|
| 3195 | - "`{$column_name}` = CASE |
|
| 3130 | + $table_pk_value |
|
| 3131 | + ); |
|
| 3132 | + $result = $wpdb->query($query); |
|
| 3133 | + foreach ($fields as $field) { |
|
| 3134 | + // If it was successful, we'd like to know the new value. |
|
| 3135 | + // If it failed, we'd also like to know the new value. |
|
| 3136 | + $new_value = $this->get_model()->get_var( |
|
| 3137 | + $this->get_model()->alter_query_params_to_restrict_by_ID( |
|
| 3138 | + $this->get_model()->get_index_primary_key_string( |
|
| 3139 | + $this->model_field_array() |
|
| 3140 | + ), |
|
| 3141 | + array( |
|
| 3142 | + 'default_where_conditions' => 'minimum', |
|
| 3143 | + ) |
|
| 3144 | + ), |
|
| 3145 | + $field->get_name() |
|
| 3146 | + ); |
|
| 3147 | + $this->set_from_db( |
|
| 3148 | + $field->get_name(), |
|
| 3149 | + $new_value |
|
| 3150 | + ); |
|
| 3151 | + } |
|
| 3152 | + return (bool) $result; |
|
| 3153 | + } |
|
| 3154 | + |
|
| 3155 | + |
|
| 3156 | + /** |
|
| 3157 | + * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()). |
|
| 3158 | + * Does not allow negative values, however. |
|
| 3159 | + * |
|
| 3160 | + * @since 4.9.80.p |
|
| 3161 | + * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them |
|
| 3162 | + * (positive or negative). One important gotcha: all these values must be |
|
| 3163 | + * on the same table (eg don't pass in one field for the posts table and |
|
| 3164 | + * another for the event meta table.) |
|
| 3165 | + * @return bool |
|
| 3166 | + * @throws EE_Error |
|
| 3167 | + * @throws InvalidArgumentException |
|
| 3168 | + * @throws InvalidDataTypeException |
|
| 3169 | + * @throws InvalidInterfaceException |
|
| 3170 | + * @throws ReflectionException |
|
| 3171 | + */ |
|
| 3172 | + public function adjustNumericFieldsInDb(array $fields_n_quantities) |
|
| 3173 | + { |
|
| 3174 | + global $wpdb; |
|
| 3175 | + if (empty($fields_n_quantities)) { |
|
| 3176 | + // No fields to update? Well sure, we updated them to that value just fine. |
|
| 3177 | + return true; |
|
| 3178 | + } |
|
| 3179 | + $fields = []; |
|
| 3180 | + $set_sql_statements = []; |
|
| 3181 | + foreach ($fields_n_quantities as $field_name => $quantity) { |
|
| 3182 | + $field = $this->get_model()->field_settings_for($field_name, true); |
|
| 3183 | + $fields[] = $field; |
|
| 3184 | + $column_name = $field->get_table_column(); |
|
| 3185 | + |
|
| 3186 | + $abs_qty = absint($quantity); |
|
| 3187 | + if ($quantity > 0) { |
|
| 3188 | + // don't let the value be negative as often these fields are unsigned |
|
| 3189 | + $set_sql_statements[] = $wpdb->prepare( |
|
| 3190 | + "`{$column_name}` = `{$column_name}` + %d", |
|
| 3191 | + $abs_qty |
|
| 3192 | + ); |
|
| 3193 | + } else { |
|
| 3194 | + $set_sql_statements[] = $wpdb->prepare( |
|
| 3195 | + "`{$column_name}` = CASE |
|
| 3196 | 3196 | WHEN (`{$column_name}` >= %d) |
| 3197 | 3197 | THEN `{$column_name}` - %d |
| 3198 | 3198 | ELSE 0 |
| 3199 | 3199 | END", |
| 3200 | - $abs_qty, |
|
| 3201 | - $abs_qty |
|
| 3202 | - ); |
|
| 3203 | - } |
|
| 3204 | - } |
|
| 3205 | - return $this->updateFieldsInDB( |
|
| 3206 | - $fields, |
|
| 3207 | - implode(', ', $set_sql_statements) |
|
| 3208 | - ); |
|
| 3209 | - } |
|
| 3210 | - |
|
| 3211 | - |
|
| 3212 | - /** |
|
| 3213 | - * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of |
|
| 3214 | - * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value. |
|
| 3215 | - * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold. |
|
| 3216 | - * Returns true if the value was successfully bumped, and updates the value on this model object. |
|
| 3217 | - * Otherwise returns false. |
|
| 3218 | - * |
|
| 3219 | - * @since 4.9.80.p |
|
| 3220 | - * @param string $field_name_to_bump |
|
| 3221 | - * @param string $field_name_affecting_total |
|
| 3222 | - * @param string $limit_field_name |
|
| 3223 | - * @param int $quantity |
|
| 3224 | - * @return bool |
|
| 3225 | - * @throws EE_Error |
|
| 3226 | - * @throws InvalidArgumentException |
|
| 3227 | - * @throws InvalidDataTypeException |
|
| 3228 | - * @throws InvalidInterfaceException |
|
| 3229 | - * @throws ReflectionException |
|
| 3230 | - */ |
|
| 3231 | - public function incrementFieldConditionallyInDb($field_name_to_bump, $field_name_affecting_total, $limit_field_name, $quantity) |
|
| 3232 | - { |
|
| 3233 | - global $wpdb; |
|
| 3234 | - $field = $this->get_model()->field_settings_for($field_name_to_bump, true); |
|
| 3235 | - $column_name = $field->get_table_column(); |
|
| 3236 | - |
|
| 3237 | - $field_affecting_total = $this->get_model()->field_settings_for($field_name_affecting_total, true); |
|
| 3238 | - $column_affecting_total = $field_affecting_total->get_table_column(); |
|
| 3239 | - |
|
| 3240 | - $limiting_field = $this->get_model()->field_settings_for($limit_field_name, true); |
|
| 3241 | - $limiting_column = $limiting_field->get_table_column(); |
|
| 3242 | - return $this->updateFieldsInDB( |
|
| 3243 | - [$field], |
|
| 3244 | - $wpdb->prepare( |
|
| 3245 | - "`{$column_name}` = |
|
| 3200 | + $abs_qty, |
|
| 3201 | + $abs_qty |
|
| 3202 | + ); |
|
| 3203 | + } |
|
| 3204 | + } |
|
| 3205 | + return $this->updateFieldsInDB( |
|
| 3206 | + $fields, |
|
| 3207 | + implode(', ', $set_sql_statements) |
|
| 3208 | + ); |
|
| 3209 | + } |
|
| 3210 | + |
|
| 3211 | + |
|
| 3212 | + /** |
|
| 3213 | + * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of |
|
| 3214 | + * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value. |
|
| 3215 | + * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold. |
|
| 3216 | + * Returns true if the value was successfully bumped, and updates the value on this model object. |
|
| 3217 | + * Otherwise returns false. |
|
| 3218 | + * |
|
| 3219 | + * @since 4.9.80.p |
|
| 3220 | + * @param string $field_name_to_bump |
|
| 3221 | + * @param string $field_name_affecting_total |
|
| 3222 | + * @param string $limit_field_name |
|
| 3223 | + * @param int $quantity |
|
| 3224 | + * @return bool |
|
| 3225 | + * @throws EE_Error |
|
| 3226 | + * @throws InvalidArgumentException |
|
| 3227 | + * @throws InvalidDataTypeException |
|
| 3228 | + * @throws InvalidInterfaceException |
|
| 3229 | + * @throws ReflectionException |
|
| 3230 | + */ |
|
| 3231 | + public function incrementFieldConditionallyInDb($field_name_to_bump, $field_name_affecting_total, $limit_field_name, $quantity) |
|
| 3232 | + { |
|
| 3233 | + global $wpdb; |
|
| 3234 | + $field = $this->get_model()->field_settings_for($field_name_to_bump, true); |
|
| 3235 | + $column_name = $field->get_table_column(); |
|
| 3236 | + |
|
| 3237 | + $field_affecting_total = $this->get_model()->field_settings_for($field_name_affecting_total, true); |
|
| 3238 | + $column_affecting_total = $field_affecting_total->get_table_column(); |
|
| 3239 | + |
|
| 3240 | + $limiting_field = $this->get_model()->field_settings_for($limit_field_name, true); |
|
| 3241 | + $limiting_column = $limiting_field->get_table_column(); |
|
| 3242 | + return $this->updateFieldsInDB( |
|
| 3243 | + [$field], |
|
| 3244 | + $wpdb->prepare( |
|
| 3245 | + "`{$column_name}` = |
|
| 3246 | 3246 | CASE |
| 3247 | 3247 | WHEN ((`{$column_name}` + `{$column_affecting_total}` + %d) <= `{$limiting_column}`) OR `{$limiting_column}` = %d |
| 3248 | 3248 | THEN `{$column_name}` + %d |
| 3249 | 3249 | ELSE `{$column_name}` |
| 3250 | 3250 | END", |
| 3251 | - $quantity, |
|
| 3252 | - EE_INF_IN_DB, |
|
| 3253 | - $quantity |
|
| 3254 | - ) |
|
| 3255 | - ); |
|
| 3256 | - } |
|
| 3257 | - |
|
| 3258 | - |
|
| 3259 | - /** |
|
| 3260 | - * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 3261 | - * (probably a bad assumption they have made, oh well) |
|
| 3262 | - * |
|
| 3263 | - * @return string |
|
| 3264 | - */ |
|
| 3265 | - public function __toString() |
|
| 3266 | - { |
|
| 3267 | - try { |
|
| 3268 | - return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 3269 | - } catch (Exception $e) { |
|
| 3270 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 3271 | - return ''; |
|
| 3272 | - } |
|
| 3273 | - } |
|
| 3274 | - |
|
| 3275 | - |
|
| 3276 | - /** |
|
| 3277 | - * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 3278 | - * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 3279 | - * This means if we have made changes to those related model objects, and want to unserialize |
|
| 3280 | - * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 3281 | - * Instead, those related model objects should be directly serialized and stored. |
|
| 3282 | - * Eg, the following won't work: |
|
| 3283 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3284 | - * $att = $reg->attendee(); |
|
| 3285 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3286 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 3287 | - * //END REQUEST |
|
| 3288 | - * //START NEXT REQUEST |
|
| 3289 | - * $reg = get_option( 'my_option' ); |
|
| 3290 | - * $reg->attendee()->save(); |
|
| 3291 | - * And would need to be replace with: |
|
| 3292 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3293 | - * $att = $reg->attendee(); |
|
| 3294 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3295 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 3296 | - * //END REQUEST |
|
| 3297 | - * //START NEXT REQUEST |
|
| 3298 | - * $att = get_option( 'my_option' ); |
|
| 3299 | - * $att->save(); |
|
| 3300 | - * |
|
| 3301 | - * @return array |
|
| 3302 | - * @throws ReflectionException |
|
| 3303 | - * @throws InvalidArgumentException |
|
| 3304 | - * @throws InvalidInterfaceException |
|
| 3305 | - * @throws InvalidDataTypeException |
|
| 3306 | - * @throws EE_Error |
|
| 3307 | - */ |
|
| 3308 | - public function __sleep() |
|
| 3309 | - { |
|
| 3310 | - $model = $this->get_model(); |
|
| 3311 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 3312 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 3313 | - $classname = 'EE_' . $model->get_this_model_name(); |
|
| 3314 | - if ($this->get_one_from_cache($relation_name) instanceof $classname |
|
| 3315 | - && $this->get_one_from_cache($relation_name)->ID() |
|
| 3316 | - ) { |
|
| 3317 | - $this->clear_cache( |
|
| 3318 | - $relation_name, |
|
| 3319 | - $this->get_one_from_cache($relation_name)->ID() |
|
| 3320 | - ); |
|
| 3321 | - } |
|
| 3322 | - } |
|
| 3323 | - } |
|
| 3324 | - $this->_props_n_values_provided_in_constructor = array(); |
|
| 3325 | - $properties_to_serialize = get_object_vars($this); |
|
| 3326 | - // don't serialize the model. It's big and that risks recursion |
|
| 3327 | - unset($properties_to_serialize['_model']); |
|
| 3328 | - return array_keys($properties_to_serialize); |
|
| 3329 | - } |
|
| 3330 | - |
|
| 3331 | - |
|
| 3332 | - /** |
|
| 3333 | - * restore _props_n_values_provided_in_constructor |
|
| 3334 | - * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 3335 | - * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 3336 | - * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 3337 | - */ |
|
| 3338 | - public function __wakeup() |
|
| 3339 | - { |
|
| 3340 | - $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 3341 | - } |
|
| 3342 | - |
|
| 3343 | - |
|
| 3344 | - /** |
|
| 3345 | - * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
|
| 3346 | - * distinct with the clone host instance are also cloned. |
|
| 3347 | - */ |
|
| 3348 | - public function __clone() |
|
| 3349 | - { |
|
| 3350 | - // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
|
| 3351 | - foreach ($this->_fields as $field => $value) { |
|
| 3352 | - if ($value instanceof DateTime) { |
|
| 3353 | - $this->_fields[ $field ] = clone $value; |
|
| 3354 | - } |
|
| 3355 | - } |
|
| 3356 | - } |
|
| 3251 | + $quantity, |
|
| 3252 | + EE_INF_IN_DB, |
|
| 3253 | + $quantity |
|
| 3254 | + ) |
|
| 3255 | + ); |
|
| 3256 | + } |
|
| 3257 | + |
|
| 3258 | + |
|
| 3259 | + /** |
|
| 3260 | + * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 3261 | + * (probably a bad assumption they have made, oh well) |
|
| 3262 | + * |
|
| 3263 | + * @return string |
|
| 3264 | + */ |
|
| 3265 | + public function __toString() |
|
| 3266 | + { |
|
| 3267 | + try { |
|
| 3268 | + return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 3269 | + } catch (Exception $e) { |
|
| 3270 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 3271 | + return ''; |
|
| 3272 | + } |
|
| 3273 | + } |
|
| 3274 | + |
|
| 3275 | + |
|
| 3276 | + /** |
|
| 3277 | + * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 3278 | + * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 3279 | + * This means if we have made changes to those related model objects, and want to unserialize |
|
| 3280 | + * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 3281 | + * Instead, those related model objects should be directly serialized and stored. |
|
| 3282 | + * Eg, the following won't work: |
|
| 3283 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3284 | + * $att = $reg->attendee(); |
|
| 3285 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3286 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 3287 | + * //END REQUEST |
|
| 3288 | + * //START NEXT REQUEST |
|
| 3289 | + * $reg = get_option( 'my_option' ); |
|
| 3290 | + * $reg->attendee()->save(); |
|
| 3291 | + * And would need to be replace with: |
|
| 3292 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3293 | + * $att = $reg->attendee(); |
|
| 3294 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3295 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 3296 | + * //END REQUEST |
|
| 3297 | + * //START NEXT REQUEST |
|
| 3298 | + * $att = get_option( 'my_option' ); |
|
| 3299 | + * $att->save(); |
|
| 3300 | + * |
|
| 3301 | + * @return array |
|
| 3302 | + * @throws ReflectionException |
|
| 3303 | + * @throws InvalidArgumentException |
|
| 3304 | + * @throws InvalidInterfaceException |
|
| 3305 | + * @throws InvalidDataTypeException |
|
| 3306 | + * @throws EE_Error |
|
| 3307 | + */ |
|
| 3308 | + public function __sleep() |
|
| 3309 | + { |
|
| 3310 | + $model = $this->get_model(); |
|
| 3311 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 3312 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 3313 | + $classname = 'EE_' . $model->get_this_model_name(); |
|
| 3314 | + if ($this->get_one_from_cache($relation_name) instanceof $classname |
|
| 3315 | + && $this->get_one_from_cache($relation_name)->ID() |
|
| 3316 | + ) { |
|
| 3317 | + $this->clear_cache( |
|
| 3318 | + $relation_name, |
|
| 3319 | + $this->get_one_from_cache($relation_name)->ID() |
|
| 3320 | + ); |
|
| 3321 | + } |
|
| 3322 | + } |
|
| 3323 | + } |
|
| 3324 | + $this->_props_n_values_provided_in_constructor = array(); |
|
| 3325 | + $properties_to_serialize = get_object_vars($this); |
|
| 3326 | + // don't serialize the model. It's big and that risks recursion |
|
| 3327 | + unset($properties_to_serialize['_model']); |
|
| 3328 | + return array_keys($properties_to_serialize); |
|
| 3329 | + } |
|
| 3330 | + |
|
| 3331 | + |
|
| 3332 | + /** |
|
| 3333 | + * restore _props_n_values_provided_in_constructor |
|
| 3334 | + * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 3335 | + * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 3336 | + * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 3337 | + */ |
|
| 3338 | + public function __wakeup() |
|
| 3339 | + { |
|
| 3340 | + $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 3341 | + } |
|
| 3342 | + |
|
| 3343 | + |
|
| 3344 | + /** |
|
| 3345 | + * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
|
| 3346 | + * distinct with the clone host instance are also cloned. |
|
| 3347 | + */ |
|
| 3348 | + public function __clone() |
|
| 3349 | + { |
|
| 3350 | + // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
|
| 3351 | + foreach ($this->_fields as $field => $value) { |
|
| 3352 | + if ($value instanceof DateTime) { |
|
| 3353 | + $this->_fields[ $field ] = clone $value; |
|
| 3354 | + } |
|
| 3355 | + } |
|
| 3356 | + } |
|
| 3357 | 3357 | } |
@@ -13,1410 +13,1410 @@ |
||
| 13 | 13 | class EE_Datetime extends EE_Soft_Delete_Base_Class |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * constant used by get_active_status, indicates datetime has no more available spaces |
|
| 18 | - */ |
|
| 19 | - const sold_out = 'DTS'; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * constant used by get_active_status, indicating datetime is still active (even is not over, can be registered-for) |
|
| 23 | - */ |
|
| 24 | - const active = 'DTA'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * constant used by get_active_status, indicating the datetime cannot be used for registrations yet, but has not |
|
| 28 | - * expired |
|
| 29 | - */ |
|
| 30 | - const upcoming = 'DTU'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Datetime is postponed |
|
| 34 | - */ |
|
| 35 | - const postponed = 'DTP'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Datetime is cancelled |
|
| 39 | - */ |
|
| 40 | - const cancelled = 'DTC'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * constant used by get_active_status, indicates datetime has expired (event is over) |
|
| 44 | - */ |
|
| 45 | - const expired = 'DTE'; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * constant used in various places indicating that an event is INACTIVE (not yet ready to be published) |
|
| 49 | - */ |
|
| 50 | - const inactive = 'DTI'; |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param array $props_n_values incoming values |
|
| 55 | - * @param string $timezone incoming timezone (if not set the timezone set for the website will be used.) |
|
| 56 | - * @param array $date_formats incoming date_formats in an array where the first value is the date_format |
|
| 57 | - * and the second value is the time format |
|
| 58 | - * @return EE_Datetime |
|
| 59 | - * @throws ReflectionException |
|
| 60 | - * @throws InvalidArgumentException |
|
| 61 | - * @throws InvalidInterfaceException |
|
| 62 | - * @throws InvalidDataTypeException |
|
| 63 | - * @throws EE_Error |
|
| 64 | - */ |
|
| 65 | - public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array()) |
|
| 66 | - { |
|
| 67 | - $has_object = parent::_check_for_object( |
|
| 68 | - $props_n_values, |
|
| 69 | - __CLASS__, |
|
| 70 | - $timezone, |
|
| 71 | - $date_formats |
|
| 72 | - ); |
|
| 73 | - return $has_object |
|
| 74 | - ? $has_object |
|
| 75 | - : new self($props_n_values, false, $timezone, $date_formats); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param array $props_n_values incoming values from the database |
|
| 81 | - * @param string $timezone incoming timezone as set by the model. If not set the timezone for |
|
| 82 | - * the website will be used. |
|
| 83 | - * @return EE_Datetime |
|
| 84 | - * @throws ReflectionException |
|
| 85 | - * @throws InvalidArgumentException |
|
| 86 | - * @throws InvalidInterfaceException |
|
| 87 | - * @throws InvalidDataTypeException |
|
| 88 | - * @throws EE_Error |
|
| 89 | - */ |
|
| 90 | - public static function new_instance_from_db($props_n_values = array(), $timezone = null) |
|
| 91 | - { |
|
| 92 | - return new self($props_n_values, true, $timezone); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @param $name |
|
| 98 | - * @throws ReflectionException |
|
| 99 | - * @throws InvalidArgumentException |
|
| 100 | - * @throws InvalidInterfaceException |
|
| 101 | - * @throws InvalidDataTypeException |
|
| 102 | - * @throws EE_Error |
|
| 103 | - */ |
|
| 104 | - public function set_name($name) |
|
| 105 | - { |
|
| 106 | - $this->set('DTT_name', $name); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @param $description |
|
| 112 | - * @throws ReflectionException |
|
| 113 | - * @throws InvalidArgumentException |
|
| 114 | - * @throws InvalidInterfaceException |
|
| 115 | - * @throws InvalidDataTypeException |
|
| 116 | - * @throws EE_Error |
|
| 117 | - */ |
|
| 118 | - public function set_description($description) |
|
| 119 | - { |
|
| 120 | - $this->set('DTT_description', $description); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Set event start date |
|
| 126 | - * set the start date for an event |
|
| 127 | - * |
|
| 128 | - * @param string $date a string representation of the event's date ex: Dec. 25, 2025 or 12-25-2025 |
|
| 129 | - * @throws ReflectionException |
|
| 130 | - * @throws InvalidArgumentException |
|
| 131 | - * @throws InvalidInterfaceException |
|
| 132 | - * @throws InvalidDataTypeException |
|
| 133 | - * @throws EE_Error |
|
| 134 | - */ |
|
| 135 | - public function set_start_date($date) |
|
| 136 | - { |
|
| 137 | - $this->_set_date_for($date, 'DTT_EVT_start'); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Set event start time |
|
| 143 | - * set the start time for an event |
|
| 144 | - * |
|
| 145 | - * @param string $time a string representation of the event time ex: 9am or 7:30 PM |
|
| 146 | - * @throws ReflectionException |
|
| 147 | - * @throws InvalidArgumentException |
|
| 148 | - * @throws InvalidInterfaceException |
|
| 149 | - * @throws InvalidDataTypeException |
|
| 150 | - * @throws EE_Error |
|
| 151 | - */ |
|
| 152 | - public function set_start_time($time) |
|
| 153 | - { |
|
| 154 | - $this->_set_time_for($time, 'DTT_EVT_start'); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Set event end date |
|
| 160 | - * set the end date for an event |
|
| 161 | - * |
|
| 162 | - * @param string $date a string representation of the event's date ex: Dec. 25, 2025 or 12-25-2025 |
|
| 163 | - * @throws ReflectionException |
|
| 164 | - * @throws InvalidArgumentException |
|
| 165 | - * @throws InvalidInterfaceException |
|
| 166 | - * @throws InvalidDataTypeException |
|
| 167 | - * @throws EE_Error |
|
| 168 | - */ |
|
| 169 | - public function set_end_date($date) |
|
| 170 | - { |
|
| 171 | - $this->_set_date_for($date, 'DTT_EVT_end'); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Set event end time |
|
| 177 | - * set the end time for an event |
|
| 178 | - * |
|
| 179 | - * @param string $time a string representation of the event time ex: 9am or 7:30 PM |
|
| 180 | - * @throws ReflectionException |
|
| 181 | - * @throws InvalidArgumentException |
|
| 182 | - * @throws InvalidInterfaceException |
|
| 183 | - * @throws InvalidDataTypeException |
|
| 184 | - * @throws EE_Error |
|
| 185 | - */ |
|
| 186 | - public function set_end_time($time) |
|
| 187 | - { |
|
| 188 | - $this->_set_time_for($time, 'DTT_EVT_end'); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Set registration limit |
|
| 194 | - * set the maximum number of attendees that can be registered for this datetime slot |
|
| 195 | - * |
|
| 196 | - * @param int $reg_limit |
|
| 197 | - * @throws ReflectionException |
|
| 198 | - * @throws InvalidArgumentException |
|
| 199 | - * @throws InvalidInterfaceException |
|
| 200 | - * @throws InvalidDataTypeException |
|
| 201 | - * @throws EE_Error |
|
| 202 | - */ |
|
| 203 | - public function set_reg_limit($reg_limit) |
|
| 204 | - { |
|
| 205 | - $this->set('DTT_reg_limit', $reg_limit); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * get the number of tickets sold for this datetime slot |
|
| 211 | - * |
|
| 212 | - * @return mixed int on success, FALSE on fail |
|
| 213 | - * @throws ReflectionException |
|
| 214 | - * @throws InvalidArgumentException |
|
| 215 | - * @throws InvalidInterfaceException |
|
| 216 | - * @throws InvalidDataTypeException |
|
| 217 | - * @throws EE_Error |
|
| 218 | - */ |
|
| 219 | - public function sold() |
|
| 220 | - { |
|
| 221 | - return $this->get_raw('DTT_sold'); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * @param int $sold |
|
| 227 | - * @throws ReflectionException |
|
| 228 | - * @throws InvalidArgumentException |
|
| 229 | - * @throws InvalidInterfaceException |
|
| 230 | - * @throws InvalidDataTypeException |
|
| 231 | - * @throws EE_Error |
|
| 232 | - */ |
|
| 233 | - public function set_sold($sold) |
|
| 234 | - { |
|
| 235 | - // sold can not go below zero |
|
| 236 | - $sold = max(0, $sold); |
|
| 237 | - $this->set('DTT_sold', $sold); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Increments sold by amount passed by $qty, and persists it immediately to the database. |
|
| 243 | - * Simultaneously decreases the reserved count, unless $also_decrease_reserved is false. |
|
| 244 | - * |
|
| 245 | - * @param int $qty |
|
| 246 | - * @param boolean $also_decrease_reserved |
|
| 247 | - * @return boolean indicating success |
|
| 248 | - * @throws ReflectionException |
|
| 249 | - * @throws InvalidArgumentException |
|
| 250 | - * @throws InvalidInterfaceException |
|
| 251 | - * @throws InvalidDataTypeException |
|
| 252 | - * @throws EE_Error |
|
| 253 | - */ |
|
| 254 | - public function increaseSold($qty = 1, $also_decrease_reserved = true) |
|
| 255 | - { |
|
| 256 | - $qty = absint($qty); |
|
| 257 | - if ($also_decrease_reserved) { |
|
| 258 | - $success = $this->adjustNumericFieldsInDb( |
|
| 259 | - [ |
|
| 260 | - 'DTT_reserved' => $qty * -1, |
|
| 261 | - 'DTT_sold' => $qty |
|
| 262 | - ] |
|
| 263 | - ); |
|
| 264 | - } else { |
|
| 265 | - $success = $this->adjustNumericFieldsInDb( |
|
| 266 | - [ |
|
| 267 | - 'DTT_sold' => $qty |
|
| 268 | - ] |
|
| 269 | - ); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - do_action( |
|
| 273 | - 'AHEE__EE_Datetime__increase_sold', |
|
| 274 | - $this, |
|
| 275 | - $qty, |
|
| 276 | - $this->sold(), |
|
| 277 | - $success |
|
| 278 | - ); |
|
| 279 | - return $success; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Decrements (subtracts) sold amount passed by $qty directly in the DB and on the model object. (Ie, no need |
|
| 285 | - * to save afterwards.) |
|
| 286 | - * |
|
| 287 | - * @param int $qty |
|
| 288 | - * @return boolean indicating success |
|
| 289 | - * @throws ReflectionException |
|
| 290 | - * @throws InvalidArgumentException |
|
| 291 | - * @throws InvalidInterfaceException |
|
| 292 | - * @throws InvalidDataTypeException |
|
| 293 | - * @throws EE_Error |
|
| 294 | - */ |
|
| 295 | - public function decreaseSold($qty = 1) |
|
| 296 | - { |
|
| 297 | - $qty = absint($qty); |
|
| 298 | - $success = $this->adjustNumericFieldsInDb( |
|
| 299 | - [ |
|
| 300 | - 'DTT_sold' => $qty * -1 |
|
| 301 | - ] |
|
| 302 | - ); |
|
| 303 | - do_action( |
|
| 304 | - 'AHEE__EE_Datetime__decrease_sold', |
|
| 305 | - $this, |
|
| 306 | - $qty, |
|
| 307 | - $this->sold(), |
|
| 308 | - $success |
|
| 309 | - ); |
|
| 310 | - return $success; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Gets qty of reserved tickets for this datetime |
|
| 316 | - * |
|
| 317 | - * @return int |
|
| 318 | - * @throws ReflectionException |
|
| 319 | - * @throws InvalidArgumentException |
|
| 320 | - * @throws InvalidInterfaceException |
|
| 321 | - * @throws InvalidDataTypeException |
|
| 322 | - * @throws EE_Error |
|
| 323 | - */ |
|
| 324 | - public function reserved() |
|
| 325 | - { |
|
| 326 | - return $this->get_raw('DTT_reserved'); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Sets qty of reserved tickets for this datetime |
|
| 332 | - * |
|
| 333 | - * @param int $reserved |
|
| 334 | - * @throws ReflectionException |
|
| 335 | - * @throws InvalidArgumentException |
|
| 336 | - * @throws InvalidInterfaceException |
|
| 337 | - * @throws InvalidDataTypeException |
|
| 338 | - * @throws EE_Error |
|
| 339 | - */ |
|
| 340 | - public function set_reserved($reserved) |
|
| 341 | - { |
|
| 342 | - // reserved can not go below zero |
|
| 343 | - $reserved = max(0, (int) $reserved); |
|
| 344 | - $this->set('DTT_reserved', $reserved); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Increments reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 350 | - * |
|
| 351 | - * @param int $qty |
|
| 352 | - * @return boolean indicating success |
|
| 353 | - * @throws ReflectionException |
|
| 354 | - * @throws InvalidArgumentException |
|
| 355 | - * @throws InvalidInterfaceException |
|
| 356 | - * @throws InvalidDataTypeException |
|
| 357 | - * @throws EE_Error |
|
| 358 | - */ |
|
| 359 | - public function increaseReserved($qty = 1) |
|
| 360 | - { |
|
| 361 | - $qty = absint($qty); |
|
| 362 | - $success = $this->incrementFieldConditionallyInDb( |
|
| 363 | - 'DTT_reserved', |
|
| 364 | - 'DTT_sold', |
|
| 365 | - 'DTT_reg_limit', |
|
| 366 | - $qty |
|
| 367 | - ); |
|
| 368 | - do_action( |
|
| 369 | - 'AHEE__EE_Datetime__increase_reserved', |
|
| 370 | - $this, |
|
| 371 | - $qty, |
|
| 372 | - $this->reserved(), |
|
| 373 | - $success |
|
| 374 | - ); |
|
| 375 | - return $success; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 381 | - * |
|
| 382 | - * @param int $qty |
|
| 383 | - * @return boolean indicating success |
|
| 384 | - * @throws ReflectionException |
|
| 385 | - * @throws InvalidArgumentException |
|
| 386 | - * @throws InvalidInterfaceException |
|
| 387 | - * @throws InvalidDataTypeException |
|
| 388 | - * @throws EE_Error |
|
| 389 | - */ |
|
| 390 | - public function decreaseReserved($qty = 1) |
|
| 391 | - { |
|
| 392 | - $qty = absint($qty); |
|
| 393 | - $success = $this->adjustNumericFieldsInDb( |
|
| 394 | - [ |
|
| 395 | - 'DTT_reserved' => $qty * -1 |
|
| 396 | - ] |
|
| 397 | - ); |
|
| 398 | - do_action( |
|
| 399 | - 'AHEE__EE_Datetime__decrease_reserved', |
|
| 400 | - $this, |
|
| 401 | - $qty, |
|
| 402 | - $this->reserved(), |
|
| 403 | - $success |
|
| 404 | - ); |
|
| 405 | - return $success; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * total sold and reserved tickets |
|
| 411 | - * |
|
| 412 | - * @return int |
|
| 413 | - * @throws ReflectionException |
|
| 414 | - * @throws InvalidArgumentException |
|
| 415 | - * @throws InvalidInterfaceException |
|
| 416 | - * @throws InvalidDataTypeException |
|
| 417 | - * @throws EE_Error |
|
| 418 | - */ |
|
| 419 | - public function sold_and_reserved() |
|
| 420 | - { |
|
| 421 | - return $this->sold() + $this->reserved(); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * returns the datetime name |
|
| 427 | - * |
|
| 428 | - * @return string |
|
| 429 | - * @throws ReflectionException |
|
| 430 | - * @throws InvalidArgumentException |
|
| 431 | - * @throws InvalidInterfaceException |
|
| 432 | - * @throws InvalidDataTypeException |
|
| 433 | - * @throws EE_Error |
|
| 434 | - */ |
|
| 435 | - public function name() |
|
| 436 | - { |
|
| 437 | - return $this->get('DTT_name'); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * returns the datetime description |
|
| 443 | - * |
|
| 444 | - * @return string |
|
| 445 | - * @throws ReflectionException |
|
| 446 | - * @throws InvalidArgumentException |
|
| 447 | - * @throws InvalidInterfaceException |
|
| 448 | - * @throws InvalidDataTypeException |
|
| 449 | - * @throws EE_Error |
|
| 450 | - */ |
|
| 451 | - public function description() |
|
| 452 | - { |
|
| 453 | - return $this->get('DTT_description'); |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * This helper simply returns whether the event_datetime for the current datetime is a primary datetime |
|
| 459 | - * |
|
| 460 | - * @return boolean TRUE if is primary, FALSE if not. |
|
| 461 | - * @throws ReflectionException |
|
| 462 | - * @throws InvalidArgumentException |
|
| 463 | - * @throws InvalidInterfaceException |
|
| 464 | - * @throws InvalidDataTypeException |
|
| 465 | - * @throws EE_Error |
|
| 466 | - */ |
|
| 467 | - public function is_primary() |
|
| 468 | - { |
|
| 469 | - return $this->get('DTT_is_primary'); |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - |
|
| 473 | - /** |
|
| 474 | - * This helper simply returns the order for the datetime |
|
| 475 | - * |
|
| 476 | - * @return int The order of the datetime for this event. |
|
| 477 | - * @throws ReflectionException |
|
| 478 | - * @throws InvalidArgumentException |
|
| 479 | - * @throws InvalidInterfaceException |
|
| 480 | - * @throws InvalidDataTypeException |
|
| 481 | - * @throws EE_Error |
|
| 482 | - */ |
|
| 483 | - public function order() |
|
| 484 | - { |
|
| 485 | - return $this->get('DTT_order'); |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * This helper simply returns the parent id for the datetime |
|
| 491 | - * |
|
| 492 | - * @return int |
|
| 493 | - * @throws ReflectionException |
|
| 494 | - * @throws InvalidArgumentException |
|
| 495 | - * @throws InvalidInterfaceException |
|
| 496 | - * @throws InvalidDataTypeException |
|
| 497 | - * @throws EE_Error |
|
| 498 | - */ |
|
| 499 | - public function parent() |
|
| 500 | - { |
|
| 501 | - return $this->get('DTT_parent'); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * show date and/or time |
|
| 507 | - * |
|
| 508 | - * @param string $date_or_time whether to display a date or time or both |
|
| 509 | - * @param string $start_or_end whether to display start or end datetimes |
|
| 510 | - * @param string $dt_frmt |
|
| 511 | - * @param string $tm_frmt |
|
| 512 | - * @param bool $echo whether we echo or return (note echoing uses "pretty" formats, |
|
| 513 | - * otherwise we use the standard formats) |
|
| 514 | - * @return string|bool string on success, FALSE on fail |
|
| 515 | - * @throws ReflectionException |
|
| 516 | - * @throws InvalidArgumentException |
|
| 517 | - * @throws InvalidInterfaceException |
|
| 518 | - * @throws InvalidDataTypeException |
|
| 519 | - * @throws EE_Error |
|
| 520 | - */ |
|
| 521 | - private function _show_datetime( |
|
| 522 | - $date_or_time = null, |
|
| 523 | - $start_or_end = 'start', |
|
| 524 | - $dt_frmt = '', |
|
| 525 | - $tm_frmt = '', |
|
| 526 | - $echo = false |
|
| 527 | - ) { |
|
| 528 | - $field_name = "DTT_EVT_{$start_or_end}"; |
|
| 529 | - $dtt = $this->_get_datetime( |
|
| 530 | - $field_name, |
|
| 531 | - $dt_frmt, |
|
| 532 | - $tm_frmt, |
|
| 533 | - $date_or_time, |
|
| 534 | - $echo |
|
| 535 | - ); |
|
| 536 | - if (! $echo) { |
|
| 537 | - return $dtt; |
|
| 538 | - } |
|
| 539 | - return ''; |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - |
|
| 543 | - /** |
|
| 544 | - * get event start date. Provide either the date format, or NULL to re-use the |
|
| 545 | - * last-used format, or '' to use the default date format |
|
| 546 | - * |
|
| 547 | - * @param string $dt_frmt string representation of date format defaults to 'F j, Y' |
|
| 548 | - * @return mixed string on success, FALSE on fail |
|
| 549 | - * @throws ReflectionException |
|
| 550 | - * @throws InvalidArgumentException |
|
| 551 | - * @throws InvalidInterfaceException |
|
| 552 | - * @throws InvalidDataTypeException |
|
| 553 | - * @throws EE_Error |
|
| 554 | - */ |
|
| 555 | - public function start_date($dt_frmt = '') |
|
| 556 | - { |
|
| 557 | - return $this->_show_datetime('D', 'start', $dt_frmt); |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Echoes start_date() |
|
| 563 | - * |
|
| 564 | - * @param string $dt_frmt |
|
| 565 | - * @throws ReflectionException |
|
| 566 | - * @throws InvalidArgumentException |
|
| 567 | - * @throws InvalidInterfaceException |
|
| 568 | - * @throws InvalidDataTypeException |
|
| 569 | - * @throws EE_Error |
|
| 570 | - */ |
|
| 571 | - public function e_start_date($dt_frmt = '') |
|
| 572 | - { |
|
| 573 | - $this->_show_datetime('D', 'start', $dt_frmt, null, true); |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * get end date. Provide either the date format, or NULL to re-use the |
|
| 579 | - * last-used format, or '' to use the default date format |
|
| 580 | - * |
|
| 581 | - * @param string $dt_frmt string representation of date format defaults to 'F j, Y' |
|
| 582 | - * @return mixed string on success, FALSE on fail |
|
| 583 | - * @throws ReflectionException |
|
| 584 | - * @throws InvalidArgumentException |
|
| 585 | - * @throws InvalidInterfaceException |
|
| 586 | - * @throws InvalidDataTypeException |
|
| 587 | - * @throws EE_Error |
|
| 588 | - */ |
|
| 589 | - public function end_date($dt_frmt = '') |
|
| 590 | - { |
|
| 591 | - return $this->_show_datetime('D', 'end', $dt_frmt); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Echoes the end date. See end_date() |
|
| 597 | - * |
|
| 598 | - * @param string $dt_frmt |
|
| 599 | - * @throws ReflectionException |
|
| 600 | - * @throws InvalidArgumentException |
|
| 601 | - * @throws InvalidInterfaceException |
|
| 602 | - * @throws InvalidDataTypeException |
|
| 603 | - * @throws EE_Error |
|
| 604 | - */ |
|
| 605 | - public function e_end_date($dt_frmt = '') |
|
| 606 | - { |
|
| 607 | - $this->_show_datetime('D', 'end', $dt_frmt, null, true); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * get date_range - meaning the start AND end date |
|
| 613 | - * |
|
| 614 | - * @access public |
|
| 615 | - * @param string $dt_frmt string representation of date format defaults to WP settings |
|
| 616 | - * @param string $conjunction conjunction junction what's your function ? |
|
| 617 | - * this string joins the start date with the end date ie: Jan 01 "to" Dec 31 |
|
| 618 | - * @return mixed string on success, FALSE on fail |
|
| 619 | - * @throws ReflectionException |
|
| 620 | - * @throws InvalidArgumentException |
|
| 621 | - * @throws InvalidInterfaceException |
|
| 622 | - * @throws InvalidDataTypeException |
|
| 623 | - * @throws EE_Error |
|
| 624 | - */ |
|
| 625 | - public function date_range($dt_frmt = '', $conjunction = ' - ') |
|
| 626 | - { |
|
| 627 | - $dt_frmt = ! empty($dt_frmt) ? $dt_frmt : $this->_dt_frmt; |
|
| 628 | - $start = str_replace( |
|
| 629 | - ' ', |
|
| 630 | - ' ', |
|
| 631 | - $this->get_i18n_datetime('DTT_EVT_start', $dt_frmt) |
|
| 632 | - ); |
|
| 633 | - $end = str_replace( |
|
| 634 | - ' ', |
|
| 635 | - ' ', |
|
| 636 | - $this->get_i18n_datetime('DTT_EVT_end', $dt_frmt) |
|
| 637 | - ); |
|
| 638 | - return $start !== $end ? $start . $conjunction . $end : $start; |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - |
|
| 642 | - /** |
|
| 643 | - * @param string $dt_frmt |
|
| 644 | - * @param string $conjunction |
|
| 645 | - * @throws ReflectionException |
|
| 646 | - * @throws InvalidArgumentException |
|
| 647 | - * @throws InvalidInterfaceException |
|
| 648 | - * @throws InvalidDataTypeException |
|
| 649 | - * @throws EE_Error |
|
| 650 | - */ |
|
| 651 | - public function e_date_range($dt_frmt = '', $conjunction = ' - ') |
|
| 652 | - { |
|
| 653 | - echo $this->date_range($dt_frmt, $conjunction); |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - |
|
| 657 | - /** |
|
| 658 | - * get start time |
|
| 659 | - * |
|
| 660 | - * @param string $tm_format - string representation of time format defaults to 'g:i a' |
|
| 661 | - * @return mixed string on success, FALSE on fail |
|
| 662 | - * @throws ReflectionException |
|
| 663 | - * @throws InvalidArgumentException |
|
| 664 | - * @throws InvalidInterfaceException |
|
| 665 | - * @throws InvalidDataTypeException |
|
| 666 | - * @throws EE_Error |
|
| 667 | - */ |
|
| 668 | - public function start_time($tm_format = '') |
|
| 669 | - { |
|
| 670 | - return $this->_show_datetime('T', 'start', null, $tm_format); |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - |
|
| 674 | - /** |
|
| 675 | - * @param string $tm_format |
|
| 676 | - * @throws ReflectionException |
|
| 677 | - * @throws InvalidArgumentException |
|
| 678 | - * @throws InvalidInterfaceException |
|
| 679 | - * @throws InvalidDataTypeException |
|
| 680 | - * @throws EE_Error |
|
| 681 | - */ |
|
| 682 | - public function e_start_time($tm_format = '') |
|
| 683 | - { |
|
| 684 | - $this->_show_datetime('T', 'start', null, $tm_format, true); |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - |
|
| 688 | - /** |
|
| 689 | - * get end time |
|
| 690 | - * |
|
| 691 | - * @param string $tm_format string representation of time format defaults to 'g:i a' |
|
| 692 | - * @return mixed string on success, FALSE on fail |
|
| 693 | - * @throws ReflectionException |
|
| 694 | - * @throws InvalidArgumentException |
|
| 695 | - * @throws InvalidInterfaceException |
|
| 696 | - * @throws InvalidDataTypeException |
|
| 697 | - * @throws EE_Error |
|
| 698 | - */ |
|
| 699 | - public function end_time($tm_format = '') |
|
| 700 | - { |
|
| 701 | - return $this->_show_datetime('T', 'end', null, $tm_format); |
|
| 702 | - } |
|
| 703 | - |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * @param string $tm_format |
|
| 707 | - * @throws ReflectionException |
|
| 708 | - * @throws InvalidArgumentException |
|
| 709 | - * @throws InvalidInterfaceException |
|
| 710 | - * @throws InvalidDataTypeException |
|
| 711 | - * @throws EE_Error |
|
| 712 | - */ |
|
| 713 | - public function e_end_time($tm_format = '') |
|
| 714 | - { |
|
| 715 | - $this->_show_datetime('T', 'end', null, $tm_format, true); |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - |
|
| 719 | - /** |
|
| 720 | - * get time_range |
|
| 721 | - * |
|
| 722 | - * @access public |
|
| 723 | - * @param string $tm_format string representation of time format defaults to 'g:i a' |
|
| 724 | - * @param string $conjunction conjunction junction what's your function ? |
|
| 725 | - * this string joins the start date with the end date ie: Jan 01 "to" Dec 31 |
|
| 726 | - * @return mixed string on success, FALSE on fail |
|
| 727 | - * @throws ReflectionException |
|
| 728 | - * @throws InvalidArgumentException |
|
| 729 | - * @throws InvalidInterfaceException |
|
| 730 | - * @throws InvalidDataTypeException |
|
| 731 | - * @throws EE_Error |
|
| 732 | - */ |
|
| 733 | - public function time_range($tm_format = '', $conjunction = ' - ') |
|
| 734 | - { |
|
| 735 | - $tm_format = ! empty($tm_format) ? $tm_format : $this->_tm_frmt; |
|
| 736 | - $start = str_replace( |
|
| 737 | - ' ', |
|
| 738 | - ' ', |
|
| 739 | - $this->get_i18n_datetime('DTT_EVT_start', $tm_format) |
|
| 740 | - ); |
|
| 741 | - $end = str_replace( |
|
| 742 | - ' ', |
|
| 743 | - ' ', |
|
| 744 | - $this->get_i18n_datetime('DTT_EVT_end', $tm_format) |
|
| 745 | - ); |
|
| 746 | - return $start !== $end ? $start . $conjunction . $end : $start; |
|
| 747 | - } |
|
| 748 | - |
|
| 749 | - |
|
| 750 | - /** |
|
| 751 | - * @param string $tm_format |
|
| 752 | - * @param string $conjunction |
|
| 753 | - * @throws ReflectionException |
|
| 754 | - * @throws InvalidArgumentException |
|
| 755 | - * @throws InvalidInterfaceException |
|
| 756 | - * @throws InvalidDataTypeException |
|
| 757 | - * @throws EE_Error |
|
| 758 | - */ |
|
| 759 | - public function e_time_range($tm_format = '', $conjunction = ' - ') |
|
| 760 | - { |
|
| 761 | - echo $this->time_range($tm_format, $conjunction); |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - |
|
| 765 | - /** |
|
| 766 | - * This returns a range representation of the date and times. |
|
| 767 | - * Output is dependent on the difference (or similarity) between DTT_EVT_start and DTT_EVT_end. |
|
| 768 | - * Also, the return value is localized. |
|
| 769 | - * |
|
| 770 | - * @param string $dt_format |
|
| 771 | - * @param string $tm_format |
|
| 772 | - * @param string $conjunction used between two different dates or times. |
|
| 773 | - * ex: Dec 1{$conjunction}}Dec 6, or 2pm{$conjunction}3pm |
|
| 774 | - * @param string $separator used between the date and time formats. |
|
| 775 | - * ex: Dec 1, 2016{$separator}2pm |
|
| 776 | - * @return string |
|
| 777 | - * @throws ReflectionException |
|
| 778 | - * @throws InvalidArgumentException |
|
| 779 | - * @throws InvalidInterfaceException |
|
| 780 | - * @throws InvalidDataTypeException |
|
| 781 | - * @throws EE_Error |
|
| 782 | - */ |
|
| 783 | - public function date_and_time_range( |
|
| 784 | - $dt_format = '', |
|
| 785 | - $tm_format = '', |
|
| 786 | - $conjunction = ' - ', |
|
| 787 | - $separator = ' ' |
|
| 788 | - ) { |
|
| 789 | - $dt_format = ! empty($dt_format) ? $dt_format : $this->_dt_frmt; |
|
| 790 | - $tm_format = ! empty($tm_format) ? $tm_format : $this->_tm_frmt; |
|
| 791 | - $full_format = $dt_format . $separator . $tm_format; |
|
| 792 | - // the range output depends on various conditions |
|
| 793 | - switch (true) { |
|
| 794 | - // start date timestamp and end date timestamp are the same. |
|
| 795 | - case ($this->get_raw('DTT_EVT_start') === $this->get_raw('DTT_EVT_end')): |
|
| 796 | - $output = $this->get_i18n_datetime('DTT_EVT_start', $full_format); |
|
| 797 | - break; |
|
| 798 | - // start and end date are the same but times are different |
|
| 799 | - case ($this->start_date() === $this->end_date()): |
|
| 800 | - $output = $this->get_i18n_datetime('DTT_EVT_start', $full_format) |
|
| 801 | - . $conjunction |
|
| 802 | - . $this->get_i18n_datetime('DTT_EVT_end', $tm_format); |
|
| 803 | - break; |
|
| 804 | - // all other conditions |
|
| 805 | - default: |
|
| 806 | - $output = $this->get_i18n_datetime('DTT_EVT_start', $full_format) |
|
| 807 | - . $conjunction |
|
| 808 | - . $this->get_i18n_datetime('DTT_EVT_end', $full_format); |
|
| 809 | - break; |
|
| 810 | - } |
|
| 811 | - return $output; |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - |
|
| 815 | - /** |
|
| 816 | - * This echos the results of date and time range. |
|
| 817 | - * |
|
| 818 | - * @see date_and_time_range() for more details on purpose. |
|
| 819 | - * @param string $dt_format |
|
| 820 | - * @param string $tm_format |
|
| 821 | - * @param string $conjunction |
|
| 822 | - * @return void |
|
| 823 | - * @throws ReflectionException |
|
| 824 | - * @throws InvalidArgumentException |
|
| 825 | - * @throws InvalidInterfaceException |
|
| 826 | - * @throws InvalidDataTypeException |
|
| 827 | - * @throws EE_Error |
|
| 828 | - */ |
|
| 829 | - public function e_date_and_time_range($dt_format = '', $tm_format = '', $conjunction = ' - ') |
|
| 830 | - { |
|
| 831 | - echo $this->date_and_time_range($dt_format, $tm_format, $conjunction); |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - |
|
| 835 | - /** |
|
| 836 | - * get start date and start time |
|
| 837 | - * |
|
| 838 | - * @param string $dt_format - string representation of date format defaults to 'F j, Y' |
|
| 839 | - * @param string $tm_format - string representation of time format defaults to 'g:i a' |
|
| 840 | - * @return mixed string on success, FALSE on fail |
|
| 841 | - * @throws ReflectionException |
|
| 842 | - * @throws InvalidArgumentException |
|
| 843 | - * @throws InvalidInterfaceException |
|
| 844 | - * @throws InvalidDataTypeException |
|
| 845 | - * @throws EE_Error |
|
| 846 | - */ |
|
| 847 | - public function start_date_and_time($dt_format = '', $tm_format = '') |
|
| 848 | - { |
|
| 849 | - return $this->_show_datetime('', 'start', $dt_format, $tm_format); |
|
| 850 | - } |
|
| 851 | - |
|
| 852 | - |
|
| 853 | - /** |
|
| 854 | - * @param string $dt_frmt |
|
| 855 | - * @param string $tm_format |
|
| 856 | - * @throws ReflectionException |
|
| 857 | - * @throws InvalidArgumentException |
|
| 858 | - * @throws InvalidInterfaceException |
|
| 859 | - * @throws InvalidDataTypeException |
|
| 860 | - * @throws EE_Error |
|
| 861 | - */ |
|
| 862 | - public function e_start_date_and_time($dt_frmt = '', $tm_format = '') |
|
| 863 | - { |
|
| 864 | - $this->_show_datetime('', 'start', $dt_frmt, $tm_format, true); |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - |
|
| 868 | - /** |
|
| 869 | - * Shows the length of the event (start to end time). |
|
| 870 | - * Can be shown in 'seconds','minutes','hours', or 'days'. |
|
| 871 | - * By default, rounds up. (So if you use 'days', and then event |
|
| 872 | - * only occurs for 1 hour, it will return 1 day). |
|
| 873 | - * |
|
| 874 | - * @param string $units 'seconds','minutes','hours','days' |
|
| 875 | - * @param bool $round_up |
|
| 876 | - * @return float|int|mixed |
|
| 877 | - * @throws ReflectionException |
|
| 878 | - * @throws InvalidArgumentException |
|
| 879 | - * @throws InvalidInterfaceException |
|
| 880 | - * @throws InvalidDataTypeException |
|
| 881 | - * @throws EE_Error |
|
| 882 | - */ |
|
| 883 | - public function length($units = 'seconds', $round_up = false) |
|
| 884 | - { |
|
| 885 | - $start = $this->get_raw('DTT_EVT_start'); |
|
| 886 | - $end = $this->get_raw('DTT_EVT_end'); |
|
| 887 | - $length_in_units = $end - $start; |
|
| 888 | - switch ($units) { |
|
| 889 | - // NOTE: We purposefully don't use "break;" in order to chain the divisions |
|
| 890 | - /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 891 | - // phpcs:disable PSR2.ControlStructures.SwitchDeclaration.TerminatingComment |
|
| 892 | - case 'days': |
|
| 893 | - $length_in_units /= 24; |
|
| 894 | - /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 895 | - case 'hours': |
|
| 896 | - // fall through is intentional |
|
| 897 | - $length_in_units /= 60; |
|
| 898 | - /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 899 | - case 'minutes': |
|
| 900 | - // fall through is intentional |
|
| 901 | - $length_in_units /= 60; |
|
| 902 | - case 'seconds': |
|
| 903 | - default: |
|
| 904 | - $length_in_units = ceil($length_in_units); |
|
| 905 | - } |
|
| 906 | - // phpcs:enable |
|
| 907 | - if ($round_up) { |
|
| 908 | - $length_in_units = max($length_in_units, 1); |
|
| 909 | - } |
|
| 910 | - return $length_in_units; |
|
| 911 | - } |
|
| 912 | - |
|
| 913 | - |
|
| 914 | - /** |
|
| 915 | - * get end date and time |
|
| 916 | - * |
|
| 917 | - * @param string $dt_frmt - string representation of date format defaults to 'F j, Y' |
|
| 918 | - * @param string $tm_format - string representation of time format defaults to 'g:i a' |
|
| 919 | - * @return mixed string on success, FALSE on fail |
|
| 920 | - * @throws ReflectionException |
|
| 921 | - * @throws InvalidArgumentException |
|
| 922 | - * @throws InvalidInterfaceException |
|
| 923 | - * @throws InvalidDataTypeException |
|
| 924 | - * @throws EE_Error |
|
| 925 | - */ |
|
| 926 | - public function end_date_and_time($dt_frmt = '', $tm_format = '') |
|
| 927 | - { |
|
| 928 | - return $this->_show_datetime('', 'end', $dt_frmt, $tm_format); |
|
| 929 | - } |
|
| 930 | - |
|
| 931 | - |
|
| 932 | - /** |
|
| 933 | - * @param string $dt_frmt |
|
| 934 | - * @param string $tm_format |
|
| 935 | - * @throws ReflectionException |
|
| 936 | - * @throws InvalidArgumentException |
|
| 937 | - * @throws InvalidInterfaceException |
|
| 938 | - * @throws InvalidDataTypeException |
|
| 939 | - * @throws EE_Error |
|
| 940 | - */ |
|
| 941 | - public function e_end_date_and_time($dt_frmt = '', $tm_format = '') |
|
| 942 | - { |
|
| 943 | - $this->_show_datetime('', 'end', $dt_frmt, $tm_format, true); |
|
| 944 | - } |
|
| 945 | - |
|
| 946 | - |
|
| 947 | - /** |
|
| 948 | - * get start timestamp |
|
| 949 | - * |
|
| 950 | - * @return int |
|
| 951 | - * @throws ReflectionException |
|
| 952 | - * @throws InvalidArgumentException |
|
| 953 | - * @throws InvalidInterfaceException |
|
| 954 | - * @throws InvalidDataTypeException |
|
| 955 | - * @throws EE_Error |
|
| 956 | - */ |
|
| 957 | - public function start() |
|
| 958 | - { |
|
| 959 | - return $this->get_raw('DTT_EVT_start'); |
|
| 960 | - } |
|
| 961 | - |
|
| 962 | - |
|
| 963 | - /** |
|
| 964 | - * get end timestamp |
|
| 965 | - * |
|
| 966 | - * @return int |
|
| 967 | - * @throws ReflectionException |
|
| 968 | - * @throws InvalidArgumentException |
|
| 969 | - * @throws InvalidInterfaceException |
|
| 970 | - * @throws InvalidDataTypeException |
|
| 971 | - * @throws EE_Error |
|
| 972 | - */ |
|
| 973 | - public function end() |
|
| 974 | - { |
|
| 975 | - return $this->get_raw('DTT_EVT_end'); |
|
| 976 | - } |
|
| 977 | - |
|
| 978 | - |
|
| 979 | - /** |
|
| 980 | - * get the registration limit for this datetime slot |
|
| 981 | - * |
|
| 982 | - * @return mixed int on success, FALSE on fail |
|
| 983 | - * @throws ReflectionException |
|
| 984 | - * @throws InvalidArgumentException |
|
| 985 | - * @throws InvalidInterfaceException |
|
| 986 | - * @throws InvalidDataTypeException |
|
| 987 | - * @throws EE_Error |
|
| 988 | - */ |
|
| 989 | - public function reg_limit() |
|
| 990 | - { |
|
| 991 | - return $this->get_raw('DTT_reg_limit'); |
|
| 992 | - } |
|
| 993 | - |
|
| 994 | - |
|
| 995 | - /** |
|
| 996 | - * have the tickets sold for this datetime, met or exceed the registration limit ? |
|
| 997 | - * |
|
| 998 | - * @return boolean |
|
| 999 | - * @throws ReflectionException |
|
| 1000 | - * @throws InvalidArgumentException |
|
| 1001 | - * @throws InvalidInterfaceException |
|
| 1002 | - * @throws InvalidDataTypeException |
|
| 1003 | - * @throws EE_Error |
|
| 1004 | - */ |
|
| 1005 | - public function sold_out() |
|
| 1006 | - { |
|
| 1007 | - return $this->reg_limit() > 0 && $this->sold() >= $this->reg_limit(); |
|
| 1008 | - } |
|
| 1009 | - |
|
| 1010 | - |
|
| 1011 | - /** |
|
| 1012 | - * return the total number of spaces remaining at this venue. |
|
| 1013 | - * This only takes the venue's capacity into account, NOT the tickets available for sale |
|
| 1014 | - * |
|
| 1015 | - * @param bool $consider_tickets Whether to consider tickets remaining when determining if there are any spaces left |
|
| 1016 | - * Because if all tickets attached to this datetime have no spaces left, |
|
| 1017 | - * then this datetime IS effectively sold out. |
|
| 1018 | - * However, there are cases where we just want to know the spaces |
|
| 1019 | - * remaining for this particular datetime, hence the flag. |
|
| 1020 | - * @return int |
|
| 1021 | - * @throws ReflectionException |
|
| 1022 | - * @throws InvalidArgumentException |
|
| 1023 | - * @throws InvalidInterfaceException |
|
| 1024 | - * @throws InvalidDataTypeException |
|
| 1025 | - * @throws EE_Error |
|
| 1026 | - */ |
|
| 1027 | - public function spaces_remaining($consider_tickets = false) |
|
| 1028 | - { |
|
| 1029 | - // tickets remaining available for purchase |
|
| 1030 | - // no need for special checks for infinite, because if DTT_reg_limit == EE_INF, then EE_INF - x = EE_INF |
|
| 1031 | - $dtt_remaining = $this->reg_limit() - $this->sold_and_reserved(); |
|
| 1032 | - if (! $consider_tickets) { |
|
| 1033 | - return $dtt_remaining; |
|
| 1034 | - } |
|
| 1035 | - $tickets_remaining = $this->tickets_remaining(); |
|
| 1036 | - return min($dtt_remaining, $tickets_remaining); |
|
| 1037 | - } |
|
| 1038 | - |
|
| 1039 | - |
|
| 1040 | - /** |
|
| 1041 | - * Counts the total tickets available |
|
| 1042 | - * (from all the different types of tickets which are available for this datetime). |
|
| 1043 | - * |
|
| 1044 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1045 | - * @return int |
|
| 1046 | - * @throws ReflectionException |
|
| 1047 | - * @throws InvalidArgumentException |
|
| 1048 | - * @throws InvalidInterfaceException |
|
| 1049 | - * @throws InvalidDataTypeException |
|
| 1050 | - * @throws EE_Error |
|
| 1051 | - */ |
|
| 1052 | - public function tickets_remaining($query_params = array()) |
|
| 1053 | - { |
|
| 1054 | - $sum = 0; |
|
| 1055 | - $tickets = $this->tickets($query_params); |
|
| 1056 | - if (! empty($tickets)) { |
|
| 1057 | - foreach ($tickets as $ticket) { |
|
| 1058 | - if ($ticket instanceof EE_Ticket) { |
|
| 1059 | - // get the actual amount of tickets that can be sold |
|
| 1060 | - $qty = $ticket->qty('saleable'); |
|
| 1061 | - if ($qty === EE_INF) { |
|
| 1062 | - return EE_INF; |
|
| 1063 | - } |
|
| 1064 | - // no negative ticket quantities plz |
|
| 1065 | - if ($qty > 0) { |
|
| 1066 | - $sum += $qty; |
|
| 1067 | - } |
|
| 1068 | - } |
|
| 1069 | - } |
|
| 1070 | - } |
|
| 1071 | - return $sum; |
|
| 1072 | - } |
|
| 1073 | - |
|
| 1074 | - |
|
| 1075 | - /** |
|
| 1076 | - * Gets the count of all the tickets available at this datetime (not ticket types) |
|
| 1077 | - * before any were sold |
|
| 1078 | - * |
|
| 1079 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1080 | - * @return int |
|
| 1081 | - * @throws ReflectionException |
|
| 1082 | - * @throws InvalidArgumentException |
|
| 1083 | - * @throws InvalidInterfaceException |
|
| 1084 | - * @throws InvalidDataTypeException |
|
| 1085 | - * @throws EE_Error |
|
| 1086 | - */ |
|
| 1087 | - public function sum_tickets_initially_available($query_params = array()) |
|
| 1088 | - { |
|
| 1089 | - return $this->sum_related('Ticket', $query_params, 'TKT_qty'); |
|
| 1090 | - } |
|
| 1091 | - |
|
| 1092 | - |
|
| 1093 | - /** |
|
| 1094 | - * Returns the lesser-of-the two: spaces remaining at this datetime, or |
|
| 1095 | - * the total tickets remaining (a sum of the tickets remaining for each ticket type |
|
| 1096 | - * that is available for this datetime). |
|
| 1097 | - * |
|
| 1098 | - * @return int |
|
| 1099 | - * @throws ReflectionException |
|
| 1100 | - * @throws InvalidArgumentException |
|
| 1101 | - * @throws InvalidInterfaceException |
|
| 1102 | - * @throws InvalidDataTypeException |
|
| 1103 | - * @throws EE_Error |
|
| 1104 | - */ |
|
| 1105 | - public function total_tickets_available_at_this_datetime() |
|
| 1106 | - { |
|
| 1107 | - return $this->spaces_remaining(true); |
|
| 1108 | - } |
|
| 1109 | - |
|
| 1110 | - |
|
| 1111 | - /** |
|
| 1112 | - * This simply compares the internal dtt for the given string with NOW |
|
| 1113 | - * and determines if the date is upcoming or not. |
|
| 1114 | - * |
|
| 1115 | - * @access public |
|
| 1116 | - * @return boolean |
|
| 1117 | - * @throws ReflectionException |
|
| 1118 | - * @throws InvalidArgumentException |
|
| 1119 | - * @throws InvalidInterfaceException |
|
| 1120 | - * @throws InvalidDataTypeException |
|
| 1121 | - * @throws EE_Error |
|
| 1122 | - */ |
|
| 1123 | - public function is_upcoming() |
|
| 1124 | - { |
|
| 1125 | - return ($this->get_raw('DTT_EVT_start') > time()); |
|
| 1126 | - } |
|
| 1127 | - |
|
| 1128 | - |
|
| 1129 | - /** |
|
| 1130 | - * This simply compares the internal datetime for the given string with NOW |
|
| 1131 | - * and returns if the date is active (i.e. start and end time) |
|
| 1132 | - * |
|
| 1133 | - * @return boolean |
|
| 1134 | - * @throws ReflectionException |
|
| 1135 | - * @throws InvalidArgumentException |
|
| 1136 | - * @throws InvalidInterfaceException |
|
| 1137 | - * @throws InvalidDataTypeException |
|
| 1138 | - * @throws EE_Error |
|
| 1139 | - */ |
|
| 1140 | - public function is_active() |
|
| 1141 | - { |
|
| 1142 | - return ($this->get_raw('DTT_EVT_start') < time() && $this->get_raw('DTT_EVT_end') > time()); |
|
| 1143 | - } |
|
| 1144 | - |
|
| 1145 | - |
|
| 1146 | - /** |
|
| 1147 | - * This simply compares the internal dtt for the given string with NOW |
|
| 1148 | - * and determines if the date is expired or not. |
|
| 1149 | - * |
|
| 1150 | - * @return boolean |
|
| 1151 | - * @throws ReflectionException |
|
| 1152 | - * @throws InvalidArgumentException |
|
| 1153 | - * @throws InvalidInterfaceException |
|
| 1154 | - * @throws InvalidDataTypeException |
|
| 1155 | - * @throws EE_Error |
|
| 1156 | - */ |
|
| 1157 | - public function is_expired() |
|
| 1158 | - { |
|
| 1159 | - return ($this->get_raw('DTT_EVT_end') < time()); |
|
| 1160 | - } |
|
| 1161 | - |
|
| 1162 | - |
|
| 1163 | - /** |
|
| 1164 | - * This returns the active status for whether an event is active, upcoming, or expired |
|
| 1165 | - * |
|
| 1166 | - * @return int return value will be one of the EE_Datetime status constants. |
|
| 1167 | - * @throws ReflectionException |
|
| 1168 | - * @throws InvalidArgumentException |
|
| 1169 | - * @throws InvalidInterfaceException |
|
| 1170 | - * @throws InvalidDataTypeException |
|
| 1171 | - * @throws EE_Error |
|
| 1172 | - */ |
|
| 1173 | - public function get_active_status() |
|
| 1174 | - { |
|
| 1175 | - $total_tickets_for_this_dtt = $this->total_tickets_available_at_this_datetime(); |
|
| 1176 | - if ($total_tickets_for_this_dtt !== false && $total_tickets_for_this_dtt < 1) { |
|
| 1177 | - return EE_Datetime::sold_out; |
|
| 1178 | - } |
|
| 1179 | - if ($this->is_expired()) { |
|
| 1180 | - return EE_Datetime::expired; |
|
| 1181 | - } |
|
| 1182 | - if ($this->is_upcoming()) { |
|
| 1183 | - return EE_Datetime::upcoming; |
|
| 1184 | - } |
|
| 1185 | - if ($this->is_active()) { |
|
| 1186 | - return EE_Datetime::active; |
|
| 1187 | - } |
|
| 1188 | - return null; |
|
| 1189 | - } |
|
| 1190 | - |
|
| 1191 | - |
|
| 1192 | - /** |
|
| 1193 | - * This returns a nice display name for the datetime that is contingent on the span between the dates and times. |
|
| 1194 | - * |
|
| 1195 | - * @param boolean $use_dtt_name if TRUE then we'll use DTT->name() if its not empty. |
|
| 1196 | - * @return string |
|
| 1197 | - * @throws ReflectionException |
|
| 1198 | - * @throws InvalidArgumentException |
|
| 1199 | - * @throws InvalidInterfaceException |
|
| 1200 | - * @throws InvalidDataTypeException |
|
| 1201 | - * @throws EE_Error |
|
| 1202 | - */ |
|
| 1203 | - public function get_dtt_display_name($use_dtt_name = false) |
|
| 1204 | - { |
|
| 1205 | - if ($use_dtt_name) { |
|
| 1206 | - $dtt_name = $this->name(); |
|
| 1207 | - if (! empty($dtt_name)) { |
|
| 1208 | - return $dtt_name; |
|
| 1209 | - } |
|
| 1210 | - } |
|
| 1211 | - // first condition is to see if the months are different |
|
| 1212 | - if (date('m', $this->get_raw('DTT_EVT_start')) !== date('m', $this->get_raw('DTT_EVT_end')) |
|
| 1213 | - ) { |
|
| 1214 | - $display_date = $this->start_date('M j\, Y g:i a') . ' - ' . $this->end_date('M j\, Y g:i a'); |
|
| 1215 | - // next condition is if its the same month but different day |
|
| 1216 | - } else { |
|
| 1217 | - if (date('m', $this->get_raw('DTT_EVT_start')) === date('m', $this->get_raw('DTT_EVT_end')) |
|
| 1218 | - && date('d', $this->get_raw('DTT_EVT_start')) !== date('d', $this->get_raw('DTT_EVT_end')) |
|
| 1219 | - ) { |
|
| 1220 | - $display_date = $this->start_date('M j\, g:i a') . ' - ' . $this->end_date('M j\, g:i a Y'); |
|
| 1221 | - } else { |
|
| 1222 | - $display_date = $this->start_date('F j\, Y') |
|
| 1223 | - . ' @ ' |
|
| 1224 | - . $this->start_date('g:i a') |
|
| 1225 | - . ' - ' |
|
| 1226 | - . $this->end_date('g:i a'); |
|
| 1227 | - } |
|
| 1228 | - } |
|
| 1229 | - return $display_date; |
|
| 1230 | - } |
|
| 1231 | - |
|
| 1232 | - |
|
| 1233 | - /** |
|
| 1234 | - * Gets all the tickets for this datetime |
|
| 1235 | - * |
|
| 1236 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1237 | - * @return EE_Base_Class[]|EE_Ticket[] |
|
| 1238 | - * @throws ReflectionException |
|
| 1239 | - * @throws InvalidArgumentException |
|
| 1240 | - * @throws InvalidInterfaceException |
|
| 1241 | - * @throws InvalidDataTypeException |
|
| 1242 | - * @throws EE_Error |
|
| 1243 | - */ |
|
| 1244 | - public function tickets($query_params = array()) |
|
| 1245 | - { |
|
| 1246 | - return $this->get_many_related('Ticket', $query_params); |
|
| 1247 | - } |
|
| 1248 | - |
|
| 1249 | - |
|
| 1250 | - /** |
|
| 1251 | - * Gets all the ticket types currently available for purchase |
|
| 1252 | - * |
|
| 1253 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1254 | - * @return EE_Ticket[] |
|
| 1255 | - * @throws ReflectionException |
|
| 1256 | - * @throws InvalidArgumentException |
|
| 1257 | - * @throws InvalidInterfaceException |
|
| 1258 | - * @throws InvalidDataTypeException |
|
| 1259 | - * @throws EE_Error |
|
| 1260 | - */ |
|
| 1261 | - public function ticket_types_available_for_purchase($query_params = array()) |
|
| 1262 | - { |
|
| 1263 | - // first check if datetime is valid |
|
| 1264 | - if ($this->sold_out() || ! ($this->is_upcoming() || $this->is_active())) { |
|
| 1265 | - return array(); |
|
| 1266 | - } |
|
| 1267 | - if (empty($query_params)) { |
|
| 1268 | - $query_params = array( |
|
| 1269 | - array( |
|
| 1270 | - 'TKT_start_date' => array('<=', EEM_Ticket::instance()->current_time_for_query('TKT_start_date')), |
|
| 1271 | - 'TKT_end_date' => array('>=', EEM_Ticket::instance()->current_time_for_query('TKT_end_date')), |
|
| 1272 | - 'TKT_deleted' => false, |
|
| 1273 | - ), |
|
| 1274 | - ); |
|
| 1275 | - } |
|
| 1276 | - return $this->tickets($query_params); |
|
| 1277 | - } |
|
| 1278 | - |
|
| 1279 | - |
|
| 1280 | - /** |
|
| 1281 | - * @return EE_Base_Class|EE_Event |
|
| 1282 | - * @throws ReflectionException |
|
| 1283 | - * @throws InvalidArgumentException |
|
| 1284 | - * @throws InvalidInterfaceException |
|
| 1285 | - * @throws InvalidDataTypeException |
|
| 1286 | - * @throws EE_Error |
|
| 1287 | - */ |
|
| 1288 | - public function event() |
|
| 1289 | - { |
|
| 1290 | - return $this->get_first_related('Event'); |
|
| 1291 | - } |
|
| 1292 | - |
|
| 1293 | - |
|
| 1294 | - /** |
|
| 1295 | - * Updates the DTT_sold attribute (and saves) based on the number of registrations for this datetime |
|
| 1296 | - * (via the tickets). |
|
| 1297 | - * |
|
| 1298 | - * @return int |
|
| 1299 | - * @throws ReflectionException |
|
| 1300 | - * @throws InvalidArgumentException |
|
| 1301 | - * @throws InvalidInterfaceException |
|
| 1302 | - * @throws InvalidDataTypeException |
|
| 1303 | - * @throws EE_Error |
|
| 1304 | - */ |
|
| 1305 | - public function update_sold() |
|
| 1306 | - { |
|
| 1307 | - $count_regs_for_this_datetime = EEM_Registration::instance()->count( |
|
| 1308 | - array( |
|
| 1309 | - array( |
|
| 1310 | - 'STS_ID' => EEM_Registration::status_id_approved, |
|
| 1311 | - 'REG_deleted' => 0, |
|
| 1312 | - 'Ticket.Datetime.DTT_ID' => $this->ID(), |
|
| 1313 | - ), |
|
| 1314 | - ) |
|
| 1315 | - ); |
|
| 1316 | - $this->set_sold($count_regs_for_this_datetime); |
|
| 1317 | - $this->save(); |
|
| 1318 | - return $count_regs_for_this_datetime; |
|
| 1319 | - } |
|
| 1320 | - |
|
| 1321 | - |
|
| 1322 | - /******************************************************************* |
|
| 16 | + /** |
|
| 17 | + * constant used by get_active_status, indicates datetime has no more available spaces |
|
| 18 | + */ |
|
| 19 | + const sold_out = 'DTS'; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * constant used by get_active_status, indicating datetime is still active (even is not over, can be registered-for) |
|
| 23 | + */ |
|
| 24 | + const active = 'DTA'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * constant used by get_active_status, indicating the datetime cannot be used for registrations yet, but has not |
|
| 28 | + * expired |
|
| 29 | + */ |
|
| 30 | + const upcoming = 'DTU'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Datetime is postponed |
|
| 34 | + */ |
|
| 35 | + const postponed = 'DTP'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Datetime is cancelled |
|
| 39 | + */ |
|
| 40 | + const cancelled = 'DTC'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * constant used by get_active_status, indicates datetime has expired (event is over) |
|
| 44 | + */ |
|
| 45 | + const expired = 'DTE'; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * constant used in various places indicating that an event is INACTIVE (not yet ready to be published) |
|
| 49 | + */ |
|
| 50 | + const inactive = 'DTI'; |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param array $props_n_values incoming values |
|
| 55 | + * @param string $timezone incoming timezone (if not set the timezone set for the website will be used.) |
|
| 56 | + * @param array $date_formats incoming date_formats in an array where the first value is the date_format |
|
| 57 | + * and the second value is the time format |
|
| 58 | + * @return EE_Datetime |
|
| 59 | + * @throws ReflectionException |
|
| 60 | + * @throws InvalidArgumentException |
|
| 61 | + * @throws InvalidInterfaceException |
|
| 62 | + * @throws InvalidDataTypeException |
|
| 63 | + * @throws EE_Error |
|
| 64 | + */ |
|
| 65 | + public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array()) |
|
| 66 | + { |
|
| 67 | + $has_object = parent::_check_for_object( |
|
| 68 | + $props_n_values, |
|
| 69 | + __CLASS__, |
|
| 70 | + $timezone, |
|
| 71 | + $date_formats |
|
| 72 | + ); |
|
| 73 | + return $has_object |
|
| 74 | + ? $has_object |
|
| 75 | + : new self($props_n_values, false, $timezone, $date_formats); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param array $props_n_values incoming values from the database |
|
| 81 | + * @param string $timezone incoming timezone as set by the model. If not set the timezone for |
|
| 82 | + * the website will be used. |
|
| 83 | + * @return EE_Datetime |
|
| 84 | + * @throws ReflectionException |
|
| 85 | + * @throws InvalidArgumentException |
|
| 86 | + * @throws InvalidInterfaceException |
|
| 87 | + * @throws InvalidDataTypeException |
|
| 88 | + * @throws EE_Error |
|
| 89 | + */ |
|
| 90 | + public static function new_instance_from_db($props_n_values = array(), $timezone = null) |
|
| 91 | + { |
|
| 92 | + return new self($props_n_values, true, $timezone); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @param $name |
|
| 98 | + * @throws ReflectionException |
|
| 99 | + * @throws InvalidArgumentException |
|
| 100 | + * @throws InvalidInterfaceException |
|
| 101 | + * @throws InvalidDataTypeException |
|
| 102 | + * @throws EE_Error |
|
| 103 | + */ |
|
| 104 | + public function set_name($name) |
|
| 105 | + { |
|
| 106 | + $this->set('DTT_name', $name); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @param $description |
|
| 112 | + * @throws ReflectionException |
|
| 113 | + * @throws InvalidArgumentException |
|
| 114 | + * @throws InvalidInterfaceException |
|
| 115 | + * @throws InvalidDataTypeException |
|
| 116 | + * @throws EE_Error |
|
| 117 | + */ |
|
| 118 | + public function set_description($description) |
|
| 119 | + { |
|
| 120 | + $this->set('DTT_description', $description); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Set event start date |
|
| 126 | + * set the start date for an event |
|
| 127 | + * |
|
| 128 | + * @param string $date a string representation of the event's date ex: Dec. 25, 2025 or 12-25-2025 |
|
| 129 | + * @throws ReflectionException |
|
| 130 | + * @throws InvalidArgumentException |
|
| 131 | + * @throws InvalidInterfaceException |
|
| 132 | + * @throws InvalidDataTypeException |
|
| 133 | + * @throws EE_Error |
|
| 134 | + */ |
|
| 135 | + public function set_start_date($date) |
|
| 136 | + { |
|
| 137 | + $this->_set_date_for($date, 'DTT_EVT_start'); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Set event start time |
|
| 143 | + * set the start time for an event |
|
| 144 | + * |
|
| 145 | + * @param string $time a string representation of the event time ex: 9am or 7:30 PM |
|
| 146 | + * @throws ReflectionException |
|
| 147 | + * @throws InvalidArgumentException |
|
| 148 | + * @throws InvalidInterfaceException |
|
| 149 | + * @throws InvalidDataTypeException |
|
| 150 | + * @throws EE_Error |
|
| 151 | + */ |
|
| 152 | + public function set_start_time($time) |
|
| 153 | + { |
|
| 154 | + $this->_set_time_for($time, 'DTT_EVT_start'); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Set event end date |
|
| 160 | + * set the end date for an event |
|
| 161 | + * |
|
| 162 | + * @param string $date a string representation of the event's date ex: Dec. 25, 2025 or 12-25-2025 |
|
| 163 | + * @throws ReflectionException |
|
| 164 | + * @throws InvalidArgumentException |
|
| 165 | + * @throws InvalidInterfaceException |
|
| 166 | + * @throws InvalidDataTypeException |
|
| 167 | + * @throws EE_Error |
|
| 168 | + */ |
|
| 169 | + public function set_end_date($date) |
|
| 170 | + { |
|
| 171 | + $this->_set_date_for($date, 'DTT_EVT_end'); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Set event end time |
|
| 177 | + * set the end time for an event |
|
| 178 | + * |
|
| 179 | + * @param string $time a string representation of the event time ex: 9am or 7:30 PM |
|
| 180 | + * @throws ReflectionException |
|
| 181 | + * @throws InvalidArgumentException |
|
| 182 | + * @throws InvalidInterfaceException |
|
| 183 | + * @throws InvalidDataTypeException |
|
| 184 | + * @throws EE_Error |
|
| 185 | + */ |
|
| 186 | + public function set_end_time($time) |
|
| 187 | + { |
|
| 188 | + $this->_set_time_for($time, 'DTT_EVT_end'); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Set registration limit |
|
| 194 | + * set the maximum number of attendees that can be registered for this datetime slot |
|
| 195 | + * |
|
| 196 | + * @param int $reg_limit |
|
| 197 | + * @throws ReflectionException |
|
| 198 | + * @throws InvalidArgumentException |
|
| 199 | + * @throws InvalidInterfaceException |
|
| 200 | + * @throws InvalidDataTypeException |
|
| 201 | + * @throws EE_Error |
|
| 202 | + */ |
|
| 203 | + public function set_reg_limit($reg_limit) |
|
| 204 | + { |
|
| 205 | + $this->set('DTT_reg_limit', $reg_limit); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * get the number of tickets sold for this datetime slot |
|
| 211 | + * |
|
| 212 | + * @return mixed int on success, FALSE on fail |
|
| 213 | + * @throws ReflectionException |
|
| 214 | + * @throws InvalidArgumentException |
|
| 215 | + * @throws InvalidInterfaceException |
|
| 216 | + * @throws InvalidDataTypeException |
|
| 217 | + * @throws EE_Error |
|
| 218 | + */ |
|
| 219 | + public function sold() |
|
| 220 | + { |
|
| 221 | + return $this->get_raw('DTT_sold'); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @param int $sold |
|
| 227 | + * @throws ReflectionException |
|
| 228 | + * @throws InvalidArgumentException |
|
| 229 | + * @throws InvalidInterfaceException |
|
| 230 | + * @throws InvalidDataTypeException |
|
| 231 | + * @throws EE_Error |
|
| 232 | + */ |
|
| 233 | + public function set_sold($sold) |
|
| 234 | + { |
|
| 235 | + // sold can not go below zero |
|
| 236 | + $sold = max(0, $sold); |
|
| 237 | + $this->set('DTT_sold', $sold); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Increments sold by amount passed by $qty, and persists it immediately to the database. |
|
| 243 | + * Simultaneously decreases the reserved count, unless $also_decrease_reserved is false. |
|
| 244 | + * |
|
| 245 | + * @param int $qty |
|
| 246 | + * @param boolean $also_decrease_reserved |
|
| 247 | + * @return boolean indicating success |
|
| 248 | + * @throws ReflectionException |
|
| 249 | + * @throws InvalidArgumentException |
|
| 250 | + * @throws InvalidInterfaceException |
|
| 251 | + * @throws InvalidDataTypeException |
|
| 252 | + * @throws EE_Error |
|
| 253 | + */ |
|
| 254 | + public function increaseSold($qty = 1, $also_decrease_reserved = true) |
|
| 255 | + { |
|
| 256 | + $qty = absint($qty); |
|
| 257 | + if ($also_decrease_reserved) { |
|
| 258 | + $success = $this->adjustNumericFieldsInDb( |
|
| 259 | + [ |
|
| 260 | + 'DTT_reserved' => $qty * -1, |
|
| 261 | + 'DTT_sold' => $qty |
|
| 262 | + ] |
|
| 263 | + ); |
|
| 264 | + } else { |
|
| 265 | + $success = $this->adjustNumericFieldsInDb( |
|
| 266 | + [ |
|
| 267 | + 'DTT_sold' => $qty |
|
| 268 | + ] |
|
| 269 | + ); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + do_action( |
|
| 273 | + 'AHEE__EE_Datetime__increase_sold', |
|
| 274 | + $this, |
|
| 275 | + $qty, |
|
| 276 | + $this->sold(), |
|
| 277 | + $success |
|
| 278 | + ); |
|
| 279 | + return $success; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Decrements (subtracts) sold amount passed by $qty directly in the DB and on the model object. (Ie, no need |
|
| 285 | + * to save afterwards.) |
|
| 286 | + * |
|
| 287 | + * @param int $qty |
|
| 288 | + * @return boolean indicating success |
|
| 289 | + * @throws ReflectionException |
|
| 290 | + * @throws InvalidArgumentException |
|
| 291 | + * @throws InvalidInterfaceException |
|
| 292 | + * @throws InvalidDataTypeException |
|
| 293 | + * @throws EE_Error |
|
| 294 | + */ |
|
| 295 | + public function decreaseSold($qty = 1) |
|
| 296 | + { |
|
| 297 | + $qty = absint($qty); |
|
| 298 | + $success = $this->adjustNumericFieldsInDb( |
|
| 299 | + [ |
|
| 300 | + 'DTT_sold' => $qty * -1 |
|
| 301 | + ] |
|
| 302 | + ); |
|
| 303 | + do_action( |
|
| 304 | + 'AHEE__EE_Datetime__decrease_sold', |
|
| 305 | + $this, |
|
| 306 | + $qty, |
|
| 307 | + $this->sold(), |
|
| 308 | + $success |
|
| 309 | + ); |
|
| 310 | + return $success; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Gets qty of reserved tickets for this datetime |
|
| 316 | + * |
|
| 317 | + * @return int |
|
| 318 | + * @throws ReflectionException |
|
| 319 | + * @throws InvalidArgumentException |
|
| 320 | + * @throws InvalidInterfaceException |
|
| 321 | + * @throws InvalidDataTypeException |
|
| 322 | + * @throws EE_Error |
|
| 323 | + */ |
|
| 324 | + public function reserved() |
|
| 325 | + { |
|
| 326 | + return $this->get_raw('DTT_reserved'); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Sets qty of reserved tickets for this datetime |
|
| 332 | + * |
|
| 333 | + * @param int $reserved |
|
| 334 | + * @throws ReflectionException |
|
| 335 | + * @throws InvalidArgumentException |
|
| 336 | + * @throws InvalidInterfaceException |
|
| 337 | + * @throws InvalidDataTypeException |
|
| 338 | + * @throws EE_Error |
|
| 339 | + */ |
|
| 340 | + public function set_reserved($reserved) |
|
| 341 | + { |
|
| 342 | + // reserved can not go below zero |
|
| 343 | + $reserved = max(0, (int) $reserved); |
|
| 344 | + $this->set('DTT_reserved', $reserved); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Increments reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 350 | + * |
|
| 351 | + * @param int $qty |
|
| 352 | + * @return boolean indicating success |
|
| 353 | + * @throws ReflectionException |
|
| 354 | + * @throws InvalidArgumentException |
|
| 355 | + * @throws InvalidInterfaceException |
|
| 356 | + * @throws InvalidDataTypeException |
|
| 357 | + * @throws EE_Error |
|
| 358 | + */ |
|
| 359 | + public function increaseReserved($qty = 1) |
|
| 360 | + { |
|
| 361 | + $qty = absint($qty); |
|
| 362 | + $success = $this->incrementFieldConditionallyInDb( |
|
| 363 | + 'DTT_reserved', |
|
| 364 | + 'DTT_sold', |
|
| 365 | + 'DTT_reg_limit', |
|
| 366 | + $qty |
|
| 367 | + ); |
|
| 368 | + do_action( |
|
| 369 | + 'AHEE__EE_Datetime__increase_reserved', |
|
| 370 | + $this, |
|
| 371 | + $qty, |
|
| 372 | + $this->reserved(), |
|
| 373 | + $success |
|
| 374 | + ); |
|
| 375 | + return $success; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 381 | + * |
|
| 382 | + * @param int $qty |
|
| 383 | + * @return boolean indicating success |
|
| 384 | + * @throws ReflectionException |
|
| 385 | + * @throws InvalidArgumentException |
|
| 386 | + * @throws InvalidInterfaceException |
|
| 387 | + * @throws InvalidDataTypeException |
|
| 388 | + * @throws EE_Error |
|
| 389 | + */ |
|
| 390 | + public function decreaseReserved($qty = 1) |
|
| 391 | + { |
|
| 392 | + $qty = absint($qty); |
|
| 393 | + $success = $this->adjustNumericFieldsInDb( |
|
| 394 | + [ |
|
| 395 | + 'DTT_reserved' => $qty * -1 |
|
| 396 | + ] |
|
| 397 | + ); |
|
| 398 | + do_action( |
|
| 399 | + 'AHEE__EE_Datetime__decrease_reserved', |
|
| 400 | + $this, |
|
| 401 | + $qty, |
|
| 402 | + $this->reserved(), |
|
| 403 | + $success |
|
| 404 | + ); |
|
| 405 | + return $success; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * total sold and reserved tickets |
|
| 411 | + * |
|
| 412 | + * @return int |
|
| 413 | + * @throws ReflectionException |
|
| 414 | + * @throws InvalidArgumentException |
|
| 415 | + * @throws InvalidInterfaceException |
|
| 416 | + * @throws InvalidDataTypeException |
|
| 417 | + * @throws EE_Error |
|
| 418 | + */ |
|
| 419 | + public function sold_and_reserved() |
|
| 420 | + { |
|
| 421 | + return $this->sold() + $this->reserved(); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * returns the datetime name |
|
| 427 | + * |
|
| 428 | + * @return string |
|
| 429 | + * @throws ReflectionException |
|
| 430 | + * @throws InvalidArgumentException |
|
| 431 | + * @throws InvalidInterfaceException |
|
| 432 | + * @throws InvalidDataTypeException |
|
| 433 | + * @throws EE_Error |
|
| 434 | + */ |
|
| 435 | + public function name() |
|
| 436 | + { |
|
| 437 | + return $this->get('DTT_name'); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * returns the datetime description |
|
| 443 | + * |
|
| 444 | + * @return string |
|
| 445 | + * @throws ReflectionException |
|
| 446 | + * @throws InvalidArgumentException |
|
| 447 | + * @throws InvalidInterfaceException |
|
| 448 | + * @throws InvalidDataTypeException |
|
| 449 | + * @throws EE_Error |
|
| 450 | + */ |
|
| 451 | + public function description() |
|
| 452 | + { |
|
| 453 | + return $this->get('DTT_description'); |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * This helper simply returns whether the event_datetime for the current datetime is a primary datetime |
|
| 459 | + * |
|
| 460 | + * @return boolean TRUE if is primary, FALSE if not. |
|
| 461 | + * @throws ReflectionException |
|
| 462 | + * @throws InvalidArgumentException |
|
| 463 | + * @throws InvalidInterfaceException |
|
| 464 | + * @throws InvalidDataTypeException |
|
| 465 | + * @throws EE_Error |
|
| 466 | + */ |
|
| 467 | + public function is_primary() |
|
| 468 | + { |
|
| 469 | + return $this->get('DTT_is_primary'); |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + |
|
| 473 | + /** |
|
| 474 | + * This helper simply returns the order for the datetime |
|
| 475 | + * |
|
| 476 | + * @return int The order of the datetime for this event. |
|
| 477 | + * @throws ReflectionException |
|
| 478 | + * @throws InvalidArgumentException |
|
| 479 | + * @throws InvalidInterfaceException |
|
| 480 | + * @throws InvalidDataTypeException |
|
| 481 | + * @throws EE_Error |
|
| 482 | + */ |
|
| 483 | + public function order() |
|
| 484 | + { |
|
| 485 | + return $this->get('DTT_order'); |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * This helper simply returns the parent id for the datetime |
|
| 491 | + * |
|
| 492 | + * @return int |
|
| 493 | + * @throws ReflectionException |
|
| 494 | + * @throws InvalidArgumentException |
|
| 495 | + * @throws InvalidInterfaceException |
|
| 496 | + * @throws InvalidDataTypeException |
|
| 497 | + * @throws EE_Error |
|
| 498 | + */ |
|
| 499 | + public function parent() |
|
| 500 | + { |
|
| 501 | + return $this->get('DTT_parent'); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * show date and/or time |
|
| 507 | + * |
|
| 508 | + * @param string $date_or_time whether to display a date or time or both |
|
| 509 | + * @param string $start_or_end whether to display start or end datetimes |
|
| 510 | + * @param string $dt_frmt |
|
| 511 | + * @param string $tm_frmt |
|
| 512 | + * @param bool $echo whether we echo or return (note echoing uses "pretty" formats, |
|
| 513 | + * otherwise we use the standard formats) |
|
| 514 | + * @return string|bool string on success, FALSE on fail |
|
| 515 | + * @throws ReflectionException |
|
| 516 | + * @throws InvalidArgumentException |
|
| 517 | + * @throws InvalidInterfaceException |
|
| 518 | + * @throws InvalidDataTypeException |
|
| 519 | + * @throws EE_Error |
|
| 520 | + */ |
|
| 521 | + private function _show_datetime( |
|
| 522 | + $date_or_time = null, |
|
| 523 | + $start_or_end = 'start', |
|
| 524 | + $dt_frmt = '', |
|
| 525 | + $tm_frmt = '', |
|
| 526 | + $echo = false |
|
| 527 | + ) { |
|
| 528 | + $field_name = "DTT_EVT_{$start_or_end}"; |
|
| 529 | + $dtt = $this->_get_datetime( |
|
| 530 | + $field_name, |
|
| 531 | + $dt_frmt, |
|
| 532 | + $tm_frmt, |
|
| 533 | + $date_or_time, |
|
| 534 | + $echo |
|
| 535 | + ); |
|
| 536 | + if (! $echo) { |
|
| 537 | + return $dtt; |
|
| 538 | + } |
|
| 539 | + return ''; |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + |
|
| 543 | + /** |
|
| 544 | + * get event start date. Provide either the date format, or NULL to re-use the |
|
| 545 | + * last-used format, or '' to use the default date format |
|
| 546 | + * |
|
| 547 | + * @param string $dt_frmt string representation of date format defaults to 'F j, Y' |
|
| 548 | + * @return mixed string on success, FALSE on fail |
|
| 549 | + * @throws ReflectionException |
|
| 550 | + * @throws InvalidArgumentException |
|
| 551 | + * @throws InvalidInterfaceException |
|
| 552 | + * @throws InvalidDataTypeException |
|
| 553 | + * @throws EE_Error |
|
| 554 | + */ |
|
| 555 | + public function start_date($dt_frmt = '') |
|
| 556 | + { |
|
| 557 | + return $this->_show_datetime('D', 'start', $dt_frmt); |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * Echoes start_date() |
|
| 563 | + * |
|
| 564 | + * @param string $dt_frmt |
|
| 565 | + * @throws ReflectionException |
|
| 566 | + * @throws InvalidArgumentException |
|
| 567 | + * @throws InvalidInterfaceException |
|
| 568 | + * @throws InvalidDataTypeException |
|
| 569 | + * @throws EE_Error |
|
| 570 | + */ |
|
| 571 | + public function e_start_date($dt_frmt = '') |
|
| 572 | + { |
|
| 573 | + $this->_show_datetime('D', 'start', $dt_frmt, null, true); |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * get end date. Provide either the date format, or NULL to re-use the |
|
| 579 | + * last-used format, or '' to use the default date format |
|
| 580 | + * |
|
| 581 | + * @param string $dt_frmt string representation of date format defaults to 'F j, Y' |
|
| 582 | + * @return mixed string on success, FALSE on fail |
|
| 583 | + * @throws ReflectionException |
|
| 584 | + * @throws InvalidArgumentException |
|
| 585 | + * @throws InvalidInterfaceException |
|
| 586 | + * @throws InvalidDataTypeException |
|
| 587 | + * @throws EE_Error |
|
| 588 | + */ |
|
| 589 | + public function end_date($dt_frmt = '') |
|
| 590 | + { |
|
| 591 | + return $this->_show_datetime('D', 'end', $dt_frmt); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Echoes the end date. See end_date() |
|
| 597 | + * |
|
| 598 | + * @param string $dt_frmt |
|
| 599 | + * @throws ReflectionException |
|
| 600 | + * @throws InvalidArgumentException |
|
| 601 | + * @throws InvalidInterfaceException |
|
| 602 | + * @throws InvalidDataTypeException |
|
| 603 | + * @throws EE_Error |
|
| 604 | + */ |
|
| 605 | + public function e_end_date($dt_frmt = '') |
|
| 606 | + { |
|
| 607 | + $this->_show_datetime('D', 'end', $dt_frmt, null, true); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * get date_range - meaning the start AND end date |
|
| 613 | + * |
|
| 614 | + * @access public |
|
| 615 | + * @param string $dt_frmt string representation of date format defaults to WP settings |
|
| 616 | + * @param string $conjunction conjunction junction what's your function ? |
|
| 617 | + * this string joins the start date with the end date ie: Jan 01 "to" Dec 31 |
|
| 618 | + * @return mixed string on success, FALSE on fail |
|
| 619 | + * @throws ReflectionException |
|
| 620 | + * @throws InvalidArgumentException |
|
| 621 | + * @throws InvalidInterfaceException |
|
| 622 | + * @throws InvalidDataTypeException |
|
| 623 | + * @throws EE_Error |
|
| 624 | + */ |
|
| 625 | + public function date_range($dt_frmt = '', $conjunction = ' - ') |
|
| 626 | + { |
|
| 627 | + $dt_frmt = ! empty($dt_frmt) ? $dt_frmt : $this->_dt_frmt; |
|
| 628 | + $start = str_replace( |
|
| 629 | + ' ', |
|
| 630 | + ' ', |
|
| 631 | + $this->get_i18n_datetime('DTT_EVT_start', $dt_frmt) |
|
| 632 | + ); |
|
| 633 | + $end = str_replace( |
|
| 634 | + ' ', |
|
| 635 | + ' ', |
|
| 636 | + $this->get_i18n_datetime('DTT_EVT_end', $dt_frmt) |
|
| 637 | + ); |
|
| 638 | + return $start !== $end ? $start . $conjunction . $end : $start; |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + |
|
| 642 | + /** |
|
| 643 | + * @param string $dt_frmt |
|
| 644 | + * @param string $conjunction |
|
| 645 | + * @throws ReflectionException |
|
| 646 | + * @throws InvalidArgumentException |
|
| 647 | + * @throws InvalidInterfaceException |
|
| 648 | + * @throws InvalidDataTypeException |
|
| 649 | + * @throws EE_Error |
|
| 650 | + */ |
|
| 651 | + public function e_date_range($dt_frmt = '', $conjunction = ' - ') |
|
| 652 | + { |
|
| 653 | + echo $this->date_range($dt_frmt, $conjunction); |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + |
|
| 657 | + /** |
|
| 658 | + * get start time |
|
| 659 | + * |
|
| 660 | + * @param string $tm_format - string representation of time format defaults to 'g:i a' |
|
| 661 | + * @return mixed string on success, FALSE on fail |
|
| 662 | + * @throws ReflectionException |
|
| 663 | + * @throws InvalidArgumentException |
|
| 664 | + * @throws InvalidInterfaceException |
|
| 665 | + * @throws InvalidDataTypeException |
|
| 666 | + * @throws EE_Error |
|
| 667 | + */ |
|
| 668 | + public function start_time($tm_format = '') |
|
| 669 | + { |
|
| 670 | + return $this->_show_datetime('T', 'start', null, $tm_format); |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + |
|
| 674 | + /** |
|
| 675 | + * @param string $tm_format |
|
| 676 | + * @throws ReflectionException |
|
| 677 | + * @throws InvalidArgumentException |
|
| 678 | + * @throws InvalidInterfaceException |
|
| 679 | + * @throws InvalidDataTypeException |
|
| 680 | + * @throws EE_Error |
|
| 681 | + */ |
|
| 682 | + public function e_start_time($tm_format = '') |
|
| 683 | + { |
|
| 684 | + $this->_show_datetime('T', 'start', null, $tm_format, true); |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + |
|
| 688 | + /** |
|
| 689 | + * get end time |
|
| 690 | + * |
|
| 691 | + * @param string $tm_format string representation of time format defaults to 'g:i a' |
|
| 692 | + * @return mixed string on success, FALSE on fail |
|
| 693 | + * @throws ReflectionException |
|
| 694 | + * @throws InvalidArgumentException |
|
| 695 | + * @throws InvalidInterfaceException |
|
| 696 | + * @throws InvalidDataTypeException |
|
| 697 | + * @throws EE_Error |
|
| 698 | + */ |
|
| 699 | + public function end_time($tm_format = '') |
|
| 700 | + { |
|
| 701 | + return $this->_show_datetime('T', 'end', null, $tm_format); |
|
| 702 | + } |
|
| 703 | + |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * @param string $tm_format |
|
| 707 | + * @throws ReflectionException |
|
| 708 | + * @throws InvalidArgumentException |
|
| 709 | + * @throws InvalidInterfaceException |
|
| 710 | + * @throws InvalidDataTypeException |
|
| 711 | + * @throws EE_Error |
|
| 712 | + */ |
|
| 713 | + public function e_end_time($tm_format = '') |
|
| 714 | + { |
|
| 715 | + $this->_show_datetime('T', 'end', null, $tm_format, true); |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + |
|
| 719 | + /** |
|
| 720 | + * get time_range |
|
| 721 | + * |
|
| 722 | + * @access public |
|
| 723 | + * @param string $tm_format string representation of time format defaults to 'g:i a' |
|
| 724 | + * @param string $conjunction conjunction junction what's your function ? |
|
| 725 | + * this string joins the start date with the end date ie: Jan 01 "to" Dec 31 |
|
| 726 | + * @return mixed string on success, FALSE on fail |
|
| 727 | + * @throws ReflectionException |
|
| 728 | + * @throws InvalidArgumentException |
|
| 729 | + * @throws InvalidInterfaceException |
|
| 730 | + * @throws InvalidDataTypeException |
|
| 731 | + * @throws EE_Error |
|
| 732 | + */ |
|
| 733 | + public function time_range($tm_format = '', $conjunction = ' - ') |
|
| 734 | + { |
|
| 735 | + $tm_format = ! empty($tm_format) ? $tm_format : $this->_tm_frmt; |
|
| 736 | + $start = str_replace( |
|
| 737 | + ' ', |
|
| 738 | + ' ', |
|
| 739 | + $this->get_i18n_datetime('DTT_EVT_start', $tm_format) |
|
| 740 | + ); |
|
| 741 | + $end = str_replace( |
|
| 742 | + ' ', |
|
| 743 | + ' ', |
|
| 744 | + $this->get_i18n_datetime('DTT_EVT_end', $tm_format) |
|
| 745 | + ); |
|
| 746 | + return $start !== $end ? $start . $conjunction . $end : $start; |
|
| 747 | + } |
|
| 748 | + |
|
| 749 | + |
|
| 750 | + /** |
|
| 751 | + * @param string $tm_format |
|
| 752 | + * @param string $conjunction |
|
| 753 | + * @throws ReflectionException |
|
| 754 | + * @throws InvalidArgumentException |
|
| 755 | + * @throws InvalidInterfaceException |
|
| 756 | + * @throws InvalidDataTypeException |
|
| 757 | + * @throws EE_Error |
|
| 758 | + */ |
|
| 759 | + public function e_time_range($tm_format = '', $conjunction = ' - ') |
|
| 760 | + { |
|
| 761 | + echo $this->time_range($tm_format, $conjunction); |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + |
|
| 765 | + /** |
|
| 766 | + * This returns a range representation of the date and times. |
|
| 767 | + * Output is dependent on the difference (or similarity) between DTT_EVT_start and DTT_EVT_end. |
|
| 768 | + * Also, the return value is localized. |
|
| 769 | + * |
|
| 770 | + * @param string $dt_format |
|
| 771 | + * @param string $tm_format |
|
| 772 | + * @param string $conjunction used between two different dates or times. |
|
| 773 | + * ex: Dec 1{$conjunction}}Dec 6, or 2pm{$conjunction}3pm |
|
| 774 | + * @param string $separator used between the date and time formats. |
|
| 775 | + * ex: Dec 1, 2016{$separator}2pm |
|
| 776 | + * @return string |
|
| 777 | + * @throws ReflectionException |
|
| 778 | + * @throws InvalidArgumentException |
|
| 779 | + * @throws InvalidInterfaceException |
|
| 780 | + * @throws InvalidDataTypeException |
|
| 781 | + * @throws EE_Error |
|
| 782 | + */ |
|
| 783 | + public function date_and_time_range( |
|
| 784 | + $dt_format = '', |
|
| 785 | + $tm_format = '', |
|
| 786 | + $conjunction = ' - ', |
|
| 787 | + $separator = ' ' |
|
| 788 | + ) { |
|
| 789 | + $dt_format = ! empty($dt_format) ? $dt_format : $this->_dt_frmt; |
|
| 790 | + $tm_format = ! empty($tm_format) ? $tm_format : $this->_tm_frmt; |
|
| 791 | + $full_format = $dt_format . $separator . $tm_format; |
|
| 792 | + // the range output depends on various conditions |
|
| 793 | + switch (true) { |
|
| 794 | + // start date timestamp and end date timestamp are the same. |
|
| 795 | + case ($this->get_raw('DTT_EVT_start') === $this->get_raw('DTT_EVT_end')): |
|
| 796 | + $output = $this->get_i18n_datetime('DTT_EVT_start', $full_format); |
|
| 797 | + break; |
|
| 798 | + // start and end date are the same but times are different |
|
| 799 | + case ($this->start_date() === $this->end_date()): |
|
| 800 | + $output = $this->get_i18n_datetime('DTT_EVT_start', $full_format) |
|
| 801 | + . $conjunction |
|
| 802 | + . $this->get_i18n_datetime('DTT_EVT_end', $tm_format); |
|
| 803 | + break; |
|
| 804 | + // all other conditions |
|
| 805 | + default: |
|
| 806 | + $output = $this->get_i18n_datetime('DTT_EVT_start', $full_format) |
|
| 807 | + . $conjunction |
|
| 808 | + . $this->get_i18n_datetime('DTT_EVT_end', $full_format); |
|
| 809 | + break; |
|
| 810 | + } |
|
| 811 | + return $output; |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + |
|
| 815 | + /** |
|
| 816 | + * This echos the results of date and time range. |
|
| 817 | + * |
|
| 818 | + * @see date_and_time_range() for more details on purpose. |
|
| 819 | + * @param string $dt_format |
|
| 820 | + * @param string $tm_format |
|
| 821 | + * @param string $conjunction |
|
| 822 | + * @return void |
|
| 823 | + * @throws ReflectionException |
|
| 824 | + * @throws InvalidArgumentException |
|
| 825 | + * @throws InvalidInterfaceException |
|
| 826 | + * @throws InvalidDataTypeException |
|
| 827 | + * @throws EE_Error |
|
| 828 | + */ |
|
| 829 | + public function e_date_and_time_range($dt_format = '', $tm_format = '', $conjunction = ' - ') |
|
| 830 | + { |
|
| 831 | + echo $this->date_and_time_range($dt_format, $tm_format, $conjunction); |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + |
|
| 835 | + /** |
|
| 836 | + * get start date and start time |
|
| 837 | + * |
|
| 838 | + * @param string $dt_format - string representation of date format defaults to 'F j, Y' |
|
| 839 | + * @param string $tm_format - string representation of time format defaults to 'g:i a' |
|
| 840 | + * @return mixed string on success, FALSE on fail |
|
| 841 | + * @throws ReflectionException |
|
| 842 | + * @throws InvalidArgumentException |
|
| 843 | + * @throws InvalidInterfaceException |
|
| 844 | + * @throws InvalidDataTypeException |
|
| 845 | + * @throws EE_Error |
|
| 846 | + */ |
|
| 847 | + public function start_date_and_time($dt_format = '', $tm_format = '') |
|
| 848 | + { |
|
| 849 | + return $this->_show_datetime('', 'start', $dt_format, $tm_format); |
|
| 850 | + } |
|
| 851 | + |
|
| 852 | + |
|
| 853 | + /** |
|
| 854 | + * @param string $dt_frmt |
|
| 855 | + * @param string $tm_format |
|
| 856 | + * @throws ReflectionException |
|
| 857 | + * @throws InvalidArgumentException |
|
| 858 | + * @throws InvalidInterfaceException |
|
| 859 | + * @throws InvalidDataTypeException |
|
| 860 | + * @throws EE_Error |
|
| 861 | + */ |
|
| 862 | + public function e_start_date_and_time($dt_frmt = '', $tm_format = '') |
|
| 863 | + { |
|
| 864 | + $this->_show_datetime('', 'start', $dt_frmt, $tm_format, true); |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + |
|
| 868 | + /** |
|
| 869 | + * Shows the length of the event (start to end time). |
|
| 870 | + * Can be shown in 'seconds','minutes','hours', or 'days'. |
|
| 871 | + * By default, rounds up. (So if you use 'days', and then event |
|
| 872 | + * only occurs for 1 hour, it will return 1 day). |
|
| 873 | + * |
|
| 874 | + * @param string $units 'seconds','minutes','hours','days' |
|
| 875 | + * @param bool $round_up |
|
| 876 | + * @return float|int|mixed |
|
| 877 | + * @throws ReflectionException |
|
| 878 | + * @throws InvalidArgumentException |
|
| 879 | + * @throws InvalidInterfaceException |
|
| 880 | + * @throws InvalidDataTypeException |
|
| 881 | + * @throws EE_Error |
|
| 882 | + */ |
|
| 883 | + public function length($units = 'seconds', $round_up = false) |
|
| 884 | + { |
|
| 885 | + $start = $this->get_raw('DTT_EVT_start'); |
|
| 886 | + $end = $this->get_raw('DTT_EVT_end'); |
|
| 887 | + $length_in_units = $end - $start; |
|
| 888 | + switch ($units) { |
|
| 889 | + // NOTE: We purposefully don't use "break;" in order to chain the divisions |
|
| 890 | + /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 891 | + // phpcs:disable PSR2.ControlStructures.SwitchDeclaration.TerminatingComment |
|
| 892 | + case 'days': |
|
| 893 | + $length_in_units /= 24; |
|
| 894 | + /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 895 | + case 'hours': |
|
| 896 | + // fall through is intentional |
|
| 897 | + $length_in_units /= 60; |
|
| 898 | + /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 899 | + case 'minutes': |
|
| 900 | + // fall through is intentional |
|
| 901 | + $length_in_units /= 60; |
|
| 902 | + case 'seconds': |
|
| 903 | + default: |
|
| 904 | + $length_in_units = ceil($length_in_units); |
|
| 905 | + } |
|
| 906 | + // phpcs:enable |
|
| 907 | + if ($round_up) { |
|
| 908 | + $length_in_units = max($length_in_units, 1); |
|
| 909 | + } |
|
| 910 | + return $length_in_units; |
|
| 911 | + } |
|
| 912 | + |
|
| 913 | + |
|
| 914 | + /** |
|
| 915 | + * get end date and time |
|
| 916 | + * |
|
| 917 | + * @param string $dt_frmt - string representation of date format defaults to 'F j, Y' |
|
| 918 | + * @param string $tm_format - string representation of time format defaults to 'g:i a' |
|
| 919 | + * @return mixed string on success, FALSE on fail |
|
| 920 | + * @throws ReflectionException |
|
| 921 | + * @throws InvalidArgumentException |
|
| 922 | + * @throws InvalidInterfaceException |
|
| 923 | + * @throws InvalidDataTypeException |
|
| 924 | + * @throws EE_Error |
|
| 925 | + */ |
|
| 926 | + public function end_date_and_time($dt_frmt = '', $tm_format = '') |
|
| 927 | + { |
|
| 928 | + return $this->_show_datetime('', 'end', $dt_frmt, $tm_format); |
|
| 929 | + } |
|
| 930 | + |
|
| 931 | + |
|
| 932 | + /** |
|
| 933 | + * @param string $dt_frmt |
|
| 934 | + * @param string $tm_format |
|
| 935 | + * @throws ReflectionException |
|
| 936 | + * @throws InvalidArgumentException |
|
| 937 | + * @throws InvalidInterfaceException |
|
| 938 | + * @throws InvalidDataTypeException |
|
| 939 | + * @throws EE_Error |
|
| 940 | + */ |
|
| 941 | + public function e_end_date_and_time($dt_frmt = '', $tm_format = '') |
|
| 942 | + { |
|
| 943 | + $this->_show_datetime('', 'end', $dt_frmt, $tm_format, true); |
|
| 944 | + } |
|
| 945 | + |
|
| 946 | + |
|
| 947 | + /** |
|
| 948 | + * get start timestamp |
|
| 949 | + * |
|
| 950 | + * @return int |
|
| 951 | + * @throws ReflectionException |
|
| 952 | + * @throws InvalidArgumentException |
|
| 953 | + * @throws InvalidInterfaceException |
|
| 954 | + * @throws InvalidDataTypeException |
|
| 955 | + * @throws EE_Error |
|
| 956 | + */ |
|
| 957 | + public function start() |
|
| 958 | + { |
|
| 959 | + return $this->get_raw('DTT_EVT_start'); |
|
| 960 | + } |
|
| 961 | + |
|
| 962 | + |
|
| 963 | + /** |
|
| 964 | + * get end timestamp |
|
| 965 | + * |
|
| 966 | + * @return int |
|
| 967 | + * @throws ReflectionException |
|
| 968 | + * @throws InvalidArgumentException |
|
| 969 | + * @throws InvalidInterfaceException |
|
| 970 | + * @throws InvalidDataTypeException |
|
| 971 | + * @throws EE_Error |
|
| 972 | + */ |
|
| 973 | + public function end() |
|
| 974 | + { |
|
| 975 | + return $this->get_raw('DTT_EVT_end'); |
|
| 976 | + } |
|
| 977 | + |
|
| 978 | + |
|
| 979 | + /** |
|
| 980 | + * get the registration limit for this datetime slot |
|
| 981 | + * |
|
| 982 | + * @return mixed int on success, FALSE on fail |
|
| 983 | + * @throws ReflectionException |
|
| 984 | + * @throws InvalidArgumentException |
|
| 985 | + * @throws InvalidInterfaceException |
|
| 986 | + * @throws InvalidDataTypeException |
|
| 987 | + * @throws EE_Error |
|
| 988 | + */ |
|
| 989 | + public function reg_limit() |
|
| 990 | + { |
|
| 991 | + return $this->get_raw('DTT_reg_limit'); |
|
| 992 | + } |
|
| 993 | + |
|
| 994 | + |
|
| 995 | + /** |
|
| 996 | + * have the tickets sold for this datetime, met or exceed the registration limit ? |
|
| 997 | + * |
|
| 998 | + * @return boolean |
|
| 999 | + * @throws ReflectionException |
|
| 1000 | + * @throws InvalidArgumentException |
|
| 1001 | + * @throws InvalidInterfaceException |
|
| 1002 | + * @throws InvalidDataTypeException |
|
| 1003 | + * @throws EE_Error |
|
| 1004 | + */ |
|
| 1005 | + public function sold_out() |
|
| 1006 | + { |
|
| 1007 | + return $this->reg_limit() > 0 && $this->sold() >= $this->reg_limit(); |
|
| 1008 | + } |
|
| 1009 | + |
|
| 1010 | + |
|
| 1011 | + /** |
|
| 1012 | + * return the total number of spaces remaining at this venue. |
|
| 1013 | + * This only takes the venue's capacity into account, NOT the tickets available for sale |
|
| 1014 | + * |
|
| 1015 | + * @param bool $consider_tickets Whether to consider tickets remaining when determining if there are any spaces left |
|
| 1016 | + * Because if all tickets attached to this datetime have no spaces left, |
|
| 1017 | + * then this datetime IS effectively sold out. |
|
| 1018 | + * However, there are cases where we just want to know the spaces |
|
| 1019 | + * remaining for this particular datetime, hence the flag. |
|
| 1020 | + * @return int |
|
| 1021 | + * @throws ReflectionException |
|
| 1022 | + * @throws InvalidArgumentException |
|
| 1023 | + * @throws InvalidInterfaceException |
|
| 1024 | + * @throws InvalidDataTypeException |
|
| 1025 | + * @throws EE_Error |
|
| 1026 | + */ |
|
| 1027 | + public function spaces_remaining($consider_tickets = false) |
|
| 1028 | + { |
|
| 1029 | + // tickets remaining available for purchase |
|
| 1030 | + // no need for special checks for infinite, because if DTT_reg_limit == EE_INF, then EE_INF - x = EE_INF |
|
| 1031 | + $dtt_remaining = $this->reg_limit() - $this->sold_and_reserved(); |
|
| 1032 | + if (! $consider_tickets) { |
|
| 1033 | + return $dtt_remaining; |
|
| 1034 | + } |
|
| 1035 | + $tickets_remaining = $this->tickets_remaining(); |
|
| 1036 | + return min($dtt_remaining, $tickets_remaining); |
|
| 1037 | + } |
|
| 1038 | + |
|
| 1039 | + |
|
| 1040 | + /** |
|
| 1041 | + * Counts the total tickets available |
|
| 1042 | + * (from all the different types of tickets which are available for this datetime). |
|
| 1043 | + * |
|
| 1044 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1045 | + * @return int |
|
| 1046 | + * @throws ReflectionException |
|
| 1047 | + * @throws InvalidArgumentException |
|
| 1048 | + * @throws InvalidInterfaceException |
|
| 1049 | + * @throws InvalidDataTypeException |
|
| 1050 | + * @throws EE_Error |
|
| 1051 | + */ |
|
| 1052 | + public function tickets_remaining($query_params = array()) |
|
| 1053 | + { |
|
| 1054 | + $sum = 0; |
|
| 1055 | + $tickets = $this->tickets($query_params); |
|
| 1056 | + if (! empty($tickets)) { |
|
| 1057 | + foreach ($tickets as $ticket) { |
|
| 1058 | + if ($ticket instanceof EE_Ticket) { |
|
| 1059 | + // get the actual amount of tickets that can be sold |
|
| 1060 | + $qty = $ticket->qty('saleable'); |
|
| 1061 | + if ($qty === EE_INF) { |
|
| 1062 | + return EE_INF; |
|
| 1063 | + } |
|
| 1064 | + // no negative ticket quantities plz |
|
| 1065 | + if ($qty > 0) { |
|
| 1066 | + $sum += $qty; |
|
| 1067 | + } |
|
| 1068 | + } |
|
| 1069 | + } |
|
| 1070 | + } |
|
| 1071 | + return $sum; |
|
| 1072 | + } |
|
| 1073 | + |
|
| 1074 | + |
|
| 1075 | + /** |
|
| 1076 | + * Gets the count of all the tickets available at this datetime (not ticket types) |
|
| 1077 | + * before any were sold |
|
| 1078 | + * |
|
| 1079 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1080 | + * @return int |
|
| 1081 | + * @throws ReflectionException |
|
| 1082 | + * @throws InvalidArgumentException |
|
| 1083 | + * @throws InvalidInterfaceException |
|
| 1084 | + * @throws InvalidDataTypeException |
|
| 1085 | + * @throws EE_Error |
|
| 1086 | + */ |
|
| 1087 | + public function sum_tickets_initially_available($query_params = array()) |
|
| 1088 | + { |
|
| 1089 | + return $this->sum_related('Ticket', $query_params, 'TKT_qty'); |
|
| 1090 | + } |
|
| 1091 | + |
|
| 1092 | + |
|
| 1093 | + /** |
|
| 1094 | + * Returns the lesser-of-the two: spaces remaining at this datetime, or |
|
| 1095 | + * the total tickets remaining (a sum of the tickets remaining for each ticket type |
|
| 1096 | + * that is available for this datetime). |
|
| 1097 | + * |
|
| 1098 | + * @return int |
|
| 1099 | + * @throws ReflectionException |
|
| 1100 | + * @throws InvalidArgumentException |
|
| 1101 | + * @throws InvalidInterfaceException |
|
| 1102 | + * @throws InvalidDataTypeException |
|
| 1103 | + * @throws EE_Error |
|
| 1104 | + */ |
|
| 1105 | + public function total_tickets_available_at_this_datetime() |
|
| 1106 | + { |
|
| 1107 | + return $this->spaces_remaining(true); |
|
| 1108 | + } |
|
| 1109 | + |
|
| 1110 | + |
|
| 1111 | + /** |
|
| 1112 | + * This simply compares the internal dtt for the given string with NOW |
|
| 1113 | + * and determines if the date is upcoming or not. |
|
| 1114 | + * |
|
| 1115 | + * @access public |
|
| 1116 | + * @return boolean |
|
| 1117 | + * @throws ReflectionException |
|
| 1118 | + * @throws InvalidArgumentException |
|
| 1119 | + * @throws InvalidInterfaceException |
|
| 1120 | + * @throws InvalidDataTypeException |
|
| 1121 | + * @throws EE_Error |
|
| 1122 | + */ |
|
| 1123 | + public function is_upcoming() |
|
| 1124 | + { |
|
| 1125 | + return ($this->get_raw('DTT_EVT_start') > time()); |
|
| 1126 | + } |
|
| 1127 | + |
|
| 1128 | + |
|
| 1129 | + /** |
|
| 1130 | + * This simply compares the internal datetime for the given string with NOW |
|
| 1131 | + * and returns if the date is active (i.e. start and end time) |
|
| 1132 | + * |
|
| 1133 | + * @return boolean |
|
| 1134 | + * @throws ReflectionException |
|
| 1135 | + * @throws InvalidArgumentException |
|
| 1136 | + * @throws InvalidInterfaceException |
|
| 1137 | + * @throws InvalidDataTypeException |
|
| 1138 | + * @throws EE_Error |
|
| 1139 | + */ |
|
| 1140 | + public function is_active() |
|
| 1141 | + { |
|
| 1142 | + return ($this->get_raw('DTT_EVT_start') < time() && $this->get_raw('DTT_EVT_end') > time()); |
|
| 1143 | + } |
|
| 1144 | + |
|
| 1145 | + |
|
| 1146 | + /** |
|
| 1147 | + * This simply compares the internal dtt for the given string with NOW |
|
| 1148 | + * and determines if the date is expired or not. |
|
| 1149 | + * |
|
| 1150 | + * @return boolean |
|
| 1151 | + * @throws ReflectionException |
|
| 1152 | + * @throws InvalidArgumentException |
|
| 1153 | + * @throws InvalidInterfaceException |
|
| 1154 | + * @throws InvalidDataTypeException |
|
| 1155 | + * @throws EE_Error |
|
| 1156 | + */ |
|
| 1157 | + public function is_expired() |
|
| 1158 | + { |
|
| 1159 | + return ($this->get_raw('DTT_EVT_end') < time()); |
|
| 1160 | + } |
|
| 1161 | + |
|
| 1162 | + |
|
| 1163 | + /** |
|
| 1164 | + * This returns the active status for whether an event is active, upcoming, or expired |
|
| 1165 | + * |
|
| 1166 | + * @return int return value will be one of the EE_Datetime status constants. |
|
| 1167 | + * @throws ReflectionException |
|
| 1168 | + * @throws InvalidArgumentException |
|
| 1169 | + * @throws InvalidInterfaceException |
|
| 1170 | + * @throws InvalidDataTypeException |
|
| 1171 | + * @throws EE_Error |
|
| 1172 | + */ |
|
| 1173 | + public function get_active_status() |
|
| 1174 | + { |
|
| 1175 | + $total_tickets_for_this_dtt = $this->total_tickets_available_at_this_datetime(); |
|
| 1176 | + if ($total_tickets_for_this_dtt !== false && $total_tickets_for_this_dtt < 1) { |
|
| 1177 | + return EE_Datetime::sold_out; |
|
| 1178 | + } |
|
| 1179 | + if ($this->is_expired()) { |
|
| 1180 | + return EE_Datetime::expired; |
|
| 1181 | + } |
|
| 1182 | + if ($this->is_upcoming()) { |
|
| 1183 | + return EE_Datetime::upcoming; |
|
| 1184 | + } |
|
| 1185 | + if ($this->is_active()) { |
|
| 1186 | + return EE_Datetime::active; |
|
| 1187 | + } |
|
| 1188 | + return null; |
|
| 1189 | + } |
|
| 1190 | + |
|
| 1191 | + |
|
| 1192 | + /** |
|
| 1193 | + * This returns a nice display name for the datetime that is contingent on the span between the dates and times. |
|
| 1194 | + * |
|
| 1195 | + * @param boolean $use_dtt_name if TRUE then we'll use DTT->name() if its not empty. |
|
| 1196 | + * @return string |
|
| 1197 | + * @throws ReflectionException |
|
| 1198 | + * @throws InvalidArgumentException |
|
| 1199 | + * @throws InvalidInterfaceException |
|
| 1200 | + * @throws InvalidDataTypeException |
|
| 1201 | + * @throws EE_Error |
|
| 1202 | + */ |
|
| 1203 | + public function get_dtt_display_name($use_dtt_name = false) |
|
| 1204 | + { |
|
| 1205 | + if ($use_dtt_name) { |
|
| 1206 | + $dtt_name = $this->name(); |
|
| 1207 | + if (! empty($dtt_name)) { |
|
| 1208 | + return $dtt_name; |
|
| 1209 | + } |
|
| 1210 | + } |
|
| 1211 | + // first condition is to see if the months are different |
|
| 1212 | + if (date('m', $this->get_raw('DTT_EVT_start')) !== date('m', $this->get_raw('DTT_EVT_end')) |
|
| 1213 | + ) { |
|
| 1214 | + $display_date = $this->start_date('M j\, Y g:i a') . ' - ' . $this->end_date('M j\, Y g:i a'); |
|
| 1215 | + // next condition is if its the same month but different day |
|
| 1216 | + } else { |
|
| 1217 | + if (date('m', $this->get_raw('DTT_EVT_start')) === date('m', $this->get_raw('DTT_EVT_end')) |
|
| 1218 | + && date('d', $this->get_raw('DTT_EVT_start')) !== date('d', $this->get_raw('DTT_EVT_end')) |
|
| 1219 | + ) { |
|
| 1220 | + $display_date = $this->start_date('M j\, g:i a') . ' - ' . $this->end_date('M j\, g:i a Y'); |
|
| 1221 | + } else { |
|
| 1222 | + $display_date = $this->start_date('F j\, Y') |
|
| 1223 | + . ' @ ' |
|
| 1224 | + . $this->start_date('g:i a') |
|
| 1225 | + . ' - ' |
|
| 1226 | + . $this->end_date('g:i a'); |
|
| 1227 | + } |
|
| 1228 | + } |
|
| 1229 | + return $display_date; |
|
| 1230 | + } |
|
| 1231 | + |
|
| 1232 | + |
|
| 1233 | + /** |
|
| 1234 | + * Gets all the tickets for this datetime |
|
| 1235 | + * |
|
| 1236 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1237 | + * @return EE_Base_Class[]|EE_Ticket[] |
|
| 1238 | + * @throws ReflectionException |
|
| 1239 | + * @throws InvalidArgumentException |
|
| 1240 | + * @throws InvalidInterfaceException |
|
| 1241 | + * @throws InvalidDataTypeException |
|
| 1242 | + * @throws EE_Error |
|
| 1243 | + */ |
|
| 1244 | + public function tickets($query_params = array()) |
|
| 1245 | + { |
|
| 1246 | + return $this->get_many_related('Ticket', $query_params); |
|
| 1247 | + } |
|
| 1248 | + |
|
| 1249 | + |
|
| 1250 | + /** |
|
| 1251 | + * Gets all the ticket types currently available for purchase |
|
| 1252 | + * |
|
| 1253 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1254 | + * @return EE_Ticket[] |
|
| 1255 | + * @throws ReflectionException |
|
| 1256 | + * @throws InvalidArgumentException |
|
| 1257 | + * @throws InvalidInterfaceException |
|
| 1258 | + * @throws InvalidDataTypeException |
|
| 1259 | + * @throws EE_Error |
|
| 1260 | + */ |
|
| 1261 | + public function ticket_types_available_for_purchase($query_params = array()) |
|
| 1262 | + { |
|
| 1263 | + // first check if datetime is valid |
|
| 1264 | + if ($this->sold_out() || ! ($this->is_upcoming() || $this->is_active())) { |
|
| 1265 | + return array(); |
|
| 1266 | + } |
|
| 1267 | + if (empty($query_params)) { |
|
| 1268 | + $query_params = array( |
|
| 1269 | + array( |
|
| 1270 | + 'TKT_start_date' => array('<=', EEM_Ticket::instance()->current_time_for_query('TKT_start_date')), |
|
| 1271 | + 'TKT_end_date' => array('>=', EEM_Ticket::instance()->current_time_for_query('TKT_end_date')), |
|
| 1272 | + 'TKT_deleted' => false, |
|
| 1273 | + ), |
|
| 1274 | + ); |
|
| 1275 | + } |
|
| 1276 | + return $this->tickets($query_params); |
|
| 1277 | + } |
|
| 1278 | + |
|
| 1279 | + |
|
| 1280 | + /** |
|
| 1281 | + * @return EE_Base_Class|EE_Event |
|
| 1282 | + * @throws ReflectionException |
|
| 1283 | + * @throws InvalidArgumentException |
|
| 1284 | + * @throws InvalidInterfaceException |
|
| 1285 | + * @throws InvalidDataTypeException |
|
| 1286 | + * @throws EE_Error |
|
| 1287 | + */ |
|
| 1288 | + public function event() |
|
| 1289 | + { |
|
| 1290 | + return $this->get_first_related('Event'); |
|
| 1291 | + } |
|
| 1292 | + |
|
| 1293 | + |
|
| 1294 | + /** |
|
| 1295 | + * Updates the DTT_sold attribute (and saves) based on the number of registrations for this datetime |
|
| 1296 | + * (via the tickets). |
|
| 1297 | + * |
|
| 1298 | + * @return int |
|
| 1299 | + * @throws ReflectionException |
|
| 1300 | + * @throws InvalidArgumentException |
|
| 1301 | + * @throws InvalidInterfaceException |
|
| 1302 | + * @throws InvalidDataTypeException |
|
| 1303 | + * @throws EE_Error |
|
| 1304 | + */ |
|
| 1305 | + public function update_sold() |
|
| 1306 | + { |
|
| 1307 | + $count_regs_for_this_datetime = EEM_Registration::instance()->count( |
|
| 1308 | + array( |
|
| 1309 | + array( |
|
| 1310 | + 'STS_ID' => EEM_Registration::status_id_approved, |
|
| 1311 | + 'REG_deleted' => 0, |
|
| 1312 | + 'Ticket.Datetime.DTT_ID' => $this->ID(), |
|
| 1313 | + ), |
|
| 1314 | + ) |
|
| 1315 | + ); |
|
| 1316 | + $this->set_sold($count_regs_for_this_datetime); |
|
| 1317 | + $this->save(); |
|
| 1318 | + return $count_regs_for_this_datetime; |
|
| 1319 | + } |
|
| 1320 | + |
|
| 1321 | + |
|
| 1322 | + /******************************************************************* |
|
| 1323 | 1323 | *********************** DEPRECATED METHODS ********************** |
| 1324 | 1324 | *******************************************************************/ |
| 1325 | 1325 | |
| 1326 | 1326 | |
| 1327 | - /** |
|
| 1328 | - * Increments sold by amount passed by $qty, and persists it immediately to the database. |
|
| 1329 | - * |
|
| 1330 | - * @deprecated 4.9.80.p |
|
| 1331 | - * @param int $qty |
|
| 1332 | - * @return boolean |
|
| 1333 | - * @throws ReflectionException |
|
| 1334 | - * @throws InvalidArgumentException |
|
| 1335 | - * @throws InvalidInterfaceException |
|
| 1336 | - * @throws InvalidDataTypeException |
|
| 1337 | - * @throws EE_Error |
|
| 1338 | - */ |
|
| 1339 | - public function increase_sold($qty = 1) |
|
| 1340 | - { |
|
| 1341 | - EE_Error::doing_it_wrong( |
|
| 1342 | - __FUNCTION__, |
|
| 1343 | - esc_html__('Please use EE_Datetime::increaseSold() instead', 'event_espresso'), |
|
| 1344 | - '4.9.80.p', |
|
| 1345 | - '5.0.0.p' |
|
| 1346 | - ); |
|
| 1347 | - return $this->increaseSold($qty); |
|
| 1348 | - } |
|
| 1349 | - |
|
| 1350 | - |
|
| 1351 | - /** |
|
| 1352 | - * Decrements (subtracts) sold amount passed by $qty directly in the DB and on the model object. (Ie, no need |
|
| 1353 | - * to save afterwards.) |
|
| 1354 | - * |
|
| 1355 | - * @deprecated 4.9.80.p |
|
| 1356 | - * @param int $qty |
|
| 1357 | - * @return boolean |
|
| 1358 | - * @throws ReflectionException |
|
| 1359 | - * @throws InvalidArgumentException |
|
| 1360 | - * @throws InvalidInterfaceException |
|
| 1361 | - * @throws InvalidDataTypeException |
|
| 1362 | - * @throws EE_Error |
|
| 1363 | - */ |
|
| 1364 | - public function decrease_sold($qty = 1) |
|
| 1365 | - { |
|
| 1366 | - EE_Error::doing_it_wrong( |
|
| 1367 | - __FUNCTION__, |
|
| 1368 | - esc_html__('Please use EE_Datetime::decreaseSold() instead', 'event_espresso'), |
|
| 1369 | - '4.9.80.p', |
|
| 1370 | - '5.0.0.p' |
|
| 1371 | - ); |
|
| 1372 | - return $this->decreaseSold($qty); |
|
| 1373 | - } |
|
| 1374 | - |
|
| 1375 | - |
|
| 1376 | - /** |
|
| 1377 | - * Increments reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 1378 | - * |
|
| 1379 | - * @deprecated 4.9.80.p |
|
| 1380 | - * @param int $qty |
|
| 1381 | - * @return boolean indicating success |
|
| 1382 | - * @throws ReflectionException |
|
| 1383 | - * @throws InvalidArgumentException |
|
| 1384 | - * @throws InvalidInterfaceException |
|
| 1385 | - * @throws InvalidDataTypeException |
|
| 1386 | - * @throws EE_Error |
|
| 1387 | - */ |
|
| 1388 | - public function increase_reserved($qty = 1) |
|
| 1389 | - { |
|
| 1390 | - EE_Error::doing_it_wrong( |
|
| 1391 | - __FUNCTION__, |
|
| 1392 | - esc_html__('Please use EE_Datetime::increaseReserved() instead', 'event_espresso'), |
|
| 1393 | - '4.9.80.p', |
|
| 1394 | - '5.0.0.p' |
|
| 1395 | - ); |
|
| 1396 | - return $this->increaseReserved($qty); |
|
| 1397 | - } |
|
| 1398 | - |
|
| 1399 | - |
|
| 1400 | - /** |
|
| 1401 | - * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 1402 | - * |
|
| 1403 | - * @deprecated 4.9.80.p |
|
| 1404 | - * @param int $qty |
|
| 1405 | - * @return boolean |
|
| 1406 | - * @throws ReflectionException |
|
| 1407 | - * @throws InvalidArgumentException |
|
| 1408 | - * @throws InvalidInterfaceException |
|
| 1409 | - * @throws InvalidDataTypeException |
|
| 1410 | - * @throws EE_Error |
|
| 1411 | - */ |
|
| 1412 | - public function decrease_reserved($qty = 1) |
|
| 1413 | - { |
|
| 1414 | - EE_Error::doing_it_wrong( |
|
| 1415 | - __FUNCTION__, |
|
| 1416 | - esc_html__('Please use EE_Datetime::decreaseReserved() instead', 'event_espresso'), |
|
| 1417 | - '4.9.80.p', |
|
| 1418 | - '5.0.0.p' |
|
| 1419 | - ); |
|
| 1420 | - return $this->decreaseReserved($qty); |
|
| 1421 | - } |
|
| 1327 | + /** |
|
| 1328 | + * Increments sold by amount passed by $qty, and persists it immediately to the database. |
|
| 1329 | + * |
|
| 1330 | + * @deprecated 4.9.80.p |
|
| 1331 | + * @param int $qty |
|
| 1332 | + * @return boolean |
|
| 1333 | + * @throws ReflectionException |
|
| 1334 | + * @throws InvalidArgumentException |
|
| 1335 | + * @throws InvalidInterfaceException |
|
| 1336 | + * @throws InvalidDataTypeException |
|
| 1337 | + * @throws EE_Error |
|
| 1338 | + */ |
|
| 1339 | + public function increase_sold($qty = 1) |
|
| 1340 | + { |
|
| 1341 | + EE_Error::doing_it_wrong( |
|
| 1342 | + __FUNCTION__, |
|
| 1343 | + esc_html__('Please use EE_Datetime::increaseSold() instead', 'event_espresso'), |
|
| 1344 | + '4.9.80.p', |
|
| 1345 | + '5.0.0.p' |
|
| 1346 | + ); |
|
| 1347 | + return $this->increaseSold($qty); |
|
| 1348 | + } |
|
| 1349 | + |
|
| 1350 | + |
|
| 1351 | + /** |
|
| 1352 | + * Decrements (subtracts) sold amount passed by $qty directly in the DB and on the model object. (Ie, no need |
|
| 1353 | + * to save afterwards.) |
|
| 1354 | + * |
|
| 1355 | + * @deprecated 4.9.80.p |
|
| 1356 | + * @param int $qty |
|
| 1357 | + * @return boolean |
|
| 1358 | + * @throws ReflectionException |
|
| 1359 | + * @throws InvalidArgumentException |
|
| 1360 | + * @throws InvalidInterfaceException |
|
| 1361 | + * @throws InvalidDataTypeException |
|
| 1362 | + * @throws EE_Error |
|
| 1363 | + */ |
|
| 1364 | + public function decrease_sold($qty = 1) |
|
| 1365 | + { |
|
| 1366 | + EE_Error::doing_it_wrong( |
|
| 1367 | + __FUNCTION__, |
|
| 1368 | + esc_html__('Please use EE_Datetime::decreaseSold() instead', 'event_espresso'), |
|
| 1369 | + '4.9.80.p', |
|
| 1370 | + '5.0.0.p' |
|
| 1371 | + ); |
|
| 1372 | + return $this->decreaseSold($qty); |
|
| 1373 | + } |
|
| 1374 | + |
|
| 1375 | + |
|
| 1376 | + /** |
|
| 1377 | + * Increments reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 1378 | + * |
|
| 1379 | + * @deprecated 4.9.80.p |
|
| 1380 | + * @param int $qty |
|
| 1381 | + * @return boolean indicating success |
|
| 1382 | + * @throws ReflectionException |
|
| 1383 | + * @throws InvalidArgumentException |
|
| 1384 | + * @throws InvalidInterfaceException |
|
| 1385 | + * @throws InvalidDataTypeException |
|
| 1386 | + * @throws EE_Error |
|
| 1387 | + */ |
|
| 1388 | + public function increase_reserved($qty = 1) |
|
| 1389 | + { |
|
| 1390 | + EE_Error::doing_it_wrong( |
|
| 1391 | + __FUNCTION__, |
|
| 1392 | + esc_html__('Please use EE_Datetime::increaseReserved() instead', 'event_espresso'), |
|
| 1393 | + '4.9.80.p', |
|
| 1394 | + '5.0.0.p' |
|
| 1395 | + ); |
|
| 1396 | + return $this->increaseReserved($qty); |
|
| 1397 | + } |
|
| 1398 | + |
|
| 1399 | + |
|
| 1400 | + /** |
|
| 1401 | + * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database. |
|
| 1402 | + * |
|
| 1403 | + * @deprecated 4.9.80.p |
|
| 1404 | + * @param int $qty |
|
| 1405 | + * @return boolean |
|
| 1406 | + * @throws ReflectionException |
|
| 1407 | + * @throws InvalidArgumentException |
|
| 1408 | + * @throws InvalidInterfaceException |
|
| 1409 | + * @throws InvalidDataTypeException |
|
| 1410 | + * @throws EE_Error |
|
| 1411 | + */ |
|
| 1412 | + public function decrease_reserved($qty = 1) |
|
| 1413 | + { |
|
| 1414 | + EE_Error::doing_it_wrong( |
|
| 1415 | + __FUNCTION__, |
|
| 1416 | + esc_html__('Please use EE_Datetime::decreaseReserved() instead', 'event_espresso'), |
|
| 1417 | + '4.9.80.p', |
|
| 1418 | + '5.0.0.p' |
|
| 1419 | + ); |
|
| 1420 | + return $this->decreaseReserved($qty); |
|
| 1421 | + } |
|
| 1422 | 1422 | } |
@@ -8,24 +8,24 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | interface CommandHandlerInterface |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * verifies that the supplied command is the correct class for the handler. |
|
| 13 | - * |
|
| 14 | - * !!! IMPORTANT !!! |
|
| 15 | - * Must return $this (ie: the handler itself) |
|
| 16 | - * as the CommandBus utilizes method chaining |
|
| 17 | - * |
|
| 18 | - * @param CommandInterface $command |
|
| 19 | - * @return CommandHandlerInterface |
|
| 20 | - * @since 4.9.80.p |
|
| 21 | - */ |
|
| 22 | - public function verify(CommandInterface $command); |
|
| 11 | + /** |
|
| 12 | + * verifies that the supplied command is the correct class for the handler. |
|
| 13 | + * |
|
| 14 | + * !!! IMPORTANT !!! |
|
| 15 | + * Must return $this (ie: the handler itself) |
|
| 16 | + * as the CommandBus utilizes method chaining |
|
| 17 | + * |
|
| 18 | + * @param CommandInterface $command |
|
| 19 | + * @return CommandHandlerInterface |
|
| 20 | + * @since 4.9.80.p |
|
| 21 | + */ |
|
| 22 | + public function verify(CommandInterface $command); |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Performs the command handler's logic. |
|
| 26 | - * |
|
| 27 | - * @param CommandInterface $command |
|
| 28 | - * @return mixed |
|
| 29 | - */ |
|
| 30 | - public function handle(CommandInterface $command); |
|
| 24 | + /** |
|
| 25 | + * Performs the command handler's logic. |
|
| 26 | + * |
|
| 27 | + * @param CommandInterface $command |
|
| 28 | + * @return mixed |
|
| 29 | + */ |
|
| 30 | + public function handle(CommandInterface $command); |
|
| 31 | 31 | } |