首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除所有邮件,当解垃圾邮件时

删除所有邮件,当解垃圾邮件时
EN

WordPress Development用户
提问于 2022-05-09 15:26:44
回答 1查看 27关注 0票数 0

我正在尝试用error类替换默认的反垃圾消息。admin_notices按预期做了所有事情,但是即使我在bulk_post_updated_messages过滤器中返回空消息数组,我仍然可以看到注意事项,但是带有编辑链接的内容是空的。

根据wp-admin/edit.php的说法,消息是基于$bulk_counts生成的,但是bulk_post_updated_messages过滤器只返回$bulk_messages,即使我更改了这个数组,也什么也没有发生。如何访问$bulk_counts

代码语言:javascript
复制
$bulk_messages = apply_filters( 'bulk_post_updated_messages', $bulk_messages, $bulk_counts );
$bulk_counts   = array_filter( $bulk_counts );

...

// If we have a bulk message to issue:
$messages = array();
foreach ( $bulk_counts as $message => $count ) {
    if ( isset( $bulk_messages[ $post_type ][ $message ] ) ) {
        $messages[] = sprintf( $bulk_messages[ $post_type ][ $message ], number_format_i18n( $count ) );
    } elseif ( isset( $bulk_messages['post'][ $message ] ) ) {
        $messages[] = sprintf( $bulk_messages['post'][ $message ], number_format_i18n( $count ) );
    }

    if ( 'trashed' === $message && isset( $_REQUEST['ids'] ) ) {
        $ids        = preg_replace( '/[^0-9,]/', '', $_REQUEST['ids'] );
        $messages[] = '' . __( 'Undo' ) . '';
    }

    if ( 'untrashed' === $message && isset( $_REQUEST['ids'] ) ) {
        $ids = explode( ',', $_REQUEST['ids'] );

        if ( 1 === count( $ids ) && current_user_can( 'edit_post', $ids[0] ) ) {
            $messages[] = sprintf(
                '%2$s',
                esc_url( get_edit_post_link( $ids[0] ) ),
                esc_html( get_post_type_object( get_post_type( $ids[0] ) )->labels->edit_item )
            );
        }
    }
}
EN

回答 1

WordPress Development用户

发布于 2022-05-10 17:17:13

如前所述,批量消息输出基于$bulk_counts变量,不能在admin_notices钩子中进行编辑。这很奇怪,因为他们在wp-admin/edit.php中有自己的html包装,没有任何操作和过滤器。所以我更深入地研究WP核心。

$bulk_counts变量在该行的wp-admin/edit.php中生成:

代码语言:javascript
复制
$bulk_counts = array(
    'updated'   => isset( $_REQUEST['updated'] ) ? absint( $_REQUEST['updated'] ) : 0,
    'locked'    => isset( $_REQUEST['locked'] ) ? absint( $_REQUEST['locked'] ) : 0,
    'deleted'   => isset( $_REQUEST['deleted'] ) ? absint( $_REQUEST['deleted'] ) : 0,
    'trashed'   => isset( $_REQUEST['trashed'] ) ? absint( $_REQUEST['trashed'] ) : 0,
    'untrashed' => isset( $_REQUEST['untrashed'] ) ? absint( $_REQUEST['untrashed'] ) : 0,
);

正如我们所看到的,untrashed键从查询字符串中获取。如果我们清拆单个记录,则此代码中wp-admin/post.php中的操作处理:

代码语言:javascript
复制
case 'untrash':
        check_admin_referer( 'untrash-post_' . $post_id );

        if ( ! $post ) {
            wp_die( __( 'The item you are trying to restore from the Trash no longer exists.' ) );
        }

        if ( ! $post_type_object ) {
            wp_die( __( 'Invalid post type.' ) );
        }

        if ( ! current_user_can( 'delete_post', $post_id ) ) {
            wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
        }

        if ( ! wp_untrash_post( $post_id ) ) {
            wp_die( __( 'Error in restoring the item from Trash.' ) );
        }

        $sendback = add_query_arg(
            array(
                'untrashed' => 1,
                'ids'       => $post_id,
            ),
            $sendback
        );
        wp_redirect( $sendback );
        exit;

没有过滤器和操作,但是在最后我们得到了wp_redirect()函数,它有一个过滤器wp_redirect

要禁用消息,我们必须删除untrashed=1参数和带有当前post id的可选ids=***。在我的例子中,我在会话中编写一个标志,并在wp_redirect中检查这个标志

重定向后

代码语言:javascript
复制
'untrashed' => isset( $_REQUEST['untrashed'] ) ? absint( $_REQUEST['untrashed'] ) : 0;

部件返回0,根据我最初发布的代码,消息将消失。

这是我找到的唯一解决办法。欢迎任何其他解决办法。

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/405560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档