Нет описания

Registry.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Implements a batch process registry class.
  10. *
  11. * @since 1.7.0
  12. *
  13. * @see PUM_Abstract_Registry
  14. */
  15. class PUM_Batch_Process_Registry extends PUM_Abstract_Registry {
  16. /**
  17. * @var PUM_Batch_Process_Registry
  18. */
  19. public static $instance;
  20. /**
  21. * @return PUM_Batch_Process_Registry
  22. */
  23. public static function instance() {
  24. if ( ! isset( self::$instance ) ) {
  25. self::$instance = new self();
  26. self::$instance->init();
  27. }
  28. return self::$instance;
  29. }
  30. /**
  31. * Initializes the batch registry.
  32. */
  33. public function init() {
  34. $this->register_core_processes();
  35. /**
  36. * Fires during instantiation of the batch processing registry.
  37. *
  38. * @param PUM_Batch_Process_Registry $this Registry instance.
  39. */
  40. do_action( 'pum_batch_process_init', $this );
  41. }
  42. /**
  43. * Registers core batch processes.
  44. */
  45. protected function register_core_processes() {
  46. }
  47. /**
  48. * Registers a new batch process.
  49. *
  50. * @param string $batch_id Unique batch process ID.
  51. * @param array $process_args {
  52. * Arguments for registering a new batch process.
  53. *
  54. * @type string $class Batch processor class to use.
  55. * @type string $file File containing the batch processor class.
  56. * }
  57. *
  58. * @return WP_Error|true True on successful registration, otherwise a WP_Error object.
  59. */
  60. public function register_process( $batch_id, $process_args ) {
  61. $process_args = wp_parse_args( $process_args, array_fill_keys( array( 'class', 'file' ), '' ) );
  62. if ( empty( $process_args['class'] ) ) {
  63. return new WP_Error( 'invalid_batch_class', __( 'A batch process class must be specified.', 'popup-maker' ) );
  64. }
  65. if ( empty( $process_args['file'] ) ) {
  66. return new WP_Error( 'missing_batch_class_file', __( 'No batch class handler file has been supplied.', 'popup-maker' ) );
  67. }
  68. // 2 if Windows path.
  69. if ( ! in_array( validate_file( $process_args['file'] ), array( 0, 2 ), true ) ) {
  70. return new WP_Error( 'invalid_batch_class_file', __( 'An invalid batch class handler file has been supplied.', 'popup-maker' ) );
  71. }
  72. return $this->add_item( $batch_id, $process_args );
  73. }
  74. /**
  75. * Removes a batch process from the registry by ID.
  76. *
  77. * @param string $batch_id Batch process ID.
  78. */
  79. public function remove_process( $batch_id ) {
  80. $this->remove_item( $batch_id );
  81. }
  82. }