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.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 4.2.1.1 All 137 releases
learnpress / inc / Webhook / WebhookEvents.php

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

164 lines 6.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\Webhook;
4
5 defined( 'ABSPATH' ) || exit;
6
7 /**
8 * Registry of webhook event keys available to administrators.
9 */
10 class WebhookEvents {
11 /**
12 * Return registered event keys and labels.
13 *
14 * @return array<string, string>
15 */
16 public static function all(): array {
17 $events = self::core_events();
18
19 if ( self::is_plugin_active( 'learnpress-membership/learnpress-membership.php' ) ) {
20 $events = array_merge( $events, self::membership_events() );
21 }
22
23 if ( self::is_plugin_active( 'learnpress-assignments/learnpress-assignments.php' ) ) {
24 $events = array_merge( $events, self::assignment_events() );
25 }
26
27 if ( self::is_plugin_active( 'learnpress-announcements/learnpress-announcements.php' ) ) {
28 $events = array_merge( $events, self::announcement_events() );
29 }
30
31 $filtered = apply_filters( 'learn-press/webhook/events', $events );
32
33 return is_array( $filtered ) ? $filtered : $events;
34 }
35
36 /**
37 * Return LearnPress core webhook events.
38 *
39 * @return array<string, string>
40 */
41 protected static function core_events(): array {
42 return array(
43 'user.course_enrolled' => __( 'User enrolled course', 'learnpress' ),
44 'course.enrolled' => __( 'Course enrolled', 'learnpress' ),
45 'order.created' => __( 'Order created', 'learnpress' ),
46 'order.status_changed' => __( 'Order status changed', 'learnpress' ),
47 'order.pending_to_processing' => __( 'Order pending to processing', 'learnpress' ),
48 'order.pending_to_completed' => __( 'Order pending to completed', 'learnpress' ),
49 'order.processing' => __( 'Order processing', 'learnpress' ),
50 'order.completed' => __( 'Order completed', 'learnpress' ),
51 'order.cancelled' => __( 'Order cancelled', 'learnpress' ),
52 'order.failed' => __( 'Order failed', 'learnpress' ),
53 'order.refunded' => __( 'Order refunded', 'learnpress' ),
54 'checkout.order_processed' => __( 'Checkout order processed', 'learnpress' ),
55 'course.submit_rejected' => __( 'Course submission rejected', 'learnpress' ),
56 'course.submit_approved' => __( 'Course submission approved', 'learnpress' ),
57 'course.submit_for_review' => __( 'Course submitted for review', 'learnpress' ),
58 'lesson.completed' => __( 'Lesson completed', 'learnpress' ),
59 'quiz.started' => __( 'Quiz started', 'learnpress' ),
60 'quiz.finished' => __( 'Quiz finished', 'learnpress' ),
61 'quiz.retried' => __( 'Quiz retried', 'learnpress' ),
62 'course.finished' => __( 'Course finished', 'learnpress' ),
63 'user_item.created' => __( 'User item created', 'learnpress' ),
64 'user.password_reset_requested' => __( 'User password reset requested', 'learnpress' ),
65 'instructor.requested' => __( 'Instructor request submitted', 'learnpress' ),
66 'instructor.accepted' => __( 'Instructor request accepted', 'learnpress' ),
67 'instructor.denied' => __( 'Instructor request denied', 'learnpress' ),
68 );
69 }
70
71 /**
72 * Return LearnPress Membership addon webhook events.
73 *
74 * @return array<string, string>
75 */
76 protected static function membership_events(): array {
77 return array(
78 'membership.order_completed' => __( 'Membership order completed', 'learnpress' ),
79 'membership.subscription_renewed' => __( 'Membership subscription renewed', 'learnpress' ),
80 'membership.subscription_cancelled' => __( 'Membership subscription cancelled', 'learnpress' ),
81 'membership.subscription_suspended' => __( 'Membership subscription suspended', 'learnpress' ),
82 'membership.subscription_expired' => __( 'Membership subscription expired', 'learnpress' ),
83 'membership.payment_failed' => __( 'Membership payment failed', 'learnpress' ),
84 'membership.reminder_expiring' => __( 'Membership reminder expiring', 'learnpress' ),
85 'membership.course_resumed' => __( 'Membership course resumed', 'learnpress' ),
86 );
87 }
88
89 /**
90 * Return LearnPress Assignments addon webhook events.
91 *
92 * @return array<string, string>
93 */
94 protected static function assignment_events(): array {
95 return array(
96 'assignment.started' => __( 'Assignment started', 'learnpress' ),
97 'assignment.submitted' => __( 'Assignment submitted', 'learnpress' ),
98 'assignment.evaluated' => __( 'Assignment evaluated', 'learnpress' ),
99 'assignment.re_evaluated' => __( 'Assignment re-evaluated', 'learnpress' ),
100 'assignment.retried' => __( 'Assignment retried', 'learnpress' ),
101 );
102 }
103
104 /**
105 * Return LearnPress Announcements addon webhook events.
106 *
107 * @return array<string, string>
108 */
109 protected static function announcement_events(): array {
110 return array(
111 'announcement.created' => __( 'Announcement created', 'learnpress' ),
112 'announcement.email_queued' => __( 'Announcement email queued', 'learnpress' ),
113 );
114 }
115
116 /**
117 * Check whether a plugin basename is active.
118 *
119 * @param string $plugin_file Plugin basename.
120 *
121 * @return bool
122 */
123 protected static function is_plugin_active( string $plugin_file ): bool {
124 if ( function_exists( 'is_plugin_active' ) && is_plugin_active( $plugin_file ) ) {
125 return true;
126 }
127
128 $active_plugins = (array) get_option( 'active_plugins', array() );
129 if ( in_array( $plugin_file, $active_plugins, true ) ) {
130 return true;
131 }
132
133 if ( is_multisite() ) {
134 $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
135 if ( isset( $network_plugins[ $plugin_file ] ) ) {
136 return true;
137 }
138 }
139
140 return false;
141 }
142
143 /**
144 * Keep only registered event keys.
145 *
146 * @param array<int, mixed> $event_keys Raw event keys.
147 *
148 * @return array<int, string>
149 */
150 public static function sanitize( array $event_keys ): array {
151 $allowed = array_keys( self::all() );
152 $valid = array();
153
154 foreach ( $event_keys as $event_key ) {
155 $event_key = sanitize_text_field( (string) $event_key );
156 if ( in_array( $event_key, $allowed, true ) ) {
157 $valid[] = $event_key;
158 }
159 }
160
161 return array_values( array_unique( $valid ) );
162 }
163 }
164