Ticket #2731: 2731.5.diff
| File 2731.5.diff, 14.3 KB (added by , 11 years ago) |
|---|
-
src/includes/admin/admin.php
588 588 break; 589 589 } 590 590 } 591 592 if( 'tools_page_bbp-repair' === get_current_screen()->id ) { 593 wp_enqueue_script( 'bbp-tools-js', $this->js_url . 'tools.js', array( 'jquery' ), $version ); 594 } 591 595 } 592 596 593 597 /** -
src/includes/admin/js/tools.js
1 jQuery( document ).ready( function( ) { 2 3 // add in something here to check if the recalculate menu order box is ticked. 4 // If it is, it is the *only* one that can be ticked, maybe grey out the others? 5 6 var input = {}; 7 var kill = false; 8 var the_transient; 9 10 function bbp_repair_reply_menu_order_call( nonce, iteration, continue_menu_order_zero ) { 11 12 if( jQuery( '#the-list a#bbp-sync-all-reply-positions.killed' ).length > 0 ){ 13 jQuery( '#the-list a#bbp-sync-all-reply-positions.killed' ).removeClass( 'killed' ); 14 alert( 'Process killed.' ); 15 return; 16 } 17 18 input = { 19 'action' : 'bbp_admin_repair_reply_menu_order', 20 'nonce' : nonce, 21 'iteration' : iteration, 22 'continue_menu_order_zero' : continue_menu_order_zero 23 }; 24 25 var bbp_repair_ajax = jQuery.ajax({ 26 type: 'POST', 27 url : ajaxurl, // this is already there, let's use it 28 data : input, 29 dataType : 'json' 30 }); 31 32 jQuery.when( bbp_repair_ajax ).then( function( data ){ 33 34 if (data.result === 'keep-going' ) { 35 36 input = {}; 37 38 if( jQuery( '#the-list a#bbp-sync-all-reply-positions.killed' ).length > 0 ){ 39 jQuery( '#the-list a#bbp-sync-all-reply-positions.killed' ).addClass( 'run_sync' ); 40 } 41 42 confirm( data.notice + ' Do you want to keep going?' ); 43 return bbp_repair_reply_menu_order_call( data.passed_nonce, data.iteration, data.continue_menu_order_zero ); 44 } 45 else if (data.result === 'all-done' ){ 46 alert( data.notice ); 47 } 48 else if (data.result === 'nonce-failed' ){ 49 alert( data.notice ); 50 } 51 else{ 52 alert( 'boo' ); 53 } 54 }); 55 } 56 57 function bbp_repair_transient( the_transient, kill ){ 58 input = {}; 59 input = { 60 'action' : 'bbp_admin_repair_killswitch', 61 'kill' : kill, 62 'transient' : the_transient 63 } 64 65 jQuery.ajax({ 66 type: 'POST', 67 url : ajaxurl, // this is already there, let's use it 68 data : input, 69 dataType : 'json' 70 }); 71 72 } 73 74 // add run_sync to link 75 var sync_reply_positions = jQuery( '#the-list a#bbp-sync-all-reply-positions' ); 76 jQuery( sync_reply_positions ).addClass( 'run_sync' ); 77 78 jQuery( '#the-list' ).on( 'click', 'a#bbp-sync-all-reply-positions.run_sync', function(e){ 79 80 e.preventDefault(); 81 82 // set the initial values 83 var nonce = jQuery( this ).attr( 'data-nonce' ); 84 var iteration; 85 var continue_menu_order_zero = true; 86 87 // make the run link a kill link 88 jQuery( this ).removeClass( 'run_sync' ).addClass( 'kill' ).html( '<strong>Kill process (will take a few seconds)</strong>' ); 89 jQuery( this ).closest( '.row-actions' ).css({ 'visibility' : 'visible' }); 90 91 // and now call the function to start the repair 92 bbp_repair_reply_menu_order_call( nonce, iteration, continue_menu_order_zero ); 93 94 }); 95 96 jQuery( '#the-list' ).on( 'click', 'a#bbp-sync-all-reply-positions.kill', function(e){ 97 e.preventDefault(); 98 99 // don't kill anymore, we can run again 100 jQuery( this ).removeClass( 'kill' ).addClass( 'killed').html( 'Process killed. Click to restart.') 101 }); 102 103 104 }); -
src/includes/admin/tools.php
Property changes on: src/includes/admin/js/tools.js ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property
10 10 // Exit if accessed directly 11 11 defined( 'ABSPATH' ) || exit; 12 12 13 // The reply menu order repair tool uses ajax 14 add_action( 'wp_ajax_bbp_admin_repair_reply_menu_order', 'bbp_admin_repair_reply_menu_order_callback' ); 15 13 16 /** Repair ********************************************************************/ 14 17 15 18 /** … … 93 96 <strong><?php echo esc_html( $item['description'] ); ?></strong> 94 97 <div class="row-actions hide-if-no-js"> 95 98 <span class="run"> 96 <a href="<?php bbp_admin_repair_tool_run_url( $item['id'] ); ?>" aria-label="<?php printf( esc_html__( 'Run %s', 'bbpress' ), $item['description'] ); ?>" id="<?php echo esc_attr( $item['id'] ); ?>" ><?php esc_html_e( 'Run', 'bbpress' ); ?></a>99 <a href="<?php bbp_admin_repair_tool_run_url( $item['id'] ); ?>" aria-label="<?php printf( esc_html__( 'Run %s', 'bbpress' ), $item['description'] ); ?>" id="<?php echo esc_attr( $item['id'] ); ?>" data-nonce="<?php echo wp_create_nonce( esc_attr( $item['id'] ) ); ?>" ><?php esc_html_e( 'Run', 'bbpress' ); ?></a> 97 100 </span> 98 101 </div> 99 102 <button type="button" class="toggle-row"> … … 2074 2077 * @uses bbp_update_reply_position() To update the reply position 2075 2078 * @return array An array of the status code and the message 2076 2079 */ 2077 function bbp_admin_repair_reply_menu_order() { 2080 function bbp_admin_repair_reply_menu_order_callback() { 2081 global $wpdb; 2078 2082 2083 // nonceify this 2084 $nonce = ( !empty( $_POST['nonce'] ) ) ? $_POST['nonce'] : ''; 2085 $iteration = ( !empty( $_POST['iteration'] ) ) ? intval( $_POST['iteration'] ) : 0; 2086 2087 $nonce_action = ( $iteration === 0 ) ? 'bbp-sync-all-reply-positions' : 'bbp-sync-all-reply-positions' . $iteration; 2088 2089 if( empty( wp_verify_nonce( $nonce, $nonce_action ) ) ){ 2090 $data = array('notice' => 'Nonce failed.' . $create_nonce . $nonce, 'continue_menu_order_zero' => '', 'result' => 'nonce-failed'); 2091 echo json_encode( $data ); 2092 wp_die(); 2093 2094 } 2095 2096 $continue_menu_order_zero = ( !empty( $_POST['continue_menu_order_zero'] ) ) ? $_POST['continue_menu_order_zero'] : false; 2097 2079 2098 // Define variables 2080 2099 $bbp_db = bbp_db(); 2081 2100 $statement = __( 'Recalculating reply menu order … %s', 'bbpress' ); … … 2089 2108 // Post type 2090 2109 $rpt = bbp_get_reply_post_type(); 2091 2110 2111 // Pending status 2112 $pst = bbp_get_pending_status_id(); 2113 // Public status 2114 $pub = bbp_get_public_status_id(); 2115 2116 $offset = 0; 2117 $limit = 500; 2118 $replies_count = 0; 2119 2120 // ready steady GO 2121 $startTime = microtime(true); 2122 2123 // First look through the menu_order 0 replies. Are there any published replies with menu_order 0? 2124 // But don't do this if you don't need to. 2125 2126 $menu_order_zero_published = ( !$continue_menu_order_zero ) ? array() : 2127 $wpdb->get_results( 2128 "SELECT `ID` 2129 FROM `{$wpdb->posts}` 2130 WHERE `post_type` = '{$rpt}' AND `post_status` = '{$pub}' AND `menu_order` = 0 2131 LIMIT {$limit}", OBJECT_K ); 2132 2133 $menu_order_zero_count = count( $menu_order_zero_published ); 2134 2135 // only get the array with duplicate menu orders if we're going to use it. 2136 if( $menu_order_zero_count < $limit ){ 2137 2138 // Get an array of reply ids to update the menu oder for each reply 2139 $menu_order_repeated = $wpdb->get_results( 2140 "SELECT `a`.`ID` FROM `{$wpdb->posts}` AS `a` 2141 INNER JOIN ( 2142 SELECT `menu_order`, `post_parent` 2143 FROM `{$wpdb->posts}` WHERE `post_type` = '{$rpt}' AND `menu_order` != 0 2144 GROUP BY `menu_order`, `post_parent` 2145 HAVING COUNT( * ) >1 2146 )`b` 2147 ON `a`.`menu_order` = `b`.`menu_order` 2148 AND `a`.`post_parent` = `b`.`post_parent` 2149 LIMIT {$limit}", OBJECT_K ); 2150 2151 $replies = array_merge( $menu_order_zero_published, $menu_order_repeated ); 2152 } else{ 2153 $replies = $menu_order_zero_published; 2154 } 2155 2156 $replies_count = count( $replies ); 2157 2158 // Bail if no replies returned 2159 if ( $replies_count === 0 ) { 2160 // Flush the cache; things are about to get ugly. 2161 $data = array('notice' => 'All done.', 'replies' => $replies, 'result' => 'all-done'); 2162 echo json_encode( $data ); 2163 wp_die(); 2164 } 2165 2166 $update_count = 0; 2167 2168 // Recalculate the menu order position for each reply 2169 foreach ( $replies as $reply ) { 2170 $data = bbp_update_reply_position( $reply->ID ); 2171 $update_count++; 2172 } 2173 2174 // Cleanup 2175 unset( $replies, $reply ); 2176 $endTime = microtime(true); 2177 2178 // If you did a limit's worth of updating replies and that amount is the same as menu_order=0 replies, 2179 // then check again to see if there are any more. If not, don't do that query, they're all done. 2180 $continue_menu_order_zero = ( $menu_order_zero_count === $update_count ) ? true : false; 2181 2182 // Let's make a new nonce for each pass through. Just for laughs. 2183 $iteration++; 2184 $new_nonce = wp_create_nonce( 'bbp-sync-all-reply-positions' . $iteration ); 2185 2186 $data = array('notice' => 'still more to do, it has been ' . number_format( $endTime - $startTime, 2 ) . ' seconds.', 'continue_menu_order_zero' => $continue_menu_order_zero, 'passed_nonce'=> $new_nonce, 'iteration' => $iteration, 'result' => 'keep-going'); 2187 echo json_encode( $data ); 2188 wp_die(); 2189 2190 //return array( 0, sprintf( $statement, __('Average update time: ' . $averageUpdateTime . "\n\nStandard deviation: " . $stdDevUpdateTime . "\n\nAverage query time: " . $averageQueryTime . "\n\nOffset: " . $offset . ' // Replies count: ' . $replies_count . ' // Mid time: ' . ($midTime - $startTime) . ' // End time: ' . ($endTime - $startTime), 'bbpress' ) ) ); 2191 } 2192 2193 /** 2194 * Recalculate reply menu order 2195 * 2196 * @since bbPress (r5367) 2197 * 2198 * @uses wpdb::query() To run our recount sql queries 2199 * @uses is_wp_error() To check if the executed query returned {@link WP_Error} 2200 * @uses bbp_get_reply_post_type() To get the reply post type 2201 * @uses bbp_update_reply_position() To update the reply position 2202 * @return array An array of the status code and the message 2203 */ 2204 function bbp_admin_repair_reply_menu_order() { 2205 global $wpdb; 2206 2207 $statement = __( 'Recalculating reply menu order … %s', 'bbpress' ); 2208 $result = __( 'No reply positions to recalculate!', 'bbpress' ); 2209 2210 // Delete cases where `_bbp_reply_to` was accidentally set to itself 2211 if ( is_wp_error( $wpdb->query( "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_reply_to' AND `post_id` = `meta_value`;" ) ) ) { 2212 return array( 1, sprintf( $statement, $result ) ); 2213 } 2214 2215 // Post type 2216 $rpt = bbp_get_reply_post_type(); 2217 2092 2218 // Get an array of reply id's to update the menu oder for each reply 2093 $replies = $ bbp_db->get_results( "SELECT `a`.`ID` FROM `{$bbp_db->posts}` AS `a`2219 $replies = $wpdb->get_results( "SELECT `a`.`ID` FROM `{$wpdb->posts}` AS `a` 2094 2220 INNER JOIN ( 2095 2221 SELECT `menu_order`, `post_parent` 2096 FROM `{$ bbp_db->posts}`2222 FROM `{$wpdb->posts}` 2097 2223 GROUP BY `menu_order`, `post_parent` 2098 2224 HAVING COUNT( * ) >1 2099 2225 )`b` -
src/includes/common/functions.php
1612 1612 * parent id and post type 1613 1613 * @return array The array of children 1614 1614 */ 1615 function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) {1615 function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post', $order_by = 'ID', $use_cache = true ) { 1616 1616 1617 1617 // Bail if nothing passed 1618 1618 if ( empty( $parent_id ) ) { … … 1620 1620 } 1621 1621 1622 1622 // The ID of the cached query 1623 $cache_id = 'bbp_parent_public_' . $parent_id . '_type_' . $post_type . '_child_ids'; 1623 if( $order_by === 'ID' ){ 1624 $cache_id = 'bbp_parent_public_' . $parent_id . '_type_' . $post_type . '_child_ids'; 1625 } 1626 else{ 1627 $cache_id = 'bbp_' . $parent_id . '_public_children_orderby_' . $order_by; 1628 } 1624 1629 1625 1630 // Check for cache and set if needed 1626 $child_ids = wp_cache_get( $cache_id, 'bbpress_posts' ); 1631 $child_ids = ( $use_cache ) ? wp_cache_get( $cache_id, 'bbpress_posts' ) : false; 1632 1627 1633 if ( false === $child_ids ) { 1628 1634 $post_status = array( bbp_get_public_status_id() ); 1629 1635 … … 1635 1641 // Join post statuses together 1636 1642 $post_status = "'" . implode( "', '", $post_status ) . "'"; 1637 1643 $bbp_db = bbp_db(); 1638 $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY IDDESC;", $parent_id, $post_type );1644 $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY {$order_by} DESC;", $parent_id, $post_type ); 1639 1645 $child_ids = (array) $bbp_db->get_col( $query ); 1640 1646 1641 1647 wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' ); … … 1642 1648 } else { 1643 1649 $child_ids = (array) $child_ids; 1644 1650 } 1645 1646 1651 // Filter and return 1647 1652 return (array) apply_filters( 'bbp_get_public_child_ids', $child_ids, $parent_id, $post_type ); 1648 1653 } -
src/includes/replies/functions.php
2383 2383 if ( ! empty( $reply_count ) ) { 2384 2384 2385 2385 // Get reply id's 2386 $topic_replies = bbp_get_ all_child_ids( $topic_id, bbp_get_reply_post_type());2386 $topic_replies = bbp_get_public_child_ids( $topic_id, bbp_get_reply_post_type(), 'post_date', false ); 2387 2387 if ( ! empty( $topic_replies ) ) { 2388 2388 2389 2389 2390 // Reverse replies array and search for current reply position 2390 $topic_replies = array_reverse( $topic_replies );2391 $topic_replies = array_reverse( $topic_replies ); 2391 2392 $reply_position = array_search( (string) $reply_id, $topic_replies ); 2392 2393 2393 2394 // Bump the position to compensate for the lead topic post