* @since 3.0.0 */ namespace Beyondwords\Wordpress\Component\Settings; use Beyondwords\Wordpress\Component\Settings\ApiKey\ApiKey; use Beyondwords\Wordpress\Component\Settings\Languages\Languages; use Beyondwords\Wordpress\Component\Settings\Preselect\Preselect; use Beyondwords\Wordpress\Component\Settings\PrependExcerpt\PrependExcerpt; use Beyondwords\Wordpress\Component\Settings\PlayerUI\PlayerUI; use Beyondwords\Wordpress\Component\Settings\PlayerStyle\PlayerStyle; use Beyondwords\Wordpress\Component\Settings\PlayerVersion\PlayerVersion; use Beyondwords\Wordpress\Component\Settings\ProjectId\ProjectId; use Beyondwords\Wordpress\Component\Settings\SettingsUpdated\SettingsUpdated; use Beyondwords\Wordpress\Component\Settings\SettingsUtils; use Beyondwords\Wordpress\Core\Environment; /** * Settings setup * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * * @since 3.0.0 */ class Settings { /** * API Client. * * @since 3.0.0 */ private $apiClient; /** * Constructor. * * @since 3.0.0 */ public function __construct($apiClient) { $this->apiClient = $apiClient; } /** * Init */ public function init() { (new ApiKey())->init(); (new ProjectId())->init(); (new Preselect())->init(); (new PrependExcerpt())->init(); (new PlayerVersion($this->apiClient))->init(); (new PlayerUI())->init(); (new PlayerStyle($this->apiClient))->init(); (new Languages($this->apiClient))->init(); (new SettingsUpdated())->init(); add_action('admin_menu', array($this, 'addOptionsPage')); add_action('admin_init', array($this, 'addSettingsSections')); add_action('admin_notices', array($this, 'printPluginAdminNotices')); add_action('rest_api_init', array($this, 'restApiInit')); add_filter('plugin_action_links_speechkit/speechkit.php', array($this, 'addSettingsLinkToPluginPage')); add_action('updated_option', array($this, 'updatedOption'), 99); add_action('added_option', array($this, 'addedOption'), 99); } /** * Add items to the WordPress admin menu. * * @since 3.0.0 * * @return void */ public function addOptionsPage() { // Settings > BeyondWords add_options_page( __('BeyondWords', 'speechkit'), __('BeyondWords', 'speechkit'), 'manage_options', 'beyondwords', array($this, 'createAdminInterface') ); } /** * Add Settings sections. * * ===== * Basic * ===== * 'api_key' => '', * 'project_id' => '', * * ============== * Player * ============== * 'version' => '0', * * ============== * Generate audio * ============== * 'preselect' => array('post' => '1', 'page' => '1'), * * ======== * Advanced * ======== * 'merge_excerpt' => false, * * @since 3.0.0 */ public function addSettingsSections() { // Add Settings Section: Basic add_settings_section( 'basic', __('Basic settings', 'speechkit'), array($this, 'basicSectionCallback'), 'beyondwords' ); if (SettingsUtils::hasApiSettings()) { // Add Settings Section: Player add_settings_section( 'player', __('Player settings', 'speechkit'), array($this, 'playerSectionCallback'), 'beyondwords' ); // Add Settings Section: Content add_settings_section( 'content', __('Content settings', 'speechkit'), array($this, 'contentSectionCallback'), 'beyondwords' ); // Add Settings Section: Generate audio add_settings_section( 'generate-audio', __('‘Generate audio’ settings', 'speechkit'), array($this, 'generateAudioSectionCallback'), 'beyondwords' ); } } /** * "Basic section" callback * * @since 3.0.0 * * @return void **/ public function basicSectionCallback() { delete_transient('beyondwords_settings_errors'); ?>

' . __('Settings', 'speechkit') . ''; return $links; } public function createAdminInterface() { ?>

%s', esc_attr__('WordPress support: Latest player', 'speechkit'), esc_html__('let us know', 'speechkit') ) ); ?>

the_content() by default. You can change the location via the WordPress Editor.', // phpcs:ignore Generic.Files.LineLength.TooLong 'speechkit' ); ?>

%s', esc_url(admin_url('options-general.php?page=beyondwords')), esc_html__('plugin settings', 'speechkit') ) ); ?>

%s', esc_url(admin_url('options-general.php?page=beyondwords')), esc_html__('plugin settings', 'speechkit') ) ); ?>

\WP_REST_Server::READABLE, 'callback' => array($this, 'restApiResponse'), 'permission_callback' => function () { return current_user_can('edit_posts'); }, )); } /** * WP REST API response (required for the Gutenberg editor). * * DO NOT expose ALL settings e.g. be sure to never expose the API key. * * @since 3.0.0 * @since 3.4.0 Add pluginVersion and wpVersion. * * @return \WP_REST_Response */ public function restApiResponse() { global $wp_version; return new \WP_REST_Response([ 'apiKey' => get_option('beyondwords_api_key', ''), 'pluginVersion' => BEYONDWORDS__PLUGIN_VERSION, 'projectId' => get_option('beyondwords_project_id', ''), 'preselect' => get_option('beyondwords_preselect', Preselect::DEFAULT_PRESELECT), 'languages' => get_option('beyondwords_languages', Languages::DEFAULT_LANGUAGES), 'wpVersion' => $wp_version, ]); } /** * Check API creds are valid whenever any setting is added. * * @since 4.0.0 * * @return void */ public function addedOption($optionName) { if ($optionName === 'beyondwords_settings_updated') { $this->checkApiCreds(); } } /** * Check API creds are valid whenever the settings are updated. * * @since 4.0.0 * * @return void */ public function updatedOption($optionName) { if ($optionName === 'beyondwords_settings_updated') { $this->checkApiCreds(); } } /** * Check to see if user has valid BeyondWords API credentials by performing * a GET request using the API Key and Project ID. * * @since 4.0.0 * * @return void */ public function checkApiCreds() { // Assume invalid connection delete_option('beyondwords_valid_api_connection'); $apiKey = get_option('beyondwords_api_key'); $projectId = get_option('beyondwords_project_id'); if (empty($apiKey) || empty($projectId)) { return; } $project = $this->apiClient->getProject(); $validConnection = ( is_array($project) && array_key_exists('id', $project) && strval($project['id']) === $projectId ); if ($validConnection) { // Store date of last check update_option('beyondwords_valid_api_connection', gmdate(DATE_ISO8601)); } else { $errors = get_transient('beyondwords_settings_errors', []); $errors['Settings/ValidApiConnection'] = __( 'Please check and re-enter your BeyondWords API key and project ID. They appear to be invalid.', 'speechkit' ); set_transient('beyondwords_settings_errors', $errors); } } }