Make WordPress Core

Changeset 42613


Ignore:
Timestamp:
01/29/2018 11:55:44 PM (7 years ago)
Author:
westonruter
Message:

Customize: Ensure media playlists get initialized after selective refresh; expose new wp.playlist.initialize() API.

In particular allows audio and video playlists to be added to the Text widget and previewed.

Props bpayton, westonruter.
See #40854.
Fixes #42495.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/customize-selective-refresh.js

    r42037 r42613  
    487487            if ( wp.mediaelement ) {
    488488                wp.mediaelement.initialize();
     489            }
     490
     491            if ( wp.playlist ) {
     492                wp.playlist.initialize();
    489493            }
    490494
  • trunk/src/wp-includes/js/mediaelement/wp-playlist.js

    r42403 r42613  
    33(function ($, _, Backbone) {
    44    'use strict';
     5
     6    /** @namespace wp */
     7    window.wp = window.wp || {};
    58
    69    var WPPlaylistView = Backbone.View.extend(/** @lends WPPlaylistView.prototype */{
     
    169172    });
    170173
    171     $(document).ready(function () {
    172         $('.wp-playlist').each( function() {
    173             return new WPPlaylistView({ el: this });
     174    /**
     175     * Initialize media playlists in the document.
     176     *
     177     * Only initializes new playlists not previously-initialized.
     178     *
     179     * @since 4.9.3
     180     * @returns {void}
     181     */
     182    function initialize() {
     183        $( '.wp-playlist:not(:has(.mejs-container))' ).each( function() {
     184            new WPPlaylistView( { el: this } );
    174185        } );
    175     });
     186    }
     187
     188    /**
     189     * Expose the API publicly on window.wp.playlist.
     190     *
     191     * @namespace wp.playlist
     192     * @since 4.9.3
     193     * @type {object}
     194     */
     195    window.wp.playlist = {
     196        initialize: initialize
     197    };
     198
     199    $( document ).ready( initialize );
    176200
    177201    window.WPPlaylistView = WPPlaylistView;
  • trunk/src/wp-includes/widgets/class-wp-widget-text.php

    r42545 r42613  
    5757
    5858        wp_add_inline_script( 'text-widgets', sprintf( 'wp.textWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
     59
     60        if ( $this->is_preview() ) {
     61            add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) );
     62        }
    5963
    6064        // Note that the widgets component in the customizer will also do the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
     
    392396
    393397        return $instance;
     398    }
     399
     400    /**
     401     * Enqueue preview scripts.
     402     *
     403     * These scripts normally are enqueued just-in-time when a playlist shortcode is used.
     404     * However, in the customizer, a playlist shortcode may be used in a text widget and
     405     * dynamically added via selective refresh, so it is important to unconditionally enqueue them.
     406     *
     407     * @since 4.9.3
     408     */
     409    public function enqueue_preview_scripts() {
     410        require_once dirname( dirname( __FILE__ ) ) . '/media.php';
     411
     412        wp_playlist_scripts( 'audio' );
     413        wp_playlist_scripts( 'video' );
    394414    }
    395415
  • trunk/tests/phpunit/tests/widgets/text-widget.php

    r42343 r42613  
    6767        $this->assertEquals( 10, has_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) ) );
    6868        $this->assertContains( 'wp.textWidgets.idBases.push( "text" );', wp_scripts()->registered['text-widgets']->extra['after'] );
     69        $this->assertFalse( has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
     70    }
     71
     72    /**
     73     * Test register in customize preview.
     74     *
     75     * @global WP_Customize_Manager $wp_customize
     76     * @covers WP_Widget_Text::__construct()
     77     * @covers WP_Widget_Text::_register()
     78     */
     79    function test__register_in_customize_preview() {
     80        global $wp_customize;
     81        wp_set_current_user(
     82            $this->factory()->user->create(
     83                array(
     84                    'role' => 'administrator',
     85                )
     86            )
     87        );
     88        require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
     89        $wp_customize = new WP_Customize_Manager(
     90            array(
     91                'changeset_uuid' => wp_generate_uuid4(),
     92            )
     93        );
     94        $wp_customize->start_previewing_theme();
     95
     96        $widget = new WP_Widget_Text();
     97        $widget->_register();
     98        $this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
     99    }
     100
     101    /**
     102     * Test enqueue_preview_scripts method.
     103     *
     104     * @global WP_Scripts $wp_scripts
     105     * @global WP_Styles $wp_styles
     106     * @covers WP_Widget_Text::enqueue_preview_scripts
     107     */
     108    function test_enqueue_preview_scripts() {
     109        global $wp_scripts, $wp_styles;
     110        $wp_scripts = null;
     111        $wp_styles  = null;
     112        $widget     = new WP_Widget_Text();
     113
     114        $this->assertFalse( wp_style_is( 'wp-mediaelement' ) );
     115        $this->assertFalse( wp_script_is( 'wp-playlist' ) );
     116
     117        ob_start();
     118        $widget->enqueue_preview_scripts();
     119        ob_end_clean();
     120
     121        $this->assertTrue( wp_style_is( 'wp-mediaelement' ) );
     122        $this->assertTrue( wp_script_is( 'wp-playlist' ) );
    69123    }
    70124
Note: See TracChangeset for help on using the changeset viewer.