PluginProbe
BeyondWords – AI audio for publishers / 4.6.1
BeyondWords – AI audio for publishers v4.6.1
7.2.0 7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 All 44 releases
speechkit / src / Component / Settings / Preselect / Preselect.php

Preselect.php in BeyondWords – AI audio for publishers 4.6.1, at src/Component/Settings/Preselect/Preselect.php

293 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * Setting: Preselect (labelled as "Preselect ‘Generate audio’")
7 *
8 * @package Beyondwords\Wordpress
9 * @author Stuart McAlpine <[email protected]>
10 * @since 3.0.0
11 */
12
13 namespace Beyondwords\Wordpress\Component\Settings\Preselect;
14
15 use Beyondwords\Wordpress\Component\Settings\SettingsUtils;
16
17 /**
18 * Preselect setup
19 *
20 * @since 3.0.0
21 */
22 class Preselect
23 {
24 public const DEFAULT_PRESELECT = [
25 'post' => '1',
26 'page' => '1',
27 ];
28
29 /**
30 * Init.
31 *
32 * @since 4.0.0
33 */
34 public function init()
35 {
36 add_action('admin_init', array($this, 'registerSetting'));
37 add_action('admin_init', array($this, 'addSettingsField'));
38 add_action('admin_enqueue_scripts', array($this, 'enqueueScripts'));
39 }
40
41 /**
42 * Init setting.
43 *
44 * @since 3.0.0
45 *
46 * @return void
47 */
48 public function registerSetting()
49 {
50 if (! SettingsUtils::hasApiSettings()) {
51 return;
52 }
53
54 register_setting(
55 'beyondwords',
56 'beyondwords_preselect',
57 [
58 'default' => Preselect::DEFAULT_PRESELECT,
59 ]
60 );
61 }
62
63 /**
64 * Init setting.
65 *
66 * @since 3.0.0
67 *
68 * @return void
69 */
70 public function addSettingsField()
71 {
72 add_settings_field(
73 'beyondwords-preselect',
74 __('Preselect ‘Generate audio’', 'speechkit'),
75 array($this, 'render'),
76 'beyondwords',
77 'generate-audio'
78 );
79 }
80
81 /**
82 * Render setting field.
83 *
84 * @since 3.0.0
85 *
86 * @return void
87 **/
88 public function render()
89 {
90 $postTypes = SettingsUtils::getCompatiblePostTypes();
91
92 if (! is_array($postTypes) || count($postTypes) === 0) :
93 ?>
94 <p class="description">
95 <?php
96 esc_html_e(
97 'No compatible post types found. This plugin will only work with post types that support custom fields.', // phpcs:ignore Generic.Files.LineLength.TooLong
98 'speechkit'
99 );
100 ?>
101 </p>
102 <?php
103 return;
104 endif;
105
106 foreach ($postTypes as $name) :
107 $postType = get_post_type_object($name);
108 ?>
109 <div class="beyondwords-setting--preselect--post-type">
110 <label>
111 <input
112 type="checkbox"
113 name="beyondwords_preselect[<?php echo esc_attr($postType->name); ?>]"
114 value="1"
115 <?php checked($this->postTypeIsSelected($postType), true); ?>
116 />
117 <?php echo esc_html($postType->label); ?>
118 </label>
119 <?php $this->renderTaxonomyFields($postType); ?>
120 </div>
121 <?php
122 endforeach;
123 }
124
125 /**
126 * Get the taxonomy fields, as a hierarchical list of nested checkboxes.
127 *
128 * @since 3.0.0
129 *
130 * @return void
131 **/
132 public function renderTaxonomyFields($postType)
133 {
134 $taxonomies = get_object_taxonomies($postType->name, 'objects');
135
136 if ($taxonomies) {
137 ?>
138 <div class="beyondwords-setting--preselect--taxonomy" style="margin: 0.5rem 0;">
139 <?php
140 foreach ($taxonomies as $taxonomy) {
141 // Ignore the "Format" taxonomy (aside, etc)
142 if ($taxonomy->name === 'post_format') {
143 continue;
144 }
145 // todo enable for custom taxonomies, and add tests for them
146 if ($taxonomy->name !== 'category') {
147 continue;
148 }
149 ?>
150 <h4 style="margin: 0.5rem 0 0.5rem 1.5rem;"><?php echo esc_html($taxonomy->label); ?></h4>
151 <?php
152 $this->renderTaxonomyTerms($postType, $taxonomy);
153 }
154 ?>
155 </div>
156 <?php
157 }
158 }
159
160 /**
161 * Get the taxonomy terms, as a hierarchical list of nested checkboxes.
162 *
163 * @since 3.0.0
164 *
165 * @return void
166 **/
167 public function renderTaxonomyTerms($postType, $taxonomy, $parent = 0)
168 {
169 $terms = get_terms([
170 'taxonomy' => $taxonomy->name,
171 'hide_empty' => false,
172 'parent' => $parent,
173 ]);
174
175 if ($terms) {
176 ?>
177 <ul style="margin: 0; padding: 0; list-style:none;">
178 <?php
179 foreach ($terms as $term) :
180 $inputName = sprintf(
181 "beyondwords_preselect[%s][%s][]",
182 $postType->name,
183 $taxonomy->name
184 );
185 ?>
186 <li class="beyondwords-setting--preselect--term" style="margin: 0.5rem 0 0 1.5rem;">
187 <label>
188 <input
189 type="checkbox"
190 name="<?php echo esc_attr($inputName); ?>"
191 value="<?php echo esc_attr($term->term_id); ?>"
192 <?php checked($this->termIsSelected($postType, $taxonomy, $term), true) ?>
193 />
194 <?php echo esc_html($term->name); ?>
195 </label>
196 <?php $this->renderTaxonomyTerms($postType, $taxonomy, $term->term_id); ?>
197 </li>
198 <?php
199 endforeach;
200 ?>
201 </ul>
202 <?php
203 }
204 }
205
206 public function postTypeIsSelected($postType)
207 {
208 $preselect = get_option('beyondwords_preselect');
209
210 if (! is_array($preselect)) {
211 return false;
212 }
213
214 return array_key_exists($postType->name, $preselect) && $preselect[$postType->name] === '1';
215 }
216
217 public function taxonomyIsSelected($postType, $taxonomy)
218 {
219 $preselect = get_option('beyondwords_preselect');
220
221 if (! is_array($preselect)) {
222 return false;
223 }
224
225 if (! isset($preselect[$postType->name]) || ! is_array($preselect[$postType->name])) {
226 return false;
227 }
228
229 return in_array($taxonomy->name, $preselect[$postType->name]);
230 }
231
232 public function termIsSelected($postType, $taxonomy, $term)
233 {
234 $preselect = get_option('beyondwords_preselect');
235
236 if (! is_array($preselect)) {
237 return false;
238 }
239
240 if (! isset($preselect[$postType->name]) || ! is_array($preselect[$postType->name])) {
241 return false;
242 }
243
244 if (! isset($preselect[$postType->name][$taxonomy->name]) || ! is_array($preselect[$postType->name][$taxonomy->name])) { // phpcs:ignore Generic.Files.LineLength.TooLong
245 return false;
246 }
247
248 return in_array($term->term_id, $preselect[$postType->name][$taxonomy->name]);
249 }
250
251 /**
252 * Register the component scripts.
253 *
254 * @since 3.0.0
255 *
256 * @param string $hook Page hook
257 *
258 * @return void
259 */
260 public function enqueueScripts($hook)
261 {
262 if ($hook === 'settings_page_beyondwords') {
263 wp_enqueue_script(
264 'beyondwords-settings--preselect-settings',
265 BEYONDWORDS__PLUGIN_URI . 'src/Component/Settings/Preselect/settings.js',
266 ['jquery', 'underscore'],
267 BEYONDWORDS__PLUGIN_VERSION,
268 true
269 );
270 }
271
272 if ($hook === 'post.php' || $hook === 'post-new.php') {
273 wp_register_script(
274 'beyondwords-settings--preselect-post',
275 BEYONDWORDS__PLUGIN_URI . 'src/Component/Settings/Preselect/post.js',
276 ['jquery', 'underscore'],
277 BEYONDWORDS__PLUGIN_VERSION,
278 true
279 );
280
281 // Localize the script with new data
282 $data = [
283 'postType' => get_post_type(),
284 'preselect' => get_option('beyondwords_preselect'),
285 ];
286
287 wp_localize_script('beyondwords-settings--preselect-post', 'beyondwords', $data);
288
289 wp_enqueue_script('beyondwords-settings--preselect-post');
290 }
291 }
292 }
293