Astra_Update_Scroll_To_Top::execute( array $args )
Execute the ability.
Description
Parameters
- $args
-
(array) (Required) Input arguments.
Return
(array) Result array.
Source
File: inc/abilities/customizer/general/scroll-to-top/class-astra-update-scroll-to-top.php
public function execute( $args ) {
if ( ! defined( 'ASTRA_THEME_SETTINGS' ) ) {
return Astra_Abilities_Response::error(
__( 'Astra theme is not active.', 'astra' ),
__( 'Please activate the Astra theme to use this feature.', 'astra' )
);
}
$updated = false;
$update_messages = array();
if ( isset( $args['enabled'] ) ) {
astra_update_option( 'scroll-to-top-enable', (bool) $args['enabled'] );
$updated = true;
$update_messages[] = $args['enabled'] ? __( 'Scroll to top enabled', 'astra' ) : __( 'Scroll to top disabled', 'astra' );
}
if ( isset( $args['position'] ) ) {
$position = sanitize_text_field( $args['position'] );
$valid_positions = array( 'right', 'left' );
if ( ! in_array( $position, $valid_positions, true ) ) {
return Astra_Abilities_Response::error(
/* translators: %s: invalid position value */
sprintf( __( 'Invalid position: %s.', 'astra' ), $position ),
__( 'Valid options: right, left', 'astra' )
);
}
astra_update_option( 'scroll-to-top-icon-position', $position );
$updated = true;
/* translators: %s: position value */
$update_messages[] = sprintf( __( 'Position set to %s', 'astra' ), $position );
}
if ( isset( $args['on_devices'] ) ) {
$on_devices = sanitize_text_field( $args['on_devices'] );
$valid_devices = array( 'both', 'desktop', 'mobile' );
if ( ! in_array( $on_devices, $valid_devices, true ) ) {
return Astra_Abilities_Response::error(
/* translators: %s: invalid device value */
sprintf( __( 'Invalid on_devices: %s.', 'astra' ), $on_devices ),
__( 'Valid options: both, desktop, mobile', 'astra' )
);
}
astra_update_option( 'scroll-to-top-on-devices', $on_devices );
$updated = true;
/* translators: %s: device visibility value */
$update_messages[] = sprintf( __( 'Device visibility set to %s', 'astra' ), $on_devices );
}
if ( isset( $args['icon_size'] ) ) {
$icon_size = absint( $args['icon_size'] );
astra_update_option( 'scroll-to-top-icon-size', $icon_size );
$updated = true;
/* translators: %d: icon size in pixels */
$update_messages[] = sprintf( __( 'Icon size set to %dpx', 'astra' ), $icon_size );
}
if ( isset( $args['colors'] ) && is_array( $args['colors'] ) ) {
$color_map = array(
'icon_color' => 'scroll-to-top-icon-color',
'icon_bg_color' => 'scroll-to-top-icon-bg-color',
'icon_h_color' => 'scroll-to-top-icon-h-color',
'icon_h_bg_color' => 'scroll-to-top-icon-h-bg-color',
);
foreach ( $color_map as $key => $option_key ) {
if ( isset( $args['colors'][ $key ] ) ) {
astra_update_option( $option_key, sanitize_text_field( $args['colors'][ $key ] ) );
$updated = true;
}
}
$update_messages[] = __( 'Colors updated', 'astra' );
}
if ( isset( $args['border_radius'] ) && is_array( $args['border_radius'] ) ) {
$sanitized = $this->sanitize_spacing( $args['border_radius'] );
astra_update_option( 'scroll-to-top-icon-radius-fields', $sanitized );
$updated = true;
$update_messages[] = __( 'Border radius updated', 'astra' );
}
if ( ! $updated ) {
return Astra_Abilities_Response::error(
__( 'No changes specified.', 'astra' ),
__( 'Please provide at least one setting to update.', 'astra' )
);
}
/* translators: %s: comma-separated list of updated settings */
$message = sprintf( __( 'Scroll to top settings updated: %s.', 'astra' ), implode( ', ', $update_messages ) );
return Astra_Abilities_Response::success(
$message,
array( 'updated' => true )
);
}
Expand full source code Collapse full source code View on Trac