PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / includes / Ajax / Collaboration / Sender.php
kirki / includes / Ajax / Collaboration Last commit date
Collaboration.php 3 weeks ago Sender.php 2 months ago
Sender.php
205 lines
1 <?php
2 /**
3 * Collaboration server
4 *
5 * @package kirki
6 */
7
8 namespace Kirki\Ajax\Collaboration;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 use Kirki\HelperFunctions;
15
16
17 /**
18 * Sender class will send and manage collaboration data, also tracking send info.
19 */
20 class Sender {
21
22 /**
23 * Sender instance which will send event-strem as content type
24 *
25 * @return void
26 */
27 public function __construct() {
28 header( 'Content-Type: text/event-stream' );
29 header( 'Cache-Control: no-cache' );
30 header( 'Connection: keep-alive' );
31 }
32
33 /**
34 * Start method will start sending to single client.
35 *
36 * @return void
37 */
38 public function start() {
39 $this->fetch_and_send_events();
40 }
41
42 /**
43 * This method will collect active events and send events to a perticular user.
44 * This mathod also seperate event using parent and parent_id.
45 *
46 * @return void
47 */
48 private function fetch_and_send_events() {
49
50 // Fetch events from your data source.
51 $events = $this->get_custom_events(); // Implement this function to retrieve your custom events.
52
53 foreach ( $events as $event ) {
54 $event_data = $event['data'];
55
56 $parent = $event['parent'];
57 $parent_id = $event['parent_id'];
58 //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
59 echo "event: collaboration-$parent-$parent_id\n"; // Specify the event name.
60 // this data is a json data. No need to Escape.
61 //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
62 echo "data: $event_data\n\n";
63
64 // Flush the output buffer to send the event immediately.
65 ob_flush();
66 flush();
67 }
68 }
69
70 /**
71 * Get custom events.
72 * Remove same user and same session.
73 */
74 private static function get_custom_events() {
75 self::clean_expired_rows();
76 // Get the current user's session ID.
77 // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotValidated
78 $session_id = HelperFunctions::sanitize_text( sanitize_text_field( wp_unslash( $_GET['session_id'] ) ) );
79 $events = array();
80 global $wpdb;
81 $query = $wpdb->prepare(
82 'SELECT * FROM %1s WHERE session_id != %s AND status = %d ORDER BY created_at ASC LIMIT %d OFFSET %d',
83 $wpdb->prefix . KIRKI_COLLABORATION_TABLE,
84 $session_id,
85 1,
86 10,
87 0
88 );
89 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared
90 $rows = $wpdb->get_results( $query, ARRAY_A );
91 foreach ( $rows as &$row ) {
92 if ( ! self::is_already_sent( $row['id'], $session_id ) ) {
93 $events[] = $row;
94 self::add_to_sent_tracking_table( $row['id'], $session_id );
95 }
96 self::delete_single_collaboration( $row );
97 }
98
99 return $events;
100 }
101
102 /**
103 * Add events send status data inside sent table for tracking.
104 *
105 * @param int $collaboration_id collbaration table id.
106 * @param string $session_id user current session id.
107 * @return void
108 */
109 private static function add_to_sent_tracking_table( $collaboration_id, $session_id ) {
110 global $wpdb;
111 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
112 $wpdb->insert(
113 $wpdb->prefix . KIRKI_COLLABORATION_TABLE . '_sent',
114 array(
115 'collaboration_id' => (int) $collaboration_id,
116 'session_id' => $session_id,
117 ),
118 array(
119 '%d',
120 '%s',
121 )
122 );
123 }
124
125 /**
126 * Check event is already sent or not.
127 *
128 * @param int $collaboration_id collbaration table id.
129 * @param string $session_id user current session id.
130 * @return bool
131 */
132 private static function is_already_sent( $collaboration_id, $session_id ) {
133 global $wpdb;
134 $query = $wpdb->prepare(
135 'SELECT * FROM %1s WHERE collaboration_id = %d AND session_id = %s',
136 $wpdb->prefix . KIRKI_COLLABORATION_TABLE . '_sent',
137 $collaboration_id,
138 $session_id
139 );
140 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared
141 $d = $wpdb->get_row( $query );
142 if ( $d ) {
143 return true;
144 }
145 return false;
146 }
147
148 /**
149 * Check event is already sent or not.
150 *
151 * @param array $collaboration collbaration table single row.
152 * @return void
153 */
154 private static function delete_single_collaboration( $collaboration ) {
155 global $wpdb;
156 $collaboration_id = $collaboration['id'];
157 $my_session_id = $collaboration['session_id'];
158 $all_connected_rows = Collaboration::get_all_connected_rows();
159 $flag = true;
160
161 if ( $all_connected_rows ) {
162 foreach ( $all_connected_rows as $key => $row ) {
163 if ( ! self::is_already_sent( $collaboration_id, $row->session_id ) && $row->session_id !== $my_session_id ) {
164 $flag = false;
165 break;
166 }
167 }
168 }
169
170 if ( $flag ) {
171 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
172 $wpdb->delete( $wpdb->prefix . KIRKI_COLLABORATION_TABLE, array( 'id' => $collaboration_id ) );
173 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
174 $wpdb->delete( $wpdb->prefix . KIRKI_COLLABORATION_TABLE . '_sent', array( 'collaboration_id' => $collaboration_id ) );
175 }
176 }
177
178 /**
179 * Clean expired row.
180 * expired if collboration data sent with in 60sec.
181 *
182 * @return void
183 */
184 private static function clean_expired_rows() {
185 global $wpdb;
186 $sixty_seconds_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-20 seconds' ) );
187 $query1 = $wpdb->prepare(
188 'DELETE FROM %1s WHERE updated_at <= %s',
189 $wpdb->prefix . KIRKI_COLLABORATION_TABLE,
190 $sixty_seconds_ago
191 );
192 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared
193 $wpdb->query( $query1 );
194
195 $query2 = $wpdb->prepare(
196 'DELETE FROM %1s WHERE updated_at <= %s',
197 $wpdb->prefix . KIRKI_COLLABORATION_TABLE . '_sent',
198 $sixty_seconds_ago
199 );
200 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared
201 $wpdb->query( $query2 );
202 }
203
204 }
205