I have a function that has a few and/or operators in it. I'm sure there is a better way to write this but can't seem to figure it out. I thought I could use arrays but ran into an issue with having more than one search value I need to check for.
My function: If the user (level 3) has clicked a button, user_meta is updated with what the current quarter is. This user meta is going to be used to track if and in which quarter the button has been clicked for said user:
// When book now button is clicked, update the user meta with current month.
$month = date('n');
$curtQuarter = 'cc_events_Q' . ceil($month / 3);
if (!empty($_REQUEST['add_qrtly_user_meta_cc']) && $level_id == 3) {
update_user_meta($member_id, $curtQuarter, true);
}
Level 3 users that have clicked the button will no long have access to the content for the rest of the quarter unless they pay or until the next quarter. In the function below, if the current quarter and user_meta quarter does not match they are able to access the content. If the current quarter and user_meta quarter does match they are not able to access the content. I then added code to remove old user_meta for quarters that aren't the current one to ensure that the following year I would get the same results:
function all_access_levels($levels)
{
global $current_user;
$member_id = $current_user->ID;
$month = date('n');
$curtQuarter = 'cc_events_Q' . ceil($month / 3);
$key = get_user_meta($member_id, 'cc_events_Q1', true);
$key2 = get_user_meta($member_id, 'cc_events_Q2', true);
$key3 = get_user_meta($member_id, 'cc_events_Q3', true);
$key4 = get_user_meta($member_id, 'cc_events_Q4', true);
if (
$curtQuarter == 'cc_events_Q1' && empty($key) ||
$curtQuarter == 'cc_events_Q2' && empty($key2) ||
$curtQuarter == 'cc_events_Q3' && empty($key3) ||
$curtQuarter == 'cc_events_Q4' && empty($key4)
) { // Change post ID and user ID value. Adjust this accordingly.
$levels = array('3');
return $levels;
} else {
if (
$curtQuarter == 'cc_events_Q1' && !empty($key2) ||
$curtQuarter == 'cc_events_Q1' && !empty($key3) ||
$curtQuarter == 'cc_events_Q1' && !empty($key4)
) {
delete_user_meta($member_id, 'cc_events_Q2');
delete_user_meta($member_id, 'cc_events_Q3');
delete_user_meta($member_id, 'cc_events_Q4');
}
if (
$curtQuarter == 'cc_events_Q2' && !empty($key) ||
$curtQuarter == 'cc_events_Q2' && !empty($key3) ||
$curtQuarter == 'cc_events_Q2' && !empty($key4)
) {
delete_user_meta($member_id, 'cc_events_Q1');
delete_user_meta($member_id, 'cc_events_Q3');
delete_user_meta($member_id, 'cc_events_Q4');
}
if (
$curtQuarter == 'cc_events_Q3' && !empty($key) ||
$curtQuarter == 'cc_events_Q3' && !empty($key2) ||
$curtQuarter == 'cc_events_Q3' && !empty($key4)
) {
delete_user_meta($member_id, 'cc_events_Q1');
delete_user_meta($member_id, 'cc_events_Q2');
delete_user_meta($member_id, 'cc_events_Q4');
}
if (
$curtQuarter == 'cc_events_Q4' && !empty($key) ||
$curtQuarter == 'cc_events_Q4' && !empty($key2) ||
$curtQuarter == 'cc_events_Q4' && !empty($key3)
) {
delete_user_meta($member_id, 'cc_events_Q1');
delete_user_meta($member_id, 'cc_events_Q2');
delete_user_meta($member_id, 'cc_events_Q3');
}
return false;
}
}
add_filter('cc_all_access_levels', 'all_access_levels', 10, 3);
I would really like to clean up my code to make it more efficient. Thank you in advance for your insight.
UPDATE:
The quarter is a calendar quarter e.g. Jan, Feb, Mar = Q1, Apr, May, Jun = Q2 etc. This is for a membership site. I have a member level (3) that will be able to access content for free once a quarter. If they want more access that quarter they will have to pay for additional access until the next quarter.
The code works as is but I know it's sub-par and I'm not sure of the proper way to clean it up.