Skip to content

Commit

Permalink
#38 Checkbox for subscriber only content
Browse files Browse the repository at this point in the history
* Added `subscriber_only` boolean field
* Updated `tni_is_subscription_only` function to fetch posts where `subscriber_only` is true
  • Loading branch information
misfist authored Oct 10, 2017
2 parents a34e5c8 + cbe6d06 commit 55ab6d7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
21 changes: 20 additions & 1 deletion includes/class-tni-core-custom-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,29 @@ public function register_field_groups() {
'menu_order' => 0,
));

register_field_group( array (
register_field_group( array(
'key' => 'group_subscriber_content',
'title' => __( 'Subscriber Only Content', 'tni-core' ),
'fields' => array (
array (
'key' => 'field_subscriber_only',
'label' => __( 'Subscriber Only', 'tni-core' ),
'name' => 'subscriber_only',
'type' => 'true_false',
'instructions' => __( 'Viewable by Subscribers Only?', 'tni-core' ),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => __( 'True', 'tni-core' ),
'ui_off_text' => __( 'False', 'tni-core' ),
),
array (
'key' => 'field_subscriber_only_date',
'label' => __( 'Date', 'tni-core' ),
Expand Down
13 changes: 6 additions & 7 deletions includes/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,22 @@ function tni_get_users_meta( $field = null ) {
* Checks if content is subscription only today
*
* @since 1.2.8
* @since 1.2.13
*
* @param int $post
* @return bool true|false
*/
function tni_is_subscription_only( $post ) {
$post = (int) $post;

$subscription_date = get_post_meta( $post, 'subscriber_only_date', true );
/* We could just return `get_post_meta( $post, 'subscriber_only', true )`, but I think this is more readable */
$subscription_only = get_post_meta( $post, 'subscriber_only', true );

if( !isset( $subscription_date ) || empty( $subscription_date ) ) {
if( !isset( $subscription_only ) || empty( $subscription_only ) ) {
return false;
}

$today = date( 'Ymd' );

return strtotime( $subscription_date ) > strtotime( $today );
return true;
}

/**
Expand Down Expand Up @@ -141,7 +141,7 @@ function tni_core_get_unauthorized_posts() {
$posts = array_values( array_filter( $post_query, 'tni_is_subscription_only' ) );
}

set_transient( 'tni_unauthorized_posts', $posts, MINUTE_IN_SECONDS * 5);
set_transient( 'tni_unauthorized_posts', $posts, MINUTE_IN_SECONDS * 5 );

}

Expand All @@ -151,7 +151,6 @@ function tni_core_get_unauthorized_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()
Expand Down
9 changes: 7 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Contributors: misfist
Tags: custom
Requires at least: 4.7
Tested up to: 4.8
Version: 1.2.12
Tested up to: 4.8.2
Version: 1.2.13
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -22,6 +22,11 @@ This section describes how to install the plugin and get it working.

== Changelog ==

= 1.2.13 October 10, 2017 =
* #38 Checkbox for subscriber only content @link https://github.com/thenewinquiry/tni-core-functionality/issues/38
* Added `subscriber_only` boolean field
* Updated `tni_is_subscription_only` function to fetch posts where `subscriber_only` is true

= 1.2.12 October 3, 2017 =
* Added tags (`post_tag`) taxonomy to blogs post type @link https://github.com/thenewinquiry/tni-theme/issues/91

Expand Down
4 changes: 2 additions & 2 deletions tni-core-functionality.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Text Domain: tni-core
* Domain Path: /languages
*
* Version: 1.2.12
* Version: 1.2.13
*
* @package Tni_Core_Functionality
*/
Expand Down Expand Up @@ -52,7 +52,7 @@
* @return object Tni_Core
*/
function Tni_Core() {
$instance = Tni_Core::instance( __FILE__, 'l.2.12' );
$instance = Tni_Core::instance( __FILE__, 'l.2.13' );

return $instance;
}
Expand Down

0 comments on commit 55ab6d7

Please sign in to comment.