PluginProbe
UpStream: a Project Management Plugin for WordPress / 1.39.0
UpStream: a Project Management Plugin for WordPress v1.39.0
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / includes / up-comments-migration.php

up-comments-migration.php in UpStream: a Project Management Plugin for WordPress 1.39.0, at includes/up-comments-migration.php

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