Skip to content

Use the new wp.domReady helper if it is available #757

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

Merged
merged 16 commits into from
Apr 15, 2020
Merged
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
24 changes: 12 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ language: php

matrix:
include:
- php: 5.3
- php: '5.3'
env: WP_VERSION=4.7.6
dist: precise
- php: 5.3
- php: '5.3'
env: WP_VERSION=latest
dist: precise
- php: 5.6
- php: '5.6'
env: WP_VERSION=latest
- php: 5.6
- php: '5.6'
env: WP_TRAVISCI=phpcs
- php: 7.0
- php: '7.0'
env: WP_VERSION=latest
- php: 7.0
env: WP_VERSION=nightly
- php: 7.1
- php: '7.1'
env: WP_VERSION=latest
- php: '7.2'
env: WP_VERSION=latest
- php: 7.2
- php: '7.3'
env: WP_VERSION=latest
- php: '7.3'
env: WP_VERSION=nightly
fast_finish: true
allow_failures:
- php: 7.2

branches:
only:
Expand Down Expand Up @@ -88,4 +88,4 @@ script:
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
cd $OG_DIR
grunt phpcs
fi
fi
17 changes: 14 additions & 3 deletions fieldmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
* Fieldmanager Base Plugin File.
*
* @package Fieldmanager
* @version 1.2.4
* @version 1.2.5-beta2
*/

/*
Plugin Name: Fieldmanager
Plugin URI: https://github.com/alleyinteractive/wordpress-fieldmanager
Description: Add fields to WordPress programatically.
Author: Alley
Version: 1.2.4
Version: 1.2.5-beta2
Author URI: https://www.alley.co/
*/

/**
* Current version of Fieldmanager.
*/
define( 'FM_VERSION', '1.2.4' );
define( 'FM_VERSION', '1.2.5-beta2' );

/**
* Filesystem path to Fieldmanager.
Expand Down Expand Up @@ -160,6 +160,17 @@ function fieldmanager_get_template( $tpl_slug ) {
* @param bool $admin Deprecated.
*/
function fm_add_script( $handle, $path = false, $deps = array(), $ver = false, $in_footer = false, $data_object = '', $data = array(), $plugin_dir = '', $admin = true ) {
// Ensure the Fieldmanager loader has been enqueued.
Fieldmanager_Util_Assets::instance()->add_script(
array(
'handle' => 'fm_loader',
'path' => 'js/fieldmanager-loader.js',
'deps' => array( 'jquery' ),
'ver' => FM_VERSION,
)
);

// Enqueue the requested script.
Fieldmanager_Util_Assets::instance()->add_script( compact( 'handle', 'path', 'deps', 'ver', 'in_footer', 'data_object', 'data', 'plugin_dir' ) );
}

Expand Down
5 changes: 3 additions & 2 deletions js/fieldmanager-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ fm.autocomplete = {
}
} );
}
}
};

fmLoadModule( fm.autocomplete.enable_autocomplete );

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

} ) ( jQuery );
7 changes: 4 additions & 3 deletions js/fieldmanager-colorpicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
init: function() {
$( '.fm-colorpicker-popup:visible' ).wpColorPicker();
}
}
};

fmLoadModule( fm.colorpicker.init );

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

} )( jQuery );
} )( jQuery );
7 changes: 4 additions & 3 deletions js/fieldmanager-datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
}
} );
}
}
};

fmLoadModule( fm.datepicker.add_datepicker );

$( document ).ready( fm.datepicker.add_datepicker );
$( document ).on( 'fm_collapsible_toggle fm_added_element fm_displayif_toggle fm_activate_tab', fm.datepicker.add_datepicker );
} ) ( jQuery );
} ) ( jQuery );
8 changes: 5 additions & 3 deletions js/fieldmanager-draggablepost.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(function($) {
$(document).ready(function() {
function draggablePostInit() {
resetEmptyMessages();
$('.sortables').sortable({
connectWith: '.sortables',
Expand All @@ -19,7 +19,7 @@
setTimeout(function() { resetEmptyMessages(); populateHiddenElements(); }, 10);
}
});
});
}
function resetEmptyMessages() {
$('.post-bin').each(function(i) {
if ($(this).find('.draggable-post').length > 0) {
Expand All @@ -40,4 +40,6 @@
$('#' + input_name).val(post_ids.join(','));
});
}
})(jQuery);

