wpdb = null !== $wpdb ? $wpdb : $GLOBALS['wpdb']; } /** * {@inheritDoc} */ public function query( string $sql ): bool { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter return false !== $this->wpdb->query( $sql ); } /** * {@inheritDoc} */ public function rows( string $sql ): array { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter $rows = $this->wpdb->get_results( $sql, ARRAY_A ); return is_array( $rows ) ? $rows : array(); } /** * {@inheritDoc} */ public function row( string $sql ): ?array { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter $row = $this->wpdb->get_row( $sql, ARRAY_A ); return is_array( $row ) ? $row : null; } /** * {@inheritDoc} */ public function var( string $sql, $fallback = null ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter $value = $this->wpdb->get_var( $sql ); return null === $value ? $fallback : $value; } /** * {@inheritDoc} */ public function column( string $sql ): array { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter $values = $this->wpdb->get_col( $sql ); return is_array( $values ) ? $values : array(); } /** * {@inheritDoc} */ public function escape( string $value ): string { if ( null !== $this->wpdb && method_exists( $this->wpdb, '_real_escape' ) ) { $escaped = (string) $this->wpdb->_real_escape( $value ); if ( method_exists( $this->wpdb, 'remove_placeholder_escape' ) ) { $escaped = (string) $this->wpdb->remove_placeholder_escape( $escaped ); } return $escaped; } return addcslashes( $value, "\0\n\r\\'\"\x1a" ); } /** * {@inheritDoc} */ public function prefix(): string { return $this->wpdb->prefix; } /** * {@inheritDoc} */ public function insert( string $table, array $data ): int { $table = $this->wpdb->prefix . $table; // phpcs:ignore WordPress.DB.DirectDatabaseQuery $result = $this->wpdb->insert( $table, $data ); return false === $result ? 0 : (int) $this->wpdb->insert_id; } /** * {@inheritDoc} */ public function update( string $table, array $data, array $where ): int { $table = $this->wpdb->prefix . $table; // phpcs:ignore WordPress.DB.DirectDatabaseQuery $result = $this->wpdb->update( $table, $data, $where ); return false === $result ? 0 : (int) $result; } /** * {@inheritDoc} */ public function prepare( string $query, ...$args ): string { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared return $this->wpdb->prepare( $query, ...$args ); } /** * {@inheritDoc} */ public function error(): string { return (string) $this->wpdb->last_error; } }