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