Skip to content

Commit 08c22fb

Browse files
committed
Merge branch 'dev' into multiple_assignment_issue
2 parents 667c30a + 9e74ec7 commit 08c22fb

File tree

6 files changed

+98
-23
lines changed

6 files changed

+98
-23
lines changed

classes/LDtoTutorMigration.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ public function ld_migrate_all_data_to_tutor() {
114114
}
115115

116116
// Send response & clear on finish.
117-
$status = MigrationLogger::get_log();
118-
MigrationLogger::clear_log();
119-
wp_send_json_success( $status );
117+
$response = MigrationLogger::get_log();
118+
wp_send_json_success( $response );
120119
}
121120

122121
wp_send_json_error();

inc/ActionHandler.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct() {
3030
add_action( 'tlmt_quiz_migrated', array( $this, 'migrate_post_meta' ), 10, 2 );
3131
add_action( 'tlmt_attach_product', array( $this, 'migrate_products' ), 10, 2 );
3232
add_action( 'tlmt_student_progress_migrated', array( $this, 'migrate_student_progress' ) );
33-
add_action( 'tlmt_assignment_migrated', array( $this, 'migrate_post_meta' ), 10, 2 );
33+
add_action( 'tlmt_assignment_migrated', array( $this, 'migrate_assignment_meta' ) );
3434
add_action( 'tlml_delete_learndash_quiz_questions', array( $this, 'delete_learndash_quiz_questions' ) );
3535
}
3636

@@ -65,6 +65,25 @@ public function migrate_post_meta( $post_id, $migration_type ) {
6565
}
6666
}
6767

