在wp_INSERT_COMMENT上使用静态回调

时间:2020-07-17 作者:marccaps

我试图在wp\\u insert\\u注释上添加一个函数。但是,当我试图在WP管理面板的帖子上添加评论时,我得到了一个内部服务器错误。日志上没有生成错误。

这是我的代码:

<?php
if (!defined( \'CANVAS_DIR\' )) {
    die();
}

class CanvasLd {

    static public function init() {
        add_action( \'learndash_assignment_approved\', array( \'CanvasLd\', \'learndash_assignment_approved\' ) );
        add_action( \'wp_insert_comment\', array( \'CanvasLd\', \'learndash_new_assignment_comment\' ) );
    }

    /**
    * Send approved assignments using push notifications
    *
    * @param int $assignment_id The assignment post ID
    */
    static public function learndash_assignment_approved( $assignment_id ) {

        if (!self::option_on( \'ld_approved_assignments\' )) {
            return;
        }

        $push_api = self::get_push_api();

        $push_api->save_ld_log( \'assignment-approved\', $assignment_id );

        $title = \'Your assignment has been approved.\';
        $text = html_entity_decode(get_the_title( $assignment_id )) . \' has been approved.\';
        $url = get_post_permalink( $assignment_id );

        $users_list = array( get_post_field( \'post_author\', $assignment_id ) );

        /**
        * Allow to customize title of push notification.
        *
        * @since 3.2
        *
        * @param string $title Title.
        * @param int[] $users_list Array with single user ID.
        * @param string $url URL.
        * @param string $text Text.
        */
        $title = apply_filters( \'canvas_push_ld_assignment_approved_title\', $title, $users_list, $url, $text );

        /**
        * Allow to customize text of push notification.
        *
        * @since 3.2
        *
        * @param string $text Text.
        * @param int[] $users_list Array with single user ID.
        * @param string $url URL.
        * @param string $title Title.
        */
        $text = apply_filters( \'canvas_push_ld_assignment_approved_msg\', $text, $users_list, $url, $title );

        /**
        * Allow to customize url of push notification.
        *
        * @since 3.2
        *
        * @param string $url URL.
        * @param int[] $users_list Array with single user ID.
        * @param string $title Title.
        * @param string $text Text.
        */
        $url = apply_filters( \'canvas_push_ld_assignment_approved_url\', $url, $users_list, $title, $text );

        $push_api->send_to_users( $title, $text, $users_list, $url);
    }

    /**
    * Send new assignment comment using push notifications
    *
    * @param int        $comment_id       The comment ID.
    * @param WP_Comment $comment_approved Comment object.
    */
    static public function learndash_new_assignment_comment( $comment_id, $comment ) {

        if (get_post_type($comment->comment_post_ID) !== \'sfwd-assignment\') {
            return;
        }

        $push_api = self::get_push_api();

        $push_api->save_ld_log(\'new-assignment-comment\', $comment->comment_post_ID);

        $title = \'Your assignment has a new comment.\';
        $text = html_entity_decode(get_the_title( $comment->comment_post_ID )) . \' has a new comment.\';
        $url = get_post_permalink( $comment->comment_post_ID );

        $users_list = array( get_post_field( \'post_author\', $comment->comment_post_ID ) );

        /**
        * Allow to customize title of push notification.
        *
        * @since 3.2
        *
        * @param string $title Title.
        * @param int[] $users_list Array with single user ID.
        * @param string $url URL.
        * @param string $text Text.
        */
        $title = apply_filters( \'canvas_push_ld_comment_title\', $title, $users_list, $url, $text );

        /**
        * Allow to customize text of push notification.
        *
        * @since 3.2
        *
        * @param string $text Text.
        * @param int[] $users_list Array with single user ID.
        * @param string $url URL.
        * @param string $title Title.
        */
        $text = apply_filters( \'canvas_push_ld_comment_msg\', $text, $users_list, $url, $title );

        /**
        * Allow to customize url of push notification.
        *
        * @since 3.2
        *
        * @param string $url URL.
        * @param int[] $users_list Array with single user ID.
        * @param string $title Title.
        * @param string $text Text.
        */
        $url = apply_filters( \'canvas_push_ld_comment_url\', $url, $users_list, $title, $text );

        $push_api->send_to_users( $title, $text, $users_list, $url);
    }

    static private function option_on($name) {
        return Canvas::get_option($name);
    }

    /**
    * Return CanvasNotifications instance
    *
    * @return CanvasNotifications
    */
    static function get_push_api() {
        if (!class_exists( \'CanvasNotifications\' )) {
            require_once(dirname(__FILE__) . \'/push/canvas-notifications.class.php\' );
        }
        return CanvasNotifications::get();
    }
}

if (Canvas::get_option( \'ld_approved_assignments\' ) || Canvas::get_option( \'ld_new_assignment_comment\' )) {
    add_action( \'wp_loaded\',  array( \'CanvasLd\', \'init\' ) );
}

1 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

添加挂钩时add_action(), 第4个参数是回调函数接受的参数数:

$accepted_args
(int)(可选)函数接受的参数数
默认值:1

您的回调函数,CanvasLd::learndash_new_assignment_comment, 接受两种:

static public function learndash_new_assignment_comment( $comment_id, $comment ) {
由于传递给回调的默认参数数为1,$comment 不会发送到回调函数。这会导致一个致命错误,因为函数需要两个写入的参数。

因此,请确保使用add_action():

add_action( \'wp_insert_comment\', array( \'CanvasLd\', \'learndash_new_assignment_comment\' ), 10, 2 );