-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See #235
- Loading branch information
Showing
8 changed files
with
164 additions
and
96 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,13 @@ | ||
<?php | ||
namespace epiphyt\Embed_Privacy\integration; | ||
|
||
use DOMDocument; | ||
use DOMElement; | ||
use DOMXPath; | ||
|
||
/** | ||
* Twitter integration for Embed Privacy. | ||
* Twitter/X integration for Embed Privacy. | ||
* | ||
* @author Epiphyt | ||
* @license GPL2 | ||
* @package epiphyt\Embed_Privacy | ||
* @since 1.10.0 | ||
* @author Epiphyt | ||
* @deprecated 1.10.5 Use epiphyt\Embed_Privacy\integration\X instead | ||
* @license GPL2 | ||
* @package epiphyt\Embed_Privacy | ||
* @since 1.10.0 | ||
*/ | ||
final class Twitter { | ||
/** | ||
* Transform a tweet into a local one. | ||
* | ||
* @param string $html Embed code | ||
* @return string Local embed | ||
*/ | ||
public static function get_local_tweet( $html ) { | ||
\libxml_use_internal_errors( true ); | ||
$dom = new DOMDocument(); | ||
$dom->loadHTML( | ||
'<html><meta charset="utf-8">' . $html . '</html>', | ||
\LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD | ||
); | ||
|
||
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | ||
// remove script tag | ||
foreach ( $dom->getElementsByTagName( 'script' ) as $script ) { | ||
$script->parentNode->removeChild( $script ); | ||
} | ||
|
||
$xpath = new DOMXPath( $dom ); | ||
|
||
// get text node, which represents the author name | ||
// and give it a span with class | ||
foreach ( $xpath->query( '//blockquote/text()' ) as $node ) { | ||
$author_node = $dom->createElement( 'span', $node->nodeValue ); | ||
$author_node->setAttribute( 'class', 'embed-privacy-author-meta' ); | ||
$node->parentNode->replaceChild( $author_node, $node ); | ||
} | ||
|
||
// wrap author name by a meta div | ||
/** @var \DOMElement $node */ | ||
foreach ( $dom->getElementsByTagName( 'span' ) as $node ) { | ||
if ( $node->getAttribute( 'class' ) !== 'embed-privacy-author-meta' ) { | ||
continue; | ||
} | ||
|
||
// create meta cite | ||
$parent_node = $dom->createElement( 'cite' ); | ||
$parent_node->setAttribute( 'class', 'embed-privacy-tweet-meta' ); | ||
// append created cite to blockquote | ||
$node->parentNode->appendChild( $parent_node ); | ||
// move author meta inside meta cite | ||
$parent_node->appendChild( $node ); | ||
} | ||
|
||
/** @var \DOMElement $link */ | ||
foreach ( $dom->getElementsByTagName( 'a' ) as $link ) { | ||
if ( ! \preg_match( '/https?:\/\/twitter.com\/([^\/]+)\/status\/(\d+)/', $link->getAttribute( 'href' ) ) ) { | ||
continue; | ||
} | ||
|
||
// modify date in link to tweet | ||
$l10n_date = \wp_date( \get_option( 'date_format' ), \strtotime( $link->nodeValue ) ); | ||
|
||
if ( \is_string( $l10n_date ) ) { | ||
$link->nodeValue = $l10n_date; | ||
} | ||
|
||
// move link inside meta div | ||
if ( isset( $parent_node ) && $parent_node instanceof DOMElement ) { | ||
$parent_node->appendChild( $link ); | ||
} | ||
} | ||
|
||
$content = $dom->saveHTML( $dom->documentElement ); | ||
// phpcs:enable | ||
|
||
return \str_replace( [ '<html><meta charset="utf-8">', '</html>' ], [ '<div class="embed-privacy-local-tweet">', '</div>' ], $content ); | ||
} | ||
} | ||
final class Twitter extends X {} |
Oops, something went wrong.