Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Verbum_Moderate
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 disable_rich_editing
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 enqueue_scripts
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
6
 get_block_comment_being_edited
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * Moderation feature for Verbum comments.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8declare( strict_types = 1 );
9
10namespace Automattic\Jetpack;
11
12require_once __DIR__ . '/class-verbum-asset-loader.php';
13
14/**
15 * Verbum_Moderate is responsible for moderating Verbum comments in wp-admin.
16 *
17 * @phan-constructor-used-for-side-effects
18 */
19class Verbum_Moderate {
20
21    /**
22     * Class constructor.
23     */
24    public function __construct() {
25        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
26        add_filter( 'wp_editor_settings', array( $this, 'disable_rich_editing' ) );
27    }
28
29    /**
30     * Disable rich editing for comments with Gutenberg blocks.
31     *
32     * @param array $settings The editor settings.
33     * @return array The modified editor settings.
34     */
35    public function disable_rich_editing( $settings ) {
36        $comment = $this->get_block_comment_being_edited();
37        if ( $comment ) {
38            $settings['tinymce']   = false;
39            $settings['quicktags'] = false;
40        }
41
42        return $settings;
43    }
44
45    /**
46     * Enqueue scripts for loading Verbum
47     *
48     * @param string $hook The current admin page.
49     */
50    public function enqueue_scripts( $hook ) {
51        $comment = $this->get_block_comment_being_edited( $hook );
52        if ( ! $comment ) {
53            return;
54        }
55
56        \Verbum_Asset_Loader::load_editor();
57
58        wp_add_inline_style(
59            'verbum-gutenberg-css',
60            '
61            #content {
62                visibility: hidden;
63            }
64        '
65        );
66
67        Assets::register_script(
68            'verbum-comments-moderation',
69            '../../../build/verbum-comments/assets/comments-moderation.js',
70            __FILE__,
71            array(
72                'strategy'  => 'defer',
73                'in_footer' => true,
74                'enqueue'   => true,
75            )
76        );
77
78        wp_add_inline_script(
79            'verbum',
80            'window.VerbumComments = ' . wp_json_encode(
81                array(
82                    'embedNonce' => wp_create_nonce( 'embed_nonce' ),
83                    'isRTL'      => is_rtl(),
84                ),
85                JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
86            ),
87            'before'
88        );
89    }
90
91    /**
92     * Get the block-based comment being edited if on the comment edit screen.
93     *
94     * @param string|null $hook The current admin page hook.
95     * @return \WP_Comment|false The comment object if we're editing a block-based comment, false otherwise.
96     */
97    private function get_block_comment_being_edited( $hook = null ) {
98        // Check if we're on the comment.php admin page
99        if ( $hook === null ) {
100            $screen          = get_current_screen();
101            $editing_comment = $screen && 'comment' === $screen->id;
102        } else {
103            $editing_comment = 'comment.php' === $hook;
104        }
105        if ( ! $editing_comment ) {
106            return false;
107        }
108
109        // Check if we have a comment ID
110        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
111        $comment_id = isset( $_GET['c'] ) ? absint( $_GET['c'] ) : 0;
112        if ( ! $comment_id ) {
113            return false;
114        }
115
116        $comment = get_comment( $comment_id );
117        if ( ! $comment || ! has_blocks( $comment->comment_content ) ) {
118            return false;
119        }
120
121        return $comment;
122    }
123}