| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
die( 'You are not allowed to call this page directly.' ); |
| 5 |
} |
| 6 |
|
| 7 |
class FrmYoutubeFeedApi extends FrmFormApi { |
| 8 |
|
| 9 |
/** |
| 10 |
* The YouTube API URL. |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
private $api_url = 'https://formidableforms.com/wp-json/s11-sites/v1/youtube-feed/'; |
| 15 |
|
| 16 |
/** |
| 17 |
* The YouTube API endpoints. |
| 18 |
* |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
private $api_endpoints = array( |
| 22 |
'welcome' => 'get-welcome-video', |
| 23 |
'latest' => 'get-latest-video', |
| 24 |
'featured' => 'get-featured-video', |
| 25 |
); |
| 26 |
|
| 27 |
/** |
| 28 |
* The active API endpoint. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
private $api_active_endpoint = ''; |
| 33 |
|
| 34 |
/** |
| 35 |
* The cache key names. |
| 36 |
* |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
private $cache_keys = array( |
| 40 |
'welcome' => 'frm-welcome-video', |
| 41 |
'latest' => 'frm-latest-video', |
| 42 |
'featured' => 'frm-featured-video', |
| 43 |
); |
| 44 |
|
| 45 |
public function __construct() { |
| 46 |
$this->init_api_options(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Build the YouTube API URL. |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
protected function api_url() { |
| 55 |
return $this->api_url . '?action=' . $this->get_api_endpoint(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Build the API endpoint |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
private function get_api_endpoint() { |
| 64 |
return $this->api_active_endpoint; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Init api options: cache key and url endpoint. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
private function init_api_options( $endpoint = null, $cache_key = null ) { |
| 73 |
$this->cache_key = $this->cache_keys['welcome']; |
| 74 |
$this->api_active_endpoint = $this->api_endpoints['welcome']; |
| 75 |
|
| 76 |
if ( $endpoint ) { |
| 77 |
$this->api_active_endpoint = $endpoint; |
| 78 |
} |
| 79 |
if ( $cache_key ) { |
| 80 |
$this->cache_key = $cache_key; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get the YouTube video. It gets the data from cache, if cache is no available it will make a new request to YouTube API. |
| 86 |
* |
| 87 |
* @param string $type The video type that will be fetched: welcome-video|featured-video|latest-video. |
| 88 |
* |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
public function get_video( $type = 'welcome' ) { |
| 92 |
return $this->get_feed_by_api_endpoint( $this->api_endpoints[ $type ], $this->cache_keys[ $type ] ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Makes request to YouTube feed API. Gets data from specified endpoint. After request is done, it will store data to wp cache. |
| 97 |
* |
| 98 |
* @param string $api_endpoint The YouTube API endpoint. |
| 99 |
* @param string $cache_key The cache key used to store data to wp cache. |
| 100 |
* |
| 101 |
* @return array |
| 102 |
*/ |
| 103 |
private function get_feed_by_api_endpoint( $api_endpoint, $cache_key ) { |
| 104 |
$this->init_api_options( $api_endpoint, $cache_key ); |
| 105 |
return $this->get_api_info(); |
| 106 |
} |
| 107 |
} |
| 108 |
|