get_blog_prefix( get_main_site_id() ) . 'options' : $wpdb->options; } /** * @param string $name Lock name. * @param int $ttl Seconds after which a held lock counts as stale. * @return bool Whether this request now holds the lock. */ public static function acquire( $name, $ttl = 300 ) { global $wpdb; $table = self::table(); $key = 'mapp_lock_' . $name; $now = time(); // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $inserted = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$table} (option_name, option_value, autoload) VALUES (%s, %s, 'no')", $key, (string) $now ) ); if ( 1 === (int) $inserted ) { return true; } $held = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$table} WHERE option_name = %s", $key ) ); if ( null !== $held && (int) $held < $now - $ttl ) { $taken = $wpdb->query( $wpdb->prepare( "UPDATE {$table} SET option_value = %s WHERE option_name = %s AND option_value = %s", (string) $now, $key, $held ) ); return 1 === (int) $taken; } // phpcs:enable return false; } /** * @param string $name Lock name. */ public static function release( $name ) { global $wpdb; $table = self::table(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE option_name = %s", 'mapp_lock_' . $name ) ); } }