forked from studiopress/seo-data-transporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
156 lines (113 loc) · 4.77 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* This function converts $old meta_key entries in the postmeta table into $new entries.
*
* It first checks to see what records for the $new meta_key already exist,
* storing the corresponding post_id values in an array. When the conversion
* happens, rows that contain a post_id in that array will be ignored, to
* avoid duplicate $new meta_key entries.
*
* The $old entries will be left as-is if $delete_old is left false. If set
* to true, the $old entries will be deleted, rather than retained.
*
* The function returns an object for error detection, and the number of affected rows.
*/
function seodt_meta_key_convert( $old = '', $new = '', $delete_old = false ) {
global $wpdb;
$output = new stdClass;
if ( !$old || !$new ) {
$output->WP_Error = 1;
return $output;
}
// See which records we need to ignore, if any
$exclude = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $new ) );
// If no records to ignore, we'll do a basic UPDATE and DELETE
if ( !$exclude ) {
$output->updated = $wpdb->update( $wpdb->postmeta, array( 'meta_key' => $new ), array( 'meta_key' => $old ) );
$output->deleted = $delete_old ? $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $old ) ) : 0;
$output->ignored = 0;
}
// Else, do a more complex UPDATE and DELETE
else {
foreach ( (array)$exclude as $key => $value ) {
$not_in[] = $value->post_id;
}
$not_in = implode(', ', (array)$not_in );
$output->updated = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_key = %s WHERE meta_key = %s AND post_id NOT IN ($not_in)", $new, $old ) );
$output->deleted = $delete_old ? $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $old ) ) : 0;
$output->ignored = count( $exclude );
}
return $output;
}
/**
* This function cycles through all compatible SEO entries of two platforms,
* performs a seodt_meta_key_convert() conversion for each key, and returns
* the results as an object.
*
* It first checks for compatible entries between the two platforms. When it
* finds compatible entries, it loops through them and preforms the conversion
* on each entry.
*/
function seodt_post_meta_convert( $old_platform = '', $new_platform = '', $delete_old = false ) {
global $_seodt_platforms;
$output = new stdClass;
if ( empty( $_seodt_platforms[$old_platform] ) || empty( $_seodt_platforms[$new_platform] ) ) {
$output->WP_Error = 1;
return $output;
}
$output->updated = 0;
$output->deleted = 0;
$output->ignored = 0;
foreach ( (array)$_seodt_platforms[$old_platform] as $label => $meta_key ) {
// skip iterations where no $new analog exists
if ( empty( $_seodt_platforms[$new_platform][$label] ) )
continue;
// set $old and $new meta_key values
$old = $_seodt_platforms[$old_platform][$label];
$new = $_seodt_platforms[$new_platform][$label];
// convert
$result = seodt_meta_key_convert( $old, $new, $delete_old );
// error check
if ( is_wp_error( $result ) )
continue;
// update total updated/ignored count
$output->updated = $output->updated + (int)$result->updated;
$output->ignored = $output->ignored + (int)$result->ignored;
}
return $output;
}
/**
* This function analyzes two platforms to see what Compatible elements they share,
* what data can be converted from one to the other, and which elements to ignore (future).
*/
function seodt_post_meta_analyze( $old_platform = '', $new_platform = '' ) {
global $wpdb, $_seodt_platforms;
$output = new stdClass;
if ( empty( $_seodt_platforms[$old_platform] ) || empty( $_seodt_platforms[$new_platform] ) ) {
$output->WP_Error = 1;
return $output;
}
$output->update = 0;
$output->ignore = 0;
$outout->elements = '';
foreach ( (array)$_seodt_platforms[$old_platform] as $label => $meta_key ) {
// skip iterations where no $new analog exists
if ( empty( $_seodt_platforms[$new_platform][$label] ) )
continue;
$elements[] = $label;
// see which records to ignore, if any
// $ignore = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key ) );
// see which records to update, if any
$update = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key ) );
// count items in returned arrays
// $ignore = count( (array)$ignore );
$update = count( (array)$update );
// calculate update/ignore by comparison
// $update = ( (int)$update > (int)$ignore ) ? ( (int)$update - (int)$ignore ) : 0;
// update output numbers
$output->update = $output->update + (int)$update;
$output->ignore = $output->ignore + (int)$ignore;
} // endforeach
$output->elements = $elements;
return $output;
}