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

WordPoints_Hook_Extension::update_settings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4286
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
/**
4
 * Hook extension class.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since 1.0.0
8
 */
9
10
/**
11
 * Represents a hook extension.
12
 *
13
 * Hook extensions extend the basic hooks API, and can modify whether a particular
14
 * hook firing should hit the target. Each extension makes this decision based on
15
 * custom settings it offers for each reaction.
16
 *
17
 * @since 1.0.0
18
 */
19
abstract class WordPoints_Hook_Extension implements WordPoints_Hook_SettingsI {
20
21
	/**
22
	 * The unique slug for identifying this extension.
23
	 *
24
	 * @since 1.0.0
25
	 *
26
	 * @var string
27
	 */
28
	protected $slug;
29
30
	/**
31
	 * The validator for the current reaction.
32
	 *
33
	 * @since 1.0.0
34
	 *
35
	 * @var WordPoints_Hook_Reaction_Validator
36
	 */
37
	protected $validator;
38
39
	/**
40
	 * The args for the current event.
41
	 *
42
	 * @since 1.0.0
43
	 *
44
	 * @var WordPoints_Hook_Event_Args
45
	 */
46
	protected $event_args;
47
48
	/**
49
	 * Get the slug of this extension.
50
	 *
51
	 * @since 1.0.0
52
	 *
53
	 * @return string The extension's slug.
54
	 */
55
	public function get_slug() {
56
		return $this->slug;
57
	}
58
59
	/**
60
	 * @since 1.0.0
61
	 */
62
	public function validate_settings(
63
		array $settings,
64
		WordPoints_Hook_Reaction_Validator $validator,
65
		WordPoints_Hook_Event_Args $event_args
66
	) {
67
68
		if ( ! isset( $settings[ $this->slug ] ) ) {
69
			return $settings;
70
		}
71
72
		$this->validator = $validator;
73
		$this->event_args = $event_args;
74
75
		$this->validator->push_field( $this->slug );
76
		$settings[ $this->slug ] = $this->{"validate_{$this->slug}"}( $settings[ $this->slug ] );
77
		$this->validator->pop_field();
78
79
		return $settings;
80
	}
81
82
	/**
83
	 * @since 1.0.0
84
	 */
85
	public function update_settings( WordPoints_Hook_ReactionI $reaction, array $settings ) {
86
87
		if ( isset( $settings[ $this->slug ] ) ) {
88
			$reaction->update_meta( $this->slug, $settings[ $this->slug ] );
89
		} else {
90
			$reaction->delete_meta( $this->slug );
91
		}
92
	}
93
94
	/**
95
	 * Check whether this hook firing should hit the target.
96
	 *
97
	 * @since 1.0.0
98
	 *
99
	 * @param WordPoints_Hook_Reaction_Validator $reaction   The reaction.
100
	 * @param WordPoints_Hook_Event_Args         $event_args The event args.
101
	 *
102
	 * @return bool Whether the target should be hit by this hook firing.
103
	 */
104
	abstract public function should_hit(
105
		WordPoints_Hook_Reaction_Validator $reaction,
106
		WordPoints_Hook_Event_Args $event_args
107
	);
108
109
	/**
110
	 * After a reaction has hit the target.
111
	 *
112
	 * @since 1.0.0
113
	 *
114
	 * @param WordPoints_Hook_Reaction_Validator $reaction   The reaction.
115
	 * @param WordPoints_Hook_Event_Args         $event_args The event args.
116
	 */
117
	public function after_hit(
118
		WordPoints_Hook_Reaction_Validator $reaction,
119
		WordPoints_Hook_Event_Args $event_args
120
	) {}
121
122
	/**
123
	 * Get the data the scripts need for the UI.
124
	 *
125
	 * @since 1.0.0
126
	 *
127
	 * @return array Any data that needs to be present for the scripts in the UI.
128
	 */
129
	public function get_ui_script_data() {
130
		return array();
131
	}
132
}
133
134
// EOF
135