Skip to content

Commit

Permalink
Add search term.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdharmesh committed Oct 11, 2024
1 parent 32b40bf commit f142087
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
45 changes: 27 additions & 18 deletions includes/Classifai/Admin/SimilarTermsListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,34 @@ public function get_columns() {
*/
public function prepare_items() {
$per_page = $this->get_items_per_page( 'edit_post_per_page' );
$current = $this->get_pagenum();
$offset = ( $current - 1 ) * $per_page;
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$search = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : '';

Check warning on line 72 in includes/Classifai/Admin/SimilarTermsListTable.php

View workflow job for this annotation

GitHub Actions / vipcs

Processing form data without nonce verification.

Check warning on line 72 in includes/Classifai/Admin/SimilarTermsListTable.php

View workflow job for this annotation

GitHub Actions / vipcs

Processing form data without nonce verification.

Check warning on line 72 in includes/Classifai/Admin/SimilarTermsListTable.php

View workflow job for this annotation

GitHub Actions / test

WordPress.Security.NonceVerification.Recommended

Processing form data without nonce verification.

Check warning on line 72 in includes/Classifai/Admin/SimilarTermsListTable.php

View workflow job for this annotation

GitHub Actions / test

WordPress.Security.NonceVerification.Recommended

Processing form data without nonce verification.

$this->_column_headers = array( $columns, $hidden, $sortable );

$total = wp_count_terms(
[
'taxonomy' => $this->taxonomy,
'hide_empty' => false,
'meta_key' => 'classifai_similar_terms', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_compare' => 'EXISTS',
'search' => $search,
]
);

$this->set_pagination_args(
array(
'total_items' => $total, // WE have to calculate the total number of items.
'per_page' => $per_page, // WE have to determine how many items to show on a page.
'total_pages' => ceil( $total / $per_page ), // WE have to calculate the total number of pages.
)
);

$current = $this->get_pagenum();
$offset = ( $current - 1 ) * $per_page;

$terms = get_terms(
[
'taxonomy' => $this->taxonomy,
Expand All @@ -85,6 +105,7 @@ public function prepare_items() {
'meta_compare' => 'EXISTS',
'number' => $per_page,
'offset' => $offset,
'search' => $search,
]
);

Expand Down Expand Up @@ -116,23 +137,7 @@ public function prepare_items() {
}
}

$total = wp_count_terms(
[
'taxonomy' => $this->taxonomy,
'hide_empty' => false,
'meta_key' => 'classifai_similar_terms', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_compare' => 'EXISTS',
]
);

$this->items = $items;
$this->set_pagination_args(
array(
'total_items' => $total, // WE have to calculate the total number of items.
'per_page' => $per_page, // WE have to determine how many items to show on a page.
'total_pages' => ceil( $total / $per_page ), // WE have to calculate the total number of pages.
)
);
}

/**
Expand All @@ -149,6 +154,8 @@ public function generate_term_html( $term, $similar_term, $score = null ) {
'taxonomy' => $this->taxonomy,
'from' => $similar_term->term_id,
'to' => $term->term_id,
'paged' => $this->get_pagenum(),
's' => isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : false,

Check warning on line 158 in includes/Classifai/Admin/SimilarTermsListTable.php

View workflow job for this annotation

GitHub Actions / vipcs

Processing form data without nonce verification.

Check warning on line 158 in includes/Classifai/Admin/SimilarTermsListTable.php

View workflow job for this annotation

GitHub Actions / vipcs

Processing form data without nonce verification.

Check warning on line 158 in includes/Classifai/Admin/SimilarTermsListTable.php

View workflow job for this annotation

GitHub Actions / test

WordPress.Security.NonceVerification.Recommended

Processing form data without nonce verification.

Check warning on line 158 in includes/Classifai/Admin/SimilarTermsListTable.php

View workflow job for this annotation

GitHub Actions / test

WordPress.Security.NonceVerification.Recommended

Processing form data without nonce verification.
);
$merge_url = add_query_arg( $args, wp_nonce_url( admin_url( 'admin-post.php' ), 'classifai_merge_term' ) );
$score = $score ? ( $score > 1 ? $score - 1 : $score ) : '';
Expand Down Expand Up @@ -215,6 +222,8 @@ public function column_actions( $item ) {
'taxonomy' => $this->taxonomy,
'term' => $term->term_id,
'similar_term' => $similar_term->term_id,
'paged' => $this->get_pagenum(),
's' => isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : false,

Check warning on line 226 in includes/Classifai/Admin/SimilarTermsListTable.php

View workflow job for this annotation

GitHub Actions / vipcs

Processing form data without nonce verification.

Check warning on line 226 in includes/Classifai/Admin/SimilarTermsListTable.php

View workflow job for this annotation

GitHub Actions / vipcs

Processing form data without nonce verification.

Check warning on line 226 in includes/Classifai/Admin/SimilarTermsListTable.php

View workflow job for this annotation

GitHub Actions / test

WordPress.Security.NonceVerification.Recommended

Processing form data without nonce verification.

Check warning on line 226 in includes/Classifai/Admin/SimilarTermsListTable.php

View workflow job for this annotation

GitHub Actions / test

WordPress.Security.NonceVerification.Recommended

Processing form data without nonce verification.
);
$skip_url = add_query_arg( $args, wp_nonce_url( admin_url( 'admin-post.php' ), 'classifai_skip_similar_term' ) );

Expand Down
27 changes: 22 additions & 5 deletions includes/Classifai/Features/TermCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -988,10 +988,17 @@ public function render_similar_terms( $taxonomy ) {
printf( esc_html__( 'Similar %s', 'classifai' ), esc_html( $label ) );
?>
</h3>
<form id="import-history" method="get">
<input type="hidden" name="page" value="classifai-term-cleanup">
<input type="hidden" name="tax" value="<?php echo esc_attr( $taxonomy ); ?>">
<?php
$list_table = new SimilarTermsListTable( $taxonomy );
$list_table->prepare_items();
$list_table->search_box( esc_html__( 'Search', 'classifai' ), 'search-term' );
$list_table->display();
?>
</form>
<?php
$list_table = new SimilarTermsListTable( $taxonomy );
$list_table->prepare_items();
$list_table->display();
}
}

Expand Down Expand Up @@ -1060,7 +1067,12 @@ public function merge_term() {
$from = isset( $_GET['from'] ) ? absint( wp_unslash( $_GET['from'] ) ) : 0;
$to_term = get_term( $to, $taxonomy );
$from_term = get_term( $from, $taxonomy );
$redirect = add_query_arg( 'tax', $taxonomy, $this->setting_page_url );
$args = [
'tax' => $taxonomy,
's' => isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : false,
'paged' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : false,
];
$redirect = add_query_arg( $args, $this->setting_page_url );

if ( empty( $taxonomy ) || empty( $to ) || empty( $from ) ) {
$this->add_notice(
Expand Down Expand Up @@ -1127,7 +1139,12 @@ public function skip_similar_term() {
$taxonomy = isset( $_GET['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) : '';
$term = isset( $_GET['term'] ) ? absint( wp_unslash( $_GET['term'] ) ) : 0;
$similar_term = isset( $_GET['similar_term'] ) ? absint( wp_unslash( $_GET['similar_term'] ) ) : 0;
$redirect = add_query_arg( 'tax', $taxonomy, $this->setting_page_url );
$args = [
'tax' => $taxonomy,
's' => isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : false,
'paged' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : false,
];
$redirect = add_query_arg( $args, $this->setting_page_url );

// SKip/Ignore the similar term.
$term_meta = get_term_meta( $term, 'classifai_similar_terms', true );
Expand Down

0 comments on commit f142087

Please sign in to comment.