Completed
Pull Request — dev/6.0.0 (#445)
by Rajan
17:14 queued 12:37
created

DeleteTermMetaModule::do_delete()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 4
nop 1
dl 0
loc 21
ccs 14
cts 14
cp 1
crap 7
rs 8.8333
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    = 'meta_term';
19 4
		$this->meta_box_slug = 'bd-meta-term';
20 4
		$this->action        = 'delete_meta_term';
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
			'cron_label' => __( 'Delete Term Meta', 'bulk-delete' ),
26
		);
27 4
	}
28
29
	/**
30
	 * Render the Modules.
31
	 */
32
	public function render() {
33
		?>
34
		<!-- Term Meta box start-->
35
		<fieldset class="options">
36
		<h4><?php _e( 'Select the taxonomy whose term meta fields you want to delete', 'bulk-delete' ); ?></h4>
37
		<table class="optiontable">
38
			<tr>
39
				<td>
40
					<?php $this->render_taxonomy_dropdown(); ?>
41
				</td>
42
			</tr>
43
		</table>
44
45
		<h4><?php _e( 'Choose your term want to delete', 'bulk-delete' ); ?></h4>
46
		<table class="optiontable">
47
			<tr>
48
				<td>
49
					<select class="enhanced-terms-dropdown" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_term">
50
						<option><?php _e( 'Choose Terms', 'bulk-delete' ); ?></option>
51
					</select>
52
				</td>
53
			</tr>
54
		</table>
55
56
		<h4><?php _e( 'Select the term meta that you want to delete', 'bulk-delete' ); ?></h4>
57
		<table class="optiontable">
58
			<tr>
59
				<td>
60
					<select class="enhanced-term-meta-dropdown" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_term_meta">
61
						<option><?php _e( 'Choose Term Meta', 'bulk-delete' ); ?></option>
62
					</select>
63
64
					<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_term_meta_option">
65
						<option value="equal"><?php _e( 'Equal to', 'bulk-delete' ); ?></option>
66
						<option value="not_equal"><?php _e( 'Not equal to', 'bulk-delete' ); ?></option>
67
					</select>
68
69
					<input type="text" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_term_meta_value" />
70
				</td>
71
			</tr>
72
		</table>
73
74
		<?php
75
		/**
76
		 * Add more fields to the delete term meta field form.
77
		 * This hook can be used to add more fields to the delete term meta field form.
78
		 *
79
		 * @since 6.0.0
80
		 */
81
		do_action( 'bd_delete_term_meta_form' );
82
		?>
83
84
		</fieldset>
85
86
		<?php $this->render_submit_button(); ?>
87
88
		<!-- Term Meta box end-->
89
		<?php
90
	}
91
92
	/**
93
	 * Convert user input to bulkwp standard.
94
	 *
95
	 * @param array $request Request array.
96
	 * @param array $options User options.
97
	 *
98
	 * @return array User options.
99
	 */
100
	protected function convert_user_input_to_options( $request, $options ) {
101
		$options['term'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_term', '' ) );
102
103
		$options['term_meta']       = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_meta', '' ) );
104
		$options['term_meta_value'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_meta_value', '' ) );
105
106
		$options['term_meta_option'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_meta_option', '' ) );
107
108
		return $options;
109
	}
110
111
	/**
112
	 * Delete action.
113
	 *
114
	 * @param array $options User options.
115
	 */
116 4
	public function do_delete( $options ) {
117 4
		$count = 0;
118
119 4
		if ( 'equal' === $options['term_meta_option'] ) {
120 2
			$is_delete = delete_term_meta( $options['term'], $options['term_meta'], $options['term_meta_value'] );
121 2
			if ( $is_delete ) {
122 2
				$count++;
123
			}
124 2
		} elseif ( 'not_equal' === $options['term_meta_option'] ) {
125 2
			$term_values = get_term_meta( $options['term'], $options['term_meta'] );
126 2
			foreach ( $term_values as $term_value ) {
127 2
				if ( $options['term_meta_value'] !== $term_value ) {
128 2
					$is_delete = delete_term_meta( $options['term'], $options['term_meta'], $term_value );
129 2
					if ( $is_delete ) {
130 2
						$count++;
131
					}
132
				}
133
			}
134
		}
135
136 4
		return $count;
137
	}
138
139
	/**
140
	 * Filter JS Array and add pro hooks.
141
	 *
142
	 * @param array $js_array JavaScript Array.
143
	 *
144
	 * @return array Modified JavaScript Array.
145
	 */
146
	public function filter_js_array( $js_array ) {
147
		$js_array['dt_iterators'][]                 = '_' . $this->field_slug;
148
		$js_array['validators']['delete_meta_term'] = 'noValidation';
149
150
		$js_array['pre_action_msg']['delete_meta_term'] = 'deleteTMWarning';
151
		$js_array['msg']['deleteTMWarning']             = __( 'Are you sure you want to delete all the term meta fields?', 'bulk-delete' );
152
153
		return $js_array;
154
	}
155
156
	/**
157
	 * Get Success Message.
158
	 *
159
	 * @param int $items_deleted Number of items that were deleted.
160
	 *
161
	 * @return string Success message.
162
	 */
163
	protected function get_success_message( $items_deleted ) {
164
		/* translators: 1 Number of posts deleted */
165
		return _n( 'Deleted %d term meta field', 'Deleted %d term meta field', $items_deleted, 'bulk-delete' );
166
	}
167
}
168