-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpcampus-data.php
More file actions
435 lines (341 loc) · 11.1 KB
/
wpcampus-data.php
File metadata and controls
435 lines (341 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
/**
* Plugin Name: WPCampus: Data
* Plugin URI: https://wpcampus.org
* Description: Manages data for the WPCampus network of sites.
* Version: 1.0.0
* Author: WPCampus
* Author URI: https://wpcampus.org
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: wpcampus
* Domain Path: /languages
*/
defined( 'ABSPATH' ) or die();
require_once wpcampus_data()->plugin_dir . 'inc/class-wpcampus-data-global.php';
final class WPCampus_Data {
/**
* Holds the absolute URL and
* the directory path to the
* main plugin directory.
*
* @var string
*/
public $plugin_url;
public $plugin_dir;
/**
* Holds the class instance.
*
* @access private
* @var WPCampus_Data
*/
private static $instance;
/**
* Returns the instance of this class.
*
* @access public
* @return WPCampus_Data
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
$class_name = __CLASS__;
self::$instance = new $class_name;
}
return self::$instance;
}
/**
* Magic method to output a string if
* trying to use the object as a string.
*
* @return string
*/
public function __toString() {
return sprintf( __( '%s Data', 'wpcampus-data' ), 'WPCampus' );
}
/**
* Method to keep our instance
* from being cloned or unserialized
* and to prevent a fatal error when
* calling a method that doesn't exist.
*
* @return void
*/
public function __clone() {}
public function __wakeup() {}
public function __call( $method = '', $args = array() ) {}
/**
* Warming up the engine.
*/
protected function __construct() {
// Store the plugin URL and DIR.
$this->plugin_url = plugin_dir_url( __FILE__ );
$this->plugin_dir = plugin_dir_path( __FILE__ );
}
/**
* Get the sessions from all of our events.
*/
public function get_sessions( $args = array() ) {
if ( ! function_exists( 'wpcampus_get_sessions' ) ) {
return null;
}
// Force these defaults for sessions data feed.
$args['proposal_status'] = 'confirmed';
$args['get_profiles'] = true;
$args['get_wp_user'] = true;
$args['get_subjects'] = true;
// Make sure subjects are term IDs.
if ( ! empty( $args['subject'] ) ) {
// Make sure its an array.
if ( ! is_array( $args['subject'] ) ) {
$args['subject'] = explode( ',', str_replace( ' ', '', $args['subject'] ) );
}
// Make sure its a term ID.
foreach ( $args['subject'] as &$subject ) {
if ( is_numeric( $subject ) ) {
continue;
}
$term = get_term_by( 'slug', $subject, 'subjects' );
if ( empty( $term->term_id ) ) {
continue;
}
$subject = $term->term_id;
}
}
// Make sure subjects are term IDs.
if ( ! empty( $args['format'] ) ) {
// Make sure its an array.
if ( ! is_array( $args['format'] ) ) {
$args['format'] = explode( ',', str_replace( ' ', '', $args['format'] ) );
}
// Make sure its a term ID.
foreach ( $args['format'] as &$format ) {
if ( is_numeric( $format ) ) {
continue;
}
$term = get_term_by( 'slug', $format, 'session_format' );
if ( empty( $term->term_id ) ) {
continue;
}
$format = $term->term_id;
}
}
// Set default order for date orderby.
if ( ! empty( $args['orderby'] ) ) {
// If sorting by date, then desc is default order.
if ( 'date' == $args['orderby'] && empty( $args['order'] ) ) {
$args['order'] = 'desc';
}
}
// Change event argument.
if ( ! empty( $args['event'] ) ) {
$args['proposal_event'] = $args['event'];
unset( $args['event'] );
}
// Make sure we only get certain events.
$display_events = get_option( 'options_wpc_sessions_event_display');
if ( empty( $args['proposal_event'] ) ) {
if ( ! empty( $display_events ) ) {
$args['proposal_event'] = $display_events;
}
}
$sessions = wpcampus_get_sessions( $args );
if ( empty( $sessions ) ) {
return null;
}
// Remove these fields.
// @TODO did we need this data for session review?
$session_remove = [
'post_status',
'post_type',
'format_preferred',
'format_preferred_slug',
'format_preferred_name',
'proposal_status',
];
$speaker_remove = [
'slug',
'wordpress_user',
'email',
'phone',
'nicename',
];
/*
* @TODO
* - session_video_id] => 18226
[session_video_url] => https://www.youtube.com/watch?v=g7Qnk5jPDfY
[session_video_thumbnail
*/
foreach ( $sessions as &$session ) {
foreach ( $session_remove as $key ) {
unset( $session->{$key} );
}
if ( ! empty( $session->speakers ) ) {
foreach ( $session->speakers as &$speaker ) {
foreach ( $speaker_remove as $key ) {
unset( $speaker->{$key} );
}
}
}
}
return $sessions;
}
/**
* Get our videos.
*/
public function get_videos( $args = array() ) {
global $wpdb;
$args = wp_parse_args( $args, array(
'playlist' => null,
'category' => null,
'search' => null,
));
$playlist = '';
if ( ! empty( $args['playlist'] ) ) {
$playlist = $args['playlist'];
if ( ! is_array( $playlist ) ) {
$playlist = explode( ',', str_replace( ' ', '', $playlist ) );
}
$playlist = array_map( 'sanitize_text_field', $playlist );
}
$post_type = array( 'podcast', 'video' );
$podcast_search = ! empty( $playlist ) ? array_search( 'podcast', $playlist ) : false;
if ( false !== $podcast_search ) {
unset( $playlist[ $podcast_search ] );
// This means we're only looking for podcasts.
if ( empty( $playlist ) ) {
$post_type = array( 'podcast' );
}
}
$post_type_str = implode( "','", $post_type );
$category = '';
if ( ! empty( $args['category'] ) ) {
$category = $args['category'];
if ( ! is_array( $category ) ) {
$category = explode( ',', str_replace( ' ', '', $category ) );
}
$category = array_map( 'sanitize_text_field', $category );
}
$search = '';
if ( ! empty( $args['search'] ) ) {
$search = sanitize_text_field( $args['search'] );
}
$select = "SELECT posts.ID,
posts.post_author,
posts.post_content,
IF ( video_title.meta_value IS NOT NULL AND video_title.meta_value != '', video_title.meta_value, posts.post_title ) AS post_title,
posts.post_name,
proposal.post_id AS proposal,
IF ( proposal.post_id IS NOT NULL, ( SELECT post_name FROM {$wpdb->posts} WHERE ID = proposal.post_id AND post_type = 'proposal' AND post_status = 'publish' ), NULL ) AS proposal_slug,
posts.post_type,
posts.comment_count,
playlist_terms.name AS event_name,
playlist_terms.slug AS event_slug";
$from = " FROM {$wpdb->posts} posts";
$join = " LEFT JOIN {$wpdb->postmeta} video_title ON video_title.post_id = posts.ID and video_title.meta_key = 'video_title'
LEFT JOIN {$wpdb->postmeta} proposal ON proposal.meta_value = posts.ID AND proposal.meta_key = 'session_video'";
$join .= " INNER JOIN {$wpdb->term_relationships} playlist_rel ON playlist_rel.object_id = posts.ID
INNER JOIN {$wpdb->term_taxonomy} playlist_tax ON playlist_tax.term_taxonomy_id = playlist_rel.term_taxonomy_id AND playlist_tax.taxonomy = 'playlist'
INNER JOIN {$wpdb->terms} playlist_terms ON playlist_terms.term_id = playlist_tax.term_id";
if ( ! empty( $category ) ) {
$category_str = implode( "','", $category );
$join .= " INNER JOIN {$wpdb->term_relationships} category_rel ON category_rel.object_id = posts.ID
INNER JOIN {$wpdb->term_taxonomy} category_tax ON category_tax.term_taxonomy_id = category_rel.term_taxonomy_id AND category_tax.taxonomy = 'category'
INNER JOIN {$wpdb->terms} category_terms ON category_terms.term_id = category_tax.term_id AND category_terms.slug IN ('" . $category_str . "')";
}
$where = " WHERE posts.post_type IN ('" . $post_type_str . "') AND posts.post_status = 'publish'";
if ( ! empty( $playlist ) ) {
$where .= " AND playlist_terms.slug IN ('" . implode( "','", $playlist ) . "')";
}
if ( ! empty( $search ) ) {
$where .= " AND ( posts.post_title LIKE '%" . $search . "%' OR posts.post_content LIKE '%" . $search . "%')";
}
$groupby = " GROUP BY posts.ID";
if ( ! empty( $playlist ) ) {
$havingby = " HAVING event_name IS NOT NULL";
if ( false !== $podcast_search ) {
$havingby .= " OR posts.post_type = 'podcast'";
}
} else {
$havingby = '';
}
$orderby = " ORDER BY post_title ASC";
$query = $select . $from . $join . $where . $groupby . $havingby . $orderby;
$videos = $wpdb->get_results( $query );
if ( empty( $videos ) ) {
return $videos;
}
$media_exists = function_exists( 'wpcampus_media' );
foreach ( $videos as &$video ) {
$video->youtube = $media_exists ? wpcampus_media()->get_youtube_video_id( $video->ID ) : null;
$video->watch_permalink = ! empty( $video->youtube ) ? wpcampus_media()->get_youtube_watch_url( $video->youtube ) : null;
if ( 'podcast' == $video->post_type ) {
$video->permalink = get_permalink( $video->ID );
} else {
$video->permalink = null;
switch( $video->event_slug ) {
case 'wpcampus-online-2017':
case 'wpcampus-online-2018':
case 'wpcampus-online-2019':
$video->permalink = 'https://online.wpcampus.org/schedule/';
break;
case 'wpcampus-2016':
$video->permalink = 'https://2016.wpcampus.org/schedule/';
break;
case 'wpcampus-2017':
$video->permalink = 'https://2017.wpcampus.org/schedule/';
break;
case 'wpcampus-2018':
$video->permalink = 'https://2018.wpcampus.org/schedule/';
break;
case 'wpcampus-2019':
$video->permalink = 'https://2019.wpcampus.org/schedule/';
break;
}
if ( ! empty( $video->proposal_slug ) ) {
$video->permalink .= $video->proposal_slug;
}
}
if ( empty( $video->permalink ) && ! empty( $video->watch_permalink ) ) {
$video->permalink = $video->watch_permalink;
}
// Get authors.
$video->authors = $media_exists ? wpcampus_media()->get_video_authors( $video->ID ) : array();
// Get the thumbnail.
$video->thumbnail = null;
// Get the YouTube snippet to get the thumbnail.
$snippet = get_post_meta( $video->ID, 'wpc_youtube_video_snippet', true );
if ( ! empty( $snippet ) ) {
// Get the thumbnails.
$thumbnails = $snippet && ! empty( $snippet->thumbnails ) ? (array) $snippet->thumbnails : null;
// Get the thumbnail.
if ( $thumbnails ) {
foreach ( array( 'standard', 'high', 'medium', 'default' ) as $size ) {
if ( isset( $thumbnails[ $size ] ) && ! empty( $thumbnails[ $size ]->url ) ) {
$video->thumbnail = $thumbnails[ $size ]->url;
break;
}
}
}
}
// If no thumbnail, use default set at standard size: 640 x 480.
if ( empty( $video->thumbnail ) ) {
$video->thumbnail = '/wp-content/plugins/wpcampus-media-plugin/assets/images/video-thumbnail-standard.png';
}
}
return $videos;
}
}
/**
* Returns the instance of our main WPCampus_Data class.
*
* Will come in handy when we need to access the
* class to retrieve data throughout the plugin.
*
* @access public
* @return WPCampus_Data
*/
function wpcampus_data() {
return WPCampus_Data::instance();
}
// Let's get this show on the road
wpcampus_data();