PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.17.0
GiveWP – Donation Plugin and Fundraising Platform v4.17.0
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
← All changes | includes/class-give-logging.php +144 -575 2.3.0 → 4.17.0 View file →
@@ -1,11 +1,17 @@
1 1 <?php
2 +
3 +use Give\Log\Log;
4 +use Give\Log\LogFactory;
5 +use Give\Log\LogRepository;
6 +use Give\Log\ValueObjects\LogType;
7 +use Give\Log\Helpers\LogTypeHelper;
2 8 /**
3 9 * Class for logging events and errors
4 10 *
5 11 * @package Give
6 12 * @subpackage Classes/Give_Logging
7 - * @copyright Copyright (c) 2016, WordImpress
13 + * @copyright Copyright (c) 2016, GiveWP
8 14 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 15 * @since 1.0
10 16 */
11 17
@@ -18,28 +24,24 @@
18 24 * Give_Logging Class
19 25 *
20 26 * A general use class for logging events and errors.
21 27 *
28 + * @deprecated 2.10.0
29 + * @use Log
30 + * @see Log
31 + *
22 32 * @since 1.0
23 33 */
24 34 class Give_Logging {
25 35 /**
26 - * Logs data operation handler object.
27 - *
28 - * @since 2.0
29 - * @access private
30 - * @var Give_DB_Logs
36 + * @var LogRepository
31 37 */
32 - public $log_db;
38 + private $logRepository;
33 39
34 40 /**
35 - * Log meta data operation handler object.
36 - *
37 - * @since 2.0
38 - * @access private
39 - * @var Give_DB_Log_Meta
41 + * @var LogTypeHelper
40 42 */
41 - public $logmeta_db;
43 + private $logTypeHelper;
42 44
43 45 /**
44 46 * Class Constructor
45 47 *
@@ -48,129 +50,22 @@
48 50 * @since 1.0
49 51 * @access public
50 52 */
51 53 public function __construct() {
52 - /**
53 - * Setup properties
54 - */
55 -
56 - require_once GIVE_PLUGIN_DIR . 'includes/class-give-db-logs.php';
57 - require_once GIVE_PLUGIN_DIR . 'includes/class-give-db-logs-meta.php';
58 - $this->log_db = new Give_DB_Logs();
59 - $this->logmeta_db = new Give_DB_Log_Meta();
60 -
61 - /**
62 - * Setup hooks.
63 - */
64 -
65 - add_action( 'save_post_give_payment', array( $this, 'background_process_delete_cache' ) );
66 - add_action( 'save_post_give_forms', array( $this, 'background_process_delete_cache' ) );
67 - add_action( 'save_post_give_log', array( $this, 'background_process_delete_cache' ) );
68 - add_action( 'give_delete_log_cache', array( $this, 'delete_cache' ) );
69 - add_action( 'update_log_metadata', array( $this, 'bc_200_set_payment_as_log_parent' ), 10, 4 );
70 -
71 - // Backward compatibility.
72 - if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
73 - // Create the log post type
74 - add_action( 'init', array( $this, 'register_post_type' ), -2 );
75 - }
76 -
77 - // Create types taxonomy and default types
78 - // @todo: remove this taxonomy, some addon use this taxonomy with there custom log post type for example: recurring
79 - // Do not use this taxonomy with your log type because we will remove it in future releases.
80 - add_action( 'init', array( $this, 'register_taxonomy' ), -2 );
54 + $this->logRepository = give( LogRepository::class );
55 + $this->logTypeHelper = give( LogTypeHelper::class );
81 56 }
82 57
83 -
84 58 /**
85 - * Log Post Type
86 - *
87 - * Registers the 'give_log' Post Type.
88 - *
89 - * @since 1.0
90 - * @access public
91 - *
92 - * @return void
93 - */
94 - public function register_post_type() {
95 - /* Logs post type */
96 - $log_args = array(
97 - 'labels' => array(
98 - 'name' => esc_html__( 'Logs', 'give' ),
99 - ),
100 - 'public' => false,
101 - 'exclude_from_search' => true,
102 - 'publicly_queryable' => false,
103 - 'show_ui' => false,
104 - 'query_var' => false,
105 - 'rewrite' => false,
106 - 'capability_type' => 'post',
107 - 'supports' => array( 'title', 'editor' ),
108 - 'can_export' => true,
109 - );
110 -
111 - register_post_type( 'give_log', $log_args );
112 - }
113 -
114 - /**
115 - * Log Type Taxonomy
116 - *
117 - * Registers the "Log Type" taxonomy. Used to determine the type of log entry.
118 - *
119 - * @since 1.0
120 - * @access public
121 - *
122 - * @return void
123 - */
124 - public function register_taxonomy() {
125 - register_taxonomy( 'give_log_type', 'give_log', array(
126 - 'public' => false,
127 - ) );
128 - }
129 -
130 - /**
131 - * Log Types
132 - *
133 - * Sets up the default log types and allows for new ones to be created.
134 - *
135 - * @since 1.0
136 - * @access public
137 - *
138 - * @return array $terms
139 - */
140 - public function log_types() {
141 - $terms = array(
142 - 'sale',
143 - 'gateway_error',
144 - 'api_request',
145 - 'update',
146 - );
147 -
148 - return apply_filters( 'give_log_types', $terms );
149 - }
150 -
151 - /**
152 - * Check if a log type is valid
153 - *
154 - * Checks to see if the specified type is in the registered list of types.
155 - *
156 - * @since 1.0
157 - * @access public
158 - *
159 - * @param string $type Log type.
160 - *
161 - * @return bool Whether log type is valid.
162 - */
163 - public function valid_type( $type ) {
164 - return in_array( $type, $this->log_types() );
165 - }
166 -
167 - /**
168 59 * Create new log entry
169 60 *
170 61 * This is just a simple and fast way to log something. Use $this->insert_log()
171 62 * if you need to store custom meta data.
172 63 *
64 + * @deprecated 2.10.0
65 + * @use Log::LOG_TYPE( $message );
66 + * @see Log
67 + *
173 68 * @since 1.0
174 69 * @access public
175 70 *
176 71 * @param string $title Log entry title. Default is empty.
@@ -180,23 +75,62 @@
180 75 *
181 76 * @return int Log ID.
182 77 */
183 78 public function add( $title = '', $message = '', $parent = 0, $type = '' ) {
184 - $log_data = array(
79 + $log_data = [
185 80 'post_title' => $title,
186 81 'post_content' => $message,
187 82 'post_parent' => $parent,
188 83 'log_type' => $type,
189 - );
84 + ];
190 85
191 86 return $this->insert_log( $log_data );
192 87 }
193 88
194 89 /**
90 + * Helper method used to map the fields from the old system to the new system
91 + *
92 + * @since 2.10.0
93 + *
94 + * @param array $logData
95 + * @param array $logMeta
96 + *
97 + * @return array
98 + */
99 + private function getLogData( $logData, $logMeta ) {
100 + $oldType = isset( $logData['log_type'] )
101 + ? $logData['log_type']
102 + : LogType::getDefault();
103 +
104 + $data = $this->logTypeHelper->getDataFromType( $oldType );
105 +
106 + $content = esc_html__( 'Something went wrong', 'give' );
107 +
108 + if ( isset( $logData['log_content'] ) ) {
109 + $content = $logData['log_content'];
110 + } else {
111 + if ( isset( $logData['post_title'] ) ) {
112 + $content = $logData['post_title'];
113 + }
114 + }
115 +
116 + return [
117 + 'type' => $data['type'],
118 + 'category' => $data['category'],
119 + 'message' => $content,
120 + 'context' => array_merge( $logData, $logMeta ),
121 + ];
122 + }
123 +
124 + /**
195 125 * Get Logs
196 126 *
197 127 * Retrieves log items for a particular object ID.
198 128 *
129 + * @deprecated 2.10.0
130 + * @use LogRepository::getLogs();
131 + * @see LogRepository
132 + *
199 133 * @since 1.0
200 134 * @access public
201 135 *
202 136 * @param int $object_id Log object ID. Default is 0.
@@ -205,18 +139,51 @@
205 139 *
206 140 * @return array An array of the connected logs.
207 141 */
208 142 public function get_logs( $object_id = 0, $type = '', $paged = null ) {
209 - return $this->get_connected_logs( array(
210 - 'log_parent' => $object_id,
211 - 'paged' => $paged,
212 - 'log_type' => $type,
213 - ) );
143 + if ( $object_id ) {
144 + $log = $this->logRepository->getLog( $object_id );
145 +
146 + return [
147 + (object) [
148 + 'ID' => $log->getId(),
149 + 'log_date' => $log->getDate(),
150 + 'log_date_gmt' => '',
151 + 'log_content' => $log->getMessage(),
152 + 'log_title' => $log->getCategory() . ' - ' . $log->getSource(),
153 + 'log_type' => $log->getType(),
154 + ],
155 + ];
156 + }
157 +
158 + if ( ! empty( $type ) ) {
159 + $data = [];
160 + $logs = $this->logRepository->getLogsByType( $type );
161 +
162 + foreach ( $logs as $log ) {
163 + $data[] = (object) [
164 + 'ID' => $log->getId(),
165 + 'log_date' => $log->getDate(),
166 + 'log_date_gmt' => '',
167 + 'log_content' => $log->getMessage(),
168 + 'log_title' => $log->getCategory() . ' - ' . $log->getSource(),
169 + 'log_type' => $log->getType(),
170 + ];
171 + }
172 +
173 + return $data;
174 + }
175 +
176 + return [];
214 177 }
215 178
216 179 /**
217 180 * Stores a log entry
218 181 *
182 + * @deprecated 2.10.0
183 + * @use Log::LOG_TYPE( $message );
184 + * @see Log
185 + *
219 186 * @since 1.0
220 187 * @access public
221 188 *
222 189 * @param array $log_data Log entry data.
@@ -223,56 +190,43 @@
223 190 * @param array $log_meta Log entry meta.
224 191 *
225 192 * @return int The ID of the newly created log item.
226 193 */
227 - public function insert_log( $log_data = array(), $log_meta = array() ) {
228 - $log_id = 0;
194 + //phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
195 + public function insert_log( $log_data = [], $log_meta = [] ) {
196 + // Extract data from parameters
197 + $data = $this->getLogData( $log_data, $log_meta );
229 198
230 - $defaults = array(
231 - 'log_parent' => 0,
232 - 'log_content' => '',
233 - 'log_type' => false,
199 + $backtrace = debug_backtrace();
234 200
235 - // Backward compatibility.
236 - 'post_type' => 'give_log',
237 - 'post_status' => 'publish',
238 - );
239 -
240 - $args = wp_parse_args( $log_data, $defaults );
241 - $this->bc_200_validate_params( $args, $log_meta );
242 -
243 - if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
244 - global $wpdb;
245 -
246 - // Backward Compatibility.
247 - if ( ! $wpdb->get_var( "SELECT ID from {$this->log_db->table_name} ORDER BY id DESC LIMIT 1" ) ) {
248 - $latest_log_id = $wpdb->get_var( "SELECT ID from $wpdb->posts ORDER BY id DESC LIMIT 1" );
249 - $latest_log_id = empty( $latest_log_id ) ? 1 : ++ $latest_log_id;
250 -
251 - $args['ID'] = $latest_log_id;
252 - $this->log_db->insert( $args );
253 - }
201 + // Add more context
202 + if (
203 + isset( $backtrace[1] ) &&
204 + ! array_diff( [ 'file', 'line', 'function', 'class' ], array_keys( $backtrace[1] ) )
205 + ) {
206 + $data['context']['file'] = $backtrace[1]['file'];
207 + $data['context']['line'] = $backtrace[1]['line'];
208 + $data['context']['function'] = $backtrace[1]['function'];
209 + $data['context']['class'] = $backtrace[1]['class'];
254 210 }
255 211
256 - $log_id = $this->log_db->add( $args );
212 + try {
213 + $log = LogFactory::makeFromArray( $data );
214 + $log->save();
257 215
258 - // Set log meta, if any
259 - if ( $log_id && ! empty( $log_meta ) ) {
260 - foreach ( (array) $log_meta as $key => $meta ) {
261 - $this->logmeta_db->update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta );
262 - }
216 + return $log->getId();
217 + } catch ( Exception $exception ) {
218 + error_log( $exception->getMessage() );
263 219 }
264 -
265 -
266 - // Delete cache.
267 - $this->delete_cache();
268 -
269 - return $log_id;
270 220 }
271 221
272 222 /**
273 223 * Update and existing log item
274 224 *
225 + * @deprecated 2.10.0
226 + * @use Log::LOG_TYPE( $message );
227 + * @see Log
228 + *
275 229 * @since 1.0
276 230 * @access public
277 231 *
278 232 * @param array $log_data Log entry data.
@@ -279,66 +233,10 @@
279 233 * @param array $log_meta Log entry meta.
280 234 *
281 235 * @return bool|null True if successful, false otherwise.
282 236 */
283 - public function update_log( $log_data = array(), $log_meta = array() ) {
284 - $log_id = 0;
285 -
286 - /**
287 - * Fires before updating log entry.
288 - *
289 - * @since 1.0
290 - *
291 - * @param array $log_data Log entry data.
292 - * @param array $log_meta Log entry meta.
293 - */
294 - do_action( 'give_pre_update_log', $log_data, $log_meta );
295 -
296 - $defaults = array(
297 - 'log_parent' => 0,
298 -
299 - // Backward compatibility.
300 - 'post_type' => 'give_log',
301 - 'post_status' => 'publish',
302 - );
303 -
304 - $args = wp_parse_args( $log_data, $defaults );
305 - $this->bc_200_validate_params( $args, $log_meta );
306 -
307 - // Store the log entry
308 - if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
309 - // Backward compatibility.
310 - $log_id = wp_update_post( $args );
311 -
312 - if ( $log_id && ! empty( $log_meta ) ) {
313 - foreach ( (array) $log_meta as $key => $meta ) {
314 - if ( ! empty( $meta ) ) {
315 - give_update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta );
316 - }
317 - }
318 - }
319 - } else {
320 - $log_id = $this->log_db->add( $args );
321 -
322 - if ( $log_id && ! empty( $log_meta ) ) {
323 - foreach ( (array) $log_meta as $key => $meta ) {
324 - if ( ! empty( $meta ) ) {
325 - $this->logmeta_db->update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta );
326 - }
327 - }
328 - }
329 - }
330 -
331 - /**
332 - * Fires after updating log entry.
333 - *
334 - * @since 1.0
335 - *
336 - * @param int $log_id Log entry id.
337 - * @param array $log_data Log entry data.
338 - * @param array $log_meta Log entry meta.
339 - */
340 - do_action( 'give_post_update_log', $log_id, $log_data, $log_meta );
237 + public function update_log( $log_data = [], $log_meta = [] ) {
238 + return $this->insert_log( $log_data, $log_meta );
341 239 }
342 240
343 241 /**
344 242 * Retrieve all connected logs
@@ -345,8 +243,10 @@
345 243 *
346 244 * Used for retrieving logs related to particular items, such as a specific donation.
347 245 * For new table params check: Give_DB_Logs::get_column_defaults and Give_DB_Logs::get_sql#L262
348 246 *
247 + * @deprecated 2.10.0
248 + *
349 249 * @since 1.0
350 250 * @since 2.0 Added new table logic.
351 251 * @access public
352 252 *
@@ -353,32 +253,10 @@
353 253 * @param array $args Query arguments.
354 254 *
355 255 * @return array|false Array if logs were found, false otherwise.
356 256 */
357 - public function get_connected_logs( $args = array() ) {
358 - $logs = array();
359 -
360 - $defaults = array(
361 - 'number' => 20,
362 - 'paged' => get_query_var( 'paged' ),
363 - 'log_type' => false,
364 -
365 - // Backward compatibility.
366 - 'post_type' => 'give_log',
367 - 'post_status' => 'publish',
368 - );
369 - $query_args = wp_parse_args( $args, $defaults );
370 - $this->bc_200_validate_params( $query_args );
371 -
372 - if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
373 - // Backward compatibility.
374 - $logs = get_posts( $query_args );
375 - $this->bc_200_add_new_properties( $logs );
376 - } else {
377 - $logs = $this->log_db->get_logs( $query_args );
378 - }
379 -
380 - return ( ! empty( $logs ) ? $logs : false );
257 + public function get_connected_logs( $args = [] ) {
258 + return false;
381 259 }
382 260
383 261 /**
384 262 * Retrieve Log Count
@@ -384,8 +262,12 @@
384 262 * Retrieve Log Count
385 263 *
386 264 * Retrieves number of log entries connected to particular object ID.
387 265 *
266 + * @deprecated 2.10.0
267 + * @use LogRepository::getTotalCount()
268 + * @see LogRepository
269 + *
388 270 * @since 1.0
389 271 * @access public
390 272 *
391 273 * @param int $object_id Log object ID. Default is 0.
@@ -395,45 +277,18 @@
395 277 *
396 278 * @return int Log count.
397 279 */
398 280 public function get_log_count( $object_id = 0, $type = '', $meta_query = null, $date_query = null ) {
399 - $logs_count = 0;
400 -
401 - $query_args = array(
402 - 'number' => - 1,
403 -
404 - // Backward comatibility.
405 - 'post_type' => 'give_log',
406 - 'post_status' => 'publish',
407 - );
408 -
409 281 if ( $object_id ) {
410 - $query_args['log_parent'] = $object_id;
282 + return 0;
411 283 }
412 284
413 - if ( ! empty( $type ) && $this->valid_type( $type ) ) {
414 - $query_args['log_type'] = $type;
285 + if ( ! empty( $type ) ) {
286 + $logs = $this->logRepository->getLogsByType( $type );
287 + return count( $logs );
415 288 }
416 289
417 - if ( ! empty( $meta_query ) ) {
418 - $query_args['meta_query'] = $meta_query;
419 - }
420 -
421 - if ( ! empty( $date_query ) ) {
422 - $query_args['date_query'] = $date_query;
423 - }
424 -
425 - $this->bc_200_validate_params( $query_args );
426 -
427 - if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
428 - // Backward compatibility.
429 - $logs = new WP_Query( $query_args );
430 - $logs_count = (int) $logs->post_count;
431 - } else {
432 - $logs_count = $this->log_db->count( $query_args );
433 - }
434 -
435 - return $logs_count;
290 + return $this->logRepository->getTotalCount();
436 291 }
437 292
438 293 /**
439 294 * Delete Logs
@@ -439,8 +294,12 @@
439 294 * Delete Logs
440 295 *
441 296 * Remove log entries connected to particular object ID.
442 297 *
298 + * @deprecated 2.10.0
299 + * @use LogRepository::deleteLogs()
300 + * @see LogRepository
301 + *
443 302 * @since 1.0
444 303 * @access public
445 304 *
446 305 * @param int $object_id Log object ID. Default is 0.
@@ -449,298 +308,8 @@
449 308 *
450 309 * @return void
451 310 */
452 311 public function delete_logs( $object_id = 0, $type = '', $meta_query = null ) {
453 - $query_args = array(
454 - 'log_parent' => $object_id,
455 - 'number' => - 1,
456 - 'fields' => 'ID',
457 -
458 - // Backward compatibility.
459 - 'post_type' => 'give_log',
460 - 'post_status' => 'publish',
461 - );
462 -
463 - if ( ! empty( $type ) && $this->valid_type( $type ) ) {
464 - $query_args['log_type'] = $type;
465 - }
466 -
467 - if ( ! empty( $meta_query ) ) {
468 - $query_args['meta_query'] = $meta_query;
469 - }
470 -
471 - $this->bc_200_validate_params( $query_args );
472 -
473 - if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
474 - // Backward compatibility.
475 - $logs = get_posts( $query_args );
476 -
477 - if ( $logs ) {
478 - foreach ( $logs as $log ) {
479 - wp_delete_post( $log, true );
480 - }
481 - }
482 - } else {
483 - $logs = $this->log_db->get_logs( $query_args );
484 -
485 - if ( $logs ) {
486 - foreach ( $logs as $log ) {
487 - if ( $this->log_db->delete( $log->ID ) ) {
488 - $this->logmeta_db->delete_row( $log->ID );
489 - }
490 - }
491 - }
492 - }
493 -
494 - $this->delete_cache();
312 + $this->logRepository->deleteLogs();
495 313 }
496 314
497 - /**
498 - * Setup cron to delete log cache in background.
499 - *
500 - * @since 1.7
501 - * @access public
502 - *
503 - * @param int $post_id
504 - */
505 - public function background_process_delete_cache( $post_id ) {
506 - // Delete log cache immediately
507 - wp_schedule_single_event( time() - 5, 'give_delete_log_cache' );
508 - }
509 -
510 - /**
511 - * Delete all logging cache when form, log or payment updates
512 - *
513 - * @since 1.7
514 - * @access public
515 - *
516 - * @return bool
517 - */
518 - public function delete_cache() {
519 - // Add log related keys to delete.
520 - $cache_give_logs = Give_Cache::get_options_like( 'give_logs' );
521 - $cache_give_log_count = Give_Cache::get_options_like( 'log_count' );
522 -
523 - $cache_option_names = array_merge( $cache_give_logs, $cache_give_log_count );
524 -
525 - // Bailout.
526 - if ( empty( $cache_option_names ) ) {
527 - return false;
528 - }
529 -
530 - Give_Cache::delete( $cache_option_names );
531 - }
532 -
533 - /**
534 - * Validate query params.
535 - *
536 - * @since 2.0
537 - * @access private
538 - *
539 - * @param array $log_query
540 - * @param array $log_meta
541 - */
542 - private function bc_200_validate_params( &$log_query, &$log_meta = array() ) {
543 - $query_params = array(
544 - 'log_title' => 'post_title',
545 - 'log_parent' => 'post_parent',
546 - 'log_content' => 'post_content',
547 - 'log_type' => 'tax_query',
548 - 'log_date' => 'post_date',
549 - 'log_date_gmt' => 'post_date_gmt',
550 - 'number' => 'posts_per_page',
551 - 'meta_query' => 'meta_query',
552 - );
553 -
554 - if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
555 - // Set old params.
556 - foreach ( $query_params as $new_query_param => $old_query_param ) {
557 -
558 - if ( isset( $log_query[ $old_query_param ] ) && empty( $log_query[ $new_query_param ] ) ) {
559 - $log_query[ $new_query_param ] = $log_query[ $old_query_param ];
560 - continue;
561 - } elseif ( ! isset( $log_query[ $new_query_param ] ) ) {
562 - continue;
563 - } elseif( empty( $log_query[ $new_query_param ] ) ) {
564 - continue;
565 - }
566 -
567 - switch ( $new_query_param ) {
568 - case 'log_type':
569 - $log_query['tax_query'] = array(
570 - array(
571 - 'taxonomy' => 'give_log_type',
572 - 'field' => 'slug',
573 - 'terms' => $log_query[ $new_query_param ],
574 - ),
575 - );
576 - break;
577 -
578 - case 'meta_query':
579 - if( ! empty( $log_query['meta_query'] ) && empty( $log_query['post_parent'] ) ) {
580 - foreach ( $log_query['meta_query'] as $index => $meta_query ){
581 - if( ! is_array( $meta_query ) || empty( $meta_query['key'] ) ) {
582 - continue;
583 - }
584 -
585 - switch ( $meta_query['key'] ) {
586 - case '_give_log_form_id':
587 - $log_query['post_parent'] = $meta_query['value'];
588 - unset( $log_query['meta_query'][$index] );
589 - break;
590 - }
591 - }
592 - }
593 - break;
594 -
595 - default:
596 - switch( $new_query_param ){
597 - case 'log_parent':
598 - $log_query['meta_query'][] = array(
599 - 'key' => '_give_log_payment_id',
600 - 'value' => $log_query[ $new_query_param ]
601 - );
602 -
603 - break;
604 -
605 - default:
606 - $log_query[ $old_query_param ] = $log_query[ $new_query_param ];
607 - }
608 - }
609 - }
610 - } else {
611 - // Set only old params.
612 - $query_params = array_flip( $query_params );
613 - foreach ( $query_params as $old_query_param => $new_query_param ) {
614 - if ( isset( $log_query[ $new_query_param ] ) && empty( $log_query[ $old_query_param ] ) ) {
615 - $log_query[ $old_query_param ] = $log_query[ $new_query_param ];
616 - continue;
617 - } elseif ( ! isset( $log_query[ $old_query_param ] ) ) {
618 - continue;
619 - }
620 -
621 - switch ( $old_query_param ) {
622 - case 'tax_query':
623 - if ( isset( $log_query[ $old_query_param ][0]['terms'] ) ) {
624 - $log_query[ $new_query_param ] = $log_query[ $old_query_param ][0]['terms'];
625 - }
626 - break;
627 -
628 - default:
629 - $log_query[ $new_query_param ] = $log_query[ $old_query_param ];
630 - }
631 - }
632 - }
633 - }
634 -
635 - /**
636 - * Set new log properties.
637 - *
638 - * @since 2.0
639 - * @access private
640 - *
641 - * @param array $logs
642 - */
643 - private function bc_200_add_new_properties( &$logs ) {
644 - if ( empty( $logs ) ) {
645 - return;
646 - }
647 -
648 - $query_params = array(
649 - 'log_title' => 'post_title',
650 - 'log_parent' => 'post_parent',
651 - 'log_content' => 'post_content',
652 - 'log_date' => 'post_date',
653 - 'log_date_gmt' => 'post_date_gmt',
654 - 'log_type' => 'give_log_type',
655 - );
656 -
657 - if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
658 - foreach ( $logs as $index => $log ) {
659 - foreach ( $query_params as $new_query_param => $old_query_param ) {
660 - if ( ! property_exists( $log, $old_query_param ) ) {
661 - /**
662 - * Set unmatched properties.
663 - */
664 -
665 - // 1. log_type
666 - $term = get_the_terms( $log->ID, 'give_log_type' );
667 - $term = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
668 -
669 - $logs[ $index ]->{$new_query_param} = ! empty( $term ) ? $term->slug : '';
670 -
671 - continue;
672 - }
673 -
674 - switch ( $old_query_param ) {
675 - case 'post_parent':
676 - $logs[ $index ]->{$new_query_param} = give_get_meta( $log->ID, '_give_log_payment_id', true );
677 - break;
678 -
679 - default:
680 - $logs[ $index ]->{$new_query_param} = $log->{$old_query_param};
681 - }
682 - }
683 - }
684 - }
685 - }
686 -
687 - /**
688 - * Change log parent to payment if set to form.
689 - *
690 - * @since 2.0
691 - * @access public
692 - *
693 - * @param mixed $check
694 - * @param int $log_id
695 - * @param array $meta_key
696 - * @param array $meta_value
697 - *
698 - * @return mixed
699 - */
700 - public function bc_200_set_payment_as_log_parent( $check, $log_id, $meta_key, $meta_value ) {
701 - global $wpdb;
702 - $update_status = false;
703 - $post_type = get_post_type( $log_id );
704 -
705 - // Bailout.
706 - if (
707 - 'give_payment' === $post_type ||
708 - '_give_log_payment_id' !== $meta_key
709 - ) {
710 - return $check;
711 - }
712 -
713 - $form_id = $wpdb->get_var(
714 - $wpdb->prepare(
715 - "
716 - SELECT log_parent FROM {$this->log_db->table_name}
717 - WHERE ID=%d
718 - ",
719 - $log_id
720 - )
721 - );
722 -
723 - if ( $form_id ) {
724 - $this->logmeta_db->delete_meta( $log_id, '_give_log_payment_id' );
725 - $this->logmeta_db->update_meta( $log_id, '_give_log_form_id', $form_id );
726 -
727 - $update_status = $wpdb->update(
728 - $this->log_db->table_name,
729 - array(
730 - 'log_parent' => $meta_value,
731 - ),
732 - array(
733 - 'ID' => $log_id,
734 - ),
735 - array(
736 - '%s',
737 - ),
738 - array(
739 - '%d',
740 - )
741 - );
742 - }
743 -
744 - return $update_status;
745 - }
746 315 }