Make WordPress Core

Changeset 42622


Ignore:
Timestamp:
01/30/2018 02:55:25 PM (6 years ago)
Author:
SergeyBiryukov
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.
Merges [42613], [42617] to the 4.9 branch.
Fixes #42495.

Location:
branches/4.9
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/js/customize-selective-refresh.js

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

    r36783 r42622  
    33(function ($, _, Backbone) {
    44    'use strict';
     5
     6    /** @namespace wp */
     7    window.wp = window.wp || {};
    58
    69    var WPPlaylistView = Backbone.View.extend({
     
    165168    });
    166169
    167     $(document).ready(function () {
    168         $('.wp-playlist').each( function() {
    169             return new WPPlaylistView({ el: this });
     170    /**
     171     * Initialize media playlists in the document.
     172     *
     173     * Only initializes new playlists not previously-initialized.
     174     *
     175     * @since 4.9.3
     176     * @returns {void}
     177     */
     178    function initialize() {
     179        $( '.wp-playlist:not(:has(.mejs-container))' ).each( function() {
     180            new WPPlaylistView( { el: this } );
    170181        } );
    171     });
     182    }
     183
     184    /**
     185     * Expose the API publicly on window.wp.playlist.
     186     *
     187     * @namespace wp.playlist
     188     * @since 4.9.3
     189     * @type {object}
     190     */
     191    window.wp.playlist = {
     192        initialize: initialize
     193    };
     194
     195    $( document ).ready( initialize );
    172196
    173197    window.WPPlaylistView = WPPlaylistView;
  • branches/4.9/src/wp-includes/widgets/class-wp-widget-text.php

    r42546 r42622  
    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().
     
    388392
    389393        return $instance;
     394    }
     395
     396    /**
     397     * Enqueue preview scripts.
     398     *
     399     * These scripts normally are enqueued just-in-time when a playlist shortcode is used.
     400     * However, in the customizer, a playlist shortcode may be used in a text widget and
     401     * dynamically added via selective refresh, so it is important to unconditionally enqueue them.
     402     *
     403     * @since 4.9.3
     404     */
     405    public function enqueue_preview_scripts() {
     406        require_once dirname( dirname( __FILE__ ) ) . '/media.php';
     407
     408        wp_playlist_scripts( 'audio' );
     409        wp_playlist_scripts( 'video' );
    390410    }
    391411
  • branches/4.9/tests/phpunit/tests/widgets/text-widget.php

    r41361 r42622  
    3030     * Clean up global scope.
    3131     *
    32      * @global WP_Scripts $wp_scripts
    33      * @global WP_Styles  $wp_style
     32     * @global WP_Scripts           $wp_scripts
     33     * @global WP_Styles            $wp_style
     34     * @global WP_Customize_Manager $wp_customize
    3435     */
    3536    function clean_up_global_scope() {
    36         global $wp_scripts, $wp_styles;
     37        global $wp_scripts, $wp_styles, $wp_customize;
    3738        parent::clean_up_global_scope();
    38         $wp_scripts = null;
    39         $wp_styles = null;
     39        $wp_scripts   = null;
     40        $wp_styles    = null;
     41        $wp_customize = null;
    4042    }
    4143
     
    6769        $this->assertEquals( 10, has_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) ) );
    6870        $this->assertContains( 'wp.textWidgets.idBases.push( "text" );', wp_scripts()->registered['text-widgets']->extra['after'] );
     71        $this->assertFalse( has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
     72    }
     73
     74    /**
     75     * Test register in customize preview.
     76     *
     77     * @global WP_Customize_Manager $wp_customize
     78     * @covers WP_Widget_Text::__construct()
     79     * @covers WP_Widget_Text::_register()
     80     */
     81    function test__register_in_customize_preview() {
     82        global $wp_customize;
     83        wp_set_current_user(
     84            $this->factory()->user->create(
     85                array(
     86                    'role' => 'administrator',
     87                )
     88            )
     89        );
     90        require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
     91        $wp_customize = new WP_Customize_Manager(
     92            array(
     93                'changeset_uuid' => wp_generate_uuid4(),
     94            )
     95        );
     96        $wp_customize->start_previewing_theme();
     97
     98        $widget = new WP_Widget_Text();
     99        $widget->_register();
     100        $this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
     101    }
     102
     103    /**
     104     * Test enqueue_preview_scripts method.
     105     *
     106     * @global WP_Scripts $wp_scripts
     107     * @global WP_Styles $wp_styles
     108     * @covers WP_Widget_Text::enqueue_preview_scripts
     109     */
     110    function test_enqueue_preview_scripts() {
     111        global $wp_scripts, $wp_styles;
     112        $wp_scripts = null;
     113        $wp_styles  = null;
     114        $widget     = new WP_Widget_Text();
     115
     116        $this->assertFalse( wp_style_is( 'wp-mediaelement' ) );
     117        $this->assertFalse( wp_script_is( 'wp-playlist' ) );
     118
     119        ob_start();
     120        $widget->enqueue_preview_scripts();
     121        ob_end_clean();
     122
     123        $this->assertTrue( wp_style_is( 'wp-mediaelement' ) );
     124        $this->assertTrue( wp_script_is( 'wp-playlist' ) );
    69125    }
    70126
Note: See TracChangeset for help on using the changeset viewer.