| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Core; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Utils\Base; |
| 6 |
use WPDeveloper\BetterDocs\Utils\Helper; |
| 7 |
|
| 8 |
class KBMigration extends Base { |
| 9 |
public function migrate() { |
| 10 |
$existing_plugins = $this->knowledge_base_plugins(); |
| 11 |
$existing_plugins_data = $this->existing_plugins_data( $existing_plugins[0][0] ); |
| 12 |
if ( isset( $existing_plugins_data['name'] ) && isset( $existing_plugins_data['url'] ) ) { |
| 13 |
if ( $existing_plugins_data['name'] === 'echo-knowledge-base' ) { |
| 14 |
$this->eco_knowledgebase_migration(); |
| 15 |
} elseif ( $existing_plugins_data['name'] === 'pressapps-knowledge-base' ) { |
| 16 |
$this->pressapps_migration(); |
| 17 |
} |
| 18 |
deactivate_plugins( $existing_plugins_data['url'] ); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
public function knowledge_base_plugins() { |
| 23 |
$plugins = []; |
| 24 |
|
| 25 |
if ( Helper::is_plugin_active( 'wedocs/wedocs.php' ) ) { |
| 26 |
$plugins[] = ['wedocs', 'weDocs']; |
| 27 |
} |
| 28 |
if ( Helper::is_plugin_active( 'bsf-docs/bsf-docs.php' ) ) { |
| 29 |
$plugins[] = ['bsf-docs', 'BSF docs']; |
| 30 |
} |
| 31 |
if ( Helper::is_plugin_active( 'documentor-lite/documentor-lite.php' ) ) { |
| 32 |
$plugins[] = ['documentor-lite', 'Documentor']; |
| 33 |
} |
| 34 |
if ( Helper::is_plugin_active( 'echo-knowledge-base/echo-knowledge-base.php' ) ) { |
| 35 |
$plugins[] = ['echo-knowledge-base', 'Echo Knowledge Base']; |
| 36 |
} |
| 37 |
if ( Helper::is_plugin_active( 'pressapps-knowledge-base/pressapps-knowledge-base.php' ) ) { |
| 38 |
$plugins[] = ['pressapps-knowledge-base', 'PressApps Knowledge Base']; |
| 39 |
} |
| 40 |
|
| 41 |
return $plugins; |
| 42 |
} |
| 43 |
|
| 44 |
public function insert_terms_hierarchically( $existing_term, $new_term, $parentId = 0 ) { |
| 45 |
$into = []; |
| 46 |
$cats = get_terms( $existing_term, ['hide_empty' => false] ); |
| 47 |
if ( $cats ) { |
| 48 |
foreach ( $cats as $i => $cat ) { |
| 49 |
if ( $cat->parent == $parentId ) { |
| 50 |
$into[$cat->term_id] = $cat; |
| 51 |
unset( $cats[$i] ); |
| 52 |
$doc_parent_term = term_exists( $cat->name, $new_term ); |
| 53 |
wp_insert_term( |
| 54 |
$cat->name, |
| 55 |
$new_term, |
| 56 |
[ |
| 57 |
'alias_of' => $cat->slug, |
| 58 |
'description' => $cat->description, |
| 59 |
'slug' => $cat->slug, |
| 60 |
'parent' => $cat->parent |
| 61 |
] |
| 62 |
); |
| 63 |
} |
| 64 |
} |
| 65 |
if ( $cats ) { |
| 66 |
foreach ( $cats as $i => $cat ) { |
| 67 |
$get_existing_term = get_term_by( 'id', $cat->parent, $existing_term ); |
| 68 |
$doc_parent_term = term_exists( $get_existing_term->name, $new_term ); |
| 69 |
wp_insert_term( |
| 70 |
$cat->name, |
| 71 |
$new_term, |
| 72 |
[ |
| 73 |
'alias_of' => $cat->slug, |
| 74 |
'description' => $cat->description, |
| 75 |
'slug' => $cat->slug, |
| 76 |
'parent' => $doc_parent_term['term_id'] |
| 77 |
] |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public function insert_posts( $existing_post, $existing_cat, $existing_tag ) { |
| 85 |
$args = [ |
| 86 |
'post_type' => $existing_post, |
| 87 |
'post_status' => 'any', |
| 88 |
'posts_per_page' => -1 |
| 89 |
]; |
| 90 |
$postslist = get_posts( $args ); |
| 91 |
if ( $postslist ) { |
| 92 |
foreach ( $postslist as $post ) { |
| 93 |
// Create post object |
| 94 |
if ( ! get_page_by_title( $post->post_title, 'OBJECT', 'docs' ) ) { |
| 95 |
$post_args = [ |
| 96 |
'post_type' => 'docs', |
| 97 |
'post_title' => $post->post_title, |
| 98 |
'post_content' => $post->post_content, |
| 99 |
'post_status' => $post->post_status, |
| 100 |
'post_author' => $post->post_author, |
| 101 |
'post_date' => $post->post_date, |
| 102 |
'post_date_gmt' => $post->post_date_gmt, |
| 103 |
'post_excerpt' => $post->post_excerpt, |
| 104 |
'comment_status' => $post->comment_status, |
| 105 |
'ping_status' => $post->ping_status, |
| 106 |
'post_password' => $post->post_password, |
| 107 |
'post_name' => $post->post_name, |
| 108 |
'to_ping' => $post->to_ping, |
| 109 |
'pinged' => $post->pinged, |
| 110 |
'post_modified' => $post->post_modified, |
| 111 |
'post_modified_gmt' => $post->post_modified_gmt, |
| 112 |
'post_content_filtered' => $post->post_content_filtered, |
| 113 |
'post_parent' => $post->post_parent, |
| 114 |
'post_mime_type' => $post->post_mime_type, |
| 115 |
'comment_count' => $post->comment_count, |
| 116 |
'filter' => $post->filter |
| 117 |
]; |
| 118 |
// Insert the post into the database |
| 119 |
$result = wp_insert_post( $post_args ); |
| 120 |
if ( $result && ! is_wp_error( $result ) ) { |
| 121 |
$cat_list = wp_get_post_terms( $post->ID, $existing_cat, ['fields' => 'all'] ); |
| 122 |
if ( $cat_list ) { |
| 123 |
$post_id = $result; |
| 124 |
wp_set_object_terms( $post_id, [$cat_list['0']->name], 'doc_category', false ); |
| 125 |
} |
| 126 |
$tag_list = wp_get_post_terms( $post->ID, $existing_tag, ['fields' => 'all'] ); |
| 127 |
if ( $tag_list ) { |
| 128 |
$post_id = $result; |
| 129 |
wp_set_object_terms( $post_id, [$tag_list['0']->name], 'doc_tag', false ); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
public function existing_plugins_data( $plugins ) { |
| 138 |
$plugins_data = []; |
| 139 |
if ( $plugins === 'wedocs' ) { |
| 140 |
$plugins_data['name'] = 'wedocs'; |
| 141 |
$plugins_data['url'] = 'wedocs/wedocs.php'; |
| 142 |
} |
| 143 |
if ( $plugins === 'bsf-docs' ) { |
| 144 |
$plugins_data['name'] = 'bsf-docs'; |
| 145 |
$plugins_data['url'] = 'bsf-docs/bsf-docs.php'; |
| 146 |
} |
| 147 |
if ( $plugins === 'documentor-lite' ) { |
| 148 |
$plugins_data['name'] = 'documentor-lite'; |
| 149 |
$plugins_data['url'] = 'documentor-lite/documentor-lite.php'; |
| 150 |
} |
| 151 |
if ( $plugins === 'echo-knowledge-base' ) { |
| 152 |
$plugins_data['name'] = 'echo-knowledge-base'; |
| 153 |
$plugins_data['url'] = 'echo-knowledge-base/echo-knowledge-base.php'; |
| 154 |
} |
| 155 |
if ( $plugins === 'pressapps-knowledge-base' ) { |
| 156 |
$plugins_data['name'] = 'pressapps-knowledge-base'; |
| 157 |
$plugins_data['url'] = 'pressapps-knowledge-base/pressapps-knowledge-base.php'; |
| 158 |
} |
| 159 |
return $plugins_data; |
| 160 |
} |
| 161 |
|
| 162 |
public function eco_knowledgebase_migration() { |
| 163 |
$this->insert_terms_hierarchically( 'epkb_post_type_1_category', 'doc_category' ); |
| 164 |
$this->insert_terms_hierarchically( 'epkb_post_type_1_tag', 'doc_tag' ); |
| 165 |
$this->insert_posts( 'epkb_post_type_1', 'epkb_post_type_1_category', 'epkb_post_type_1_tag' ); |
| 166 |
} |
| 167 |
|
| 168 |
public function pressapps_migration() { |
| 169 |
$this->insert_terms_hierarchically( 'knowledgebase_category', 'doc_category' ); |
| 170 |
$this->insert_terms_hierarchically( 'knowledgebase_tags', 'doc_tag' ); |
| 171 |
$this->insert_posts( 'knowledgebase', 'knowledgebase_category', 'knowledgebase_tags' );; |
| 172 |
} |
| 173 |
} |
| 174 |
|