importer.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * VamTam Ninja Forms 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_NinjaForms_Import extends WP_Importer {
  21. private $dir;
  22. public function __construct() {
  23. $this->dir = VAMTAM_SAMPLES_DIR . 'ninja-forms';
  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-ninja-forms' );
  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_forms();
  45. wp_suspend_cache_invalidation( false );
  46. $this->import_end();
  47. }
  48. private function import_forms() {
  49. $dir = opendir( $this->dir );
  50. while ( $file = readdir( $dir ) ) {
  51. if ( $file != '.' && $file != '..' && preg_match( '/\.nff$/', $file ) ) {
  52. $origpath = $this->dir . '/' . $file;
  53. if ( WP_DEBUG ) {
  54. echo wp_kses_post( sprintf( __( 'Importing %s <br>', 'wpv' ), $origpath ) );
  55. }
  56. Ninja_Forms()->form()->import_form( file_get_contents( $origpath ), true, (int) pathinfo( $file, PATHINFO_FILENAME ) );
  57. }
  58. }
  59. }
  60. private function import_start() {
  61. if ( ! is_dir( $this->dir ) ) {
  62. echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
  63. echo esc_html__( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
  64. $this->footer();
  65. die();
  66. }
  67. do_action( 'import_start' );
  68. }
  69. /**
  70. * Performs post-import cleanup of files and the cache
  71. */
  72. private function import_end() {
  73. $redirect = admin_url( '' );
  74. echo '<p>' . esc_html__( 'All done.', 'wordpress-importer' ) . ' <a href="' . esc_url( $redirect ) . '">' . esc_html__( 'Have fun!', 'wordpress-importer' ) . '</a></p>';
  75. echo '<!-- all done -->';
  76. do_action( 'import_end' );
  77. }
  78. // Display import page title
  79. private function header() {
  80. echo '<div class="wrap">';
  81. echo '<h2>' . esc_html__( 'Import Ninja Forms Samples', 'wordpress-importer' ) . '</h2>'; }
  82. // Close div.wrap
  83. private function footer() {
  84. echo '</div>';
  85. }
  86. /**
  87. * Added to http_request_timeout filter to force timeout at 120 seconds during import
  88. * @return int 120
  89. */
  90. public function bump_request_timeout( $imp ) {
  91. return 120;
  92. }
  93. }
  94. } // class_exists( 'WP_Importer' )
  95. function vamtam_ninja_forms_importer_init() {
  96. $GLOBALS['vamtam_ninja_forms_import'] = new Vamtam_NinjaForms_Import();
  97. register_importer( 'vamtam_ninjaforms', 'Vamtam Ninja Forms Importer', sprintf( esc_html__( 'Import Ninja Forms samples bundled with VamTam themes, not to be used as a stand-alone product.', 'wpv' ), VAMTAM_THEME_NAME ), array( $GLOBALS['vamtam_ninja_forms_import'], 'dispatch' ) );
  98. }
  99. add_action( 'admin_init', 'vamtam_ninja_forms_importer_init' );