PluginProbe
Boxzilla – WordPress Popup Builder / 3.1.15
Boxzilla – WordPress Popup Builder v3.1.15
3.4.11 3.4.10 3.4.9 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 trunk 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 All 72 releases
boxzilla / src / class-bootstrapper.php

class-bootstrapper.php in Boxzilla – WordPress Popup Builder 3.1.15, at src/class-bootstrapper.php

103 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Boxzilla;
4
5 use InvalidArgumentException;
6
7 /**
8 * Class Bootstrapper
9 * @package Boxzilla
10 *
11 * @method void admin( callable $callback )
12 * @method void cron( callable $callback )
13 * @method void front( callable $callback )
14 * @method void ajax( callable $callback )
15 * @method void cli( callable $callback )
16 */
17 class Bootstrapper {
18
19 /**
20 * @var array
21 */
22 private $bootstrappers = array(
23 'admin' => array(),
24 'ajax' => array(),
25 'cli' => array(),
26 'cron' => array(),
27 'front' => array(),
28 'global' => array(),
29 );
30
31 /**
32 * @param string $section
33 * @param callable $callable
34 */
35 public function register( $section, $callable ) {
36
37 if( ! isset( $this->bootstrappers[ $section ] ) ) {
38 throw new InvalidArgumentException( "Section $section is invalid." );
39 }
40
41 if( ! is_callable( $callable ) ) {
42 throw new InvalidArgumentException( 'Callable argument is not callable.' );
43 }
44
45 $this->bootstrappers[ $section ][] = $callable;
46 }
47
48 /**
49 * @param string $name
50 * @param array $arguments
51 */
52 public function __call( $name, $arguments ) {
53 if( isset( $this->bootstrappers[ $name ] ) ) {
54 $this->register( $name, $arguments[0] );
55 }
56 }
57
58 /**
59 * Run registered bootstrappers
60 *
61 * @param string $section
62 */
63 public function run( $section = '' ) {
64
65 if( ! $section ) {
66 $section = $this->section();
67 }
68
69 // call all global callbacks
70 foreach( $this->bootstrappers['global'] as $callback ) {
71 $callback();
72 }
73
74 // call section specific callbacks
75 foreach( $this->bootstrappers[ $section ] as $callback ) {
76 $callback();
77 }
78 }
79
80 /**
81 * Get currently active section.
82 *
83 * @return string
84 */
85 public function section() {
86 if( is_admin() ) {
87 if( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
88 return 'ajax';
89 } else {
90 return 'admin';
91 }
92 } else {
93 if( defined( 'DOING_CRON' ) && DOING_CRON ) {
94 return 'cron';
95 } else if( defined( 'WP_CLI' ) && WP_CLI ) {
96 return 'cli';
97 } else {
98 return 'front';
99 }
100 }
101 }
102
103 }