y_key_exists( 'id', $args ) ) { unset( $args['id'] ); } $args = wp_parse_args( $args, [ 'sources' => '', 'url_to' => '', 'header_code' => '301', 'hits' => '0', 'status' => 'active', 'created' => current_time( 'mysql' ), 'updated' => current_time( 'mysql' ), ] ); if ( in_array( $args['header_code'], [ 410, 451 ], true ) ) { $args['url_to'] = ''; } $args['sources'] = maybe_serialize( $args['sources'] ); return self::table()->insert( $args, [ '%s', '%s', '%d', '%d', '%s', '%s', '%s' ] ); } /** * Update a record. * * @param array $args Values to update. * * @return bool|int */ public static function update( $args = [] ) { if ( empty( $args ) ) { return false; } $args = wp_parse_args( $args, [ 'id' => '', 'sources' => '', 'url_to' => '', 'header_code' => '301', 'status' => 'active', 'updated' => current_time( 'mysql' ), ] ); $id = absint( $args['id'] ); if ( 0 === $id ) { return false; } $args['sources'] = maybe_serialize( $args['sources'] ); unset( $args['id'] ); if ( in_array( $args['header_code'], [ 410, 451 ], true ) ) { $args['url_to'] = ''; } Cache::purge( $id ); return self::table()->set( $args )->where( 'id', $id )->update(); } /** * Add or Update record. * * @param array $redirection Single redirection item. * * @return int */ public static function update_iff( $redirection ) { // Update record. if ( isset( $redirection['id'] ) && ! empty( $redirection['id'] ) ) { self::update( $redirection ); return $redirection['id']; } $existing_redirection = self::match_redirections_source( maybe_serialize( $redirection['sources'] ) ); if ( ! empty( $existing_redirection ) && isset( $existing_redirection[0]['id'] ) ) { $redirection['id'] = $existing_redirection[0]['id']; self::update( $redirection ); return $redirection['id']; } // Add record. return self::add( $redirection ); } /** * Update counter for redirection. * * @param object $redirection Record to update. * * @return int|false The number of rows updated, or false on error. */ public static function update_access( $redirection = false ) { if ( empty( $redirection ) ) { return false; } $args['hits'] = absint( $redirection['hits'] ) + 1; $args['last_accessed'] = current_time( 'mysql' ); return self::table()->set( $args )->where( 'id', $redirection['id'] )->update(); } /** * Delete multiple records. * * @param array $ids Array of ids to delete. * * @return int Number of records deleted. */ public static function delete( $ids ) { Cache::purge( $ids ); $deleted = self::table()->whereIn( 'id', (array) $ids )->delete(); /** * Fires after deleting redirections. */ do_action( 'rank_math/redirection/deleted', $ids, $deleted ); return $deleted; } /** * Change record status to active or inactive. * * @param array $ids Array of ids. * @param bool $status Active=1, Inactive=0. * * @return int Number of records updated. */ public static function change_status( $ids, $status ) { if ( ! self::is_valid_status( $status ) ) { return false; } return self::table()->set( 'status', $status ) ->set( 'updated', current_time( 'mysql' ) ) ->whereIn( 'id', (array) $ids )->update(); } /** * Clean trashed redirects after 30 days. * * @return void */ public static function periodic_clean_trash() { $ids = self::table()->select( 'id' )->where( 'status', 'trashed' )->where( 'updated', '<=', date_i18n( 'Y-m-d', strtotime( '30 days ago' ) ) )->get( ARRAY_A ); if ( empty( $ids ) ) { return; } self::delete( wp_list_pluck( $ids, 'id' ) ); } /** * Delete all trashed redirections and associated sources. * * @return int Number of records deleted. */ public static function clear_trashed() { $ids = self::table()->select( 'id' )->where( 'status', 'trashed' )->get(); if ( empty( $ids ) ) { return 0; } return self::delete( wp_list_pluck( $ids, 'id' ) ); } /** * Check if status is valid. * * @param string $status Status to validate. * * @return bool */ private static function is_valid_status( $status ) { $allowed = [ 'active', 'inactive', 'trashed' ]; return in_array( $status, $allowed, true ); } /** * Get redirection source. * * @param array $sources Unserialized sources. * * @return array */ private static function get_sources( $sources ) { if ( ! is_array( $sources ) || empty( $sources ) ) { return $sources; } foreach ( $sources as $key => $source ) { $sources[ $key ]['pattern'] = wp_specialchars_decode( $source['pattern'] ); } return $sources; } }