class-import-wpseo.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * File with the class to handle data from wpSEO.de.
  4. *
  5. * @package WPSEO\Admin\Import\Plugins
  6. */
  7. /**
  8. * Class WPSEO_Import_WPSEO.
  9. *
  10. * Class with functionality to import & clean wpSEO.de post metadata.
  11. */
  12. class WPSEO_Import_WPSEO extends WPSEO_Plugin_Importer {
  13. /**
  14. * The plugin name.
  15. *
  16. * @var string
  17. */
  18. protected $plugin_name = 'wpSEO.de';
  19. /**
  20. * Meta key, used in SQL LIKE clause for delete query.
  21. *
  22. * @var string
  23. */
  24. protected $meta_key = '_wpseo_edit_%';
  25. /**
  26. * Array of meta keys to detect and import.
  27. *
  28. * @var array
  29. */
  30. protected $clone_keys = [
  31. [
  32. 'old_key' => '_wpseo_edit_description',
  33. 'new_key' => 'metadesc',
  34. ],
  35. [
  36. 'old_key' => '_wpseo_edit_title',
  37. 'new_key' => 'title',
  38. ],
  39. [
  40. 'old_key' => '_wpseo_edit_canonical',
  41. 'new_key' => 'canonical',
  42. ],
  43. [
  44. 'old_key' => '_wpseo_edit_og_title',
  45. 'new_key' => 'opengraph-title',
  46. ],
  47. [
  48. 'old_key' => '_wpseo_edit_og_description',
  49. 'new_key' => 'opengraph-description',
  50. ],
  51. [
  52. 'old_key' => '_wpseo_edit_og_image',
  53. 'new_key' => 'opengraph-image',
  54. ],
  55. [
  56. 'old_key' => '_wpseo_edit_twittercard_title',
  57. 'new_key' => 'twitter-title',
  58. ],
  59. [
  60. 'old_key' => '_wpseo_edit_twittercard_description',
  61. 'new_key' => 'twitter-description',
  62. ],
  63. [
  64. 'old_key' => '_wpseo_edit_twittercard_image',
  65. 'new_key' => 'twitter-image',
  66. ],
  67. ];
  68. /**
  69. * The values 1 - 6 are the configured values from wpSEO. This array will map the values of wpSEO to our values.
  70. *
  71. * There are some double array like 1-6 and 3-4. The reason is they only set the index value. The follow value is
  72. * the default we use in the cases there isn't a follow value present.
  73. *
  74. * @var array
  75. */
  76. private $robot_values = [
  77. // In wpSEO: index, follow.
  78. 1 => [
  79. 'index' => 2,
  80. 'follow' => 0,
  81. ],
  82. // In wpSEO: index, nofollow.
  83. 2 => [
  84. 'index' => 2,
  85. 'follow' => 1,
  86. ],
  87. // In wpSEO: noindex.
  88. 3 => [
  89. 'index' => 1,
  90. 'follow' => 0,
  91. ],
  92. // In wpSEO: noindex, follow.
  93. 4 => [
  94. 'index' => 1,
  95. 'follow' => 0,
  96. ],
  97. // In wpSEO: noindex, nofollow.
  98. 5 => [
  99. 'index' => 1,
  100. 'follow' => 1,
  101. ],
  102. // In wpSEO: index.
  103. 6 => [
  104. 'index' => 2,
  105. 'follow' => 0,
  106. ],
  107. ];
  108. /**
  109. * Imports wpSEO settings.
  110. *
  111. * @return bool Import success status.
  112. */
  113. protected function import() {
  114. $status = parent::import();
  115. if ( $status ) {
  116. $this->import_post_robots();
  117. $this->import_taxonomy_metas();
  118. }
  119. return $status;
  120. }
  121. /**
  122. * Removes wpseo.de post meta's.
  123. *
  124. * @return bool Cleanup status.
  125. */
  126. protected function cleanup() {
  127. $this->cleanup_term_meta();
  128. $result = $this->cleanup_post_meta();
  129. return $result;
  130. }
  131. /**
  132. * Detects whether there is post meta data to import.
  133. *
  134. * @return bool Boolean indicating whether there is something to import.
  135. */
  136. protected function detect() {
  137. if ( parent::detect() ) {
  138. return true;
  139. }
  140. global $wpdb;
  141. $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->options} WHERE option_name LIKE 'wpseo_category_%'" );
  142. if ( $count !== '0' ) {
  143. return true;
  144. }
  145. return false;
  146. }
  147. /**
  148. * Imports the robot values from WPSEO plugin. These have to be converted to the Yoast format.
  149. *
  150. * @return void
  151. */
  152. private function import_post_robots() {
  153. $query_posts = new WP_Query( 'post_type=any&meta_key=_wpseo_edit_robots&order=ASC&fields=ids&nopaging=true' );
  154. if ( ! empty( $query_posts->posts ) ) {
  155. foreach ( array_values( $query_posts->posts ) as $post_id ) {
  156. $this->import_post_robot( $post_id );
  157. }
  158. }
  159. }
  160. /**
  161. * Gets the wpSEO robot value and map this to Yoast SEO values.
  162. *
  163. * @param integer $post_id The post id of the current post.
  164. *
  165. * @return void
  166. */
  167. private function import_post_robot( $post_id ) {
  168. $wpseo_robots = get_post_meta( $post_id, '_wpseo_edit_robots', true );
  169. $robot_value = $this->get_robot_value( $wpseo_robots );
  170. // Saving the new meta values for Yoast SEO.
  171. $this->maybe_save_post_meta( 'meta-robots-noindex', $robot_value['index'], $post_id );
  172. $this->maybe_save_post_meta( 'meta-robots-nofollow', $robot_value['follow'], $post_id );
  173. }
  174. /**
  175. * Imports the taxonomy metas from wpSEO.
  176. *
  177. * @return void
  178. */
  179. private function import_taxonomy_metas() {
  180. $terms = get_terms( get_taxonomies(), [ 'hide_empty' => false ] );
  181. $tax_meta = get_option( 'wpseo_taxonomy_meta' );
  182. foreach ( $terms as $term ) {
  183. $this->import_taxonomy_description( $tax_meta, $term->taxonomy, $term->term_id );
  184. $this->import_taxonomy_robots( $tax_meta, $term->taxonomy, $term->term_id );
  185. }
  186. update_option( 'wpseo_taxonomy_meta', $tax_meta );
  187. }
  188. /**
  189. * Imports the meta description to Yoast SEO.
  190. *
  191. * @param array $tax_meta The array with the current metadata.
  192. * @param string $taxonomy String with the name of the taxonomy.
  193. * @param string $term_id The ID of the current term.
  194. *
  195. * @return void
  196. */
  197. private function import_taxonomy_description( &$tax_meta, $taxonomy, $term_id ) {
  198. $description = get_option( 'wpseo_' . $taxonomy . '_' . $term_id, false );
  199. if ( $description !== false ) {
  200. // Import description.
  201. $tax_meta[ $taxonomy ][ $term_id ]['wpseo_desc'] = $description;
  202. }
  203. }
  204. /**
  205. * Imports the robot value to Yoast SEO.
  206. *
  207. * @param array $tax_meta The array with the current metadata.
  208. * @param string $taxonomy String with the name of the taxonomy.
  209. * @param string $term_id The ID of the current term.
  210. *
  211. * @return void
  212. */
  213. private function import_taxonomy_robots( &$tax_meta, $taxonomy, $term_id ) {
  214. $wpseo_robots = get_option( 'wpseo_' . $taxonomy . '_' . $term_id . '_robots', false );
  215. if ( $wpseo_robots === false ) {
  216. return;
  217. }
  218. // The value 1, 2 and 6 are the index values in wpSEO.
  219. $new_robot_value = 'noindex';
  220. if ( in_array( (int) $wpseo_robots, [ 1, 2, 6 ], true ) ) {
  221. $new_robot_value = 'index';
  222. }
  223. $tax_meta[ $taxonomy ][ $term_id ]['wpseo_noindex'] = $new_robot_value;
  224. }
  225. /**
  226. * Deletes the wpSEO taxonomy meta data.
  227. *
  228. * @param string $taxonomy String with the name of the taxonomy.
  229. * @param string $term_id The ID of the current term.
  230. *
  231. * @return void
  232. */
  233. private function delete_taxonomy_metas( $taxonomy, $term_id ) {
  234. delete_option( 'wpseo_' . $taxonomy . '_' . $term_id );
  235. delete_option( 'wpseo_' . $taxonomy . '_' . $term_id . '_robots' );
  236. }
  237. /**
  238. * Gets the robot config by given wpSEO robots value.
  239. *
  240. * @param string $wpseo_robots The value in wpSEO that needs to be converted to the Yoast format.
  241. *
  242. * @return string The correct robot value.
  243. */
  244. private function get_robot_value( $wpseo_robots ) {
  245. if ( array_key_exists( $wpseo_robots, $this->robot_values ) ) {
  246. return $this->robot_values[ $wpseo_robots ];
  247. }
  248. return $this->robot_values[1];
  249. }
  250. /**
  251. * Deletes wpSEO postmeta from the database.
  252. *
  253. * @return bool Cleanup status.
  254. */
  255. private function cleanup_post_meta() {
  256. global $wpdb;
  257. // If we get to replace the data, let's do some proper cleanup.
  258. return $wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_wpseo_edit_%'" );
  259. }
  260. /**
  261. * Cleans up the wpSEO term meta.
  262. *
  263. * @return void
  264. */
  265. private function cleanup_term_meta() {
  266. $terms = get_terms( get_taxonomies(), [ 'hide_empty' => false ] );
  267. foreach ( $terms as $term ) {
  268. $this->delete_taxonomy_metas( $term->taxonomy, $term->term_id );
  269. }
  270. }
  271. }