PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / trunk
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses vtrunk
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / Databases / WebhookDB.php

WebhookDB.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses trunk, at inc/Databases/WebhookDB.php

179 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\Databases;
4
5 use LearnPress\Filters\WebhookFilter;
6 use LP_Helper;
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Database access for LearnPress webhooks.
12 */
13 class WebhookDB extends DataBase {
14 /**
15 * @var self|null
16 */
17 private static $_instance;
18
19 /**
20 * Get singleton instance.
21 *
22 * @return self
23 */
24 public static function getInstance(): self {
25 if ( is_null( self::$_instance ) ) {
26 self::$_instance = new self();
27 }
28
29 return self::$_instance;
30 }
31
32 /**
33 * Query webhook rows.
34 *
35 * @param WebhookFilter $filter Webhook query filter.
36 * @param int $total_rows Total matching rows.
37 *
38 * @return array|int|string|null
39 * @throws \Exception
40 */
41 public function get_webhooks( WebhookFilter $filter, int &$total_rows = 0 ) {
42 $filter->fields = array_merge( $filter->all_fields, $filter->fields );
43
44 if ( empty( $filter->collection ) ) {
45 $filter->collection = $this->tb_lp_webhooks;
46 }
47
48 if ( empty( $filter->collection_alias ) ) {
49 $filter->collection_alias = 'w';
50 }
51
52 if ( isset( $filter->webhook_id ) ) {
53 $filter->where[] = $this->wpdb->prepare( 'AND w.webhook_id = %d', $filter->webhook_id );
54 }
55
56 if ( ! empty( $filter->webhook_ids ) ) {
57 $ids_format = LP_Helper::db_format_array( $filter->webhook_ids, '%d' );
58 // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Placeholders are generated for sanitized integer IDs.
59 $filter->where[] = $this->wpdb->prepare( "AND w.webhook_id IN ($ids_format)", $filter->webhook_ids );
60 }
61
62 if ( isset( $filter->user_id ) ) {
63 $filter->where[] = $this->wpdb->prepare( 'AND w.user_id = %d', $filter->user_id );
64 }
65
66 if ( isset( $filter->status ) ) {
67 $filter->where[] = $this->wpdb->prepare( 'AND w.status = %s', $filter->status );
68 }
69
70 if ( ! empty( $filter->statuses ) ) {
71 $statuses_format = LP_Helper::db_format_array( $filter->statuses );
72 // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Placeholders are generated by LP_Helper.
73 $filter->where[] = $this->wpdb->prepare( "AND w.status IN ($statuses_format)", $filter->statuses );
74 }
75
76 if ( isset( $filter->name ) ) {
77 $filter->where[] = $this->wpdb->prepare( 'AND w.name = %s', $filter->name );
78 }
79
80 if ( isset( $filter->delivery_url ) ) {
81 $filter->where[] = $this->wpdb->prepare( 'AND w.delivery_url = %s', $filter->delivery_url );
82 }
83
84 if ( isset( $filter->event ) ) {
85 $event = '%"' . $this->wpdb->esc_like( $filter->event ) . '"%';
86 $filter->where[] = $this->wpdb->prepare( 'AND w.events LIKE %s', $event );
87 }
88
89 if ( ! empty( $filter->key_word ) ) {
90 $search = '%' . $this->wpdb->esc_like( $filter->key_word ) . '%';
91 $filter->where[] = $this->wpdb->prepare( 'AND (w.name LIKE %s OR w.delivery_url LIKE %s)', $search, $search );
92 }
93
94 $filter = apply_filters( 'learn-press/webhook/query/filter', $filter );
95
96 return $this->execute( $filter, $total_rows );
97 }
98
99 /**
100 * Get a webhook row by ID.
101 *
102 * @param int $webhook_id Webhook ID.
103 *
104 * @return object|null
105 * @throws \Exception
106 */
107 public function get_webhook( int $webhook_id ) {
108 $filter = new WebhookFilter();
109 $filter->webhook_id = absint( $webhook_id );
110 $filter->limit = 1;
111 $filter->run_query_count = false;
112 $rows = $this->get_webhooks( $filter );
113
114 return is_array( $rows ) && ! empty( $rows ) ? reset( $rows ) : null;
115 }
116
117 /**
118 * Insert a webhook row.
119 *
120 * @param array<string, mixed> $data Webhook data.
121 *
122 * @return int
123 * @throws \Exception
124 */
125 public function insert_webhook( array $data ): int {
126 return $this->insert_data(
127 array(
128 'data' => $data,
129 'filter' => new WebhookFilter(),
130 'table_name' => $this->tb_lp_webhooks,
131 'key_auto_increment' => WebhookFilter::COL_WEBHOOK_ID,
132 )
133 );
134 }
135
136 /**
137 * Update a webhook row.
138 *
139 * @param array<string, mixed> $data Webhook data.
140 *
141 * @return bool
142 * @throws \Exception
143 */
144 public function update_webhook( array $data ): bool {
145 return $this->update_data(
146 array(
147 'data' => $data,
148 'filter' => new WebhookFilter(),
149 'table_name' => $this->tb_lp_webhooks,
150 'where_key' => WebhookFilter::COL_WEBHOOK_ID,
151 )
152 );
153 }
154
155 /**
156 * Delete webhook rows by ID.
157 *
158 * @param int[] $webhook_ids Webhook IDs.
159 *
160 * @return int
161 * @throws \Exception
162 */
163 public function delete_webhooks( array $webhook_ids ): int {
164 $webhook_ids = array_values( array_filter( array_map( 'absint', $webhook_ids ) ) );
165 if ( empty( $webhook_ids ) ) {
166 return 0;
167 }
168
169 $filter = new WebhookFilter();
170 $filter->collection = $this->tb_lp_webhooks;
171 $ids_format = LP_Helper::db_format_array( $webhook_ids, '%d' );
172 // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Placeholders are generated for sanitized integer IDs.
173 $filter->where[] = $this->wpdb->prepare( "AND webhook_id IN ($ids_format)", $webhook_ids );
174 $deleted = $this->delete_execute( $filter );
175
176 return $deleted > 0 ? (int) $deleted : 0;
177 }
178 }
179