| 1 |
<?php |
| 2 |
/** |
| 3 |
* Welcart Item Images |
| 4 |
* |
| 5 |
* Functions for product related. |
| 6 |
* |
| 7 |
* @package Welcart |
| 8 |
*/ |
| 9 |
|
| 10 |
add_action( 'admin_enqueue_scripts', 'usces_item_image_enqueue_scripts' ); |
| 11 |
add_action( 'wp_ajax_wel_item_image_ajax', 'wel_item_image_ajax' ); |
| 12 |
|
| 13 |
/** |
| 14 |
* Hook load js, css use for tab update item image. |
| 15 |
* |
| 16 |
* @param string $hook_suffix name of hook. |
| 17 |
*/ |
| 18 |
function usces_item_image_enqueue_scripts( $hook_suffix ) { |
| 19 |
|
| 20 |
$action = filter_input( INPUT_GET, 'action' ); |
| 21 |
$post = filter_input( INPUT_GET, 'post', FILTER_DEFAULT, FILTER_REQUIRE_SCALAR ); |
| 22 |
|
| 23 |
if ( ( $action && $post && 'welcart-shop_page_usces_itemedit' === $hook_suffix ) || 'welcart-shop_page_usces_itemnew' === $hook_suffix ) { |
| 24 |
|
| 25 |
if ( 'welcart-shop_page_usces_itemnew' === $hook_suffix ) { |
| 26 |
|
| 27 |
$jquery_cookie_url = USCES_FRONT_PLUGIN_URL . '/js/jquery/jquery-cookie.js'; |
| 28 |
wp_enqueue_script( 'jquery-cookie', $jquery_cookie_url, array( 'jquery', 'jquery-ui-dialog' ), USCES_VERSION, true ); |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
if ( 'welcart-shop_page_usces_itemedit' === $hook_suffix || 'welcart-shop_page_usces_itemnew' === $hook_suffix ) { |
| 33 |
|
| 34 |
$style_item_images = USCES_FRONT_PLUGIN_URL . '/css/admin-item-images.css'; |
| 35 |
wp_enqueue_style( 'usces_item_images_css', $style_item_images, array(), '1.0.0', 'all' ); |
| 36 |
|
| 37 |
$js_item_images = USCES_FRONT_PLUGIN_URL . '/js/admin-item-images.js'; |
| 38 |
wp_enqueue_script( 'usces_item_images_js', $js_item_images, array( 'jquery', 'jquery-ui-core', 'jquery-ui-tabs' ), USCES_VERSION, true ); |
| 39 |
wp_localize_script( |
| 40 |
'usces_item_images_js', |
| 41 |
'usces_item_images_js_setting', |
| 42 |
array( |
| 43 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 44 |
'_ajax_nonce' => wp_create_nonce( 'wel_item_images_nonce' ), |
| 45 |
'title_image_frame' => __( 'Select Media', 'usces' ), |
| 46 |
'text_close' => __( 'close', 'usces' ), |
| 47 |
'msg_choose_item_image' => __( 'Please choose at least one item image.', 'usces' ), |
| 48 |
'msg_upload_file_size_error' => __( 'File size error.', 'usces' ), |
| 49 |
'msg_nonce_expried' => __( 'The link you followed has expired. Please refresh your site.', 'usces' ), |
| 50 |
) |
| 51 |
); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Handle sync item image from old workflow. |
| 58 |
* |
| 59 |
* @param integer $post_id item post id. |
| 60 |
* @param boolean $cache Switch of cache. |
| 61 |
*/ |
| 62 |
function wel_sync_item_images( $post_id, $cache = true ) { |
| 63 |
$meta_key = '_itemPicts'; |
| 64 |
$item_picts = usces_get_post_meta( $post_id, $meta_key, $cache ); |
| 65 |
if ( is_array( $item_picts ) && 0 === count( $item_picts ) ) { |
| 66 |
|
| 67 |
// sync value init. |
| 68 |
$arr_pict_id = array(); |
| 69 |
$main_pict_id = (int) wel_get_main_pict_id( $post_id, $cache ); |
| 70 |
if ( false !== $main_pict_id && 0 < $main_pict_id ) { |
| 71 |
$arr_pict_id[] = $main_pict_id; |
| 72 |
} |
| 73 |
$sub_pict_ids = wel_get_sub_pict_ids( $post_id, $cache ); |
| 74 |
if ( $sub_pict_ids && is_array( $sub_pict_ids ) ) { |
| 75 |
$arr_pict_id = array_merge( $arr_pict_id, $sub_pict_ids ); |
| 76 |
} |
| 77 |
$arr_pict_id_db = array(); |
| 78 |
foreach ( $arr_pict_id as $pict_id ) { |
| 79 |
if ( 0 < $pict_id ) { |
| 80 |
$arr_pict_id_db[] = $pict_id; |
| 81 |
} |
| 82 |
} |
| 83 |
if ( 0 < count( $arr_pict_id_db ) ) { |
| 84 |
$arr_pict_id_db = array_unique( $arr_pict_id_db ); |
| 85 |
$pict_ids = implode( ';', $arr_pict_id_db ); |
| 86 |
} else { |
| 87 |
$pict_ids = null; |
| 88 |
} |
| 89 |
update_post_meta( $post_id, $meta_key, $pict_ids ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Build layout box item image. |
| 95 |
* |
| 96 |
* @param object $post object post info. |
| 97 |
*/ |
| 98 |
function wel_post_item_pict_box_html( $post ) { |
| 99 |
$post_id = isset( $post->ID ) ? $post->ID : 0; |
| 100 |
// init sync list image to meta post. |
| 101 |
// wel_sync_item_images( $post_id );. |
| 102 |
wp_enqueue_media( array( 'post' => $post->ID ) ); |
| 103 |
$item = wel_get_item( $post_id, false ); |
| 104 |
?> |
| 105 |
<div id="wel-item-image-loading"> |
| 106 |
<img id="wel-loading-image" src="<?php echo esc_attr( USCES_PLUGIN_URL ) . '/images/box_image-loading.gif'; ?>" alt="Loading..." /> |
| 107 |
</div> |
| 108 |
<div id="uscestabs_item_images" class="uscestabs usces_item_image" style="display: none"> |
| 109 |
<?php if ( empty( $item ) ) : ?> |
| 110 |
<div class="wel-item-image-notes"> |
| 111 |
<?php esc_html_e( 'The item details such as the item code have to be confirmed when setting the product image.', 'usces' ); ?><br> |
| 112 |
<?php esc_html_e( 'Before setting the item image, the item registration has to be finalized by clicking either "Draft" or "Save" button.', 'usces' ); ?> |
| 113 |
</div> |
| 114 |
<?php endif; ?> |
| 115 |
<ul> |
| 116 |
<li><a id="usces_tabs_item_img" href="#uscestabs_item_img"><?php esc_attr_e( 'Image', 'usces' ); ?></a></li> |
| 117 |
<li><a id="usces_tabs_item_file" href="#uscestabs_item_file"><?php esc_attr_e( 'File', 'usces' ); ?></a></li> |
| 118 |
<li><a id="usces_tabs_item_upload" href="#uscestabs_item_upload"><?php esc_attr_e( 'Upload', 'usces' ); ?></a></li> |
| 119 |
<li class="wel-tab-item-apply"><input type='button' class="custom-show-modal" value="<?php echo( esc_attr_x( 'Apply', 'image file', 'usces' ) ); ?>" id="wel_item_img_media_manager"/></li> |
| 120 |
</ul> |
| 121 |
<div id="uscestabs_item_img"> |
| 122 |
<?php echo wel_sub_item_pict_box_img_html( $post_id ); ?> |
| 123 |
</div> |
| 124 |
<div id="uscestabs_item_file"> |
| 125 |
<?php echo wel_sub_item_pict_box_file_html( $post_id ); ?> |
| 126 |
</div> |
| 127 |
<div id="uscestabs_item_upload"> |
| 128 |
<?php echo wel_sub_item_pict_box_upload_html( $post_id ); ?> |
| 129 |
</div> |
| 130 |
<input type="hidden" name="wel_image_post_id" id="wel_image_post_id" value="<?php echo esc_attr( $post_id ); ?>" /> |
| 131 |
<div class="wel_item_image_dialog_wrap" id="wel_item_image_dialog_wrap" title="<?php esc_attr_e( 'Error', 'usces' ); ?>"> |
| 132 |
<div id="wel_item_image_dialog_content"></div> |
| 133 |
</div> |
| 134 |
</div> |
| 135 |
<?php |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Build layout tab Image. |
| 140 |
* |
| 141 |
* @param integer $post_id item post id. |
| 142 |
*/ |
| 143 |
function wel_sub_item_pict_box_img_html( $post_id ) { |
| 144 |
$item_picts = array(); |
| 145 |
$arr_pict_ids = wel_get_item_pict_ids( $post_id, false ); |
| 146 |
foreach ( $arr_pict_ids as $pict_id ) { |
| 147 |
$alt = trim( strip_tags( get_post_meta( $pict_id, '_wp_attachment_image_alt', true ) ) ); |
| 148 |
$post_img = get_post( $pict_id, ARRAY_A ); |
| 149 |
$caption = isset( $post_img['post_excerpt'] ) ? $post_img['post_excerpt'] : ''; |
| 150 |
$item_picts[] = array( |
| 151 |
'pict_id' => $pict_id, |
| 152 |
'alt' => $alt, |
| 153 |
'caption' => $caption, |
| 154 |
'main_img' => wp_get_attachment_image( $pict_id, array( 270, 270 ), true ), |
| 155 |
'thumb_img' => wp_get_attachment_image( $pict_id, array( 50, 50 ), true ), |
| 156 |
); |
| 157 |
} |
| 158 |
$main_item_pict = isset( $item_picts[0] ) ? $item_picts[0] : array(); |
| 159 |
ob_start(); |
| 160 |
?> |
| 161 |
<div class="item-main-pict"> |
| 162 |
<div class="wel_wrap_img"> |
| 163 |
<div id="item-select-pict"> |
| 164 |
<?php echo isset( $main_item_pict['main_img'] ) ? $main_item_pict['main_img'] : ''; ?> |
| 165 |
</div> |
| 166 |
<?php if ( isset( $item_picts[0] ) ) { ?> |
| 167 |
<div class="mask"> |
| 168 |
<div class="wel-img-item-edit wel-img-alt-text" data-setting="alt"> |
| 169 |
<label for="wel-img-alt-text" class="name"><?php _e( 'Alt Text' ); ?></label> |
| 170 |
<input type="text" onblur="wel_item_images.save_pict_info('alt' )" id="wel-img-alt" value="<?php echo esc_attr( $main_item_pict['alt'] ); ?>" > |
| 171 |
</div> |
| 172 |
<div class="wel-img-item-edit wel-img-caption-text" data-setting="caption"> |
| 173 |
<label for="wel-img-caption" class="name"><?php _e( 'Caption' ); ?></label> |
| 174 |
<textarea rows="1" id="wel-img-caption" onblur="wel_item_images.save_pict_info('caption')" ><?php echo esc_attr( $main_item_pict['caption'] ); ?></textarea> |
| 175 |
</div> |
| 176 |
<input type="hidden" id="wel-img-pict-id" value="<?php echo (int) $main_item_pict['pict_id']; ?>"/> |
| 177 |
<input type="hidden" id="wel-img-pict-ids" value="<?php echo esc_attr( implode( ',', $arr_pict_ids ) ); ?>" /> |
| 178 |
</div> |
| 179 |
<?php } ?> |
| 180 |
</div> |
| 181 |
<div class="clearfix"> |
| 182 |
<?php foreach ( $item_picts as $item_pict ) { ?> |
| 183 |
<div class="subpict"> |
| 184 |
<a class="wel-view-main-img" id="wel-pict-<?php echo (int) $item_pict['pict_id']; ?>" data-id="<?php echo (int) $item_pict['pict_id']; ?>" data-img='<?php wel_esc_script_e( $item_pict['main_img'] ); ?>' data-alt="<?php echo esc_attr( $item_pict['alt'] ); ?>" data-caption="<?php echo esc_attr( $item_pict['caption'] ); ?>"><?php wel_esc_script_e( $item_pict['thumb_img'] ); ?></a> |
| 185 |
</div> |
| 186 |
<?php } ?> |
| 187 |
</div> |
| 188 |
</div> |
| 189 |
<?php |
| 190 |
$html = ob_get_contents(); |
| 191 |
ob_end_clean(); |
| 192 |
return $html; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Build layout tab File. |
| 197 |
* |
| 198 |
* @param integer $post_id item post id. |
| 199 |
*/ |
| 200 |
function wel_sub_item_pict_box_file_html( $post_id ) { |
| 201 |
ob_start(); |
| 202 |
?> |
| 203 |
<div id="wrapper_tab_file_item_pict"> |
| 204 |
<?php echo wel_build_list_detail_tab_file( $post_id ); ?> |
| 205 |
</div> |
| 206 |
<div class="file_action"> |
| 207 |
<input class="button-primary" type="button" value="<?php esc_attr_e( 'Exclude', 'usces' ); ?>" name="wel_file_exclude" onclick="wel_item_images.exclude_item_image()" id="wel_file_exclude"> |
| 208 |
<input class="button-primary wel-item-pict-del" type="button" value="<?php echo( esc_attr_x( 'Delete', 'image file', 'usces' ) ); ?>" name="wel_file_delete" onclick="wel_item_images.check_before_delete_item_image()" id="wel_file_delete"> |
| 209 |
</div> |
| 210 |
<?php |
| 211 |
$html = ob_get_contents(); |
| 212 |
ob_end_clean(); |
| 213 |
return $html; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Build layout detail list file name. |
| 218 |
* |
| 219 |
* @param integer $post_id item post id. |
| 220 |
*/ |
| 221 |
function wel_build_list_detail_tab_file( $post_id ) { |
| 222 |
$arr_pict_file_name = wel_get_item_pict_filename( $post_id ); |
| 223 |
ob_start(); |
| 224 |
foreach ( $arr_pict_file_name as $pict_id => $filename ) { |
| 225 |
?> |
| 226 |
<div class="tab_file_item_pict" id="<?php echo esc_attr( $pict_id ); ?>"> |
| 227 |
<input type="checkbox" value="<?php echo esc_attr( $pict_id ); ?>" name="file_item_picts[]"> |
| 228 |
<?php echo wp_get_attachment_image( $pict_id, array( 30, 30 ), true ); ?> |
| 229 |
<label><?php echo esc_html( $filename ) . '(' . esc_attr( $pict_id ) . ')'; ?></label> |
| 230 |
</div> |
| 231 |
<?php |
| 232 |
} |
| 233 |
?> |
| 234 |
<img id="wel-tab-file-loading" style="display: none" src="<?php echo esc_attr( USCES_PLUGIN_URL ) . '/images/loading.gif'; ?>" alt="Loading..." /> |
| 235 |
<?php |
| 236 |
$html = ob_get_contents(); |
| 237 |
ob_end_clean(); |
| 238 |
return $html; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Build layout tab Upload. |
| 243 |
* |
| 244 |
* @param mixed $post_id item post id. |
| 245 |
*/ |
| 246 |
function wel_sub_item_pict_box_upload_html( $post_id ) { |
| 247 |
$max_upload_size = wp_max_upload_size(); |
| 248 |
if ( ! $max_upload_size ) { |
| 249 |
$max_upload_size = 0; |
| 250 |
} |
| 251 |
ob_start(); |
| 252 |
?> |
| 253 |
<div id="plupload-upload-ui" class="hide-if-no-js"> |
| 254 |
<div id="drag-drop-area"> |
| 255 |
<div class="drag-drop-inside"> |
| 256 |
<p class="drag-drop-info"><?php esc_attr_e( 'Drop files to upload', 'usces' ); ?></p> |
| 257 |
<p><?php echo( esc_attr_x( 'or', 'Uploader: Drop files here - or - Select Files', 'usces' ) ); ?></p> |
| 258 |
<p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e( 'Select Files', 'usces' ); ?>" class="button" /></p> |
| 259 |
<p class="max-upload-size"> |
| 260 |
<?php |
| 261 |
// translators: %s: Maximum allowed file size. |
| 262 |
printf( esc_html__( 'Maximum upload file size: %s', 'usces' ), esc_html( size_format( $max_upload_size ) ) ); |
| 263 |
?> |
| 264 |
</p> |
| 265 |
</div> |
| 266 |
</div> |
| 267 |
</div> |
| 268 |
<?php |
| 269 |
// we should probably not apply this filter, plugins may expect wp's media uploader... |
| 270 |
$plupload_init = wel_plupload_init_item_images( $post_id ); |
| 271 |
?> |
| 272 |
<script type="text/javascript"> |
| 273 |
jQuery(document).ready(function ($) { |
| 274 |
// create the uploader and pass the config from above |
| 275 |
var uploader = new plupload.Uploader(<?php echo wp_json_encode( $plupload_init ); ?>); |
| 276 |
// checks if browser supports drag and drop upload, makes some css adjustments if necessary |
| 277 |
uploader.bind('Init', function (up) { |
| 278 |
var uploaddiv = $('#plupload-upload-ui'); |
| 279 |
if (up.features.dragdrop) { |
| 280 |
uploaddiv.addClass('drag-drop'); |
| 281 |
$('#drag-drop-area') |
| 282 |
.bind('dragover.wp-uploader', function () { |
| 283 |
uploaddiv.addClass('drag-over'); |
| 284 |
}) |
| 285 |
.bind('dragleave.wp-uploader, drop.wp-uploader', function () { |
| 286 |
uploaddiv.removeClass('drag-over'); |
| 287 |
}); |
| 288 |
} else { |
| 289 |
uploaddiv.removeClass('drag-drop'); |
| 290 |
$('#drag-drop-area').unbind('.wp-uploader'); |
| 291 |
} |
| 292 |
}); |
| 293 |
uploader.init(); |
| 294 |
uploader.bind('BeforeUpload', function (up, file) { |
| 295 |
// add effect loading. |
| 296 |
$("#uscestabs_item_upload #plupload-upload-ui").addClass("wel-loading-upload"); |
| 297 |
$("#uscestabs_item_upload #plupload-browse-button").prop("disabled", true); |
| 298 |
}); |
| 299 |
// a file was added in the queue |
| 300 |
uploader.bind('FilesAdded', function (up, files) { |
| 301 |
var hundredmb = 100 * 1024 * 1024, max = parseInt(up.settings.max_file_size, 10); |
| 302 |
plupload.each(files, function (file) { |
| 303 |
if (max > hundredmb && file.size > hundredmb && up.runtime != 'html5') { |
| 304 |
// file size error. |
| 305 |
$("#wel_item_image_dialog_content").html(usces_item_images_js_setting.msg_upload_file_size_error); |
| 306 |
$("#uscestabs_item_images #wel_item_image_dialog_wrap").dialog("open"); |
| 307 |
// remove effect loading. |
| 308 |
$("#uscestabs_item_upload #plupload-upload-ui").removeClass("wel-loading-upload"); |
| 309 |
$("#uscestabs_item_upload #plupload-browse-button").removeAttr('disabled'); |
| 310 |
} |
| 311 |
}); |
| 312 |
up.refresh(); |
| 313 |
up.start(); |
| 314 |
}); |
| 315 |
uploader.bind('UploadProgress', function (up, files) { |
| 316 |
$("#item-main-pict #wel-tab-file-loading").show(); |
| 317 |
}); |
| 318 |
// a file was uploaded |
| 319 |
uploader.bind('FileUploaded', function (up, file, res) { |
| 320 |
// this is your ajax response, update the DOM with it or something. |
| 321 |
var obj_res = jQuery.parseJSON(res.response); |
| 322 |
if (obj_res.status) { |
| 323 |
// Load list on the tab file. |
| 324 |
$('#uscestabs_item_images #uscestabs_item_file #wrapper_tab_file_item_pict').html(obj_res.data_tab_file); |
| 325 |
// Load list on the tab image. |
| 326 |
$('#uscestabs_item_images #uscestabs_item_img').html(obj_res.data_tab_image); |
| 327 |
// active tab file. |
| 328 |
$("#uscestabs_item_images #usces_tabs_item_file").click(); |
| 329 |
// add more scoll. |
| 330 |
$("#uscestabs_item_file #wrapper_tab_file_item_pict").animate({ scrollTop: $("#uscestabs_item_file #wrapper_tab_file_item_pict")[0].scrollHeight}, 700); |
| 331 |
} else { |
| 332 |
$("#item-main-pict #wel-tab-file-loading").hide(); |
| 333 |
$("#wel_item_image_dialog_content").append(obj_res.msg); |
| 334 |
$("#wel_item_image_dialog_wrap").dialog("open"); |
| 335 |
} |
| 336 |
// remove effect loading. |
| 337 |
$("#uscestabs_item_upload #plupload-upload-ui").removeClass("wel-loading-upload"); |
| 338 |
$("#uscestabs_item_upload #plupload-browse-button").removeAttr('disabled'); |
| 339 |
}); |
| 340 |
}); |
| 341 |
</script> |
| 342 |
<?php |
| 343 |
$html = ob_get_contents(); |
| 344 |
ob_end_clean(); |
| 345 |
return $html; |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* List all file name item pict by item post id. |
| 350 |
* |
| 351 |
* @param integer $post_id item post id. |
| 352 |
* |
| 353 |
* @return array |
| 354 |
*/ |
| 355 |
function wel_get_item_pict_filename( $post_id ) { |
| 356 |
$arr_result = array(); |
| 357 |
$arr_pict_ids = wel_get_item_pict_ids( $post_id, false ); |
| 358 |
if ( 0 < count( $arr_pict_ids ) ) { |
| 359 |
foreach ( $arr_pict_ids as $pict_id ) { |
| 360 |
$arr_result[ $pict_id ] = wel_get_item_image_name( $pict_id ); |
| 361 |
} |
| 362 |
} |
| 363 |
return $arr_result; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Get name image from string file. |
| 368 |
* |
| 369 |
* @param int $pict_id file id. |
| 370 |
* |
| 371 |
* @return string file name. |
| 372 |
*/ |
| 373 |
function wel_get_item_image_name( $pict_id ) { |
| 374 |
$file = get_attached_file( $pict_id ); |
| 375 |
$filename = ''; |
| 376 |
if ( $file ) { |
| 377 |
$filename = wp_basename( $file ); |
| 378 |
} |
| 379 |
return $filename; |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Handle all ajax request of item image. |
| 384 |
*/ |
| 385 |
function wel_item_image_ajax() { |
| 386 |
$arr_res = array( |
| 387 |
'status' => false, |
| 388 |
'data' => array(), |
| 389 |
'msg' => '', |
| 390 |
); |
| 391 |
$mode = isset( $_POST['mode'] ) ? sanitize_text_field( wp_unslash( $_POST['mode'] ) ) : ''; |
| 392 |
$post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0; |
| 393 |
|
| 394 |
if ( ! current_user_can( 'wel_publish_products', $post_id ) ) { |
| 395 |
$arr_res['status'] = false; |
| 396 |
$arr_res['msg'] .= __( 'You are not allowed to edit this item.', 'usces' ) . '<br>'; |
| 397 |
wp_send_json( $arr_res ); |
| 398 |
} |
| 399 |
|
| 400 |
switch ( $mode ) { |
| 401 |
case 'wel_photo_gallery_upload': |
| 402 |
check_ajax_referer( 'photo-upload' ); |
| 403 |
$name_image = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; |
| 404 |
$img_post_ids = wel_item_image_gallery_upload(); |
| 405 |
if ( is_integer( $img_post_ids ) ) { |
| 406 |
// register image for item post. |
| 407 |
$result = wel_add_item_image_ids( $post_id, $img_post_ids ); |
| 408 |
if ( false !== $result ) { |
| 409 |
$arr_res['status'] = true; |
| 410 |
$arr_res['data_tab_file'] = wel_build_list_detail_tab_file( $post_id ); |
| 411 |
$arr_res['data_tab_image'] = wel_sub_item_pict_box_img_html( $post_id ); |
| 412 |
} else { |
| 413 |
$arr_res['status'] = false; |
| 414 |
$arr_res['msg'] = '<b>' . esc_attr( $name_image ) . '</b>'; |
| 415 |
$arr_res['msg'] .= __( 'Upload item image fail', 'usces' ) . '<br>'; |
| 416 |
} |
| 417 |
} else { |
| 418 |
$arr_res['status'] = false; |
| 419 |
if ( isset( $img_post_ids->errors['upload_error'] ) ) { |
| 420 |
$arr_res['msg'] = '<b>' . esc_attr( $name_image ) . '</b>'; |
| 421 |
$arr_res['msg'] .= '<div>'; |
| 422 |
foreach ( $img_post_ids->errors['upload_error'] as $msg ) { |
| 423 |
$arr_res['msg'] .= esc_attr( $msg ) . '<br>'; |
| 424 |
} |
| 425 |
$arr_res['msg'] .= '</div>'; |
| 426 |
} |
| 427 |
} |
| 428 |
break; |
| 429 |
case 'choose_images_from_media': |
| 430 |
check_ajax_referer( 'wel_item_images_nonce' ); |
| 431 |
$str_pict_ids = isset( $_POST['str_pict_ids'] ) ? sanitize_text_field( wp_unslash( $_POST['str_pict_ids'] ) ) : ''; |
| 432 |
if ( ! empty( $str_pict_ids ) ) { |
| 433 |
// register image for item post. |
| 434 |
$img_post_ids = explode( ',', $str_pict_ids ); |
| 435 |
$img_post_ids = wel_validate_item_pict_ids( $img_post_ids ); |
| 436 |
$result = wel_add_item_image_ids( $post_id, $img_post_ids ); |
| 437 |
$arr_res['status'] = true; |
| 438 |
$arr_res['data_tab_file'] = wel_build_list_detail_tab_file( $post_id ); |
| 439 |
$arr_res['data_tab_image'] = wel_sub_item_pict_box_img_html( $post_id ); |
| 440 |
} |
| 441 |
break; |
| 442 |
case 'sort_order_item_image': |
| 443 |
check_ajax_referer( 'wel_item_images_nonce' ); |
| 444 |
$item_pict_ids = filter_input( INPUT_POST, 'item_pict_ids', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 445 |
$item_pict_ids = wel_validate_item_pict_ids( $item_pict_ids ); |
| 446 |
if ( is_array( $item_pict_ids ) && 0 < count( $item_pict_ids ) ) { |
| 447 |
// update new item image. |
| 448 |
$update = wel_update_item_pict( $post_id, $item_pict_ids ); |
| 449 |
if ( false !== $update ) { |
| 450 |
$arr_res['status'] = true; |
| 451 |
$arr_res['data_tab_file'] = wel_build_list_detail_tab_file( $post_id ); |
| 452 |
$arr_res['data_tab_image'] = wel_sub_item_pict_box_img_html( $post_id ); |
| 453 |
} else { |
| 454 |
$arr_res['status'] = false; |
| 455 |
$arr_res['msg'] = __( 'Sort item images fail', 'usces' ); |
| 456 |
} |
| 457 |
} |
| 458 |
break; |
| 459 |
case 'exclude_item_image': |
| 460 |
check_ajax_referer( 'wel_item_images_nonce' ); |
| 461 |
$exclude_pict_ids = filter_input( INPUT_POST, 'item_pict_ids', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 462 |
$exclude_pict_ids = array_map( 'intval', $exclude_pict_ids ); |
| 463 |
if ( is_array( $exclude_pict_ids ) && 0 < count( $exclude_pict_ids ) ) { |
| 464 |
$arr_pict_ids = array(); |
| 465 |
$arr_item_picts = wel_get_item_pict_ids( $post_id, false ); |
| 466 |
foreach ( $arr_item_picts as $pict_id ) { |
| 467 |
if ( ! in_array( (int) $pict_id, $exclude_pict_ids, true ) ) { |
| 468 |
$arr_pict_ids[] = (int) $pict_id; |
| 469 |
} |
| 470 |
} |
| 471 |
// update new item image. |
| 472 |
$update = wel_update_item_pict( $post_id, $arr_pict_ids ); |
| 473 |
if ( false !== $update ) { |
| 474 |
$arr_res['status'] = true; |
| 475 |
$arr_res['data_tab_file'] = wel_build_list_detail_tab_file( $post_id ); |
| 476 |
$arr_res['data_tab_image'] = wel_sub_item_pict_box_img_html( $post_id ); |
| 477 |
} else { |
| 478 |
$arr_res['status'] = false; |
| 479 |
$arr_res['msg'] = __( 'Exclude item images fail', 'usces' ); |
| 480 |
} |
| 481 |
} |
| 482 |
break; |
| 483 |
case 'validate_item_image_before_delete': |
| 484 |
check_ajax_referer( 'wel_item_images_nonce' ); |
| 485 |
$item_pict_ids = filter_input( INPUT_POST, 'item_pict_ids', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 486 |
$item_pict_ids = array_map( 'intval', $item_pict_ids ); |
| 487 |
if ( is_array( $item_pict_ids ) && 0 < count( $item_pict_ids ) ) { |
| 488 |
$check_in_other_product = false; |
| 489 |
foreach ( $item_pict_ids as $item_pict_id ) { |
| 490 |
$check_post_id = wel_check_item_pict_id_other_product( $post_id, $item_pict_id ); |
| 491 |
if ( 0 < $check_post_id ) { |
| 492 |
$check_in_other_product = true; |
| 493 |
break; |
| 494 |
} |
| 495 |
} |
| 496 |
$arr_res['status'] = true; |
| 497 |
$arr_res['check_in_other_product'] = $check_in_other_product; |
| 498 |
$arr_res['msg_show_confirm'] = ''; |
| 499 |
if ( $check_in_other_product ) { |
| 500 |
$arr_res['msg_show_confirm'] = __( 'This image also applies to other products. Are you sure you want to delete?', 'usces' ); |
| 501 |
} |
| 502 |
$arr_res['none_item_delete'] = wp_create_nonce( 'wel_item_images_delete_none' ); |
| 503 |
} |
| 504 |
break; |
| 505 |
case 'delete_item_image': |
| 506 |
check_ajax_referer( 'wel_item_images_delete_none' ); |
| 507 |
$delete_pict_ids = filter_input( INPUT_POST, 'item_pict_ids', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 508 |
$delete_pict_ids = array_map( 'intval', $delete_pict_ids ); |
| 509 |
if ( is_array( $delete_pict_ids ) && 0 < count( $delete_pict_ids ) ) { |
| 510 |
// update exclude other item product if has. |
| 511 |
foreach ( $delete_pict_ids as $del_pict_id ) { |
| 512 |
// find all other product has pict id. |
| 513 |
$other_post_ids = wel_get_all_item_pict_id_other_product( $del_pict_id ); |
| 514 |
foreach ( $other_post_ids as $item_id ) { |
| 515 |
$arr_pict_ids = array(); |
| 516 |
$arr_item_picts = wel_get_item_pict_ids( $item_id, false ); |
| 517 |
foreach ( $arr_item_picts as $pict_id ) { |
| 518 |
if ( (int) $pict_id !== (int) $del_pict_id ) { |
| 519 |
$arr_pict_ids[] = (int) $pict_id; |
| 520 |
} |
| 521 |
} |
| 522 |
wel_update_item_pict( $item_id, $arr_pict_ids ); |
| 523 |
} |
| 524 |
// delete form media WP. |
| 525 |
wp_delete_attachment( (int) $del_pict_id, true ); |
| 526 |
} |
| 527 |
$arr_res['status'] = true; |
| 528 |
$arr_res['data_tab_file'] = wel_build_list_detail_tab_file( $post_id ); |
| 529 |
$arr_res['data_tab_image'] = wel_sub_item_pict_box_img_html( $post_id ); |
| 530 |
} else { |
| 531 |
$arr_res['status'] = false; |
| 532 |
$arr_res['msg'] = __( 'Delete item images fail', 'usces' ); |
| 533 |
} |
| 534 |
break; |
| 535 |
case 'save_pict_info': |
| 536 |
check_ajax_referer( 'wel_item_images_nonce' ); |
| 537 |
wel_save_pict_info( $arr_res ); |
| 538 |
break; |
| 539 |
} |
| 540 |
wp_send_json( $arr_res ); |
| 541 |
} |
| 542 |
|
| 543 |
/** |
| 544 |
* Format all pict item id to integer. |
| 545 |
* |
| 546 |
* @param array $item_pict_ids item pict ids. |
| 547 |
* |
| 548 |
* @return array |
| 549 |
*/ |
| 550 |
function wel_validate_item_pict_ids( $item_pict_ids ) { |
| 551 |
$result_item_pict_ids = array(); |
| 552 |
if ( is_array( $item_pict_ids ) && 0 < count( $item_pict_ids ) ) { |
| 553 |
foreach ( $item_pict_ids as $item_pict_id ) { |
| 554 |
$item_pict_id = (int) $item_pict_id; |
| 555 |
if ( 0 < $item_pict_id ) { |
| 556 |
$result_item_pict_ids[] = $item_pict_id; |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
return $result_item_pict_ids; |
| 561 |
} |
| 562 |
|
| 563 |
/** |
| 564 |
* Update pict id for item post. |
| 565 |
* |
| 566 |
* @param integer $post_id item post id. |
| 567 |
* @param array $arr_pict_ids array pict id. |
| 568 |
*/ |
| 569 |
function wel_update_item_pict( $post_id, $arr_pict_ids ) { |
| 570 |
|
| 571 |
$item = wel_get_item( $post_id, false ); |
| 572 |
$item['itemPicts'] = (array) $arr_pict_ids; |
| 573 |
$update = wel_update_item_data( $item, $post_id ); |
| 574 |
|
| 575 |
return $update; |
| 576 |
} |
| 577 |
|
| 578 |
/** |
| 579 |
* Add new IDs to the current product image IDs. |
| 580 |
* |
| 581 |
* @param integer $post_id item Post ID. |
| 582 |
* @param array $new_ids array New Pict ids. |
| 583 |
*/ |
| 584 |
function wel_add_item_image_ids( $post_id, $new_ids ) { |
| 585 |
|
| 586 |
if ( is_int( $new_ids ) ) { |
| 587 |
$new_ids = array( $new_ids ); |
| 588 |
} elseif ( ! is_array( $new_ids ) ) { |
| 589 |
return false; |
| 590 |
} |
| 591 |
|
| 592 |
$current_ids = wel_get_item_pict_ids( $post_id, false ); |
| 593 |
|
| 594 |
foreach ( $new_ids as $pict_id ) { |
| 595 |
if ( ! in_array( (int) $pict_id, $current_ids, true ) ) { |
| 596 |
$current_ids[] = (int) $pict_id; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
$item = wel_get_item( $post_id, false ); |
| 601 |
$item['itemPicts'] = $current_ids; |
| 602 |
$update = wel_update_item_data( $item, $post_id ); |
| 603 |
return $update; |
| 604 |
} |
| 605 |
|
| 606 |
/** |
| 607 |
* Handle build param for tab upload image. |
| 608 |
* |
| 609 |
* @param integer $post_id item post id. |
| 610 |
* |
| 611 |
* @return array |
| 612 |
*/ |
| 613 |
function wel_plupload_init_item_images( $post_id ) { |
| 614 |
$plupload_init = array( |
| 615 |
'runtimes' => 'html5,silverlight,flash,html4', |
| 616 |
'browse_button' => 'plupload-browse-button', |
| 617 |
'container' => 'plupload-upload-ui', |
| 618 |
'drop_element' => 'drag-drop-area', |
| 619 |
'file_data_name' => 'async-upload', |
| 620 |
'multiple_queues' => true, |
| 621 |
'max_file_size' => wp_max_upload_size() . 'b', |
| 622 |
'url' => admin_url( 'admin-ajax.php' ), |
| 623 |
'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ), |
| 624 |
'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ), |
| 625 |
'filters' => array( |
| 626 |
array( |
| 627 |
'title' => __( 'Allowed Files' ), |
| 628 |
'extensions' => 'jpg,jpeg,jpe,png,gif,webp,jfif', // 'jpg', 'jpeg', 'jpe', 'png', 'gif', 'webp' or * (for all) |
| 629 |
), |
| 630 |
), |
| 631 |
'multipart' => true, |
| 632 |
'urlstream_upload' => true, |
| 633 |
// additional post data to send to our ajax hook. |
| 634 |
'multipart_params' => array( |
| 635 |
'_ajax_nonce' => wp_create_nonce( 'photo-upload' ), |
| 636 |
'action' => 'wel_item_image_ajax', |
| 637 |
'mode' => 'wel_photo_gallery_upload', |
| 638 |
'post_id' => $post_id, |
| 639 |
), |
| 640 |
); |
| 641 |
|
| 642 |
// Should probably not apply this filter, plugins may expect wp's media uploader. |
| 643 |
return apply_filters( 'plupload_init', $plupload_init ); |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* Handle upload image to media WP. |
| 648 |
* |
| 649 |
* @return pict_id OR WP error object. |
| 650 |
*/ |
| 651 |
function wel_item_image_gallery_upload() { |
| 652 |
$img_post_id = media_handle_upload( |
| 653 |
'async-upload', |
| 654 |
0, |
| 655 |
array(), |
| 656 |
array( |
| 657 |
'test_form' => true, |
| 658 |
'action' => 'wel_item_image_ajax', |
| 659 |
) |
| 660 |
); |
| 661 |
return $img_post_id; |
| 662 |
} |
| 663 |
|
| 664 |
/** |
| 665 |
* Check pict id has used other product item. |
| 666 |
* |
| 667 |
* @param integer $post_id item post id. |
| 668 |
* @param array $item_pict_id array item pict id. |
| 669 |
* |
| 670 |
* @return boolean |
| 671 |
*/ |
| 672 |
function wel_check_item_pict_id_other_product( $post_id, $item_pict_id ) { |
| 673 |
global $wpdb; |
| 674 |
|
| 675 |
$table = $wpdb->prefix . 'usces_item'; |
| 676 |
$item_pict_id = (int) $item_pict_id; |
| 677 |
$val_case_1 = $item_pict_id; |
| 678 |
$val_case_2 = $wpdb->esc_like( $item_pict_id ) . ';%'; |
| 679 |
$val_case_3 = '%;' . $wpdb->esc_like( $item_pict_id ) . ';%'; |
| 680 |
$val_case_4 = '%;' . $wpdb->esc_like( $item_pict_id ); |
| 681 |
$result = $wpdb->get_var( |
| 682 |
$wpdb->prepare( |
| 683 |
"SELECT `post_id` FROM {$table} WHERE post_id <> %d AND ( `itemPicts` = %s OR `itemPicts` LIKE %s OR `itemPicts` LIKE %s OR `itemPicts` LIKE %s )", |
| 684 |
(int) $post_id, |
| 685 |
$val_case_1, |
| 686 |
$val_case_2, |
| 687 |
$val_case_3, |
| 688 |
$val_case_4 |
| 689 |
) |
| 690 |
); |
| 691 |
return (int) $result; |
| 692 |
} |
| 693 |
|
| 694 |
/** |
| 695 |
* Handle get all list item post used by pict id. |
| 696 |
* |
| 697 |
* @param integer $item_pict_id pict id. |
| 698 |
* |
| 699 |
* @return array |
| 700 |
*/ |
| 701 |
function wel_get_all_item_pict_id_other_product( $item_pict_id ) { |
| 702 |
global $wpdb; |
| 703 |
|
| 704 |
$table = $wpdb->prefix . 'usces_item'; |
| 705 |
$item_pict_id = (int) $item_pict_id; |
| 706 |
$val_case_1 = $item_pict_id; |
| 707 |
$val_case_2 = $wpdb->esc_like( $item_pict_id ) . ';%'; |
| 708 |
$val_case_3 = '%;' . $wpdb->esc_like( $item_pict_id ) . ';%'; |
| 709 |
$val_case_4 = '%;' . $wpdb->esc_like( $item_pict_id ); |
| 710 |
$post_ids = $wpdb->get_col( |
| 711 |
$wpdb->prepare( |
| 712 |
"SELECT `post_id` FROM {$table} WHERE `itemPicts` = %s OR `itemPicts` LIKE %s OR `itemPicts` LIKE %s OR `itemPicts` LIKE %s", |
| 713 |
$val_case_1, |
| 714 |
$val_case_2, |
| 715 |
$val_case_3, |
| 716 |
$val_case_4 |
| 717 |
) |
| 718 |
); |
| 719 |
return $post_ids; |
| 720 |
} |
| 721 |
|
| 722 |
/** |
| 723 |
* Ajax handler for updating image attributes. |
| 724 |
* |
| 725 |
* @param array $arr_res array response. |
| 726 |
*/ |
| 727 |
function wel_save_pict_info( &$arr_res ) { |
| 728 |
$field = filter_input( INPUT_POST, 'field' ); |
| 729 |
$pict_id = (int) filter_input( INPUT_POST, 'pict_id' ); |
| 730 |
$value = filter_input( INPUT_POST, 'value' ); |
| 731 |
switch ( $field ) { |
| 732 |
case 'alt': |
| 733 |
$alt = wp_unslash( $value ); |
| 734 |
if ( get_post_meta( $pict_id, '_wp_attachment_image_alt', true ) !== $alt ) { |
| 735 |
$alt = wp_strip_all_tags( $alt, true ); |
| 736 |
update_post_meta( $pict_id, '_wp_attachment_image_alt', wp_slash( $alt ) ); |
| 737 |
} |
| 738 |
$arr_res['status'] = true; |
| 739 |
break; |
| 740 |
case 'caption': |
| 741 |
$post = get_post( $pict_id, ARRAY_A ); |
| 742 |
if ( $post ) { |
| 743 |
$post['post_excerpt'] = $value; |
| 744 |
wp_update_post( $post ); |
| 745 |
} |
| 746 |
$arr_res['status'] = true; |
| 747 |
break; |
| 748 |
} |
| 749 |
} |
| 750 |
|