Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/tools/importers/LessonImporter.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Lesson Importer
4 + *
5 + * @package TutorPro\Tools
6 + * @author Themeum<support@themeum.com>
7 + * @link https://themeum.com
8 + * @since 3.6.0
9 + */
10 +
11 + namespace TutorPro\Tools;
12 +
13 + use Tutor\Helpers\QueryHelper;
14 + use Tutor\Lesson;
15 +
16 + if ( ! defined( 'ABSPATH' ) ) {
17 + exit;
18 + }
19 +
20 + /**
21 + * Lesson Importer class.
22 + */
23 + class LessonImporter {
24 +
25 + /**
26 + * Prepare lesson post meta.
27 + *
28 + * @since 3.8.1
29 + *
30 + * @param array $lesson_metas the lesson post meta values.
31 + * @param bool $keep_media_files whether to keep media files.
32 + *
33 + * @return void
34 + */
35 + public function prepare_lesson_meta( $lesson_metas, $keep_media_files ) {
36 + $lesson_metas = array_map( fn( $val ) => $val[0], $lesson_metas );
37 +
38 + Helper::reset_post_data( 'video' );
39 +
40 + if ( isset( $lesson_metas['_video'] ) ) {
41 + $video_data = $lesson_metas['_video'];
42 +
43 + if ( isset( $video_data['source'] ) && 'html5' === $video_data['source'] && $keep_media_files ) {
44 + $video_url = $video_data['source_html5'];
45 + $upload_data = Helper::upload_file_by_url( $video_url );
46 + if ( ! is_wp_error( $upload_data ) ) {
47 + $video_data['source_video_id'] = $upload_data['id'];
48 + $video_data['source_html5'] = $upload_data['url'];
49 + }
50 + }
51 +
52 + $_POST['video'] = $video_data;
53 + }
54 +
55 + Helper::reset_post_data( 'content_drip_settings' );
56 +
57 + if ( isset( $lesson_metas['_content_drip_settings'] ) ) {
58 + $_POST['content_drip_settings'] = $lesson_metas['_content_drip_settings'];
59 + }
60 +
61 + Helper::reset_post_data( '_is_preview' );
62 + // Handle lesson post meta.
63 + if ( isset( $lesson_metas['_is_preview'] ) ) {
64 + $_POST['_is_preview'] = $lesson_metas['_is_preview'];
65 + }
66 + }
67 +
68 + /**
69 + * Insert lesson post.
70 + *
71 + * @since 3.8.1
72 + *
73 + * @param array $content the lesson post content.
74 + * @param bool $keep_media_files whether to keep media files.
75 + * @param int $parent_id the parent course id.
76 + *
77 + * @return int|\WP_Error
78 + */
79 + public function insert_lesson( $content, $keep_media_files = false, $parent_id = 0 ) {
80 + global $wpdb;
81 +
82 + $previous_lesson_id = $content['ID'] ?? 0;
83 + $meta = $content['meta'] ?? null;
84 + $thumbnail_url = $content['thumbnail_url'] ?? null;
85 + $attachment_links = $content['attachment_links'] ?? null;
86 + $attachment_ids = array();
87 +
88 + $courses_map = ContentMapHandler::get_content_map()['courses'] ?? null;
89 +
90 + $content = Helper::unset_post_data( $content );
91 +
92 + if ( $attachment_links && $keep_media_files ) {
93 + $attachment_ids = Helper::get_attachment_ids_from_urls( $attachment_links );
94 + }
95 +
96 + $lesson_id = wp_insert_post( $content, true, true );
97 +
98 + if ( is_wp_error( $lesson_id ) ) {
99 + return $lesson_id;
100 + }
101 +
102 + if ( $courses_map && $parent_id ) {
103 + if ( isset( $courses_map[ $parent_id ] ) ) {
104 + $map = $courses_map[ $parent_id ];
105 + $map[ tutor()->lesson_post_type ][ $previous_lesson_id ] = $lesson_id;
106 + $courses_map[ $parent_id ] = $map;
107 + }
108 + }
109 +
110 + if ( $meta ) {
111 + $meta = Helper::prepare_meta( $lesson_id, $meta, $keep_media_files );
112 + }
113 +
114 + if ( $attachment_ids ) {
115 + $meta[] = array(
116 + 'post_id' => $lesson_id,
117 + 'meta_key' => '_tutor_attachments',
118 + 'meta_value' => maybe_serialize( $attachment_ids ),
119 + );
120 + }
121 +
122 + try {
123 + QueryHelper::insert_multiple_rows( $wpdb->postmeta, $meta, false, false );
124 + } catch ( \Throwable $th ) {
125 + ErrorHandler::set_error( $content['post_type'], 'Error saving meta value for lesson : ' . $content['post_title'] );
126 + }
127 +
128 + if ( $thumbnail_url && $keep_media_files ) {
129 + Helper::save_post_thumbnail( $thumbnail_url, $lesson_id );
130 + }
131 +
132 + ContentMapHandler::update_content_map( 'courses', $courses_map );
133 +
134 + unset( $meta );
135 + unset( $courses_map );
136 +
137 + return $lesson_id;
138 + }
139 + }
140 +