WordPress.org

Ready to get started?Download WordPress

Forums

Widget Logic
[resolved] Hook to override eval and to use custom function for logic validation (19 posts)

  1. Mohan Chevuri
    Member
    Posted 2 years ago #

    Is it possible to add a hook to override the default "eval" and use a custom function that validates the logic?

    Basically we want to use Drupal blocks URL pattern way of handling the logic, and don't use eval.

    We can manually go into the code and change "eval" to our function, but as always there is a cleaner WP way of doing it. :)

    Thanks!

    http://wordpress.org/extend/plugins/widget-logic/

  2. Mohan Chevuri
    Member
    Posted 2 years ago #

    Something like this...

    if(!(function_exists("widget_logic_eval_override")?widget_logic_eval_override($wl_value):eval($wl_value)))

  3. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    I could put in an action that passes through the widget logic, and then does the eval if the result is true/false - I'll think about which makes more sense.

    can i ask why you wouldn't simply put your function in the widget logic like this:

    widget_logic_eval_override('some value for my function')

    which would have the effect of calling your function

  4. Mohan Chevuri
    Member
    Posted 2 years ago #

    Thanks for the reply. Yeah I can modify widget logic plugin code a tiny bit, but thought it will be better if I don't modify the code and use some kind of hook (or action as you said) to call in my custom plugin. That way I can do future updates without going and updating widget logic code everytime.

  5. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    what I was getting at is you could put in your logic in the admin interface as the above without altering the WL plugin code, and that would have the same effect. Obviously you'd have to do it for each widget but it looks like you'd be putting something in the WL code anyway? (for your function to use as a param)

  6. Mohan Chevuri
    Member
    Posted 2 years ago #

    That is the way we are using widget logic with our function, now in testing. But we decided not to use eval in production, as this WPMU installation will be used by about 500 people and might become a security issue.

  7. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    ah fair point :-)

    I will try adding something to the DEV version soon for you to try - but apologies in advance as I have been promising to play with the code for weeks (to fix some bad coding and a couple of other small requests). However I'll take a quick look at the code and write some basic amendment including a do_action for you to look at here. Just got to go light a barbecue now ;-)

  8. Mohan Chevuri
    Member
    Posted 2 years ago #

    Thanks much!

    You already seem to be in the party mode. Have fun!

  9. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    How about a filter instead of an action and I change the crucial code in WL plugin to

    foreach($widget_list as $pos => $widget_id)
    {	if (empty($wl_options[$widget_id]))  continue;
    	$wl_value=trim($wl_options[$widget_id]);
    	if (empty($wl_value))  continue;
    
    	$wl_value=apply_filters( "widget_logic_eval_override", $wl_value )
    	if ($wl_value===false)
    	{	unset($sidebars_widgets[$widget_area][$pos]);
    		continue;
    	}
    	if ($wl_value===true) continue;
    
    	if (stristr($wl_value,"return")===false)
    		$wl_value="return (" . $wl_value . ");";
    
    	if (!eval($wl_value))
    		unset($sidebars_widgets[$widget_area][$pos]);
    }

    so the filter gives you the chance to step in and return a boolean value directly?

  10. Mohan Chevuri
    Member
    Posted 2 years ago #

    Awesome! I will try it out when I get back home.

  11. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    hang on, I'll try adding that now to see if it's harmless, and then add it to the DEV codeā€¦

  12. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    looks ok IF you add a ';' at the end of the apply_filters line

    right now I can't update the SVN to add those lines to the DEV version. i've just discovered that Mountain Lion doesn't have SVN by default, and the main way to install it is to download the 1.5GB Xcode install :-/

  13. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    that's live in the dev version now

    http://wordpress.org/extend/plugins/widget-logic/developers/

    filter: widget_logic_eval_override

    any function hooked into this filter is passed the widget logic text. you can return the text altered in some way before it is EVAL'd, or you can bypass any EVAL'ing happening by returning with a boolean TRUE (show widget) or FALSE (don't show)

  14. Mohan Chevuri
    Member
    Posted 2 years ago #

    Thanks! I downloaded it from dev. I'm writing a small plugin to use the filter. I will let you know how it works.

  15. Mohan Chevuri
    Member
    Posted 2 years ago #

    It works. Could you also add a filter or something for the "WL field text", which says "Widget Logic". Then I will be able to add a link/popup to display some help.

    OR

    You can add a tiny help link which by default points to "http://wordpress.org/extend/plugins/widget-logic/other_notes/". Then if you provide a way to change the link, I will change it to point to this plugin help page.

    Whichever you think will be more appropriate.

  16. alanft
    Member
    Plugin Author

    Posted 1 year ago #

    (been off the grid for a couple of weeks, will get back and look at this soon)

  17. Mohan Chevuri
    Member
    Posted 1 year ago #

    Thanks! Appreciate your help!

  18. Mohan Chevuri
    Member
    Posted 1 year ago #

    FYI

    Notice: Undefined index: widget_logic-options-filter in wp-content/plugins/widget-logic/widget_logic.php on line 171

    Same notice on lines 176, 184

  19. Mohan Chevuri
    Member
    Posted 1 year ago #

    The new plugin based on this filter is on the plugin repository.

    http://wordpress.org/extend/plugins/widget-logic-by-path/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.