Completed
Pull Request — dev/6.0.0 (#445)
by Rajan
24:05 queued 20:54
created

DeleteTermMetaModule::get_success_message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
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
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
	protected function initialize() {
18
		$this->field_slug    = 'meta_term';
19
		$this->meta_box_slug = 'bd-meta-term';
20
		$this->action        = 'delete_meta_term';
21
		$this->cron_hook     = 'do-bulk-delete-term-meta';
22
		$this->messages      = array(
23
			'box_label'  => __( 'Bulk Delete Term Meta', 'bulk-delete' ),
24
			'scheduled'  => __( 'Term meta fields from the posts with the selected criteria are scheduled for deletion.', 'bulk-delete' ),
25
			'cron_label' => __( 'Delete Term Meta', 'bulk-delete' ),
26
		);
27
	}
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
102
		$options['term'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_term', '' ) );
103
104
		$options['term_meta']       = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_meta', '' ) );
105
		$options['term_meta_value'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_meta_value', '' ) );
106
107
		$options['term_meta_option'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_meta_option', '' ) );
108
109
		return $options;
110
	}
111
112
	/**
113
	 * Delete action.
114
	 *
115
	 * @param array $options User options.
116
	 */
117
	public function do_delete( $options ) {
118
		$count = 0;
119
120
		if ( 'equal' === $options['term_meta_option'] ) {
121
			$is_delete = delete_term_meta( $options['term'], $options['term_meta'], $options['term_meta_value'] );
122
			if ( $is_delete ) {
123
				$count++;
124
			}
125
		} elseif ( 'not_equal' === $options['term_meta_option'] ) {
126
			$term_values = get_term_meta( $options['term'], $options['term_meta'] );
127
			foreach ( $term_values as $term_value ) {
128
				if ( $options['term_meta_value'] !== $term_value ) {
129
					delete_term_meta( $options['term'], $options['term_meta'], $term_value );
130
					$count++;
131
				}
132
			}
133
		}
134
135
		return $count;
136
	}
137
138
	/**
139
	 * Filter JS Array and add pro hooks.
140
	 *
141
	 * @param array $js_array JavaScript Array.
142
	 *
143
	 * @return array Modified JavaScript Array.
144
	 */
145
	public function filter_js_array( $js_array ) {
146
		$js_array['dt_iterators'][]                 = '_' . $this->field_slug;
147
		$js_array['validators']['delete_meta_term'] = 'noValidation';
148
149
		$js_array['pre_action_msg']['delete_meta_term'] = 'deleteTMWarning';
150
		$js_array['msg']['deleteTMWarning']             = __( 'Are you sure you want to delete all the term meta fields?', 'bulk-delete' );
151
152
		return $js_array;
153
	}
154
155
	/**
156
	 * Get Success Message.
157
	 *
158
	 * @param int $items_deleted Number of items that were deleted.
159
	 *
160
	 * @return string Success message.
161
	 */
162
	protected function get_success_message( $items_deleted ) {
163
		/* translators: 1 Number of posts deleted */
164
		return _n( 'Deleted %d term meta field', 'Deleted %d term meta field', $items_deleted, 'bulk-delete' );
165
	}
166
}
167