| 1 |
<?php |
| 2 |
/** |
| 3 |
* Description of ProductAdminWidgetController |
| 4 |
* |
| 5 |
* @author Ali2Woo Team |
| 6 |
* |
| 7 |
* @autoload: a2wl_admin_init |
| 8 |
* |
| 9 |
* @ajax: true |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace AliNext_Lite;; |
| 13 |
|
| 14 |
use Pages; |
| 15 |
use WC_Product; |
| 16 |
|
| 17 |
class ProductInfoWidgetController extends AbstractController |
| 18 |
{ |
| 19 |
public const PARAM_SHOW_PRODUCT_VIDEO_TAB = 'a2wl_show_product_video_tab'; |
| 20 |
|
| 21 |
protected VideoShortcodeService $VideoShortcodeService; |
| 22 |
protected ImportedProductServiceFactory $ImportedProductServiceFactory; |
| 23 |
protected ProductShippingDataService $ProductShippingDataService; |
| 24 |
|
| 25 |
public function __construct( |
| 26 |
VideoShortcodeService $VideoShortcodeService, |
| 27 |
ImportedProductServiceFactory $ImportedProductServiceFactory, |
| 28 |
ProductShippingDataService $ProductShippingDataService |
| 29 |
) { |
| 30 |
parent::__construct(); |
| 31 |
|
| 32 |
$this->VideoShortcodeService = $VideoShortcodeService; |
| 33 |
$this->ImportedProductServiceFactory = $ImportedProductServiceFactory; |
| 34 |
$this->ProductShippingDataService = $ProductShippingDataService; |
| 35 |
|
| 36 |
add_action('a2wl_admin_assets', [$this, 'enqueueAdminAssets'], 2); |
| 37 |
add_action('add_meta_boxes', [$this, 'addProductInfoMetabox']); |
| 38 |
add_action( |
| 39 |
'woocommerce_admin_process_product_object', |
| 40 |
[$this, 'woocommerce_admin_process_product_object'] |
| 41 |
); |
| 42 |
add_action('wp_ajax_a2wl_reset_product_shipping_cache', [$this, 'ajaxResetProductShippingCache']); |
| 43 |
} |
| 44 |
|
| 45 |
public function enqueueAdminAssets(): void |
| 46 |
{ |
| 47 |
wp_localize_script( |
| 48 |
'a2wl-admin-script', |
| 49 |
'a2wl_product_info_data', |
| 50 |
[ |
| 51 |
'lang' => [ |
| 52 |
'video_shortcode_copied' => esc_html__('Video shortcode copied', 'ali2woo') |
| 53 |
] |
| 54 |
] |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
public function addProductInfoMetabox(): void |
| 59 |
{ |
| 60 |
global $pagenow, $post; |
| 61 |
|
| 62 |
if ($pagenow === 'post.php' && $post) { |
| 63 |
$WC_Product = wc_get_product($post->ID); |
| 64 |
if (!$WC_Product) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
$ImportedProductService = $this->ImportedProductServiceFactory->createFromProduct($WC_Product); |
| 69 |
|
| 70 |
if ($ImportedProductService->getExternalId()) { |
| 71 |
add_meta_box( |
| 72 |
'a2wl-product-info', |
| 73 |
esc_html__( 'AliExpress product info', 'ali2woo' ), |
| 74 |
[$this, 'productInfoMetaboxCallback'], |
| 75 |
'product', |
| 76 |
'side', |
| 77 |
'high' |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function productInfoMetaboxCallback(): void |
| 84 |
{ |
| 85 |
global $post; |
| 86 |
|
| 87 |
$WC_Product = wc_get_product($post->ID); |
| 88 |
$ImportedProductService = $this->ImportedProductServiceFactory->createFromProduct($WC_Product); |
| 89 |
|
| 90 |
$videoData = $ImportedProductService->getVideoData(); |
| 91 |
|
| 92 |
if ($videoData) { |
| 93 |
$videoShortcodeContent = $this->VideoShortcodeService->buildFromVideoData($videoData); |
| 94 |
$this->model_put('videoShortcodeContent', $videoShortcodeContent); |
| 95 |
} else { |
| 96 |
$this->model_put('videoShortcodeContent', null); |
| 97 |
} |
| 98 |
|
| 99 |
$showVideoTabGlobalText = get_setting(Settings::SETTING_SHOW_PRODUCT_VIDEO_TAB) ? |
| 100 |
esc_html__( 'Show', 'ali2woo' ) : |
| 101 |
esc_html__( 'Hide', 'ali2woo' ); |
| 102 |
|
| 103 |
$defaultGlobalSettingText = wp_kses_post( |
| 104 |
sprintf(esc_html__( 'Global setting(%s)', 'ali2woo' ), $showVideoTabGlobalText) |
| 105 |
); |
| 106 |
|
| 107 |
$this->model_put('wcProductId', $post->ID); |
| 108 |
$this->model_put('defaultGlobalSettingText', $defaultGlobalSettingText); |
| 109 |
$this->model_put('ImportedProductService', $ImportedProductService); |
| 110 |
|
| 111 |
$this->include_view("product_info_widget.php"); |
| 112 |
} |
| 113 |
|
| 114 |
public function woocommerce_admin_process_product_object(WC_Product $WC_Product): void |
| 115 |
{ |
| 116 |
if (isset($_POST[self::PARAM_SHOW_PRODUCT_VIDEO_TAB])) { |
| 117 |
check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE); |
| 118 |
$shouldShow = sanitize_text_field($_POST[self::PARAM_SHOW_PRODUCT_VIDEO_TAB]); |
| 119 |
$ImportedProductService = $this->ImportedProductServiceFactory->createFromProduct($WC_Product); |
| 120 |
$ImportedProductService->setShouldShowVideoTab($shouldShow); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public function ajaxResetProductShippingCache(): void |
| 125 |
{ |
| 126 |
check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE); |
| 127 |
|
| 128 |
if (!PageGuardHelper::canAccessPage(Pages::IMPORT_LIST)) { |
| 129 |
$result = ResultBuilder::buildError($this->getErrorTextNoPermissions()); |
| 130 |
echo wp_json_encode($result); |
| 131 |
wp_die(); |
| 132 |
} |
| 133 |
|
| 134 |
if (empty($_POST['id'])) { |
| 135 |
$errorText = esc_html__( |
| 136 |
'Can`t reset product shipping data: wrong params', |
| 137 |
'ali2woo' |
| 138 |
); |
| 139 |
$result = ResultBuilder::buildError($errorText); |
| 140 |
echo wp_json_encode($result); |
| 141 |
wp_die(); |
| 142 |
} |
| 143 |
|
| 144 |
$productId = intval($_POST['id']); |
| 145 |
|
| 146 |
try { |
| 147 |
$this->ProductShippingDataService->resetProductShippingCache($productId); |
| 148 |
} |
| 149 |
catch (RepositoryException $RepositoryException) { |
| 150 |
error_log($RepositoryException->getMessage()); |
| 151 |
$errorText = esc_html__( |
| 152 |
'Can`t reset product shipping cache: repository error', |
| 153 |
'ali2woo' |
| 154 |
); |
| 155 |
$result = ResultBuilder::buildError($errorText); |
| 156 |
echo wp_json_encode($result); |
| 157 |
wp_die(); |
| 158 |
} |
| 159 |
|
| 160 |
echo wp_json_encode(ResultBuilder::buildOk()); |
| 161 |
wp_die(); |
| 162 |
} |
| 163 |
} |
| 164 |
|