Skip to content

Commit eafedae

Browse files
committed
Add functionality to pass "queryargs" to Media field
1 parent 5942c28 commit eafedae

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

js/media/fieldmanager-media.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ $( document ).on( 'click', '.fm-media-button', function( event ) {
2828
library.type = $el.data( 'mime-type' );
2929
}
3030

31+
// Add query arguments to the media window request.
32+
// Gives "data-mime-type" priority over "mime-type" defined in "data-queryargs".
33+
if ( $el.data( 'queryargs' ) ) {
34+
library = $.extend( {}, $el.data( 'queryargs' ), library );
35+
}
36+
3137
// Create the media frame.
3238
fm_media_frame[ $el.attr('id') ] = wp.media({
3339
// Set the library attributes.

php/class-fieldmanager-media.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ class Fieldmanager_Media extends Fieldmanager_Field {
8585
*/
8686
public $mime_type = 'all';
8787

88+
/**
89+
* Query arguments to pass to wp.media window.
90+
*
91+
* @var array
92+
*/
93+
public $queryargs = array();
94+
8895
/**
8996
* Static variable so we only load media JS once.
9097
*
@@ -174,7 +181,7 @@ public function form_element( $value = array() ) {
174181
$preview = '';
175182
}
176183
return sprintf(
177-
'<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 />
184+
'<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 />
178185
<input type="hidden" name="%2$s" value="%4$s" class="fm-element fm-media-id" />
179186
<div class="media-wrapper">%5$s</div>',
180187
esc_attr( $this->get_element_id() ),
@@ -185,6 +192,7 @@ public function form_element( $value = array() ) {
185192
esc_attr( $this->preview_size ),
186193
esc_attr( $this->modal_title ),
187194
esc_attr( $this->modal_button_label ),
195+
esc_attr( wp_json_encode( $this->queryargs ) ),
188196
esc_attr( $this->mime_type ),
189197
$this->get_element_attributes()
190198
);

tests/php/test-fieldmanager-media-field.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function test_basic_render() {
3434
'selected_file_label' => rand_str(),
3535
'remove_media_label' => rand_str(),
3636
'preview_size' => rand_str(),
37+
'queryargs' => rand_str(),
3738
);
3839
$fm = new Fieldmanager_Media( $args );
3940
$context = $fm->add_meta_box( 'Test Media', 'post' );
@@ -43,11 +44,12 @@ public function test_basic_render() {
4344
$html = ob_get_clean();
4445
$this->assertRegExp(
4546
sprintf(
46-
'#<input type="button" class="[^"]*fm-media-button[^>]+value="%s" data-choose="%s" data-update="%s" data-preview-size="%s" data-mime-type="all" */>#',
47+
'#<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" */>#',
4748
$args['button_label'],
4849
$args['modal_title'],
4950
$args['modal_button_label'],
50-
$args['preview_size']
51+
$args['preview_size'],
52+
$args['queryargs']
5153
),
5254
$html
5355
);

0 commit comments

Comments
 (0)