| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Core; |
| 4 |
|
| 5 |
use WP_Query; |
| 6 |
use WPDeveloper\BetterDocs\Utils\Base; |
| 7 |
use WPDeveloper\BetterDocs\Utils\Database; |
| 8 |
use WPDeveloper\BetterDocs\Dependencies\DI\Container; |
| 9 |
|
| 10 |
class Migration extends Base { |
| 11 |
/** |
| 12 |
* Database |
| 13 |
* @var Database |
| 14 |
*/ |
| 15 |
private $database; |
| 16 |
private $settings; |
| 17 |
private $container; |
| 18 |
|
| 19 |
public function __construct( Container $container ) { |
| 20 |
$this->container = $container; |
| 21 |
$this->database = $container->get( Database::class ); |
| 22 |
$this->settings = $container->get( Settings::class ); |
| 23 |
} |
| 24 |
|
| 25 |
public function init( $version ) { |
| 26 |
if ( $version > 250 ) { |
| 27 |
for ( $_version = 250; $_version <= $version; $_version++ ) { |
| 28 |
if ( method_exists( $this, "v$_version" ) ) { |
| 29 |
call_user_func( [ $this, "v$_version" ] ); |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
$this->search_migration(); |
| 35 |
|
| 36 |
/** |
| 37 |
* Settings Migration |
| 38 |
*/ |
| 39 |
$this->settings->migration( $version ); |
| 40 |
} |
| 41 |
|
| 42 |
public function search_migration() { |
| 43 |
global $wpdb; |
| 44 |
if ( ! $this->database->get( 'betterdocs_search_data_migration', false ) ) { |
| 45 |
$search_data = $this->database->get( 'betterdocs_search_data' ); |
| 46 |
if ( ! empty( $search_data ) ) { |
| 47 |
$search_data_arr = unserialize( $search_data ); |
| 48 |
foreach ( $search_data_arr as $key => $value ) { |
| 49 |
$args = [ |
| 50 |
'post_type' => 'docs', |
| 51 |
'post_status' => 'publish', |
| 52 |
'posts_per_page' => -1, |
| 53 |
'suppress_filters' => true, |
| 54 |
's' => $key |
| 55 |
]; |
| 56 |
|
| 57 |
$loop = new WP_Query( $args ); |
| 58 |
if ( $loop->have_posts() ) { |
| 59 |
$count = $value; |
| 60 |
$not_found_count = 0; |
| 61 |
} else { |
| 62 |
$count = 0; |
| 63 |
$not_found_count = $value; |
| 64 |
} |
| 65 |
|
| 66 |
$keyword = $wpdb->get_var( |
| 67 |
$wpdb->prepare( |
| 68 |
" |
| 69 |
SELECT keyword |
| 70 |
FROM {$wpdb->prefix}betterdocs_search_keyword |
| 71 |
WHERE keyword = %s", |
| 72 |
$key |
| 73 |
) |
| 74 |
); |
| 75 |
|
| 76 |
if ( $keyword == null ) { |
| 77 |
$insert = $wpdb->query( |
| 78 |
$wpdb->prepare( |
| 79 |
"INSERT INTO {$wpdb->prefix}betterdocs_search_keyword |
| 80 |
( keyword ) |
| 81 |
VALUES ( %s )", |
| 82 |
[ |
| 83 |
$key |
| 84 |
] |
| 85 |
) |
| 86 |
); |
| 87 |
|
| 88 |
if ( $insert ) { |
| 89 |
$wpdb->query( |
| 90 |
$wpdb->prepare( |
| 91 |
"INSERT INTO {$wpdb->prefix}betterdocs_search_log |
| 92 |
(keyword_id, count, not_found_count, created_at) |
| 93 |
VALUES (%d, %d, %d, %s)", |
| 94 |
[ |
| 95 |
$wpdb->insert_id, |
| 96 |
$count, |
| 97 |
$not_found_count, |
| 98 |
date( 'Y-m-d' ) |
| 99 |
] |
| 100 |
) |
| 101 |
); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
$this->database->save( 'betterdocs_search_data_migration', '1.0' ); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|