Plugin Directory

Changeset 2080878


Ignore:
Timestamp:
05/05/2019 02:35:18 PM (7 years ago)
Author:
2plus2is4
Message:
  • Added Simple HTML DOM lib to run through DOM easily
  • Removed lot's of code afterwards
  • Now the whole figure is replaced with div with image (not just iframe. It caused troubles)
  • Readme and stable tag
Location:
simple-youtube-gdpr
Files:
35 added
4 deleted
10 edited

Legend:

Unmodified
Added
Removed
  • simple-youtube-gdpr/trunk/README.txt

    r2072368 r2080878  
    55Requires at least: 5.0.1
    66Tested up to: 5.1.1
    7 Stable tag: 0.5
     7Stable tag: 0.6
    88Requires PHP: 5.2.4
    99License: GPLv2 or later
     
    64641. Replace an iframe with a picture
    65651. Apply scripts so that if user clicks iframe appears
     66
     67== Changelog ==
     68
     69= 0.6 =
     70* Added Simple HTML DOM lib to run through DOM easily
     71* Removed lot's of code afterwards
     72* Now the whole figure is replaced with div with image (not just iframe. It caused troubles)
     73
     74== Upgrade Notice ==
     75
     76= 0.6 =
     77Twenty Nineteen theme bug fixed. It replace the whole WordPress <figure> tag with an image.
  • simple-youtube-gdpr/trunk/includes/class-simple-youtube-gdpr-activator.php

    r2072368 r2080878  
    55 *
    66 * @link       https://alexeyvolkov.com/
    7  * @since      0.5.0
     7 * @since      0.6
    88 *
    99 * @package    Simple_Youtube_Gdpr
     
    1616 * This class defines all code necessary to run during the plugin's activation.
    1717 *
    18  * @since      0.5.0
     18 * @since      0.6
    1919 * @package    Simple_Youtube_Gdpr
    2020 * @subpackage Simple_Youtube_Gdpr/includes
     
    2828     * Long Description.
    2929     *
    30      * @since    0.5.0
     30     * @since    0.6
    3131     */
    3232    public static function activate() {
  • simple-youtube-gdpr/trunk/includes/class-simple-youtube-gdpr-deactivator.php

    r2072368 r2080878  
    55 *
    66 * @link       https://alexeyvolkov.com/
    7  * @since      0.5.0
     7 * @since      0.6
    88 *
    99 * @package    Simple_Youtube_Gdpr
     
    1616 * This class defines all code necessary to run during the plugin's deactivation.
    1717 *
    18  * @since      0.5.0
     18 * @since      0.6
    1919 * @package    Simple_Youtube_Gdpr
    2020 * @subpackage Simple_Youtube_Gdpr/includes
     
    2828     * Long Description.
    2929     *
    30      * @since    0.5.0
     30     * @since    0.6
    3131     */
    3232    public static function deactivate() {
  • simple-youtube-gdpr/trunk/includes/class-simple-youtube-gdpr-i18n.php

    r2072368 r2080878  
    88 *
    99 * @link       https://alexeyvolkov.com/
    10  * @since      0.5.0
     10 * @since      0.6
    1111 *
    1212 * @package    Simple_Youtube_Gdpr
     
    2020 * so that it is ready for translation.
    2121 *
    22  * @since      0.5.0
     22 * @since      0.6
    2323 * @package    Simple_Youtube_Gdpr
    2424 * @subpackage Simple_Youtube_Gdpr/includes
     
    3131     * Load the plugin text domain for translation.
    3232     *
    33      * @since    0.5.0
     33     * @since    0.6
    3434     */
    3535    public function load_plugin_textdomain() {
  • simple-youtube-gdpr/trunk/includes/class-simple-youtube-gdpr-loader.php

    r2072368 r2080878  
    55 *
    66 * @link       https://alexeyvolkov.com/
    7  * @since      0.5.0
     7 * @since      0.6
    88 *
    99 * @package    Simple_Youtube_Gdpr
     
    2727     * The array of actions registered with WordPress.
    2828     *
    29      * @since    0.5.0
     29     * @since    0.6
    3030     * @access   protected
    3131     * @var      array    $actions    The actions registered with WordPress to fire when the plugin loads.
     
    3636     * The array of filters registered with WordPress.
    3737     *
    38      * @since    0.5.0
     38     * @since    0.6
    3939     * @access   protected
    4040     * @var      array    $filters    The filters registered with WordPress to fire when the plugin loads.
     
    4545     * Initialize the collections used to maintain the actions and filters.
    4646     *
    47      * @since    0.5.0
     47     * @since    0.6
    4848     */
    4949    public function __construct() {
     
    5757     * Add a new action to the collection to be registered with WordPress.
    5858     *
    59      * @since    0.5.0
     59     * @since    0.6
    6060     * @param    string               $hook             The name of the WordPress action that is being registered.
    6161     * @param    object               $component        A reference to the instance of the object on which the action is defined.
     
    7171     * Add a new filter to the collection to be registered with WordPress.
    7272     *
    73      * @since    0.5.0
     73     * @since    0.6
    7474     * @param    string               $hook             The name of the WordPress filter that is being registered.
    7575     * @param    object               $component        A reference to the instance of the object on which the filter is defined.
     
    8686     * collection.
    8787     *
    88      * @since    0.5.0
     88     * @since    0.6
    8989     * @access   private
    9090     * @param    array                $hooks            The collection of hooks that is being registered (that is, actions or filters).
     
    113113     * Register the filters and actions with WordPress.
    114114     *
    115      * @since    0.5.0
     115     * @since    0.6
    116116     */
    117117    public function run() {
  • simple-youtube-gdpr/trunk/includes/class-simple-youtube-gdpr.php

    r2072368 r2080878  
    88 *
    99 * @link       https://alexeyvolkov.com/
    10  * @since      0.5.0
     10 * @since      0.6
    1111 *
    1212 * @package    Simple_Youtube_Gdpr
     
    2323 * version of the plugin.
    2424 *
    25  * @since      0.5.0
     25 * @since      0.6
    2626 * @package    Simple_Youtube_Gdpr
    2727 * @subpackage Simple_Youtube_Gdpr/includes
     
    3434     * the plugin.
    3535     *
    36      * @since    0.5.0
     36     * @since    0.6
    3737     * @access   protected
    38      * @var      Simple_Youtube_Gdpr_Loader    $loader    Maintains and registers all hooks for the plugin.
     38     * @var      Simple_Youtube_Gdpr_Loader $loader Maintains and registers all hooks for the plugin.
    3939     */
    4040    protected $loader;
     
    4343     * The unique identifier of this plugin.
    4444     *
    45      * @since    0.5.0
     45     * @since    0.6
    4646     * @access   protected
    47      * @var      string    $plugin_name    The string used to uniquely identify this plugin.
     47     * @var      string $plugin_name The string used to uniquely identify this plugin.
    4848     */
    4949    protected $plugin_name;
     
    5252     * The current version of the plugin.
    5353     *
    54      * @since    0.5.0
     54     * @since    0.6
    5555     * @access   protected
    56      * @var      string    $version    The current version of the plugin.
     56     * @var      string $version The current version of the plugin.
    5757     */
    5858    protected $version;
     
    6565     * the public-facing side of the site.
    6666     *
    67      * @since    0.5.0
     67     * @since    0.6
    6868     */
    6969    public function __construct() {
     
    7171            $this->version = SIMPLE_YOUTUBE_GDPR_VERSION;
    7272        } else {
    73             $this->version = '0.5.0';
     73            $this->version = '0.6';
    7474        }
    7575        $this->plugin_name = 'simple-youtube-gdpr';
     
    9595     * with WordPress.
    9696     *
    97      * @since    0.5.0
     97     * @since    0.6
    9898     * @access   private
    9999     */
     
    123123        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-simple-youtube-gdpr-public.php';
    124124
     125        /**
     126         * The file responsible for DOM search.
     127         */
     128        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/simple_html_dom.php';
     129
    125130        $this->loader = new Simple_Youtube_Gdpr_Loader();
    126131
     
    133138     * with WordPress.
    134139     *
    135      * @since    0.5.0
     140     * @since    0.6
    136141     * @access   private
    137142     */
     
    148153     * of the plugin.
    149154     *
    150      * @since    0.5.0
     155     * @since    0.6
    151156     * @access   private
    152157     */
     
    164169     * of the plugin.
    165170     *
    166      * @since    0.5.0
     171     * @since    0.6
    167172     * @access   private
    168173     */
     
    180185     * Run the loader to execute all of the hooks with WordPress.
    181186     *
    182      * @since    0.5.0
     187     * @since    0.6
    183188     */
    184189    public function run() {
     
    190195     * WordPress and to define internationalization functionality.
    191196     *
    192      * @since     0.5.0
     197     * @since     0.6
    193198     * @return    string    The name of the plugin.
    194199     */
     
    200205     * The reference to the class that orchestrates the hooks with the plugin.
    201206     *
    202      * @since     0.5.0
     207     * @since     0.6
    203208     * @return    Simple_Youtube_Gdpr_Loader    Orchestrates the hooks of the plugin.
    204209     */
     
    210215     * Retrieve the version number of the plugin.
    211216     *
    212      * @since     0.5.0
     217     * @since     0.6
    213218     * @return    string    The version number of the plugin.
    214219     */
  • simple-youtube-gdpr/trunk/public/class-simple-youtube-gdpr-public.php

    r2072368 r2080878  
    55 *
    66 * @link       https://alexeyvolkov.com/
    7  * @since      0.5.0
     7 * @since      0.6
    88 *
    99 * @package    Simple_Youtube_Gdpr
     
    2626     * The ID of this plugin.
    2727     *
    28      * @since    0.5.0
     28     * @since    0.6
    2929     * @access   private
    3030     * @var      string $plugin_name The ID of this plugin.
     
    3535     * The version of this plugin.
    3636     *
    37      * @since    0.5.0
     37     * @since    0.6
    3838     * @access   private
    3939     * @var      string $version The current version of this plugin.
     
    4444     * Initialize the class and set its properties.
    4545     *
    46      * @since    0.5.0
     46     * @since    0.6
    4747     *
    4848     * @param      string $plugin_name The name of the plugin.
     
    5959     * Register the stylesheets for the public-facing side of the site.
    6060     *
    61      * @since    0.5.0
     61     * @since    0.6
    6262     */
    6363    public function enqueue_styles() {
     
    8989     * Register the JavaScript for the public-facing side of the site.
    9090     *
    91      * @since    0.5.0
     91     * @since    0.6
    9292     */
    9393    public function enqueue_scripts() {
     
    121121        if ( is_single() || is_page() ) {
    122122            $content = $this->lazyloadIframes( $content );
    123 
    124123        }
    125124
     
    134133     * @return string
    135134     */
    136     private function lazyloadIframes( $html, $args = [] ) {
    137 
    138         $buffer = $this->ignoreScripts( $html );
    139         $buffer = $this->ignoreNoscripts( $buffer );
    140 
    141         $defaults = [
    142             'youtube' => true,
    143         ];
    144 
    145         $args = wp_parse_args( $args, $defaults );
    146 
    147         if ( ! preg_match_all( '@<iframe(?<atts>\s.+)>.*</iframe>@iUs', $buffer, $iframes, PREG_SET_ORDER ) ) {
    148             return $html;
    149         }
    150 
    151         $iframes = array_unique( $iframes, SORT_REGULAR );
    152 
    153         foreach ( $iframes as $iframe ) {
    154             // Given the previous regex pattern, $iframe['atts'] starts with a whitespace character.
    155             if ( ! preg_match( '@\ssrc\s*=\s*(\'|")(?<src>.*)\1@iUs', $iframe['atts'], $atts ) ) {
     135    private function lazyloadIframes( $html, $container = 'figure.wp-block-embed-youtube' ) {
     136        $html = str_get_html( $html );
     137        foreach ( $html->find( $container ) as $figure ) {
     138            if ( '' === trim( $figure->innertext ) ) {// if empty -> next item
    156139                continue;
    157140            }
    158141
    159             $iframe['src'] = esc_html( trim( $atts['src'] ) );
    160 
    161             if ( '' === $iframe['src'] ) {
    162                 continue;
    163             }
    164 
    165             if ( $args['youtube'] ) {
    166                 $iframe_lazyload = $this->replaceYoutubeThumbnail( $iframe );
    167             }
    168 
    169             $html = str_replace( $iframe[0], $iframe_lazyload, $html );
    170 
    171             unset( $iframe_lazyload );
    172         }
    173 
    174         return $html;
     142            foreach ( $figure->find( 'iframe' ) as $iframe ) {
     143                if ( '' === trim( $iframe->src ) ) {// if empty -> next item
     144                    continue;
     145                }
     146                $imgHTML = $this->getThumbnail( $iframe->outertext );// send <iframe> and get <div.youtube_lazyload> <img> </div>
     147                $imgDiv  = str_get_html( $imgHTML );// from raw HTML to Node Object
     148                $iframe  = $imgDiv->find( 'div.youtube_lazyload' )[0];// Find the only one(first) div.youtube_lazyload
     149            }
     150
     151            // Replace <figure> with <img>
     152            if ( ! $iframe->class ) {// if no class -> create
     153                $iframe->class = '';
     154            }
     155            $iframe->class     = $iframe->class . ' ' . $figure->class;
     156            $figure->outertext = $iframe->outertext;
     157        }
     158
     159        return $html->outertext;
    175160    }
    176161
     
    182167     * @return bool|string
    183168     */
    184     private function replaceYoutubeThumbnail( $iframe ) {
    185         $youtube_id = esc_html( $this->getYoutubeIDFromURL( $iframe['src'] ) );
     169    private function getThumbnail( $iframeHTML ) {
     170        $iframeHTML = str_get_html( $iframeHTML );
     171        $iframe     = $iframeHTML->find( 'iframe' )[0];// Find the only one(first) iframe
     172        $youtube_id = esc_html( $this->getYoutubeIDFromURL( $iframe->src ) );// get YouTube ID (fM6JWeQFnNk)
    186173
    187174        if ( ! $youtube_id ) {
     
    283270        file_put_contents( $file, $image_data );
    284271    }
    285 
    286     /**
    287      * Remove inline scripts from the HTML to parse
    288      *
    289      * @param string $html HTML content.
    290      *
    291      * @return string
    292      */
    293     private function ignoreScripts( $html ) {
    294         return preg_replace( '/<script\b(?:[^>]*)>(?:.+)?<\/script>/Umsi', '', $html );
    295     }
    296 
    297     /**
    298      * Remove noscript tags from the HTML to parse
    299      *
    300      * @param string $html HTML content.
    301      *
    302      * @return string
    303      */
    304     private function ignoreNoscripts( $html ) {
    305         return preg_replace( '#<noscript>(?:.+)</noscript>#Umsi', '', $html );
    306     }
    307272}
  • simple-youtube-gdpr/trunk/public/partials/simple-youtube-gdpr-public-display.php

    r2072368 r2080878  
    77 *
    88 * @link       https://alexeyvolkov.com/
    9  * @since      0.5.0
     9 * @since      0.6
    1010 *
    1111 * @package    Simple_Youtube_Gdpr
  • simple-youtube-gdpr/trunk/simple-youtube-gdpr.php

    r2072368 r2080878  
    1010 *
    1111 * @link              https://alexeyvolkov.com/
    12  * @since             0.5.0
     12 * @since             0.6
    1313 * @package           Simple_Youtube_Gdpr
    1414 *
     
    1717 * Plugin URI:        https://alexeyvolkov.com/blog/simple-youtube-gdpr
    1818 * Description:       Replaces all YouTube video iframes with a local image and loads a no cookie verion of video.
    19  * Version:           0.5.0
     19 * Version:           0.6
    2020 * Author:            Alexey Volkov
    2121 * Author URI:        https://alexeyvolkov.com/
     
    3333/**
    3434 * Currently plugin version.
    35  * Start at version 0.5.0 and use SemVer - https://semver.org
     35 * Start at version 0.6 and use SemVer - https://semver.org
    3636 * Rename this for your plugin and update it as you release new versions.
    3737 */
    38 define( 'SIMPLE_YOUTUBE_GDPR_VERSION', '0.5.0' );
     38define( 'SIMPLE_YOUTUBE_GDPR_VERSION', '0.6' );
    3939
    4040/**
     
    7272 * not affect the page life cycle.
    7373 *
    74  * @since    0.5.0
     74 * @since    0.6
    7575 */
    7676function run_simple_youtube_gdpr() {
  • simple-youtube-gdpr/trunk/uninstall.php

    r2072368 r2080878  
    2121 *
    2222 * @link       https://alexeyvolkov.com/
    23  * @since      0.5.0
     23 * @since      0.6
    2424 *
    2525 * @package    Simple_Youtube_Gdpr
Note: See TracChangeset for help on using the changeset viewer.