| 1 |
<?php |
| 2 |
/** |
| 3 |
* Welcart item base class |
| 4 |
* |
| 5 |
* @package Welcart |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Database check. |
| 14 |
* |
| 15 |
* Database update check for each version. |
| 16 |
* |
| 17 |
* @since 2.6 |
| 18 |
*/ |
| 19 |
function wel_db_check() { |
| 20 |
|
| 21 |
if ( ! is_admin() ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
global $usces; |
| 26 |
|
| 27 |
$update_history = get_option( 'usces_db_version', array() ); |
| 28 |
$action_status = ''; |
| 29 |
$first_install = wel_is_first_install(); |
| 30 |
|
| 31 |
if ( version_compare( USCES_VERSION, '2.7-beta', '>=' ) ) { |
| 32 |
$action_status = '2.7'; |
| 33 |
$usces->create_table(); |
| 34 |
if ( ! isset( $update_history[ $action_status ] ) ) { |
| 35 |
if ( $first_install ) { |
| 36 |
$update_history[ $action_status ] = 1; |
| 37 |
} else { |
| 38 |
$update_history[ $action_status ] = 0; |
| 39 |
} |
| 40 |
update_option( 'usces_db_version', $update_history ); |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
if ( version_compare( USCES_VERSION, '2.6-beta', '>=' ) ) { |
| 45 |
$action_status = '2.6'; |
| 46 |
if ( ! isset( $update_history[ $action_status ] ) ) { |
| 47 |
if ( $first_install ) { |
| 48 |
$update_history[ $action_status ] = 1; |
| 49 |
} else { |
| 50 |
$update_history[ $action_status ] = 0; |
| 51 |
} |
| 52 |
update_option( 'usces_db_version', $update_history ); |
| 53 |
} |
| 54 |
}; |
| 55 |
/* |
| 56 |
if ( version_compare( USCES_VERSION, '2.0-beta', '>=' ) ) { |
| 57 |
$action_status = '2.0'; |
| 58 |
if ( ! isset( $update_history[ $action_status ] ) ) { |
| 59 |
$update_history[ $action_status ] = 0; |
| 60 |
update_option( 'usces_db_version', $update_history ); |
| 61 |
} |
| 62 |
}; |
| 63 |
*/ |
| 64 |
|
| 65 |
$notice_flag = false; |
| 66 |
foreach ( $update_history as $v => $f ) { |
| 67 |
|
| 68 |
$function_name = 'wel_update_db_'; |
| 69 |
$function_name .= str_replace( '.', '_', $v ); |
| 70 |
if ( function_exists( $function_name ) && 0 === (int) $f ) { |
| 71 |
$notice_flag = true; |
| 72 |
break; |
| 73 |
} |
| 74 |
} |
| 75 |
if ( $notice_flag ) { |
| 76 |
add_action( 'admin_notices', 'wel_db_notice' ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Database update notification. |
| 82 |
* |
| 83 |
* @since 2.6 |
| 84 |
*/ |
| 85 |
function wel_db_notice() { |
| 86 |
global $current_screen; |
| 87 |
$update_history = get_option( 'usces_db_version' ); |
| 88 |
$update_number = 0; |
| 89 |
foreach ( $update_history as $key => $flag ) { |
| 90 |
$function_name = 'wel_update_db_'; |
| 91 |
$function_name .= str_replace( '.', '_', $key ); |
| 92 |
if ( function_exists( $function_name ) && 0 === (int) $flag ) { |
| 93 |
$update_number++; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if ( isset( $current_screen->base ) && 'toplevel_page_usc-e-shop/usc-e-shop' === $current_screen->base ) { |
| 98 |
$action = filter_input( INPUT_GET, 'wel_action', FILTER_DEFAULT, FILTER_REQUIRE_SCALAR ); |
| 99 |
if ( 'update_db' === $action ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
$class = 'notice notice-warning'; |
| 105 |
$message1 = __( 'Shop data needs to be updated.', 'usces' ); |
| 106 |
$message2 = __( 'Be sure to back up your database before updating.', 'usces' ) . '[Welcart]'; |
| 107 |
$message3 = __( 'Make an update', 'usces' ); |
| 108 |
// translators: %s: Number of updates. |
| 109 |
$message4 = sprintf( _n( 'There is %s update.', 'There are %s updates.', $update_number, 'usces' ), $update_number ); |
| 110 |
$url = USCES_ADMIN_URL . '?page=' . rawurlencode( 'usc-e-shop/usc-e-shop.php' ) . '&wel_action=update_db'; |
| 111 |
$nonce_url = wp_nonce_url( $url, 'wel_update_database', '_welnonce' ); |
| 112 |
|
| 113 |
printf( '<div class="%1$s"><p>%2$s<br>%3$s</p><p><a class="button" href="%6$s">%4$s</a> ( %5$s )</p></div>', esc_attr( $class ), esc_html( $message1 ), esc_html( $message2 ), esc_html( $message3 ), esc_html( $message4 ), esc_url( $nonce_url ) ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Check if the database needs to be updated. |
| 118 |
* |
| 119 |
* @since 2.6 |
| 120 |
* @return boolean Returns true if necessary. |
| 121 |
*/ |
| 122 |
function wel_need_to_update_db() { |
| 123 |
$update_history = get_option( 'usces_db_version' ); |
| 124 |
if ( in_array( 0, $update_history, true ) ) { |
| 125 |
return true; |
| 126 |
} else { |
| 127 |
return false; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Database update process. |
| 133 |
* |
| 134 |
* @since 2.6 |
| 135 |
*/ |
| 136 |
function wel_db_update_ajax() { |
| 137 |
check_ajax_referer( 'wel_db_update' ); |
| 138 |
|
| 139 |
if ( ! current_user_can( 'wel_manage_setting' ) ) { |
| 140 |
wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); |
| 141 |
} |
| 142 |
|
| 143 |
define( 'USCES_DB_UP_INTERBAL', 10 ); |
| 144 |
|
| 145 |
$update_history = get_option( 'usces_db_version' ); |
| 146 |
ksort( $update_history ); |
| 147 |
|
| 148 |
foreach ( $update_history as $version => $flag ) { |
| 149 |
$function_name = 'wel_update_db_'; |
| 150 |
if ( 0 === $flag ) { |
| 151 |
$function_name .= str_replace( '.', '_', $version ); |
| 152 |
$function_name( $version ); |
| 153 |
break; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Database update 2.0. |
| 160 |
* |
| 161 |
* @since 2.6 |
| 162 |
* @param string $version Version namber. |
| 163 |
*/ |
| 164 |
function wel_update_db_2_0( $version ) { |
| 165 |
global $wpdb, $usces; |
| 166 |
|
| 167 |
/** |
| 168 |
* Preparation process. |
| 169 |
*/ |
| 170 |
$log = ''; |
| 171 |
$total_num = 0; |
| 172 |
$comp_num = 0; |
| 173 |
$err_num = 0; |
| 174 |
$line_num = 0; |
| 175 |
$file_info = 'バージョン2.0の更新'; |
| 176 |
// translators: %s: Version number. |
| 177 |
$log .= sprintf( __( 'Version %s update started', 'usces' ), $version ) . "\n"; |
| 178 |
$progress = array( |
| 179 |
'info' => $file_info, |
| 180 |
'status' => __( 'processing', 'usces' ), |
| 181 |
'i' => $line_num, |
| 182 |
'all' => $total_num, |
| 183 |
'log' => $log, |
| 184 |
); |
| 185 |
wel_record_progress( $progress ); |
| 186 |
|
| 187 |
/** |
| 188 |
* Update process. |
| 189 |
*/ |
| 190 |
$target = $wpdb->get_col( |
| 191 |
$wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = %s", '_itemPicts' ) |
| 192 |
); |
| 193 |
if ( is_array( $target ) ) { |
| 194 |
$total_num = count( $target ); |
| 195 |
} else { |
| 196 |
$total_num = 0; |
| 197 |
} |
| 198 |
|
| 199 |
if ( 0 < $total_num ) { |
| 200 |
|
| 201 |
for ( $line_num = 0; $line_num < $total_num; $line_num++ ) { |
| 202 |
|
| 203 |
$meta_id = $target[ $line_num ]; |
| 204 |
|
| 205 |
$res = $wpdb->query( |
| 206 |
$wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_id = %d", $meta_id ) |
| 207 |
); |
| 208 |
if ( false === $res ) { |
| 209 |
$err_num++; |
| 210 |
} else { |
| 211 |
$comp_num++; |
| 212 |
} |
| 213 |
|
| 214 |
if ( 0 === ( $line_num % 20 ) ) { |
| 215 |
$progress = array( |
| 216 |
'info' => $file_info, |
| 217 |
'status' => __( 'processing', 'usces' ), |
| 218 |
// translators: %1$s: Number of successes. %2$s: Number of errors. |
| 219 |
'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ), |
| 220 |
'i' => ( $line_num + 1 ), |
| 221 |
'all' => $total_num, |
| 222 |
); |
| 223 |
wel_record_progress( $progress ); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Final processing. |
| 229 |
*/ |
| 230 |
$update_history = get_option( 'usces_db_version' ); |
| 231 |
$update_history[ $version ] = 1; |
| 232 |
update_option( 'usces_db_version', $update_history ); |
| 233 |
|
| 234 |
sleep( 2 ); |
| 235 |
$log .= __( 'Completion', 'usces' ) . "\n"; |
| 236 |
$progress = array( |
| 237 |
'info' => $file_info, |
| 238 |
'status' => __( 'End', 'usces' ), |
| 239 |
// translators: %1$s: Number of successes. %2$s: Number of errors. |
| 240 |
'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ), |
| 241 |
'log' => $log, |
| 242 |
'i' => $line_num, |
| 243 |
'all' => $total_num, |
| 244 |
'flag' => 'complete', |
| 245 |
); |
| 246 |
wel_record_progress( $progress ); |
| 247 |
die( wp_json_encode( $progress ) ); |
| 248 |
|
| 249 |
} else { |
| 250 |
|
| 251 |
/** |
| 252 |
* Final processing. |
| 253 |
*/ |
| 254 |
$update_history = get_option( 'usces_db_version' ); |
| 255 |
$update_history[ $version ] = 1; |
| 256 |
update_option( 'usces_db_version', $update_history ); |
| 257 |
|
| 258 |
sleep( 2 ); |
| 259 |
$log .= __( 'There was no data to update.', 'usces' ) . "\n"; |
| 260 |
$progress = array( |
| 261 |
'info' => $file_info, |
| 262 |
'status' => __( 'End', 'usces' ), |
| 263 |
// translators: %1$s: Number of successes. %2$s: Number of errors. |
| 264 |
'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ), |
| 265 |
'log' => $log, |
| 266 |
'i' => $line_num, |
| 267 |
'all' => $total_num, |
| 268 |
'flag' => 'complete', |
| 269 |
); |
| 270 |
wel_record_progress( $progress ); |
| 271 |
die( wp_json_encode( $progress ) ); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Database update 2.6. |
| 277 |
* |
| 278 |
* @since 2.6 |
| 279 |
* @param string $version Version namber. |
| 280 |
*/ |
| 281 |
function wel_update_db_2_6( $version ) { |
| 282 |
global $wpdb, $usces; |
| 283 |
|
| 284 |
$time_start = filter_input( INPUT_POST, 'time_start', FILTER_DEFAULT, array( 'options' => array( 'default' => 0 ) ) ); |
| 285 |
if ( 0 === (int) $time_start ) { |
| 286 |
$time_start = microtime( true ); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Preparation process. |
| 291 |
*/ |
| 292 |
$log = ''; |
| 293 |
$total_num = 0; |
| 294 |
$work_number = filter_input( INPUT_POST, 'work_number', FILTER_VALIDATE_INT, array( 'options' => array( 'default' => 0 ) ) ); |
| 295 |
$comp_num = filter_input( INPUT_POST, 'comp_num', FILTER_VALIDATE_INT, array( 'options' => array( 'default' => 0 ) ) ); |
| 296 |
$err_num = filter_input( INPUT_POST, 'err_num', FILTER_VALIDATE_INT, array( 'options' => array( 'default' => 0 ) ) ); |
| 297 |
$line_num = 0; |
| 298 |
$file_info = 'バージョン2.6の更新<br>画像の� |
| 299 |
報を新様式に整理します。<br>商品点数、商品画像が多い場合は処理に時間がかかります。<p>終了するまで他の操作はせず、そのままお� |
| 300 |
ちください。</p>'; |
| 301 |
// translators: %s: Version number. |
| 302 |
$log .= sprintf( __( 'Version %s update started', 'usces' ), $version ) . "\n"; |
| 303 |
$progress = array( |
| 304 |
'info' => $file_info, |
| 305 |
'status' => __( 'processing', 'usces' ), |
| 306 |
'i' => $line_num, |
| 307 |
'all' => $total_num, |
| 308 |
'log' => $log, |
| 309 |
); |
| 310 |
//wel_record_progress( $progress ); |
| 311 |
|
| 312 |
/** |
| 313 |
* Update process. |
| 314 |
*/ |
| 315 |
$target = $wpdb->get_col( |
| 316 |
$wpdb->prepare( |
| 317 |
"SELECT ID FROM $wpdb->posts |
| 318 |
WHERE (post_status = %s OR post_status = %s OR post_status = %s OR post_status = %s OR post_status LIKE %s ) |
| 319 |
AND post_type = %s AND post_mime_type = %s", |
| 320 |
'publish', |
| 321 |
'private', |
| 322 |
'future', |
| 323 |
'pending', |
| 324 |
'%draft%', |
| 325 |
'post', |
| 326 |
'item' |
| 327 |
) |
| 328 |
); |
| 329 |
|
| 330 |
if ( is_array( $target ) ) { |
| 331 |
$total_num = count( $target ); |
| 332 |
} else { |
| 333 |
$total_num = 0; |
| 334 |
} |
| 335 |
|
| 336 |
if ( 0 < $total_num ) { |
| 337 |
|
| 338 |
for ( $line_num = $work_number; $line_num < $total_num; $line_num++ ) { |
| 339 |
|
| 340 |
$post_id = $target[ $line_num ]; |
| 341 |
$cache = false; |
| 342 |
wel_make_item_picts( $post_id, $cache ); |
| 343 |
|
| 344 |
$comp_num++; |
| 345 |
$memory = (int) ( memory_get_peak_usage() / ( 1024 * 1024 ) ); |
| 346 |
$time = (int) ( microtime( true ) - $time_start ); |
| 347 |
if ( 0 === ( $line_num % 100 ) ) { |
| 348 |
$file_info2 = '<br>[メモリ最大使用量]:' . $memory . 'MB'; |
| 349 |
$file_info2 .= '<br>[経過時間]:' . $time . '秒'; |
| 350 |
$progress = array( |
| 351 |
'info' => $file_info . $file_info2, |
| 352 |
'status' => __( 'processing', 'usces' ), |
| 353 |
// translators: %1$s: Number of successes. %2$s: Number of errors. |
| 354 |
'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ), |
| 355 |
'i' => ( $line_num + 1 ), |
| 356 |
'all' => $total_num, |
| 357 |
); |
| 358 |
wel_record_progress( $progress ); |
| 359 |
} |
| 360 |
|
| 361 |
if ( 80 < $memory ) { |
| 362 |
$con = array( |
| 363 |
'work_number' => $line_num + 1, |
| 364 |
'comp_num' => $comp_num, |
| 365 |
'err_num' => $err_num, |
| 366 |
'time_start' => $time_start, |
| 367 |
'flag' => 'continue', |
| 368 |
); |
| 369 |
die( wp_json_encode( $con ) ); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Final processing. |
| 375 |
*/ |
| 376 |
$update_history = get_option( 'usces_db_version' ); |
| 377 |
$update_history[ $version ] = 1; |
| 378 |
update_option( 'usces_db_version', $update_history ); |
| 379 |
|
| 380 |
sleep( 2 ); |
| 381 |
$memory = (int) ( memory_get_peak_usage() / ( 1024 * 1024 ) ); |
| 382 |
$time = (int) ( microtime( true ) - $time_start ); |
| 383 |
$file_info2 = '<br>[メモリ最大使用量]:' . $memory . 'MB'; |
| 384 |
$file_info2 .= '<br>[経過時間]:' . $time . '秒'; |
| 385 |
$file_info = 'バージョン2.6の更新<br>画像の� |
| 386 |
報を新様式に整理します。<br>商品点数、商品画像が多い場合は処理に時間がかかります。<p>処理が完了しました。</p>'; |
| 387 |
$file_info .= $file_info2; |
| 388 |
$log .= __( 'Completion', 'usces' ) . "\n"; |
| 389 |
$progress = array( |
| 390 |
'info' => $file_info, |
| 391 |
'status' => __( 'End', 'usces' ), |
| 392 |
// translators: %1$s: Number of successes. %2$s: Number of errors. |
| 393 |
'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ), |
| 394 |
'log' => $log, |
| 395 |
'i' => $line_num, |
| 396 |
'all' => $total_num, |
| 397 |
'flag' => 'complete', |
| 398 |
); |
| 399 |
wel_record_progress( $progress ); |
| 400 |
die( wp_json_encode( $progress ) ); |
| 401 |
|
| 402 |
} else { |
| 403 |
/** |
| 404 |
* Final processing. |
| 405 |
*/ |
| 406 |
$update_history = get_option( 'usces_db_version' ); |
| 407 |
$update_history[ $version ] = 1; |
| 408 |
update_option( 'usces_db_version', $update_history ); |
| 409 |
|
| 410 |
sleep( 2 ); |
| 411 |
$memory = (int) ( memory_get_peak_usage() / ( 1024 * 1024 ) ); |
| 412 |
$time = (int) ( microtime( true ) - $time_start ); |
| 413 |
$file_info2 = '<br>[メモリ最大使用量]:' . $memory . 'MB'; |
| 414 |
$file_info2 .= '<br>[経過時間]:' . $time . '秒'; |
| 415 |
$file_info = 'バージョン2.6の更新<br>画像の� |
| 416 |
報を新様式に整理します。<br>商品点数、商品画像が多い場合は処理に時間がかかります。<p>処理が完了しました。</p>'; |
| 417 |
$file_info .= $file_info2; |
| 418 |
$log .= __( 'There was no data to update.', 'usces' ) . "\n"; |
| 419 |
$progress = array( |
| 420 |
'info' => $file_info, |
| 421 |
'status' => __( 'End', 'usces' ), |
| 422 |
// translators: %1$s: Number of successes. %2$s: Number of errors. |
| 423 |
'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ), |
| 424 |
'log' => $log, |
| 425 |
'i' => $line_num, |
| 426 |
'all' => $total_num, |
| 427 |
'flag' => 'complete', |
| 428 |
); |
| 429 |
wel_record_progress( $progress ); |
| 430 |
die( wp_json_encode( $progress ) ); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Make Image post meta. |
| 436 |
* |
| 437 |
* @param integer $post_id item post id. |
| 438 |
* @param boolean $cache Switch of cache. |
| 439 |
*/ |
| 440 |
function wel_make_item_picts( $post_id, $cache = true ) { |
| 441 |
$meta_key = '_itemPicts'; |
| 442 |
$item_picts = usces_get_post_meta( $post_id, $meta_key, $cache ); |
| 443 |
$arr_pict_id = array(); |
| 444 |
if ( is_array( $item_picts ) && 0 === count( $item_picts ) ) { |
| 445 |
// sync value init image from the old rule. |
| 446 |
$item_code = get_post_meta( $post_id, '_itemCode', true ); |
| 447 |
if ( ! empty( $item_code ) ) { |
| 448 |
global $usces, $wpdb; |
| 449 |
// get main pict id. |
| 450 |
$main_pict_id = $wpdb->get_var( |
| 451 |
$wpdb->prepare( |
| 452 |
"SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'attachment' LIMIT 1", |
| 453 |
$item_code |
| 454 |
) |
| 455 |
); |
| 456 |
if ( false !== $main_pict_id && 0 < $main_pict_id ) { |
| 457 |
$arr_pict_id[] = $main_pict_id; |
| 458 |
} |
| 459 |
// get sub pict id. |
| 460 |
if ( ! $usces->options['system']['subimage_rule'] ) { |
| 461 |
$codestr = $wpdb->esc_like( $item_code ) . '-%'; |
| 462 |
$query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title LIKE %s AND post_title <> %s AND post_type = 'attachment' ORDER BY post_title", $codestr, $item_code ); |
| 463 |
} else { |
| 464 |
$codestr = $wpdb->esc_like( $item_code ) . '--%'; |
| 465 |
$codestr2 = $wpdb->esc_like( $item_code ) . '\_\_%'; |
| 466 |
$query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE (post_title LIKE %s OR post_title LIKE %s) AND post_type = 'attachment' ORDER BY post_title", $codestr, $codestr2 ); |
| 467 |
} |
| 468 |
$sub_pict_ids = $wpdb->get_col( $query ); |
| 469 |
if ( $sub_pict_ids && is_array( $sub_pict_ids ) ) { |
| 470 |
$arr_pict_id = array_merge( $arr_pict_id, $sub_pict_ids ); |
| 471 |
} |
| 472 |
} |
| 473 |
// save to db. |
| 474 |
$arr_pict_id_db = array(); |
| 475 |
foreach ( $arr_pict_id as $pict_id ) { |
| 476 |
if ( 0 < $pict_id ) { |
| 477 |
$arr_pict_id_db[] = $pict_id; |
| 478 |
} |
| 479 |
} |
| 480 |
if ( 0 < count( $arr_pict_id_db ) ) { |
| 481 |
$arr_pict_id_db = array_unique( $arr_pict_id_db ); |
| 482 |
$pict_ids = implode( ';', $arr_pict_id_db ); |
| 483 |
} else { |
| 484 |
$pict_ids = null; |
| 485 |
} |
| 486 |
update_post_meta( $post_id, $meta_key, $pict_ids ); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Database update 2.7. |
| 492 |
* |
| 493 |
* @since 2.7 |
| 494 |
* @param string $version Version namber. |
| 495 |
*/ |
| 496 |
function wel_update_db_2_7( $version ) { |
| 497 |
global $wpdb, $usces; |
| 498 |
|
| 499 |
set_time_limit( 0 ); |
| 500 |
|
| 501 |
$usces->update_db_2_7 = 1; |
| 502 |
|
| 503 |
$time_start = filter_input( INPUT_POST, 'time_start', FILTER_DEFAULT, array( 'options' => array( 'default' => 0 ) ) ); |
| 504 |
if ( 0 === (int) $time_start ) { |
| 505 |
$time_start = microtime( true ); |
| 506 |
} |
| 507 |
$get_ini = ini_get_all(); |
| 508 |
$memory_limit = str_replace( 'M', '', $get_ini['memory_limit']['global_value'] ); |
| 509 |
if ( false !== strpos( $memory_limit, 'G' ) ) { |
| 510 |
$memory_limit = str_replace( 'G', '', $memory_limit ); |
| 511 |
$memory_limit = $memory_limit * 1024; |
| 512 |
} |
| 513 |
$mlmit = ceil( $memory_limit * 0.5 ); |
| 514 |
if ( 150 < $mlmit ) { |
| 515 |
$mlmit = 150; |
| 516 |
} |
| 517 |
|
| 518 |
/** |
| 519 |
* Preparation process. |
| 520 |
*/ |
| 521 |
$log = ''; |
| 522 |
$total_num = 0; |
| 523 |
$work_number = filter_input( INPUT_POST, 'work_number', FILTER_VALIDATE_INT, array( 'options' => array( 'default' => 0 ) ) ); |
| 524 |
$comp_num = filter_input( INPUT_POST, 'comp_num', FILTER_VALIDATE_INT, array( 'options' => array( 'default' => 0 ) ) ); |
| 525 |
$err_num = filter_input( INPUT_POST, 'err_num', FILTER_VALIDATE_INT, array( 'options' => array( 'default' => 0 ) ) ); |
| 526 |
$line_num = 0; |
| 527 |
$file_info = 'バージョン2.7の更新<br>商品データを新様式に整理します。<br>商品点数が多い場合は処理に時間がかかります。<p>終了するまで他の操作はせず、そのままお� |
| 528 |
ちください。</p>'; |
| 529 |
$file_info .= '<p>PHP Memory Limit : ' . $memory_limit . 'M</p>'; |
| 530 |
// translators: %s: Version number. |
| 531 |
$log .= sprintf( __( 'Version %s update started', 'usces' ), $version ) . "\n"; |
| 532 |
$progress = array( |
| 533 |
'info' => $file_info, |
| 534 |
'status' => __( 'processing', 'usces' ), |
| 535 |
'i' => $line_num, |
| 536 |
'all' => $total_num, |
| 537 |
'log' => $log, |
| 538 |
); |
| 539 |
|
| 540 |
/** |
| 541 |
* Update process. |
| 542 |
*/ |
| 543 |
|
| 544 |
// Common Option data. |
| 545 |
$com_opt = $wpdb->get_results( |
| 546 |
$wpdb->prepare( |
| 547 |
"SELECT * FROM {$wpdb->postmeta} WHERE `post_id` = %d AND `meta_key` = %s", |
| 548 |
USCES_CART_NUMBER, |
| 549 |
'_iopt_' |
| 550 |
) |
| 551 |
); |
| 552 |
foreach ( (array) $com_opt as $data_opt ) { |
| 553 |
wel_backup_remove_data( $data_opt ); |
| 554 |
$meta_id = $data_opt->meta_id; |
| 555 |
$post_id = $data_opt->post_id; |
| 556 |
$opt = wel_safe_unserialize( $data_opt->meta_value ); |
| 557 |
$res = wel_update_opt_data_by_id( $meta_id, $post_id, $opt ); |
| 558 |
} |
| 559 |
|
| 560 |
// Start. |
| 561 |
$target = $wpdb->get_col( |
| 562 |
$wpdb->prepare( |
| 563 |
"SELECT ID FROM $wpdb->posts |
| 564 |
WHERE (post_status = %s OR post_status = %s OR post_status = %s OR post_status = %s OR post_status LIKE %s ) |
| 565 |
AND post_type = %s AND post_mime_type = %s", |
| 566 |
'publish', |
| 567 |
'private', |
| 568 |
'future', |
| 569 |
'pending', |
| 570 |
'%draft%', |
| 571 |
'post', |
| 572 |
'item' |
| 573 |
) |
| 574 |
); |
| 575 |
if ( is_array( $target ) ) { |
| 576 |
$total_num = count( $target ); |
| 577 |
} else { |
| 578 |
$total_num = 0; |
| 579 |
} |
| 580 |
|
| 581 |
if ( 0 < $total_num ) { |
| 582 |
|
| 583 |
$item_table = $wpdb->prefix . 'usces_item'; |
| 584 |
$sku_table = $wpdb->prefix . 'usces_skus'; |
| 585 |
$opt_table = $wpdb->prefix . 'usces_opts'; |
| 586 |
|
| 587 |
for ( $line_num = $work_number; $line_num < $total_num; $line_num++ ) { |
| 588 |
|
| 589 |
$post_id = $target[ $line_num ]; |
| 590 |
|
| 591 |
// Item data. |
| 592 |
$res_meta = $wpdb->get_results( |
| 593 |
$wpdb->prepare( |
| 594 |
"SELECT * FROM {$wpdb->postmeta} WHERE `post_id` = %d", |
| 595 |
$post_id |
| 596 |
) |
| 597 |
); |
| 598 |
$item = array(); |
| 599 |
$WelItem = new Welcart\ItemData( $post_id, false ); |
| 600 |
|
| 601 |
foreach ( $res_meta as $meta ) { |
| 602 |
|
| 603 |
$value_arr = array(); |
| 604 |
$reserved_key = ltrim( $meta->meta_key, '_' ); |
| 605 |
|
| 606 |
if ( array_key_exists( $reserved_key, $WelItem->get_item_format() ) ) { |
| 607 |
|
| 608 |
wel_backup_remove_data( $meta ); |
| 609 |
$item[ $reserved_key ] = wel_safe_maybe_unserialize( $meta->meta_value ); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
if ( ! empty( $item ) ) { |
| 614 |
wel_update_item_data( $item, $post_id ); |
| 615 |
} |
| 616 |
|
| 617 |
// SKU data. |
| 618 |
$res_sku = $wpdb->get_results( |
| 619 |
$wpdb->prepare( |
| 620 |
"SELECT * FROM {$wpdb->postmeta} WHERE `post_id` = %d AND `meta_key` = %s", |
| 621 |
$post_id, |
| 622 |
'_isku_' |
| 623 |
) |
| 624 |
); |
| 625 |
foreach ( (array) $res_sku as $data_sku ) { |
| 626 |
wel_backup_remove_data( $data_sku ); |
| 627 |
$meta_id = $data_sku->meta_id; |
| 628 |
$sku = wel_safe_unserialize( $data_sku->meta_value ); |
| 629 |
$res = wel_update_sku_data_by_id( $meta_id, $post_id, $sku ); |
| 630 |
} |
| 631 |
|
| 632 |
// Option data. |
| 633 |
$res_opt = $wpdb->get_results( |
| 634 |
$wpdb->prepare( |
| 635 |
"SELECT * FROM {$wpdb->postmeta} WHERE `post_id` = %d AND `meta_key` = %s", |
| 636 |
$post_id, |
| 637 |
'_iopt_' |
| 638 |
) |
| 639 |
); |
| 640 |
foreach ( (array) $res_opt as $data_opt ) { |
| 641 |
wel_backup_remove_data( $data_opt ); |
| 642 |
$meta_id = $data_opt->meta_id; |
| 643 |
$opt = wel_safe_unserialize( $data_opt->meta_value ); |
| 644 |
$res = wel_update_opt_data_by_id( $meta_id, $post_id, $opt ); |
| 645 |
} |
| 646 |
|
| 647 |
$comp_num++; |
| 648 |
$memory = (int) ( memory_get_peak_usage() / ( 1024 * 1024 ) ); |
| 649 |
$time = (int) ( microtime( true ) - $time_start ); |
| 650 |
if ( 0 === ( $line_num % 200 ) ) { |
| 651 |
$file_info2 = '<br>[メモリ最大使用量]:' . $memory . 'MB'; |
| 652 |
$file_info2 .= '<br>[経過時間]:' . $time . '秒'; |
| 653 |
$progress = array( |
| 654 |
'info' => $file_info . $file_info2, |
| 655 |
'status' => __( 'processing', 'usces' ), |
| 656 |
// translators: %1$s: Number of successes. %2$s: Number of errors. |
| 657 |
'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ), |
| 658 |
'i' => ( $line_num + 1 ), |
| 659 |
'all' => $total_num, |
| 660 |
); |
| 661 |
wel_record_progress( $progress ); |
| 662 |
} |
| 663 |
|
| 664 |
if ( $mlmit < $memory ) { |
| 665 |
$con = array( |
| 666 |
'work_number' => $line_num + 1, |
| 667 |
'comp_num' => $comp_num, |
| 668 |
'err_num' => $err_num, |
| 669 |
'time_start' => $time_start, |
| 670 |
'flag' => 'continue', |
| 671 |
); |
| 672 |
die( wp_json_encode( $con ) ); |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
$memory = (int) ( memory_get_peak_usage() / ( 1024 * 1024 ) ); |
| 677 |
$time = (int) ( microtime( true ) - $time_start ); |
| 678 |
$file_info2 = '<br>[メモリ最大使用量]:' . $memory . 'MB'; |
| 679 |
$file_info2 .= '<br>[経過時間]:' . $time . '秒'; |
| 680 |
$file_info = 'Post Meta の削除中'; |
| 681 |
$file_info .= $file_info2; |
| 682 |
$log .= 'Post Meta の削除中' . "\n"; |
| 683 |
$progress = array( |
| 684 |
'info' => $file_info, |
| 685 |
'status' => __( 'processing', 'usces' ), |
| 686 |
// translators: %1$s: Number of successes. %2$s: Number of errors. |
| 687 |
'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ), |
| 688 |
'log' => $log, |
| 689 |
'i' => $line_num, |
| 690 |
'all' => $total_num, |
| 691 |
); |
| 692 |
wel_record_progress( $progress ); |
| 693 |
|
| 694 |
$update_history = get_option( 'usces_db_version' ); |
| 695 |
$update_history[ $version ] = 1; |
| 696 |
update_option( 'usces_db_version', $update_history ); |
| 697 |
|
| 698 |
$key_map = $WelItem->get_item_old_key(); |
| 699 |
$format = array( '%s' ); |
| 700 |
foreach ( $key_map as $newkey => $oldkey ) { |
| 701 |
$where = array( 'meta_key' => $oldkey ); |
| 702 |
$wpdb->delete( $wpdb->postmeta, $where, $format ); |
| 703 |
} |
| 704 |
|
| 705 |
$where = array( 'meta_key' => '_isku_' ); |
| 706 |
$wpdb->delete( $wpdb->postmeta, $where, $format ); |
| 707 |
|
| 708 |
$where = array( 'meta_key' => '_iopt_' ); |
| 709 |
$wpdb->delete( $wpdb->postmeta, $where, $format ); |
| 710 |
|
| 711 |
$wpdb->query( "OPTIMIZE TABLE `{$wpdb->postmeta}`" ); |
| 712 |
|
| 713 |
/** |
| 714 |
* Final processing. |
| 715 |
*/ |
| 716 |
|
| 717 |
$memory = (int) ( memory_get_peak_usage() / ( 1024 * 1024 ) ); |
| 718 |
$time = (int) ( microtime( true ) - $time_start ); |
| 719 |
$file_info2 = '<br>[メモリ最大使用量]:' . $memory . 'MB'; |
| 720 |
$file_info2 .= '<br>[経過時間]:' . $time . '秒'; |
| 721 |
$file_info = '<p>処理が完了しました。</p>'; |
| 722 |
$file_info .= $file_info2; |
| 723 |
$log .= '削除完了'. "\n"; |
| 724 |
$progress = array( |
| 725 |
'info' => $file_info, |
| 726 |
'status' => __( 'End', 'usces' ), |
| 727 |
// translators: %1$s: Number of successes. %2$s: Number of errors. |
| 728 |
'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ), |
| 729 |
'log' => $log, |
| 730 |
'i' => $line_num, |
| 731 |
'all' => $total_num, |
| 732 |
'flag' => 'complete', |
| 733 |
); |
| 734 |
wel_record_progress( $progress ); |
| 735 |
die( wp_json_encode( $progress ) ); |
| 736 |
|
| 737 |
} else { |
| 738 |
/** |
| 739 |
* Final processing. |
| 740 |
*/ |
| 741 |
$update_history = get_option( 'usces_db_version' ); |
| 742 |
$update_history[ $version ] = 1; |
| 743 |
update_option( 'usces_db_version', $update_history ); |
| 744 |
|
| 745 |
sleep( 2 ); |
| 746 |
$memory = (int) ( memory_get_peak_usage() / ( 1024 * 1024 ) ); |
| 747 |
$time = (int) ( microtime( true ) - $time_start ); |
| 748 |
$file_info2 = '<br>[メモリ最大使用量]:' . $memory . 'MB'; |
| 749 |
$file_info2 .= '<br>[経過時間]:' . $time . '秒'; |
| 750 |
$file_info = 'バージョン2.7の更新<br>商品データを新様式に整理します。<br>商品点数が多い場合は処理に時間がかかります。<p>処理が完了しました。</p>'; |
| 751 |
$file_info .= $file_info2; |
| 752 |
$log .= __( 'There was no data to update.', 'usces' ) . "\n"; |
| 753 |
$progress = array( |
| 754 |
'info' => $file_info, |
| 755 |
'status' => __( 'End', 'usces' ), |
| 756 |
// translators: %1$s: Number of successes. %2$s: Number of errors. |
| 757 |
'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ), |
| 758 |
'log' => $log, |
| 759 |
'i' => $line_num, |
| 760 |
'all' => $total_num, |
| 761 |
'flag' => 'complete', |
| 762 |
); |
| 763 |
wel_record_progress( $progress ); |
| 764 |
|
| 765 |
unset( $usces->update_db_2_7 ); |
| 766 |
|
| 767 |
die( wp_json_encode( $progress ) ); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
/** |
| 772 |
* Record progress. |
| 773 |
* |
| 774 |
* @since 2.6 |
| 775 |
* @param array $arr_content Content. |
| 776 |
*/ |
| 777 |
function wel_record_progress( $arr_content ) { |
| 778 |
|
| 779 |
$upload_folder = WP_CONTENT_DIR . USCES_UPLOAD_TEMP . '/'; |
| 780 |
$mkdir = wp_mkdir_p( $upload_folder ); |
| 781 |
$progress_file = $upload_folder . 'db-progress.txt'; |
| 782 |
$log_file = $upload_folder . 'db-log.txt'; |
| 783 |
|
| 784 |
if ( $mkdir ) { |
| 785 |
|
| 786 |
if ( ( isset( $arr_content['status'] ) || isset( $arr_content['progress'] ) ) ) { |
| 787 |
file_put_contents( $progress_file, wp_json_encode( $arr_content ), LOCK_EX ); |
| 788 |
} |
| 789 |
|
| 790 |
if ( isset( $arr_content['log'] ) ) { |
| 791 |
if ( 'clear' === $arr_content['log'] ) { |
| 792 |
file_put_contents( $log_file, '', LOCK_EX ); |
| 793 |
} elseif ( isset( $arr_content['flag'] ) && 'complete' === $arr_content['flag'] ) { |
| 794 |
$add_text = $arr_content['log']; |
| 795 |
file_put_contents( $log_file, $add_text, LOCK_EX ); |
| 796 |
} |
| 797 |
} |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
/** |
| 802 |
* Backup DB. |
| 803 |
* |
| 804 |
* @since 2.7 |
| 805 |
* @param int $post_id Data. |
| 806 |
* @param string $key Data. |
| 807 |
* @param string $value Data. |
| 808 |
*/ |
| 809 |
function wel_backup_remove_data( $db_data, $flag = null ) { |
| 810 |
|
| 811 |
$upload_folder = WP_CONTENT_DIR . USCES_UPLOAD_TEMP . '/'; |
| 812 |
$mkdir = wp_mkdir_p( $upload_folder ); |
| 813 |
$today = current_time( 'Ymd' ); |
| 814 |
$backup_file = $upload_folder . 'db-backup' . $today . '.csv'; |
| 815 |
$data = wp_json_encode( $db_data ) . "\n"; |
| 816 |
|
| 817 |
if ( $mkdir ) { |
| 818 |
|
| 819 |
if ( 'clear' === $flag ) { |
| 820 |
file_put_contents( $backup_file, '', LOCK_EX ); |
| 821 |
} else { |
| 822 |
file_put_contents( $backup_file, $data, FILE_APPEND ); |
| 823 |
} |
| 824 |
|
| 825 |
} else { |
| 826 |
die( 'Not found the required working directory.' ); |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
/** |
| 831 |
* Check progress. |
| 832 |
* |
| 833 |
* @since 2.6 |
| 834 |
*/ |
| 835 |
function wel_check_progress_ajax() { |
| 836 |
check_ajax_referer( 'wel_db_update' ); |
| 837 |
|
| 838 |
if ( ! current_user_can( 'wel_manage_setting' ) ) { |
| 839 |
wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); |
| 840 |
} |
| 841 |
|
| 842 |
$progressfile = filter_input( INPUT_POST, 'progressfile' ); |
| 843 |
|
| 844 |
sleep( 1 ); |
| 845 |
// Make sure the file is exist. |
| 846 |
if ( usces_is_reserved_file( $progressfile ) ) { |
| 847 |
// Get the content and echo it. |
| 848 |
$text = file_get_contents( $progressfile ); |
| 849 |
die( $text ); |
| 850 |
} else { |
| 851 |
die( "logfile dosn't exist" ); |
| 852 |
} |
| 853 |
exit; |
| 854 |
} |
| 855 |
|