cl01/wp-content/themes/hester/inc/customizer/widgets/class-hester-customizer-widget-button.php
2025-02-24 12:39:24 +08:00

166 lines
5.9 KiB
PHP
Executable File

<?php
/**
* Hester Customizer widgets class.
*
* @package Hester
* @author Peregrine Themes
* @since 1.0.0
*/
/**
* Do not allow direct script access.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Hester_Customizer_Widget_Button' ) ) :
/**
* Hester Customizer widget class
*/
class Hester_Customizer_Widget_Button extends Hester_Customizer_Widget {
/**
* Menu Location for this widget
*
* @since 1.0.0
* @var string
*/
public $styles = array();
/**
* Primary class constructor.
*
* @since 1.0.0
* @param array $args An array of the values for this widget.
*/
public function __construct( $args = array() ) {
$values = array(
'text' => '',
'url' => '',
'target' => '_self',
'class' => '',
'style' => '',
'visibility' => 'all',
);
$args['values'] = isset( $args['values'] ) ? wp_parse_args( $args['values'], $values ) : $values;
$args['values']['text'] = wp_kses( $args['values']['text'], hester_get_allowed_html_tags() );
$args['values']['url'] = esc_url_raw( $args['values']['url'] );
$args['values']['target'] = sanitize_text_field( $args['values']['target'] );
$args['values']['class'] = sanitize_text_field( $args['values']['class'] );
$args['values']['style'] = sanitize_text_field( $args['values']['style'] );
$args['values']['visibility'] = isset( $args['values']['visibility'] ) ? sanitize_text_field( $args['values']['visibility'] ) : 'hide-mobile-tablet';
parent::__construct( $args );
$this->name = __( 'Button', 'hester' );
$this->description = __( 'A button with custom link.', 'hester' );
$this->icon = 'dashicons dashicons-admin-links';
$this->type = 'button';
$this->styles = isset( $args['styles'] ) ? $args['styles'] : array();
}
/**
* Displays the form for this widget on the Widgets page of the WP Admin area.
*
* @since 1.0.0
* @return void
*/
public function form() {
?>
<!-- Text -->
<p>
<label for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-text"><?php esc_html_e( 'Text', 'hester' ); ?>:</label>
<input
type="text"
id="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-text"
name="widget-button[<?php echo esc_attr( $this->number ); ?>][text]"
data-option-name="text"
value="<?php echo esc_html( $this->values['text'] ); ?>"
placeholder="<?php esc_attr_e( 'Button Text', 'hester' ); ?>"/>
</p>
<!-- URL -->
<p>
<label for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-url"><?php esc_html_e( 'URL', 'hester' ); ?>:</label>
<input
type="text"
id="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-url"
name="widget-button[<?php echo esc_attr( $this->number ); ?>][url]"
data-option-name="url"
value="<?php echo esc_html( $this->values['url'] ); ?>"
placeholder="<?php esc_attr_e( 'Button URL', 'hester' ); ?>" />
</p>
<!-- Target -->
<p>
<label for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-target"><?php esc_html_e( 'Open link in', 'hester' ); ?>:</label>
<span class="buttonset">
<input
class="switch-input screen-reader-text"
type="radio"
value="_self"
name="widget-button[<?php echo esc_attr( $this->number ); ?>][target]"
id="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-target-_self"
<?php checked( '_self', $this->values['target'], true ); ?>
data-option-name="target">
<label
class="switch-label"
for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-target-_self">
<?php esc_html_e( 'Same Tab', 'hester' ); ?>
</label>
</input>
<input
class="switch-input screen-reader-text"
type="radio"
value="_blank"
name="widget-button[<?php echo esc_attr( $this->number ); ?>][target]"
id="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-target-_blank"
<?php checked( '_blank', $this->values['target'], true ); ?>
data-option-name="target">
<label
class="switch-label"
for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-target-_blank">
<?php esc_html_e( 'New Tab', 'hester' ); ?>
</label>
</input>
</span>
</p>
<?php if ( ! empty( $this->styles ) ) { ?>
<!-- Styles -->
<p class="hester-widget-button-style">
<label for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-style">
<?php esc_html_e( 'Style', 'hester' ); ?>:
</label>
<select id="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-style" name="widget-button[<?php echo esc_attr( $this->number ); ?>][style]" data-option-name="style">
<?php foreach ( $this->styles as $key => $value ) { ?>
<option
value="<?php echo esc_attr( $key ); ?>"
<?php selected( $key, $this->values['style'], true ); ?>>
<?php echo esc_html( $value ); ?>
</option>
<?php } ?>
</select>
</p>
<?php } ?>
<!-- Class -->
<p>
<label for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-class"><?php esc_html_e( 'Additional class', 'hester' ); ?>:</label>
<input
type="text"
id="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-class"
name="widget-button[<?php echo esc_attr( $this->number ); ?>][class]"
data-option-name="class"
value="<?php echo esc_html( $this->values['class'] ); ?>" />
</p>
<?php
}
}
endif;