Ticket #2731: 2731.7.diff
| File 2731.7.diff, 15.1 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 8 // add run_sync to link 9 var sync_reply_positions = jQuery( '#the-list a#bbp-sync-all-reply-positions' ); 10 jQuery( sync_reply_positions ).addClass( 'run_sync' ); 11 12 // disable checkbox for now 13 jQuery( '#the-list a#bbp-sync-all-reply-positions' ).closest( '#bbp-repair-tools' ).find( 'input[type=checkbox]' ).attr( 'disabled', 'disabled' ); 14 15 function bbp_repair_reply_menu_order_call( nonce, iteration, continue_menu_order_zero ) { 16 17 if( jQuery( '#the-list a#bbp-sync-all-reply-positions.killed' ).length > 0 ){ 18 jQuery( '#the-list a#bbp-sync-all-reply-positions.killed' ).removeClass( 'killed' ); 19 console.log( 'Process killed.' ); 20 return; 21 } 22 23 input = { 24 'action' : 'bbp_admin_repair_reply_menu_order', 25 'nonce' : nonce, 26 'iteration' : iteration, 27 'continue_menu_order_zero' : continue_menu_order_zero 28 }; 29 30 var bbp_repair_ajax = jQuery.ajax({ 31 type: 'POST', 32 url : ajaxurl, // this is already there, let's use it 33 data : input, 34 dataType : 'json' 35 }); 36 37 jQuery.when( bbp_repair_ajax ).then( function( data ){ 38 39 if (data.result === 'keep-going' ) { 40 41 input = {}; 42 43 if( jQuery( '#the-list a#bbp-sync-all-reply-positions.killed' ).length > 0 ){ 44 jQuery( '#the-list a#bbp-sync-all-reply-positions.killed' ).addClass( 'run_sync' ); 45 } 46 47 console.log( data.notice ); 48 return bbp_repair_reply_menu_order_call( data.passed_nonce, data.iteration, data.continue_menu_order_zero ); 49 } 50 else if (data.result === 'all-done' ){ 51 console.log( data.notice ); 52 jQuery( '#the-list a#bbp-sync-all-reply-positions' ).removeClass( 'killed' ).removeClass( 'kill' ).html( 'Finished. Run again' ); 53 } 54 else if (data.result === 'nonce-failed' ){ 55 console.log( data.notice ); 56 } 57 else{ 58 console.log( 'boo' ); 59 } 60 }); 61 } 62 63 jQuery( '#the-list' ).on( 'click', 'a#bbp-sync-all-reply-positions.run_sync', function(e){ 64 65 e.preventDefault(); 66 67 // set the initial values 68 var nonce = jQuery( this ).attr( 'data-nonce' ); 69 var iteration; 70 var continue_menu_order_zero = true; 71 72 // make the run link a kill link 73 jQuery( this ).removeClass( 'run_sync' ).addClass( 'kill' ).html( '<strong>Kill process (will take a few seconds)</strong>' ); 74 jQuery( this ).closest( '.row-actions' ).css({ 'visibility' : 'visible' }); 75 76 // and now call the function to start the repair 77 bbp_repair_reply_menu_order_call( nonce, iteration, continue_menu_order_zero ); 78 79 }); 80 81 jQuery( '#the-list' ).on( 'click', 'a#bbp-sync-all-reply-positions.kill', function(e){ 82 e.preventDefault(); 83 84 // don't kill anymore, we can run again 85 jQuery( this ).removeClass( 'kill' ).addClass( 'killed').html( 'Process killed. Click to restart.'); 86 }); 87 88 89 }); -
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() { 2078 2081 2082 // nonceify this 2083 $nonce = ( !empty( $_POST['nonce'] ) ) ? $_POST['nonce'] : ''; 2084 $iteration = ( !empty( $_POST['iteration'] ) ) ? intval( $_POST['iteration'] ) : 0; 2085 2086 $nonce_action = ( $iteration === 0 ) ? 'bbp-sync-all-reply-positions' : 'bbp-sync-all-reply-positions' . $iteration; 2087 2088 if( empty( wp_verify_nonce( $nonce, $nonce_action ) ) ){ 2089 $data = array('notice' => 'Nonce failed.' . $create_nonce . $nonce, 'continue_menu_order_zero' => '', 'result' => 'nonce-failed'); 2090 echo json_encode( $data ); 2091 wp_die(); 2092 2093 } 2094 2095 $continue_menu_order_zero = ( !empty( $_POST['continue_menu_order_zero'] ) ) ? $_POST['continue_menu_order_zero'] : false; 2096 2079 2097 // Define variables 2080 2098 $bbp_db = bbp_db(); 2081 2099 $statement = __( 'Recalculating reply menu order … %s', 'bbpress' ); … … 2089 2107 // Post type 2090 2108 $rpt = bbp_get_reply_post_type(); 2091 2109 2110 // Pending status 2111 $pst = bbp_get_pending_status_id(); 2112 // Public status 2113 $pub = bbp_get_public_status_id(); 2114 2115 $offset = 0; 2116 $limit = 300; 2117 $replies_count = 0; 2118 2119 // ready steady GO 2120 $startTime = microtime(true); 2121 2122 // First look through the menu_order 0 replies. Are there any published replies with menu_order 0? 2123 // But don't do this if you don't need to. 2124 2125 $menu_order_zero_published = ( !$continue_menu_order_zero ) ? array() : 2126 $bbp_db->get_results( 2127 "SELECT `ID` 2128 FROM `{$bbp_db->posts}` 2129 WHERE `post_type` = '{$rpt}' AND `post_status` = '{$pub}' AND `menu_order` = 0 2130 LIMIT {$limit}", OBJECT_K ); 2131 2132 $menu_order_zero_count = count( $menu_order_zero_published ); 2133 2134 // only get the array with duplicate menu orders if we're going to use it. 2135 if( $menu_order_zero_count < $limit ){ 2136 2137 $limit = $limit - $menu_order_zero_count; 2138 2139 // Get an array of reply ids to update the menu oder for each reply 2140 $menu_order_repeated = $bbp_db->get_results( 2141 "SELECT `a`.`ID` FROM `{$bbp_db->posts}` AS `a` 2142 INNER JOIN ( 2143 SELECT `menu_order`, `post_parent` 2144 FROM `{$bbp_db->posts}` WHERE `post_type` = '{$rpt}' AND `menu_order` != 0 2145 GROUP BY `menu_order`, `post_parent` 2146 HAVING COUNT( * ) >1 2147 )`b` 2148 ON `a`.`menu_order` = `b`.`menu_order` 2149 AND `a`.`post_parent` = `b`.`post_parent` 2150 LIMIT {$limit}", OBJECT_K ); 2151 2152 $replies = array_merge( $menu_order_zero_published, $menu_order_repeated ); 2153 } else{ 2154 $replies = $menu_order_zero_published; 2155 } 2156 2157 $replies_count = count( $replies ); 2158 2159 // Bail if no replies returned 2160 if ( $replies_count === 0 ) { 2161 // Flush the cache; things are about to get ugly. 2162 $data = array('notice' => 'All done.', 'replies' => $replies, 'result' => 'all-done'); 2163 echo json_encode( $data ); 2164 wp_die(); 2165 } 2166 2167 $update_count = 0; 2168 2169 // Recalculate the menu order position for each reply 2170 foreach ( $replies as $reply ) { 2171 $data = bbp_update_reply_position( $reply->ID ); 2172 $update_count++; 2173 } 2174 2175 // Cleanup 2176 unset( $replies, $reply ); 2177 $endTime = microtime(true); 2178 2179 // If you did a limit's worth of updating replies and that amount is the same as menu_order=0 replies, 2180 // then check again to see if there are any more. If not, don't do that query, they're all done. 2181 $continue_menu_order_zero = ( $menu_order_zero_count === $update_count ) ? true : false; 2182 2183 // Let's make a new nonce for each pass through. Just for laughs. 2184 $iteration++; 2185 $new_nonce = wp_create_nonce( 'bbp-sync-all-reply-positions' . $iteration ); 2186 2187 $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'); 2188 echo json_encode( $data ); 2189 wp_die(); 2190 2191 //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' ) ) ); 2192 } 2193 2194 /** 2195 * Recalculate reply menu order 2196 * 2197 * @since bbPress (r5367) 2198 * 2199 * @uses wpdb::query() To run our recount sql queries 2200 * @uses is_wp_error() To check if the executed query returned {@link WP_Error} 2201 * @uses bbp_get_reply_post_type() To get the reply post type 2202 * @uses bbp_update_reply_position() To update the reply position 2203 * @return array An array of the status code and the message 2204 */ 2205 function bbp_admin_repair_reply_menu_order() { 2206 global $wpdb; 2207 2208 $statement = __( 'Recalculating reply menu order … %s', 'bbpress' ); 2209 $result = __( 'No reply positions to recalculate!', 'bbpress' ); 2210 2211 // Delete cases where `_bbp_reply_to` was accidentally set to itself 2212 if ( is_wp_error( $wpdb->query( "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_reply_to' AND `post_id` = `meta_value`;" ) ) ) { 2213 return array( 1, sprintf( $statement, $result ) ); 2214 } 2215 2216 // Post type 2217 $rpt = bbp_get_reply_post_type(); 2218 2092 2219 // 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`2220 $replies = $wpdb->get_results( "SELECT `a`.`ID` FROM `{$wpdb->posts}` AS `a` 2094 2221 INNER JOIN ( 2095 2222 SELECT `menu_order`, `post_parent` 2096 FROM `{$ bbp_db->posts}`2223 FROM `{$wpdb->posts}` 2097 2224 GROUP BY `menu_order`, `post_parent` 2098 2225 HAVING COUNT( * ) >1 2099 2226 )`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
2335 2335 2336 2336 // If no position was passed, get it from the db and update the menu_order 2337 2337 if ( empty( $reply_position ) ) { 2338 $reply_position = bbp_get_reply_position_ raw( $reply_id, bbp_get_reply_topic_id( $reply_id ) );2338 $reply_position = bbp_get_reply_position_published( $reply_id, bbp_get_reply_topic_id( $reply_id ) ); 2339 2339 } 2340 2340 2341 2341 // Toggle revisions off as we are not altering content … … 2369 2369 * @param int $topic_id 2370 2370 */ 2371 2371 function bbp_get_reply_position_raw( $reply_id = 0, $topic_id = 0 ) { 2372 return bbp_get_reply_position_any( $reply_id, $topic_id, $post_status = 'all' ); 2373 } 2372 2374 2375 function bbp_get_reply_position_published( $reply_id = 0, $topic_id = 0 ) { 2376 return bbp_get_reply_position_any( $reply_id, $topic_id, $post_status = 'publish' ); 2377 } 2378 2379 function bbp_get_reply_position_any( $reply_id = 0, $topic_id = 0, $post_status = 'all' ){ 2380 2373 2381 // Get required data 2374 2382 $reply_id = bbp_get_reply_id( $reply_id ); 2375 2383 $topic_id = ! empty( $topic_id ) ? bbp_get_topic_id( $topic_id ) : bbp_get_reply_topic_id( $reply_id ); … … 2383 2391 if ( ! empty( $reply_count ) ) { 2384 2392 2385 2393 // Get reply id's 2386 $topic_replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() ); 2394 switch ( $post_status ) { 2395 case 'publish' : 2396 $topic_replies = bbp_get_public_child_ids( $topic_id, bbp_get_reply_post_type(), 'post_date DESC, ID', false ); 2397 break; 2398 default: 2399 $topic_replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() ); 2400 } 2401 2387 2402 if ( ! empty( $topic_replies ) ) { 2388 2403 2389 2404 // Reverse replies array and search for current reply position