A better way to write this PHP function

I have a function that has some if and/or operators. I’m sure there’s a better way to write this, but can’t figure it out. I thought I could use arrays, but ran into a problem with more than one search value that I need to check.

My function: If the user has clicked a button in another function, the current quarter information is added to user_meta at the time of the click. Users who click the button will not have access to the content until the next quarter. In the function below, if current quarter and user_meta quarter does not Match the content they are able to access. if current quarter and user_meta quarter does Match They are not able to access the content. Then I added code to delete the old user_meta for quarters that are not current to make sure I get the same results next year.

I really want to clean up my code to make it more efficient. Thanks in advance for your insight.

function all_access_levels($levels, $user_id, $post_id)
{

    global $wpdb, $current_user, $user_ID;

    if (is_user_logged_in()) {
        $member_id = $current_user->ID;

        $query = "SELECT * FROM " . $wpdb->prefix . "cc_memberships_users WHERE membership_id = %d";
        $wpdb->get_row($wpdb->prepare($query, $member_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_Q3' && !empty($key2) ||
            $curtQuarter == 'cc_events_Q3' && !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);

Leave a Comment