static function send_single_from_queue()
 {
     global $wpdb;
     //$emails = $this->wpdbQuery("SELECT * FROM ".$this->queue_table()." WHERE success = 0 AND max_attempts != attempts LIMIT ".$limit,"get_results");
     $count = 0;
     $attempts = 0;
     $queue_table = SendPress_Data::queue_table();
     if (SendPress_Manager::limit_reached()) {
         return array('attempted' => $attempts, 'sent' => $count);
     }
     SendPress_Email_Cache::build_cache();
     $email = SendPress_Data::get_single_email_from_queue(true);
     if (is_object($email)) {
         //$email = $email[0];
         if (SendPress_Manager::limit_reached()) {
             return array('attempted' => $attempts, 'sent' => $count);
         }
         $attempts++;
         SendPress_Data::queue_email_process($email->id);
         $result = SendPress_Manager::send_email_from_queue($email);
         if ($result) {
             if ($result === true) {
                 $wpdb->update(SendPress_Data::queue_table(), array('success' => 1, 'inprocess' => 3), array('id' => $email->id));
                 //( $sid, $rid, $lid=null, $uid=null, $ip=null, $device_type=null, $device=null, $type='confirm' )
                 $wpdb->insert(SendPress_Data::subscriber_tracker_table(), array('subscriberID' => $email->subscriberID, 'emailID' => $email->emailID, 'sent_at' => get_gmt_from_date(date('Y-m-d H:i:s'))));
                 SendPress_Data::add_subscriber_event($email->subscriberID, $email->emailID, $email->listID, null, null, null, null, 'send');
             } else {
                 $wpdb->update(SendPress_Data::queue_table(), array('success' => 2, 'inprocess' => 3), array('id' => $email->id));
                 SendPress_Data::add_subscriber_event($email->subscriberID, $email->emailID, $email->listID, null, null, null, null, 'bounce');
                 SendPress_Data::bounce_subscriber_by_id($email->subscriberID);
             }
             $count++;
         } else {
             $wpdb->update($queue_table, array('attempts' => $email->attempts + 1, 'inprocess' => 0, 'last_attempt' => date('Y-m-d H:i:s')), array('id' => $email->id));
         }
     } else {
         //We ran out of emails to process.
         return array('attempted' => $attempts, 'sent' => $count);
     }
     //SendPress_Manager::increase_email_count( $attempts );
     return array('attempted' => $attempts, 'sent' => $count);
 }
 /** ************************************************************************
  * REQUIRED! This is where you prepare your data for display. This method will
  * usually be used to query the database, sort and filter the data, and generally
  * get it ready to be displayed. At a minimum, we should set $this->items and
  * $this->set_pagination_args(), although the following properties and methods
  * are frequently interacted with here...
  * 
  * @uses $this->_column_headers
  * @uses $this->items
  * @uses $this->get_columns()
  * @uses $this->get_sortable_columns()
  * @uses $this->get_pagenum()
  * @uses $this->set_pagination_args()
  **************************************************************************/
 function prepare_items()
 {
     global $wpdb, $_wp_column_headers;
     $screen = get_current_screen();
     /*      
             select t1.* from `sp_sendpress_list_subscribers` as t1 , `sp_sendpress_subscribers` as t2
             where t1.subscriberID = t2.subscriberID and t1.listID = 2*/
     $query = "SELECT * FROM " . SendPress_Data::queue_table();
     /* -- Pagination parameters -- */
     //Number of elements in your table?
     $totalitems = SendPress_Data::emails_sent_in_queue('All');
     //$wpdb->query($query); //return the total number of affected rows
     //How many to display per page?
     // get the current user ID
     $user = get_current_user_id();
     // get the current admin screen
     $screen = get_current_screen();
     // retrieve the "per_page" option
     $per_page = 10;
     $screen_option = $screen->get_option('per_page', 'option');
     if (!empty($screen_option)) {
         // retrieve the value of the option stored for the current user
         $per_page = get_user_meta($user, $screen_option, true);
         if (empty($per_page) || $per_page < 1) {
             // get the default value if none is set
             $per_page = $screen->get_option('per_page', 'default');
         }
     }
     //Which page is this?
     $paged = !empty($_GET["paged"]) ? esc_sql($_GET["paged"]) : '';
     //Page Number
     if (empty($paged) || !is_numeric($paged) || $paged <= 0) {
         $paged = 1;
     }
     //How many pages do we have in total?
     $totalpages = ceil($totalitems / $per_page);
     $query .= ' WHERE success = 1 ';
     //$query.="AND ( date_sent = '0000-00-00 00:00:00' or date_sent < '".date_i18n('Y-m-d H:i:s')."') ";
     if (isset($_GET["listid"]) && $_GET["listid"] > 0) {
         $query .= ' AND listID = ' . $_GET["listid"];
     }
     if (isset($_GET["qs"])) {
         $query .= ' AND to_email LIKE "%' . $_GET["qs"] . '%"';
     }
     /* -- Ordering parameters -- */
     //Parameters that are going to be used to order the result
     $orderby = !empty($_GET["orderby"]) ? esc_sql($_GET["orderby"]) : '';
     $order = !empty($_GET["order"]) ? esc_sql($_GET["order"]) : 'ASC';
     if (!empty($orderby) & !empty($order)) {
         $query .= ' ORDER BY ' . $orderby . ' ' . $order;
     }
     if (empty($orderby)) {
         $query .= ' ORDER BY id DESC ';
     }
     //adjust the query to take pagination into account
     if (!empty($paged) && !empty($per_page)) {
         $offset = ($paged - 1) * $per_page;
         $query .= ' LIMIT ' . (int) $offset . ',' . (int) $per_page;
     }
     /* -- Register the pagination -- */
     $this->set_pagination_args(array("total_items" => $totalitems, "total_pages" => $totalpages, "per_page" => $per_page));
     //The pagination links are automatically built according to those parameters
     /* -- Register the Columns -- */
     $columns = $this->get_columns();
     $hidden = array();
     $sortable = $this->get_sortable_columns();
     $this->_column_headers = array($columns, $hidden, $sortable);
     /* -- Fetch the items -- */
     $this->items = $wpdb->get_results($query);
 }
 static function add_email_to_queue($values)
 {
     global $wpdb;
     $table = SendPress_Data::queue_table();
     $messageid = SendPress_Data::unique_message_id();
     $values["messageID"] = $messageid;
     $values["max_attempts"] = 1;
     $values["date_published"] = date('Y-m-d H:i:s');
     $wpdb->insert($table, $values);
     return $wpdb->insert_id;
 }
    function html()
    {
        SendPress_Tracking::event('Queue Tab');
        if (SPNL()->validate->_isset('cron')) {
            SPNL()->fetch_mail_from_queue();
        }
        //Create an instance of our package class...
        $testListTable = new SendPress_Queue_Table();
        //Fetch, prepare, sort, and filter our data...
        $testListTable->prepare_items();
        SendPress_Option::set('no_cron_send', 'false');
        SPNL()->cron_start();
        $open_info = array("id" => 13, "report" => 10, "view" => "open");
        $autocron = SendPress_Option::get('autocron', 'no');
        ?>

<br>
	<div id="taskbar" class="lists-dashboard rounded group"> 

	<div id="button-area">  
	<?php 
        $pause_sending = SendPress_Option::get('pause-sending', 'no');
        $txt = __('Pause Sending', 'sendpress');
        //Stop Sending for now
        if ($pause_sending == 'yes') {
            $txt = __('Resume Sending', 'sendpress');
        }
        ?>
	<div class="btn-group">

       <?php 
        if (SendPress_Option::get('emails-credits') && SendPress_Option::get('sendmethod') === 'Jaiminho_Sender_NetWork' || SendPress_Option::get('sendmethod') != 'Jaiminho_Sender_NetWork') {
            SendPress_Option::set('pause-sending', 'no');
            ?>
          <a class="btn btn-large btn-default " href="<?php 
            echo SendPress_Admin::link('Queue');
            ?>
&action=pause-queue" ><i class="icon-repeat icon-white "></i> <?php 
            echo $txt;
            ?>
</a>
          <a id="send-now" class="btn btn-primary btn-large " data-toggle="modal" href="#sendpress-sending"   ><i class="icon-white icon-refresh"></i> <?php 
            _e('Send Emails Now', 'sendpress');
            ?>
</a>
	</div>
	</div>
	<?php 
        } else {
            SendPress_Option::set('pause-sending', 'yes');
        }
        $emails_per_day = SendPress_Option::get('emails-per-day');
        if ($emails_per_day == 0) {
            $emails_per_day = __('Unlimited', 'sendpress');
        }
        $emails_per_hour = SendPress_Option::get('emails-per-hour');
        $hourly_emails = SendPress_Data::emails_sent_in_queue("hour");
        $emails_so_far = SendPress_Data::emails_sent_in_queue("day");
        $credits = SendPress_Option::get('emails-credits');
        //print_r(SendPress_Data::emails_stuck_in_queue());
        global $wpdb;
        $table = SendPress_Data::queue_table();
        $date = getdate();
        // Maurilio TODO: fazer com os créditos sejam contados a partir da 00:00:00 do primeiro dia do mês atual
        $hour_ago = strtotime('-' . $date["mday"] . ' day');
        $time = date('Y-m-d H:i:s', $hour_ago);
        $query = $wpdb->prepare("SELECT COUNT(*) FROM {$table} where last_attempt > %s and success = %d", $time, 1);
        $credits_so_far = $wpdb->get_var($query);
        $result_credits = $credits - $credits_so_far;
        if ($credits <= 0) {
            echo "<p class='alert alert-danger' style='width:70%;'>" . __("Vixe! Você não tem créditos. Para enviar emails em sua fila ou enviar novos emails, você precisa obter mais créditos.", "jaiminho") . "</p>";
        } else {
            ?>
        <h2><?php 
            echo $credits ? __('Você tem', 'jaiminho') : "";
            ?>
        <strong><?php 
            echo $result_credits ? $result_credits : "";
            ?>
</strong> <?php 
            echo $credits ? __('créditos', 'jaiminho') : "";
            ?>
.
        </h2>
        <?php 
        }
        ?>
       
      <h2><strong><?php 
        echo $emails_so_far;
        ?>
</strong> <?php 
        _e('of a possible', 'sendpress');
        ?>
 <strong><?php 
        echo $emails_per_day;
        ?>
</strong> <?php 
        _e('emails sent in the last 24 hours', 'sendpress');
        ?>
.</h2>
      <h2><strong><?php 
        echo $hourly_emails;
        ?>
</strong> <?php 
        _e('of a possible', 'sendpress');
        ?>
 <strong><?php 
        echo $emails_per_hour;
        ?>
</strong> <?php 
        _e('emails sent in the last hour', 'sendpress');
        ?>
.</h2>
      <?php 
        if (is_multisite() && is_super_admin() || !is_multisite()) {
            ?>
      <small><?php 
            _e('You can adjust these settings here', 'sendpress');
            ?>
: <a href="<?php 
            echo SendPress_Admin::link('Settings_Account');
            ?>
"><?php 
            _e('Settings', 'sendpress');
            ?>
 > <?php 
            _e('Sending Account', 'sendpress');
            ?>
</a>.</small>
                 <?php 
        }
        ?>
 		<?php 
        if ($autocron == 'no') {
            $offset = get_option('gmt_offset') * 60 * 60;
            // Time offset in seconds
            $local_timestamp = wp_next_scheduled('sendpress_cron_action') + $offset;
            ?>
<br><small><?php 
            _e('The cron will run again around', 'sendpress');
            ?>
: <?php 
            echo date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $local_timestamp);
            ?>
</small>
<?php 
        }
        ?>
 		<br><br>
		</div>
	<!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions -->
	<form id="email-filter" action="<?php 
        echo SendPress_Admin::link('Queue');
        ?>
