/wp-content/plugins/ninja-forms/deprecated/includes/admin/upgrades/class-upgrade-handler.php

https://gitlab.com/lamovible/grand-regis · PHP · 162 lines · 113 code · 43 blank · 6 comment · 23 complexity · 36f8d1fa06cf0a30a5f76971141a564d MD5 · raw file

  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. if( ! function_exists( 'nf_is_func_disabled' ) ) {
  3. function nf_is_func_disabled($function)
  4. {
  5. $disabled = explode(',', ini_get('disable_functions'));
  6. return in_array($function, $disabled);
  7. }
  8. }
  9. require_once( NF_PLUGIN_DIR . 'includes/admin/upgrades/database-migrations.php' );
  10. require_once( NF_PLUGIN_DIR . 'includes/admin/upgrades/convert-forms.php' );
  11. require_once( NF_PLUGIN_DIR . 'includes/admin/upgrades/convert-notifications.php' );
  12. require_once( NF_PLUGIN_DIR . 'includes/admin/upgrades/convert-subs.php' );
  13. require_once( NF_PLUGIN_DIR . 'includes/admin/upgrades/update-email-settings.php' );
  14. /**
  15. * Class NF_Upgrade_Handler
  16. */
  17. class NF_UpgradeHandler
  18. {
  19. static $instance;
  20. public $upgrades;
  21. private $page;
  22. public static function instance()
  23. {
  24. if ( ! isset( self::$instance ) ) {
  25. self::$instance = new NF_UpgradeHandler();
  26. }
  27. return self::$instance;
  28. }
  29. public function __construct()
  30. {
  31. if ( function_exists( 'ignore_user_abort' ) && ! nf_is_func_disabled( 'ignore_user_abort' ) ) {
  32. ignore_user_abort( true );
  33. }
  34. $this->register_upgrades();
  35. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  36. add_action( 'wp_ajax_nf_upgrade_handler', array( $this, 'ajax_response' ) );
  37. return;
  38. } else {
  39. $this->page = new NF_UpgradeHandlerPage();
  40. }
  41. }
  42. public function register_upgrades()
  43. {
  44. $this->upgrades[] = new NF_Upgrade_Database_Migrations();
  45. $this->upgrades[] = new NF_Upgrade_Forms();
  46. $this->upgrades[] = new NF_Upgrade_Notifications();
  47. $this->upgrades[] = new NF_Upgrade_Submissions();
  48. $this->upgrades[] = new NF_Upgrade_Email_Settings();
  49. $this->upgrades = apply_filters( 'nf_upgrade_handler_register', $this->upgrades );
  50. usort( $this->upgrades, array( $this, 'compare_upgrade_priority' ) ) ;
  51. }
  52. private function compare_upgrade_priority( $a, $b )
  53. {
  54. return version_compare( $a->priority, $b->priority );
  55. }
  56. public function ajax_response()
  57. {
  58. $current_step = ( isset( $_REQUEST['step'] ) ) ? $_REQUEST['step'] : 0;
  59. $current_upgrade = $this->getUpgradeByName( $_REQUEST['upgrade'] );
  60. $current_upgrade->total_steps = $_REQUEST['total_steps'];
  61. if( isset( $_REQUEST['args'] ) ) {
  62. $current_upgrade->args = $_REQUEST['args'];
  63. }
  64. if( 0 == $current_step ) {
  65. $current_upgrade->loading();
  66. }
  67. $response = array(
  68. 'upgrade' => $current_upgrade->name,
  69. 'total_steps' => (int) $current_upgrade->total_steps,
  70. 'args' => $current_upgrade->args,
  71. );
  72. if( 0 != $current_step ) {
  73. if (is_array($current_upgrade->errors) AND $current_upgrade->errors) {
  74. $response['errors'] = $current_upgrade->errors;
  75. }
  76. if ($current_upgrade->total_steps < $current_step ) {
  77. $current_upgrade->complete();
  78. $response['complete'] = TRUE;
  79. $next_upgrade = $this->getNextUpgrade($current_upgrade);
  80. if ($next_upgrade) {
  81. if( ! $next_upgrade->isComplete() ) {
  82. $response['nextUpgrade'] = $next_upgrade->name;
  83. }
  84. }
  85. } else {
  86. $current_upgrade->_step($current_step);
  87. }
  88. }
  89. $response['step'] = $current_step + 1;
  90. echo json_encode( $response );
  91. die();
  92. }
  93. /*
  94. * UTILITY METHODS
  95. */
  96. public function getUpgradeByName( $name )
  97. {
  98. foreach ( $this->upgrades as $index => $upgrade ) {
  99. if ( $name == $upgrade->name ) {
  100. return $upgrade;
  101. }
  102. }
  103. }
  104. public function getNextUpgrade( $current_upgrade )
  105. {
  106. foreach ( $this->upgrades as $index => $upgrade ) {
  107. if ( $current_upgrade->name == $upgrade->name ) {
  108. if( isset( $this->upgrades[ $index + 1 ] ) ) {
  109. return $this->upgrades[ $index + 1 ];
  110. }
  111. }
  112. }
  113. return FALSE;
  114. }
  115. }
  116. function NF_UpgradeHandler() {
  117. return NF_UpgradeHandler::instance();
  118. }
  119. NF_UpgradeHandler();