@@ -13,93 +13,93 @@ discard block |
||
13 | 13 | class EEM_Change_Log extends EEM_Base |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * the related object was created log type |
|
18 | - */ |
|
19 | - const type_create = 'create'; |
|
20 | - /** |
|
21 | - * the related object was updated (changed, or soft-deleted) |
|
22 | - */ |
|
23 | - const type_update = 'update'; |
|
24 | - /** |
|
25 | - * the related object was deleted permanently |
|
26 | - */ |
|
27 | - const type_delete = 'delete'; |
|
28 | - /** |
|
29 | - * the related item had something worth noting happen on it, but |
|
30 | - * only for the purposes of debugging problems |
|
31 | - */ |
|
32 | - const type_debug = 'debug'; |
|
33 | - /** |
|
34 | - * the related item had an error occur on it |
|
35 | - */ |
|
36 | - const type_error = 'error'; |
|
37 | - /** |
|
38 | - * the related item is regarding some gateway interaction, like an IPN |
|
39 | - * or request to process a payment |
|
40 | - */ |
|
41 | - const type_gateway = 'gateway'; |
|
16 | + /** |
|
17 | + * the related object was created log type |
|
18 | + */ |
|
19 | + const type_create = 'create'; |
|
20 | + /** |
|
21 | + * the related object was updated (changed, or soft-deleted) |
|
22 | + */ |
|
23 | + const type_update = 'update'; |
|
24 | + /** |
|
25 | + * the related object was deleted permanently |
|
26 | + */ |
|
27 | + const type_delete = 'delete'; |
|
28 | + /** |
|
29 | + * the related item had something worth noting happen on it, but |
|
30 | + * only for the purposes of debugging problems |
|
31 | + */ |
|
32 | + const type_debug = 'debug'; |
|
33 | + /** |
|
34 | + * the related item had an error occur on it |
|
35 | + */ |
|
36 | + const type_error = 'error'; |
|
37 | + /** |
|
38 | + * the related item is regarding some gateway interaction, like an IPN |
|
39 | + * or request to process a payment |
|
40 | + */ |
|
41 | + const type_gateway = 'gateway'; |
|
42 | 42 | |
43 | - /** |
|
44 | - * private instance of the EEM_Change_Log object |
|
45 | - * |
|
46 | - * @access private |
|
47 | - * @var EEM_Change_Log $_instance |
|
48 | - */ |
|
49 | - protected static $_instance = null; |
|
43 | + /** |
|
44 | + * private instance of the EEM_Change_Log object |
|
45 | + * |
|
46 | + * @access private |
|
47 | + * @var EEM_Change_Log $_instance |
|
48 | + */ |
|
49 | + protected static $_instance = null; |
|
50 | 50 | |
51 | 51 | |
52 | - /** |
|
53 | - * constructor |
|
54 | - * |
|
55 | - * @access protected |
|
56 | - * @param null $timezone |
|
57 | - * @throws EE_Error |
|
58 | - */ |
|
59 | - protected function __construct($timezone = null) |
|
60 | - { |
|
61 | - global $current_user; |
|
62 | - $this->singular_item = esc_html__('Log', 'event_espresso'); |
|
63 | - $this->plural_item = esc_html__('Logs', 'event_espresso'); |
|
64 | - $this->_tables = array( |
|
65 | - 'Log' => new EE_Primary_Table('esp_log', 'LOG_ID'), |
|
66 | - ); |
|
67 | - $models_this_can_attach_to = array_keys(EE_Registry::instance()->non_abstract_db_models); |
|
68 | - $this->_fields = array( |
|
69 | - 'Log' => array( |
|
70 | - 'LOG_ID' => new EE_Primary_Key_Int_Field('LOG_ID', esc_html__('Log ID', 'event_espresso')), |
|
71 | - 'LOG_time' => new EE_Datetime_Field( |
|
72 | - 'LOG_time', |
|
73 | - esc_html__("Log Time", 'event_espresso'), |
|
74 | - false, |
|
75 | - EE_Datetime_Field::now |
|
76 | - ), |
|
77 | - 'OBJ_ID' => new EE_Foreign_Key_String_Field( |
|
78 | - 'OBJ_ID', |
|
79 | - esc_html__("Object ID (int or string)", 'event_espresso'), |
|
80 | - true, |
|
81 | - null, |
|
82 | - $models_this_can_attach_to |
|
83 | - ), |
|
84 | - 'OBJ_type' => new EE_Any_Foreign_Model_Name_Field( |
|
85 | - 'OBJ_type', |
|
86 | - esc_html__("Object Type", 'event_espresso'), |
|
87 | - true, |
|
88 | - null, |
|
89 | - $models_this_can_attach_to |
|
90 | - ), |
|
91 | - 'LOG_type' => new EE_Plain_Text_Field( |
|
92 | - 'LOG_type', |
|
93 | - esc_html__("Type of log entry", "event_espresso"), |
|
94 | - false, |
|
95 | - self::type_debug |
|
96 | - ), |
|
97 | - 'LOG_message' => new EE_Maybe_Serialized_Text_Field( |
|
98 | - 'LOG_message', |
|
99 | - esc_html__("Log Message (body)", 'event_espresso'), |
|
100 | - true |
|
101 | - ), |
|
102 | - /* |
|
52 | + /** |
|
53 | + * constructor |
|
54 | + * |
|
55 | + * @access protected |
|
56 | + * @param null $timezone |
|
57 | + * @throws EE_Error |
|
58 | + */ |
|
59 | + protected function __construct($timezone = null) |
|
60 | + { |
|
61 | + global $current_user; |
|
62 | + $this->singular_item = esc_html__('Log', 'event_espresso'); |
|
63 | + $this->plural_item = esc_html__('Logs', 'event_espresso'); |
|
64 | + $this->_tables = array( |
|
65 | + 'Log' => new EE_Primary_Table('esp_log', 'LOG_ID'), |
|
66 | + ); |
|
67 | + $models_this_can_attach_to = array_keys(EE_Registry::instance()->non_abstract_db_models); |
|
68 | + $this->_fields = array( |
|
69 | + 'Log' => array( |
|
70 | + 'LOG_ID' => new EE_Primary_Key_Int_Field('LOG_ID', esc_html__('Log ID', 'event_espresso')), |
|
71 | + 'LOG_time' => new EE_Datetime_Field( |
|
72 | + 'LOG_time', |
|
73 | + esc_html__("Log Time", 'event_espresso'), |
|
74 | + false, |
|
75 | + EE_Datetime_Field::now |
|
76 | + ), |
|
77 | + 'OBJ_ID' => new EE_Foreign_Key_String_Field( |
|
78 | + 'OBJ_ID', |
|
79 | + esc_html__("Object ID (int or string)", 'event_espresso'), |
|
80 | + true, |
|
81 | + null, |
|
82 | + $models_this_can_attach_to |
|
83 | + ), |
|
84 | + 'OBJ_type' => new EE_Any_Foreign_Model_Name_Field( |
|
85 | + 'OBJ_type', |
|
86 | + esc_html__("Object Type", 'event_espresso'), |
|
87 | + true, |
|
88 | + null, |
|
89 | + $models_this_can_attach_to |
|
90 | + ), |
|
91 | + 'LOG_type' => new EE_Plain_Text_Field( |
|
92 | + 'LOG_type', |
|
93 | + esc_html__("Type of log entry", "event_espresso"), |
|
94 | + false, |
|
95 | + self::type_debug |
|
96 | + ), |
|
97 | + 'LOG_message' => new EE_Maybe_Serialized_Text_Field( |
|
98 | + 'LOG_message', |
|
99 | + esc_html__("Log Message (body)", 'event_espresso'), |
|
100 | + true |
|
101 | + ), |
|
102 | + /* |
|
103 | 103 | * Note: when querying for a change log's user, the OBJ_ID and OBJ_type fields are used, |
104 | 104 | * not the LOG_wp_user field. E.g., |
105 | 105 | * `EEM_Change_Log::instance()->get_all(array(array('WP_User.ID'=>1)))` will actually return |
@@ -108,158 +108,158 @@ discard block |
||
108 | 108 | * If you want the latter, you can't use the model's magic joining. E.g, you would need to do |
109 | 109 | * `EEM_Change_Log::instance()->get_all(array(array('LOG_wp_user' => 1)))`. |
110 | 110 | */ |
111 | - 'LOG_wp_user' => new EE_WP_User_Field( |
|
112 | - 'LOG_wp_user', |
|
113 | - esc_html__("User who was logged in while this occurred", 'event_espresso'), |
|
114 | - true |
|
115 | - ), |
|
116 | - ), |
|
117 | - ); |
|
118 | - $this->_model_relations = array(); |
|
119 | - foreach ($models_this_can_attach_to as $model) { |
|
120 | - if ($model != 'Change_Log') { |
|
121 | - $this->_model_relations[$model] = new EE_Belongs_To_Any_Relation(); |
|
122 | - } |
|
123 | - } |
|
124 | - //use completely custom caps for this |
|
125 | - $this->_cap_restriction_generators = false; |
|
126 | - //caps-wise this is all-or-nothing: if you have the default role you can access anything, otherwise nothing |
|
127 | - foreach ($this->_cap_contexts_to_cap_action_map as $cap_context => $action) { |
|
128 | - $this->_cap_restrictions[$cap_context][EE_Restriction_Generator_Base::get_default_restrictions_cap()] |
|
129 | - = new EE_Return_None_Where_Conditions(); |
|
130 | - } |
|
131 | - parent::__construct($timezone); |
|
132 | - } |
|
111 | + 'LOG_wp_user' => new EE_WP_User_Field( |
|
112 | + 'LOG_wp_user', |
|
113 | + esc_html__("User who was logged in while this occurred", 'event_espresso'), |
|
114 | + true |
|
115 | + ), |
|
116 | + ), |
|
117 | + ); |
|
118 | + $this->_model_relations = array(); |
|
119 | + foreach ($models_this_can_attach_to as $model) { |
|
120 | + if ($model != 'Change_Log') { |
|
121 | + $this->_model_relations[$model] = new EE_Belongs_To_Any_Relation(); |
|
122 | + } |
|
123 | + } |
|
124 | + //use completely custom caps for this |
|
125 | + $this->_cap_restriction_generators = false; |
|
126 | + //caps-wise this is all-or-nothing: if you have the default role you can access anything, otherwise nothing |
|
127 | + foreach ($this->_cap_contexts_to_cap_action_map as $cap_context => $action) { |
|
128 | + $this->_cap_restrictions[$cap_context][EE_Restriction_Generator_Base::get_default_restrictions_cap()] |
|
129 | + = new EE_Return_None_Where_Conditions(); |
|
130 | + } |
|
131 | + parent::__construct($timezone); |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * @param string $log_type !see the acceptable values of LOG_type in EEM__Change_Log::__construct |
|
136 | - * @param mixed $message array|string of the message you want to record |
|
137 | - * @param EE_Base_Class $related_model_obj |
|
138 | - * @return EE_Change_Log |
|
139 | - * @throws EE_Error |
|
140 | - */ |
|
141 | - public function log($log_type, $message, $related_model_obj) |
|
142 | - { |
|
143 | - if ($related_model_obj instanceof EE_Base_Class) { |
|
144 | - $obj_id = $related_model_obj->ID(); |
|
145 | - $obj_type = $related_model_obj->get_model()->get_this_model_name(); |
|
146 | - } else { |
|
147 | - $obj_id = null; |
|
148 | - $obj_type = null; |
|
149 | - } |
|
150 | - /** @var EE_Change_Log $log */ |
|
151 | - $log = EE_Change_Log::new_instance(array( |
|
152 | - 'LOG_type' => $log_type, |
|
153 | - 'LOG_message' => $message, |
|
154 | - 'OBJ_ID' => $obj_id, |
|
155 | - 'OBJ_type' => $obj_type, |
|
156 | - )); |
|
157 | - $log->save(); |
|
158 | - return $log; |
|
159 | - } |
|
134 | + /** |
|
135 | + * @param string $log_type !see the acceptable values of LOG_type in EEM__Change_Log::__construct |
|
136 | + * @param mixed $message array|string of the message you want to record |
|
137 | + * @param EE_Base_Class $related_model_obj |
|
138 | + * @return EE_Change_Log |
|
139 | + * @throws EE_Error |
|
140 | + */ |
|
141 | + public function log($log_type, $message, $related_model_obj) |
|
142 | + { |
|
143 | + if ($related_model_obj instanceof EE_Base_Class) { |
|
144 | + $obj_id = $related_model_obj->ID(); |
|
145 | + $obj_type = $related_model_obj->get_model()->get_this_model_name(); |
|
146 | + } else { |
|
147 | + $obj_id = null; |
|
148 | + $obj_type = null; |
|
149 | + } |
|
150 | + /** @var EE_Change_Log $log */ |
|
151 | + $log = EE_Change_Log::new_instance(array( |
|
152 | + 'LOG_type' => $log_type, |
|
153 | + 'LOG_message' => $message, |
|
154 | + 'OBJ_ID' => $obj_id, |
|
155 | + 'OBJ_type' => $obj_type, |
|
156 | + )); |
|
157 | + $log->save(); |
|
158 | + return $log; |
|
159 | + } |
|
160 | 160 | |
161 | 161 | |
162 | - /** |
|
163 | - * Adds a gateway log for the specified object, given its ID and type |
|
164 | - * |
|
165 | - * @param string $message |
|
166 | - * @param mixed $related_obj_id |
|
167 | - * @param string $related_obj_type |
|
168 | - * @throws EE_Error |
|
169 | - * @return EE_Change_Log |
|
170 | - */ |
|
171 | - public function gateway_log($message, $related_obj_id, $related_obj_type) |
|
172 | - { |
|
173 | - if (! EE_Registry::instance()->is_model_name($related_obj_type)) { |
|
174 | - throw new EE_Error( |
|
175 | - sprintf( |
|
176 | - esc_html__( |
|
177 | - "'%s' is not a model name. A model name must be provided when making a gateway log. Eg, 'Payment', 'Payment_Method', etc", |
|
178 | - "event_espresso" |
|
179 | - ), |
|
180 | - $related_obj_type |
|
181 | - ) |
|
182 | - ); |
|
183 | - } |
|
184 | - /** @var EE_Change_Log $log */ |
|
185 | - $log = EE_Change_Log::new_instance(array( |
|
186 | - 'LOG_type' => EEM_Change_Log::type_gateway, |
|
187 | - 'LOG_message' => $message, |
|
188 | - 'OBJ_ID' => $related_obj_id, |
|
189 | - 'OBJ_type' => $related_obj_type, |
|
190 | - )); |
|
191 | - $log->save(); |
|
192 | - return $log; |
|
193 | - } |
|
162 | + /** |
|
163 | + * Adds a gateway log for the specified object, given its ID and type |
|
164 | + * |
|
165 | + * @param string $message |
|
166 | + * @param mixed $related_obj_id |
|
167 | + * @param string $related_obj_type |
|
168 | + * @throws EE_Error |
|
169 | + * @return EE_Change_Log |
|
170 | + */ |
|
171 | + public function gateway_log($message, $related_obj_id, $related_obj_type) |
|
172 | + { |
|
173 | + if (! EE_Registry::instance()->is_model_name($related_obj_type)) { |
|
174 | + throw new EE_Error( |
|
175 | + sprintf( |
|
176 | + esc_html__( |
|
177 | + "'%s' is not a model name. A model name must be provided when making a gateway log. Eg, 'Payment', 'Payment_Method', etc", |
|
178 | + "event_espresso" |
|
179 | + ), |
|
180 | + $related_obj_type |
|
181 | + ) |
|
182 | + ); |
|
183 | + } |
|
184 | + /** @var EE_Change_Log $log */ |
|
185 | + $log = EE_Change_Log::new_instance(array( |
|
186 | + 'LOG_type' => EEM_Change_Log::type_gateway, |
|
187 | + 'LOG_message' => $message, |
|
188 | + 'OBJ_ID' => $related_obj_id, |
|
189 | + 'OBJ_type' => $related_obj_type, |
|
190 | + )); |
|
191 | + $log->save(); |
|
192 | + return $log; |
|
193 | + } |
|
194 | 194 | |
195 | 195 | |
196 | - /** |
|
197 | - * Just gets the bare-bones wpdb results as an array in cases where efficiency is essential |
|
198 | - * |
|
199 | - * @param array $query_params @see EEM_Base::get_all |
|
200 | - * @return array of arrays |
|
201 | - * @throws EE_Error |
|
202 | - */ |
|
203 | - public function get_all_efficiently($query_params) |
|
204 | - { |
|
205 | - return $this->_get_all_wpdb_results($query_params); |
|
206 | - } |
|
196 | + /** |
|
197 | + * Just gets the bare-bones wpdb results as an array in cases where efficiency is essential |
|
198 | + * |
|
199 | + * @param array $query_params @see EEM_Base::get_all |
|
200 | + * @return array of arrays |
|
201 | + * @throws EE_Error |
|
202 | + */ |
|
203 | + public function get_all_efficiently($query_params) |
|
204 | + { |
|
205 | + return $this->_get_all_wpdb_results($query_params); |
|
206 | + } |
|
207 | 207 | |
208 | 208 | |
209 | - /** |
|
210 | - * Executes a database query to delete gateway logs. Does not affect model objects, so if you attempt to use |
|
211 | - * models after this, they may be out-of-sync with the database |
|
212 | - * |
|
213 | - * @param DateTime $datetime |
|
214 | - * @return false|int |
|
215 | - * @throws EE_Error |
|
216 | - */ |
|
217 | - public function delete_gateway_logs_older_than(DateTime $datetime) |
|
218 | - { |
|
219 | - global $wpdb; |
|
220 | - return $wpdb->query( |
|
221 | - $wpdb->prepare( |
|
222 | - 'DELETE FROM ' . $this->table() . ' WHERE LOG_type = %s AND LOG_time < %s', |
|
223 | - EEM_Change_Log::type_gateway, |
|
224 | - $datetime->format(EE_Datetime_Field::mysql_timestamp_format) |
|
225 | - ) |
|
226 | - ); |
|
227 | - } |
|
209 | + /** |
|
210 | + * Executes a database query to delete gateway logs. Does not affect model objects, so if you attempt to use |
|
211 | + * models after this, they may be out-of-sync with the database |
|
212 | + * |
|
213 | + * @param DateTime $datetime |
|
214 | + * @return false|int |
|
215 | + * @throws EE_Error |
|
216 | + */ |
|
217 | + public function delete_gateway_logs_older_than(DateTime $datetime) |
|
218 | + { |
|
219 | + global $wpdb; |
|
220 | + return $wpdb->query( |
|
221 | + $wpdb->prepare( |
|
222 | + 'DELETE FROM ' . $this->table() . ' WHERE LOG_type = %s AND LOG_time < %s', |
|
223 | + EEM_Change_Log::type_gateway, |
|
224 | + $datetime->format(EE_Datetime_Field::mysql_timestamp_format) |
|
225 | + ) |
|
226 | + ); |
|
227 | + } |
|
228 | 228 | |
229 | 229 | |
230 | - /** |
|
231 | - * Returns the map of type to pretty label for identifiers used for `LOG_type`. Client code can register their own |
|
232 | - * map vai the given filter. |
|
233 | - * |
|
234 | - * @return array |
|
235 | - */ |
|
236 | - public static function get_pretty_label_map_for_registered_types() |
|
237 | - { |
|
238 | - return apply_filters( |
|
239 | - 'FHEE__EEM_Change_Log__get_pretty_label_map_for_registered_types', |
|
240 | - array( |
|
241 | - self::type_create=> esc_html__("Create", "event_espresso"), |
|
242 | - self::type_update=> esc_html__("Update", "event_espresso"), |
|
243 | - self::type_delete => esc_html__("Delete", "event_espresso"), |
|
244 | - self::type_debug=> esc_html__("Debug", "event_espresso"), |
|
245 | - self::type_error=> esc_html__("Error", "event_espresso"), |
|
246 | - self::type_gateway=> esc_html__("Gateway Interaction (IPN or Direct Payment)", 'event_espresso') |
|
247 | - ) |
|
248 | - ); |
|
249 | - } |
|
230 | + /** |
|
231 | + * Returns the map of type to pretty label for identifiers used for `LOG_type`. Client code can register their own |
|
232 | + * map vai the given filter. |
|
233 | + * |
|
234 | + * @return array |
|
235 | + */ |
|
236 | + public static function get_pretty_label_map_for_registered_types() |
|
237 | + { |
|
238 | + return apply_filters( |
|
239 | + 'FHEE__EEM_Change_Log__get_pretty_label_map_for_registered_types', |
|
240 | + array( |
|
241 | + self::type_create=> esc_html__("Create", "event_espresso"), |
|
242 | + self::type_update=> esc_html__("Update", "event_espresso"), |
|
243 | + self::type_delete => esc_html__("Delete", "event_espresso"), |
|
244 | + self::type_debug=> esc_html__("Debug", "event_espresso"), |
|
245 | + self::type_error=> esc_html__("Error", "event_espresso"), |
|
246 | + self::type_gateway=> esc_html__("Gateway Interaction (IPN or Direct Payment)", 'event_espresso') |
|
247 | + ) |
|
248 | + ); |
|
249 | + } |
|
250 | 250 | |
251 | 251 | |
252 | - /** |
|
253 | - * Return the pretty (localized) label for the given log type identifier. |
|
254 | - * @param string $type_identifier |
|
255 | - * @return string |
|
256 | - */ |
|
257 | - public static function get_pretty_label_for_type($type_identifier) |
|
258 | - { |
|
259 | - $type_identifier_map = self::get_pretty_label_map_for_registered_types(); |
|
260 | - //we fallback to the incoming type identifier if there is no localized label for it. |
|
261 | - return isset($type_identifier_map[$type_identifier]) |
|
262 | - ? $type_identifier_map[$type_identifier] |
|
263 | - : $type_identifier; |
|
264 | - } |
|
252 | + /** |
|
253 | + * Return the pretty (localized) label for the given log type identifier. |
|
254 | + * @param string $type_identifier |
|
255 | + * @return string |
|
256 | + */ |
|
257 | + public static function get_pretty_label_for_type($type_identifier) |
|
258 | + { |
|
259 | + $type_identifier_map = self::get_pretty_label_map_for_registered_types(); |
|
260 | + //we fallback to the incoming type identifier if there is no localized label for it. |
|
261 | + return isset($type_identifier_map[$type_identifier]) |
|
262 | + ? $type_identifier_map[$type_identifier] |
|
263 | + : $type_identifier; |
|
264 | + } |
|
265 | 265 | } |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | ), |
116 | 116 | ), |
117 | 117 | ); |
118 | - $this->_model_relations = array(); |
|
118 | + $this->_model_relations = array(); |
|
119 | 119 | foreach ($models_this_can_attach_to as $model) { |
120 | 120 | if ($model != 'Change_Log') { |
121 | 121 | $this->_model_relations[$model] = new EE_Belongs_To_Any_Relation(); |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | */ |
171 | 171 | public function gateway_log($message, $related_obj_id, $related_obj_type) |
172 | 172 | { |
173 | - if (! EE_Registry::instance()->is_model_name($related_obj_type)) { |
|
173 | + if ( ! EE_Registry::instance()->is_model_name($related_obj_type)) { |
|
174 | 174 | throw new EE_Error( |
175 | 175 | sprintf( |
176 | 176 | esc_html__( |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | global $wpdb; |
220 | 220 | return $wpdb->query( |
221 | 221 | $wpdb->prepare( |
222 | - 'DELETE FROM ' . $this->table() . ' WHERE LOG_type = %s AND LOG_time < %s', |
|
222 | + 'DELETE FROM '.$this->table().' WHERE LOG_type = %s AND LOG_time < %s', |
|
223 | 223 | EEM_Change_Log::type_gateway, |
224 | 224 | $datetime->format(EE_Datetime_Field::mysql_timestamp_format) |
225 | 225 | ) |