From 412e344af697959070f6907c3de439687ab1b1ed Mon Sep 17 00:00:00 2001 From: Connor Jennings Date: Tue, 12 Mar 2013 17:26:52 -0400 Subject: [PATCH 1/3] Add filter to limit number of posts on zones. --- js/zoninator.js | 21 +++++++++++++++++++-- zoninator.php | 6 ++++++ 2 files changed, 25 insertions(+), 2 deletions(-) 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..41355ab 100644 --- a/zoninator.php +++ b/zoninator.php @@ -560,6 +560,8 @@ 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 = apply_filters( 'zoninator_post_count_limit', $num_posts = 0, $zone_id ); // Verify nonce $this->verify_nonce( $this->zone_ajax_nonce_action ); @@ -568,6 +570,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 ); From 9e5a3ed7a8d899dfca3b958506ad80e03d83e2a3 Mon Sep 17 00:00:00 2001 From: Connor Jennings Date: Tue, 12 Mar 2013 17:39:15 -0400 Subject: [PATCH 2/3] Just pass the zone_id, not the number of posts to allow. --- zoninator.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zoninator.php b/zoninator.php index 41355ab..0bb7fb1 100644 --- a/zoninator.php +++ b/zoninator.php @@ -561,7 +561,8 @@ 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 = apply_filters( 'zoninator_post_count_limit', $num_posts = 0, $zone_id ); + $num_posts = 0; + $num_posts = apply_filters( 'zoninator_post_count_limit', $zone_id ); // Verify nonce $this->verify_nonce( $this->zone_ajax_nonce_action ); From 97992ff31021091b861fad4b7f2632f07e3aba4c Mon Sep 17 00:00:00 2001 From: Connor Jennings Date: Tue, 12 Mar 2013 19:32:57 -0400 Subject: [PATCH 3/3] Need to pass num_posts to filter since that's the default value that will return (and the one that should be technically modified). --- zoninator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zoninator.php b/zoninator.php index 0bb7fb1..b7a915c 100644 --- a/zoninator.php +++ b/zoninator.php @@ -562,8 +562,8 @@ function ajax_add_post() { $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', $zone_id ); - + $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 );