importer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * VamTam Booked 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_Booked_Import extends WP_Importer {
  21. private $file;
  22. public function __construct() {
  23. $this->file = VAMTAM_SAMPLES_DIR . 'booked-settings.json';
  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-booked' );
  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. $settings = json_decode( file_get_contents( $this->file ), true );
  45. foreach ( $settings as $opt_name => $opt_val ) {
  46. update_option( $opt_name, $opt_val );
  47. }
  48. wp_suspend_cache_invalidation( false );
  49. $this->import_end();
  50. }
  51. protected function import_start() {
  52. if ( ! file_exists( $this->file ) ) {
  53. echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
  54. echo esc_html__( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
  55. $this->footer();
  56. die();
  57. }
  58. do_action( 'import_start' );
  59. }
  60. /**
  61. * Performs post-import cleanup of files and the cache
  62. */
  63. protected function import_end() {
  64. $redirect = admin_url( '' );
  65. echo '<p>' . esc_html__( 'All done.', 'wordpress-importer' ) . ' <a href="' . esc_url( $redirect ) . '">' . esc_html__( 'Have fun!', 'wordpress-importer' ) . '</a></p>';
  66. echo '<!-- all done -->';
  67. do_action( 'import_end' );
  68. }
  69. // Display import page title
  70. protected function header() {
  71. echo '<div class="wrap">';
  72. echo '<h2>' . esc_html__( 'Import Booked Settings', 'wordpress-importer' ) . '</h2>'; }
  73. // Close div.wrap
  74. protected function footer() {
  75. echo '</div>';
  76. }
  77. /**
  78. * Added to http_request_timeout filter to force timeout at 120 seconds during import
  79. * @return int 120
  80. */
  81. public function bump_request_timeout( $imp ) {
  82. return 120;
  83. }
  84. }
  85. } // class_exists( 'WP_Importer' )
  86. function vamtam_booked_importer_init() {
  87. $GLOBALS['vamtam_booked_import'] = new Vamtam_Booked_Import();
  88. register_importer( 'vamtam_booked', 'Vamtam Booked Importer', sprintf( esc_html__( 'Import Booked settings, for use with the demo content provided with VamTam themes, not to be used as a stand-alone product.', 'wpv' ), VAMTAM_THEME_NAME ), array( $GLOBALS['vamtam_booked_import'], 'dispatch' ) );
  89. }
  90. add_action( 'admin_init', 'vamtam_booked_importer_init' );