importer.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * VamTam Revslider Importer
  4. */
  5. if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
  6. return;
  7. // Load Importer API
  8. require_once ABSPATH . 'wp-admin/includes/import.php';
  9. if ( ! class_exists( 'WP_Importer' ) ) {
  10. $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
  11. if ( file_exists( $class_wp_importer ) )
  12. require $class_wp_importer;
  13. }
  14. /**
  15. * WordPress Importer class for managing the import process of a WXR file
  16. *
  17. * @package Importer
  18. */
  19. if ( class_exists( 'WP_Importer' ) ) {
  20. class Vamtam_RevSlider_Import extends WP_Importer {
  21. private $dir;
  22. public function __construct() {
  23. $this->dir = VAMTAM_SAMPLES_DIR . 'revslider';
  24. }
  25. /**
  26. * Registered callback function for the WordPress Importer
  27. *
  28. * Manages the three separate stages of the WXR import process
  29. */
  30. public function dispatch() {
  31. $this->header();
  32. check_admin_referer( 'vamtam-import-revslider' );
  33. set_time_limit( 0 );
  34. $this->import( );
  35. $this->footer();
  36. }
  37. /**
  38. * The main controller for the actual import stage.
  39. */
  40. public function import() {
  41. add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
  42. $this->import_start();
  43. wp_suspend_cache_invalidation( true );
  44. $this->import_sliders();
  45. wp_suspend_cache_invalidation( false );
  46. $this->import_end();
  47. }
  48. private function import_sliders() {
  49. $dir = opendir( $this->dir );
  50. ob_start();
  51. while ( $file = readdir( $dir ) ) {
  52. if ( $file != '.' && $file != '..' && preg_match( '/\.zip$/', $file ) ) {
  53. $origpath = $this->dir . '/' . $file;
  54. if ( ! isset( $_FILES['import_file'] ) ) {
  55. $_FILES['import_file'] = array(
  56. 'name' => $file,
  57. 'type' => 'application/zip',
  58. 'error' => 0,
  59. 'size' => filesize( $origpath ),
  60. );
  61. }
  62. $temp = tmpfile();
  63. fwrite( $temp, file_get_contents( $origpath ) );
  64. $meta_data = stream_get_meta_data( $temp );
  65. $filepath = $meta_data['uri'];
  66. $_FILES['import_file']['tmp_name'] = $filepath;
  67. if ( ! defined( 'FS_CHMOD_DIR' ) ) {
  68. define( 'FS_CHMOD_DIR', (0755 & ~ umask()) );
  69. }
  70. $slider = new RevSlider();
  71. $response = $slider->importSliderFromPost( 'true', 'none', false, false, false, 'true' );
  72. if ( WP_DEBUG ) {
  73. echo '<strong>WP_DEBUG enabled</strong><br><br>';
  74. echo 'Import status:<br>';
  75. }
  76. fclose( $temp );
  77. }
  78. }
  79. if ( WP_DEBUG ) {
  80. echo ob_get_clean(); // xss ok
  81. } else {
  82. ob_end_clean();
  83. }
  84. }
  85. private function import_start() {
  86. if ( ! is_dir( $this->dir ) ) {
  87. echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
  88. echo esc_html__( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
  89. $this->footer();
  90. die();
  91. }
  92. do_action( 'import_start' );
  93. }
  94. /**
  95. * Performs post-import cleanup of files and the cache
  96. */
  97. private function import_end() {
  98. $redirect = admin_url( '' );
  99. echo '<p>' . esc_html__( 'All done.', 'wordpress-importer' ) . ' <a href="' . esc_url( $redirect ) . '">' . esc_html__( 'Have fun!', 'wordpress-importer' ) . '</a></p>';
  100. echo '<!-- all done -->';
  101. do_action( 'import_end' );
  102. }
  103. // Display import page title
  104. private function header() {
  105. echo '<div class="wrap">';
  106. echo '<h2>' . esc_html__( 'Import Slider Revolution', 'wordpress-importer' ) . '</h2>'; }
  107. // Close div.wrap
  108. private function footer() {
  109. echo '</div>';
  110. }
  111. /**
  112. * Added to http_request_timeout filter to force timeout at 120 seconds during import
  113. * @return int 120
  114. */
  115. public function bump_request_timeout( $imp ) {
  116. return 120;
  117. }
  118. }
  119. } // class_exists( 'WP_Importer' )
  120. function vamtam_revslider_importer_init() {
  121. $GLOBALS['vamtam_revslider_import'] = new Vamtam_RevSlider_Import();
  122. register_importer( 'vamtam_revslider', 'Vamtam Slider Revolution Import', sprintf( esc_html__( 'Import Slider Revolution sliders from Vamtam themes, not to be used as a stand-alone product.', 'wpv' ), VAMTAM_THEME_NAME ), array( $GLOBALS['vamtam_revslider_import'], 'dispatch' ) );
  123. }
  124. add_action( 'admin_init', 'vamtam_revslider_importer_init' );