| 1 |
<?php |
| 2 |
|
| 3 |
/* * class |
| 4 |
* Description of WooCommerceProductEditController |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
* |
| 8 |
* @autoload: a2wl_admin_init |
| 9 |
* |
| 10 |
* @ajax: true |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace AliNext_Lite;; |
| 14 |
|
| 15 |
use Pages; |
| 16 |
|
| 17 |
class WooCommerceProductEditController extends AbstractController |
| 18 |
{ |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
parent::__construct(); |
| 22 |
|
| 23 |
add_action('current_screen', [$this, 'current_screen']); |
| 24 |
add_action('edit_form_advanced', array($this, 'edit_form_advanced')); |
| 25 |
add_action('a2wl_after_import', array($this, 'edit_form_advanced')); |
| 26 |
add_action('wp_ajax_a2wl_get_image_by_id', [$this, 'ajax_get_image_by_id']); |
| 27 |
|
| 28 |
|
| 29 |
add_action('wp_ajax_a2wl_edit_image_url', [$this, 'ajax_edit_image_url']); |
| 30 |
|
| 31 |
add_filter('get_sample_permalink_html', [$this, 'get_sample_permalink_html'], 10, 2); |
| 32 |
} |
| 33 |
|
| 34 |
public function get_sample_permalink_html(string $return, int $id): string |
| 35 |
{ |
| 36 |
$external_id = get_post_meta($id, "_a2w_external_id", true); |
| 37 |
if ($external_id) { |
| 38 |
$return .= sprintf( |
| 39 |
'<button type="button" data-id="%d" class="%s">%s</button>', |
| 40 |
$id, |
| 41 |
'sync-ali-product button button-small hide-if-no-js', |
| 42 |
esc_html_x("AliExpress Sync", 'product editing page', 'ali2woo') |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
return $return; |
| 47 |
} |
| 48 |
|
| 49 |
function current_screen($current_screen): void |
| 50 |
{ |
| 51 |
$importPageScreenId = A2WL()->plugin_slug . '_page_a2wl_import'; |
| 52 |
$checkPage = $current_screen->id == 'product' || $current_screen->id == $importPageScreenId; |
| 53 |
if ($current_screen->in_admin() && $checkPage) { |
| 54 |
if (!wp_script_is('a2wl-admin-script', 'enqueued')) { |
| 55 |
wp_enqueue_script('a2wl-admin-script', |
| 56 |
A2WL()->plugin_url() . '/assets/js/admin_script.js', |
| 57 |
['jquery'], |
| 58 |
A2WL()->version |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
AbstractAdminPage::localizeAdminScript(); |
| 63 |
|
| 64 |
$lang_data = [ |
| 65 |
/* translators: %d is replaced with "digit" */ |
| 66 |
'process_loading_d_of_d_erros_d' => esc_html_x( |
| 67 |
'Process loading %d of %d. Errors: %d.', |
| 68 |
'Status', |
| 69 |
'ali2woo' |
| 70 |
), |
| 71 |
/* translators: %d is replaced with "digit" */ |
| 72 |
'load_button_text' => esc_html_x('Load %d images', 'Status', 'ali2woo'), |
| 73 |
'all_images_loaded_text' => esc_html_x('All images loaded', 'Status', 'ali2woo'), |
| 74 |
]; |
| 75 |
wp_localize_script( |
| 76 |
'a2wl-admin-script', |
| 77 |
'a2wl_external_images_data', |
| 78 |
['lang' => $lang_data] |
| 79 |
); |
| 80 |
|
| 81 |
$lang_data = [ |
| 82 |
'sync_successfully' => esc_html_x('Synchronized successfully.', 'Status', 'ali2woo'), |
| 83 |
'sync_failed' => esc_html_x('Sync failed.', 'Status', 'ali2woo'), |
| 84 |
]; |
| 85 |
wp_localize_script('a2wl-admin-script', 'a2wl_sync_data', ['lang' => $lang_data]); |
| 86 |
|
| 87 |
|
| 88 |
wp_enqueue_style( |
| 89 |
'a2wl-admin-style', |
| 90 |
A2WL()->plugin_url() . '/assets/css/admin_style.css', |
| 91 |
[], |
| 92 |
A2WL()->version |
| 93 |
); |
| 94 |
|
| 95 |
wp_enqueue_style( |
| 96 |
'a2wl-wc-spectrum-style', |
| 97 |
A2WL()->plugin_url() . '/assets/js/spectrum/spectrum.css', |
| 98 |
[], |
| 99 |
A2WL()->version |
| 100 |
); |
| 101 |
|
| 102 |
wp_enqueue_script( |
| 103 |
'a2wl-wc-spectrum-script', |
| 104 |
A2WL()->plugin_url() . '/assets/js/spectrum/spectrum.js', |
| 105 |
[], |
| 106 |
A2WL()->version |
| 107 |
); |
| 108 |
|
| 109 |
wp_enqueue_script( |
| 110 |
'tui-image-editor-FileSaver', |
| 111 |
A2WL()->plugin_url() . '/assets/js/image-editor/FileSaver.min.js', |
| 112 |
['jquery'], |
| 113 |
A2WL()->version) |
| 114 |
; |
| 115 |
wp_enqueue_script( |
| 116 |
'tui-image-editor', |
| 117 |
A2WL()->plugin_url() . '/assets/js/image-editor/tui-image-editor.js', |
| 118 |
['jquery'], |
| 119 |
A2WL()->version |
| 120 |
); |
| 121 |
|
| 122 |
wp_enqueue_script( |
| 123 |
'a2wl-wc-pe-script', |
| 124 |
A2WL()->plugin_url() . '/assets/js/wc_pe_script.js', |
| 125 |
[], |
| 126 |
A2WL()->version |
| 127 |
); |
| 128 |
wp_enqueue_style( |
| 129 |
'a2wl-wc-pe-style', |
| 130 |
A2WL()->plugin_url() . '/assets/css/wc_pe_style.css', |
| 131 |
[], |
| 132 |
A2WL()->version |
| 133 |
); |
| 134 |
|
| 135 |
$data = [ |
| 136 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 137 |
'nonce_action' => wp_create_nonce(self::AJAX_NONCE_ACTION), |
| 138 |
]; |
| 139 |
wp_localize_script('a2wl-wc-pe-script', 'a2wl_wc_pe_script', $data); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
function edit_form_advanced($post) |
| 144 |
{ |
| 145 |
$current_screen = get_current_screen(); |
| 146 |
$importPageScreenId = A2WL()->plugin_slug . '_page_a2wl_import'; |
| 147 |
$checkPage = $current_screen->id == 'product' || $current_screen->id == $importPageScreenId; |
| 148 |
if ($current_screen && $current_screen->in_admin() && $checkPage) { |
| 149 |
$srickers = get_setting('image_editor_srickers', []); |
| 150 |
|
| 151 |
foreach($srickers as $key=>$sricker){ |
| 152 |
if(substr($sricker, 0, strlen("http")) !== "http"){ |
| 153 |
$srickers[$key] = A2WL()->plugin_url().$sricker; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
$this->model_put('srickers', $srickers); |
| 158 |
$this->include_view('product_edit_photo.php'); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
function ajax_get_image_by_id(): void |
| 163 |
{ |
| 164 |
check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE); |
| 165 |
|
| 166 |
if (!PageGuardHelper::canAccessPage(Pages::IMPORT_LIST)) { |
| 167 |
$result = ResultBuilder::buildError($this->getErrorTextNoPermissions()); |
| 168 |
echo wp_json_encode($result); |
| 169 |
wp_die(); |
| 170 |
} |
| 171 |
|
| 172 |
if (empty($_POST['attachment_id'])) { |
| 173 |
$result = ResultBuilder::buildError("waiting for attachment_id..."); |
| 174 |
} else { |
| 175 |
$image_url = wp_get_attachment_url($_POST['attachment_id']); |
| 176 |
if (!$image_url){ |
| 177 |
$result = ResultBuilder::buildError("waiting for attachment_id..."); |
| 178 |
} else{ |
| 179 |
$result = ResultBuilder::buildOk(array('image_url' => $image_url)); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
echo wp_json_encode($result); |
| 184 |
wp_die(); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
function ajax_edit_image_url(): void |
| 189 |
{ |
| 190 |
$result = ResultBuilder::buildOk(); |
| 191 |
|
| 192 |
check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE); |
| 193 |
|
| 194 |
if (empty($_POST['url'])) { |
| 195 |
$result = ResultBuilder::buildError("waiting url..."); |
| 196 |
} else { |
| 197 |
$result = ResultBuilder::buildOk([ |
| 198 |
'url' => a2wl_image_url($_POST['url']) |
| 199 |
]); |
| 200 |
} |
| 201 |
|
| 202 |
echo wp_json_encode($result); |
| 203 |
wp_die(); |
| 204 |
} |
| 205 |
|
| 206 |
} |
| 207 |
|