Skip to content

Instantly share code, notes, and snippets.

@ilicfilip
Created August 18, 2020 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilicfilip/9b61a63a4b4ee1d54e18cbf984bd3071 to your computer and use it in GitHub Desktop.
Save ilicfilip/9b61a63a4b4ee1d54e18cbf984bd3071 to your computer and use it in GitHub Desktop.
/**
* Export Widgets.
*
* @todo: We need replace menu slug with menu name in Menu Widgets.
*/
$fusion_export_widgets_cmd = function() {
// TODO: we don't need $posted_array as we're always exporting all widgets.
$posted_array = [];
$all_widgets = wp_get_sidebars_widgets();
foreach ( $all_widgets as $widget_area => $widget_array ) {
foreach ( $widget_array as $widget ) {
$posted_array[ $widget ] = 'on';
}
}
// Begin plugin code.
$sidebars_array = get_option( 'sidebars_widgets' );
$sidebar_export = [];
foreach ( $sidebars_array as $sidebar => $widgets ) {
if ( ! empty( $widgets ) && is_array( $widgets ) ) {
foreach ( $widgets as $sidebar_widget ) {
if ( in_array( $sidebar_widget, array_keys( $posted_array ) ) ) {
$sidebar_export[ $sidebar ][] = $sidebar_widget;
}
}
}
}
$widgets = [];
foreach ( $posted_array as $k => $v ) {
$widget = [];
$widget['type'] = trim( substr( $k, 0, strrpos( $k, '-' ) ) );
$widget['type-index'] = trim( substr( $k, strrpos( $k, '-' ) + 1 ) );
$widget['export_flag'] = ( $v == 'on' ) ? true : false;
$widgets[] = $widget;
}
$widgets_array = [];
foreach ( $widgets as $widget ) {
$widget_val = get_option( 'widget_' . $widget['type'] );
$widget_val = apply_filters( 'widget_data_export', $widget_val, $widget['type'] );
$multiwidget_val = $widget_val['_multiwidget'];
$widgets_array[ $widget['type'] ][ $widget['type-index'] ] = $widget_val[ $widget['type-index'] ];
if ( isset( $widgets_array[$widget['type']]['_multiwidget'] ) ) {
unset( $widgets_array[$widget['type']]['_multiwidget'] );
}
$widgets_array[ $widget['type'] ]['_multiwidget'] = $multiwidget_val;
}
unset( $widgets_array['export'] );
$export_array = array( $sidebar_export, $widgets_array );
$json = json_encode( $export_array );
echo $json;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment