Passed
Push — 247-fix/delete-term-meta ( 132475...02ff37 )
by
unknown
05:23
created

DeleteTermMetaModule::append_to_js_array()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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