listing-deleted.php
3 weeks ago
listing-published.php
3 weeks ago
listing-updated.php
3 weeks ago
new-listing-submitted.php
3 weeks ago
new-review-submitted.php
3 weeks ago
review-published.php
3 weeks ago
listing-deleted.php
124 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ListingDeleted |
| 4 | * |
| 5 | * @package SureTriggers |
| 6 | * @category Integration |
| 7 | * @author BSF |
| 8 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 9 | * @link https://www.brainstormforce.com/ |
| 10 | * @since 1.1.26 |
| 11 | */ |
| 12 | |
| 13 | namespace SureTriggers\Integrations\DirectoriesPro\Triggers; |
| 14 | |
| 15 | use SureTriggers\Controllers\AutomationController; |
| 16 | use SureTriggers\Integrations\DirectoriesPro\DirectoriesPro; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | |
| 19 | if ( ! class_exists( 'ListingDeleted' ) ) : |
| 20 | |
| 21 | /** |
| 22 | * ListingDeleted |
| 23 | * |
| 24 | * @category ListingDeleted |
| 25 | * @package SureTriggers |
| 26 | * @author BSF <username@example.com> |
| 27 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 | * @link https://www.brainstormforce.com/ |
| 29 | * @since 1.1.26 |
| 30 | */ |
| 31 | class ListingDeleted { |
| 32 | |
| 33 | use SingletonLoader; |
| 34 | |
| 35 | /** |
| 36 | * Integration name. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $integration = 'DirectoriesPro'; |
| 41 | |
| 42 | /** |
| 43 | * Trigger name. |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | public $trigger = 'directories_pro_listing_deleted'; |
| 48 | |
| 49 | /** |
| 50 | * Constructor. |
| 51 | */ |
| 52 | public function __construct() { |
| 53 | add_filter( 'sure_trigger_register_trigger', [ $this, 'register' ] ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Register trigger. |
| 58 | * |
| 59 | * @param array $triggers Registered triggers. |
| 60 | * @return array Modified triggers. |
| 61 | */ |
| 62 | public function register( $triggers ) { |
| 63 | $triggers[ $this->integration ][ $this->trigger ] = [ |
| 64 | 'label' => __( 'Listing Deleted', 'suretriggers' ), |
| 65 | 'action' => $this->trigger, |
| 66 | // wp_trash_post: moved to trash. before_delete_post: permanently deleted |
| 67 | // (either force-deleted directly, or emptied from trash) — both count as "deleted". |
| 68 | 'common_action' => [ 'wp_trash_post', 'before_delete_post' ], |
| 69 | 'function' => [ $this, 'trigger_listener' ], |
| 70 | 'priority' => 10, |
| 71 | 'accepted_args' => 1, |
| 72 | ]; |
| 73 | |
| 74 | return $triggers; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Trigger listener. |
| 79 | * |
| 80 | * @param int $post_id Post ID. |
| 81 | * @return void |
| 82 | */ |
| 83 | public function trigger_listener( $post_id ) { |
| 84 | $post_id = absint( $post_id ); |
| 85 | if ( ! $post_id ) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | $post = get_post( $post_id ); |
| 90 | if ( ! $post ) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | $bundle = DirectoriesPro::get_bundle( $post->post_type ); |
| 95 | if ( ! DirectoriesPro::is_listing_bundle( $bundle ) ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | $context = DirectoriesPro::build_entity_context( $post, $bundle ); |
| 100 | |
| 101 | // Both hooks fire *before* the post's row is actually updated/removed, so |
| 102 | // $post->post_status here still reflects the pre-deletion state — override it |
| 103 | // with the real outcome rather than leaving stale data (e.g. 'publish') in the context. |
| 104 | if ( 'before_delete_post' === current_action() ) { |
| 105 | $context['status'] = 'deleted'; |
| 106 | $context['deletion_type'] = 'permanent'; |
| 107 | } else { |
| 108 | $context['status'] = 'trash'; |
| 109 | $context['deletion_type'] = 'trashed'; |
| 110 | } |
| 111 | |
| 112 | AutomationController::sure_trigger_handle_trigger( |
| 113 | [ |
| 114 | 'trigger' => $this->trigger, |
| 115 | 'context' => $context, |
| 116 | ] |
| 117 | ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | ListingDeleted::get_instance(); |
| 122 | |
| 123 | endif; |
| 124 |