PluginProbe
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress / 8.5.71
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress v8.5.71
9.1.3 9.1.2 9.1.1 9.1.0 9.0.3 9.0.2 9.0.1 9.0.0 8.5.79 8.5.78 8.5.77 8.5.76 8.5.75 8.5.74 8.5.73 8.5.72 8.5.71 8.5.70 8.5.69 8.5.68 8.5.35 8.5.36 8.5.37 8.5.38 8.5.39 All 222 releases
wpvr / vendor / linno / telemetry / src / Queue.php

Queue.php in WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress 8.5.71, at vendor/linno/telemetry/src/Queue.php

148 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace LinnoSDK\Telemetry;
3
4 class Queue {
5 /**
6 * The name of the custom table
7 *
8 * @var string
9 */
10 private string $table_name;
11
12 /**
13 * Constructor
14 */
15 public function __construct($wpdb = null)
16 {
17 if ( ! $wpdb ) {
18 global $wpdb;
19 }
20 $prefix = ( $wpdb && isset( $wpdb->prefix ) ) ? $wpdb->prefix : 'wp_';
21 $this->table_name = $prefix . 'linno_telemetry_queue';
22 }
23
24 /**
25 * Check whether the queue table exists.
26 *
27 * @return bool
28 */
29 public function table_exists(): bool {
30 global $wpdb;
31
32 $table = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $this->table_name ) );
33
34 return $table === $this->table_name;
35 }
36
37 /**
38 * Create the custom table
39 *
40 * @return void
41 */
42 public function create_table(): void {
43 global $wpdb;
44 $charset_collate = $wpdb->get_charset_collate();
45
46 $sql = "CREATE TABLE IF NOT EXISTS {$this->table_name} (
47 id mediumint(9) NOT NULL AUTO_INCREMENT,
48 plugin_slug varchar(255) NOT NULL,
49 event varchar(255) NOT NULL,
50 properties longtext NOT NULL,
51 timestamp datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
52 PRIMARY KEY (id),
53 INDEX plugin_slug_index (plugin_slug)
54 ) $charset_collate;";
55
56 if ( ! function_exists( 'dbDelta' ) ) {
57 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
58 }
59 dbDelta( $sql );
60 }
61
62 /**
63 * Add an event to the queue
64 *
65 * @param string $event
66 * @param array $properties
67 * @return void
68 */
69 public function add( string $plugin_slug, string $event, array $properties ): void {
70 global $wpdb;
71
72 if ( ! $this->table_exists() ) {
73 $this->create_table();
74 }
75
76 $wpdb->insert(
77 $this->table_name,
78 [
79 'plugin_slug' => $plugin_slug,
80 'event' => $event,
81 'properties' => wp_json_encode( $properties ),
82 'timestamp' => current_time( 'mysql' ),
83 ]
84 );
85 }
86
87 /**
88 * Get all events from the queue
89 *
90 * @return array
91 */
92 public function get_all( string $plugin_slug ): array {
93 global $wpdb;
94
95 if ( ! $this->table_exists() ) {
96 return [];
97 }
98
99 $results = $wpdb->get_results(
100 $wpdb->prepare(
101 "SELECT * FROM {$this->table_name} WHERE plugin_slug = %s ORDER BY timestamp ASC",
102 $plugin_slug
103 )
104 );
105
106 return $results;
107 }
108
109 /**
110 * Delete events from the queue
111 *
112 * @param array $ids
113 * @return void
114 */
115 public function delete( array $ids ): void {
116 global $wpdb;
117
118 if ( ! $this->table_exists() || empty( $ids ) ) {
119 return;
120 }
121
122 $ids = implode( ',', array_map( 'absint', $ids ) );
123
124 $wpdb->query( "DELETE FROM {$this->table_name} WHERE id IN ($ids)" );
125 }
126
127 /**
128 * Clear all events for a specific plugin from the queue.
129 *
130 * @param string $plugin_slug The slug of the plugin whose events should be cleared.
131 * @return void
132 */
133 public function clear_for_plugin( string $plugin_slug ): void {
134 global $wpdb;
135
136 if ( ! $this->table_exists() ) {
137 return;
138 }
139
140 $wpdb->query(
141 $wpdb->prepare(
142 "DELETE FROM {$this->table_name} WHERE plugin_slug = %s",
143 $plugin_slug
144 )
145 );
146 }
147 }
148