Skip to content

Commit

Permalink
Update to WPCS 3, and add remove URL from comment functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jdevalk committed Sep 18, 2024
1 parent 8c95466 commit 30f07bb
Show file tree
Hide file tree
Showing 15 changed files with 790 additions and 110 deletions.
56 changes: 40 additions & 16 deletions admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace EmiliaProjects\WP\Comment\Admin;

use EmiliaProjects\WP\Comment\Inc\Hacks;
use WP_Comment;
use WP_Post;
use EmiliaProjects\WP\Comment\Inc\Hacks;

/**
* Admin handling class.
Expand All @@ -16,7 +16,7 @@ class Admin {
*
* @var string
*/
const NOTIFICATION_RECIPIENT_KEY = '_comment_notification_recipient';
private const NOTIFICATION_RECIPIENT_KEY = '_comment_notification_recipient';

/**
* The plugin page hook.
Expand All @@ -26,7 +26,7 @@ class Admin {
/**
* Holds the plugins options.
*
* @var mixed[]
* @var string[]
*/
public array $options = [];

Expand Down Expand Up @@ -67,6 +67,8 @@ public function __construct() {
*
* @param string $comment_text Text of the current comment.
* @param WP_Comment $comment The comment object. Null if not found.
*
* @return string
*/
public function show_forward_status( $comment_text, $comment ): string {
if ( ! \is_admin() ) {
Expand All @@ -85,6 +87,8 @@ public function show_forward_status( $comment_text, $comment ): string {

/**
* Forwards a comment to an email address chosen in the settings.
*
* @return void
*/
public function forward_comment(): void {
if ( empty( $this->options['forward_email'] ) ) {
Expand Down Expand Up @@ -114,7 +118,7 @@ public function forward_comment(): void {
/* translators: %1$s is replaced by (a link to) the blog's name, %2$s by (a link to) the title of the post. */
\esc_html__( 'This comment was forwarded from %1$s where it was left on: %2$s.', 'comment-hacks' ),
'<a href=" ' . \esc_url( \get_site_url() ) . ' ">' . \esc_html( \get_bloginfo( 'name' ) ) . '</a>',
'<a href="' . \esc_url( \get_permalink( (int) $comment->comment_post_ID ) ) . '">' . esc_html( \get_the_title( (int) $comment->comment_post_ID ) ) . '</a>'
'<a href="' . \esc_url( \get_permalink( (int) $comment->comment_post_ID ) ) . '">' . \esc_html( \get_the_title( (int) $comment->comment_post_ID ) ) . '</a>'
) . "\n\n";

if ( ! empty( $this->options['forward_extra'] ) ) {
Expand Down Expand Up @@ -149,6 +153,8 @@ public function forward_comment(): void {
*
* @param string[] $actions The actions shown underneath comments.
* @param WP_Comment $comment The individual comment object.
*
* @return string[]
*/
public function forward_to_support_action_link( $actions, $comment ): array {
if ( empty( $this->options['forward_email'] ) ) {
Expand All @@ -164,13 +170,15 @@ public function forward_to_support_action_link( $actions, $comment ): array {
$label = \__( 'Forward to support & trash', 'comment-hacks' );
}

$actions['ch_forward'] = '<a href="' . \esc_url( \admin_url( 'edit-comments.php' ) . '?comment_id=' . $comment->comment_ID . '&ch_action=forward_comment&nonce=' . \wp_create_nonce( 'comment-hacks-forward' ) ) . '">' . esc_html( $label ) . '</a>';
$actions['ch_forward'] = '<a href="' . \esc_url( \admin_url( 'edit-comments.php' ) . '?comment_id=' . $comment->comment_ID . '&ch_action=forward_comment&nonce=' . \wp_create_nonce( 'comment-hacks-forward' ) ) . '">' . \esc_html( $label ) . '</a>';

return $actions;
}

/**
* Register meta box(es).
*
* @return void
*/
public function register_meta_boxes(): void {
\add_meta_box(
Expand All @@ -189,6 +197,8 @@ public function register_meta_boxes(): void {
* Meta box display callback.
*
* @param WP_Post $post Current post object.
*
* @return void
*/
public function meta_box_callback( $post ): void {
?>
Expand Down Expand Up @@ -230,6 +240,8 @@ public function meta_box_callback( $post ): void {

/**
* Register the options array along with the validation function.
*
* @return void
*/
public function init(): void {
// Register our option array.
Expand All @@ -245,6 +257,8 @@ public function init(): void {

/**
* Enqueue our admin script.
*
* @return void
*/
public function enqueue(): void {
$page = \filter_input( \INPUT_GET, 'page' );
Expand All @@ -271,6 +285,8 @@ public function enqueue(): void {
* Saves the comment email recipients post meta.
*
* @param int $post_id The post ID.
*
* @return void
*/
public function save_reroute_comment_emails( $post_id ): void {

Expand All @@ -289,7 +305,9 @@ public function save_reroute_comment_emails( $post_id ): void {
*
* @since 1.0
*
* @param array $input Input with unvalidated options.
* @param string[] $input Input with unvalidated options.
*
* @return string[]
*/
public function options_validate( array $input ): array {
$defaults = Hacks::get_defaults();
Expand All @@ -303,7 +321,7 @@ public function options_validate( array $input ): array {
$input[ $key ] = (int) $value;
break;
case 'version':
$input[ $key ] = EMILIA_COMMENT_HACKS_VERSION;
$input[ $key ] = \EMILIA_COMMENT_HACKS_VERSION;
break;
case 'comment_policy':
case 'clean_emails':
Expand Down Expand Up @@ -342,7 +360,9 @@ public function options_validate( array $input ): array {
/**
* Turns checkbox values into booleans.
*
* @param mixed $value The input value to cast to boolean.
* @param string|bool $value The input value to cast to boolean.
*
* @return bool
*/
private function sanitize_bool( $value ): bool {
return ( $value || ! empty( $value ) );
Expand All @@ -351,13 +371,13 @@ private function sanitize_bool( $value ): bool {
/**
* Turns empty string into defaults.
*
* @param mixed $value The input value.
* @param string $default The default value of the string.
* @param string $value The input value.
* @param string $default_value The default value of the string.
*
* @return array $input The array with sanitized input values.
* @return string
*/
private function sanitize_string( $value, $default ) {
return ( $value === '' ) ? $default : $value;
private function sanitize_string( $value, $default_value ) {
return ( $value === '' ) ? $default_value : $value;
}

/**
Expand All @@ -381,14 +401,16 @@ public function add_config_page() {
/**
* Register the settings link for the plugins page.
*
* @param array $links The plugin action links.
* @param string $file The plugin file.
* @param string[] $links The plugin action links.
* @param string $file The plugin file.
*
* @return string[]
*/
public function filter_plugin_actions( $links, $file ): array {
/* Static so we don't call plugin_basename on every plugin row. */
static $this_plugin;
if ( ! $this_plugin ) {
$this_plugin = \plugin_basename( EMILIA_COMMENT_HACKS_FILE );
$this_plugin = \plugin_basename( \EMILIA_COMMENT_HACKS_FILE );
}

if ( $file === $this_plugin ) {
Expand All @@ -402,6 +424,8 @@ public function filter_plugin_actions( $links, $file ): array {

/**
* Output the config page.
*
* @return void
*/
public function config_page(): void {
require_once \EMILIA_COMMENT_HACKS_PATH . 'admin/views/config-page.php';
Expand Down
36 changes: 36 additions & 0 deletions admin/assets/js/remove-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* global chCommentBlockEdit */

jQuery( document ).ready( function( $ ) {
$( ".comment-remove-url" ).on( "click", function( e ) {
e.preventDefault();

var commentId = $( this ).data( "comment-id" );

$.ajax( {
url: chCommentBlockEdit.ajax_url,
type: "POST",
data: {
action: "ch_remove_comment_url",
commentId: commentId,
nonce: chCommentBlockEdit.nonce,
},
/**
* Handle the AJAX response.
*
* @param {Object} response The response object.
* @param {boolean} response.success Indicates if the request was successful.
* @param {string} response.data The response data.
*
* @returns {void}
*/
success: function( response ) {
if ( response.success ) {
// Reload the page to reflect the changes.
location.reload();
} else {
console.error( response.data );
}
},
} );
} );
} );
2 changes: 1 addition & 1 deletion admin/comment-parent.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function load_comment_parent_box() {
if ( \function_exists( 'add_meta_box' ) ) {
\add_meta_box(
'comment_parent',
esc_html__( 'Comment Parent', 'comment-hacks' ),
\esc_html__( 'Comment Parent', 'comment-hacks' ),
[
$this,
'comment_parent_box',
Expand Down
3 changes: 2 additions & 1 deletion admin/views/config-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
/**
* Config page admin view.
*
* @var \EmiliaProjects\WP\Comment\Admin\Admin $this
* @var Admin $this
*/

use EmiliaProjects\WP\Comment\Admin\Admin;
use EmiliaProjects\WP\Comment\Inc\Hacks;

?>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"composer/installers": "^1.12.0"
},
"require-dev": {
"yoast/yoastcs": "^2.2.1",
"yoast/yoastcs": "^3.1",
"yoast/wp-test-utils": "^1.1.1",
"phpstan/phpstan": "^1.10",
"szepeviktor/phpstan-wordpress": "^1.3",
Expand Down
Loading

0 comments on commit 30f07bb

Please sign in to comment.