Astra_Migrate_Header_Components::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-migrate-header-components.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' )
);
}
$dry_run = isset( $args['dry_run'] ) ? (bool) $args['dry_run'] : false;
$theme_options = get_option( ASTRA_THEME_SETTINGS, array() );
if ( ! is_array( $theme_options ) ) {
$theme_options = array();
}
$changes = array();
$total_fixes = 0;
// Migrate desktop header items.
if ( isset( $theme_options['header-desktop-items'] ) && is_array( $theme_options['header-desktop-items'] ) ) {
$desktop_result = $this->migrate_layout( $theme_options['header-desktop-items'], 'desktop' );
if ( ! empty( $desktop_result['changed'] ) ) {
$changes['desktop'] = isset( $desktop_result['changes'] ) ? $desktop_result['changes'] : array();
$total_fixes += isset( $desktop_result['count'] ) ? (int) $desktop_result['count'] : 0;
if ( ! $dry_run && isset( $desktop_result['layout'] ) ) {
$theme_options['header-desktop-items'] = $desktop_result['layout'];
}
}
}
// Migrate mobile header items.
if ( isset( $theme_options['header-mobile-items'] ) && is_array( $theme_options['header-mobile-items'] ) ) {
$mobile_result = $this->migrate_layout( $theme_options['header-mobile-items'], 'mobile' );
if ( ! empty( $mobile_result['changed'] ) ) {
$changes['mobile'] = isset( $mobile_result['changes'] ) ? $mobile_result['changes'] : array();
$total_fixes += isset( $mobile_result['count'] ) ? (int) $mobile_result['count'] : 0;
if ( ! $dry_run && isset( $mobile_result['layout'] ) ) {
$theme_options['header-mobile-items'] = $mobile_result['layout'];
}
}
}
if ( 0 === $total_fixes ) {
return Astra_Abilities_Response::success(
__( 'No component IDs need migration. All component IDs are already correct.', 'astra' ),
array(
'fixes_needed' => 0,
'dry_run' => $dry_run,
)
);
}
if ( ! $dry_run ) {
update_option( ASTRA_THEME_SETTINGS, $theme_options );
// Clear customizer cache.
if ( function_exists( 'wp_cache_flush' ) ) {
wp_cache_flush();
}
$message = sprintf(
/* translators: %d: number of fixes */
_n(
'Successfully migrated %d component ID. Changes are now visible in customizer and frontend.',
'Successfully migrated %d component IDs. Changes are now visible in customizer and frontend.',
$total_fixes,
'astra'
),
$total_fixes
);
} else {
$message = sprintf(
/* translators: %d: number of fixes */
_n(
'Found %d component ID that needs migration. Run without dry_run to apply changes.',
'Found %d component IDs that need migration. Run without dry_run to apply changes.',
$total_fixes,
'astra'
),
$total_fixes
);
}
return Astra_Abilities_Response::success(
$message,
array(
'fixes_applied' => ! $dry_run,
'total_fixes' => $total_fixes,
'changes' => $changes,
'dry_run' => $dry_run,
)
);
}
Expand full source code Collapse full source code View on Trac