コード例 #1
0
 function handle_communication_updates()
 {
     global $action, $page;
     wp_reset_vars(array('action', 'page'));
     if (isset($_GET['doaction']) || isset($_GET['doaction2'])) {
         if (addslashes($_GET['action']) == 'delete' || addslashes($_GET['action2']) == 'delete') {
             $action = 'bulk-delete';
         }
         if (addslashes($_GET['action']) == 'toggle' || addslashes($_GET['action2']) == 'toggle') {
             $action = 'bulk-toggle';
         }
     }
     switch (addslashes($action)) {
         case 'removeheader':
             $this->dismiss_user_help($page);
             wp_safe_redirect(esc_url_raw(remove_query_arg('action')));
             break;
         case 'added':
             check_admin_referer('add-comm');
             $comm = new M_Communication(false);
             if ($comm->add()) {
                 wp_safe_redirect(esc_url_raw(add_query_arg('msg', 8, 'admin.php?page=' . $page)));
             } else {
                 wp_safe_redirect(esc_url_raw(add_query_arg('msg', 9, 'admin.php?page=' . $page)));
             }
             break;
         case 'updated':
             $id = (int) $_POST['ID'];
             check_admin_referer('update-comm_' . $id);
             if ($id) {
                 $comm = new M_Communication($id);
                 if ($comm->update()) {
                     wp_safe_redirect(esc_url_raw(add_query_arg('msg', 1, 'admin.php?page=' . $page)));
                 } else {
                     wp_safe_redirect(esc_url_raw(add_query_arg('msg', 2, 'admin.php?page=' . $page)));
                 }
             } else {
                 wp_safe_redirect(esc_url_raw(add_query_arg('msg', 2, 'admin.php?page=' . $page)));
             }
             break;
         case 'delete':
             if (isset($_GET['comm'])) {
                 $id = (int) $_GET['comm'];
                 check_admin_referer('delete-comm_' . $id);
                 $comm = new M_Communication($id);
                 if ($comm->delete()) {
                     wp_safe_redirect(esc_url_raw(add_query_arg('msg', 10, wp_get_referer())));
                 } else {
                     wp_safe_redirect(esc_url_raw(add_query_arg('msg', 11, wp_get_referer())));
                 }
             }
             break;
         case 'sendme':
             $id = filter_input(INPUT_GET, 'comm', FILTER_VALIDATE_INT, array('options' => array('min_range' => 1)));
             if ($id) {
                 check_admin_referer('sendme-' . $id);
                 $comm = new M_Communication($id);
                 $comm->send_message(get_current_user_id());
                 wp_safe_redirect(esc_url_raw(add_query_arg('msg', 12, wp_get_referer())));
             }
             break;
         case 'deactivate':
             if (isset($_GET['comm'])) {
                 $id = (int) $_GET['comm'];
                 check_admin_referer('toggle-comm_' . $id);
                 $comm = new M_Communication($id);
                 if ($comm->toggle()) {
                     wp_safe_redirect(esc_url_raw(add_query_arg('msg', 5, wp_get_referer())));
                 } else {
                     wp_safe_redirect(esc_url_raw(add_query_arg('msg', 6, wp_get_referer())));
                 }
             }
             break;
         case 'activate':
             if (isset($_GET['comm'])) {
                 $id = (int) $_GET['comm'];
                 check_admin_referer('toggle-comm_' . $id);
                 $comm = new M_Communication($id);
                 if ($comm->toggle()) {
                     wp_safe_redirect(esc_url_raw(add_query_arg('msg', 3, wp_get_referer())));
                 } else {
                     wp_safe_redirect(esc_url_raw(add_query_arg('msg', 4, wp_get_referer())));
                 }
             }
             break;
         case 'bulk-delete':
             check_admin_referer('bulk-comms');
             foreach ($_GET['commcheck'] as $value) {
                 if (is_numeric($value)) {
                     $id = (int) $value;
                     $comm = new M_Communication($id);
                     $comm->delete();
                 }
             }
             wp_safe_redirect(esc_url_raw(add_query_arg('msg', 10, wp_get_referer())));
             break;
         case 'bulk-toggle':
             check_admin_referer('bulk-comms');
             foreach ($_GET['commcheck'] as $value) {
                 if (is_numeric($value)) {
                     $id = (int) $value;
                     $comm = new M_Communication($id);
                     $comm->toggle();
                 }
             }
             wp_safe_redirect(esc_url_raw(add_query_arg('msg', 7, wp_get_referer())));
             break;
     }
 }
