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