0 ) {
$response['percentage'] = $processed / $total * 100;
} else {
$response['percentage'] = 0;
}
}
$response['feedback'] = sprintf(
// translators: Number of posts indexed on this go, total number of posts indexed so far, number of posts processed on this go, total number of posts to process.
_n(
'Indexed %1$d post (total %2$d), processed %3$d / %4$d.',
'Indexed %1$d posts (total %2$d), processed %3$d / %4$d.',
$indexing_response['indexed'],
'relevanssi'
),
$indexing_response['indexed'],
$completed,
$processed,
$total
) . "\n";
$response['offset'] = $offset;
echo wp_json_encode( $response );
wp_die();
}
/**
* Counts the posts to index.
*
* AJAX wrapper for relevanssi_count_total_posts().
*/
function relevanssi_count_posts_ajax_wrapper() {
$count = relevanssi_count_total_posts();
echo wp_json_encode( $count );
wp_die();
}
/**
* Counts the posts missing from the index.
*
* AJAX wrapper for relevanssi_count_missing_posts().
*/
function relevanssi_count_missing_posts_ajax_wrapper() {
$count = relevanssi_count_missing_posts();
echo wp_json_encode( $count );
wp_die();
}
/**
* Lists categories.
*
* AJAX wrapper for get_categories().
*/
function relevanssi_list_categories() {
$categories = get_categories(
array(
'taxonomy' => 'category',
'hide_empty' => false,
)
);
echo wp_json_encode( $categories );
wp_die();
}
/**
* Performs an admin search.
*
* Performs an admin dashboard search.
*
* @since 2.2.0
*/
function relevanssi_admin_search() {
check_ajax_referer( 'relevanssi_admin_search_nonce', 'security' );
$args = array();
if ( isset( $_POST['args'] ) ) {
parse_str( $_POST['args'], $args );
}
if ( isset( $_POST['posts_per_page'] ) ) {
$posts_per_page = intval( $_POST['posts_per_page'] );
if ( $posts_per_page > 0 ) {
$args['posts_per_page'] = $posts_per_page;
}
}
if ( isset( $_POST['post_types'] ) ) {
$post_type = $_POST['post_types'];
$args['post_types'] = $post_type;
}
if ( isset( $_POST['offset'] ) ) {
$offset = intval( $_POST['offset'] );
if ( $offset > 0 ) {
$args['offset'] = $offset;
}
}
if ( isset( $_POST['s'] ) ) {
$args['s'] = $_POST['s'];
}
$query = new WP_Query();
$query->parse_query( $args );
$query->set( 'relevanssi_admin_search', true );
$query = apply_filters( 'relevanssi_modify_wp_query', $query );
relevanssi_do_query( $query );
$results = relevanssi_admin_search_debugging_info( $query );
// Take the posts array and create a string out of it.
$offset = 0;
if ( isset( $query->query_vars['offset'] ) ) {
$offset = $query->query_vars['offset'];
}
$results .= relevanssi_admin_search_format_posts( $query->posts, $query->found_posts, $offset, $args['s'] );
echo wp_json_encode( $results );
wp_die();
}
/**
* Formats the posts for admin search.
*
* Results are presented as an ordered list of posts. The format is very basic, and
* can be modified with the 'relevanssi_admin_search_element' filter hook.
*
* @param array $posts The posts array.
* @param int $total The number of posts found in total.
* @param int $offset Offset value.
* @param string $query The search query.
*
* @return string The formatted posts.
*
* @since 2.2.0
*/
function relevanssi_admin_search_format_posts( $posts, $total, $offset, $query ) {
$result = '
' . __( 'Results', 'relevanssi' ) . '
';
$start = $offset + 1;
$end = $offset + count( $posts );
// Translators: %1$d is the total number of posts found, %2$d is the current search result count, %3$d is the offset.
$result .= '
' . sprintf( __( 'Found a total of %1$d posts, showing posts %2$d–%3$s.', 'relevanssi' ), $total, $start, '' . $end . '' ) . '
element. Feel free to edit the element any way you want to.
*
* @param string $post_element The post element.
*/
$result .= apply_filters( 'relevanssi_admin_search_element', $post_element );
if ( isset( $post->blog_id ) ) {
restore_current_blog();
}
}
$result .= '
';
return $result;
}
/**
* Shows debugging information about the search.
*
* Formats the WP_Query parameters, looks at some filter hooks and presents the
* information in an easy-to-read format.
*
* @param array $query The WP_Query object.
*
* @return string The formatted debugging information.
*
* @since 2.2.0
*/
function relevanssi_admin_search_debugging_info( $query ) {
$result = '
';
$result .= '
' . __( 'Query variables', 'relevanssi' ) . '
';
$result .= '
';
foreach ( $query->query_vars as $key => $value ) {
if ( 'tax_query' === $key ) {
$result .= '