Skip to content

Commit

Permalink
Merge branch 'master' into colorpicker
Browse files Browse the repository at this point in the history
  • Loading branch information
mboynes committed Dec 31, 2015
2 parents 03b7beb + 6f64e74 commit 1e7abc8
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -11,10 +11,10 @@ php:
env:
- WP_VERSION=master WP_MULTISITE=0
- WP_VERSION=master WP_MULTISITE=1
- WP_VERSION=4.4 WP_MULTISITE=0
- WP_VERSION=4.4 WP_MULTISITE=1
- WP_VERSION=4.3 WP_MULTISITE=0
- WP_VERSION=4.3 WP_MULTISITE=1
- WP_VERSION=4.2 WP_MULTISITE=0
- WP_VERSION=4.2 WP_MULTISITE=1

matrix:
include:
Expand Down
53 changes: 41 additions & 12 deletions css/fieldmanager.css
Expand Up @@ -96,31 +96,48 @@ div.fm-group-label-wrapper {
clear: both;
}

.fmjs-drag-icon:before, .fmjs-remove:before, .fmjs-clear:before {
font-family: dashicons;
font-size: 15px;
line-height: 1;
color: #a0a5aa;
}

.fmjs-remove, .fmjs-clear {
margin: 7px 0 0 3px;
margin: 4px 0 0 5px;
display: block;
background: url('../images/xit.gif') 0 17% no-repeat;
height: 10px;
width: 10px;
height: 15px;
width: 15px;
float: left;
text-indent: -9999px;
text-decoration: none;
}

.fmjs-remove:before, .fmjs-clear:before {
content: "\f153";
}

.fmjs-remove:hover {
background-position: 100% 17%;
cursor: pointer;
}

.fmjs-remove:hover:before, .fmjs-clear:hover:before {
color: #d54e21;
}

.fmjs-drag-icon {
background: transparent url(../images/draggable.png) no-repeat 0px 0px;
height: 15px;
width: 15px;
margin: 3px 3px 0 0;
text-indent: -9999px;
float: left;
}

.fmjs-drag-icon:before {
content: "\f156";
}

.fmjs-drag-icon:hover {
background-position: 0px -20px;
cursor: move;
color: #777;
}

