| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Admin\Settings; |
| 4 |
|
| 5 |
use AgeGate\Common\Settings; |
| 6 |
use AgeGate\Admin\Taxonomy\TermHelper; |
| 7 |
|
| 8 |
trait Content |
| 9 |
{ |
| 10 |
protected function getContentFields() |
| 11 |
{ |
| 12 |
$postTypes = get_post_types(['public' => true], 'objects'); |
| 13 |
|
| 14 |
return [ |
| 15 |
[ |
| 16 |
'title' => __('Post Types', 'age-gate'), |
| 17 |
'subtitle' => __('Do not show Age Gate publish actions for the following post types', 'age-gate'), |
| 18 |
'fields' => collect($postTypes)->mapWithKeys(function ($postType) { |
| 19 |
if ($postType->name === 'attachment') { |
| 20 |
return []; |
| 21 |
} |
| 22 |
return [ |
| 23 |
'disable.' . $postType->name => [ |
| 24 |
'default' => false, |
| 25 |
'type' => 'checkbox', |
| 26 |
'attributes' => [ |
| 27 |
|
| 28 |
], |
| 29 |
'label' => $postType->label, |
| 30 |
] |
| 31 |
]; |
| 32 |
})->toArray(), |
| 33 |
], |
| 34 |
[ |
| 35 |
'title' => __('Ancestor inheritance', 'age-gate'), |
| 36 |
'subtitle' => __('Should hierarchical post types cascade restrictions', 'age-gate'), |
| 37 |
'fields' => collect($postTypes)->mapWithKeys(function ($postType) { |
| 38 |
if ($postType->hierarchical !== true) { |
| 39 |
return []; |
| 40 |
} |
| 41 |
return [ |
| 42 |
'ancestor.' . $postType->name => [ |
| 43 |
'default' => false, |
| 44 |
'type' => 'checkbox', |
| 45 |
'attributes' => [ |
| 46 |
|
| 47 |
], |
| 48 |
'label' => $postType->label, |
| 49 |
'sublabel' => __('ALPHA', 'age-gate'), |
| 50 |
] |
| 51 |
]; |
| 52 |
})->toArray(), |
| 53 |
], |
| 54 |
[ |
| 55 |
'title' => __('Taxonomy Inheritance', 'age-gate'), |
| 56 |
'model' => ['inherit'], |
| 57 |
'subtitle' => __('Content can inherit restrictions from its taxonomy. Note the most restrictive will be selected. See docs for more information', 'age-gate'), |
| 58 |
'fields' => [ |
| 59 |
'inherit' => [ |
| 60 |
'label' => __('Inherit taxonomies', 'age-gate'), |
| 61 |
'type' => 'checkbox', |
| 62 |
'default' => false, |
| 63 |
'attributes' => [ |
| 64 |
'x-on:change' => 'inherit = ! inherit' |
| 65 |
] |
| 66 |
], |
| 67 |
], |
| 68 |
], |
| 69 |
[ |
| 70 |
'title' => __('Taxonomies', 'age-gate'), |
| 71 |
'fields' => $this->getTaxonomyFields(), |
| 72 |
'condition' => [ |
| 73 |
'x-show' => 'inherit != \'\'', |
| 74 |
] |
| 75 |
], |
| 76 |
[ |
| 77 |
'title' => __('Errors', 'age-gate'), |
| 78 |
'subtitle' => __('How Age Gate should handle certain error pages', 'age-gate'), |
| 79 |
'fields' => [ |
| 80 |
'error_404' => [ |
| 81 |
'label' => __('404 error', 'age-gate'), |
| 82 |
'subtext' => 'Show Age Gate on 404 error', |
| 83 |
'type' => 'checkbox', |
| 84 |
'default' => false, |
| 85 |
], |
| 86 |
], |
| 87 |
], |
| 88 |
[ |
| 89 |
'title' => __('Archives', 'age-gate'), |
| 90 |
'subtitle' => __('How Age Gate should handle certain archive pages', 'age-gate'), |
| 91 |
'fields' => $this->getArchiveFields($postTypes), |
| 92 |
], |
| 93 |
[ |
| 94 |
'title' => __('Author archives', 'age-gate'), |
| 95 |
'subtitle' => __('How Age Gate should handle certain author pages', 'age-gate'), |
| 96 |
'fields' => $this->getAuthorFields($postTypes), |
| 97 |
], |
| 98 |
]; |
| 99 |
} |
| 100 |
|
| 101 |
private function getTaxonomyFields(): array |
| 102 |
{ |
| 103 |
$terms = collect(get_taxonomies(['public' => true, 'show_ui' => true], 'objects'))->mapWithKeys(function ($taxonomy, $slug) { |
| 104 |
return [ |
| 105 |
$slug => [ |
| 106 |
'type' => 'group', |
| 107 |
'label' => $taxonomy->label, |
| 108 |
'fields' => $this->getPaginatedTermFields($taxonomy), |
| 109 |
'parent' => 'terms', |
| 110 |
] |
| 111 |
]; |
| 112 |
})->toArray(); |
| 113 |
|
| 114 |
return $terms; |
| 115 |
} |
| 116 |
|
| 117 |
private function getTermFields($terms, $taxonomy): array |
| 118 |
{ |
| 119 |
$fields = []; |
| 120 |
|
| 121 |
foreach ($terms as $term) { |
| 122 |
foreach ($taxonomy->object_type ?? [] as $postType) { |
| 123 |
$fields[$term->term_id . '.' . $postType] = [ |
| 124 |
'type' => 'checkbox', |
| 125 |
'label' => 'Post', |
| 126 |
'subtext' => sprintf('%s (%s)', esc_html($term->name), esc_html($postType)), |
| 127 |
]; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return $fields; |
| 132 |
} |
| 133 |
|
| 134 |
private function getPaginatedTermFields($taxonomy) |
| 135 |
{ |
| 136 |
$count = 20; |
| 137 |
|
| 138 |
$fields = [ |
| 139 |
$taxonomy->name . '.terms' => [ |
| 140 |
'type' => 'paginated', |
| 141 |
'fields' => [], |
| 142 |
'total' => wp_count_terms(['taxonomy' => $taxonomy->name]), |
| 143 |
'current' => 1, |
| 144 |
'perpage' => $count, |
| 145 |
'taxonomy' => $taxonomy->name, |
| 146 |
'label' => $taxonomy->label, |
| 147 |
'pages' => (int) ceil(wp_count_terms(['taxonomy' => $taxonomy->name]) / 20), |
| 148 |
], |
| 149 |
]; |
| 150 |
|
| 151 |
return $fields; |
| 152 |
} |
| 153 |
|
| 154 |
private function getAuthorFields(): array |
| 155 |
{ |
| 156 |
$roles = []; |
| 157 |
$fields = []; |
| 158 |
$archives = []; |
| 159 |
|
| 160 |
foreach (wp_roles()->roles as $role_name => $role_obj) { |
| 161 |
if (! empty($role_obj['capabilities']['edit_posts'])) { |
| 162 |
$roles[] = $role_name; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
$users = get_users( |
| 167 |
[ |
| 168 |
'role__in' => $roles |
| 169 |
] |
| 170 |
); |
| 171 |
|
| 172 |
foreach ($users as $user) { |
| 173 |
$archives['user.' . $user->ID] = $user->display_name; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
$settings = Settings::getInstance(); |
| 178 |
|
| 179 |
// TODO:: Apply paermissions? |
| 180 |
foreach ($archives as $archive => $title) { |
| 181 |
$fields[$archive] = [ |
| 182 |
'type' => 'group', |
| 183 |
'label' => $title, |
| 184 |
'fields' => [ |
| 185 |
'bypass' => [ |
| 186 |
'type' => $settings->type === 'all' ? 'checkbox' : 'hidden', |
| 187 |
'label' => __('Bypass', 'age-gate'), |
| 188 |
'subtext' => __('Bypass', 'age-gate'), |
| 189 |
], |
| 190 |
'restrict' => [ |
| 191 |
'type' => $settings->type !== 'all' ? 'checkbox' : 'hidden', |
| 192 |
'label' => __('Restrict', 'age-gate'), |
| 193 |
'subtext' => __('Restrict', 'age-gate'), |
| 194 |
], |
| 195 |
'age' => [ |
| 196 |
'type' => $settings->multiAge ? 'number' : 'hidden', |
| 197 |
'label' => __('Age', 'age-gate'), |
| 198 |
'subtext' => __('Age', 'age-gate'), |
| 199 |
'attributes' => [ |
| 200 |
'class' => 'small-text' |
| 201 |
] |
| 202 |
] |
| 203 |
], |
| 204 |
]; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
return $fields; |
| 209 |
} |
| 210 |
|
| 211 |
private function getArchiveFields($postTypes): array |
| 212 |
{ |
| 213 |
$fields = []; |
| 214 |
|
| 215 |
|
| 216 |
$archives = [ |
| 217 |
'home' => __('Posts archive', 'age-gate'), |
| 218 |
'year' => __('Year archives', 'age-gate'), |
| 219 |
'month' => __('Month archives', 'age-gate'), |
| 220 |
'day' => __('Day archives', 'age-gate'), |
| 221 |
]; |
| 222 |
|
| 223 |
|
| 224 |
if (get_option('page_for_posts')) { |
| 225 |
unset($archives['home']); |
| 226 |
} |
| 227 |
|
| 228 |
foreach ($postTypes as $postType) { |
| 229 |
if ($postType->has_archive !== false) { |
| 230 |
if ($postType->name === 'product' && get_option('woocommerce_shop_page_id')) { |
| 231 |
continue; |
| 232 |
} |
| 233 |
$archives[$postType->name] = $postType->label; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
$settings = Settings::getInstance(); |
| 238 |
|
| 239 |
// TODO:: Apply paermissions? |
| 240 |
foreach ($archives as $archive => $title) { |
| 241 |
$fields['archives.' . $archive] = [ |
| 242 |
'type' => 'group', |
| 243 |
'label' => $title, |
| 244 |
'fields' => [ |
| 245 |
'bypass' => [ |
| 246 |
'type' => $settings->type === 'all' ? 'checkbox' : 'hidden', |
| 247 |
'label' => __('Bypass', 'age-gate'), |
| 248 |
'subtext' => __('Bypass', 'age-gate'), |
| 249 |
], |
| 250 |
'restrict' => [ |
| 251 |
'type' => $settings->type !== 'all' ? 'checkbox' : 'hidden', |
| 252 |
'label' => __('Restrict', 'age-gate'), |
| 253 |
'subtext' => __('Restrict', 'age-gate'), |
| 254 |
], |
| 255 |
'age' => [ |
| 256 |
'type' => $settings->multiAge ? 'number' : 'hidden', |
| 257 |
'label' => __('Age', 'age-gate'), |
| 258 |
'subtext' => __('Age', 'age-gate'), |
| 259 |
'attributes' => [ |
| 260 |
'class' => 'small-text' |
| 261 |
] |
| 262 |
] |
| 263 |
], |
| 264 |
]; |
| 265 |
} |
| 266 |
|
| 267 |
return $fields; |
| 268 |
} |
| 269 |
} |
| 270 |
|