Make WordPress Core

Changeset 47597


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.

Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r47550 r47597  
    12841284        $comment_author_url   = wp_slash( $user->user_url );
    12851285        $comment_content      = trim( $_POST['content'] );
    1286         $comment_type         = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : '';
     1286        $comment_type         = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : 'comment';
    12871287
    12881288        if ( current_user_can( 'unfiltered_html' ) ) {
  • trunk/src/wp-admin/includes/schema.php

    r47550 r47597  
    112112    comment_approved varchar(20) NOT NULL default '1',
    113113    comment_agent varchar(255) NOT NULL default '',
    114     comment_type varchar(20) NOT NULL default '',
     114    comment_type varchar(20) NOT NULL default 'comment',
    115115    comment_parent bigint(20) unsigned NOT NULL default '0',
    116116    user_id bigint(20) unsigned NOT NULL default '0',
  • trunk/src/wp-admin/includes/upgrade.php

    r47550 r47597  
    284284                'comment_date_gmt'     => $now_gmt,
    285285                'comment_content'      => $first_comment,
     286                'comment_type'         => 'comment',
    286287            )
    287288        );
     
    835836    }
    836837
     838    if ( $wp_current_db_version < 47597 ) {
     839        upgrade_550();
     840    }
     841
    837842    maybe_disable_link_manager();
    838843
     
    21562161
    21572162/**
     2163 * Executes changes made in WordPress 5.5.0.
     2164 *
     2165 * @ignore
     2166 * @since 5.5.0
     2167 */
     2168function upgrade_550() {
     2169    update_option( 'finished_updating_comment_type', 0 );
     2170    wp_schedule_single_event( time() + ( 1 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' );
     2171}
     2172
     2173/**
    21582174 * Executes network-level upgrade routines.
    21592175 *
  • trunk/src/wp-content/themes/twentyten/functions.php

    r47122 r47597  
    419419        switch ( $comment->comment_type ) :
    420420            case '':
     421            case 'comment':
    421422                ?>
    422423        <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
  • trunk/src/wp-includes/class-wp-comment-query.php

    r47550 r47597  
    742742                    case 'comment':
    743743                    case 'comments':
    744                         $comment_types[ $operator ][] = "''";
     744                        $comment_types[ $operator ][] = "'comment'";
    745745                        break;
    746746
  • trunk/src/wp-includes/class-wp-comment.php

    r47550 r47597  
    115115     *
    116116     * @since 4.4.0
    117      * @var string
    118      */
    119     public $comment_type = '';
     117     * @since 5.5.0 Default value changed to `comment`.
     118     * @var string
     119     */
     120    public $comment_type = 'comment';
    120121
    121122    /**
  • 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}
  • trunk/src/wp-includes/default-filters.php

    r47554 r47597  
    439439add_action( 'wp_split_shared_term_batch', '_wp_batch_split_terms' );
    440440
     441// Comment type updates.
     442add_action( 'admin_init', '_wp_check_for_scheduled_update_comment_type' );
     443add_action( 'wp_update_comment_type_batch', '_wp_batch_update_comment_type' );
     444
    441445// Email notifications.
    442446add_action( 'comment_post', 'wp_new_comment_notify_moderator' );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r47391 r47597  
    586586        }
    587587
    588         $prepared_comment['comment_type'] = '';
     588        $prepared_comment['comment_type'] = 'comment';
    589589
    590590        /*
  • trunk/src/wp-includes/version.php

    r47426 r47597  
    2121 * @global int $wp_db_version
    2222 */
    23 $wp_db_version = 47018;
     23$wp_db_version = 47597;
    2424
    2525/**
  • trunk/tests/phpunit/tests/comment-submission.php

    r47122 r47597  
    743743                'comment_author_url'   => $user->user_url,
    744744                'comment_content'      => $data['comment'],
    745                 'comment_type'         => '',
     745                'comment_type'         => 'comment',
    746746                'comment_parent'       => '0',
    747747                'user_ID'              => $user->ID,
Note: See TracChangeset for help on using the changeset viewer.