Passed
Pull Request — dev/6.0.0 (#445)
by Rajan
20:39 queued 11:49
created

DeleteTermMetaModule::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 66
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 66
ccs 0
cts 30
cp 0
rs 9.28
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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