Last active
August 5, 2022 19:56
-
-
Save afragen/36ab01da116aadd27a3f4d85cce90390 to your computer and use it in GitHub Desktop.
WordPress plugin to embed gists or gist files.
This file contains hidden or 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
<?php | |
/** | |
* Gist oEmbed. | |
* | |
* @package Fragen\Gist_OEmbed | |
* | |
* Plugin Name: Gist oEmbed | |
* Plugin URI: https://gist.github.com/afragen/36ab01da116aadd27a3f4d85cce90390 | |
* Description: oEmbed Gist or files within Gists. | |
* Version: 0.6.2 | |
* Author: Andy Fragen, Colin Stewart | |
* License: MIT | |
* Requires at least: 5.9 | |
* Requires PHP: 7.1 | |
* GitHub Plugin URI: https://github.com/afragen/oembed-gist-files | |
* Primary Branch: main | |
*/ | |
namespace Fragen; | |
/** | |
* Exit if called directly. | |
*/ | |
if ( ! defined( 'WPINC' ) ) { | |
die; | |
} | |
wp_embed_register_handler( 'gist', '#^https?://gist.github.com/.*#i', [ new Gist_OEmbed(), 'gist_result' ] ); | |
/** | |
* Class Gist_OEmbed. | |
*/ | |
class Gist_OEmbed { | |
/** | |
* Render Gist for embed. | |
* | |
* @param array $url Gist URL. | |
* | |
* @return string | |
*/ | |
public function gist_result( $url ) { | |
$url = $url[0]; | |
$fragment = ''; | |
$parsed = explode( '#', $url ); | |
// Parse elements of URL for specific file within Gist. | |
if ( isset( $parsed[1] ) ) { | |
$url = $parsed[0]; | |
$fragment = str_replace( 'file-', '', $parsed[1] ); | |
$file_arr = explode( '-', $fragment ); | |
$ext = array_pop( $file_arr ); | |
$fragment = implode( '-', $file_arr ) . '.' . $ext; | |
$fragment = '?file=' . $fragment; | |
} | |
if ( ! str_ends_with( strtolower( $url ), '.js' ) ) { | |
$url .= '.js'; | |
} | |
$url = ! empty( $fragment ) ? $url . $fragment : $url; | |
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript | |
return '<script src="' . esc_url( $url ) . '"></script>'; | |
} | |
} |
Hmm, I think I added it correctly.
return '<script src="' . esc_url( $url ) . '" defer></script>';
which returns
<script src="https://gist.github.com/afragen/e1aa3ffccf1a73618ee6e756bd95d297.js" defer></script>
Unfortunately, it no longer renders. Do I have this set up correctly?
Apparently this is a thing. wp-media/wp-rocket#1117
Ah that’s a bug in their embed script. Tweeted Helen for help, hope that
helps.
…On Wed, 3 Aug 2022 at 18:42, Andy Fragen ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Apparently this is a thing. wp-media/wp-rocket#1117
<wp-media/wp-rocket#1117>
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/36ab01da116aadd27a3f4d85cce90390#gistcomment-4254883>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADXBTOC6NJAGHE4A674JBLVXKHN3ANCNFSM55IVIICA>
.
You are receiving this because you commented.Message ID:
***@***.***>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Might I suggest a
defer
be added to thisscript
tag here?