2010-09-17 56 views
2

我試圖修改wordpress插件來自定義類別,所以當調用random_post_link時,我可以使用random_post_link添加自定義類別('Random Link',3 。)3爲類別名稱array(__ CLASS__,如何在wordpress中工作?

  1. 如何插件下面創建類random_Post_Link的新對象我想你做的東西像創建新的對象:?

    $ A =新random_post_link;

但是我沒有在插件中看到。我認爲它使用鉤子在init函數中創建新對象:

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

如果是這樣的話,我該如何給跳轉函數添加參數?

我想我知道ADD_ACTION是如何工作的,第二個參數應該是函數名,如何 「陣列(CLASS,‘跳’)」工作?

下面是插件的完整代碼:

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

如果WP的混亂中有任何類,我會非常驚訝。當你寫'CLASS'時,你的意思是'CLASS'還是你的意思是'__CLASS__'。這是誤導給你的問題標題 – 2010-09-17 08:24:29

+0

這是從插件源直接複製,所以是的,__CLASS__(帶下劃線) – pood 2010-09-24 00:19:11

+1

下劃線傾向於大膽的文字在這裏,所以如果你想寫他們包圍的類,嘗試使用一個「反引號」的引用,將其標記爲代碼:'__CLASS__'' – WraithKenny 2015-11-30 19:12:15

回答

2

一些WordPress的作者在PHP中使用類結構基本上減少全局變量。這個類被認爲是一種「單例」,所以它通常不會被實例化(代碼在底部調用Random_Post_Link::init();)。例如,類函數被視爲類成員,而不是實例成員,有點像其他語言中的Math.max()。

__CLASS__ PHP關鍵字是當前類的標記,就這麼過去了的時候,可調用變得Random_Post_Link::method()array('Random_Post_Link', 'method')

如果你需要解開,嘗試remove_action('init', array('Random_Post_Link, 'jump'));

(還要注意,使用的方法這種方式應該宣佈爲static function jump() {...}

PS爲了進一步澄清:

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

array('class', 'function')語法是一個PHP的事情,而WordPress的行動期待callable它可以是任何的PHP調用的東西。