-
+ 459556F5EA256DC1203C791544082897E2923ED4DF25923ADFF286C330CFB3691A972CCA10572A20D7AD0BCD876DC11ADD4F47E0DF57025B9BF6B53D82EE3507mp-wp/wp-includes/cron.php(0 . 0)(1 . 371)
86652 <?php
86653 /**
86654 * WordPress CRON API
86655 *
86656 * @package WordPress
86657 */
86658
86659 /**
86660 * Schedules a hook to run only once.
86661 *
86662 * Schedules a hook which will be executed once by the Wordpress actions core at
86663 * a time which you specify. The action will fire off when someone visits your
86664 * WordPress site, if the schedule time has passed.
86665 *
86666 * @since 2.1.0
86667 * @link http://codex.wordpress.org/Function_Reference/wp_schedule_single_event
86668 *
86669 * @param int $timestamp Timestamp for when to run the event.
86670 * @param callback $hook Function or method to call, when cron is run.
86671 * @param array $args Optional. Arguments to pass to the hook function.
86672 */
86673 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
86674 // don't schedule a duplicate if there's already an identical event due in the next 10 minutes
86675 $next = wp_next_scheduled($hook, $args);
86676 if ( $next && $next <= $timestamp + 600 )
86677 return;
86678
86679 $crons = _get_cron_array();
86680 $key = md5(serialize($args));
86681 $crons[$timestamp][$hook][$key] = array( 'schedule' => false, 'args' => $args );
86682 uksort( $crons, "strnatcasecmp" );
86683 _set_cron_array( $crons );
86684 }
86685
86686 /**
86687 * Schedule a periodic event.
86688 *
86689 * Schedules a hook which will be executed by the WordPress actions core on a
86690 * specific interval, specified by you. The action will trigger when someone
86691 * visits your WordPress site, if the scheduled time has passed.
86692 *
86693 * @since 2.1.0
86694 *
86695 * @param int $timestamp Timestamp for when to run the event.
86696 * @param string $recurrence How often the event should recur.
86697 * @param callback $hook Function or method to call, when cron is run.
86698 * @param array $args Optional. Arguments to pass to the hook function.
86699 * @return bool|null False on failure, null when complete with scheduling event.
86700 */
86701 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
86702 $crons = _get_cron_array();
86703 $schedules = wp_get_schedules();
86704 $key = md5(serialize($args));
86705 if ( !isset( $schedules[$recurrence] ) )
86706 return false;
86707 $crons[$timestamp][$hook][$key] = array( 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
86708 uksort( $crons, "strnatcasecmp" );
86709 _set_cron_array( $crons );
86710 }
86711
86712 /**
86713 * Reschedule a recurring event.
86714 *
86715 * @since 2.1.0
86716 *
86717 * @param int $timestamp Timestamp for when to run the event.
86718 * @param string $recurrence How often the event should recur.
86719 * @param callback $hook Function or method to call, when cron is run.
86720 * @param array $args Optional. Arguments to pass to the hook function.
86721 * @return bool|null False on failure. Null when event is rescheduled.
86722 */
86723 function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array()) {
86724 $crons = _get_cron_array();
86725 $schedules = wp_get_schedules();
86726 $key = md5(serialize($args));
86727 $interval = 0;
86728
86729 // First we try to get it from the schedule
86730 if ( 0 == $interval )
86731 $interval = $schedules[$recurrence]['interval'];
86732 // Now we try to get it from the saved interval in case the schedule disappears
86733 if ( 0 == $interval )
86734 $interval = $crons[$timestamp][$hook][$key]['interval'];
86735 // Now we assume something is wrong and fail to schedule
86736 if ( 0 == $interval )
86737 return false;
86738
86739 while ( $timestamp < time() + 1 )
86740 $timestamp += $interval;
86741
86742 wp_schedule_event( $timestamp, $recurrence, $hook, $args );
86743 }
86744
86745 /**
86746 * Unschedule a previously scheduled cron job.
86747 *
86748 * The $timestamp and $hook parameters are required, so that the event can be
86749 * identified.
86750 *
86751 * @since 2.1.0
86752 *
86753 * @param int $timestamp Timestamp for when to run the event.
86754 * @param callback $hook Function or method to call, when cron is run.
86755 * @param array $args Optional. Arguments to pass to the hook function.
86756 */
86757 function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
86758 $crons = _get_cron_array();
86759 $key = md5(serialize($args));
86760 unset( $crons[$timestamp][$hook][$key] );
86761 if ( empty($crons[$timestamp][$hook]) )
86762 unset( $crons[$timestamp][$hook] );
86763 if ( empty($crons[$timestamp]) )
86764 unset( $crons[$timestamp] );
86765 _set_cron_array( $crons );
86766 }
86767
86768 /**
86769 * Unschedule all cron jobs attached to a specific hook.
86770 *
86771 * @since 2.1.0
86772 *
86773 * @param callback $hook Function or method to call, when cron is run.
86774 * @param mixed $args,... Optional. Event arguments.
86775 */
86776 function wp_clear_scheduled_hook( $hook ) {
86777 $args = array_slice( func_get_args(), 1 );
86778
86779 while ( $timestamp = wp_next_scheduled( $hook, $args ) )
86780 wp_unschedule_event( $timestamp, $hook, $args );
86781 }
86782
86783 /**
86784 * Retrieve the next timestamp for a cron event.
86785 *
86786 * @since 2.1.0
86787 *
86788 * @param callback $hook Function or method to call, when cron is run.
86789 * @param array $args Optional. Arguments to pass to the hook function.
86790 * @return bool|int The UNIX timestamp of the next time the scheduled event will occur.
86791 */
86792 function wp_next_scheduled( $hook, $args = array() ) {
86793 $crons = _get_cron_array();
86794 $key = md5(serialize($args));
86795 if ( empty($crons) )
86796 return false;
86797 foreach ( $crons as $timestamp => $cron ) {
86798 if ( isset( $cron[$hook][$key] ) )
86799 return $timestamp;
86800 }
86801 return false;
86802 }
86803
86804 /**
86805 * Send request to run cron through HTTP request that doesn't halt page loading.
86806 *
86807 * @since 2.1.0
86808 *
86809 * @return null Cron could not be spawned, because it is not needed to run.
86810 */
86811 function spawn_cron( $local_time ) {
86812
86813 /*
86814 * do not even start the cron if local server timer has drifted
86815 * such as due to power failure, or misconfiguration
86816 */
86817 $timer_accurate = check_server_timer( $local_time );
86818 if ( !$timer_accurate )
86819 return;
86820
86821 //sanity check
86822 $crons = _get_cron_array();
86823 if ( !is_array($crons) )
86824 return;
86825
86826 $keys = array_keys( $crons );
86827 $timestamp = $keys[0];
86828 if ( $timestamp > $local_time )
86829 return;
86830
86831 $cron_url = get_option( 'siteurl' ) . '/wp-cron.php?check=' . wp_hash('187425');
86832 /*
86833 * multiple processes on multiple web servers can run this code concurrently
86834 * try to make this as atomic as possible by setting doing_cron switch
86835 */
86836 $flag = get_option('doing_cron');
86837
86838 // clean up potential invalid value resulted from various system chaos
86839 if ( $flag != 0 ) {
86840 if ( $flag > $local_time + 10*60 || $flag < $local_time - 10*60 ) {
86841 update_option('doing_cron', 0);
86842 $flag = 0;
86843 }
86844 }
86845
86846 //don't run if another process is currently running it
86847 if ( $flag > $local_time )
86848 return;
86849
86850 update_option( 'doing_cron', $local_time + 30 );
86851
86852 wp_remote_post($cron_url, array('timeout' => 0.01, 'blocking' => false));
86853 }
86854
86855 /**
86856 * Run scheduled callbacks or spawn cron for all scheduled events.
86857 *
86858 * @since 2.1.0
86859 *
86860 * @return null When doesn't need to run Cron.
86861 */
86862 function wp_cron() {
86863
86864 // Prevent infinite loops caused by lack of wp-cron.php
86865 if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false )
86866 return;
86867
86868 $crons = _get_cron_array();
86869
86870 if ( !is_array($crons) )
86871 return;
86872
86873 $keys = array_keys( $crons );
86874 if ( isset($keys[0]) && $keys[0] > time() )
86875 return;
86876
86877 $local_time = time();
86878 $schedules = wp_get_schedules();
86879 foreach ( $crons as $timestamp => $cronhooks ) {
86880 if ( $timestamp > $local_time ) break;
86881 foreach ( (array) $cronhooks as $hook => $args ) {
86882 if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
86883 continue;
86884 spawn_cron( $local_time );
86885 break 2;
86886 }
86887 }
86888 }
86889
86890 /**
86891 * Retrieve supported and filtered Cron recurrences.
86892 *
86893 * The supported recurrences are 'hourly' and 'daily'. A plugin may add more by
86894 * hooking into the 'cron_schedules' filter. The filter accepts an array of
86895 * arrays. The outer array has a key that is the name of the schedule or for
86896 * example 'weekly'. The value is an array with two keys, one is 'interval' and
86897 * the other is 'display'.
86898 *
86899 * The 'interval' is a number in seconds of when the cron job should run. So for
86900 * 'hourly', the time is 3600 or 60*60. For weekly, the value would be
86901 * 60*60*24*7 or 604800. The value of 'interval' would then be 604800.
86902 *
86903 * The 'display' is the description. For the 'weekly' key, the 'display' would
86904 * be <code>__('Once Weekly')</code>.
86905 *
86906 * For your plugin, you will be passed an array. you can easily add your
86907 * schedule by doing the following.
86908 * <code>
86909 * // filter parameter variable name is 'array'
86910 * $array['weekly'] = array(
86911 * 'interval' => 604800,
86912 * 'display' => __('Once Weekly')
86913 * );
86914 * </code>
86915 *
86916 * @since 2.1.0
86917 *
86918 * @return array
86919 */
86920 function wp_get_schedules() {
86921 $schedules = array(
86922 'hourly' => array( 'interval' => 3600, 'display' => __('Once Hourly') ),
86923 'twicedaily' => array( 'interval' => 43200, 'display' => __('Twice Daily') ),
86924 'daily' => array( 'interval' => 86400, 'display' => __('Once Daily') ),
86925 );
86926 return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
86927 }
86928
86929 /**
86930 * Retrieve Cron schedule for hook with arguments.
86931 *
86932 * @since 2.1.0
86933 *
86934 * @param callback $hook Function or method to call, when cron is run.
86935 * @param array $args Optional. Arguments to pass to the hook function.
86936 * @return string|bool False, if no schedule. Schedule on success.
86937 */
86938 function wp_get_schedule($hook, $args = array()) {
86939 $crons = _get_cron_array();
86940 $key = md5(serialize($args));
86941 if ( empty($crons) )
86942 return false;
86943 foreach ( $crons as $timestamp => $cron ) {
86944 if ( isset( $cron[$hook][$key] ) )
86945 return $cron[$hook][$key]['schedule'];
86946 }
86947 return false;
86948 }
86949
86950 //
86951 // Private functions
86952 //
86953
86954 /**
86955 * Retrieve cron info array option.
86956 *
86957 * @since 2.1.0
86958 * @access private
86959 *
86960 * @return array CRON info array.
86961 */
86962 function _get_cron_array() {
86963 $cron = get_option('cron');
86964 if ( ! is_array($cron) )
86965 return false;
86966
86967 if ( !isset($cron['version']) )
86968 $cron = _upgrade_cron_array($cron);
86969
86970 unset($cron['version']);
86971
86972 return $cron;
86973 }
86974
86975 /**
86976 * Updates the CRON option with the new CRON array.
86977 *
86978 * @since 2.1.0
86979 * @access private
86980 *
86981 * @param array $cron Cron info array from {@link _get_cron_array()}.
86982 */
86983 function _set_cron_array($cron) {
86984 $cron['version'] = 2;
86985 update_option( 'cron', $cron );
86986 }
86987
86988 /**
86989 * Upgrade a Cron info array.
86990 *
86991 * This function upgrades the Cron info array to version 2.
86992 *
86993 * @since 2.1.0
86994 * @access private
86995 *
86996 * @param array $cron Cron info array from {@link _get_cron_array()}.
86997 * @return array An upgraded Cron info array.
86998 */
86999 function _upgrade_cron_array($cron) {
87000 if ( isset($cron['version']) && 2 == $cron['version'])
87001 return $cron;
87002
87003 $new_cron = array();
87004
87005 foreach ( (array) $cron as $timestamp => $hooks) {
87006 foreach ( (array) $hooks as $hook => $args ) {
87007 $key = md5(serialize($args['args']));
87008 $new_cron[$timestamp][$hook][$key] = $args;
87009 }
87010 }
87011
87012 $new_cron['version'] = 2;
87013 update_option( 'cron', $new_cron );
87014 return $new_cron;
87015 }
87016
87017 // stub for checking server timer accuracy, using outside standard time sources
87018 function check_server_timer( $local_time ) {
87019 return true;
87020 }
87021
87022 ?>