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