'sfwd-courses', 's' => $search, 'posts_per_page' => 100, ] ); if ( ( isset( $course_query->posts ) ) && ( ! empty( $course_query->posts ) ) ) { $items = array_map( function ( $post ) { return (object) [ 'id' => $post->ID, 'label' => $post->post_title, ]; }, $course_query->posts ); } return $items; } /** * Get the individual item. * * @param string $id Id for the record. * * @return object The item for the integration. */ public function getItem( $id ) { $course = get_post( $id ); if ( ! $course ) { return []; } return (object) [ 'id' => $id, 'provider_label' => __( 'LearnDash Course', 'surecart' ), 'label' => $course->post_title, ]; } /** * The id must be a real LearnDash course. * * @param string $id Id for the record. * * @return bool */ public function isValidItem( $id ): bool { $course = absint( $id ) ? get_post( absint( $id ) ) : null; return ! empty( $course ) && 'sfwd-courses' === $course->post_type; } /** * Enable Access to the course. * * @param \SureCart\Models\Integration $integration The integrations. * @param \WP_User $wp_user The user. * * @return boolean|void Returns true if the user course access updation was successful otherwise false. */ public function onPurchaseCreated( $integration, $wp_user ) { $this->updateAccess( $integration->integration_id, $wp_user, true ); } /** * Enable access when purchase is invoked * * @param \SureCart\Models\Integration $integration The integrations. * @param \WP_User $wp_user The user. * * @return boolean|void Returns true if the user course access updation was successful otherwise false. */ public function onPurchaseInvoked( $integration, $wp_user ) { $this->onPurchaseCreated( $integration, $wp_user ); } /** * Remove a user role. * * @param \SureCart\Models\Integration $integration The integrations. * @param \WP_User $wp_user The user. * * @return boolean|void Returns true if the user course access updation was successful otherwise false. */ public function onPurchaseRevoked( $integration, $wp_user ) { $this->updateAccess( $integration->integration_id, $wp_user, false ); } /** * Update access to a course. * * @param integer $course_id The course id. * @param \WP_User $wp_user The user. * @param boolean $add True to add the user to the course, false to remove. * * @return boolean|void Returns true if the user course access updation was successful otherwise false. */ public function updateAccess( $course_id, $wp_user, $add = true ) { // we don't have learndash installed. if ( ! function_exists( 'ld_update_course_access' ) ) { return; } // update course access. return \ld_update_course_access( $wp_user->ID, $course_id, ! $add ); } }