Nasıl array (__CLASS__, iş wordpress yapar?

0 Cevap php

Ben özel kategoriler almaya eklentisi wordpress mod çalışıyorum. Yani random_post_link çağrıldığında, ben random_post_link kullanarak özel kategori eklemek ('Rasgele Link', 3). Olabilir 3 kategori adı olarak.

  1. Nasıl aşağıda eklenti sınıf Random_Post_Link yeni bir nesne oluşturur? Senin gibi bir şey yaparak yeni nesneler oluşturulan düşündüm:

    $ A = new random_post_link;

Ama eklentisi görmüyorum. Ben bir kanca kullanarak init fonksiyonu yeni bir nesne oluşturur düşünüyorum:

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

Bu durum buysa, nasıl işlev atlamak için parametre ekleyebilirsiniz?

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

Burada eklenti için tam kod:

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();

0 Cevap