__( 'Products', 'affiliate-product-boxes' ),
'singular_name' => __( 'Product', 'affiliate-product-boxes' ),
'menu_name' => __( 'Affiliate Products', 'affiliate-product-boxes' ),
'name_admin_bar' => __( 'Product', 'affiliate-product-boxes' ),
'add_new' => __( 'Add New', 'affiliate-product-boxes' ),
'add_new_item' => __( 'Add New Product', 'affiliate-product-boxes' ),
'edit_item' => __( 'Edit Product', 'affiliate-product-boxes' ),
'new_item' => __( 'New Product', 'affiliate-product-boxes' ),
'view_item' => __( 'View Product', 'affiliate-product-boxes' ),
'all_items' => __( 'All Products', 'affiliate-product-boxes' ),
'search_items' => __( 'Search Products', 'affiliate-product-boxes' ),
'not_found' => __( 'No products found.', 'affiliate-product-boxes' ),
'not_found_in_trash' => __( 'No products found in Trash.', 'affiliate-product-boxes' )
];
$args = [
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'has_archive' => false,
'supports' => [ 'thumbnail' ],
'menu_icon' => 'dashicons-cart',
'rewrite' => false,
'capability_type' => 'post'
];
register_post_type( 'apbox_product', $args );
}
// Add new form fields when clicking 'Add New'
public static function add_meta_boxes() {
add_meta_box(
'apbox_product_details',
__( 'Product Details', 'affiliate-product-boxes' ),
[ self::class, 'render_meta_box' ],
'apbox_product',
'normal',
'default'
);
remove_meta_box( 'postimagediv', 'apbox_product', 'side' );
}
// Display new form field data when viewing product
public static function render_meta_box( $post ) {
wp_nonce_field( 'apbox_save_product_meta', 'apbox_product_meta_nonce' );
$title = $post->post_title;
$description = $post->post_content;
$price = get_post_meta( $post->ID, '_apbox_price', true );
$affiliate_link = get_post_meta( $post->ID, '_apbox_affiliate_link', true );
$rating = get_post_meta( $post->ID, '_apbox_rating', true );
// Get currency from settings
$currency = get_option( 'apbox_currency', 'USD' );
$currencies = apbox_Settings::get_currencies();
$currency_label = isset( $currencies[ $currency ] ) ? $currencies[ $currency ] : $currency;
// Extract symbol for the label
if ( preg_match( '/\((.*?)\)/', $currency_label, $matches ) ) {
$currency_display = $matches[1]; // e.g. €
} else {
$currency_display = $currency;
}
?>
ID );
$image_url = $image_id ? wp_get_attachment_url( $image_id ) : '';
echo '
';
if ( $image_url ) {
echo '
 . ')
';
}
echo '
';
echo '';
echo '
';
?>
post_type !== 'apbox_product' ) return;
wp_enqueue_media(); // Load WordPress media uploader
wp_enqueue_script(
'apbox-media-upload',
plugin_dir_url( __DIR__ ) . 'assets/js/apbox-media-upload.js',
[ 'jquery' ],
'1.0',
true
);
}
// Save product data
public static function save_product_meta( $post_id ) {
// Security & sanity checks FIRST
if ( ! isset( $_POST['apbox_product_meta_nonce'] ) ||
! wp_verify_nonce(
sanitize_text_field( wp_unslash( $_POST['apbox_product_meta_nonce'] ) ),
'apbox_save_product_meta'
)
) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Temporarily unhook to avoid recursion
remove_action( 'save_post_apbox_product', [ self::class, 'save_product_meta' ] );
// Save custom title
if ( isset( $_POST['apbox_title'] ) ) {
wp_update_post( [
'ID' => $post_id,
'post_title' => sanitize_text_field( wp_unslash( $_POST['apbox_title'] ) ),
] );
}
// Save custom description
if ( isset( $_POST['apbox_description'] ) ) {
wp_update_post( [
'ID' => $post_id,
'post_content' => wp_kses_post( wp_unslash( $_POST['apbox_description'] ) ),
] );
}
// Save price
if ( isset( $_POST['apbox_price'] ) ) {
update_post_meta(
$post_id,
'_apbox_price',
sanitize_text_field( wp_unslash( $_POST['apbox_price'] ) )
);
}
// Save affiliate link
if ( isset( $_POST['apbox_affiliate_link'] ) ) {
update_post_meta(
$post_id,
'_apbox_affiliate_link',
esc_url_raw( wp_unslash( $_POST['apbox_affiliate_link'] ) )
);
}
// Save rating
if ( isset( $_POST['apbox_rating'] ) ) {
$rating = (int) $_POST['apbox_rating'];
if ( $rating >= 1 && $rating <= 5 ) {
update_post_meta( $post_id, '_apbox_rating', $rating );
}
}
// Save thumbnail
if ( isset( $_POST['apbox_product_thumbnail'] ) ) {
set_post_thumbnail( $post_id, (int) $_POST['apbox_product_thumbnail'] );
}
// Re-hook save handler
add_action( 'save_post_apbox_product', [ self::class, 'save_product_meta' ] );
}
// Show product ID in submit box
public static function show_product_id_in_submitbox() {
global $post;
if ( $post && $post->post_type === 'apbox_product' ) {
echo '';
echo '
Shortcode: ';
echo '
[apbox_product_box id="' . (int) $post->ID . '"]';
echo '
Copy & paste this shortcode into any post or page to display the product box.
';
echo '
';
}
}
public static function apbox_add_id_column( $columns ) {
$columns['apbox_id'] = esc_html__( 'ID', 'affiliate-product-boxes' );
return $columns;
}
public static function apbox_render_id_column( $column, $post_id ) {
if ( 'apbox_id' !== $column ) {
return;
}
echo esc_html( (string) absint( $post_id ) );
}
public static function apbox_register_sortable_columns( $columns ) {
$columns['apbox_id'] = 'ID';
return $columns;
}
}