wordpress内核揭秘之day5_apply_filters和apply_actions

稍微先说些体外话吧

很多人都会说yii框架很屌,thinkphp也很多人用,国内的phpcms,织梦简直逆天

wordpress框架太烂了

看看

“apply_filters”和"actions"就知道了,功能弱爆了

那么我倒想问问大家

“apply_filters”和"add_filter"有什么区别

这时候,很多人就会马上取查api,(这我多次提到,最low b的人才会去查api)

apply_filters比add_filter多了1个s,没看到么?

我不是在说笑

apply_filters 多了一个s的意思就是filter可能有n个,大部分情况不会是一个

如果apply_filters只有一个,那么此模组方法的名字就会是apply_filter而不是apply_filters

人家wordpress的光是命名已经是标准得不能再标准

明白我的题外话了么,1个屋子有多屌,并不是你以为多屌就能多屌,并不是意识形态决定的,人家如果门面都刷得很漂亮,那么屋子里面也一般比较屌


说远了,我们再来分析下apply_filters和apply_actions是怎么实现的

方法

public function apply_filters( $value, $args ) {

在类wp-includes/class-wp-hook.php

(ctm,又真的没有什么秘密,要不要我们做老师的活啊,人家就明显告诉你这是一个钩子)

private $iterations = array();
public function apply_filters( $value, $args ) {
		if ( ! $this->callbacks ) {
			return $value;
		}

		$nesting_level = $this->nesting_level++;

		$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
		$num_args = count( $args );//下面省略
}
代码再次告诉你。。。。。。array是php里面的神器,和javascript的prototype一样,所有api文档都不会告诉你一个事实,array就是神器,要好好学

(还在研究hashtable,map,有什么区别的同学,还在想用堆栈还是链表的同学,事实就是什么才是有用的,看出来了么?)

2.方法apply_filters里面有一些对数组统计,排优先顺序的操作。。。。。也是php里面很经典的array操作

$nesting_level = $this->nesting_level++;

		$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
		$num_args = count( $args );


3.apply_actions就不用看了吧,和filter会有差别么?


(事实是,并没有apply_actions的方法,没必要copy & paste)

/**
	 * Executes the callback functions hooked on a specific action hook.
	 *
	 * @since 4.7.0
	 * @access public
	 *
	 * @param mixed $args Arguments to pass to the hook callbacks.
	 */
	public function do_action( $args ) {
		$this->doing_action = true;
		$this->apply_filters( '', $args );

		// If there are recursive calls to the current action, we haven't finished it until we get to the last one.
		if ( ! $this->nesting_level ) {
			$this->doing_action = false;
		}
	}

我也不懂英文,所以姑且这么理解,1个action = filters的集合

所以do_action就是filters的封装而已,这不就是传说中的“重用”么。。。不一定要面向对象的。。。而且是很直观的。。。懂吗


4.wordpress框架是什么时候,在哪里引入hook的?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

avi9111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值