" method="get">
		<!-- For plugins, we also need to ensure that the form posts back to our current page -->
	     <input type="hidden" name="page" value="<?php 
        echo SPNL()->validate->page();
        ?>
" /> 
	    <!-- Now we can render the completed list table -->
	    <?php 
        $testListTable->display();
        ?>
	    <?php 
        wp_nonce_field($this->_nonce_value);
        ?>
	</form>
	<br>
	<!--
		<a class="btn btn-large btn-success " href="<?php 
        echo SendPress_Admin::link('Queue');
        ?>
&action=reset-queue" ><i class="icon-repeat icon-white "></i> <?php 
        _e('Re-queue All Emails', 'sendpress');
        ?>
</a><br><br>
	-->
	<form  method='get'>
		<input type='hidden' value="<?php 
        echo SPNL()->validate->page();
        ?>
" name="page" />
		
		<input type='hidden' value="empty-queue" name="action" />
		<a class="btn btn-large  btn-danger" data-toggle="modal" href="#sendpress-empty-queue" ><i class="icon-warning-sign "></i> <?php 
        _e('Delete All Emails in the Queue', 'sendpress');
        ?>
</a>
		<?php 
        wp_nonce_field($this->_nonce_value);
        ?>
	</form>
<div class="modal fade" id="sendpress-empty-queue" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
  	<div class="modal-content">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal">×</button>
		<h3><?php 
        _e('Really? Delete All Emails in the Queue.', 'sendpress');
        ?>
