Files
wordpress-theme-aksrt/inc/classes/admin/class-aksrt-admin-shop-controller-product-admin-columns.php
T

188 lines
7.8 KiB
PHP

<?php
/**
* AKSRT admin shop product admin list operations.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
trait AKSRT_Admin_Shop_Controller_Product_Admin_Columns {
/**
* Register custom columns for the aksrt_product admin list table.
*/
public function init_product_admin_columns(): void {
add_filter('manage_edit-aksrt_product_columns', [$this, 'product_columns']);
add_action('manage_aksrt_product_posts_custom_column', [$this, 'render_product_column'], 10, 2);
add_filter('manage_edit-aksrt_product_sortable_columns', [$this, 'sortable_product_columns']);
add_action('quick_edit_custom_box', [$this, 'render_product_quick_edit'], 10, 2);
add_action('bulk_edit_custom_box', [$this, 'render_product_quick_edit'], 10, 2);
add_action('save_post_aksrt_product', [$this, 'save_product_quick_edit'], 10, 2);
add_action('admin_footer-edit.php', [$this, 'product_quick_edit_script']);
}
public function product_columns(array $columns): array {
$new = [];
foreach ($columns as $key => $label) {
$new[$key] = $label;
if ($key === 'title') {
$new['product_price'] = __('价格', 'aksrt');
$new['product_stock'] = __('库存', 'aksrt');
$new['product_sales'] = __('销量', 'aksrt');
$new['product_stats'] = __('综合数据', 'aksrt');
$new['product_author_name'] = __('商家', 'aksrt');
$new['product_discount'] = __('优惠活动', 'aksrt');
}
}
return $new;
}
public function render_product_column(string $column, int $post_id): void {
switch ($column) {
case 'product_price':
$price = get_post_meta($post_id, '_aksrt_price', true);
echo esc_html(number_format_i18n((float) $price, 2));
break;
case 'product_stock':
$stock = get_post_meta($post_id, '_aksrt_stock', true);
$stock_type = get_post_meta($post_id, '_aksrt_stock_type', true);
if ($stock_type === 'opts') {
echo esc_html__('按选项', 'aksrt');
} elseif ((int) $stock === -1) {
echo esc_html__('无限', 'aksrt');
} else {
echo esc_html((string) (int) $stock);
}
break;
case 'product_sales':
echo esc_html((string) (int) get_post_meta($post_id, '_aksrt_sales', true));
break;
case 'product_stats':
$views = (int) get_post_meta($post_id, '_aksrt_views', true);
$fav = (int) get_post_meta($post_id, '_aksrt_favorite_count', true);
printf(
esc_html__('阅读 %1$d / 收藏 %2$d', 'aksrt'),
$views,
$fav
);
break;
case 'product_author_name':
$post = get_post($post_id);
if ($post) {
$author = get_userdata($post->post_author);
echo esc_html($author ? $author->display_name : '—');
}
break;
case 'product_discount':
$terms = wp_get_object_terms($post_id, 'shop_discount', ['fields' => 'all']);
if (!is_wp_error($terms) && !empty($terms)) {
$term_ids = wp_list_pluck($terms, 'term_id');
$names = wp_list_pluck($terms, 'name');
echo '<span class="aksrt-col-data" data-discount-id="' . esc_attr((string) ($term_ids[0] ?? 0)) . '"></span>';
echo esc_html(implode(', ', $names));
} else {
echo '<span class="aksrt-col-data" data-discount-id="0"></span>—';
}
break;
}
}
public function sortable_product_columns(array $columns): array {
$columns['product_sales'] = 'product_sales';
return $columns;
}
public function render_product_quick_edit(string $column_name, string $post_type): void {
if ($column_name !== 'product_discount' || $post_type !== 'aksrt_product') {
return;
}
wp_nonce_field('aksrt_product_quick_edit', 'aksrt_product_qe_nonce');
$discounts = get_terms([
'taxonomy' => 'shop_discount',
'hide_empty' => false,
]);
?>
<fieldset class="inline-edit-col-right" id="aksrt-product-quick-edit">
<div class="inline-edit-col">
<h4><?php esc_html_e('商品优惠活动', 'aksrt'); ?></h4>
<input type="hidden" name="aksrt_product_qe_flag" value="1">
<label class="alignleft">
<span class="title"><?php esc_html_e('优惠活动', 'aksrt'); ?></span>
<select name="aksrt_qe_discount" class="aksrt-qe-discount">
<option value="-1"><?php esc_html_e('— 不更改 —', 'aksrt'); ?></option>
<option value="0"><?php esc_html_e('无优惠', 'aksrt'); ?></option>
<?php if (!is_wp_error($discounts)) : ?>
<?php foreach ($discounts as $term) : ?>
<option value="<?php echo esc_attr($term->term_id); ?>"><?php echo esc_html($term->name); ?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</label>
</div>
</fieldset>
<?php
}
public function save_product_quick_edit(int $post_id, \WP_Post $post): void {
if (!isset($_POST['aksrt_product_qe_nonce']) || !wp_verify_nonce($_POST['aksrt_product_qe_nonce'], 'aksrt_product_quick_edit')) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
if (!isset($_POST['aksrt_product_qe_flag'])) {
return;
}
$discount_id = isset($_POST['aksrt_qe_discount']) ? sanitize_text_field(wp_unslash($_POST['aksrt_qe_discount'])) : '-1';
if ($discount_id === '-1') {
return;
}
if ($discount_id === '0') {
wp_set_object_terms($post_id, [], 'shop_discount');
} else {
$term_id = (int) $discount_id;
if ($term_id > 0 && term_exists($term_id, 'shop_discount')) {
wp_set_object_terms($post_id, [$term_id], 'shop_discount');
}
}
}
public function product_quick_edit_script(): void {
$screen = get_current_screen();
if (!$screen || $screen->post_type !== 'aksrt_product') {
return;
}
?>
<script>
(function(){
var wpInlineEdit = window.inlineEditPost ? window.inlineEditPost.edit : null;
if (!wpInlineEdit) return;
window.inlineEditPost.edit = function(id) {
wpInlineEdit.apply(this, arguments);
var postId = 0;
if (typeof id === 'object') {
postId = parseInt(this.getId(id));
}
if (!postId) return;
var row = document.getElementById('post-' + postId);
if (!row) return;
var dataEl = row.querySelector('.column-product_discount .aksrt-col-data');
if (!dataEl) return;
var termId = dataEl.getAttribute('data-discount-id') || '0';
var select = document.querySelector('.aksrt-qe-discount');
if (select) {
select.value = termId;
}
};
})();
</script>
<?php
}
}