create-pretty-link.php
9 months ago
delete-pretty-link.php
9 months ago
fetch-pretty-link.php
9 months ago
list-pretty-links.php
9 months ago
update-pretty-link.php
9 months ago
delete-pretty-link.php
145 lines
| 1 | <?php |
| 2 | /** |
| 3 | * DeletePrettyLink. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category DeletePrettyLink |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.1.9 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\PrettyLinks\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | |
| 20 | /** |
| 21 | * DeletePrettyLink |
| 22 | * |
| 23 | * @category DeletePrettyLink |
| 24 | * @package SureTriggers |
| 25 | * @author BSF <username@example.com> |
| 26 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 27 | * @link https://www.brainstormforce.com/ |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class DeletePrettyLink extends AutomateAction { |
| 31 | |
| 32 | /** |
| 33 | * Integration type. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public $integration = 'PrettyLinks'; |
| 38 | |
| 39 | /** |
| 40 | * Action name. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | public $action = 'prettylinks_delete_pretty_link'; |
| 45 | |
| 46 | use SingletonLoader; |
| 47 | |
| 48 | /** |
| 49 | * Register a action. |
| 50 | * |
| 51 | * @param array $actions actions. |
| 52 | * @return array |
| 53 | */ |
| 54 | public function register( $actions ) { |
| 55 | $actions[ $this->integration ][ $this->action ] = [ |
| 56 | 'label' => __( 'Delete Pretty Link', 'suretriggers' ), |
| 57 | 'action' => $this->action, |
| 58 | 'function' => [ $this, 'action_listener' ], |
| 59 | ]; |
| 60 | return $actions; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Action listener. |
| 65 | * |
| 66 | * @param int $user_id user_id. |
| 67 | * @param int $automation_id automation_id. |
| 68 | * @param array $fields fields. |
| 69 | * @param array $selected_options selected_options. |
| 70 | * |
| 71 | * @return array|void |
| 72 | * |
| 73 | * @throws Exception Exception. |
| 74 | */ |
| 75 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 76 | $link_id = isset( $selected_options['link_id'] ) ? absint( $selected_options['link_id'] ) : 0; |
| 77 | |
| 78 | if ( empty( $link_id ) ) { |
| 79 | return [ |
| 80 | 'status' => 'error', |
| 81 | 'message' => __( 'Link ID is required to delete a pretty link.', 'suretriggers' ), |
| 82 | ]; |
| 83 | } |
| 84 | |
| 85 | if ( ! defined( 'PRLI_VERSION' ) ) { |
| 86 | return [ |
| 87 | 'status' => 'error', |
| 88 | 'message' => __( 'Pretty Links plugin is not active or not found.', 'suretriggers' ), |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | global $wpdb; |
| 93 | $prli_link_table = $wpdb->prefix . 'prli_links'; |
| 94 | $prli_clicks_table = $wpdb->prefix . 'prli_clicks'; |
| 95 | |
| 96 | // Get link data before deletion for response. |
| 97 | $existing_link = $wpdb->get_row( |
| 98 | $wpdb->prepare( |
| 99 | "SELECT * FROM `{$wpdb->prefix}prli_links` WHERE id = %d", |
| 100 | $link_id |
| 101 | ), |
| 102 | ARRAY_A |
| 103 | ); |
| 104 | |
| 105 | if ( ! $existing_link ) { |
| 106 | return [ |
| 107 | 'status' => 'error', |
| 108 | 'message' => __( 'Pretty Link not found with the provided ID.', 'suretriggers' ), |
| 109 | ]; |
| 110 | } |
| 111 | |
| 112 | // Delete associated clicks first. |
| 113 | $clicks_deleted = $wpdb->delete( $prli_clicks_table, [ 'link_id' => $link_id ] ); |
| 114 | |
| 115 | // Delete the link. |
| 116 | $result = $wpdb->delete( $prli_link_table, [ 'id' => $link_id ] ); |
| 117 | |
| 118 | if ( false === $result ) { |
| 119 | return [ |
| 120 | 'status' => 'error', |
| 121 | 'message' => __( 'Failed to delete Pretty Link from database.', 'suretriggers' ), |
| 122 | ]; |
| 123 | } |
| 124 | |
| 125 | if ( 0 === $result ) { |
| 126 | return [ |
| 127 | 'status' => 'error', |
| 128 | 'message' => __( 'No Pretty Link was deleted. Link may not exist.', 'suretriggers' ), |
| 129 | ]; |
| 130 | } |
| 131 | |
| 132 | return [ |
| 133 | 'status' => 'success', |
| 134 | 'message' => __( 'Pretty Link deleted successfully.', 'suretriggers' ), |
| 135 | 'data' => [ |
| 136 | 'deleted_link_id' => $link_id, |
| 137 | 'deleted_link_data' => $existing_link, |
| 138 | 'clicks_deleted' => $clicks_deleted, |
| 139 | ], |
| 140 | ]; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | DeletePrettyLink::get_instance(); |
| 145 |