On this page
WP_Upgrader::create_lock( string $lock_name, int $release_timeout = null ): bool
Creates a lock using WordPress options.
Parameters
$lock_namestring Required-
The name of this unique lock.
$release_timeoutint Optional-
The duration in seconds to respect an existing lock.
Default: 1 hour.Default:
null
Return
bool False if a lock couldn't be created or if the lock is still valid. True otherwise.
Source
File: wp-admin/includes/class-wp-upgrader.php. View all references
public static function create_lock( $lock_name, $release_timeout = null ) {
global $wpdb;
if ( ! $release_timeout ) {
$release_timeout = HOUR_IN_SECONDS;
}
$lock_option = $lock_name . '.lock';
// Try to lock.
$lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_option, time() ) );
if ( ! $lock_result ) {
$lock_result = get_option( $lock_option );
// If a lock couldn't be created, and there isn't a lock, bail.
if ( ! $lock_result ) {
return false;
}
// Check to see if the lock is still valid. If it is, bail.
if ( $lock_result > ( time() - $release_timeout ) ) {
return false;
}
// There must exist an expired lock, clear it and re-gain it.
WP_Upgrader::release_lock( $lock_name );
return WP_Upgrader::create_lock( $lock_name, $release_timeout );
}
// Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
update_option( $lock_option, time() );
return true;
}
Related
Uses
| Uses | Description |
|---|---|
| WP_Upgrader::release_lock() wp-admin/includes/class-wp-upgrader.php | Releases an upgrader lock. |
| WP_Upgrader::create_lock() wp-admin/includes/class-wp-upgrader.php | Creates a lock using WordPress options. |
| wpdb::query() wp-includes/class-wpdb.php | Performs a database query, using current database connection. |
| update_option() wp-includes/option.php | Updates the value of an option that was already added. |
| get_option() wp-includes/option.php | Retrieves an option value based on an option name. |
| wpdb::prepare() wp-includes/class-wpdb.php | Prepares a SQL query for safe execution. |
Used By
| Used By | Description |
|---|---|
| WP_Upgrader::create_lock() wp-admin/includes/class-wp-upgrader.php | Creates a lock using WordPress options. |
| WP_Automatic_Updater::run() wp-admin/includes/class-wp-automatic-updater.php | Kicks off the background update process, looping through all pending updates. |
| Core_Upgrader::upgrade() wp-admin/includes/class-core-upgrader.php | Upgrade WordPress core. |
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_upgrader/create_lock