Passed
Push — 247-fix/delete-term-meta ( 0c7c80...e69629 )
by Rajan
07:33
created

DeleteTermMetaModule::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
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.1.0
12
 */
13
class DeleteTermMetaModule extends MetasModule {
14
	protected function initialize() {
15
		$this->field_slug    = 'meta_term';
16
		$this->meta_box_slug = 'bd-meta-term';
17
		$this->action        = 'delete_meta_term';
18
		$this->cron_hook     = 'do-bulk-delete-term-meta';
19
		$this->messages      = array(
20
			'box_label'  => __( 'Bulk Delete Term Meta', 'bulk-delete' ),
21
			'scheduled'  => __( 'Term meta fields from the posts with the selected criteria are scheduled for deletion.', 'bulk-delete' ),
22
			'cron_label' => __( 'Delete Term Meta', 'bulk-delete' ),
23
		);
24
	}
25
26
	/**
27
	 * Render the Modules.
28
	 *
29
	 * @return void
30
	 */
31
	public function render() {
32
		?>
33
		<!-- Term Meta box start-->
34
        <fieldset class="options">
35
<?php
36
		$taxonomies = $this->get_taxonomies();
37
?>
38
        <h4><?php _e( 'Select the taxonomy whose term meta fields you want to delete', 'bulk-delete' ); ?></h4>
39
        <table class="optiontable">
40
<?php
41
		foreach ( $taxonomies as $taxonomy ) {
42
?>
43
            <tr>
44
                <td>
45
                    <input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_taxonomy" value = "<?php echo $taxonomy; ?>" type = "radio" class = "smbd_<?php echo esc_attr( $this->field_slug ); ?>_taxonomy">
46
                    <label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_taxonomy"><?php echo $taxonomy; ?> </label>
47
                </td>
48
            </tr>
49
<?php
50
		}
51
?>
52
        </table>
53
54
        <h4><?php _e( 'Choose your term want to delete', 'bulk-delete' ); ?></h4>
55
        <table class="optiontable">
56
            <tr>
57
                <td>
58
                    <select class="select2 select2-terms" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_term">
59
                        <option>Choose Terms</option>
60
                    </select>
61
                </td>
62
            </tr>
63
        </table>
64
65
        <h4><?php _e( 'Choose your term meta want to delete', 'bulk-delete' ); ?></h4>
66
        <table class="optiontable">
67
            <tr>
68
                <td>
69
                    <select class="select2 select2-term-meta" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_term_meta">
70
                        <option>Choose Term Meta</option>
71
                    </select>
72
                </td>
73
            </tr>
74
        </table>
75
76
        <h4><?php _e( 'Term Meta Value', 'bulk-delete' ); ?></h4>
77
        <table class="optiontable">
78
            <tr>
79
                <td>
80
                    <input type="text" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_term_meta_value" />
81
                </td>
82
            </tr>
83
        </table>
84
<?php
85
		/**
86
		 * Add more fields to the delete post meta field form.
87
		 * This hook can be used to add more fields to the delete post meta field form.
88
		 *
89
		 * @since 5.4
90
		 */
91
		do_action( 'bd_delete_post_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
	protected function convert_user_input_to_options( $request, $options ) {
104
		$options['term'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_term', 'term' ) );
105
106
		$options['term_meta'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_meta', 'term_meta' );
107
		$options['term_meta_value']  = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_term_meta_value', '' ) );
108
109
		return $options;
110
	}
111
112
	public function delete( $options ) {
113
		$count = 10;
114
115
        if( delete_term_meta( $options['term'], $options['term_meta'] ) ){
116
            return 1;
117
        }
118
119
		return $count;
120
	}
121
122
	public function filter_js_array( $js_array ) {
123
		$js_array['dt_iterators'][]                 = '_' . $this->field_slug;
124
		$js_array['validators']['delete_meta_term'] = 'noValidation';
125
126
		$js_array['pre_action_msg']['delete_meta_term'] = 'deleteTMWarning';
127
		$js_array['msg']['deleteTMWarning']             = __( 'Are you sure you want to delete all the term meta fields?', 'bulk-delete' );
128
129
		return $js_array;
130
	}
131
132
	protected function get_success_message( $items_deleted ) {
133
		/* translators: 1 Number of posts deleted */
134
		return _n( 'Deleted %d term meta field', 'Deleted %d term meta field', $items_deleted, 'bulk-delete' );
135
	}
136
}
137