Skip to content

Commit

Permalink
Add functionality to pass "queryargs" to Media field
Browse files Browse the repository at this point in the history
  • Loading branch information
mae829 committed Feb 6, 2020
1 parent 5213e07 commit 7c28009
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions js/media/fieldmanager-media.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ $( document ).on( 'click', '.fm-media-button', function( event ) {
library.type = $el.data( 'mime-type' );
}

// Add query arguments to the media window request.
// Gives "data-mime-type" priority over "mime-type" defined in "data-queryargs".
if ( $el.data( 'queryargs' ) ) {
library = $.extend( {}, $el.data( 'queryargs' ), library );
}

// Create the media frame.
fm_media_frame[ $el.attr('id') ] = wp.media({
// Set the library attributes.
Expand Down
10 changes: 9 additions & 1 deletion php/class-fieldmanager-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ class Fieldmanager_Media extends Fieldmanager_Field {
*/
public $mime_type = 'all';

/**
* Query arguments to pass to wp.media window.
*
* @var array
*/
public $queryargs = array();

/**
* Static variable so we only load media JS once.
*
Expand Down Expand Up @@ -169,7 +176,7 @@ public function form_element( $value = array() ) {
$preview = '';
}
return sprintf(
'<input type="button" class="fm-media-button button-secondary fm-incrementable" id="%1$s" value="%3$s" data-choose="%7$s" data-update="%8$s" data-preview-size="%6$s" data-mime-type="%9$s" %10$s />
'<input type="button" class="fm-media-button button-secondary fm-incrementable" id="%1$s" value="%3$s" data-choose="%7$s" data-update="%8$s" data-preview-size="%6$s" data-queryargs="%9$s" data-mime-type="%10$s" %11$s />
<input type="hidden" name="%2$s" value="%4$s" class="fm-element fm-media-id" />
<div class="media-wrapper">%5$s</div>',
esc_attr( $this->get_element_id() ),
Expand All @@ -180,6 +187,7 @@ public function form_element( $value = array() ) {
esc_attr( $this->preview_size ),
esc_attr( $this->modal_title ),
esc_attr( $this->modal_button_label ),
esc_attr( wp_json_encode( $this->queryargs ) ),
esc_attr( $this->mime_type ),
$this->get_element_attributes()
);
Expand Down
6 changes: 4 additions & 2 deletions tests/php/test-fieldmanager-media-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function test_basic_render() {
'selected_file_label' => rand_str(),
'remove_media_label' => rand_str(),
'preview_size' => rand_str(),
'queryargs' => rand_str(),
);
$fm = new Fieldmanager_Media( $args );
$context = $fm->add_meta_box( 'Test Media', 'post' );
Expand All @@ -38,11 +39,12 @@ public function test_basic_render() {
$html = ob_get_clean();
$this->assertRegExp(
sprintf(
'#<input type="button" class="[^"]*fm-media-button[^>]+value="%s" data-choose="%s" data-update="%s" data-preview-size="%s" data-mime-type="all" */>#',
'#<input type="button" class="[^"]*fm-media-button[^>]+value="%s" data-choose="%s" data-update="%s" data-preview-size="%s" data-queryargs="%s" data-mime-type="all" */>#',
$args['button_label'],
$args['modal_title'],
$args['modal_button_label'],
$args['preview_size']
$args['preview_size'],
$args['queryargs']
),
$html
);
Expand Down

0 comments on commit 7c28009

Please sign in to comment.