/** * Send confirmation for enrollment in moodle course * * @access public * @param array $enrolments * @return void */ public function send_moodle_enrollment_confirmation($enrolments) { global $DC_Woodle; $moodle_access_url = woodle_get_settings('access_url', 'dc_woodle_general'); $enrollment_data = array(); foreach ($enrolments as $enrolment) { $post_id = woodle_get_post_by_moodle_id($enrolment['courseid'], 'course'); $course = get_post($post_id); $enrollment_data['email'] = $DC_Woodle->enrollment->wc_order->billing_email; $enrollment_data['course_name'] = $course->post_title; $enrollment_data['course_url'] = "{$moodle_access_url}/course/view.php?id={$enrolment['courseid']}"; } $this->send_email('DC_Woodle_Emails_New_Enrollment', $enrollment_data); }
/** * Trash related product. * * @access public * @param int $post_id * @return void */ public function trash_course_callback($post_id) { $post = get_post($post_id); if ($post && $post->post_type == 'course') { $course_id = get_post_meta($post_id, '_course_id', true); $product_id = woodle_get_post_by_moodle_id($course_id, 'product'); wp_delete_post($product_id, false); } }
/** * Save product meta. * * @access public * @param int $post_id * @return void */ public function save_product_callback($post_id) { require_once ABSPATH . 'wp-admin/includes/screen.php'; $screen = get_current_screen(); $post = get_post($post_id); if ($post && !empty($screen) && $screen->id == 'product' && ($post->post_type = 'product' && !empty($_POST) && array_key_exists('course_id', $_POST))) { $product_id = woodle_get_post_by_moodle_id($_POST['course_id'], 'product'); if ($product_id == $post_id || !$product_id) { $course_id = woodle_get_post_by_moodle_id($_POST['course_id'], 'course'); if ($_POST['course_id'] == 0) { delete_post_meta($post_id, '_course_id'); delete_post_meta($post_id, '_category_id'); update_post_meta($post_id, '_visibility', 'hidden'); delete_post_meta($post_id, '_virtual'); delete_post_meta($post_id, '_sku'); return; } if ($course_id) { $categoryid = get_post_meta($course_id, '_category_id', true); $visibility = get_post_meta($course_id, '_visibility', true); $term_id = woodle_get_term_by_moodle_id($categoryid, 'product_cat', 'woocommerce_term'); if ($term_id) { wp_set_post_terms($post_id, $term_id, 'product_cat'); } update_post_meta($post_id, '_course_id', (int) $_POST['course_id']); update_post_meta($post_id, '_category_id', (int) $categoryid); update_post_meta($post_id, '_visibility', $visibility); update_post_meta($post_id, '_virtual', 'yes'); update_post_meta($post_id, '_sku', 'course-' . $_POST['course_id']); update_post_meta($post_id, '_sold_individually', 'yes'); } } } }
/** * Update moodle courses in Wordpress site. * * @access private * @param array $courses * @param string $post_type (default: null) * @param string $taxonomy (default: null) * @param string $meta_key (default: null) * @return void */ private function update_posts($courses, $post_type = '', $taxonomy = '', $meta_key = '') { if (empty($post_type) || !post_type_exists($post_type) || empty($taxonomy) || !taxonomy_exists($taxonomy) || empty($meta_key)) { return; } $course_ids = array(); $create_wc_product = $post_type == 'product' ? woodle_get_settings('create_wc_product', 'dc_woodle_general') : ''; if (!empty($courses)) { foreach ($courses as $course) { if ($course['format'] == 'site') { continue; } $post_id = woodle_get_post_by_moodle_id($course['id'], $post_type); $post_status = 'publish'; if ($post_type == 'product' && $post_id) { $product = get_post($post_id); $post_status = $product->post_status; } $args = array('post_title' => $course['fullname'], 'post_name' => $course['shortname'], 'post_content' => $course['summary'], 'post_status' => $post_status); if ($post_id) { $args['ID'] = $post_id; $post_id = wp_update_post($args); if ($post_id) { if ($post_type != 'product') { update_post_meta($post_id, '_course_short_name', $course['shortname']); update_post_meta($post_id, '_course_idnumber', $course['idnumber']); } update_post_meta($post_id, '_category_id', (int) $course['categoryid']); update_post_meta($post_id, '_visibility', $course['visible'] ? 'visible' : 'hidden'); if ($post_type == 'product') { update_post_meta($post_id, '_virtual', 'yes'); update_post_meta($post_id, '_sold_individually', 'yes'); } } } else { if ($post_type != 'product' || $create_wc_product == 'yes') { $args['post_type'] = $post_type; $post_id = wp_insert_post($args); if ($post_id) { if ($post_type != 'product') { add_post_meta($post_id, '_course_short_name', $course['shortname']); add_post_meta($post_id, '_course_idnumber', $course['idnumber']); } add_post_meta($post_id, '_course_id', (int) $course['id']); add_post_meta($post_id, '_category_id', (int) $course['categoryid']); add_post_meta($post_id, '_visibility', $course['visible'] ? 'visible' : 'hidden'); if ($post_type == 'product') { add_post_meta($post_id, '_sku', 'course-' . $course['id']); add_post_meta($post_id, '_virtual', 'yes'); add_post_meta($post_id, '_sold_individually', 'yes'); } } } } $course_ids[$course['id']] = $course['categoryid']; } } $posts = woodle_get_posts(array('post_type' => $post_type)); if ($posts) { foreach ($posts as $post) { $course_id = get_post_meta($post->ID, '_course_id', true); if (array_key_exists($course_id, $course_ids)) { $term_id = woodle_get_term_by_moodle_id($course_ids[$course_id], $taxonomy, $meta_key); wp_set_post_terms($post->ID, $term_id, $taxonomy); } else { if (!empty($course_id)) { wp_delete_post($post->ID, false); } } } } }