Skip to content

Commit

Permalink
5.8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotcondon authored and remcotolsma committed Sep 12, 2019
1 parent b429a3f commit 9bcadd0
Show file tree
Hide file tree
Showing 11 changed files with 546 additions and 398 deletions.
596 changes: 267 additions & 329 deletions acf.php

Large diffs are not rendered by default.

261 changes: 213 additions & 48 deletions assets/js/acf-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,168 @@
});
};

/**
* acf.debounce
*
* Returns a debounced version of the passed function which will postpone its execution until after `wait` milliseconds have elapsed since the last time it was invoked.
*
* @date 28/8/19
* @since 5.8.1
*
* @param function callback The callback function.
* @return int wait The number of milliseconds to wait.
*/
acf.debounce = function( callback, wait ){
var timeout;
return function(){
var context = this;
var args = arguments;
var later = function(){
callback.apply( context, args );
};
clearTimeout( timeout );
timeout = setTimeout( later, wait );
};
};

/**
* acf.throttle
*
* Returns a throttled version of the passed function which will allow only one execution per `limit` time period.
*
* @date 28/8/19
* @since 5.8.1
*
* @param function callback The callback function.
* @return int wait The number of milliseconds to wait.
*/
acf.throttle = function( callback, limit ){
var busy = false;
return function(){
if( busy ) return;
busy = true;
setTimeout(function(){
busy = false;
}, limit);
callback.apply( this, arguments );
};
};

/**
* acf.isInView
*
* Returns true if the given element is in view.
*
* @date 29/8/19
* @since 5.8.1
*
* @param elem el The dom element to inspect.
* @return bool
*/
acf.isInView = function( el ){
var rect = el.getBoundingClientRect();
return (
rect.top !== rect.bottom &&
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
};

/**
* acf.onceInView
*
* Watches for a dom element to become visible in the browser and then excecutes the passed callback.
*
* @date 28/8/19
* @since 5.8.1
*
* @param dom el The dom element to inspect.
* @param function callback The callback function.
*/
acf.onceInView = (function() {

// Define list.
var items = [];
var id = 0;

// Define check function.
var check = function() {
items.forEach(function( item ){
if( acf.isInView(item.el) ) {
item.callback.apply( this );
pop( item.id );
}
});
};

// And create a debounced version.
var debounced = acf.debounce( check, 300 );

// Define add function.
var push = function( el, callback ) {

// Add event listener.
if( !items.length ) {
$(window).on( 'scroll resize', debounced ).on( 'acfrefresh orientationchange', check );
}

// Append to list.
items.push({ id: id++, el: el, callback: callback });
}

// Define remove function.
var pop = function( id ) {

// Remove from list.
items = items.filter(function(item) {
return (item.id !== id);
});

// Clean up listener.
if( !items.length ) {
$(window).off( 'scroll resize', debounced ).off( 'acfrefresh orientationchange', check );
}
}

// Define returned function.
return function( el, callback ){

// Allow jQuery object.
if( el instanceof jQuery )
el = el[0];

// Execute callback if already in view or add to watch list.
if( acf.isInView(el) ) {
callback.apply( this );
} else {
push( el, callback );
}
}
})();

/**
* acf.once
*
* Creates a function that is restricted to invoking `func` once.
*
* @date 2/9/19
* @since 5.8.1
*
* @param function func The function to restrict.
* @return function
*/
acf.once = function( func ){
var i = 0;
return function(){
if( i++ > 0 ) {
return (func = undefined);
}
return func.apply(this, arguments);
}
}

/*
* exists
*
Expand Down Expand Up @@ -6828,15 +6990,15 @@
getNodeValue: function(){
var $node = this.get('node');
return {
title: $node.html(),
title: acf.decode( $node.html() ),
url: $node.attr('href'),
target: $node.attr('target')
};
},

setNodeValue: function( val ){
var $node = this.get('node');
$node.html( val.title );
$node.text( val.title );
$node.attr('href', val.url);
$node.attr('target', val.target);
$node.trigger('change');
Expand Down Expand Up @@ -7205,8 +7367,7 @@
'change [data-filter]': 'onChangeFilter',
'keyup [data-filter]': 'onChangeFilter',
'click .choices-list .acf-rel-item': 'onClickAdd',
'click [data-name="remove_item"]': 'onClickRemove',
'mouseover': 'onHover'
'click [data-name="remove_item"]': 'onClickRemove',
},

$control: function(){
Expand Down Expand Up @@ -7252,60 +7413,59 @@
].join('');
},

addSortable: function( self ){

// sortable
this.$list('values').sortable({
items: 'li',
forceHelperSize: true,
forcePlaceholderSize: true,
scroll: true,
update: function(){
self.$input().trigger('change');
}
});
},

initialize: function(){

// scroll
var onScroll = this.proxy(function(e){
// Delay initialization until "interacted with" or "in view".
var delayed = this.proxy(acf.once(function(){

// Add sortable.
this.$list('values').sortable({
items: 'li',
forceHelperSize: true,
forcePlaceholderSize: true,
scroll: true,
update: this.proxy(function(){
this.$input().trigger('change');
})
});

// bail early if no more results
if( this.get('loading') || !this.get('more') ) {
return;
}
// Avoid browser remembering old scroll position and add event.
this.$list('choices').scrollTop(0).on('scroll', this.proxy(this.onScrollChoices));

// Scrolled to bottom
var $list = this.$list('choices');
var scrollTop = Math.ceil( $list.scrollTop() );
var scrollHeight = Math.ceil( $list[0].scrollHeight );
var innerHeight = Math.ceil( $list.innerHeight() );
var paged = this.get('paged') || 1;
if( (scrollTop + innerHeight) >= scrollHeight ) {

// update paged
this.set('paged', (paged+1));

// fetch
this.fetch();
}
// Fetch choices.
this.fetch();

});
}));

this.$list('choices').scrollTop(0).on('scroll', onScroll);
// Bind "interacted with".
this.$el.one( 'mouseover', delayed );
this.$el.one( 'focus', 'input', delayed );

// fetch
this.fetch();
// Bind "in view".
acf.onceInView( this.$el, delayed );
},

onHover: function( e ){

// only once
$().off(e);
onScrollChoices: function(e){

// bail early if no more results
if( this.get('loading') || !this.get('more') ) {
return;
}

// add sortable
this.addSortable( this );
// Scrolled to bottom
var $list = this.$list('choices');
var scrollTop = Math.ceil( $list.scrollTop() );
var scrollHeight = Math.ceil( $list[0].scrollHeight );
var innerHeight = Math.ceil( $list.innerHeight() );
var paged = this.get('paged') || 1;
if( (scrollTop + innerHeight) >= scrollHeight ) {

// update paged
this.set('paged', (paged+1));

// fetch
this.fetch();
}
},

onKeypressFilter: function( e, $el ){
Expand Down Expand Up @@ -13260,6 +13420,10 @@
*/
addInputEvents: function( $el ){

// Bug exists in Safari where custom "invalid" handeling prevents draft from saving.
if( acf.get('browser') === 'safari' )
return;

// vars
var $inputs = $('.acf-field [name]', $el);

Expand Down Expand Up @@ -13492,6 +13656,7 @@
clearTimeout( this.timeout );
this.timeout = setTimeout(function(){
acf.doAction('refresh');
$(window).trigger('acfrefresh');
}, 0);
}
});
Expand Down
8 changes: 4 additions & 4 deletions assets/js/acf-input.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions includes/acf-field-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ function acf_validate_field( $field = array() ) {
//'attributes' => array()
));

// Convert types.
$field['ID'] = (int) $field['ID'];
$field['menu_order'] = (int) $field['menu_order'];

// Add backwards compatibility for wrapper attributes.
// Todo: Remove need for this.
$field['wrapper'] = wp_parse_args($field['wrapper'], array(
Expand Down
6 changes: 6 additions & 0 deletions includes/acf-field-group-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ function acf_validate_field_group( $field_group = array() ) {
'description' => '',
));

// Convert types.
$field_group['ID'] = (int) $field_group['ID'];
$field_group['menu_order'] = (int) $field_group['menu_order'];

// Field group is now valid.
$field_group['_valid'] = 1;

Expand Down Expand Up @@ -515,6 +519,8 @@ function acf_update_field_group( $field_group ) {
'post_excerpt' => sanitize_title( $field_group['title'] ),
'post_content' => maybe_serialize( $_field_group ),
'menu_order' => $field_group['menu_order'],
'comment_status' => 'closed',
'ping_status' => 'closed',
);

// Unhook wp_targeted_link_rel() filter from WP 5.1 corrupting serialized data.
Expand Down
24 changes: 23 additions & 1 deletion includes/acf-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,26 @@ function acf_punctify( $str = '' ) {
return trim($str, '.') . '.';
}


/**
* acf_did
*
* Returns true if ACF already did an event.
*
* @date 30/8/19
* @since 5.8.1
*
* @param string $name The name of the event.
* @return bool
*/
function acf_did( $name ) {

// Return true if already did the event (preventing event).
if( acf_get_data("acf_did_$name") ) {
return true;

// Otherwise, update store and return false (alowing event).
} else {
acf_set_data("acf_did_$name", true);
return false;
}
}
Loading

0 comments on commit 9bcadd0

Please sign in to comment.