Replace fictioneer_deleted_by_user with user_deleted comment type

One meta field less.
This commit is contained in:
Tetrakern 2024-09-23 03:01:53 +02:00
parent ed1d3c6f86
commit d319025dbb
10 changed files with 60 additions and 24 deletions

View File

@ -324,7 +324,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| WORDPRESS HOOK | FICTIONEER FILTERS
| ---: | :--- |
| `admin_body_class` | `fictioneer_add_classes_to_admin_body`
| `admin_comment_types_dropdown` | `fictioneer_add_private_to_comment_filter`
| `admin_comment_types_dropdown` | `fictioneer_add_private_to_comment_filter`, `fictioneer_add_user_deleted_to_comment_filter`
| `allowed_block_types_all` | `fictioneer_allowed_block_types`, `fictioneer_restrict_block_types`
| `block_editor_settings_all` | `fictioneer_disable_font_library`
| `body_class` | `fictioneer_add_classes_to_body`

View File

@ -39,7 +39,7 @@ $is_ajax_comments = get_option( 'fictioneer_enable_ajax_comments' );
);
if ( ! get_option( 'fictioneer_disable_comment_query' ) ) {
$query_args['type'] = ['comment', 'private'];
$query_args['type'] = ['comment', 'private', 'user_deleted'];
} else {
$query_args['type'] = ['comment'];
}

View File

