C:\xampp\htdocs\landing\wp-content\plugins\amp\includes\embeds\class-amp-twitter-embed-handler.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
 * Class AMP_Twitter_Embed_Handler
 *
 * @package AMP
 */

use AmpProject\Dom\Document;

/**
 * Class AMP_Twitter_Embed_Handler
 *
 * Much of this class is borrowed from Jetpack embeds
 *
 * @internal
 */
class AMP_Twitter_Embed_Handler extends AMP_Base_Embed_Handler {

    
/**
     * URL pattern for a Tweet URL.
     *
     * @since 0.2
     * @var string
     */
    
const URL_PATTERN '#https?:\/\/twitter\.com(?:\/\#\!\/|\/)(?P<username>[a-zA-Z0-9_]{1,20})\/status(?:es)?\/(?P<tweet>\d+)#i';

    
/**
     * URL pattern for a Twitter timeline.
     *
     * @since 1.0
     * @var string
     */
    
const URL_PATTERN_TIMELINE '#https?:\/\/twitter\.com(?:\/\#\!\/|\/)(?P<username>[a-zA-Z0-9_]{1,20})(?:$|\/(?P<type>likes|lists)(\/(?P<id>[a-zA-Z0-9_-]+))?)#i';

    
/**
     * Tag.
     *
     * @var string embed HTML blockquote tag to identify and replace with AMP version.
     */
    
protected $sanitize_tag 'blockquote';

    
/**
     * Tag.
     *
     * @var string AMP amp-facebook tag
     */
    
private $amp_tag 'amp-twitter';

    
/**
     * Registers embed.
     */
    
public function register_embed() {
        
wp_embed_register_handler'amp-twitter-timeline'self::URL_PATTERN_TIMELINE, [ $this'oembed_timeline' ], -);
    }

    
/**
     * Unregisters embed.
     */
    
public function unregister_embed() {
        
wp_embed_unregister_handler'amp-twitter-timeline', -);
    }

    
/**
     * Render oEmbed for a timeline.
     *
     * @since 1.0
     *
     * @param array $matches URL pattern matches.
     * @return string Rendered oEmbed.
     */
    
public function oembed_timeline$matches ) {
        if ( ! isset( 
$matches['username'] ) ) {
            return 
'';
        }

        
$attributes = [
            
'data-timeline-source-type' => 'profile',
            
'data-timeline-screen-name' => $matches['username'],
        ];

        if ( isset( 
$matches['type'] ) ) {
            switch ( 
$matches['type'] ) {
                case 
'likes':
                    
$attributes['data-timeline-source-type'] = 'likes';
                    break;
                case 
'lists':
                    if ( ! isset( 
$matches['id'] ) ) {
                        return 
'';
                    }
                    
$attributes['data-timeline-source-type']       = 'list';
                    
$attributes['data-timeline-slug']              = $matches['id'];
                    
$attributes['data-timeline-owner-screen-name'] = $attributes['data-timeline-screen-name'];
                    unset( 
$attributes['data-timeline-screen-name'] );
                    break;
                default:
                    return 
'';
            }
        }

        
$attributes['layout'] = 'responsive';
        
$attributes['width']  = $this->args['width'];
        
$attributes['height'] = $this->args['height'];

        
$this->did_convert_elements true;

        return 
AMP_HTML_Utils::build_tag$this->amp_tag$attributes );
    }

    
/**
     * Sanitized <blockquote class="twitter-tweet"> tags to <amp-twitter>.
     *
     * @param Document $dom DOM.
     */
    
public function sanitize_raw_embedsDocument $dom ) {
        
$nodes     $dom->getElementsByTagName$this->sanitize_tag );
        
$num_nodes $nodes->length;

        if ( 
=== $num_nodes ) {
            return;
        }

        for ( 
$i $num_nodes 1$i >= 0$i-- ) {
            
$node $nodes->item$i );
            if ( ! 
$node instanceof DOMElement ) {
                continue;
            }

            if ( 
$this->is_tweet_raw_embed$node ) ) {
                
$this->create_amp_twitter_and_replace_node$dom$node );
            }
        }
    }

    
