@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | ), |
144 | 144 | ], |
145 | 145 | ]; |
146 | - $this->_model_relations = [ |
|
146 | + $this->_model_relations = [ |
|
147 | 147 | 'Registration' => new EE_Has_Many_Relation(), |
148 | 148 | 'Payment' => new EE_Has_Many_Relation(), |
149 | 149 | 'Status' => new EE_Belongs_To_Relation(), |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | ], |
212 | 212 | OBJECT, |
213 | 213 | [ |
214 | - 'txnDate' => ['DATE(' . $query_interval . ')', '%s'], |
|
214 | + 'txnDate' => ['DATE('.$query_interval.')', '%s'], |
|
215 | 215 | 'revenue' => ['SUM(TransactionTable.TXN_paid)', '%d'], |
216 | 216 | ] |
217 | 217 | ); |
@@ -230,17 +230,17 @@ discard block |
||
230 | 230 | public function get_revenue_per_event_report($period = '-1 month') |
231 | 231 | { |
232 | 232 | global $wpdb; |
233 | - $transaction_table = $wpdb->prefix . 'esp_transaction'; |
|
234 | - $registration_table = $wpdb->prefix . 'esp_registration'; |
|
235 | - $registration_payment_table = $wpdb->prefix . 'esp_registration_payment'; |
|
233 | + $transaction_table = $wpdb->prefix.'esp_transaction'; |
|
234 | + $registration_table = $wpdb->prefix.'esp_registration'; |
|
235 | + $registration_payment_table = $wpdb->prefix.'esp_registration_payment'; |
|
236 | 236 | $event_table = $wpdb->posts; |
237 | - $payment_table = $wpdb->prefix . 'esp_payment'; |
|
237 | + $payment_table = $wpdb->prefix.'esp_payment'; |
|
238 | 238 | $sql_date = date('Y-m-d H:i:s', strtotime($period)); |
239 | 239 | $approved_payment_status = EEM_Payment::status_id_approved; |
240 | 240 | $extra_event_on_join = ''; |
241 | 241 | // exclude events not authored by user if permissions in effect |
242 | - if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
243 | - $extra_event_on_join = ' AND Event.post_author = ' . get_current_user_id(); |
|
242 | + if ( ! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
243 | + $extra_event_on_join = ' AND Event.post_author = '.get_current_user_id(); |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | return $wpdb->get_results( |
@@ -318,7 +318,7 @@ discard block |
||
318 | 318 | public function update_based_on_payments($transaction_obj_or_id, $save_txn = true) |
319 | 319 | { |
320 | 320 | EE_Error::doing_it_wrong( |
321 | - __CLASS__ . '::' . __FUNCTION__, |
|
321 | + __CLASS__.'::'.__FUNCTION__, |
|
322 | 322 | sprintf( |
323 | 323 | esc_html__('This method is deprecated. Please use "%s" instead', 'event_espresso'), |
324 | 324 | 'EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment()' |
@@ -390,7 +390,7 @@ discard block |
||
390 | 390 | $time_to_leave_alone |
391 | 391 | ); |
392 | 392 | // now that we have the ids to delete |
393 | - if (! empty($txn_ids) && is_array($txn_ids)) { |
|
393 | + if ( ! empty($txn_ids) && is_array($txn_ids)) { |
|
394 | 394 | // first, make sure these TXN's are removed the "ee_locked_transactions" array |
395 | 395 | EEM_Transaction::unset_locked_transactions($txn_ids); |
396 | 396 | |
@@ -403,7 +403,7 @@ discard block |
||
403 | 403 | // let's get deleting... |
404 | 404 | // We got the ids from the original query to get them FROM |
405 | 405 | // the db (which is sanitized) so no need to prepare them again. |
406 | - $query = $wpdb->prepare("DELETE FROM " . $this->table() . " WHERE TXN_ID IN ( $format )", $txn_ids); |
|
406 | + $query = $wpdb->prepare("DELETE FROM ".$this->table()." WHERE TXN_ID IN ( $format )", $txn_ids); |
|
407 | 407 | $deleted = $wpdb->query($query); |
408 | 408 | } |
409 | 409 | if ($deleted) { |
@@ -427,8 +427,8 @@ discard block |
||
427 | 427 | $locked_transactions = get_option('ee_locked_transactions', []); |
428 | 428 | $update = false; |
429 | 429 | foreach ($transaction_IDs as $TXN_ID) { |
430 | - if (isset($locked_transactions[ $TXN_ID ])) { |
|
431 | - unset($locked_transactions[ $TXN_ID ]); |
|
430 | + if (isset($locked_transactions[$TXN_ID])) { |
|
431 | + unset($locked_transactions[$TXN_ID]); |
|
432 | 432 | $update = true; |
433 | 433 | } |
434 | 434 | } |
@@ -19,231 +19,231 @@ discard block |
||
19 | 19 | */ |
20 | 20 | class EEM_Transaction extends EEM_Base |
21 | 21 | { |
22 | - // private instance of the Transaction object |
|
23 | - protected static $_instance; |
|
24 | - |
|
25 | - /** |
|
26 | - * Status ID(STS_ID on esp_status table) to indicate the transaction is complete, |
|
27 | - * but payment is pending. This is the state for transactions where payment is promised |
|
28 | - * from an offline gateway. |
|
29 | - */ |
|
30 | - // const open_status_code = 'TPN'; |
|
31 | - |
|
32 | - /** |
|
33 | - * Status ID(STS_ID on esp_status table) to indicate the transaction failed, |
|
34 | - * either due to a technical reason (server or computer crash during registration), |
|
35 | - * or some other reason that prevent the collection of any useful contact information from any of the registrants |
|
36 | - */ |
|
37 | - const failed_status_code = 'TFL'; |
|
38 | - |
|
39 | - /** |
|
40 | - * Status ID(STS_ID on esp_status table) to indicate the transaction was abandoned, |
|
41 | - * either due to a technical reason (server or computer crash during registration), |
|
42 | - * or due to an abandoned cart after registrant chose not to complete the registration process |
|
43 | - * HOWEVER... |
|
44 | - * an abandoned TXN differs from a failed TXN in that it was able to capture contact information for at least one |
|
45 | - * registrant |
|
46 | - */ |
|
47 | - const abandoned_status_code = 'TAB'; |
|
48 | - |
|
49 | - /** |
|
50 | - * Status ID(STS_ID on esp_status table) to indicate an incomplete transaction, |
|
51 | - * meaning that monies are still owing: TXN_paid < TXN_total |
|
52 | - */ |
|
53 | - const incomplete_status_code = 'TIN'; |
|
54 | - |
|
55 | - /** |
|
56 | - * Status ID (STS_ID on esp_status table) to indicate a complete transaction. |
|
57 | - * meaning that NO monies are owing: TXN_paid == TXN_total |
|
58 | - */ |
|
59 | - const complete_status_code = 'TCM'; |
|
60 | - |
|
61 | - /** |
|
62 | - * Status ID(STS_ID on esp_status table) to indicate the transaction is overpaid. |
|
63 | - * This is the same as complete, but site admins actually owe clients the moneys! TXN_paid > TXN_total |
|
64 | - */ |
|
65 | - const overpaid_status_code = 'TOP'; |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * private constructor to prevent direct creation |
|
70 | - * |
|
71 | - * @Constructor |
|
72 | - * @access protected |
|
73 | - * |
|
74 | - * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any |
|
75 | - * incoming timezone data that gets saved). Note this just sends the timezone info to the |
|
76 | - * date time model field objects. Default is NULL (and will be assumed using the set |
|
77 | - * timezone in the 'timezone_string' wp option) |
|
78 | - * |
|
79 | - * @throws EE_Error |
|
80 | - */ |
|
81 | - protected function __construct($timezone) |
|
82 | - { |
|
83 | - $this->singular_item = esc_html__('Transaction', 'event_espresso'); |
|
84 | - $this->plural_item = esc_html__('Transactions', 'event_espresso'); |
|
85 | - |
|
86 | - $this->_tables = [ |
|
87 | - 'TransactionTable' => new EE_Primary_Table('esp_transaction', 'TXN_ID'), |
|
88 | - ]; |
|
89 | - $this->_fields = [ |
|
90 | - 'TransactionTable' => [ |
|
91 | - 'TXN_ID' => new EE_Primary_Key_Int_Field('TXN_ID', esc_html__('Transaction ID', 'event_espresso')), |
|
92 | - 'TXN_timestamp' => new EE_Datetime_Field( |
|
93 | - 'TXN_timestamp', |
|
94 | - esc_html__('date when transaction was created', 'event_espresso'), |
|
95 | - false, |
|
96 | - EE_Datetime_Field::now, |
|
97 | - $timezone |
|
98 | - ), |
|
99 | - 'TXN_total' => new EE_Money_Field( |
|
100 | - 'TXN_total', |
|
101 | - esc_html__('Total value of Transaction', 'event_espresso'), |
|
102 | - false, |
|
103 | - 0 |
|
104 | - ), |
|
105 | - 'TXN_paid' => new EE_Money_Field( |
|
106 | - 'TXN_paid', |
|
107 | - esc_html__('Amount paid towards transaction to date', 'event_espresso'), |
|
108 | - false, |
|
109 | - 0 |
|
110 | - ), |
|
111 | - 'STS_ID' => new EE_Foreign_Key_String_Field( |
|
112 | - 'STS_ID', |
|
113 | - esc_html__('Status ID', 'event_espresso'), |
|
114 | - false, |
|
115 | - EEM_Transaction::failed_status_code, |
|
116 | - 'Status' |
|
117 | - ), |
|
118 | - 'TXN_session_data' => new EE_Serialized_Text_Field( |
|
119 | - 'TXN_session_data', |
|
120 | - esc_html__('Serialized session data', 'event_espresso'), |
|
121 | - true, |
|
122 | - '' |
|
123 | - ), |
|
124 | - 'TXN_hash_salt' => new EE_Plain_Text_Field( |
|
125 | - 'TXN_hash_salt', |
|
126 | - esc_html__('Transaction Hash Salt', 'event_espresso'), |
|
127 | - true, |
|
128 | - '' |
|
129 | - ), |
|
130 | - 'PMD_ID' => new EE_Foreign_Key_Int_Field( |
|
131 | - 'PMD_ID', |
|
132 | - esc_html__("Last Used Payment Method", 'event_espresso'), |
|
133 | - true, |
|
134 | - null, |
|
135 | - 'Payment_Method' |
|
136 | - ), |
|
137 | - 'TXN_reg_steps' => new EE_Serialized_Text_Field( |
|
138 | - 'TXN_reg_steps', |
|
139 | - esc_html__('Registration Steps', 'event_espresso'), |
|
140 | - false, |
|
141 | - [] |
|
142 | - ), |
|
143 | - ], |
|
144 | - ]; |
|
145 | - $this->_model_relations = [ |
|
146 | - 'Registration' => new EE_Has_Many_Relation(), |
|
147 | - 'Payment' => new EE_Has_Many_Relation(), |
|
148 | - 'Status' => new EE_Belongs_To_Relation(), |
|
149 | - 'Line_Item' => new EE_Has_Many_Relation(false), |
|
150 | - // you can delete a transaction without needing to delete its line items |
|
151 | - 'Payment_Method' => new EE_Belongs_To_Relation(), |
|
152 | - 'Message' => new EE_Has_Many_Relation(), |
|
153 | - ]; |
|
154 | - $this->_model_chain_to_wp_user = 'Registration.Event'; |
|
155 | - parent::__construct($timezone); |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * txn_status_array |
|
161 | - * get list of transaction statuses |
|
162 | - * |
|
163 | - * @access public |
|
164 | - * @return array |
|
165 | - */ |
|
166 | - public static function txn_status_array() |
|
167 | - { |
|
168 | - return apply_filters( |
|
169 | - 'FHEE__EEM_Transaction__txn_status_array', |
|
170 | - [ |
|
171 | - EEM_Transaction::overpaid_status_code, |
|
172 | - EEM_Transaction::complete_status_code, |
|
173 | - EEM_Transaction::incomplete_status_code, |
|
174 | - EEM_Transaction::abandoned_status_code, |
|
175 | - EEM_Transaction::failed_status_code, |
|
176 | - ] |
|
177 | - ); |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - /** |
|
182 | - * get the revenue per day for the Transaction Admin page Reports Tab |
|
183 | - * |
|
184 | - * @access public |
|
185 | - * |
|
186 | - * @param string $period |
|
187 | - * |
|
188 | - * @return stdClass[] |
|
189 | - * @throws EE_Error |
|
190 | - * @throws EE_Error |
|
191 | - */ |
|
192 | - public function get_revenue_per_day_report($period = '-1 month') |
|
193 | - { |
|
194 | - $sql_date = $this->convert_datetime_for_query( |
|
195 | - 'TXN_timestamp', |
|
196 | - date('Y-m-d H:i:s', strtotime($period)), |
|
197 | - 'Y-m-d H:i:s', |
|
198 | - 'UTC' |
|
199 | - ); |
|
200 | - |
|
201 | - $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'TXN_timestamp'); |
|
202 | - |
|
203 | - return $this->_get_all_wpdb_results( |
|
204 | - [ |
|
205 | - [ |
|
206 | - 'TXN_timestamp' => ['>=', $sql_date], |
|
207 | - ], |
|
208 | - 'group_by' => 'txnDate', |
|
209 | - 'order_by' => ['TXN_timestamp' => 'ASC'], |
|
210 | - ], |
|
211 | - OBJECT, |
|
212 | - [ |
|
213 | - 'txnDate' => ['DATE(' . $query_interval . ')', '%s'], |
|
214 | - 'revenue' => ['SUM(TransactionTable.TXN_paid)', '%d'], |
|
215 | - ] |
|
216 | - ); |
|
217 | - } |
|
218 | - |
|
219 | - |
|
220 | - /** |
|
221 | - * get the revenue per event for the Transaction Admin page Reports Tab |
|
222 | - * |
|
223 | - * @access public |
|
224 | - * |
|
225 | - * @param string $period |
|
226 | - * |
|
227 | - * @return EE_Transaction[] |
|
228 | - */ |
|
229 | - public function get_revenue_per_event_report($period = '-1 month') |
|
230 | - { |
|
231 | - global $wpdb; |
|
232 | - $transaction_table = $wpdb->prefix . 'esp_transaction'; |
|
233 | - $registration_table = $wpdb->prefix . 'esp_registration'; |
|
234 | - $registration_payment_table = $wpdb->prefix . 'esp_registration_payment'; |
|
235 | - $event_table = $wpdb->posts; |
|
236 | - $payment_table = $wpdb->prefix . 'esp_payment'; |
|
237 | - $sql_date = date('Y-m-d H:i:s', strtotime($period)); |
|
238 | - $approved_payment_status = EEM_Payment::status_id_approved; |
|
239 | - $extra_event_on_join = ''; |
|
240 | - // exclude events not authored by user if permissions in effect |
|
241 | - if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
242 | - $extra_event_on_join = ' AND Event.post_author = ' . get_current_user_id(); |
|
243 | - } |
|
244 | - |
|
245 | - return $wpdb->get_results( |
|
246 | - "SELECT |
|
22 | + // private instance of the Transaction object |
|
23 | + protected static $_instance; |
|
24 | + |
|
25 | + /** |
|
26 | + * Status ID(STS_ID on esp_status table) to indicate the transaction is complete, |
|
27 | + * but payment is pending. This is the state for transactions where payment is promised |
|
28 | + * from an offline gateway. |
|
29 | + */ |
|
30 | + // const open_status_code = 'TPN'; |
|
31 | + |
|
32 | + /** |
|
33 | + * Status ID(STS_ID on esp_status table) to indicate the transaction failed, |
|
34 | + * either due to a technical reason (server or computer crash during registration), |
|
35 | + * or some other reason that prevent the collection of any useful contact information from any of the registrants |
|
36 | + */ |
|
37 | + const failed_status_code = 'TFL'; |
|
38 | + |
|
39 | + /** |
|
40 | + * Status ID(STS_ID on esp_status table) to indicate the transaction was abandoned, |
|
41 | + * either due to a technical reason (server or computer crash during registration), |
|
42 | + * or due to an abandoned cart after registrant chose not to complete the registration process |
|
43 | + * HOWEVER... |
|
44 | + * an abandoned TXN differs from a failed TXN in that it was able to capture contact information for at least one |
|
45 | + * registrant |
|
46 | + */ |
|
47 | + const abandoned_status_code = 'TAB'; |
|
48 | + |
|
49 | + /** |
|
50 | + * Status ID(STS_ID on esp_status table) to indicate an incomplete transaction, |
|
51 | + * meaning that monies are still owing: TXN_paid < TXN_total |
|
52 | + */ |
|
53 | + const incomplete_status_code = 'TIN'; |
|
54 | + |
|
55 | + /** |
|
56 | + * Status ID (STS_ID on esp_status table) to indicate a complete transaction. |
|
57 | + * meaning that NO monies are owing: TXN_paid == TXN_total |
|
58 | + */ |
|
59 | + const complete_status_code = 'TCM'; |
|
60 | + |
|
61 | + /** |
|
62 | + * Status ID(STS_ID on esp_status table) to indicate the transaction is overpaid. |
|
63 | + * This is the same as complete, but site admins actually owe clients the moneys! TXN_paid > TXN_total |
|
64 | + */ |
|
65 | + const overpaid_status_code = 'TOP'; |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * private constructor to prevent direct creation |
|
70 | + * |
|
71 | + * @Constructor |
|
72 | + * @access protected |
|
73 | + * |
|
74 | + * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any |
|
75 | + * incoming timezone data that gets saved). Note this just sends the timezone info to the |
|
76 | + * date time model field objects. Default is NULL (and will be assumed using the set |
|
77 | + * timezone in the 'timezone_string' wp option) |
|
78 | + * |
|
79 | + * @throws EE_Error |
|
80 | + */ |
|
81 | + protected function __construct($timezone) |
|
82 | + { |
|
83 | + $this->singular_item = esc_html__('Transaction', 'event_espresso'); |
|
84 | + $this->plural_item = esc_html__('Transactions', 'event_espresso'); |
|
85 | + |
|
86 | + $this->_tables = [ |
|
87 | + 'TransactionTable' => new EE_Primary_Table('esp_transaction', 'TXN_ID'), |
|
88 | + ]; |
|
89 | + $this->_fields = [ |
|
90 | + 'TransactionTable' => [ |
|
91 | + 'TXN_ID' => new EE_Primary_Key_Int_Field('TXN_ID', esc_html__('Transaction ID', 'event_espresso')), |
|
92 | + 'TXN_timestamp' => new EE_Datetime_Field( |
|
93 | + 'TXN_timestamp', |
|
94 | + esc_html__('date when transaction was created', 'event_espresso'), |
|
95 | + false, |
|
96 | + EE_Datetime_Field::now, |
|
97 | + $timezone |
|
98 | + ), |
|
99 | + 'TXN_total' => new EE_Money_Field( |
|
100 | + 'TXN_total', |
|
101 | + esc_html__('Total value of Transaction', 'event_espresso'), |
|
102 | + false, |
|
103 | + 0 |
|
104 | + ), |
|
105 | + 'TXN_paid' => new EE_Money_Field( |
|
106 | + 'TXN_paid', |
|
107 | + esc_html__('Amount paid towards transaction to date', 'event_espresso'), |
|
108 | + false, |
|
109 | + 0 |
|
110 | + ), |
|
111 | + 'STS_ID' => new EE_Foreign_Key_String_Field( |
|
112 | + 'STS_ID', |
|
113 | + esc_html__('Status ID', 'event_espresso'), |
|
114 | + false, |
|
115 | + EEM_Transaction::failed_status_code, |
|
116 | + 'Status' |
|
117 | + ), |
|
118 | + 'TXN_session_data' => new EE_Serialized_Text_Field( |
|
119 | + 'TXN_session_data', |
|
120 | + esc_html__('Serialized session data', 'event_espresso'), |
|
121 | + true, |
|
122 | + '' |
|
123 | + ), |
|
124 | + 'TXN_hash_salt' => new EE_Plain_Text_Field( |
|
125 | + 'TXN_hash_salt', |
|
126 | + esc_html__('Transaction Hash Salt', 'event_espresso'), |
|
127 | + true, |
|
128 | + '' |
|
129 | + ), |
|
130 | + 'PMD_ID' => new EE_Foreign_Key_Int_Field( |
|
131 | + 'PMD_ID', |
|
132 | + esc_html__("Last Used Payment Method", 'event_espresso'), |
|
133 | + true, |
|
134 | + null, |
|
135 | + 'Payment_Method' |
|
136 | + ), |
|
137 | + 'TXN_reg_steps' => new EE_Serialized_Text_Field( |
|
138 | + 'TXN_reg_steps', |
|
139 | + esc_html__('Registration Steps', 'event_espresso'), |
|
140 | + false, |
|
141 | + [] |
|
142 | + ), |
|
143 | + ], |
|
144 | + ]; |
|
145 | + $this->_model_relations = [ |
|
146 | + 'Registration' => new EE_Has_Many_Relation(), |
|
147 | + 'Payment' => new EE_Has_Many_Relation(), |
|
148 | + 'Status' => new EE_Belongs_To_Relation(), |
|
149 | + 'Line_Item' => new EE_Has_Many_Relation(false), |
|
150 | + // you can delete a transaction without needing to delete its line items |
|
151 | + 'Payment_Method' => new EE_Belongs_To_Relation(), |
|
152 | + 'Message' => new EE_Has_Many_Relation(), |
|
153 | + ]; |
|
154 | + $this->_model_chain_to_wp_user = 'Registration.Event'; |
|
155 | + parent::__construct($timezone); |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * txn_status_array |
|
161 | + * get list of transaction statuses |
|
162 | + * |
|
163 | + * @access public |
|
164 | + * @return array |
|
165 | + */ |
|
166 | + public static function txn_status_array() |
|
167 | + { |
|
168 | + return apply_filters( |
|
169 | + 'FHEE__EEM_Transaction__txn_status_array', |
|
170 | + [ |
|
171 | + EEM_Transaction::overpaid_status_code, |
|
172 | + EEM_Transaction::complete_status_code, |
|
173 | + EEM_Transaction::incomplete_status_code, |
|
174 | + EEM_Transaction::abandoned_status_code, |
|
175 | + EEM_Transaction::failed_status_code, |
|
176 | + ] |
|
177 | + ); |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + /** |
|
182 | + * get the revenue per day for the Transaction Admin page Reports Tab |
|
183 | + * |
|
184 | + * @access public |
|
185 | + * |
|
186 | + * @param string $period |
|
187 | + * |
|
188 | + * @return stdClass[] |
|
189 | + * @throws EE_Error |
|
190 | + * @throws EE_Error |
|
191 | + */ |
|
192 | + public function get_revenue_per_day_report($period = '-1 month') |
|
193 | + { |
|
194 | + $sql_date = $this->convert_datetime_for_query( |
|
195 | + 'TXN_timestamp', |
|
196 | + date('Y-m-d H:i:s', strtotime($period)), |
|
197 | + 'Y-m-d H:i:s', |
|
198 | + 'UTC' |
|
199 | + ); |
|
200 | + |
|
201 | + $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'TXN_timestamp'); |
|
202 | + |
|
203 | + return $this->_get_all_wpdb_results( |
|
204 | + [ |
|
205 | + [ |
|
206 | + 'TXN_timestamp' => ['>=', $sql_date], |
|
207 | + ], |
|
208 | + 'group_by' => 'txnDate', |
|
209 | + 'order_by' => ['TXN_timestamp' => 'ASC'], |
|
210 | + ], |
|
211 | + OBJECT, |
|
212 | + [ |
|
213 | + 'txnDate' => ['DATE(' . $query_interval . ')', '%s'], |
|
214 | + 'revenue' => ['SUM(TransactionTable.TXN_paid)', '%d'], |
|
215 | + ] |
|
216 | + ); |
|
217 | + } |
|
218 | + |
|
219 | + |
|
220 | + /** |
|
221 | + * get the revenue per event for the Transaction Admin page Reports Tab |
|
222 | + * |
|
223 | + * @access public |
|
224 | + * |
|
225 | + * @param string $period |
|
226 | + * |
|
227 | + * @return EE_Transaction[] |
|
228 | + */ |
|
229 | + public function get_revenue_per_event_report($period = '-1 month') |
|
230 | + { |
|
231 | + global $wpdb; |
|
232 | + $transaction_table = $wpdb->prefix . 'esp_transaction'; |
|
233 | + $registration_table = $wpdb->prefix . 'esp_registration'; |
|
234 | + $registration_payment_table = $wpdb->prefix . 'esp_registration_payment'; |
|
235 | + $event_table = $wpdb->posts; |
|
236 | + $payment_table = $wpdb->prefix . 'esp_payment'; |
|
237 | + $sql_date = date('Y-m-d H:i:s', strtotime($period)); |
|
238 | + $approved_payment_status = EEM_Payment::status_id_approved; |
|
239 | + $extra_event_on_join = ''; |
|
240 | + // exclude events not authored by user if permissions in effect |
|
241 | + if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
242 | + $extra_event_on_join = ' AND Event.post_author = ' . get_current_user_id(); |
|
243 | + } |
|
244 | + |
|
245 | + return $wpdb->get_results( |
|
246 | + "SELECT |
|
247 | 247 | Transaction_Event.event_name AS event_name, |
248 | 248 | SUM(Transaction_Event.paid) AS revenue |
249 | 249 | FROM |
@@ -271,236 +271,236 @@ discard block |
||
271 | 271 | $extra_event_on_join |
272 | 272 | ) AS Transaction_Event |
273 | 273 | GROUP BY event_name" |
274 | - ); |
|
275 | - } |
|
276 | - |
|
277 | - |
|
278 | - /** |
|
279 | - * Gets the current transaction given the reg_url_link, or assumes the reg_url_link is in the |
|
280 | - * request global variable. Either way, tries to find the current transaction (through |
|
281 | - * the registration pointed to by reg_url_link), if not returns null |
|
282 | - * |
|
283 | - * @param string $reg_url_link |
|
284 | - * |
|
285 | - * @return EE_Transaction |
|
286 | - * @throws EE_Error |
|
287 | - */ |
|
288 | - public function get_transaction_from_reg_url_link($reg_url_link = '') |
|
289 | - { |
|
290 | - if (empty($reg_url_link)) { |
|
291 | - $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
292 | - $reg_url_link = $request->getRequestParam('e_reg_url_link'); |
|
293 | - } |
|
294 | - return $this->get_one( |
|
295 | - [ |
|
296 | - [ |
|
297 | - 'Registration.REG_url_link' => $reg_url_link, |
|
298 | - ], |
|
299 | - ] |
|
300 | - ); |
|
301 | - } |
|
302 | - |
|
303 | - |
|
304 | - /** |
|
305 | - * Updates the provided EE_Transaction with all the applicable payments |
|
306 | - * (or fetch the EE_Transaction from its ID) |
|
307 | - * |
|
308 | - * @param EE_Transaction|int $transaction_obj_or_id |
|
309 | - * @param boolean $save_txn whether or not to save the transaction during this function call |
|
310 | - * |
|
311 | - * @return array |
|
312 | - * @throws EE_Error |
|
313 | - * @throws ReflectionException |
|
314 | - * @deprecated |
|
315 | - * |
|
316 | - */ |
|
317 | - public function update_based_on_payments($transaction_obj_or_id, $save_txn = true) |
|
318 | - { |
|
319 | - EE_Error::doing_it_wrong( |
|
320 | - __CLASS__ . '::' . __FUNCTION__, |
|
321 | - sprintf( |
|
322 | - esc_html__('This method is deprecated. Please use "%s" instead', 'event_espresso'), |
|
323 | - 'EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment()' |
|
324 | - ), |
|
325 | - '4.6.0' |
|
326 | - ); |
|
327 | - /** @type EE_Transaction_Processor $transaction_processor */ |
|
328 | - $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor'); |
|
329 | - |
|
330 | - return $transaction_processor->update_transaction_and_registrations_after_checkout_or_payment( |
|
331 | - $this->ensure_is_obj($transaction_obj_or_id) |
|
332 | - ); |
|
333 | - } |
|
334 | - |
|
335 | - |
|
336 | - /** |
|
337 | - * Deletes "junk" transactions that were probably added by bots. There might be TONS |
|
338 | - * of these, so we are very careful to NOT select (which the models do even when deleting), |
|
339 | - * and so we only use wpdb directly and only do minimal joins. |
|
340 | - * Transactions are considered "junk" if they're failed for longer than a week. |
|
341 | - * Also, there is an extra check for payments related to the transaction, because if a transaction has a payment on |
|
342 | - * it, it's probably not junk (regardless of what status it has). |
|
343 | - * The downside to this approach is that is addons are listening for object deletions |
|
344 | - * on EEM_Base::delete() they won't be notified of this. However, there is an action that plugins can hook into |
|
345 | - * to catch these types of deletions. |
|
346 | - * |
|
347 | - * @return int |
|
348 | - * @throws EE_Error |
|
349 | - * @throws EE_Error |
|
350 | - * @global WPDB $wpdb |
|
351 | - */ |
|
352 | - public function delete_junk_transactions() |
|
353 | - { |
|
354 | - global $wpdb; |
|
355 | - $deleted = false; |
|
356 | - $time_to_leave_alone = (int) apply_filters( |
|
357 | - 'FHEE__EEM_Transaction__delete_junk_transactions__time_to_leave_alone', |
|
358 | - WEEK_IN_SECONDS |
|
359 | - ); |
|
360 | - |
|
361 | - |
|
362 | - /** |
|
363 | - * This allows code to filter the query arguments used for retrieving the transaction IDs to delete. |
|
364 | - * Useful for plugins that want to exclude transactions matching certain query parameters. |
|
365 | - * The query parameters should be in the format accepted by the EEM_Base model queries. |
|
366 | - */ |
|
367 | - $ids_query = apply_filters( |
|
368 | - 'FHEE__EEM_Transaction__delete_junk_transactions__initial_query_args', |
|
369 | - [ |
|
370 | - 0 => [ |
|
371 | - 'STS_ID' => EEM_Transaction::failed_status_code, |
|
372 | - 'Payment.PAY_ID' => ['IS NULL'], |
|
373 | - 'TXN_timestamp' => ['<', time() - $time_to_leave_alone], |
|
374 | - ], |
|
375 | - 'order_by' => ['TXN_timestamp' => 'ASC'], |
|
376 | - 'limit' => 1000, |
|
377 | - ], |
|
378 | - $time_to_leave_alone |
|
379 | - ); |
|
380 | - |
|
381 | - |
|
382 | - /** |
|
383 | - * This filter is for when code needs to filter the list of transaction ids that represent transactions |
|
384 | - * about to be deleted based on some other criteria that isn't easily done via the query args filter. |
|
385 | - */ |
|
386 | - $txn_ids = apply_filters( |
|
387 | - 'FHEE__EEM_Transaction__delete_junk_transactions__transaction_ids_to_delete', |
|
388 | - EEM_Transaction::instance()->get_col($ids_query, 'TXN_ID'), |
|
389 | - $time_to_leave_alone |
|
390 | - ); |
|
391 | - // now that we have the ids to delete |
|
392 | - if (! empty($txn_ids) && is_array($txn_ids)) { |
|
393 | - // first, make sure these TXN's are removed the "ee_locked_transactions" array |
|
394 | - EEM_Transaction::unset_locked_transactions($txn_ids); |
|
395 | - |
|
396 | - // Create IDs placeholder. |
|
397 | - $placeholders = array_fill(0, count($txn_ids), '%d'); |
|
398 | - |
|
399 | - // Glue it together to use inside $wpdb->prepare. |
|
400 | - $format = implode(', ', $placeholders); |
|
401 | - |
|
402 | - // let's get deleting... |
|
403 | - // We got the ids from the original query to get them FROM |
|
404 | - // the db (which is sanitized) so no need to prepare them again. |
|
405 | - $query = $wpdb->prepare("DELETE FROM " . $this->table() . " WHERE TXN_ID IN ( $format )", $txn_ids); |
|
406 | - $deleted = $wpdb->query($query); |
|
407 | - } |
|
408 | - if ($deleted) { |
|
409 | - /** |
|
410 | - * Allows code to do something after the transactions have been deleted. |
|
411 | - */ |
|
412 | - do_action('AHEE__EEM_Transaction__delete_junk_transactions__successful_deletion', $txn_ids); |
|
413 | - } |
|
414 | - |
|
415 | - return $deleted; |
|
416 | - } |
|
417 | - |
|
418 | - |
|
419 | - /** |
|
420 | - * @param array $transaction_IDs |
|
421 | - * |
|
422 | - * @return bool |
|
423 | - */ |
|
424 | - public static function unset_locked_transactions(array $transaction_IDs) |
|
425 | - { |
|
426 | - $locked_transactions = get_option('ee_locked_transactions', []); |
|
427 | - $update = false; |
|
428 | - foreach ($transaction_IDs as $TXN_ID) { |
|
429 | - if (isset($locked_transactions[ $TXN_ID ])) { |
|
430 | - unset($locked_transactions[ $TXN_ID ]); |
|
431 | - $update = true; |
|
432 | - } |
|
433 | - } |
|
434 | - if ($update) { |
|
435 | - update_option('ee_locked_transactions', $locked_transactions); |
|
436 | - } |
|
437 | - |
|
438 | - return $update; |
|
439 | - } |
|
440 | - |
|
441 | - |
|
442 | - /** |
|
443 | - * returns an array of EE_Transaction objects whose timestamp is greater than |
|
444 | - * the current time minus the session lifespan, which defaults to 60 minutes |
|
445 | - * |
|
446 | - * @return EE_Base_Class[]|EE_Transaction[] |
|
447 | - * @throws EE_Error |
|
448 | - * @throws InvalidArgumentException |
|
449 | - * @throws InvalidDataTypeException |
|
450 | - * @throws InvalidInterfaceException |
|
451 | - */ |
|
452 | - public function get_transactions_in_progress() |
|
453 | - { |
|
454 | - return $this->_get_transactions_in_progress(); |
|
455 | - } |
|
456 | - |
|
457 | - |
|
458 | - /** |
|
459 | - * returns an array of EE_Transaction objects whose timestamp is less than |
|
460 | - * the current time minus the session lifespan, which defaults to 60 minutes |
|
461 | - * |
|
462 | - * @return EE_Base_Class[]|EE_Transaction[] |
|
463 | - * @throws EE_Error |
|
464 | - * @throws InvalidArgumentException |
|
465 | - * @throws InvalidDataTypeException |
|
466 | - * @throws InvalidInterfaceException |
|
467 | - */ |
|
468 | - public function get_transactions_not_in_progress() |
|
469 | - { |
|
470 | - return $this->_get_transactions_in_progress('<='); |
|
471 | - } |
|
472 | - |
|
473 | - |
|
474 | - /** |
|
475 | - * @param string $comparison |
|
476 | - * @return EE_Transaction[] |
|
477 | - * @throws EE_Error |
|
478 | - * @throws InvalidArgumentException |
|
479 | - * @throws InvalidDataTypeException |
|
480 | - * @throws InvalidInterfaceException |
|
481 | - */ |
|
482 | - private function _get_transactions_in_progress($comparison = '>=') |
|
483 | - { |
|
484 | - $comparison = $comparison === '>=' || $comparison === '<=' |
|
485 | - ? $comparison |
|
486 | - : '>='; |
|
487 | - /** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */ |
|
488 | - $session_lifespan = LoaderFactory::getLoader()->getShared( |
|
489 | - 'EventEspresso\core\domain\values\session\SessionLifespan' |
|
490 | - ); |
|
491 | - return $this->get_all( |
|
492 | - [ |
|
493 | - [ |
|
494 | - 'TXN_timestamp' => [ |
|
495 | - $comparison, |
|
496 | - $session_lifespan->expiration(), |
|
497 | - ], |
|
498 | - 'STS_ID' => [ |
|
499 | - '!=', |
|
500 | - EEM_Transaction::complete_status_code, |
|
501 | - ], |
|
502 | - ], |
|
503 | - ] |
|
504 | - ); |
|
505 | - } |
|
274 | + ); |
|
275 | + } |
|
276 | + |
|
277 | + |
|
278 | + /** |
|
279 | + * Gets the current transaction given the reg_url_link, or assumes the reg_url_link is in the |
|
280 | + * request global variable. Either way, tries to find the current transaction (through |
|
281 | + * the registration pointed to by reg_url_link), if not returns null |
|
282 | + * |
|
283 | + * @param string $reg_url_link |
|
284 | + * |
|
285 | + * @return EE_Transaction |
|
286 | + * @throws EE_Error |
|
287 | + */ |
|
288 | + public function get_transaction_from_reg_url_link($reg_url_link = '') |
|
289 | + { |
|
290 | + if (empty($reg_url_link)) { |
|
291 | + $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
292 | + $reg_url_link = $request->getRequestParam('e_reg_url_link'); |
|
293 | + } |
|
294 | + return $this->get_one( |
|
295 | + [ |
|
296 | + [ |
|
297 | + 'Registration.REG_url_link' => $reg_url_link, |
|
298 | + ], |
|
299 | + ] |
|
300 | + ); |
|
301 | + } |
|
302 | + |
|
303 | + |
|
304 | + /** |
|
305 | + * Updates the provided EE_Transaction with all the applicable payments |
|
306 | + * (or fetch the EE_Transaction from its ID) |
|
307 | + * |
|
308 | + * @param EE_Transaction|int $transaction_obj_or_id |
|
309 | + * @param boolean $save_txn whether or not to save the transaction during this function call |
|
310 | + * |
|
311 | + * @return array |
|
312 | + * @throws EE_Error |
|
313 | + * @throws ReflectionException |
|
314 | + * @deprecated |
|
315 | + * |
|
316 | + */ |
|
317 | + public function update_based_on_payments($transaction_obj_or_id, $save_txn = true) |
|
318 | + { |
|
319 | + EE_Error::doing_it_wrong( |
|
320 | + __CLASS__ . '::' . __FUNCTION__, |
|
321 | + sprintf( |
|
322 | + esc_html__('This method is deprecated. Please use "%s" instead', 'event_espresso'), |
|
323 | + 'EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment()' |
|
324 | + ), |
|
325 | + '4.6.0' |
|
326 | + ); |
|
327 | + /** @type EE_Transaction_Processor $transaction_processor */ |
|
328 | + $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor'); |
|
329 | + |
|
330 | + return $transaction_processor->update_transaction_and_registrations_after_checkout_or_payment( |
|
331 | + $this->ensure_is_obj($transaction_obj_or_id) |
|
332 | + ); |
|
333 | + } |
|
334 | + |
|
335 | + |
|
336 | + /** |
|
337 | + * Deletes "junk" transactions that were probably added by bots. There might be TONS |
|
338 | + * of these, so we are very careful to NOT select (which the models do even when deleting), |
|
339 | + * and so we only use wpdb directly and only do minimal joins. |
|
340 | + * Transactions are considered "junk" if they're failed for longer than a week. |
|
341 | + * Also, there is an extra check for payments related to the transaction, because if a transaction has a payment on |
|
342 | + * it, it's probably not junk (regardless of what status it has). |
|
343 | + * The downside to this approach is that is addons are listening for object deletions |
|
344 | + * on EEM_Base::delete() they won't be notified of this. However, there is an action that plugins can hook into |
|
345 | + * to catch these types of deletions. |
|
346 | + * |
|
347 | + * @return int |
|
348 | + * @throws EE_Error |
|
349 | + * @throws EE_Error |
|
350 | + * @global WPDB $wpdb |
|
351 | + */ |
|
352 | + public function delete_junk_transactions() |
|
353 | + { |
|
354 | + global $wpdb; |
|
355 | + $deleted = false; |
|
356 | + $time_to_leave_alone = (int) apply_filters( |
|
357 | + 'FHEE__EEM_Transaction__delete_junk_transactions__time_to_leave_alone', |
|
358 | + WEEK_IN_SECONDS |
|
359 | + ); |
|
360 | + |
|
361 | + |
|
362 | + /** |
|
363 | + * This allows code to filter the query arguments used for retrieving the transaction IDs to delete. |
|
364 | + * Useful for plugins that want to exclude transactions matching certain query parameters. |
|
365 | + * The query parameters should be in the format accepted by the EEM_Base model queries. |
|
366 | + */ |
|
367 | + $ids_query = apply_filters( |
|
368 | + 'FHEE__EEM_Transaction__delete_junk_transactions__initial_query_args', |
|
369 | + [ |
|
370 | + 0 => [ |
|
371 | + 'STS_ID' => EEM_Transaction::failed_status_code, |
|
372 | + 'Payment.PAY_ID' => ['IS NULL'], |
|
373 | + 'TXN_timestamp' => ['<', time() - $time_to_leave_alone], |
|
374 | + ], |
|
375 | + 'order_by' => ['TXN_timestamp' => 'ASC'], |
|
376 | + 'limit' => 1000, |
|
377 | + ], |
|
378 | + $time_to_leave_alone |
|
379 | + ); |
|
380 | + |
|
381 | + |
|
382 | + /** |
|
383 | + * This filter is for when code needs to filter the list of transaction ids that represent transactions |
|
384 | + * about to be deleted based on some other criteria that isn't easily done via the query args filter. |
|
385 | + */ |
|
386 | + $txn_ids = apply_filters( |
|
387 | + 'FHEE__EEM_Transaction__delete_junk_transactions__transaction_ids_to_delete', |
|
388 | + EEM_Transaction::instance()->get_col($ids_query, 'TXN_ID'), |
|
389 | + $time_to_leave_alone |
|
390 | + ); |
|
391 | + // now that we have the ids to delete |
|
392 | + if (! empty($txn_ids) && is_array($txn_ids)) { |
|
393 | + // first, make sure these TXN's are removed the "ee_locked_transactions" array |
|
394 | + EEM_Transaction::unset_locked_transactions($txn_ids); |
|
395 | + |
|
396 | + // Create IDs placeholder. |
|
397 | + $placeholders = array_fill(0, count($txn_ids), '%d'); |
|
398 | + |
|
399 | + // Glue it together to use inside $wpdb->prepare. |
|
400 | + $format = implode(', ', $placeholders); |
|
401 | + |
|
402 | + // let's get deleting... |
|
403 | + // We got the ids from the original query to get them FROM |
|
404 | + // the db (which is sanitized) so no need to prepare them again. |
|
405 | + $query = $wpdb->prepare("DELETE FROM " . $this->table() . " WHERE TXN_ID IN ( $format )", $txn_ids); |
|
406 | + $deleted = $wpdb->query($query); |
|
407 | + } |
|
408 | + if ($deleted) { |
|
409 | + /** |
|
410 | + * Allows code to do something after the transactions have been deleted. |
|
411 | + */ |
|
412 | + do_action('AHEE__EEM_Transaction__delete_junk_transactions__successful_deletion', $txn_ids); |
|
413 | + } |
|
414 | + |
|
415 | + return $deleted; |
|
416 | + } |
|
417 | + |
|
418 | + |
|
419 | + /** |
|
420 | + * @param array $transaction_IDs |
|
421 | + * |
|
422 | + * @return bool |
|
423 | + */ |
|
424 | + public static function unset_locked_transactions(array $transaction_IDs) |
|
425 | + { |
|
426 | + $locked_transactions = get_option('ee_locked_transactions', []); |
|
427 | + $update = false; |
|
428 | + foreach ($transaction_IDs as $TXN_ID) { |
|
429 | + if (isset($locked_transactions[ $TXN_ID ])) { |
|
430 | + unset($locked_transactions[ $TXN_ID ]); |
|
431 | + $update = true; |
|
432 | + } |
|
433 | + } |
|
434 | + if ($update) { |
|
435 | + update_option('ee_locked_transactions', $locked_transactions); |
|
436 | + } |
|
437 | + |
|
438 | + return $update; |
|
439 | + } |
|
440 | + |
|
441 | + |
|
442 | + /** |
|
443 | + * returns an array of EE_Transaction objects whose timestamp is greater than |
|
444 | + * the current time minus the session lifespan, which defaults to 60 minutes |
|
445 | + * |
|
446 | + * @return EE_Base_Class[]|EE_Transaction[] |
|
447 | + * @throws EE_Error |
|
448 | + * @throws InvalidArgumentException |
|
449 | + * @throws InvalidDataTypeException |
|
450 | + * @throws InvalidInterfaceException |
|
451 | + */ |
|
452 | + public function get_transactions_in_progress() |
|
453 | + { |
|
454 | + return $this->_get_transactions_in_progress(); |
|
455 | + } |
|
456 | + |
|
457 | + |
|
458 | + /** |
|
459 | + * returns an array of EE_Transaction objects whose timestamp is less than |
|
460 | + * the current time minus the session lifespan, which defaults to 60 minutes |
|
461 | + * |
|
462 | + * @return EE_Base_Class[]|EE_Transaction[] |
|
463 | + * @throws EE_Error |
|
464 | + * @throws InvalidArgumentException |
|
465 | + * @throws InvalidDataTypeException |
|
466 | + * @throws InvalidInterfaceException |
|
467 | + */ |
|
468 | + public function get_transactions_not_in_progress() |
|
469 | + { |
|
470 | + return $this->_get_transactions_in_progress('<='); |
|
471 | + } |
|
472 | + |
|
473 | + |
|
474 | + /** |
|
475 | + * @param string $comparison |
|
476 | + * @return EE_Transaction[] |
|
477 | + * @throws EE_Error |
|
478 | + * @throws InvalidArgumentException |
|
479 | + * @throws InvalidDataTypeException |
|
480 | + * @throws InvalidInterfaceException |
|
481 | + */ |
|
482 | + private function _get_transactions_in_progress($comparison = '>=') |
|
483 | + { |
|
484 | + $comparison = $comparison === '>=' || $comparison === '<=' |
|
485 | + ? $comparison |
|
486 | + : '>='; |
|
487 | + /** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */ |
|
488 | + $session_lifespan = LoaderFactory::getLoader()->getShared( |
|
489 | + 'EventEspresso\core\domain\values\session\SessionLifespan' |
|
490 | + ); |
|
491 | + return $this->get_all( |
|
492 | + [ |
|
493 | + [ |
|
494 | + 'TXN_timestamp' => [ |
|
495 | + $comparison, |
|
496 | + $session_lifespan->expiration(), |
|
497 | + ], |
|
498 | + 'STS_ID' => [ |
|
499 | + '!=', |
|
500 | + EEM_Transaction::complete_status_code, |
|
501 | + ], |
|
502 | + ], |
|
503 | + ] |
|
504 | + ); |
|
505 | + } |
|
506 | 506 | } |
@@ -23,32 +23,32 @@ |
||
23 | 23 | */ |
24 | 24 | class EEM_Extra_Join extends EEM_Base |
25 | 25 | { |
26 | - // private instance of the Extra Join object |
|
27 | - protected static $_instance = null; |
|
26 | + // private instance of the Extra Join object |
|
27 | + protected static $_instance = null; |
|
28 | 28 | |
29 | - public function __construct($timezone = null) |
|
30 | - { |
|
31 | - $models_this_can_join = array_keys(EE_Registry::instance()->non_abstract_db_models); |
|
32 | - $this->_tables = array( |
|
33 | - 'Extra_Join' => new EE_Primary_Table('esp_extra_join', 'EXJ_ID'), |
|
34 | - ); |
|
35 | - $this->_fields = array( |
|
36 | - 'Extra_Join' => array( |
|
37 | - 'EXJ_ID' => new EE_Primary_Key_Int_Field('EXJ_ID', esc_html__('Extra Join ID', 'event_espresso')), |
|
38 | - 'EXJ_first_model_ID' => new EE_Foreign_Key_String_Field('EXJ_first_model_ID', esc_html__('First Model ID', 'event_espresso'), true, 0, $models_this_can_join), |
|
39 | - 'EXJ_first_model_name' => new EE_Any_Foreign_Model_Name_Field('EXJ_first_model_name', esc_html__('First Model Name', 'event_espresso'), true, '', $models_this_can_join), |
|
40 | - 'EXJ_second_model_ID' => new EE_Foreign_Key_String_Field('EXJ_second_model_ID', esc_html__('Second Model ID', 'event_espresso'), true, 0, $models_this_can_join), |
|
41 | - 'EXJ_second_model_name' => new EE_Any_Foreign_Model_Name_Field('EXJ_second_model_name', esc_html__('Second Model Name', 'event_espresso'), true, '', $models_this_can_join), |
|
29 | + public function __construct($timezone = null) |
|
30 | + { |
|
31 | + $models_this_can_join = array_keys(EE_Registry::instance()->non_abstract_db_models); |
|
32 | + $this->_tables = array( |
|
33 | + 'Extra_Join' => new EE_Primary_Table('esp_extra_join', 'EXJ_ID'), |
|
34 | + ); |
|
35 | + $this->_fields = array( |
|
36 | + 'Extra_Join' => array( |
|
37 | + 'EXJ_ID' => new EE_Primary_Key_Int_Field('EXJ_ID', esc_html__('Extra Join ID', 'event_espresso')), |
|
38 | + 'EXJ_first_model_ID' => new EE_Foreign_Key_String_Field('EXJ_first_model_ID', esc_html__('First Model ID', 'event_espresso'), true, 0, $models_this_can_join), |
|
39 | + 'EXJ_first_model_name' => new EE_Any_Foreign_Model_Name_Field('EXJ_first_model_name', esc_html__('First Model Name', 'event_espresso'), true, '', $models_this_can_join), |
|
40 | + 'EXJ_second_model_ID' => new EE_Foreign_Key_String_Field('EXJ_second_model_ID', esc_html__('Second Model ID', 'event_espresso'), true, 0, $models_this_can_join), |
|
41 | + 'EXJ_second_model_name' => new EE_Any_Foreign_Model_Name_Field('EXJ_second_model_name', esc_html__('Second Model Name', 'event_espresso'), true, '', $models_this_can_join), |
|
42 | 42 | |
43 | - ) |
|
44 | - ); |
|
45 | - // this model is weird in that it has two foreign key columns which can point to any model/table. |
|
46 | - // eg a foreign key to event will be in "EXJ_first_model_ID", provided the other |
|
47 | - // model linked to is alphabetically greater than event (eg venue). |
|
48 | - // but if the model linked to is alphabetically lower (eg attendee), |
|
49 | - // the foreign key to the event will be in "EXJ_second_model_ID" |
|
50 | - // so normal usage of foreign keys is weird. So don't define any |
|
51 | - // relations to other models because they won't work properly with this model |
|
52 | - parent::__construct($timezone); |
|
53 | - } |
|
43 | + ) |
|
44 | + ); |
|
45 | + // this model is weird in that it has two foreign key columns which can point to any model/table. |
|
46 | + // eg a foreign key to event will be in "EXJ_first_model_ID", provided the other |
|
47 | + // model linked to is alphabetically greater than event (eg venue). |
|
48 | + // but if the model linked to is alphabetically lower (eg attendee), |
|
49 | + // the foreign key to the event will be in "EXJ_second_model_ID" |
|
50 | + // so normal usage of foreign keys is weird. So don't define any |
|
51 | + // relations to other models because they won't work properly with this model |
|
52 | + parent::__construct($timezone); |
|
53 | + } |
|
54 | 54 | } |
@@ -21,7 +21,7 @@ |
||
21 | 21 | { |
22 | 22 | |
23 | 23 | $this->singular_item = esc_html__('Registration Payment', 'event_espresso'); |
24 | - $this->plural_item = esc_html__('Registration Payments', 'event_espresso'); |
|
24 | + $this->plural_item = esc_html__('Registration Payments', 'event_espresso'); |
|
25 | 25 | |
26 | 26 | $this->_tables = array( |
27 | 27 | 'Registration_Payment' => new EE_Primary_Table('esp_registration_payment', 'RPY_ID') |
@@ -12,34 +12,34 @@ |
||
12 | 12 | */ |
13 | 13 | class EEM_Registration_Payment extends EEM_Base |
14 | 14 | { |
15 | - // private instance |
|
16 | - protected static $_instance = null; |
|
15 | + // private instance |
|
16 | + protected static $_instance = null; |
|
17 | 17 | |
18 | 18 | |
19 | - protected function __construct($timezone = null) |
|
20 | - { |
|
19 | + protected function __construct($timezone = null) |
|
20 | + { |
|
21 | 21 | |
22 | - $this->singular_item = esc_html__('Registration Payment', 'event_espresso'); |
|
23 | - $this->plural_item = esc_html__('Registration Payments', 'event_espresso'); |
|
22 | + $this->singular_item = esc_html__('Registration Payment', 'event_espresso'); |
|
23 | + $this->plural_item = esc_html__('Registration Payments', 'event_espresso'); |
|
24 | 24 | |
25 | - $this->_tables = array( |
|
26 | - 'Registration_Payment' => new EE_Primary_Table('esp_registration_payment', 'RPY_ID') |
|
27 | - ); |
|
25 | + $this->_tables = array( |
|
26 | + 'Registration_Payment' => new EE_Primary_Table('esp_registration_payment', 'RPY_ID') |
|
27 | + ); |
|
28 | 28 | |
29 | - $this->_fields = array( |
|
30 | - 'Registration_Payment' => array( |
|
31 | - 'RPY_ID' => new EE_Primary_Key_Int_Field('RPY_ID', esc_html__('Registration Payment ID', 'event_espresso')), |
|
32 | - 'REG_ID' => new EE_Foreign_Key_Int_Field('REG_ID', esc_html__('Registration ID', 'event_espresso'), false, 0, 'Registration'), |
|
33 | - 'PAY_ID' => new EE_Foreign_Key_Int_Field('PAY_ID', esc_html__('Payment ID', 'event_espresso'), true, null, 'Payment'), |
|
34 | - 'RPY_amount' => new EE_Money_Field('RPY_amount', esc_html__('Amount attributed to the registration', 'event_espresso'), false, 0), |
|
35 | - ) |
|
36 | - ); |
|
29 | + $this->_fields = array( |
|
30 | + 'Registration_Payment' => array( |
|
31 | + 'RPY_ID' => new EE_Primary_Key_Int_Field('RPY_ID', esc_html__('Registration Payment ID', 'event_espresso')), |
|
32 | + 'REG_ID' => new EE_Foreign_Key_Int_Field('REG_ID', esc_html__('Registration ID', 'event_espresso'), false, 0, 'Registration'), |
|
33 | + 'PAY_ID' => new EE_Foreign_Key_Int_Field('PAY_ID', esc_html__('Payment ID', 'event_espresso'), true, null, 'Payment'), |
|
34 | + 'RPY_amount' => new EE_Money_Field('RPY_amount', esc_html__('Amount attributed to the registration', 'event_espresso'), false, 0), |
|
35 | + ) |
|
36 | + ); |
|
37 | 37 | |
38 | - $this->_model_relations = array( |
|
39 | - 'Registration' => new EE_Belongs_To_Relation(), |
|
40 | - 'Payment' => new EE_Belongs_To_Relation(), |
|
41 | - ); |
|
38 | + $this->_model_relations = array( |
|
39 | + 'Registration' => new EE_Belongs_To_Relation(), |
|
40 | + 'Payment' => new EE_Belongs_To_Relation(), |
|
41 | + ); |
|
42 | 42 | |
43 | - parent::__construct($timezone); |
|
44 | - } |
|
43 | + parent::__construct($timezone); |
|
44 | + } |
|
45 | 45 | } |
@@ -84,63 +84,63 @@ discard block |
||
84 | 84 | } |
85 | 85 | } |
86 | 86 | // set default wp post statuses if child has not already set. |
87 | - if (! isset($this->_fields[ $primary_table_name ]['status'])) { |
|
88 | - $this->_fields[ $primary_table_name ]['status'] = new EE_WP_Post_Status_Field( |
|
87 | + if ( ! isset($this->_fields[$primary_table_name]['status'])) { |
|
88 | + $this->_fields[$primary_table_name]['status'] = new EE_WP_Post_Status_Field( |
|
89 | 89 | 'post_status', |
90 | 90 | esc_html__("Event Status", "event_espresso"), |
91 | 91 | false, |
92 | 92 | 'draft' |
93 | 93 | ); |
94 | 94 | } |
95 | - if (! isset($this->_fields[ $primary_table_name ]['to_ping'])) { |
|
96 | - $this->_fields[ $primary_table_name ]['to_ping'] = new EE_DB_Only_Text_Field( |
|
95 | + if ( ! isset($this->_fields[$primary_table_name]['to_ping'])) { |
|
96 | + $this->_fields[$primary_table_name]['to_ping'] = new EE_DB_Only_Text_Field( |
|
97 | 97 | 'to_ping', |
98 | 98 | esc_html__('To Ping', 'event_espresso'), |
99 | 99 | false, |
100 | 100 | '' |
101 | 101 | ); |
102 | 102 | } |
103 | - if (! isset($this->_fields[ $primary_table_name ]['pinged'])) { |
|
104 | - $this->_fields[ $primary_table_name ]['pinged'] = new EE_DB_Only_Text_Field( |
|
103 | + if ( ! isset($this->_fields[$primary_table_name]['pinged'])) { |
|
104 | + $this->_fields[$primary_table_name]['pinged'] = new EE_DB_Only_Text_Field( |
|
105 | 105 | 'pinged', |
106 | 106 | esc_html__('Pinged', 'event_espresso'), |
107 | 107 | false, |
108 | 108 | '' |
109 | 109 | ); |
110 | 110 | } |
111 | - if (! isset($this->_fields[ $primary_table_name ]['comment_status'])) { |
|
112 | - $this->_fields[ $primary_table_name ]['comment_status'] = new EE_Plain_Text_Field( |
|
111 | + if ( ! isset($this->_fields[$primary_table_name]['comment_status'])) { |
|
112 | + $this->_fields[$primary_table_name]['comment_status'] = new EE_Plain_Text_Field( |
|
113 | 113 | 'comment_status', |
114 | 114 | esc_html__('Comment Status', 'event_espresso'), |
115 | 115 | false, |
116 | 116 | 'open' |
117 | 117 | ); |
118 | 118 | } |
119 | - if (! isset($this->_fields[ $primary_table_name ]['ping_status'])) { |
|
120 | - $this->_fields[ $primary_table_name ]['ping_status'] = new EE_Plain_Text_Field( |
|
119 | + if ( ! isset($this->_fields[$primary_table_name]['ping_status'])) { |
|
120 | + $this->_fields[$primary_table_name]['ping_status'] = new EE_Plain_Text_Field( |
|
121 | 121 | 'ping_status', |
122 | 122 | esc_html__('Ping Status', 'event_espresso'), |
123 | 123 | false, |
124 | 124 | 'open' |
125 | 125 | ); |
126 | 126 | } |
127 | - if (! isset($this->_fields[ $primary_table_name ]['post_content_filtered'])) { |
|
128 | - $this->_fields[ $primary_table_name ]['post_content_filtered'] = new EE_DB_Only_Text_Field( |
|
127 | + if ( ! isset($this->_fields[$primary_table_name]['post_content_filtered'])) { |
|
128 | + $this->_fields[$primary_table_name]['post_content_filtered'] = new EE_DB_Only_Text_Field( |
|
129 | 129 | 'post_content_filtered', |
130 | 130 | esc_html__('Post Content Filtered', 'event_espresso'), |
131 | 131 | false, |
132 | 132 | '' |
133 | 133 | ); |
134 | 134 | } |
135 | - if (! isset($this->_model_relations['Post_Meta'])) { |
|
135 | + if ( ! isset($this->_model_relations['Post_Meta'])) { |
|
136 | 136 | // don't block deletes though because we want to maintain the current behaviour |
137 | 137 | $this->_model_relations['Post_Meta'] = new EE_Has_Many_Relation(false); |
138 | 138 | } |
139 | - if (! $this->_minimum_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
139 | + if ( ! $this->_minimum_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
140 | 140 | // nothing was set during child constructor, so set default |
141 | 141 | $this->_minimum_where_conditions_strategy = new EE_CPT_Minimum_Where_Conditions($this->post_type()); |
142 | 142 | } |
143 | - if (! $this->_default_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
143 | + if ( ! $this->_default_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
144 | 144 | // nothing was set during child constructor, so set default |
145 | 145 | // it's ok for child classes to specify this, but generally this is more DRY |
146 | 146 | $this->_default_where_conditions_strategy = new EE_CPT_Where_Conditions($this->post_type()); |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | protected function _alter_query_params_so_only_trashed_items_included($query_params) |
215 | 215 | { |
216 | 216 | $post_status_field_name = $this->post_status_field_name(); |
217 | - $query_params[0][ $post_status_field_name ] = self::post_status_trashed; |
|
217 | + $query_params[0][$post_status_field_name] = self::post_status_trashed; |
|
218 | 218 | return $query_params; |
219 | 219 | } |
220 | 220 | |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | $all_fields = array_merge($this->_get_fields_for_table($alias), $all_fields); |
283 | 283 | } |
284 | 284 | } |
285 | - if (! $all) { |
|
285 | + if ( ! $all) { |
|
286 | 286 | foreach ($all_fields as $name => $obj) { |
287 | 287 | if ($obj instanceof EE_DB_Only_Field_Base) { |
288 | 288 | continue; |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | $parent_term_taxonomy_id = null |
315 | 315 | ) { |
316 | 316 | // create term |
317 | - require_once(EE_MODELS . 'EEM_Term.model.php'); |
|
317 | + require_once(EE_MODELS.'EEM_Term.model.php'); |
|
318 | 318 | // first, check for a term by the same name or slug |
319 | 319 | $category_slug = sanitize_title($category_name); |
320 | 320 | $term = EEM_Term::instance()->get_one( |
@@ -328,7 +328,7 @@ discard block |
||
328 | 328 | ), |
329 | 329 | ) |
330 | 330 | ); |
331 | - if (! $term) { |
|
331 | + if ( ! $term) { |
|
332 | 332 | $term = EE_Term::new_instance( |
333 | 333 | array( |
334 | 334 | 'name' => $category_name, |
@@ -338,7 +338,7 @@ discard block |
||
338 | 338 | $term->save(); |
339 | 339 | } |
340 | 340 | // make sure there's a term-taxonomy entry too |
341 | - require_once(EE_MODELS . 'EEM_Term_Taxonomy.model.php'); |
|
341 | + require_once(EE_MODELS.'EEM_Term_Taxonomy.model.php'); |
|
342 | 342 | $term_taxonomy = EEM_Term_Taxonomy::instance()->get_one( |
343 | 343 | array( |
344 | 344 | array( |
@@ -348,7 +348,7 @@ discard block |
||
348 | 348 | ) |
349 | 349 | ); |
350 | 350 | /** @var $term_taxonomy EE_Term_Taxonomy */ |
351 | - if (! $term_taxonomy) { |
|
351 | + if ( ! $term_taxonomy) { |
|
352 | 352 | $term_taxonomy = EE_Term_Taxonomy::new_instance( |
353 | 353 | array( |
354 | 354 | 'term_id' => $term->ID(), |
@@ -421,7 +421,7 @@ discard block |
||
421 | 421 | global $wp_post_statuses; |
422 | 422 | $statuses = array(); |
423 | 423 | foreach ($wp_post_statuses as $post_status => $args_object) { |
424 | - $statuses[ $post_status ] = $args_object->label; |
|
424 | + $statuses[$post_status] = $args_object->label; |
|
425 | 425 | } |
426 | 426 | return $statuses; |
427 | 427 | } |
@@ -438,7 +438,7 @@ discard block |
||
438 | 438 | // first the global filter |
439 | 439 | $statuses = apply_filters('FHEE_EEM_CPT_Base__get_status_array', $statuses); |
440 | 440 | // now the class specific filter |
441 | - $statuses = apply_filters('FHEE_EEM_' . get_class($this) . '__get_status_array', $statuses); |
|
441 | + $statuses = apply_filters('FHEE_EEM_'.get_class($this).'__get_status_array', $statuses); |
|
442 | 442 | return $statuses; |
443 | 443 | } |
444 | 444 | |
@@ -452,7 +452,7 @@ discard block |
||
452 | 452 | { |
453 | 453 | $new_stati = array(); |
454 | 454 | foreach ($this->_custom_stati as $status => $props) { |
455 | - $new_stati[ $status ] = $props['label']; |
|
455 | + $new_stati[$status] = $props['label']; |
|
456 | 456 | } |
457 | 457 | return $new_stati; |
458 | 458 | } |
@@ -476,15 +476,15 @@ discard block |
||
476 | 476 | $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
477 | 477 | foreach ($fields_for_that_table as $field_obj) { |
478 | 478 | if ( |
479 | - ! isset($post[ $field_obj->get_table_column() ]) |
|
480 | - && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
479 | + ! isset($post[$field_obj->get_table_column()]) |
|
480 | + && ! isset($post[$field_obj->get_qualified_column()]) |
|
481 | 481 | ) { |
482 | 482 | $has_all_necessary_fields_for_table = false; |
483 | 483 | } |
484 | 484 | } |
485 | 485 | } |
486 | 486 | // if we don't have all the fields we need, then just fetch the proper model from the DB |
487 | - if (! $has_all_necessary_fields_for_table) { |
|
487 | + if ( ! $has_all_necessary_fields_for_table) { |
|
488 | 488 | return $this->get_one_by_ID($post['ID']); |
489 | 489 | } else { |
490 | 490 | return $this->instantiate_class_from_array_or_object($post); |
@@ -508,10 +508,10 @@ discard block |
||
508 | 508 | $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
509 | 509 | foreach ($fields_for_that_table as $field_obj) { |
510 | 510 | if ( |
511 | - ! isset($post[ $field_obj->get_table_column() ]) |
|
512 | - && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
511 | + ! isset($post[$field_obj->get_table_column()]) |
|
512 | + && ! isset($post[$field_obj->get_qualified_column()]) |
|
513 | 513 | ) { |
514 | - $tables_needing_to_be_queried[ $table_obj->get_table_alias() ] = $table_obj; |
|
514 | + $tables_needing_to_be_queried[$table_obj->get_table_alias()] = $table_obj; |
|
515 | 515 | } |
516 | 516 | } |
517 | 517 | } |
@@ -537,7 +537,7 @@ discard block |
||
537 | 537 | ARRAY_A, |
538 | 538 | ) |
539 | 539 | ); |
540 | - if (! empty($missing_data)) { |
|
540 | + if ( ! empty($missing_data)) { |
|
541 | 541 | $post = array_merge($post, $missing_data); |
542 | 542 | } |
543 | 543 | } else { |
@@ -14,565 +14,565 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class EEM_CPT_Base extends EEM_Soft_Delete_Base |
16 | 16 | { |
17 | - const EVENT_CATEGORY_TAXONOMY = 'espresso_event_categories'; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var string post_status_publish - the wp post status for published cpts |
|
21 | - */ |
|
22 | - const post_status_publish = 'publish'; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var string post_status_future - the wp post status for scheduled cpts |
|
26 | - */ |
|
27 | - const post_status_future = 'future'; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var string post_status_draft - the wp post status for draft cpts |
|
31 | - */ |
|
32 | - const post_status_draft = 'draft'; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var string post_status_pending - the wp post status for pending cpts |
|
36 | - */ |
|
37 | - const post_status_pending = 'pending'; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var string post_status_private - the wp post status for private cpts |
|
41 | - */ |
|
42 | - const post_status_private = 'private'; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var string post_status_trashed - the wp post status for trashed cpts |
|
46 | - */ |
|
47 | - const post_status_trashed = 'trash'; |
|
48 | - |
|
49 | - /** |
|
50 | - * This is an array of custom statuses for the given CPT model (modified by children) |
|
51 | - * format: |
|
52 | - * array( |
|
53 | - * 'status_name' => array( |
|
54 | - * 'label' => esc_html__('Status Name', 'event_espresso'), |
|
55 | - * 'public' => TRUE //whether a public status or not. |
|
56 | - * ) |
|
57 | - * ) |
|
58 | - * |
|
59 | - * @var array |
|
60 | - */ |
|
61 | - protected $_custom_stati = array(); |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * Adds a relationship to Term_Taxonomy for each CPT_Base |
|
66 | - * |
|
67 | - * @param string $timezone |
|
68 | - * @throws \EE_Error |
|
69 | - */ |
|
70 | - protected function __construct($timezone = null) |
|
71 | - { |
|
72 | - // adds a relationship to Term_Taxonomy for all these models. For this to work |
|
73 | - // Term_Relationship must have a relation to each model subclassing EE_CPT_Base explicitly |
|
74 | - // eg, in EEM_Term_Relationship, inside the _model_relations array, there must be an entry |
|
75 | - // with key equalling the subclassing model's model name (eg 'Event' or 'Venue'), and the value |
|
76 | - // must also be new EE_HABTM_Relation('Term_Relationship'); |
|
77 | - $this->_model_relations['Term_Taxonomy'] = new EE_HABTM_Relation('Term_Relationship'); |
|
78 | - $primary_table_name = null; |
|
79 | - // add the common _status field to all CPT primary tables. |
|
80 | - foreach ($this->_tables as $alias => $table_obj) { |
|
81 | - if ($table_obj instanceof EE_Primary_Table) { |
|
82 | - $primary_table_name = $alias; |
|
83 | - } |
|
84 | - } |
|
85 | - // set default wp post statuses if child has not already set. |
|
86 | - if (! isset($this->_fields[ $primary_table_name ]['status'])) { |
|
87 | - $this->_fields[ $primary_table_name ]['status'] = new EE_WP_Post_Status_Field( |
|
88 | - 'post_status', |
|
89 | - esc_html__("Event Status", "event_espresso"), |
|
90 | - false, |
|
91 | - 'draft' |
|
92 | - ); |
|
93 | - } |
|
94 | - if (! isset($this->_fields[ $primary_table_name ]['to_ping'])) { |
|
95 | - $this->_fields[ $primary_table_name ]['to_ping'] = new EE_DB_Only_Text_Field( |
|
96 | - 'to_ping', |
|
97 | - esc_html__('To Ping', 'event_espresso'), |
|
98 | - false, |
|
99 | - '' |
|
100 | - ); |
|
101 | - } |
|
102 | - if (! isset($this->_fields[ $primary_table_name ]['pinged'])) { |
|
103 | - $this->_fields[ $primary_table_name ]['pinged'] = new EE_DB_Only_Text_Field( |
|
104 | - 'pinged', |
|
105 | - esc_html__('Pinged', 'event_espresso'), |
|
106 | - false, |
|
107 | - '' |
|
108 | - ); |
|
109 | - } |
|
110 | - if (! isset($this->_fields[ $primary_table_name ]['comment_status'])) { |
|
111 | - $this->_fields[ $primary_table_name ]['comment_status'] = new EE_Plain_Text_Field( |
|
112 | - 'comment_status', |
|
113 | - esc_html__('Comment Status', 'event_espresso'), |
|
114 | - false, |
|
115 | - 'open' |
|
116 | - ); |
|
117 | - } |
|
118 | - if (! isset($this->_fields[ $primary_table_name ]['ping_status'])) { |
|
119 | - $this->_fields[ $primary_table_name ]['ping_status'] = new EE_Plain_Text_Field( |
|
120 | - 'ping_status', |
|
121 | - esc_html__('Ping Status', 'event_espresso'), |
|
122 | - false, |
|
123 | - 'open' |
|
124 | - ); |
|
125 | - } |
|
126 | - if (! isset($this->_fields[ $primary_table_name ]['post_content_filtered'])) { |
|
127 | - $this->_fields[ $primary_table_name ]['post_content_filtered'] = new EE_DB_Only_Text_Field( |
|
128 | - 'post_content_filtered', |
|
129 | - esc_html__('Post Content Filtered', 'event_espresso'), |
|
130 | - false, |
|
131 | - '' |
|
132 | - ); |
|
133 | - } |
|
134 | - if (! isset($this->_model_relations['Post_Meta'])) { |
|
135 | - // don't block deletes though because we want to maintain the current behaviour |
|
136 | - $this->_model_relations['Post_Meta'] = new EE_Has_Many_Relation(false); |
|
137 | - } |
|
138 | - if (! $this->_minimum_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
139 | - // nothing was set during child constructor, so set default |
|
140 | - $this->_minimum_where_conditions_strategy = new EE_CPT_Minimum_Where_Conditions($this->post_type()); |
|
141 | - } |
|
142 | - if (! $this->_default_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
143 | - // nothing was set during child constructor, so set default |
|
144 | - // it's ok for child classes to specify this, but generally this is more DRY |
|
145 | - $this->_default_where_conditions_strategy = new EE_CPT_Where_Conditions($this->post_type()); |
|
146 | - } |
|
147 | - parent::__construct($timezone); |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * @return array |
|
153 | - */ |
|
154 | - public function public_event_stati() |
|
155 | - { |
|
156 | - // @see wp-includes/post.php |
|
157 | - return get_post_stati(array('public' => true)); |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * Searches for field on this model of type 'deleted_flag'. if it is found, |
|
163 | - * returns it's name. BUT That doesn't apply to CPTs. We should instead use post_status_field_name |
|
164 | - * |
|
165 | - * @return string |
|
166 | - * @throws EE_Error |
|
167 | - */ |
|
168 | - public function deleted_field_name() |
|
169 | - { |
|
170 | - throw new EE_Error( |
|
171 | - sprintf( |
|
172 | - esc_html__( |
|
173 | - "EEM_CPT_Base should nto call deleted_field_name! It should instead use post_status_field_name", |
|
174 | - "event_espresso" |
|
175 | - ) |
|
176 | - ) |
|
177 | - ); |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - /** |
|
182 | - * Gets the field's name that sets the post status |
|
183 | - * |
|
184 | - * @return string |
|
185 | - * @throws EE_Error |
|
186 | - */ |
|
187 | - public function post_status_field_name() |
|
188 | - { |
|
189 | - $field = $this->get_a_field_of_type('EE_WP_Post_Status_Field'); |
|
190 | - if ($field) { |
|
191 | - return $field->get_name(); |
|
192 | - } else { |
|
193 | - throw new EE_Error( |
|
194 | - sprintf( |
|
195 | - esc_html__( |
|
196 | - 'We are trying to find the post status flag field on %s, but none was found. Are you sure there is a field of type EE_Trashed_Flag_Field in %s constructor?', |
|
197 | - 'event_espresso' |
|
198 | - ), |
|
199 | - get_class($this), |
|
200 | - get_class($this) |
|
201 | - ) |
|
202 | - ); |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - |
|
207 | - /** |
|
208 | - * Alters the query params so that only trashed/soft-deleted items are considered |
|
209 | - * |
|
210 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
211 | - * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
212 | - */ |
|
213 | - protected function _alter_query_params_so_only_trashed_items_included($query_params) |
|
214 | - { |
|
215 | - $post_status_field_name = $this->post_status_field_name(); |
|
216 | - $query_params[0][ $post_status_field_name ] = self::post_status_trashed; |
|
217 | - return $query_params; |
|
218 | - } |
|
219 | - |
|
220 | - |
|
221 | - /** |
|
222 | - * Alters the query params so each item's deleted status is ignored. |
|
223 | - * |
|
224 | - * @param array $query_params |
|
225 | - * @return array |
|
226 | - */ |
|
227 | - protected function _alter_query_params_so_deleted_and_undeleted_items_included($query_params) |
|
228 | - { |
|
229 | - $query_params['default_where_conditions'] = 'minimum'; |
|
230 | - return $query_params; |
|
231 | - } |
|
232 | - |
|
233 | - |
|
234 | - /** |
|
235 | - * Performs deletes or restores on items. Both soft-deleted and non-soft-deleted items considered. |
|
236 | - * |
|
237 | - * @param boolean $delete true to indicate deletion, false to indicate restoration |
|
238 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
239 | - * @return boolean success |
|
240 | - */ |
|
241 | - public function delete_or_restore($delete = true, $query_params = array()) |
|
242 | - { |
|
243 | - $post_status_field_name = $this->post_status_field_name(); |
|
244 | - $query_params = $this->_alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
245 | - $new_status = $delete ? self::post_status_trashed : 'draft'; |
|
246 | - if ($this->update(array($post_status_field_name => $new_status), $query_params)) { |
|
247 | - return true; |
|
248 | - } else { |
|
249 | - return false; |
|
250 | - } |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * meta_table |
|
256 | - * returns first EE_Secondary_Table table name |
|
257 | - * |
|
258 | - * @access public |
|
259 | - * @return string |
|
260 | - */ |
|
261 | - public function meta_table() |
|
262 | - { |
|
263 | - $meta_table = $this->_get_other_tables(); |
|
264 | - $meta_table = reset($meta_table); |
|
265 | - return $meta_table instanceof EE_Secondary_Table ? $meta_table->get_table_name() : null; |
|
266 | - } |
|
267 | - |
|
268 | - |
|
269 | - /** |
|
270 | - * This simply returns an array of the meta table fields (useful for when we just need to update those fields) |
|
271 | - * |
|
272 | - * @param bool $all triggers whether we include DB_Only fields or JUST non DB_Only fields. Defaults to false (no |
|
273 | - * db only fields) |
|
274 | - * @return array |
|
275 | - */ |
|
276 | - public function get_meta_table_fields($all = false) |
|
277 | - { |
|
278 | - $all_fields = $fields_to_return = array(); |
|
279 | - foreach ($this->_tables as $alias => $table_obj) { |
|
280 | - if ($table_obj instanceof EE_Secondary_Table) { |
|
281 | - $all_fields = array_merge($this->_get_fields_for_table($alias), $all_fields); |
|
282 | - } |
|
283 | - } |
|
284 | - if (! $all) { |
|
285 | - foreach ($all_fields as $name => $obj) { |
|
286 | - if ($obj instanceof EE_DB_Only_Field_Base) { |
|
287 | - continue; |
|
288 | - } |
|
289 | - $fields_to_return[] = $name; |
|
290 | - } |
|
291 | - } else { |
|
292 | - $fields_to_return = array_keys($all_fields); |
|
293 | - } |
|
294 | - return $fields_to_return; |
|
295 | - } |
|
296 | - |
|
297 | - |
|
298 | - /** |
|
299 | - * Adds an event category with the specified name and description to the specified |
|
300 | - * $cpt_model_object. Intelligently adds a term if necessary, and adds a term_taxonomy if necessary, |
|
301 | - * and adds an entry in the term_relationship if necessary. |
|
302 | - * |
|
303 | - * @param EE_CPT_Base $cpt_model_object |
|
304 | - * @param string $category_name (used to derive the term slug too) |
|
305 | - * @param string $category_description |
|
306 | - * @param int $parent_term_taxonomy_id |
|
307 | - * @return EE_Term_Taxonomy |
|
308 | - */ |
|
309 | - public function add_event_category( |
|
310 | - EE_CPT_Base $cpt_model_object, |
|
311 | - $category_name, |
|
312 | - $category_description = '', |
|
313 | - $parent_term_taxonomy_id = null |
|
314 | - ) { |
|
315 | - // create term |
|
316 | - require_once(EE_MODELS . 'EEM_Term.model.php'); |
|
317 | - // first, check for a term by the same name or slug |
|
318 | - $category_slug = sanitize_title($category_name); |
|
319 | - $term = EEM_Term::instance()->get_one( |
|
320 | - array( |
|
321 | - array( |
|
322 | - 'OR' => array( |
|
323 | - 'name' => $category_name, |
|
324 | - 'slug' => $category_slug, |
|
325 | - ), |
|
326 | - 'Term_Taxonomy.taxonomy' => self::EVENT_CATEGORY_TAXONOMY |
|
327 | - ), |
|
328 | - ) |
|
329 | - ); |
|
330 | - if (! $term) { |
|
331 | - $term = EE_Term::new_instance( |
|
332 | - array( |
|
333 | - 'name' => $category_name, |
|
334 | - 'slug' => $category_slug, |
|
335 | - ) |
|
336 | - ); |
|
337 | - $term->save(); |
|
338 | - } |
|
339 | - // make sure there's a term-taxonomy entry too |
|
340 | - require_once(EE_MODELS . 'EEM_Term_Taxonomy.model.php'); |
|
341 | - $term_taxonomy = EEM_Term_Taxonomy::instance()->get_one( |
|
342 | - array( |
|
343 | - array( |
|
344 | - 'term_id' => $term->ID(), |
|
345 | - 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
346 | - ), |
|
347 | - ) |
|
348 | - ); |
|
349 | - /** @var $term_taxonomy EE_Term_Taxonomy */ |
|
350 | - if (! $term_taxonomy) { |
|
351 | - $term_taxonomy = EE_Term_Taxonomy::new_instance( |
|
352 | - array( |
|
353 | - 'term_id' => $term->ID(), |
|
354 | - 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
355 | - 'description' => $category_description, |
|
356 | - 'term_count' => 1, |
|
357 | - 'parent' => $parent_term_taxonomy_id, |
|
358 | - ) |
|
359 | - ); |
|
360 | - $term_taxonomy->save(); |
|
361 | - } else { |
|
362 | - $term_taxonomy->set_count($term_taxonomy->count() + 1); |
|
363 | - $term_taxonomy->save(); |
|
364 | - } |
|
365 | - return $this->add_relationship_to($cpt_model_object, $term_taxonomy, 'Term_Taxonomy'); |
|
366 | - } |
|
367 | - |
|
368 | - |
|
369 | - /** |
|
370 | - * Removed the category specified by name as having a relation to this event. |
|
371 | - * Does not remove the term or term_taxonomy. |
|
372 | - * |
|
373 | - * @param EE_CPT_Base $cpt_model_object_event |
|
374 | - * @param string $category_name name of the event category (term) |
|
375 | - * @return bool |
|
376 | - */ |
|
377 | - public function remove_event_category(EE_CPT_Base $cpt_model_object_event, $category_name) |
|
378 | - { |
|
379 | - // find the term_taxonomy by that name |
|
380 | - $term_taxonomy = $this->get_first_related( |
|
381 | - $cpt_model_object_event, |
|
382 | - 'Term_Taxonomy', |
|
383 | - array(array('Term.name' => $category_name, 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY)) |
|
384 | - ); |
|
385 | - /** @var $term_taxonomy EE_Term_Taxonomy */ |
|
386 | - if ($term_taxonomy) { |
|
387 | - $term_taxonomy->set_count($term_taxonomy->count() - 1); |
|
388 | - $term_taxonomy->save(); |
|
389 | - } |
|
390 | - return $this->remove_relationship_to($cpt_model_object_event, $term_taxonomy, 'Term_Taxonomy'); |
|
391 | - } |
|
392 | - |
|
393 | - |
|
394 | - /** |
|
395 | - * This is a wrapper for the WordPress get_the_post_thumbnail() function that returns the feature image for the |
|
396 | - * given CPT ID. It accepts the same params as what get_the_post_thumbnail() accepts. |
|
397 | - * |
|
398 | - * @link http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail |
|
399 | - * @access public |
|
400 | - * @param int $id the ID for the cpt we want the feature image for |
|
401 | - * @param string|array $size (optional) Image size. Defaults to 'post-thumbnail' but can also be a 2-item array |
|
402 | - * representing width and height in pixels (i.e. array(32,32) ). |
|
403 | - * @param string|array $attr Optional. Query string or array of attributes. |
|
404 | - * @return string HTML image element |
|
405 | - */ |
|
406 | - public function get_feature_image($id, $size = 'thumbnail', $attr = '') |
|
407 | - { |
|
408 | - return get_the_post_thumbnail($id, $size, $attr); |
|
409 | - } |
|
410 | - |
|
411 | - |
|
412 | - /** |
|
413 | - * Just a handy way to get the list of post statuses currently registered with WP. |
|
414 | - * |
|
415 | - * @global array $wp_post_statuses set in wp core for storing all the post stati |
|
416 | - * @return array |
|
417 | - */ |
|
418 | - public function get_post_statuses() |
|
419 | - { |
|
420 | - global $wp_post_statuses; |
|
421 | - $statuses = array(); |
|
422 | - foreach ($wp_post_statuses as $post_status => $args_object) { |
|
423 | - $statuses[ $post_status ] = $args_object->label; |
|
424 | - } |
|
425 | - return $statuses; |
|
426 | - } |
|
427 | - |
|
428 | - |
|
429 | - /** |
|
430 | - * public method that can be used to retrieve the protected status array on the instantiated cpt model |
|
431 | - * |
|
432 | - * @return array array of statuses. |
|
433 | - */ |
|
434 | - public function get_status_array() |
|
435 | - { |
|
436 | - $statuses = $this->get_post_statuses(); |
|
437 | - // first the global filter |
|
438 | - $statuses = apply_filters('FHEE_EEM_CPT_Base__get_status_array', $statuses); |
|
439 | - // now the class specific filter |
|
440 | - $statuses = apply_filters('FHEE_EEM_' . get_class($this) . '__get_status_array', $statuses); |
|
441 | - return $statuses; |
|
442 | - } |
|
443 | - |
|
444 | - |
|
445 | - /** |
|
446 | - * this returns the post statuses that are NOT the default wordpress status |
|
447 | - * |
|
448 | - * @return array |
|
449 | - */ |
|
450 | - public function get_custom_post_statuses() |
|
451 | - { |
|
452 | - $new_stati = array(); |
|
453 | - foreach ($this->_custom_stati as $status => $props) { |
|
454 | - $new_stati[ $status ] = $props['label']; |
|
455 | - } |
|
456 | - return $new_stati; |
|
457 | - } |
|
458 | - |
|
459 | - |
|
460 | - /** |
|
461 | - * Creates a child of EE_CPT_Base given a WP_Post or array of wpdb results which |
|
462 | - * are a row from the posts table. If we're missing any fields required for the model, |
|
463 | - * we just fetch the entire entry from the DB (ie, if you want to use this to save DB queries, |
|
464 | - * make sure you are attaching all the model's fields onto the post) |
|
465 | - * |
|
466 | - * @param WP_Post|array $post |
|
467 | - * @return EE_Base_Class|EE_Soft_Delete_Base_Class |
|
468 | - */ |
|
469 | - public function instantiate_class_from_post_object_orig($post) |
|
470 | - { |
|
471 | - $post = (array) $post; |
|
472 | - $has_all_necessary_fields_for_table = true; |
|
473 | - // check if the post has fields on the meta table already |
|
474 | - foreach ($this->_get_other_tables() as $table_obj) { |
|
475 | - $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
|
476 | - foreach ($fields_for_that_table as $field_obj) { |
|
477 | - if ( |
|
478 | - ! isset($post[ $field_obj->get_table_column() ]) |
|
479 | - && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
480 | - ) { |
|
481 | - $has_all_necessary_fields_for_table = false; |
|
482 | - } |
|
483 | - } |
|
484 | - } |
|
485 | - // if we don't have all the fields we need, then just fetch the proper model from the DB |
|
486 | - if (! $has_all_necessary_fields_for_table) { |
|
487 | - return $this->get_one_by_ID($post['ID']); |
|
488 | - } else { |
|
489 | - return $this->instantiate_class_from_array_or_object($post); |
|
490 | - } |
|
491 | - } |
|
492 | - |
|
493 | - |
|
494 | - /** |
|
495 | - * @param null $post |
|
496 | - * @return EE_Base_Class|EE_Soft_Delete_Base_Class |
|
497 | - */ |
|
498 | - public function instantiate_class_from_post_object($post = null) |
|
499 | - { |
|
500 | - if (empty($post)) { |
|
501 | - global $post; |
|
502 | - } |
|
503 | - $post = (array) $post; |
|
504 | - $tables_needing_to_be_queried = array(); |
|
505 | - // check if the post has fields on the meta table already |
|
506 | - foreach ($this->get_tables() as $table_obj) { |
|
507 | - $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
|
508 | - foreach ($fields_for_that_table as $field_obj) { |
|
509 | - if ( |
|
510 | - ! isset($post[ $field_obj->get_table_column() ]) |
|
511 | - && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
512 | - ) { |
|
513 | - $tables_needing_to_be_queried[ $table_obj->get_table_alias() ] = $table_obj; |
|
514 | - } |
|
515 | - } |
|
516 | - } |
|
517 | - // if we don't have all the fields we need, then just fetch the proper model from the DB |
|
518 | - if ($tables_needing_to_be_queried) { |
|
519 | - if ( |
|
520 | - count($tables_needing_to_be_queried) == 1 |
|
521 | - && reset($tables_needing_to_be_queried) |
|
522 | - instanceof |
|
523 | - EE_Secondary_Table |
|
524 | - ) { |
|
525 | - // so we're only missing data from a secondary table. Well that's not too hard to query for |
|
526 | - $table_to_query = reset($tables_needing_to_be_queried); |
|
527 | - $missing_data = $this->_do_wpdb_query( |
|
528 | - 'get_row', |
|
529 | - array( |
|
530 | - 'SELECT * FROM ' |
|
531 | - . $table_to_query->get_table_name() |
|
532 | - . ' WHERE ' |
|
533 | - . $table_to_query->get_fk_on_table() |
|
534 | - . ' = ' |
|
535 | - . $post['ID'], |
|
536 | - ARRAY_A, |
|
537 | - ) |
|
538 | - ); |
|
539 | - if (! empty($missing_data)) { |
|
540 | - $post = array_merge($post, $missing_data); |
|
541 | - } |
|
542 | - } else { |
|
543 | - return $this->get_one_by_ID($post['ID']); |
|
544 | - } |
|
545 | - } |
|
546 | - return $this->instantiate_class_from_array_or_object($post); |
|
547 | - } |
|
548 | - |
|
549 | - |
|
550 | - /** |
|
551 | - * Gets the post type associated with this |
|
552 | - * |
|
553 | - * @throws EE_Error |
|
554 | - * @return string |
|
555 | - */ |
|
556 | - public function post_type() |
|
557 | - { |
|
558 | - $post_type_field = null; |
|
559 | - foreach ($this->field_settings(true) as $field_obj) { |
|
560 | - if ($field_obj instanceof EE_WP_Post_Type_Field) { |
|
561 | - $post_type_field = $field_obj; |
|
562 | - break; |
|
563 | - } |
|
564 | - } |
|
565 | - if ($post_type_field == null) { |
|
566 | - throw new EE_Error( |
|
567 | - sprintf( |
|
568 | - esc_html__( |
|
569 | - "CPT Model %s should have a field of type EE_WP_Post_Type, but doesnt", |
|
570 | - "event_espresso" |
|
571 | - ), |
|
572 | - get_class($this) |
|
573 | - ) |
|
574 | - ); |
|
575 | - } |
|
576 | - return $post_type_field->get_default_value(); |
|
577 | - } |
|
17 | + const EVENT_CATEGORY_TAXONOMY = 'espresso_event_categories'; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var string post_status_publish - the wp post status for published cpts |
|
21 | + */ |
|
22 | + const post_status_publish = 'publish'; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var string post_status_future - the wp post status for scheduled cpts |
|
26 | + */ |
|
27 | + const post_status_future = 'future'; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var string post_status_draft - the wp post status for draft cpts |
|
31 | + */ |
|
32 | + const post_status_draft = 'draft'; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var string post_status_pending - the wp post status for pending cpts |
|
36 | + */ |
|
37 | + const post_status_pending = 'pending'; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var string post_status_private - the wp post status for private cpts |
|
41 | + */ |
|
42 | + const post_status_private = 'private'; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var string post_status_trashed - the wp post status for trashed cpts |
|
46 | + */ |
|
47 | + const post_status_trashed = 'trash'; |
|
48 | + |
|
49 | + /** |
|
50 | + * This is an array of custom statuses for the given CPT model (modified by children) |
|
51 | + * format: |
|
52 | + * array( |
|
53 | + * 'status_name' => array( |
|
54 | + * 'label' => esc_html__('Status Name', 'event_espresso'), |
|
55 | + * 'public' => TRUE //whether a public status or not. |
|
56 | + * ) |
|
57 | + * ) |
|
58 | + * |
|
59 | + * @var array |
|
60 | + */ |
|
61 | + protected $_custom_stati = array(); |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * Adds a relationship to Term_Taxonomy for each CPT_Base |
|
66 | + * |
|
67 | + * @param string $timezone |
|
68 | + * @throws \EE_Error |
|
69 | + */ |
|
70 | + protected function __construct($timezone = null) |
|
71 | + { |
|
72 | + // adds a relationship to Term_Taxonomy for all these models. For this to work |
|
73 | + // Term_Relationship must have a relation to each model subclassing EE_CPT_Base explicitly |
|
74 | + // eg, in EEM_Term_Relationship, inside the _model_relations array, there must be an entry |
|
75 | + // with key equalling the subclassing model's model name (eg 'Event' or 'Venue'), and the value |
|
76 | + // must also be new EE_HABTM_Relation('Term_Relationship'); |
|
77 | + $this->_model_relations['Term_Taxonomy'] = new EE_HABTM_Relation('Term_Relationship'); |
|
78 | + $primary_table_name = null; |
|
79 | + // add the common _status field to all CPT primary tables. |
|
80 | + foreach ($this->_tables as $alias => $table_obj) { |
|
81 | + if ($table_obj instanceof EE_Primary_Table) { |
|
82 | + $primary_table_name = $alias; |
|
83 | + } |
|
84 | + } |
|
85 | + // set default wp post statuses if child has not already set. |
|
86 | + if (! isset($this->_fields[ $primary_table_name ]['status'])) { |
|
87 | + $this->_fields[ $primary_table_name ]['status'] = new EE_WP_Post_Status_Field( |
|
88 | + 'post_status', |
|
89 | + esc_html__("Event Status", "event_espresso"), |
|
90 | + false, |
|
91 | + 'draft' |
|
92 | + ); |
|
93 | + } |
|
94 | + if (! isset($this->_fields[ $primary_table_name ]['to_ping'])) { |
|
95 | + $this->_fields[ $primary_table_name ]['to_ping'] = new EE_DB_Only_Text_Field( |
|
96 | + 'to_ping', |
|
97 | + esc_html__('To Ping', 'event_espresso'), |
|
98 | + false, |
|
99 | + '' |
|
100 | + ); |
|
101 | + } |
|
102 | + if (! isset($this->_fields[ $primary_table_name ]['pinged'])) { |
|
103 | + $this->_fields[ $primary_table_name ]['pinged'] = new EE_DB_Only_Text_Field( |
|
104 | + 'pinged', |
|
105 | + esc_html__('Pinged', 'event_espresso'), |
|
106 | + false, |
|
107 | + '' |
|
108 | + ); |
|
109 | + } |
|
110 | + if (! isset($this->_fields[ $primary_table_name ]['comment_status'])) { |
|
111 | + $this->_fields[ $primary_table_name ]['comment_status'] = new EE_Plain_Text_Field( |
|
112 | + 'comment_status', |
|
113 | + esc_html__('Comment Status', 'event_espresso'), |
|
114 | + false, |
|
115 | + 'open' |
|
116 | + ); |
|
117 | + } |
|
118 | + if (! isset($this->_fields[ $primary_table_name ]['ping_status'])) { |
|
119 | + $this->_fields[ $primary_table_name ]['ping_status'] = new EE_Plain_Text_Field( |
|
120 | + 'ping_status', |
|
121 | + esc_html__('Ping Status', 'event_espresso'), |
|
122 | + false, |
|
123 | + 'open' |
|
124 | + ); |
|
125 | + } |
|
126 | + if (! isset($this->_fields[ $primary_table_name ]['post_content_filtered'])) { |
|
127 | + $this->_fields[ $primary_table_name ]['post_content_filtered'] = new EE_DB_Only_Text_Field( |
|
128 | + 'post_content_filtered', |
|
129 | + esc_html__('Post Content Filtered', 'event_espresso'), |
|
130 | + false, |
|
131 | + '' |
|
132 | + ); |
|
133 | + } |
|
134 | + if (! isset($this->_model_relations['Post_Meta'])) { |
|
135 | + // don't block deletes though because we want to maintain the current behaviour |
|
136 | + $this->_model_relations['Post_Meta'] = new EE_Has_Many_Relation(false); |
|
137 | + } |
|
138 | + if (! $this->_minimum_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
139 | + // nothing was set during child constructor, so set default |
|
140 | + $this->_minimum_where_conditions_strategy = new EE_CPT_Minimum_Where_Conditions($this->post_type()); |
|
141 | + } |
|
142 | + if (! $this->_default_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
143 | + // nothing was set during child constructor, so set default |
|
144 | + // it's ok for child classes to specify this, but generally this is more DRY |
|
145 | + $this->_default_where_conditions_strategy = new EE_CPT_Where_Conditions($this->post_type()); |
|
146 | + } |
|
147 | + parent::__construct($timezone); |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * @return array |
|
153 | + */ |
|
154 | + public function public_event_stati() |
|
155 | + { |
|
156 | + // @see wp-includes/post.php |
|
157 | + return get_post_stati(array('public' => true)); |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * Searches for field on this model of type 'deleted_flag'. if it is found, |
|
163 | + * returns it's name. BUT That doesn't apply to CPTs. We should instead use post_status_field_name |
|
164 | + * |
|
165 | + * @return string |
|
166 | + * @throws EE_Error |
|
167 | + */ |
|
168 | + public function deleted_field_name() |
|
169 | + { |
|
170 | + throw new EE_Error( |
|
171 | + sprintf( |
|
172 | + esc_html__( |
|
173 | + "EEM_CPT_Base should nto call deleted_field_name! It should instead use post_status_field_name", |
|
174 | + "event_espresso" |
|
175 | + ) |
|
176 | + ) |
|
177 | + ); |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + /** |
|
182 | + * Gets the field's name that sets the post status |
|
183 | + * |
|
184 | + * @return string |
|
185 | + * @throws EE_Error |
|
186 | + */ |
|
187 | + public function post_status_field_name() |
|
188 | + { |
|
189 | + $field = $this->get_a_field_of_type('EE_WP_Post_Status_Field'); |
|
190 | + if ($field) { |
|
191 | + return $field->get_name(); |
|
192 | + } else { |
|
193 | + throw new EE_Error( |
|
194 | + sprintf( |
|
195 | + esc_html__( |
|
196 | + 'We are trying to find the post status flag field on %s, but none was found. Are you sure there is a field of type EE_Trashed_Flag_Field in %s constructor?', |
|
197 | + 'event_espresso' |
|
198 | + ), |
|
199 | + get_class($this), |
|
200 | + get_class($this) |
|
201 | + ) |
|
202 | + ); |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + |
|
207 | + /** |
|
208 | + * Alters the query params so that only trashed/soft-deleted items are considered |
|
209 | + * |
|
210 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
211 | + * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
212 | + */ |
|
213 | + protected function _alter_query_params_so_only_trashed_items_included($query_params) |
|
214 | + { |
|
215 | + $post_status_field_name = $this->post_status_field_name(); |
|
216 | + $query_params[0][ $post_status_field_name ] = self::post_status_trashed; |
|
217 | + return $query_params; |
|
218 | + } |
|
219 | + |
|
220 | + |
|
221 | + /** |
|
222 | + * Alters the query params so each item's deleted status is ignored. |
|
223 | + * |
|
224 | + * @param array $query_params |
|
225 | + * @return array |
|
226 | + */ |
|
227 | + protected function _alter_query_params_so_deleted_and_undeleted_items_included($query_params) |
|
228 | + { |
|
229 | + $query_params['default_where_conditions'] = 'minimum'; |
|
230 | + return $query_params; |
|
231 | + } |
|
232 | + |
|
233 | + |
|
234 | + /** |
|
235 | + * Performs deletes or restores on items. Both soft-deleted and non-soft-deleted items considered. |
|
236 | + * |
|
237 | + * @param boolean $delete true to indicate deletion, false to indicate restoration |
|
238 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
239 | + * @return boolean success |
|
240 | + */ |
|
241 | + public function delete_or_restore($delete = true, $query_params = array()) |
|
242 | + { |
|
243 | + $post_status_field_name = $this->post_status_field_name(); |
|
244 | + $query_params = $this->_alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
245 | + $new_status = $delete ? self::post_status_trashed : 'draft'; |
|
246 | + if ($this->update(array($post_status_field_name => $new_status), $query_params)) { |
|
247 | + return true; |
|
248 | + } else { |
|
249 | + return false; |
|
250 | + } |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * meta_table |
|
256 | + * returns first EE_Secondary_Table table name |
|
257 | + * |
|
258 | + * @access public |
|
259 | + * @return string |
|
260 | + */ |
|
261 | + public function meta_table() |
|
262 | + { |
|
263 | + $meta_table = $this->_get_other_tables(); |
|
264 | + $meta_table = reset($meta_table); |
|
265 | + return $meta_table instanceof EE_Secondary_Table ? $meta_table->get_table_name() : null; |
|
266 | + } |
|
267 | + |
|
268 | + |
|
269 | + /** |
|
270 | + * This simply returns an array of the meta table fields (useful for when we just need to update those fields) |
|
271 | + * |
|
272 | + * @param bool $all triggers whether we include DB_Only fields or JUST non DB_Only fields. Defaults to false (no |
|
273 | + * db only fields) |
|
274 | + * @return array |
|
275 | + */ |
|
276 | + public function get_meta_table_fields($all = false) |
|
277 | + { |
|
278 | + $all_fields = $fields_to_return = array(); |
|
279 | + foreach ($this->_tables as $alias => $table_obj) { |
|
280 | + if ($table_obj instanceof EE_Secondary_Table) { |
|
281 | + $all_fields = array_merge($this->_get_fields_for_table($alias), $all_fields); |
|
282 | + } |
|
283 | + } |
|
284 | + if (! $all) { |
|
285 | + foreach ($all_fields as $name => $obj) { |
|
286 | + if ($obj instanceof EE_DB_Only_Field_Base) { |
|
287 | + continue; |
|
288 | + } |
|
289 | + $fields_to_return[] = $name; |
|
290 | + } |
|
291 | + } else { |
|
292 | + $fields_to_return = array_keys($all_fields); |
|
293 | + } |
|
294 | + return $fields_to_return; |
|
295 | + } |
|
296 | + |
|
297 | + |
|
298 | + /** |
|
299 | + * Adds an event category with the specified name and description to the specified |
|
300 | + * $cpt_model_object. Intelligently adds a term if necessary, and adds a term_taxonomy if necessary, |
|
301 | + * and adds an entry in the term_relationship if necessary. |
|
302 | + * |
|
303 | + * @param EE_CPT_Base $cpt_model_object |
|
304 | + * @param string $category_name (used to derive the term slug too) |
|
305 | + * @param string $category_description |
|
306 | + * @param int $parent_term_taxonomy_id |
|
307 | + * @return EE_Term_Taxonomy |
|
308 | + */ |
|
309 | + public function add_event_category( |
|
310 | + EE_CPT_Base $cpt_model_object, |
|
311 | + $category_name, |
|
312 | + $category_description = '', |
|
313 | + $parent_term_taxonomy_id = null |
|
314 | + ) { |
|
315 | + // create term |
|
316 | + require_once(EE_MODELS . 'EEM_Term.model.php'); |
|
317 | + // first, check for a term by the same name or slug |
|
318 | + $category_slug = sanitize_title($category_name); |
|
319 | + $term = EEM_Term::instance()->get_one( |
|
320 | + array( |
|
321 | + array( |
|
322 | + 'OR' => array( |
|
323 | + 'name' => $category_name, |
|
324 | + 'slug' => $category_slug, |
|
325 | + ), |
|
326 | + 'Term_Taxonomy.taxonomy' => self::EVENT_CATEGORY_TAXONOMY |
|
327 | + ), |
|
328 | + ) |
|
329 | + ); |
|
330 | + if (! $term) { |
|
331 | + $term = EE_Term::new_instance( |
|
332 | + array( |
|
333 | + 'name' => $category_name, |
|
334 | + 'slug' => $category_slug, |
|
335 | + ) |
|
336 | + ); |
|
337 | + $term->save(); |
|
338 | + } |
|
339 | + // make sure there's a term-taxonomy entry too |
|
340 | + require_once(EE_MODELS . 'EEM_Term_Taxonomy.model.php'); |
|
341 | + $term_taxonomy = EEM_Term_Taxonomy::instance()->get_one( |
|
342 | + array( |
|
343 | + array( |
|
344 | + 'term_id' => $term->ID(), |
|
345 | + 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
346 | + ), |
|
347 | + ) |
|
348 | + ); |
|
349 | + /** @var $term_taxonomy EE_Term_Taxonomy */ |
|
350 | + if (! $term_taxonomy) { |
|
351 | + $term_taxonomy = EE_Term_Taxonomy::new_instance( |
|
352 | + array( |
|
353 | + 'term_id' => $term->ID(), |
|
354 | + 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
355 | + 'description' => $category_description, |
|
356 | + 'term_count' => 1, |
|
357 | + 'parent' => $parent_term_taxonomy_id, |
|
358 | + ) |
|
359 | + ); |
|
360 | + $term_taxonomy->save(); |
|
361 | + } else { |
|
362 | + $term_taxonomy->set_count($term_taxonomy->count() + 1); |
|
363 | + $term_taxonomy->save(); |
|
364 | + } |
|
365 | + return $this->add_relationship_to($cpt_model_object, $term_taxonomy, 'Term_Taxonomy'); |
|
366 | + } |
|
367 | + |
|
368 | + |
|
369 | + /** |
|
370 | + * Removed the category specified by name as having a relation to this event. |
|
371 | + * Does not remove the term or term_taxonomy. |
|
372 | + * |
|
373 | + * @param EE_CPT_Base $cpt_model_object_event |
|
374 | + * @param string $category_name name of the event category (term) |
|
375 | + * @return bool |
|
376 | + */ |
|
377 | + public function remove_event_category(EE_CPT_Base $cpt_model_object_event, $category_name) |
|
378 | + { |
|
379 | + // find the term_taxonomy by that name |
|
380 | + $term_taxonomy = $this->get_first_related( |
|
381 | + $cpt_model_object_event, |
|
382 | + 'Term_Taxonomy', |
|
383 | + array(array('Term.name' => $category_name, 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY)) |
|
384 | + ); |
|
385 | + /** @var $term_taxonomy EE_Term_Taxonomy */ |
|
386 | + if ($term_taxonomy) { |
|
387 | + $term_taxonomy->set_count($term_taxonomy->count() - 1); |
|
388 | + $term_taxonomy->save(); |
|
389 | + } |
|
390 | + return $this->remove_relationship_to($cpt_model_object_event, $term_taxonomy, 'Term_Taxonomy'); |
|
391 | + } |
|
392 | + |
|
393 | + |
|
394 | + /** |
|
395 | + * This is a wrapper for the WordPress get_the_post_thumbnail() function that returns the feature image for the |
|
396 | + * given CPT ID. It accepts the same params as what get_the_post_thumbnail() accepts. |
|
397 | + * |
|
398 | + * @link http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail |
|
399 | + * @access public |
|
400 | + * @param int $id the ID for the cpt we want the feature image for |
|
401 | + * @param string|array $size (optional) Image size. Defaults to 'post-thumbnail' but can also be a 2-item array |
|
402 | + * representing width and height in pixels (i.e. array(32,32) ). |
|
403 | + * @param string|array $attr Optional. Query string or array of attributes. |
|
404 | + * @return string HTML image element |
|
405 | + */ |
|
406 | + public function get_feature_image($id, $size = 'thumbnail', $attr = '') |
|
407 | + { |
|
408 | + return get_the_post_thumbnail($id, $size, $attr); |
|
409 | + } |
|
410 | + |
|
411 | + |
|
412 | + /** |
|
413 | + * Just a handy way to get the list of post statuses currently registered with WP. |
|
414 | + * |
|
415 | + * @global array $wp_post_statuses set in wp core for storing all the post stati |
|
416 | + * @return array |
|
417 | + */ |
|
418 | + public function get_post_statuses() |
|
419 | + { |
|
420 | + global $wp_post_statuses; |
|
421 | + $statuses = array(); |
|
422 | + foreach ($wp_post_statuses as $post_status => $args_object) { |
|
423 | + $statuses[ $post_status ] = $args_object->label; |
|
424 | + } |
|
425 | + return $statuses; |
|
426 | + } |
|
427 | + |
|
428 | + |
|
429 | + /** |
|
430 | + * public method that can be used to retrieve the protected status array on the instantiated cpt model |
|
431 | + * |
|
432 | + * @return array array of statuses. |
|
433 | + */ |
|
434 | + public function get_status_array() |
|
435 | + { |
|
436 | + $statuses = $this->get_post_statuses(); |
|
437 | + // first the global filter |
|
438 | + $statuses = apply_filters('FHEE_EEM_CPT_Base__get_status_array', $statuses); |
|
439 | + // now the class specific filter |
|
440 | + $statuses = apply_filters('FHEE_EEM_' . get_class($this) . '__get_status_array', $statuses); |
|
441 | + return $statuses; |
|
442 | + } |
|
443 | + |
|
444 | + |
|
445 | + /** |
|
446 | + * this returns the post statuses that are NOT the default wordpress status |
|
447 | + * |
|
448 | + * @return array |
|
449 | + */ |
|
450 | + public function get_custom_post_statuses() |
|
451 | + { |
|
452 | + $new_stati = array(); |
|
453 | + foreach ($this->_custom_stati as $status => $props) { |
|
454 | + $new_stati[ $status ] = $props['label']; |
|
455 | + } |
|
456 | + return $new_stati; |
|
457 | + } |
|
458 | + |
|
459 | + |
|
460 | + /** |
|
461 | + * Creates a child of EE_CPT_Base given a WP_Post or array of wpdb results which |
|
462 | + * are a row from the posts table. If we're missing any fields required for the model, |
|
463 | + * we just fetch the entire entry from the DB (ie, if you want to use this to save DB queries, |
|
464 | + * make sure you are attaching all the model's fields onto the post) |
|
465 | + * |
|
466 | + * @param WP_Post|array $post |
|
467 | + * @return EE_Base_Class|EE_Soft_Delete_Base_Class |
|
468 | + */ |
|
469 | + public function instantiate_class_from_post_object_orig($post) |
|
470 | + { |
|
471 | + $post = (array) $post; |
|
472 | + $has_all_necessary_fields_for_table = true; |
|
473 | + // check if the post has fields on the meta table already |
|
474 | + foreach ($this->_get_other_tables() as $table_obj) { |
|
475 | + $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
|
476 | + foreach ($fields_for_that_table as $field_obj) { |
|
477 | + if ( |
|
478 | + ! isset($post[ $field_obj->get_table_column() ]) |
|
479 | + && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
480 | + ) { |
|
481 | + $has_all_necessary_fields_for_table = false; |
|
482 | + } |
|
483 | + } |
|
484 | + } |
|
485 | + // if we don't have all the fields we need, then just fetch the proper model from the DB |
|
486 | + if (! $has_all_necessary_fields_for_table) { |
|
487 | + return $this->get_one_by_ID($post['ID']); |
|
488 | + } else { |
|
489 | + return $this->instantiate_class_from_array_or_object($post); |
|
490 | + } |
|
491 | + } |
|
492 | + |
|
493 | + |
|
494 | + /** |
|
495 | + * @param null $post |
|
496 | + * @return EE_Base_Class|EE_Soft_Delete_Base_Class |
|
497 | + */ |
|
498 | + public function instantiate_class_from_post_object($post = null) |
|
499 | + { |
|
500 | + if (empty($post)) { |
|
501 | + global $post; |
|
502 | + } |
|
503 | + $post = (array) $post; |
|
504 | + $tables_needing_to_be_queried = array(); |
|
505 | + // check if the post has fields on the meta table already |
|
506 | + foreach ($this->get_tables() as $table_obj) { |
|
507 | + $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
|
508 | + foreach ($fields_for_that_table as $field_obj) { |
|
509 | + if ( |
|
510 | + ! isset($post[ $field_obj->get_table_column() ]) |
|
511 | + && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
512 | + ) { |
|
513 | + $tables_needing_to_be_queried[ $table_obj->get_table_alias() ] = $table_obj; |
|
514 | + } |
|
515 | + } |
|
516 | + } |
|
517 | + // if we don't have all the fields we need, then just fetch the proper model from the DB |
|
518 | + if ($tables_needing_to_be_queried) { |
|
519 | + if ( |
|
520 | + count($tables_needing_to_be_queried) == 1 |
|
521 | + && reset($tables_needing_to_be_queried) |
|
522 | + instanceof |
|
523 | + EE_Secondary_Table |
|
524 | + ) { |
|
525 | + // so we're only missing data from a secondary table. Well that's not too hard to query for |
|
526 | + $table_to_query = reset($tables_needing_to_be_queried); |
|
527 | + $missing_data = $this->_do_wpdb_query( |
|
528 | + 'get_row', |
|
529 | + array( |
|
530 | + 'SELECT * FROM ' |
|
531 | + . $table_to_query->get_table_name() |
|
532 | + . ' WHERE ' |
|
533 | + . $table_to_query->get_fk_on_table() |
|
534 | + . ' = ' |
|
535 | + . $post['ID'], |
|
536 | + ARRAY_A, |
|
537 | + ) |
|
538 | + ); |
|
539 | + if (! empty($missing_data)) { |
|
540 | + $post = array_merge($post, $missing_data); |
|
541 | + } |
|
542 | + } else { |
|
543 | + return $this->get_one_by_ID($post['ID']); |
|
544 | + } |
|
545 | + } |
|
546 | + return $this->instantiate_class_from_array_or_object($post); |
|
547 | + } |
|
548 | + |
|
549 | + |
|
550 | + /** |
|
551 | + * Gets the post type associated with this |
|
552 | + * |
|
553 | + * @throws EE_Error |
|
554 | + * @return string |
|
555 | + */ |
|
556 | + public function post_type() |
|
557 | + { |
|
558 | + $post_type_field = null; |
|
559 | + foreach ($this->field_settings(true) as $field_obj) { |
|
560 | + if ($field_obj instanceof EE_WP_Post_Type_Field) { |
|
561 | + $post_type_field = $field_obj; |
|
562 | + break; |
|
563 | + } |
|
564 | + } |
|
565 | + if ($post_type_field == null) { |
|
566 | + throw new EE_Error( |
|
567 | + sprintf( |
|
568 | + esc_html__( |
|
569 | + "CPT Model %s should have a field of type EE_WP_Post_Type, but doesnt", |
|
570 | + "event_espresso" |
|
571 | + ), |
|
572 | + get_class($this) |
|
573 | + ) |
|
574 | + ); |
|
575 | + } |
|
576 | + return $post_type_field->get_default_value(); |
|
577 | + } |
|
578 | 578 | } |
@@ -16,502 +16,502 @@ |
||
16 | 16 | */ |
17 | 17 | abstract class EE_Model_Relation_Base implements HasSchemaInterface |
18 | 18 | { |
19 | - /** |
|
20 | - * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
|
21 | - * |
|
22 | - * @var string eg Event, Question_Group, Registration |
|
23 | - */ |
|
24 | - private $_this_model_name; |
|
25 | - /** |
|
26 | - * The model name pointed to by this relation (ie, the model we want to establish a relationship to) |
|
27 | - * |
|
28 | - * @var string eg Event, Question_Group, Registration |
|
29 | - */ |
|
30 | - private $_other_model_name; |
|
31 | - |
|
32 | - /** |
|
33 | - * this is typically used when calling the relation models to make sure they inherit any set timezone from the |
|
34 | - * initiating model. |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $_timezone; |
|
39 | - |
|
40 | - /** |
|
41 | - * If you try to delete "this_model", and there are related "other_models", |
|
42 | - * and this isn't null, then abandon the deletion and add this warning. |
|
43 | - * This effectively makes it impossible to delete "this_model" while there are |
|
44 | - * related "other_models" along this relation. |
|
45 | - * |
|
46 | - * @var string (internationalized) |
|
47 | - */ |
|
48 | - protected $_blocking_delete_error_message; |
|
49 | - |
|
50 | - protected $_blocking_delete = false; |
|
51 | - |
|
52 | - /** |
|
53 | - * Object representing the relationship between two models. This knows how to join the models, |
|
54 | - * get related models across the relation, and add-and-remove the relationships. |
|
55 | - * |
|
56 | - * @param boolean $block_deletes if there are related models across this relation, block (prevent |
|
57 | - * and add an error) the deletion of this model |
|
58 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
59 | - * default |
|
60 | - */ |
|
61 | - public function __construct($block_deletes, $blocking_delete_error_message) |
|
62 | - { |
|
63 | - $this->_blocking_delete = $block_deletes; |
|
64 | - $this->_blocking_delete_error_message = $blocking_delete_error_message; |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * @param $this_model_name |
|
70 | - * @param $other_model_name |
|
71 | - * @throws EE_Error |
|
72 | - */ |
|
73 | - public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
74 | - { |
|
75 | - $this->_this_model_name = $this_model_name; |
|
76 | - $this->_other_model_name = $other_model_name; |
|
77 | - if (is_string($this->_blocking_delete)) { |
|
78 | - throw new EE_Error(sprintf( |
|
79 | - esc_html__( |
|
80 | - "When instantiating the relation of type %s from %s to %s, the \$block_deletes argument should be a boolean, not a string (%s)", |
|
81 | - "event_espresso" |
|
82 | - ), |
|
83 | - get_class($this), |
|
84 | - $this_model_name, |
|
85 | - $other_model_name, |
|
86 | - $this->_blocking_delete |
|
87 | - )); |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * Gets the model where this relation is defined. |
|
94 | - * |
|
95 | - * @return EEM_Base |
|
96 | - */ |
|
97 | - public function get_this_model() |
|
98 | - { |
|
99 | - return $this->_get_model($this->_this_model_name); |
|
100 | - } |
|
101 | - |
|
102 | - |
|
103 | - /** |
|
104 | - * Gets the model which this relation establishes the relation TO (ie, |
|
105 | - * this relation object was defined on get_this_model(), get_other_model() is the other one) |
|
106 | - * |
|
107 | - * @return EEM_Base |
|
108 | - */ |
|
109 | - public function get_other_model() |
|
110 | - { |
|
111 | - return $this->_get_model($this->_other_model_name); |
|
112 | - } |
|
113 | - |
|
114 | - |
|
115 | - /** |
|
116 | - * Internally used by get_this_model() and get_other_model() |
|
117 | - * |
|
118 | - * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
119 | - * @return EEM_Base |
|
120 | - */ |
|
121 | - protected function _get_model($model_name) |
|
122 | - { |
|
123 | - $modelInstance = EE_Registry::instance()->load_model($model_name); |
|
124 | - $modelInstance->set_timezone($this->_timezone); |
|
125 | - return $modelInstance; |
|
126 | - } |
|
127 | - |
|
128 | - |
|
129 | - /** |
|
130 | - * entirely possible that relations may be called from a model and we need to make sure those relations have their |
|
131 | - * timezone set correctly. |
|
132 | - * |
|
133 | - * @param string $timezone timezone to set. |
|
134 | - */ |
|
135 | - public function set_timezone($timezone) |
|
136 | - { |
|
137 | - if ($timezone !== null) { |
|
138 | - $this->_timezone = $timezone; |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - |
|
143 | - /** |
|
144 | - * @param $other_table |
|
145 | - * @param $other_table_alias |
|
146 | - * @param $other_table_column |
|
147 | - * @param $this_table_alias |
|
148 | - * @param $this_table_join_column |
|
149 | - * @param string $extra_join_sql |
|
150 | - * @return string |
|
151 | - */ |
|
152 | - protected function _left_join( |
|
153 | - $other_table, |
|
154 | - $other_table_alias, |
|
155 | - $other_table_column, |
|
156 | - $this_table_alias, |
|
157 | - $this_table_join_column, |
|
158 | - $extra_join_sql = '' |
|
159 | - ) { |
|
160 | - return " LEFT JOIN " . $other_table . " AS " . $other_table_alias . " ON " . $other_table_alias . "." . $other_table_column . "=" . $this_table_alias . "." . $this_table_join_column . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
161 | - } |
|
162 | - |
|
163 | - |
|
164 | - /** |
|
165 | - * Gets all the model objects of type of other model related to $model_object, |
|
166 | - * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
|
167 | - * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
|
168 | - * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
|
169 | - * EE_Belongs_To_Relation doesn't need to be saved before querying. |
|
170 | - * |
|
171 | - * @param EE_Base_Class|int $model_object_or_id or the primary key of this model |
|
172 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
173 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
174 | - * @return EE_Base_Class[] |
|
175 | - * @throws \EE_Error |
|
176 | - */ |
|
177 | - public function get_all_related( |
|
178 | - $model_object_or_id, |
|
179 | - $query_params = array(), |
|
180 | - $values_already_prepared_by_model_object = false |
|
181 | - ) { |
|
182 | - if ($values_already_prepared_by_model_object !== false) { |
|
183 | - EE_Error::doing_it_wrong( |
|
184 | - 'EE_Model_Relation_Base::get_all_related', |
|
185 | - esc_html__('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
186 | - '4.8.1' |
|
187 | - ); |
|
188 | - } |
|
189 | - $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
190 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() |
|
191 | - . "." |
|
192 | - . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
193 | - $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
194 | - $query_params[0][ $query_param_where_this_model_pk ] = $model_object_id; |
|
195 | - return $this->get_other_model()->get_all($query_params); |
|
196 | - } |
|
197 | - |
|
198 | - |
|
199 | - /** |
|
200 | - * Alters the $query_params to disable default where conditions, unless otherwise specified |
|
201 | - * |
|
202 | - * @param string $query_params |
|
203 | - * @return array |
|
204 | - */ |
|
205 | - protected function _disable_default_where_conditions_on_query_param($query_params) |
|
206 | - { |
|
207 | - if (! isset($query_params['default_where_conditions'])) { |
|
208 | - $query_params['default_where_conditions'] = 'none'; |
|
209 | - } |
|
210 | - return $query_params; |
|
211 | - } |
|
212 | - |
|
213 | - |
|
214 | - /** |
|
215 | - * Deletes the related model objects which meet the query parameters. If no |
|
216 | - * parameters are specified, then all related model objects will be deleted. |
|
217 | - * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
218 | - * model objects will only be soft-deleted. |
|
219 | - * |
|
220 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
221 | - * @param array $query_params |
|
222 | - * @return int of how many related models got deleted |
|
223 | - * @throws \EE_Error |
|
224 | - */ |
|
225 | - public function delete_all_related($model_object_or_id, $query_params = array()) |
|
226 | - { |
|
227 | - // for each thing we would delete, |
|
228 | - $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
229 | - // determine if it's blocked by anything else before it can be deleted |
|
230 | - $deleted_count = 0; |
|
231 | - foreach ($related_model_objects as $related_model_object) { |
|
232 | - $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
233 | - $related_model_object, |
|
234 | - $model_object_or_id |
|
235 | - ); |
|
236 | - /* @var $model_object_or_id EE_Base_Class */ |
|
237 | - if (! $delete_is_blocked) { |
|
238 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
239 | - $related_model_object->delete(); |
|
240 | - $deleted_count++; |
|
241 | - } |
|
242 | - } |
|
243 | - return $deleted_count; |
|
244 | - } |
|
245 | - |
|
246 | - |
|
247 | - /** |
|
248 | - * Deletes the related model objects which meet the query parameters. If no |
|
249 | - * parameters are specified, then all related model objects will be deleted. |
|
250 | - * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
251 | - * model objects will only be soft-deleted. |
|
252 | - * |
|
253 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
254 | - * @param array $query_params |
|
255 | - * @return int of how many related models got deleted |
|
256 | - * @throws \EE_Error |
|
257 | - */ |
|
258 | - public function delete_related_permanently($model_object_or_id, $query_params = array()) |
|
259 | - { |
|
260 | - // for each thing we would delete, |
|
261 | - $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
262 | - // determine if it's blocked by anything else before it can be deleted |
|
263 | - $deleted_count = 0; |
|
264 | - foreach ($related_model_objects as $related_model_object) { |
|
265 | - $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
266 | - $related_model_object, |
|
267 | - $model_object_or_id |
|
268 | - ); |
|
269 | - /* @var $model_object_or_id EE_Base_Class */ |
|
270 | - if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
|
271 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
272 | - $deleted_count++; |
|
273 | - if (! $delete_is_blocked) { |
|
274 | - $related_model_object->delete_permanently(); |
|
275 | - } else { |
|
276 | - // delete is blocked |
|
277 | - // brent and darren, in this case, wanted to just soft delete it then |
|
278 | - $related_model_object->delete(); |
|
279 | - } |
|
280 | - } else { |
|
281 | - // its not a soft-deletable thing anyways. do the normal logic. |
|
282 | - if (! $delete_is_blocked) { |
|
283 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
284 | - $related_model_object->delete(); |
|
285 | - $deleted_count++; |
|
286 | - } |
|
287 | - } |
|
288 | - } |
|
289 | - return $deleted_count; |
|
290 | - } |
|
291 | - |
|
292 | - |
|
293 | - /** |
|
294 | - * this just returns a model_object_id for incoming item that could be an object or id. |
|
295 | - * |
|
296 | - * @param EE_Base_Class|int $model_object_or_id model object or the primary key of this model |
|
297 | - * @throws EE_Error |
|
298 | - * @return int |
|
299 | - */ |
|
300 | - protected function _get_model_object_id($model_object_or_id) |
|
301 | - { |
|
302 | - $model_object_id = $model_object_or_id; |
|
303 | - if ($model_object_or_id instanceof EE_Base_Class) { |
|
304 | - $model_object_id = $model_object_or_id->ID(); |
|
305 | - } |
|
306 | - if (! $model_object_id) { |
|
307 | - throw new EE_Error(sprintf( |
|
308 | - esc_html__( |
|
309 | - "Sorry, we cant get the related %s model objects to %s model object before it has an ID. You can solve that by just saving it before trying to get its related model objects", |
|
310 | - "event_espresso" |
|
311 | - ), |
|
312 | - $this->get_other_model()->get_this_model_name(), |
|
313 | - $this->get_this_model()->get_this_model_name() |
|
314 | - )); |
|
315 | - } |
|
316 | - return $model_object_id; |
|
317 | - } |
|
318 | - |
|
319 | - |
|
320 | - /** |
|
321 | - * Gets the SQL string for performing the join between this model and the other model. |
|
322 | - * |
|
323 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
324 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
325 | - * other_model_primary_table.fk" etc |
|
326 | - */ |
|
327 | - abstract public function get_join_statement($model_relation_chain); |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
|
332 | - * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
|
333 | - * relationship only allows this model to be related to a single other model of this type) |
|
334 | - * |
|
335 | - * @param $this_obj_or_id |
|
336 | - * @param $other_obj_or_id |
|
337 | - * @param array $extra_join_model_fields_n_values |
|
338 | - * @return \EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
|
339 | - * $other_obj_or_id) |
|
340 | - */ |
|
341 | - abstract public function add_relation_to( |
|
342 | - $this_obj_or_id, |
|
343 | - $other_obj_or_id, |
|
344 | - $extra_join_model_fields_n_values = array() |
|
345 | - ); |
|
346 | - |
|
347 | - |
|
348 | - /** |
|
349 | - * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
|
350 | - * model objects |
|
351 | - * |
|
352 | - * @param $this_obj_or_id |
|
353 | - * @param $other_obj_or_id |
|
354 | - * @param array $where_query |
|
355 | - * @return bool |
|
356 | - */ |
|
357 | - abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()); |
|
358 | - |
|
359 | - |
|
360 | - /** |
|
361 | - * Removes ALL relation instances for this relation obj |
|
362 | - * |
|
363 | - * @param EE_Base_Class|int $this_obj_or_id |
|
364 | - * @param array $where_query_param @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
365 | - * @return EE_Base_Class[] |
|
366 | - * @throws \EE_Error |
|
367 | - */ |
|
368 | - public function remove_relations($this_obj_or_id, $where_query_param = array()) |
|
369 | - { |
|
370 | - $related_things = $this->get_all_related($this_obj_or_id, array($where_query_param)); |
|
371 | - $objs_removed = array(); |
|
372 | - foreach ($related_things as $related_thing) { |
|
373 | - $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing); |
|
374 | - } |
|
375 | - return $objs_removed; |
|
376 | - } |
|
377 | - |
|
378 | - |
|
379 | - /** |
|
380 | - * If you aren't allowed to delete this model when there are related models across this |
|
381 | - * relation object, return true. Otherwise, if you can delete this model even though |
|
382 | - * related objects exist, returns false. |
|
383 | - * |
|
384 | - * @return boolean |
|
385 | - */ |
|
386 | - public function block_delete_if_related_models_exist() |
|
387 | - { |
|
388 | - return $this->_blocking_delete; |
|
389 | - } |
|
390 | - |
|
391 | - |
|
392 | - /** |
|
393 | - * Gets the error message to show |
|
394 | - * |
|
395 | - * @return string |
|
396 | - */ |
|
397 | - public function get_deletion_error_message() |
|
398 | - { |
|
399 | - if ($this->_blocking_delete_error_message) { |
|
400 | - return $this->_blocking_delete_error_message; |
|
401 | - } else { |
|
19 | + /** |
|
20 | + * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
|
21 | + * |
|
22 | + * @var string eg Event, Question_Group, Registration |
|
23 | + */ |
|
24 | + private $_this_model_name; |
|
25 | + /** |
|
26 | + * The model name pointed to by this relation (ie, the model we want to establish a relationship to) |
|
27 | + * |
|
28 | + * @var string eg Event, Question_Group, Registration |
|
29 | + */ |
|
30 | + private $_other_model_name; |
|
31 | + |
|
32 | + /** |
|
33 | + * this is typically used when calling the relation models to make sure they inherit any set timezone from the |
|
34 | + * initiating model. |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $_timezone; |
|
39 | + |
|
40 | + /** |
|
41 | + * If you try to delete "this_model", and there are related "other_models", |
|
42 | + * and this isn't null, then abandon the deletion and add this warning. |
|
43 | + * This effectively makes it impossible to delete "this_model" while there are |
|
44 | + * related "other_models" along this relation. |
|
45 | + * |
|
46 | + * @var string (internationalized) |
|
47 | + */ |
|
48 | + protected $_blocking_delete_error_message; |
|
49 | + |
|
50 | + protected $_blocking_delete = false; |
|
51 | + |
|
52 | + /** |
|
53 | + * Object representing the relationship between two models. This knows how to join the models, |
|
54 | + * get related models across the relation, and add-and-remove the relationships. |
|
55 | + * |
|
56 | + * @param boolean $block_deletes if there are related models across this relation, block (prevent |
|
57 | + * and add an error) the deletion of this model |
|
58 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
59 | + * default |
|
60 | + */ |
|
61 | + public function __construct($block_deletes, $blocking_delete_error_message) |
|
62 | + { |
|
63 | + $this->_blocking_delete = $block_deletes; |
|
64 | + $this->_blocking_delete_error_message = $blocking_delete_error_message; |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * @param $this_model_name |
|
70 | + * @param $other_model_name |
|
71 | + * @throws EE_Error |
|
72 | + */ |
|
73 | + public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
74 | + { |
|
75 | + $this->_this_model_name = $this_model_name; |
|
76 | + $this->_other_model_name = $other_model_name; |
|
77 | + if (is_string($this->_blocking_delete)) { |
|
78 | + throw new EE_Error(sprintf( |
|
79 | + esc_html__( |
|
80 | + "When instantiating the relation of type %s from %s to %s, the \$block_deletes argument should be a boolean, not a string (%s)", |
|
81 | + "event_espresso" |
|
82 | + ), |
|
83 | + get_class($this), |
|
84 | + $this_model_name, |
|
85 | + $other_model_name, |
|
86 | + $this->_blocking_delete |
|
87 | + )); |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * Gets the model where this relation is defined. |
|
94 | + * |
|
95 | + * @return EEM_Base |
|
96 | + */ |
|
97 | + public function get_this_model() |
|
98 | + { |
|
99 | + return $this->_get_model($this->_this_model_name); |
|
100 | + } |
|
101 | + |
|
102 | + |
|
103 | + /** |
|
104 | + * Gets the model which this relation establishes the relation TO (ie, |
|
105 | + * this relation object was defined on get_this_model(), get_other_model() is the other one) |
|
106 | + * |
|
107 | + * @return EEM_Base |
|
108 | + */ |
|
109 | + public function get_other_model() |
|
110 | + { |
|
111 | + return $this->_get_model($this->_other_model_name); |
|
112 | + } |
|
113 | + |
|
114 | + |
|
115 | + /** |
|
116 | + * Internally used by get_this_model() and get_other_model() |
|
117 | + * |
|
118 | + * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
119 | + * @return EEM_Base |
|
120 | + */ |
|
121 | + protected function _get_model($model_name) |
|
122 | + { |
|
123 | + $modelInstance = EE_Registry::instance()->load_model($model_name); |
|
124 | + $modelInstance->set_timezone($this->_timezone); |
|
125 | + return $modelInstance; |
|
126 | + } |
|
127 | + |
|
128 | + |
|
129 | + /** |
|
130 | + * entirely possible that relations may be called from a model and we need to make sure those relations have their |
|
131 | + * timezone set correctly. |
|
132 | + * |
|
133 | + * @param string $timezone timezone to set. |
|
134 | + */ |
|
135 | + public function set_timezone($timezone) |
|
136 | + { |
|
137 | + if ($timezone !== null) { |
|
138 | + $this->_timezone = $timezone; |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + |
|
143 | + /** |
|
144 | + * @param $other_table |
|
145 | + * @param $other_table_alias |
|
146 | + * @param $other_table_column |
|
147 | + * @param $this_table_alias |
|
148 | + * @param $this_table_join_column |
|
149 | + * @param string $extra_join_sql |
|
150 | + * @return string |
|
151 | + */ |
|
152 | + protected function _left_join( |
|
153 | + $other_table, |
|
154 | + $other_table_alias, |
|
155 | + $other_table_column, |
|
156 | + $this_table_alias, |
|
157 | + $this_table_join_column, |
|
158 | + $extra_join_sql = '' |
|
159 | + ) { |
|
160 | + return " LEFT JOIN " . $other_table . " AS " . $other_table_alias . " ON " . $other_table_alias . "." . $other_table_column . "=" . $this_table_alias . "." . $this_table_join_column . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
161 | + } |
|
162 | + |
|
163 | + |
|
164 | + /** |
|
165 | + * Gets all the model objects of type of other model related to $model_object, |
|
166 | + * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
|
167 | + * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
|
168 | + * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
|
169 | + * EE_Belongs_To_Relation doesn't need to be saved before querying. |
|
170 | + * |
|
171 | + * @param EE_Base_Class|int $model_object_or_id or the primary key of this model |
|
172 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
173 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
174 | + * @return EE_Base_Class[] |
|
175 | + * @throws \EE_Error |
|
176 | + */ |
|
177 | + public function get_all_related( |
|
178 | + $model_object_or_id, |
|
179 | + $query_params = array(), |
|
180 | + $values_already_prepared_by_model_object = false |
|
181 | + ) { |
|
182 | + if ($values_already_prepared_by_model_object !== false) { |
|
183 | + EE_Error::doing_it_wrong( |
|
184 | + 'EE_Model_Relation_Base::get_all_related', |
|
185 | + esc_html__('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
186 | + '4.8.1' |
|
187 | + ); |
|
188 | + } |
|
189 | + $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
190 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() |
|
191 | + . "." |
|
192 | + . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
193 | + $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
194 | + $query_params[0][ $query_param_where_this_model_pk ] = $model_object_id; |
|
195 | + return $this->get_other_model()->get_all($query_params); |
|
196 | + } |
|
197 | + |
|
198 | + |
|
199 | + /** |
|
200 | + * Alters the $query_params to disable default where conditions, unless otherwise specified |
|
201 | + * |
|
202 | + * @param string $query_params |
|
203 | + * @return array |
|
204 | + */ |
|
205 | + protected function _disable_default_where_conditions_on_query_param($query_params) |
|
206 | + { |
|
207 | + if (! isset($query_params['default_where_conditions'])) { |
|
208 | + $query_params['default_where_conditions'] = 'none'; |
|
209 | + } |
|
210 | + return $query_params; |
|
211 | + } |
|
212 | + |
|
213 | + |
|
214 | + /** |
|
215 | + * Deletes the related model objects which meet the query parameters. If no |
|
216 | + * parameters are specified, then all related model objects will be deleted. |
|
217 | + * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
218 | + * model objects will only be soft-deleted. |
|
219 | + * |
|
220 | + * @param EE_Base_Class|int|string $model_object_or_id |
|
221 | + * @param array $query_params |
|
222 | + * @return int of how many related models got deleted |
|
223 | + * @throws \EE_Error |
|
224 | + */ |
|
225 | + public function delete_all_related($model_object_or_id, $query_params = array()) |
|
226 | + { |
|
227 | + // for each thing we would delete, |
|
228 | + $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
229 | + // determine if it's blocked by anything else before it can be deleted |
|
230 | + $deleted_count = 0; |
|
231 | + foreach ($related_model_objects as $related_model_object) { |
|
232 | + $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
233 | + $related_model_object, |
|
234 | + $model_object_or_id |
|
235 | + ); |
|
236 | + /* @var $model_object_or_id EE_Base_Class */ |
|
237 | + if (! $delete_is_blocked) { |
|
238 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
239 | + $related_model_object->delete(); |
|
240 | + $deleted_count++; |
|
241 | + } |
|
242 | + } |
|
243 | + return $deleted_count; |
|
244 | + } |
|
245 | + |
|
246 | + |
|
247 | + /** |
|
248 | + * Deletes the related model objects which meet the query parameters. If no |
|
249 | + * parameters are specified, then all related model objects will be deleted. |
|
250 | + * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
251 | + * model objects will only be soft-deleted. |
|
252 | + * |
|
253 | + * @param EE_Base_Class|int|string $model_object_or_id |
|
254 | + * @param array $query_params |
|
255 | + * @return int of how many related models got deleted |
|
256 | + * @throws \EE_Error |
|
257 | + */ |
|
258 | + public function delete_related_permanently($model_object_or_id, $query_params = array()) |
|
259 | + { |
|
260 | + // for each thing we would delete, |
|
261 | + $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
262 | + // determine if it's blocked by anything else before it can be deleted |
|
263 | + $deleted_count = 0; |
|
264 | + foreach ($related_model_objects as $related_model_object) { |
|
265 | + $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
266 | + $related_model_object, |
|
267 | + $model_object_or_id |
|
268 | + ); |
|
269 | + /* @var $model_object_or_id EE_Base_Class */ |
|
270 | + if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
|
271 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
272 | + $deleted_count++; |
|
273 | + if (! $delete_is_blocked) { |
|
274 | + $related_model_object->delete_permanently(); |
|
275 | + } else { |
|
276 | + // delete is blocked |
|
277 | + // brent and darren, in this case, wanted to just soft delete it then |
|
278 | + $related_model_object->delete(); |
|
279 | + } |
|
280 | + } else { |
|
281 | + // its not a soft-deletable thing anyways. do the normal logic. |
|
282 | + if (! $delete_is_blocked) { |
|
283 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
284 | + $related_model_object->delete(); |
|
285 | + $deleted_count++; |
|
286 | + } |
|
287 | + } |
|
288 | + } |
|
289 | + return $deleted_count; |
|
290 | + } |
|
291 | + |
|
292 | + |
|
293 | + /** |
|
294 | + * this just returns a model_object_id for incoming item that could be an object or id. |
|
295 | + * |
|
296 | + * @param EE_Base_Class|int $model_object_or_id model object or the primary key of this model |
|
297 | + * @throws EE_Error |
|
298 | + * @return int |
|
299 | + */ |
|
300 | + protected function _get_model_object_id($model_object_or_id) |
|
301 | + { |
|
302 | + $model_object_id = $model_object_or_id; |
|
303 | + if ($model_object_or_id instanceof EE_Base_Class) { |
|
304 | + $model_object_id = $model_object_or_id->ID(); |
|
305 | + } |
|
306 | + if (! $model_object_id) { |
|
307 | + throw new EE_Error(sprintf( |
|
308 | + esc_html__( |
|
309 | + "Sorry, we cant get the related %s model objects to %s model object before it has an ID. You can solve that by just saving it before trying to get its related model objects", |
|
310 | + "event_espresso" |
|
311 | + ), |
|
312 | + $this->get_other_model()->get_this_model_name(), |
|
313 | + $this->get_this_model()->get_this_model_name() |
|
314 | + )); |
|
315 | + } |
|
316 | + return $model_object_id; |
|
317 | + } |
|
318 | + |
|
319 | + |
|
320 | + /** |
|
321 | + * Gets the SQL string for performing the join between this model and the other model. |
|
322 | + * |
|
323 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
324 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
325 | + * other_model_primary_table.fk" etc |
|
326 | + */ |
|
327 | + abstract public function get_join_statement($model_relation_chain); |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
|
332 | + * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
|
333 | + * relationship only allows this model to be related to a single other model of this type) |
|
334 | + * |
|
335 | + * @param $this_obj_or_id |
|
336 | + * @param $other_obj_or_id |
|
337 | + * @param array $extra_join_model_fields_n_values |
|
338 | + * @return \EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
|
339 | + * $other_obj_or_id) |
|
340 | + */ |
|
341 | + abstract public function add_relation_to( |
|
342 | + $this_obj_or_id, |
|
343 | + $other_obj_or_id, |
|
344 | + $extra_join_model_fields_n_values = array() |
|
345 | + ); |
|
346 | + |
|
347 | + |
|
348 | + /** |
|
349 | + * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
|
350 | + * model objects |
|
351 | + * |
|
352 | + * @param $this_obj_or_id |
|
353 | + * @param $other_obj_or_id |
|
354 | + * @param array $where_query |
|
355 | + * @return bool |
|
356 | + */ |
|
357 | + abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()); |
|
358 | + |
|
359 | + |
|
360 | + /** |
|
361 | + * Removes ALL relation instances for this relation obj |
|
362 | + * |
|
363 | + * @param EE_Base_Class|int $this_obj_or_id |
|
364 | + * @param array $where_query_param @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
365 | + * @return EE_Base_Class[] |
|
366 | + * @throws \EE_Error |
|
367 | + */ |
|
368 | + public function remove_relations($this_obj_or_id, $where_query_param = array()) |
|
369 | + { |
|
370 | + $related_things = $this->get_all_related($this_obj_or_id, array($where_query_param)); |
|
371 | + $objs_removed = array(); |
|
372 | + foreach ($related_things as $related_thing) { |
|
373 | + $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing); |
|
374 | + } |
|
375 | + return $objs_removed; |
|
376 | + } |
|
377 | + |
|
378 | + |
|
379 | + /** |
|
380 | + * If you aren't allowed to delete this model when there are related models across this |
|
381 | + * relation object, return true. Otherwise, if you can delete this model even though |
|
382 | + * related objects exist, returns false. |
|
383 | + * |
|
384 | + * @return boolean |
|
385 | + */ |
|
386 | + public function block_delete_if_related_models_exist() |
|
387 | + { |
|
388 | + return $this->_blocking_delete; |
|
389 | + } |
|
390 | + |
|
391 | + |
|
392 | + /** |
|
393 | + * Gets the error message to show |
|
394 | + * |
|
395 | + * @return string |
|
396 | + */ |
|
397 | + public function get_deletion_error_message() |
|
398 | + { |
|
399 | + if ($this->_blocking_delete_error_message) { |
|
400 | + return $this->_blocking_delete_error_message; |
|
401 | + } else { |
|
402 | 402 | // return sprintf(esc_html__('Cannot delete %1$s when there are related %2$s', "event_espresso"),$this->get_this_model()->item_name(2),$this->get_other_model()->item_name(2)); |
403 | - return sprintf( |
|
404 | - esc_html__( |
|
405 | - 'This %1$s is currently linked to one or more %2$s records. If this %1$s is incorrect, then please remove it from all %3$s before attempting to delete it.', |
|
406 | - "event_espresso" |
|
407 | - ), |
|
408 | - $this->get_this_model()->item_name(1), |
|
409 | - $this->get_other_model()->item_name(1), |
|
410 | - $this->get_other_model()->item_name(2) |
|
411 | - ); |
|
412 | - } |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Returns whatever is set as the nicename for the object. |
|
417 | - * |
|
418 | - * @return string |
|
419 | - */ |
|
420 | - public function getSchemaDescription() |
|
421 | - { |
|
422 | - $description = $this instanceof EE_Belongs_To_Relation |
|
423 | - ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
|
424 | - : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
|
425 | - return sprintf( |
|
426 | - $description, |
|
427 | - $this->get_other_model()->get_this_model_name(), |
|
428 | - $this->get_this_model()->get_this_model_name() |
|
429 | - ); |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * Returns whatever is set as the $_schema_type property for the object. |
|
434 | - * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
435 | - * |
|
436 | - * @return string|array |
|
437 | - */ |
|
438 | - public function getSchemaType() |
|
439 | - { |
|
440 | - return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
445 | - * this method and return the properties for the schema. |
|
446 | - * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
447 | - * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
448 | - * |
|
449 | - * @return array |
|
450 | - */ |
|
451 | - public function getSchemaProperties() |
|
452 | - { |
|
453 | - return array(); |
|
454 | - } |
|
455 | - |
|
456 | - /** |
|
457 | - * If a child class has enum values, they should override this method and provide a simple array |
|
458 | - * of the enum values. |
|
459 | - * The reason this is not a property on the class is because there may be filterable enum values that |
|
460 | - * are set on the instantiated object that could be filtered after construct. |
|
461 | - * |
|
462 | - * @return array |
|
463 | - */ |
|
464 | - public function getSchemaEnum() |
|
465 | - { |
|
466 | - return array(); |
|
467 | - } |
|
468 | - |
|
469 | - /** |
|
470 | - * This returns the value of the $_schema_format property on the object. |
|
471 | - * |
|
472 | - * @return string |
|
473 | - */ |
|
474 | - public function getSchemaFormat() |
|
475 | - { |
|
476 | - return array(); |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * This returns the value of the $_schema_readonly property on the object. |
|
481 | - * |
|
482 | - * @return bool |
|
483 | - */ |
|
484 | - public function getSchemaReadonly() |
|
485 | - { |
|
486 | - return true; |
|
487 | - } |
|
488 | - |
|
489 | - /** |
|
490 | - * This returns elements used to represent this field in the json schema. |
|
491 | - * |
|
492 | - * @link http://json-schema.org/ |
|
493 | - * @return array |
|
494 | - */ |
|
495 | - public function getSchema() |
|
496 | - { |
|
497 | - $schema = array( |
|
498 | - 'description' => $this->getSchemaDescription(), |
|
499 | - 'type' => $this->getSchemaType(), |
|
500 | - 'relation' => true, |
|
501 | - 'relation_type' => get_class($this), |
|
502 | - 'readonly' => $this->getSchemaReadonly() |
|
503 | - ); |
|
504 | - |
|
505 | - if ($this instanceof EE_HABTM_Relation) { |
|
506 | - $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name(); |
|
507 | - } |
|
508 | - |
|
509 | - if ($this->getSchemaType() === 'array') { |
|
510 | - $schema['items'] = array( |
|
511 | - 'type' => 'object' |
|
512 | - ); |
|
513 | - } |
|
514 | - |
|
515 | - return $schema; |
|
516 | - } |
|
403 | + return sprintf( |
|
404 | + esc_html__( |
|
405 | + 'This %1$s is currently linked to one or more %2$s records. If this %1$s is incorrect, then please remove it from all %3$s before attempting to delete it.', |
|
406 | + "event_espresso" |
|
407 | + ), |
|
408 | + $this->get_this_model()->item_name(1), |
|
409 | + $this->get_other_model()->item_name(1), |
|
410 | + $this->get_other_model()->item_name(2) |
|
411 | + ); |
|
412 | + } |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Returns whatever is set as the nicename for the object. |
|
417 | + * |
|
418 | + * @return string |
|
419 | + */ |
|
420 | + public function getSchemaDescription() |
|
421 | + { |
|
422 | + $description = $this instanceof EE_Belongs_To_Relation |
|
423 | + ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
|
424 | + : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
|
425 | + return sprintf( |
|
426 | + $description, |
|
427 | + $this->get_other_model()->get_this_model_name(), |
|
428 | + $this->get_this_model()->get_this_model_name() |
|
429 | + ); |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * Returns whatever is set as the $_schema_type property for the object. |
|
434 | + * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
435 | + * |
|
436 | + * @return string|array |
|
437 | + */ |
|
438 | + public function getSchemaType() |
|
439 | + { |
|
440 | + return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
445 | + * this method and return the properties for the schema. |
|
446 | + * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
447 | + * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
448 | + * |
|
449 | + * @return array |
|
450 | + */ |
|
451 | + public function getSchemaProperties() |
|
452 | + { |
|
453 | + return array(); |
|
454 | + } |
|
455 | + |
|
456 | + /** |
|
457 | + * If a child class has enum values, they should override this method and provide a simple array |
|
458 | + * of the enum values. |
|
459 | + * The reason this is not a property on the class is because there may be filterable enum values that |
|
460 | + * are set on the instantiated object that could be filtered after construct. |
|
461 | + * |
|
462 | + * @return array |
|
463 | + */ |
|
464 | + public function getSchemaEnum() |
|
465 | + { |
|
466 | + return array(); |
|
467 | + } |
|
468 | + |
|
469 | + /** |
|
470 | + * This returns the value of the $_schema_format property on the object. |
|
471 | + * |
|
472 | + * @return string |
|
473 | + */ |
|
474 | + public function getSchemaFormat() |
|
475 | + { |
|
476 | + return array(); |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * This returns the value of the $_schema_readonly property on the object. |
|
481 | + * |
|
482 | + * @return bool |
|
483 | + */ |
|
484 | + public function getSchemaReadonly() |
|
485 | + { |
|
486 | + return true; |
|
487 | + } |
|
488 | + |
|
489 | + /** |
|
490 | + * This returns elements used to represent this field in the json schema. |
|
491 | + * |
|
492 | + * @link http://json-schema.org/ |
|
493 | + * @return array |
|
494 | + */ |
|
495 | + public function getSchema() |
|
496 | + { |
|
497 | + $schema = array( |
|
498 | + 'description' => $this->getSchemaDescription(), |
|
499 | + 'type' => $this->getSchemaType(), |
|
500 | + 'relation' => true, |
|
501 | + 'relation_type' => get_class($this), |
|
502 | + 'readonly' => $this->getSchemaReadonly() |
|
503 | + ); |
|
504 | + |
|
505 | + if ($this instanceof EE_HABTM_Relation) { |
|
506 | + $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name(); |
|
507 | + } |
|
508 | + |
|
509 | + if ($this->getSchemaType() === 'array') { |
|
510 | + $schema['items'] = array( |
|
511 | + 'type' => 'object' |
|
512 | + ); |
|
513 | + } |
|
514 | + |
|
515 | + return $schema; |
|
516 | + } |
|
517 | 517 | } |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | $this_table_join_column, |
158 | 158 | $extra_join_sql = '' |
159 | 159 | ) { |
160 | - return " LEFT JOIN " . $other_table . " AS " . $other_table_alias . " ON " . $other_table_alias . "." . $other_table_column . "=" . $this_table_alias . "." . $this_table_join_column . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
160 | + return " LEFT JOIN ".$other_table." AS ".$other_table_alias." ON ".$other_table_alias.".".$other_table_column."=".$this_table_alias.".".$this_table_join_column.($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
161 | 161 | } |
162 | 162 | |
163 | 163 | |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | . "." |
192 | 192 | . $this->get_this_model()->get_primary_key_field()->get_name(); |
193 | 193 | $model_object_id = $this->_get_model_object_id($model_object_or_id); |
194 | - $query_params[0][ $query_param_where_this_model_pk ] = $model_object_id; |
|
194 | + $query_params[0][$query_param_where_this_model_pk] = $model_object_id; |
|
195 | 195 | return $this->get_other_model()->get_all($query_params); |
196 | 196 | } |
197 | 197 | |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | */ |
205 | 205 | protected function _disable_default_where_conditions_on_query_param($query_params) |
206 | 206 | { |
207 | - if (! isset($query_params['default_where_conditions'])) { |
|
207 | + if ( ! isset($query_params['default_where_conditions'])) { |
|
208 | 208 | $query_params['default_where_conditions'] = 'none'; |
209 | 209 | } |
210 | 210 | return $query_params; |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | $model_object_or_id |
235 | 235 | ); |
236 | 236 | /* @var $model_object_or_id EE_Base_Class */ |
237 | - if (! $delete_is_blocked) { |
|
237 | + if ( ! $delete_is_blocked) { |
|
238 | 238 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
239 | 239 | $related_model_object->delete(); |
240 | 240 | $deleted_count++; |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
271 | 271 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
272 | 272 | $deleted_count++; |
273 | - if (! $delete_is_blocked) { |
|
273 | + if ( ! $delete_is_blocked) { |
|
274 | 274 | $related_model_object->delete_permanently(); |
275 | 275 | } else { |
276 | 276 | // delete is blocked |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | } |
280 | 280 | } else { |
281 | 281 | // its not a soft-deletable thing anyways. do the normal logic. |
282 | - if (! $delete_is_blocked) { |
|
282 | + if ( ! $delete_is_blocked) { |
|
283 | 283 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
284 | 284 | $related_model_object->delete(); |
285 | 285 | $deleted_count++; |
@@ -303,7 +303,7 @@ discard block |
||
303 | 303 | if ($model_object_or_id instanceof EE_Base_Class) { |
304 | 304 | $model_object_id = $model_object_or_id->ID(); |
305 | 305 | } |
306 | - if (! $model_object_id) { |
|
306 | + if ( ! $model_object_id) { |
|
307 | 307 | throw new EE_Error(sprintf( |
308 | 308 | esc_html__( |
309 | 309 | "Sorry, we cant get the related %s model objects to %s model object before it has an ID. You can solve that by just saving it before trying to get its related model objects", |
@@ -112,26 +112,26 @@ discard block |
||
112 | 112 | public function prepare_where_conditions_for_querying($where_conditions, $model_relation_chain) |
113 | 113 | { |
114 | 114 | $where_conditions_with_model_relation_chain_prefixes = array(); |
115 | - if (! is_array($where_conditions)) { |
|
115 | + if ( ! is_array($where_conditions)) { |
|
116 | 116 | $where_conditions = array(); |
117 | 117 | } |
118 | 118 | foreach ($where_conditions as $key => $value) { |
119 | 119 | if ( |
120 | - in_array($key, array( 'OR', 'AND', 'NOT' )) |
|
120 | + in_array($key, array('OR', 'AND', 'NOT')) |
|
121 | 121 | || strpos($key, 'OR*') !== false |
122 | 122 | || strpos($key, 'AND*') !== false |
123 | 123 | || strpos($key, 'NOT*') !== false |
124 | 124 | ) { |
125 | - $where_conditions_with_model_relation_chain_prefixes[ $key ] = $this->prepare_where_conditions_for_querying( |
|
125 | + $where_conditions_with_model_relation_chain_prefixes[$key] = $this->prepare_where_conditions_for_querying( |
|
126 | 126 | $value, |
127 | 127 | $model_relation_chain |
128 | 128 | ); |
129 | 129 | } else { |
130 | 130 | if ( |
131 | 131 | $model_relation_chain != '' |
132 | - && $model_relation_chain[ strlen($model_relation_chain) - 1 ] != '.' |
|
132 | + && $model_relation_chain[strlen($model_relation_chain) - 1] != '.' |
|
133 | 133 | ) { |
134 | - $model_relation_chain = $model_relation_chain . "."; |
|
134 | + $model_relation_chain = $model_relation_chain."."; |
|
135 | 135 | } |
136 | 136 | // check for the current user id place holder, and if present change it |
137 | 137 | if ($value === self::current_user_placeholder) { |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | } |
140 | 140 | // check for user field placeholder |
141 | 141 | if ($key == self::user_field_name_placeholder) { |
142 | - if (! $this->_model->wp_user_field_name()) { |
|
142 | + if ( ! $this->_model->wp_user_field_name()) { |
|
143 | 143 | throw new EE_Error( |
144 | 144 | sprintf( |
145 | 145 | esc_html__( |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | } |
153 | 153 | $key = $this->_model->wp_user_field_name(); |
154 | 154 | } |
155 | - $where_conditions_with_model_relation_chain_prefixes[ $model_relation_chain . $key ] = $value; |
|
155 | + $where_conditions_with_model_relation_chain_prefixes[$model_relation_chain.$key] = $value; |
|
156 | 156 | } |
157 | 157 | } |
158 | 158 | return $where_conditions_with_model_relation_chain_prefixes; |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | /** |
4 | 4 | * |
5 | 5 | * Class EE_Default_Where_Conditions |
6 | - * |
|
6 | + * |
|
7 | 7 | * Strategy to be used for getting default where conditions for EEM_Base children. |
8 | 8 | * Should be initialized and set on construction of model |
9 | 9 | * |
@@ -15,145 +15,145 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class EE_Default_Where_Conditions |
17 | 17 | { |
18 | - /** |
|
19 | - * This const can be used in EE_Default_Where_Conditions values, and at the time of querying it will be |
|
20 | - * replaced with the current user's ID (because we don't want to use the current user's ID at time of |
|
21 | - * initializing the models because it's too early) |
|
22 | - */ |
|
23 | - const current_user_placeholder = '%$current_user_placeholder_should_be_replaced_automatically$%'; |
|
24 | - |
|
25 | - /** |
|
26 | - * This const can be used in EE_Default_Where_Conditions where parameters as the name |
|
27 | - * of the user field. When we are actually generating the where conditions it will be |
|
28 | - * replaced with the model's wp user fieldname |
|
29 | - */ |
|
30 | - const user_field_name_placeholder = '%$user_field_name_placeholder$%'; |
|
31 | - |
|
32 | - /** |
|
33 | - * Model for which this strategy find default where conditions |
|
34 | - * @var EEM_Base |
|
35 | - */ |
|
36 | - protected $_model; |
|
37 | - |
|
38 | - /** |
|
39 | - * Where conditions specified on construction |
|
40 | - * @var array |
|
41 | - */ |
|
42 | - protected $_where_conditions_provided = array(); |
|
43 | - |
|
44 | - /** |
|
45 | - * Custom where conditions. Model relation chains will be automatically added |
|
46 | - * onto any field names |
|
47 | - * @param array $custom_where_conditions |
|
48 | - */ |
|
49 | - public function __construct($custom_where_conditions = array()) |
|
50 | - { |
|
51 | - $this->_where_conditions_provided = $custom_where_conditions; |
|
52 | - } |
|
53 | - |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * finalizes construction of the strategy for use in getting default where conditions |
|
58 | - * for querying of the model. |
|
59 | - * @param EEM_Base $model |
|
60 | - */ |
|
61 | - public function _finalize_construct(EEM_Base $model) |
|
62 | - { |
|
63 | - $this->_model = $model; |
|
64 | - } |
|
65 | - |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * Returns the where conditions explicitly passed in the constructor |
|
70 | - * @return array |
|
71 | - */ |
|
72 | - public function get_where_conditions_provided() |
|
73 | - { |
|
74 | - return $this->_where_conditions_provided; |
|
75 | - } |
|
76 | - |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * Gets the where conditions to be added onto the query |
|
81 | - * @param string $model_relation_chain |
|
82 | - * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
83 | - */ |
|
84 | - public function get_default_where_conditions($model_relation_chain = '') |
|
85 | - { |
|
86 | - return $this->prepare_where_conditions_for_querying(array_merge($this->_get_default_where_conditions(), $this->get_where_conditions_provided()), $model_relation_chain); |
|
87 | - } |
|
88 | - |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * Gets the default where conditions that are specific to this child of EE_Default_Where_Conditions. |
|
93 | - * Adding model relation chains is handled by the public method get_default_where_conditions |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - protected function _get_default_where_conditions() |
|
97 | - { |
|
98 | - return array(); |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - |
|
103 | - /** |
|
104 | - * Takes the default query parameters, and traverses them, adding the model relation chain |
|
105 | - * onto them (intelligently doesn't do that to logic query params like NOT, OR, and AND) |
|
106 | - * @param array $where_conditions |
|
107 | - * @param string $model_relation_chain |
|
108 | - * @return array |
|
109 | - * @throws \EE_Error |
|
110 | - */ |
|
111 | - public function prepare_where_conditions_for_querying($where_conditions, $model_relation_chain) |
|
112 | - { |
|
113 | - $where_conditions_with_model_relation_chain_prefixes = array(); |
|
114 | - if (! is_array($where_conditions)) { |
|
115 | - $where_conditions = array(); |
|
116 | - } |
|
117 | - foreach ($where_conditions as $key => $value) { |
|
118 | - if ( |
|
119 | - in_array($key, array( 'OR', 'AND', 'NOT' )) |
|
120 | - || strpos($key, 'OR*') !== false |
|
121 | - || strpos($key, 'AND*') !== false |
|
122 | - || strpos($key, 'NOT*') !== false |
|
123 | - ) { |
|
124 | - $where_conditions_with_model_relation_chain_prefixes[ $key ] = $this->prepare_where_conditions_for_querying( |
|
125 | - $value, |
|
126 | - $model_relation_chain |
|
127 | - ); |
|
128 | - } else { |
|
129 | - if ( |
|
130 | - $model_relation_chain != '' |
|
131 | - && $model_relation_chain[ strlen($model_relation_chain) - 1 ] != '.' |
|
132 | - ) { |
|
133 | - $model_relation_chain = $model_relation_chain . "."; |
|
134 | - } |
|
135 | - // check for the current user id place holder, and if present change it |
|
136 | - if ($value === self::current_user_placeholder) { |
|
137 | - $value = get_current_user_id(); |
|
138 | - } |
|
139 | - // check for user field placeholder |
|
140 | - if ($key == self::user_field_name_placeholder) { |
|
141 | - if (! $this->_model->wp_user_field_name()) { |
|
142 | - throw new EE_Error( |
|
143 | - sprintf( |
|
144 | - esc_html__( |
|
145 | - 'There is no foreign key to the WP_User model on model %s. Please either modify your default where conditions, add a _model_chain_to_wp_user onto the model, or a proper EE_WP_User_Field onto the model', |
|
146 | - 'event_espresso' |
|
147 | - ), |
|
148 | - $this->_model->get_this_model_name() |
|
149 | - ) |
|
150 | - ); |
|
151 | - } |
|
152 | - $key = $this->_model->wp_user_field_name(); |
|
153 | - } |
|
154 | - $where_conditions_with_model_relation_chain_prefixes[ $model_relation_chain . $key ] = $value; |
|
155 | - } |
|
156 | - } |
|
157 | - return $where_conditions_with_model_relation_chain_prefixes; |
|
158 | - } |
|
18 | + /** |
|
19 | + * This const can be used in EE_Default_Where_Conditions values, and at the time of querying it will be |
|
20 | + * replaced with the current user's ID (because we don't want to use the current user's ID at time of |
|
21 | + * initializing the models because it's too early) |
|
22 | + */ |
|
23 | + const current_user_placeholder = '%$current_user_placeholder_should_be_replaced_automatically$%'; |
|
24 | + |
|
25 | + /** |
|
26 | + * This const can be used in EE_Default_Where_Conditions where parameters as the name |
|
27 | + * of the user field. When we are actually generating the where conditions it will be |
|
28 | + * replaced with the model's wp user fieldname |
|
29 | + */ |
|
30 | + const user_field_name_placeholder = '%$user_field_name_placeholder$%'; |
|
31 | + |
|
32 | + /** |
|
33 | + * Model for which this strategy find default where conditions |
|
34 | + * @var EEM_Base |
|
35 | + */ |
|
36 | + protected $_model; |
|
37 | + |
|
38 | + /** |
|
39 | + * Where conditions specified on construction |
|
40 | + * @var array |
|
41 | + */ |
|
42 | + protected $_where_conditions_provided = array(); |
|
43 | + |
|
44 | + /** |
|
45 | + * Custom where conditions. Model relation chains will be automatically added |
|
46 | + * onto any field names |
|
47 | + * @param array $custom_where_conditions |
|
48 | + */ |
|
49 | + public function __construct($custom_where_conditions = array()) |
|
50 | + { |
|
51 | + $this->_where_conditions_provided = $custom_where_conditions; |
|
52 | + } |
|
53 | + |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * finalizes construction of the strategy for use in getting default where conditions |
|
58 | + * for querying of the model. |
|
59 | + * @param EEM_Base $model |
|
60 | + */ |
|
61 | + public function _finalize_construct(EEM_Base $model) |
|
62 | + { |
|
63 | + $this->_model = $model; |
|
64 | + } |
|
65 | + |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * Returns the where conditions explicitly passed in the constructor |
|
70 | + * @return array |
|
71 | + */ |
|
72 | + public function get_where_conditions_provided() |
|
73 | + { |
|
74 | + return $this->_where_conditions_provided; |
|
75 | + } |
|
76 | + |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * Gets the where conditions to be added onto the query |
|
81 | + * @param string $model_relation_chain |
|
82 | + * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
83 | + */ |
|
84 | + public function get_default_where_conditions($model_relation_chain = '') |
|
85 | + { |
|
86 | + return $this->prepare_where_conditions_for_querying(array_merge($this->_get_default_where_conditions(), $this->get_where_conditions_provided()), $model_relation_chain); |
|
87 | + } |
|
88 | + |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * Gets the default where conditions that are specific to this child of EE_Default_Where_Conditions. |
|
93 | + * Adding model relation chains is handled by the public method get_default_where_conditions |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + protected function _get_default_where_conditions() |
|
97 | + { |
|
98 | + return array(); |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + |
|
103 | + /** |
|
104 | + * Takes the default query parameters, and traverses them, adding the model relation chain |
|
105 | + * onto them (intelligently doesn't do that to logic query params like NOT, OR, and AND) |
|
106 | + * @param array $where_conditions |
|
107 | + * @param string $model_relation_chain |
|
108 | + * @return array |
|
109 | + * @throws \EE_Error |
|
110 | + */ |
|
111 | + public function prepare_where_conditions_for_querying($where_conditions, $model_relation_chain) |
|
112 | + { |
|
113 | + $where_conditions_with_model_relation_chain_prefixes = array(); |
|
114 | + if (! is_array($where_conditions)) { |
|
115 | + $where_conditions = array(); |
|
116 | + } |
|
117 | + foreach ($where_conditions as $key => $value) { |
|
118 | + if ( |
|
119 | + in_array($key, array( 'OR', 'AND', 'NOT' )) |
|
120 | + || strpos($key, 'OR*') !== false |
|
121 | + || strpos($key, 'AND*') !== false |
|
122 | + || strpos($key, 'NOT*') !== false |
|
123 | + ) { |
|
124 | + $where_conditions_with_model_relation_chain_prefixes[ $key ] = $this->prepare_where_conditions_for_querying( |
|
125 | + $value, |
|
126 | + $model_relation_chain |
|
127 | + ); |
|
128 | + } else { |
|
129 | + if ( |
|
130 | + $model_relation_chain != '' |
|
131 | + && $model_relation_chain[ strlen($model_relation_chain) - 1 ] != '.' |
|
132 | + ) { |
|
133 | + $model_relation_chain = $model_relation_chain . "."; |
|
134 | + } |
|
135 | + // check for the current user id place holder, and if present change it |
|
136 | + if ($value === self::current_user_placeholder) { |
|
137 | + $value = get_current_user_id(); |
|
138 | + } |
|
139 | + // check for user field placeholder |
|
140 | + if ($key == self::user_field_name_placeholder) { |
|
141 | + if (! $this->_model->wp_user_field_name()) { |
|
142 | + throw new EE_Error( |
|
143 | + sprintf( |
|
144 | + esc_html__( |
|
145 | + 'There is no foreign key to the WP_User model on model %s. Please either modify your default where conditions, add a _model_chain_to_wp_user onto the model, or a proper EE_WP_User_Field onto the model', |
|
146 | + 'event_espresso' |
|
147 | + ), |
|
148 | + $this->_model->get_this_model_name() |
|
149 | + ) |
|
150 | + ); |
|
151 | + } |
|
152 | + $key = $this->_model->wp_user_field_name(); |
|
153 | + } |
|
154 | + $where_conditions_with_model_relation_chain_prefixes[ $model_relation_chain . $key ] = $value; |
|
155 | + } |
|
156 | + } |
|
157 | + return $where_conditions_with_model_relation_chain_prefixes; |
|
158 | + } |
|
159 | 159 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function model() |
67 | 67 | { |
68 | - if (! $this->_model instanceof EEM_Base) { |
|
68 | + if ( ! $this->_model instanceof EEM_Base) { |
|
69 | 69 | throw new EE_Error(sprintf(esc_html__('Cannot generate capability restrictions because model has not yet been set on the %s. Please ensure _construct_finalize() was called', 'event_espresso'), get_class($this))); |
70 | 70 | } |
71 | 71 | return $this->_model; |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | */ |
81 | 81 | public function action() |
82 | 82 | { |
83 | - if (! $this->_action) { |
|
83 | + if ( ! $this->_action) { |
|
84 | 84 | throw new EE_Error(sprintf(esc_html__('Cannot generate capability restrictions because model has not yet been set on the %s. Please ensure _construct_finalize() was called', 'event_espresso'), get_class($this))); |
85 | 85 | } |
86 | 86 | return $this->_action; |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | */ |
93 | 93 | public function construction_finalized() |
94 | 94 | { |
95 | - if ($this->_model instanceof EEM_Base && $this->_action) { |
|
95 | + if ($this->_model instanceof EEM_Base && $this->_action) { |
|
96 | 96 | return true; |
97 | 97 | } else { |
98 | 98 | return false; |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | */ |
146 | 146 | public static function get_cap_name($model, $action) |
147 | 147 | { |
148 | - return apply_filters('FHEE__EE_Restriction_Generator__get_cap_name', ( $model->is_wp_core_model() ? '' : 'ee_' ) . $action . '_' . $model->cap_slug(), $model, $action); |
|
148 | + return apply_filters('FHEE__EE_Restriction_Generator__get_cap_name', ($model->is_wp_core_model() ? '' : 'ee_').$action.'_'.$model->cap_slug(), $model, $action); |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | /** |
@@ -189,19 +189,19 @@ discard block |
||
189 | 189 | if ($check_if_published) { |
190 | 190 | $published_value = 'publish'; |
191 | 191 | } else { |
192 | - $published_value = array('!=','private'); |
|
192 | + $published_value = array('!=', 'private'); |
|
193 | 193 | } |
194 | 194 | // only add a check for the previous event status if the model is the event or it's related to the event model |
195 | 195 | if ($this->model() instanceof EEM_Event || strpos($path_to_event_model, 'Event') !== false) { |
196 | 196 | $where_conditions['OR*status'] = array( |
197 | - $path_to_event_model . 'status' => $published_value, |
|
197 | + $path_to_event_model.'status' => $published_value, |
|
198 | 198 | 'AND' => array( |
199 | - $path_to_event_model . 'Post_Meta.meta_key' => '_previous_event_status', |
|
200 | - $path_to_event_model . 'Post_Meta.meta_value' => $published_value |
|
199 | + $path_to_event_model.'Post_Meta.meta_key' => '_previous_event_status', |
|
200 | + $path_to_event_model.'Post_Meta.meta_value' => $published_value |
|
201 | 201 | ) |
202 | 202 | ); |
203 | 203 | } else { |
204 | - $where_conditions[ $path_to_event_model . 'status' ] = $published_value; |
|
204 | + $where_conditions[$path_to_event_model.'status'] = $published_value; |
|
205 | 205 | } |
206 | 206 | return $where_conditions; |
207 | 207 | } |
@@ -26,182 +26,182 @@ |
||
26 | 26 | */ |
27 | 27 | abstract class EE_Restriction_Generator_Base |
28 | 28 | { |
29 | - /** |
|
30 | - * The restrictions generated by this object. FALSE before any are made. |
|
31 | - * @var EE_Default_Where_Conditions[] |
|
32 | - */ |
|
33 | - protected $_cap_restrictions_generated = false; |
|
29 | + /** |
|
30 | + * The restrictions generated by this object. FALSE before any are made. |
|
31 | + * @var EE_Default_Where_Conditions[] |
|
32 | + */ |
|
33 | + protected $_cap_restrictions_generated = false; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Model for which restrictions are generated |
|
37 | - * @var EEM_Base |
|
38 | - */ |
|
39 | - protected $_model; |
|
35 | + /** |
|
36 | + * Model for which restrictions are generated |
|
37 | + * @var EEM_Base |
|
38 | + */ |
|
39 | + protected $_model; |
|
40 | 40 | |
41 | - /** |
|
42 | - * One of EEM_Base::valid_cap_contexts() |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - protected $_action; |
|
46 | - /** |
|
47 | - * Puts the last necessary info onto the restriction generator class. This is usually |
|
48 | - * called by EEM_Base during its constructor, so child classes don't have to |
|
49 | - * always provide this info. |
|
50 | - * @param EEM_Base $model |
|
51 | - * @param string $action |
|
52 | - */ |
|
53 | - public function _construct_finalize(EEM_Base $model, $action) |
|
54 | - { |
|
55 | - $this->_model = $model; |
|
56 | - $this->_action = $action; |
|
57 | - } |
|
41 | + /** |
|
42 | + * One of EEM_Base::valid_cap_contexts() |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + protected $_action; |
|
46 | + /** |
|
47 | + * Puts the last necessary info onto the restriction generator class. This is usually |
|
48 | + * called by EEM_Base during its constructor, so child classes don't have to |
|
49 | + * always provide this info. |
|
50 | + * @param EEM_Base $model |
|
51 | + * @param string $action |
|
52 | + */ |
|
53 | + public function _construct_finalize(EEM_Base $model, $action) |
|
54 | + { |
|
55 | + $this->_model = $model; |
|
56 | + $this->_action = $action; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Returns the model set for this restriction generator |
|
61 | - * |
|
62 | - * @throws EE_Error |
|
63 | - * @return EEM_Base | EEM_Soft_Delete_Base |
|
64 | - */ |
|
65 | - public function model() |
|
66 | - { |
|
67 | - if (! $this->_model instanceof EEM_Base) { |
|
68 | - throw new EE_Error(sprintf(esc_html__('Cannot generate capability restrictions because model has not yet been set on the %s. Please ensure _construct_finalize() was called', 'event_espresso'), get_class($this))); |
|
69 | - } |
|
70 | - return $this->_model; |
|
71 | - } |
|
59 | + /** |
|
60 | + * Returns the model set for this restriction generator |
|
61 | + * |
|
62 | + * @throws EE_Error |
|
63 | + * @return EEM_Base | EEM_Soft_Delete_Base |
|
64 | + */ |
|
65 | + public function model() |
|
66 | + { |
|
67 | + if (! $this->_model instanceof EEM_Base) { |
|
68 | + throw new EE_Error(sprintf(esc_html__('Cannot generate capability restrictions because model has not yet been set on the %s. Please ensure _construct_finalize() was called', 'event_espresso'), get_class($this))); |
|
69 | + } |
|
70 | + return $this->_model; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Returns the action this restriction generator will generate restrictions for. |
|
75 | - * It should be one of EEM_Base::valid_cap_contexts() |
|
76 | - * |
|
77 | - * @throws EE_Error |
|
78 | - * @return string |
|
79 | - */ |
|
80 | - public function action() |
|
81 | - { |
|
82 | - if (! $this->_action) { |
|
83 | - throw new EE_Error(sprintf(esc_html__('Cannot generate capability restrictions because model has not yet been set on the %s. Please ensure _construct_finalize() was called', 'event_espresso'), get_class($this))); |
|
84 | - } |
|
85 | - return $this->_action; |
|
86 | - } |
|
73 | + /** |
|
74 | + * Returns the action this restriction generator will generate restrictions for. |
|
75 | + * It should be one of EEM_Base::valid_cap_contexts() |
|
76 | + * |
|
77 | + * @throws EE_Error |
|
78 | + * @return string |
|
79 | + */ |
|
80 | + public function action() |
|
81 | + { |
|
82 | + if (! $this->_action) { |
|
83 | + throw new EE_Error(sprintf(esc_html__('Cannot generate capability restrictions because model has not yet been set on the %s. Please ensure _construct_finalize() was called', 'event_espresso'), get_class($this))); |
|
84 | + } |
|
85 | + return $this->_action; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Returns whether or not _construct_finalize() has been called on this restriction generator object |
|
90 | - * @return boolean |
|
91 | - */ |
|
92 | - public function construction_finalized() |
|
93 | - { |
|
94 | - if ($this->_model instanceof EEM_Base && $this->_action) { |
|
95 | - return true; |
|
96 | - } else { |
|
97 | - return false; |
|
98 | - } |
|
99 | - } |
|
88 | + /** |
|
89 | + * Returns whether or not _construct_finalize() has been called on this restriction generator object |
|
90 | + * @return boolean |
|
91 | + */ |
|
92 | + public function construction_finalized() |
|
93 | + { |
|
94 | + if ($this->_model instanceof EEM_Base && $this->_action) { |
|
95 | + return true; |
|
96 | + } else { |
|
97 | + return false; |
|
98 | + } |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Gets the capability restrictions generated by this object. Caches them in |
|
103 | - * case they are required for subsequent requests |
|
104 | - * @return array @see EEM_Base::_cap_restrictions |
|
105 | - */ |
|
106 | - public function generate_restrictions() |
|
107 | - { |
|
108 | - if ($this->_cap_restrictions_generated === false) { |
|
109 | - $this->_cap_restrictions_generated = apply_filters('FHEE__EE_Restriction_Generator_Base__generate_restrictions__first_time', $this->_generate_restrictions(), $this); |
|
110 | - } |
|
111 | - return apply_filters('FHEE__EE_Restriction_Generator_Base__generate_restrictions__every_time', $this->_cap_restrictions_generated, $this); |
|
112 | - } |
|
101 | + /** |
|
102 | + * Gets the capability restrictions generated by this object. Caches them in |
|
103 | + * case they are required for subsequent requests |
|
104 | + * @return array @see EEM_Base::_cap_restrictions |
|
105 | + */ |
|
106 | + public function generate_restrictions() |
|
107 | + { |
|
108 | + if ($this->_cap_restrictions_generated === false) { |
|
109 | + $this->_cap_restrictions_generated = apply_filters('FHEE__EE_Restriction_Generator_Base__generate_restrictions__first_time', $this->_generate_restrictions(), $this); |
|
110 | + } |
|
111 | + return apply_filters('FHEE__EE_Restriction_Generator_Base__generate_restrictions__every_time', $this->_cap_restrictions_generated, $this); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Provided with the model, and using global knowledge about what capabilities exist, |
|
116 | - * generates an array for use in one of the sub-arrays in EEM_Base::_cap_restrictions, |
|
117 | - * where keys are capability names, and values are children of EE_Default_Where_Conditions |
|
118 | - * @return array @see EEM_Base::_cap_restrictions |
|
119 | - */ |
|
120 | - abstract protected function _generate_restrictions(); |
|
114 | + /** |
|
115 | + * Provided with the model, and using global knowledge about what capabilities exist, |
|
116 | + * generates an array for use in one of the sub-arrays in EEM_Base::_cap_restrictions, |
|
117 | + * where keys are capability names, and values are children of EE_Default_Where_Conditions |
|
118 | + * @return array @see EEM_Base::_cap_restrictions |
|
119 | + */ |
|
120 | + abstract protected function _generate_restrictions(); |
|
121 | 121 | |
122 | - /** |
|
123 | - * Whether or not this restriction generator has already done its job of |
|
124 | - * making restrictions and caching them on itself in case its asked later |
|
125 | - * @return boolean |
|
126 | - */ |
|
127 | - public function has_generated_cap_restrictions() |
|
128 | - { |
|
129 | - if ($this->_cap_restrictions_generated === false) { |
|
130 | - return false; |
|
131 | - } else { |
|
132 | - return true; |
|
133 | - } |
|
134 | - } |
|
122 | + /** |
|
123 | + * Whether or not this restriction generator has already done its job of |
|
124 | + * making restrictions and caching them on itself in case its asked later |
|
125 | + * @return boolean |
|
126 | + */ |
|
127 | + public function has_generated_cap_restrictions() |
|
128 | + { |
|
129 | + if ($this->_cap_restrictions_generated === false) { |
|
130 | + return false; |
|
131 | + } else { |
|
132 | + return true; |
|
133 | + } |
|
134 | + } |
|
135 | 135 | |
136 | - /** |
|
137 | - * Given an action like 'edit' generates the cap name based off |
|
138 | - * the EEM_Base::_cap_slug, which for events would be 'events', to generate the |
|
139 | - * cap name like 'ee_edit_events'. |
|
140 | - * If a $qualifier is passed, |
|
141 | - * @param EEM_Base $model |
|
142 | - * @param string $action |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public static function get_cap_name($model, $action) |
|
146 | - { |
|
147 | - return apply_filters('FHEE__EE_Restriction_Generator__get_cap_name', ( $model->is_wp_core_model() ? '' : 'ee_' ) . $action . '_' . $model->cap_slug(), $model, $action); |
|
148 | - } |
|
136 | + /** |
|
137 | + * Given an action like 'edit' generates the cap name based off |
|
138 | + * the EEM_Base::_cap_slug, which for events would be 'events', to generate the |
|
139 | + * cap name like 'ee_edit_events'. |
|
140 | + * If a $qualifier is passed, |
|
141 | + * @param EEM_Base $model |
|
142 | + * @param string $action |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public static function get_cap_name($model, $action) |
|
146 | + { |
|
147 | + return apply_filters('FHEE__EE_Restriction_Generator__get_cap_name', ( $model->is_wp_core_model() ? '' : 'ee_' ) . $action . '_' . $model->cap_slug(), $model, $action); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Checks if there is a cap for this model and this action |
|
152 | - * @param EEM_Base $model |
|
153 | - * @param string $action |
|
154 | - * @return boolean |
|
155 | - */ |
|
156 | - public static function is_cap($model, $action) |
|
157 | - { |
|
158 | - $caps_for_admin = EE_Registry::instance()->CAP->get_ee_capabilities('administrator'); |
|
159 | - if (in_array(self::get_cap_name($model, $action), $caps_for_admin)) { |
|
160 | - return true; |
|
161 | - } else { |
|
162 | - return false; |
|
163 | - } |
|
164 | - } |
|
150 | + /** |
|
151 | + * Checks if there is a cap for this model and this action |
|
152 | + * @param EEM_Base $model |
|
153 | + * @param string $action |
|
154 | + * @return boolean |
|
155 | + */ |
|
156 | + public static function is_cap($model, $action) |
|
157 | + { |
|
158 | + $caps_for_admin = EE_Registry::instance()->CAP->get_ee_capabilities('administrator'); |
|
159 | + if (in_array(self::get_cap_name($model, $action), $caps_for_admin)) { |
|
160 | + return true; |
|
161 | + } else { |
|
162 | + return false; |
|
163 | + } |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * Returns the default capability used to determine if the current user can |
|
168 | - * access something. |
|
169 | - * @return string |
|
170 | - */ |
|
171 | - public static function get_default_restrictions_cap() |
|
172 | - { |
|
173 | - return apply_filters('FHEE__EE_Restriction_Generator_Base__default_restrictions_cap', 'manage_options'); |
|
174 | - } |
|
166 | + /** |
|
167 | + * Returns the default capability used to determine if the current user can |
|
168 | + * access something. |
|
169 | + * @return string |
|
170 | + */ |
|
171 | + public static function get_default_restrictions_cap() |
|
172 | + { |
|
173 | + return apply_filters('FHEE__EE_Restriction_Generator_Base__default_restrictions_cap', 'manage_options'); |
|
174 | + } |
|
175 | 175 | |
176 | 176 | |
177 | - /** |
|
178 | - * Gets WHERE conditions for the query that show the posty-y model is published, |
|
179 | - * or that it's sold out and it was previously published |
|
180 | - * @param array $where_conditions |
|
181 | - * @param boolean $check_if_published if true, will add conditions like status=publish. If false, will add conditions |
|
182 | - * like status!=private |
|
183 | - * @param string $path_to_event_model including the period at the end |
|
184 | - * @return array |
|
185 | - */ |
|
186 | - protected function addPublishedPostConditions($where_conditions = array(), $check_if_published = true, $path_to_event_model = '') |
|
187 | - { |
|
188 | - if ($check_if_published) { |
|
189 | - $published_value = 'publish'; |
|
190 | - } else { |
|
191 | - $published_value = array('!=','private'); |
|
192 | - } |
|
193 | - // only add a check for the previous event status if the model is the event or it's related to the event model |
|
194 | - if ($this->model() instanceof EEM_Event || strpos($path_to_event_model, 'Event') !== false) { |
|
195 | - $where_conditions['OR*status'] = array( |
|
196 | - $path_to_event_model . 'status' => $published_value, |
|
197 | - 'AND' => array( |
|
198 | - $path_to_event_model . 'Post_Meta.meta_key' => '_previous_event_status', |
|
199 | - $path_to_event_model . 'Post_Meta.meta_value' => $published_value |
|
200 | - ) |
|
201 | - ); |
|
202 | - } else { |
|
203 | - $where_conditions[ $path_to_event_model . 'status' ] = $published_value; |
|
204 | - } |
|
205 | - return $where_conditions; |
|
206 | - } |
|
177 | + /** |
|
178 | + * Gets WHERE conditions for the query that show the posty-y model is published, |
|
179 | + * or that it's sold out and it was previously published |
|
180 | + * @param array $where_conditions |
|
181 | + * @param boolean $check_if_published if true, will add conditions like status=publish. If false, will add conditions |
|
182 | + * like status!=private |
|
183 | + * @param string $path_to_event_model including the period at the end |
|
184 | + * @return array |
|
185 | + */ |
|
186 | + protected function addPublishedPostConditions($where_conditions = array(), $check_if_published = true, $path_to_event_model = '') |
|
187 | + { |
|
188 | + if ($check_if_published) { |
|
189 | + $published_value = 'publish'; |
|
190 | + } else { |
|
191 | + $published_value = array('!=','private'); |
|
192 | + } |
|
193 | + // only add a check for the previous event status if the model is the event or it's related to the event model |
|
194 | + if ($this->model() instanceof EEM_Event || strpos($path_to_event_model, 'Event') !== false) { |
|
195 | + $where_conditions['OR*status'] = array( |
|
196 | + $path_to_event_model . 'status' => $published_value, |
|
197 | + 'AND' => array( |
|
198 | + $path_to_event_model . 'Post_Meta.meta_key' => '_previous_event_status', |
|
199 | + $path_to_event_model . 'Post_Meta.meta_value' => $published_value |
|
200 | + ) |
|
201 | + ); |
|
202 | + } else { |
|
203 | + $where_conditions[ $path_to_event_model . 'status' ] = $published_value; |
|
204 | + } |
|
205 | + return $where_conditions; |
|
206 | + } |
|
207 | 207 | } |
@@ -13,41 +13,41 @@ |
||
13 | 13 | */ |
14 | 14 | class EE_Soft_Delete_Where_Conditions extends EE_Default_Where_Conditions |
15 | 15 | { |
16 | - /** |
|
17 | - * Strategy for setting default soft delete where conditions. This strategy will find |
|
18 | - * the field of type 'EE_Trashed_Flag_Field', and add a condition that it be FALSE on all queries involving |
|
19 | - * the model. |
|
20 | - * If you want to override these default where conditions, you may explicitly in the query you send to the model. |
|
21 | - * Eg, |
|
22 | - * |
|
23 | - */ |
|
24 | - public function __construct() |
|
25 | - { |
|
26 | - } |
|
27 | - /** |
|
28 | - * Gets the where default where conditions for a custom post type model |
|
29 | - * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
30 | - */ |
|
31 | - protected function _get_default_where_conditions() |
|
32 | - { |
|
33 | - $trashed_field_name = $this->deleted_field_name(); |
|
34 | - return array( |
|
35 | - $trashed_field_name => false |
|
36 | - ); |
|
37 | - } |
|
38 | - /** |
|
39 | - * Searches for field on the model of type 'deleted_flag'. if it is found, |
|
40 | - * returns it's name. |
|
41 | - * @return string |
|
42 | - * @throws EE_Error |
|
43 | - */ |
|
44 | - private function deleted_field_name() |
|
45 | - { |
|
46 | - $field = $this->_model->get_a_field_of_type('EE_Trashed_Flag_Field'); |
|
47 | - if ($field) { |
|
48 | - return $field->get_name(); |
|
49 | - } else { |
|
50 | - throw new EE_Error(sprintf(esc_html__('We are trying to find the deleted flag field on %s, but none was found. Are you sure there is a field of type EE_Trashed_Flag_Field in %s constructor?', 'event_espresso'), get_class($this), get_class($this))); |
|
51 | - } |
|
52 | - } |
|
16 | + /** |
|
17 | + * Strategy for setting default soft delete where conditions. This strategy will find |
|
18 | + * the field of type 'EE_Trashed_Flag_Field', and add a condition that it be FALSE on all queries involving |
|
19 | + * the model. |
|
20 | + * If you want to override these default where conditions, you may explicitly in the query you send to the model. |
|
21 | + * Eg, |
|
22 | + * |
|
23 | + */ |
|
24 | + public function __construct() |
|
25 | + { |
|
26 | + } |
|
27 | + /** |
|
28 | + * Gets the where default where conditions for a custom post type model |
|
29 | + * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
30 | + */ |
|
31 | + protected function _get_default_where_conditions() |
|
32 | + { |
|
33 | + $trashed_field_name = $this->deleted_field_name(); |
|
34 | + return array( |
|
35 | + $trashed_field_name => false |
|
36 | + ); |
|
37 | + } |
|
38 | + /** |
|
39 | + * Searches for field on the model of type 'deleted_flag'. if it is found, |
|
40 | + * returns it's name. |
|
41 | + * @return string |
|
42 | + * @throws EE_Error |
|
43 | + */ |
|
44 | + private function deleted_field_name() |
|
45 | + { |
|
46 | + $field = $this->_model->get_a_field_of_type('EE_Trashed_Flag_Field'); |
|
47 | + if ($field) { |
|
48 | + return $field->get_name(); |
|
49 | + } else { |
|
50 | + throw new EE_Error(sprintf(esc_html__('We are trying to find the deleted flag field on %s, but none was found. Are you sure there is a field of type EE_Trashed_Flag_Field in %s constructor?', 'event_espresso'), get_class($this), get_class($this))); |
|
51 | + } |
|
52 | + } |
|
53 | 53 | } |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | */ |
43 | 43 | public function get_base_type_name($base_type_int) |
44 | 44 | { |
45 | - return $this->base_types[ $base_type_int ]; |
|
45 | + return $this->base_types[$base_type_int]; |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | /** |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | 'WP_User' => new EE_Belongs_To_Relation(), |
96 | 96 | ); |
97 | 97 | // this model is generally available for reading |
98 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
98 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Public(); |
|
99 | 99 | // all price types are "default" in terms of capability names |
100 | 100 | $this->_caps_slug = 'default_price_types'; |
101 | 101 | parent::__construct($timezone); |
@@ -142,15 +142,15 @@ discard block |
||
142 | 142 | $would_be_deleted_price_types = $this->get_all_deleted_and_undeleted($query_params); |
143 | 143 | $would_be_deleted_price_type_ids = array_keys($would_be_deleted_price_types); |
144 | 144 | |
145 | - $ID = $query_params[0][ $this->get_primary_key_field()->get_name() ]; |
|
145 | + $ID = $query_params[0][$this->get_primary_key_field()->get_name()]; |
|
146 | 146 | |
147 | 147 | // check if any prices use this price type |
148 | - $prc_query_params = array(array('PRT_ID' => array('IN',$would_be_deleted_price_type_ids))); |
|
148 | + $prc_query_params = array(array('PRT_ID' => array('IN', $would_be_deleted_price_type_ids))); |
|
149 | 149 | if ($prices = $this->get_all_related($ID, 'Price', $prc_query_params)) { |
150 | 150 | $prices_names_and_ids = array(); |
151 | 151 | foreach ($prices as $price) { |
152 | 152 | /* @var $price EE_Price */ |
153 | - $prices_names_and_ids[] = $price->name() . "(" . $price->ID() . ")"; |
|
153 | + $prices_names_and_ids[] = $price->name()."(".$price->ID().")"; |
|
154 | 154 | } |
155 | 155 | $msg = sprintf(esc_html__('The Price Type(s) could not be deleted because there are existing Prices that currently use this Price Type. If you still wish to delete this Price Type, then either delete those Prices or change them to use other Price Types.The prices are: %s', 'event_espresso'), implode(",", $prices_names_and_ids)); |
156 | 156 | EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
@@ -11,108 +11,108 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class EEM_Price_Type extends EEM_Soft_Delete_Base |
13 | 13 | { |
14 | - // private instance of the Price Type object |
|
15 | - protected static $_instance = null; |
|
16 | - // An array of the price type objects |
|
17 | - public $type = null; |
|
18 | - |
|
19 | - /** |
|
20 | - * Price Base types |
|
21 | - * |
|
22 | - * @access private |
|
23 | - * @var int |
|
24 | - */ |
|
25 | - public $base_types = null; |
|
26 | - |
|
27 | - /** |
|
28 | - * return an array of Base types. Keys are INTs which are used in the database, |
|
29 | - * values are text-representations of the base type. |
|
30 | - * @return array |
|
31 | - */ |
|
32 | - public function get_base_types() |
|
33 | - { |
|
34 | - return $this->base_types; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Gets the name of the base |
|
39 | - * @param type $base_type_int |
|
40 | - * @return type |
|
41 | - */ |
|
42 | - public function get_base_type_name($base_type_int) |
|
43 | - { |
|
44 | - return $this->base_types[ $base_type_int ]; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * constants for price base types. In the DB, we decided to store the price base type |
|
49 | - * as an integer. So, to avoid just having magic numbers everwhere (eg, querying for |
|
50 | - * all price types with PBT_ID = 2), we define these constants, to make code more understandable. |
|
51 | - * So, as an example, to query for all price types that are a tax, we'd do |
|
52 | - * EEM_PRice_Type::instance()->get_all(array(array('PBT_ID'=>EEM_Price_Type::base_type_tax))) |
|
53 | - * instead of |
|
54 | - * EEM_Price_Type::instance()->get_all(array(array('PBT_ID'=>2))) |
|
55 | - * Although the 2nd is shorter, it's much less obvious what it's doing. Also, should these magic IDs ever |
|
56 | - * change, we can continue to use the constant, by simply change its value. |
|
57 | - */ |
|
58 | - const base_type_base_price = 1; |
|
59 | - const base_type_discount = 2; |
|
60 | - const base_type_surcharge = 3; |
|
61 | - const base_type_tax = 4; |
|
62 | - /** |
|
63 | - * private constructor to prevent direct creation |
|
64 | - * @Constructor |
|
65 | - * @access protected |
|
66 | - * @return void |
|
67 | - */ |
|
68 | - protected function __construct($timezone = null) |
|
69 | - { |
|
70 | - $this->base_types = array( |
|
71 | - EEM_Price_Type::base_type_base_price => esc_html__('Price', 'event_espresso'), |
|
72 | - EEM_Price_Type::base_type_discount => esc_html__('Discount', 'event_espresso'), |
|
73 | - EEM_Price_Type::base_type_surcharge => esc_html__('Surcharge', 'event_espresso'), |
|
74 | - EEM_Price_Type::base_type_tax => esc_html__('Tax', 'event_espresso') ); |
|
75 | - $this->singular_item = esc_html__('Price Type', 'event_espresso'); |
|
76 | - $this->plural_item = esc_html__('Price Types', 'event_espresso'); |
|
77 | - |
|
78 | - $this->_tables = array( |
|
79 | - 'Price_Type' => new EE_Primary_Table('esp_price_type', 'PRT_ID') |
|
80 | - ); |
|
81 | - $this->_fields = array( |
|
82 | - 'Price_Type' => array( |
|
83 | - 'PRT_ID' => new EE_Primary_Key_Int_Field('PRT_ID', esc_html__('Price Type ID', 'event_espresso')), |
|
84 | - 'PRT_name' => new EE_Plain_Text_Field('PRT_name', esc_html__('Price Type Name', 'event_espresso'), false, ''), |
|
85 | - 'PBT_ID' => new EE_Enum_Integer_Field('PBT_ID', esc_html__('Price Base type ID, 1 = Price , 2 = Discount , 3 = Surcharge , 4 = Tax', 'event_espresso'), false, EEM_Price_Type::base_type_base_price, $this->base_types), |
|
86 | - 'PRT_is_percent' => new EE_Boolean_Field('PRT_is_percent', esc_html__('Flag indicating price is a percentage', 'event_espresso'), false, false), |
|
87 | - 'PRT_order' => new EE_Integer_Field('PRT_order', esc_html__('Order in which price should be applied. ', 'event_espresso'), false, 0), |
|
88 | - 'PRT_deleted' => new EE_Trashed_Flag_Field('PRT_deleted', esc_html__('Flag indicating price type has been trashed', 'event_espresso'), false, false), |
|
89 | - 'PRT_wp_user' => new EE_WP_User_Field('PRT_wp_user', esc_html__('Price Type Creator ID', 'event_espresso'), false), |
|
90 | - ) |
|
91 | - ); |
|
92 | - $this->_model_relations = array( |
|
93 | - 'Price' => new EE_Has_Many_Relation(), |
|
94 | - 'WP_User' => new EE_Belongs_To_Relation(), |
|
95 | - ); |
|
96 | - // this model is generally available for reading |
|
97 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
98 | - // all price types are "default" in terms of capability names |
|
99 | - $this->_caps_slug = 'default_price_types'; |
|
100 | - parent::__construct($timezone); |
|
101 | - } |
|
102 | - |
|
103 | - |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * instantiate a new price type object with blank/empty properties |
|
108 | - * |
|
109 | - * @access public |
|
110 | - * @return mixed array on success, FALSE on fail |
|
111 | - */ |
|
112 | - public function get_new_price_type() |
|
113 | - { |
|
114 | - return EE_Price_Type::new_instance(); |
|
115 | - } |
|
14 | + // private instance of the Price Type object |
|
15 | + protected static $_instance = null; |
|
16 | + // An array of the price type objects |
|
17 | + public $type = null; |
|
18 | + |
|
19 | + /** |
|
20 | + * Price Base types |
|
21 | + * |
|
22 | + * @access private |
|
23 | + * @var int |
|
24 | + */ |
|
25 | + public $base_types = null; |
|
26 | + |
|
27 | + /** |
|
28 | + * return an array of Base types. Keys are INTs which are used in the database, |
|
29 | + * values are text-representations of the base type. |
|
30 | + * @return array |
|
31 | + */ |
|
32 | + public function get_base_types() |
|
33 | + { |
|
34 | + return $this->base_types; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Gets the name of the base |
|
39 | + * @param type $base_type_int |
|
40 | + * @return type |
|
41 | + */ |
|
42 | + public function get_base_type_name($base_type_int) |
|
43 | + { |
|
44 | + return $this->base_types[ $base_type_int ]; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * constants for price base types. In the DB, we decided to store the price base type |
|
49 | + * as an integer. So, to avoid just having magic numbers everwhere (eg, querying for |
|
50 | + * all price types with PBT_ID = 2), we define these constants, to make code more understandable. |
|
51 | + * So, as an example, to query for all price types that are a tax, we'd do |
|
52 | + * EEM_PRice_Type::instance()->get_all(array(array('PBT_ID'=>EEM_Price_Type::base_type_tax))) |
|
53 | + * instead of |
|
54 | + * EEM_Price_Type::instance()->get_all(array(array('PBT_ID'=>2))) |
|
55 | + * Although the 2nd is shorter, it's much less obvious what it's doing. Also, should these magic IDs ever |
|
56 | + * change, we can continue to use the constant, by simply change its value. |
|
57 | + */ |
|
58 | + const base_type_base_price = 1; |
|
59 | + const base_type_discount = 2; |
|
60 | + const base_type_surcharge = 3; |
|
61 | + const base_type_tax = 4; |
|
62 | + /** |
|
63 | + * private constructor to prevent direct creation |
|
64 | + * @Constructor |
|
65 | + * @access protected |
|
66 | + * @return void |
|
67 | + */ |
|
68 | + protected function __construct($timezone = null) |
|
69 | + { |
|
70 | + $this->base_types = array( |
|
71 | + EEM_Price_Type::base_type_base_price => esc_html__('Price', 'event_espresso'), |
|
72 | + EEM_Price_Type::base_type_discount => esc_html__('Discount', 'event_espresso'), |
|
73 | + EEM_Price_Type::base_type_surcharge => esc_html__('Surcharge', 'event_espresso'), |
|
74 | + EEM_Price_Type::base_type_tax => esc_html__('Tax', 'event_espresso') ); |
|
75 | + $this->singular_item = esc_html__('Price Type', 'event_espresso'); |
|
76 | + $this->plural_item = esc_html__('Price Types', 'event_espresso'); |
|
77 | + |
|
78 | + $this->_tables = array( |
|
79 | + 'Price_Type' => new EE_Primary_Table('esp_price_type', 'PRT_ID') |
|
80 | + ); |
|
81 | + $this->_fields = array( |
|
82 | + 'Price_Type' => array( |
|
83 | + 'PRT_ID' => new EE_Primary_Key_Int_Field('PRT_ID', esc_html__('Price Type ID', 'event_espresso')), |
|
84 | + 'PRT_name' => new EE_Plain_Text_Field('PRT_name', esc_html__('Price Type Name', 'event_espresso'), false, ''), |
|
85 | + 'PBT_ID' => new EE_Enum_Integer_Field('PBT_ID', esc_html__('Price Base type ID, 1 = Price , 2 = Discount , 3 = Surcharge , 4 = Tax', 'event_espresso'), false, EEM_Price_Type::base_type_base_price, $this->base_types), |
|
86 | + 'PRT_is_percent' => new EE_Boolean_Field('PRT_is_percent', esc_html__('Flag indicating price is a percentage', 'event_espresso'), false, false), |
|
87 | + 'PRT_order' => new EE_Integer_Field('PRT_order', esc_html__('Order in which price should be applied. ', 'event_espresso'), false, 0), |
|
88 | + 'PRT_deleted' => new EE_Trashed_Flag_Field('PRT_deleted', esc_html__('Flag indicating price type has been trashed', 'event_espresso'), false, false), |
|
89 | + 'PRT_wp_user' => new EE_WP_User_Field('PRT_wp_user', esc_html__('Price Type Creator ID', 'event_espresso'), false), |
|
90 | + ) |
|
91 | + ); |
|
92 | + $this->_model_relations = array( |
|
93 | + 'Price' => new EE_Has_Many_Relation(), |
|
94 | + 'WP_User' => new EE_Belongs_To_Relation(), |
|
95 | + ); |
|
96 | + // this model is generally available for reading |
|
97 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
98 | + // all price types are "default" in terms of capability names |
|
99 | + $this->_caps_slug = 'default_price_types'; |
|
100 | + parent::__construct($timezone); |
|
101 | + } |
|
102 | + |
|
103 | + |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * instantiate a new price type object with blank/empty properties |
|
108 | + * |
|
109 | + * @access public |
|
110 | + * @return mixed array on success, FALSE on fail |
|
111 | + */ |
|
112 | + public function get_new_price_type() |
|
113 | + { |
|
114 | + return EE_Price_Type::new_instance(); |
|
115 | + } |
|
116 | 116 | |
117 | 117 | |
118 | 118 | |
@@ -131,33 +131,33 @@ discard block |
||
131 | 131 | * |
132 | 132 | * @param type $query_params |
133 | 133 | * @param boolean $allow_blocking if TRUE, matched objects will only be deleted if there is no related model info |
134 | - * that blocks it (ie, there' sno other data that depends on this data); if false, deletes regardless of other objects |
|
135 | - * which may depend on it. Its generally advisable to always leave this as TRUE, otherwise you could easily corrupt your DB |
|
134 | + * that blocks it (ie, there' sno other data that depends on this data); if false, deletes regardless of other objects |
|
135 | + * which may depend on it. Its generally advisable to always leave this as TRUE, otherwise you could easily corrupt your DB |
|
136 | 136 | * @return boolean |
137 | 137 | */ |
138 | - public function delete_permanently($query_params = array(), $allow_blocking = true) |
|
139 | - { |
|
138 | + public function delete_permanently($query_params = array(), $allow_blocking = true) |
|
139 | + { |
|
140 | 140 | |
141 | - $would_be_deleted_price_types = $this->get_all_deleted_and_undeleted($query_params); |
|
142 | - $would_be_deleted_price_type_ids = array_keys($would_be_deleted_price_types); |
|
141 | + $would_be_deleted_price_types = $this->get_all_deleted_and_undeleted($query_params); |
|
142 | + $would_be_deleted_price_type_ids = array_keys($would_be_deleted_price_types); |
|
143 | 143 | |
144 | - $ID = $query_params[0][ $this->get_primary_key_field()->get_name() ]; |
|
144 | + $ID = $query_params[0][ $this->get_primary_key_field()->get_name() ]; |
|
145 | 145 | |
146 | - // check if any prices use this price type |
|
147 | - $prc_query_params = array(array('PRT_ID' => array('IN',$would_be_deleted_price_type_ids))); |
|
148 | - if ($prices = $this->get_all_related($ID, 'Price', $prc_query_params)) { |
|
149 | - $prices_names_and_ids = array(); |
|
150 | - foreach ($prices as $price) { |
|
151 | - /* @var $price EE_Price */ |
|
152 | - $prices_names_and_ids[] = $price->name() . "(" . $price->ID() . ")"; |
|
153 | - } |
|
154 | - $msg = sprintf(esc_html__('The Price Type(s) could not be deleted because there are existing Prices that currently use this Price Type. If you still wish to delete this Price Type, then either delete those Prices or change them to use other Price Types.The prices are: %s', 'event_espresso'), implode(",", $prices_names_and_ids)); |
|
155 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
156 | - return false; |
|
157 | - } |
|
146 | + // check if any prices use this price type |
|
147 | + $prc_query_params = array(array('PRT_ID' => array('IN',$would_be_deleted_price_type_ids))); |
|
148 | + if ($prices = $this->get_all_related($ID, 'Price', $prc_query_params)) { |
|
149 | + $prices_names_and_ids = array(); |
|
150 | + foreach ($prices as $price) { |
|
151 | + /* @var $price EE_Price */ |
|
152 | + $prices_names_and_ids[] = $price->name() . "(" . $price->ID() . ")"; |
|
153 | + } |
|
154 | + $msg = sprintf(esc_html__('The Price Type(s) could not be deleted because there are existing Prices that currently use this Price Type. If you still wish to delete this Price Type, then either delete those Prices or change them to use other Price Types.The prices are: %s', 'event_espresso'), implode(",", $prices_names_and_ids)); |
|
155 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
156 | + return false; |
|
157 | + } |
|
158 | 158 | |
159 | 159 | |
160 | 160 | |
161 | - return parent::delete_permanently($query_params); |
|
162 | - } |
|
161 | + return parent::delete_permanently($query_params); |
|
162 | + } |
|
163 | 163 | } |