@ -443,7 +443,6 @@ if ( ! function_exists( 'fictioneer_soft_delete_user_comments' ) ) {
$comment_count = count( $comments );
$count = 0;
$complete_one = true;
$complete_two = true;
// Not found or empty
if ( empty( $comments ) ) {
@ -455,7 +454,8 @@ if ( ! function_exists( 'fictioneer_soft_delete_user_comments' ) ) {
$result_one = wp_update_comment(
array(
'user_ID' => 0,
'comment_author' => __( 'Deleted', 'fictioneer' ),
'comment_type' => 'user_deleted',
'comment_author' => _x( 'Deleted', 'Deleted comment author name.', 'fictioneer' ),
'comment_ID' => $comment->comment_ID,
'comment_content' => __( 'Comment has been deleted by user.', 'fictioneer' ),
'comment_author_email' => '',
@ -465,10 +465,8 @@ if ( ! function_exists( 'fictioneer_soft_delete_user_comments' ) ) {
)
);
$result_two = fictioneer_update_comment_meta( $comment->comment_ID, 'fictioneer_deleted_by_user', true );
// Keep track of updated comments
if ( $result_one && $result_two ) {
if ( $result_one ) {
$count++;
}
@ -476,17 +474,13 @@ if ( ! function_exists( 'fictioneer_soft_delete_user_comments' ) ) {
if ( ! $result_one || is_wp_error( $result_one ) ) {
$complete_one = false;
}
if ( ! $result_two || is_wp_error( $result_two ) ) {
$complete_two = false;
}
}
// Report result
return array(
'complete' => $complete_one && $complete_two,
'complete' => $complete_one,
'failure' => $count == 0,
'success' => $count == $comment_count && $complete_one && $complete_two,
'success' => $count == $comment_count && $complete_one,
'comment_count' => $comment_count,
'updated_count' => $count
);

View File

@ -74,7 +74,7 @@ function fictioneer_post_comment_to_discord( $comment_id, $comment_approved ) {
'title' => html_entity_decode( get_the_title( $post ) ),
'description' => html_entity_decode( get_comment_excerpt( $comment ) ),
'url' => get_comment_link( $comment ),
'color' => $comment_status == 'approved' ? '9692513' : '14112322',
'color' => $comment_status === 'approved' ? '9692513' : '14112322',
'fields' => array(
array(
'name' => _x( 'Status', 'Discord message "Status" field.', 'fictioneer' ),

View File

@ -389,6 +389,36 @@ function fictioneer_remove_first_publish_date_from_meta() {
}
add_action( 'fictioneer_after_update', 'fictioneer_remove_first_publish_date_from_meta' );
/**
* Convert user deleted comments to user_deleted type and delete meta fields
*
* @since 5.24.1
*/
function fictioneer_migrate_deleted_comment_meta() {
global $wpdb;
$wpdb->query(
"
UPDATE $wpdb->comments
SET comment_type = 'user_deleted'
WHERE comment_ID IN (
SELECT comment_id
FROM $wpdb->commentmeta
WHERE meta_key = 'fictioneer_deleted_by_user'
)
"
);
$wpdb->query(
"
DELETE FROM $wpdb->commentmeta
WHERE meta_key = 'fictioneer_deleted_by_user'
"
);
}
add_action( 'fictioneer_after_update', 'fictioneer_migrate_deleted_comment_meta' );
// =============================================================================
// PROTECT META FIELDS
// =============================================================================

View File

@ -121,7 +121,7 @@ function fictioneer_ajax_get_comment_section() {
$query_args = array( 'post_id' => $post_id );
if ( ! get_option( 'fictioneer_disable_comment_query' ) ) {
$query_args['type'] = ['comment', 'private'];
$query_args['type'] = ['comment', 'private', 'user_deleted'];
$query_args['order'] = $order;
} else {
// Still hide private comments but do not limit the types preemptively
@ -619,11 +619,10 @@ function fictioneer_ajax_delete_my_comment() {
}
// Soft-delete comment
fictioneer_update_comment_meta( $comment->comment_ID, 'fictioneer_deleted_by_user', true );
$result = wp_update_comment(
array(
'user_ID' => 0,
'comment_type' => 'user_deleted',
'comment_author' => _x( 'Deleted', 'Deleted comment author name.', 'fictioneer' ),
'comment_ID' => $comment->comment_ID,
'comment_content' => __( 'Comment has been deleted by user.', 'fictioneer' ),

View File

@ -182,7 +182,7 @@ function fictioneer_validate_comment_form( $commentdata ) {
}
// Abort if direct parent comment is deleted
if ( $parent_id && get_comment_meta( $parent_id, 'fictioneer_deleted_by_user', true ) ) {
if ( $parent_id && $commentdata['comment_type'] === 'user_deleted' ) {
if ( $is_ajax ) {
wp_send_json_error( array( 'error' => __( 'You cannot reply to deleted comments.', 'fictioneer' ) ) );
} else {

View File

@ -47,7 +47,7 @@ function fictioneer_user_can_moderate( $comment, $user_id = null ) {
}
// =============================================================================
// ADD PRIVATE TYPE TO FILTER
// ADD CUSTOM COMMENT TYPES TO FILTER
// =============================================================================
/**
@ -61,9 +61,21 @@ function fictioneer_add_private_to_comment_filter( $comment_types ) {
return $comment_types;
}
add_filter( 'admin_comment_types_dropdown', 'fictioneer_add_private_to_comment_filter' );
/**
* Add user_deleted comments to dropdown filter menu
*
* @since 5.24.1
*/
function fictioneer_add_user_deleted_to_comment_filter( $comment_types ) {
$comment_types['user_deleted'] = _x( 'Deleted', 'Deleted comments filter in comment screen dropdown.', 'fictioneer' );
return $comment_types;
}
add_filter( 'admin_comment_types_dropdown', 'fictioneer_add_user_deleted_to_comment_filter' );
// =============================================================================
// ADD METABOX TO COMMENT EDIT SCREEN
// =============================================================================
@ -570,7 +582,7 @@ function fictioneer_ajax_moderate_comment() {
wp_send_json_error( ['error' => __( 'Child comments cannot be sticky.', 'fictioneer' )] );
break;
}
if ( get_comment_meta( $comment->comment_ID, 'fictioneer_deleted_by_user', true ) ) {
if ( $comment->comment_type === 'user_deleted' ) {
wp_send_json_error( ['error' => __( 'Deleted comments cannot be sticky.', 'fictioneer' )] );
break;
}

View File

@ -55,7 +55,8 @@ function fictioneer_shift_sticky_comments( $comments ) {
if (
get_comment_meta( $comment->comment_ID, 'fictioneer_sticky', true ) &&
! get_comment_meta( $comment->comment_ID, 'fictioneer_marked_offensive', true ) &&
! get_comment_meta( $comment->comment_ID, 'fictioneer_deleted_by_user', true )
$comment->comment_approved &&
$comment->comment_type !== 'user_deleted'
) {
$sticky_comments[] = $comment;
}
@ -437,7 +438,7 @@ if ( ! function_exists( 'fictioneer_theme_comment' ) ) {
$is_approved = $comment->comment_approved;
$is_offensive = get_comment_meta( $comment->comment_ID, 'fictioneer_marked_offensive', true );
$is_closed = get_comment_meta( $comment->comment_ID, 'fictioneer_thread_closed', true );
$is_deleted_by_owner = get_comment_meta( $comment->comment_ID, 'fictioneer_deleted_by_user', true );
$is_deleted_by_owner = $comment->comment_type === 'user_deleted';
$is_ignoring_reports = get_comment_meta( $comment->comment_ID, 'fictioneer_ignore_reports', true );
$is_child_of_private = false;
$is_report_hidden = false;

View File

@ -229,7 +229,7 @@ function fictioneer_rest_get_story_comments( WP_REST_Request $request ) {
$reports = get_comment_meta( $comment->comment_ID, 'fictioneer_user_reports', true );
// Render empty if...
if ( get_comment_meta( $comment->comment_ID, 'fictioneer_deleted_by_user', true ) ) {
if ( $comment->comment_type === 'user_deleted' ) {
// Start HTML ---> ?>
<li class="fictioneer-comment _deleted">
<div class="fictioneer-comment__container">