| 1 |
<?php |
| 2 |
namespace BulkWP\BulkDelete\Core\Terms; |
| 3 |
|
| 4 |
use BulkWP\BulkDelete\Core\Base\BaseModule; |
| 5 |
|
| 6 |
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 7 |
|
| 8 |
/** |
| 9 |
* Module for deleting terms. |
| 10 |
* |
| 11 |
* @since 6.0.0 |
| 12 |
*/ |
| 13 |
abstract class TermsModule extends BaseModule { |
| 14 |
/** |
| 15 |
* Get the list of terms ids that need to be deleted. |
| 16 |
* |
| 17 |
* Return an empty query array to short-circuit deletion. |
| 18 |
* |
| 19 |
* @param array $options Delete options. |
| 20 |
* |
| 21 |
* @return int[] List of term ids to delete. |
| 22 |
*/ |
| 23 |
abstract protected function get_term_ids_to_delete( $options ); |
| 24 |
|
| 25 |
protected $item_type = 'terms'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Handle common filters. |
| 29 |
* |
| 30 |
* @param array $request Request array. |
| 31 |
* |
| 32 |
* @return array User options. |
| 33 |
*/ |
| 34 |
protected function parse_common_filters( $request ) { |
| 35 |
$options = array(); |
| 36 |
|
| 37 |
$options['taxonomy'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_taxonomy' ) ); |
| 38 |
|
| 39 |
return $options; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Perform the deletion. |
| 44 |
* |
| 45 |
* @param array $options Array of Delete options. |
| 46 |
* |
| 47 |
* @return int Number of items that were deleted. |
| 48 |
*/ |
| 49 |
protected function do_delete( $options ) { |
| 50 |
$term_ids_to_delete = $this->get_term_ids_to_delete( $options ); |
| 51 |
|
| 52 |
if ( $term_ids_to_delete <= 0 ) { |
| 53 |
// Short circuit deletion, if nothing needs to be deleted. |
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
return $this->delete_terms_by_id( $term_ids_to_delete, $options ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Delete terms by ids. |
| 62 |
* |
| 63 |
* @param int[] $term_ids List of term ids to delete. |
| 64 |
* @param array $options User options. |
| 65 |
* |
| 66 |
* @return int Number of terms deleted. |
| 67 |
*/ |
| 68 |
protected function delete_terms_by_id( $term_ids, $options ) { |
| 69 |
$count = 0; |
| 70 |
|
| 71 |
foreach ( $term_ids as $term_id ) { |
| 72 |
if ( wp_delete_term( $term_id, $options['taxonomy'] ) ) { |
| 73 |
$count ++; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return $count; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get all terms from a taxonomy. |
| 82 |
* |
| 83 |
* @param string $taxonomy Taxonomy name. |
| 84 |
* |
| 85 |
* @return \WP_Term[] List of terms. |
| 86 |
*/ |
| 87 |
protected function get_all_terms( $taxonomy ) { |
| 88 |
$args = array( |
| 89 |
'taxonomy' => $taxonomy, |
| 90 |
'fields' => 'all', |
| 91 |
); |
| 92 |
|
| 93 |
return $this->query_terms( $args ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Query terms using WP_Term_Query. |
| 98 |
* |
| 99 |
* @param array $query Query args. |
| 100 |
* |
| 101 |
* @return array List of terms. |
| 102 |
*/ |
| 103 |
protected function query_terms( $query ) { |
| 104 |
$defaults = array( |
| 105 |
'fields' => 'ids', // retrieve only ids. |
| 106 |
'hide_empty' => false, |
| 107 |
'count' => false, |
| 108 |
'update_term_meta_cache' => false, |
| 109 |
); |
| 110 |
|
| 111 |
$query = wp_parse_args( $query, $defaults ); |
| 112 |
|
| 113 |
$term_query = new \WP_Term_Query(); |
| 114 |
|
| 115 |
/** |
| 116 |
* This action runs before the query happens. |
| 117 |
* |
| 118 |
* @since 6.0.0 |
| 119 |
* |
| 120 |
* @param \WP_Term_Query $term_query Query object. |
| 121 |
*/ |
| 122 |
do_action( 'bd_before_query', $term_query ); //phpcs:ignore |
| 123 |
|
| 124 |
$terms = $term_query->query( $query ); |
| 125 |
|
| 126 |
/** |
| 127 |
* This action runs after the query happens. |
| 128 |
* |
| 129 |
* @since 6.0.0 |
| 130 |
* |
| 131 |
* @param \WP_Term_Query $term_query Query object. |
| 132 |
*/ |
| 133 |
do_action( 'bd_after_query', $term_query ); //phpcs:ignore |
| 134 |
|
| 135 |
return $terms; |
| 136 |
} |
| 137 |
|
| 138 |
protected function get_success_message( $items_deleted ) { |
| 139 |
/* translators: 1 Number of terms deleted */ |
| 140 |
return _n( 'Deleted %d term with the selected options', 'Deleted %d terms with the selected options', $items_deleted, 'bulk-delete' ); |
| 141 |
} |
| 142 |
} |
| 143 |
|