| 1 |
<?php |
| 2 |
/** |
| 3 |
* File with the class to handle data from SEOPressor. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Import\Plugins |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class WPSEO_Import_SEOPressor. |
| 10 |
* |
| 11 |
* Class with functionality to import & clean SEOPressor post metadata. |
| 12 |
*/ |
| 13 |
class WPSEO_Import_SEOPressor extends WPSEO_Plugin_Importer { |
| 14 |
|
| 15 |
/** |
| 16 |
* The plugin name. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
protected $plugin_name = 'SEOpressor'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Meta key, used in SQL LIKE clause for delete query. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $meta_key = '_seop_settings'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Array of meta keys to detect and import. |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
protected $clone_keys = [ |
| 35 |
[ |
| 36 |
'old_key' => '_seop_settings', |
| 37 |
], |
| 38 |
]; |
| 39 |
|
| 40 |
/** |
| 41 |
* Imports the post meta values to Yoast SEO. |
| 42 |
* |
| 43 |
* @return bool Import success status. |
| 44 |
*/ |
| 45 |
protected function import() { |
| 46 |
// Query for all the posts that have an _seop_settings meta set. |
| 47 |
$query_posts = new WP_Query( 'post_type=any&meta_key=_seop_settings&order=ASC&fields=ids&nopaging=true' ); |
| 48 |
foreach ( $query_posts->posts as $key => $post_id ) { |
| 49 |
$this->import_post_focus_keywords( $post_id ); |
| 50 |
$this->import_seopressor_post_settings( $post_id ); |
| 51 |
} |
| 52 |
|
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Removes all the post meta fields SEOpressor creates. |
| 58 |
* |
| 59 |
* @return bool Cleanup status. |
| 60 |
*/ |
| 61 |
protected function cleanup() { |
| 62 |
global $wpdb; |
| 63 |
|
| 64 |
// If we get to replace the data, let's do some proper cleanup. |
| 65 |
return $wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_seop_%'" ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Imports the data. SEOpressor stores most of the data in one post array, this loops over it. |
| 70 |
* |
| 71 |
* @param int $post_id Post ID. |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
private function import_seopressor_post_settings( $post_id ) { |
| 76 |
$settings = get_post_meta( $post_id, '_seop_settings', true ); |
| 77 |
|
| 78 |
foreach ( |
| 79 |
[ |
| 80 |
'fb_description' => 'opengraph-description', |
| 81 |
'fb_title' => 'opengraph-title', |
| 82 |
'fb_type' => 'og_type', |
| 83 |
'fb_img' => 'opengraph-image', |
| 84 |
'meta_title' => 'title', |
| 85 |
'meta_description' => 'metadesc', |
| 86 |
'meta_canonical' => 'canonical', |
| 87 |
'tw_description' => 'twitter-description', |
| 88 |
'tw_title' => 'twitter-title', |
| 89 |
'tw_image' => 'twitter-image', |
| 90 |
] as $seopressor_key => $yoast_key ) { |
| 91 |
$this->import_meta_helper( $seopressor_key, $yoast_key, $settings, $post_id ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( isset( $settings['meta_rules'] ) ) { |
| 95 |
$this->import_post_robots( $settings['meta_rules'], $post_id ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Imports the focus keywords, and stores them for later use. |
| 101 |
* |
| 102 |
* @param int $post_id Post ID. |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
private function import_post_focus_keywords( $post_id ) { |
| 107 |
// Import the focus keyword. |
| 108 |
$focuskw = trim( get_post_meta( $post_id, '_seop_kw_1', true ) ); |
| 109 |
$this->maybe_save_post_meta( 'focuskw', $focuskw, $post_id ); |
| 110 |
|
| 111 |
// Import additional focus keywords for use in premium. |
| 112 |
$focuskw2 = trim( get_post_meta( $post_id, '_seop_kw_2', true ) ); |
| 113 |
$focuskw3 = trim( get_post_meta( $post_id, '_seop_kw_3', true ) ); |
| 114 |
|
| 115 |
$focus_keywords = []; |
| 116 |
if ( ! empty( $focuskw2 ) ) { |
| 117 |
$focus_keywords[] = $focuskw2; |
| 118 |
} |
| 119 |
if ( ! empty( $focuskw3 ) ) { |
| 120 |
$focus_keywords[] = $focuskw3; |
| 121 |
} |
| 122 |
|
| 123 |
if ( $focus_keywords !== [] ) { |
| 124 |
$this->maybe_save_post_meta( 'focuskeywords', WPSEO_Utils::format_json_encode( $focus_keywords ), $post_id ); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Retrieves the SEOpressor robot value and map this to Yoast SEO values. |
| 130 |
* |
| 131 |
* @param string $meta_rules The meta rules taken from the SEOpressor settings array. |
| 132 |
* @param int $post_id The post id of the current post. |
| 133 |
* |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
private function import_post_robots( $meta_rules, $post_id ) { |
| 137 |
$seopressor_robots = explode( '#|#|#', $meta_rules ); |
| 138 |
$robot_value = $this->get_robot_value( $seopressor_robots ); |
| 139 |
|
| 140 |
// Saving the new meta values for Yoast SEO. |
| 141 |
$this->maybe_save_post_meta( 'meta-robots-noindex', $robot_value['index'], $post_id ); |
| 142 |
$this->maybe_save_post_meta( 'meta-robots-nofollow', $robot_value['follow'], $post_id ); |
| 143 |
$this->maybe_save_post_meta( 'meta-robots-adv', $robot_value['advanced'], $post_id ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Gets the robot config by given SEOpressor robots value. |
| 148 |
* |
| 149 |
* @param array $seopressor_robots The value in SEOpressor that needs to be converted to the Yoast format. |
| 150 |
* |
| 151 |
* @return array The robots values in Yoast format. |
| 152 |
*/ |
| 153 |
private function get_robot_value( $seopressor_robots ) { |
| 154 |
$return = [ |
| 155 |
'index' => 2, |
| 156 |
'follow' => 0, |
| 157 |
'advanced' => '', |
| 158 |
]; |
| 159 |
|
| 160 |
if ( in_array( 'noindex', $seopressor_robots, true ) ) { |
| 161 |
$return['index'] = 1; |
| 162 |
} |
| 163 |
if ( in_array( 'nofollow', $seopressor_robots, true ) ) { |
| 164 |
$return['follow'] = 1; |
| 165 |
} |
| 166 |
foreach ( [ 'noarchive', 'nosnippet', 'noimageindex' ] as $needle ) { |
| 167 |
if ( in_array( $needle, $seopressor_robots, true ) ) { |
| 168 |
$return['advanced'] .= $needle . ','; |
| 169 |
} |
| 170 |
} |
| 171 |
$return['advanced'] = rtrim( $return['advanced'], ',' ); |
| 172 |
|
| 173 |
return $return; |
| 174 |
} |
| 175 |
} |
| 176 |
|