Completed
Pull Request — dev/5.7.0 (#195)
by
unknown
05:37 queued 02:48
created

Bulk_Delete_By_Days   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse_query() 0 7 2
A __construct() 0 2 1
A remove_where() 0 2 1
A filter_where() 0 4 1
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