PluginProbe
Bulk Delete / trunk
Bulk Delete vtrunk
6.12 trunk 0.2 0.3 0.4 0.6 0.7 0.8 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.2.1 2.2.2 3.0 3.1 All 64 releases
bulk-delete / include / Core / Metas / Modules / DeletePostMetaModule.php

DeletePostMetaModule.php in Bulk Delete trunk, at include/Core/Metas/Modules/DeletePostMetaModule.php

194 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Post Meta.
10 *
11 * @since 6.0.0
12 */
13 class DeletePostMetaModule extends MetasModule {
14 protected function initialize() {
15 $this->field_slug = 'pm'; // Ideally it should be `meta_post`. But we are keeping it as pm for backward compatibility.
16 $this->meta_box_slug = 'bd-post-meta';
17 $this->action = 'delete_post_meta';
18 $this->cron_hook = 'do-bulk-delete-post-meta';
19 $this->scheduler_url = 1;
20 $this->messages = array(
21 'box_label' => __( 'Bulk Delete Post Meta', 'bulk-delete' ),
22 'scheduled' => __( 'Post meta fields from the posts with the selected criteria are scheduled for deletion.', 'bulk-delete' ),
23 'cron_label' => __( 'Delete Post Meta', 'bulk-delete' ),
24 );
25 }
26
27 /**
28 * Render the Modules.
29 *
30 * @return void
31 */
32 public function render() {
33 ?>
34 <!-- Post Meta box start-->
35 <fieldset class="options">
36 <h4><?php esc_html_e( 'Select the post type whose post meta fields you want to delete', 'bulk-delete' ); ?></h4>
37
38 <table class="optiontable">
39 <?php $this->render_post_type_with_status( false ); ?>
40 </table>
41
42 <h4><?php esc_html_e( 'Choose your post meta field settings', 'bulk-delete' ); ?></h4>
43 <table class="optiontable">
44 <tr>
45 <td>
46 <input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="use_key" type="radio" checked>
47 <label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo esc_html__( 'Delete based on post meta key name only', 'bulk-delete' ); ?></label>
48 </td>
49 </tr>
50
51 <tr>
52 <td>
53 <input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smdb_<?php echo esc_attr( $this->field_slug ); ?>_use_key_compare" value="use_key_compare" type="radio" disabled>
54 <label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo esc_html__( 'Delete based on post meta key name prefix or postfix', 'bulk-delete' ); ?></label>
55 <span class="bd-pm-pro" style="color:red; vertical-align: middle;">
56 <span class="open-upsell pro-feature-inline" data-pro-feature="delete-post-meta">Available in PRO</span>
57
58 </span>
59 </td>
60 </tr>
61
62 <tr>
63 <td>
64 <input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="use_value" type="radio" disabled>
65 <label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo esc_html__( 'Delete based on post meta key name and value', 'bulk-delete' ); ?></label>
66 <span class="bd-pm-pro" style="color:red; vertical-align: middle;">
67 <span class="open-upsell pro-feature-inline" data-pro-feature="delete-post-meta">Available in PRO</span>
68 </span>
69 </td>
70 </tr>
71
72 <tr>
73 <td>
74 <label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key"><?php esc_html_e( 'Post Meta Key ', 'bulk-delete' ); ?></label>
75 <select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key_prefix_postfix" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key_prefix_postfix" style="display: none;">
76 <option value="starts_with">starts with</option>
77 <option value="contains">contains</option>
78 <option value="ends_with">ends with</option>
79 </select>
80 <input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key" placeholder="<?php esc_html_e( 'Meta Key', 'bulk-delete' ); ?>">
81 </td>
82 </tr>
83 </table>
84
85 <?php
86 /**
87 * Add more fields to the delete post meta field form.
88 * This hook can be used to add more fields to the delete post meta field form.
89 *
90 * @since 5.4
91 */
92 do_action( 'bd_delete_post_meta_form' ); //phpcs:ignore
93 ?>
94
95 <table class="optiontable">
96 <tr>
97 <td colspan="2">
98 <h4><?php esc_html_e( 'Choose your deletion options', 'bulk-delete' ); ?></h4>
99 </td>
100 </tr>
101
102 <?php $this->render_restrict_settings(); ?>
103 <?php $this->render_limit_settings(); ?>
104 <?php $this->render_cron_settings(); ?>
105
106 </table>
107 </fieldset>
108
109 <?php $this->render_submit_button(); ?>
110
111 <!-- Post Meta box end-->
112 <?php
113 }
114
115 protected function convert_user_input_to_options( $request, $options ) {
116 $options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug ) );
117
118 $options['use_value'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_use_value', 'use_key' );
119 $options['meta_key'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_key', '' ) );
120
121 /**
122 * Delete post-meta delete options filter.
123 *
124 * This filter is for processing filtering options for deleting post meta.
125 *
126 * @since 5.4
127 */
128 return apply_filters( 'bd_delete_post_meta_options', $options, $request ); //phpcs:ignore
129 }
130
131 protected function do_delete( $options ) {
132 $args = $this->get_post_type_and_status_args( $options['post_type'] );
133
134 if ( $options['limit_to'] > 0 ) {
135 $args['number'] = $options['limit_to'];
136 } else {
137 $args['nopaging'] = 'true';
138 }
139
140 if ( $options['restrict'] ) {
141 $args['date_query'] = array(
142 array(
143 'column' => 'post_date',
144 $options['date_op'] => "{$options['days']} day ago",
145 ),
146 );
147 }
148
149 if ( 'use_key' === $options['use_value'] ) {
150 $options['meta_key'] = $options['meta_key'];
151 } else {
152 $options['meta_query'] = apply_filters( 'bd_delete_post_meta_query', array(), $options ); //phpcs:ignore
153 }
154
155 $metas_deleted = 0;
156
157 $post_ids = bd_query( $args );
158 foreach ( $post_ids as $post_id ) {
159 if ( isset( $options['meta_key'] ) && is_array( $options['meta_key'] ) ) {
160 $is_post_id_counted = false;
161 foreach ( $options['meta_key'] as $meta_key ) {
162 if ( delete_post_meta( $post_id, $meta_key ) ) {
163 if ( $is_post_id_counted ) {
164 continue;
165 }
166 $metas_deleted++;
167 $is_post_id_counted = true;
168 }
169 }
170 } else {
171 if ( delete_post_meta( $post_id, $options['meta_key'] ) ) {
172 $metas_deleted++;
173 }
174 }
175 }
176
177 return $metas_deleted;
178 }
179
180 protected function append_to_js_array( $js_array ) {
181 $js_array['validators'][ $this->action ] = 'noValidation';
182
183 $js_array['pre_action_msg'][ $this->action ] = 'deletePMWarning';
184 $js_array['msg']['deletePMWarning'] = __( 'Are you sure you want to delete all the post meta fields that match the selected filters?', 'bulk-delete' );
185
186 return $js_array;
187 }
188
189 protected function get_success_message( $items_deleted ) {
190 /* translators: 1 Number of posts deleted */
191 return _n( 'Deleted post meta field from %d post', 'Deleted post meta field from %d posts', $items_deleted, 'bulk-delete' );
192 }
193 }
194