Skip to content

Commit

Permalink
🚚 Rename Twitter to X
Browse files Browse the repository at this point in the history
See #235
  • Loading branch information
MatzeKitt committed Sep 27, 2024
1 parent 4d1f404 commit 0eeccfb
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 96 deletions.
Binary file removed assets/images/embed-twitter.png
Binary file not shown.
Binary file added assets/images/embed-x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion inc/admin/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function register() {
'embed_privacy',
'embed_privacy_general',
[
'description' => \__( 'By enabling this option, tweets are embedded locally as text without any connection to Twitter, and no privacy overlay is required.', 'embed-privacy' ),
'description' => \__( 'By enabling this option, tweets are embedded locally as text without any connection to X, and no privacy overlay is required.', 'embed-privacy' ),
'name' => 'embed_privacy_local_tweets',
'option_type' => 'option',
'title' => \__( 'Local tweets', 'embed-privacy' ),
Expand Down
10 changes: 5 additions & 5 deletions inc/class-embed-privacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use epiphyt\Embed_Privacy\integration\Maps_Marker;
use epiphyt\Embed_Privacy\integration\Polylang;
use epiphyt\Embed_Privacy\integration\Shortcodes_Ultimate;
use epiphyt\Embed_Privacy\integration\Twitter;
use epiphyt\Embed_Privacy\integration\X;
use epiphyt\Embed_Privacy\thumbnail\Thumbnail;
use ReflectionMethod;

Expand Down Expand Up @@ -91,7 +91,7 @@ class Embed_Privacy {
Maps_Marker::class,
Polylang::class,
Shortcodes_Ultimate::class,
Twitter::class,
X::class,
];

/**
Expand Down Expand Up @@ -978,7 +978,7 @@ public function replace_embeds_divi( $item_embed, $url ) { // phpcs:ignore Gener
}

/**
* Replace twitter embeds.
* Replace X embeds.
*
* @deprecated 1.6.3
* @since 1.6.1
Expand All @@ -1000,7 +1000,7 @@ public function replace_embeds_twitter( $output, $url, $args ) {
return $output;
}

$provider = Providers::get_instance()->get_by_name( 'twitter' );
$provider = Providers::get_instance()->get_by_name( 'x' );

if ( ! $provider->is_matching( $url ) ) {
return $output;
Expand All @@ -1012,7 +1012,7 @@ public function replace_embeds_twitter( $output, $url, $args ) {

if ( \get_option( 'embed_privacy_local_tweets' ) ) {
// check for local tweets
return Twitter::get_local_tweet( $output );
return X::get_local_tweet( $output );
}

$args['embed_url'] = $url;
Expand Down
64 changes: 58 additions & 6 deletions inc/class-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Migration {
* @var string Current migration version
* @since 1.2.2
*/
private $version = '1.8.0';
private $version = '1.10.5';

/**
* Migration constructor.
Expand Down Expand Up @@ -196,45 +196,56 @@ public function migrate( $deprecated = null, $deprecated2 = null ) {
case $this->version:
// most recent version, do nothing
break;
case '1.8.0':
$this->migrate_1_10_5();
break;
case '1.7.3':
$this->migrate_1_10_5();
$this->migrate_1_8_0();
break;
case '1.7.0':
$this->migrate_1_10_5();
$this->migrate_1_8_0();
$this->migrate_1_7_3();
break;
case '1.6.0':
$this->migrate_1_10_5();
$this->migrate_1_8_0();
$this->migrate_1_7_3();
$this->migrate_1_7_0();
break;
case '1.5.0':
$this->migrate_1_10_5();
$this->migrate_1_8_0();
$this->migrate_1_7_3();
$this->migrate_1_7_0();
$this->migrate_1_6_0();
break;
case '1.4.7':
$this->migrate_1_10_5();
$this->migrate_1_8_0();
$this->migrate_1_7_0();
$this->migrate_1_6_0();
$this->migrate_1_5_0();
break;
case '1.4.0':
$this->migrate_1_10_5();
$this->migrate_1_8_0();
$this->migrate_1_7_0();
$this->migrate_1_6_0();
$this->migrate_1_5_0();
$this->migrate_1_4_7();
break;
case '1.3.0':
$this->migrate_1_10_5();
$this->migrate_1_8_0();
$this->migrate_1_7_0();
$this->migrate_1_6_0();
$this->migrate_1_5_0();
$this->migrate_1_4_0();
break;
case '1.2.2':
$this->migrate_1_10_5();
$this->migrate_1_8_0();
$this->migrate_1_7_0();
$this->migrate_1_6_0();
Expand All @@ -243,6 +254,7 @@ public function migrate( $deprecated = null, $deprecated2 = null ) {
$this->migrate_1_3_0();
break;
case '1.2.1':
$this->migrate_1_10_5();
$this->migrate_1_8_0();
$this->migrate_1_7_0();
$this->migrate_1_6_0();
Expand All @@ -252,6 +264,7 @@ public function migrate( $deprecated = null, $deprecated2 = null ) {
$this->migrate_1_2_2();
break;
case '1.2.0':
$this->migrate_1_10_5();
$this->migrate_1_8_0();
$this->migrate_1_7_0();
$this->migrate_1_6_0();
Expand Down Expand Up @@ -560,7 +573,7 @@ private function migrate_1_6_0() {
* @see https://github.com/epiphyt/embed-privacy/issues/163
* @since 1.7.0
*
* - Update Google Maps regex
* - Update CrowdSignal regex
*/
private function migrate_1_7_0() {
$crowdsignal_provider = \get_posts( [
Expand Down Expand Up @@ -674,6 +687,45 @@ private function migrate_1_8_0() {
] );
}

/**
* Migrations for version 1.10.5.
*
* @see https://github.com/epiphyt/embed-privacy/issues/235
* @since 1.10.5
*
* - Rename Twitter to X
*/
private function migrate_1_10_5() {
$twitter_provider = \get_posts( [
'meta_key' => 'is_system',
'meta_value' => 'yes',
'name' => 'twitter',
'no_found_rows' => true,
'post_type' => 'epi_embed',
'update_post_term_cache' => false,
] );
$twitter_provider = \reset( $twitter_provider );

if ( $twitter_provider instanceof WP_Post ) {
$x_provider = [
'ID' => $twitter_provider->ID,
'post_name' => \sanitize_title( \_x( 'X', 'embed provider', 'embed-privacy' ) ),
'post_title' => \_x( 'X', 'embed provider', 'embed-privacy' ),
];

/* translators: embed provider */
if ( $twitter_provider->post_content === \sprintf( \__( 'Click here to display content from %s.', 'embed-privacy' ), \_x( 'Twitter', 'embed provider', 'embed-privacy' ) ) ) {
/* translators: embed provider */
$x_provider['post_content'] = \sprintf( \__( 'Click here to display content from %s.', 'embed-privacy' ), \_x( 'X', 'embed provider', 'embed-privacy' ) );
}

\wp_update_post( $x_provider );
\update_post_meta( $twitter_provider->ID, 'privacy_policy_url', \__( 'https://x.com/privacy', 'embed-privacy' ) );
\update_post_meta( $twitter_provider->ID, 'regex_default', '/(twitter|x)\\\.com/' );
\update_post_meta( $twitter_provider->ID, 'is_system', 'yes' );
}
}

/**
* Register default embed providers.
*/
Expand Down Expand Up @@ -1066,13 +1118,13 @@ public function register_default_embed_providers() {
[
'meta_input' => [
'is_system' => 'yes',
'privacy_policy_url' => \__( 'https://twitter.com/privacy', 'embed-privacy' ),
'regex_default' => '/twitter\\\.com/',
'privacy_policy_url' => \__( 'https://x.com/privacy', 'embed-privacy' ),
'regex_default' => '/(twitter|x)\\\.com/',
],
/* translators: embed provider */
'post_content' => \sprintf( \__( 'Click here to display content from %s.', 'embed-privacy' ), \_x( 'Twitter', 'embed provider', 'embed-privacy' ) ),
'post_content' => \sprintf( \__( 'Click here to display content from %s.', 'embed-privacy' ), \_x( 'X', 'embed provider', 'embed-privacy' ) ),
'post_status' => 'publish',
'post_title' => \_x( 'Twitter', 'embed provider', 'embed-privacy' ),
'post_title' => \_x( 'X', 'embed provider', 'embed-privacy' ),
'post_type' => 'epi_embed',
],
[
Expand Down
6 changes: 3 additions & 3 deletions inc/data/class-replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use epiphyt\Embed_Privacy\embed\Replacement;
use epiphyt\Embed_Privacy\Embed_Privacy;
use epiphyt\Embed_Privacy\handler\Oembed;
use epiphyt\Embed_Privacy\integration\Twitter;
use epiphyt\Embed_Privacy\integration\X;

/**
* Replacer functionality.
Expand Down Expand Up @@ -191,8 +191,8 @@ public static function replace_oembed( $output, $url, array $attributes ) {
}

// check for local tweets
if ( $provider->is( 'twitter' ) && \get_option( 'embed_privacy_local_tweets' ) ) {
return Twitter::get_local_tweet( $output );
if ( $provider->is( 'x' ) && \get_option( 'embed_privacy_local_tweets' ) ) {
return X::get_local_tweet( $output );
}

$output = $replacement->get( $attributes );
Expand Down
88 changes: 7 additions & 81 deletions inc/integration/class-twitter.php
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 {}
Loading

0 comments on commit 0eeccfb

Please sign in to comment.