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(remove_query_arg('action'));
             break;
         case 'added':
             check_admin_referer('add-comm');
             $comm = new M_Communication(false);
             if ($comm->add()) {
                 wp_safe_redirect(add_query_arg('msg', 8, 'admin.php?page=' . $page));
             } else {
                 wp_safe_redirect(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(add_query_arg('msg', 1, 'admin.php?page=' . $page));
                 } else {
                     wp_safe_redirect(add_query_arg('msg', 2, 'admin.php?page=' . $page));
                 }
             } else {
                 wp_safe_redirect(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(add_query_arg('msg', 10, wp_get_referer()));
                 } else {
                     wp_safe_redirect(add_query_arg('msg', 11, 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(add_query_arg('msg', 5, wp_get_referer()));
                 } else {
                     wp_safe_redirect(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(add_query_arg('msg', 3, wp_get_referer()));
                 } else {
                     wp_safe_redirect(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(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(add_query_arg('msg', 7, wp_get_referer()));
             break;
     }
 }
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);
    }
}
        function show_communication_edit($comm_id)
        {
            global $page;
            if ($comm_id === false) {
                $addcomm = new M_Communication(0);
                echo "<div class='wrap'>";
                echo "<h2>" . __('Add Message', 'membership') . "</h2>";
                echo '<div id="poststuff" class="metabox-holder">';
                ?>
								<div class="postbox">
										<h3 class="hndle" style='cursor:auto;'><span><?php 
                _e('Add message', 'membership');
                ?>
</span></h3>
										<div class="inside">
												<?php 
                echo '<form method="post" action="?page=' . $page . '">';
                echo '<input type="hidden" name="ID" value="" />';
                echo "<input type='hidden' name='action' value='added' />";
                wp_nonce_field('add-comm');
                $addcomm->addform();
                echo '<p class="submit">';
                echo '<input type="reset" class="button" value="', __('Reset', 'membership'), '">';
                echo '<input class="button-primary alignright" type="submit" name="go" value="' . __('Add message', 'membership') . '" /></p>';
                echo '</form>';
                ?>
										</div>
								</div>
								<?php 
                echo "</div>";
                echo "</div>";
            } else {
                $editcomm = new M_Communication((int) $comm_id);
                echo "<div class='wrap'>";
                echo "<h2>" . __('Edit Message', 'membership') . "</h2>";
                echo '<div id="poststuff" class="metabox-holder">';
                ?>
								<div class="postbox">
										<h3 class="hndle" style='cursor:auto;'><span><?php 
                _e('Edit message', 'membership');
                ?>
</span></h3>
										<div class="inside">
												<?php 
                echo '<form method="post" action="?page=' . $page . '">';
                echo '<input type="hidden" name="ID" value="' . $comm_id . '" />';
                echo "<input type='hidden' name='action' value='updated' />";
                wp_nonce_field('update-comm_' . $comm_id);
                $editcomm->editform();
                echo '<p class="submit">';
                echo '<input type="reset" class="button" value="', __('Reset', 'membership'), '">';
                echo '<input class="button-primary alignright" type="submit" name="go" value="' . __('Update message', 'membership') . '" /></p>';
                echo '</form>';
                ?>
										</div>
								</div>
								<?php 
                echo "</div>";
                echo "</div>";
            }
        }
Exemple #4
0
function affiliate_membership_get_subscription_levels()
{
    static $subscriptions = '';
    if (!$subscriptions) {
        if (class_exists('M_Communication')) {
            $comm = new M_Communication(false);
            $subscriptions = $comm->get_active_subscriptions();
        }
    }
    //echo "subscriptions<pre>"; print_r($subscriptions); echo "</pre>";
    return $subscriptions;
}