/**
     * Checks whether it's a twitter blockquote or not.
     *
     * @param DOMElement $node The DOMNode to adjust and replace.
     * @return bool Whether node is for raw embed.
     */
    
private function is_tweet_raw_embed$node ) {
        
// Skip processing blockquotes that have already been passed through while being wrapped with <amp-twitter>.
        
if ( $node->parentNode && 'amp-twitter' === $node->parentNode->nodeName ) {
            return 
false;
        }

        
$class_attr $node->getAttribute'class' );

        return 
null !== $class_attr && false !== strpos$class_attr'twitter-tweet' );
    }

    
/**
     * Make final modifications to DOMNode
     *
     * @param Document   $dom The HTML Document.
     * @param DOMElement $node The DOMNode to adjust and replace.
     */
    
private function create_amp_twitter_and_replace_nodeDocument $domDOMElement $node ) {
        
$tweet_id $this->get_tweet_id$node );
        if ( empty( 
$tweet_id ) ) {
            return;
        }

        
$attributes = [
            
'width'        => $this->DEFAULT_WIDTH,
            
'height'       => $this->DEFAULT_HEIGHT,
            
'layout'       => 'responsive',
            
'data-tweetid' => $tweet_id,
        ];

        if ( 
$node->hasAttributes() ) {
            foreach ( 
$node->attributes as $attr ) {
                
$attributes$attr->nodeName ] = $attr->nodeValue;
            }
        }

        
$new_node AMP_DOM_Utils::create_node(
            
$dom,
            
$this->amp_tag,
            
$attributes
        
);

        
/**
         * Placeholder element to append to the new node.
         *
         * @var DOMElement $placeholder
         */
        
$placeholder $node->cloneNodetrue );
        
$placeholder->setAttribute'placeholder''' );

        
$new_node->appendChild$placeholder );

        
$this->sanitize_embed_script$node );

        
$node->parentNode->replaceChild$new_node$node );

        
$this->did_convert_elements true;
    }

    
/**
     * Extracts Tweet id.
     *
     * @param DOMElement $node The DOMNode to adjust and replace.
     * @return string Tweet ID, or an empty string if not found.
     */
    
private function get_tweet_id$node ) {
        
/**
         * DOMNode
         *
         * @var DOMNodeList $anchors
         */
        
$anchors $node->getElementsByTagName'a' );

        
/**
         * Anchor.
         *
         * @var DOMElement $anchor
         */
        
foreach ( $anchors as $anchor ) {
            
$found preg_matchself::URL_PATTERN$anchor->getAttribute'href' ), $matches );
            if ( 
$found ) {
                return 
$matches['tweet'];
            }
        }

        return 
'';
    }

    
/**
     * Removes Twitter's embed <script> tag.
     *
     * @param DOMElement $node The DOMNode to whose sibling is the Twitter script.
     */
    
private function sanitize_embed_script$node ) {
        
$next_element_sibling $node->nextSibling;
        while ( 
$next_element_sibling && ! ( $next_element_sibling instanceof DOMElement ) ) {
            
$next_element_sibling $next_element_sibling->nextSibling;
        }

        
$script_src 'platform.twitter.com/widgets.js';

        
// Handle case where script is wrapped in paragraph by wpautop.
        
if ( $next_element_sibling instanceof DOMElement && 'p' === $next_element_sibling->nodeName ) {
            
$children $next_element_sibling->getElementsByTagName'*' );
            if ( 
=== $children->length && 'script' === $children->item)->nodeName && false !== strpos$children->item)->getAttribute'src' ), $script_src ) ) {
                
$next_element_sibling->parentNode->removeChild$next_element_sibling );
                return;
            }
        }

        
// Handle case where script is immediately following.
        
$is_embed_script = (
            
$next_element_sibling instanceof DOMElement
            
&&
            
'script' === strtolower$next_element_sibling->nodeName )
            &&
            
false !== strpos$next_element_sibling->getAttribute'src' ), $script_src )
        );
        if ( 
$is_embed_script ) {
            
$next_element_sibling->parentNode->removeChild$next_element_sibling );
        }
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586