diff --git a/includes/helpers.php b/includes/helpers.php index 8cfe131..41148bb 100644 --- a/includes/helpers.php +++ b/includes/helpers.php @@ -105,3 +105,65 @@ function tni_is_subscription_only( $post ) { return strtotime( $subscription_date ) > strtotime( $today ); } + +/** + * Get Unauthorized Posts + * + * @uses tni_is_subscription_only() + * @uses tni_core_check_auth() + * @uses get_transient() + * @uses set_transient() + * @link https://codex.wordpress.org/Transients_API + * + * @since 1.2.9 + * + * @return array $posts | false + * + */ +function tni_core_get_unauthorized_posts() { + /* If user is authorized, all posts are shown regardless of whether they are "subscription_only" */ + if( !( function_exists( 'tni_core_check_auth' ) ) && $auth = tni_core_check_auth() ) { + return false; + } + + $posts = get_transient( 'tni_unauthorized_posts' ); + + if( false === $posts ) { + + $args = array( + 'fields' => 'ids', + 'posts_per_page' => -1 + ); + $query = new WP_Query( $args ); + $post_query = $query->get_posts(); + + if( !empty( $post_query ) || !is_wp_error( $post_query ) ) { + $posts = array_values( array_filter( $post_query, 'tni_is_subscription_only' ) ); + } + + set_transient( 'tni_unauthorized_posts', $posts, HOURS_IN_SECONDS * 12 ); + + } + + return $posts; +} + +/** + * Purge Transients + * Each time a post is published, delete the transient + * Currently, disabled but can be activated to delete transients upon publishing post + * Note: It will not run when a post is updated + * + * @uses delete_transient() + * + * @param int $ID + * @param obj $post + * + * @return void + */ +function tni_core_purge_transients( $ID, $post ) { + if( 'post' === $post->post_type ) { + delete_transient( 'tni_unauthorized_posts' ); + } +} +//add_action( 'publish_post', 'tni_core_purge_transients' ); diff --git a/readme.txt b/readme.txt index fbca792..fa4d936 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: misfist Tags: custom Requires at least: 4.7 Tested up to: 4.8 -Version: 1.2.8 +Version: 1.2.9 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -22,9 +22,12 @@ This section describes how to install the plugin and get it working. == Changelog == += 1.2.9 Aug 18, 2017 = +* Added function return array of unauthorized post IDs + = 1.2.8 July 6, 2017 = * #33 Fixed PHP error caused when `TNI_Core_Authorization` class is instantiated without passing required values `$file` and `$version` -* Added new functionality for subscriber-only content - In single.php template, add conditionals `tni_is_subscription_only` and `tni_core_check_auth` to serve alternate content to non-subscribers +* #77 & #78 Added new functionality for subscriber-only content - In single.php template, add conditionals `tni_is_subscription_only` and `tni_core_check_auth` to serve alternate content to non-subscribers * Added `subscriber_only_date` date field * Moved new Subscriber Only Content metabox below publish box on edit screen * Added `tni_is_subscription_only` that returns true or false if content is subscription-only today @@ -32,7 +35,7 @@ This section describes how to install the plugin and get it working. = 1.2.7 June 22, 2017 = * #29 Added `audio_url` custom field. - * To access value: `get_post_meta( post->ID, 'audio_url', true );` + * To access value: `get_post_meta( $post->ID, 'audio_url', true );` = 1.2.6 June 21, 2017 = * Show scheduled (future) single posts to authenticated users diff --git a/tni-core-functionality.php b/tni-core-functionality.php index af54f95..f96a6f5 100644 --- a/tni-core-functionality.php +++ b/tni-core-functionality.php @@ -10,7 +10,7 @@ * Text Domain: tni-core * Domain Path: /languages * - * Version: 1.2.8 + * Version: 1.2.9 * * @package Tni_Core_Functionality */ @@ -52,7 +52,7 @@ * @return object Tni_Core */ function Tni_Core() { - $instance = Tni_Core::instance( __FILE__, 'l.2.8' ); + $instance = Tni_Core::instance( __FILE__, 'l.2.9' ); return $instance; }