示例#1
0
    function admin_print_footer_scripts()
    {
        global $wpdb;
        ?>

<script type="text/javascript">
jQuery(document).ready(function($) {
	if ( $("form input[name^='search_index']").length ) {
		$('form p.submit').html('');
	}
});
</script>

<?php 
        if (!empty($_POST['search_index']['update_index'])) {
            $total_listings = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_content_filtered = '' AND post_type IN ('" . implode("', '", APP_Search_Index::get_registered_post_types()) . "')");
            $speed = isset($_POST['search_index']['speed']) ? $_POST['search_index']['speed'] : 'slow';
            $speed = self::get_index_speed($speed);
            ?>

<script type="text/javascript">
	jQuery(document).ready(function($){
		var si_total = <?php 
            echo $total_listings;
            ?>
;
		var si_speed = <?php 
            echo $speed;
            ?>
;
		var si_count = 1;
		var si_percent = 0;
		var si_nonce = "<?php 
            echo wp_create_nonce('build-index');
            ?>
";
		var si_timestart = new Date().getTime();
		var si_continue = true;

		// Create the progress bar
		$("#search-index-progress-bar").progressbar();
		$("#search-index-progress-bar-percent").html( "0%" );

		// Called after each indexing. Updates the progress bar.
		function appthemes_search_index_update_status( success, data ) {
			si_percent = ( si_count / si_total ) * 100;
			if ( si_percent > 100 ) {
				si_percent = 100;
			}
			$("#search-index-progress-bar").progressbar( "value", si_percent );
			$("#search-index-progress-bar-percent").html( Math.round( si_percent ) + "%" );
			si_count = si_count + si_speed;

			if ( ! success || data.completed == true ) {
				si_continue = false;
			}
		}

		// Called when all posts have been processed.
		function appthemes_search_index_finish( data ) {
			$("#search-index-message").html( data.message );
		}

		// Generate search index via AJAX
		function appthemes_search_index_process() {
			$.ajax({
				type: 'POST',
				url: ajaxurl,
				dataType: "json",
				data: {
					action: "build-search-index",
					speed: si_speed,
					nonce: si_nonce
				},
				success: function( data ) {
					if ( data.success == true ) {
						appthemes_search_index_update_status( true, data );
					} else {
						appthemes_search_index_update_status( false, data );
					}

					if ( si_continue ) {
						setTimeout( appthemes_search_index_process, 1000 );
					} else {
						appthemes_search_index_finish( data );
					}
				},
				error: function( data ) {
					appthemes_search_index_update_status( false, data );

					if ( si_continue ) {
						appthemes_search_index_process();
					} else {
						appthemes_search_index_finish( data );
					}
				}
			});
		}

		appthemes_search_index_process();
	});
</script>

<?php 
        }
    }