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