Completed
Push — 50-feature/delete-blogs-in-mul... ( ad0625...05ec87 )
by
unknown
42:17 queued 38:54
created

DeleteTermMetaModule   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 60
c 1
b 0
f 0
dl 0
loc 132
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
B do_delete() 0 21 7
A convert_user_input_to_options() 0 9 1
A append_to_js_array() 0 4 1
A initialize() 0 13 1
A render() 0 53 1
1
<?php
2
namespace BulkWP\BulkDelete\Core\Metas\Modules;
3
4
use BulkWP\BulkDelete\Core\Metas\MetasModule;
5
6
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
7
8
/**
9
 * Delete Term Meta.
10
 *
11
 * @since 6.1.0
12
 */
13
class DeleteTermMetaModule extends MetasModule {
14
	/**
15
	 * Initialize the Module.
16
	 */
17
	protected function initialize() {
18
		$this->field_slug    = 'term_meta';
19
		$this->meta_box_slug = 'bd-term-meta';
20
		$this->action        = 'delete_term_meta';
21
		$this->cron_hook     = 'do-bulk-delete-term-meta';
22
		$this->messages      = array(
23
			'box_label'        => __( 'Bulk Delete Term Meta', 'bulk-delete' ),
24
			'scheduled'        => __( 'Meta fields from the selected term with the selected criteria are scheduled for deletion.', 'bulk-delete' ),
25
			'confirm_deletion' => __( 'Are you sure you want to delete all the term meta fields that match the selected criteria?', 'bulk-delete' ),
26
			/* translators: 1 Number of term meta deleted */
27
			'deleted_one'      => __( 'Deleted %d term meta', 'bulk-delete' ),
28
			/* translators: 1 Number of term meta deleted */
29
			'deleted_multiple' => __( 'Deleted %d term metas', 'bulk-delete' ),
30
		);
31
	}
32
33
	public function render() {
34
		?>
35
		<!-- Term Meta box start-->
36
		<fieldset class="options">
37
		<h4><?php _e( 'Select the taxonomy from which you want to delete term meta fields', 'bulk-delete' ); ?></h4>
38
		<table class="optiontable">
39
			<tr>
40
				<td>
41
					<?php $this->render_taxonomy_dropdown(); ?>
42
				</td>
43
			</tr>
44
		</table>
45
46
		<h4><?php _e( 'Select the taxonomy term from which you want to delete term meta fields', 'bulk-delete' ); ?></h4>
47
		<table class="optiontable">
48
			<tr>
49
				<td>
50
					<select class="enhanced-terms-dropdown" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_term">
51
						<option value="0"><?php _e( 'Select Term', 'bulk-delete' ); ?></option>
52
					</select>
53
				</td>
54
			</tr>
55
		</table>
56
57
		<h4><?php _e( 'Select the term meta that you want to delete', 'bulk-delete' ); ?></h4>
58
		<table class="optiontable">
59
			<tr>
60
				<td>
61
					<select class="enhanced-term-meta-dropdown" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key">
62
						<option value="0"><?php _e( 'Select Term Meta Key', 'bulk-delete' ); ?></option>
63
					</select>
64
65
					<?php $this->render_string_operators_dropdown( 'string', array( '=', '!=' ) ); ?>
66
67
					<input type="text" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value" placeholder="<?php esc_attr_e( 'Term Meta Value', 'bulk-delete' ); ?>">
68
				</td>
69
			</tr>
70
		</table>
71
72
		<?php
73
		/**
74
		 * Add more fields to the delete term meta field form.
75
		 * This hook can be used to add more fields to the delete term meta field form.
76
		 *
77
		 * @since 6.1.0
78
		 */
79
		do_action( 'bd_delete_term_meta_form' );
80
		?>
81
82
		</fieldset>
83
84
		<?php $this->render_submit_button(); ?>
85
86
		<!-- Term Meta box end-->
87
		<?php
88
	}
89
90
	/**
91
	 * Convert user input to bulkwp standard.
92
	 *
93
	 * @param array $request Request array.
94
	 * @param array $options User options.
95
	 *
96
	 * @return array User options.
97
	 */
98
	protected function convert_user_input_to_options( $request, $options ) {
99
		$options['term_id'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_term', '' ) );
100
101
		$options['term_meta_key']   = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_key', '' ) );
102
		$options['term_meta_value'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_value', '' ) );
103
104
		$options['term_meta_operator'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator', '' ) );
105
106
		return $options;
107
	}
108
109
	public function do_delete( $options ) {
110
		$count = 0;
111
112
		if ( '=' === $options['term_meta_operator'] ) {
113
			$is_deleted = delete_term_meta( $options['term_id'], $options['term_meta_key'], $options['term_meta_value'] );
114
			if ( $is_deleted ) {
115
				$count++;
116
			}
117
		} elseif ( '!=' === $options['term_meta_operator'] ) {
118
			$term_values = get_term_meta( $options['term_id'], $options['term_meta_key'] );
119
			foreach ( $term_values as $term_value ) {
120
				if ( $options['term_meta_value'] !== $term_value ) {
121
					$is_deleted = delete_term_meta( $options['term_id'], $options['term_meta_key'], $term_value );
122
					if ( $is_deleted ) {
123
						$count++;
124
					}
125
				}
126
			}
127
		}
128
129
		return $count;
130
	}
131
132
	/**
133
	 * Append any module specific options to JS array.
134
	 *
135
	 * This function will be overridden by the child classes.
136
	 *
137
	 * @param array $js_array JavaScript Array.
138
	 *
139
	 * @return array Modified JavaScript Array
140
	 */
141
	public function append_to_js_array( $js_array ) {
142
		$js_array['validators'][ $this->action ] = 'noValidation';
143
144
		return $js_array;
145
	}
146
}
147