Skip to:
Content

bbPress.org

Ticket #2533: 2533.3.diff

File 2533.3.diff, 4.2 KB (added by netweb, 7 years ago)
  • includes/admin/tools.php

     
    262262                                LEFT JOIN `{$wpdb->postmeta}` AS `postmeta`
    263263                                        ON `posts`.`ID` = `postmeta`.`post_id`
    264264                                        AND `postmeta`.`meta_key` = '_bbp_topic_id'
    265                                 WHERE `posts`.`post_type` IN ( '{$tpt}', '{$rpt}' )
     265                                WHERE `posts`.`post_type` = '{$tpt}'
    266266                                        AND `posts`.`post_status` IN ( '{$pps}', '{$cps}' )
    267267                                        AND `posts`.`post_author` != '0'
    268268                                GROUP BY `postmeta`.`meta_value`);";
     
    908908                        WHERE `topic`.`meta_id` IS NULL AND `forum`.`post_type` = '{$fpt}' );" ) ) )
    909909                return array( 5, sprintf( $statement, $result ) );
    910910
    911         // After that, we give all the topics with replies the ID their last reply (again, this time for a different reason).
     911        // Give all the topics with replies the ID of their last reply or topics without a reply an ID of themself.
    912912        if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    913                         ( SELECT `topic`.`ID`, '_bbp_last_active_id', MAX( `reply`.`ID` )
    914                         FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    915                         WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
    916                         GROUP BY `topic`.`ID` );" ) ) )
     913                        ( SELECT `topic`.`ID`,  '_bbp_last_active_id', COALESCE( ( SELECT MAX( `reply`.`ID` )
     914                                                                                                                        FROM `$wpdb->posts` AS `reply`
     915                                                                                                                        WHERE `topic`.`ID` = `reply`.`post_parent` AND `reply`.`post_status` = '{$pps}') , `topic`.`ID` )
     916                        FROM `$wpdb->posts` AS `topic`
     917                        WHERE `topic`.`post_type` = '{$tpt}' );" ) ) )
    917918                return array( 6, sprintf( $statement, $result ) );
    918919
    919         // For any remaining topics, give a reply ID of themself.
     920        // Give topics with or without replies their last update time.
    920921        if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    921                         ( SELECT `ID`, '_bbp_last_active_id', `ID`
    922                         FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
    923                         ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_id'
    924                         WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' );" ) ) )
     922                        ( SELECT `topic`.`ID`,  '_bbp_last_active_time', COALESCE( ( SELECT MAX( `reply`.`post_date` )
     923                                                                                                                        FROM `$wpdb->posts` AS `reply`
     924                                                                                                                        WHERE `topic`.`ID` = `reply`.`post_parent` AND `reply`.`post_status` = '{$pps}') , `topic`.`post_date` )
     925                        FROM `$wpdb->posts` AS `topic`
     926                        WHERE `topic`.`post_type` = '{$tpt}' );" ) ) )
    925927                return array( 7, sprintf( $statement, $result ) );
    926928
    927         // Give topics with replies their last update time.
    928         if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    929                         ( SELECT `topic`.`ID`, '_bbp_last_active_time', MAX( `reply`.`post_date` )
    930                         FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    931                         WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
    932                         GROUP BY `topic`.`ID` );" ) ) )
    933                 return array( 8, sprintf( $statement, $result ) );
    934 
    935         // Give topics without replies their last update time.
    936         if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    937                         ( SELECT `ID`, '_bbp_last_active_time', `post_date`
    938                         FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
    939                         ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_time'
    940                         WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' );" ) ) )
    941                 return array( 9, sprintf( $statement, $result ) );
    942 
    943929        // Forums need to know what their last active item is as well. Now it gets a bit more complex to do in the database.
    944930        $forums = $wpdb->get_col( "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type` = '{$fpt}' and `post_status` != 'auto-draft';" );
    945931        if ( is_wp_error( $forums ) )
    946                 return array( 10, sprintf( $statement, $result ) );
     932                return array( 8, sprintf( $statement, $result ) );
    947933
    948934        // Loop through forums
    949935        foreach ( $forums as $forum_id ) {