fmLoadModule( draggablePostInit );
})(jQuery);
11 changes: 3 additions & 8 deletions js/fieldmanager-group-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ var FieldmanagerGroupTabs;
*/
init: function() {

this.bindEvents();
FieldmanagerGroupTabs.bindEvents();

this.restoreSelectedTabs();
FieldmanagerGroupTabs.restoreSelectedTabs();

},

Expand Down Expand Up @@ -164,10 +164,5 @@ var FieldmanagerGroupTabs;

};

$(document).ready( function(){

FieldmanagerGroupTabs.init();

});

fmLoadModule( FieldmanagerGroupTabs.init );
} )( jQuery );
25 changes: 25 additions & 0 deletions js/fieldmanager-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* A wrapper for DOM ready handlers in the global wp object and jQuery,
* with a shim fallback that mimics the behavior of wp.domReady.
* Ensures that metaboxes have loaded before initializing functionality.
* @param {function} callback - The callback function to execute when the DOM is ready.
*/
function fmLoadModule( callback ) {
if ( 'object' === typeof wp && 'function' === typeof wp.domReady ) {
wp.domReady( callback );
} else if ( jQuery ) {
jQuery( document ).ready( callback );
} else {
// Shim wp.domReady.
if (
document.readyState === 'complete' || // DOMContentLoaded + Images/Styles/etc loaded, so we call directly.
document.readyState === 'interactive' // DOMContentLoaded fires at this point, so we call directly.
) {
callback();
return;
}

// DOMContentLoaded has not fired yet, delay callback until then.
document.addEventListener( 'DOMContentLoaded', callback );
}
}
34 changes: 18 additions & 16 deletions js/fieldmanager-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var fm_show_post_type = function( $element, post_type ) {
var fm_show_post_date = function( $element, post_date ) {
if ( $element.data( 'showPostDate' ) == 1 ) {
$element.parent().siblings('.fmjs-post-date').remove();
$element.parent().siblings(".fmjs-remove, .fmjs-clear").after('<div class="fmjs-post-date">' + post_date + '</div>');
$element.parent().siblings(".fmjs-remove, .fmjs-clear").after('<div class="fmjs-post-date">' + post_date + '</div>');
}
}

Expand All @@ -35,47 +35,47 @@ var fm_typeahead_action = function( $element ) {
});
}
},
updater: function ( item ) {
updater: function ( item ) {
// Get the post ID and post type and store them in data attributes
$element.data( 'id', fm_typeahead_results[item]['id'] );
$element.data( 'postType', fm_typeahead_results[item]['post_type'] );
$element.data( 'postDate', fm_typeahead_results[item]['post_date'] );

// If the clear handle is enabled, show it
$element.parent().siblings('.fmjs-clear').show();

// Show the selected post type and/or date after the clear/remove handle
fm_show_post_type($element, fm_typeahead_results[item]['post_type']);
fm_show_post_date($element, fm_typeahead_results[item]['post_date']);

// Trigger that the update happened in case any other functions want to execute something here
$element.trigger( 'fm_post_update', [item, fm_typeahead_results[item]] );

// Remove the post type and/or date from the title and return the title for the text field
return fm_typeahead_results[item]['post_title'];
},
items:5
} );
}

