1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: steve |
6
|
|
|
* Date: 4/17/2018 |
7
|
|
|
* Time: 10:09 PM |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
if ( !class_exists("FooGallery_Pro_Video_Query") ){ |
11
|
|
|
|
12
|
|
|
require_once dirname(__FILE__) . '/class-foogallery-pro-video-base.php'; |
13
|
|
|
require_once dirname(__FILE__) . '/class-foogallery-pro-video-self-hosted.php'; |
14
|
|
|
require_once dirname(__FILE__) . '/class-foogallery-pro-video-oembed.php'; |
15
|
|
|
require_once dirname(__FILE__) . '/class-foogallery-pro-video-youtube.php'; |
16
|
|
|
require_once dirname(__FILE__) . '/class-foogallery-pro-video-vimeo.php'; |
17
|
|
|
|
18
|
|
|
class FooGallery_Pro_Video_Query extends FooGallery_Pro_Video_Base { |
|
|
|
|
19
|
|
|
|
20
|
|
|
public $self_hosted; |
21
|
|
|
public $oembed; |
22
|
|
|
public $youtube; |
23
|
|
|
public $vimeo; |
24
|
|
|
|
25
|
|
|
function __construct() { |
|
|
|
|
26
|
|
|
|
27
|
|
|
$this->self_hosted = new FooGallery_Pro_Video_Self_Hosted(); |
28
|
|
|
$this->oembed = new FooGallery_Pro_Video_oEmbed(); |
29
|
|
|
$this->youtube = new FooGallery_Pro_Video_YouTube(); |
30
|
|
|
$this->vimeo = new FooGallery_Pro_Video_Vimeo(); |
31
|
|
|
|
32
|
|
|
add_action('wp_ajax_fgi_query', array($this, 'ajax')); |
33
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Gets the query arguments from the variables supplied to the script via HTTP POST. |
38
|
|
|
* |
39
|
|
|
* @return array( |
|
|
|
|
40
|
|
|
* "query" => string, |
41
|
|
|
* "page" => number, |
42
|
|
|
* "offset" => number, |
43
|
|
|
* "nonce" => string |
44
|
|
|
* ) |
45
|
|
|
*/ |
46
|
|
|
public function get_args() { |
47
|
|
|
return array( |
48
|
|
|
"query" => !empty($_POST["query"]) ? trim($_POST["query"]) : null, |
49
|
|
|
"page" => !empty($_POST["page"]) ? (int)$_POST["page"] : 1, |
50
|
|
|
"offset" => !empty($_POST["offset"]) ? (int)$_POST["offset"] : 0, |
51
|
|
|
"nonce" => !empty($_POST["fgi_nonce"]) ? $_POST["fgi_nonce"] : null |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The AJAX handler for the query side of the plugin that returns videos to the front-end so the user can decide what to import. |
57
|
|
|
*/ |
58
|
|
|
public function ajax() { |
59
|
|
|
$args = $this->get_args(); |
60
|
|
|
if (wp_verify_nonce($args["nonce"], "fgi_nonce")){ |
61
|
|
|
if (empty($args["query"]) || mb_strlen($args["query"], "UTF-8") < 3) { |
62
|
|
|
wp_send_json_error("The 'query' argument is required and must be a minimum of 3 characters in length."); |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
$response = $this->handle_query($args["query"], $args["page"], $args["offset"]); |
66
|
|
|
// regardless of the request all successful queries contain the original query in the response |
67
|
|
|
$response["query"] = $args["query"]; |
68
|
|
|
wp_send_json_success($response); |
69
|
|
|
} |
70
|
|
|
die(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $query |
75
|
|
|
* @param int $page |
76
|
|
|
* @param int $offset |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function handle_query($query, $page = 1, $offset = 0) { |
80
|
|
|
// parse the query to test if it is a valid url |
81
|
|
|
$url = wp_http_validate_url($query); |
82
|
|
|
|
83
|
|
|
// if we have a valid url then try fetch the required data |
84
|
|
|
if ($url !== false) { |
85
|
|
|
// handle YouTube specific urls as we parse playlists etc. |
86
|
|
|
if ($this->youtube->handles($url)) { |
87
|
|
|
return $this->youtube->fetch($url); |
88
|
|
|
} |
89
|
|
|
// handle Vimeo specific urls as we parse albums etc. |
90
|
|
|
if ($this->vimeo->handles($url)) { |
91
|
|
|
return $this->vimeo->fetch($url, $page, $offset); |
92
|
|
|
} |
93
|
|
|
// check if this is a self hosted video |
94
|
|
|
if ($this->self_hosted->handles($url)){ |
95
|
|
|
return $this->self_hosted->fetch($url); |
96
|
|
|
} |
97
|
|
|
// if we get here let the url be handled by the built in WP_oEmbed class |
98
|
|
|
return $this->oembed->fetch($url); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// if we get here the query was a not a url |
102
|
|
|
return $this->youtube->query($query, $page, $offset); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.