settings = Plugin::get_settings( 'posts' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( empty( $this->settings['comment_form_control_enabled'] ) ) { return; } add_filter( 'comment_form_fields', [ $this, 'filter_fields' ], 999 ); add_filter( 'comment_form_defaults', [ $this, 'filter_defaults' ], 999 ); if( ! empty( $this->settings['comment_form_remove_logged_in_as'] ) ) { add_filter( 'comment_form_logged_in', '__return_empty_string', 999 ); } if( ! empty( $this->settings['comment_form_remove_website'] ) ) { add_filter( 'preprocess_comment', [ $this, 'drop_website' ] ); } } /** * Adds the switch and its child fields to the Posts tab. * * @param array $fields The settings fields keyed by tab. * * @return array The modified settings fields. */ public function adminease_settings_fields( array $fields ): array { $switches = [ 'comment_form_remove_website' => __( 'Remove the Website field', 'adminease' ), 'comment_form_remove_cookies' => __( 'Remove the "Save my name and email" tick box', 'adminease' ), 'comment_form_remove_allowed_tags' => __( 'Remove the text under the form (such as the allowed HTML hint)', 'adminease' ), 'comment_form_remove_logged_in_as' => __( 'Remove the "Logged in as" line', 'adminease' ), ]; $children = []; foreach( $switches as $key => $label ) { $children[] = [ 'type' => 'switch', 'id' => str_replace( '_', '-', $key ), 'name' => 'adminease[posts][' . $key . ']', 'value' => $this->settings[ $key ] ?? '', 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => $label, 'risk' => 'safe', 'breaks' => __( 'Visitors no longer see this part of the comment form.', 'adminease' ), 'attributes' => [ 'data-parent' => 'comment-form-control-enabled', ], ]; } $texts = [ 'comment_form_submit_label' => [ 'text', __( 'Submit button text', 'adminease' ), __( 'Leave empty to keep the theme text.', 'adminease' ) ], 'comment_form_title' => [ 'text', __( 'Form title', 'adminease' ), __( 'Leave empty to keep the theme text, usually "Leave a Reply". Some themes and WooCommerce product reviews set their own texts, which win over the ones here.', 'adminease' ) ], 'comment_form_note' => [ 'textarea', __( 'Note above the form', 'adminease' ), __( 'Shown to visitors who are not logged in. Plain text only. It replaces the "required fields are marked" line, so mention that if you need it.', 'adminease' ) ], ]; foreach( $texts as $key => $text ) { $children[] = [ 'type' => $text[0], 'id' => str_replace( '_', '-', $key ), 'name' => 'adminease[posts][' . $key . ']', 'value' => $this->settings[ $key ] ?? '', 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => $text[1], 'field_description' => $text[2], 'attributes' => [ 'data-parent' => 'comment-form-control-enabled', ], ]; } $fields['posts']['fields'][] = [ 'type' => 'switch', 'id' => 'comment-form-control-enabled', 'name' => 'adminease[posts][comment_form_control_enabled]', 'value' => $this->settings['comment_form_control_enabled'] ?? '', 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Comment form control', 'adminease' ), 'risk' => 'safe', 'breaks' => __( 'The comment form your theme shows can look different.', 'adminease' ), 'description' => __( 'Remove fields from the comment form and change its button, title and note, without editing the theme.', 'adminease' ), 'child_fields' => $children, ]; return $fields; } /** * Removes the chosen fields from the default comment form fields. * * @param array $fields The default comment form fields. * * @return array The fields left. */ public function filter_fields( $fields ) { if( ! is_array( $fields ) ) { return $fields; } if( ! empty( $this->settings['comment_form_remove_website'] ) ) { unset( $fields['url'] ); } if( ! empty( $this->settings['comment_form_remove_cookies'] ) ) { unset( $fields['cookies'] ); } return $fields; } /** * Changes the texts and hints of the comment form. * * @param array $defaults The default comment form arguments. * * @return array The changed arguments. */ public function filter_defaults( $defaults ) { if( ! is_array( $defaults ) ) { return $defaults; } $submit = $this->get_text( 'comment_form_submit_label' ); $title = $this->get_text( 'comment_form_title' ); $note = $this->get_text( 'comment_form_note' ); if( '' !== $submit ) { $defaults['label_submit'] = $submit; } if( '' !== $title ) { $defaults['title_reply'] = esc_html( $title ); } if( '' !== $note ) { $defaults['comment_notes_before'] = '

' . nl2br( esc_html( $note ) ) . '

'; } if( ! empty( $this->settings['comment_form_remove_allowed_tags'] ) ) { $defaults['comment_notes_after'] = ''; } return $defaults; } /** * Drops a posted website from comments and reviews, so a forged post stores none. Pingbacks and trackbacks keep their link. * * @param array $commentdata The comment data about to be stored. * * @return array The comment data without a website. */ public function drop_website( $commentdata ) { $type = is_array( $commentdata ) ? ( $commentdata['comment_type'] ?? '' ) : ''; if( is_array( $commentdata ) && in_array( $type, [ '', 'comment', 'review' ], true ) ) { $commentdata['comment_author_url'] = ''; } return $commentdata; } /** * Reads one saved text setting. * * @param string $key The setting key. * * @return string The trimmed text, or an empty string. */ private function get_text( string $key ): string { $value = $this->settings[ $key ] ?? ''; return is_string( $value ) ? trim( $value ) : ''; } }