Make WordPress Core

Ticket #42495: 42495.2.diff

File 42495.2.diff, 5.4 KB (added by westonruter, 7 years ago)

Add jsdoc and phpunit tests

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

    diff --git src/wp-includes/js/customize-selective-refresh.js src/wp-includes/js/customize-selective-refresh.js
    index b1169eb277..a3d71f76d1 100644
    wp.customize.selectiveRefresh = ( function( $, api ) { 
    488488                                wp.mediaelement.initialize();
    489489                        }
    490490
     491                        if ( wp.playlist ) {
     492                                wp.playlist.initialize();
     493                        }
     494
    491495                        /**
    492496                         * Announce when a partial's placement has been rendered so that dynamic elements can be re-built.
    493497                         */
  • src/wp-includes/js/mediaelement/wp-playlist.js

    diff --git src/wp-includes/js/mediaelement/wp-playlist.js src/wp-includes/js/mediaelement/wp-playlist.js
    index 19cab125ba..81c7da6c42 100644
     
    33(function ($, _, Backbone) {
    44        'use strict';
    55
     6        /** @namespace wp */
     7        window.wp = window.wp || {};
     8
    69        var WPPlaylistView = Backbone.View.extend(/** @lends WPPlaylistView.prototype */{
    710                /**
    811                 * @constructs
     
    168171                }
    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;
    178202
  • src/wp-includes/widgets/class-wp-widget-text.php

    diff --git src/wp-includes/widgets/class-wp-widget-text.php src/wp-includes/widgets/class-wp-widget-text.php
    index 5d73eb4ce0..21feb3f20a 100644
    class WP_Widget_Text extends WP_Widget { 
    5757
    5858                wp_add_inline_script( 'text-widgets', sprintf( 'wp.textWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
    5959
     60                if ( $this->is_preview() ) {
     61                        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) );
     62                }
     63
    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().
    6165                add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
    6266
    class WP_Widget_Text extends WP_Widget { 
    393397                return $instance;
    394398        }
    395399
     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' );
     414        }
     415
    396416        /**
    397417         * Loads the required scripts and styles for the widget control.
    398418         *
  • tests/phpunit/tests/widgets/text-widget.php

    diff --git tests/phpunit/tests/widgets/text-widget.php tests/phpunit/tests/widgets/text-widget.php
    index 490875416b..c50cf31e68 100644
    class Test_WP_Widget_Text extends WP_UnitTestCase { 
    6666                $this->assertEquals( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) );
    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
    71125        /**