Completed
Push — master ( 2a5bdf...b12cef )
by Sudar
08:15 queued 04:50
created

Bulk_Delete_By_Days::parse_query()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class that encapsulates the deletion of posts based on days.
4
 *
5
 * @author Sudar
6
 *
7
 * @package BulkDelete
8
 */
9
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
10
11
class Bulk_Delete_By_Days {
12
	public $days;
13
	public $op;
14
15
	/**
16
	 * Default constructor.
17
	 */
18
	public function __construct() {
19
		add_action( 'parse_query', array( $this, 'parse_query' ) );
20
	}
21
22
	/**
23
	 * Parse the query.
24
	 *
25
	 * @param array $query
26
	 */
27
	public function parse_query( $query ) {
28
		if ( isset( $query->query_vars['days'] ) ) {
29
			$this->days = $query->query_vars['days'];
30
			$this->op   = $query->query_vars['op'];
31
32
			add_filter( 'posts_where', array( $this, 'filter_where' ) );
33
			add_filter( 'posts_selection', array( $this, 'remove_where' ) );
34
		}
35
	}
36
37
	/**
38
	 * Modify the where clause.
39
	 *
40
	 * @param string $where (optional)
41
	 *
42
	 * @return string
43
	 */
44
	public function filter_where( $where = '' ) {
45
		$where .= ' AND post_date ' . $this->op . " '" . date( 'y-m-d', strtotime( '-' . $this->days . ' days' ) ) . "'";
46
47
		return $where;
48
	}
49
50
	/**
51
	 * Remove the `posts_where` filter.
52
	 */
53
	public function remove_where() {
54
		remove_filter( 'posts_where', array( $this, 'filter_where' ) );
55
	}
56
}
57