Astra_Update_Header_Builder::execute( array $args )
Execute the ability.
Description
Parameters
- $args
-
(array) (Required) Input arguments.
Return
(array) Result array.
Source
File: inc/abilities/customizer/header/class-astra-update-header-builder.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' )
);
}
if ( ! function_exists( 'astra_get_option' ) ) {
return Astra_Abilities_Response::error(
__( 'Astra theme helper functions not available.', 'astra' ),
__( 'The astra_get_option function is required but not found.', 'astra' )
);
}
$updated = false;
$update_messages = array();
$detailed_changes = array();
if ( isset( $args['desktop'] ) && is_array( $args['desktop'] ) ) {
$current_desktop_raw = astra_get_option( 'header-desktop-items', array() );
if ( ! is_array( $current_desktop_raw ) ) {
$current_desktop_raw = array();
}
$default_desktop = $this->get_default_desktop_structure();
$current_desktop = $this->array_merge_recursive_distinct( $default_desktop, $current_desktop_raw );
$desktop_layout = $this->sanitize_header_layout( $args['desktop'], 'desktop' );
$changes = $this->get_layout_changes( $current_desktop, $desktop_layout, 'desktop' );
if ( ! empty( $changes ) ) {
$detailed_changes = array_merge( $detailed_changes, $changes );
}
$new_desktop = $this->array_merge_recursive_distinct( $current_desktop, $desktop_layout );
astra_update_option( 'header-desktop-items', $new_desktop );
$updated = true;
$update_messages[] = 'Desktop header layout updated';
}
if ( isset( $args['mobile'] ) && is_array( $args['mobile'] ) ) {
$current_mobile_raw = astra_get_option( 'header-mobile-items', array() );
if ( ! is_array( $current_mobile_raw ) ) {
$current_mobile_raw = array();
}
$default_mobile = $this->get_default_mobile_structure();
$current_mobile = $this->array_merge_recursive_distinct( $default_mobile, $current_mobile_raw );
$mobile_layout = $this->sanitize_header_layout( $args['mobile'], 'mobile' );
$changes = $this->get_layout_changes( $current_mobile, $mobile_layout, 'mobile' );
if ( ! empty( $changes ) ) {
$detailed_changes = array_merge( $detailed_changes, $changes );
}
$new_mobile = $this->array_merge_recursive_distinct( $current_mobile, $mobile_layout );
astra_update_option( 'header-mobile-items', $new_mobile );
$updated = true;
$update_messages[] = 'Mobile header layout updated';
}
if ( ! $updated ) {
return Astra_Abilities_Response::error(
__( 'No changes specified.', 'astra' ),
__( 'Please provide at least one layout to update (desktop or mobile).', 'astra' )
);
}
$message = implode( ', ', $update_messages ) . '.';
if ( ! empty( $detailed_changes ) ) {
$message .= ' Details: ' . implode( '; ', $detailed_changes ) . '.';
}
return Astra_Abilities_Response::success(
$message,
array(
'updated' => true,
'changes' => $detailed_changes,
)
);
}
Expand full source code Collapse full source code View on Trac