0

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.

2
  • You've made a block copy error with the last if block: you've got a mix of 3s and 4s.
    – Rup
    Commented Oct 30, 2022 at 17:21
  • @Rup Ah yes I did catch that in the working code but not here. Thanks! Commented Oct 30, 2022 at 17:26

2 Answers 2

1

This answer is completely based on the code and help from -KIKO Software. I did have to add in the global '$current_user' for it to work correctly. Also, per @kikosoftware foresight, I added a means to track by year, just in case a user did not visit the site until the same quarter the following year.

function all_access_levels($current_user)
{
    global $current_user;

    $user_id = $current_user->ID;
    $current_quarter = 'Q' . ceil(date('n') / 3) . '_' . date('Y');
    $past_meta_year = get_user_meta($user_id,'cc_event_date_clicked');
    $get_meta_date = $past_meta_year[0];
    $last_four_of_past_year = substr($get_meta_date,-4);

    // unset meta data for all other quarters
    for ($quarter = 1; $quarter <= 4; $quarter++) {
        if ('Q' . $quarter . '_' . $last_four_of_past_year != $current_quarter) {
            delete_user_meta($user_id, 'cc_event_date_clicked',  'Q' . $quarter . '_' . $last_four_of_past_year);
        }
    }

    // level 3 users can access when they haven't any meta data
    if (empty(get_user_meta($user_id, 'cc_event_date_clicked'))) {
        return ['3'];
    }
    return false;
}


add_filter('cc_all_access_levels', 'all_access_levels', 10, 3);
0

I don't completely follow what you're trying to do, but:

  1. If the user isn't logged in you probably don't want to run the rest of the function. At the moment it looks like you will with $member_id unset.

  2. Your first check is essentially if (empty(get_user_meta($member_id, $curtQuarter, true))) so you might as well just call that rather than loading the four other keys?

  3. To delete the other keys, you could just use a loop. Something like

    $curtQuarterNumber = ceil($month / 3);
    for ( $quarter = 1; $quarter <= 4; $quarter++ ) {
        if ( $quarter === $curtQuarterNumber ) continue;
    
        $quarterKey = 'cc_events_Q' . $quarter;
        if ( ! empty( get_user_meta( $member_id, $quarterKey, true ) ) ) {
            delete_user_meta( $member_id, $quarterKey );
        }
    }
    
1
  • 1
    Thank you for this. I ended up getting an answer that worked out as well. There were a few things I didn't think about like the year etc. But the code you have here did what I originally set out to do. I can't upvote because I need more reputation points but I appreciate your time. Commented Nov 1, 2022 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.