← All changes
|
integrations/wordpress/actions/delete-multiple-posts.php
+365
-0
5.8.1
→
6.0.3
View file →
| @@ -1,0 +1,365 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Delete Multiple Posts | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\WordPress\Actions\Delete_Multiple_Posts | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_WordPress_Delete_Multiple_Posts extends AutomatorWP_Integration_Action { | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * Initialize the trigger | |
| 16 | + * | |
| 17 | + * @since 1.0.0 | |
| 18 | + */ | |
| 19 | + public function __construct( $integration ) { | |
| 20 | + | |
| 21 | + $this->integration = $integration; | |
| 22 | + $this->action = $integration . '_delete_multiple_posts'; | |
| 23 | + | |
| 24 | + parent::__construct(); | |
| 25 | + | |
| 26 | + } | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * The post field conditions | |
| 30 | + * | |
| 31 | + * @since 1.0.0 | |
| 32 | + * | |
| 33 | + * @var array $field_conditions | |
| 34 | + */ | |
| 35 | + public $field_conditions = array(); | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * The post meta conditions | |
| 39 | + * | |
| 40 | + * @since 1.0.0 | |
| 41 | + * | |
| 42 | + * @var array $meta_conditions | |
| 43 | + */ | |
| 44 | + public $meta_conditions = array(); | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * The post meta | |
| 48 | + * | |
| 49 | + * @since 1.0.0 | |
| 50 | + * | |
| 51 | + * @var array $post_meta | |
| 52 | + */ | |
| 53 | + public $post_meta = array(); | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Store the action result | |
| 57 | + * | |
| 58 | + * @since 1.0.0 | |
| 59 | + * | |
| 60 | + * @var string $result | |
| 61 | + */ | |
| 62 | + public $result = ''; | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Register the trigger | |
| 66 | + * | |
| 67 | + * @since 1.0.0 | |
| 68 | + */ | |
| 69 | + public function register() { | |
| 70 | + | |
| 71 | + $post_type_options = array(); | |
| 72 | + | |
| 73 | + foreach( get_post_types( array(), 'objects' ) as $post_type ) { | |
| 74 | + /* translators: %1$s: Post type key (post, page). %2$s: Post type name (Post, Page). */ | |
| 75 | + $post_type_options[] = sprintf( __( '<code>%1$s</code> for %2$s', 'automatorwp' ), $post_type->name, $post_type->labels->name ); | |
| 76 | + } | |
| 77 | + | |
| 78 | + $post_status_options = array(); | |
| 79 | + | |
| 80 | + foreach( get_post_statuses() as $post_status => $post_status_label ) { | |
| 81 | + /* translators: %1$s: Post status key (draft, pending). %2$s: Post status label (Draft, Pending Review). */ | |
| 82 | + $post_status_options[] = sprintf( __( '<code>%1$s</code> for %2$s', 'automatorwp' ), $post_status, $post_status_label ); | |
| 83 | + } | |
| 84 | + | |
| 85 | + automatorwp_register_action( $this->action, array( | |
| 86 | + 'integration' => $this->integration, | |
| 87 | + 'label' => __( 'Delete multiple posts', 'automatorwp' ), | |
| 88 | + 'select_option' => __( 'Delete <strong>multiple posts</strong>', 'automatorwp' ), | |
| 89 | + /* translators: %1$s: Multiple Posts. */ | |
| 90 | + 'edit_label' => sprintf( __( 'Delete %1$s', 'automatorwp' ), '{conditions}' ), | |
| 91 | + /* translators: %1$s: Multiple Posts. */ | |
| 92 | + 'log_label' => sprintf( __( 'Delete %1$s', 'automatorwp' ), '{conditions}' ), | |
| 93 | + 'options' => array( | |
| 94 | + 'conditions' => array( | |
| 95 | + 'default' => __( 'multiple posts', 'automatorwp' ), | |
| 96 | + 'fields' => array( | |
| 97 | + 'post_field_conditions' => array( | |
| 98 | + 'name' => __( 'Field Conditions:', 'automatorwp' ), | |
| 99 | + 'desc' => __( 'Set conditions to filter posts to update by post fields.', 'automatorwp' ), | |
| 100 | + 'type' => 'group', | |
| 101 | + 'classes' => 'automatorwp-fields-table', | |
| 102 | + 'options' => array( | |
| 103 | + 'add_button' => __( 'Add condition', 'automatorwp' ), | |
| 104 | + 'remove_button' => '<span class="dashicons dashicons-no-alt"></span>', | |
| 105 | + ), | |
| 106 | + 'fields' => array( | |
| 107 | + 'field' => array( | |
| 108 | + 'name' => __( 'Field:', 'automatorwp' ), | |
| 109 | + 'type' => 'select', | |
| 110 | + 'options_cb' => 'automatorwp_options_cb_post_fields', | |
| 111 | + 'option_none' => true, | |
| 112 | + 'option_none_value' => '', | |
| 113 | + 'option_none_label' => __( 'Choose a field', 'automatorwp' ), | |
| 114 | + 'default' => '' | |
| 115 | + ), | |
| 116 | + 'condition' => automatorwp_utilities_condition_field(), | |
| 117 | + 'value' => array( | |
| 118 | + 'name' => __( 'Value:', 'automatorwp' ), | |
| 119 | + 'type' => 'text', | |
| 120 | + 'default' => '' | |
| 121 | + ), | |
| 122 | + ), | |
| 123 | + ), | |
| 124 | + 'post_meta_conditions' => array( | |
| 125 | + 'name' => __( 'Meta Conditions:', 'automatorwp' ), | |
| 126 | + 'desc' => __( 'Set conditions to filter posts to update by post metas.', 'automatorwp' ), | |
| 127 | + 'type' => 'group', | |
| 128 | + 'classes' => 'automatorwp-fields-table', | |
| 129 | + 'options' => array( | |
| 130 | + 'add_button' => __( 'Add condition', 'automatorwp' ), | |
| 131 | + 'remove_button' => '<span class="dashicons dashicons-no-alt"></span>', | |
| 132 | + ), | |
| 133 | + 'fields' => array( | |
| 134 | + 'meta_key' => array( | |
| 135 | + 'name' => __( 'Meta Key:', 'automatorwp' ), | |
| 136 | + 'type' => 'text', | |
| 137 | + 'default' => '' | |
| 138 | + ), | |
| 139 | + 'condition' => automatorwp_utilities_condition_field(), | |
| 140 | + 'meta_value' => array( | |
| 141 | + 'name' => __( 'Meta Value:', 'automatorwp' ), | |
| 142 | + 'type' => 'text', | |
| 143 | + 'default' => '' | |
| 144 | + ), | |
| 145 | + ), | |
| 146 | + ), | |
| 147 | + ) | |
| 148 | + ), | |
| 149 | + ), | |
| 150 | + ) ); | |
| 151 | + | |
| 152 | + } | |
| 153 | + | |
| 154 | + /** | |
| 155 | + * Action execution function | |
| 156 | + * | |
| 157 | + * @since 1.0.0 | |
| 158 | + * | |
| 159 | + * @param stdClass $action The action object | |
| 160 | + * @param int $user_id The user ID | |
| 161 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 162 | + * @param stdClass $automation The action's automation object | |
| 163 | + */ | |
| 164 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 165 | + | |
| 166 | + $this->result = ''; | |
| 167 | + $this->field_conditions = array(); | |
| 168 | + $this->meta_conditions = array(); | |
| 169 | + $this->post_meta = array(); | |
| 170 | + | |
| 171 | + global $wpdb; | |
| 172 | + | |
| 173 | + // Filter the posts | |
| 174 | + $field_conditions = $action_options['post_field_conditions']; | |
| 175 | + $meta_conditions = $action_options['post_meta_conditions']; | |
| 176 | + | |
| 177 | + $joins = array(); | |
| 178 | + $where = array(); | |
| 179 | + | |
| 180 | + // Setup the post field conditions | |
| 181 | + foreach( $field_conditions as $condition ) { | |
| 182 | + | |
| 183 | + // Parse automation tags replacements to both, key and value | |
| 184 | + $field = automatorwp_parse_automation_tags( $automation->id, $user_id, $condition['field'] ); | |
| 185 | + $value = automatorwp_parse_automation_tags( $automation->id, $user_id, $condition['value'] ); | |
| 186 | + | |
| 187 | + // Sanitize | |
| 188 | + $field = sanitize_text_field( $field ); | |
| 189 | + $value = sanitize_text_field( $value ); | |
| 190 | + | |
| 191 | + if( ! empty( $field ) ) { | |
| 192 | + $where[] = automatorwp_utilities_parse_condition_to_sql( 'p.' . $field, $condition['condition'], $value ); | |
| 193 | + | |
| 194 | + $this->field_conditions[] = array( | |
| 195 | + 'field' => $field, | |
| 196 | + 'condition' => $condition['condition'], | |
| 197 | + 'value' => $value, | |
| 198 | + ); | |
| 199 | + } | |
| 200 | + } | |
| 201 | + | |
| 202 | + // Setup the post meta conditions | |
| 203 | + foreach( $meta_conditions as $condition ) { | |
| 204 | + | |
| 205 | + // Parse automation tags replacements to both, key and value | |
| 206 | + $meta_key = automatorwp_parse_automation_tags( $automation->id, $user_id, $condition['meta_key'] ); | |
| 207 | + $meta_value = automatorwp_parse_automation_tags( $automation->id, $user_id, $condition['meta_value'] ); | |
| 208 | + | |
| 209 | + // Sanitize | |
| 210 | + $meta_key = sanitize_text_field( $meta_key ); | |
| 211 | + $meta_value = sanitize_text_field( $meta_value ); | |
| 212 | + | |
| 213 | + if( ! empty( $meta_key ) ) { | |
| 214 | + $index = count( $joins ); | |
| 215 | + | |
| 216 | + $joins[] = "INNER JOIN {$wpdb->postmeta} AS pm{$index} ON ( pm{$index}.post_id = p.ID AND pm{$index}.meta_key = '{$meta_key}' )"; | |
| 217 | + | |
| 218 | + $where[] = automatorwp_utilities_parse_condition_to_sql( "pm{$index}.meta_value", $condition['condition'], $meta_value, false ); | |
| 219 | + | |
| 220 | + $this->meta_conditions[] = array( | |
| 221 | + 'meta_key' => $meta_key, | |
| 222 | + 'condition' => $condition['condition'], | |
| 223 | + 'meta_value' => $meta_value, | |
| 224 | + ); | |
| 225 | + } | |
| 226 | + } | |
| 227 | + | |
| 228 | + // Turn arrays into strings | |
| 229 | + $joins = implode( ' ', $joins ); | |
| 230 | + $where = ( ! empty( $where ) ? 'WHERE ( ' . implode( ' ) AND ( ', $where ) . ' ) ' : '' ); | |
| 231 | + | |
| 232 | + $sql = "SELECT p.ID | |
| 233 | + FROM {$wpdb->posts} AS p | |
| 234 | + {$joins} | |
| 235 | + {$where}"; | |
| 236 | + | |
| 237 | + $post_ids = $wpdb->get_col( $sql ); | |
| 238 | + | |
| 239 | + // Bail if not posts found | |
| 240 | + if( count( $post_ids ) === 0 ) { | |
| 241 | + $this->result = __( 'No posts found to be deleted.', 'automatorwp' ); | |
| 242 | + return; | |
| 243 | + } | |
| 244 | + | |
| 245 | + // Delete the posts | |
| 246 | + foreach( $post_ids as $post_id ) { | |
| 247 | + | |
| 248 | + wp_delete_post( $post_id, true ); | |
| 249 | + | |
| 250 | + } | |
| 251 | + | |
| 252 | + $this->result = sprintf( __( '%d posts deleted.', 'automatorwp' ), count( $post_ids ) ); | |
| 253 | + | |
| 254 | + } | |
| 255 | + | |
| 256 | + /** | |
| 257 | + * Register required hooks | |
| 258 | + * | |
| 259 | + * @since 1.0.0 | |
| 260 | + */ | |
| 261 | + public function hooks() { | |
| 262 | + | |
| 263 | + // Log meta data | |
| 264 | + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); | |
| 265 | + | |
| 266 | + // Log fields | |
| 267 | + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); | |
| 268 | + | |
| 269 | + parent::hooks(); | |
| 270 | + } | |
| 271 | + | |
| 272 | + /** | |
| 273 | + * Action custom log meta | |
| 274 | + * | |
| 275 | + * @since 1.0.0 | |
| 276 | + * | |
| 277 | + * @param array $log_meta Log meta data | |
| 278 | + * @param stdClass $action The action object | |
| 279 | + * @param int $user_id The user ID | |
| 280 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 281 | + * @param stdClass $automation The action's automation object | |
| 282 | + * | |
| 283 | + * @return array | |
| 284 | + */ | |
| 285 | + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { | |
| 286 | + | |
| 287 | + // Bail if action type don't match this action | |
| 288 | + if( $action->type !== $this->action ) { | |
| 289 | + return $log_meta; | |
| 290 | + } | |
| 291 | + | |
| 292 | + // Store the filters applied | |
| 293 | + $log_meta['field_conditions'] = $this->field_conditions; | |
| 294 | + $log_meta['field_conditions_parsed'] = ''; | |
| 295 | + | |
| 296 | + foreach( $this->field_conditions as $condition ) { | |
| 297 | + $log_meta['field_conditions_parsed'] .= sprintf( '%s %s %s', | |
| 298 | + $condition['field'], | |
| 299 | + automatorwp_utilities_get_condition_label( $condition['condition'] ), | |
| 300 | + $condition['value'] | |
| 301 | + ) . "<br>"; | |
| 302 | + } | |
| 303 | + | |
| 304 | + $log_meta['meta_conditions'] = $this->meta_conditions; | |
| 305 | + $log_meta['meta_conditions_parsed'] = ''; | |
| 306 | + | |
| 307 | + foreach( $this->meta_conditions as $condition ) { | |
| 308 | + $log_meta['meta_conditions_parsed'] .= sprintf( '%s %s %s', | |
| 309 | + $condition['meta_key'], | |
| 310 | + automatorwp_utilities_get_condition_label( $condition['condition'] ), | |
| 311 | + $condition['meta_value'] | |
| 312 | + ) . "<br>"; | |
| 313 | + } | |
| 314 | + | |
| 315 | + // Store result | |
| 316 | + $log_meta['result'] = $this->result; | |
| 317 | + | |
| 318 | + return $log_meta; | |
| 319 | + } | |
| 320 | + | |
| 321 | + /** | |
| 322 | + * Action custom log fields | |
| 323 | + * | |
| 324 | + * @since 1.0.0 | |
| 325 | + * | |
| 326 | + * @param array $log_fields The log fields | |
| 327 | + * @param stdClass $log The log object | |
| 328 | + * @param stdClass $object The trigger/action/automation object attached to the log | |
| 329 | + * | |
| 330 | + * @return array | |
| 331 | + */ | |
| 332 | + public function log_fields( $log_fields, $log, $object ) { | |
| 333 | + | |
| 334 | + // Bail if log is not assigned to an action | |
| 335 | + if( $log->type !== 'action' || $object->type !== $this->action ) | |
| 336 | + return $log_fields; | |
| 337 | + | |
| 338 | + $log_fields['posts_found_info'] = array( | |
| 339 | + 'name' => __( 'Posts Filtered', 'automatorwp' ), | |
| 340 | + 'desc' => __( 'Information about the filters applied and the posts found.', 'automatorwp' ), | |
| 341 | + 'type' => 'title', | |
| 342 | + ); | |
| 343 | + | |
| 344 | + $log_fields['field_conditions_parsed'] = array( | |
| 345 | + 'name' => __( 'Field conditions:', 'automatorwp' ), | |
| 346 | + 'type' => 'text', | |
| 347 | + ); | |
| 348 | + | |
| 349 | + $log_fields['meta_conditions_parsed'] = array( | |
| 350 | + 'name' => __( 'Meta conditions:', 'automatorwp' ), | |
| 351 | + 'type' => 'text', | |
| 352 | + ); | |
| 353 | + | |
| 354 | + $log_fields['result'] = array( | |
| 355 | + 'name' => __( 'Result:', 'automatorwp' ), | |
| 356 | + 'type' => 'text', | |
| 357 | + ); | |
| 358 | + | |
| 359 | + return $log_fields; | |
| 360 | + } | |
| 361 | + | |
| 362 | +} | |
| 363 | + | |
| 364 | +new AutomatorWP_WordPress_Delete_Multiple_Posts( 'wordpress' ); | |
| 365 | +new AutomatorWP_WordPress_Delete_Multiple_Posts( 'posts' ); | |