* @since 3.0.0
*/
namespace Beyondwords\Wordpress\Component\Settings\Preselect;
use Beyondwords\Wordpress\Component\Settings\SettingsUtils;
/**
* Preselect setup
*
* @since 3.0.0
*/
class Preselect
{
public const DEFAULT_PRESELECT = [
'post' => '1',
'page' => '1',
];
/**
* Constructor
*/
public function __construct()
{
add_action('admin_init', array($this, 'init'));
add_action('admin_enqueue_scripts', array($this, 'enqueueScripts'));
}
/**
* Init setting.
*
* @since 3.0.0
*
* @return void
*/
public function init()
{
if (! SettingsUtils::hasApiSettings()) {
return;
}
register_setting(
'beyondwords',
'beyondwords_preselect',
[
'default' => Preselect::DEFAULT_PRESELECT,
]
);
add_settings_field(
'beyondwords-preselect',
__('Preselect ‘Generate audio’', 'speechkit'),
array($this, 'render'),
'beyondwords',
'generate-audio'
);
}
/**
* Render setting field.
*
* @since 3.0.0
*
* @return void
**/
public function render()
{
$postTypes = SettingsUtils::getSupportedPostTypes();
if (! is_array($postTypes) || count($postTypes) === 0) :
?>
postTypeIsSelected($postType), true); ?>
/>
label); ?>
renderTaxonomyFields($postType); ?>
name, 'objects');
if ($taxonomies) {
?>
name === 'post_format') {
continue;
}
// todo enable for custom taxonomies, and add tests for them
if ($taxonomy->name !== 'category') {
continue;
}
?>
label); ?>
renderTaxonomyTerms($postType, $taxonomy);
}
?>
$taxonomy->name,
'hide_empty' => false,
'parent' => $parent,
]);
if ($terms) {
?>
name, $preselect) && $preselect[$postType->name] === '1';
}
public function taxonomyIsSelected($postType, $taxonomy)
{
$preselect = get_option('beyondwords_preselect');
if (! is_array($preselect)) {
return false;
}
if (! isset($preselect[$postType->name]) || ! is_array($preselect[$postType->name])) {
return false;
}
return in_array($taxonomy->name, $preselect[$postType->name]);
}
public function termIsSelected($postType, $taxonomy, $term)
{
$preselect = get_option('beyondwords_preselect');
if (! is_array($preselect)) {
return false;
}
if (! isset($preselect[$postType->name]) || ! is_array($preselect[$postType->name])) {
return false;
}
if (! isset($preselect[$postType->name][$taxonomy->name]) || ! is_array($preselect[$postType->name][$taxonomy->name])) { // phpcs:ignore Generic.Files.LineLength.TooLong
return false;
}
return in_array($term->term_id, $preselect[$postType->name][$taxonomy->name]);
}
/**
* Register the component scripts.
*
* @since 3.0.0
*
* @param string $hook Page hook
*
* @return void
*/
public function enqueueScripts($hook)
{
if ($hook === 'settings_page_beyondwords') {
wp_enqueue_script(
'beyondwords-settings--preselect-settings',
BEYONDWORDS__PLUGIN_URI . 'src/Component/Settings/Preselect/settings.js',
['jquery', 'underscore'],
BEYONDWORDS__PLUGIN_VERSION,
true
);
}
if ($hook === 'post.php' || $hook === 'post-new.php') {
wp_register_script(
'beyondwords-settings--preselect-post',
BEYONDWORDS__PLUGIN_URI . 'src/Component/Settings/Preselect/post.js',
['jquery', 'underscore'],
BEYONDWORDS__PLUGIN_VERSION,
true
);
// Localize the script with new data
$data = [
'postType' => get_post_type(),
'preselect' => get_option('beyondwords_preselect'),
];
wp_localize_script('beyondwords-settings--preselect-post', 'beyondwords', $data);
wp_enqueue_script('beyondwords-settings--preselect-post');
}
}
}