WPSEO_Import_WPSEO

Class WPSEO_Import_WPSEO.

Defined (1)

The class is defined in the following location(s).

/admin/class-import-wpseo.php  
  1. class WPSEO_Import_WPSEO extends WPSEO_Import_External { 
  2.  
  3. /** 
  4. * Import wpSEO settings 
  5. */ 
  6. public function __construct() { 
  7. parent::__construct(); 
  8.  
  9. $this->import_post_metas(); 
  10. $this->import_taxonomy_metas(); 
  11.  
  12. $this->set_msg( 
  13. sprintf( 
  14. /** translators: 1: link open tag; 2: link close tag. */ 
  15. __( 'wpSEO data successfully imported. Would you like to %1$sdisable the wpSEO plugin%2$s?', 'wordpress-seo' ),  
  16. '<a href="' . esc_url( admin_url( 'admin.php?page=wpseo_tools&tool=import-export&deactivate_wpseo=1#top#import-seo' ) ) . '">',  
  17. '</a>' 
  18. ); 
  19.  
  20.  
  21. /** 
  22. * Import the post meta values to Yoast SEO by replacing the wpSEO fields by Yoast SEO fields 
  23. */ 
  24. private function import_post_metas() { 
  25. WPSEO_Meta::replace_meta( '_wpseo_edit_title', WPSEO_Meta::$meta_prefix . 'title', $this->replace ); 
  26. WPSEO_Meta::replace_meta( '_wpseo_edit_description', WPSEO_Meta::$meta_prefix . 'metadesc', $this->replace ); 
  27. WPSEO_Meta::replace_meta( '_wpseo_edit_keywords', WPSEO_Meta::$meta_prefix . 'keywords', $this->replace ); 
  28. WPSEO_Meta::replace_meta( '_wpseo_edit_canonical', WPSEO_Meta::$meta_prefix . 'canonical', $this->replace ); 
  29.  
  30. $this->import_post_robots(); 
  31.  
  32. /** 
  33. * Importing the robot values from WPSEO plugin. These have to be converted to the Yoast format. 
  34. */ 
  35. private function import_post_robots() { 
  36. $query_posts = new WP_Query( 'post_type=any&meta_key=_wpseo_edit_robots&order=ASC' ); 
  37.  
  38. if ( ! empty( $query_posts->posts ) ) { 
  39. foreach ( $query_posts->posts as $post ) { 
  40. $this->import_post_robot( $post->ID ); 
  41.  
  42. /** 
  43. * Getting the wpSEO robot value and map this to Yoast SEO values. 
  44. * @param integer $post_id The post id of the current post. 
  45. */ 
  46. private function import_post_robot( $post_id ) { 
  47. $wpseo_robots = get_post_meta( $post_id, '_wpseo_edit_robots', true ); 
  48.  
  49. // Does the value exists in our mapping. 
  50. if ( $robot_value = $this->get_robot_value( $wpseo_robots ) ) { 
  51. // Saving the new meta values for Yoast SEO. 
  52. WPSEO_Meta::set_value( $robot_value['index'], 'meta-robots-noindex', $post_id ); 
  53. WPSEO_Meta::set_value( $robot_value['follow'], 'meta-robots-nofollow', $post_id ); 
  54.  
  55. $this->delete_post_robot( $post_id ); 
  56.  
  57. /** 
  58. * Delete the wpSEO robot values, because they aren't needed anymore. 
  59. * @param integer $post_id The post id of the current post. 
  60. */ 
  61. private function delete_post_robot( $post_id ) { 
  62. if ( $this->replace ) { 
  63. delete_post_meta( $post_id, '_wpseo_edit_robots' ); 
  64.  
  65. /** 
  66. * Import the taxonomy metas from wpSEO 
  67. */ 
  68. private function import_taxonomy_metas() { 
  69. $terms = get_terms( get_taxonomies(), array( 'hide_empty' => false ) ); 
  70. $tax_meta = get_option( 'wpseo_taxonomy_meta' ); 
  71.  
  72. foreach ( $terms as $term ) { 
  73. $this->import_taxonomy_description( $tax_meta, $term->taxonomy, $term->term_id ); 
  74. $this->import_taxonomy_robots( $tax_meta, $term->taxonomy, $term->term_id ); 
  75. $this->delete_taxonomy_metas( $term->taxonomy, $term->term_id ); 
  76.  
  77. update_option( 'wpseo_taxonomy_meta', $tax_meta ); 
  78.  
  79. /** 
  80. * Import the meta description to Yoast SEO 
  81. * @param array $tax_meta The array with the current metadata. 
  82. * @param string $taxonomy String with the name of the taxonomy. 
  83. * @param string $term_id The ID of the current term. 
  84. */ 
  85. private function import_taxonomy_description( & $tax_meta, $taxonomy, $term_id ) { 
  86. $description = get_option( 'wpseo_' . $taxonomy . '_' . $term_id, false ); 
  87. if ( $description !== false ) { 
  88. // Import description. 
  89. $tax_meta[ $taxonomy ][ $term_id ]['wpseo_desc'] = $description; 
  90.  
  91. /** 
  92. * Import the robot value to Yoast SEO 
  93. * @param array $tax_meta The array with the current metadata. 
  94. * @param string $taxonomy String with the name of the taxonomy. 
  95. * @param string $term_id The ID of the current term. 
  96. */ 
  97. private function import_taxonomy_robots( & $tax_meta, $taxonomy, $term_id ) { 
  98. $wpseo_robots = get_option( 'wpseo_' . $taxonomy . '_' . $term_id . '_robots', false ); 
  99. if ( $wpseo_robots !== false ) { 
  100. // The value 1, 2 and 6 are the index values in wpSEO. 
  101. $new_robot_value = ( in_array( $wpseo_robots, array( 1, 2, 6 ) ) ) ? 'index' : 'noindex'; 
  102.  
  103. $tax_meta[ $taxonomy ][ $term_id ]['wpseo_noindex'] = $new_robot_value; 
  104.  
  105. /** 
  106. * Delete the wpSEO taxonomy meta data. 
  107. * @param string $taxonomy String with the name of the taxonomy. 
  108. * @param string $term_id The ID of the current term. 
  109. */ 
  110. private function delete_taxonomy_metas( $taxonomy, $term_id ) { 
  111. if ( $this->replace ) { 
  112. delete_option( 'wpseo_' . $taxonomy . '_' . $term_id ); 
  113. delete_option( 'wpseo_' . $taxonomy . '_' . $term_id . '_robots' ); 
  114.  
  115. /** 
  116. * Getting the robot config by given wpSEO robots value. 
  117. * @param string $wpseo_robots The value in wpSEO that needs to be converted to the Yoast format. 
  118. * @return array 
  119. */ 
  120. private function get_robot_value( $wpseo_robots ) { 
  121. static $robot_values; 
  122.  
  123. if ( $robot_values === null ) { 
  124. /** 
  125. * The values 1 - 6 are the configured values from wpSEO. This array will map the values of wpSEO to our values. 
  126. * There are some double array like 1-6 and 3-4. The reason is they only set the index value. The follow value is 
  127. * the default we use in the cases there isn't a follow value present. 
  128. * @var array 
  129. */ 
  130. $robot_values = array( 
  131. 1 => array( 'index' => 2, 'follow' => 0 ), // In wpSEO: index, follow. 
  132. 2 => array( 'index' => 2, 'follow' => 1 ), // In wpSEO: index, nofollow. 
  133. 3 => array( 'index' => 1, 'follow' => 0 ), // In wpSEO: noindex. 
  134. 4 => array( 'index' => 1, 'follow' => 0 ), // In wpSEO: noindex, follow. 
  135. 5 => array( 'index' => 1, 'follow' => 1 ), // In wpSEO: noindex, nofollow. 
  136. 6 => array( 'index' => 2, 'follow' => 0 ), // In wpSEO: index. 
  137. ); 
  138.  
  139. if ( array_key_exists( $wpseo_robots, $robot_values ) ) { 
  140. return $robot_values[ $wpseo_robots ]; 
  141.  
  142. return array( 'index' => 2, 'follow' => 0 );