importer.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * VamTam Widget Importer
  4. */
  5. if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
  6. return;
  7. /** Display verbose errors */
  8. if ( ! defined( 'IMPORT_DEBUG' ) ) {
  9. define( 'IMPORT_DEBUG', false );
  10. }
  11. // Load Importer API
  12. require_once ABSPATH . 'wp-admin/includes/import.php';
  13. if ( ! class_exists( 'WP_Importer' ) ) {
  14. $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
  15. if ( file_exists( $class_wp_importer ) )
  16. require $class_wp_importer;
  17. }
  18. /**
  19. * WordPress Importer class for managing the import process of a WXR file
  20. *
  21. * @package Importer
  22. */
  23. if ( class_exists( 'WP_Importer' ) ) {
  24. class Vamtam_Widget_Import extends WP_Importer {
  25. public function __construct() {
  26. /* nothing */ }
  27. /**
  28. * Registered callback function for the WordPress Importer
  29. *
  30. * Manages the three separate stages of the WXR import process
  31. */
  32. public function dispatch() {
  33. $this->header();
  34. check_admin_referer( 'vamtam-import' );
  35. $file = VAMTAM_SAMPLES_DIR . 'sidebars';
  36. set_time_limit( 0 );
  37. $this->import( $file );
  38. $this->footer();
  39. }
  40. /**
  41. * The main controller for the actual import stage.
  42. *
  43. * @param string $file Path to the WXR file for importing
  44. */
  45. private function import( $file ) {
  46. add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
  47. add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
  48. $this->import_start( $file );
  49. wp_suspend_cache_invalidation( true );
  50. $this->import_widgets( $file );
  51. wp_suspend_cache_invalidation( false );
  52. $this->import_end();
  53. }
  54. private function import_widgets( $file ) {
  55. $data = unserialize( base64_decode( file_get_contents( $file ) ) );
  56. $data['positions']['wp_inactive_widgets'] = array();
  57. wp_set_sidebars_widgets( $data['positions'] );
  58. $map = get_option( 'vamtam_last_import_map', array() );
  59. foreach ( $data['widgets'] as $class => $widget ) {
  60. update_option( $class, $this->process_widget_conditions( $class, $widget, $map ) );
  61. }
  62. }
  63. private function process_widget_conditions( $class, $widgets, & $map ) {
  64. if ( ! empty( $map ) ) {
  65. foreach ( $widgets as $id => &$widget ) {
  66. if ( is_array( $widget['conditions'] ) ) {
  67. // key is the widget visibility rule type, value is the key in $map
  68. $major_tr = array(
  69. 'author' => 'authors',
  70. 'page' => 'posts',
  71. 'category' => 'terms',
  72. 'tag' => 'terms',
  73. // 'taxonomy' => 'terms', // unimplemented, yet
  74. );
  75. foreach ( $widget['conditions']['rules'] as $rule_id => &$rule ) {
  76. if ( isset( $major_tr[ $rule['major'] ] ) && is_numeric( $rule[ 'minor' ] ) ) {
  77. $rule['minor'] = $map[ $major_tr[ $rule['major'] ] ][ (int) $rule[ 'minor' ] ];
  78. }
  79. }
  80. }
  81. if ( $class === 'widget_nav_menu' && is_array( $widget ) ) {
  82. $from = (int) $widget['nav_menu'];
  83. if ( isset( $map['terms'][ $from ] ) ) {
  84. $mapped = $map['terms'][ $from ];
  85. echo "will map menu {$from} to {$mapped}<br>";
  86. $widget['nav_menu'] = $mapped;
  87. } else {
  88. echo "no mapped id for nav_menu {$from}<br>";
  89. }
  90. }
  91. }
  92. }
  93. return $widgets;
  94. }
  95. /**
  96. * Parses the WXR file and prepares us for the task of processing parsed data
  97. *
  98. * @param string $file Path to the WXR file for importing
  99. */
  100. private function import_start( $file ) {
  101. if ( ! is_file( $file ) ) {
  102. echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
  103. esc_html_e( 'The file does not exist, please try again.', 'wordpress-importer' );
  104. echo '</p>';
  105. $this->footer();
  106. die();
  107. }
  108. do_action( 'import_start' );
  109. }
  110. /**
  111. * Performs post-import cleanup of files and the cache
  112. */
  113. private function import_end() {
  114. echo '<p>' . esc_html__( 'All done.', 'wordpress-importer' ) . ' <a href="' . esc_url( admin_url() ) . '">' . esc_html__( 'Have fun!', 'wordpress-importer' ) . '</a></p>'; $redirect = admin_url( '' );
  115. do_action( 'import_end' );
  116. }
  117. // Display import page title
  118. private function header() {
  119. echo '<div class="wrap">';
  120. echo '<h2>' . esc_html__( 'Import Vamtam Widgets', 'wordpress-importer' ) . '</h2>'; }
  121. // Close div.wrap
  122. private function footer() {
  123. echo '</div>';
  124. }
  125. /**
  126. * Added to http_request_timeout filter to force timeout at 60 seconds during import
  127. * @return int 60
  128. */
  129. public function bump_request_timeout( $imp ) {
  130. return 60;
  131. }
  132. }
  133. }
  134. function vamtam_widget_importer_init() {
  135. if ( defined( 'VAMTAM_SAMPLES_DIR' ) ) {
  136. $GLOBALS['vamtam_widget_import'] = new Vamtam_Widget_Import();
  137. register_importer( 'vamtam_widgets', 'Vamtam Widget Import', sprintf( esc_html__( 'Import widgets from Vamtam themes, not to be used as a stand-alone product.', 'wpv' ), VAMTAM_THEME_NAME ), array( $GLOBALS['vamtam_widget_import'], 'dispatch' ) );
  138. }
  139. }
  140. add_action( 'admin_init', 'vamtam_widget_importer_init' );