| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPBot Actions |
| 4 |
* |
| 5 |
* @package WPbot_Automator |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPbot_Automator\Actions; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class WPBot_Actions |
| 16 |
*/ |
| 17 |
class WPBot_Actions extends Action { |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
$this->id = 'wpbot'; |
| 24 |
$this->group = __( 'WPBot', 'wpbot-automator' ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get available actions |
| 29 |
*/ |
| 30 |
public static function get_actions() { |
| 31 |
return array( |
| 32 |
'delete_session' => array( |
| 33 |
'title' => __( 'Delete Chat Session', 'wpbot-automator' ), |
| 34 |
'description' => __( 'Deletes a specific chat session from the database.', 'wpbot-automator' ), |
| 35 |
'fields' => array( |
| 36 |
array( |
| 37 |
'id' => 'session_id', |
| 38 |
'type' => 'text', |
| 39 |
'label' => __( 'Session ID', 'wpbot-automator' ), |
| 40 |
'description' => __( 'The ID of the session to delete.', 'wpbot-automator' ), |
| 41 |
'required' => true, |
| 42 |
), |
| 43 |
), |
| 44 |
), |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Execute the action |
| 50 |
* |
| 51 |
* @param array $action_data Action data. |
| 52 |
* @param array $trigger_data Trigger data. |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
public function execute( $action_data, $trigger_data ) { |
| 56 |
$action_id = $action_data['actionId']; |
| 57 |
$config = isset( $action_data['config'] ) ? $action_data['config'] : array(); |
| 58 |
|
| 59 |
switch ( $action_id ) { |
| 60 |
case 'delete_session': |
| 61 |
return $this->delete_session( $config, $trigger_data ); |
| 62 |
case 'get_chat_sessions': |
| 63 |
return $this->get_chat_sessions( $config, $trigger_data ); |
| 64 |
} |
| 65 |
|
| 66 |
return array( |
| 67 |
'success' => false, |
| 68 |
'message' => __( 'Action not found', 'wpbot-automator' ), |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Delete a chat session |
| 74 |
*/ |
| 75 |
private function delete_session( $config, $trigger_data ) { |
| 76 |
$session_id = $this->parse_tags( $config['session_id'], $trigger_data ); |
| 77 |
|
| 78 |
if ( empty( $session_id ) ) { |
| 79 |
return array( |
| 80 |
'success' => false, |
| 81 |
'message' => __( 'Session ID is required.', 'wpbot-automator' ), |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
global $wpdb; |
| 86 |
$table_name = $wpdb->prefix . 'wpbot_user'; |
| 87 |
|
| 88 |
$result = $wpdb->delete( |
| 89 |
$table_name, |
| 90 |
array( 'session_id' => $session_id ), |
| 91 |
array( '%s' ) |
| 92 |
); |
| 93 |
|
| 94 |
if ( $result !== false ) { |
| 95 |
return array( |
| 96 |
'success' => true, |
| 97 |
'message' => sprintf( __( 'Chat session %s deleted successfully.', 'wpbot-automator' ), $session_id ), |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
return array( |
| 102 |
'success' => false, |
| 103 |
'message' => __( 'Failed to delete chat session.', 'wpbot-automator' ), |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get chat sessions by date |
| 109 |
*/ |
| 110 |
private function get_chat_sessions( $config, $trigger_data ) { |
| 111 |
$start_date_raw = $this->parse_tags( $config['start_date'], $trigger_data ); |
| 112 |
$end_date_raw = $this->parse_tags( $config['end_date'], $trigger_data ); |
| 113 |
|
| 114 |
if ( empty( $start_date_raw ) || empty( $end_date_raw ) ) { |
| 115 |
return array( |
| 116 |
'success' => false, |
| 117 |
'message' => __( 'Start Date and End Date are required.', 'wpbot-automator' ), |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
// Convert human-readable dates to timestamps |
| 122 |
$start_time = strtotime( $start_date_raw ); |
| 123 |
$end_time = strtotime( $end_date_raw ); |
| 124 |
|
| 125 |
if ( ! $start_time || ! $end_time ) { |
| 126 |
return array( |
| 127 |
'success' => false, |
| 128 |
'message' => __( 'Invalid date format provided.', 'wpbot-automator' ), |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
// Format to Y-m-d H:i:s |
| 133 |
$start_date = date( 'Y-m-d 00:00:00', $start_time ); |
| 134 |
$end_date = date( 'Y-m-d 23:59:59', $end_time ); |
| 135 |
|
| 136 |
global $wpdb; |
| 137 |
$user_table = $wpdb->prefix . 'wpbot_user'; |
| 138 |
$conv_table = $wpdb->prefix . 'wpbot_conversation'; |
| 139 |
|
| 140 |
// Join user table with conversation table using user_id |
| 141 |
$query = $wpdb->prepare( |
| 142 |
"SELECT u.session_id, u.name, u.email, u.phone, u.date, c.conversation |
| 143 |
FROM {$user_table} u |
| 144 |
LEFT JOIN {$conv_table} c ON u.user_id = c.user_id |
| 145 |
WHERE u.date >= %s AND u.date <= %s", |
| 146 |
$start_date, |
| 147 |
$end_date |
| 148 |
); |
| 149 |
|
| 150 |
$results = $wpdb->get_results( $query, ARRAY_A ); |
| 151 |
|
| 152 |
if ( empty( $results ) ) { |
| 153 |
return array( |
| 154 |
'success' => true, // Not an error, just empty |
| 155 |
'sessions' => array(), |
| 156 |
'message' => __( 'No chat sessions found for the specified date range.', 'wpbot-automator' ), |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
return array( |
| 161 |
'success' => true, |
| 162 |
'sessions' => $results, |
| 163 |
); |
| 164 |
} |
| 165 |
} |
| 166 |
|