Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does array(__CLASS__, work in wordpress?

Tags:

php

wordpress

I'm trying to mod a wordpress plugin to take custom categories. So when the random_post_link is called, I can add the custom category by using random_post_link('Random Link',3). 3 being the category name.

  1. How does the plugin below create a new object of class Random_Post_Link? I thought you created new objects by doing something like:

    $a = new random_post_link;

But I don't see that in the plugin. I think it creates the new object in the init function by using a hook:

add_action('init', array(CLASS, 'jump'));

If that's the case, how can I add parameter to jump function?

I think I know how add_action works, the second parameter should be the function name, how does " array(CLASS, 'jump')" work?

Here's the full code for the plugin:

function random_post_link($text = 'Random Post',$the_cat = 36) {
    printf('<a href="%s">%s</a>', get_random_post_url(), $text);
    $the_category = $the_cat;
}

function get_random_post_url() {
    return trailingslashit(get_bloginfo('url')) . '?' . Random_Post_Link::query_var;
}


class Random_Post_Link {
    const query_var = 'random';
    const name = 'wp_random_posts';
    public $the_category;

    function init() {
        add_action('init', array(__CLASS__, 'jump'));

        // Fire just after post selection
        add_action('wp', array(__CLASS__, 'manage_cookie'));
    }

    // Jump to a random post
    function jump() {
        if ( ! isset($_GET[self::query_var]) )
            return;

        $args = apply_filters('random_post_args', array(
            'post__not_in' => self::read_cookie(),
        ));

        $args = array_merge($args, array(
            'orderby' => 'rand',
            'cat' => $the_category,
            'showposts' => 1,
        ));

        $posts = get_posts($args);

        if ( empty($posts) ) {
            self::update_cookie(array());
            unset($args['post__not_in']);

            $posts = get_posts($args);
        }

        if ( empty($posts) )
            wp_redirect(get_bloginfo('url'));

        $id = $posts[0]->ID;

        wp_redirect(get_permalink($id));
        die;
    }

    // Collect post ids that the user has already seen
    function manage_cookie() {
        if ( ! is_single() )
            return;

        $ids = self::read_cookie();
        $id = $GLOBALS['posts'][0]->ID;

        if ( count($ids) > 200 )
            $ids = array($id);
        elseif ( ! in_array($id, $ids) )
            $ids[] = $id;

        self::update_cookie($ids);
    }

    private function read_cookie() {
        return explode(' ', @$_COOKIE[self::name]);
    }

    private function update_cookie($ids) {
        setcookie(self::name, trim(implode(' ', $ids)), 0, '/');
    }
}

Random_Post_Link::init();
like image 321
pood Avatar asked May 08 '26 03:05

pood


1 Answers

Some WordPress authors use the Class structure in PHP to basically reduce global vars. The class is meant as a 'singleton' of sorts, and so it isn't usually instantiated (that code calls Random_Post_Link::init(); at the bottom instead). The class functions are treated as Class members, not instance members, sort of like Math.max() in other languages, for example.

The __CLASS__ php keyword is simply a token for the current class, so when passed, the callable becomes Random_Post_Link::method() or array( 'Random_Post_Link', 'method' )

If you need to unhook, try remove_action( 'init', array( 'Random_Post_Link, 'jump' ) );

(Also note, that methods used that way should be declared static function jump() {...})

P.S. To further clarify:

http://php.net/manual/en/language.types.callable.php

The array('class', 'function') syntax is a PHP thing, while WordPress actions expect a callable which could be any of the PHP callable things.

like image 115
WraithKenny Avatar answered May 10 '26 19:05

WraithKenny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!