コード例 #2
0
function M_Communication_process()
{
    // This function checks for any communication messages that need to be sent for this user and sends them
    $lastatid = M_get_option('membership_communication_last_user_processed', 0);
    if (empty($lastatid)) {
        $lastatid = 0;
    }
    $members = M_Communication_get_members($lastatid);
    if (empty($members)) {
        // do nothing
        if ($lastatid != 0) {
            M_update_option('membership_communication_last_user_processed', 0);
        }
    } else {
        // Our starting time
        $timestart = current_time('timestamp');
        //Or processing limit
        $timelimit = 3;
        // max seconds for processing
        foreach ((array) $members as $user_id) {
            // Makes sure that messages only takes 3 seconds of processing power. Prevents timeouts.
            if (current_time('timestamp') > $timestart + $timelimit) {
                M_update_option('membership_communication_last_user_processed', $user_id);
                break;
            }
            if (apply_filters('membership_prevent_communication', get_user_meta($user_id, 'membership_signup_gateway_can_communicate', true)) != 'yes') {
                $starts = M_Communication_get_startstamps($user_id);
                $comms = M_Communication_get_post_messages();
                if (!empty($starts) && !empty($comms)) {
                    foreach ($starts as $start) {
                        $starttime = $start->meta_value;
                        $now = current_time('timestamp');
                        $sub_id = str_replace('start_current_', '', $start->meta_key);
                        $sentalready = get_user_meta($user_id, 'sent_msgs_' . $sub_id, true);
                        if (empty($sentalready) || !is_array($sentalready)) {
                            $sentalready = array();
                        }
                        foreach ((array) $comms as $comm) {
                            if (in_array($comm->id, $sentalready) || $comm->sub_id != $sub_id && $comm->sub_id > 0) {
                                continue;
                            }
                            $withperiod = $starttime + $comm->periodstamp;
                            // Get 24 hour previous and after so we have a range in which to fit a communication
                            $onedaybefore = strtotime('-6 hours', $withperiod);
                            $onedayafter = strtotime('+6 hours', $withperiod);
                            if ($now > $onedaybefore && $now < $onedayafter) {
                                $message = new M_Communication($comm->id);
                                $sentalready[$comm->id] = $comm->id;
                                $message->send_message($user_id, $sub_id);
                                break;
                            }
                        }
                        update_user_meta($user_id, 'sent_msgs_' . $sub_id, $sentalready);
                    }
                }
                $ends = M_Communication_get_endstamps($user_id);
                $comms = M_Communication_get_pre_messages();
                if (!empty($ends) && !empty($comms)) {
                    foreach ($ends as $end) {
                        $endtime = $end->meta_value;
                        $now = current_time('timestamp');
                        $sub_id = str_replace('expire_current_', '', $end->meta_key);
                        $sentalready = get_user_meta($user_id, 'sent_msgs_' . $sub_id, true);
                        if (empty($sentalready) || !is_array($sentalready)) {
                            $sentalready = array();
                        }
                        foreach ((array) $comms as $comm) {
                            if (in_array($comm->id, $sentalready) || $comm->sub_id != $sub_id && $comm->sub_id > 0) {
                                continue;
                            }
                            $withperiod = $endtime + $comm->periodstamp;
                            // Get 24 hour previous and after so we have a range in which to fit a communication
                            $onedaybefore = strtotime('-6 hours', $withperiod);
                            $onedayafter = strtotime('+6 hours', $withperiod);
                            if ($now > $onedaybefore && $now < $onedayafter) {
                                $message = new M_Communication($comm->id);
                                $sentalready[$comm->id] = $comm->id;
                                $message->send_message($user_id, $sub_id);
                                break;
                            }
                        }
                        update_user_meta($user_id, 'sent_msgs_' . $sub_id, $sentalready);
                    }
                }
            }
        }
        M_update_option('membership_communication_last_user_processed', $user_id);
    }
}