Twitter.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\AMP\Adapter; |
| 4 | |
| 5 | |
| 6 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 7 | |
| 8 | /** |
| 9 | * Entity that represents the embed provider for AMP. |
| 10 | * |
| 11 | * @package EmbedPress |
| 12 | * @author EmbedPress <help@embedpress.com> |
| 13 | * @copyright Copyright (C) 2020 WPDeveloper. All rights reserved. |
| 14 | * @license GPLv3 or later |
| 15 | * @since 1.4.0 |
| 16 | * @abstract |
| 17 | */ |
| 18 | class Twitter |
| 19 | { |
| 20 | /** |
| 21 | * @var object |
| 22 | */ |
| 23 | private $ampEmbedHandler; |
| 24 | |
| 25 | /** |
| 26 | * @var object |
| 27 | */ |
| 28 | private $urlData; |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * @var string |
| 33 | */ |
| 34 | private $parsedContent; |
| 35 | |
| 36 | /** |
| 37 | * @var array |
| 38 | */ |
| 39 | private $attributes; |
| 40 | |
| 41 | /** |
| 42 | * The constructor. |
| 43 | * |
| 44 | * @param object $urlData |
| 45 | */ |
| 46 | public function __construct($parsedContent, $urlData, $attributes) |
| 47 | { |
| 48 | if (class_exists('AMP_Twitter_Embed_Handler')) { |
| 49 | $this->ampEmbedHandler = new \AMP_Twitter_Embed_Handler; |
| 50 | |
| 51 | add_action('amp_post_template_head', [$this, 'addScripts']); |
| 52 | } |
| 53 | |
| 54 | $this->parsedContent = $parsedContent; |
| 55 | $this->urlData = $urlData; |
| 56 | $this->attributes = $attributes; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Convert the HTML for AMP compatible tag. |
| 61 | * |
| 62 | * @return string |
| 63 | */ |
| 64 | public function process() |
| 65 | { |
| 66 | // Check we have the adapter set |
| 67 | if ( ! isset($this->ampEmbedHandler)) { |
| 68 | return $this->parsedContent; |
| 69 | } |
| 70 | |
| 71 | $attr = [ |
| 72 | 'tweet' => $this->urlData->url, |
| 73 | ]; |
| 74 | |
| 75 | $parsedContent = $this->ampEmbedHandler->shortcode($attr); |
| 76 | |
| 77 | return $parsedContent; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Add scripts to the output. |
| 82 | */ |
| 83 | public function addScripts() |
| 84 | { |
| 85 | if ( ! defined('PPEMB_TWITTER_AMP_SCRIPT_LOADED')) { |
| 86 | echo '<script async custom-element="amp-twitter" src="https://cdn.ampproject.org/v0/amp-twitter-0.1.js"></script>'; |
| 87 | |
| 88 | define('PPEMB_TWITTER_AMP_SCRIPT_LOADED', 1); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 |