* @since 3.0.0 */ namespace Beyondwords\Wordpress\Component\Post\GenerateAudio; use Beyondwords\Wordpress\Core\Core; use Beyondwords\Wordpress\Component\Post\PostMetaUtils; use Beyondwords\Wordpress\Component\Settings\SettingsUtils; /** * GenerateAudio setup * * @since 3.0.0 */ class GenerateAudio { /** * Constructor */ public function __construct() { add_action('wp_loaded', function () { $postTypes = SettingsUtils::getSupportedPostTypes(); if (is_array($postTypes)) { foreach ($postTypes as $postType) { add_action("save_post_{$postType}", array($this, 'save'), 10); } } }); } /** * todo move this function to somewhere reusable, for the Block editor & Elementor. */ public function shouldPreselectGenerateAudio($post) { $postType = get_post_type($post); if (! $postType) { return false; } $preselect = get_option('beyondwords_preselect'); if (! is_array($preselect)) { return false; } // Preselect if the post type in the settings has been checked (not the taxonomies) if (array_key_exists($postType, $preselect) && $preselect[$postType] === '1') { return true; } return false; } public function element($post) { wp_nonce_field('beyondwords_generate_audio', 'beyondwords_generate_audio_nonce'); $generateAudio = PostMetaUtils::hasGenerateAudio($post->ID); if (! $generateAudio) { // Check whether "0" has explicitly been saved $generateAudioMeta = PostMetaUtils::getRenamedPostMeta($post->ID, 'generate_audio', true); if ($generateAudioMeta !== '0' && $this->shouldPreselectGenerateAudio($post)) { $generateAudio = true; } } ?>

/>