woocommerce_model = new A2WL_Woocommerce();
$this->aliexpress_model = new A2WL_Aliexpress();
}
function init() {
$this->bulk_actions[] = 'a2wl_product_update_manual';
$this->bulk_actions_text['a2wl_product_update_manual'] = __("AliExpress Sync", 'ali2woo-lite');
list($this->bulk_actions, $this->bulk_actions_text) = apply_filters('a2wl_wcpl_bulk_actions_init', array($this->bulk_actions, $this->bulk_actions_text));
}
function row_actions($actions, $post) {
if ('product' === $post->post_type) {
$external_id = get_post_meta($post->ID, "_a2wl_external_id", true);
if ($external_id) {
$actions = array_merge($actions, array('a2wl_product_info' => sprintf('%2$s', $post->ID, 'Aliexpress Info')));
}
}
return $actions;
}
function assets() {
wp_enqueue_style('a2wl-wc-pl-style', A2WL()->plugin_url() . '/assets/css/wc_pl_style.css', array(), A2WL()->version);
wp_style_add_data( 'a2wl-wc-pl-style', 'rtl', 'replace' );
wp_enqueue_script('jquery-ui-dialog');
wp_enqueue_script('a2wl-wc-pl-script', A2WL()->plugin_url() . '/assets/js/wc_pl_script.js', array(), A2WL()->version);
wp_enqueue_script('a2wl-sprintf-script', A2WL()->plugin_url() . '/assets/js/sprintf.js', array(), A2WL()->version);
$lang_data = array(
'please_wait_data_loads' => _x('Please wait, data loads..', 'Status', 'ali2woo-lite'),
'process_update_d_of_d' => _x('Process update %d of %d.', 'Status', 'ali2woo-lite'),
'process_update_d_of_d_erros_d' => _x('Process update %d of %d. Errors: %d.', 'Status', 'ali2woo-lite'),
'complete_result_updated_d_erros_d' => _x('Complete! Result updated: %d; errors: %d.', 'Status', 'ali2woo-lite'),
);
wp_localize_script('a2wl-wc-pl-script', 'a2wl_wc_pl_script', array('lang' => $lang_data, 'lang_cookies'=>A2WL_AliexpressLocalizator::getInstance()->getLocaleCookies(false)));
}
function scripts() {
global $post_type;
if ($post_type == 'product') {
foreach ($this->bulk_actions as $action) {
$text = $this->bulk_actions_text[$action];
?>
current_action();
$allowed_actions = $this->bulk_actions;
if (!in_array($action, $allowed_actions))
return;
check_admin_referer('bulk-posts');
// make sure ids are submitted. depending on the resource type, this may be 'media' or 'ids'
if (isset($_REQUEST['post'])) {
$post_ids = array_map('intval', $_REQUEST['post']);
}
if (empty($post_ids))
return;
$sendback = remove_query_arg(array_merge($allowed_actions, array('untrashed', 'deleted', 'ids')), wp_get_referer());
if (!$sendback)
$sendback = admin_url("edit.php?post_type=$post_type");
$pagenum = $wp_list_table->get_pagenum();
$sendback = add_query_arg('paged', $pagenum, $sendback);
$sendback = apply_filters('a2wl_wcpl_bulk_actions_perform', $sendback, $action, $post_ids);
$sendback = remove_query_arg(array('action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view'), $sendback);
wp_redirect($sendback);
exit();
}
}
function ajax_product_info() {
$result = array("state" => "ok", "data" => "");
$post_id = isset($_POST['id']) ? $_POST['id'] : false;
if (!$post_id) {
$result['state'] = 'error';
echo json_encode($result);
wp_die();
}
$external_id = get_post_meta($post_id, "_a2wl_external_id", true);
$time_value = get_post_meta($post_id, '_a2wl_last_update', true);
$time_value = $time_value ? date("Y-m-d H:i:s", $time_value) : 'not updated';
$product_url = get_post_meta($post_id, '_product_url', true);
if(!$product_url){
$product_url = get_post_meta($post_id, '_a2wl_original_product_url', true);
}
$content = array();
$content[] = "Product url: here";
/*
* Seller URL disabled, maybe in future we turn it on again
$seller_url = get_post_meta($post_id, '_a2wl_seller_url', true);
if ($seller_url) {
$content[] = "Seller url: here";
}
*/
$content[] = "External ID: " . $external_id . "";
$content[] = "Last auto-update: " . $time_value . "";
$content = apply_filters('a2wl_ajax_product_info', $content, $post_id, $external_id);
$result['data'] = array('content' => $content, 'id' => $post_id);
echo json_encode($result);
wp_die();
}
function ajax_sync_products() {
a2wl_init_error_handler();
try {
$ids = isset($_POST['ids']) ? (is_array($_POST['ids']) ? $_POST['ids'] : array($_POST['ids'])) : array();
$a2wl_sync_type = a2wl_get_setting('sync_type');
$products = array();
foreach ($ids as $post_id) {
$product = $this->woocommerce_model->get_product_by_post_id($post_id, false);
if ($product) {
if($a2wl_sync_type === 'price'){
$product['disable_var_quantity_change'] = true;
}else if($a2wl_sync_type === 'stock'){
$product['disable_var_price_change'] = true;
}else if($a2wl_sync_type === 'no'){
$product['disable_var_price_change'] = true;
$product['disable_var_quantity_change'] = true;
}
$products[$product['id']] = $product;
}
}
$result = array("state" => "ok", "update_state" => array('ok' => count($ids), 'error' => 0));
if(count($products)>0){
$res = $this->aliexpress_model->sync_products(array_keys($products),array('manual_update'=>1));
if ($res['state'] === 'error') {
$result = $res;
} else {
foreach ($res['products'] as $product) {
$product = array_replace_recursive($products[$product['id']], $product);
$product = A2WL_PriceFormula::apply_formula($product);
$this->woocommerce_model->upd_product($product['post_id'], $product, array('manual_update'=>1));
}
}
}
} catch (Exception $e) {
$result = A2WL_ResultBuilder::buildError($e->getMessage());
}
echo json_encode($result);
wp_die();
}
function ajax_get_product_id(){
if (!empty($_POST['post_id'])) {
$id = $this->woocommerce_model->get_product_external_id($_POST['post_id']);
if($id){
$result = A2WL_ResultBuilder::buildOk(array('id'=>$id));
}else{
$result = A2WL_ResultBuilder::buildError('uncknown ID');
}
}else{
$result = A2WL_ResultBuilder::buildError("get_product_id: waiting for ID...");
}
echo json_encode($result);
wp_die();
}
}
}