Ticket #2731: 2731.3.diff
File 2731.3.diff, 10.5 KB (added by , 9 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 function bbp_repair_reply_call(){ 7 var input = {}; 8 input = { 9 'action' : 'bbp_admin_repair_reply_menu_order' 10 }; 11 12 jQuery.ajax({ 13 type: 'POST', 14 url : ajaxurl, // this is already there, let's use it 15 data : input, 16 dataType : 'json', 17 success : function( reply ) { 18 if( reply.result === 'keep-going') { 19 alert( reply.notice ); 20 jQuery( '#the-list a#bbp-sync-all-reply-positions' ).click(); 21 } 22 else{ 23 // be super sad 24 // jQuery( '.nav-tab-wrapper' ).after( "<h1>" + reply.notice + "</h1>" ); 25 alert( reply.notice ); 26 location.reload(); 27 } 28 } 29 }); 30 } 31 32 jQuery( '#the-list a#bbp-sync-all-reply-positions' ).on( 'click', function(e){ 33 e.preventDefault(); 34 bbp_repair_reply_call(); 35 }); 36 37 38 }); -
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' ), '' ); ?>" ><?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' ), '' ); ?>" id="<?php printf( esc_attr( '%s' ), $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 2079 2083 // Define variables 2080 2084 $bbp_db = bbp_db(); … … 2088 2092 2089 2093 // Post type 2090 2094 $rpt = bbp_get_reply_post_type(); 2095 $pst = bbp_get_pending_status_id(); 2091 2096 2092 // 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` 2094 INNER JOIN ( 2095 SELECT `menu_order`, `post_parent` 2096 FROM `{$bbp_db->posts}` 2097 GROUP BY `menu_order`, `post_parent` 2098 HAVING COUNT( * ) >1 2099 )`b` 2100 ON `a`.`menu_order` = `b`.`menu_order` 2101 AND `a`.`post_parent` = `b`.`post_parent` 2102 WHERE `post_type` = '{$rpt}';", OBJECT_K ); 2097 // Set the menu_order of pending replies who have a post_date_gmt of 0 (posts that have been posted but never published) 2098 $wpdb->update('wp_posts', array('menu_order'=>'0'), array('post_type'=>$rpt, 'post_status'=>$pst, 'post_date_gmt'=>'0000-00-00 00:00:00') ); 2103 2099 2104 // Bail if no replies returned 2105 if ( empty( $replies ) ) { 2106 return array( 1, sprintf( $statement, $result ) ); 2107 } 2100 // Post type 2101 $offset = 0; 2102 $limit = 500; 2103 $not_done = false; // we'll find out in a moment that this is wrong. Could be better named. 2104 $replies_count = 0; 2108 2105 2109 // Recalculate the menu order position for each reply 2110 foreach ( $replies as $reply ) { 2111 bbp_update_reply_position( $reply->ID ); 2112 } 2106 $queryTimes = array(); 2107 $updateTimes = array(); 2113 2108 2114 // Cleanup 2115 unset( $replies, $reply);2109 // ready steady GO 2110 $startTime = microtime(true); 2116 2111 2117 // Flush the cache; things are about to get ugly. 2118 wp_cache_flush(); 2112 // first look through the menu_order 0 replies. Are there any in topics which aren't spammed or pending? Only topics that are.... 2113 $replies_mozero = $wpdb->get_results( "SELECT `a`.`post_parent` FROM `{$wpdb->posts}` AS `a` 2114 INNER JOIN ( 2115 SELECT `menu_order`, `post_parent` 2116 FROM `{$wpdb->posts}` WHERE `post_type` = '{$rpt}' AND `menu_order` = 0 2117 GROUP BY `menu_order`, `post_parent` 2118 HAVING COUNT( * ) >1 2119 )`b` 2120 ON `a`.`menu_order` = `b`.`menu_order` 2121 AND `a`.`post_parent` = `b`.`post_parent` 2119 2122 2120 return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) ); 2121 } 2123 LIMIT {$limit}", OBJECT_K ); 2122 2124 2125 while( !$not_done ){ 2126 // Get an array of reply id's to update the menu oder for each reply 2127 $startQueryTime = microtime(true); 2128 $replies = $wpdb->get_results( "SELECT `a`.`ID` FROM `{$wpdb->posts}` AS `a` 2129 INNER JOIN ( 2130 SELECT `menu_order`, `post_parent` 2131 FROM `{$wpdb->posts}` WHERE `post_type` = '{$rpt}' AND `menu_order` != 0 2132 GROUP BY `menu_order`, `post_parent` 2133 HAVING COUNT( * ) >1 2134 )`b` 2135 ON `a`.`menu_order` = `b`.`menu_order` 2136 AND `a`.`post_parent` = `b`.`post_parent` 2137 2138 LIMIT {$limit}", OBJECT_K ); 2139 2140 $endQueryTime = microtime(true); 2141 2142 $queryTimes[] = ( $endQueryTime - $startQueryTime ); 2143 2144 // this is not handling more than 500 at a time 2145 2146 2147 $replies_count += count( $replies ); 2148 $offset += $limit; 2149 2150 // Bail if no replies returned 2151 if ( $replies_count == 0 ) { 2152 // Flush the cache; things are about to get ugly. 2153 wp_cache_flush(); 2154 $data = array('notice' => 'all done all the way' , 'result' => 'all_done'); 2155 echo json_encode( $data ); 2156 wp_die(); 2157 2158 } 2159 2160 // Recalculate the menu order position for each reply 2161 foreach ( $replies as $reply ) { 2162 $startUpdateTime = microtime(true); 2163 bbp_update_reply_position( $reply->ID ); 2164 $endUpdateTime = microtime(true); 2165 $updateTimes[] = ( $endUpdateTime - $startUpdateTime ); 2166 2167 } 2168 2169 $not_done = true; 2170 } 2171 // Cleanup 2172 unset( $replies, $reply ); 2173 $midTime = microtime(true); 2174 2175 $averageQueryTime = array_sum( $queryTimes ) / count( $queryTimes ); 2176 2177 $averageUpdateTime = array_sum( $updateTimes ) / count( $updateTimes ); 2178 $stdDevUpdateTime = stats_standard_deviation( $updateTimes ); 2179 2180 $endTime = microtime(true); 2181 2182 $data = array('notice' => 'still more to do ' . ($endTime - $startTime) . ' seconds.', 'result' => 'keep-going'); 2183 echo json_encode( $data ); 2184 wp_die(); 2185 2186 //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' ) ) ); 2187 } 2188 2189 /** 2190 * standard deviation thing from http://php.net/manual/en/function.stats-standard-deviation.php 2191 */ 2192 2193 if (!function_exists('stats_standard_deviation')) { 2194 /** 2195 * This user-land implementation follows the implementation quite strictly; 2196 * it does not attempt to improve the code or algorithm in any way. It will 2197 * raise a warning if you have fewer than 2 values in your array, just like 2198 * the extension does (although as an E_USER_WARNING, not E_WARNING). 2199 * 2200 * @param array $a 2201 * @param bool $sample [optional] Defaults to false 2202 * @return float|bool The standard deviation or false on error. 2203 */ 2204 function stats_standard_deviation(array $a, $sample = false) { 2205 $n = count($a); 2206 if ($n === 0) { 2207 trigger_error("The array has zero elements", E_USER_WARNING); 2208 return false; 2209 } 2210 if ($sample && $n === 1) { 2211 trigger_error("The array has only 1 element", E_USER_WARNING); 2212 return false; 2213 } 2214 $mean = array_sum($a) / $n; 2215 $carry = 0.0; 2216 foreach ($a as $val) { 2217 $d = ((double) $val) - $mean; 2218 $carry += $d * $d; 2219 }; 2220 if ($sample) { 2221 --$n; 2222 } 2223 return sqrt($carry / $n); 2224 } 2225 } 2226 2123 2227 /** Reset ********************************************************************/ 2124 2228 2125 2229 /** -
src/includes/common/functions.php
1682 1682 // The ID of the cached query 1683 1683 $cache_id = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids'; 1684 1684 1685 wp_cache_flush(); 1685 1686 // Check for cache and set if needed 1686 1687 $child_ids = wp_cache_get( $cache_id, 'bbpress_posts' ); 1687 1688 if ( false === $child_ids ) { … … 1715 1716 // Join post statuses together 1716 1717 $post_status = "'" . implode( "', '", $post_status ) . "'"; 1717 1718 $bbp_db = bbp_db(); 1718 $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 );1719 $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 post_date DESC;", $parent_id, $post_type ); 1719 1720 $child_ids = (array) $bbp_db->get_col( $query ); 1720 1721 1721 1722 wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );