#5050 closed enhancement (fixed)
the_category and get_the_category_list should accept post ID as an argument
| Reported by: | filosofo | Owned by: | ryan |
|---|---|---|---|
| Priority: | normal | Milestone: | 2.5 |
| Component: | Template | Version: | 2.3 |
| Severity: | normal | Keywords: | the_category get_the_category get_the_category_list has-patch |
| Cc: | Focuses: |
Description
There are a number of times when it would be nice to get the categories for a post outside of the loop.
My patch allows one to specify the post id, with no back-compat issues, as get_the_category_list calls get_the_category, which already can accept post ID as an argument.
Attachments (1)
Change History (7)
This ticket was mentioned in PR #5683 on WordPress/wordpress-develop by @dmsnell.
3 years ago
#4
👷🏗️🚧
This is a work-in-progress PR building full tokenization scanning of an HTML document. This is being added into the Tag Processor and will be a necessary component for a number of related changes to the HTML API:
- Reading and modifying inner and outer content.
- Serializing HTML.
- Methods to transform text content while preserving or stripping away markup.
Enables syntax-aware processing such as wp_truncate_html() [gist]
Replaces/incorporates chunked/extended processing in #5050
Replaces/incorporates stopping at comments in dmsnell/wordpress-develop#7
Provides critical functionality for inner/outer getter/setter in [dmsnell/wordpress-develop#10, #4965]
## Design Changes
In this change we're introducing two features stemming from two internal changes:
next_token()provides the ability to scan every token in the HTML stream.- it is possible to parse HTML in chunks without having the entire document in memory.
The internal changes powering this are:
- internal state adopts a new parsing mode which allows resuming from the middle of a previous match.
- the new concept of _modifiable text_ and a _token_ proper tracks the bounds of the currently-matched token as well as a safe region that can be changed without impacting the document syntax, if one exists.
For example, when encountering an HTML comment the parser will track the following token information:
This .
│ │ │ └ end of token
│ │ └─── end of text
│ └──────────────── start of text
└──────────────────── start of token
Not every token will have a text region, but it's important to track the entire token and any text region because similar tokens may have different syntax. For example, an invalid comment is still a comment.
This <? is also a comment --!>.
│ │ │ └ end of token
│ │ └──── end of text
│ └────────────────────── start of text
└──────────────────────── start of token
This holds for tokens whose entire content is text, such as with the #text node.
<div>This is text.</div>
│ ├ end of token
│ └ end of text
├──────────── start of text
└──────────── start of token
### Scanning tokens
In order to keep the next_tag() interface and use clear, it is left unchanged. For operations needing access to the token stream, there is no built-in query mechanism and querying ought to be performed inside a next_token() loop. get_node_name() indicates what kind of token is currently matched, and get_node_text() returns the modifiable text if available.
function wp_strip_all_tags( $html, $remove_breaks ) { $processor = new WP_HTML_Processor( $html ); $text_content = ''; while ( $processor->next_token() ) { if ( '#text' === $processor->get_node_name() ) { $text_content .= $processor->get_node_text(); } } return $remove_breaks ? trim( preg_replace( '/[\r\n\t ]+/', ' ', $text_content ) ) : $text_content; }
- Most tags have no modifiable content.
- The inner contents of special tags whose contents cannot contain HTML is their modifiable content. The inner contents of these tags are not rendered in the page.
- IFRAME
- NOEMBED, NOFRAMES, NOSCRIPT
- SCRIPT
- STYLE
- TEXTAREA
- TITLE
## TODO
- [x] Add
next_token()method to scan each token. - [x] Stop at RCDATA sections and
SCRIPT,STYLE,TITLE,TEXTAREA, etc… - [x] Stop at text nodes.
- [x] Indicate a continuation state to support resumable parsing. This is necessary for stopping at
SCRIPTtags and other tags with special closing rules. These are currently handled by skipping to the end of the element when finding the starting tag, but this has introduced a few challenges and bugs (for example, the Tag Processor fails to stop at a<title>tag if the document ends before the</title>closer is found). - [x] Add
rewind()method to reverse to the start of the document.
@jonsurrell commented on PR #5683:
3 years ago
#5
There are two TODO items unchecked in the first list:
Review what this enables in the html5lib test suite.
☝️ I think we can handle that after we introduce the test suite.
Distinguish too-short HTML comments that may cause trouble when modifying. E.g. <!--->.
Does this still need to be handled?
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
(In [6899]) Add optional post ID arg to the_category and get_the_category_list. Props filosofo. fixes #5050