| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle comments data migration. |
| 4 |
* |
| 5 |
* @package UpStream\Migrations |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace UpStream\Migrations; |
| 9 |
|
| 10 |
// Prevent direct access. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* This class transform all existent project comments into WordPress Comments. |
| 17 |
* |
| 18 |
* @since 1.13.0 |
| 19 |
*/ |
| 20 |
final class Comments_Migration { |
| 21 |
|
| 22 |
/** |
| 23 |
* Run the migration if needed. |
| 24 |
* |
| 25 |
* @since 1.13.0 |
| 26 |
* @static |
| 27 |
*/ |
| 28 |
public static function run() { |
| 29 |
if ( ! self::is_migration_needed() ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
$rowset = self::fetch_metas_rowset(); |
| 34 |
if ( count( $rowset ) > 0 ) { |
| 35 |
global $wpdb; |
| 36 |
|
| 37 |
foreach ( $rowset as $project_id => $legacy_comments ) { |
| 38 |
foreach ( $legacy_comments as $legacy_comment ) { |
| 39 |
if ( ! isset( $legacy_comment['created_by'] ) |
| 40 |
|| ! isset( $legacy_comment['created_time'] ) |
| 41 |
|| ! isset( $legacy_comment['comment'] ) |
| 42 |
|| empty( $legacy_comment['comment'] ) |
| 43 |
) { |
| 44 |
continue; |
| 45 |
} |
| 46 |
|
| 47 |
$user = get_user_by( 'id', $legacy_comment['created_by'] ); |
| 48 |
|
| 49 |
$date = \DateTime::createFromFormat( 'U', $legacy_comment['created_time'] ); |
| 50 |
|
| 51 |
$new_comment_data = array( |
| 52 |
'comment_post_ID' => $project_id, |
| 53 |
'comment_author' => $user->display_name, |
| 54 |
'comment_author_email' => $user->user_email, |
| 55 |
'comment_date_gmt' => $date->format( 'Y-m-d H:i:s' ), |
| 56 |
'comment_content' => $legacy_comment['comment'], |
| 57 |
'user_id' => $user->ID, |
| 58 |
'comment_approved' => 1, |
| 59 |
); |
| 60 |
|
| 61 |
$new_comment_data['comment_date'] = $date->format( 'Y-m-d H:i:s' ); |
| 62 |
|
| 63 |
$wpdb->insert( $wpdb->prefix . 'comments', $new_comment_data ); |
| 64 |
|
| 65 |
update_comment_meta( $wpdb->insert_id, 'type', 'project' ); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
update_option( 'upstream:migration.comments', 'yes' ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Check if migration is needed. |
| 75 |
* |
| 76 |
* @since 1.13.0 |
| 77 |
* @access private |
| 78 |
* @static |
| 79 |
*/ |
| 80 |
private static function is_migration_needed() { |
| 81 |
return (string) get_option( 'upstream:migration.comments' ) !== 'yes'; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Fetch all discussion metas from all projects. |
| 86 |
* It returns an associative array with project ID as keys and a list of comments as values. |
| 87 |
* |
| 88 |
* @since 1.13.0 |
| 89 |
* @access private |
| 90 |
* @static |
| 91 |
* |
| 92 |
* @global $wpdb |
| 93 |
* |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
private static function fetch_metas_rowset() { |
| 97 |
global $wpdb; |
| 98 |
|
| 99 |
$data = array(); |
| 100 |
|
| 101 |
// Fetch all existent discussion metas. |
| 102 |
$metas_rowset = $wpdb->get_results( |
| 103 |
' |
| 104 |
SELECT `post_id`, `meta_key`, `meta_value` |
| 105 |
FROM `' . $wpdb->prefix . 'postmeta` |
| 106 |
WHERE `meta_key` = "_upstream_project_discussion"' |
| 107 |
); |
| 108 |
|
| 109 |
if ( count( $metas_rowset ) > 0 ) { |
| 110 |
foreach ( $metas_rowset as $meta ) { |
| 111 |
$project_id = (int) $meta->post_id; |
| 112 |
$meta_value = (array) maybe_unserialize( $meta->meta_value ); |
| 113 |
|
| 114 |
if ( ! empty( $meta_value ) ) { |
| 115 |
$meta_value = isset( $meta_value[0] ) ? $meta_value[0] : $meta_value; |
| 116 |
} |
| 117 |
|
| 118 |
if ( ! empty( $meta_value ) && is_array( $meta_value ) ) { |
| 119 |
if ( ! isset( $data[ $project_id ] ) ) { |
| 120 |
$data[ $project_id ] = array(); |
| 121 |
} |
| 122 |
|
| 123 |
$data[ $project_id ][] = $meta_value; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $data; |
| 129 |
} |
| 130 |
} |
| 131 |
|