PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 3.3.7
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v3.3.7
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 4 years ago ProviderCollection 5 years ago Embera.php 4 years ago Url.php 5 years ago
Embera.php
229 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->config = array_merge([
69 'https_only' => false,
70 'fake_responses' => self::ALLOW_FAKE_RESPONSES,
71 'ignore_tags' => [ 'pre', 'code', 'a', 'img', 'iframe', 'oembed' ],
72 'responsive' => false,
73 'width' => 0,
74 'height' => 0,
75 'maxheight' => 0,
76 'maxwidth' => 0,
77 ], $config);
78
79 $this->config['maxwidth'] = max($this->config['width'], $this->config['maxwidth']);
80 $this->config['maxheight'] = max($this->config['height'], $this->config['maxheight']);
81 unset($this->config['height'], $this->config['width']);
82
83 $this->providerCollection = $collection;
84 if (!$collection) {
85 $this->providerCollection = new DefaultProviderCollection($this->config);
86 }
87
88 $this->httpClient = $httpClient;
89 if (!$httpClient) {
90 $this->httpClient = new HttpClient($this->config);
91 }
92
93 // Set the config just in case.
94 $this->providerCollection->setConfig($this->config);
95 $this->httpClient->setConfig($this->config);
96 }
97
98 /**
99 * Embeds known/available services into the given text.
100 *
101 * @param string $text
102 * @return string
103 */
104 public function autoEmbed($text)
105 {
106 if (is_string($text)) {
107 $table = [];
108 $providers = $this->getUrlData($text);
109 foreach ($providers as $url => $response) {
110 if (!empty($response['html'])) {
111 $table[$url] = $response['html'];
112 }
113 }
114
115 if (!empty($table)) {
116
117 if (!empty($this->config['ignore_tags']) && strpos($text, '>') !== false) {
118 $ignoreTags = new IgnoreTags($this->config['ignore_tags']);
119 return $ignoreTags->replace($text, $table);
120 }
121
122 return strtr($text, $table);
123 }
124
125 } else {
126 $this->errors[] = 'For auto-embedding purposes, the input must be a string';
127 }
128
129 return $text;
130 }
131
132 /**
133 * Returns the oembed response from the given data.
134 *
135 * @param array|string $urls An array with urls or a string with urls.
136 * @return array
137 */
138 public function getUrlData($urls)
139 {
140 $return = [];
141 /**
142 * @var string $url
143 * @var ProviderInterface $provider
144 */
145 foreach ($this->providerCollection->findProviders($urls) as $url => $provider) {
146 try {
147 if ( $provider->shouldSendRequest() ) {
148 $oembedClient = new OembedClient($this->config, $this->httpClient);
149 $response = $oembedClient->getResponseFrom($provider);
150 }else{
151 $response = $provider->getStaticResponse();
152 }
153
154
155 if ($this->config['responsive'] && !$provider->hasResponsiveSupport()) {
156 $responsive = new ResponsiveEmbeds();
157 $response = $responsive->transform($response);
158 }
159
160
161 $return[$url] = $this->applyFilters($response);
162
163 } catch (\Exception $e) {
164 $this->errors[] = $e->getMessage();
165 }
166 }
167
168 return array_filter($return);
169 }
170
171 /**
172 * Adds a filter to the oembed response
173 *
174 * @param callable $closure
175 * @return void
176 */
177 public function addFilter(callable $closure)
178 {
179 $this->filters[] = $closure;
180 }
181
182 /**
183 * Applies registered filters/closures
184 * to the oembed response.
185 *
186 * @param array $response
187 * @return array
188 */
189 protected function applyFilters(array $response)
190 {
191 foreach ($this->filters as $closure) {
192 $response = $closure($response);
193 }
194
195 return $response;
196 }
197
198 /**
199 * Gets the last error found
200 *
201 * @return string
202 */
203 public function getLastError()
204 {
205 return end($this->errors);
206 }
207
208 /**
209 * Returns an array with all the errors
210 *
211 * @return array
212 */
213 public function getErrors()
214 {
215 return $this->errors;
216 }
217
218 /**
219 * Checks if there were errors
220 *
221 * @return bool
222 */
223 public function hasErrors()
224 {
225 return (!empty($this->errors));
226 }
227
228 }
229