CodePen.php
| 1 | <?php |
| 2 | /** |
| 3 | * CodePen.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\Provider; |
| 14 | |
| 15 | use Embera\Url; |
| 16 | |
| 17 | /** |
| 18 | * CodePen Provider |
| 19 | * @link https://codepen.io |
| 20 | */ |
| 21 | class CodePen extends ProviderAdapter implements ProviderInterface |
| 22 | { |
| 23 | /** inline {@inheritdoc} */ |
| 24 | protected $endpoint = 'http://codepen.io/api/oembed?format=json'; |
| 25 | |
| 26 | /** inline {@inheritdoc} */ |
| 27 | protected static $hosts = [ |
| 28 | 'codepen.io' |
| 29 | ]; |
| 30 | |
| 31 | /** inline {@inheritdoc} */ |
| 32 | protected $httpsSupport = true; |
| 33 | |
| 34 | /** inline {@inheritdoc} */ |
| 35 | protected $responsiveSupport = true; |
| 36 | |
| 37 | /** inline {@inheritdoc} */ |
| 38 | public function validateUrl(Url $url) |
| 39 | { |
| 40 | return (bool) (preg_match('~codepen\.io/(?:[^/]+)/pen/(?:[^/]+)~i', (string) $url)); |
| 41 | } |
| 42 | |
| 43 | /** inline {@inheritdoc} */ |
| 44 | public function normalizeUrl(Url $url) |
| 45 | { |
| 46 | $url->convertToHttps(); |
| 47 | $url->removeQueryString(); |
| 48 | $url->removeLastSlash(); |
| 49 | return $url; |
| 50 | } |
| 51 | |
| 52 | /** inline {@inheritdoc} */ |
| 53 | public function getFakeResponse() |
| 54 | { |
| 55 | preg_match('~codepen\.io/([^/]+)/pen/([^/]+)~i', (string) $this->url, $matches); |
| 56 | |
| 57 | $embedUrl = 'https://codepen.io/' . $matches['1']. '/embed/preview/' . $matches['2'] . '?height=300&slug-hash=' . $matches['2'] . '&default-tabs=html,result&host=https://codepen.io'; |
| 58 | |
| 59 | $attr = []; |
| 60 | $attr[] = 'id="cp_embed_' . $matches['2'] . '"'; |
| 61 | $attr[] = 'src="' . $embedUrl . '"'; |
| 62 | $attr[] = 'title=""'; |
| 63 | $attr[] = 'scrolling="no"'; |
| 64 | $attr[] = 'frameborder="0"'; |
| 65 | $attr[] = 'height="{height}"'; |
| 66 | $attr[] = 'allowtransparency="true"'; |
| 67 | $attr[] = 'class="cp_embed_iframe"'; |
| 68 | $attr[] = 'style="width: 100%; overflow: hidden;"'; |
| 69 | |
| 70 | return [ |
| 71 | 'type' => 'rich', |
| 72 | 'provider_name' => 'CodePen', |
| 73 | 'provider_url' => 'https://codepen.io', |
| 74 | 'title' => 'Unknown title', |
| 75 | 'html' => '<iframe ' . implode(' ', $attr). '></iframe>', |
| 76 | ]; |
| 77 | } |
| 78 | |
| 79 | } |
| 80 |