-
Notifications
You must be signed in to change notification settings - Fork 24
E2E: Add field text testing. #103
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
Conversation
$field_object = get_field_object( 'movie_title' ); | ||
|
||
// Only proceed if the field exists and is a valid type. | ||
if ( $field_object && isset( $field_object['type'] ) && 'text' === $field_object['type'] ) { | ||
$field = get_field( 'movie_title' ); | ||
|
||
// Ensure we have a string value and sanitize it. | ||
$field = is_string( $field ) ? $field : ''; | ||
|
||
// Sanitize the field value using WordPress sanitization functions. | ||
$field = sanitize_text_field( $field ); | ||
|
||
// Escape the output for HTML context. | ||
$escaped_field = esc_html( $field ); | ||
|
||
// Use wp_kses_post to allow safe HTML if needed, but escape by default. | ||
$output = wp_kses_post( '<br><p id="scf-test-movie-title">Movie title: ' . $escaped_field . '</p>' ); |
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.
@pkevan Should SCF escape automatically within the get_field function?
Docs says that you need to do this $escaped_wysiwyg = get_field('wysiwyg', false, true, true);
, but also recommends using wp_kses_post
.
Am I being redundant here? I have that feeling.
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.
wp_kses_post
filters out unallowed tags and attributes. In effect, if you would not escape the field and the HTML tags are allowed they would get printed inside the paragraph. So it sounds like a decision of whether you allow any HTML for the field here.
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.
@pkevan Should SCF escape automatically within the get_field function?
Generally this isn't expected, and would more than likely get flagged when using phpcs anyway.
Escaping should be in the realm of whatever is outputting the data, so in this case it's not needed, but generally any outputting functions within the plugin which requires no user input should be escaping it after using get_field
i.e. the shortcodes.
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.
This one does not require user input cause the input will be only filled in an automated test. So I guess we are fine with removing it then.
Let's add a cleaning step before every test to avoid the errors we have right now. |
What
Includes a test and the boilerplate for a text field creation, cleaning, and frontend rendering check.