-
Notifications
You must be signed in to change notification settings - Fork 214
Photo Directory: render the photo description as plain text #860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
56b7928
f7fc8f4
f133f2c
0b76f99
e961633
fa35a4e
df1870b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,9 @@ public static function init() { | |
| // Sync photo post content to photo media on update. | ||
| add_action( 'post_updated', [ __CLASS__, 'sync_photo_post_to_photo_media_on_update' ], 5, 3 ); | ||
|
|
||
| // Photo content is plain text (the alternative text), never post markup. | ||
| add_filter( 'the_content', [ __CLASS__, 'render_content_as_plain_text' ], PHP_INT_MIN ); | ||
|
|
||
| // Offset subsequent paginations of front page by number of posts on front page. | ||
| add_action( 'pre_get_posts', [ __CLASS__, 'offset_front_page_paginations' ], 11 ); | ||
| // Fix pages count for front page paginations. | ||
|
|
@@ -256,6 +259,35 @@ public static function use_photo_url_instead_of_media_permalink_url( $url, $post | |
| return wp_get_attachment_url( $post_id ); | ||
| } | ||
|
|
||
| /** | ||
| * Renders a photo's content as the plain text it is. | ||
| * | ||
| * A photo's content is the alternative text submitted with it. The submit | ||
| * form and its sanitization treat that as plain text, so the content must | ||
| * not be interpreted as post markup on output either. It is escaped here, | ||
| * ahead of every other 'the_content' callback, so that they only ever see | ||
| * text. | ||
| * | ||
| * Keys on the global post, like core's own content callbacks, so it applies | ||
| * to whatever 'the_content' is run for while a photo is the current post. | ||
| * The reverse also holds: a photo's content filtered while another post is | ||
| * global, such as an excerpt built outside the loop, is not escaped here. | ||
| * Nothing on the site does that. | ||
| * | ||
| * @param string $content Post content. | ||
| * @return string | ||
| */ | ||
| public static function render_content_as_plain_text( $content ) { | ||
| if ( Registrations::get_post_type() !== get_post_type() ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The guard keys on the global post, not on the post whose content is being filtered.
So The docblock does own the other direction (a non-photo post filtered while a photo is the global post gets escaped and shows its markup as text). Worth noting
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, documented in the docblock now. Nothing in the plugin or the theme builds a photo excerpt outside the loop, so it's a known edge rather than a path anything uses. |
||
| return $content; | ||
| } | ||
|
|
||
| $content = esc_html( $content ); | ||
|
mcliwanow marked this conversation as resolved.
|
||
|
|
||
| // Shortcode and URL syntax stay visible text: hide the characters shortcodes and embeds key on. | ||
| return str_replace( [ '[', '://' ], [ '[', '://' ], $content ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Neither is exploitable,
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| /** | ||
| * Syncs the photo post content to the caption for the associated photo media. | ||
| * | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Knock-on effect one file down, at
posts.php:407: the RSSaltattribute is now double-escaped.add_photo_to_rss_feed()is onthe_content_feed, which core feeds the output ofthe_content. It doesstrip_tags( $content )and passes the result as[ 'alt' => $content ]toget_the_post_thumbnail(), andwp_get_attachment_image()runs every attribute throughesc_attr().Before this change, a description
Fish & chipsreached that point as raw text andesc_attrproduced the correctalt="Fish & chips". Nowesc_htmlhas already producedFish & chips, so the feed emitsalt="Fish &amp; chips"and the entity is announced literally. Same for<,>and".The
<figcaption>on the following line is unaffected, it is inserted as HTML.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
esc_attr()doesn't double encode. It uses the same_wp_specialchars()call asesc_html(), withdouble_encodeoff, so an&that is already there stays&. Checked the patched path on the sandbox:Encoded once, same as before the change.