Repository navigation
Render the single listing template apart from the content filters - #3185
Conversation
The template prints listing fields, forms and request data inside the_content, where later filters process it as if it were post content. Hold the rendered template aside, give the later filters a placeholder, and put the template in its place after the last of them. Shortcodes in the description of a trusted listing are still expanded. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… filters Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
|
||
| // A filter may have rewritten the placeholder element, so it is found by its token. | ||
| $content = preg_replace_callback( | ||
| '/<div\b[^<>]*' . preg_quote( $token, '/' ) . '[^<>]*>\s*<\/div>/', |
There was a problem hiding this comment.
Is a html regex the best way to do this?
There was a problem hiding this comment.
I think so here, with one gap that is now closed in f1887c93. The pattern doesn't parse anyone's HTML: it matches one empty element that this class wrote a moment earlier, located by a random token made for that render. It allows for extra attributes or different quoting because optimisation plugins add attributes to elements. WP_HTML_Tag_Processor can find the element by class but has no public way to replace an element with other markup, so it would only do half the job.
The gap: if a filter changed the placeholder into something the pattern doesn't match (put content inside it, say), the listing was dropped. It is now added after the content instead, with a test.
| } | ||
|
|
||
| // What the later content filters would have done to the template that is still wanted. | ||
| $job_content = $this->strip_untrusted_listing_media( (string) $job_content ); |
There was a problem hiding this comment.
Why the string cast?
There was a problem hiding this comment.
A job_manager_single_job_content filter can return null or false, and the next two calls expect a string. Moved the cast to its own line with a comment saying so (f1887c93).
| return $description; | ||
| } | ||
|
|
||
| $post = self::get_listing_for_content( null === $post ? null : get_post( $post ) ); |
There was a problem hiding this comment.
Can we replace the ternary with something more explicit for readability
There was a problem hiding this comment.
Done in f1887c93, here and in expand_filtered_description_shortcodes().
Changes Proposed in this Pull Request
On a single listing page the listing template is rendered from a
the_contentfilter, so everything it prints (fields, forms, output from extensions) is processed by the filters that run afterwards as if it were post content. This renders the template apart from them.job_content()holds the rendered template and returns a placeholder named with a random token. A new filter at the last priority puts the template in its place. Filters in between see only the placeholder; content they add before or after it is kept.wpjm_the_job_description()after the HTML filter (the same order as before), or bythe_job_descriptionwhen a template calls that filter itself, as older templates did.wp_filter_content_tags()are applied to the rendered template directly, since the later filters no longer reach it.job_manager_isolate_single_job_content(defaulttrue): returnfalseto pass the template through the content filters as before.docs/adr/0002-…records the decision and its consequences.Behaviour changes to be aware of
single_job_listing_end. Such code can calldo_shortcode()on its own text, or the site can use the filter above.job_content()now returns the placeholder when called directly.Testing Instructions
vendor/bin/phpunit --filter 'Tests_Single_Listing_Content_Processing|Tests_Listing_Field_Display'add_filter( 'job_manager_isolate_single_job_content', '__return_false' );and confirm the previous behaviour.Release Notes
job_manager_isolate_single_job_contentfilter to restore the previous behaviour.