/**
  * 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);
                 }
             }
         }
     }
 }
    /**
     * Add meta box content.
     *
     * @access public
     * @return void
     */
    public function add_product_meta_boxes()
    {
        global $post, $DC_Woodle;
        $course_id = get_post_meta($post->ID, '_course_id', true);
        $post_id = woodle_get_post_by_moodle_id($course_id, 'course');
        $course_short_name = get_post_meta($post_id, '_course_short_name', true);
        $courses = woodle_get_posts(array('post_type' => 'course', 'post_status' => 'publish'));
        ?>
		<p>
			<label for="courses"><?php 
        _e('Course Name', $DC_Woodle->text_domain);
        ?>
</label>
			<select id="courses-select" name="course_id">
				<option value="0"><?php 
        _e('Select course...', $DC_Woodle->text_domain);
        ?>
</option>
				<?php 
        if (!empty($courses)) {
            foreach ($courses as $course) {
                $id = get_post_meta($course->ID, '_course_id', true);
                $category_id = get_post_meta($course->ID, '_category_id', true);
                $term_id = woodle_get_term_by_moodle_id($category_id, 'course_cat', 'woodle_term');
                $course_category_path = get_woodle_term_meta($term_id, '_category_path', true);
                $category_ids = explode('/', $course_category_path);
                $course_path = array();
                $x = array();
                if (!empty($category_ids)) {
                    foreach ($category_ids as $cat_id) {
                        if (!empty($cat_id)) {
                            $term_id = woodle_get_term_by_moodle_id(intval($cat_id), 'course_cat', 'woodle_term');
                            $term = get_term($term_id, 'course_cat');
                            $course_path[] = $term->name;
                        }
                    }
                }
                $course_name = '';
                if (!empty($course_path)) {
                    $course_name = implode(' / ', $course_path);
                    $course_name .= ' - ';
                }
                $course_name .= $course->post_title;
                if (!empty($id)) {
                    ?>
							<option value="<?php 
                    echo $id;
                    ?>
" <?php 
                    if (!empty($course_id) && $id == $course_id) {
                        echo 'selected="selected"';
                    }
                    ?>
><?php 
                    echo $course_name;
                    ?>
</option>
							<?php 
                }
            }
        }
        ?>
			</select>
		</p>
    <p>
		  <label for="course_short_name"><?php 
        _e('Course Short Name', $DC_Woodle->text_domain);
        ?>
</label>
		  <input type="text" id="course_short_name" name="course_short_name" value="<?php 
        if (!empty($course_short_name)) {
            echo $course_short_name;
        }
        ?>
" readonly>
		</p>
    <?php 
    }
/**
 * Returns post id by moodle category id.
 *
 * @param int $course_id
 * @param string $post_type (default: null)
 * @return int
 */
function woodle_get_post_by_moodle_id($course_id, $post_type = '')
{
    if (empty($course_id) || !is_numeric($course_id) || empty($post_type) || !post_type_exists($post_type)) {
        return 0;
    }
    $posts = woodle_get_posts(array('post_type' => $post_type));
    if ($posts) {
        foreach ($posts as $post) {
            if (get_post_meta($post->ID, '_course_id', true) == $course_id) {
                return $post->ID;
            }
        }
    }
    return 0;
}