Collector.php
2.97 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* Content Control debug collector.
*
* @package ContentControl
*/
namespace ContentControl\QueryMonitor;
use ContentControl\QueryMonitor\Data;
use QM_DataCollector;
defined( 'ABSPATH' ) || exit;
/**
* Debug data collector.
*
* @phpstan-template T of \ContentControl\QueryMonitor\Data
*/
class Collector extends QM_DataCollector {
/**
* Collector ID.
*
* @var string
*/
public $id = 'content-control';
/**
* The data.
*
* @var Data
* @phpstan-var T
*/
protected $data;
/**
* Name of the output class.
*
* @return string
*/
public function name() {
return __( 'Content Control', 'content-control' );
}
/**
* Set up.
*
* @return void
*/
public function set_up() {
parent::set_up();
add_filter( 'content_control/user_can_view_content', [ $this, 'filter_user_can_view_content' ], 999999, 3 );
add_action( 'content_control/restrict_main_query', [ $this, 'action_restrict_main_query' ], 999999 );
add_action( 'content_control/restrict_main_query_post', [ $this, 'action_restrict_main_query_post' ], 999999, 2 );
}
/**
* Tear down.
*
* @return void
*/
public function tear_down() {
remove_filter( 'content_control/user_can_view_content', [ $this, 'filter_user_can_view_content' ], 999999 );
remove_action( 'content_control/restrict_main_query', [ $this, 'action_restrict_main_query' ], 999999 );
remove_action( 'content_control/restrict_main_query_post', [ $this, 'action_restrict_main_query_post' ], 999999 );
parent::tear_down();
}
/**
* Listen for user_can_view_content filter.
*
* @param bool $user_can_view Whether content is restricted.
* @param int|null $post_id Post ID.
* @param \ContentControl\Models\Restriction[] $restrictions Restrictions.
*
* @return bool
*/
public function filter_user_can_view_content( $user_can_view, $post_id, $restrictions ) {
$key = isset( $post_id ) ? $post_id : 'main';
$this->data->user_can_view_content[ $key ] = [
'user_can_view' => $user_can_view,
'post_id' => $post_id,
'restrictions' => $restrictions,
'context' => \ContentControl\current_query_context(),
];
return $user_can_view;
}
/**
* Listen for restrict_main_query action.
*
* @param \ContentControl\Models\Restriction $restriction Restriction.
*
* @return void
*/
public function action_restrict_main_query( $restriction ) {
$this->data->main_query_restriction = $restriction;
}
/**
* Listen for restrict_main_query action.
*
* @param \ContentControl\Models\Restriction $restriction Restriction.
* @param int[] $post_ids Post IDs.
*
* @return void
*/
public function action_restrict_main_query_post( $restriction, $post_ids ) {
$this->data->restrict_main_query_posts[] = [
'restriction' => $restriction,
'post_ids' => $post_ids,
];
}
/**
* Data to expose on the Query Monitor debug bar.
*
* @return void
*/
public function process() {
}
}