settings
2 years ago
globals.php
2 years ago
import-export.php
2 years ago
notices.php
2 years ago
welcome.php
2 years ago
import-export.php
718 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Widget Options Importer |
| 5 | * |
| 6 | * @category Widgets |
| 7 | * @author Jeffrey Carandang |
| 8 | * @version 1.0 |
| 9 | */ |
| 10 | // Exit if accessed directly. |
| 11 | if (!defined('ABSPATH')) exit; |
| 12 | if (!class_exists('WP_Widget_Options_Importer')) : |
| 13 | |
| 14 | /** |
| 15 | * Main WP_Widget_Options_Importer Class. |
| 16 | * |
| 17 | * @since 1.0 |
| 18 | */ |
| 19 | class WP_Widget_Options_Importer |
| 20 | { |
| 21 | |
| 22 | protected $imported = array(); |
| 23 | |
| 24 | function __construct() |
| 25 | { |
| 26 | global $widget_options; |
| 27 | if ((isset($widget_options['import_export']) && 'activate' == $widget_options['import_export']) || |
| 28 | (isset($widget_options['widget_area']) && 'activate' == $widget_options['widget_area']) |
| 29 | ) { |
| 30 | add_action('admin_menu', array(&$this, 'options_page'), 10); |
| 31 | add_action('wp_ajax_widgetopts_migrator', array(&$this, 'ajax_migration')); |
| 32 | add_action('load-tools_page_widgetopts_migrator_settings', array(&$this, 'export_json_file')); |
| 33 | add_action('load-tools_page_widgetopts_migrator_settings', array(&$this, 'import_json_file')); |
| 34 | |
| 35 | if (!isset($widget_options['import_export']) || (isset($widget_options['import_export']) && 'activate' != $widget_options['import_export'])) { |
| 36 | add_action('admin_footer', array(&$this, 'admin_footer'), 10); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function options_page() |
| 42 | { |
| 43 | add_management_page( |
| 44 | __('Import / Export Widgets', 'widget-options'), |
| 45 | __('Import / Export Widgets', 'widget-options'), |
| 46 | 'manage_options', |
| 47 | 'widgetopts_migrator_settings', |
| 48 | array(&$this, 'settings_page') |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | function settings_page() |
| 53 | { |
| 54 | $upgrade_url = apply_filters('widget_options_site_url', trailingslashit(WIDGETOPTS_PLUGIN_WEBSITE) . '?utm_source=wordpressadmin&utm_medium=importexport&utm_campaign=titlelink'); |
| 55 | ?> |
| 56 | <div class="wrap"> |
| 57 | <h1> |
| 58 | <?php _e('Import or Export Widgets', 'widget-options'); ?> |
| 59 | <a href="<?php echo esc_url(admin_url('options-general.php?page=widgetopts_plugin_settings')); ?>" class="page-title-action"><?php _e('Manage Widget Options', 'widget-options'); ?></a> |
| 60 | <a href="<?php echo esc_url(apply_filters('widget_options_upgrade_url', $upgrade_url)); ?>" target="_blank" class="page-title-action"><?php _e('Upgrade', 'widget-options'); ?></a> |
| 61 | </h1> |
| 62 | <div class="widgetopts-imex"> |
| 63 | <?php if (!empty($this->imported)) { |
| 64 | global $wp_registered_sidebars; |
| 65 | |
| 66 | //move inactive widgets to bottom |
| 67 | if (isset($this->imported['wp_inactive_widgets'])) { |
| 68 | $inactive = $this->imported['wp_inactive_widgets']; |
| 69 | unset($this->imported['wp_inactive_widgets']); |
| 70 | $this->imported['wp_inactive_widgets'] = $inactive; |
| 71 | } ?> |
| 72 | <div class="widgetopts-imex-imported-widgets widgetopts-imex-col"> |
| 73 | <h3><?php _e('Widget Import Results', 'widget-options'); ?></h3> |
| 74 | <p><?php |
| 75 | printf( |
| 76 | wp_kses( |
| 77 | __('Imported widgets displayed in their respective widget areas. Please take note that whenever the widget area is not available, the widget will be assigned automatically to Inactive Sidebar & Widgets for you to be able to still use them. To check and manage imported widgets go to <a href="%1$s">Appearance > Widgets</a>.', 'widget-options'), |
| 78 | array( |
| 79 | 'a' => array( |
| 80 | 'href' => array(), |
| 81 | ), |
| 82 | ) |
| 83 | ), |
| 84 | esc_url(admin_url('widgets.php')) |
| 85 | ); ?></p> |
| 86 | <?php foreach ($this->imported as $sidebar_id => $sidebar) { |
| 87 | if ($sidebar_id == 'sidebars') continue; |
| 88 | if (!isset($wp_registered_sidebars[$sidebar_id]) && $sidebar_id != 'wp_inactive_widgets') { |
| 89 | continue; |
| 90 | } |
| 91 | if (isset($sidebar['name'])) { |
| 92 | echo '<h4>' . $sidebar['name'] . '</h4>'; |
| 93 | } |
| 94 | if (isset($sidebar['widgets']) && !empty($sidebar['widgets'])) { |
| 95 | echo '<ul>'; |
| 96 | foreach ($sidebar['widgets'] as $widget_id => $widget) { |
| 97 | |
| 98 | ?> |
| 99 | |
| 100 | <li class="widgetopts-imex-imported-<?php echo $widget['message_type']; ?>"> |
| 101 | <span class="dashicons dashicons-<?php echo $widget['message_type']; ?>"></span> |
| 102 | <span class="widgetopts-imex-imported"> |
| 103 | <?php echo (isset($widget['title'])) ? $widget['title'] : ''; ?> |
| 104 | <span class="widgetopts-imex-wid"><?php echo $widget_id; ?></span> |
| 105 | <span class="widgetopts-imex-msg"><?php echo $widget['message']; ?></span> |
| 106 | </span> |
| 107 | </li> |
| 108 | |
| 109 | <?php } |
| 110 | echo '</ul>'; |
| 111 | } |
| 112 | } ?> |
| 113 | |
| 114 | </div> |
| 115 | <?php } ?> |
| 116 | |
| 117 | <div class="widgetopts-imex-col widgetopts-imex-col-1"> |
| 118 | <h3><?php _e('Export Widgets', 'widget-options'); ?></h3> |
| 119 | <p><?php _e('Click the button below to export all your website\'s widgets.', 'widget-options'); ?></p> |
| 120 | <form action="<?php echo esc_url(admin_url(basename($_SERVER['PHP_SELF']))); ?>" method="GET"> |
| 121 | <input type="hidden" name="page" value="<?php echo (isset($_GET['page'])) ? sanitize_text_field($_GET['page']) : 'widgetopts_migrator_settings'; ?>" /> |
| 122 | <input type="hidden" name="action" value="export" /> |
| 123 | <?php wp_nonce_field('widgeopts_export', 'widgeopts_nonce_export'); ?> |
| 124 | <p> |
| 125 | <input type="checkbox" value="1" name="inactive" id="widgetopts-imex-ex" /> |
| 126 | <label for="widgetopts-imex-ex"><?php _e('Check this option if you wish to include inactive widgets', 'widget-options'); ?></label> |
| 127 | </p> |
| 128 | <input type="submit" class="button button-primary" value="<?php _e('Export Widgets', 'widget-options'); ?>" /> |
| 129 | </form> |
| 130 | |
| 131 | </div> |
| 132 | |
| 133 | |
| 134 | <div class="widgetopts-imex-col"> |
| 135 | <h3><?php _e('Import Widgets', 'widget-options'); ?></h3> |
| 136 | <p><?php _e('Upload <strong>.json</strong> file that\'s generated by this plugin. Then manage the widgets to be imported.', 'widget-options'); ?></p> |
| 137 | |
| 138 | <form method="POST" enctype="multipart/form-data"> |
| 139 | <input type="hidden" name="page" value="<?php echo (isset($_GET['page'])) ? sanitize_text_field($_GET['page']) : 'widgetopts_migrator_settings'; ?>" /> |
| 140 | <input type="hidden" name="action" value="import" /> |
| 141 | <?php wp_nonce_field('widgeopts_import', 'widgeopts_nonce_import'); ?> |
| 142 | <p> |
| 143 | <input type="file" name="widgeopts_file" id="widgetopts-imex-file" /> |
| 144 | </p> |
| 145 | <input type="submit" class="button button-primary" value="<?php _e('Upload', 'widget-options'); ?>" /> |
| 146 | </form> |
| 147 | </div> |
| 148 | |
| 149 | </div> |
| 150 | |
| 151 | </div> |
| 152 | <style type="text/css"> |
| 153 | .widgetopts-imex-col { |
| 154 | float: left; |
| 155 | width: 48%; |
| 156 | background: #fff; |
| 157 | border: 1px solid #e5e5e5; |
| 158 | padding: 25px; |
| 159 | box-sizing: border-box; |
| 160 | margin-top: 30px; |
| 161 | min-height: 250px; |
| 162 | } |
| 163 | |
| 164 | .widgetopts-imex-col-1 { |
| 165 | margin-right: 1.5%; |
| 166 | } |
| 167 | |
| 168 | .widgetopts-imex-col.widgetopts-imex-imported-widgets { |
| 169 | width: 97.5%; |
| 170 | position: relative; |
| 171 | } |
| 172 | |
| 173 | .widgetopts-imex-col h3 { |
| 174 | margin-top: 0px; |
| 175 | } |
| 176 | |
| 177 | .widgetopts-imex-col p { |
| 178 | margin-bottom: 25px; |
| 179 | } |
| 180 | |
| 181 | .widgetopts-imex-col .button { |
| 182 | font-size: 14px; |
| 183 | padding: 0px 10px 0px; |
| 184 | } |
| 185 | |
| 186 | .widgetopts-imex-imported-widgets li { |
| 187 | padding-bottom: 10px; |
| 188 | } |
| 189 | |
| 190 | .widgetopts-imex-imported-widgets li .dashicons { |
| 191 | float: left; |
| 192 | font-size: 26px; |
| 193 | line-height: 40px; |
| 194 | height: 50px; |
| 195 | width: 31px; |
| 196 | } |
| 197 | |
| 198 | .widgetopts-imex-imported-widgets .dashicons-no-alt { |
| 199 | color: #a53e3e; |
| 200 | } |
| 201 | |
| 202 | .widgetopts-imex-imported-widgets .dashicons-warning { |
| 203 | color: #ec6c3b; |
| 204 | } |
| 205 | |
| 206 | .widgetopts-imex-imported-widgets .dashicons-yes { |
| 207 | color: #46b450; |
| 208 | } |
| 209 | |
| 210 | .widgetopts-imex-imported-widgets li .widgetopts-imex-wid, |
| 211 | .widgetopts-imex-imported-widgets li .widgetopts-imex-msg { |
| 212 | /* padding-left: 8px; */ |
| 213 | font-size: 12px; |
| 214 | color: #999; |
| 215 | } |
| 216 | |
| 217 | .widgetopts-imex-imported-widgets li.widgetopts-imex-imported-yes .widgetopts-imex-msg { |
| 218 | color: #46b450; |
| 219 | } |
| 220 | |
| 221 | .widgetopts-imex-imported-widgets li .widgetopts-imex-msg { |
| 222 | display: block; |
| 223 | color: #a53e3e; |
| 224 | } |
| 225 | |
| 226 | .widgetopts-imex-imported-widgets li label { |
| 227 | vertical-align: unset; |
| 228 | } |
| 229 | </style> |
| 230 | <?php } |
| 231 | |
| 232 | function admin_footer() |
| 233 | { ?> |
| 234 | <script type="text/javascript"> |
| 235 | jQuery(document).ready(function() { |
| 236 | jQuery('#adminmenu .menu-icon-tools a[href="tools.php?page=widgetopts_migrator_settings"]').hide(); |
| 237 | }); |
| 238 | </script> |
| 239 | <?php } |
| 240 | |
| 241 | /** |
| 242 | * Generate export data |
| 243 | * |
| 244 | * @since 1.0 |
| 245 | * @return string Export file contents |
| 246 | */ |
| 247 | function generate_export_data($inactive = false, $single_sidebar = false) |
| 248 | { |
| 249 | |
| 250 | global $wp_registered_widgets; |
| 251 | $checked = array(); |
| 252 | $sidebars_dummy = array(); |
| 253 | |
| 254 | // Store all widgets in array |
| 255 | $widget_instances = $this->get_widget_instances(); |
| 256 | |
| 257 | // print_r( $this->get_available_widgets() ); |
| 258 | |
| 259 | // get all sidebar widgets |
| 260 | $sidebars_widgets = get_option('sidebars_widgets'); |
| 261 | $sidebars_widget_instances = array(); |
| 262 | |
| 263 | if ($single_sidebar && isset($sidebars_widgets[$single_sidebar])) { |
| 264 | $sidebars_dummy[$single_sidebar] = $sidebars_widgets[$single_sidebar]; |
| 265 | |
| 266 | //switch to single sidebar |
| 267 | $sidebars_widgets = $sidebars_dummy; |
| 268 | } |
| 269 | |
| 270 | foreach ($sidebars_widgets as $sidebar_id => $widgetids) { |
| 271 | |
| 272 | // Skip inactive widgets. |
| 273 | if (!$inactive && 'wp_inactive_widgets' === $sidebar_id) { |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | // Skip not array or empty data |
| 278 | if (!is_array($widgetids) || empty($widgetids)) { |
| 279 | continue; |
| 280 | } |
| 281 | |
| 282 | // Loop widget IDs for this sidebar. |
| 283 | foreach ($widgetids as $widgetid) { |
| 284 | |
| 285 | // Is there an instance for this widget ID? |
| 286 | if (isset($widget_instances[$widgetid])) { |
| 287 | |
| 288 | // Add to array. |
| 289 | $sidebars_widget_instances[$sidebar_id][$widgetid] = $widget_instances[$widgetid]; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | $sidebars_widget_instances['widgetopts_exported'] = true; |
| 295 | |
| 296 | $data = apply_filters('widgetopts_unencoded_export_data', $sidebars_widget_instances); |
| 297 | $encoded = wp_json_encode($data); |
| 298 | |
| 299 | return apply_filters('widgetopts_exported_data', $encoded); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Export .json file with widgets data |
| 304 | */ |
| 305 | function export_json_file() |
| 306 | { |
| 307 | |
| 308 | // Export requested. |
| 309 | if (isset($_GET['action']) && 'export' == sanitize_text_field($_GET['action'])) { |
| 310 | |
| 311 | // Check referer before doing anything else. |
| 312 | check_admin_referer('widgeopts_export', 'widgeopts_nonce_export'); |
| 313 | |
| 314 | $inactive = false; |
| 315 | $single_sidebar = false; |
| 316 | |
| 317 | if (isset($_GET['inactive']) && !empty($_GET['inactive'])) { |
| 318 | $inactive = true; |
| 319 | } |
| 320 | |
| 321 | if (isset($_GET['single_sidebar']) && !empty($_GET['single_sidebar'])) { |
| 322 | $single_sidebar = sanitize_text_field($_GET['single_sidebar']); |
| 323 | } |
| 324 | |
| 325 | // Build filename similar with Widget Importer & Exporter Plugin but on json extension |
| 326 | // Single Site: yoursite.com-widgets.json |
| 327 | // Multisite: site.multisite.com-widgets.json or multisite.com-site-widgets.json. |
| 328 | $site_url = site_url('', 'http'); |
| 329 | $site_url = trim($site_url, '/\\'); // Remove trailing slash. |
| 330 | $filename = str_replace('http://', '', $site_url); // Remove http://. |
| 331 | $filename = str_replace(array('/', '\\'), '-', $filename); // Replace slashes with - . |
| 332 | |
| 333 | if ($single_sidebar) { |
| 334 | $filename .= '-' . $single_sidebar; |
| 335 | } |
| 336 | |
| 337 | $filename .= '-widgets.json'; // Append. |
| 338 | $filename = apply_filters('widgetopts_exported_file', $filename); |
| 339 | |
| 340 | |
| 341 | $file_contents = $this->generate_export_data($inactive, $single_sidebar); |
| 342 | $filesize = strlen($file_contents); |
| 343 | |
| 344 | // Headers to prompt "Save As". |
| 345 | header('Content-Type: application/octet-stream'); |
| 346 | header('Content-Disposition: attachment; filename=' . $filename); |
| 347 | header('Expires: 0'); |
| 348 | header('Cache-Control: must-revalidate'); |
| 349 | header('Pragma: public'); |
| 350 | header('Content-Length: ' . $filesize); |
| 351 | |
| 352 | // Clear buffering just in case. |
| 353 | // @codingStandardsIgnoreLine |
| 354 | @ob_end_clean(); |
| 355 | flush(); |
| 356 | |
| 357 | // Output file contents. |
| 358 | echo $file_contents; |
| 359 | |
| 360 | // Stop execution. |
| 361 | exit; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Import .json file with widgets data |
| 367 | */ |
| 368 | function import_json_file() |
| 369 | { |
| 370 | // Export requested. |
| 371 | if (!empty($_POST) && isset($_POST['action']) && 'import' == sanitize_text_field($_POST['action']) && check_admin_referer('widgeopts_import', 'widgeopts_nonce_import')) { |
| 372 | |
| 373 | //allow .json upload |
| 374 | add_filter('wp_check_filetype_and_ext', array(&$this, 'disable_real_mime_check'), 10, 4); |
| 375 | add_filter('upload_mimes', array(&$this, 'add_mime_types')); |
| 376 | |
| 377 | $uploaded_file = $_FILES['widgeopts_file']; |
| 378 | $wp_filetype = wp_check_filetype_and_ext($uploaded_file['tmp_name'], $uploaded_file['name'], false); |
| 379 | |
| 380 | if ('json' !== $wp_filetype['ext'] && !wp_match_mime_types('json', $wp_filetype['type'])) { |
| 381 | |
| 382 | wp_die( |
| 383 | wp_kses( |
| 384 | __('Invalid File! Please upload Widget Options exported data.' . $wp_filetype['ext'], 'widget-options'), |
| 385 | array( |
| 386 | 'b' => array(), |
| 387 | ) |
| 388 | ), |
| 389 | '', |
| 390 | array( |
| 391 | 'back_link' => true, |
| 392 | ) |
| 393 | ); |
| 394 | } |
| 395 | |
| 396 | // Check and move file to uploads directory |
| 397 | $file_data = wp_handle_upload($uploaded_file, array( |
| 398 | 'test_form' => false, |
| 399 | )); |
| 400 | |
| 401 | if (isset($file_data['error'])) { |
| 402 | wp_die( |
| 403 | esc_html($file_data['error']), |
| 404 | '', |
| 405 | array( |
| 406 | 'back_link' => true, |
| 407 | ) |
| 408 | ); |
| 409 | } |
| 410 | |
| 411 | // Check if file exists? |
| 412 | if (!file_exists($file_data['file'])) { |
| 413 | wp_die( |
| 414 | esc_html__('Import file could not be found. Please try again.', 'widget-options'), |
| 415 | '', |
| 416 | array( |
| 417 | 'back_link' => true, |
| 418 | ) |
| 419 | ); |
| 420 | } |
| 421 | |
| 422 | // Get file contents and decode. |
| 423 | $data = implode('', file($file_data['file'])); |
| 424 | $data = json_decode($data, true); |
| 425 | |
| 426 | // Delete import file. |
| 427 | unlink($file_data['file']); |
| 428 | |
| 429 | if (!is_array($data) || !isset($data['widgetopts_exported'])) { |
| 430 | wp_die( |
| 431 | esc_html__('Imported file invalid and not generated by Widget Options Plugin.', 'widget-options'), |
| 432 | '', |
| 433 | array( |
| 434 | 'back_link' => true, |
| 435 | ) |
| 436 | ); |
| 437 | } |
| 438 | |
| 439 | //assign to variable to be able to used on frontend |
| 440 | $this->imported = $this->organized_imported_data($data); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * |
| 446 | * Parse imported data and compares to existing widgets |
| 447 | * |
| 448 | */ |
| 449 | function organized_imported_data($data) |
| 450 | { |
| 451 | global $wp_registered_sidebars, $wp_registered_widgets; |
| 452 | |
| 453 | //variables |
| 454 | $checked = array(); |
| 455 | $inactive = false; |
| 456 | $supported_widgets = $this->supported_widgets(); |
| 457 | |
| 458 | //Add Inactive Widgets |
| 459 | $wpregistered_sidebars = $wp_registered_sidebars; |
| 460 | $wpregistered_sidebars['wp_inactive_widgets'] = array('name' => __('Inactive Sidebars & Widgets', 'widget-options'), 'id' => 'wp_inactive_widgets'); |
| 461 | |
| 462 | // Hook before import. |
| 463 | do_action('widgetopts_before_render_import'); |
| 464 | |
| 465 | //create filter for developers |
| 466 | $data = apply_filters('widgetopts_imported_data', $data); |
| 467 | |
| 468 | if (isset($data['wp_inactive_widgets']) && !empty($data['wp_inactive_widgets'])) { |
| 469 | $inactive = true; |
| 470 | } |
| 471 | |
| 472 | $widget_instances = json_decode($this->generate_export_data($inactive), true); |
| 473 | |
| 474 | //remove placeholder |
| 475 | unset($data['widgetopts_exported']); |
| 476 | |
| 477 | // Begin results. |
| 478 | $results = array(); |
| 479 | |
| 480 | // Loop imported data's sidebars. |
| 481 | foreach ($data as $sidebar => $widgets) { |
| 482 | |
| 483 | // Check if sidebar is available on this site. |
| 484 | if (isset($wpregistered_sidebars[$sidebar])) { |
| 485 | $is_sidebar_available = true; |
| 486 | $sidebar_id = $sidebar; |
| 487 | $sidebar_message_type = 'yes'; |
| 488 | $sidebar_message = ''; |
| 489 | } else { |
| 490 | //add to inactive if sidebar not available |
| 491 | $is_sidebar_available = false; |
| 492 | $sidebar_id = 'wp_inactive_widgets'; // Add to inactive if sidebar does not exist in theme. |
| 493 | $sidebar_message_type = 'error'; |
| 494 | $sidebar_message = esc_html__('Widget area does not exist in theme (using Inactive)', 'widget-options'); |
| 495 | } |
| 496 | |
| 497 | // Result for sidebar |
| 498 | // Sidebar name if theme supports it; otherwise ID. |
| 499 | $results[$sidebar]['name'] = !empty($wpregistered_sidebars[$sidebar]['name']) ? $wpregistered_sidebars[$sidebar]['name'] : $sidebar; |
| 500 | $results[$sidebar]['message_type'] = $sidebar_message_type; |
| 501 | $results[$sidebar]['message'] = $sidebar_message; |
| 502 | $results[$sidebar]['widgets'] = array(); |
| 503 | $results['sidebars'][$sidebar] = $results[$sidebar]['name']; |
| 504 | |
| 505 | //Loop assigned widgets |
| 506 | foreach ($widgets as $widget_id => $widget) { |
| 507 | $fail = false; |
| 508 | |
| 509 | // Get id_base (remove -# from end) and instance ID number. |
| 510 | $id_base = preg_replace('/-[0-9]+$/', '', $widget_id); |
| 511 | $instance_id_number = str_replace($id_base . '-', '', $widget_id); |
| 512 | |
| 513 | // Does site support this widget? |
| 514 | if (!$fail && !isset($supported_widgets[$id_base])) { |
| 515 | $fail = true; |
| 516 | $widget_message_type = 'error'; |
| 517 | $widget_message = esc_html__('Widget Type not available.', 'widget-options'); // Explain why widget not imported. |
| 518 | } |
| 519 | |
| 520 | //check if same widget already available |
| 521 | if (!$fail && isset($widget_instances[$sidebar][$widget_id])) { |
| 522 | //compare if same sidebar and widget id have same contents too |
| 523 | if ($widget === $widget_instances[$sidebar][$widget_id]) { |
| 524 | $fail = true; |
| 525 | $widget_message_type = 'no-alt'; |
| 526 | $widget_message = esc_html__('Widget already exists', 'widget-options'); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | //double check if values exists with different widget keys |
| 531 | if (!$fail && isset($widget_instances[$sidebar]) && is_array($widget_instances[$sidebar]) && in_array($widget, $widget_instances[$sidebar])) { |
| 532 | $in_array_key = array_search($widget, $widget_instances[$sidebar]); |
| 533 | $in_array_id_base = preg_replace('/-[0-9]+$/', '', $in_array_key); |
| 534 | |
| 535 | if ($id_base == $in_array_id_base) { |
| 536 | $fail = true; |
| 537 | $widget_message_type = 'no-alt'; |
| 538 | $widget_message = esc_html__('Widget already exists', 'widget-options'); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | // No failure. |
| 543 | if (!$fail) { |
| 544 | $get_widget_instances = get_option('widget_' . $id_base); |
| 545 | $get_widget_instances = !empty($get_widget_instances) ? $get_widget_instances : array( |
| 546 | '_multiwidget' => 1, |
| 547 | ); |
| 548 | |
| 549 | // add widget values to instances - to update later |
| 550 | $get_widget_instances[] = $widget; |
| 551 | |
| 552 | // Get the given key |
| 553 | end($get_widget_instances); |
| 554 | $new_instance_number = key($get_widget_instances); |
| 555 | |
| 556 | // Change number to 1 when it's 0 to avoid issues |
| 557 | if ('0' === strval($new_instance_number)) { |
| 558 | $new_instance_number = 1; |
| 559 | $get_widget_instances[$new_instance_number] = $get_widget_instances[0]; |
| 560 | unset($get_widget_instances[0]); |
| 561 | } |
| 562 | |
| 563 | // Change widget options key to the given id_base and number to work perfectly |
| 564 | if (isset($get_widget_instances[$new_instance_number]) && isset($get_widget_instances[$new_instance_number]['extended_widget_opts'])) { |
| 565 | $get_widget_instances[$new_instance_number]['extended_widget_opts-' . $id_base . '-' . $new_instance_number] = $get_widget_instances[$new_instance_number]['extended_widget_opts']; |
| 566 | unset($get_widget_instances[$new_instance_number]['extended_widget_opts']); |
| 567 | } |
| 568 | |
| 569 | // Move _multiwidget to the end |
| 570 | if (isset($get_widget_instances['_multiwidget'])) { |
| 571 | $multiwidget = $get_widget_instances['_multiwidget']; |
| 572 | unset($get_widget_instances['_multiwidget']); |
| 573 | $get_widget_instances['_multiwidget'] = $multiwidget; |
| 574 | } |
| 575 | |
| 576 | // Update option with new widget. |
| 577 | update_option('widget_' . $id_base, $get_widget_instances); |
| 578 | |
| 579 | // Get sidebar widgets |
| 580 | $sidebars_widgets = get_option('sidebars_widgets'); |
| 581 | |
| 582 | // Avoid error when empty |
| 583 | if (!$sidebars_widgets) { |
| 584 | $sidebars_widgets = array(); |
| 585 | } |
| 586 | |
| 587 | // Use ID number from new widget instance. |
| 588 | $new_instance_id = $id_base . '-' . $new_instance_number; |
| 589 | |
| 590 | // Add new instance to sidebar. |
| 591 | $sidebars_widgets[$sidebar][] = $new_instance_id; |
| 592 | |
| 593 | // Save new sidebar widgets options |
| 594 | update_option('sidebars_widgets', $sidebars_widgets); |
| 595 | |
| 596 | // Success message. |
| 597 | if ($is_sidebar_available) { |
| 598 | $widget_message_type = 'yes'; |
| 599 | $widget_message = esc_html__('Successfully Imported', 'widget-options'); |
| 600 | } else { |
| 601 | $widget_message_type = 'warning'; |
| 602 | $widget_message = esc_html__('Sidebar Widget Area Not Available', 'widget-options'); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | // Result for widget instance |
| 607 | $results[$sidebar_id]['widgets'][$widget_id]['name'] = isset($available_widgets[$id_base]['name']) ? $available_widgets[$id_base]['name'] : $id_base; |
| 608 | $results[$sidebar_id]['widgets'][$widget_id]['title'] = !empty($widget['title']) ? $widget['title'] : esc_html__('No Title', 'widget-options'); |
| 609 | $results[$sidebar_id]['widgets'][$widget_id]['message_type'] = $widget_message_type; |
| 610 | $results[$sidebar_id]['widgets'][$widget_id]['message'] = $widget_message; |
| 611 | } |
| 612 | } //endforeach |
| 613 | |
| 614 | // Hook after import. |
| 615 | do_action('widgetopts_after_render_import'); |
| 616 | |
| 617 | // Return results. |
| 618 | return $this->imported = apply_filters('widgetopts_imported_results', $results); |
| 619 | } |
| 620 | |
| 621 | function get_widget_instances() |
| 622 | { |
| 623 | global $wp_registered_widgets; |
| 624 | $checked = array(); |
| 625 | |
| 626 | // Store all widgets in array |
| 627 | $widget_instances = array(); |
| 628 | |
| 629 | foreach ($wp_registered_widgets as $widget) { |
| 630 | $id_base = is_array($widget['callback']) ? $widget['callback'][0]->id_base : ''; |
| 631 | $opts = array(); |
| 632 | if (!empty($id_base)) { |
| 633 | $instance = get_option('widget_' . $id_base); |
| 634 | } |
| 635 | |
| 636 | $number = $widget['params'][0]['number']; |
| 637 | if (!isset($instance[$number])) { |
| 638 | continue; |
| 639 | } |
| 640 | |
| 641 | $widget_id = $id_base . '-' . $number; |
| 642 | |
| 643 | //bypass if widget id already checked |
| 644 | if (isset($checked[$widget_id])) { |
| 645 | continue; |
| 646 | } |
| 647 | $checked[$widget_id] = '1'; |
| 648 | $k = 'extended_widget_opts-' . $widget_id; |
| 649 | if (isset($instance[$number][$k])) { |
| 650 | |
| 651 | //unset id_base |
| 652 | if (isset($instance[$number][$k]['id_base'])) { |
| 653 | unset($instance[$number][$k]['id_base']); |
| 654 | } |
| 655 | |
| 656 | $instance[$number]['extended_widget_opts'] = $instance[$number][$k]; |
| 657 | unset($instance[$number][$k]); |
| 658 | } |
| 659 | $widget_instances[$widget_id] = $instance[$number]; |
| 660 | } |
| 661 | |
| 662 | return apply_filters('widgetopts_widget_instances', $widget_instances); |
| 663 | } |
| 664 | |
| 665 | function supported_widgets() |
| 666 | { |
| 667 | |
| 668 | global $wp_registered_widget_controls; |
| 669 | |
| 670 | $widget_controls = $wp_registered_widget_controls; |
| 671 | |
| 672 | $available_widgets = array(); |
| 673 | |
| 674 | foreach ($widget_controls as $widget) { |
| 675 | |
| 676 | // No duplicates. |
| 677 | if (!empty($widget['id_base']) && !isset($available_widgets[$widget['id_base']])) { |
| 678 | $available_widgets[$widget['id_base']]['id_base'] = $widget['id_base']; |
| 679 | $available_widgets[$widget['id_base']]['name'] = $widget['name']; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return apply_filters('widgetopts_supported_widgets', $available_widgets); |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Add mime type for upload |
| 688 | * |
| 689 | * Make sure the WordPress install will accept .json uploads. |
| 690 | * |
| 691 | */ |
| 692 | function add_mime_types($mime_types) |
| 693 | { |
| 694 | |
| 695 | $mime_types['json'] = 'application/json'; |
| 696 | |
| 697 | return $mime_types; |
| 698 | } |
| 699 | |
| 700 | function disable_real_mime_check($data, $file, $filename, $mimes) |
| 701 | { |
| 702 | $wp_version = get_bloginfo('version'); |
| 703 | |
| 704 | if (version_compare($wp_version, '4.7', '<=')) { |
| 705 | return $data; |
| 706 | } |
| 707 | $wp_filetype = wp_check_filetype($filename, $mimes); |
| 708 | $ext = $wp_filetype['ext']; |
| 709 | $type = $wp_filetype['type']; |
| 710 | $proper_filename = $data['proper_filename']; |
| 711 | return compact('ext', 'type', 'proper_filename'); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | new WP_Widget_Options_Importer(); |
| 716 | |
| 717 | endif; |
| 718 |