Make WordPress Core

Changeset 41951


Ignore:
Timestamp:
10/19/2017 11:43:22 PM (7 years ago)
Author:
westonruter
Message:

Widgets: Improve extensibility of the Gallery widget and of media widgets generally.

  • Introduce a widget_{$id_base}_instance_schema filter for plugins to add new properties to a media widget's instance schema.
  • Pass all of a gallery widget's instance props to the gallery media frame, not just the ones that core supports.

See #32417, #41914.
Fixes #42285.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/widgets/media-gallery-widget.js

    r41872 r41951  
    202202
    203203            mediaFrameProps = control.mapModelToMediaFrameProps( control.model.toJSON() );
    204             selection.gallery = new Backbone.Model( _.pick( mediaFrameProps, 'columns', 'link', 'size', '_orderbyRandom' ) );
     204            selection.gallery = new Backbone.Model( mediaFrameProps );
    205205            if ( mediaFrameProps.size ) {
    206206                control.displaySettings.set( 'size', mediaFrameProps.size );
  • trunk/src/wp-includes/widgets/class-wp-widget-media-gallery.php

    r41840 r41951  
    4747     */
    4848    public function get_instance_schema() {
    49         return array(
     49        $schema = array(
    5050            'title' => array(
    5151                'type' => 'string',
     
    8888            ),
    8989        );
     90
     91        /** This filter is documented in wp-includes/widgets/class-wp-widget-media.php */
     92        $schema = apply_filters( "widget_{$this->id_base}_instance_schema", $schema, $this );
     93
     94        return $schema;
    9095    }
    9196
     
    101106        $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
    102107
    103         $shortcode_atts = array(
    104             'ids'     => $instance['ids'],
    105             'columns' => $instance['columns'],
    106             'link'    => $instance['link_type'],
    107             'size'    => $instance['size'],
     108        $shortcode_atts = array_merge(
     109            $instance,
     110            array(
     111                'link' => $instance['link_type'],
     112            )
    108113        );
    109114
  • trunk/src/wp-includes/widgets/class-wp-widget-media.php

    r41866 r41951  
    128128     */
    129129    public function get_instance_schema() {
    130         return array(
     130        $schema = array(
    131131            'attachment_id' => array(
    132132                'type' => 'integer',
     
    150150            ),
    151151        );
     152
     153        /**
     154         * Filters the media widget instance schema to add additional properties.
     155         *
     156         * @since 4.9.0
     157         *
     158         * @param array           $schema Instance schema.
     159         * @param WP_Widget_Media $this   Widget object.
     160         */
     161        $schema = apply_filters( "widget_{$this->id_base}_instance_schema", $schema, $this );
     162
     163        return $schema;
    152164    }
    153165
  • trunk/tests/phpunit/tests/widgets/media-widget.php

    r40674 r41951  
    166166
    167167    /**
     168     * Instance schema args.
     169     *
     170     * @var array
     171     */
     172    protected $filter_instance_schema_args;
     173
     174    /**
     175     * Filter instance schema.
     176     *
     177     * @param array           $schema Schema.
     178     * @param WP_Widget_Media $widget Widget.
     179     *
     180     * @return array
     181     */
     182    public function filter_instance_schema( $schema, $widget ) {
     183        $this->filter_instance_schema_args = compact( 'schema', 'widget' );
     184        $schema['injected'] = array(
     185            'type' => 'boolean',
     186        );
     187        return $schema;
     188    }
     189
     190    /**
    168191     * Test get_instance_schema method.
    169192     *
     
    179202            'url',
    180203        ), array_keys( $schema ) );
     204
     205        // Check filter usage.
     206        $this->filter_instance_schema_args = null;
     207        add_filter( 'widget_mocked_instance_schema', array( $this, 'filter_instance_schema' ), 10, 2 );
     208        $schema = $widget->get_instance_schema();
     209        $this->assertInternalType( 'array', $this->filter_instance_schema_args );
     210        $this->assertSame( $widget, $this->filter_instance_schema_args['widget'] );
     211        $this->assertEqualSets( array( 'attachment_id', 'title', 'url' ), array_keys( $this->filter_instance_schema_args['schema'] ) );
     212        $this->assertArrayHasKey( 'injected', $schema );
    181213    }
    182214
Note: See TracChangeset for help on using the changeset viewer.