Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions class-opengraph-fallback-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class OpenGraph_Fallback_Embed {
/** @var int Cache duration in seconds (default 7 days). */
const CACHE_TTL = 604800;

/** @var string Transient key prefix for cached OG data. */
const CACHE_KEY_PREFIX = 'opengraph_fallback_embed_';

/* ------------------------------------------------------------------
* Bootstrap
* ----------------------------------------------------------------*/
Expand Down Expand Up @@ -104,8 +107,10 @@ public static function intercept_oembed_proxy( $response, $handler, $request ) {

$site_name = ! empty( $og['site_name'] ) ? $og['site_name'] : wp_parse_url( $url, PHP_URL_HOST );

// The editor renders "rich" embeds inside a sandboxed iframe,
// so styles must be inlined.
// The block editor renders "rich" type oEmbed responses inside a sandboxed
// iframe (wp.components.SandBox). The Embed block does not forward styles
// to SandBox, so wp_enqueue_style() / wp_add_inline_style() cannot reach
// the iframe. Inline styles in the oEmbed HTML are the only option.
$css_file = plugin_dir_path( __FILE__ ) . 'build/og-embed/style-index.css';
$css = file_exists( $css_file ) ? file_get_contents( $css_file ) : ''; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$card_html = self::render_card( $og, $url );
Expand Down Expand Up @@ -252,7 +257,7 @@ public static function rest_preview( WP_REST_Request $request ): WP_REST_Respons
* @return array<string, string> Associative array of OG properties.
*/
public static function get_og_data( string $url ): array {
$cache_key = 'og_embed_' . md5( $url );
$cache_key = self::CACHE_KEY_PREFIX . md5( $url );
$cached = get_transient( $cache_key );

if ( is_array( $cached ) ) {
Expand Down
12 changes: 12 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,20 @@ No. This plugin only activates when WordPress has no other embed handler for the

The card uses the `.og-fallback-embed` CSS class. You can override styles in your theme.

== Development ==

The source code for this plugin, including unminified JavaScript and CSS, is available on GitHub:

https://github.com/pento/opengraph-fallback-embed

The plugin uses [@wordpress/scripts](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/) for building. Source files are in the `src/` directory. See the repository README for build instructions.

== Changelog ==

= 1.3.5 =
* Add source code repository link to readme.
* Use plugin-specific prefix for transient cache keys.

Comment thread
pento marked this conversation as resolved.
= 1.3.4 =
* Tweak the plugin header.

Expand Down
4 changes: 2 additions & 2 deletions tests/EmbedBlockFallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class EmbedBlockFallbackTest extends WP_UnitTestCase {

public function tear_down(): void {
parent::tear_down();
delete_transient( 'og_embed_' . md5( self::TEST_URL ) );
delete_transient( 'og_embed_' . md5( self::YOUTUBE_URL ) );
delete_transient( OpenGraph_Fallback_Embed::CACHE_KEY_PREFIX . md5( self::TEST_URL ) );
delete_transient( OpenGraph_Fallback_Embed::CACHE_KEY_PREFIX . md5( self::YOUTUBE_URL ) );
}

/* ------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/MaybeEmbedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class MaybeEmbedTest extends WP_UnitTestCase {

public function tear_down(): void {
parent::tear_down();
delete_transient( 'og_embed_' . md5( 'https://example.com/article' ) );
delete_transient( OpenGraph_Fallback_Embed::CACHE_KEY_PREFIX . md5( 'https://example.com/article' ) );
}

public function test_url_with_og_data_returns_card(): void {
Expand Down
2 changes: 1 addition & 1 deletion tests/ParseOgTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ParseOgTagsTest extends WP_UnitTestCase {

public function tear_down(): void {
parent::tear_down();
delete_transient( 'og_embed_' . md5( 'https://example.com/article' ) );
delete_transient( OpenGraph_Fallback_Embed::CACHE_KEY_PREFIX . md5( 'https://example.com/article' ) );
}

/**
Expand Down
Loading