</h3>
	</div>
	<div class="modal-body">
		<p><?php 
        _e('This will remove all emails from the queue without attempting to send them', 'sendpress');
        ?>
.</p>
	</div>
	<div class="modal-footer">
	<a href="#" class="btn btn-primary" data-dismiss="modal"><?php 
        _e('No! I was Joking', 'sendpress');
        ?>
</a><a href="<?php 
        echo SendPress_Admin::link('Queue');
        ?>
&action=empty-queue" id="confirm-delete" class="btn btn-danger" ><?php 
        _e('Yes! Delete All Emails', 'sendpress');
        ?>
</a>
	</div>
</div></div>
</div>

<div class="modal fade" id="sendpress-sending" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
	<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">×</button>
    <h3><?php 
        _e('Sending Emails', 'sendpress');
        ?>
</h3>
  </div>
  <div class="modal-body">
    <div id="sendbar" class="progress progress-striped
     active">
  <div id="sendbar-inner" class="progress-bar"
       style="width: 40%;"></div>
</div>
	<span id="queue-sent">-</span> <?php 
        _e('of', 'sendpress');
        ?>
 <span id="queue-total">-</span> <?php 
        _e('emails left to send', 'sendpress');
        ?>
.<br>
	<br>
	<?php 
        _e('You are also limited to', 'sendpress');
        ?>
 <?php 
        echo $emails_per_hour;
        ?>
 <?php 
        _e('emails per hour', 'sendpress');
        ?>
.<br>
	<?php 
        _e('To change these settings go to', 'sendpress');
        ?>
 <a href="<?php 
        echo SendPress_Admin::link('Settings_Account');
        ?>
"> <?php 
        _e('Settings', 'sendpress');
        ?>
 > <?php 
        _e('Sending Account', 'sendpress');
        ?>
</a>.
  </div>
  <div class="modal-footer">
   <?php 
        _e('If you close this window sending will stop. ', 'sendpress');
        ?>
<a href="#" class="btn btn-primary" data-dismiss="modal"><?php 
        _e('Close', 'sendpress');
        ?>
</a>
  </div>
</div>
</div></div>
<?php 
    }
 static function send_mail_cron()
 {
     //@ini_set('max_execution_time',0);
     global $wpdb;
     $count = SendPress_Option::get('emails-per-hour');
     $count = SendPress_Option::get('wpcron-per-call', 25);
     $email_count = 0;
     $attempts = 0;
     if (SendPress_Manager::limit_reached($count)) {
         return;
     }
     SendPress_Email_Cache::build_cache();
     for ($i = 0; $i < $count; $i++) {
         $email = SendPress_Data::get_single_email_from_queue();
         if ($email != null) {
             $attempts++;
             SendPress_Data::queue_email_process($email->id);
             $result = SendPress_Manager::send_email_from_queue($email);
             $email_count++;
             if ($result) {
                 if ($result === true) {
                     $wpdb->update(SendPress_Data::queue_table(), array('success' => 1, 'inprocess' => 3), array('id' => $email->id));
                     //( $sid, $rid, $lid=null, $uid=null, $ip=null, $device_type=null, $device=null, $type='confirm' )
                     //$wpdb->update( SendPress_Data::queue_table() , array('success'=>1,'inprocess'=>3 ) , array('id'=> $email->id ));
                     //$wpdb->insert(SendPress_Data::subscriber_tracker_table() , array('subscriberID'=>$email->subscriberID,'emailID'=>$email->emailID,'sent_at' => get_gmt_from_date( date('Y-m-d H:i:s') )) );
                     SPNL()->db("Subscribers_Tracker")->add(array('subscriber_id' => intval($email->subscriberID), 'email_id' => intval($email->emailID)));
                     SendPress_Data::add_subscriber_event($email->subscriberID, $email->emailID, $email->listID, null, null, null, null, 'send');
                 } else {
                     $wpdb->update(SendPress_Data::queue_table(), array('success' => 2, 'inprocess' => 3), array('id' => $email->id));
                     SendPress_Data::add_subscriber_event($email->subscriberID, $email->emailID, $email->listID, null, null, null, null, 'bounce');
                     SendPress_Data::bounce_subscriber_by_id($email->subscriberID);
                 }
                 //$wpdb->insert( $this->subscriber_open_table(),  $senddata);
                 $count++;
                 //SendPress_Data::update_report_sent_count( $email->emailID );
             } else {
                 $wpdb->update(SendPress_Data::queue_table(), array('attempts' => $email->attempts + 1, 'inprocess' => 0, 'last_attempt' => date('Y-m-d H:i:s')), array('id' => $email->id));
             }
         } else {
             //We ran out of emails to process.
             break;
         }
         set_time_limit(30);
     }
     return;
 }
예제 #6
0
 function queue_table()
 {
     _deprecated_function(__FUNCTION__, '0.8.9', 'SendPress_Data::queue_table()');
     return SendPress_Data::queue_table();
 }