PluginProbe
Embed Privacy / 1.10.0
Embed Privacy v1.10.0
1.14.0 1.13.0 trunk 0.1 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.10 1.10.2 1.10.3 1.10.4 1.10.5 1.10.6 1.10.7 1.10.8 1.10.9 1.11.0 1.11.1 1.11.2 All 68 releases
embed-privacy / inc / integration / class-twitter.php

class-twitter.php in Embed Privacy 1.10.0, at inc/integration/class-twitter.php

88 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace epiphyt\Embed_Privacy\integration;
3
4 use DOMDocument;
5 use DOMElement;
6 use DOMXPath;
7
8 /**
9 * Twitter integration for Embed Privacy.
10 *
11 * @author Epiphyt
12 * @license GPL2
13 * @package epiphyt\Embed_Privacy
14 * @since 1.10.0
15 */
16 final class Twitter {
17 /**
18 * Transform a tweet into a local one.
19 *
20 * @param string $html Embed code
21 * @return string Local embed
22 */
23 public static function get_local_tweet( $html ) {
24 \libxml_use_internal_errors( true );
25 $dom = new DOMDocument();
26 $dom->loadHTML(
27 '<html><meta charset="utf-8">' . $html . '</html>',
28 \LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD
29 );
30
31 // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
32 // remove script tag
33 foreach ( $dom->getElementsByTagName( 'script' ) as $script ) {
34 $script->parentNode->removeChild( $script );
35 }
36
37 $xpath = new DOMXPath( $dom );
38
39 // get text node, which represents the author name
40 // and give it a span with class
41 foreach ( $xpath->query( '//blockquote/text()' ) as $node ) {
42 $author_node = $dom->createElement( 'span', $node->nodeValue );
43 $author_node->setAttribute( 'class', 'embed-privacy-author-meta' );
44 $node->parentNode->replaceChild( $author_node, $node );
45 }
46
47 // wrap author name by a meta div
48 /** @var \DOMElement $node */
49 foreach ( $dom->getElementsByTagName( 'span' ) as $node ) {
50 if ( $node->getAttribute( 'class' ) !== 'embed-privacy-author-meta' ) {
51 continue;
52 }
53
54 // create meta cite
55 $parent_node = $dom->createElement( 'cite' );
56 $parent_node->setAttribute( 'class', 'embed-privacy-tweet-meta' );
57 // append created cite to blockquote
58 $node->parentNode->appendChild( $parent_node );
59 // move author meta inside meta cite
60 $parent_node->appendChild( $node );
61 }
62
63 /** @var \DOMElement $link */
64 foreach ( $dom->getElementsByTagName( 'a' ) as $link ) {
65 if ( ! \preg_match( '/https?:\/\/twitter.com\/([^\/]+)\/status\/(\d+)/', $link->getAttribute( 'href' ) ) ) {
66 continue;
67 }
68
69 // modify date in link to tweet
70 $l10n_date = \wp_date( \get_option( 'date_format' ), \strtotime( $link->nodeValue ) );
71
72 if ( \is_string( $l10n_date ) ) {
73 $link->nodeValue = $l10n_date;
74 }
75
76 // move link inside meta div
77 if ( isset( $parent_node ) && $parent_node instanceof DOMElement ) {
78 $parent_node->appendChild( $link );
79 }
80 }
81
82 $content = $dom->saveHTML( $dom->documentElement );
83 // phpcs:enable
84
85 return \str_replace( [ '<html><meta charset="utf-8">', '</html>' ], [ '<div class="embed-privacy-local-tweet">', '</div>' ], $content );
86 }
87 }
88