class-import-external.php
5.68 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* @package WPSEO\Admin\Import\External
*/
/**
* Class WPSEO_Import_External
*
* Class with functionality to import Yoast SEO settings from other plugins
*/
class WPSEO_Import_External {
/**
* Whether or not to delete old data
*
* @var boolean
*/
protected $replace;
/**
* Message about the import
*
* @var string
*/
public $msg = '';
/**
* Class constructor
*
* @param boolean $replace
*/
public function __construct( $replace = false ) {
$this->replace = $replace;
WPSEO_Options::initialize();
}
/**
* Convenience function to set import message
*
* @param string $msg
*/
protected function set_msg( $msg ) {
if ( ! empty( $this->msg ) ) {
$this->msg .= PHP_EOL;
}
$this->msg .= $msg;
}
/**
* Deletes an option depending on the class replace state
*
* @param string $option
*/
protected function perhaps_delete( $option ) {
if ( $this->replace ) {
delete_option( $option );
}
}
/**
* Import HeadSpace SEO settings
*/
public function import_headspace() {
global $wpdb;
WPSEO_Meta::replace_meta( '_headspace_description', WPSEO_Meta::$meta_prefix . 'metadesc', $this->replace );
WPSEO_Meta::replace_meta( '_headspace_keywords', WPSEO_Meta::$meta_prefix . 'metakeywords', $this->replace );
WPSEO_Meta::replace_meta( '_headspace_page_title', WPSEO_Meta::$meta_prefix . 'title', $this->replace );
/**
* @todo [JRF => whomever] verify how headspace sets these metas ( 'noindex', 'nofollow', 'noarchive', 'noodp', 'noydir' )
* and if the values saved are concurrent with the ones we use (i.e. 0/1/2)
*/
WPSEO_Meta::replace_meta( '_headspace_noindex', WPSEO_Meta::$meta_prefix . 'meta-robots-noindex', $this->replace );
WPSEO_Meta::replace_meta( '_headspace_nofollow', WPSEO_Meta::$meta_prefix . 'meta-robots-nofollow', $this->replace );
/*
* @todo - [JRF => whomever] check if this can be done more efficiently by querying only the meta table
* possibly directly changing it using concat on the existing values
*/
$posts = $wpdb->get_results( "SELECT ID FROM $wpdb->posts" );
if ( is_array( $posts ) && $posts !== array() ) {
foreach ( $posts as $post ) {
$custom = get_post_custom( $post->ID );
$robotsmeta_adv = '';
if ( isset( $custom['_headspace_noarchive'] ) ) {
$robotsmeta_adv .= 'noarchive,';
}
if ( isset( $custom['_headspace_noodp'] ) ) {
$robotsmeta_adv .= 'noodp,';
}
if ( isset( $custom['_headspace_noydir'] ) ) {
$robotsmeta_adv .= 'noydir';
}
$robotsmeta_adv = preg_replace( '`,$`', '', $robotsmeta_adv );
WPSEO_Meta::set_value( 'meta-robots-adv', $robotsmeta_adv, $post->ID );
}
}
if ( $this->replace ) {
$hs_meta = array( 'noarchive', 'noodp', 'noydir' );
foreach ( $hs_meta as $meta ) {
delete_post_meta_by_key( '_headspace_' . $meta );
}
unset( $hs_meta, $meta );
}
$this->set_msg( __( 'HeadSpace2 data successfully imported', 'wordpress-seo' ) );
}
/**
* Import from Joost's old robots meta plugin
*/
public function import_robots_meta() {
global $wpdb;
$posts = $wpdb->get_results( "SELECT ID, robotsmeta FROM $wpdb->posts" );
if ( ! $posts ) {
$this->set_msg( __( 'Error: no Robots Meta data found to import.', 'wordpress-seo' ) );
return;
}
if ( is_array( $posts ) && $posts !== array() ) {
foreach ( $posts as $post ) {
// Sync all possible settings.
if ( $post->robotsmeta ) {
$pieces = explode( ',', $post->robotsmeta );
foreach ( $pieces as $meta ) {
switch ( $meta ) {
case 'noindex':
WPSEO_Meta::set_value( 'meta-robots-noindex', '1', $post->ID );
break;
case 'index':
WPSEO_Meta::set_value( 'meta-robots-noindex', '2', $post->ID );
break;
case 'nofollow':
WPSEO_Meta::set_value( 'meta-robots-nofollow', '1', $post->ID );
break;
}
}
}
}
}
$this->set_msg( __( sprintf( 'Robots Meta values imported. We recommend %sdisabling the Robots-Meta plugin%s to avoid any conflicts.', '<a href="' . esc_url( admin_url( 'admin.php?page=wpseo_tools&tool=import-export&deactivate_robots_meta=1#top#import-other' ) ) . '">', '</a>' ), 'wordpress-seo' ) );
}
/**
* Import from old Yoast RSS Footer plugin
*/
public function import_rss_footer() {
$optold = get_option( 'RSSFooterOptions' );
$optnew = get_option( 'wpseo_rss' );
if ( $optold['position'] == 'after' ) {
if ( $optnew['rssafter'] === '' || $optnew['rssafter'] === WPSEO_Options::get_default( 'wpseo_rss', 'rssafter' ) ) {
$optnew['rssafter'] = $optold['footerstring'];
}
}
else {
/* @internal Uncomment the second part if a default would be given to the rssbefore value */
if ( $optnew['rssbefore'] === '' /*|| $optnew['rssbefore'] === WPSEO_Options::get_default( 'wpseo_rss', 'rssbefore' )*/ ) {
$optnew['rssbefore'] = $optold['footerstring'];
}
}
update_option( 'wpseo_rss', $optnew );
$this->set_msg( __( 'RSS Footer options imported successfully.', 'wordpress-seo' ) );
}
/**
* Import from Yoast Breadcrumbs plugin
*/
public function import_yoast_breadcrumbs() {
$optold = get_option( 'yoast_breadcrumbs' );
$optnew = get_option( 'wpseo_internallinks' );
if ( is_array( $optold ) && $optold !== array() ) {
foreach ( $optold as $opt => $val ) {
if ( is_bool( $val ) && $val === true ) {
$optnew[ 'breadcrumbs-' . $opt ] = true;
}
else {
$optnew[ 'breadcrumbs-' . $opt ] = $val;
}
}
unset( $opt, $val );
update_option( 'wpseo_internallinks', $optnew );
$this->set_msg( __( 'Yoast Breadcrumbs options imported successfully.', 'wordpress-seo' ) );
}
else {
$this->set_msg( __( 'Yoast Breadcrumbs options could not be found', 'wordpress-seo' ) );
}
}
}