.fm-group div.fm-group-label-wrapper.fmjs-drag-header {
Expand Down Expand Up @@ -238,24 +255,36 @@ div.fm-group-label-wrapper .fmjs-remove {
display: block;
height: 20px;
padding: 5px;
background: #fff 4px 4px url(../images/grid.png) no-repeat;
width: 200px;
border: solid 1px #000;
padding-left: 30px;
margin: 5px 0;
text-decoration: none;
}

.grid-activate:before {
font-family: dashicons;
font-size: 17px;
line-height: 1;
margin-right: 5px;
content: '\f509';
vertical-align: middle;
color: #a0a5aa;
}

.grid-activate:hover {
border: solid 1px #d54e21;
}

.grid-activate:hover:before {
color: #777;
}

.with-grid .grid-activate:hover {
border: none;
}

.with-grid .grid-activate {
background: #fff;
padding-left: 0;
border: none;
}

Expand Down
Binary file removed images/draggable.png
Binary file not shown.
Binary file removed images/grid.png
Binary file not shown.
Binary file removed images/xit.gif
Binary file not shown.
36 changes: 35 additions & 1 deletion js/media/fieldmanager-media.js
Expand Up @@ -12,7 +12,8 @@ $( document ).on( 'click', '.media-wrapper a', function( event ){
$(this).closest('.media-wrapper').siblings('.fm-media-button').click();
} );
$( document ).on( 'click', '.fm-media-button', function( event ) {
var $el = $(this);
var $el = $(this),
library = {};
event.preventDefault();

// If the media frame already exists, reopen it.
Expand All @@ -21,8 +22,17 @@ $( document ).on( 'click', '.fm-media-button', function( event ) {
return;
}

// If mime type has been restricted, make sure the library only shows that
// type.
if ( $el.data( 'mime-type' ) && 'all' !== $el.data( 'mime-type' ) ) {
library.type = $el.data( 'mime-type' );
}

// Create the media frame.
fm_media_frame[ $el.attr('id') ] = wp.media({
// Set the library attributes.
library: library,

// Set the title of the modal.
title: $el.data('choose'),

Expand All @@ -33,6 +43,30 @@ $( document ).on( 'click', '.fm-media-button', function( event ) {
}
});

// If mime type has been restricted, make sure the library doesn't autoselect
// an uploaded file if it's the wrong mime type
if ( $el.data( 'mime-type' ) && 'all' !== $el.data( 'mime-type' ) ) {
// This event is only fired when a file is uploaded.
// @see {wp.media.controller.Library:uploading()}
fm_media_frame[ $el.attr('id') ].on( 'library:selection:add', function() {
// This event gets fired for every frame that has ever been created on
// the current page, which causes errors. We only care about the visible
// frame. Also, FM can change the ID of buttons, which means some older
// frames may no longer be valid.
if ( 'undefined' === typeof fm_media_frame[ $el.attr('id') ] || ! fm_media_frame[ $el.attr('id') ].$el.is(':visible') ) {
return;
}

// Get the Selection object and the currently selected attachment.
var selection = fm_media_frame[ $el.attr('id') ].state().get('selection'),
attachment = selection.first();
// If the mime type is wrong, deselect the file.
if ( attachment.attributes.type !== $el.data( 'mime-type' ) ) {
selection.remove(attachment);
}
});
}

// When an image is selected, run a callback.
fm_media_frame[ $el.attr('id') ].on( 'select', function() {
// Grab the selected attachment.
Expand Down
4 changes: 2 additions & 2 deletions php/class-fieldmanager-field.php
Expand Up @@ -923,15 +923,15 @@ public function add_another() {
* @return string
*/
public function get_sort_handle() {
return sprintf( '<div class="fmjs-drag fmjs-drag-icon">%s</div>', esc_html__( 'Move', 'fieldmanager' ) );
return sprintf( '<div class="fmjs-drag fmjs-drag-icon"><span class="screen-reader-text">%s</span></div>', esc_html__( 'Move', 'fieldmanager' ) );
}

/**
* Return HTML for the remove handle (multi-tools); a separate function to override
* @return string
*/
public function get_remove_handle() {
return sprintf( '<a href="#" class="fmjs-remove" title="%1$s">%1$s</a>', esc_attr__( 'Remove', 'fieldmanager' ) );
return sprintf( '<a href="#" class="fmjs-remove" title="%1$s"><span class="screen-reader-text">%1$s</span></a>', esc_attr__( 'Remove', 'fieldmanager' ) );
}

/**
Expand Down
14 changes: 12 additions & 2 deletions php/class-fieldmanager-media.php
Expand Up @@ -46,6 +46,14 @@ class Fieldmanager_Media extends Fieldmanager_Field {

/**
* @var string
* What mime types are available to choose from.
* Valid options are "all" or a partial or full mimetype (e.g. "image" or
* "application/pdf").
*/
public $mime_type = 'all';

/**
* @var boolean
* Static variable so we only load media JS once
*/
public static $has_registered_media = false;
Expand Down Expand Up @@ -112,7 +120,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" />
'<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="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 @@ -122,7 +130,9 @@ public function form_element( $value = array() ) {
$preview,
esc_attr( $this->preview_size ),
esc_attr( $this->modal_title ),
esc_attr( $this->modal_button_label )
esc_attr( $this->modal_button_label ),
esc_attr( $this->mime_type ),
$this->get_element_attributes()
);
}

Expand Down
21 changes: 14 additions & 7 deletions php/class-fieldmanager-options.php
Expand Up @@ -65,13 +65,20 @@ public function __construct( $label = '', $options = array() ) {
fm_add_style( 'fm_options_css', 'css/fieldmanager-options.css' );

// Sanitization
$this->sanitize = function( $value ) {
if ( isset( $value ) && is_array( $value ) && !empty( $value ) ) {
return array_map( 'sanitize_text_field', $value );
} else {
return sanitize_text_field( $value );
}
};
$this->sanitize = array( $this, 'sanitize' );
}

/**
* Sanitize function that can handle arrays as well as string values.
* @param array|string $value
* @return array|string Sanitized $value
*/
public function sanitize( $value ) {
if ( isset( $value ) && is_array( $value ) && ! empty( $value ) ) {
return array_map( 'sanitize_text_field', $value );
} else {
return sanitize_text_field( $value );
}
}

/**
Expand Down
7 changes: 5 additions & 2 deletions php/context/class-fieldmanager-context-quickedit.php
Expand Up @@ -154,7 +154,10 @@ public function render_ajax_form() {
$this->fm->data_id = $post_id;
$post_type = get_post_type( $post_id );

return $this->add_quickedit_box( $column_name, $post_type, $this->load() );
$this->add_quickedit_box( $column_name, $post_type, $this->load() );
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
exit;
}
}

/**
Expand Down Expand Up @@ -245,4 +248,4 @@ protected function delete_data( $post_id, $meta_key, $meta_value = '' ) {
return delete_post_meta( $post_id, $meta_key, $meta_value );
}

}
}
30 changes: 29 additions & 1 deletion tests/php/test-fieldmanager-media-field.php
Expand Up @@ -33,7 +33,7 @@ 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" />#',
'#<input type="button" class="[^"]*fm-media-button[^>]+value="%s" data-choose="%s" data-update="%s" data-preview-size="%s" data-mime-type="all" */>#',
$args['button_label'],
$args['modal_title'],
$args['modal_button_label'],
Expand All @@ -53,4 +53,32 @@ public function test_basic_save() {
$saved_data = get_post_meta( $this->post->ID, 'test_media', true );
$this->assertEquals( $test_data, $saved_data );
}

public function test_mime_type() {
$args = array(
'name' => 'test_media',
'mime_type' => 'image',
);

$fm = new Fieldmanager_Media( $args );
ob_start();
$fm->add_meta_box( 'Test Media', 'post' )->render_meta_box( $this->post, array() );
$html = ob_get_clean();
$this->assertRegExp( '/<input[^>]+type=[\'"]button[\'"][^>]+data-mime-type=[\'"]image[\'"]/', $html );
}

public function test_attributes() {
$args = array(
'name' => 'test_media',
'attributes' => array(
'data-test' => rand_str(),
),
);

$fm = new Fieldmanager_Media( $args );
ob_start();
$fm->add_meta_box( 'Test Media', 'post' )->render_meta_box( $this->post, array() );
$html = ob_get_clean();
$this->assertRegExp( '/<input[^>]+type=[\'"]button[\'"][^>]+data-test=[\'"]' . $args['attributes']['data-test'] . '[\'"]/', $html );
}
}
21 changes: 17 additions & 4 deletions tests/php/test-fieldmanager-richtextarea-field.php
Expand Up @@ -108,7 +108,20 @@ public function test_default_value() {
$fm->add_meta_box( 'Test RichTextArea', 'post' )->render_meta_box( $this->post, array() );
$html = ob_get_clean();

$this->assertRegExp( '/<textarea[^>]+>' . preg_quote( apply_filters( 'the_editor_content', $value ), '/' ) . "<\/textarea>/", $html );
if ( _fm_phpunit_is_wp_at_least( 4.3 ) ) {
// Core always adds this since 4.3. We'll do this too to match that
// functionality. It will be removed if this is at least 4.5.
add_filter( 'the_editor_content', 'format_for_editor', 10, 2 );
}

$this->assertRegExp( '/<textarea[^>]+>' . preg_quote( apply_filters( 'the_editor_content', $value, 'tinymce' ), '/' ) . "<\/textarea>/", $html );

// WordPress 4.5 fixed an issue with multiple editors and this filter.
// _WP_Editors::editor() now removes it after use, which is what we'll
// do here to match that functionality.
if ( _fm_phpunit_is_wp_at_least( 4.5 ) ) {
remove_filter( 'the_editor_content', 'format_for_editor' );
}
}

public function test_custom_buttons() {
Expand Down Expand Up @@ -373,9 +386,9 @@ public function test_editor_mode_conflicts() {
}

if ( _fm_phpunit_is_wp_at_least( 4.3 ) ) {
$this->assertContains( format_for_editor( $args['default_value'] ), $html_1 );
$this->assertContains( format_for_editor( $args['default_value'] ), $html_2 );
$this->assertContains( format_for_editor( $args['default_value'] ), $html_3 );
$this->assertContains( format_for_editor( $args['default_value'], 'html' ), $html_1 );
$this->assertContains( format_for_editor( $args['default_value'], 'tinymce' ), $html_2 );
$this->assertContains( format_for_editor( $args['default_value'], 'html' ), $html_3 );
} elseif ( _fm_phpunit_is_wp_at_least( 3.9 ) ) {
$this->assertContains( wp_htmledit_pre( $args['default_value'] ), $html_1 );
$this->assertContains( wp_richedit_pre( $args['default_value'] ), $html_2 );
Expand Down
8 changes: 4 additions & 4 deletions tests/php/test_fieldmanager_field.php
Expand Up @@ -488,7 +488,7 @@ public function test_multi_tools_in_group_without_label() {
// minimum count of 5
$field->minimum_count = 5;
$html = $this->_get_html_for( $field );
$this->assertEquals( 6, substr_count( $html, '<a href="#" class="fmjs-remove" title="Remove">Remove</a>' ) );
$this->assertEquals( 6, substr_count( $html, '<a href="#" class="fmjs-remove" title="Remove"><span class="screen-reader-text">Remove</span></a>' ) );
$this->assertEquals( 6, substr_count( $html, 'fmjs-drag-icon' ) );
}

Expand Down Expand Up @@ -555,7 +555,7 @@ public function test_multi_tools_in_group_with_label() {
// minimum count of 5
$field->minimum_count = 5;
$html = $this->_get_html_for( $field );
$this->assertEquals( 6, substr_count( $html, '<a href="#" class="fmjs-remove" title="Remove">Remove</a>' ) );
$this->assertEquals( 6, substr_count( $html, '<a href="#" class="fmjs-remove" title="Remove"><span class="screen-reader-text">Remove</span></a>' ) );
$this->assertEquals( 6, substr_count( $html, 'fmjs-drag-icon' ) );
}

Expand Down Expand Up @@ -603,7 +603,7 @@ public function test_multi_tools_in_field_without_label() {
// minimum count of 5
$field->minimum_count = 5;
$html = $this->_get_html_for( $field );
$this->assertEquals( 6, substr_count( $html, '<a href="#" class="fmjs-remove" title="Remove">Remove</a>' ) );
$this->assertEquals( 6, substr_count( $html, '<a href="#" class="fmjs-remove" title="Remove"><span class="screen-reader-text">Remove</span></a>' ) );
$this->assertEquals( 6, substr_count( $html, 'fmjs-drag-icon' ) );
}

Expand Down Expand Up @@ -656,7 +656,7 @@ public function test_multi_tools_in_field_with_label() {
// minimum count of 5
$field->minimum_count = 5;
$html = $this->_get_html_for( $field );
$this->assertEquals( 6, substr_count( $html, '<a href="#" class="fmjs-remove" title="Remove">Remove</a>' ) );
$this->assertEquals( 6, substr_count( $html, '<a href="#" class="fmjs-remove" title="Remove"><span class="screen-reader-text">Remove</span></a>' ) );
$this->assertEquals( 6, substr_count( $html, 'fmjs-drag-icon' ) );
}

Expand Down

0 comments on commit 1e7abc8

Please sign in to comment.