我们是否可以将类的静态方法挂钩到该类内部的Add_action?

时间:2016-12-06 作者:GreyWolfram

所以基本上,我要做的是将一个类的静态方法挂接到同一个类的另一个静态方法上。

代码在此处:

class LocatorMap {
    
    public static function init() {

        add_action( \'wp_enqueue_scripts\', array( __CLASS__, \'register_scripts\' ) );

    }

    /* add_action( \'wp_enqueue_script\', array( \'LocatorMap\', \'register_scripts\' ) ); */
    public function register_scripts() {

        global $post;

        /* http or https */
        $scheme = parse_url( get_bloginfo(\'url\'), PHP_URL_SCHEME );

        /* register gmaps api and info box */
        wp_register_script( \'google-maps-api\', $scheme . \'://maps.googleapis.com/maps/api/js\', array(\'jquery\'), FALSE, true );
        wp_register_script( \'google-maps-info-box\', $scheme . \'://cdn.rawgit.com/googlemaps/v3-utility-library/infobox/1.1.13/src/infobox.js\', array( \'jquery\', \'google-maps-api\' ), \'1.1.13\', true ); 

    }
}
这可能吗?我不知道,因为我对这种结构有点陌生。

UPDATE我还在外部文件上调用这个类。

define( DEALERSHIP_MAP_URL, untrailingslashit( plugin_dir_url( __FILE__ ) )  );
define( DEALERSHIP_MAP_DIR, untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( DEALERSHIP_MAP_TEMPLATE, DEALERSHIP_MAP_DIR . \'/templates\' );

require_once( \'core/class-locator-map.php\' );

register_activation_hook( __FILE__, array( \'LocatorMap\', \'init\' ) );

3 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

register_activation_hook 仅运行一次,即插件首次激活时-使用init 挂接以“启动”插件:

add_action( \'init\', \'LocatorMap::init\' );

SO网友:jonathan

使用该功能get_called_class()

public static function init() {
    add_action( \'wp_enqueue_scripts\', array( get_called_class(), \'register_scripts\' ) );
}

SO网友:Andrew Odri

I recently had to do the same thing and ended up using the static:: late static binding along with special ::class constant. The static:: binding will reference the calling class, while the ::class constant will return a string that is the name of the calling class (with namespacing!)

Using the example from the questiom, the implementation would like something like this:

class LocatorMap {
    
    public static function init() {
        add_action( \'wp_enqueue_scripts\', array( static::class, \'registerScripts\' ) );
    }

    public function registerScripts() {}
}

Since static:: references the calling class, this allows to write reusable classes for your plugins/themes, and kind of wrap up some of WordPress\' legacy ugliness.

For example, you could write an abstract class for custom post types like this:


namespace WordpressPlugin;

abstract class AbstractPostType {
  const SLUG;

  private function __construct() {}

  public static function initialize() {
    add_action( \'init\', array( static::class, \'registerPostType\' ), 0 );
    add_action( \'init\', array( static::class, \'registerTaxonomies\' ), 1 );
    add_action( \'add_meta_boxes\', array( static::class, \'addMetaBox\' ) );
  }

  public static function registerPostType() {}

  public static function registerTaxonomies() {}

  public static function addMetaBox() {}
}

Aftet that, you can now create custom post types without having to duplicate all of the boilerplate in each subclass, like this:

namespace WordpressPlugin;

class ExamplePostType extends AbstractPostType {
  const SLUG = \'example-post-type\';

  public static function registerPostType() {
    register_post_type( self::SLUG, array(
      ...
    ) );
  }
}

相关推荐

Can I make plugins required?

我正在开发自己的Wordpress主题,将用于多个客户端。它有一个主题选项页面,这样我每次都可以轻松自定义网站,但我也会在我制作的每个网站上使用一些插件(如SEO插件、安全性等)。有没有办法让它们成为“必需的”,这样我就可以得到这些插件的列表,这样当我在新网站上安装主题时就不必在插件目录中找到它们了?