{execution_time}.';
} else {
$msg = '"' . $asset->asset . '" (ID ' . $asset->id . ') ' . __( 'could not be downloaded and resized - duration: ', $this->the_plugin->localizationName ) . ' {execution_time}.{execution_time}.';
}
die( json_encode(array(
'status' => 'valid',
'msg' => $msg, //'"img_3_large" (ID 202) was successfully downloaded and resized in {execution_time}.',
'msg_last' => $msg_last,
'data' => $request['id']
)) );
}
public function get_asset_by_id( $asset_id, $inprogress=false, $include_err=false, $include_invalid_post=false ) {
return $this->get_asset_generic($asset_id, 1, 0, false, $inprogress, $include_err, $include_invalid_post);
}
public function get_asset_by_postid( $nb_dw, $post_id, $include_variations, $inprogress=false, $include_err=false, $include_invalid_post=false ) {
return $this->get_asset_generic(0, $nb_dw, $post_id, $include_variations, $inprogress, $include_err, $include_invalid_post);
}
public function get_asset_multiple( $nb_dw='all', $inprogress=false, $include_err=false ) {
return $this->get_asset_generic(0, $nb_dw, 0, true, $inprogress, $include_err, $include_invalid_post);
}
private function get_asset_generic( $asset_id=0, $nb_dw='all', $post_id=0, $include_variations=true, $inprogress=false, $include_err=false, $include_invalid_post=false ) {
global $wpdb;
$asset_id = (int) $asset_id;
$post_id = (int) $post_id;
$tables = array('assets' => $wpdb->prefix . 'amz_assets', 'products' => $wpdb->prefix . 'amz_products', 'posts' => $wpdb->prefix . 'posts');
if ( $include_err ) $__q_dw = "and (a.download_status in ('new', 'error') or isnull(a.download_status) or a.download_status = '')";
else $__q_dw = "and a.download_status = 'new'";
$q = "select a.id, a.post_id, a.asset, a.thumb, a.download_status, a.hash, a.media_id, a.image_sizes, b.title, b.type, b.post_parent from " . $tables['assets'] . " as a left join " . $tables['products'] . " as b on a.post_id = b.post_id where 1=1 $__q_dw ";
if ( !$include_invalid_post ) {
$q = "select a.id, a.post_id, a.asset, a.thumb, a.download_status, a.hash, a.media_id, a.image_sizes, b.title, b.type, b.post_parent from " . $tables['assets'] . " as a left join " . $tables['products'] . " as b on a.post_id = b.post_id left join " . $tables['posts'] . " as c on b.post_id = c.ID where 1=1 and !isnull(b.post_id) and !isnull(c.ID) $__q_dw ";
}
if ( is_int($asset_id) && $asset_id > 0 ) {
$q .= "and a.id = '$asset_id' ";
}
if ( is_int($post_id) && $post_id > 0 ) {
if ( $include_variations ) {
$q .= "and ( a.post_id = '$post_id' or b.post_parent = '$post_id' ) ";
} else {
$q .= "and a.post_id = '$post_id' ";
}
}
$q .= "order by a.id asc ";
if ( $nb_dw == 'all' ) ;
else {
$nb_dw = (int) $nb_dw;
$q .= "limit 0, $nb_dw";
}
$q .= ";";
//var_dump('', $q , ''); echo __FILE__ . ":" . __LINE__;die . PHP_EOL; $res = $wpdb->get_results( $q, OBJECT ); $ret = array(); if (is_array($res) && count($res)>0) { foreach ($res as $k=>$v) { $ret["{$v->id}"] = $v; } } // all selected assets have in progress status now! if ( $inprogress && !empty($ret) ) { $idList = implode(', ', array_map(array($this, 'prepareForInList'), array_keys($ret))); $qUpdStat = "update " . $tables['assets'] . " as a set a.download_status = 'inprogress' where 1=1 and a.id in ( $idList );"; $statUpdStat = $wpdb->query($qUpdStat); //COMMENT TO DEBUG } return $ret; } private function upd_asset_db( $asset, $first_item=false ) { global $wpdb; $tables = array('assets' => $wpdb->prefix . 'amz_assets', 'products' => $wpdb->prefix . 'amz_products'); if ( !is_object($asset) && is_int($asset) && ($asset > 0) ) { $q = "select a.id, a.post_id, a.asset, a.download_status, a.hash, a.media_id, b.title, b.type, b.post_parent from " . $tables['assets'] . " as a left join " . $tables['products'] . " as b on a.post_id = b.post_id where 1=1 and a.id = $asset;"; $asset = $wpdb->get_row( $q, OBJECT ); } if ( empty($asset) ) return false; $asset_id = $asset->id; $post_id = $asset->post_id; $post_parent = $asset->post_parent; $type = $asset->type; $dwimg = $this->the_plugin->download_image($asset->asset, $asset->post_id, 'insert', $asset->title, 0); $dwStatus = false; if ( isset($dwimg['attach_id']) && $dwimg['attach_id'] > 0 ) { // image was downloaded and inserted as media in wp posts $dwStatus = true; // product featured image //if ( $first_item ) { // update_post_meta($post_id, "_thumbnail_id", $dwimg['attach_id']); //} else { $current_thumb_id = get_post_meta($post_id, "_thumbnail_id", true); if ( empty($current_thumb_id) ) $current_thumb_id = 0; else $current_thumb_id = (int) $current_thumb_id; if ( $current_thumb_id == 0 || ( $current_thumb_id > $dwimg['attach_id'] ) ) { update_post_meta($post_id, "_thumbnail_id", $dwimg['attach_id']); $current_thumb_id = $dwimg['attach_id']; } //} // build product gallery $current_prod_gallery = get_post_meta($post_id, "_product_image_gallery", true); if ( empty($current_prod_gallery) ) $__current_prod_gallery = array(); else $__current_prod_gallery = explode(',', $current_prod_gallery); $__current_prod_gallery = array_merge( $__current_prod_gallery, array($dwimg['attach_id']) ); $__current_prod_gallery = array_unique($__current_prod_gallery); update_post_meta($post_id, "_product_image_gallery", implode(',', $__current_prod_gallery)); // _AVI_additional_images meta if ( $this->the_plugin->is_plugin_avi_active() && ( 'variation' == $type ) ) { $current_avi = get_post_meta($post_id, "_AVI_additional_images", true); if ( empty($current_avi) || ! is_array($current_avi) ) $current_avi = array(); $current_avi[] = $dwimg['attach_id']; foreach ( $current_avi as $kk => $vv ) { if ( (int) $current_thumb_id === (int) $vv ) { unset( $current_avi["$kk"] ); break; } } $current_avi = array_unique( array_filter( $current_avi ) ); update_post_meta( $post_id, "_AVI_additional_images", $current_avi ); } } $mediaValues = (object) array( 'download_status' => ( $dwStatus ? 'success' : 'error' ), 'hash' => ( $dwStatus ? $dwimg['hash'] : null ), 'media_id' => ( $dwStatus ? $dwimg['attach_id'] : 0 ), 'msg' => ( $dwStatus ? 'success' : $dwimg['msg'] ) ); // update row in assets table $statUpdAsset = $wpdb->update( $tables['assets'], array( 'download_status' => $mediaValues->download_status, 'hash' => $mediaValues->hash, 'media_id' => $mediaValues->media_id, 'date_download' => date("Y-m-d H:i:s"), 'msg' => $mediaValues->msg ), array( 'id' => $asset_id ), array( '%s', '%s', '%d', '%s', '%s' ), array( '%d' ) ); if ($statUpdAsset === false || !$dwStatus) { return array( 'status' => 'invalid', 'msg' => $mediaValues->msg ); } // update row in products table /*$wpdb->update( $tables['products'], array( 'nb_assets_done' => $nb_assets_done + 1 ), array( 'post_id' => $post_id ), array( '%d' ), array( '%d' ) );*/ $qUpdProd = "update " . $tables['products'] . " as a set a.nb_assets_done = a.nb_assets_done + 1 where 1=1 and a.post_id = $post_id;"; $statUpdProd = $wpdb->query($qUpdProd); $duration = $this->the_plugin->timer_end(); if ( 1 ) { $this->the_plugin->add_last_imports('last_import_images_download', array( 'duration' => $duration, )); // End Timer & Add Report } $this->the_plugin->import_stats_update_imgdw( array( 'post_id' => $post_parent ? $post_parent : $post_id, 'duration_img_dw' => $duration, 'duration_img_nb_dw' => 1, )); if ($statUpdProd=== false) { } return array( 'status' => 'valid', 'msg' => sprintf( __('%s was successfully downloaded', $this->the_plugin->localizationName), $asset->asset ), ); } public function upd_prod_poststable( $post_id, $new_status='draft', $allowedRatio='75' ) { global $wpdb; $tables = array('assets' => $wpdb->prefix . 'amz_assets', 'products' => $wpdb->prefix . 'amz_products'); $q = "select a.post_id, a.post_parent, (a.nb_assets_done / a.nb_assets) * 100 as ratio from " . $tables['products'] . " as a where 1=1 and ( a.post_id = $post_id or a.post_parent = $post_id );"; $res = $wpdb->get_results( $q, OBJECT ); $ret = array(); $ratioTotal = array(); if (is_array($res) && count($res)>0) { foreach ($res as $k=>$v) { $key = empty($v->post_parent) ? $v->post_id : $v->post_parent; $ret["$key"] = $v; $ratioTotal[] = (int) $v->ratio; } } if ( empty($ret) ) return false; $ratioTotal = ( array_sum($ratioTotal) / count($ret) ); $ratioTotal = number_format( $ratioTotal, 2 ); // verify if ratio allow product update in wp posts table! if ( $ratioTotal < $allowedRatio ) { return false; } $idList = implode(', ', array_map(array($this, 'prepareForInList'), array_keys($ret))); $qUpdProd = "update " . $tables['products'] . " as a set a.status = 'success' where 1=1 and ( a.post_id in ( $idList ) or a.post_parent in ( $idList ) );"; $statUpdProd = $wpdb->query($qUpdProd); if ($statUpdProd=== false) { } $qUpdStat = "update " . ($wpdb->prefix . 'posts') . " as a set a.post_status = '$new_status' where 1=1 and a.ID in ( $idList );"; $statUpdStat = $wpdb->query($qUpdStat); if ($statUpdStat=== false) { return false; } return true; } public function verifyProdImageHash( $hash ) { global $wpdb; $tables = array('assets' => $wpdb->prefix . 'amz_assets', 'products' => $wpdb->prefix . 'amz_products'); $q = "select a.id, a.post_id, a.media_id from " . $tables['assets'] . " as a where 1=1 and a.hash regexp '$hash' limit 1;"; $res = $wpdb->get_row( $q, OBJECT ); if ( !empty($res) && isset($res->media_id) ) { $attach = wp_get_attachment_metadata( $res->media_id ); $file = is_array($attach) && !empty($attach) && isset($attach['file']) ? $attach['file'] : ''; // Find Upload dir path $uploads = wp_upload_dir(); $uploads_path = $uploads['path'] . ''; $image_path = $uploads_path . '/' . basename($file); if ( !empty($file) && $this->the_plugin->u->verifyFileExists($image_path) ) { $res->image_path = $image_path; return $res; } } return false; } public function cronjob( $pms, $return='die' ) { $ret = array('status' => 'failed'); $current_cron_status = $pms['status']; //'new'; // if ( $this->the_plugin->is_remote_images ) { $ret = array_merge($ret, array( 'status' => 'done', 'msg' => 'you are using remote images option, so there is no need to download assets.', )); return $ret; } $max_images = 500; $cronNbImages = (int) $this->settings['cron_number_of_images']; $cronNbImages = $cronNbImages <= 0 || $cronNbImages > $max_images ? $max_images : $cronNbImages; $assetsList = $this->get_asset_multiple( $cronNbImages, true ); if ( count($assetsList) <= 0 ) { $ret = array_merge($ret, array( 'status' => 'done', 'msg' => 'no assets (to be downloaded) found.', )); return $ret; //false; } $cc = 1; $len = count($assetsList); $durationQueue = array(); $post_id_list = array(); foreach( $assetsList as $k=>$asset ) { $this->the_plugin->timer_start(); // Start Timer $stat = $this->upd_asset_db( $asset ); if ( isset($stat['status']) && $stat['status'] == 'valid' ) { $durationQueue[] = $this->the_plugin->timer_end(); // End Timer } $post_id_list[] = $asset->post_id; if ( !empty($durationQueue) && ( ( count($durationQueue) % 10 == 0 ) || $cc == $len ) ) { if ( 1 ) { $this->the_plugin->add_last_imports('last_import_images_download', array( 'duration' => round( array_sum($durationQueue) / count($durationQueue), 4 ), )); // End Timer & Add Report } $durationQueue = array(); } $cc++; } //var_dump('
products: ',$post_id_list,''); // update product status for the above assets products! $allowedRation = (int) $this->settings['ratio_prod_validate']; if ( $allowedRation <= 0 || $allowedRation > 100 ) $allowedRation = 90; $importProdStatus = $this->default_import; if ( !empty($post_id_list) ) { foreach ($post_id_list as $key => $value) { $updProdStat = $this->upd_prod_poststable($value, $importProdStatus, $allowedRation); //var_dump('
',$value, $updProdStat,''); } } $ret = array_merge($ret, array( 'status' => 'done', 'msg' => sprintf( 'we\ve found %s assets to be downloaded at this step.', count($post_id_list) ), )); return $ret; } public function product_assets_download( $post_id ) { //$ret = array('status' => 'failed'); $ret = array( 'status' => 'invalid', 'msg' => '', ); $assetsList = $this->get_asset_by_postid( 'all', $post_id, true ); if ( count($assetsList) <= 0 ) { $ret = array_merge($ret, array( 'msg' => __('no assets available for download', $this->the_plugin->localizationName), )); return $ret; } $msg = array(); $cc = 1; $len = count($assetsList); $durationQueue = array(); $post_id_list = array(); foreach( $assetsList as $k=>$asset ) { $this->the_plugin->timer_start(); // Start Timer $stat = $this->upd_asset_db( $asset ); if ( isset($stat['status']) && $stat['status'] == 'valid' ) { $durationQueue[] = $this->the_plugin->timer_end(); // End Timer } if ( isset($stat['msg']) ) { $msg[] = $stat['msg']; } else { $msg[] = __('empty asset', $this->the_plugin->localizationName); } $post_id_list[] = $asset->post_id; if ( !empty($durationQueue) && ( ( count($durationQueue) % 10 == 0 ) || $cc == $len ) ) { if ( 1 ) { $this->the_plugin->add_last_imports('last_import_images_download', array( 'duration' => round( array_sum($durationQueue) / count($durationQueue), 4 ), )); // End Timer & Add Report } $durationQueue = array(); } $cc++; } $post_id_list = array_unique( array_filter( $post_id_list ) ); //var_dump('
products: ',$post_id_list,''); // update product status for the above assets products! $allowedRation = (int) $this->settings['ratio_prod_validate']; if ( $allowedRation <= 0 || $allowedRation > 100 ) $allowedRation = 90; $importProdStatus = $this->default_import; if ( !empty($post_id_list) ) { foreach ($post_id_list as $key => $value) { $updProdStat = $this->upd_prod_poststable($value, $importProdStatus, $allowedRation); //var_dump('
',$value, $updProdStat,''); } } //$ret = array_merge($ret, array( // 'status' => 'done', //)); $ret = array_merge($ret, array( 'status' => 'valid', 'msg' => implode('
', $ret, ''); die('debug...'); } public function update_products_status_all() { global $wpdb; $tables = array('assets' => $wpdb->prefix . 'amz_assets', 'products' => $wpdb->prefix . 'amz_products'); $q = "select distinct(a.post_id) from " . $tables['products'] . " as a where 1=1 and status = 'new' order by a.post_id asc;"; $res = $wpdb->get_results( $q, OBJECT ); $ret = array(); if (is_array($res) && count($res)>0) { foreach ($res as $k=>$v) { $ret["{$v->post_id}"] = $v; } } $post_id_list = array_keys($ret); if ( empty($post_id_list) || !is_array($post_id_list) ) return false; $allowedRation = (int) $this->settings['ratio_prod_validate']; if ( $allowedRation <= 0 || $allowedRation > 100 ) $allowedRation = 90; $importProdStatus = $this->default_import; var_dump('
', 'post_id', 'status',''); foreach ($post_id_list as $key => $value) { // update product status for the above assets products! $updProdStat = $this->upd_prod_poststable($value, $importProdStatus, $allowedRation); var_dump('
',$value, $updProdStat,''); } echo __FILE__ . ":" . __LINE__;die . PHP_EOL; } public function restore_assets_bystatus($status='error') { global $wpdb; $tables = array('assets' => $wpdb->prefix . 'amz_assets', 'products' => $wpdb->prefix . 'amz_products'); $q = "update " . $tables['assets'] . " set download_status = 'new' where 1=1 and download_status = '" . $status . "';"; $res = $wpdb->query( $q ); var_dump('
', $res, ''); die('debug...'); } public function test_assets() { //$this->get_assets_bystatus('error'); //$this->restore_assets_bystatus('error'); //$this->get_assets_bystatus('inprogress'); //$this->restore_assets_bystatus('inprogress'); // $this->update_products_status_all(); // $this->get_asset_by_id(6); $assets = $this->get_asset_by_postid('all', 43, true); if ( !empty($assets) ) { // array_shift($assets) foreach ($assets as $k=>$v) { // $this->upd_asset_db( $v ); } } // $this->upd_prod_poststable(83, 'publish', 90); } /** * Utils */ private function prepareForInList($v) { return "'".$v."'"; } } } /*if ( !function_exists('WooZoneAssetDownload_cronjob') ) { function WooZoneAssetDownload_cronjob() { // Initialize the WooZoneAssetDownload class $amzaffAssetDownload = new WooZoneAssetDownload(); $amzaffAssetDownload->cronjob(); } }*/ // Initialize the WooZoneAssetDownload class $WooZoneAssetDownload = WooZoneAssetDownload::getInstance();