|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Module API classes. |
|
5
|
|
|
* |
|
6
|
|
|
* @package WordPointsOrg |
|
7
|
|
|
* @since 1.0.0 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class representing the types of available update APIs. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 1.0.0 |
|
14
|
|
|
*/ |
|
15
|
|
View Code Duplication |
final class WordPoints_Module_APIs extends WordPoints_Container_Static { |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @since 1.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static $instance; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Initialize the container. |
|
24
|
|
|
* |
|
25
|
|
|
* This function must be called before the container can be used. |
|
26
|
|
|
* |
|
27
|
|
|
* @since 1.0.0 |
|
28
|
|
|
* |
|
29
|
|
|
* @return bool True if the container was initialized, false if it was already. |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function init() { |
|
32
|
|
|
|
|
33
|
|
|
if ( ! isset( self::$instance ) ) { |
|
34
|
|
|
|
|
35
|
|
|
self::$instance = new self; |
|
36
|
|
|
|
|
37
|
|
|
self::register_apis(); |
|
38
|
|
|
|
|
39
|
|
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @since 1.0.0 |
|
47
|
|
|
* |
|
48
|
|
|
* @see WordPoints_Container::_add() |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function register( $slug, $item, $class = null ) { |
|
51
|
|
|
return self::$instance->_add( $slug, $item, $class ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @since 1.0.0 |
|
56
|
|
|
* |
|
57
|
|
|
* @see WordPoints_Container::_remove() |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function deregister( $slug ) { |
|
60
|
|
|
return self::$instance->_remove( $slug ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @since 1.0.0 |
|
65
|
|
|
* |
|
66
|
|
|
* @see WordPoints_Container::_contains() |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function is_registered( $slug ) { |
|
69
|
|
|
return self::$instance->_contains( $slug ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @since 1.0.0 |
|
74
|
|
|
* |
|
75
|
|
|
* @see WordPoints_Container::_get() |
|
76
|
|
|
* |
|
77
|
|
|
* @return WordPoints_Module_API[]|WordPoints_Module_API |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function get( $slug = null ) { |
|
80
|
|
|
return self::$instance->_get( $slug ); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Call the action to register all of the installed APIs. |
|
85
|
|
|
* |
|
86
|
|
|
* @since 1.0.0 |
|
87
|
|
|
*/ |
|
88
|
|
|
private static function register_apis() { |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Register the available module APIs. |
|
92
|
|
|
* |
|
93
|
|
|
* @since 1.0.0 |
|
94
|
|
|
*/ |
|
95
|
|
|
do_action( 'wordpoints_register_module_apis' ); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
add_action( 'admin_init', 'WordPoints_Module_APIs::init' ); |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Abstract class for representing module update API types. |
|
102
|
|
|
* |
|
103
|
|
|
* Module update APIs are basically web URL endpoints that supply module updates. |
|
104
|
|
|
* Each API might be of a different type, using different GET parameters to identify |
|
105
|
|
|
* the module, for example. Each type of update API needs to be handled a little |
|
106
|
|
|
* differently, but they all have certain things in common. This class provides a |
|
107
|
|
|
* common base for all update API types. Each update API type then provides a common |
|
108
|
|
|
* handler for all APIs of that type. |
|
109
|
|
|
* |
|
110
|
|
|
* @since 1.0.0 |
|
111
|
|
|
*/ |
|
112
|
|
|
abstract class WordPoints_Module_API { |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* The slug of the API type. |
|
116
|
|
|
* |
|
117
|
|
|
* @since 1.0.0 |
|
118
|
|
|
* |
|
119
|
|
|
* @type string $slug |
|
120
|
|
|
*/ |
|
121
|
|
|
protected $slug; |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* The features supported by this API. |
|
125
|
|
|
* |
|
126
|
|
|
* @since 1.0.0 |
|
127
|
|
|
* |
|
128
|
|
|
* @type array $supports |
|
129
|
|
|
*/ |
|
130
|
|
|
protected $supports; |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Construct the API type with the slug and other data. |
|
134
|
|
|
* |
|
135
|
|
|
* @since 1.0.0 |
|
136
|
|
|
*/ |
|
137
|
|
|
public function __construct() { |
|
138
|
|
|
|
|
139
|
|
|
$this->hooks(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Check if a given feature is supported by the API. |
|
144
|
|
|
* |
|
145
|
|
|
* @since 1.0.0 |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $feature The feature to check support for. |
|
148
|
|
|
* |
|
149
|
|
|
* @return bool Whether the feature is supported. |
|
150
|
|
|
*/ |
|
151
|
|
|
public function supports( $feature ) { |
|
152
|
|
|
|
|
153
|
|
|
return isset( $this->supports[ $feature ] ); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Hook up any actions and filters used by this API type. |
|
158
|
|
|
* |
|
159
|
|
|
* @snce 1.0.0 |
|
160
|
|
|
*/ |
|
161
|
|
|
public function hooks() {} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Check for updates for the modules on a channel. |
|
165
|
|
|
* |
|
166
|
|
|
* @since 1.0.0 |
|
167
|
|
|
* |
|
168
|
|
|
* @param WordPoints_Module_Channel $channel The channel to check for updates on. |
|
169
|
|
|
* |
|
170
|
|
|
* @return string[] The new versions of modules needing updates, indexed by module file. |
|
171
|
|
|
*/ |
|
172
|
|
|
abstract public function check_for_updates( $channel ); |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Get the URL of zip package for the latest version of a module. |
|
176
|
|
|
* |
|
177
|
|
|
* @since 1.0.0 |
|
178
|
|
|
* |
|
179
|
|
|
* @param WordPoints_Module_Channel $channel The channel the package should be from. |
|
180
|
|
|
* @param array $module The module's data. |
|
181
|
|
|
* |
|
182
|
|
|
* @return string The package URL. |
|
183
|
|
|
*/ |
|
184
|
|
|
abstract public function get_package_url( $channel, $module ); |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get the changelog for the latest version of a module. |
|
188
|
|
|
* |
|
189
|
|
|
* @since 1.1.0 |
|
190
|
|
|
* |
|
191
|
|
|
* @param WordPoints_Module_Channel $channel The channel the changelog should be from. |
|
192
|
|
|
* @param array $module The module's data. |
|
193
|
|
|
* |
|
194
|
|
|
* @return string The changelog URL. |
|
195
|
|
|
*/ |
|
196
|
|
|
abstract public function get_changelog( $channel, $module ); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
// EOF |
|
200
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.