Sunday, October 30, 2022
HomeWordPress DevelopmentA greater option to write this php operate

A greater option to write this php operate


I’ve a operate that has a couple of and/or operators in it. I am positive there’s a higher option to write this however can not seem to determine it out. I believed I might use arrays however bumped into a problem with having a couple of search worth I have to verify for.

My operate: If the consumer has clicked a button that’s in one other operate, present quarter data at time of click on is added to the user_meta. Customers which have clicked the button will no lengthy have entry to the content material till the subsequent quarter. Within the operate beneath, if the present quarter and user_meta quarter doesn’t match they’re able to entry the content material. If the present quarter and user_meta quarter does match they aren’t capable of entry the content material.
I then added code to take away previous user_meta for quarters that are not the present one to make sure that the next yr I’d get the identical outcomes.

I would like to wash up my code to make it extra environment friendly. Thanks prematurely in your perception.

operate all_access_levels($ranges, $user_id, $post_id)
{

    international $wpdb, $current_user, $user_ID;

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

        $question = "SELECT * FROM " . $wpdb->prefix . "cc_memberships_users WHERE membership_id = %d";
        $wpdb->get_row($wpdb->put together($question, $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 submit ID and consumer ID worth. Alter this accordingly.

        $ranges = array('3');
        return $ranges;
    } 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);

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments