* @since 3.0.0 */ namespace Beyondwords\Wordpress\Component\Settings; use Beyondwords\Wordpress\Component\Settings\Languages\Languages; use Beyondwords\Wordpress\Component\Settings\Preselect\Preselect; use Beyondwords\Wordpress\Component\Settings\SettingsUtils; use Beyondwords\Wordpress\Core\Core; use Beyondwords\Wordpress\Core\Environment; /** * Settings setup * * @since 3.0.0 */ class Settings { private $apiClient; /** * Constructor */ public function __construct($apiClient) { $this->apiClient = $apiClient; add_action('admin_menu', array($this, 'addOptionsPage')); add_action('admin_init', array($this, 'init')); 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, 3); add_action('added_option', array($this, 'addedOption'), 99, 2); } /** * 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') ); } /** * Init Settings. * * ===== * 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 init() { // 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. * * @return \WP_REST_Response */ public function restApiResponse() { return new \WP_REST_Response([ 'apiKey' => get_option('beyondwords_api_key', ''), 'projectId' => get_option('beyondwords_project_id', ''), 'preselect' => get_option('beyondwords_preselect', Preselect::DEFAULT_PRESELECT), 'languages' => get_option('beyondwords_languages', Languages::DEFAULT_LANGUAGES), 'debug' => defined('BEYONDWORDS_DEBUG') ? boolval(BEYONDWORDS_DEBUG) : false, ]); } /** * Check API creds are valid whenever any setting is added. * * @SuppressWarnings(PHPMD.UnusedFormalParameter) * * @since 4.0.0 * * @return void */ public function addedOption($optionName, $value) { if ($optionName === 'beyondwords_settings_updated') { $this->checkApiCreds(); } } /** * Check API creds are valid whenever the settings are updated. * * @SuppressWarnings(PHPMD.UnusedFormalParameter) * * @since 4.0.0 * * @return void */ public function updatedOption($optionName, $oldValue, $value) { 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); } } }