initial
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace Vrts\Core\Settings;
|
||||
|
||||
class Manager {
|
||||
/**
|
||||
* Sections
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $sections = [];
|
||||
|
||||
/**
|
||||
* Settings.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $settings = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'admin_init', [ $this, 'register_settings' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register settings.
|
||||
*/
|
||||
public function register_settings() {
|
||||
$this->add_sections();
|
||||
$this->add_settings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get option.
|
||||
*
|
||||
* @param string $id Settings setting ID.
|
||||
* @param bool $transform_value Transform value.
|
||||
*
|
||||
* @return mixed Option value.
|
||||
*/
|
||||
public function get_option( $id, $transform_value = true ) {
|
||||
$default_value = isset( $this->settings[ $id ] ) ? $this->settings[ $id ]['default'] ?? '' : false;
|
||||
$value = get_option( $id, $default_value );
|
||||
|
||||
if ( $transform_value && isset( $this->settings[ $id ]['return_value_callback'] ) ) {
|
||||
$value = call_user_func( $this->settings[ $id ]['return_value_callback'], $value );
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get settings section page.
|
||||
*
|
||||
* @param string $id Setting section ID.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_section_page( $id ) {
|
||||
return isset( $this->sections[ $id ] ) ? $this->sections[ $id ]['page'] : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output field.
|
||||
*
|
||||
* @param array $args Setting args.
|
||||
*/
|
||||
private function get_field( $args ) {
|
||||
$value = $args['value'] ?? $this->get_option( $args['id'], false );
|
||||
include vrts()->get_plugin_path( "includes/core/settings/field-{$args['type']}/index.php" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all sections.
|
||||
*/
|
||||
private function add_sections() {
|
||||
foreach ( $this->sections as $id => $args ) {
|
||||
add_settings_section(
|
||||
$id,
|
||||
$args['title'] ?? '',
|
||||
$args['callback'] ?? '__return_false',
|
||||
$args['page'] ?? 'general'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all settings.
|
||||
*/
|
||||
private function add_settings() {
|
||||
foreach ( $this->settings as $id => $args ) {
|
||||
register_setting(
|
||||
$this->get_section_page( $args['section'] ),
|
||||
$id,
|
||||
[
|
||||
'type' => $args['value_type'] ?? 'string',
|
||||
'sanitize_callback' => $args['sanitize_callback'] ?? null,
|
||||
'show_in_rest' => $args['show_in_rest'] ?? false,
|
||||
'default' => $args['default'] ?? '',
|
||||
]
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
$id,
|
||||
$args['title'] ?? '',
|
||||
function () use ( $args ) {
|
||||
$this->get_field( $args );
|
||||
},
|
||||
$this->get_section_page( $args['section'] ),
|
||||
$args['section'],
|
||||
[
|
||||
'class' => "vrts-settings-{$args['type']}",
|
||||
'label_for' => ! in_array( $args['type'], [ 'info', 'checkbox', 'radio' ], true ) ? $id : '',
|
||||
]
|
||||
);
|
||||
}//end foreach
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a settings section.
|
||||
*
|
||||
* @param array $args Array of properties for the new section object.
|
||||
*/
|
||||
public function add_section( $args = [] ) {
|
||||
$this->sections[ $args['id'] ] = $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a settings setting.
|
||||
*
|
||||
* @param array $args Array of properties for the new setting and control object.
|
||||
*/
|
||||
public function add_setting( $args = [] ) {
|
||||
$this->settings[ $args['id'] ] = $args;
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<fieldset <?php echo isset( $args['is_pro'] ) && false === $args['is_pro'] ? 'data-a11y-dialog-show="vrts-modal-pro-settings"' : ''; ?>>
|
||||
<legend class="screen-reader-text"><?php echo esc_html( $args['title'] ); ?></legend>
|
||||
<label>
|
||||
<input
|
||||
type="checkbox"
|
||||
id="<?php echo esc_attr( $args['id'] ); ?>"
|
||||
name="<?php echo esc_html( $args['id'] ); ?>"
|
||||
value="1"
|
||||
<?php checked( $value, 1 ); ?>
|
||||
<?php wp_readonly( isset( $args['readonly'] ) && $args['readonly'] ); ?>
|
||||
<?php disabled( isset( $args['disabled'] ) && $args['disabled'] ); ?>>
|
||||
<?php echo wp_kses_post( $args['label'] ); ?>
|
||||
</label>
|
||||
<?php
|
||||
if ( isset( $args['is_pro'] ) && false === $args['is_pro'] ) :
|
||||
?>
|
||||
<span class="vrts-settings__pro-label"><?php esc_html_e( 'Pro', 'visual-regression-tests' ); ?></span>
|
||||
<?php
|
||||
endif;
|
||||
?>
|
||||
</fieldset>
|
||||
|
||||
<?php
|
||||
if ( isset( $args['description'] ) ) :
|
||||
?>
|
||||
<p class="description"><?php echo wp_kses_post( $args['description'] ); ?></p>
|
||||
<?php
|
||||
endif;
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
<fieldset>
|
||||
<?php echo wp_kses_post( $args['description'] ); ?>
|
||||
<?php
|
||||
if ( isset( $args['is_pro'] ) && false === $args['is_pro'] ) :
|
||||
?>
|
||||
<span class="vrts-settings__pro-label"><?php esc_html_e( 'Pro', 'visual-regression-tests' ); ?></span>
|
||||
<?php
|
||||
endif;
|
||||
?>
|
||||
</fieldset>
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<fieldset <?php echo isset( $args['is_pro'] ) && false === $args['is_pro'] ? 'data-a11y-dialog-show="vrts-modal-pro-settings"' : ''; ?>>
|
||||
<input
|
||||
type="password" id="<?php echo esc_attr( $args['id'] ); ?>"
|
||||
class="regular-text"
|
||||
name="<?php echo esc_attr( $args['id'] ); ?>"
|
||||
value="<?php echo esc_attr( $value ); ?>"
|
||||
placeholder="<?php echo esc_attr( $args['placeholder'] ); ?>"
|
||||
<?php wp_readonly( isset( $args['readonly'] ) && $args['readonly'] ); ?>
|
||||
<?php disabled( isset( $args['disabled'] ) && $args['disabled'] ); ?>>
|
||||
<?php
|
||||
if ( isset( $args['is_pro'] ) && false === $args['is_pro'] ) :
|
||||
?>
|
||||
<span class="vrts-settings__pro-label"><?php esc_html_e( 'Pro', 'visual-regression-tests' ); ?></span>
|
||||
<?php
|
||||
endif;
|
||||
?>
|
||||
</fieldset>
|
||||
<?php
|
||||
if ( isset( $args['description'] ) ) :
|
||||
?>
|
||||
<p class="description"><?php echo wp_kses_post( $args['description'] ); ?></p>
|
||||
<?php
|
||||
endif;
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
<fieldset <?php echo isset( $args['is_pro'] ) && false === $args['is_pro'] ? 'data-a11y-dialog-show="vrts-modal-pro-settings"' : ''; ?>>
|
||||
<select
|
||||
name="<?php echo esc_attr( $args['id'] ); ?>"
|
||||
id="<?php echo esc_attr( $args['id'] ); ?>"
|
||||
<?php wp_readonly( isset( $args['readonly'] ) && $args['readonly'] ); ?>
|
||||
<?php disabled( isset( $args['disabled'] ) && $args['disabled'] ); ?>>
|
||||
<?php
|
||||
foreach ( $args['choices'] as $key => $name ) :
|
||||
?>
|
||||
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key, $value ); ?>><?php echo esc_html( $name ); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<?php
|
||||
if ( isset( $args['is_pro'] ) && false === $args['is_pro'] ) :
|
||||
?>
|
||||
<span class="vrts-settings__pro-label"><?php esc_html_e( 'Pro', 'visual-regression-tests' ); ?></span>
|
||||
<?php
|
||||
endif;
|
||||
?>
|
||||
</fieldset>
|
||||
<?php
|
||||
if ( isset( $args['description'] ) ) :
|
||||
?>
|
||||
<p class="description"><?php echo wp_kses_post( $args['description'] ); ?></p>
|
||||
<?php
|
||||
endif;
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<fieldset <?php echo isset( $args['is_pro'] ) && false === $args['is_pro'] ? 'data-a11y-dialog-show="vrts-modal-pro-settings"' : ''; ?>>
|
||||
<input
|
||||
type="text" id="<?php echo esc_attr( $args['id'] ); ?>"
|
||||
class="regular-text"
|
||||
name="<?php echo esc_attr( $args['id'] ); ?>"
|
||||
value="<?php echo esc_attr( $value ); ?>"
|
||||
placeholder="<?php echo esc_attr( $args['placeholder'] ); ?>"
|
||||
<?php wp_readonly( isset( $args['readonly'] ) && $args['readonly'] ); ?>
|
||||
<?php disabled( isset( $args['disabled'] ) && $args['disabled'] ); ?>>
|
||||
<?php
|
||||
if ( isset( $args['is_pro'] ) && false === $args['is_pro'] ) :
|
||||
?>
|
||||
<span class="vrts-settings__pro-label"><?php esc_html_e( 'Pro', 'visual-regression-tests' ); ?></span>
|
||||
<?php
|
||||
endif;
|
||||
?>
|
||||
</fieldset>
|
||||
<?php
|
||||
if ( isset( $args['description'] ) ) :
|
||||
?>
|
||||
<p class="description"><?php echo wp_kses_post( $args['description'] ); ?></p>
|
||||
<?php
|
||||
endif;
|
||||
Reference in New Issue
Block a user