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