← All changes
|
includes/chat-sessions/reports/reporting-helper-functions.php
+218
-229
8.5.8
→
8.8.0
View file →
| @@ -1,229 +1,218 @@ | ||
| 1 | -<?php | |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | - exit; | |
| 4 | -} | |
| 5 | - | |
| 6 | -/****************************************** | |
| 7 | - * Get all conversations | |
| 8 | - ******************************************/ | |
| 9 | -function botreports_get_all_conversations() { | |
| 10 | - global $wpdb; | |
| 11 | - | |
| 12 | - $tableUser = $wpdb->prefix . 'wpbot_user'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 13 | - $tableConversation = $wpdb->prefix . 'wpbot_conversation'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 14 | - | |
| 15 | - $preparedSqlStatement = $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 16 | - 'SELECT * FROM %i JOIN %i ON %i.id = %i.user_id', | |
| 17 | - $tableUser, | |
| 18 | - $tableConversation, | |
| 19 | - $tableUser, | |
| 20 | - $tableConversation | |
| 21 | - ); | |
| 22 | - | |
| 23 | - $results = $wpdb->get_results( $preparedSqlStatement ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 24 | - | |
| 25 | - return $results; | |
| 26 | -} | |
| 27 | - | |
| 28 | -/****************************************** | |
| 29 | - * Get Latest 5 Conversations | |
| 30 | - ******************************************/ | |
| 31 | -function botreports_get_last5_conversations() { | |
| 32 | - global $wpdb; | |
| 33 | - | |
| 34 | - $tableUser = $wpdb->prefix . 'wpbot_user'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 35 | - $tableConversation = $wpdb->prefix . 'wpbot_conversation'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 36 | - | |
| 37 | - $preparedSqlStatement = $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 38 | - 'SELECT * FROM %i users JOIN %i conversations ON users.id = conversations.user_id ORDER BY users.date DESC LIMIT %d', | |
| 39 | - $tableUser, | |
| 40 | - $tableConversation, | |
| 41 | - 5 | |
| 42 | - ); | |
| 43 | - | |
| 44 | - $results = $wpdb->get_results( $preparedSqlStatement ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 45 | - | |
| 46 | - return $results; | |
| 47 | -} | |
| 48 | - | |
| 49 | -/****************************************** | |
| 50 | - * Get total conversation count | |
| 51 | - ******************************************/ | |
| 52 | -function botreports_get_total_conversation_count() { | |
| 53 | - global $wpdb; | |
| 54 | - | |
| 55 | - $tableConversation = $wpdb->prefix . 'wpbot_conversation'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 56 | - | |
| 57 | - $preparedSqlStatement = $wpdb->prepare( 'SELECT count(*) FROM %i', $tableConversation ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 58 | - | |
| 59 | - $count = $wpdb->get_var( $preparedSqlStatement ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 60 | - | |
| 61 | - return (int) $count; | |
| 62 | -} | |
| 63 | - | |
| 64 | -function wpbot_get_report_stats_count() { | |
| 65 | - global $wpdb; | |
| 66 | - $table = $wpdb->prefix . 'wpbot_chat_report'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 67 | - | |
| 68 | - return array( | |
| 69 | - 'likes' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE feedback = 'like'", $table ) ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 70 | - 'dislikes' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE feedback = 'dislike'", $table ) ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 71 | - 'total_feedback' => (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE feedback IS NOT NULL', $table ) ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 72 | - 'total_reports' => (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $table ) ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 73 | - 'reports_only' => (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE feedback IS NULL', $table ) ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 74 | - ); | |
| 75 | -} | |
| 76 | - | |
| 77 | -function wpbot_get_reports_list( $limit = 20 ) { | |
| 78 | - global $wpdb; | |
| 79 | - $table = $wpdb->prefix . 'wpbot_chat_report'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 80 | - | |
| 81 | - // Fetch reports only (exclude feedback rows). | |
| 82 | - $results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 83 | - $wpdb->prepare( | |
| 84 | - 'SELECT id, message, meta_info, created_at FROM %i WHERE feedback IS NULL ORDER BY created_at DESC LIMIT %d', | |
| 85 | - $table, | |
| 86 | - $limit | |
| 87 | - ), | |
| 88 | - ARRAY_A | |
| 89 | - ); | |
| 90 | - | |
| 91 | - // Try to extract shopper email from meta_info if stored | |
| 92 | - foreach ( $results as &$row ) { | |
| 93 | - $email = ''; | |
| 94 | - if ( preg_match( '/Email:\s*([^\s]+)/i', $row['meta_info'], $matches ) ) { | |
| 95 | - $email = sanitize_email( $matches[1] ); | |
| 96 | - } | |
| 97 | - | |
| 98 | - $row['email'] = $email ?: 'Unknown'; | |
| 99 | - } | |
| 100 | - | |
| 101 | - return $results; | |
| 102 | -} | |
| 103 | - | |
| 104 | -/****************************************** | |
| 105 | - * Get today's conversation count | |
| 106 | - ******************************************/ | |
| 107 | -function botreports_get_todays_conversation_count() { | |
| 108 | - global $wpdb; | |
| 109 | - | |
| 110 | - $tableUser = $wpdb->prefix . 'wpbot_user'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 111 | - | |
| 112 | - $preparedSqlStatement = $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 113 | - 'SELECT * FROM %i as user WHERE user.date >= CURDATE() AND user.date < CURDATE() + INTERVAL 1 DAY', | |
| 114 | - $tableUser | |
| 115 | - ); | |
| 116 | - | |
| 117 | - $wpdb->get_results( $preparedSqlStatement ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 118 | - | |
| 119 | - return (int) $wpdb->num_rows; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 120 | -} | |
| 121 | - | |
| 122 | -/****************************************** | |
| 123 | - * Get This weeks conversation count | |
| 124 | - ******************************************/ | |
| 125 | -function botreports_get_weeks_conversation_count() { | |
| 126 | - global $wpdb; | |
| 127 | - | |
| 128 | - $tableUser = $wpdb->prefix . 'wpbot_user'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 129 | - | |
| 130 | - $preparedSqlStatement = $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 131 | - 'SELECT * FROM %i as user WHERE user.date >= CURDATE() AND user.date < CURDATE() + INTERVAL %d DAY', | |
| 132 | - $tableUser, | |
| 133 | - 6 | |
| 134 | - ); | |
| 135 | - | |
| 136 | - $wpdb->get_results( $preparedSqlStatement ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 137 | - | |
| 138 | - return (int) $wpdb->num_rows; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 139 | -} | |
| 140 | - | |
| 141 | -/****************************************** | |
| 142 | - * Get last 30 days conversation count | |
| 143 | - ******************************************/ | |
| 144 | -function botreports_get_last30days_conversation_count() { | |
| 145 | - global $wpdb; | |
| 146 | - | |
| 147 | - $tableUser = $wpdb->prefix . 'wpbot_user'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 148 | - | |
| 149 | - $preparedSqlStatement = $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 150 | - 'SELECT * FROM %i as user WHERE user.date >= CURDATE() - INTERVAL %d DAY', | |
| 151 | - $tableUser, | |
| 152 | - 30 | |
| 153 | - ); | |
| 154 | - | |
| 155 | - $wpdb->get_results( $preparedSqlStatement ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 156 | - | |
| 157 | - return (int) $wpdb->num_rows; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 158 | -} | |
| 159 | - | |
| 160 | -/****************************************** | |
| 161 | - * Average daily conversation count in last 30 days | |
| 162 | - ******************************************/ | |
| 163 | -function botreports_get_last30days_conversation_average() { | |
| 164 | - global $wpdb; | |
| 165 | - | |
| 166 | - $tableUser = $wpdb->prefix . 'wpbot_user'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 167 | - | |
| 168 | - $preparedSqlStatement = $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 169 | - 'SELECT * FROM %i as user WHERE user.date >= CURDATE() - INTERVAL %d DAY', | |
| 170 | - $tableUser, | |
| 171 | - 30 | |
| 172 | - ); | |
| 173 | - | |
| 174 | - $wpdb->get_results( $preparedSqlStatement ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 175 | - | |
| 176 | - return round( $wpdb->num_rows / 30 ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 177 | -} | |
| 178 | - | |
| 179 | -/****************************************** | |
| 180 | - * Conversation Density in last 30 days | |
| 181 | - ******************************************/ | |
| 182 | -function botreports_get_last30days_conversation_density() { | |
| 183 | - global $wpdb; | |
| 184 | - | |
| 185 | - $tableUser = $wpdb->prefix . 'wpbot_user'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 186 | - | |
| 187 | - $preparedSqlStatement = $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 188 | - 'SELECT substring(date,1,10) as CONVERSATION_DATE, COUNT(*) as CONVERSATION_NUM FROM %i WHERE date >= CURDATE() - INTERVAL %d DAY GROUP BY CONVERSATION_DATE', | |
| 189 | - $tableUser, | |
| 190 | - 30 | |
| 191 | - ); | |
| 192 | - | |
| 193 | - $results = $wpdb->get_results( $preparedSqlStatement ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 194 | - | |
| 195 | - return $results; | |
| 196 | -} | |
| 197 | - | |
| 198 | -/****************************************** | |
| 199 | - * Find busiest period of the day | |
| 200 | - ******************************************/ | |
| 201 | -function botreports_get_busiest_period() { | |
| 202 | - global $wpdb; | |
| 203 | - | |
| 204 | - $tableUser = $wpdb->prefix . 'wpbot_user'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 205 | - | |
| 206 | - $preparedSqlStatement = $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 207 | - "SELECT DATE_FORMAT(date,'%%H') as hours, count(*) as count FROM %i GROUP BY hours ORDER BY count DESC LIMIT 1", | |
| 208 | - $tableUser | |
| 209 | - ); | |
| 210 | - | |
| 211 | - $results = $wpdb->get_results( $preparedSqlStatement ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 212 | - | |
| 213 | - return $results; | |
| 214 | -} | |
| 215 | - | |
| 216 | -/****************************************** | |
| 217 | - * Get user interactions count in a conversation | |
| 218 | - ******************************************/ | |
| 219 | -function get_user_interaction_count( $conversation ) { | |
| 220 | - if ( ! empty( $conversation ) ) { | |
| 221 | - $interaction = (int) substr_count( $conversation, 'wp-chat-user-msg' ); | |
| 222 | - | |
| 223 | - if ( $interaction == 0 ) { | |
| 224 | - $interaction = (int) substr_count( $conversation, 'woo-chat-user-msg' ); | |
| 225 | - } | |
| 226 | - | |
| 227 | - return $interaction; | |
| 228 | - } | |
| 229 | -} | |
| 1 | +<?php | |
| 2 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | + exit; | |
| 4 | +} | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * Escape a database table name for use in SQL queries. | |
| 8 | + * | |
| 9 | + * @param string $table_name Table name. | |
| 10 | + * @return string | |
| 11 | + */ | |
| 12 | +function qcld_chatbot_sql_table( $table_name ) { | |
| 13 | + return '`' . esc_sql( $table_name ) . '`'; | |
| 14 | +} | |
| 15 | + | |
| 16 | +/****************************************** | |
| 17 | + * Get all conversations | |
| 18 | + ******************************************/ | |
| 19 | +function botreports_get_all_conversations() { | |
| 20 | + global $wpdb; | |
| 21 | + | |
| 22 | + $table_user_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_user' ); | |
| 23 | + $table_conversation_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_conversation' ); | |
| 24 | + | |
| 25 | + $results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 26 | + "SELECT * FROM {$table_user_sql} JOIN {$table_conversation_sql} ON {$table_user_sql}.id = {$table_conversation_sql}.user_id" | |
| 27 | + ); | |
| 28 | + | |
| 29 | + return $results; | |
| 30 | +} | |
| 31 | + | |
| 32 | +/****************************************** | |
| 33 | + * Get Latest 5 Conversations | |
| 34 | + ******************************************/ | |
| 35 | +function botreports_get_last5_conversations() { | |
| 36 | + global $wpdb; | |
| 37 | + | |
| 38 | + $table_user_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_user' ); | |
| 39 | + $table_conversation_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_conversation' ); | |
| 40 | + | |
| 41 | + $results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 42 | + $wpdb->prepare( | |
| 43 | + "SELECT * FROM {$table_user_sql} users JOIN {$table_conversation_sql} conversations ON users.id = conversations.user_id ORDER BY users.date DESC LIMIT %d", | |
| 44 | + 5 | |
| 45 | + ) | |
| 46 | + ); | |
| 47 | + | |
| 48 | + return $results; | |
| 49 | +} | |
| 50 | + | |
| 51 | +/****************************************** | |
| 52 | + * Get total conversation count | |
| 53 | + ******************************************/ | |
| 54 | +function botreports_get_total_conversation_count() { | |
| 55 | + global $wpdb; | |
| 56 | + | |
| 57 | + $table_conversation_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_conversation' ); | |
| 58 | + | |
| 59 | + $count = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 60 | + "SELECT count(*) FROM {$table_conversation_sql}" | |
| 61 | + ); | |
| 62 | + | |
| 63 | + return (int) $count; | |
| 64 | +} | |
| 65 | + | |
| 66 | +function wpbot_get_report_stats_count() { | |
| 67 | + global $wpdb; | |
| 68 | + $table_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_chat_report' ); | |
| 69 | + | |
| 70 | + return array( | |
| 71 | + 'likes' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$table_sql} WHERE feedback = %s", 'like' ) ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 72 | + 'dislikes' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$table_sql} WHERE feedback = %s", 'dislike' ) ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 73 | + 'total_feedback' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table_sql} WHERE feedback IS NOT NULL" ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 74 | + 'total_reports' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table_sql}" ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 75 | + 'reports_only' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table_sql} WHERE feedback IS NULL" ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 76 | + ); | |
| 77 | +} | |
| 78 | + | |
| 79 | +function wpbot_get_reports_list( $limit = 20 ) { | |
| 80 | + global $wpdb; | |
| 81 | + $table_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_chat_report' ); | |
| 82 | + | |
| 83 | + $results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 84 | + $wpdb->prepare( | |
| 85 | + "SELECT id, message, meta_info, created_at FROM {$table_sql} WHERE feedback IS NULL ORDER BY created_at DESC LIMIT %d", | |
| 86 | + $limit | |
| 87 | + ), | |
| 88 | + ARRAY_A | |
| 89 | + ); | |
| 90 | + | |
| 91 | + foreach ( $results as &$row ) { | |
| 92 | + $email = ''; | |
| 93 | + if ( preg_match( '/Email:\s*([^\s]+)/i', $row['meta_info'], $matches ) ) { | |
| 94 | + $email = sanitize_email( $matches[1] ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + $row['email'] = $email ?: 'Unknown'; | |
| 98 | + } | |
| 99 | + | |
| 100 | + return $results; | |
| 101 | +} | |
| 102 | + | |
| 103 | +/****************************************** | |
| 104 | + * Get today's conversation count | |
| 105 | + ******************************************/ | |
| 106 | +function botreports_get_todays_conversation_count() { | |
| 107 | + global $wpdb; | |
| 108 | + | |
| 109 | + $table_user_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_user' ); | |
| 110 | + | |
| 111 | + $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 112 | + "SELECT * FROM {$table_user_sql} AS user WHERE user.date >= CURDATE() AND user.date < CURDATE() + INTERVAL 1 DAY" | |
| 113 | + ); | |
| 114 | + | |
| 115 | + return (int) $wpdb->num_rows; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 116 | +} | |
| 117 | + | |
| 118 | +/****************************************** | |
| 119 | + * Get This weeks conversation count | |
| 120 | + ******************************************/ | |
| 121 | +function botreports_get_weeks_conversation_count() { | |
| 122 | + global $wpdb; | |
| 123 | + | |
| 124 | + $table_user_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_user' ); | |
| 125 | + | |
| 126 | + $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 127 | + $wpdb->prepare( | |
| 128 | + "SELECT * FROM {$table_user_sql} AS user WHERE user.date >= CURDATE() AND user.date < CURDATE() + INTERVAL %d DAY", | |
| 129 | + 6 | |
| 130 | + ) | |
| 131 | + ); | |
| 132 | + | |
| 133 | + return (int) $wpdb->num_rows; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 134 | +} | |
| 135 | + | |
| 136 | +/****************************************** | |
| 137 | + * Get last 30 days conversation count | |
| 138 | + ******************************************/ | |
| 139 | +function botreports_get_last30days_conversation_count() { | |
| 140 | + global $wpdb; | |
| 141 | + | |
| 142 | + $table_user_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_user' ); | |
| 143 | + | |
| 144 | + $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 145 | + $wpdb->prepare( | |
| 146 | + "SELECT * FROM {$table_user_sql} AS user WHERE user.date >= CURDATE() - INTERVAL %d DAY", | |
| 147 | + 30 | |
| 148 | + ) | |
| 149 | + ); | |
| 150 | + | |
| 151 | + return (int) $wpdb->num_rows; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 152 | +} | |
| 153 | + | |
| 154 | +/****************************************** | |
| 155 | + * Average daily conversation count in last 30 days | |
| 156 | + ******************************************/ | |
| 157 | +function botreports_get_last30days_conversation_average() { | |
| 158 | + global $wpdb; | |
| 159 | + | |
| 160 | + $table_user_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_user' ); | |
| 161 | + | |
| 162 | + $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 163 | + $wpdb->prepare( | |
| 164 | + "SELECT * FROM {$table_user_sql} AS user WHERE user.date >= CURDATE() - INTERVAL %d DAY", | |
| 165 | + 30 | |
| 166 | + ) | |
| 167 | + ); | |
| 168 | + | |
| 169 | + return round( $wpdb->num_rows / 30 ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 170 | +} | |
| 171 | + | |
| 172 | +/****************************************** | |
| 173 | + * Conversation Density in last 30 days | |
| 174 | + ******************************************/ | |
| 175 | +function botreports_get_last30days_conversation_density() { | |
| 176 | + global $wpdb; | |
| 177 | + | |
| 178 | + $table_user_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_user' ); | |
| 179 | + | |
| 180 | + $results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 181 | + $wpdb->prepare( | |
| 182 | + "SELECT substring(date,1,10) as CONVERSATION_DATE, COUNT(*) as CONVERSATION_NUM FROM {$table_user_sql} WHERE date >= CURDATE() - INTERVAL %d DAY GROUP BY CONVERSATION_DATE", | |
| 183 | + 30 | |
| 184 | + ) | |
| 185 | + ); | |
| 186 | + | |
| 187 | + return $results; | |
| 188 | +} | |
| 189 | + | |
| 190 | +/****************************************** | |
| 191 | + * Find busiest period of the day | |
| 192 | + ******************************************/ | |
| 193 | +function botreports_get_busiest_period() { | |
| 194 | + global $wpdb; | |
| 195 | + | |
| 196 | + $table_user_sql = qcld_chatbot_sql_table( $wpdb->prefix . 'wpbot_user' ); | |
| 197 | + | |
| 198 | + $results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 199 | + "SELECT DATE_FORMAT(date,'%H') as hours, count(*) as count FROM {$table_user_sql} GROUP BY hours ORDER BY count DESC LIMIT 1" | |
| 200 | + ); | |
| 201 | + | |
| 202 | + return $results; | |
| 203 | +} | |
| 204 | + | |
| 205 | +/****************************************** | |
| 206 | + * Get user interactions count in a conversation | |
| 207 | + ******************************************/ | |
| 208 | +function get_user_interaction_count( $conversation ) { | |
| 209 | + if ( ! empty( $conversation ) ) { | |
| 210 | + $interaction = (int) substr_count( $conversation, 'wp-chat-user-msg' ); | |
| 211 | + | |
| 212 | + if ( $interaction == 0 ) { | |
| 213 | + $interaction = (int) substr_count( $conversation, 'woo-chat-user-msg' ); | |
| 214 | + } | |
| 215 | + | |
| 216 | + return $interaction; | |
| 217 | + } | |
| 218 | +} | |