WordPress.org

Forums

new add_filter in WP 2.2 breaks some plugins (8 posts)

  1. weyhan
    Member
    Posted 8 years ago #

    Hi,

    I’ve think in WP 2.2 onwards, the add_filter have change in a way that breaks some plugins that is a class and the filter function the plugins ads are member functions. Particularly I’m talking about the post-teaser plugin. Please correct me if I’m wrong because I’m not too familiar with PHP.

    In the post-teaser plugin, the call to add_filter to the_content hook is:

    add_filter('the_content', array(&$post_teaser, 'process'));

    So in the add_filter function in plugin.php the line that adds the filter function is:

    wp_filter[$tag][$priority][serialize($function_to_add)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);

    The part I thinks breaks the plugin I think is: [serialize($function_to_add)]

    The serialize array of a class and a member function used as the index to wp_filter seem wrong to me. So I’ve tried to capture the value of the serialize of the function_to_add and it is:

    a:2:{i:0;O:11:"post_teaser":16:{s:7:"version";d:3.79999999999999982236431605997495353221893310546875;s:5:"debug";b:0;s:15:"default_options";a:8:{s:13:"full_template";s:166:"Permanent link to this post (%word_image_count%, estimated %reading_time% reading time)";s:15:"teaser_template";s:194:"This is a preview of <q>%title%</q>. Read the full post (%word_image_count%, estimated %reading_time% reading time)";s:6:"target";s:3:"100";s:9:"word_mins";s:5:" mins";s:9:"word_secs";s:5:" secs";s:14:"time_separator";s:1:":";s:11:"zero_counts";s:1:"0";s:15:"count_separator";s:2:", ";}s:14:"static_options";a:1:{s:6:"blocks";s:30:"p|li|dt|dd|address|form|pre|tr";}s:13:"debug_message";N;s:13:"full_template";s:166:"Permanent link to this post (%word_image_count%, estimated %reading_time% reading time)";s:15:"teaser_template";s:194:"This is a preview of <q>%title%</q>. Read the full post (%word_image_count%, estimated %reading_time% reading time)";s:6:"target";s:3:"100";s:9:"word_mins";s:5:" mins";s:9:"word_secs";s:5:" secs";s:14:"time_separator";s:1:":";s:11:"zero_counts";s:1:"0";s:15:"count_separator";s:2:", ";s:6:"blocks";s:30:"p|li|dt|dd|address|form|pre|tr";s:18:"doing_reading_time";b:1;s:12:"doing_counts";b:1;}i:1;s:7:"process";}

    I think this is a bug and it stops plugins authors from using OOP to develop their plugins. I would raise a bug if there is a bug system for the public but I couldn’t find one (I assume it is a developers only thing?)

    So, the next best thing is to ask for help as to how I would be able to change the call to add_filter so to fix the post-teaser plugin. It seems that the original author has stop developing for it. Does anyone know how that can be done?

  2. jasonmez
    Member
    Posted 8 years ago #

    As for raising a bug: There's a ticket system on trac.wordpress.org but see this page in the codex for all the info on reporting bugs.

    Perhaps someone else can be more helpful with the add_filter question. Sorry I can't be of more help.

    Jason

  3. weyhan
    Member
    Posted 8 years ago #

    Thanks Jasonmez,

    I've manage to find the ticket for the add_filter change. I've requested for the developer to have a look at it.

  4. daviding
    Member
    Posted 8 years ago #

    Weyhan,

    I really miss Post-Teaser working on WordPress 2.2.1 . (On of my readers had told me that he really likes it. It could be because some of my posts are longer than the layman actually would like to read!)

    Could you post the ticket the you're following, so that others could follow along as well. I'm not a PHP coder, so it's not clear to me whether we should expect Post-Teaser to be changed, or whether the WordPress releases will fix everything.

    In the meantime, I've been using Evermore.

  5. weyhan
    Member
    Posted 8 years ago #

    daviding,

    Sure thing and here it is

  6. Jacob Santos
    Member
    Posted 8 years ago #

    I would have to say that a __sleep() method should be added to those classes to function with serialize. Another option would be to create a function which calls the class.

    The issue is that you are serializing an array, an object, and an string. It doesn't seem to me that it is needed. The purpose is so that it is unique. However, with the object, the properties can change and will change at some point. Therefore, you might end up adding two or more at some later point depending on how it is set up and if you change any properties before hooking to another action or filter.

    First Option use __sleep() method:

    function __sleep()
    {
    return get_class($this);
    }

    What this does, is return the name of the class, which will be serialized. It won't interfere with the rest of the code, because it is not being serialized.
    $wp_filter[$tag][$priority][serialize($function_to_add)]['function' ] is called with call_user_func(), it will work like it would normally.

    Second Option Use static version:

    array('class_name', 'method_name')

    The only downside is that the method can not use $this, because it will be called statically. So instead of using $this, use self::.

    I should make the assumption that you are using PHP 4, since you are referencing the object and that isn't needed in PHP 5.

  7. weyhan
    Member
    Posted 8 years ago #

    Hi santosj,

    I understand what you are saying but the code I've included in my initial post is not mine. The code belongs to WordPress in plugin.php.

    The only code I've included that belongs to Post Teaser is:

    add_filter('the_content', array(&$post_teaser, 'process'));

    Hope that clears up the confusion.

    Anyway, I've decided and have made changes to Post Teaser to work with WP 2.2.x. What I did was to add a wrapper function that would call the member function of the post-teaser class. The change seems to work fine in WP 2.2.x and have since been released. However, I'm not sure that is the best thing to do.

  8. Jacob Santos
    Member
    Posted 7 years ago #

    I know. I've fixed the problem and applied a patch to the Trac ticket, but it hasn't been approved yet. You can apply the patch manually and it fixes the issue in add_filter and remove_filter.

    The only issue is that my solution is slower. Whereas serialize is log(O) or consist, mine isn't. You have to decide whether you want something that is fast or something that works. if you (or whoever) used functions or a registry model for variables, it might be easier.

    From my testing, there are a lot of bugs (and/or inconsistencies) in plugin.php. That it works, means it was tested with functions with objects only as an afterthought.

Topic Closed

This topic has been closed to new replies.

About this Topic