importer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * VamTam Google Maps Easy 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_GMP_Easy_Import extends WP_Importer {
  21. private $file;
  22. public function __construct() {
  23. $this->file = VAMTAM_SAMPLES_DIR . 'gmp-easy.csv';
  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-gmp-easy' );
  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. $_GET['type'] = 'maps';
  45. $_FILES['csv_import_file_maps'] = array(
  46. 'name' => basename( $this->file ),
  47. 'tmp_name' => $this->file,
  48. 'type' => 'application/csv',
  49. 'error' => 0,
  50. 'size' => filesize( $this->file ),
  51. );
  52. \frameGmp::_()->getModule( 'csv' )->getController()->import();
  53. wp_suspend_cache_invalidation( false );
  54. $this->import_end();
  55. }
  56. private function import_start() {
  57. if ( ! file_exists( $this->file ) ) {
  58. echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
  59. echo esc_html__( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
  60. $this->footer();
  61. die();
  62. }
  63. do_action( 'import_start' );
  64. }
  65. /**
  66. * Performs post-import cleanup of files and the cache
  67. */
  68. private function import_end() {
  69. $redirect = admin_url( '' );
  70. echo '<p>' . esc_html__( 'All done.', 'wordpress-importer' ) . ' <a href="' . esc_url( $redirect ) . '">' . esc_html__( 'Have fun!', 'wordpress-importer' ) . '</a></p>';
  71. echo '<!-- all done -->';
  72. do_action( 'import_end' );
  73. }
  74. // Display import page title
  75. private function header() {
  76. echo '<div class="wrap">';
  77. echo '<h2>' . esc_html__( 'Import Google Maps Easy', 'wordpress-importer' ) . '</h2>'; }
  78. // Close div.wrap
  79. private function footer() {
  80. echo '</div>';
  81. }
  82. /**
  83. * Added to http_request_timeout filter to force timeout at 120 seconds during import
  84. * @return int 120
  85. */
  86. public function bump_request_timeout( $imp ) {
  87. return 120;
  88. }
  89. }
  90. } // class_exists( 'WP_Importer' )
  91. function vamtam_gmp_easy_importer_init() {
  92. $GLOBALS['vamtam_gmp_easy_import'] = new VAMTAM_GMP_Easy_Import();
  93. register_importer( 'vamtam_gmp_easy', 'Vamtam Google Maps Easy Importer', sprintf( esc_html__( 'Import Google Maps Easy bundled with VamTam themes, not to be used as a stand-alone product.', 'wpv' ), VAMTAM_THEME_NAME ), array( $GLOBALS['vamtam_gmp_easy_import'], 'dispatch' ) );
  94. }
  95. add_action( 'admin_init', 'vamtam_gmp_easy_importer_init' );