From 58cc28193f4be478e48336de5397e3c6d46220d9 Mon Sep 17 00:00:00 2001 From: onpubcom Date: Wed, 23 Dec 2015 13:26:18 -0500 Subject: [PATCH 1/3] Fixes to how the 'ad_layers' WP option gets saved when an Ad Layer is updated, deleted, or restored from Trash. Also, added code to detect and remove orphaned Ad Layer Posts from the 'ad_layer' WP option array prior to any time it gets updated. --- php/class-ad-layers-post-type.php | 4 ++-- php/class-ad-layers.php | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/php/class-ad-layers-post-type.php b/php/class-ad-layers-post-type.php index dfcf0c7..763284c 100644 --- a/php/class-ad-layers-post-type.php +++ b/php/class-ad-layers-post-type.php @@ -281,7 +281,7 @@ public function save_post( $post_id, $post, $update ) { $ad_layers[ $position ] = $new_layer; - update_option( 'ad_layers', apply_filters( 'ad_layers_save_post', $ad_layers ) ); + update_option( 'ad_layers', apply_filters( 'ad_layers_save_post', array_values( $ad_layers ) ) ); } /** @@ -306,7 +306,7 @@ public function delete_post( $post_id ) { } } - update_option( 'ad_layers', apply_filters( 'ad_layers_delete_post', $ad_layers ) ); + update_option( 'ad_layers', apply_filters( 'ad_layers_delete_post', array_values( $ad_layers ) ) ); } } diff --git a/php/class-ad-layers.php b/php/class-ad-layers.php index 6a46cdc..f100f55 100644 --- a/php/class-ad-layers.php +++ b/php/class-ad-layers.php @@ -59,6 +59,9 @@ public function setup() { // Set the active ad layer before anything else. add_action( 'wp_head', array( $this, 'set_active_ad_layer' ), 1 ); + + // Check for Ad Layer Post orphans before ad_layers option gets saved + add_filter( 'pre_update_option_ad_layers', array( $this, 'maybe_remove_ad_layer_orphans' ), 10, 3 ); } /** @@ -415,6 +418,28 @@ public function get_current_page_type() { return apply_filters( 'ad_layers_current_page_type', $page_type ); } + + /** + * Finds and removes orphaned Ad Layers prior to the ad_layers WP option + * being updated in the database. + * + * @access public + * @param array $ad_layers + * @return array + */ + public function maybe_remove_ad_layer_orphans( $value, $old_value, $option ) { + $ad_layers = array(); + + if ( $option === 'ad_layers' ) { + foreach ( $value as $ad_layer ) { + if ( null !== get_post( $ad_layer['post_id'] ) ) { + $ad_layers[] = $ad_layer; + } + } + } + + return count( $value ) === count( $ad_layers ) ? $value : $ad_layers; + } } Ad_Layers::instance(); From 3d5f76f2b9d081e2b93fd918dc351c79282d5d0d Mon Sep 17 00:00:00 2001 From: onpubcom Date: Wed, 30 Dec 2015 14:11:39 -0500 Subject: [PATCH 2/3] Made maybe_remove_ad_layer_orphans() filter callback compatible with WP < 4.4 (no longer rely on new filter $option param added in WP 4.4). --- php/class-ad-layers.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/php/class-ad-layers.php b/php/class-ad-layers.php index f100f55..8735a50 100644 --- a/php/class-ad-layers.php +++ b/php/class-ad-layers.php @@ -61,7 +61,7 @@ public function setup() { add_action( 'wp_head', array( $this, 'set_active_ad_layer' ), 1 ); // Check for Ad Layer Post orphans before ad_layers option gets saved - add_filter( 'pre_update_option_ad_layers', array( $this, 'maybe_remove_ad_layer_orphans' ), 10, 3 ); + add_filter( 'pre_update_option_ad_layers', array( $this, 'maybe_remove_ad_layer_orphans' ), 10, 2 ); } /** @@ -427,14 +427,12 @@ public function get_current_page_type() { * @param array $ad_layers * @return array */ - public function maybe_remove_ad_layer_orphans( $value, $old_value, $option ) { + public function maybe_remove_ad_layer_orphans( $value, $old_value ) { $ad_layers = array(); - if ( $option === 'ad_layers' ) { - foreach ( $value as $ad_layer ) { - if ( null !== get_post( $ad_layer['post_id'] ) ) { - $ad_layers[] = $ad_layer; - } + foreach ( $value as $ad_layer ) { + if ( null !== get_post( $ad_layer['post_id'] ) ) { + $ad_layers[] = $ad_layer; } } From d507840fd8c2def6943b878ba2a2e4e2732a116a Mon Sep 17 00:00:00 2001 From: onpubcom Date: Tue, 26 Apr 2016 16:17:31 -0400 Subject: [PATCH 3/3] Linting fix so that upstream pull request (https://github.com/alleyinteractive/ad-layers/pull/26) passes Travis CI checks. --- php/class-ad-layers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/class-ad-layers.php b/php/class-ad-layers.php index 8735a50..fcf33e3 100644 --- a/php/class-ad-layers.php +++ b/php/class-ad-layers.php @@ -422,7 +422,7 @@ public function get_current_page_type() { /** * Finds and removes orphaned Ad Layers prior to the ad_layers WP option * being updated in the database. - * + * * @access public * @param array $ad_layers * @return array