PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents / 2.7.2
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents v2.7.2
4.6.1 4.6.0 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 5 years ago ProviderCollection 5 years ago Embera.php 5 years ago Url.php 5 years ago
Embera.php
219 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\ProviderCollection\ProviderCollectionInterface;
21 use Embera\ProviderCollection\DefaultProviderCollection;
22
23 /**
24 * Embera
25 * The Main Class of this library.
26 */
27 class Embera
28 {
29 /** @var string Current Library Version */
30 const VERSION = '2.0.16';
31
32 /**
33 * Constants describing how the library is
34 * going to handle fake responses.
35 *
36 * ALLOW_FAKE_RESPONSES is the default value.
37 */
38 const ALLOW_FAKE_RESPONSES = 1;
39 const DISABLE_FAKE_RESPONSES = 2;
40 const ONLY_FAKE_RESPONSES = 3;
41
42 /** @var array Configuration settings */
43 protected $config = [];
44
45 /** @var array Logged errors */
46 protected $errors = [];
47
48 /** @var array Closures to be used on oembed responses */
49 protected $filters = [];
50
51 /** @var ProviderCollectionInterface */
52 protected $providerCollection;
53
54 /** @var HttpClientInterface */
55 protected $httpClient;
56
57 /**
58 * Constructor
59 *
60 * @param array $config
61 * @param ProviderCollectionInterface $collection
62 * @param HttpClientInterface $httpClient
63 * @return void
64 */
65 public function __construct(array $config = [], ProviderCollectionInterface $collection = null, HttpClientInterface $httpClient = null)
66 {
67 $this->config = array_merge([
68 'https_only' => false,
69 'fake_responses' => self::ALLOW_FAKE_RESPONSES,
70 'ignore_tags' => [ 'pre', 'code', 'a', 'img', 'iframe', 'oembed' ],
71 'responsive' => false,
72 'width' => 0,
73 'height' => 0,
74 'maxheight' => 0,
75 'maxwidth' => 0,
76 ], $config);
77
78 $this->config['maxwidth'] = max($this->config['width'], $this->config['maxwidth']);
79 $this->config['maxheight'] = max($this->config['height'], $this->config['maxheight']);
80 unset($this->config['height'], $this->config['width']);
81
82 $this->providerCollection = $collection;
83 if (!$collection) {
84 $this->providerCollection = new DefaultProviderCollection($this->config);
85 }
86
87 $this->httpClient = $httpClient;
88 if (!$httpClient) {
89 $this->httpClient = new HttpClient($this->config);
90 }
91
92 // Set the config just in case.
93 $this->providerCollection->setConfig($this->config);
94 $this->httpClient->setConfig($this->config);
95 }
96
97 /**
98 * Embeds known/available services into the given text.
99 *
100 * @param string $text
101 * @return string
102 */
103 public function autoEmbed($text)
104 {
105 if (is_string($text)) {
106 $table = [];
107 $providers = $this->getUrlData($text);
108 foreach ($providers as $url => $response) {
109 if (!empty($response['html'])) {
110 $table[$url] = $response['html'];
111 }
112 }
113
114 if (!empty($table)) {
115
116 if (!empty($this->config['ignore_tags']) && strpos($text, '>') !== false) {
117 $ignoreTags = new IgnoreTags($this->config['ignore_tags']);
118 return $ignoreTags->replace($text, $table);
119 }
120
121 return strtr($text, $table);
122 }
123
124 } else {
125 $this->errors[] = 'For auto-embedding purposes, the input must be a string';
126 }
127
128 return $text;
129 }
130
131 /**
132 * Returns the oembed response from the given data.
133 *
134 * @param array|string $urls An array with urls or a string with urls.
135 * @return array
136 */
137 public function getUrlData($urls)
138 {
139 $return = [];
140 foreach ($this->providerCollection->findProviders($urls) as $url => $provider) {
141 try {
142
143 $oembedClient = new OembedClient($this->config, $this->httpClient);
144 $response = $oembedClient->getResponseFrom($provider);
145
146 if ($this->config['responsive'] && !$provider->hasResponsiveSupport()) {
147 $responsive = new ResponsiveEmbeds();
148 $response = $responsive->transform($response);
149 }
150
151 $return[$url] = $this->applyFilters($response);
152
153 } catch (\Exception $e) {
154 $this->errors[] = $e->getMessage();
155 }
156 }
157
158 return array_filter($return);
159 }
160
161 /**
162 * Adds a filter to the oembed response
163 *
164 * @param callable $closure
165 * @return void
166 */
167 public function addFilter(callable $closure)
168 {
169 $this->filters[] = $closure;
170 }
171
172 /**
173 * Applies registered filters/closures
174 * to the oembed response.
175 *
176 * @param array $response
177 * @return array
178 */
179 protected function applyFilters(array $response)
180 {
181 foreach ($this->filters as $closure) {
182 $response = $closure($response);
183 }
184
185 return $response;
186 }
187
188 /**
189 * Gets the last error found
190 *
191 * @return string
192 */
193 public function getLastError()
194 {
195 return end($this->errors);
196 }
197
198 /**
199 * Returns an array with all the errors
200 *
201 * @return array
202 */
203 public function getErrors()
204 {
205 return $this->errors;
206 }
207
208 /**
209 * Checks if there were errors
210 *
211 * @return bool
212 */
213 public function hasErrors()
214 {
215 return (!empty($this->errors));
216 }
217
218 }
219