Make WordPress Core


Ignore:
Timestamp:
04/17/2020 07:33:52 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Comments: Use comment instead of an empty string for the comment_type DB field value in comments table.

This is the first step to bring support for custom comment types into WordPress.

Add a scheduled upgrade routine to update the type value for existing comments, in batches of 100 at a time.

Props imath, aaroncampbell, jeremyfelt, dshanske.
Fixes #49236.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r47550 r47597  
    18541854 *
    18551855 * @since 2.0.0
    1856  * @since 4.4.0 Introduced `$comment_meta` argument.
     1856 * @since 4.4.0 Introduced the `$comment_meta` argument.
     1857 * @since 5.5.0 Default value for `$comment_type` argument changed to `comment`.
    18571858 *
    18581859 * @global wpdb $wpdb WordPress database abstraction object.
     
    18781879 *     @type int        $comment_post_ID      ID of the post that relates to the comment, if any.
    18791880 *                                            Default 0.
    1880  *     @type string     $comment_type         Comment type. Default empty.
     1881 *     @type string     $comment_type         Comment type. Default 'comment'.
    18811882 *     @type array      $comment_meta         Optional. Array of key/value pairs to be stored in commentmeta for the
    18821883 *                                            new comment.
     
    19021903    $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved'];
    19031904    $comment_agent    = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent'];
    1904     $comment_type     = ! isset( $data['comment_type'] ) ? '' : $data['comment_type'];
     1905    $comment_type     = ! isset( $data['comment_type'] ) ? 'comment' : $data['comment_type'];
    19051906    $comment_parent   = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent'];
    19061907
     
    20442045 *
    20452046 * @since 1.5.0
    2046  * @since 4.3.0 'comment_agent' and 'comment_author_IP' can be set via `$commentdata`.
     2047 * @since 4.3.0 Introduced the `comment_agent` and `comment_author_IP` arguments.
    20472048 * @since 4.7.0 The `$avoid_die` parameter was added, allowing the function to
    20482049 *              return a WP_Error object instead of dying.
     2050 * @since 5.5.0 Introduced the `comment_type` argument.
    20492051 *
    20502052 * @see wp_insert_comment()
     
    20612063 *     @type string $comment_date_gmt     The date the comment was submitted in the GMT timezone.
    20622064 *                                        Default is `$comment_date` in the GMT timezone.
     2065 *     @type string $comment_type         Comment type. Default 'comment'.
    20632066 *     @type int    $comment_parent       The ID of this comment's parent, if any. Default 0.
    20642067 *     @type int    $comment_post_ID      The ID of the post that relates to the comment.
     
    21212124    if ( empty( $commentdata['comment_date_gmt'] ) ) {
    21222125        $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 );
     2126    }
     2127
     2128    if ( empty( $commentdata['comment_type'] ) ) {
     2129        $commentdata['comment_type'] = 'comment';
    21232130    }
    21242131
     
    33493356    }
    33503357
    3351     $comment_type = '';
     3358    $comment_type = 'comment';
    33523359
    33533360    if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
     
    36373644    wp_cache_set( 'last_changed', microtime(), 'comment' );
    36383645}
     3646
     3647/**
     3648 * Updates the comment type for a batch of comments.
     3649 *
     3650 * @since 5.5.0
     3651 *
     3652 * @global wpdb $wpdb WordPress database abstraction object.
     3653 */
     3654function _wp_batch_update_comment_type() {
     3655    global $wpdb;
     3656
     3657    $lock_name = 'update_comment_type.lock';
     3658
     3659    // Try to lock.
     3660    $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) );
     3661
     3662    if ( ! $lock_result ) {
     3663        $lock_result = get_option( $lock_name );
     3664
     3665        // Bail if we were unable to create a lock, or if the existing lock is still valid.
     3666        if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) {
     3667            wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' );
     3668            return;
     3669        }
     3670    }
     3671
     3672    // Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
     3673    update_option( $lock_name, time() );
     3674
     3675    // Check if there's still an empty comment type.
     3676    $empty_comment_type = $wpdb->get_var(
     3677        "SELECT comment_ID FROM $wpdb->comments
     3678        WHERE comment_type = ''
     3679        LIMIT 1"
     3680    );
     3681
     3682    // No empty comment type, we're done here.
     3683    if ( ! $empty_comment_type ) {
     3684        update_option( 'finished_updating_comment_type', true );
     3685        delete_option( $lock_name );
     3686        return;
     3687    }
     3688
     3689    // Empty comment type found? We'll need to run this script again.
     3690    wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' );
     3691
     3692    // Update the `comment_type` field value to be `comment` for the next 100 rows of comments.
     3693    $wpdb->query(
     3694        "UPDATE {$wpdb->comments}
     3695        SET comment_type = 'comment'
     3696        WHERE comment_type = ''
     3697        ORDER BY comment_ID DESC
     3698        LIMIT 100"
     3699    );
     3700
     3701    delete_option( $lock_name );
     3702}
     3703
     3704/**
     3705 * In order to avoid the _wp_batch_update_comment_type() job being accidentally removed,
     3706 * check that it's still scheduled while we haven't finished updating comment types.
     3707 *
     3708 * @ignore
     3709 * @since 5.5.0
     3710 */
     3711function _wp_check_for_scheduled_update_comment_type() {
     3712    if ( ! get_option( 'finished_updating_comment_type' ) && ! wp_next_scheduled( 'wp_update_comment_type_batch' ) ) {
     3713        wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'wp_update_comment_type_batch' );
     3714    }
     3715}
Note: See TracChangeset for help on using the changeset viewer.