support-genix.php
217 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Support Genix core integration file. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package SureTrigger |
| 7 | */ |
| 8 | |
| 9 | namespace SureTriggers\Integrations\SupportGenix; |
| 10 | |
| 11 | use SureTriggers\Controllers\IntegrationsController; |
| 12 | use SureTriggers\Integrations\Integrations; |
| 13 | use SureTriggers\Traits\SingletonLoader; |
| 14 | |
| 15 | /** |
| 16 | * Class SupportGenix |
| 17 | * |
| 18 | * @package SureTriggers\Integrations\SupportGenix |
| 19 | */ |
| 20 | class SupportGenix extends Integrations { |
| 21 | |
| 22 | use SingletonLoader; |
| 23 | |
| 24 | /** |
| 25 | * ID |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $id = 'SupportGenix'; |
| 30 | |
| 31 | /** |
| 32 | * Status code → label map used by Mapbd_wps_ticket::GetPropertyRawOptions. |
| 33 | * |
| 34 | * @var array<string, string> |
| 35 | */ |
| 36 | protected static $status_labels = [ |
| 37 | 'N' => 'New', |
| 38 | 'C' => 'Closed', |
| 39 | 'P' => 'In-progress', |
| 40 | 'R' => 'Re-open', |
| 41 | 'A' => 'Active', |
| 42 | 'I' => 'Inactive', |
| 43 | 'D' => 'Deleted', |
| 44 | ]; |
| 45 | |
| 46 | /** |
| 47 | * Priority code → label map. |
| 48 | * |
| 49 | * @var array<string, string> |
| 50 | */ |
| 51 | protected static $priority_labels = [ |
| 52 | 'N' => 'Normal', |
| 53 | 'M' => 'Medium', |
| 54 | 'H' => 'High', |
| 55 | ]; |
| 56 | |
| 57 | /** |
| 58 | * User type code → label map. |
| 59 | * |
| 60 | * @var array<string, string> |
| 61 | */ |
| 62 | protected static $user_type_labels = [ |
| 63 | 'A' => 'Agent', |
| 64 | 'U' => 'User', |
| 65 | 'G' => 'Guest', |
| 66 | ]; |
| 67 | |
| 68 | /** |
| 69 | * SureTrigger constructor. |
| 70 | */ |
| 71 | public function __construct() { |
| 72 | $this->name = __( 'Support Genix', 'suretriggers' ); |
| 73 | $this->description = __( 'Support Genix is a WordPress helpdesk and support ticketing plugin.', 'suretriggers' ); |
| 74 | $this->icon_url = SURE_TRIGGERS_URL . 'assets/icons/supportgenix.svg'; |
| 75 | |
| 76 | parent::__construct(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Is plugin dependency installed or not. |
| 81 | * |
| 82 | * @return bool |
| 83 | */ |
| 84 | public function is_plugin_installed() { |
| 85 | return defined( 'SUPPORT_GENIX_LITE_VERSION' ) |
| 86 | || defined( 'SUPPORT_GENIX_VERSION' ) |
| 87 | || class_exists( 'Mapbd_wps_ticket' ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Flatten a Mapbd_wps_ticket (or similar) object into an associative array |
| 92 | * with human readable status / priority / user type labels appended. |
| 93 | * |
| 94 | * @param object|array $ticket Ticket model or array. |
| 95 | * @return array |
| 96 | */ |
| 97 | public static function prepare_ticket_context( $ticket ) { |
| 98 | $data = self::object_to_array( $ticket ); |
| 99 | |
| 100 | if ( ! is_array( $data ) ) { |
| 101 | return []; |
| 102 | } |
| 103 | |
| 104 | if ( isset( $data['status'] ) && isset( self::$status_labels[ $data['status'] ] ) ) { |
| 105 | $data['status_label'] = self::$status_labels[ $data['status'] ]; |
| 106 | } |
| 107 | |
| 108 | if ( isset( $data['priority'] ) && isset( self::$priority_labels[ $data['priority'] ] ) ) { |
| 109 | $data['priority_label'] = self::$priority_labels[ $data['priority'] ]; |
| 110 | } |
| 111 | |
| 112 | if ( isset( $data['user_type'] ) && isset( self::$user_type_labels[ $data['user_type'] ] ) ) { |
| 113 | $data['user_type_label'] = self::$user_type_labels[ $data['user_type'] ]; |
| 114 | } |
| 115 | |
| 116 | $ticket_user_id = isset( $data['ticket_user'] ) ? (int) $data['ticket_user'] : 0; |
| 117 | if ( $ticket_user_id > 0 ) { |
| 118 | $user = get_userdata( $ticket_user_id ); |
| 119 | if ( $user ) { |
| 120 | $data['ticket_user_email'] = $user->user_email; |
| 121 | $data['ticket_user_name'] = $user->display_name; |
| 122 | $data['ticket_user_login'] = $user->user_login; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return $data; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Flatten a reply object. |
| 131 | * |
| 132 | * @param object|array $reply Reply object. |
| 133 | * @return array |
| 134 | */ |
| 135 | public static function prepare_reply_context( $reply ) { |
| 136 | $data = self::object_to_array( $reply ); |
| 137 | |
| 138 | if ( ! is_array( $data ) ) { |
| 139 | return []; |
| 140 | } |
| 141 | |
| 142 | if ( isset( $data['replied_by_type'] ) && isset( self::$user_type_labels[ $data['replied_by_type'] ] ) ) { |
| 143 | $data['replied_by_type_label'] = self::$user_type_labels[ $data['replied_by_type'] ]; |
| 144 | } |
| 145 | |
| 146 | if ( isset( $data['ticket_status'] ) && isset( self::$status_labels[ $data['ticket_status'] ] ) ) { |
| 147 | $data['ticket_status_label'] = self::$status_labels[ $data['ticket_status'] ]; |
| 148 | } |
| 149 | |
| 150 | $replied_by = isset( $data['replied_by'] ) ? (int) $data['replied_by'] : 0; |
| 151 | if ( $replied_by > 0 && isset( $data['replied_by_type'] ) && 'G' !== $data['replied_by_type'] ) { |
| 152 | $user = get_userdata( $replied_by ); |
| 153 | if ( $user ) { |
| 154 | $data['replied_by_email'] = $user->user_email; |
| 155 | $data['replied_by_name'] = $user->display_name; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return $data; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Resolve an agent user id for making replies. Falls back to the first |
| 164 | * administrator if the requested user isn't available. |
| 165 | * |
| 166 | * @param int $agent_id Requested agent id. |
| 167 | * @return int |
| 168 | */ |
| 169 | public static function resolve_agent_id( $agent_id ) { |
| 170 | $agent_id = (int) $agent_id; |
| 171 | if ( $agent_id > 0 && get_userdata( $agent_id ) ) { |
| 172 | return $agent_id; |
| 173 | } |
| 174 | |
| 175 | $admins = get_users( |
| 176 | [ |
| 177 | 'role' => 'administrator', |
| 178 | 'number' => 1, |
| 179 | 'fields' => 'ID', |
| 180 | ] |
| 181 | ); |
| 182 | |
| 183 | return ! empty( $admins ) ? (int) $admins[0] : 0; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Safely cast an object / model to array. |
| 188 | * |
| 189 | * @param object|array $value Value to cast. |
| 190 | * @return array |
| 191 | */ |
| 192 | protected static function object_to_array( $value ) { |
| 193 | if ( is_array( $value ) ) { |
| 194 | return $value; |
| 195 | } |
| 196 | |
| 197 | if ( is_object( $value ) ) { |
| 198 | if ( method_exists( $value, 'toArray' ) ) { |
| 199 | $maybe = $value->toArray(); |
| 200 | if ( is_array( $maybe ) ) { |
| 201 | return $maybe; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | $vars = get_object_vars( $value ); |
| 206 | if ( is_array( $vars ) ) { |
| 207 | return $vars; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return []; |
| 212 | } |
| 213 | |
| 214 | } |
| 215 | |
| 216 | IntegrationsController::register( SupportGenix::class ); |
| 217 |