* @since 4.0.0 */ namespace Beyondwords\Wordpress\Component\Post\SelectVoice; use Beyondwords\Wordpress\Core\ApiClient; use Beyondwords\Wordpress\Core\Core; use Beyondwords\Wordpress\Core\CoreUtils; use Beyondwords\Wordpress\Component\Post\PostMetaUtils; use Beyondwords\Wordpress\Component\Settings\Languages\Languages; use Beyondwords\Wordpress\Component\Settings\SettingsUtils; /** * SelectVoice setup * * @since 4.0.0 */ class SelectVoice { /** * API Client */ private $apiClient; /** * Constructor */ public function __construct($apiClient) { $this->apiClient = $apiClient; add_action('rest_api_init', array($this, 'restApiInit')); add_action('admin_enqueue_scripts', array($this, 'adminEnqueueScripts')); 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); } } }); } /** * @since 4.0.0 */ public function element($post) { if (! get_option('beyondwords_languages')) { return; } $currentLanguageId = get_post_meta($post->ID, 'beyondwords_language_id', true); $currentVoiceId = get_post_meta($post->ID, 'beyondwords_body_voice_id', true); $languages = $this->getFilteredLanguages(); $voices = []; if ($currentLanguageId) { $voices = $this->apiClient->getVoices($currentLanguageId); } wp_nonce_field('beyondwords_select_voice', 'beyondwords_select_voice_nonce'); ?>

\WP_REST_Server::READABLE, 'callback' => array($this, 'languagesRestApiResponse'), 'permission_callback' => function () { return current_user_can('edit_posts'); }, )); // Voices endpoint register_rest_route('beyondwords/v1', '/languages/(?P[0-9]+)/voices', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array($this, 'voicesRestApiResponse'), 'permission_callback' => function () { return current_user_can('edit_posts'); }, )); } /** * Get languages from BeyondWords API and filter by "Languages" plugin setting. * * @since 4.0.0 * * @return array Array of languages */ public function getFilteredLanguages() { $languages = $this->apiClient->getLanguages(); $languagesSetting = get_option('beyondwords_languages', Languages::DEFAULT_LANGUAGES); // Filter languages according to "Languages" plugin setting if (is_array($languages) && is_array($languagesSetting)) { $languages = array_values(array_filter($languages, function ($language) { return $this->languageIsInSettings($language); })); } return $languages; } /** * Get languages from BeyondWords API and filter by "Languages" plugin setting. * * @since 4.0.0 * * @return array Array of languages */ public function languageIsInSettings($language) { if (! is_array($language)) { return false; } if (! array_key_exists('id', $language)) { return false; } $languagesSetting = get_option('beyondwords_languages', Languages::DEFAULT_LANGUAGES); if (! is_array($languagesSetting)) { return false; } if (! in_array(strval($language['id']), $languagesSetting)) { return false; } return true; } /** * "Languages" WP REST API response (required for the Gutenberg editor). * * @since 4.0.0 * * @return \WP_REST_Response */ public function languagesRestApiResponse() { $languages = $this->getFilteredLanguages(); return new \WP_REST_Response($languages); } /** * "Voices" WP REST API response (required for the Gutenberg editor * and Block Editor). * * @since 4.0.0 * * @return \WP_REST_Response */ public function voicesRestApiResponse(\WP_REST_Request $data) { $params = $data->get_url_params(); $voices = $this->apiClient->getVoices($params['languageId']); return new \WP_REST_Response($voices); } /** * Register the component scripts. * * @since 4.0.0 * * @param string $hook Page hook * * @return void */ public function adminEnqueueScripts($hook) { if (! CoreUtils::isGutenbergPage() && ( $hook === 'post.php' || $hook === 'post-new.php')) { wp_register_script( 'beyondwords-metabox--select-voice', BEYONDWORDS__PLUGIN_URI . 'src/Component/Post/SelectVoice/classic-metabox.js', ['jquery', 'underscore'], BEYONDWORDS__PLUGIN_VERSION, true ); /** * Localize the script to handle ajax requests */ wp_localize_script( 'beyondwords-metabox--select-voice', 'beyondwordsData', array( 'nonce' => wp_create_nonce('wp_rest'), 'root' => esc_url_raw(rest_url()), ) ); wp_enqueue_script('beyondwords-metabox--select-voice'); } } }