最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

comments - Using a static callback on wp_insert_comment

programmeradmin0浏览0评论

I'm trying to add a function on the wp_insert_comment hook. But when I try to add the comment on the post on the WP admin panel, I'm getting an internal server error. There are no errors being generated on the logs.

Here's my code:

<?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' ) );
}

I'm trying to add a function on the wp_insert_comment hook. But when I try to add the comment on the post on the WP admin panel, I'm getting an internal server error. There are no errors being generated on the logs.

Here's my code:

<?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' ) );
}
Share Improve this question asked Jul 17, 2020 at 8:19 marccapsmarccaps 592 silver badges8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

When adding a hook with add_action(), the 4th argument is the number of arguments accepted by the callback function:

$accepted_args
(int) (Optional) The number of arguments the function accepts.
Default value: 1

Your callback function, CanvasLd::learndash_new_assignment_comment, accepts two:

static public function learndash_new_assignment_comment( $comment_id, $comment ) {

Since the default number of arguments passed to the callback is 1, $comment is not sent to the callback function. This causes a fatal error because the function requires 2 arguments as written.

So make sure to specify the number of arguments with add_action():

add_action( 'wp_insert_comment', array( 'CanvasLd', 'learndash_new_assignment_comment' ), 10, 2 );
发布评论

评论列表(0)

  1. 暂无评论