Sridhar Katakam
Sridhar Katakam

Reputation: 1226

Replace variable with its string first and then do the `if` check

I am working on a custom WordPress plugin that has

// Callback for setting the if condition where theme should be used instead of Oxygen.
function ote_on_these_views() {

    $oxygen_theme_enabler_options = get_option( 'oxygen_theme_enabler_option_name' ); // Array of All Options

    $myifcondition = $oxygen_theme_enabler_options['enter_your_if_condition_1'];

    if ( is_page( 'contact' ) ) {
        return true;
    } else {
        return false;
    }
}

With this in place, everything works fine. I am calling ote_on_these_views() a couple of times in rest of the code.

Now when I replace

if ( is_page( 'contact' ) ) {

with

if ( $myifcondition ) {

it does not work because the if condition is checking to see if the variable is present (and it is) and hence ote_on_these_views() is always returning true.

The current value of $myifcondition is is_page( 'contact' ) and is coming from the plugin's settings page.

enter image description here

So.. is there a way to replace the variable with its value first and then have the if statement check for it or any other workarounds?

Upvotes: 0

Views: 433

Answers (3)

cameronjonesweb
cameronjonesweb

Reputation: 2526

You'd be better off using filters. If your users are developer-y enough to be declaring conditions such as is_page(), then they should be able to add a filter easy enough.

function ote_on_these_views() {
    if ( ote_on_these_views_condition() ) {
        return true;
    } else {
        return false;
    }
}

function ote_on_these_views_condition() {
    // Set our default condition value
    $oxygen_theme_enabler_options = get_option( 'oxygen_theme_enabler_option_name' ); // Array of All Options
    $myifcondition = boolval( $oxygen_theme_enabler_options['enter_your_if_condition_1'] );
    // Add a filter to allow users to override the condition
    return apply_filters( 'ote_on_these_views_condition', $myifcondition );
}

Now your users can easily change the result of the condition

add_filter( 'ote_on_these_views_condition', 'filter_ote_on_these_views_condition' );

function filter_ote_on_these_views_condition( $myifcondition ) {
    if ( is_page( 'contact' ) ) {
        $myifcondition = true;
    } else {
        $myifcondition = false;
    }
    return $myifcondition;
}

Upvotes: 1

Nikita Leshchev
Nikita Leshchev

Reputation: 1844

You have to use eval function, but it's not a good practice to use it. Also you have to add return and ; to inputed string.

if (eval('return ' . $myifcondition . ';')) {

}

As you said, you want allow only particular functions, so you can validate your expression with following regex:

preg_match_all("/\(?(\w+)?\(.*?\)/", $input_lines, $output_array);

I'm not very good at regexps so I'm sure that it's possible to make this function much better.

For following expression:

is_page() && (some_function('arg') || what_else())

You'll get the following $output_array:

array(2
    0   =>  array(3
        0   =>  is_page()
        1   =>  (some_function('arg')
        2   =>  what_else()
    )
    1   =>  array(3
        0   =>  is_page
        1   =>  some_function
        2   =>  what_else
    )
)

Now you can take every used function name from $output_array[1] one by one and check if user can call this function:

if (in_array($used_function, $allowed_functions_array))

Upvotes: 0

Shubham
Shubham

Reputation: 1288

if ( $myifcondition ) {

This should be

if ( call_user_func_array($myifcondition, array('contact')) ) {

where

$myifcondition = 'is_page'

You can split user entered text via

$condition_name=explode('(',$myifcondition,2) // 2 is max number of split parts
$params=rtrim(trim($condition_name[1]),')') //trim is used to remove any trailing spaces

then use

call_user_func_array($condition_name[0], array($params))

Upvotes: 0

Related Questions