Widget Settings Importer/Exporter - Version 1.0

Version Description

  • Refactoring for performace.
  • Documentation
  • Better styles for wp-admin
Download this release

Release Info

Developer markparolisi
Plugin Icon wp plugin Widget Settings Importer/Exporter
Version 1.0
Comparing to
See all releases

Version 1.0

Files changed (6) hide show
  1. readme.txt +43 -0
  2. screenshot-1.png +0 -0
  3. screenshot-2.png +0 -0
  4. widget-data.css +71 -0
  5. widget-data.js +134 -0
  6. widget-data.php +528 -0
readme.txt ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Widget Settings Importer/Exporter ===
2
+ Contributors: kevinlangleyjr, smccafferty, markparolisi, voceplatforms
3
+ Tags: widget, import, export
4
+ Requires at least: 2.8
5
+ Tested up to: 3.3
6
+ Stable tag: 1.0
7
+
8
+ Allows you to export and import widgets settings.
9
+
10
+ == Description ==
11
+
12
+ Gives the user the ability to export the current widget settings and states as a json file. You can then import those settings on a different server or installation of WordPress so you have the same widgets within the same sidebars as the export. The import will not overwrite any data currently within the sidebars, but instead will increment the widgets and add a new instance of the widget instead.
13
+
14
+ **Please note that the plugin currently does not import anything if that particular sidebar is unavailable during the import.
15
+
16
+ == Installation ==
17
+
18
+ 1. Upload entire widget-data directory to the `/wp-content/plugins/` directory
19
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
20
+
21
+ == Screenshots ==
22
+
23
+ 1. Choose which widgets to export
24
+ 2. Upload JSON export file
25
+
26
+ == Changelog ==
27
+
28
+ = 1.0 =
29
+ * Refactoring for performace.
30
+ * Documentation
31
+ * Better styles for wp-admin
32
+
33
+ = 0.4 =
34
+ * Fixing headers already sent error
35
+
36
+ = 0.3 =
37
+ * Fixing export of empty file by instead of creating temp file, it will just output the json
38
+
39
+ = 0.2 =
40
+ * Fixing blank redirect with WP installed within sub directory
41
+
42
+ = 0.1 =
43
+ * First Version
screenshot-1.png ADDED
Binary file
screenshot-2.png ADDED
Binary file
widget-data.css ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .widget-data .title {
2
+ border-style: solid;
3
+ border-width: 1px;
4
+ font-size: 13px;
5
+ border-color: #DFDFDF;
6
+ color: #464646;
7
+ text-shadow: 0 1px 0 #FFFFFF;
8
+ background-color: #F1F1F1;
9
+ background-image: -moz-linear-gradient(center top , #F9F9F9, #ECECEC);
10
+ }
11
+
12
+
13
+ .widget-data .title h3 {
14
+ font-family: Georgia,"Times New Roman","Bitstream Charter",Times,serif;
15
+ font-size: 15px;
16
+ font-weight: normal;
17
+ margin: 0;
18
+ overflow: hidden;
19
+ padding: 8px 10px;
20
+ white-space: nowrap;
21
+ width: 100px;
22
+ }
23
+
24
+ .widget-data .sidebars {
25
+ border-color: #DFDFDF;
26
+ border-style: solid;
27
+ border-width: 1px;
28
+ }
29
+
30
+ #notifier{
31
+ display:block;
32
+ margin: 5px 0 15px;
33
+ padding: 0.6em;
34
+ -webkit-border-radius: 3px;
35
+ border-radius: 3px;
36
+ border-width: 1px;
37
+ border-style: solid;
38
+ }
39
+
40
+ #notifier p {
41
+ margin: .5em 0;
42
+ padding: 2px;
43
+ }
44
+
45
+ #notifier.success {
46
+ background-color: lightYellow;
47
+ border-color: #E6DB55;
48
+ }
49
+
50
+ #notifier.error {
51
+ background-color: #FFEBE8;
52
+ border-color: #C00;
53
+ }
54
+
55
+ #upload-button { display:none; margin:4px 0 0 10px; }
56
+
57
+ #output-text {background:#fff; display:block; width:400px; height:20px; border:solid 1px #eee; padding:5px;}
58
+
59
+ #button-upload {margin-top:20px;}
60
+
61
+ .widget-data .left {float: left; margin-bottom:10px;}
62
+ .widget-data .right {float: right}
63
+ .widget-data .sidebars .sidebar label {font-size: 12px;}
64
+ .widget-data .sidebars .sidebar h4 {margin: 0; padding: 8px 20px; background:#eee;}
65
+ .widget-data .button-bottom {margin-top: 10px;}
66
+ .widget-data .export-selection-error {float: right;}
67
+ .widget-data .title p {margin: 0; float: right; padding-top: 10px; font-size: 11px; color: red; padding: 10px 10px 0 0; display: none;}
68
+
69
+ div.sidebar div.widgets .import-form-row { padding-left:30px; }
70
+ div.sidebar div.widgets .import-form-row:nth-child(odd) { background-color:#fff; }
71
+ div.sidebar div.widgets .import-form-row:nth-child(even){ background-color:#F9F9F9; }
widget-data.js ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*global window, document,jQuery,widgets_url,ajaxurl*/
2
+ (function(){
3
+ "use strict";
4
+
5
+ var $ = jQuery;
6
+
7
+ /**
8
+ * Display the notification div, populate a message, and set a CSS class
9
+ * @param String message Message to display
10
+ * @param String className CSS class of error or success
11
+ */
12
+ function show_notification(message, className){
13
+ var $notifier = $('#notifier');
14
+ className = className || 'success';
15
+ $notifier.html('<p>'+message+'</p>');
16
+ $notifier.addClass(className).fadeIn('slow');
17
+ jQuery('body,html').animate({
18
+ scrollTop: 0
19
+ }, 800);
20
+ }
21
+
22
+ /**
23
+ * Push the user to the Widget management page in wp-admin
24
+ */
25
+ function redirect_to_widgets() {
26
+ window.location.replace(widgets_url);
27
+ }
28
+
29
+ jQuery(function($){
30
+
31
+ var wrapper = $('<div/>').css({
32
+ height:0,
33
+ width:0,
34
+ 'overflow':'hidden'
35
+ }),
36
+ $fileInput = $('#upload-file').wrap(wrapper),
37
+ $widgetCheckbox = $('.widget-checkbox'),
38
+ $widgetSelectionError = $('.widget-selection-error');
39
+
40
+ /**
41
+ * Check all widget checkbox fields on click
42
+ * @param Object e Event object
43
+ */
44
+ $('#SelectAllActive').click(function(e){
45
+ e.preventDefault();
46
+ $widgetCheckbox.not(":checked").each(function(){
47
+ $(this).attr( 'checked', true );
48
+ });
49
+ });
50
+
51
+ /**
52
+ * Uncheck all widget checkbox fields on click
53
+ * @param Object e Event object
54
+ */
55
+ $('#UnSelectAllActive').click(function(e){
56
+ e.preventDefault();
57
+ $widgetCheckbox.filter(":checked").each(function(){
58
+ $(this).attr( 'checked', false );
59
+ });
60
+ });
61
+
62
+ /**
63
+ * Handle the export form submission
64
+ * @param Object e Event object
65
+ */
66
+ $('form#widget-export-settings').submit(function(e) {
67
+ // return and show notification if no widgets are selected
68
+ if ($widgetCheckbox.filter(':checked').length === 0) {
69
+ e.preventDefault();
70
+ $widgetSelectionError.fadeIn('slow').delay(2000).fadeOut('slow');
71
+ return;
72
+ }
73
+ var message = 'All of the requested widgets have been exported.';
74
+ $('form#widget-export-settings').fadeOut('slow');
75
+ window.setTimeout(redirect_to_widgets, 4000);
76
+ show_notification(message, 'success');
77
+ });
78
+
79
+ /***
80
+ * Handle imports
81
+ * @param Object e Event object
82
+ */
83
+ $('form#import-widget-data').submit(function(e){
84
+ e.preventDefault();
85
+
86
+ if ($widgetCheckbox.filter(':checked').length === 0) {
87
+ $widgetSelectionError.fadeIn('slow').delay(2000).fadeOut('slow');
88
+ return false;
89
+ }
90
+ var message, newClass;
91
+ $.ajax({
92
+ type:'POST',
93
+ url: ajaxurl,
94
+ data: $("#import-widget-data").serialize()
95
+ })
96
+ .done(function(data){
97
+ //@TODO check response code or something better than a response string
98
+ if(data === "SUCCESS") {
99
+ message = 'All widgets with registered sidebars have been imported successfully.';
100
+ newClass = 'success';
101
+ $('.import-wrapper').fadeOut('slow');
102
+ window.setTimeout(redirect_to_widgets, 4000);
103
+ } else {
104
+ message = 'There was a problem importing your widgets. Please try again.';
105
+ newClass = 'error';
106
+ }
107
+ show_notification(message, newClass);
108
+ });
109
+
110
+ });
111
+
112
+ /**
113
+ *
114
+ */
115
+ $fileInput.change(function(){
116
+ var $this = $(this), $outputText = $('#output-text'),
117
+ sub = $this.val().lastIndexOf('\\') + 1,
118
+ newString = $this.val().substring(sub);
119
+ $outputText.text(newString);
120
+ $outputText.fadeIn('slow');
121
+ });
122
+
123
+ /**
124
+ *
125
+ */
126
+ $('#upload-button').click(function(e){
127
+ e.preventDefault();
128
+ $fileInput.click();
129
+ }).show();
130
+ });
131
+
132
+
133
+ }(window, document,jQuery,widgets_url,ajaxurl));
134
+
widget-data.php ADDED
@@ -0,0 +1,528 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Widget Data - Setting Import/Export Plugin
4
+ Description: Adds functionality to export and import widget data
5
+ Authors: Kevin Langley, Sean McCafferty, Mark Parolisi
6
+ Version: 1.0
7
+ * ******************************************************************
8
+ Copyright 2011-2011 Kevin Langley & Sean McCafferty (email : klangley@voceconnect.com & smccafferty@voceconnect.com)
9
+
10
+ This program is free software: you can redistribute it and/or modify
11
+ it under the terms of the GNU General Public License as published by
12
+ the Free Software Foundation, either version 3 of the License, or
13
+ (at your option) any later version.
14
+
15
+ This program is distributed in the hope that it will be useful,
16
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ GNU General Public License for more details.
19
+
20
+ You should have received a copy of the GNU General Public License
21
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
22
+ * ******************************************************************
23
+ */
24
+
25
+ class Widget_Data {
26
+
27
+ var $import_filename;
28
+
29
+ /**
30
+ * @constructor
31
+ */
32
+ function __construct() {
33
+ add_action( 'admin_menu', array( $this, 'add_admin_menus' ) );
34
+ add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_scripts' ) );
35
+ add_action( 'load-tools_page_widget-settings-export', array( $this, 'export_widget_settings' ) );
36
+ add_action( 'wp_ajax_widget_import_submit', array( $this, 'widget_import_submit' ) );
37
+ add_filter( 'upload_mimes', array( $this, 'json_upload_mimes' ) );
38
+ }
39
+
40
+ /**
41
+ * Load JS and CSS
42
+ * @param type $hook
43
+ */
44
+ function add_admin_scripts( $hook ) {
45
+ if ( 'tools_page_widget-settings-import' != $hook || 'tools_page_widget-settings-export' != $hook ) {
46
+ wp_register_style( 'widget_data_css', plugins_url( '/widget-data.css', __FILE__ ) );
47
+ wp_enqueue_style( 'widget_data_css' );
48
+ wp_register_script( 'widget_data', plugins_url( '/widget-data.js', __FILE__ ) );
49
+ wp_enqueue_script( 'widget_data' );
50
+ wp_localize_script( 'widget_data', 'widgets_url', get_admin_url( false, 'widgets.php' ) );
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Register admin pages
56
+ */
57
+ function add_admin_menus() {
58
+ // export
59
+ $this->submenu_export = add_submenu_page( 'tools.php', 'Widget Settings Export', 'Widget Settings Export', 'administrator', 'widget-settings-export', array( &$this, 'export_settings_page' ) );
60
+ //import
61
+ $this->submenu_import = add_submenu_page( 'tools.php', 'Widget Settings Import', 'Widget Settings Import', 'administrator', 'widget-settings-import', array( &$this, 'import_settings_page' ) );
62
+ }
63
+
64
+ /**
65
+ * HTML for export admin page
66
+ */
67
+ function export_settings_page() {
68
+ $sidebar_widgets = $this->order_sidebar_widgets( wp_get_sidebars_widgets() );
69
+ ?>
70
+ <div class="widget-data export-widget-settings">
71
+ <div class="wrap">
72
+ <h2>Widget Setting Export</h2>
73
+ <div id="notifier" style="display: none;"></div>
74
+ <form action="" method="post" id="widget-export-settings">
75
+ <div class="left">
76
+ <button class="button-bottom button button-highlighted" type="button" name="SelectAllActive" id="SelectAllActive">Select All Active Widgets</button>
77
+ <button class="button-bottom button button-highlighted" type="button" name="UnSelectAllActive" id="UnSelectAllActive">Un-Select All Active Widgets</button>
78
+ </div>
79
+ <div style="clear:both;"></div>
80
+ <div class="title">
81
+ <p class="widget-selection-error">Please select a widget to continue.</p>
82
+ <h3>Sidebars</h3>
83
+ <div class="clear"></div>
84
+ </div>
85
+ <div class="sidebars">
86
+ <?php
87
+ foreach ( $sidebar_widgets as $sidebar_name => $widget_list ) :
88
+ if ( count( $widget_list ) == 0 )
89
+ continue;
90
+
91
+ $sidebar_info = $this->get_sidebar_info( $sidebar_name );
92
+ ?>
93
+
94
+ <div class="sidebar">
95
+ <h4><?php echo $sidebar_info['name']; ?></h4>
96
+
97
+ <div class="widgets">
98
+ <?php
99
+ foreach ( $widget_list as $widget ) :
100
+
101
+ $widget_type = trim( substr( $widget, 0, strrpos( $widget, '-' ) ) );
102
+ $widget_type_index = trim( substr( $widget, strrpos( $widget, '-' ) + 1 ) );
103
+ $option_name = 'widget_' . $widget_type;
104
+ $widget_options = get_option( $option_name );
105
+ $widget_title = isset( $widget_options[$widget_type_index]['title'] ) ? $widget_options[$widget_type_index]['title'] : '';
106
+ ?>
107
+ <div class="import-form-row">
108
+ <input class="<?php echo ($sidebar_name == 'wp_inactive_widgets') ? 'inactive' : 'active'; ?> widget-checkbox" type="checkbox" name="<?php echo $widget; ?>" id="meta_<?php echo $widget; ?>" />
109
+ <label for="meta_<?php echo $widget; ?>">&nbsp;
110
+ <?php
111
+ echo ucfirst( $widget_type );
112
+
113
+ if ( !empty( $widget_title ) ) :
114
+ echo (' - ' . $widget_title);
115
+ else :
116
+ echo (' - ' . $widget_type_index);
117
+ endif;
118
+ ?>
119
+ </label>
120
+ </div>
121
+ <?php endforeach; ?>
122
+ </div> <!-- end widgets -->
123
+ </div> <!-- end sidebar -->
124
+ <?php endforeach; ?>
125
+ </div> <!-- end sidebars -->
126
+ <div class="right">
127
+ <button class="button-bottom button-primary" type="submit" name="export-widgets" id="export-widgets">Export Widget Settings</button>
128
+ </div>
129
+ </form>
130
+ </div> <!-- end wrap -->
131
+ </div> <!-- end export-widget-settings -->
132
+ <?php
133
+ }
134
+
135
+ /**
136
+ * HTML for import admin page
137
+ * @return type
138
+ */
139
+ function import_settings_page() {
140
+ ?>
141
+ <div class="widget-data import-widget-settings">
142
+ <div class="wrap">
143
+ <h2>Widget Setting Import</h2>
144
+
145
+ <?php if ( isset( $_FILES['upload-file'] ) ) : ?>
146
+ <div id="notifier" style="display: none;"></div>
147
+ <div class="import-wrapper">
148
+ <div class="left">
149
+ <button class="button-bottom button button-highlighted" type="button" name="SelectAllActive" id="SelectAllActive">Select All Active Widgets</button>
150
+ <button class="button-bottom button button-highlighted" type="button" name="UnSelectAllActive" id="UnSelectAllActive">Un-Select All Active Widgets</button>
151
+ </div>
152
+ <div style="clear:both;"></div>
153
+ <form action="" id="import-widget-data" method="post">
154
+ <?php
155
+ $json = $this->get_widget_settings_json();
156
+ $json_data = json_decode( $json[0], true );
157
+ $json_file = $json[1];
158
+
159
+ if ( !$json_data ) {
160
+ return;
161
+ }
162
+ ?>
163
+ <div class="title">
164
+ <p class="widget-selection-error">Please select a widget to continue.</p>
165
+ <h3>Sidebars</h3>
166
+ <div class="clear"></div>
167
+ </div>
168
+ <div class="sidebars">
169
+ <?php
170
+ if ( isset( $json_data[0] ) ) :
171
+ foreach ( $this->order_sidebar_widgets( $json_data[0] ) as $sidebar_name => $widget_list ) :
172
+ if ( count( $widget_list ) == 0 ) {
173
+ continue;
174
+ }
175
+ $sidebar_info = $this->get_sidebar_info( $sidebar_name );
176
+ ?>
177
+
178
+ <?php if ( $sidebar_info ) : ?>
179
+ <div class="sidebar">
180
+ <h4><?php echo $sidebar_info['name']; ?></h4>
181
+
182
+ <div class="widgets">
183
+ <?php
184
+ foreach ( $widget_list as $widget ) :
185
+ $widget_options = false;
186
+
187
+ $widget_type = trim( substr( $widget, 0, strrpos( $widget, '-' ) ) );
188
+ $widget_type_index = trim( substr( $widget, strrpos( $widget, '-' ) + 1 ) );
189
+ $option_name = 'widget_' . $widget_type;
190
+ $widget_type_options = $this->get_option_from_array( $widget_type, $json_data[1] );
191
+ if ( $widget_type_options ) :
192
+ $widget_title = isset( $widget_type_options[$widget_type_index]['title'] ) ? $widget_type_options[$widget_type_index]['title'] : '';
193
+ $widget_options = $widget_type_options[$widget_type_index];
194
+ endif;
195
+ ?>
196
+ <div class="import-form-row">
197
+ <input class="<?php echo ($sidebar_name == 'wp_inactive_widgets') ? 'inactive' : 'active'; ?> widget-checkbox" type="checkbox" name="widgets[<?php echo $widget_type; ?>][<?php echo $widget_type_index; ?>]" id="meta_<?php echo $widget; ?>" />
198
+ <label for="meta_<?php echo $widget; ?>">&nbsp;
199
+ <?php
200
+ echo ucfirst( $widget_type );
201
+
202
+ if ( !empty( $widget_title ) ) :
203
+ echo (' - ' . $widget_title);
204
+ else :
205
+ echo (' - ' . $widget_type_index);
206
+ endif;
207
+ ?>
208
+ </label>
209
+ </div>
210
+ <?php endforeach; ?>
211
+ </div> <!-- end widgets -->
212
+ </div> <!-- end sidebar -->
213
+ <?php endif; ?>
214
+ <?php endforeach; ?>
215
+ <?php endif; ?>
216
+ <input type="hidden" name="import_file" value="<?php echo $json_file; ?>"/>
217
+ <input type="hidden" name="action" value="widget_import_submit"/>
218
+ </div> <!-- end sidebars -->
219
+ <div class="right">
220
+ <button class="button-bottom button-primary" type="submit" name="import-widgets" id="import-widgets">Import Widget Settings</button>
221
+ </div>
222
+ </form>
223
+ </div>
224
+ <?php else : ?>
225
+ <form action="" id="upload-widget-data" method="post" enctype="multipart/form-data">
226
+ <p>Select the file that contains widget settings</p>
227
+ <div id="output-text" style="float:left;"></div>
228
+ <label>
229
+ <input type="file" name="upload-file" id="upload-file" size="40" />
230
+ <button id="upload-button" class="button-secondary">Select a file</button>
231
+ </label>
232
+ <div style="clear:both;"></div>
233
+ <div class="block">
234
+ <button type="submit" name="button-upload" id="button-upload" class="button">Show Widget Settings</button>
235
+ </div>
236
+ </form>
237
+ <?php endif; ?>
238
+ </div> <!-- end wrap -->
239
+ </div> <!-- end import-widget-settings -->
240
+ <?php
241
+ }
242
+
243
+ /**
244
+ * Retrieve widgets from sidebars and create JSON object
245
+ * @param array $posted_array
246
+ * @return string
247
+ */
248
+ function parse_export_data( $posted_array ) {
249
+ $sidebars_array = get_option( 'sidebars_widgets' );
250
+ $sidebar_export = array( );
251
+ foreach ( $sidebars_array as $sidebar => $widgets ) {
252
+ if ( !empty( $widgets ) && is_array( $widgets ) ) {
253
+ foreach ( $widgets as $sidebar_widget ) {
254
+ if ( in_array( $sidebar_widget, array_keys( $posted_array ) ) ) {
255
+ $sidebar_export[$sidebar][] = $sidebar_widget;
256
+ }
257
+ }
258
+ }
259
+ }
260
+ $widgets = array( );
261
+ foreach ( $posted_array as $k => $v ) {
262
+ $widget = array( );
263
+ $widget['type'] = trim( substr( $k, 0, strrpos( $k, '-' ) ) );
264
+ $widget['type-index'] = trim( substr( $k, strrpos( $k, '-' ) + 1 ) );
265
+ $widget['export_flag'] = ($v == 'on') ? true : false;
266
+ $widgets[] = $widget;
267
+ }
268
+ $widgets_array = array( );
269
+ foreach ( $widgets as $widget ) {
270
+ $widget_val = get_option( 'widget_' . $widget['type'] );
271
+ $multiwidget_val = $widget_val['_multiwidget'];
272
+ $widgets_array[$widget['type']][$widget['type-index']] = $widget_val[$widget['type-index']];
273
+ if ( isset( $widgets_array[$widget['type']]['_multiwidget'] ) ) {
274
+ unset( $widgets_array[$widget['type']]['_multiwidget'] );
275
+ }
276
+ $widgets_array[$widget['type']]['_multiwidget'] = $multiwidget_val;
277
+ }
278
+ unset( $widgets_array['export'] );
279
+ $export_array = array( $sidebar_export, $widgets_array );
280
+ $json = json_encode( $export_array );
281
+ return $json;
282
+ }
283
+
284
+ /**
285
+ * Import widgets
286
+ * @param array $import_array
287
+ */
288
+ function parse_import_data( $import_array ) {
289
+ $sidebars_data = $import_array[0];
290
+ $widget_data = $import_array[1];
291
+ $current_sidebars = get_option( 'sidebars_widgets' );
292
+ $new_widgets = array( );
293
+
294
+ foreach ( $sidebars_data as $import_sidebar => $import_widgets ) :
295
+
296
+ foreach ( $import_widgets as $import_widget ) :
297
+ //if the sidebar exists
298
+ if ( isset( $current_sidebars[$import_sidebar] ) ) :
299
+ $title = trim( substr( $import_widget, 0, strrpos( $import_widget, '-' ) ) );
300
+ $index = trim( substr( $import_widget, strrpos( $import_widget, '-' ) + 1 ) );
301
+ $current_widget_data = get_option( 'widget_' . $title );
302
+ $new_widget_name = $this->get_new_widget_name( $title, $index );
303
+ $new_index = trim( substr( $new_widget_name, strrpos( $new_widget_name, '-' ) + 1 ) );
304
+
305
+ if ( is_array( $new_widgets[$title] ) ) {
306
+ while ( array_key_exists( $new_index, $new_widgets[$title] ) ) {
307
+ $new_index++;
308
+ }
309
+ }
310
+ $current_sidebars[$import_sidebar][] = $title . '-' . $new_index;
311
+ if ( array_key_exists( $title, $new_widgets ) ) {
312
+ $new_widgets[$title][$new_index] = $widget_data[$title][$index];
313
+ $multiwidget = $new_widgets[$title]['_multiwidget'];
314
+ unset( $new_widgets[$title]['_multiwidget'] );
315
+ $new_widgets[$title]['_multiwidget'] = $multiwidget;
316
+ } else {
317
+ $current_widget_data[$new_index] = $widget_data[$title][$index];
318
+ $current_multiwidget = $current_widget_data['_multiwidget'];
319
+ $new_multiwidget = $widget_data[$title]['_multiwidget'];
320
+ $multiwidget = ($current_multiwidget != $new_multiwidget) ? $current_multiwidget : 1;
321
+ unset( $current_widget_data['_multiwidget'] );
322
+ $current_widget_data['_multiwidget'] = $multiwidget;
323
+ $new_widgets[$title] = $current_widget_data;
324
+ }
325
+
326
+ endif;
327
+ endforeach;
328
+ endforeach;
329
+
330
+ if ( isset( $new_widgets ) && isset( $current_sidebars ) ) {
331
+ update_option( 'sidebars_widgets', $current_sidebars );
332
+ foreach ( $new_widgets as $title => $content ) {
333
+ update_option( 'widget_' . $title, $content );
334
+ }
335
+
336
+ return true;
337
+ } else {
338
+ return false;
339
+ }
340
+ }
341
+
342
+ /**
343
+ * Output the JSON for download
344
+ */
345
+ function export_widget_settings() {
346
+ // @TODO check something better than just $_POST
347
+ if ( $_POST ) {
348
+ header( "Content-Description: File Transfer" );
349
+ header( "Content-Disposition: attachment; filename=widget_data.json" );
350
+ header( "Content-Type: application/octet-stream" );
351
+ echo $json = $this->parse_export_data( $_POST );
352
+ exit;
353
+ }
354
+ }
355
+
356
+ /**
357
+ * Parse JSON import file and load
358
+ */
359
+ function widget_import_submit() {
360
+ $widgets = $_POST['widgets'];
361
+ $json_data = file_get_contents( $_POST['import_file'] );
362
+ $json_data = json_decode( $json_data, true );
363
+ $sidebar_data = $json_data[0];
364
+ $widget_data = $json_data[1];
365
+ foreach ( $sidebar_data as $title => &$sidebar ) {
366
+ $count = count( $sidebar );
367
+ for ( $i = 0; $i < $count; $i++ ) {
368
+ $widget = array( );
369
+ $widget['type'] = trim( substr( $sidebar[$i], 0, strrpos( $sidebar[$i], '-' ) ) );
370
+ $widget['type-index'] = trim( substr( $sidebar[$i], strrpos( $sidebar[$i], '-' ) + 1 ) );
371
+ if ( !isset( $widgets[$widget['type']][$widget['type-index']] ) ) {
372
+ unset( $sidebar_data[$title][$i] );
373
+ }
374
+ }
375
+ $sidebar_data[$title] = array_values( $sidebar_data[$title] );
376
+ }
377
+
378
+ foreach ( $widgets as $widget_title => $widget_value ) {
379
+ foreach ( $widget_value as $k => $v ) {
380
+ $widgets[$widget_title][$k] = $widget_data[$widget_title][$k];
381
+ }
382
+ }
383
+
384
+ $sidebar_data = array_filter( $sidebar_data );
385
+ $new_array = array( $sidebar_data, $widgets );
386
+ ini_set( 'display_errors', 0 );
387
+ error_reporting( 0 );
388
+ if ( $this->parse_import_data( $new_array ) ) {
389
+ echo "SUCCESS";
390
+ } else {
391
+ echo "ERROR";
392
+ }
393
+
394
+ exit;
395
+ }
396
+
397
+ /**
398
+ * Read uploaded JSON file
399
+ * @return type
400
+ */
401
+ function get_widget_settings_json() {
402
+ $widget_settings = $this->upload_widget_settings_file();
403
+ $file_contents = file_get_contents( $widget_settings['file'] );
404
+ return array( $file_contents, $widget_settings['file'] );
405
+ }
406
+
407
+ /**
408
+ * Upload JSON file
409
+ * @return boolean
410
+ */
411
+ function upload_widget_settings_file() {
412
+ if ( isset( $_FILES['upload-file'] ) ) {
413
+ $overrides = array( 'test_form' => false );
414
+ $upload = wp_handle_upload( $_FILES['upload-file'], $overrides );
415
+
416
+ return $upload;
417
+ }
418
+
419
+ return false;
420
+ }
421
+
422
+ /**
423
+ *
424
+ * @param string $widget_name
425
+ * @param string $widget_index
426
+ * @return string
427
+ */
428
+ function get_new_widget_name( $widget_name, $widget_index ) {
429
+ $current_sidebars = get_option( 'sidebars_widgets' );
430
+ $all_widget_array = array( );
431
+ foreach ( $current_sidebars as $sidebar => $widgets ) {
432
+ if ( !empty( $widgets ) && is_array( $widgets ) && $sidebar != 'wp_inactive_widgets' ) {
433
+ foreach ( $widgets as $w ) {
434
+ $all_widget_array[] = $w;
435
+ }
436
+ }
437
+ }
438
+ while ( in_array( $widget_name . '-' . $widget_index, $all_widget_array ) ) {
439
+ $widget_index++;
440
+ }
441
+ $new_widget_name = $widget_name . '-' . $widget_index;
442
+ return $new_widget_name;
443
+ }
444
+
445
+ /**
446
+ *
447
+ * @param string $option_name
448
+ * @param array $array_options
449
+ * @return boolean
450
+ */
451
+ function get_option_from_array( $option_name, $array_options ) {
452
+ foreach ( $array_options as $name => $option ) {
453
+ if ( $name == $option_name ) {
454
+ return $option;
455
+ }
456
+ }
457
+ return false;
458
+ }
459
+
460
+ /**
461
+ *
462
+ * @global type $wp_registered_sidebars
463
+ * @param type $sidebar_id
464
+ * @return boolean
465
+ */
466
+ function get_sidebar_info( $sidebar_id ) {
467
+ global $wp_registered_sidebars;
468
+
469
+ //since wp_inactive_widget is only used in widgets.php
470
+ if ( $sidebar_id == 'wp_inactive_widgets' ) {
471
+ return array( 'name' => 'Inactive Widgets', 'id' => 'wp_inactive_widgets' );
472
+ }
473
+
474
+ foreach ( $wp_registered_sidebars as $sidebar ) {
475
+ if ( isset( $sidebar['id'] ) && $sidebar['id'] == $sidebar_id ) {
476
+ return $sidebar;
477
+ }
478
+ }
479
+
480
+ return false;
481
+ }
482
+
483
+ /**
484
+ *
485
+ * @global type $wp_registered_widgets
486
+ * @param type $widget
487
+ * @return boolean
488
+ */
489
+ function get_widget_info( $widget ) {
490
+ global $wp_registered_widgets;
491
+ if ( isset( $wp_registered_widgets[$widget] ) ) {
492
+ return true;
493
+ } else {
494
+ return false;
495
+ }
496
+ }
497
+
498
+ /**
499
+ *
500
+ * @param array $sidebar_widgets
501
+ * @return type
502
+ */
503
+ function order_sidebar_widgets( $sidebar_widgets ) {
504
+ $inactive_widgets = false;
505
+
506
+ //seperate inactive widget sidebar from other sidebars so it can be moved to the end of the array, if it exists
507
+ if ( isset( $sidebar_widgets['wp_inactive_widgets'] ) ) {
508
+ $inactive_widgets = $sidebar_widgets['wp_inactive_widgets'];
509
+ unset( $sidebar_widgets['wp_inactive_widgets'] );
510
+ $sidebar_widgets['wp_inactive_widgets'] = $inactive_widgets;
511
+ }
512
+
513
+ return $sidebar_widgets;
514
+ }
515
+
516
+ /**
517
+ * Add mime type for JSON
518
+ * @param array $existing_mimes
519
+ * @return string
520
+ */
521
+ function json_upload_mimes( $existing_mimes = array( ) ) {
522
+ $existing_mimes['json'] = 'application/json';
523
+ return $existing_mimes;
524
+ }
525
+
526
+ }
527
+
528
+ add_action( 'init', create_function( '', 'new Widget_Data();' ) );