summaryrefslogtreecommitdiff
blob: f6adef19e6e14a8b085e4b3e2e54b54f73fce0ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

class scbQueryManipulation {

	private $bits = array();
	private $wp_query;

	private static $filters = array(
		'posts_where',
		'posts_join',
		'posts_groupby',
		'posts_orderby',
		'posts_distinct',
		'post_limits',
		'posts_fields'
	);

	public function __construct( $callback, $once = true ) {
		$this->callback = $callback;

		$this->enable();

		if ( !$once )
			return;

		add_filter( 'posts_request', array( $this, '_disable' ) );
	}

	function _disable( $request ) {
		remove_filter( 'posts_request', array( $this, '_disable' ) );

		$this->disable();

		return $request;
	}

	public function enable() {
		foreach ( self::$filters as $filter ) {
			add_filter( $filter, array( $this, 'collect' ), 999, 2 );
			add_filter( $filter . '_request' , array( $this, 'update' ), 9 );
		}

		add_action( 'posts_selection' , array( $this, 'alter' ) );
	}

	public function disable() {
		foreach ( self::$filters as $filter ) {
			remove_filter( $filter, array( $this, 'collect' ), 999, 2 );
			remove_filter( $filter . '_request' , array( $this, 'update' ), 9 );
		}

		remove_action( 'posts_selection' , array( $this, 'alter' ) );
	}

	function collect( $value, $wp_query ) {
		// remove 'posts_'
		$key = explode( '_', current_filter() );
		$key = array_slice( $key, 1 );
		$key = implode( '_', $key );

		$this->bits[ $key ] = $value;

		$this->wp_query = $wp_query;

		return $value;
	}

	function alter( $query ) {
		$this->bits = call_user_func( $this->callback, $this->bits, $this->wp_query );
	}

	function update( $value ) {
		// remove 'posts_' and '_request'
		$key = explode( '_', current_filter() );
		$key = array_slice( $key, 1, -1 );
		$key = implode( '_', $key );

		return $this->bits[ $key ];
	}
}