68+
/**
69+
* Migrate assignment meta
70+
*
71+
* @since 2.3.0
72+
*
73+
* @param int $post_id Post id.
74+
*
75+
* @return void
76+
*/
77+
public function migrate_assignment_meta( $post_id ) {
78+
$post = get_post( $post_id );
79+
$meta_obj = tlmt_get_meta_obj( ContentTypes::ASSIGNMENT_META, MigrationTypes::LD_TO_TUTOR );
80+
try {
81+
$meta_obj->migrate( $post_id );
82+
} catch ( \Throwable $th ) {
83+
$this->update_migration_error( ContentTypes::ASSIGNMENT_META, "Failed to migrate meta data for this post: $post->post_title post_type: $post->post_type" );
84+
}
85+
}
86+
6887
/**
6988
* Get meta content type by post type
7089
*
@@ -76,20 +95,15 @@ public function migrate_post_meta( $post_id, $migration_type ) {
7695
*/
7796
public function get_meta_content_type( string $post_type ) {
7897
$content_type = null;
79-
switch ( $post_type ) {
80-
case tutor()->course_post_type:
98+
99+
if ( $post_type ) {
100+
if ( tutor()->course_post_type === $post_type || 'sfwd-courses' === $post_type ) {
81101
$content_type = ContentTypes::COURSE_META;
82-
break;
83-
case tutor()->lesson_post_type:
102+
} elseif ( tutor()->lesson_post_type === $post_type || 'sfwd-lessons' === $post_type ) {
84103
$content_type = ContentTypes::LESSON_META;
85-
break;
86-
case tutor()->quiz_post_type:
104+
} elseif ( tutor()->quiz_post_type === $post_type || 'sfwd-quiz' === $post_type ) {
87105
$content_type = ContentTypes::QUIZ_META;
88-
break;
89-
90-
default:
91-
// code...
92-
break;
106+
}
93107
}
94108

95109
return $content_type;

inc/LDMigration/PostMeta/AssignmentMeta.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function migrate( int $post_id, string $post_type = 'sfwd-lessons' ) {
7373
}
7474

7575
try {
76-
QueryHelper::insert_multiple_rows( $wpdb->postmeta, $meta );
76+
QueryHelper::insert_multiple_rows( $wpdb->postmeta, $meta, false, false );
7777
} catch ( \Throwable $th ) {
7878
throw $th;
7979
}

inc/LDMigration/PostMeta/CourseMeta.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function migrate( int $post_id ) {
5252
$meta = $this->ld_to_tutor_meta_map( $migrate_able_meta );
5353
if ( is_array( $meta ) && count( $meta ) ) {
5454
try {
55-
QueryHelper::insert_multiple_rows( $wpdb->postmeta, $meta );
55+
QueryHelper::insert_multiple_rows( $wpdb->postmeta, $meta, false, false );
5656
} catch ( \Throwable $th ) {
5757
throw $th;
5858
}
@@ -149,7 +149,7 @@ private function ld_to_tutor_meta_map( array $meta ): array {
149149
$ld_keys = array_keys( $meta );
150150
foreach ( $ld_tutor_meta_map as $key => $value ) {
151151
if ( in_array( $value, $ld_keys ) ) {
152-
$tutor_meta_map[ $key ] = $meta[ $value ];
152+
$tutor_meta_map[ $key ] = sanitize_textarea_field( $meta[ $value ] );
153153
}
154154
}
155155

inc/LDMigration/PostMeta/LessonMeta.php

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function migrate( int $post_id ) {
4747

4848
if ( is_array( $meta ) && count( $meta ) ) {
4949
try {
50-
QueryHelper::insert_multiple_rows( $wpdb->postmeta, $meta );
50+
QueryHelper::insert_multiple_rows( $wpdb->postmeta, $meta, false, false );
5151
} catch ( \Throwable $th ) {
5252
throw $th;
5353
}
@@ -73,9 +73,7 @@ public function get_meta(): array {
7373

7474
if ( isset( $meta['sfwd-lessons_lesson_video_enabled'] ) && 'on' === $meta['sfwd-lessons_lesson_video_enabled'] ) {
7575
if ( ! empty( $meta['sfwd-lessons_lesson_video_url'] ) ) {
76-
$tutor_meta['_video'] = array(
77-
'source_external_url' => $meta['sfwd-lessons_lesson_video_url'],
78-
);
76+
$tutor_meta['_video'] = maybe_serialize( $this->get_video_source_by_url( $meta['sfwd-lessons_lesson_video_url'] ) );
7977
}
8078
}
8179

@@ -136,4 +134,68 @@ public function get_migrate_able_meta(): array {
136134

137135
return array_intersect_key( $ld_meta, array_flip( $migrate_able_meta ) );
138136
}
137+
138+
/**
139+
* Get video source by url
140+
*
141+
* @since 2.3.0
142+
*
143+
* @param string $url Video url, sortcode or embed code.
144+
*
145+
* @return array
146+
*/
147+
public function get_video_source_by_url( string $url ): array {
148+
$source = 'external_url';
149+
$source_youtube = '';
150+
$source_vimeo = '';
151+
$source_shortcode = '';
152+
$source_embedded = '';
153+
$source_external_url = '';
154+
$source_html5 = '';
155+
156+
if ( preg_match( '/youtube\.com\/watch\?v=([^\&\s]+)/', $url, $matches ) || preg_match( '/youtu\.be\/([^\&\s]+)/', $url, $matches ) ) {
157+
$source = 'youtube';
158+
$source_youtube = $url;
159+
} elseif ( preg_match( '/vimeo\.com\/(\d+)/', $url, $matches ) ) {
160+
$source = 'vimeo';
161+
$source_vimeo = $url;
162+
} elseif ( preg_match( '/^\[.*\]$/s', trim( $url ) ) ) {
163+
// Shortcode starts and ends with square brackets.
164+
$source = 'shortcode';
165+
$source_shortcode = $url;
166+
} elseif ( preg_match( '/<iframe.+src="(.+?)"/s', $url, $matches ) ) {
167+
// Embedded iframe.
168+
$source = 'embedded';
169+
$source_embedded = $url;
170+
} elseif ( filter_var( $url, FILTER_VALIDATE_URL ) ) {
171+
// Generic external URL (e.g., mp4).
172+
$source = 'external_url';
173+
$source_external_url = $url;
174+
} else {
175+
// If none matched, maybe it's HTML5 file (local path).
176+
$source = 'html5';
177+
$source_html5 = $url;
178+
}
179+
180+
$video_info = array(
181+
'source' => $source,
182+
'source_video_id' => '',
183+
'poster' => '',
184+
'poster_url' => '',
185+
'source_html5' => $source_html5,
186+
'source_external_url' => $source_external_url,
187+
'source_shortcode' => $source_shortcode,
188+
'source_youtube' => $source_youtube,
189+
'source_vimeo' => $source_vimeo,
190+
'source_embedded' => $source_embedded,
191+
'runtime' => array(
192+
'hours' => 0,
193+
'minutes' => 0,
194+
'seconds' => 0,
195+
),
196+
);
197+
198+
return $video_info;
199+
}
200+
139201
}

inc/LDMigration/PostMeta/QuizMeta.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function migrate( int $post_id ) {
5656
}
5757

5858
try {
59-
QueryHelper::insert_multiple_rows( $wpdb->postmeta, $meta );
59+
QueryHelper::insert_multiple_rows( $wpdb->postmeta, $meta, false, false );
6060
} catch ( \Throwable $th ) {
6161
throw $th;
6262
}

0 commit comments

Comments
 (0)