change-ticket-status.php
2 months ago
create-ticket.php
2 months ago
reply-to-ticket.php
2 months ago
reply-to-ticket.php
167 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ReplyToTicketSupportGenix. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category ReplyToTicketSupportGenix |
| 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.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\SupportGenix\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use Mapbd_wps_ticket; |
| 18 | use Mapbd_wps_ticket_reply; |
| 19 | use SureTriggers\Integrations\AutomateAction; |
| 20 | use SureTriggers\Integrations\SupportGenix\SupportGenix; |
| 21 | use SureTriggers\Traits\SingletonLoader; |
| 22 | |
| 23 | /** |
| 24 | * ReplyToTicketSupportGenix |
| 25 | */ |
| 26 | class ReplyToTicketSupportGenix extends AutomateAction { |
| 27 | |
| 28 | use SingletonLoader; |
| 29 | |
| 30 | /** |
| 31 | * Integration type. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | public $integration = 'SupportGenix'; |
| 36 | |
| 37 | /** |
| 38 | * Action name. |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | public $action = 'reply_to_ticket_support_genix'; |
| 43 | |
| 44 | /** |
| 45 | * Register action. |
| 46 | * |
| 47 | * @param array $actions actions. |
| 48 | * @return array |
| 49 | */ |
| 50 | public function register( $actions ) { |
| 51 | $actions[ $this->integration ][ $this->action ] = [ |
| 52 | 'label' => __( 'Reply to Ticket', 'suretriggers' ), |
| 53 | 'action' => $this->action, |
| 54 | 'function' => [ $this, 'action_listener' ], |
| 55 | ]; |
| 56 | |
| 57 | return $actions; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Action listener. |
| 62 | * |
| 63 | * @param int $user_id User ID. |
| 64 | * @param int $automation_id Automation ID. |
| 65 | * @param array $fields Fields. |
| 66 | * @param array $selected_options Selected options. |
| 67 | * |
| 68 | * @return array |
| 69 | * @throws Exception Exception. |
| 70 | */ |
| 71 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 72 | if ( ! class_exists( 'Mapbd_wps_ticket' ) || ! class_exists( 'Mapbd_wps_ticket_reply' ) ) { |
| 73 | return [ |
| 74 | 'status' => 'error', |
| 75 | 'message' => __( 'Support Genix plugin is not installed or active.', 'suretriggers' ), |
| 76 | ]; |
| 77 | } |
| 78 | |
| 79 | $ticket_id = isset( $selected_options['ticket_id'] ) ? (int) $selected_options['ticket_id'] : 0; |
| 80 | $reply_text = isset( $selected_options['reply_text'] ) ? wp_kses_post( $selected_options['reply_text'] ) : ''; |
| 81 | $replied_by_type = isset( $selected_options['replied_by_type'] ) ? sanitize_text_field( $selected_options['replied_by_type'] ) : 'A'; |
| 82 | $replied_by = isset( $selected_options['replied_by'] ) ? (int) $selected_options['replied_by'] : 0; |
| 83 | $is_private = isset( $selected_options['is_private'] ) && 'Y' === $selected_options['is_private'] ? 'Y' : 'N'; |
| 84 | |
| 85 | if ( $ticket_id <= 0 || empty( $reply_text ) ) { |
| 86 | return [ |
| 87 | 'status' => 'error', |
| 88 | 'message' => __( 'Ticket ID and reply text are required.', 'suretriggers' ), |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | if ( ! in_array( $replied_by_type, [ 'A', 'U', 'G' ], true ) ) { |
| 93 | $replied_by_type = 'A'; |
| 94 | } |
| 95 | |
| 96 | if ( 'A' === $replied_by_type ) { |
| 97 | $replied_by = SupportGenix::resolve_agent_id( $replied_by ); |
| 98 | if ( $replied_by <= 0 ) { |
| 99 | return [ |
| 100 | 'status' => 'error', |
| 101 | 'message' => __( 'Could not resolve an agent user to post the reply as.', 'suretriggers' ), |
| 102 | ]; |
| 103 | } |
| 104 | } elseif ( 'U' === $replied_by_type ) { |
| 105 | if ( $replied_by <= 0 || ! get_userdata( $replied_by ) ) { |
| 106 | return [ |
| 107 | 'status' => 'error', |
| 108 | 'message' => __( 'A valid user id is required when replying as User.', 'suretriggers' ), |
| 109 | ]; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Reply entity returned by the Support Genix plugin. |
| 115 | * |
| 116 | * @var object|null $reply_obj |
| 117 | */ |
| 118 | $reply_obj = null; |
| 119 | /** |
| 120 | * Ticket entity returned by the Support Genix plugin. |
| 121 | * |
| 122 | * @var object|null $ticket |
| 123 | */ |
| 124 | $ticket = null; |
| 125 | |
| 126 | try { |
| 127 | $result = Mapbd_wps_ticket_reply::AddReply( |
| 128 | $ticket_id, |
| 129 | $reply_text, |
| 130 | $replied_by, |
| 131 | $replied_by_type, |
| 132 | $is_private, |
| 133 | [], |
| 134 | $reply_obj, |
| 135 | $ticket |
| 136 | ); |
| 137 | } catch ( Exception $e ) { |
| 138 | return [ |
| 139 | 'status' => 'error', |
| 140 | 'message' => $e->getMessage(), |
| 141 | ]; |
| 142 | } |
| 143 | |
| 144 | if ( ! $result ) { |
| 145 | return [ |
| 146 | 'status' => 'error', |
| 147 | 'message' => __( 'Support Genix rejected the reply. The ticket may not exist or the user may not have permission to reply.', 'suretriggers' ), |
| 148 | ]; |
| 149 | } |
| 150 | |
| 151 | $reply_id = null; |
| 152 | if ( ! empty( $reply_obj ) && is_object( $reply_obj ) && property_exists( $reply_obj, 'reply_id' ) ) { |
| 153 | $reply_id = $reply_obj->reply_id; |
| 154 | } |
| 155 | |
| 156 | return [ |
| 157 | 'status' => 'success', |
| 158 | 'ticket_id' => $ticket_id, |
| 159 | 'reply_id' => $reply_id, |
| 160 | 'replied_by' => $replied_by, |
| 161 | 'replied_by_type' => $replied_by_type, |
| 162 | ]; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | ReplyToTicketSupportGenix::get_instance(); |
| 167 |