-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwidget-import.php
117 lines (107 loc) · 3.2 KB
/
widget-import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/*
Plugin Name: WP CLI Widget Import
Description: Import and Export Sidebars
Author: Tom J Nowell, Code For The People
Version: 1.0
Author URI: http://codeforthepeople.net
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
/**
* Import and export all sidebars.
*
* ## EXAMPLES
*
* # Export sidebars from a site
* wp sidebars export > sidebars.tmp
*
* # Import previously exported sidebars settings
* cat sidebars.tmp | wp sidebars import
*
*/
class Sidebars_Command extends WP_CLI_Command {
/**
* Export sidebars settings.
*
* Outputs a serialised, base64 encoded array of sidebar settings. Best used by exporting to a file.
*
* ## EXAMPLES
*
* wp sidebars export > sidebars.tmp
*
* @subcommand export
*/
public function export( $args, $assoc_args ) {
$assoc_args['format'] = 'json';
$sidebars_array = get_option( 'sidebars_widgets' );
$widgets = array_values( $sidebars_array );
$tmp = array();
foreach ( $widgets as $widget_list ) {
if ( count( $widget_list ) && is_array( $widget_list ) ) {
$values = array_values( $widget_list );
$tmp = array_merge( $values, $tmp );
}
}
$widget_options = array();
foreach ( $tmp as $widget ) {
$matches = array();
$match = preg_match( '/^([a-zA-Z0-9\-_]+)\-(\d+)$/', $widget, $matches );
if ( ! $match ) {
continue;
}
if ( count( $matches ) >= 2 ) {
list( , $widget_name ) = $matches;
$options = get_option( 'widget_' . $widget_name );
$widget_options['widget_' . $widget_name] = $options;
}
}
$export = array(
'sidebars' => $sidebars_array,
'widget_options' => $widget_options,
);
echo base64_encode( serialize( $export ) );
}
/**
* Import sidebars settings.
*
* Takes the output generated by the sidebars export command and applies it to a site.
*
* ## EXAMPLES
*
* cat sidebars.tmp | wp sidebars import
*
* @subcommand import
*/
public function import( $args, $assoc_args ) {
$file = 'php://stdin';
if ( ! empty( $assoc_args['data'] ) ) {
$file = $assoc_args['data'];
}
$contents = file_get_contents( $file );
if ( empty( $contents ) ) {
WP_CLI::error( 'failed to read input' );
}
$data = unserialize( trim( base64_decode( $contents ) ) );
$sidebars = $data['sidebars'];
$widget_options = $data['widget_options'];
foreach ( $widget_options as $widget_name => $value ) {
$current_value = get_option( $widget_name );
if ( $current_value == $value ) {
continue;
}
if ( ! update_option( $widget_name, $value ) ) {
WP_CLI::error( "Could not update widget option '$widget_name'.", false ); // continue run
} else {
WP_CLI::success( "Updated options '$widget_name'." );
}
}
$updated = update_option( 'sidebars_widgets', $sidebars );
if ( $updated ) {
WP_CLI::success( 'Sidebar options updated' );
} else {
WP_CLI::error( 'Sidebar options failed' );
}
}
}
WP_CLI::add_command( 'sidebars', 'Sidebars_Command' );
}