Ticket #2731: 2731.ajaxstart.diff
| File 2731.ajaxstart.diff, 8.6 KB (added by , 11 years ago) |
|---|
-
src/includes/admin/admin.php
585 585 break; 586 586 } 587 587 } 588 elseif( 'tools_page_bbp-repair' === get_current_screen()->base ){ 589 // Enqueue the tools JS (NOT the minified version for now, change later) 590 wp_enqueue_script( 'bbp-admin-tools-js', $this->js_url . 'tools.js', array( 'jquery' ), $version ); 591 } 588 592 } 589 593 590 594 /** -
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 $( '#bbp-sync-all-reply-positions' ).change( function(){ 7 8 if( $( this ).prop( 'checked' ) ){ 9 // here, disable the rest of the form here 10 } 11 }); 12 13 $( 'form.settings' ).submit( function( e ) { 14 15 if( $( '#bbp-sync-all-reply-positions' ).prop('checked') ){ 16 17 e.preventDefault(); 18 bbp_repair_reply_call(); 19 } 20 }); 21 22 function bbp_repair_reply_call(){ 23 24 var input = {}; 25 26 input = { 27 'action' : 'bbp_admin_repair_reply_menu_order' 28 } 29 30 console.log( input ); 31 32 $.ajax({ 33 type: 'POST', 34 url : ajaxurl, // this is already there, let's use it 35 data : input, 36 dataType : 'json', 37 success : function( reply ) { 38 if( reply.result === 'success') { 39 alert( reply.notice ); 40 //keep going keep going keep going 41 } 42 else{ 43 // be super sad 44 // $( '.nav-tab-wrapper' ).after( "<h1>" + reply.notice + "</h1>" ); 45 alert( reply.notice ); 46 location.reload(); 47 } 48 } 49 }); 50 } 51 52 53 }); 54 No newline at end of file -
src/includes/admin/tools.php
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 /** … … 1366 1369 * @uses bbp_update_reply_position() To update the reply position 1367 1370 * @return array An array of the status code and the message 1368 1371 */ 1369 function bbp_admin_repair_reply_menu_order() { 1372 1373 function bbp_admin_repair_reply_menu_order_callback() { 1374 1370 1375 global $wpdb; 1371 1376 1377 1372 1378 $statement = __( 'Recalculating reply menu order … %s', 'bbpress' ); 1373 1379 $result = __( 'No reply positions to recalculate!', 'bbpress' ); 1374 1380 1381 // if this is going to get calculated via an ajax call, it needs 1382 1383 1375 1384 // Delete cases where `_bbp_reply_to` was accidentally set to itself 1376 1385 if ( is_wp_error( $wpdb->query( "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_reply_to' AND `post_id` = `meta_value`;" ) ) ) { 1377 1386 return array( 1, sprintf( $statement, $result ) ); … … 1379 1388 1380 1389 // Post type 1381 1390 $rpt = bbp_get_reply_post_type(); 1391 $pst = bbp_get_pending_status_id(); 1382 1392 1383 // Get an array of reply id's to update the menu oder for each reply 1384 $replies = $wpdb->get_results( "SELECT `a`.`ID` FROM `{$wpdb->posts}` AS `a` 1393 // Set the menu_order of pending replies who have a post_date_gmt of 0 (posts that have been posted but never published) 1394 $wpdb->update('wp_posts', array('menu_order'=>'0'), array('post_type'=>$rpt, 'post_status'=>$pst, 'post_date_gmt'=>'0000-00-00 00:00:00') ); 1395 1396 // Post type 1397 $offset = 0; 1398 $limit = 500; 1399 $done = false; 1400 $replies_count = 0; 1401 1402 $queryTimes = array(); 1403 $updateTimes = array(); 1404 1405 // ready steady GO 1406 $startTime = microtime(true); 1407 1408 // first look through the menu_order 0 replies. Are there any in topics which aren't spammed or pending? Only topics that are.... 1409 $replies_mozero = $wpdb->get_results( "SELECT `a`.`post_parent` FROM `{$wpdb->posts}` AS `a` 1385 1410 INNER JOIN ( 1386 1411 SELECT `menu_order`, `post_parent` 1387 FROM `{$wpdb->posts}` 1412 FROM `{$wpdb->posts}` WHERE `post_type` = '{$rpt}' AND `menu_order` = 0 1388 1413 GROUP BY `menu_order`, `post_parent` 1389 1414 HAVING COUNT( * ) >1 1390 1415 )`b` 1391 1416 ON `a`.`menu_order` = `b`.`menu_order` 1392 1417 AND `a`.`post_parent` = `b`.`post_parent` 1393 WHERE `post_type` = '{$rpt}';", OBJECT_K ); 1418 1419 LIMIT {$limit}", OBJECT_K ); 1420 1421 while( !$done ){ 1422 // Get an array of reply id's to update the menu oder for each reply 1423 $startQueryTime = microtime(true); 1424 $replies = $wpdb->get_results( "SELECT `a`.`ID` FROM `{$wpdb->posts}` AS `a` 1425 INNER JOIN ( 1426 SELECT `menu_order`, `post_parent` 1427 FROM `{$wpdb->posts}` WHERE `post_type` = '{$rpt}' AND `menu_order` != 0 1428 GROUP BY `menu_order`, `post_parent` 1429 HAVING COUNT( * ) >1 1430 )`b` 1431 ON `a`.`menu_order` = `b`.`menu_order` 1432 AND `a`.`post_parent` = `b`.`post_parent` 1433 1434 LIMIT {$limit}", OBJECT_K ); 1394 1435 1395 // Bail if no replies returned 1396 if ( empty( $replies ) ) { 1397 return array( 1, sprintf( $statement, $result ) ); 1398 } 1436 $endQueryTime = microtime(true); 1437 1438 $queryTimes[] = ( $endQueryTime - $startQueryTime ); 1439 1440 if( $offset >= 2000 ){ 1441 $done = true; 1442 } 1399 1443 1400 // Recalculate the menu order position for each reply 1401 foreach ( $replies as $reply ) { 1402 bbp_update_reply_position( $reply->ID ); 1444 $replies_count += count( $replies ); 1445 $offset += $limit; 1446 1447 // Bail if no replies returned 1448 if ( $replies_count == 0 ) { 1449 1450 $data = array('notice' => 'no replies, so so sad' , 'result' => 'no_replies'); 1451 echo json_encode( $data ); 1452 wp_die(); 1453 1454 } 1455 1456 // Recalculate the menu order position for each reply 1457 foreach ( $replies as $reply ) { 1458 $startUpdateTime = microtime(true); 1459 bbp_update_reply_position( $reply->ID ); 1460 $endUpdateTime = microtime(true); 1461 $updateTimes[] = ( $endUpdateTime - $startUpdateTime ); 1462 1463 } 1403 1464 } 1404 1405 1465 // Cleanup 1406 1466 unset( $replies, $reply ); 1407 1467 $midTime = microtime(true); 1468 1469 $averageQueryTime = array_sum( $queryTimes ) / count( $queryTimes ); 1470 1471 $averageUpdateTime = array_sum( $updateTimes ) / count( $updateTimes ); 1472 $stdDevUpdateTime = stats_standard_deviation( $updateTimes ); 1473 1408 1474 // Flush the cache; things are about to get ugly. 1409 1475 wp_cache_flush(); 1476 $endTime = microtime(true); 1410 1477 1411 return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) ); 1478 $data = array('notice' => 'whoo hoo to you too! It took ' . ($endTime - $startTime) . ' seconds.', 'result' => 'success'); 1479 echo json_encode( $data ); 1480 wp_die(); 1481 1482 //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' ) ) ); 1412 1483 } 1413 1484 1485 /** 1486 * standard deviation thing from http://php.net/manual/en/function.stats-standard-deviation.php 1487 */ 1488 1489 if (!function_exists('stats_standard_deviation')) { 1490 /** 1491 * This user-land implementation follows the implementation quite strictly; 1492 * it does not attempt to improve the code or algorithm in any way. It will 1493 * raise a warning if you have fewer than 2 values in your array, just like 1494 * the extension does (although as an E_USER_WARNING, not E_WARNING). 1495 * 1496 * @param array $a 1497 * @param bool $sample [optional] Defaults to false 1498 * @return float|bool The standard deviation or false on error. 1499 */ 1500 function stats_standard_deviation(array $a, $sample = false) { 1501 $n = count($a); 1502 if ($n === 0) { 1503 trigger_error("The array has zero elements", E_USER_WARNING); 1504 return false; 1505 } 1506 if ($sample && $n === 1) { 1507 trigger_error("The array has only 1 element", E_USER_WARNING); 1508 return false; 1509 } 1510 $mean = array_sum($a) / $n; 1511 $carry = 0.0; 1512 foreach ($a as $val) { 1513 $d = ((double) $val) - $mean; 1514 $carry += $d * $d; 1515 }; 1516 if ($sample) { 1517 --$n; 1518 } 1519 return sqrt($carry / $n); 1520 } 1521 } 1522 1414 1523 /** Reset ********************************************************************/ 1415 1524 1416 1525 /**