diff --git a/js/zoninator.js b/js/zoninator.js index d669aa2..bbb4612 100644 --- a/js/zoninator.js +++ b/js/zoninator.js @@ -53,7 +53,6 @@ var zoninator = {} post_id = $this.val(); if ( post_id ) { zoninator.addPost( post_id ); - $this.find( '[value="' + post_id + '"]' ).remove(); } }); @@ -136,11 +135,13 @@ var zoninator = {} zoninator.ajax('add_post', { zone_id: zoninator.getZoneId() , post_id: postId - }, zoninator.addPostSuccessCallback); + }, zoninator.addPostSuccessCallback + , zoninator.addPostErrorCallback ); } zoninator.addPostSuccessCallback = function(returnData) { + var post_id = zoninator.$zonePostLatest.val(); zoninator.$zonePostSearch.trigger('loading.end'); @@ -152,10 +153,26 @@ var zoninator = {} ; zoninator.initZonePost($post); + + //Update list only if a post was actually added to the zone + zoninator.$zonePostLatest.find( '[value="' + post_id + '"]' ).remove(); // Reorder Posts zoninator.updatePostOrder(true); } + + /* Override the callback so specific addPostError related functionality + * can be added. (ie: reset selectedIndex and trigger load.end). A user + * shouldn't be given too many errors if they didn't do anything "technically wrong." + */ + zoninator.addPostErrorCallback = function(returnData) { + if( typeof(returnData.content) === 'undefined' || !returnData.content ) + returnData.content = zoninatorOptions.errorGeneral; + alert(returnData.content); + + zoninator.$zonePostLatest.val( zoninator.$zonePostLatest.prop( 'selectedIndex', 0 ) ); + zoninator.$zonePostSearch.trigger('loading.end'); + } zoninator.initZonePost = function($elem) { $elem.bind('loading.start', function(e) { diff --git a/zoninator.php b/zoninator.php index 7596edb..b7a915c 100644 --- a/zoninator.php +++ b/zoninator.php @@ -560,7 +560,10 @@ function ajax_return( $status, $content = '', $action = '' ) { function ajax_add_post() { $zone_id = $this->_get_post_var( 'zone_id', 0, 'absint' ); $post_id = $this->_get_post_var( 'post_id', 0, 'absint' ); - + //Filter to limit depending on $zone_id passed (or not passed to filter all) + $num_posts = 0; + $num_posts = apply_filters( 'zoninator_post_count_limit', $num_posts, $zone_id ); + // Verify nonce $this->verify_nonce( $this->zone_ajax_nonce_action ); $this->verify_access( '', $zone_id ); @@ -568,6 +571,10 @@ function ajax_add_post() { // Validate if( ! $zone_id || ! $post_id ) $this->ajax_return( 0 ); + + //Check number of posts allowed + if($num_posts > 0 && $num_posts <= count( $this->get_zone_posts( $zone_id ) ) ) + $this->ajax_return(0, __( 'Sorry, another item cannot be added to this zone.', 'zoninator' ) ); $result = $this->add_zone_posts( $zone_id, $post_id, true );