database = $database; $this->table = $database->prefix . 'snippets'; } public function available(): bool { if ( ! $this->active_plugin_declared() || 1 !== preg_match( '/^[A-Za-z0-9_]+$/', $this->table ) ) { return false; } // Exact table name is derived from the current-site prefix and fixed suffix. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $found = $this->database->get_var( $this->database->prepare( 'SHOW TABLES LIKE %s', $this->database->esc_like( $this->table ) ) ); return $this->table === $found; } /** * Source is returned only to the in-memory tokenizer and never to storage or UI. * * @return array{id:int,code:string,size_bytes:int,oversized:bool}|null */ public function next_at( int $offset ): ?array { if ( $offset < 0 || $offset > self::MAX_SNIPPETS ) { throw new InvalidArgumentException( 'invalid_snippet_cursor' ); } if ( $offset >= self::MAX_SNIPPETS ) { return null; } // The table identifier is validated above and no callback/provider code is run. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $row = $this->database->get_row( $this->database->prepare( "SELECT id,code FROM {$this->table} WHERE active = %d ORDER BY id ASC LIMIT 1 OFFSET %d", 1, $offset ), ARRAY_A ); if ( ! is_array( $row ) ) { return null; } $code = (string) $row['code']; $size_bytes = strlen( $code ); $oversized = $size_bytes > self::MAX_BYTES; if ( $oversized ) { $code = ''; } return array( 'id' => (int) $row['id'], 'code' => $code, 'size_bytes' => $size_bytes, 'oversized' => $oversized, ); } private function active_plugin_declared(): bool { $active = get_option( 'active_plugins', array() ); $active = is_array( $active ) ? $active : array(); if ( is_multisite() ) { $network = get_site_option( 'active_sitewide_plugins', array() ); $active = array_merge( $active, is_array( $network ) ? array_keys( $network ) : array() ); } foreach ( $active as $plugin ) { if ( is_string( $plugin ) && in_array( $plugin, array( 'code-snippets/code-snippets.php', 'code-snippets-pro/code-snippets.php' ), true ) ) { return true; } } return false; } }