Skip to content

Commit 630745a

Browse files
authored
Merge pull request #757 from alleyinteractive/gutenberg-wp-domready
Use the new wp.domReady helper if it is available
2 parents 5942c28 + 7173784 commit 630745a

29 files changed

+147
-101
lines changed

.travis.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ language: php
22

33
matrix:
44
include:
5-
- php: 5.3
5+
- php: '5.3'
66
env: WP_VERSION=4.7.6
77
dist: precise
8-
- php: 5.3
8+
- php: '5.3'
99
env: WP_VERSION=latest
1010
dist: precise
11-
- php: 5.6
11+
- php: '5.6'
1212
env: WP_VERSION=latest
13-
- php: 5.6
13+
- php: '5.6'
1414
env: WP_TRAVISCI=phpcs
15-
- php: 7.0
15+
- php: '7.0'
1616
env: WP_VERSION=latest
17-
- php: 7.0
18-
env: WP_VERSION=nightly
19-
- php: 7.1
17+
- php: '7.1'
18+
env: WP_VERSION=latest
19+
- php: '7.2'
2020
env: WP_VERSION=latest
21-
- php: 7.2
21+
- php: '7.3'
2222
env: WP_VERSION=latest
23+
- php: '7.3'
24+
env: WP_VERSION=nightly
2325
fast_finish: true
24-
allow_failures:
25-
- php: 7.2
2626

2727
branches:
2828
only:
@@ -88,4 +88,4 @@ script:
8888
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
8989
cd $OG_DIR
9090
grunt phpcs
91-
fi
91+
fi

fieldmanager.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
* Fieldmanager Base Plugin File.
44
*
55
* @package Fieldmanager
6-
* @version 1.2.4
6+
* @version 1.2.5-beta2
77
*/
88

99
/*
1010
Plugin Name: Fieldmanager
1111
Plugin URI: https://github.com/alleyinteractive/wordpress-fieldmanager
1212
Description: Add fields to WordPress programatically.
1313
Author: Alley
14-
Version: 1.2.4
14+
Version: 1.2.5-beta2
1515
Author URI: https://www.alley.co/
1616
*/
1717

1818
/**
1919
* Current version of Fieldmanager.
2020
*/
21-
define( 'FM_VERSION', '1.2.4' );
21+
define( 'FM_VERSION', '1.2.5-beta2' );
2222

2323
/**
2424
* Filesystem path to Fieldmanager.
@@ -160,6 +160,17 @@ function fieldmanager_get_template( $tpl_slug ) {
160160
* @param bool $admin Deprecated.
161161
*/
162162
function fm_add_script( $handle, $path = false, $deps = array(), $ver = false, $in_footer = false, $data_object = '', $data = array(), $plugin_dir = '', $admin = true ) {
163+
// Ensure the Fieldmanager loader has been enqueued.
164+
Fieldmanager_Util_Assets::instance()->add_script(
165+
array(
166+
'handle' => 'fm_loader',
167+
'path' => 'js/fieldmanager-loader.js',
168+
'deps' => array( 'jquery' ),
169+
'ver' => FM_VERSION,
170+
)
171+
);
172+
173+
// Enqueue the requested script.
163174
Fieldmanager_Util_Assets::instance()->add_script( compact( 'handle', 'path', 'deps', 'ver', 'in_footer', 'data_object', 'data', 'plugin_dir' ) );
164175
}
165176

js/fieldmanager-autocomplete.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ fm.autocomplete = {
8080
}
8181
} );
8282
}
83-
}
83+
};
84+
85+
fmLoadModule( fm.autocomplete.enable_autocomplete );
8486

85-
$( document ).ready( fm.autocomplete.enable_autocomplete );
8687
$( document ).on( 'fm_collapsible_toggle fm_added_element fm_displayif_toggle fm_activate_tab', fm.autocomplete.enable_autocomplete );
8788

8889
} ) ( jQuery );

js/fieldmanager-colorpicker.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
init: function() {
55
$( '.fm-colorpicker-popup:visible' ).wpColorPicker();
66
}
7-
}
7+
};
8+
9+
fmLoadModule( fm.colorpicker.init );
810

9-
$( document ).ready( fm.colorpicker.init );
1011
$( document ).on( 'fm_collapsible_toggle fm_added_element fm_displayif_toggle fm_activate_tab', fm.colorpicker.init );
1112

12-
} )( jQuery );
13+
} )( jQuery );

js/fieldmanager-datepicker.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
}
99
} );
1010
}
11-
}
11+
};
12+
13+
fmLoadModule( fm.datepicker.add_datepicker );
1214

13-
$( document ).ready( fm.datepicker.add_datepicker );
1415
$( document ).on( 'fm_collapsible_toggle fm_added_element fm_displayif_toggle fm_activate_tab', fm.datepicker.add_datepicker );
15-
} ) ( jQuery );
16+
} ) ( jQuery );

js/fieldmanager-draggablepost.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(function($) {
2-
$(document).ready(function() {
2+
function draggablePostInit() {
33
resetEmptyMessages();
44
$('.sortables').sortable({
55
connectWith: '.sortables',
@@ -19,7 +19,7 @@
1919
setTimeout(function() { resetEmptyMessages(); populateHiddenElements(); }, 10);
2020
}
2121
});
22-
});
22+
}
2323
function resetEmptyMessages() {
2424
$('.post-bin').each(function(i) {
2525
if ($(this).find('.draggable-post').length > 0) {
@@ -40,4 +40,6 @@
4040
$('#' + input_name).val(post_ids.join(','));
4141
});
4242
}
43-
})(jQuery);
43+
44+
fmLoadModule( draggablePostInit );
45+
})(jQuery);

