From 7c28009a7b60b1c34514799e68efc728f7c8e56a Mon Sep 17 00:00:00 2001 From: "Miguel A. Estrada" Date: Fri, 28 Dec 2018 14:01:37 -0800 Subject: [PATCH] Add functionality to pass "queryargs" to Media field --- js/media/fieldmanager-media.js | 6 ++++++ php/class-fieldmanager-media.php | 10 +++++++++- tests/php/test-fieldmanager-media-field.php | 6 ++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/js/media/fieldmanager-media.js b/js/media/fieldmanager-media.js index 2ac367b534..667b92d91a 100644 --- a/js/media/fieldmanager-media.js +++ b/js/media/fieldmanager-media.js @@ -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. diff --git a/php/class-fieldmanager-media.php b/php/class-fieldmanager-media.php index 93015872d3..84347c2fbf 100644 --- a/php/class-fieldmanager-media.php +++ b/php/class-fieldmanager-media.php @@ -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. * @@ -169,7 +176,7 @@ public function form_element( $value = array() ) { $preview = ''; } return sprintf( - ' + '
%5$s
', esc_attr( $this->get_element_id() ), @@ -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() ); diff --git a/tests/php/test-fieldmanager-media-field.php b/tests/php/test-fieldmanager-media-field.php index 4f379d4a44..d760fbcdc2 100644 --- a/tests/php/test-fieldmanager-media-field.php +++ b/tests/php/test-fieldmanager-media-field.php @@ -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' ); @@ -38,11 +39,12 @@ public function test_basic_render() { $html = ob_get_clean(); $this->assertRegExp( sprintf( - '#]+value="%s" data-choose="%s" data-update="%s" data-preview-size="%s" data-mime-type="all" */>#', + '#]+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 );