@@ -14,268 +14,268 @@ |
||
14 | 14 | |
15 | 15 | class ListTableController extends Controller |
16 | 16 | { |
17 | - /** |
|
18 | - * @return void |
|
19 | - * @action admin_action_approve |
|
20 | - */ |
|
21 | - public function approve() |
|
22 | - { |
|
23 | - if (Application::ID != filter_input(INPUT_GET, 'plugin')) { |
|
24 | - return; |
|
25 | - } |
|
26 | - check_admin_referer('approve-review_'.($postId = $this->getPostId())); |
|
27 | - wp_update_post([ |
|
28 | - 'ID' => $postId, |
|
29 | - 'post_status' => 'publish', |
|
30 | - ]); |
|
31 | - wp_safe_redirect(wp_get_referer()); |
|
32 | - exit; |
|
33 | - } |
|
17 | + /** |
|
18 | + * @return void |
|
19 | + * @action admin_action_approve |
|
20 | + */ |
|
21 | + public function approve() |
|
22 | + { |
|
23 | + if (Application::ID != filter_input(INPUT_GET, 'plugin')) { |
|
24 | + return; |
|
25 | + } |
|
26 | + check_admin_referer('approve-review_'.($postId = $this->getPostId())); |
|
27 | + wp_update_post([ |
|
28 | + 'ID' => $postId, |
|
29 | + 'post_status' => 'publish', |
|
30 | + ]); |
|
31 | + wp_safe_redirect(wp_get_referer()); |
|
32 | + exit; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param array $columns |
|
37 | - * @return array |
|
38 | - * @filter manage_.Application::POST_TYPE._posts_columns |
|
39 | - */ |
|
40 | - public function filterColumnsForPostType($columns) |
|
41 | - { |
|
42 | - $columns = Arr::consolidate($columns); |
|
43 | - $postTypeColumns = glsr()->postTypeColumns[Application::POST_TYPE]; |
|
44 | - foreach ($postTypeColumns as $key => &$value) { |
|
45 | - if (!array_key_exists($key, $columns) || !empty($value)) { |
|
46 | - continue; |
|
47 | - } |
|
48 | - $value = $columns[$key]; |
|
49 | - } |
|
50 | - if (count(glsr(Database::class)->getReviewsMeta('review_type')) < 2) { |
|
51 | - unset($postTypeColumns['review_type']); |
|
52 | - } |
|
53 | - return array_filter($postTypeColumns, 'strlen'); |
|
54 | - } |
|
35 | + /** |
|
36 | + * @param array $columns |
|
37 | + * @return array |
|
38 | + * @filter manage_.Application::POST_TYPE._posts_columns |
|
39 | + */ |
|
40 | + public function filterColumnsForPostType($columns) |
|
41 | + { |
|
42 | + $columns = Arr::consolidate($columns); |
|
43 | + $postTypeColumns = glsr()->postTypeColumns[Application::POST_TYPE]; |
|
44 | + foreach ($postTypeColumns as $key => &$value) { |
|
45 | + if (!array_key_exists($key, $columns) || !empty($value)) { |
|
46 | + continue; |
|
47 | + } |
|
48 | + $value = $columns[$key]; |
|
49 | + } |
|
50 | + if (count(glsr(Database::class)->getReviewsMeta('review_type')) < 2) { |
|
51 | + unset($postTypeColumns['review_type']); |
|
52 | + } |
|
53 | + return array_filter($postTypeColumns, 'strlen'); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param string $status |
|
58 | - * @param WP_Post $post |
|
59 | - * @return string |
|
60 | - * @filter post_date_column_status |
|
61 | - */ |
|
62 | - public function filterDateColumnStatus($status, $post) |
|
63 | - { |
|
64 | - if (Application::POST_TYPE == Arr::get($post, 'post_type')) { |
|
65 | - $status = __('Submitted', 'site-reviews'); |
|
66 | - } |
|
67 | - return $status; |
|
68 | - } |
|
56 | + /** |
|
57 | + * @param string $status |
|
58 | + * @param WP_Post $post |
|
59 | + * @return string |
|
60 | + * @filter post_date_column_status |
|
61 | + */ |
|
62 | + public function filterDateColumnStatus($status, $post) |
|
63 | + { |
|
64 | + if (Application::POST_TYPE == Arr::get($post, 'post_type')) { |
|
65 | + $status = __('Submitted', 'site-reviews'); |
|
66 | + } |
|
67 | + return $status; |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * @param array $hidden |
|
72 | - * @param WP_Screen $post |
|
73 | - * @return array |
|
74 | - * @filter default_hidden_columns |
|
75 | - */ |
|
76 | - public function filterDefaultHiddenColumns($hidden, $screen) |
|
77 | - { |
|
78 | - if (Arr::get($screen, 'id') == 'edit-'.Application::POST_TYPE) { |
|
79 | - $hidden = Arr::consolidate($hidden); |
|
80 | - $hidden = array_unique(array_merge($hidden, [ |
|
81 | - 'email', 'ip_address', 'response', 'reviewer', |
|
82 | - ])); |
|
83 | - } |
|
84 | - return $hidden; |
|
85 | - } |
|
70 | + /** |
|
71 | + * @param array $hidden |
|
72 | + * @param WP_Screen $post |
|
73 | + * @return array |
|
74 | + * @filter default_hidden_columns |
|
75 | + */ |
|
76 | + public function filterDefaultHiddenColumns($hidden, $screen) |
|
77 | + { |
|
78 | + if (Arr::get($screen, 'id') == 'edit-'.Application::POST_TYPE) { |
|
79 | + $hidden = Arr::consolidate($hidden); |
|
80 | + $hidden = array_unique(array_merge($hidden, [ |
|
81 | + 'email', 'ip_address', 'response', 'reviewer', |
|
82 | + ])); |
|
83 | + } |
|
84 | + return $hidden; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * @param array $actions |
|
89 | - * @param WP_Post $post |
|
90 | - * @return array |
|
91 | - * @filter post_row_actions |
|
92 | - */ |
|
93 | - public function filterRowActions($actions, $post) |
|
94 | - { |
|
95 | - if (Application::POST_TYPE != Arr::get($post, 'post_type') |
|
96 | - || 'trash' == $post->post_status |
|
97 | - || !user_can(get_current_user_id(), 'edit_post', $post->ID)) { |
|
98 | - return $actions; |
|
99 | - } |
|
100 | - unset($actions['inline hide-if-no-js']); //Remove Quick-edit |
|
101 | - $rowActions = [ |
|
102 | - 'approve' => esc_attr__('Approve', 'site-reviews'), |
|
103 | - 'unapprove' => esc_attr__('Unapprove', 'site-reviews'), |
|
104 | - ]; |
|
105 | - $newActions = []; |
|
106 | - foreach ($rowActions as $key => $text) { |
|
107 | - $newActions[$key] = glsr(Builder::class)->a($text, [ |
|
108 | - 'aria-label' => sprintf(esc_attr_x('%s this review', 'Approve the review', 'site-reviews'), $text), |
|
109 | - 'class' => 'glsr-change-status', |
|
110 | - 'href' => wp_nonce_url( |
|
111 | - admin_url('post.php?post='.$post->ID.'&action='.$key.'&plugin='.Application::ID), |
|
112 | - $key.'-review_'.$post->ID |
|
113 | - ), |
|
114 | - ]); |
|
115 | - } |
|
116 | - return $newActions + Arr::consolidate($actions); |
|
117 | - } |
|
87 | + /** |
|
88 | + * @param array $actions |
|
89 | + * @param WP_Post $post |
|
90 | + * @return array |
|
91 | + * @filter post_row_actions |
|
92 | + */ |
|
93 | + public function filterRowActions($actions, $post) |
|
94 | + { |
|
95 | + if (Application::POST_TYPE != Arr::get($post, 'post_type') |
|
96 | + || 'trash' == $post->post_status |
|
97 | + || !user_can(get_current_user_id(), 'edit_post', $post->ID)) { |
|
98 | + return $actions; |
|
99 | + } |
|
100 | + unset($actions['inline hide-if-no-js']); //Remove Quick-edit |
|
101 | + $rowActions = [ |
|
102 | + 'approve' => esc_attr__('Approve', 'site-reviews'), |
|
103 | + 'unapprove' => esc_attr__('Unapprove', 'site-reviews'), |
|
104 | + ]; |
|
105 | + $newActions = []; |
|
106 | + foreach ($rowActions as $key => $text) { |
|
107 | + $newActions[$key] = glsr(Builder::class)->a($text, [ |
|
108 | + 'aria-label' => sprintf(esc_attr_x('%s this review', 'Approve the review', 'site-reviews'), $text), |
|
109 | + 'class' => 'glsr-change-status', |
|
110 | + 'href' => wp_nonce_url( |
|
111 | + admin_url('post.php?post='.$post->ID.'&action='.$key.'&plugin='.Application::ID), |
|
112 | + $key.'-review_'.$post->ID |
|
113 | + ), |
|
114 | + ]); |
|
115 | + } |
|
116 | + return $newActions + Arr::consolidate($actions); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * @param array $columns |
|
121 | - * @return array |
|
122 | - * @filter manage_edit-.Application::POST_TYPE._sortable_columns |
|
123 | - */ |
|
124 | - public function filterSortableColumns($columns) |
|
125 | - { |
|
126 | - $columns = Arr::consolidate($columns); |
|
127 | - $postTypeColumns = glsr()->postTypeColumns[Application::POST_TYPE]; |
|
128 | - unset($postTypeColumns['cb']); |
|
129 | - foreach ($postTypeColumns as $key => $value) { |
|
130 | - if (Str::startsWith('taxonomy', $key)) { |
|
131 | - continue; |
|
132 | - } |
|
133 | - $columns[$key] = $key; |
|
134 | - } |
|
135 | - return $columns; |
|
136 | - } |
|
119 | + /** |
|
120 | + * @param array $columns |
|
121 | + * @return array |
|
122 | + * @filter manage_edit-.Application::POST_TYPE._sortable_columns |
|
123 | + */ |
|
124 | + public function filterSortableColumns($columns) |
|
125 | + { |
|
126 | + $columns = Arr::consolidate($columns); |
|
127 | + $postTypeColumns = glsr()->postTypeColumns[Application::POST_TYPE]; |
|
128 | + unset($postTypeColumns['cb']); |
|
129 | + foreach ($postTypeColumns as $key => $value) { |
|
130 | + if (Str::startsWith('taxonomy', $key)) { |
|
131 | + continue; |
|
132 | + } |
|
133 | + $columns[$key] = $key; |
|
134 | + } |
|
135 | + return $columns; |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * @param string $columnName |
|
140 | - * @param string $postType |
|
141 | - * @return void |
|
142 | - * @action bulk_edit_custom_box |
|
143 | - */ |
|
144 | - public function renderBulkEditFields($columnName, $postType) |
|
145 | - { |
|
146 | - if ('assigned_to' == $columnName && Application::POST_TYPE == $postType) { |
|
147 | - glsr()->render('partials/editor/bulk-edit-assigned-to'); |
|
148 | - } |
|
149 | - } |
|
138 | + /** |
|
139 | + * @param string $columnName |
|
140 | + * @param string $postType |
|
141 | + * @return void |
|
142 | + * @action bulk_edit_custom_box |
|
143 | + */ |
|
144 | + public function renderBulkEditFields($columnName, $postType) |
|
145 | + { |
|
146 | + if ('assigned_to' == $columnName && Application::POST_TYPE == $postType) { |
|
147 | + glsr()->render('partials/editor/bulk-edit-assigned-to'); |
|
148 | + } |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * @param string $postType |
|
153 | - * @return void |
|
154 | - * @action restrict_manage_posts |
|
155 | - */ |
|
156 | - public function renderColumnFilters($postType) |
|
157 | - { |
|
158 | - glsr(Columns::class)->renderFilters($postType); |
|
159 | - } |
|
151 | + /** |
|
152 | + * @param string $postType |
|
153 | + * @return void |
|
154 | + * @action restrict_manage_posts |
|
155 | + */ |
|
156 | + public function renderColumnFilters($postType) |
|
157 | + { |
|
158 | + glsr(Columns::class)->renderFilters($postType); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * @param string $column |
|
163 | - * @param string $postId |
|
164 | - * @return void |
|
165 | - * @action manage_posts_custom_column |
|
166 | - */ |
|
167 | - public function renderColumnValues($column, $postId) |
|
168 | - { |
|
169 | - glsr(Columns::class)->renderValues($column, $postId); |
|
170 | - } |
|
161 | + /** |
|
162 | + * @param string $column |
|
163 | + * @param string $postId |
|
164 | + * @return void |
|
165 | + * @action manage_posts_custom_column |
|
166 | + */ |
|
167 | + public function renderColumnValues($column, $postId) |
|
168 | + { |
|
169 | + glsr(Columns::class)->renderValues($column, $postId); |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * @param int $postId |
|
174 | - * @return void |
|
175 | - * @action save_post_.Application::POST_TYPE |
|
176 | - */ |
|
177 | - public function saveBulkEditFields($postId) |
|
178 | - { |
|
179 | - if (!glsr()->can('edit_posts')) { |
|
180 | - return; |
|
181 | - } |
|
182 | - $assignedTo = filter_input(INPUT_GET, 'assigned_to'); |
|
183 | - if ($assignedTo && get_post($assignedTo)) { |
|
184 | - glsr(Database::class)->update($postId, 'assigned_to', $assignedTo); |
|
185 | - } |
|
186 | - } |
|
172 | + /** |
|
173 | + * @param int $postId |
|
174 | + * @return void |
|
175 | + * @action save_post_.Application::POST_TYPE |
|
176 | + */ |
|
177 | + public function saveBulkEditFields($postId) |
|
178 | + { |
|
179 | + if (!glsr()->can('edit_posts')) { |
|
180 | + return; |
|
181 | + } |
|
182 | + $assignedTo = filter_input(INPUT_GET, 'assigned_to'); |
|
183 | + if ($assignedTo && get_post($assignedTo)) { |
|
184 | + glsr(Database::class)->update($postId, 'assigned_to', $assignedTo); |
|
185 | + } |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * @return void |
|
190 | - * @action pre_get_posts |
|
191 | - */ |
|
192 | - public function setQueryForColumn(WP_Query $query) |
|
193 | - { |
|
194 | - if (!$this->hasPermission($query)) { |
|
195 | - return; |
|
196 | - } |
|
197 | - $this->setMetaQuery($query, [ |
|
198 | - 'rating', 'review_type', |
|
199 | - ]); |
|
200 | - $this->setOrderby($query); |
|
201 | - } |
|
188 | + /** |
|
189 | + * @return void |
|
190 | + * @action pre_get_posts |
|
191 | + */ |
|
192 | + public function setQueryForColumn(WP_Query $query) |
|
193 | + { |
|
194 | + if (!$this->hasPermission($query)) { |
|
195 | + return; |
|
196 | + } |
|
197 | + $this->setMetaQuery($query, [ |
|
198 | + 'rating', 'review_type', |
|
199 | + ]); |
|
200 | + $this->setOrderby($query); |
|
201 | + } |
|
202 | 202 | |
203 | - /** |
|
204 | - * @return void |
|
205 | - * @action admin_action_unapprove |
|
206 | - */ |
|
207 | - public function unapprove() |
|
208 | - { |
|
209 | - if (Application::ID != filter_input(INPUT_GET, 'plugin')) { |
|
210 | - return; |
|
211 | - } |
|
212 | - check_admin_referer('unapprove-review_'.($postId = $this->getPostId())); |
|
213 | - wp_update_post([ |
|
214 | - 'ID' => $postId, |
|
215 | - 'post_status' => 'pending', |
|
216 | - ]); |
|
217 | - wp_safe_redirect(wp_get_referer()); |
|
218 | - exit; |
|
219 | - } |
|
203 | + /** |
|
204 | + * @return void |
|
205 | + * @action admin_action_unapprove |
|
206 | + */ |
|
207 | + public function unapprove() |
|
208 | + { |
|
209 | + if (Application::ID != filter_input(INPUT_GET, 'plugin')) { |
|
210 | + return; |
|
211 | + } |
|
212 | + check_admin_referer('unapprove-review_'.($postId = $this->getPostId())); |
|
213 | + wp_update_post([ |
|
214 | + 'ID' => $postId, |
|
215 | + 'post_status' => 'pending', |
|
216 | + ]); |
|
217 | + wp_safe_redirect(wp_get_referer()); |
|
218 | + exit; |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * Check if the translation string can be modified. |
|
223 | - * @param string $domain |
|
224 | - * @return bool |
|
225 | - */ |
|
226 | - protected function canModifyTranslation($domain = 'default') |
|
227 | - { |
|
228 | - $screen = glsr_current_screen(); |
|
229 | - return 'default' == $domain |
|
230 | - && 'edit' == $screen->base |
|
231 | - && Application::POST_TYPE == $screen->post_type; |
|
232 | - } |
|
221 | + /** |
|
222 | + * Check if the translation string can be modified. |
|
223 | + * @param string $domain |
|
224 | + * @return bool |
|
225 | + */ |
|
226 | + protected function canModifyTranslation($domain = 'default') |
|
227 | + { |
|
228 | + $screen = glsr_current_screen(); |
|
229 | + return 'default' == $domain |
|
230 | + && 'edit' == $screen->base |
|
231 | + && Application::POST_TYPE == $screen->post_type; |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * @return bool |
|
236 | - */ |
|
237 | - protected function hasPermission(WP_Query $query) |
|
238 | - { |
|
239 | - global $pagenow; |
|
240 | - return is_admin() |
|
241 | - && $query->is_main_query() |
|
242 | - && Application::POST_TYPE == $query->get('post_type') |
|
243 | - && 'edit.php' == $pagenow; |
|
244 | - } |
|
234 | + /** |
|
235 | + * @return bool |
|
236 | + */ |
|
237 | + protected function hasPermission(WP_Query $query) |
|
238 | + { |
|
239 | + global $pagenow; |
|
240 | + return is_admin() |
|
241 | + && $query->is_main_query() |
|
242 | + && Application::POST_TYPE == $query->get('post_type') |
|
243 | + && 'edit.php' == $pagenow; |
|
244 | + } |
|
245 | 245 | |
246 | - /** |
|
247 | - * @return void |
|
248 | - */ |
|
249 | - protected function setMetaQuery(WP_Query $query, array $metaKeys) |
|
250 | - { |
|
251 | - foreach ($metaKeys as $key) { |
|
252 | - $value = (string) filter_input(INPUT_GET, $key); |
|
253 | - if ('' === $value) { |
|
254 | - continue; |
|
255 | - } |
|
256 | - $metaQuery = (array) $query->get('meta_query'); |
|
257 | - $metaQuery[] = [ |
|
258 | - 'key' => Str::prefix('_', $key, '_'), |
|
259 | - 'value' => $value, |
|
260 | - ]; |
|
261 | - $query->set('meta_query', array_filter($metaQuery)); |
|
262 | - } |
|
263 | - } |
|
246 | + /** |
|
247 | + * @return void |
|
248 | + */ |
|
249 | + protected function setMetaQuery(WP_Query $query, array $metaKeys) |
|
250 | + { |
|
251 | + foreach ($metaKeys as $key) { |
|
252 | + $value = (string) filter_input(INPUT_GET, $key); |
|
253 | + if ('' === $value) { |
|
254 | + continue; |
|
255 | + } |
|
256 | + $metaQuery = (array) $query->get('meta_query'); |
|
257 | + $metaQuery[] = [ |
|
258 | + 'key' => Str::prefix('_', $key, '_'), |
|
259 | + 'value' => $value, |
|
260 | + ]; |
|
261 | + $query->set('meta_query', array_filter($metaQuery)); |
|
262 | + } |
|
263 | + } |
|
264 | 264 | |
265 | - /** |
|
266 | - * @return void |
|
267 | - */ |
|
268 | - protected function setOrderby(WP_Query $query) |
|
269 | - { |
|
270 | - $orderby = $query->get('orderby'); |
|
271 | - $columns = glsr()->postTypeColumns[Application::POST_TYPE]; |
|
272 | - unset($columns['cb'], $columns['title'], $columns['date']); |
|
273 | - if (in_array($orderby, array_keys($columns))) { |
|
274 | - if ('reviewer' == $orderby) { |
|
275 | - $orderby = 'author'; |
|
276 | - } |
|
277 | - $query->set('meta_key', Str::prefix('_', $orderby, '_')); |
|
278 | - $query->set('orderby', 'meta_value'); |
|
279 | - } |
|
280 | - } |
|
265 | + /** |
|
266 | + * @return void |
|
267 | + */ |
|
268 | + protected function setOrderby(WP_Query $query) |
|
269 | + { |
|
270 | + $orderby = $query->get('orderby'); |
|
271 | + $columns = glsr()->postTypeColumns[Application::POST_TYPE]; |
|
272 | + unset($columns['cb'], $columns['title'], $columns['date']); |
|
273 | + if (in_array($orderby, array_keys($columns))) { |
|
274 | + if ('reviewer' == $orderby) { |
|
275 | + $orderby = 'author'; |
|
276 | + } |
|
277 | + $query->set('meta_key', Str::prefix('_', $orderby, '_')); |
|
278 | + $query->set('orderby', 'meta_value'); |
|
279 | + } |
|
280 | + } |
|
281 | 281 | } |
@@ -8,137 +8,137 @@ |
||
8 | 8 | |
9 | 9 | abstract class DefaultsAbstract |
10 | 10 | { |
11 | - /** |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - protected $callable = [ |
|
15 | - 'defaults', 'filter', 'merge', 'restrict', 'unguarded', |
|
16 | - ]; |
|
17 | - |
|
18 | - /** |
|
19 | - * @var array |
|
20 | - */ |
|
21 | - protected $guarded = []; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $mapped = []; |
|
27 | - |
|
28 | - /** |
|
29 | - * @param string $name |
|
30 | - * @return void|array |
|
31 | - */ |
|
32 | - public function __call($name, array $args = []) |
|
33 | - { |
|
34 | - if (!method_exists($this, $name) || !in_array($name, $this->callable)) { |
|
35 | - return; |
|
36 | - } |
|
37 | - $args[0] = $this->mapKeys(Arr::get($args, 0, [])); |
|
38 | - $defaults = call_user_func_array([$this, $name], $args); |
|
39 | - $hookName = (new ReflectionClass($this))->getShortName(); |
|
40 | - $hookName = str_replace('Defaults', '', $hookName); |
|
41 | - $hookName = Str::dashCase($hookName); |
|
42 | - return apply_filters('site-reviews/defaults/'.$hookName, $defaults, $name); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * @return array |
|
47 | - */ |
|
48 | - abstract protected function defaults(); |
|
49 | - |
|
50 | - /** |
|
51 | - * @return array |
|
52 | - */ |
|
53 | - protected function filter(array $values = []) |
|
54 | - { |
|
55 | - return $this->normalize($this->merge(array_filter($values)), $values); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @return string |
|
60 | - */ |
|
61 | - protected function filteredJson(array $values = []) |
|
62 | - { |
|
63 | - $defaults = $this->flattenArrayValues( |
|
64 | - array_diff_key($this->defaults(), array_flip($this->guarded)) |
|
65 | - ); |
|
66 | - $values = $this->flattenArrayValues( |
|
67 | - shortcode_atts($defaults, $values) |
|
68 | - ); |
|
69 | - $filtered = array_filter(array_diff_assoc($values, $defaults), function ($value) { |
|
70 | - return !$this->isEmpty($value); |
|
71 | - }); |
|
72 | - return json_encode($filtered, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @return array |
|
77 | - */ |
|
78 | - protected function flattenArrayValues(array $values) |
|
79 | - { |
|
80 | - array_walk($values, function (&$value) { |
|
81 | - if (!is_array($value)) { |
|
82 | - return; |
|
83 | - } |
|
84 | - $value = implode(',', $value); |
|
85 | - }); |
|
86 | - return $values; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @param mixed $var |
|
91 | - * @return bool |
|
92 | - */ |
|
93 | - protected function isEmpty($var) |
|
94 | - { |
|
95 | - return !is_numeric($var) && !is_bool($var) && empty($var); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @return array |
|
100 | - */ |
|
101 | - protected function mapKeys(array $args) |
|
102 | - { |
|
103 | - foreach ($this->mapped as $old => $new) { |
|
104 | - if (array_key_exists($old, $args)) { |
|
105 | - $args[$new] = $args[$old]; |
|
106 | - unset($args[$old]); |
|
107 | - } |
|
108 | - } |
|
109 | - return $args; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @return array |
|
114 | - */ |
|
115 | - protected function merge(array $values = []) |
|
116 | - { |
|
117 | - return $this->normalize(wp_parse_args($values, $this->defaults()), $values); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @return array |
|
122 | - */ |
|
123 | - protected function normalize(array $values, array $originalValues) |
|
124 | - { |
|
125 | - $values['json'] = $this->filteredJson($originalValues); |
|
126 | - return $values; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - protected function restrict(array $values = []) |
|
133 | - { |
|
134 | - return $this->normalize(shortcode_atts($this->defaults(), $values), $values); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @return array |
|
139 | - */ |
|
140 | - protected function unguarded() |
|
141 | - { |
|
142 | - return array_diff_key($this->defaults(), array_flip($this->guarded)); |
|
143 | - } |
|
11 | + /** |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + protected $callable = [ |
|
15 | + 'defaults', 'filter', 'merge', 'restrict', 'unguarded', |
|
16 | + ]; |
|
17 | + |
|
18 | + /** |
|
19 | + * @var array |
|
20 | + */ |
|
21 | + protected $guarded = []; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $mapped = []; |
|
27 | + |
|
28 | + /** |
|
29 | + * @param string $name |
|
30 | + * @return void|array |
|
31 | + */ |
|
32 | + public function __call($name, array $args = []) |
|
33 | + { |
|
34 | + if (!method_exists($this, $name) || !in_array($name, $this->callable)) { |
|
35 | + return; |
|
36 | + } |
|
37 | + $args[0] = $this->mapKeys(Arr::get($args, 0, [])); |
|
38 | + $defaults = call_user_func_array([$this, $name], $args); |
|
39 | + $hookName = (new ReflectionClass($this))->getShortName(); |
|
40 | + $hookName = str_replace('Defaults', '', $hookName); |
|
41 | + $hookName = Str::dashCase($hookName); |
|
42 | + return apply_filters('site-reviews/defaults/'.$hookName, $defaults, $name); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * @return array |
|
47 | + */ |
|
48 | + abstract protected function defaults(); |
|
49 | + |
|
50 | + /** |
|
51 | + * @return array |
|
52 | + */ |
|
53 | + protected function filter(array $values = []) |
|
54 | + { |
|
55 | + return $this->normalize($this->merge(array_filter($values)), $values); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @return string |
|
60 | + */ |
|
61 | + protected function filteredJson(array $values = []) |
|
62 | + { |
|
63 | + $defaults = $this->flattenArrayValues( |
|
64 | + array_diff_key($this->defaults(), array_flip($this->guarded)) |
|
65 | + ); |
|
66 | + $values = $this->flattenArrayValues( |
|
67 | + shortcode_atts($defaults, $values) |
|
68 | + ); |
|
69 | + $filtered = array_filter(array_diff_assoc($values, $defaults), function ($value) { |
|
70 | + return !$this->isEmpty($value); |
|
71 | + }); |
|
72 | + return json_encode($filtered, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @return array |
|
77 | + */ |
|
78 | + protected function flattenArrayValues(array $values) |
|
79 | + { |
|
80 | + array_walk($values, function (&$value) { |
|
81 | + if (!is_array($value)) { |
|
82 | + return; |
|
83 | + } |
|
84 | + $value = implode(',', $value); |
|
85 | + }); |
|
86 | + return $values; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @param mixed $var |
|
91 | + * @return bool |
|
92 | + */ |
|
93 | + protected function isEmpty($var) |
|
94 | + { |
|
95 | + return !is_numeric($var) && !is_bool($var) && empty($var); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @return array |
|
100 | + */ |
|
101 | + protected function mapKeys(array $args) |
|
102 | + { |
|
103 | + foreach ($this->mapped as $old => $new) { |
|
104 | + if (array_key_exists($old, $args)) { |
|
105 | + $args[$new] = $args[$old]; |
|
106 | + unset($args[$old]); |
|
107 | + } |
|
108 | + } |
|
109 | + return $args; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @return array |
|
114 | + */ |
|
115 | + protected function merge(array $values = []) |
|
116 | + { |
|
117 | + return $this->normalize(wp_parse_args($values, $this->defaults()), $values); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @return array |
|
122 | + */ |
|
123 | + protected function normalize(array $values, array $originalValues) |
|
124 | + { |
|
125 | + $values['json'] = $this->filteredJson($originalValues); |
|
126 | + return $values; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + protected function restrict(array $values = []) |
|
133 | + { |
|
134 | + return $this->normalize(shortcode_atts($this->defaults(), $values), $values); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @return array |
|
139 | + */ |
|
140 | + protected function unguarded() |
|
141 | + { |
|
142 | + return array_diff_key($this->defaults(), array_flip($this->guarded)); |
|
143 | + } |
|
144 | 144 | } |