class-upgrade.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Upgrade
  4. *
  5. * The Upgrade class should be extended by all upgrades to be used by the Upgrade Handler.
  6. */
  7. abstract class NF_Upgrade
  8. {
  9. /**
  10. * @var name
  11. *
  12. * The name is the unique identifier for the upgrade.
  13. */
  14. public $name;
  15. /**
  16. * @var priority
  17. *
  18. * The priority determines the oder in which the upgrades are run.
  19. * Priorities are compared as version numbers that corresponds to when they were introduced.
  20. */
  21. public $priority;
  22. /**
  23. * @var decription
  24. *
  25. * The description will be displayed for the user in the Upgrade Handler admin screen.
  26. */
  27. public $description;
  28. /**
  29. * @var total_steps
  30. *
  31. * The total number of steps that need to be processed.
  32. */
  33. public $total_steps;
  34. /**
  35. * @var args
  36. *
  37. * The args variable is passes between calls.
  38. */
  39. public $args = array();
  40. /**
  41. * @var errors
  42. *
  43. * The errors property is used to store errors for the Upgrade Handler to reference.
  44. */
  45. public $errors = array();
  46. /**
  47. * Constructor
  48. */
  49. public function __construct()
  50. {
  51. $this->nice_name = ucwords( str_replace( '_', ' ', $this->name) );
  52. $this->description = __( $this->description, 'ninja-forms' );
  53. }
  54. /**
  55. * Loading
  56. *
  57. * The loading method is used to setup the upgrade and is called by the Upgrade Handler.
  58. */
  59. abstract function loading();
  60. /**
  61. * Step
  62. *
  63. * @param $step
  64. *
  65. * The step method will be called by the parent _step method.
  66. */
  67. abstract public function step( $step );
  68. /**
  69. * Complete
  70. *
  71. * The complete method will be called by the Upgrade Handler when all steps are complete.
  72. */
  73. abstract public function complete();
  74. /**
  75. * Is Complete
  76. *
  77. * The isComplete method checks to see if the upgrade has already been completed.
  78. */
  79. abstract public function isComplete();
  80. /**
  81. * _Step
  82. *
  83. * @param $step
  84. *
  85. * The _step method is called by the Upgrade Handler and is a middleman for step.
  86. */
  87. public function _step( $step )
  88. {
  89. $last_step = $this->getLastStep();
  90. if( $step < $last_step ) {
  91. $step = $last_step;
  92. }
  93. $this->_beforeStep( $step );
  94. $this->step( $step );
  95. $this->_afterStep( $step );
  96. $this->setLastStep( $step );
  97. }
  98. /**
  99. * Before Step
  100. *
  101. * @param $step
  102. *
  103. * The _beforeStep method is called by the _step method before calling the extended step method.
  104. */
  105. public function _beforeStep( $step )
  106. {
  107. // This method is optionally extended and is intentionally left blank.
  108. }
  109. /**
  110. * After Step
  111. *
  112. * @param $step
  113. *
  114. * The _afterStep method is called by the _step method after calling the extended step method.
  115. */
  116. public function _afterStep( $step )
  117. {
  118. // This method is optionally extended and is intentionally left blank.
  119. }
  120. /**
  121. * Get Last Step
  122. *
  123. * Gets the last step processed from the wp_options table.
  124. *
  125. * @return mixed
  126. */
  127. public function getLastStep()
  128. {
  129. return get_option( 'nf_upgrade_' . $this->name . '_last_step', 0 );
  130. }
  131. /**
  132. * Set Last Step
  133. *
  134. * Updates the value in the wp_options table with the last step processed.
  135. *
  136. * @param $step
  137. */
  138. public function setLastStep( $step )
  139. {
  140. update_option( 'nf_upgrade_' . $this->name . '_last_step', $step );
  141. }
  142. }