PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 3.9.10
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v3.9.10
4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / vendor / wpdevelopers / embera / src / Embera / Embera.php
embedpress / vendor / wpdevelopers / embera / src / Embera Last commit date
Cache 5 years ago Html 5 years ago Http 5 years ago Provider 2 years ago ProviderCollection 5 years ago Embera.php 3 years ago Url.php 5 years ago
Embera.php
234 lines
1 <?php
2 /**
3 * Embera.php
4 *
5 * @package Embera
6 * @author Michael Pratt <yo@michael-pratt.com>
7 * @link http://www.michael-pratt.com/
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13 namespace Embera;
14
15 use Embera\Http\HttpClient;
16 use Embera\Http\OembedClient;
17 use Embera\Http\HttpClientInterface;
18 use Embera\Html\IgnoreTags;
19 use Embera\Html\ResponsiveEmbeds;
20 use Embera\Provider\ProviderInterface;
21 use Embera\ProviderCollection\ProviderCollectionInterface;
22 use Embera\ProviderCollection\DefaultProviderCollection;
23
24 /**
25 * Embera
26 * The Main Class of this library.
27 */
28 class Embera
29 {
30 /** @var string Current Library Version */
31 const VERSION = '2.0.16';
32
33 /**
34 * Constants describing how the library is
35 * going to handle fake responses.
36 *
37 * ALLOW_FAKE_RESPONSES is the default value.
38 */
39 const ALLOW_FAKE_RESPONSES = 1;
40 const DISABLE_FAKE_RESPONSES = 2;
41 const ONLY_FAKE_RESPONSES = 3;
42
43 /** @var array Configuration settings */
44 protected $config = [];
45
46 /** @var array Logged errors */
47 protected $errors = [];
48
49 /** @var array Closures to be used on oembed responses */
50 protected $filters = [];
51
52 /** @var ProviderCollectionInterface */
53 protected $providerCollection;
54
55 /** @var HttpClientInterface */
56 protected $httpClient;
57
58 /**
59 * Constructor
60 *
61 * @param array $config
62 * @param ProviderCollectionInterface $collection
63 * @param HttpClientInterface $httpClient
64 * @return void
65 */
66 public function __construct(array $config = [], ProviderCollectionInterface $collection = null, HttpClientInterface $httpClient = null)
67 {
68 $this->providerCollection = $collection;
69 if (!$collection) {
70 $this->providerCollection = new DefaultProviderCollection();
71 }
72
73 $this->httpClient = $httpClient;
74 if (!$httpClient) {
75 $this->httpClient = new HttpClient();
76 }
77 $this->setConfig($config);
78 }
79
80 public function setConfig(array $config = [])
81 {
82 $this->config = array_merge([
83 'https_only' => false,
84 'fake_responses' => self::ALLOW_FAKE_RESPONSES,
85 'ignore_tags' => [ 'pre', 'code', 'a', 'img', 'iframe', 'oembed' ],
86 'responsive' => false,
87 'width' => 0,
88 'height' => 0,
89 'maxheight' => 0,
90 'maxwidth' => 0,
91 ], $config);
92
93 $this->config['maxwidth'] = max($this->config['width'], $this->config['maxwidth']);
94 $this->config['maxheight'] = max($this->config['height'], $this->config['maxheight']);
95 unset($this->config['height'], $this->config['width']);
96
97
98 // Set the config just in case.
99 $this->providerCollection->setConfig($this->config);
100 $this->httpClient->setConfig($this->config);
101 }
102
103 /**
104 * Embeds known/available services into the given text.
105 *
106 * @param string $text
107 * @return string
108 */
109 public function autoEmbed($text)
110 {
111 if (is_string($text)) {
112 $table = [];
113 $providers = $this->getUrlData($text);
114 foreach ($providers as $url => $response) {
115 if (!empty($response['html'])) {
116 $table[$url] = $response['html'];
117 }
118 }
119
120 if (!empty($table)) {
121
122 if (!empty($this->config['ignore_tags']) && strpos($text, '>') !== false) {
123 $ignoreTags = new IgnoreTags($this->config['ignore_tags']);
124 return $ignoreTags->replace($text, $table);
125 }
126
127 return strtr($text, $table);
128 }
129
130 } else {
131 $this->errors[] = 'For auto-embedding purposes, the input must be a string';
132 }
133
134 return $text;
135 }
136
137 /**
138 * Returns the oembed response from the given data.
139 *
140 * @param array|string $urls An array with urls or a string with urls.
141 * @return array
142 */
143 public function getUrlData($urls)
144 {
145 $return = [];
146 /**
147 * @var string $url
148 * @var ProviderInterface $provider
149 */
150 foreach ($this->providerCollection->findProviders($urls) as $url => $provider) {
151 try {
152 if ( $provider->shouldSendRequest() ) {
153 $oembedClient = new OembedClient($this->config, $this->httpClient);
154 $response = $oembedClient->getResponseFrom($provider);
155 }else{
156 $response = $provider->getStaticResponse();
157 }
158
159
160 if ($this->config['responsive'] && !$provider->hasResponsiveSupport()) {
161 $responsive = new ResponsiveEmbeds();
162 $response = $responsive->transform($response);
163 }
164
165
166 $return[$url] = $this->applyFilters($response);
167
168 } catch (\Exception $e) {
169 $this->errors[] = $e->getMessage();
170 }
171 }
172
173 return array_filter($return);
174 }
175
176 /**
177 * Adds a filter to the oembed response
178 *
179 * @param callable $closure
180 * @return void
181 */
182 public function addFilter(callable $closure)
183 {
184 $this->filters[] = $closure;
185 }
186
187 /**
188 * Applies registered filters/closures
189 * to the oembed response.
190 *
191 * @param array $response
192 * @return array
193 */
194 protected function applyFilters(array $response)
195 {
196 foreach ($this->filters as $closure) {
197 $response = $closure($response);
198 }
199
200 return $response;
201 }
202
203 /**
204 * Gets the last error found
205 *
206 * @return string
207 */
208 public function getLastError()
209 {
210 return end($this->errors);
211 }
212
213 /**
214 * Returns an array with all the errors
215 *
216 * @return array
217 */
218 public function getErrors()
219 {
220 return $this->errors;
221 }
222
223 /**
224 * Checks if there were errors
225 *
226 * @return bool
227 */
228 public function hasErrors()
229 {
230 return (!empty($this->errors));
231 }
232
233 }
234