Completed
Push — master ( 82b9ae...b792a1 )
by J.D.
02:52
created

WordPoints_Hooks::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4286
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class for the hooks app.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since 1.0.0
8
 */
9
10
/**
11
 * Hooks app.
12
 *
13
 * The hooks API consists primarily of actions, events, reactors, args, and
14
 * other extensions. Events are "fired" at various reactors when actions occur.
15
 * The args that the event relates to is passed to any extensions, along with
16
 * the list of predefined reactions. The extensions can then analyse the args and
17
 * the reaction specifications to determine whether the reactor should "hit" or
18
 * "miss" the target entity.
19
 *
20
 * @since 1.0.0
21
 *
22
 * @property-read WordPoints_Hook_Router               $router     The hook action router.
23
 * @property-read WordPoints_Hook_Actions              $actions    The actions registry.
24
 * @property-read WordPoints_Hook_Events               $events     The events registry.
25
 * @property-read WordPoints_Class_Registry_Persistent $firers     The firers registry.
26
 * @property-read WordPoints_Class_Registry_Persistent $reactors   The reactors registry.
27
 * @property-read WordPoints_Class_Registry_Persistent $extensions The extensions registry.
28
 * @property-read WordPoints_Class_Registry_Children   $conditions The conditions registry.
29
 */
30
class WordPoints_Hooks extends WordPoints_App {
31
32
	/**
33
	 * Whether network-wide mode is turned on.
34
	 *
35
	 * @since 1.0.0
36
	 *
37
	 * @var bool
38
	 */
39
	protected $network_mode = null;
40
41
	/**
42
	 * Register the sub apps when the app is constructed.
43
	 *
44
	 * @since 1.0.0
45
	 */
46
	protected function init() {
47
48
		$sub_apps = $this->sub_apps;
49
		$sub_apps->register( 'router', 'WordPoints_Hook_Router' );
50
		$sub_apps->register( 'actions', 'WordPoints_Hook_Actions' );
51
		$sub_apps->register( 'events', 'WordPoints_Hook_Events' );
52
		$sub_apps->register( 'firers', 'WordPoints_Class_Registry_Persistent' );
53
		$sub_apps->register( 'reactors', 'WordPoints_Class_Registry_Persistent' );
54
		$sub_apps->register( 'extensions', 'WordPoints_Class_Registry_Persistent' );
55
		$sub_apps->register( 'conditions', 'WordPoints_Class_Registry_Children' );
56
57
		parent::init();
58
	}
59
60
	/**
61
	 * Gets whether network-wide mode is on.
62
	 *
63
	 * @since 1.0.0
64
	 *
65
	 * @return bool Whether network-wide mode is on.
66
	 */
67
	public function get_network_mode() {
68
69
		if ( ! isset( $this->network_mode ) ) {
70
			$this->network_mode = is_network_admin();
71
72
			// See https://core.trac.wordpress.org/ticket/22589
73
			if (
74
				defined( 'DOING_AJAX' )
75
				&& DOING_AJAX
76
				&& is_multisite()
77
				&& isset( $_SERVER['HTTP_REFERER'] )
78
				&& preg_match(
79
					'#^' . preg_quote( network_admin_url(), '#' ) . '#i'
80
					, esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) )
81
				)
82
			) {
83
				$this->network_mode = true;
84
			}
85
		}
86
87
		return $this->network_mode;
88
	}
89
90
	/**
91
	 * Sets whether network-wide mode is on.
92
	 *
93
	 * This function should not be used, especially to turn network mode off when
94
	 * WordPoints has turned it on. I won't even say, "Unless you really know what
95
	 * you're doing," because trust me, if you're trying to do that, you don't. Here
96
	 * be dragons!
97
	 *
98
	 * For those of you that are specifically looking for something to break, here is
99
	 * how to do it.
100
	 *
101
	 * What network mode does is tell the hooks API that it is in the context of the
102
	 * network itself, and not of any particular site. This feature is what allows us
103
	 * to have network-wide reactions in addition to the standard, per-site
104
	 * reactions. When network mode is on, it means that we don't want to do anything
105
	 * with standard reactions at all, because even though there may be a "current
106
	 * site" that we could pull them from, we're actually supposed to be pretending
107
	 * that we aren't on a particular site on the network, but in network-wide
108
	 * context. So when network mode is turned on, you won't be able to do anything
109
	 * with standard reactions, only network-wide ones.
110
	 *
111
	 * This is why turning it off is so dangerous—it would allow you to accidentally
112
	 * do things with standard reactions when you probably aren't expected to. The
113
	 * user is in the context of the network, for example in the network admin, so
114
	 * they're only expecting network-wide reactions to be affected. If you did
115
	 * something with standard reactions, you'd end up affecting a particular site,
116
	 * when the user doesn't think he is.
117
	 *
118
	 * @since 1.0.0
119
	 *
120
	 * @internal
121
	 *
122
	 * @param bool $on Whether network-wide mode should be on (or off).
123
	 */
124
	public function _set_network_mode( $on = true ) {
125
		$this->network_mode = (bool) $on;
126
	}
127
}
128
129
// EOF
130