home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-includes / cron.php < prev    next >
Encoding:
PHP Script  |  2008-08-02  |  5.1 KB  |  195 lines

  1. <?php
  2.  
  3. function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
  4.     $crons = _get_cron_array();
  5.     $key = md5(serialize($args));
  6.     $crons[$timestamp][$hook][$key] = array( 'schedule' => false, 'args' => $args );
  7.     uksort( $crons, "strnatcasecmp" );
  8.     _set_cron_array( $crons );
  9. }
  10.  
  11. function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
  12.     $crons = _get_cron_array();
  13.     $schedules = wp_get_schedules();
  14.     $key = md5(serialize($args));
  15.     if ( !isset( $schedules[$recurrence] ) )
  16.         return false;
  17.     $crons[$timestamp][$hook][$key] = array( 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
  18.     uksort( $crons, "strnatcasecmp" );
  19.     _set_cron_array( $crons );
  20. }
  21.  
  22. function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array()) {
  23.     $crons = _get_cron_array();
  24.     $schedules = wp_get_schedules();
  25.     $key = md5(serialize($args));
  26.     $interval = 0;
  27.  
  28.     // First we try to get it from the schedule
  29.     if ( 0 == $interval )
  30.         $interval = $schedules[$recurrence]['interval'];
  31.     // Now we try to get it from the saved interval in case the schedule disappears
  32.     if ( 0 == $interval )
  33.         $interval = $crons[$timestamp][$hook][$key]['interval'];
  34.     // Now we assume something is wrong and fail to schedule
  35.     if ( 0 == $interval )
  36.         return false;
  37.  
  38.     while ( $timestamp < time() + 1 )
  39.         $timestamp += $interval;
  40.  
  41.     wp_schedule_event( $timestamp, $recurrence, $hook, $args );
  42. }
  43.  
  44. function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
  45.     $crons = _get_cron_array();
  46.     $key = md5(serialize($args));
  47.     unset( $crons[$timestamp][$hook][$key] );
  48.     if ( empty($crons[$timestamp][$hook]) )
  49.         unset( $crons[$timestamp][$hook] );
  50.     if ( empty($crons[$timestamp]) )
  51.         unset( $crons[$timestamp] );
  52.     _set_cron_array( $crons );
  53. }
  54.  
  55. function wp_clear_scheduled_hook( $hook ) {
  56.     $args = array_slice( func_get_args(), 1 );
  57.  
  58.     while ( $timestamp = wp_next_scheduled( $hook, $args ) )
  59.         wp_unschedule_event( $timestamp, $hook, $args );
  60. }
  61.  
  62. function wp_next_scheduled( $hook, $args = array() ) {
  63.     $crons = _get_cron_array();
  64.     $key = md5(serialize($args));
  65.     if ( empty($crons) )
  66.         return false;
  67.     foreach ( $crons as $timestamp => $cron ) {
  68.         if ( isset( $cron[$hook][$key] ) )
  69.             return $timestamp;
  70.     }
  71.     return false;
  72. }
  73.  
  74. function spawn_cron() {
  75.     $crons = _get_cron_array();
  76.  
  77.     if ( !is_array($crons) )
  78.         return;
  79.  
  80.     $keys = array_keys( $crons );
  81.     if ( array_shift( $keys ) > time() )
  82.         return;
  83.  
  84.     $cron_url = get_option( 'siteurl' ) . '/wp-cron.php';
  85.     $parts = parse_url( $cron_url );
  86.  
  87.     if ($parts['scheme'] == 'https') {
  88.         // support for SSL was added in 4.3.0
  89.         if (version_compare(phpversion(), '4.3.0', '>=') && function_exists('openssl_open')) {
  90.             $port = isset($parts['port']) ? $parts['port'] : 443;
  91.             $argyle = @fsockopen('ssl://' . $parts['host'], $port, $errno, $errstr, 0.01);
  92.         } else {
  93.             return false;
  94.         }
  95.     } else {
  96.         $port = isset($parts['port']) ? $parts['port'] : 80;
  97.         $argyle = @ fsockopen( $parts['host'], $port, $errno, $errstr, 0.01 );
  98.     }
  99.  
  100.     if ( $argyle )
  101.         fputs( $argyle,
  102.               "GET {$parts['path']}?check=" . wp_hash('187425') . " HTTP/1.0\r\n"
  103.             . "Host: {$_SERVER['HTTP_HOST']}\r\n\r\n"
  104.         );
  105. }
  106.  
  107. function wp_cron() {
  108.     // Prevent infinite loops caused by lack of wp-cron.php
  109.     if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false )
  110.         return;
  111.  
  112.     $crons = _get_cron_array();
  113.  
  114.     if ( !is_array($crons) )
  115.         return;
  116.  
  117.     $keys = array_keys( $crons );
  118.     if ( isset($keys[0]) && $keys[0] > time() )
  119.         return;
  120.  
  121.     $schedules = wp_get_schedules();
  122.     foreach ( $crons as $timestamp => $cronhooks ) {
  123.         if ( $timestamp > time() ) break;
  124.         foreach ( $cronhooks as $hook => $args ) {
  125.             if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
  126.                 continue;
  127.             spawn_cron();
  128.             break 2;
  129.         }
  130.     }
  131. }
  132.  
  133. function wp_get_schedules() {
  134.     $schedules = array(
  135.         'hourly' => array( 'interval' => 3600, 'display' => __('Once Hourly') ),
  136.         'twicedaily' => array( 'interval' => 43200, 'display' => __('Twice Daily') ),
  137.         'daily' => array( 'interval' => 86400, 'display' => __('Once Daily') ),
  138.     );
  139.     return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
  140. }
  141.  
  142. function wp_get_schedule($hook, $args = array()) {
  143.     $crons = _get_cron_array();
  144.     $key = md5(serialize($args));
  145.     if ( empty($crons) )
  146.         return false;
  147.     foreach ( $crons as $timestamp => $cron ) {
  148.         if ( isset( $cron[$hook][$key] ) )
  149.             return $cron[$hook][$key]['schedule'];
  150.     }
  151.     return false;
  152. }
  153.  
  154. //
  155. // Private functions
  156. //
  157.  
  158. function _get_cron_array()  {
  159.     $cron = get_option('cron');
  160.     if ( ! is_array($cron) )
  161.         return false;
  162.  
  163.     if ( !isset($cron['version']) )
  164.         $cron = _upgrade_cron_array($cron);
  165.  
  166.     unset($cron['version']);
  167.  
  168.     return $cron;
  169. }
  170.  
  171. function _set_cron_array($cron) {
  172.     $cron['version'] = 2;
  173.     update_option( 'cron', $cron );
  174. }
  175.  
  176. function _upgrade_cron_array($cron) {
  177.     if ( isset($cron['version']) && 2 == $cron['version'])
  178.         return $cron;
  179.  
  180.     $new_cron = array();
  181.  
  182.     foreach ($cron as $timestamp => $hooks) {
  183.         foreach ( $hooks as $hook => $args ) {
  184.             $key = md5(serialize($args['args']));
  185.             $new_cron[$timestamp][$hook][$key] = $args;
  186.         }
  187.     }
  188.  
  189.     $new_cron['version'] = 2;
  190.     update_option( 'cron', $new_cron );
  191.     return $new_cron;
  192. }
  193.  
  194. ?>
  195.