now(), interval 12 hour)" ); } /** * Set all rows to pending. */ public function set_all_to_pending() { // Get the database interface. $db = $this->get_db(); // Bail if no database interface is available. if ( empty( $db ) ) { return false; } $prefixed_table_name = $db->prefix . $this->table_name; /** * Filter condition for cleaning URLS in the database. * * @param string $condition condition for cleaning URLS in the database. * @returns string */ $condition = apply_filters( 'rocket_preload_all_to_pending_condition', ' WHERE 1 = 1' ); $db->query( "UPDATE `$prefixed_table_name` SET status = 'pending', modified = '" . current_time( 'mysql', true ) . "'$condition" ); } /** * Check if the page is preloaded. * * @param string $url url from the page to check. * @return bool */ public function is_preloaded( string $url ): bool { $pending_count = $this->query( [ 'count' => true, 'status' => 'in-progress', 'url' => untrailingslashit( $url ), ] ); return 0 !== $pending_count; } /** * Check if the page is pending. * * @param string $url url from the page to check. * @return bool */ public function is_pending( string $url ): bool { $pending_count = $this->query( [ 'count' => true, 'status' => 'pending', 'url' => untrailingslashit( $url ), ] ); return 0 !== $pending_count; } /** * Remove all entries from the table. * * @return false|void */ public function remove_all() { // Get the database interface. $db = $this->get_db(); // Bail if no database interface is available. if ( empty( $db ) ) { return false; } $prefixed_table_name = $db->prefix . $this->table_name; $db->query( "DELETE FROM `$prefixed_table_name` WHERE 1 = 1" ); } /** * Lock a URL. * * @param string $url URL to lock. * * @return void */ public function lock( string $url ) { $this->create_or_update( [ 'url' => $url, 'is_locked' => true, ] ); } /** * Unlock all URLs. * * @return false|void */ public function unlock_all() { // Get the database interface. $db = $this->get_db(); // Bail if no database interface is available. if ( empty( $db ) ) { return false; } $prefixed_table_name = $db->prefix . $this->table_name; $db->query( "UPDATE `$prefixed_table_name` SET is_locked = false;" ); } /** * Unlock a URL. * * @param string $url URL to unlock. * * @return void */ public function unlock( string $url ) { $this->create_or_update( [ 'url' => $url, 'is_locked' => false, ] ); } /** * Check if the url is rejected. * * @param string $url url to check. * @return bool */ protected function is_rejected( string $url ): bool { $extensions = [ 'php' => 1, 'xml' => 1, 'xsl' => 1, 'kml' => 1, ]; $extension = pathinfo( $url, PATHINFO_EXTENSION ); return $extension && isset( $extensions[ $extension ] ); } /** * Make the status from the task to failed. * * @param int $id id from the task. * @return bool */ public function make_status_failed( int $id ) { return $this->update_item( $id, [ 'status' => 'failed', 'modified' => current_time( 'mysql', true ), ] ); } /** * Update last accessed from the row. * * @param int $id id from the row. * @return bool */ public function update_last_access( int $id ) { return $this->update_item( $id, [ 'last_accessed' => current_time( 'mysql', true ), ] ); } /** * Return outdated in-progress jobs. * * @param int $delay delay to delete. * @return array|int */ public function get_outdated_in_progress_jobs( int $delay = 3 ) { return $this->query( [ 'status' => 'in-progress', 'is_locked' => false, 'date_query' => [ [ 'column' => 'modified', 'before' => "$delay minute ago", ], ], ], false ); } }