js/fieldmanager-group-tabs.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ var FieldmanagerGroupTabs;
88
*/
99
init: function() {
1010

11-
this.bindEvents();
11+
FieldmanagerGroupTabs.bindEvents();
1212

13-
this.restoreSelectedTabs();
13+
FieldmanagerGroupTabs.restoreSelectedTabs();
1414

1515
},
1616

@@ -164,10 +164,5 @@ var FieldmanagerGroupTabs;
164164

165165
};
166166

167-
$(document).ready( function(){
168-
169-
FieldmanagerGroupTabs.init();
170-
171-
});
172-
167+
fmLoadModule( FieldmanagerGroupTabs.init );
173168
} )( jQuery );

js/fieldmanager-loader.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* A wrapper for DOM ready handlers in the global wp object and jQuery,
3+
* with a shim fallback that mimics the behavior of wp.domReady.
4+
* Ensures that metaboxes have loaded before initializing functionality.
5+
* @param {function} callback - The callback function to execute when the DOM is ready.
6+
*/
7+
function fmLoadModule( callback ) {
8+
if ( 'object' === typeof wp && 'function' === typeof wp.domReady ) {
9+
wp.domReady( callback );
10+
} else if ( jQuery ) {
11+
jQuery( document ).ready( callback );
12+
} else {
13+
// Shim wp.domReady.
14+
if (
15+
document.readyState === 'complete' || // DOMContentLoaded + Images/Styles/etc loaded, so we call directly.
16+
document.readyState === 'interactive' // DOMContentLoaded fires at this point, so we call directly.
17+
) {
18+
callback();
19+
return;
20+
}
21+
22+
// DOMContentLoaded has not fired yet, delay callback until then.
23+
document.addEventListener( 'DOMContentLoaded', callback );
24+
}
25+
}

js/fieldmanager-post.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var fm_show_post_type = function( $element, post_type ) {
1212
var fm_show_post_date = function( $element, post_date ) {
1313
if ( $element.data( 'showPostDate' ) == 1 ) {
1414
$element.parent().siblings('.fmjs-post-date').remove();
15-
$element.parent().siblings(".fmjs-remove, .fmjs-clear").after('<div class="fmjs-post-date">' + post_date + '</div>');
15+
$element.parent().siblings(".fmjs-remove, .fmjs-clear").after('<div class="fmjs-post-date">' + post_date + '</div>');
1616
}
1717
}
1818

@@ -35,47 +35,47 @@ var fm_typeahead_action = function( $element ) {
3535
});
3636
}
3737
},
38-
updater: function ( item ) {
38+
updater: function ( item ) {
3939
// Get the post ID and post type and store them in data attributes
4040
$element.data( 'id', fm_typeahead_results[item]['id'] );
4141
$element.data( 'postType', fm_typeahead_results[item]['post_type'] );
4242
$element.data( 'postDate', fm_typeahead_results[item]['post_date'] );
43-
43+
4444
// If the clear handle is enabled, show it
4545
$element.parent().siblings('.fmjs-clear').show();
46-
46+
4747
// Show the selected post type and/or date after the clear/remove handle
4848
fm_show_post_type($element, fm_typeahead_results[item]['post_type']);
4949
fm_show_post_date($element, fm_typeahead_results[item]['post_date']);
50-
50+
5151
// Trigger that the update happened in case any other functions want to execute something here
5252
$element.trigger( 'fm_post_update', [item, fm_typeahead_results[item]] );
53-
53+
5454
// Remove the post type and/or date from the title and return the title for the text field
5555
return fm_typeahead_results[item]['post_title'];
5656
},
5757
items:5
5858
} );
5959
}
6060

61-
$( document ).ready( function () {
61+
function fm_post_init() {
6262
$( '.fm-post-element' ).each( function( index ) {
6363
// Enable typeahead for each post field
6464
fm_typeahead_action( $( this ) );
65-
65+
6666
// Show the post type, date and/or clear handle (if exists) if the field is not empty and those fields are specified for display
67-
if ( $( this ).data('postType') != '' ) {
67+
if ( $( this ).data('postType') != '' ) {
6868
fm_show_post_type( $( this ), $( this ).data('postType') );
6969
}
70-
71-
if ( $( this ).data('postDate') != '' ) {
70+
71+
if ( $( this ).data('postDate') != '' ) {
7272
fm_show_post_date( $( this ), $( this ).data('postDate') );
7373
}
74-
75-
if ( $( this ).data('id') != '' ) {
74+
75+
if ( $( this ).data('id') != '' ) {
7676
$( this ).parent().siblings('.fmjs-clear').show();
7777
}
78-
78+
7979
} );
8080
$( "#post" ).submit( function( e ) {
8181
$( '.fm-post-element' ).each( function( index ) {
@@ -90,6 +90,8 @@ $( document ).ready( function () {
9090
$post_element = $(event.target).find( '.fm-post-element' );
9191
if ( $post_element.length != 0 ) fm_typeahead_action( $post_element );
9292
} );
93-
} );
93+
}
94+
95+
fmLoadModule( fm_post_init );
9496

95-
} )( jQuery );
97+
} )( jQuery );

js/fieldmanager-quickedit.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
( function( $ ) {
22

3-
$( document ).ready( function() {
3+
function fm_quickedit_init() {
44
if ( typeof( inlineEditPost ) == 'undefined' ) {
55
return;
66
}
@@ -29,6 +29,7 @@
2929
} );
3030
}
3131
}
32-
} );
32+
}
3333

34-
} )( jQuery );
34+
fmLoadModule( fm_quickedit_init );
35+
} )( jQuery );

0 commit comments

Comments
 (0)