A better way to write this php function

Question

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 has clicked a button that is in another function, current quarter information at time of click is added to the user_meta. Users that have clicked the button will no long have access to the content 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.

I would really like to clean up my code to make it more efficient. Thank you 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);

0
protagonist 1 month 2022-10-29T15:21:31-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse