table = acadlix()->helper()->acadlix_wp_prefix('posts'); } /** * Boot method to automatically apply the post type condition. */ public function scopeOfLesson($query) { return $query->where('post_type', self::$postType); } public function getCategoryAttribute() { return acadlix()->model()->lessonCategory()->get_lesson_category($this->ID); } public function getRenderedPostContentAttribute() { return acadlix()->helper()->renderShortCode($this->post_content); } public function metas() { return $this->hasMany(acadlix()->model()->wpPostMeta(), 'post_id', 'ID'); } // Define the dedicated single meta relationship public function lesson_type_meta() { return $this->hasOne(acadlix()->model()->wpPostMeta(), 'post_id', 'ID') ->where('meta_key', '_acadlix_lesson_type'); } public function lesson_categories() { return $this->belongsToMany( acadlix()->model()->wpTermTaxonomy(), acadlix()->helper()->acadlix_wp_prefix('term_relationships'), 'object_id', 'term_taxonomy_id' ) ->where('taxonomy', self::$categoryTaxonomy) ->with('term'); } public function getRenderedMetasAttribute() { $metas = $this->metas; if (empty($metas)) { return []; } $keyValueArray = []; foreach ($metas as $meta) { // Ensure meta_key and meta_value exist in the object if (isset($meta['meta_key'], $meta['meta_value'])) { $key = $meta['meta_key']; $value = $meta['meta_value']; // Decode JSON if applicable if (is_string($value) && $decoded = json_decode($value, true)) { $value = $decoded; } $keyValueArray[$key] = $value; } } $renderedMetas = !empty($keyValueArray) && is_array($keyValueArray) ? acadlix()->helper()->cpt()->acadlix_remome_prefix_meta_keys($keyValueArray, 'lesson') : []; return $renderedMetas; } public function getResourceCountAttribute() { $resources = $this->metas()->where('meta_key', '_acadlix_lesson_resources')->first(); return empty($resources) ? 0 : count($resources->meta_value); } public function author() { return $this->belongsTo(acadlix()->model()->wpUsers(), 'post_author', 'ID'); } public static function insertLesson(array $data, array $meta = []) { $data['post_content'] = wp_slash($data['post_content'] ?? ""); $data = wp_parse_args($data, [ 'post_title' => '', 'post_content' => '', 'post_status' => 'publish', 'post_type' => self::$postType, 'post_author' => 1, ]); // Add meta data to the 'meta_input' argument for wp_insert_post. if (!empty($meta)) { $data['meta_input'] = $meta; } // Insert the post and return the ID or WP_Error. $postId = wp_insert_post($data); return $postId; } public static function updateLesson(int $postId, array $data = [], array $meta = []) { $data['post_content'] = wp_slash($data['post_content'] ?? ""); // Parse default arguments for the lesson update. $data = wp_parse_args($data, [ 'ID' => $postId, ]); // Add meta data to the 'meta_input' argument for wp_update_post. if (!empty($meta)) { $data['meta_input'] = $meta; } // Update the post and return the result or WP_Error. $result = wp_update_post($data, true); return $result; } public static function deleteLesson(int $postId) { // Check if post exists $post = get_post($postId); if (!$post || $post->post_type !== self::$postType) { return new \WP_Error('invalid_post', 'Invalid post ID or not a lesson post type.'); } // Delete related course section content (cs_content) posts $courseSectionContents = acadlix()->model()->courseSectionContent()->getByLessonId($postId); if ($courseSectionContents) { foreach ($courseSectionContents as $csc) { acadlix()->model()->courseSectionContent()->deleteCourseSectionContent($csc->ID); } } // Delete post $result = wp_delete_post($postId, true); if (!$result) { return new \WP_Error('delete_failed', 'Failed to delete lesson.'); } return true; } public static function assignCategory(int $postId, int|array $categoryIds) { // Check if post exists $post = get_post($postId); if (!$post || $post->post_type !== self::$postType) { return new \WP_Error('invalid_post', __('Invalid post ID or not a lesson post type.', 'acadlix')); } if (!is_array($categoryIds)) { $categoryIds = [$categoryIds]; } $categoryIds = array_map('intval', $categoryIds); $categoryIds = array_unique($categoryIds); $term_taxonomy_ids = wp_set_object_terms($postId, $categoryIds, self::$categoryTaxonomy); return $term_taxonomy_ids; } } }