Skip to content

AlternativeFunctions: suggest using wp_(un)slash()#1466

Open
jrfnl wants to merge 1 commit into
developfrom
feature/alternativefunctions-slash-functions
Open

AlternativeFunctions: suggest using wp_(un)slash()#1466
jrfnl wants to merge 1 commit into
developfrom
feature/alternativefunctions-slash-functions

Conversation

@jrfnl

@jrfnl jrfnl commented Aug 27, 2018

Copy link
Copy Markdown
Member

The WP wp_slash() and wp_unslash() functions are capable of (un)slashing both strings, as well as (nested) arrays, while the PHP native functions can only handle strings.

For input variables, you typically encounter arrays in forms. For instance when a checkbox or dropdown field can accept multiple values. however, the usage of arrays in input variables is definitely not limited to those examples.

With that in mind, I'd like to suggest adding a recommendation to use these functions instead of the PHP native ones to the AlternativeFunctions sniff.

Refs:

The WP `wp_slash()` and `wp_unslash()` functions are capable of (un)slashing both strings, as well as (nested) arrays, while the PHP native functions can only handle strings.

For input variables, you typically encounter arrays in forms. For instance when a checkbox or dropdown field can accept multiple values. however, the usage of arrays in input variables is definitely not limited to those examples.

With that in mind, I'd like to suggest adding a recommendation to use these functions instead of the PHP native ones to the `AlternativeFunctions` sniff.
@jrfnl

jrfnl commented Sep 3, 2018

Copy link
Copy Markdown
Member Author

@WordPress-Coding-Standards/wpcs-admins Anyone has an opinion on this proposal ?

@westonruter

Copy link
Copy Markdown
Member

I agree. I think the original purpose being wp_unslash() and wp_slash() was that in the eventual case that magic quotes could be turned off, that these functions could then no-op. I doubt that will ever happen, but if it does, using these functions will ensure that things don't break.

@JDGrimes

JDGrimes commented Sep 7, 2018

Copy link
Copy Markdown
Contributor

The other side of what @westonruter pointed out is that these functions should really only be used in the context of dealing with magic quotes. Unrelated slashing and unslashing operations shouldn't use them.

@jrfnl

jrfnl commented Sep 7, 2018

Copy link
Copy Markdown
Member Author

Unrelated slashing and unslashing operations shouldn't use them.

Well, for slashing/unslashing related to database operations, the functions shouldn't be used either as prepared SQL should be used instead.

Let's gather some examples of situations in which it would be preferred to use the PHP native functions over the WP versions. Depending on the examples, either the sniff could try to account for the context in which the functions are used or this PR will be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants