Ticket #2731: 2731.6.diff
| File 2731.6.diff, 15.5 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 // add run_sync to link 11 var sync_reply_positions = jQuery( '#the-list a#bbp-sync-all-reply-positions' ); 12 jQuery( sync_reply_positions ).addClass( 'run_sync' ); 13 14 // disable checkbox for now 15 jQuery( '#the-list a#bbp-sync-all-reply-positions' ).closest( '#bbp-repair-tools' ).find( 'input[type=checkbox]' ).attr( 'disabled', 'disabled' ); 16 17 function bbp_repair_reply_menu_order_call( nonce, iteration, continue_menu_order_zero ) { 18 19 if( jQuery( '#the-list a#bbp-sync-all-reply-positions.killed' ).length > 0 ){ 20 jQuery( '#the-list a#bbp-sync-all-reply-positions.killed' ).removeClass( 'killed' ); 21 alert( 'Process killed.' ); 22 return; 23 } 24 25 input = { 26 'action' : 'bbp_admin_repair_reply_menu_order', 27 'nonce' : nonce, 28 'iteration' : iteration, 29 'continue_menu_order_zero' : continue_menu_order_zero 30 }; 31 32 var bbp_repair_ajax = jQuery.ajax({ 33 type: 'POST', 34 url : ajaxurl, // this is already there, let's use it 35 data : input, 36 dataType : 'json' 37 }); 38 39 jQuery.when( bbp_repair_ajax ).then( function( data ){ 40 41 if (data.result === 'keep-going' ) { 42 43 input = {}; 44 45 if( jQuery( '#the-list a#bbp-sync-all-reply-positions.killed' ).length > 0 ){ 46 jQuery( '#the-list a#bbp-sync-all-reply-positions.killed' ).addClass( 'run_sync' ); 47 } 48 49 confirm( data.notice + ' Do you want to keep going?' ); 50 return bbp_repair_reply_menu_order_call( data.passed_nonce, data.iteration, data.continue_menu_order_zero ); 51 } 52 else if (data.result === 'all-done' ){ 53 alert( data.notice ); 54 } 55 else if (data.result === 'nonce-failed' ){ 56 alert( data.notice ); 57 } 58 else{ 59 alert( 'boo' ); 60 } 61 }); 62 } 63 64 function bbp_repair_transient( the_transient, kill ){ 65 input = {}; 66 input = { 67 'action' : 'bbp_admin_repair_killswitch', 68 'kill' : kill, 69 'transient' : the_transient 70 } 71 72 jQuery.ajax({ 73 type: 'POST', 74 url : ajaxurl, // this is already there, let's use it 75 data : input, 76 dataType : 'json' 77 }); 78 79 } 80 81 jQuery( '#the-list' ).on( 'click', 'a#bbp-sync-all-reply-positions.run_sync', function(e){ 82 83 e.preventDefault(); 84 85 // set the initial values 86 var nonce = jQuery( this ).attr( 'data-nonce' ); 87 var iteration; 88 var continue_menu_order_zero = true; 89 90 // make the run link a kill link 91 jQuery( this ).removeClass( 'run_sync' ).addClass( 'kill' ).html( '<strong>Kill process (will take a few seconds)</strong>' ); 92 jQuery( this ).closest( '.row-actions' ).css({ 'visibility' : 'visible' }); 93 94 // and now call the function to start the repair 95 bbp_repair_reply_menu_order_call( nonce, iteration, continue_menu_order_zero ); 96 97 }); 98 99 jQuery( '#the-list' ).on( 'click', 'a#bbp-sync-all-reply-positions.kill', function(e){ 100 e.preventDefault(); 101 102 // don't kill anymore, we can run again 103 jQuery( this ).removeClass( 'kill' ).addClass( 'killed').html( 'Process killed. Click to restart.') 104 }); 105 106 107 }); -
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 $limit = $limit - $menu_order_zero_count; 2139 2140 // Get an array of reply ids to update the menu oder for each reply 2141 $menu_order_repeated = $wpdb->get_results( 2142 "SELECT `a`.`ID` FROM `{$wpdb->posts}` AS `a` 2143 INNER JOIN ( 2144 SELECT `menu_order`, `post_parent` 2145 FROM `{$wpdb->posts}` WHERE `post_type` = '{$rpt}' AND `menu_order` != 0 2146 GROUP BY `menu_order`, `post_parent` 2147 HAVING COUNT( * ) >1 2148 )`b` 2149 ON `a`.`menu_order` = `b`.`menu_order` 2150 AND `a`.`post_parent` = `b`.`post_parent` 2151 LIMIT {$limit}", OBJECT_K ); 2152 2153 $replies = array_merge( $menu_order_zero_published, $menu_order_repeated ); 2154 } else{ 2155 $replies = $menu_order_zero_published; 2156 } 2157 2158 $replies_count = count( $replies ); 2159 2160 // Bail if no replies returned 2161 if ( $replies_count === 0 ) { 2162 // Flush the cache; things are about to get ugly. 2163 $data = array('notice' => 'All done.', 'replies' => $replies, 'result' => 'all-done'); 2164 echo json_encode( $data ); 2165 wp_die(); 2166 } 2167 2168 $update_count = 0; 2169 2170 // Recalculate the menu order position for each reply 2171 foreach ( $replies as $reply ) { 2172 $data = bbp_update_reply_position( $reply->ID ); 2173 $update_count++; 2174 } 2175 2176 // Cleanup 2177 unset( $replies, $reply ); 2178 $endTime = microtime(true); 2179 2180 // If you did a limit's worth of updating replies and that amount is the same as menu_order=0 replies, 2181 // then check again to see if there are any more. If not, don't do that query, they're all done. 2182 $continue_menu_order_zero = ( $menu_order_zero_count === $update_count ) ? true : false; 2183 2184 // Let's make a new nonce for each pass through. Just for laughs. 2185 $iteration++; 2186 $new_nonce = wp_create_nonce( 'bbp-sync-all-reply-positions' . $iteration ); 2187 2188 $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'); 2189 echo json_encode( $data ); 2190 wp_die(); 2191 2192 //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' ) ) ); 2193 } 2194 2195 /** 2196 * Recalculate reply menu order 2197 * 2198 * @since bbPress (r5367) 2199 * 2200 * @uses wpdb::query() To run our recount sql queries 2201 * @uses is_wp_error() To check if the executed query returned {@link WP_Error} 2202 * @uses bbp_get_reply_post_type() To get the reply post type 2203 * @uses bbp_update_reply_position() To update the reply position 2204 * @return array An array of the status code and the message 2205 */ 2206 function bbp_admin_repair_reply_menu_order() { 2207 global $wpdb; 2208 2209 $statement = __( 'Recalculating reply menu order … %s', 'bbpress' ); 2210 $result = __( 'No reply positions to recalculate!', 'bbpress' ); 2211 2212 // Delete cases where `_bbp_reply_to` was accidentally set to itself 2213 if ( is_wp_error( $wpdb->query( "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_reply_to' AND `post_id` = `meta_value`;" ) ) ) { 2214 return array( 1, sprintf( $statement, $result ) ); 2215 } 2216 2217 // Post type 2218 $rpt = bbp_get_reply_post_type(); 2219 2092 2220 // 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`2221 $replies = $wpdb->get_results( "SELECT `a`.`ID` FROM `{$wpdb->posts}` AS `a` 2094 2222 INNER JOIN ( 2095 2223 SELECT `menu_order`, `post_parent` 2096 FROM `{$ bbp_db->posts}`2224 FROM `{$wpdb->posts}` 2097 2225 GROUP BY `menu_order`, `post_parent` 2098 2226 HAVING COUNT( * ) >1 2099 2227 )`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', 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