1
|
|
|
<?php |
2
|
|
|
namespace BulkWP\BulkDelete\Core\Terms; |
3
|
|
|
|
4
|
|
|
use BulkWP\BulkDelete\Core\Base\BaseModule; |
5
|
|
|
|
6
|
1 |
|
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
|
36 |
|
protected function do_delete( $options ) { |
50
|
36 |
|
$term_ids_to_delete = $this->get_term_ids_to_delete( $options ); |
51
|
|
|
|
52
|
36 |
|
if ( $term_ids_to_delete <= 0 ) { |
53
|
|
|
// Short circuit deletion, if nothing needs to be deleted. |
54
|
|
|
return 0; |
55
|
|
|
} |
56
|
|
|
|
57
|
36 |
|
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
|
36 |
|
protected function delete_terms_by_id( $term_ids, $options ) { |
69
|
36 |
|
$count = 0; |
70
|
|
|
|
71
|
36 |
|
foreach ( $term_ids as $term_id ) { |
72
|
28 |
|
if ( wp_delete_term( $term_id, $options['taxonomy'] ) ) { |
73
|
27 |
|
$count ++; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
36 |
|
return $count; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Query terms using WP_Term_Query. |
82
|
|
|
* |
83
|
|
|
* @param array $query Query args. |
84
|
|
|
* |
85
|
|
|
* @return array List of terms. |
86
|
|
|
*/ |
87
|
35 |
|
protected function query_terms( $query ) { |
88
|
|
|
$defaults = array( |
89
|
35 |
|
'fields' => 'ids', // retrieve only ids. |
90
|
|
|
'hide_empty' => false, |
91
|
|
|
'count' => false, |
92
|
|
|
'update_term_meta_cache' => false, |
93
|
|
|
); |
94
|
|
|
|
95
|
35 |
|
$query = wp_parse_args( $query, $defaults ); |
96
|
|
|
|
97
|
35 |
|
$term_query = new \WP_Term_Query(); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* This action runs before the query happens. |
101
|
|
|
* |
102
|
|
|
* @since 6.0.0 |
103
|
|
|
* |
104
|
|
|
* @param \WP_Term_Query $term_query Query object. |
105
|
|
|
*/ |
106
|
35 |
|
do_action( 'bd_before_query', $term_query ); |
107
|
|
|
|
108
|
35 |
|
$terms = $term_query->query( $query ); |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* This action runs after the query happens. |
112
|
|
|
* |
113
|
|
|
* @since 6.0.0 |
114
|
|
|
* |
115
|
|
|
* @param \WP_Term_Query $term_query Query object. |
116
|
|
|
*/ |
117
|
35 |
|
do_action( 'bd_after_query', $term_query ); |
118
|
|
|
|
119
|
35 |
|
return $terms; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|