$( document ).ready( function () {
function fm_post_init() {
$( '.fm-post-element' ).each( function( index ) {
// Enable typeahead for each post field
fm_typeahead_action( $( this ) );

// Show the post type, date and/or clear handle (if exists) if the field is not empty and those fields are specified for display
if ( $( this ).data('postType') != '' ) {
if ( $( this ).data('postType') != '' ) {
fm_show_post_type( $( this ), $( this ).data('postType') );
}
if ( $( this ).data('postDate') != '' ) {

if ( $( this ).data('postDate') != '' ) {
fm_show_post_date( $( this ), $( this ).data('postDate') );
}
if ( $( this ).data('id') != '' ) {

if ( $( this ).data('id') != '' ) {
$( this ).parent().siblings('.fmjs-clear').show();
}

} );
$( "#post" ).submit( function( e ) {
$( '.fm-post-element' ).each( function( index ) {
Expand All @@ -90,6 +90,8 @@ $( document ).ready( function () {
$post_element = $(event.target).find( '.fm-post-element' );
if ( $post_element.length != 0 ) fm_typeahead_action( $post_element );
} );
} );
}

fmLoadModule( fm_post_init );

} )( jQuery );
} )( jQuery );
7 changes: 4 additions & 3 deletions js/fieldmanager-quickedit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
( function( $ ) {

$( document ).ready( function() {
function fm_quickedit_init() {
if ( typeof( inlineEditPost ) == 'undefined' ) {
return;
}
Expand Down Expand Up @@ -29,6 +29,7 @@
} );
}
}
} );
}

} )( jQuery );
fmLoadModule( fm_quickedit_init );
} )( jQuery );
8 changes: 5 additions & 3 deletions js/fieldmanager-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fm_reset_chosen = function( $fm_text_field, fm_text_field_val ) {
} );
}

$( document ).ready( function() {
function fm_select_init() {

// Track changes to the chosen text field linked to the select in order to update options via Ajax
// Used for taxonomy-based fields where preload is disabled
Expand Down Expand Up @@ -99,6 +99,8 @@ $( document ).ready( function() {
if( $this_select_field.data("taxonomy") != "" && $this_select_field.data("taxonomyPreload") == false ) fm_select_clear_terms( $this_select_field, $(this).parents('.chosen-choices'), true );
} );

} );
}

fmLoadModule( fm_select_init );

} )( jQuery );
} )( jQuery );
6 changes: 4 additions & 2 deletions js/fieldmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fm_remove = function( $element ) {
fm_renumber( $wrapper );
}

$( document ).ready( function () {
var fm_init = function () {
$( document ).on( 'click', '.fm-add-another', function( e ) {
e.preventDefault();
fm_add_another( $( this ) );
Expand Down Expand Up @@ -276,6 +276,8 @@ $( document ).ready( function () {
init_sortable();

$( document ).on( 'fm_activate_tab', init_sortable );
} );
};

fmLoadModule( fm_init );

} )( jQuery );
6 changes: 2 additions & 4 deletions js/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ $( document ).on( 'fm_collapsible_toggle fm_added_element fm_displayif_toggle fm
$.fm_grid_init();
} );

$( document ).ready( function() {
$.fm_grid_init();
} );
fmLoadModule( $.fm_grid_init );

})( jQuery );
})( jQuery );
16 changes: 9 additions & 7 deletions js/richtext.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,12 @@
* On document.load, init the editors and make the global meta box drag-drop
* event reload the editors.
*/
$( function() {
fm.richtextarea.add_rte_to_visible_textareas();
$( '.meta-box-sortables' ).on( 'sortstop', function( e, obj ) {
fm.richtextarea.reload_editors( e, obj.item[0] );
} );
} );
} ) ( jQuery );
fmLoadModule(
function() {
fm.richtextarea.add_rte_to_visible_textareas();
$( '.meta-box-sortables' ).on( 'sortstop', function( e, obj ) {
fm.richtextarea.reload_editors( e, obj.item[0] );
} );
}
);
} ) ( jQuery );
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "Fieldmanager",
"description": "Fieldmanager is a comprehensive toolkit for building forms, metaboxes, and custom admin screens for WordPress.",
"version": "1.2.4",
"repository" : {
"type" : "git",
"url" : "https://github.com/alleyinteractive/wordpress-fieldmanager"
"version": "1.2.5-beta2",
"repository": {
"type": "git",
"url": "https://github.com/alleyinteractive/wordpress-fieldmanager"
},
"license": "GPL-2.0",
"devDependencies": {
Expand Down
Loading