_copy)
if ( isset( $_GET['action'], $_GET['id'] ) && 'copy' === $_GET['action'] ) {
$id = intval( sanitize_text_field( wp_unslash( $_GET['id'] ) ) );
$nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';
if ( $nonce && wp_verify_nonce( $nonce, 'adventurebook_copy_item_' . $id ) ) {
$item = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}adventurebook_item_definitions WHERE id=%d",
$id
)
); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
if ( $item ) {
// insert new item with same data, but code appended with "_copy"
$new_code = $item->code . '_copy';
$wpdb->insert(
"{$wpdb->prefix}adventurebook_item_definitions", // phpcs:ignore WordPress.DB.DirectDatabaseQuery
[
'code' => $new_code,
'name' => $item->name,
'description' => $item->description,
'stats' => $item->stats,
'image_url' => $item->image_url,
]
);
$new_id = $wpdb->insert_id;
$message = __( 'Item copied.', 'adventurebook' );
// redirect will not work because headers were already sent, so we set $is_list_view to true and show message
$is_list_view = true;
}
}
}
// Single delete (bleibt per GET hier)
if ( isset( $_GET['action'], $_GET['id'] ) && 'delete' === $_GET['action'] ) {
$id = intval( sanitize_text_field( wp_unslash( $_GET['id'] ) ) );
$nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';
if ( $nonce && wp_verify_nonce( $nonce, 'adventurebook_delete_item_' . $id ) ) {
$wpdb->delete(
"{$wpdb->prefix}adventurebook_item_definitions",
[ 'id' => $id ]
); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
$message = __( 'Item deleted.', 'adventurebook' );
}
}
echo '';
if ( $is_list_view ) {
// ----------------- LIST VIEW -----------------
$slot_filter = isset( $_GET['item_slot_filter'] ) ? '&item_slot_filter=' . urlencode( sanitize_text_field( wp_unslash( $_GET['item_slot_filter'] ) ) ) : '';
echo 'Adventurebook Items ' . esc_html__( 'New Item', 'adventurebook' ) . '
';
if ( $message ) {
echo '' . esc_html( $message ) . '
';
}
$list_table = new Adventurebook_Item_List_Table();
$list_table->prepare_items();
if ( $list_table->get_message() ) {
echo '' . esc_html( $list_table->get_message() ) . '
';
}
// SLOT FILTER
echo '';
echo '';
} else {
// ----------------- NEW / EDIT VIEW -----------------
echo '' . ( ( isset( $_GET['action'] ) && 'edit' === $_GET['action'] ) ? esc_html__( 'Edit Item', 'adventurebook' ) : esc_html__( 'New Item', 'adventurebook' ) ) . '
';
if ( $message ) {
echo '' . esc_html( $message ) . '
';
}
$item = null;
if ( isset( $_GET['action'], $_GET['id'] ) && 'edit' === $_GET['action'] ) {
$id = intval( $_GET['id'] );
$item = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}adventurebook_item_definitions WHERE id=%d",
$id
)
); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
}
$current_filter = isset( $_GET['item_slot_filter'] ) ? sanitize_text_field( wp_unslash( $_GET['item_slot_filter'] ) ) : '';
?>
';
}
/**
* =====================================================
* Admin-Post-Handler für Import
* =====================================================
*/
add_action( 'admin_post_adventurebook_import_items', 'adventurebook_handle_import_items' );
function adventurebook_handle_import_items() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have permission to do that.', 'adventurebook' ) );
}
// Nonce prüfen
$nonce = isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '';
if ( ! $nonce || ! wp_verify_nonce( $nonce, 'adventurebook_import_items' ) ) {
wp_die( __( 'Invalid nonce.', 'adventurebook' ) );
}
global $wpdb;
$json_data = isset( $_POST['json_data'] ) ? wp_unslash( $_POST['json_data'] ) : '';
$items = json_decode( $json_data, true );
if ( is_array( $items ) ) {
foreach ( $items as $item ) {
if ( isset( $item['code'], $item['name'] ) ) {
// check if an item with this code does not already exist
$item_definition = Item_Definition::get_item_definition_by_code( $item['code'] );
if ( $item_definition ) {
continue; // skip existing item
}
// take the last part of the image_url (filename) and try to get a match from the media library
$image_url = '';
if ( isset( $item['image_url'] ) && '' !== $item['image_url'] ) {
$image_filename = basename( $item['image_url'] );
$args = [
'post_type' => 'attachment',
'posts_per_page' => 1,
'post_status' => 'inherit',
'meta_query' => [
[
'key' => '_wp_attached_file',
'value' => $image_filename,
'compare' => 'LIKE',
],
],
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
$query->the_post();
$image_url = wp_get_attachment_url( get_the_ID() );
wp_reset_postdata();
} else {
$image_url = $item['image_url'];
}
}
$wpdb->insert(
"{$wpdb->prefix}adventurebook_item_definitions", // phpcs:ignore WordPress.DB.DirectDatabaseQuery
[
'code' => sanitize_text_field( $item['code'] ),
'name' => sanitize_text_field( $item['name'] ),
'description' => isset( $item['description'] ) ? sanitize_textarea_field( $item['description'] ) : '',
'stats' => isset( $item['stats'] ) ? wp_json_encode( $item['stats'] ) : '',
'image_url' => isset( $item['image_url'] ) ? esc_url_raw( $item['image_url'] ) : '',
]
);
}
}
}
// Redirect back to items page
wp_safe_redirect( admin_url( 'admin.php?page=adventurebook_items&message=' . urlencode( __( 'Items imported.', 'adventurebook' ) ) ) );
exit;
}