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