class-import-rankmath.php
4.63 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* File with the class to handle data from RankMath.
*
* @package WPSEO\Admin\Import\Plugins
*/
/**
* Class with functionality to import RankMath post metadata.
*/
class WPSEO_Import_RankMath extends WPSEO_Plugin_Importer {
/**
* The plugin name.
*
* @var string
*/
protected $plugin_name = 'RankMath';
/**
* Meta key, used in SQL LIKE clause for delete query.
*
* @var string
*/
protected $meta_key = 'rank_math_%';
/**
* Array of meta keys to detect and import.
*
* @var array
*/
protected $clone_keys = [
[
'old_key' => 'rank_math_description',
'new_key' => 'metadesc',
],
[
'old_key' => 'rank_math_title',
'new_key' => 'title',
],
[
'old_key' => 'rank_math_canonical_url',
'new_key' => 'canonical',
],
[
'old_key' => 'rank_math_primary_category',
'new_key' => 'primary_category',
],
[
'old_key' => 'rank_math_facebook_title',
'new_key' => 'opengraph-title',
],
[
'old_key' => 'rank_math_facebook_description',
'new_key' => 'opengraph-description',
],
[
'old_key' => 'rank_math_facebook_image',
'new_key' => 'opengraph-image',
],
[
'old_key' => 'rank_math_facebook_image_id',
'new_key' => 'opengraph-image-id',
],
[
'old_key' => 'rank_math_twitter_title',
'new_key' => 'twitter-title',
],
[
'old_key' => 'rank_math_twitter_description',
'new_key' => 'twitter-description',
],
[
'old_key' => 'rank_math_twitter_image',
'new_key' => 'twitter-image',
],
[
'old_key' => 'rank_math_twitter_image_id',
'new_key' => 'twitter-image-id',
],
[
'old_key' => 'rank_math_focus_keyword',
'new_key' => 'focuskw',
],
];
/**
* Handles post meta data to import.
*
* @return bool Import success status.
*/
protected function import() {
global $wpdb;
// Replace % with %% as their variables are the same except for that.
$wpdb->query( "UPDATE $wpdb->postmeta SET meta_value = REPLACE( meta_value, '%', '%%' ) WHERE meta_key IN ( 'rank_math_description', 'rank_math_title' )" );
$this->import_meta_robots();
$return = $this->meta_keys_clone( $this->clone_keys );
// Return %% to % so our import is non-destructive.
$wpdb->query( "UPDATE $wpdb->postmeta SET meta_value = REPLACE( meta_value, '%%', '%' ) WHERE meta_key IN ( 'rank_math_description', 'rank_math_title' )" );
if ( $return ) {
$this->import_settings();
}
return $return;
}
/**
* RankMath stores robots meta quite differently, so we have to parse it out.
*/
private function import_meta_robots() {
global $wpdb;
$post_metas = $wpdb->get_results( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = 'rank_math_robots'" );
foreach ( $post_metas as $post_meta ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- Reason: We can't control the form in which Rankmath sends the data.
$robots_values = unserialize( $post_meta->meta_value );
foreach ( [ 'noindex', 'nofollow' ] as $directive ) {
$directive_key = array_search( $directive, $robots_values, true );
if ( $directive_key !== false ) {
update_post_meta( $post_meta->post_id, '_yoast_wpseo_meta-robots-' . $directive, 1 );
unset( $robots_values[ $directive_key ] );
}
}
if ( count( $robots_values ) > 0 ) {
$value = implode( ',', $robots_values );
update_post_meta( $post_meta->post_id, '_yoast_wpseo_meta-robots-adv', $value );
}
}
}
/**
* Imports some of the RankMath settings.
*/
private function import_settings() {
$settings = [
'title_separator' => 'separator',
'homepage_title' => 'title-home-wpseo',
'homepage_description' => 'metadesc-home-wpseo',
'author_archive_title' => 'title-author-wpseo',
'date_archive_title' => 'title-archive-wpseo',
'search_title' => 'title-search-wpseo',
'404_title' => 'title-404-wpseo',
'pt_post_title' => 'title-post',
'pt_page_title' => 'title-page',
];
$options = get_option( 'rank-math-options-titles' );
foreach ( $settings as $import_setting_key => $setting_key ) {
if ( ! empty( $options[ $import_setting_key ] ) ) {
$value = $options[ $import_setting_key ];
// Make sure replace vars work.
$value = str_replace( '%', '%%', $value );
WPSEO_Options::set( $setting_key, $value );
}
}
}
/**
* Removes the plugin data from the database.
*
* @return bool Cleanup status.
*/
protected function cleanup() {
$return = parent::cleanup();
if ( $return ) {
global $wpdb;
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'rank-math-%'" );
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '%rank_math%'" );
}
return $return;
}
}