Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to pass "queryargs" to Media field #732

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -174,7 +181,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 @@ -185,6 +192,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 @@ -34,6 +34,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 @@ -43,11 +44,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