| 1 |
<?php |
| 2 |
/** |
| 3 |
* QueryMonitor Output |
| 4 |
* |
| 5 |
* @package ContentControl |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ContentControl\QueryMonitor; |
| 9 |
|
| 10 |
use QM_Output_Html; |
| 11 |
use QM_Collector; |
| 12 |
|
| 13 |
use function ContentControl\is_frontend; |
| 14 |
use function ContentControl\user_is_excluded; |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
/** |
| 19 |
* QueryMonitor Output |
| 20 |
*/ |
| 21 |
class Output extends QM_Output_Html { |
| 22 |
|
| 23 |
/** |
| 24 |
* Collector instance. |
| 25 |
* |
| 26 |
* @var QM_Collector Collector. |
| 27 |
*/ |
| 28 |
protected $collector; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor |
| 32 |
* |
| 33 |
* @param QM_Collector $collector Collector. |
| 34 |
*/ |
| 35 |
public function __construct( $collector ) { |
| 36 |
parent::__construct( $collector ); |
| 37 |
|
| 38 |
add_filter( 'qm/output/menus', [ $this, 'admin_menu' ], 101 ); |
| 39 |
add_filter( 'qm/output/title', [ $this, 'admin_title' ], 101 ); |
| 40 |
add_filter( 'qm/output/menu_class', [ $this, 'admin_class' ] ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Name of the output class. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function name() { |
| 49 |
return __( 'Content Control', 'content-control' ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Adds QM Memcache stats to admin panel |
| 54 |
* |
| 55 |
* @param array<string,string> $title Array of QM admin panel titles. |
| 56 |
* |
| 57 |
* @return array<string,string> |
| 58 |
*/ |
| 59 |
public function admin_title( $title ) { |
| 60 |
return $title; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Add content-control class |
| 65 |
* |
| 66 |
* @param array<string,string> $classes Array of QM classes. |
| 67 |
* |
| 68 |
* @return array<int<0,max>|string,string> |
| 69 |
*/ |
| 70 |
public function admin_class( $classes ) { |
| 71 |
$classes[] = 'qm-content-control'; |
| 72 |
|
| 73 |
return $classes; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Adds Memcache stats item to Query Monitor Menu |
| 78 |
* |
| 79 |
* @param array<string,array<string,string>> $_menu Array of QM admin menu items. |
| 80 |
* |
| 81 |
* @return array<string,array<string,string>> |
| 82 |
*/ |
| 83 |
public function admin_menu( $_menu ) { |
| 84 |
$menu = []; |
| 85 |
|
| 86 |
if ( is_frontend() ) { |
| 87 |
// Insert our panel right after qm-db_queries. |
| 88 |
$menu = array_slice( $_menu, 0, 1, true ); |
| 89 |
$menu['qm-content-control'] = $this->menu( [ |
| 90 |
'id' => 'qm-content-control', // This is the ID of the panel. |
| 91 |
'href' => '#qm-content-control', |
| 92 |
'title' => esc_html__( 'Content Control', 'content-control' ), |
| 93 |
] ); |
| 94 |
$menu += array_slice( $_menu, 1, null, true ); |
| 95 |
} |
| 96 |
|
| 97 |
return $menu; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Output the data. |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function output() { |
| 106 |
|
| 107 |
$this->before_non_tabular_output(); |
| 108 |
|
| 109 |
$this->output_global_settings(); |
| 110 |
|
| 111 |
$this->output_main_query_restrictions(); |
| 112 |
|
| 113 |
$this->output_post_restrictions(); |
| 114 |
|
| 115 |
$this->after_non_tabular_output(); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Output the data. |
| 120 |
* |
| 121 |
* @return void |
| 122 |
*/ |
| 123 |
public function output_global_settings() { |
| 124 |
$font_style = 'font-size: 14px!important;'; |
| 125 |
$admins_excluded = user_is_excluded(); |
| 126 |
$check_all_restrictions = apply_filters( 'content_control/check_all_restrictions', false ); |
| 127 |
|
| 128 |
// Render global settings. |
| 129 |
echo '<div style="gap: 20px;">'; |
| 130 |
echo '<h3 style="font-size: 18px!important;">Global Settings</h3>'; |
| 131 |
echo '<table class="qm-sortable">'; |
| 132 |
echo '<tbody>'; |
| 133 |
|
| 134 |
// Admins are excluded? |
| 135 |
echo '<tr>'; |
| 136 |
echo '<td style="' . esc_attr( $font_style ) . '">Admins Are Excluded?</td>'; |
| 137 |
echo '<td style="' . esc_attr( $font_style ) . ' color: ' . esc_attr( $admins_excluded ? 'red' : 'green' ) . '!important;">' . ( $admins_excluded ? 'Yes' : 'No' ) . '</td>'; |
| 138 |
echo '</tr>'; |
| 139 |
|
| 140 |
// check_all_restrictions filter is enabled? |
| 141 |
echo '<tr>'; |
| 142 |
echo '<td style="' . esc_attr( $font_style ) . '">Check All Restrictions Filter is Enabled?</td>'; |
| 143 |
echo '<td style="' . esc_attr( $font_style ) . 'color: ' . esc_attr( $check_all_restrictions ? 'green' : 'inherit' ) . ';">' . ( $check_all_restrictions ? 'Yes' : 'No' ) . '</td>'; |
| 144 |
echo '</tr>'; |
| 145 |
|
| 146 |
echo '</tbody>'; |
| 147 |
echo '</table>'; |
| 148 |
echo '</div>'; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Output the data. |
| 153 |
* |
| 154 |
* @return void |
| 155 |
*/ |
| 156 |
public function output_main_query_restrictions() { |
| 157 |
$font_style = 'font-size: 14px!important;'; |
| 158 |
$data = $this->get_collector()->get_data(); |
| 159 |
$main_query_restrictions = isset( $data->main_query_restriction ) ? $data->main_query_restriction : null; |
| 160 |
|
| 161 |
echo '<div>'; |
| 162 |
echo '<h3 style="font-size: 18px!important;">Main Query</h3>'; |
| 163 |
echo '<table class="qm-sortable">'; |
| 164 |
echo '<tbody>'; |
| 165 |
|
| 166 |
// Main query is restricted? |
| 167 |
echo '<tr>'; |
| 168 |
echo '<td style="' . esc_attr( $font_style ) . '">Main Query Is Restricted?</td>'; |
| 169 |
echo '<td style="' . esc_attr( $font_style ) . ' color: ' . esc_attr( null === $main_query_restrictions ? 'green' : 'red' ) . '!important;">' . ( $main_query_restrictions ? 'Yes' : 'No' ) . '</td>'; |
| 170 |
echo '</tr>'; |
| 171 |
|
| 172 |
// Main query restriction. |
| 173 |
echo '<tr>'; |
| 174 |
echo '<td style="' . esc_attr( $font_style ) . '">Main Query Restriction</td>'; |
| 175 |
echo '<td style="' . esc_attr( $font_style ) . '">' . ( $main_query_restrictions ? '<a href="' . esc_url_raw( $main_query_restrictions->get_edit_link() ) . '" target="_blank">' . esc_html( $main_query_restrictions->title ) . '</a>' : 'None' ) . '</td>'; |
| 176 |
echo '</tr>'; |
| 177 |
|
| 178 |
echo '</tbody>'; |
| 179 |
echo '</table>'; |
| 180 |
echo '</div>'; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Output the data. |
| 185 |
* |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
public function output_post_restrictions() { |
| 189 |
echo '<div style="width: 100%; margin-top: 20px; flex-grow: 4;">'; |
| 190 |
echo '<h3 style="font-size: 18px!important;">Post Restrictions</h3>'; |
| 191 |
echo '<table class="qm-sortable">'; |
| 192 |
// Render table of each post restrictions were checked for and the results, as well as which restriction (linked) was applied. |
| 193 |
echo '<thead>'; |
| 194 |
echo '<tr>'; |
| 195 |
echo '<th>Post ID</th>'; |
| 196 |
echo '<th>Post Title</th>'; |
| 197 |
echo '<th>Post Type</th>'; |
| 198 |
echo '<th>Context</th>'; |
| 199 |
echo '<th>User Can View</th>'; |
| 200 |
echo '<th>Restriction</th>'; |
| 201 |
echo '</tr>'; |
| 202 |
echo '</thead>'; |
| 203 |
echo '<tbody>'; |
| 204 |
|
| 205 |
$data = $this->get_collector()->get_data(); |
| 206 |
$posts = isset( $data->user_can_view_content ) ? $data->user_can_view_content : []; |
| 207 |
|
| 208 |
foreach ( $posts as $post_id => $post_data ) { |
| 209 |
if ( 'main' === $post_id ) { |
| 210 |
continue; |
| 211 |
} |
| 212 |
|
| 213 |
if ( strpos( $post_data['context'], 'terms' ) !== false ) { |
| 214 |
$term = get_term( $post_id ); |
| 215 |
$name = $term->name; |
| 216 |
$type = 'TAX: ' . $term->taxonomy; |
| 217 |
} elseif ( strpos( $post_data['context'], 'posts' ) !== false ) { |
| 218 |
$post = get_post( $post_id ); |
| 219 |
$name = $post->post_title; |
| 220 |
$type = 'PT: ' . $post->post_type; |
| 221 |
} else { |
| 222 |
continue; |
| 223 |
} |
| 224 |
|
| 225 |
$user_can_view = $post_data['user_can_view'] ? 'Yes' : 'No'; |
| 226 |
$restrictions = $post_data['restrictions'] ? $post_data['restrictions'] : []; |
| 227 |
|
| 228 |
echo '<tr>'; |
| 229 |
echo '<td>' . esc_html( $post_id ) . '</td>'; |
| 230 |
echo '<td>' . esc_html( $name ) . '</td>'; |
| 231 |
echo '<td>' . esc_html( $type ) . '</td>'; |
| 232 |
echo '<td>' . esc_html( $post_data['context'] ) . '</td>'; |
| 233 |
echo '<td>' . esc_html( $user_can_view ) . '</td>'; |
| 234 |
|
| 235 |
$restrictions_html = []; |
| 236 |
|
| 237 |
foreach ( $restrictions as $restriction ) { |
| 238 |
$restrictions_html[] = '<a href="' . esc_url_raw( $restriction->get_edit_link() ) . '" target="_blank">' . esc_html( $restriction->title ) . '</a>'; |
| 239 |
} |
| 240 |
|
| 241 |
echo '<td>' . wp_kses( join( ', ', $restrictions_html ), [ |
| 242 |
'a' => [ |
| 243 |
'href' => [], |
| 244 |
'target' => [], |
| 245 |
], |
| 246 |
] ) . '</td>'; |
| 247 |
echo '</tr>'; |
| 248 |
} |
| 249 |
|
| 250 |
echo '</tbody>'; |
| 251 |
echo '</table>'; |
| 252 |
echo '</div>'; |
| 253 |
} |
| 254 |
} |
| 255 |
|