| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/** |
| 6 |
* Setting: SettingsUpdated |
| 7 |
* |
| 8 |
* @package Beyondwords\Wordpress |
| 9 |
* @author Stuart McAlpine <stu@beyondwords.io> |
| 10 |
* @since 4.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Beyondwords\Wordpress\Component\Settings\SettingsUpdated; |
| 14 |
|
| 15 |
/** |
| 16 |
* SettingsUpdated setup |
| 17 |
* |
| 18 |
* @since 4.0.0 |
| 19 |
*/ |
| 20 |
class SettingsUpdated |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Constructor |
| 24 |
*/ |
| 25 |
public function __construct() |
| 26 |
{ |
| 27 |
add_action('admin_init', array($this, 'init')); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Init setting. |
| 32 |
* |
| 33 |
* @since 4.0.0 |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function init() |
| 38 |
{ |
| 39 |
register_setting( |
| 40 |
'beyondwords', |
| 41 |
'beyondwords_settings_updated', |
| 42 |
[ |
| 43 |
'default' => '', |
| 44 |
'sanitize_callback' => array($this, 'sanitize'), |
| 45 |
] |
| 46 |
); |
| 47 |
|
| 48 |
add_settings_field( |
| 49 |
'beyondwords-settings-updated', |
| 50 |
__('Settings Updated', 'speechkit'), |
| 51 |
array($this, 'render'), |
| 52 |
'beyondwords', |
| 53 |
'basic', |
| 54 |
[ |
| 55 |
'class' => 'hidden' |
| 56 |
] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Render setting field. |
| 62 |
* |
| 63 |
* @since 4.0.0 |
| 64 |
* |
| 65 |
* @return void |
| 66 |
**/ |
| 67 |
public function render() |
| 68 |
{ |
| 69 |
$settingsUpdated = get_option('beyondwords_settings_updated'); |
| 70 |
?> |
| 71 |
<input |
| 72 |
type="hidden" |
| 73 |
name="beyondwords_settings_updated" |
| 74 |
value="<?php echo esc_attr($settingsUpdated); ?>" |
| 75 |
/> |
| 76 |
<?php |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Sanitise the setting value. |
| 81 |
* |
| 82 |
* This ignores the value passed as a param and instead always uses |
| 83 |
* an ISO8601 date string. |
| 84 |
* |
| 85 |
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 86 |
* |
| 87 |
* @since 4.0.0 |
| 88 |
* @param array $value The submitted value. |
| 89 |
* |
| 90 |
* @return void |
| 91 |
**/ |
| 92 |
public function sanitize($value) |
| 93 |
{ |
| 94 |
$date = gmdate(DATE_ISO8601); |
| 95 |
|
| 96 |
$user = wp_get_current_user(); |
| 97 |
|
| 98 |
if ($user instanceof \WP_User) { |
| 99 |
return $user->user_login . '@' . $date; |
| 100 |
} |
| 101 |
|
| 102 |
return $date; |
| 103 |
} |
| 104 |
} |
| 105 |
|