Skip to content

Instantly share code, notes, and snippets.

@stingray82
Created May 2, 2025 22:40
Show Gist options
  • Select an option

  • Save stingray82/c23980772851eab0c91195ba087fef4b to your computer and use it in GitHub Desktop.

Select an option

Save stingray82/c23980772851eab0c91195ba087fef4b to your computer and use it in GitHub Desktop.
SureCart Post Access Manager - Post Expiry Scheduler - Helper Plugin
<?php
/**
* Plugin Name: Post Access Expiry Scheduler
* Description: Separately schedules and handles expiry of post access granted by the main Post Access Manager plugin, with enable/disable toggle and debug logging, linked to the main plugin
* Version: 1.0.0
* Author: Reallyusefulplugins.com
* Text Domain: rup-pamsc-access-expiry
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Option names
*/
define( 'PAE_OPTION_DAYS', 'pae_expiry_days' );
define( 'PAE_OPTION_ENABLED', 'pae_enabled' );
/**
* Add settings page under Settings → Post Access Expiry
*/
add_action( 'admin_menu', function() {
add_options_page(
'Post Access Expiry Settings',
'Post Access Expiry',
'manage_options',
'post-access-expiry',
function() {
?>
<div class="wrap">
<h1>Post Access Expiry</h1>
<form method="post" action="options.php">
<?php
settings_fields( 'post_access_expiry' );
do_settings_sections( 'post-access-expiry' );
$days = get_option( PAE_OPTION_DAYS, 30 );
$enabled = get_option( PAE_OPTION_ENABLED, '1' );
?>
<table class="form-table">
<tr>
<th scope="row"><label for="<?php echo PAE_OPTION_ENABLED; ?>">Enable Expiry</label></th>
<td>
<input name="<?php echo PAE_OPTION_ENABLED; ?>" type="checkbox" id="<?php echo PAE_OPTION_ENABLED; ?>" value="1" <?php checked( $enabled, '1' ); ?> />
<p class="description">When checked, access expiry crons will be scheduled and run. Uncheck to disable expiry.</p>
</td>
</tr>
<tr>
<th scope="row"><label for="<?php echo PAE_OPTION_DAYS; ?>">Expiry interval (days)</label></th>
<td>
<input name="<?php echo PAE_OPTION_DAYS; ?>" type="number" id="<?php echo PAE_OPTION_DAYS; ?>" value="<?php echo esc_attr( $days ); ?>" min="1" class="small-text" />
<p class="description">Remove access this many days after it was granted.</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
);
} );
add_action( 'admin_init', function() {
register_setting( 'post_access_expiry', PAE_OPTION_DAYS, [
'type' => 'integer',
'sanitize_callback' => 'absint',
'default' => 30,
] );
register_setting( 'post_access_expiry', PAE_OPTION_ENABLED, [
'type' => 'string',
'sanitize_callback' => function( $v ) { return $v === '1' ? '1' : '0'; },
'default' => '1',
] );
} );
/**
* Helper for logging only when main plugin debug enabled
*/
function pae_log( $message ) {
if ( function_exists( 'rup_sell_ind_posts_log_debug' ) ) {
rup_sell_ind_posts_log_debug( "[PAE] " . $message );
}
}
/**
* Schedule removal when main plugin fires this action.
* Respects the enable/disable setting.
*/
add_action( 'rup_sell_ind_posts_access_granted', 'pae_schedule_access_removal', 10, 1 );
function pae_schedule_access_removal( $payload ) {
if ( get_option( PAE_OPTION_ENABLED, '1' ) !== '1' ) {
pae_log( "Expiry disabled; not scheduling for user {$payload['user_id']}, post {$payload['post_id']}" );
return;
}
$user_id = absint( $payload['user_id'] );
$post_id = absint( $payload['post_id'] );
$days = get_option( PAE_OPTION_DAYS, 30 );
if ( $days < 1 ) {
$days = 30;
}
if ( ! wp_next_scheduled( 'pae_remove_access_event', [ $user_id, $post_id ] ) ) {
wp_schedule_single_event(
time() + ( $days * DAY_IN_SECONDS ),
'pae_remove_access_event',
[ $user_id, $post_id ]
);
pae_log( "Scheduled removal in {$days} days for user {$user_id}, post {$post_id}" );
} else {
pae_log( "Removal already scheduled for user {$user_id}, post {$post_id}" );
}
}
/**
* When the scheduled event fires, remove post_id from user's allowed_posts if enabled.
* Normalize allowed_posts to integers before checking.
*/
add_action( 'pae_remove_access_event', 'pae_handle_remove_access', 10, 2 );
function pae_handle_remove_access( $user_id, $post_id ) {
pae_log( "Handler triggered for user {$user_id}, post {$post_id}" );
if ( get_option( PAE_OPTION_ENABLED, '1' ) !== '1' ) {
pae_log( "Expiry disabled; handler exiting." );
return;
}
$allowed = get_user_meta( $user_id, 'allowed_posts', true );
if ( ! is_array( $allowed ) ) {
pae_log( "allowed_posts meta is not an array; exiting." );
return;
}
// Normalize to ints
$allowed = array_map( 'intval', $allowed );
pae_log( "Normalized allowed_posts: " . implode( ', ', $allowed ) );
if ( in_array( $post_id, $allowed, true ) ) {
$allowed = array_diff( $allowed, [ $post_id ] );
update_user_meta( $user_id, 'allowed_posts', array_values( $allowed ) );
pae_log( "Removed post {$post_id} from user {$user_id}" );
} else {
pae_log( "Post {$post_id} not found in user {$user_id} allowed_posts." );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment