Project

General

Profile

Feature #12155 » dirt-tapor-migration.php

Boone Gorges, 2020-09-28 06:01 PM

 
1
<?php
2

    
3
function register_tapor_schema() {
4
	register_post_type(
5
		'tapor_tool',
6
		[
7
			'label'  => __( 'TAPoR Tools', 'tapor-client' ),
8
			'public' => true,
9
			'has_archive' => true,
10
			'rewrite' => array(
11
				'slug' => _x( 'tool', 'Tool rewrite slug', 'tapor-client' ),
12
				'with_front' => false,
13
			),
14
		]
15
	);
16

    
17
	register_taxonomy(
18
		'tapor_used_by_user',
19
		'tapor_tool',
20
		[
21
			'label'  => __( 'TAPoR Tool Users', 'tapor-client' ),
22
			'public' => false,
23
		]
24
	);
25

    
26
	register_taxonomy(
27
		'tapor_category',
28
		'tapor_tool',
29
		[
30
			'label'  => __( 'TAPoR Tool Category', 'tapor-client' ),
31
			'public' => true,
32
		]
33
	);
34
}
35

    
36
global $wpdb;
37

    
38
$dry_run = true;
39
if ( ! empty( $args[0] ) && 'no-dry-run' === $args[0] ) {
40
	$dry_run = false;
41
}
42

    
43
if ( $dry_run ) {
44
	WP_CLI::log( 'No "no-dry-run" argument found. This will be a dry run.' );
45
}
46

    
47
register_tapor_schema();
48

    
49
$tool_ids   = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'ddc_tool'" );
50
$no_matches = [];
51
$migrated   = [];
52
foreach ( $tool_ids as $tool_id ) {
53
	$post = get_post( $tool_id );
54

    
55
	// Fetch matching TAPoR tools by keyword, then narrow down to one with the same title.
56
	$keyword_match_query   = wp_remote_get( 'http://tapor.ca/api/tools?query=' . $post->post_title );
57
	$keyword_match_results = json_decode( wp_remote_retrieve_body( $keyword_match_query ) );
58

    
59
	$match = null;
60
	foreach ( $keyword_match_results->tools as $keyword_match ) {
61
		if ( $keyword_match->name === $post->post_title ) {
62
			$match = $keyword_match;
63
			break;
64
		}
65
	}
66

    
67
	if ( ! $match ) {
68
		$no_matches[ $tool_id ] = $post->post_title;
69
		continue;
70
	}
71

    
72
	$postarr = [
73
		'post_type'    => 'tapor_tool',
74
		'post_title'   => $match->name,
75
		'post_status'  => 'publish',
76
		'post_content' => $match->detail,
77
	];
78

    
79
	if ( $dry_run ) {
80
		continue;
81
	}
82

    
83
	$post_id = wp_insert_post( $postarr );
84

    
85
	if ( ! $post_id || is_wp_error( $post_id ) ) {
86
		var_dump( $postarr ); die;
87
	}
88

    
89
	update_post_meta( $post_id, 'tapor_id', $match->id );
90
	update_post_meta( $post_id, 'tapor_link', 'http://tapor.ca/tools/' . $match->id );
91
	update_post_meta( $post_id, 'tapor_image', 'http://tapor.ca/' . $match->image_url );
92

    
93
	// Separate request for tool attributes.
94
	$attributes_query   = wp_remote_get( 'http://tapor.ca/api/tools/' . $match->id . '/attributes' );
95
	$attributes_results = json_decode( wp_remote_retrieve_body( $attributes_query ) );
96

    
97
	$categories         = [];
98
	if ( $attributes_results ) {
99
		foreach ( $attributes_results as $attribute_type ) {
100
			if ( 'Type of analysis' === $attribute_type->name ) {
101
				foreach ( $attribute_type->selected as $selected ) {
102
					foreach ( $attribute_type->attribute_values as $attribute_value ) {
103
						if ( $attribute_value->id === $selected->id ) {
104
							$categories[] = $attribute_value->name;
105
							break;
106
						}
107
					}
108
				}
109

    
110
				break;
111
			}
112
		}
113
	}
114

    
115
	wp_set_object_terms( $post_id, $categories, 'tapor_category' );
116

    
117
	$retval = [
118
		'title'     => $match->name,
119
		'tapor_id'  => $match->id,
120
		'new_wp_id' => $post_id,
121
		'old_wp_id' => $post->ID,
122
		'users'     => [],
123
	];
124

    
125
	// Now we migrate 'use' data.
126
	$users = ddc_get_users_of_tool( $post->ID );
127
	foreach ( $users as $user_id ) {
128
		wp_set_object_terms( $post_id, 'tapor_tool_is_used_by_user_' . $user_id, 'tapor_used_by_user' );
129

    
130
		$retval['users'][] = $user_id;
131
	}
132

    
133
	$migrated[] = $retval;
134
}
135

    
136
if ( $no_matches ) {
137
	WP_CLI::log( "\n" );
138
	WP_CLI::log( 'The following sites have no correlates in TAPoR:' );
139
	print_r( $no_matches );
140
}
141

    
142
if ( ! $dry_run ) {
143
	WP_CLI::log( "\n" );
144
	WP_CLI::log( "Migrated the following tools:" );
145
	print_r( $migrated );
146
}
147

    
(2-2/2)