|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Objects Factory |
|
4
|
|
|
* |
|
5
|
|
|
* @package SimpleCalendar |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace SimpleCalendar; |
|
8
|
|
|
|
|
9
|
|
|
use SimpleCalendar\Abstracts as Object; |
|
10
|
|
|
|
|
11
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
12
|
|
|
exit; |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Objects factory. |
|
17
|
|
|
* |
|
18
|
|
|
* Helper class to get the right type of object used across the plugin. |
|
19
|
|
|
* |
|
20
|
|
|
* @since 3.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Objects { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* Add default objects. |
|
28
|
|
|
* |
|
29
|
|
|
* @since 3.0.0 |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct() { |
|
32
|
|
|
|
|
33
|
|
|
// Add default feed type. |
|
34
|
|
|
add_filter( 'simcal_get_feed_types', function( $feed_types ) { |
|
35
|
|
|
return array_merge( $feed_types, array( |
|
36
|
|
|
'google', |
|
37
|
|
|
'grouped-calendars', |
|
38
|
|
|
) ); |
|
39
|
|
|
}, 10, 1 ); |
|
40
|
|
|
|
|
41
|
|
|
// Add default calendar type. |
|
42
|
|
|
add_filter( 'simcal_get_calendar_types', function( $calendar_types ) { |
|
43
|
|
|
return array_merge( $calendar_types, array( |
|
44
|
|
|
'default-calendar' => array( |
|
45
|
|
|
'grid', |
|
46
|
|
|
'list', |
|
47
|
|
|
), |
|
48
|
|
|
) ); |
|
49
|
|
|
}, 10, 1 ); |
|
50
|
|
|
|
|
51
|
|
|
// Add default admin objects. |
|
52
|
|
|
if ( $is_admin = is_admin() ) { |
|
53
|
|
|
add_filter( 'simcal_get_admin_pages', function( $admin_pages ) { |
|
54
|
|
|
return array_merge( $admin_pages, array( |
|
55
|
|
|
'add-ons' => array( |
|
56
|
|
|
'add-ons', |
|
57
|
|
|
), |
|
58
|
|
|
'settings' => array( |
|
59
|
|
|
'feeds', |
|
60
|
|
|
'calendars', |
|
61
|
|
|
'advanced', |
|
62
|
|
|
), |
|
63
|
|
|
'tools' => array( |
|
64
|
|
|
'system-status', |
|
65
|
|
|
), |
|
66
|
|
|
) ); |
|
67
|
|
|
}, 10, 1 ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
do_action( 'simcal_load_objects', $is_admin ); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get feed types. |
|
75
|
|
|
* |
|
76
|
|
|
* @since 3.0.0 |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public function get_feed_types() { |
|
81
|
|
|
$array = apply_filters( 'simcal_get_feed_types', array() ); |
|
82
|
|
|
ksort( $array ); |
|
83
|
|
|
return $array; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get calendar types. |
|
88
|
|
|
* |
|
89
|
|
|
* @since 3.0.0 |
|
90
|
|
|
* |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
|
|
public function get_calendar_types() { |
|
94
|
|
|
$array = apply_filters( 'simcal_get_calendar_types', array() ); |
|
95
|
|
|
ksort( $array ); |
|
96
|
|
|
return $array; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get admin pages. |
|
101
|
|
|
* |
|
102
|
|
|
* @since 3.0.0 |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
public function get_admin_pages() { |
|
107
|
|
|
return apply_filters( 'simcal_get_admin_pages', array() ); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get a calendar. |
|
112
|
|
|
* |
|
113
|
|
|
* Returns the right type of calendar. |
|
114
|
|
|
* |
|
115
|
|
|
* @since 3.0.0 |
|
116
|
|
|
* |
|
117
|
|
|
* @param int|string|object|\WP_Post|Object\Calendar $object |
|
118
|
|
|
* |
|
119
|
|
|
* @return null|Object\Calendar |
|
|
|
|
|
|
120
|
|
|
*/ |
|
121
|
|
|
public function get_calendar( $object ) { |
|
122
|
|
|
|
|
123
|
|
View Code Duplication |
if ( is_string( $object ) ) { |
|
|
|
|
|
|
124
|
|
|
return ! empty( $object ) ? $this->get_object( $object, 'calendar', '' ) : null; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if ( is_object( $object ) ) { |
|
128
|
|
|
if ( $object instanceof Object\Calendar ) { |
|
129
|
|
|
return $this->get_object( $object->type, 'feed', $object ); |
|
130
|
|
View Code Duplication |
} elseif ( $object instanceof \WP_Post ) { |
|
|
|
|
|
|
131
|
|
|
if ( $type = wp_get_object_terms( $object->ID, 'calendar_type' ) ) { |
|
132
|
|
|
$name = sanitize_title( current( $type )->name ); |
|
133
|
|
|
return $this->get_object( $name, 'calendar', $object ); |
|
134
|
|
|
} |
|
135
|
|
|
} elseif ( isset( $object->type ) && isset( $object->id ) ) { |
|
136
|
|
|
return $this->get_object( $object->type, 'calendar', $object->id ); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if ( is_int( $object ) ) { |
|
141
|
|
|
$post = get_post( $object ); |
|
142
|
|
View Code Duplication |
if ( $post && ( $type = wp_get_object_terms( $post->ID, 'calendar_type' ) ) ) { |
|
|
|
|
|
|
143
|
|
|
$name = sanitize_title( current( $type )->name ); |
|
144
|
|
|
return $this->get_object( $name, 'calendar', $post ); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return null; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get a calendar view. |
|
153
|
|
|
* |
|
154
|
|
|
* @since 3.0.0 |
|
155
|
|
|
* |
|
156
|
|
|
* @param int $id Feed post id. |
|
157
|
|
|
* @param string $name (optional) Name of calendar view. |
|
158
|
|
|
* |
|
159
|
|
|
* @return null|Object\Calendar_View |
|
|
|
|
|
|
160
|
|
|
*/ |
|
161
|
|
|
public function get_calendar_view( $id = 0, $name = '' ) { |
|
162
|
|
|
|
|
163
|
|
|
if ( ! $name && $id > 0 ) { |
|
164
|
|
|
|
|
165
|
|
|
$calendar_view = get_post_meta( $id, '_calendar_view', true ); |
|
166
|
|
|
|
|
167
|
|
|
if ( $terms = wp_get_object_terms( $id, 'calendar_type' ) ) { |
|
168
|
|
|
$calendar_type = sanitize_title( current( $terms )->name ); |
|
169
|
|
|
$name = isset( $calendar_view[ $calendar_type ] ) ? $calendar_type . '-' . $calendar_view[ $calendar_type ] : ''; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $name ? $this->get_object( $name, 'calendar-view', '' ) : null; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Get a feed. |
|
179
|
|
|
* |
|
180
|
|
|
* Returns the right type of feed. |
|
181
|
|
|
* |
|
182
|
|
|
* @since 3.0.0 |
|
183
|
|
|
* |
|
184
|
|
|
* @param int|string|object|\WP_Post|Object\Calendar $object |
|
185
|
|
|
* |
|
186
|
|
|
* @return null|Object\Feed |
|
|
|
|
|
|
187
|
|
|
*/ |
|
188
|
|
|
public function get_feed( $object ) { |
|
189
|
|
|
|
|
190
|
|
View Code Duplication |
if ( is_string( $object ) ) { |
|
|
|
|
|
|
191
|
|
|
return ! empty( $object ) ? $this->get_object( $object, 'feed', '' ) : null; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
if ( is_object( $object ) ) { |
|
195
|
|
|
if ( $object instanceof Object\Calendar ) { |
|
196
|
|
|
$feed_name = ''; |
|
197
|
|
|
if ( empty( $object->feed ) ) { |
|
198
|
|
|
if ( $feed_type = wp_get_object_terms( $object->id, 'feed_type' ) ) { |
|
199
|
|
|
$feed_name = sanitize_title( current( $feed_type )->name ); |
|
200
|
|
|
} |
|
201
|
|
|
} else { |
|
202
|
|
|
$feed_name = $object->feed; |
|
203
|
|
|
} |
|
204
|
|
|
return $this->get_object( $feed_name, 'feed', $object ); |
|
205
|
|
View Code Duplication |
} elseif ( $object instanceof \WP_Post ) { |
|
|
|
|
|
|
206
|
|
|
$calendar = $this->get_calendar( $object ); |
|
207
|
|
|
|
|
208
|
|
|
if ( isset( $calendar->feed ) ) { |
|
209
|
|
|
return $this->get_object( $calendar->feed, 'feed', $calendar ); |
|
210
|
|
|
} else { |
|
211
|
|
|
return null; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
} elseif ( isset( $object->feed ) && isset( $object->id ) ) { |
|
216
|
|
|
return $this->get_object( $object->feed, 'feed', $object ); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
View Code Duplication |
if ( is_int( $object ) ) { |
|
|
|
|
|
|
221
|
|
|
$calendar = $this->get_calendar( $object ); |
|
222
|
|
|
return isset( $calendar->feed ) ? $this->get_object( $calendar->feed, 'feed', $calendar ) : null; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return null; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Get a field. |
|
230
|
|
|
* |
|
231
|
|
|
* @since 3.0.0 |
|
232
|
|
|
* |
|
233
|
|
|
* @param array $args Field args. |
|
234
|
|
|
* @param string $name Field type. |
|
235
|
|
|
* |
|
236
|
|
|
* @return null|Object\Field |
|
|
|
|
|
|
237
|
|
|
*/ |
|
238
|
|
|
public function get_field( $args, $name = '' ) { |
|
239
|
|
|
|
|
240
|
|
|
if ( empty( $name ) ) { |
|
241
|
|
|
$name = isset( $args['type'] ) ? $args['type'] : false; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return $name ? $this->get_object( $name, 'field', $args ) : null; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Get a settings page. |
|
249
|
|
|
* |
|
250
|
|
|
* @since 3.0.0 |
|
251
|
|
|
* |
|
252
|
|
|
* @param string $name |
|
253
|
|
|
* |
|
254
|
|
|
* @return null|Object\Admin_Page |
|
|
|
|
|
|
255
|
|
|
*/ |
|
256
|
|
|
public function get_admin_page( $name ) { |
|
257
|
|
|
return $name ? $this->get_object( $name, 'admin-page' ) : null; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Get a plugin object. |
|
262
|
|
|
* |
|
263
|
|
|
* @since 3.0.0 |
|
264
|
|
|
* @access private |
|
265
|
|
|
* |
|
266
|
|
|
* @param string $name Object name. |
|
267
|
|
|
* @param string $type Object type. |
|
268
|
|
|
* @param mixed $args (optional) arguments for the class constructor. |
|
269
|
|
|
* |
|
270
|
|
|
* @return null|Object |
|
|
|
|
|
|
271
|
|
|
*/ |
|
272
|
|
|
private function get_object( $name, $type, $args = '' ) { |
|
273
|
|
|
|
|
274
|
|
|
$types = array( |
|
275
|
|
|
'admin-page', |
|
276
|
|
|
'calendar', |
|
277
|
|
|
'calendar-view', |
|
278
|
|
|
'feed', |
|
279
|
|
|
'field', |
|
280
|
|
|
); |
|
281
|
|
|
|
|
282
|
|
|
if ( in_array( $type, $types ) ) { |
|
283
|
|
|
|
|
284
|
|
|
$class_name = $this->make_class_name( $name, $type ); |
|
285
|
|
|
$parent = '\\' . __NAMESPACE__ . '\Abstracts\\' . implode( '_', array_map( 'ucfirst', explode( '-', $type ) ) ); |
|
286
|
|
|
$class = class_exists( $class_name ) ? new $class_name( $args ) : false; |
|
287
|
|
|
|
|
288
|
|
|
return $class instanceof $parent ? $class : null; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
return null; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Make class name from slug. |
|
296
|
|
|
* |
|
297
|
|
|
* Standardizes object naming and class names: <object-name> becomes <Class_Name>. |
|
298
|
|
|
* The plugin autoloader uses a similar pattern. |
|
299
|
|
|
* |
|
300
|
|
|
* @since 3.0.0 |
|
301
|
|
|
* @access private |
|
302
|
|
|
* |
|
303
|
|
|
* @param string $name Object name. |
|
304
|
|
|
* @param string $type Object type. |
|
305
|
|
|
* |
|
306
|
|
|
* @return string The class name complete with its full namespace. |
|
307
|
|
|
*/ |
|
308
|
|
|
private function make_class_name( $name, $type ) { |
|
309
|
|
|
|
|
310
|
|
|
if ( 'calendar' == $type ) { |
|
311
|
|
|
$namespace = '\\' . __NAMESPACE__ . '\Calendars\\'; |
|
312
|
|
|
} elseif ( 'calendar-view' == $type ) { |
|
313
|
|
|
$namespace = '\\' . __NAMESPACE__ . '\Calendars\Views\\'; |
|
314
|
|
|
} elseif ( 'feed' == $type ) { |
|
315
|
|
|
$namespace = '\\' . __NAMESPACE__ . '\Feeds\\'; |
|
316
|
|
|
} elseif ( 'field' == $type ) { |
|
317
|
|
|
$namespace = '\\' . __NAMESPACE__ . '\Admin\Fields\\'; |
|
318
|
|
|
} elseif ( 'admin-page' == $type ) { |
|
319
|
|
|
$namespace = '\\' . __NAMESPACE__ . '\Admin\Pages\\'; |
|
320
|
|
|
} else { |
|
321
|
|
|
return ''; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
$class_name = implode( '_', array_map( 'ucfirst', explode( '-', $name ) ) ); |
|
325
|
|
|
|
|
326
|
|
|
return $namespace . $class_name; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
} |
|
330
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.