| @@ -1,0 +1,104 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Delete Post | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\WordPress\Triggers\Delete_Post | |
| 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_Post extends AutomatorWP_Integration_Trigger { | |
| 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->trigger = $integration . '_delete_post'; | |
| 23 | + | |
| 24 | + parent::__construct(); | |
| 25 | + | |
| 26 | + } | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Register the trigger | |
| 30 | + * | |
| 31 | + * @since 1.0.0 | |
| 32 | + */ | |
| 33 | + public function register() { | |
| 34 | + | |
| 35 | + automatorwp_register_trigger( $this->trigger, array( | |
| 36 | + 'integration' => $this->integration, | |
| 37 | + 'label' => __( 'User deletes a post', 'automatorwp' ), | |
| 38 | + 'select_option' => __( 'User deletes <strong>a post</strong>', 'automatorwp' ), | |
| 39 | + /* translators: %1$s: Number of times. */ | |
| 40 | + 'edit_label' => sprintf( __( 'User deletes a post %1$s time(s)', 'automatorwp' ), '{times}' ), | |
| 41 | + 'log_label' => __( 'User deletes a post', 'automatorwp' ), | |
| 42 | + 'action' => array( | |
| 43 | + 'trashed_post', | |
| 44 | + 'before_delete_post' | |
| 45 | + ), | |
| 46 | + 'function' => array( $this, 'listener' ), | |
| 47 | + 'priority' => 10, | |
| 48 | + 'accepted_args' => 2, | |
| 49 | + 'options' => array( | |
| 50 | + 'times' => automatorwp_utilities_times_option(), | |
| 51 | + ), | |
| 52 | + 'tags' => array_merge( | |
| 53 | + automatorwp_utilities_post_tags(), | |
| 54 | + automatorwp_utilities_times_tag() | |
| 55 | + ) | |
| 56 | + ) ); | |
| 57 | + | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * Trigger listener | |
| 62 | + * | |
| 63 | + * @since 1.0.0 | |
| 64 | + * | |
| 65 | + * @param int $post_id The post ID | |
| 66 | + * @param object|string $post_data | |
| 67 | + */ | |
| 68 | + public function listener( $post_id = 0, $post_data = null ) { | |
| 69 | + | |
| 70 | + $post = get_post( $post_id ); | |
| 71 | + | |
| 72 | + if ( ! $post ) { | |
| 73 | + return; | |
| 74 | + } | |
| 75 | + | |
| 76 | + if ( $post_data === null ) { | |
| 77 | + return; | |
| 78 | + } | |
| 79 | + | |
| 80 | + // Bail if not is a post | |
| 81 | + if( $post->post_type !== 'post' ) { | |
| 82 | + return; | |
| 83 | + } | |
| 84 | + | |
| 85 | + // trashed_post hook returns string with previous status | |
| 86 | + // before_delete_post hook returns WP_post with eliminated post data | |
| 87 | + $post_status = ( is_object( $post_data ) ) ? $post_data->post_status : $post_data; | |
| 88 | + | |
| 89 | + if( ! in_array( $post_status, array( 'publish', 'private' ) ) ) { | |
| 90 | + return; | |
| 91 | + } | |
| 92 | + | |
| 93 | + automatorwp_trigger_event( array( | |
| 94 | + 'trigger' => $this->trigger, | |
| 95 | + 'user_id' => $post->post_author, | |
| 96 | + 'post_id' => $post->ID, | |
| 97 | + ) ); | |
| 98 | + | |
| 99 | + } | |
| 100 | + | |
| 101 | +} | |
| 102 | + | |
| 103 | +new AutomatorWP_WordPress_Delete_Post( 'wordpress' ); | |
| 104 | +new AutomatorWP_WordPress_Delete_Post( 'posts' ); | |