PluginProbe
Age Gate / 3.7.3
Age Gate v3.7.3
3.7.3 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 All 113 releases
age-gate / src / Admin / Post / ListTable.php

ListTable.php in Age Gate 3.7.3, at src/Admin/Post/ListTable.php

171 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AgeGate\Admin\Post;
4
5 use AgeGate\Common\Content;
6 use AgeGate\Common\Settings;
7 use AgeGate\Common\Immutable\Constants;
8 use Jawira\CaseConverter\Convert;
9 use AgeGate\Admin\Post\PostTrait as Post;
10
11 class ListTable
12 {
13 use Post;
14
15 private $settings;
16 private $view;
17
18 public function __construct($view)
19 {
20 $this->view = $view;
21 $this->settings = Settings::getInstance();
22
23 add_filter('manage_posts_columns', [$this, 'column'], 10000);
24 add_filter('manage_pages_columns', [$this, 'column'], 10000);
25
26 add_action('manage_posts_custom_column', [$this, 'data'], 10, 2);
27 add_action('manage_pages_custom_column', [$this, 'data'], 10, 2);
28
29 if (current_user_can(Constants::SET_CONTENT)) {
30 add_action('init', [$this, 'actions']);
31 }
32 }
33
34 /**
35 * Register column
36 *
37 * @param array $columns
38 * @return array
39 */
40 public function column($columns)
41 {
42 global $typenow;
43
44 $disable = array_key_exists($typenow, $this->settings->disable ?: []);
45
46
47 if (!$disable) {
48 $columns['age_gate'] = '<span data-ag-tooltip="' . esc_attr__('Age Gate', 'age-gate') . '"><i class="wp-menu-image dashicons-before dashicons-lock"></i></span> <span class="screen-reader-text">Age Gate</span>';
49 }
50
51 return $columns;
52 }
53
54 /**
55 * Output custom column
56 *
57 * @param string $column
58 * @param int $postId
59 * @return void
60 */
61 public function data($column, $postId)
62 {
63 if ($column !== 'age_gate') {
64 return;
65 }
66
67
68 echo $this->view->addData(['content' => new Content($postId)])->render('post/list-column'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
69 }
70
71 /**
72 * Register bulk actions on post types
73 *
74 * @return void
75 */
76 public function actions()
77 {
78
79 $postTypes = get_post_types(
80 [
81 'public' => true
82 ]
83 );
84
85
86 foreach ($postTypes as $postType) {
87 $settings = Settings::getInstance();
88 $disable = array_key_exists($postType, $settings->disable ?: []);
89
90
91 if (!$disable) {
92 add_filter('bulk_actions-edit-' . $postType, function ($actions) {
93 $actions['age_gate_restrict'] = esc_html__('Age Gate: Restrict', 'age-gate');
94 $actions['age_gate_unrestrict'] = esc_html__('Age Gate: Unrestrict', 'age-gate');
95
96
97 return $actions;
98 });
99
100 add_filter('handle_bulk_actions-edit-' . $postType, [$this, 'actionHandler'], 10, 3);
101 }
102 }
103 }
104
105 /**
106 * Handle bulk assigning
107 *
108 * @param string $redirect
109 * @param string $action
110 * @param array $posts
111 * @return string
112 */
113 public function actionHandler($redirect, $action, $posts)
114 {
115 if (!in_array($action, ['age_gate_restrict', 'age_gate_unrestrict'] )) {
116 return $redirect;
117 }
118
119 if (!current_user_can(Constants::SET_CONTENT)) {
120 return $redirect;
121 }
122
123 switch ($action) {
124 case 'age_gate_restrict':
125 $this->bulk('restrict', $posts);
126 break;
127 case 'age_gate_unrestrict':
128 $this->bulk('bypass', $posts);
129 break;
130 }
131
132 return $redirect;
133 }
134
135 /**
136 * Apply the bulk update
137 *
138 * @param string $action
139 * @param array $posts
140 * @return void
141 */
142 private function bulk(string $action, array $posts) : void
143 {
144 $settings = Settings::getInstance();
145
146 switch ($action) {
147 case 'restrict':
148
149 foreach ($posts as $postId) {
150 if ($settings->type === 'selected') {
151 update_post_meta($postId, Constants::META_RESTRICT, 1);
152 } else {
153 delete_post_meta($postId, Constants::META_BYPASS);
154 }
155 }
156
157 break;
158 case 'bypass':
159 foreach ($posts as $postId) {
160 if ($settings->type === 'selected') {
161 delete_post_meta($postId, Constants::META_RESTRICT);
162 } else {
163 update_post_meta($postId, Constants::META_BYPASS, 1);
164 }
165 }
166
167 break;
168 }
169 }
170 }
171