Astra_Update_Sidebar::execute( array $args )

Execute the ability.


Description


Parameters

$args

(array) (Required) Input arguments.


Return

(array) Result array.


Source

File: inc/abilities/customizer/general/sidebar/class-astra-update-sidebar.php

	public function execute( $args ) {
		$updated         = false;
		$update_messages = array();

		if ( isset( $args['layout'] ) ) {
			$layout        = sanitize_text_field( $args['layout'] );
			$valid_layouts = array( 'no-sidebar', 'left-sidebar', 'right-sidebar' );

			if ( ! in_array( $layout, $valid_layouts, true ) ) {
				return Astra_Abilities_Response::error(
					sprintf(
						/* translators: %s: layout value */
						__( 'Invalid layout: %s.', 'astra' ),
						$layout
					),
					__( 'Valid options: no-sidebar, left-sidebar, right-sidebar', 'astra' )
				);
			}

			astra_update_option( 'site-sidebar-layout', $layout );
			$updated           = true;
			$layout_labels     = array(
				'no-sidebar'    => 'No Sidebar',
				'left-sidebar'  => 'Left Sidebar',
				'right-sidebar' => 'Right Sidebar',
			);
			$update_messages[] = sprintf( 'Layout set to %s', $layout_labels[ $layout ] );
		}

		if ( isset( $args['style'] ) ) {
			$style        = sanitize_text_field( $args['style'] );
			$valid_styles = array( 'unboxed', 'boxed' );

			if ( ! in_array( $style, $valid_styles, true ) ) {
				return Astra_Abilities_Response::error(
					sprintf(
						/* translators: %s: style value */
						__( 'Invalid style: %s.', 'astra' ),
						$style
					),
					__( 'Valid options: unboxed, boxed', 'astra' )
				);
			}

			astra_update_option( 'site-sidebar-style', $style );
			$updated           = true;
			$update_messages[] = sprintf( 'Style set to %s', ucfirst( $style ) );
		}

		if ( isset( $args['width'] ) ) {
			$width = absint( $args['width'] );

			if ( $width < 15 || $width > 50 ) {
				return Astra_Abilities_Response::error(
					sprintf(
						/* translators: %d: width value */
						__( 'Invalid width: %d.', 'astra' ),
						$width
					),
					__( 'Width must be between 15 and 50 percent.', 'astra' )
				);
			}

			astra_update_option( 'site-sidebar-width', $width );
			$updated           = true;
			$update_messages[] = sprintf( 'Width set to %d%%', $width );
		}

		if ( isset( $args['sticky_enabled'] ) ) {
			$sticky = (bool) $args['sticky_enabled'];
			astra_update_option( 'site-sticky-sidebar', $sticky );
			$updated           = true;
			$update_messages[] = sprintf( 'Sticky sidebar %s', $sticky ? 'enabled' : 'disabled' );
		}

		if ( ! $updated ) {
			return Astra_Abilities_Response::error(
				__( 'No changes specified.', 'astra' ),
				__( 'Please provide at least one setting to update.', 'astra' )
			);
		}

		$message = 'Sidebar settings updated: ' . implode( ', ', $update_messages ) . '.';

		return Astra_Abilities_Response::success(
			$message,
			array( 'updated' => true )
		);
	}


User Contributed Notes

You must log in before being able to contribute a note or feedback.