| 1114 | /** |
| 1115 | * phpBB post import repair tasks |
| 1116 | * |
| 1117 | * @since bbPress (rXXXX) |
| 1118 | * |
| 1119 | * @global WPDB $wpdb |
| 1120 | * @uses is_wp_error() To check if the executed query returned {@link WP_Error} |
| 1121 | * @uses update_post_meta() To update topic meta |
| 1122 | * @uses update_post_meta() To update reply meta |
| 1123 | * @uses get_users() To get the imported phpBB bots |
| 1124 | * @uses wp_delete_user() To delete the imported phpBB users (bots, crawlers & anonymous) |
| 1125 | * @return array An array of the status code and the message |
| 1126 | */ |
| 1127 | function bbp_admin_repair_import_phpbb() { |
| 1128 | global $wpdb; |
| 1129 | |
| 1130 | /** phpBB Anonymous Topics ************************************************/ |
| 1131 | |
| 1132 | $statement = __( 'Running phpBB post import tasks… %s', 'bbpress' ); |
| 1133 | $r_count = 0; |
| 1134 | $t_count = 0; |
| 1135 | $u_count = 0; |
| 1136 | |
| 1137 | // Get the phpBB anonymous topic IDs |
| 1138 | $topic_ids = $wpdb->query( "SELECT `posts` .`ID` AS `post_id`, '_bbp_anonymous_name', `postmeta` . `meta_value` AS `meta_value` |
| 1139 | FROM `{$wpdb->posts}` AS `posts` |
| 1140 | LEFT JOIN `{$wpdb->postmeta}` AS `postmeta` |
| 1141 | ON `posts`.`ID` = `postmeta`.`post_id` |
| 1142 | AND `postmeta`.`meta_key` = '_bbp_phpbb_topic_first_poster_name' |
| 1143 | LEFT JOIN `{$wpdb->usermeta}` AS `usermeta` |
| 1144 | ON `usermeta` . `user_id` = `posts`.`post_author` |
| 1145 | AND `usermeta` . `meta_key` = '_bbp_phpbb_user_type' |
| 1146 | WHERE `posts`.`post_type` = 'topic' |
| 1147 | AND `usermeta` . `meta_value` = '2';" ); |
| 1148 | |
| 1149 | // Bail if topic IDs returned an error |
| 1150 | if ( is_wp_error( $topic_ids ) ) { |
| 1151 | return array( 1, sprintf( $statement, __( 'Failed!', 'bbpress' ) ) ); |
| 1152 | } |
| 1153 | |
| 1154 | // Stash the last results |
| 1155 | $results = $wpdb->last_result; |
| 1156 | |
| 1157 | // Update each anonymous topic |
| 1158 | foreach ( $results as $anon_topic ) { |
| 1159 | |
| 1160 | // Update topic meta _bbp_anonymous_name with anonymous user name |
| 1161 | update_post_meta( $anon_topic->post_id, '_bbp_anonymous_name', $anon_topic->meta_value ); |
| 1162 | |
| 1163 | // Bump the count for output later |
| 1164 | ++$t_count; |
| 1165 | } |
| 1166 | |
| 1167 | unset( $topic_ids, $results, $anon_topic ); |
| 1168 | |
| 1169 | /** phpBB Anonymous Replies ***********************************************/ |
| 1170 | |
| 1171 | // Get the phpBB anonymous reply IDs |
| 1172 | $reply_ids = $wpdb->query( "SELECT `posts` .`ID` AS `post_id`, '_bbp_anonymous_name', `postmeta` . `meta_value` AS `meta_value` |
| 1173 | FROM `{$wpdb->posts}` AS `posts` |
| 1174 | LEFT JOIN `{$wpdb->postmeta}` AS `postmeta` |
| 1175 | ON `posts`.`ID` = `postmeta`.`post_id` |
| 1176 | AND `postmeta`.`meta_key` = '_bbp_phpbb_post_username ' |
| 1177 | LEFT JOIN `{$wpdb->usermeta}` AS `usermeta` |
| 1178 | ON `usermeta` . `user_id` = `posts`.`post_author` |
| 1179 | AND `usermeta` . `meta_key` = '_bbp_phpbb_user_type' |
| 1180 | WHERE `posts`.`post_type` = 'reply' |
| 1181 | AND `usermeta` . `meta_value` = '2';" ); |
| 1182 | |
| 1183 | // Bail if reply IDs returned an error |
| 1184 | if ( is_wp_error( $reply_ids ) ) { |
| 1185 | return array( 2, sprintf( $statement, __( 'Failed!', 'bbpress' ) ) ); |
| 1186 | } |
| 1187 | |
| 1188 | // Stash the last results |
| 1189 | $results = $wpdb->last_result; |
| 1190 | |
| 1191 | // Update each anonymous reply |
| 1192 | foreach ( $results as $anon_reply ) { |
| 1193 | |
| 1194 | // Update reply meta _bbp_anonymous_name with anonymous user name |
| 1195 | update_post_meta( $anon_reply->post_id, '_bbp_anonymous_name', $anon_reply->meta_value ); |
| 1196 | |
| 1197 | // Bump the count for output later |
| 1198 | ++$r_count; |
| 1199 | } |
| 1200 | |
| 1201 | unset( $reply_ids, $results, $anon_reply ); |
| 1202 | |
| 1203 | /** phpBB Anonymous Users *************************************************/ |
| 1204 | |
| 1205 | // Get the phpBB anonymous, bots and crawler user IDs |
| 1206 | $bbp_anonymous_id = 0; |
| 1207 | $phpbb_bots = get_users(array( 'meta_key' => '_bbp_phpbb_user_type','meta_value' => '2', ) ); |
| 1208 | |
| 1209 | // Bail if user IDs returned an error |
| 1210 | if ( is_wp_error( $phpbb_bots ) ) { |
| 1211 | return array( 3, sprintf( $statement, __( 'Failed!', 'bbpress' ) ) ); |
| 1212 | } |
| 1213 | |
| 1214 | // Delete each anonymous user |
| 1215 | foreach ( $phpbb_bots as $phpbb_bot ) { |
| 1216 | |
| 1217 | // Delete each imported bot user replacing posts with bbPress anonymous user ID |
| 1218 | wp_delete_user( $phpbb_bot->ID, $bbp_anonymous_id ); |
| 1219 | |
| 1220 | // Bump the count for output later |
| 1221 | ++$u_count; |
| 1222 | } |
| 1223 | |
| 1224 | unset( $phpbb_bots, $results, $phpbb_bot ); |
| 1225 | |
| 1226 | // Complete results |
| 1227 | $result = sprintf( __( 'Complete! %s anonymous topics updated, %s anonymous replies updated and %s user bots deleted.', 'bbpress' ), bbp_number_format( $t_count ), bbp_number_format( $r_count ), bbp_number_format( $u_count ) ); |
| 1228 | return array( 0, sprintf( $statement, $result ) ); |
| 1229 | } |
| 1230 | |