| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/** |
| 6 |
* Setting: Project ID |
| 7 |
* |
| 8 |
* @package Beyondwords\Wordpress |
| 9 |
* @author Stuart McAlpine <stu@beyondwords.io> |
| 10 |
* @since 3.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Beyondwords\Wordpress\Component\Settings\Fields\ProjectId; |
| 14 |
|
| 15 |
use Beyondwords\Wordpress\Component\Settings\SettingsUtils; |
| 16 |
|
| 17 |
/** |
| 18 |
* ProjectId |
| 19 |
* |
| 20 |
* @since 3.0.0 |
| 21 |
*/ |
| 22 |
defined('ABSPATH') || exit; |
| 23 |
|
| 24 |
class ProjectId |
| 25 |
{ |
| 26 |
/** |
| 27 |
* Option name. |
| 28 |
* |
| 29 |
* @since 5.0.0 |
| 30 |
*/ |
| 31 |
public const OPTION_NAME = 'beyondwords_project_id'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Init. |
| 35 |
* |
| 36 |
* @since 4.0.0 |
| 37 |
* @since 6.0.0 Make static. |
| 38 |
*/ |
| 39 |
public static function init() |
| 40 |
{ |
| 41 |
add_action('admin_init', [self::class, 'addSetting']); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Init setting. |
| 46 |
* |
| 47 |
* @since 3.0.0 |
| 48 |
* @since 6.0.0 Make static. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public static function addSetting() |
| 53 |
{ |
| 54 |
register_setting( |
| 55 |
'beyondwords_credentials_settings', |
| 56 |
self::OPTION_NAME, |
| 57 |
[ |
| 58 |
'default' => '', |
| 59 |
'sanitize_callback' => [self::class, 'sanitize'], |
| 60 |
] |
| 61 |
); |
| 62 |
|
| 63 |
add_settings_field( |
| 64 |
'beyondwords-project-id', |
| 65 |
__('Project ID', 'speechkit'), |
| 66 |
[self::class, 'render'], |
| 67 |
'beyondwords_credentials', |
| 68 |
'credentials' |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Render setting field. |
| 74 |
* |
| 75 |
* @since 3.0.0 |
| 76 |
* @since 6.0.0 Make static. |
| 77 |
* |
| 78 |
* @return void |
| 79 |
**/ |
| 80 |
public static function render() |
| 81 |
{ |
| 82 |
$value = get_option(self::OPTION_NAME); |
| 83 |
?> |
| 84 |
<input |
| 85 |
type="text" |
| 86 |
id="<?php echo esc_attr(self::OPTION_NAME); ?>" |
| 87 |
name="<?php echo esc_attr(self::OPTION_NAME); ?>" |
| 88 |
value="<?php echo esc_attr($value); ?>" |
| 89 |
size="10" |
| 90 |
/> |
| 91 |
<?php |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Sanitise the setting value. |
| 96 |
* |
| 97 |
* @since 3.0.0 |
| 98 |
* @since 5.2.0 Remove creds validation from here. |
| 99 |
* @since 6.0.0 Make static. |
| 100 |
* |
| 101 |
* @param array $value The submitted value. |
| 102 |
* |
| 103 |
* @return void |
| 104 |
**/ |
| 105 |
public static function sanitize($value) |
| 106 |
{ |
| 107 |
if (empty($value)) { |
| 108 |
SettingsUtils::addSettingsErrorMessage( |
| 109 |
__( |
| 110 |
'Please enter your BeyondWords project ID. This can be found in your project settings.', |
| 111 |
'speechkit' |
| 112 |
), |
| 113 |
'Settings/ProjectId' |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
return $value; |
| 118 |
} |
| 119 |
} |
| 120 |
|