PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 1.2.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v1.2.1
4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / app / src / Integrations / TutorLMS / TutorLMSService.php
surecart / app / src / Integrations / TutorLMS Last commit date
TutorLMSService.php 3 years ago TutorLMSServiceProvider.php 3 years ago add-to-cart-surecart.php 3 years ago
TutorLMSService.php
355 lines
1 <?php
2
3 namespace SureCart\Integrations\TutorLMS;
4
5 use SureCart\Integrations\Contracts\IntegrationInterface;
6 use SureCart\Integrations\Contracts\PurchaseSyncInterface;
7 use SureCart\Integrations\IntegrationService;
8 use SureCart\Models\Integration;
9 use SureCart\Models\Price;
10 use SureCart\Models\Product;
11 use SureCart\Support\Currency;
12
13 /**
14 * Controls the LearnDash integration.
15 */
16 class TutorLMSService extends IntegrationService implements IntegrationInterface, PurchaseSyncInterface {
17
18 public function bootstrap() {
19 parent::bootstrap();
20
21 add_filter( 'tutor/course/single/entry-box/free', [ $this, 'purchaseButton' ], 10, 2 );
22 add_filter( 'tutor/course/single/entry-box/purchasable', [ $this, 'purchaseButton' ], 10, 2 );
23 add_filter( 'get_tutor_course_price', [ $this, 'coursePrice' ], 11, 2 );
24
25 add_action( 'surecart/models/price/updated', [ $this, 'clearPriceCache' ], 10, 2 );
26 add_action( 'surecart/models/price/created', [ $this, 'clearPriceCache' ], 10, 2 );
27 }
28
29 /**
30 * Clear the price cache.
31 *
32 * @param \SureCart\Models\Price $price The price model.
33 *
34 * @return void
35 */
36 public function clearPriceCache( $price ) {
37 if ( empty( $price->product ) ) {
38 return;
39 }
40
41 // get the product id.
42 $product_id = is_a( $price->product, Product::class ) ? $price->product->id : $price->product;
43
44 delete_transient( 'surecart_tutor_lms_product_' . $product_id );
45 }
46
47 /**
48 * Get cached product prices.
49 *
50 * @param array $product_ids The product ids.
51 *
52 * @return array
53 */
54 public function getCachedProductsPrices( $product_ids = [] ) {
55 $prices = [];
56 foreach ( $product_ids as $product_id ) {
57 $prices = array_merge( $prices, $this->getCachedProductPrices( $product_id ) );
58 }
59 return $prices;
60 }
61
62 /**
63 * Get the cached prices.
64 *
65 * @param string $product_id The product id.
66 *
67 * @return array
68 */
69 public function getCachedProductPrices( $product_id ) {
70 // cache key.
71 $cache_key = 'surecart_tutor_lms_product_' . $product_id;
72
73 // get the transient.
74 $prices = get_transient( $cache_key );
75
76 // if we do not have a transient.
77 if ( false === $prices ) {
78 // get purchasable prices for product.
79 $prices = Price::where(
80 [
81 'product_ids' => [ $product_id ],
82 'archived' => false,
83 ]
84 )->get();
85
86 // store in transient.
87 set_transient( $cache_key, $prices, apply_filters( 'surecart_tutor_lms_product_cache_time', DAY_IN_SECONDS, $this ) );
88 }
89
90 return $prices;
91 }
92
93 /**
94 * The course price.
95 *
96 * @param string $price The price string.
97 * @param integer $course_id The course id.
98 *
99 * @return string
100 */
101 public function coursePrice( $price, $course_id ) {
102 $integrations = Integration::where( 'integration_id', $course_id )->andWhere( 'model_name', 'product' )->get();
103
104 // we have no integrations.
105 if ( empty( $integrations ) ) {
106 return $price;
107 }
108
109 if ( empty( $integrations[0]->model_id ) ) {
110 return $price;
111 }
112
113 // get the first product.
114 $prices = $this->getCachedProductPrices( $integrations[0]->model_id );
115 if ( is_wp_error( $prices ) ) {
116 return $prices;
117 }
118
119 // there is no price.
120 if ( empty( $prices ) ) {
121 return esc_html__( 'No price', 'surecart' );
122 }
123
124 $price_array = [];
125 foreach ( $prices as $price ) {
126 if ( $price->ad_hoc ) {
127 $price_array[] = esc_html__( 'Custom amount', 'surecart' );
128 } else {
129 $price_array[] = Currency::format( $price->amount, $price->currency ?? 'usd' );
130 }
131 }
132
133 // no price.
134 return implode( ', ', $price_array );
135 }
136
137 /**
138 * Show our purchase button if we have an integration.
139 *
140 * @param string $output The button HTML.
141 * @param integer $id The course id.
142 *
143 * @return string
144 */
145 public function purchaseButton( $output, $id ) {
146 // check first to see if we have any integrations.
147 $integrations = Integration::where( 'integration_id', $id )->andWhere( 'model_name', 'product' )->get();
148 if ( empty( $integrations ) ) {
149 return $output;
150 }
151
152 // Get the model ids from the integrations.
153 $product_ids = array_column( $integrations, 'model_id' );
154 if ( empty( $product_ids ) ) {
155 return $output;
156 }
157
158 // get purchasable prices from cache.
159 $prices = $this->getCachedProductsPrices( $product_ids );
160 if ( empty( $prices ) ) {
161 return $output;
162 }
163
164 // add our components.
165 \SureCart::assets()->enqueueComponents();
166
167 // template.
168 ob_start();
169
170 include 'add-to-cart-surecart.php';
171
172 return ob_get_clean();
173 }
174
175 /**
176 * Get the slug for the integration.
177 *
178 * @return string
179 */
180 public function getName() {
181 return 'surecart/tutor-course';
182 }
183
184 /**
185 * Get the model for the integration.
186 *
187 * @return string
188 */
189 public function getModel() {
190 return 'product';
191 }
192
193 /**
194 * Get the slug for the integration.
195 *
196 * @return string
197 */
198 public function getLogo() {
199 return esc_url_raw( trailingslashit( plugin_dir_url( SURECART_PLUGIN_FILE ) ) . 'images/integrations/tutor.svg' );
200 }
201
202 /**
203 * Get the slug for the integration.
204 *
205 * @return string
206 */
207 public function getLabel() {
208 return __( 'TutorLMS Course', 'surecart' );
209 }
210
211 /**
212 * Get the slug for the integration.
213 *
214 * @return string
215 */
216 public function getItemLabel() {
217 return __( 'Course Access', 'surecart' );
218 }
219
220 /**
221 * Get the slug for the integration.
222 *
223 * @return string
224 */
225 public function getItemHelp() {
226 return __( 'Enable access to a TutorLMS course.', 'surecart' );
227 }
228
229 /**
230 * Is this enabled?
231 *
232 * @return boolean
233 */
234 public function enabled() {
235 return defined( 'TUTOR_VERSION' );
236 }
237
238 /**
239 * Get item listing for the integration.
240 *
241 * @param array $items The integration items.
242 * @param string $search The search term.
243 *
244 * @return array The items for the integration.
245 */
246 public function getItems( $items = [], $search = '' ) {
247 if ( ! function_exists( 'tutor' ) ) {
248 return $items;
249 }
250
251 wp_reset_query();
252 $course_query = new \WP_Query(
253 [
254 'post_type' => tutor()->course_post_type,
255 'post_status' => 'publish',
256 's' => $search,
257 'per_page' => 10,
258 ]
259 );
260
261 if ( ( isset( $course_query->posts ) ) && ( ! empty( $course_query->posts ) ) ) {
262 $items = array_map(
263 function( $post ) {
264 return (object) [
265 'id' => $post->ID,
266 'label' => $post->post_title,
267 ];
268 },
269 $course_query->posts
270 );
271 }
272
273 return $items;
274 }
275
276 /**
277 * Get the individual item.
278 *
279 * @param string $id Id for the record.
280 *
281 * @return object The item for the integration.
282 */
283 public function getItem( $id ) {
284 $course = get_post( $id );
285 if ( ! $course ) {
286 return [];
287 }
288 return (object) [
289 'id' => $id,
290 'provider_label' => __( 'TutorLMS Course', 'surecart' ),
291 'label' => $course->post_title,
292 ];
293 }
294
295 /**
296 * Enable Access to the course.
297 *
298 * @param \SureCart\Models\Integration $integration The integrations.
299 * @param \WP_User $wp_user The user.
300 *
301 * @return boolean|void Returns true if the user course access updation was successful otherwise false.
302 */
303 public function onPurchaseCreated( $integration, $wp_user ) {
304 $this->updateAccess( $integration->integration_id, $wp_user, true );
305 }
306
307 /**
308 * Enable access when purchase is invoked
309 *
310 * @param \SureCart\Models\Integration $integration The integrations.
311 * @param \WP_User $wp_user The user.
312 *
313 * @return boolean|void Returns true if the user course access updation was successful otherwise false.
314 */
315 public function onPurchaseInvoked( $integration, $wp_user ) {
316 $this->onPurchaseCreated( $integration, $wp_user );
317 }
318
319 /**
320 * Remove a user role.
321 *
322 * @param \SureCart\Models\Integration $integration The integrations.
323 * @param \WP_User $wp_user The user.
324 *
325 * @return boolean|void Returns true if the user course access updation was successful otherwise false.
326 */
327 public function onPurchaseRevoked( $integration, $wp_user ) {
328 $this->updateAccess( $integration->integration_id, $wp_user, false );
329 }
330
331 /**
332 * Update access to a course.
333 *
334 * @param integer $course_id The course id.
335 * @param \WP_User $wp_user The user.
336 * @param boolean $add True to add the user to the course, false to remove.
337 *
338 * @return boolean|void Returns true if the user course access updation was successful otherwise false.
339 */
340 public function updateAccess( $course_id, $wp_user, $add = true ) {
341 // we don't have learndash installed.
342 if ( ! function_exists( 'tutor_utils' ) ) {
343 return;
344 }
345
346 if ( ! $add ) {
347 tutor_utils()->cancel_course_enrol( $course_id, $wp_user->ID );
348 return;
349 }
350
351 tutor_utils()->do_enroll( $course_id, 0, $wp_user->ID );
352 tutor_utils()->complete_course_enroll( 0 );
353 }
354 }
355