[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-admin/ -> wp_cron.php (source)

   1  <?php
   2  class MP_AdminPage extends MP_adminpage_list_
   3  {
   4      const screen         = MailPress_page_wp_cron;
   5      const capability    = 'MailPress_manage_wp_cron';
   6      const help_url        = 'http://blog.mailpress.org/tutorials/add-ons/wp_cron/';
   7      const file            = __FILE__;
   8  
   9      const add_form_id     = 'add';
  10      const list_id         = 'the-list';
  11      const tr_prefix_id     = 'wpcron';
  12  
  13  ////  Redirect  ////
  14  
  15  	public static function redirect() 
  16      {
  17          if     ( !empty($_REQUEST['action'])  && ($_REQUEST['action']  != -1))    $action = $_REQUEST['action'];
  18          elseif ( !empty($_REQUEST['action2']) && ($_REQUEST['action2'] != -1) )    $action = $_REQUEST['action2'];
  19          if (!isset($action)) return;
  20  
  21          $url_parms = self::get_url_parms(array('paged', 'id', 'sig', 'next_run'));
  22          $checked    = (isset($_GET['checked'])) ? $_GET['checked'] : array();
  23  
  24          $count    = str_replace('bulk-', '', $action);
  25          $count     .= 'd';
  26          $$count    = 0;
  27  
  28          switch($action) 
  29          {
  30              case 'bulk-delete' :
  31                  $crons = _get_cron_array();
  32                  foreach($checked as $id ) 
  33                  {
  34                      $x = explode('::', $id);
  35                      if( isset( $crons[$x[2]][$x[0]][$x[1]] ) )
  36                      {
  37                          wp_unschedule_event($x[2], $x[0], $crons[$x[2]][$x[0]][$x[1]]['args']);
  38                          $$count++;
  39                      }
  40                  }
  41  
  42                  $url_parms['message'] = ($$count <= 1) ? 3 : 4;
  43                  if ($$count) $url_parms[$count] = $$count;
  44                  self::mp_redirect( self::url(MailPress_wp_cron, $url_parms) );
  45              break;
  46  
  47              case 'add':
  48                  $_POST['args'] = json_decode(stripslashes($_POST['args']), true);
  49                  if( !is_array($_POST['args']) ) $_POST['args'] = array();
  50  
  51                  $_POST['next_run'] = strtotime($_POST['next_run']);
  52                  if( $_POST['next_run'] === false || $_POST['next_run'] == -1 ) $_POST['next_run'] = time();
  53  
  54                  if( $_POST['schedule'] == '_oneoff' )
  55                      $e = wp_schedule_single_event($_POST['next_run'], $_POST['name'], $_POST['args']) === NULL;
  56                  else
  57                      $e = wp_schedule_event( $_POST['next_run'], $_POST['schedule'], $_POST['name'], $_POST['args']) === NULL;
  58  
  59                  $url_parms['message'] = ( $e ) ? 1 : 91;
  60                  self::mp_redirect( self::url(MailPress_wp_cron, $url_parms) );
  61              break;
  62              case 'edited':
  63                  unset($_GET['action']);
  64  
  65                  if (!isset($_POST['cancel'])) 
  66                  {
  67                      $crons = _get_cron_array();
  68                      $x = explode('::', $_POST['id']);
  69                      if( isset( $crons[$x[2]][$x[0]][$x[1]] ) ) 
  70                          wp_unschedule_event($x[2], $x[0], $crons[$x[2]][$x[0]][$x[1]]['args']);
  71  
  72                      $_POST['args'] = json_decode(stripslashes($_POST['args']), true);
  73                      if( !is_array($_POST['args']) ) $_POST['args'] = array();
  74  
  75                      $_POST['next_run'] = strtotime($_POST['next_run']);
  76                      if( $_POST['next_run'] === false || $_POST['next_run'] == -1 ) $_POST['next_run'] = time();
  77  
  78                      if( $_POST['schedule'] == '_oneoff' )
  79                          $e = wp_schedule_single_event($_POST['next_run'], $_POST['name'], $_POST['args']) === NULL;
  80                      else
  81                          $e = wp_schedule_event( $_POST['next_run'], $_POST['schedule'], $_POST['name'], $_POST['args']) === NULL;
  82  
  83                      $url_parms['message'] = ( $e ) ? 2 : 92 ;
  84                  }
  85                  unset($url_parms['id'], $url_parms['sig'], $url_parms['next_run']);
  86                  self::mp_redirect( self::url(MailPress_wp_cron, $url_parms) );
  87              break;
  88              case 'do_now':
  89                  unset($_GET['action']);
  90                  $e = false;
  91  
  92                  $crons = _get_cron_array();
  93                  foreach( $crons as $time => $cron ) 
  94                  {
  95                      if( isset( $cron[$_GET['id']][$_GET['sig']] ) ) 
  96                      {
  97                          wp_schedule_single_event(time()-1, $_GET['id'], $cron[$_GET['id']][$_GET['sig']]['args']);
  98                          spawn_cron();
  99                          $e = true;
 100                          break;
 101                      }
 102                  }
 103                  $url_parms['message'] = ( $e ) ? 5 : 95 ;
 104                  unset($url_parms['id'], $url_parms['sig'], $url_parms['next_run']);
 105                  self::mp_redirect( self::url(MailPress_wp_cron, $url_parms) );
 106              break;
 107              case 'delete':
 108                  $crons = _get_cron_array();
 109                  if( isset( $crons[$url_parms['next_run']][$url_parms['id']][$url_parms['sig']] ) ) 
 110                      wp_unschedule_event($url_parms['next_run'], $url_parms['id'], $crons[$url_parms['next_run']][$url_parms['id']][$url_parms['sig']]['args']);
 111  
 112                  unset($url_parms['id'], $url_parms['sig'], $url_parms['next_run']);
 113  
 114                  $url_parms['message'] = 3;
 115                  self::mp_redirect( self::url(MailPress_wp_cron, $url_parms) );
 116              break;
 117          }
 118      }
 119  
 120  ////  Styles  ////
 121  
 122  	public static function print_styles($styles = array()) 
 123      {
 124          wp_register_style ( self::screen,     '/' . MP_PATH . 'mp-admin/css/wp_cron.css' );
 125          $styles[] = self::screen;
 126  
 127          parent::print_styles($styles);
 128      }
 129  
 130  //// Scripts ////
 131  
 132  	public static function print_scripts($scripts = array()) 
 133      {
 134          $scripts = apply_filters('MailPress_autorefresh_js', $scripts);
 135          parent::print_scripts($scripts);
 136      }
 137  
 138  ////  Columns  ////
 139  
 140  	public static function get_columns() 
 141      {
 142          $columns = array(    'cb'         => "<input type='checkbox' />", 
 143                      'name'    => __('Hook name', MP_TXTDOM),
 144                      'next'    => __('Next&#160;run',  MP_TXTDOM),
 145                      'rec'        => __('Recurrence',MP_TXTDOM),
 146                      'args'    => __('Arguments', MP_TXTDOM),
 147          );
 148          return $columns;
 149      }
 150  
 151  ////  List  ////
 152  
 153  	public static function get_list($args)
 154      {
 155          extract($args);
 156  
 157          $wp_crons = array();
 158  
 159          $crons = _get_cron_array();
 160          if (!$crons) $crons = array();
 161  
 162          foreach($crons as $time => $cron)
 163          {
 164              foreach($cron as $hook => $dings)
 165              {
 166                  foreach($dings as $sig => $data)
 167                  {
 168                      $wp_crons[] = array(    'hook' => $hook,
 169                                                      'time' => $time,
 170                                                      'sig'  => $sig,
 171                                                      'data' => $data
 172                      );
 173                  }
 174              }
 175          }
 176  
 177          $total = count($wp_crons);
 178          $rows  = array_slice ($wp_crons, $start, $_per_page);
 179  
 180          return array($rows, $total);
 181      }
 182  
 183  ////  Row  ////
 184  
 185  	public static function get_row($wp_cron, $url_parms)
 186      {
 187          static $row_class = '';
 188  // url's
 189          $args = array();
 190  
 191          $args['id'] = $wp_cron['hook'];
 192          $args['sig'] = $wp_cron['sig'];
 193          $args['next_run'] = $wp_cron['time'];
 194  
 195          $id = $args['id'] . '::' . $args['sig'] . '::' . $args['next_run'];
 196  
 197          $args['action'] = 'delete';
 198          $delete_url = esc_url(self::url( MailPress_wp_cron, array_merge($args, $url_parms), 'delete-cron_' . $args['id'] . '_' . $args['sig'] . '_' . $args['next_run']));
 199          $args['action'] = 'do_now';
 200          $do_now_url = esc_url(self::url( MailPress_wp_cron, array_merge($args, $url_parms)));
 201          $args['action'] = 'edit';
 202          $edit_url = esc_url(self::url( MailPress_wp_cron, array_merge($args, $url_parms)));
 203  
 204  // actions
 205          $actions = array();
 206  
 207          $actions['edit']   = "<a href='$edit_url'>" . __('Edit') . "</a>";
 208          $actions['do_now'] = "<a href='$do_now_url'>" . __('Do now', MP_TXTDOM) . "</a>";
 209          $actions['delete'] = "<a href='$delete_url'>" . __('Delete') . "</a>";
 210  
 211          $row_class = (" class='alternate'" == $row_class) ? '' : " class='alternate'";
 212          $out  = '';
 213          $out .= "<tr id='wp_cron::$id' $row_class>";
 214  
 215          $columns = self::get_columns();
 216          $hidden  = self::get_hidden_columns();
 217  
 218          foreach ( $columns as $column_name => $column_display_name ) 
 219          {
 220              $class = "class='$column_name column-$column_name'";
 221  
 222              $style = '';
 223              if ( in_array($column_name, $hidden) )     $style .= 'display:none;';
 224              $style = ' style="' . $style . '"';
 225  
 226              $attributes = "$class$style";
 227  
 228              $out .= "<td $attributes>";
 229  
 230              switch ($column_name) 
 231              {
 232                  case 'cb':
 233                      $out .= "<input type='checkbox' name='checked[]' value='$id' />";
 234                  break;
 235                  case 'name':
 236                      $out .= $wp_cron['hook'];
 237                              $out .= self::get_actions($actions);
 238                  break;
 239                  case 'args':
 240                      $out .= json_encode($wp_cron['data']['args']);
 241                  break;
 242                  case 'next':
 243                      $timestamp = $wp_cron['time'];
 244  
 245                      $time_since = self::time_since($timestamp);
 246                      $next_date = date_i18n( 'D Y/m/d H:i:s', strtotime(get_date_from_gmt(gmdate('Y-m-d H:i:s', $timestamp))));
 247                      $next_date = str_replace(' ', '&#160;', $next_date);
 248                      
 249                      $out .= "$timestamp <abbr title=\"{$time_since}\">{$next_date}</abbr>";
 250                  break;
 251                  case 'rec':
 252                      $out .= ($wp_cron['data']['schedule']) ? ('<abbr title="' . sprintf(__('%1$s sec.'), $wp_cron['data']['interval']) . '">' . self::interval($wp_cron['data']['interval']) . '</abbr>') : __('Non-repeating', MP_TXTDOM);
 253                  break;
 254              }
 255  
 256              $out .= '</td>';
 257          }
 258          $out .= "</tr>\n";
 259  
 260          return $out;
 261      }
 262  
 263  	public static function time_since($newer_date) 
 264      {
 265          return self::interval( $newer_date - current_time('timestamp', 'gmt') );
 266      }
 267  
 268  	public static function interval( $since , $max = 2 ) 
 269      {
 270          // array of time period chunks
 271          $chunks = array ( array(60 * 60 * 24 * 365 ,    __('%s year',   MP_TXTDOM), __('%s years',   MP_TXTDOM)),
 272                      array(60 * 60 * 24 * 30 ,    __('%s month',  MP_TXTDOM), __('%s months',  MP_TXTDOM)),
 273                      array(60 * 60 * 24 * 7,        __('%s week',   MP_TXTDOM), __('%s weeks',   MP_TXTDOM)),
 274                      array(60 * 60 * 24 ,         __('%s day',    MP_TXTDOM), __('%s days',    MP_TXTDOM)),
 275                      array(60 * 60 ,                __('%s hour',   MP_TXTDOM), __('%s hours',   MP_TXTDOM)),
 276                      array(60 ,                    __('%s minute', MP_TXTDOM), __('%s minutes', MP_TXTDOM)),
 277                      array(1 ,                 __('%s second', MP_TXTDOM), __('%s seconds', MP_TXTDOM))
 278          );
 279  
 280          if( $since <= 0 ) return __('now', MP_TXTDOM);
 281  
 282          $done = $total = 0;
 283          $output = '';
 284  
 285          foreach($chunks as $chunk)
 286          {
 287              $count = floor( ($since - $total) / $chunk[0]);
 288              if (!$count) continue;
 289  
 290              $total += $count * $chunk[0];
 291  
 292              if ($done) $output .= ' ';
 293              $output .= sprintf(_n($chunk[1], $chunk[2], $count), $count);
 294              $done++;
 295              if ($done == $max) break;
 296          }
 297          return $output;
 298      }
 299  
 300  	public static function get_schedules() 
 301      {
 302          $schedules = array();
 303          $x = wp_get_schedules();
 304          uasort($x, create_function('$a,$b', 'return $a["interval"]-$b["interval"];'));
 305          foreach( $x as $name => $data ) $schedules[$name] = $data['display'] . ' (' . self::interval($data['interval']) . ')';
 306          $schedules['_oneoff'] = __('Non-repeating', MP_TXTDOM);
 307          return $schedules;
 308      }
 309  
 310  	public static function get($_hook, $_sig, $_next_run) 
 311      {
 312          $crons = _get_cron_array();
 313          foreach( $crons as $next_run => $cron ) 
 314          {
 315              foreach( $cron as $hook => $dings) 
 316              {
 317                  foreach( $dings as $sig => $data ) 
 318                  {
 319                      if( $hook == $_hook && $sig == $_sig && $next_run == $_next_run ) 
 320                      {
 321                          return array(    'hookname'    =>    $hook,
 322                                      'next_run'    =>    $next_run,
 323                                      'schedule'    =>    ($data['schedule']) ? $data['schedule'] : '_oneoff',
 324                                      'sig'        =>    $sig,
 325                                      'args'    =>    $data['args']
 326                          );
 327                              }
 328                  }
 329              }
 330          }
 331      }
 332  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1