Няма описание

Registry.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Implements a registry for core upgrade routines.
  10. *
  11. * @since 1.7.0
  12. *
  13. * @see PUM_Batch_Process_Registry
  14. */
  15. class PUM_Upgrade_Registry extends PUM_Batch_Process_Registry {
  16. /**
  17. * @var string Currently installed version.
  18. */
  19. public $version;
  20. /**
  21. * @var string Upgraded from version.
  22. */
  23. public $upgraded_from;
  24. /**
  25. * @var string Initially installed version.
  26. */
  27. public $initial_version;
  28. /**
  29. * @var PUM_Upgrade_Registry
  30. */
  31. public static $instance;
  32. /**
  33. * @return PUM_Upgrade_Registry
  34. */
  35. public static function instance() {
  36. if ( ! isset( self::$instance ) ) {
  37. self::$instance = new self();
  38. add_action( 'init', array( self::$instance, 'init' ), -9999 );
  39. }
  40. return self::$instance;
  41. }
  42. /**
  43. * Initializes the upgrade registry.
  44. */
  45. public function init() {
  46. $this->register_upgrades();
  47. /**
  48. * Fires during instantiation of the batch processing registry.
  49. *
  50. * @param PUM_Upgrade_Registry $this PUM_Abstract_Registry instance.
  51. */
  52. do_action( 'pum_upgrade_process_init', $this );
  53. }
  54. /**
  55. * Registers upgrade routines.
  56. *
  57. * @see PUM_Utils_Upgrades::add_routine()
  58. */
  59. private function register_upgrades() {
  60. /**
  61. * Fires during instantiation of the batch processing registry allowing proper registration of upgrades.
  62. *
  63. * @param PUM_Upgrade_Registry $this PUM_Abstract_Registry instance.
  64. */
  65. do_action( 'pum_register_upgrades', $this );
  66. }
  67. /**
  68. * Adds an upgrade to the registry.
  69. *
  70. * @param int $upgrade_id upgrade ID.
  71. * @param array $attributes {
  72. * Upgrade attributes.
  73. *
  74. * @type string $class upgrade handler class.
  75. * @type string $file upgrade handler class file.
  76. * }
  77. *
  78. * @return true Always true.
  79. */
  80. public function add_upgrade( $upgrade_id, $attributes ) {
  81. $attributes = wp_parse_args( $attributes, array(
  82. 'rules' => array(),
  83. 'class' => '',
  84. 'file' => '',
  85. ) );
  86. // Log an error if it's too late to register the process.
  87. if ( did_action( 'pum_upgrade_process_init' ) ) {
  88. PUM_Utils_Logging::instance()->log( sprintf( 'The %s upgrade process was registered too late. Registrations must occur while/before <code>pum_upgrade_process_init</code> fires.', esc_html( $upgrade_id ) ) );
  89. return false;
  90. }
  91. return $this->register_process( $upgrade_id, $attributes );
  92. }
  93. /**
  94. * Removes an upgrade from the registry by ID.
  95. *
  96. * @param string $upgrade_id upgrade ID.
  97. */
  98. public function remove_upgrade( $upgrade_id ) {
  99. parent::remove_process( $upgrade_id );
  100. }
  101. /**
  102. * Retrieves registered upgrades.
  103. *
  104. * @return array The list of registered upgrades.
  105. */
  106. public function get_upgrades() {
  107. return parent::get_items();
  108. }
  109. }