[ Index ]

MailPress 7.1

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

title

Body

[close]

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

   1  <?php
   2  class MP_AdminPage extends MP_WP_Admin_page_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          $action = parent::redirect();
  18          if ( !$action ) return;
  19  
  20          $url_parms    = self::get_url_parms( array( 'paged', 'id', 'sig', 'next_run' ) );
  21          $checked    = ( isset( self::$get_['checked'] ) ) ? self::$get_['checked'] : array();
  22  
  23          $count    = str_replace( 'bulk-', '', $action );
  24          $count    .= 'd';
  25          $$count    = 0;
  26  
  27          switch( $action ) 
  28          {
  29              case 'bulk-delete' :
  30                  $crons = _get_cron_array();
  31                  foreach( $checked as $id ) 
  32                  {
  33                      $x = explode( '::', $id );
  34                      if( isset( $crons[$x[2]][$x[0]][$x[1]] ) )
  35                      {
  36                          wp_unschedule_event( $x[2], $x[0], $crons[$x[2]][$x[0]][$x[1]]['args'] );
  37                          $$count++;
  38                      }
  39                  }
  40  
  41                  $url_parms['message'] = ( $$count <= 1 ) ? 3 : 4;
  42                  if ( $$count )
  43                  {
  44                      $url_parms[$count] = $$count;
  45                  }
  46                  self::mp_redirect( self::url( MailPress_wp_cron, $url_parms ) );
  47              break;
  48  
  49              case 'add':
  50                  self::$pst_['args'] = json_decode( stripslashes( self::$pst_['args'] ), true );
  51                  if( !is_array( self::$pst_['args'] ) )
  52                  {
  53                      self::$pst_['args'] = array();
  54                  }
  55  
  56                  self::$pst_['next_run'] = strtotime( self::$pst_['next_run'] );
  57                  if( self::$pst_['next_run'] === false || self::$pst_['next_run'] == -1 )
  58                  {
  59                      self::$pst_['next_run'] = time();
  60                  }
  61  
  62                  if( self::$pst_['schedule'] == '_oneoff' )
  63                  {
  64                      $e = wp_schedule_single_event( self::$pst_['next_run'], self::$pst_['name'], self::$pst_['args'] ) === NULL;
  65                  }
  66                  else
  67                  {
  68                      $e = wp_schedule_event( self::$pst_['next_run'], self::$pst_['schedule'], self::$pst_['name'], self::$pst_['args'] ) === NULL;
  69                  }
  70  
  71                  $url_parms['message'] = ( $e ) ? 1 : 91;
  72                  self::mp_redirect( self::url( MailPress_wp_cron, $url_parms ) );
  73              break;
  74              case 'edited':
  75                  unset( self::$get_['action'] );
  76  
  77                  if ( !isset( self::$pst_['cancel'] ) ) 
  78                  {
  79                      $crons = _get_cron_array();
  80                      $x = explode( '::', self::$pst_['id'] );
  81                      if( isset( $crons[$x[2]][$x[0]][$x[1]] ) )
  82                      {
  83                          wp_unschedule_event( $x[2], $x[0], $crons[$x[2]][$x[0]][$x[1]]['args'] );
  84                      }
  85  
  86                      self::$pst_['args'] = json_decode( stripslashes( self::$pst_['args'] ), true );
  87                      if( !is_array( self::$pst_['args'] ) )
  88                      {
  89                          self::$pst_['args'] = array();
  90                      }
  91  
  92                      self::$pst_['next_run'] = strtotime( self::$pst_['next_run'] );
  93                      if( self::$pst_['next_run'] === false || self::$pst_['next_run'] == -1 )
  94                      {
  95                          self::$pst_['next_run'] = time();
  96                      }
  97  
  98                      if( self::$pst_['schedule'] == '_oneoff' )
  99                      {
 100                          $e = wp_schedule_single_event( self::$pst_['next_run'], self::$pst_['name'], self::$pst_['args'] ) === NULL;
 101                      }
 102                      else
 103                      {
 104                          $e = wp_schedule_event( self::$pst_['next_run'], self::$pst_['schedule'], self::$pst_['name'], self::$pst_['args'] ) === NULL;
 105                      }
 106  
 107                      $url_parms['message'] = ( $e ) ? 2 : 92 ;
 108                  }
 109                  unset( $url_parms['id'], $url_parms['sig'], $url_parms['next_run'] );
 110                  self::mp_redirect( self::url( MailPress_wp_cron, $url_parms ) );
 111              break;
 112              case 'do_now':
 113                  unset( self::$get_['action'] );
 114                  $e = false;
 115  
 116                  $crons = _get_cron_array();
 117                  foreach( $crons as $time => $cron ) 
 118                  {
 119                      if( isset( $cron[self::$get_['id']][self::$get_['sig']] ) ) 
 120                      {
 121                          wp_schedule_single_event( time()-1, self::$get_['id'], $cron[self::$get_['id']][self::$get_['sig']]['args'] );
 122                          spawn_cron();
 123                          $e = true;
 124                          break;
 125                      }
 126                  }
 127                  $url_parms['message'] = ( $e ) ? 5 : 95 ;
 128                  unset( $url_parms['id'], $url_parms['sig'], $url_parms['next_run'] );
 129                  self::mp_redirect( self::url( MailPress_wp_cron, $url_parms ) );
 130              break;
 131              case 'delete':
 132                  $crons = _get_cron_array();
 133                  if( isset( $crons[$url_parms['next_run']][$url_parms['id']][$url_parms['sig']] ) )
 134                  {
 135                      wp_unschedule_event( $url_parms['next_run'], $url_parms['id'], $crons[$url_parms['next_run']][$url_parms['id']][$url_parms['sig']]['args'] );
 136                  }
 137  
 138                  unset( $url_parms['id'], $url_parms['sig'], $url_parms['next_run'] );
 139  
 140                  $url_parms['message'] = 3;
 141                  self::mp_redirect( self::url( MailPress_wp_cron, $url_parms ) );
 142              break;
 143          }
 144      }
 145  
 146  //// Help ////
 147  
 148  	public static function add_help_tab() 
 149      {
 150          global $current_screen, $wp_version;
 151  
 152          $content = '';
 153          $content .= '<p><strong>' . __( 'Wp_cron :', 'MailPress' ) . '</strong></p>';
 154          $content .= '<p>' . __( 'Wp_cron (or WP-Cron or WP_Cron ...) is an internal WordPress set of functions with an API to schedule some jobs inside WordPress.', 'MailPress' );
 155          $content .= '   ' . __( 'WordPress uses it for scheduled posts, for any potential upgrade (WP, plugins, themes, ...), etc .', 'MailPress' ) . '</p>';
 156          $content .= '<p>' . __( 'MailPress uses it for scheduled Mails, for add-ons such as Autoresponder, Newsletter and optionally - depends on your settings - for Batch sending, Bounce handling, Delete old mails.', 'MailPress' ) . '</p>';
 157          $content .= '<p>' . __( 'MailPress Wp_cron add-on is just a basic facility to give an insight of what is going on in the background.', 'MailPress' ) . '</p>';
 158  
 159          $current_screen->add_help_tab( array(     'id'        => 'overview',
 160                                          'title'    => __( 'Overview' ),
 161                                          'content'    => $content )
 162          );
 163  
 164          $content = '';
 165          $content .= '<p>' . __( 'You can customize the display of this screen&#8217;s content in a number of ways:', 'MailPress' ) . '</p>';
 166          $content .= '<ul>';
 167          $content .= '<li>' . __( 'You can hide/display columns based on your needs and decide how many Wp_cron jobs to list per screen using the Screen Options tab.', 'MailPress' ) . '</li>';
 168          $content .= '<li>' . __( 'You will find an autorefresh option in the Screen Options tab as well.', 'MailPress' ) . '</li>';
 169          $content .= '</ul>';
 170  
 171          $current_screen->add_help_tab( array(     'id'        => 'screen-display',
 172                                          'title'    => __( 'Screen Display' ),
 173                                          'content'    => $content )
 174          );
 175  
 176          $content = '';
 177          $content .= '<p>' . __( 'Hovering over a row in the Wp_cron jobs list will display action links that allow you to manage your Wp_cron job.', 'MailPress' ) . '</p>';
 178          $content .= '<ul>';
 179          $content .= '<li>' . __( '<strong>Edit</strong> &mdash; takes you to the editing form for that instance of a Wp_cron job. You know what you are doing, dude !', 'MailPress' ) . '</li>';
 180          $content .= '<li>' . __( '<strong>Do now</strong> &mdash; execute that instance of the Wp_cron job immediately.', 'MailPress' ) . '</li>';
 181          $content .= '<li>' . __( '<strong>Delete</strong> &mdash; unschedule that instance of the Wp_cron job immediately.', 'MailPress' ) . '</li>';
 182          $content .= '</ul>';
 183  
 184          $current_screen->add_help_tab( array(     'id'        => 'action-links',
 185                                          'title'    => __( 'Available Actions' ),
 186                                          'content'    => $content )
 187          );
 188  
 189          $content = '';
 190          $content .= '<p>' . __( 'You can also permanently unschedule multiple instances of Wp_cron jobs at once. Select the Wp_cron jobs you want to act on using the checkboxes, then select the action you want to take from the Bulk Actions menu and click Apply.', 'MailPress' ) . '</p>';
 191  
 192          $current_screen->add_help_tab( array(     'id'        => 'bulk-actions',
 193                                          'title'    => __( 'Bulk Actions' ),
 194                                          'content'    => $content )
 195          );
 196  
 197          $content = '';
 198          $content .= '<p>' . __( 'Wp_cron is not a true scheduler.', 'MailPress' );
 199          $content .= '<br />' . __( 'A true scheduler is a subcomponent of modern OS (cron for Unix/Linux, Scheduled Tasks for Windows ...) and is operational 24/7 (as long as the computer is ON !).', 'MailPress' );
 200          $content .= '<br />' . __( 'By default, Wp_cron is only operational when your site (WordPress) is visited.', 'MailPress' ) . '</p>';
 201          $content .= '<p>' . __( 'Documentation is in different places, and mostly for developers :', 'MailPress' ) . '</p>';
 202          $content .= '<ul>';
 203          $content .= '<li>' . sprintf( __( 'in %s for the wp-cron api,', 'MailPress' ), sprintf( '<a href="%1$s" target="_blank">%2$s</a>', 'https://codex.wordpress.org/Category:WP-Cron_Functions', __( 'Codex', 'MailPress' ) ) ) . '</li>';
 204          $content .= '<li>' . sprintf( __( 'in the source code for %1$s, %2$s ...', 'MailPress' ), sprintf( '<a href="%1$s" target="_blank">%2$s</a>', 'https://core.trac.wordpress.org/browser/tags/' . $wp_version . '/src/wp-cron.php', '<code>wp-cron.php</code>' ), sprintf( '<a href="%1$s" target="_blank">%2$s</a>', 'https://core.trac.wordpress.org/browser/tags/' . $wp_version . '/src/wp-includes/cron.php#L0', '<code>wp-includes/cron.php</code>' ) ) . '</li>';
 205          $content .= '<li>' . sprintf( __( '<code>define(%s, \'true\');</code> in <code>wp-config.php</code> is documented out of WordPress.', 'MailPress' ), sprintf( '<a href="%1$s" target="_blank">%2$s</a>', '', "'DISABLE_WP_CRON'" ) );
 206          $content .= '    ' . __( 'Only disables WordPress from launching Wp_cron jobs and not slowing down your visitors.', 'MailPress' );
 207          $content .= '<br />' . __( 'All other Wp_cron functions are still available. All you need is to hit <code>wp-cron.php</code> file on desired frequency (see posts below).', 'MailPress' ) . '</li>';
 208          $content .= '</ul>';
 209          $content .= '<p>' . __( 'Some posts about Wp_cron :', 'MailPress' ) . '</p>';
 210          $content .= '<ul>';
 211          $content .= '<li>' . sprintf( '<a href="%1$s" target="_blank">%2$s</a>'  , 'https://www.lucasrolff.com/wordpress/why-wp-cron-sucks/', 'Why WP-Cron sucks' ) . __( ' &mdash; how to launch Wp_cron out of WordPress on a more regular basis and not slow down the visitor who happens to visit when the Wp_cron job is needed to run.' , 'MailPress' ) . '</li>';
 212          $content .= '<li>' . sprintf( '<a href="%1$s" target="_blank">%2$s</a>'  , 'https://www.100son.net/cron-wordpress-problemes-solutions/', 'Le Cron WordPress : probl&egrave;mes et solutions' ) . __( ' &mdash; (in french)' , 'MailPress' ) . '</li>';
 213          $content .= '<li>' . sprintf( '<a href="%1$s" target="_blank">%2$s</a>'  , 'https://ben.lobaugh.net/blog/20787/wordpress-how-to-use-wp-cron', 'WordPress: How to use WP-Cron' ) . __( ' &mdash; another post' , 'MailPress' ) . '</li>';
 214          $content .= '<li>' . sprintf( '<a href="%1$s" target="_blank">%2$s</a>'  , 'http://www.inmotionhosting.com/support/website/wordpress/disabling-the-wp-cronphp-in-wordpress', 'Disabling the wp-cron.php in WordPress ' ) . __( ' &mdash; a short video (in english) from a web host, but you got the idea !' , 'MailPress' ) . '</li>';
 215          $content .= '</ul>';
 216  
 217          $current_screen->add_help_tab( array(     'id'        => 'about-wp-cron',
 218                                          'title'    => __( 'About Wp_cron', 'MailPress' ),
 219                                          'content'    => $content )
 220          );
 221      }
 222  
 223  ////  Styles  ////
 224  
 225  	public static function print_styles( $s ) 
 226      {
 227          $styles = ( is_array( $s ) ) ? $s : array();
 228  
 229          wp_register_style( self::screen,     '/' . MP_PATH . 'mp-admin/css/wp_cron.css' );
 230          $styles[] = self::screen;
 231  
 232          parent::print_styles( $styles );
 233      }
 234  
 235  //// Scripts ////
 236  
 237  	public static function print_scripts( $scripts = array() ) 
 238      {
 239          $scripts = apply_filters( 'MailPress_autorefresh_js', $scripts );
 240  
 241          parent::print_scripts( $scripts );
 242      }
 243  
 244  ////  Columns  ////
 245  
 246  	public static function get_columns() 
 247      {
 248          $columns = array(    'cb'        => '<input type="checkbox" />', 
 249                          'name'    => __( 'Hook name', 'MailPress' ),
 250                          'next'    => __( 'Next&#160;run',  'MailPress' ),
 251                          'rec'        => __( 'Recurrence','MailPress' ),
 252                          'args'    => __( 'Arguments', 'MailPress' ),
 253          );
 254          return $columns;
 255      }
 256  
 257  ////  List  ////
 258  
 259  	public static function get_list( $args )
 260      {
 261          extract( $args );
 262  
 263          $wp_crons = array();
 264  
 265          $crons = _get_cron_array();
 266          if ( !$crons )
 267          {
 268              $crons = array();
 269          }
 270  
 271          foreach( $crons as $time => $cron )
 272          {
 273              foreach( $cron as $hook => $dings )
 274              {
 275                  foreach( $dings as $sig => $data )
 276                  {
 277                      $wp_crons[] = array(    'hook'    => $hook,
 278                                      'time'    => $time,
 279                                      'sig'        => $sig,
 280                                      'data'    => $data
 281                       );
 282                  }
 283              }
 284          }
 285  
 286          $total = count( $wp_crons );
 287          $rows  = array_slice ( $wp_crons, $start, $_per_page );
 288  
 289          return array( $rows, $total );
 290      }
 291  
 292  ////  Row  ////
 293  
 294  	public static function get_row( $wp_cron, $url_parms )
 295      {
 296          static $row_class = '';
 297  // url's
 298          $args = array();
 299  
 300          $args['id'] = $wp_cron['hook'];
 301          $args['sig'] = $wp_cron['sig'];
 302          $args['next_run'] = $wp_cron['time'];
 303  
 304          $id = $args['id'] . '::' . $args['sig'] . '::' . $args['next_run'];
 305  
 306          $args['action'] = 'delete';
 307          $delete_url = esc_url( self::url( MailPress_wp_cron, array_merge( $args, $url_parms ), 'delete-cron_' . $args['id'] . '_' . $args['sig'] . '_' . $args['next_run'] ) );
 308          $args['action'] = 'do_now';
 309          $do_now_url = esc_url( self::url( MailPress_wp_cron, array_merge( $args, $url_parms ) ) );
 310          $args['action'] = 'edit';
 311          $edit_url = esc_url( self::url( MailPress_wp_cron, array_merge( $args, $url_parms ) ) );
 312  
 313  // actions
 314          $actions = array();
 315  
 316          $actions['edit']   = '<a href="' . $edit_url   . '">' . __( 'Edit' ) . '</a>';
 317          $actions['do_now'] = '<a href="' . $do_now_url . '">' . __( 'Do now', 'MailPress' ) . '</a>';
 318          $actions['delete'] = '<a href="' . $delete_url . '">' . __( 'Delete' ) . '</a>';
 319  
 320          $row_class = ( ' class="alternate"' == $row_class ) ? '' : ' class="alternate"';
 321          $out  = '';
 322          $out .= '<tr id="wp_cron::' . $id . '" ' . $row_class . '>';
 323  
 324          $columns = self::get_columns();
 325          $hidden  = self::get_hidden_columns();
 326  
 327          foreach ( $columns as $column_name => $column_display_name ) 
 328          {
 329              $attributes = sprintf( 'class="%1$s column-%1$s%2$s"', $column_name, ( in_array( $column_name, $hidden ) ) ? ' hidden' : '' );
 330  
 331              $out .= '<td ' . $attributes . '>';
 332  
 333              switch ( $column_name ) 
 334              {
 335                  case 'cb':
 336                      $out .= '<input type="checkbox" name="checked[]" value="' . $id . '" />';
 337                  break;
 338                  case 'name':
 339                      $out .= $wp_cron['hook'];
 340                              $out .= self::get_actions( $actions );
 341                  break;
 342                  case 'args':
 343                      $out .= json_encode( $wp_cron['data']['args'] );
 344                  break;
 345                  case 'next':
 346                      $timestamp = $wp_cron['time'];
 347  
 348                      $time_since = self::time_since( $timestamp );
 349                      $next_date = date_i18n( 'D Y/m/d H:i:s', strtotime( get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $timestamp ) ) ) );
 350                      $next_date = str_replace( ' ', '&#160;', $next_date );
 351                      
 352                      $out .= $timestamp . ' <abbr title="' . $time_since . '">' . $next_date . '</abbr>';
 353                  break;
 354                  case 'rec':
 355                      $out .= ( $wp_cron['data']['schedule'] ) ? ( '<abbr title="' . esc_attr( sprintf( __( '%1$s sec.' ), $wp_cron['data']['interval'] ) ) . '">' . self::interval( $wp_cron['data']['interval'] ) . '</abbr>' ) : __( 'Non-repeating', 'MailPress' );
 356                  break;
 357              }
 358  
 359              $out .= '</td>';
 360          }
 361          $out .= '</tr>';
 362  
 363          return $out;
 364      }
 365  
 366  	public static function time_since( $newer_date ) 
 367      {
 368          return self::interval( $newer_date - current_time( 'timestamp', 'gmt' ) );
 369      }
 370  
 371  	public static function interval( $since , $max = 2 ) 
 372      {
 373          // array of time period chunks
 374          $chunks = array(array( 60 * 60 * 24 * 365 ,    __( '%s year',   'MailPress' ), __( '%s years',   'MailPress' ) ),
 375                      array( 60 * 60 * 24 * 30 ,        __( '%s month',  'MailPress' ), __( '%s months',  'MailPress' ) ),
 376                      array( 60 * 60 * 24 * 7,        __( '%s week',   'MailPress' ), __( '%s weeks',   'MailPress' ) ),
 377                      array( 60 * 60 * 24 ,            __( '%s day',    'MailPress' ), __( '%s days',    'MailPress' ) ),
 378                      array( 60 * 60 ,            __( '%s hour',   'MailPress' ), __( '%s hours',   'MailPress' ) ),
 379                      array( 60 ,                __( '%s minute', 'MailPress' ), __( '%s minutes', 'MailPress' ) ),
 380                      array( 1 ,                    __( '%s second', 'MailPress' ), __( '%s seconds', 'MailPress' ) )
 381           );
 382  
 383          if( $since <= 0 )
 384          {
 385              return __( 'now', 'MailPress' );
 386          }
 387  
 388          $done = $total = 0;
 389          $out = '';
 390  
 391          foreach( $chunks as $chunk )
 392          {
 393              $count = floor( ( $since - $total ) / $chunk[0] );
 394              if ( !$count )
 395              {
 396                  continue;
 397              }
 398  
 399              $total += $count * $chunk[0];
 400  
 401              if ( $done )
 402              {
 403                  $out .= ' ';
 404              }
 405              $out .= sprintf( _n( $chunk[1], $chunk[2], $count ), $count );
 406              $done++;
 407              if ( $done == $max ) break;
 408          }
 409          return $out;
 410      }
 411  
 412  	public static function get_schedules() 
 413      {
 414          $schedules = array();
 415          $x = wp_get_schedules();
 416          uasort( $x, array( 'MP_AdminPage', 'sort_schedules' ) );
 417          foreach( $x as $name => $data )
 418          {
 419              $schedules[$name] = $data['display'] . ' ( ' . self::interval( $data['interval'] ) . ' )';
 420          }
 421          $schedules['_oneoff'] = __( 'Non-repeating', 'MailPress' );
 422          return $schedules;
 423      }
 424  
 425  	public static function sort_schedules( $a, $b ) 
 426      {
 427          return $a['interval'] - $b['interval'];
 428      }
 429  
 430  	public static function get( $_hook, $_sig, $_next_run ) 
 431      {
 432          $crons = _get_cron_array();
 433          foreach( $crons as $next_run => $cron ) 
 434          {
 435              foreach( $cron as $hook => $dings ) 
 436              {
 437                  foreach( $dings as $sig => $data ) 
 438                  {
 439                      if( $hook == $_hook && $sig == $_sig && $next_run == $_next_run ) 
 440                      {
 441                          return array(     'hookname'    =>    $hook,
 442                                      'next_run'    =>    $next_run,
 443                                      'schedule'    =>    ( $data['schedule'] ) ? $data['schedule'] : '_oneoff',
 444                                      'sig'        =>    $sig,
 445                                      'args'    =>    $data['args']
 446                           );
 447                              }
 448                  }
 449              }
 450          }
 451      }
 452  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1