PriMoThemes — now s2Member® (official notice)
This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™
function grccertify_check_for_expired_capabilities($siteuser = NULL)
{
date_default_timezone_set("America/Phoenix");
$start_date_regex = "/beg[0-9]+/i";
$end_date_regex = "/end[0-9]+/i";
$allusers = NULL;
if ($siteuser == NULL) {
// check all users
$allusers = get_users_of_blog();
//error_log("all users of the blog: " . print_r($allusers, TRUE), 0);
if ($allusers) {
foreach ($allusers as $singleuser) {
error_log("processing user: " . $singleuser->ID, 0);
$user = new WP_User( $singleuser->ID );
//error_log("all user info: " . print_r($user, TRUE), 0);
//error_log(" -- allcaps: " . print_r($user->allcaps, TRUE), 0);
// check the list for any timed capabilities in the form:
// capability_name_begyyymmdd_endyyymmdd
if ($user->allcaps) {
foreach ($user->allcaps as $single_capability => $single_capability_value){
$cap_to_process = $single_capability;
//error_log("capability to process: " . print_r($cap_to_process, TRUE), 0);
$end_date = null;
$start_date = null;
$capability = null;
// parse the capability
// get and trim off end date
if ( preg_match($end_date_regex, $cap_to_process, $end_date_string_array) ) {
//error_log("getting end_date: " . $end_date_string_array[0] . " from: " . print_r($cap_to_process, TRUE), 0);
$end_date = grccertify_string_to_date( $end_date_string_array[0] );
//error_log("end_date: " . print_r($end_date, TRUE), 0);
//error_log("trimming end_date from: " . print_r($cap_to_process, TRUE), 0);
$cap_to_process = str_replace ("_" . $end_date_string_array[0], "", $cap_to_process);
//error_log("end_date trimmed and capability string is now: " . print_r($cap_to_process, TRUE), 0);
}
// get and trim off start date
if ( preg_match($start_date_regex, $cap_to_process, $start_date_string_array) ) {
//error_log("getting start_date: " . $start_date_string_array[0] . " from: " . print_r($cap_to_process, TRUE), 0);
$start_date = grccertify_string_to_date( $start_date_string_array[0] );
//error_log("start_date: " . print_r($start_date, TRUE), 0);
//error_log("trimming start_date from: " . print_r($cap_to_process, TRUE), 0);
$cap_to_process = str_replace ("_" . $start_date_string_array[0] , "", $cap_to_process);
//error_log("start_date trimmed and capability string is now: " . print_r($cap_to_process, TRUE), 0);
}
// you are left with just the capability
$capability = $cap_to_process;
if ( $start_date || $end_date ) {
//error_log("capability to add or remove is: " . print_r($capability, TRUE), 0);
//error_log("start_date is: " . $start_date, 0);
//error_log("end_date is: " . $end_date, 0);
//error_log("now is: " . strtotime ("now"), 0);
// ensure that the capability_name is removed if the start-end range is not met
if ( strtotime ("now") > $end_date )
{
error_log("AFTER END DATE. Removing capability: " . print_r($capability, TRUE) . " from user ID: " . print_r($user->ID, TRUE), 0);
$user->remove_cap ($capability);
}
if ( strtotime ("now") < $start_date )
{
error_log("BEFORE START DATE. Removing capability: " . print_r($capability, TRUE) . " from user ID: " . print_r($user->ID, TRUE), 0);
$user->remove_cap ($capability);
}
// ensure that the capability_name is present if the start-end range is met
if ( (strtotime ("now") >= $start_date) && ($end_date == null) )
{
error_log("MET START DATE and NO END DATE. Adding capability: " . print_r($capability, TRUE) . " to user ID: " . print_r($user->ID, TRUE), 0);
$user->add_cap ($capability);
}
if ( (strtotime ("now") >= $start_date) && (strtotime ("now") <= $end_date) )
{
error_log("BETWEEN START DATE AND END DATE. Adding capability: " . print_r($capability, TRUE) . " to user ID: " . print_r($user->ID, TRUE), 0);
$user->add_cap ($capability);
}
if ( (strtotime ("now") <= $end_date) && ($start_date == null) )
{
error_log("NO START DATE AND BEFORE END DATE. Adding capability: " . print_r($capability, TRUE) . " to user ID: " . print_r($user->ID, TRUE), 0);
$user->add_cap ($capability);
}
}
else {
// this is not a timed capability, do nothing
error_log("capability is not timed: " . print_r($capability, TRUE), 0);
}
}
}
else {
error_log("no capabilities found for user:" . print_r($user->ID, TRUE), 0);
}
}
}
}
else {
// TODO: just check a single user
// TODO: make subfunction below
}
}
function grccertify_string_to_date ( $date_as_string ) {
//error_log("converting string: " . print_r($date_as_string, TRUE), 0);
$year = substr($date_as_string, 3, 4);
$month = substr($date_as_string, 7, 2);
$day = substr($date_as_string, 9, 2);
//create a date
$date_to_return = strtotime( $year . "-" . $month . "-" . $day );
//error_log("date to return: " . print_r($date_to_return, TRUE), 0);
return $date_to_return;
}
[code=php]Lorem <?php echo 'Ipsum'; ?>[/code]
$start_date_regex = "~_(\d{8})-~";
$end_date_regex = "~-(\d{8})$~";
<?php
foreach ($user->allcaps as $single_capability => $single_capability_value){
$cap_to_process = $single_capability;
//error_log("capability to process: " . print_r($cap_to_process, TRUE), 0);
$end_date = null;
$start_date = null;
$capability = null;
// parse the capability
// get and trim off end date
if ( preg_match($end_date_regex, $cap_to_process, $end_date_string_array) ) {
//error_log("getting end_date: " . $end_date_string_array[0] . " from: " . print_r($cap_to_process, TRUE), 0);
$end_date = grccertify_string_to_date( $end_date_string_array[0] );
//error_log("end_date: " . print_r($end_date, TRUE), 0);
//error_log("trimming end_date from: " . print_r($cap_to_process, TRUE), 0);
$cap_to_process = str_replace ("_" . $end_date_string_array[0], "", $cap_to_process);
//error_log("end_date trimmed and capability string is now: " . print_r($cap_to_process, TRUE), 0);
}
// get and trim off start date
if ( preg_match($start_date_regex, $cap_to_process, $start_date_string_array) ) {
//error_log("getting start_date: " . $start_date_string_array[0] . " from: " . print_r($cap_to_process, TRUE), 0);
$start_date = grccertify_string_to_date( $start_date_string_array[0] );
//error_log("start_date: " . print_r($start_date, TRUE), 0);
//error_log("trimming start_date from: " . print_r($cap_to_process, TRUE), 0);
$cap_to_process = str_replace ("_" . $start_date_string_array[0] , "", $cap_to_process);
//error_log("start_date trimmed and capability string is now: " . print_r($cap_to_process, TRUE), 0);
}
// you are left with just the capability
$capability = $cap_to_process;
<?php
foreach ($user->allcaps as $capability => $v){
// Parse the capability.
if (preg_match('~_(\d{8}?)-(\d{8}?)$~', $capability, $matches)){
$end_date = empty($matches[1]) ? null : strtotime($matches[1]);
$start_date = empty($matches[2]) ? null : strtotime($matches[2]);
$capability = str_replace($matches[0] , '', $capability);
}
smitchell360 wrote:strtotime did not convert 20110101.
<?php echo strtotime('20110101'); ?>
smitchell360 wrote:The main reason that I add the "beg" and "end" is that I want the information stored to be a bit more readable.
The custom capabilities cannot contain "-". They use "_" or alphanumeric.
'~_beg(\d{8})_end(\d{8})$~'
smitchell360 wrote:To make this even more robust, we should probably add functions that return the correct PayPal button string based on:
- custom_capability
- start_date
- end_date
I'll work on that today. Unless you have some time...
$beg_date = strtotime ("now");
$end_date = strtotime ("+1 week");
$beg_date_string = date ( "Ymd", $beg_date );
$end_date_string = date ( "Ymd", $end_date );
$custom_capabilities_string = "take_grc_professional_exam,take_grc_professional_exam_beg" . $beg_date_string . "_end" . $end_date_string;
<input type="hidden" name="item_number" value="1:<?php echo $custom_capabilities_string; ?>" />
<?php
$capability = 'take_grc_professional_exam';
$beg_time = 'now';
$end_time = '+1 week';
$capability = base64_encode($capability . ',' . $capability . '_beg' . date('Ymd', strtotime($beg_time)) . 'end' . date('Ymd', strtotime($end_time)));
echo '<input type="hidden" name="item_number" value="1:' , $capability , '" />';
?>
<?php
function capability_dated($capability, $beg_time, $end_time)
{
echo base64_encode($capability . ',' . $capability . '_beg' . date('Ymd', strtotime($beg_time)) . 'end' . date('Ymd', strtotime($end_time)));
}
?>
<input type="hidden" name="item_number" value="1:<?php capability_dated('take_grc_professional_exam', 'now', '+1 week'); ?>" />
smitchell360 wrote:Indeed. I plan to clean everything up (and will post the plugin) once I get my entire flow working. I have this odd infinite loop after getting a successful payment notification from the PayPal sandbox account.
Users browsing this forum: No registered users and 1 guest