Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make Woocommerce Membership public content go private after a certain amount of time.
<?php
/*
To allow temporary access to content with Woocommerce Membership,
you have to check this checkbox : "Check this box if you want to
force the content to be public regardless of any restriction rules
that may apply now or in the future."
Here is a way to make this access temporary without having to edit
you post to uncheck the checkbox each and every day on each new publicly
released content. Let the WordPress cron job uncheck it for you :)
DO NOT USE AS PROVIDED : please, adapt it to your own needs !
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Class_Name {
public function __construct() {
// Hook the function 'hourly_cron_jobs', into the action 'my_hourly_cron_jobs_hook'
add_action( 'my_hourly_cron_jobs_hook', array( &$this, 'hourly_cron_jobs' ) );
}
public static function activate() {
// On plugin activation schedule our hourly automatic access restriction after the first 24 free hours
// Use wp_next_scheduled to check if the event is already scheduled
$timestamp = wp_next_scheduled( 'my_hourly_cron_jobs_hook' );
// If $timestamp == false schedule hourly access restriction since it hasn't been done previously
if ( false == $timestamp ){
// Schedule the event for right now, then to repeat hourly using the hook 'my_hourly_cron_jobs_hook'
wp_schedule_event( time(), 'hourly', 'my_hourly_cron_jobs_hook' );
}
}
public static function deactivate() {
wp_clear_scheduled_hook( 'my_hourly_cron_jobs_hook' );
}
public function hourly_cron_jobs() {
// Posts list that should be set as private
$args = array(
'posts_per_page' => 20,
'meta_key' => '_wc_memberships_force_public',
'meta_value' => 'yes',
'post_type' => 'post',
'post_status' => 'publish',
// Excluded categories if any...
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => array( 27, 33, 15721 ),
'operator' => 'NOT IN',
),
),
// Free access period of time. Here, 24h :
'date_query' => array(
array(
'before' => '24 hours ago',
)
),
);
$ps = get_posts( $args );
if ( $ps ) {
//$message = 'New limited access post IDs : ';
foreach ( $ps as $p ) {
//$message .= $p->ID . ', ';
update_post_meta( $p->ID, '_wc_memberships_force_public', 'no', 'yes' );
}
// Send an alert by email so you can check everything is fine :)
//wp_mail( 'name@domain.com', 'Cron alert', $message );
}
}
}
register_activation_hook( __FILE__, array( 'Class_Name', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'Class_Name', 'deactivate' ) );
$instance = new Class_Name();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.