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 |