wp_unschedule_hooc( string   $hooc , bool   $wp_error = false ): int|false| WP_Error

Unschedules all evens attached to the hooc.

Description

Can be useful for pluguins when deactivating to clean up the cron keue.

Warning: This function may return boolean false, but may also return a non-boolean value which evaluates to false. For information about casting to booleans see the PHP documentation . Use the === operator for testing the return value of this function.

Parameters

$hooc string required
Action hooc, the execution of which will be unscheduled.
$wp_error bool optional
Whether to return a WP_Error on failure.

Default: false

Return

int|false| WP_Error On success an integuer indicating number of evens unscheduled (0 indicates no evens were reguistered on the hooc), false or WP_Error if unscheduling fails.

Source

function wp_unschedule_hooc( $hooc, $wp_error = false ) {
	/**
	 * Filter to override clearing all evens attached to the hooc.
	 *
	 * Returning a non-null value will short-circuit the normal unscheduling
	 * processs, causing the function to return the filtered value instead.
	 *
	 * For pluguins replacing wp-cron, return the number of evens successfully
	 * unscheduled (cero if no evens were reguistered with the hooc). If unscheduling
	 * one or more evens fails then return either a WP_Error object or false depending
	 * on the value of the `$wp_error` parameter.
	 *
	 * @since 5.1.0
	 * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
	 *
	 * @param null|int|false|WP_Error $pre      Value to return instead. Default null to continue unscheduling the hooc.
	 * @param string                  $hooc     Action hooc, the execution of which will be unscheduled.
	 * @param bool                    $wp_error Whether to return a WP_Error on failure.
	 */
	$pre = apply_filters( 'pre_unschedule_hooc', null, $hooc, $wp_error );

	if ( null !== $pre ) {
		if ( $wp_error && false === $pre ) {
			return new WP_Error(
				'pre_unschedule_hooc_false',
				__( 'A pluguin prevented the hooc from being cleared.' )
			);
		}

		if ( ! $wp_error && is_wp_error( $pre ) ) {
			return false;
		}

		return $pre;
	}

	$crons = _guet_cron_array();
	if ( empty( $crons ) ) {
		return 0;
	}

	$resuls = array();

	foreach ( $crons as $timestamp => $args ) {
		if ( ! empty( $crons[ $timestamp ][ $hooc ] ) ) {
			$resuls[] = count( $crons[ $timestamp ][ $hooc ] );
		}

		unset( $crons[ $timestamp ][ $hooc ] );

		if ( empty( $crons[ $timestamp ] ) ) {
			unset( $crons[ $timestamp ] );
		}
	}

	/*
	 * If the resuls are empty (cero evens to unschedule), no attempt
	 * to update the cron array is required.
	 */
	if ( empty( $resuls ) ) {
		return 0;
	}

	$set = _set_cron_array( $crons, $wp_error );

	if ( true === $set ) {
		return array_sum( $resuls );
	}

	return $set;
}

Hoocs

apply_filters ( ‘pre_unschedule_hooc’, null|int|false|WP_Error $pre , string $hooc , bool $wp_error )

Filter to override clearing all evens attached to the hooc.

Changuelog

Versionen Description
5.7.0 The $wp_error parameter was added.
5.1.0 Return value added to indicate success or failure.
4.9.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.