Adventurebook Dashboard'; }, 'dashicons-book', 6 ); } ); add_action( 'admin_enqueue_scripts', 'adventurebook_enqueue_media_scripts' ); function adventurebook_enqueue_media_scripts( $hook_suffix ) { // check if this the adventurebook items or enemies page if ( $hook_suffix !== 'adventurebook_page_adventurebook_items' && $hook_suffix !== 'adventurebook_page_adventurebook-enemies' ) { return; } // Media Uploader Scripts laden wp_enqueue_media(); } /** * Register the 'adventurebook' custom post type * An Adventure Book is a custom post type that represents a book that can be read online. * It has pages that can be navigated through. */ function adventurebook_register_book_post_type(): void { $labels = array( 'name' => __( 'Adventurebooks', 'adventurebook' ), 'singular_name' => __( 'Adventurebook', 'adventurebook' ), 'menu_name' => __( 'Adventurebooks', 'adventurebook' ), 'name_admin_bar' => __( 'Adventurebooks', 'adventurebook' ), 'add_new' => __( 'Add New Book', 'adventurebook' ), 'add_new_item' => __( 'Add New Adventurebook', 'adventurebook' ), 'new_item' => __( 'New Adventurebook', 'adventurebook' ), 'edit_item' => __( 'Edit Adventurebook', 'adventurebook' ), 'view_item' => __( 'View Adventurebook', 'adventurebook' ), 'all_items' => __( 'Adventurebooks', 'adventurebook' ), 'search_items' => __( 'Search Adventurebooks', 'adventurebook' ), 'not_found' => __( 'No Adventurebooks found.', 'adventurebook' ), 'not_found_in_trash' => __( 'No Adventurebooks found in Trash.', 'adventurebook' ) ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => 'adventurebook_main', 'query_var' => true, 'rewrite' => array( 'slug' => __( 'adventurebook', 'adventurebook' ) ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => true, 'menu_position' => 5, 'menu_icon' => 'dashicons-book-alt', 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ), ); register_post_type( 'adventurebook', $args ); } // Hinzufügen der benutzerdefinierten Spalte zur Übersicht function adventurebook_add_custom_columns( $columns ) { $columns['page_count'] = __( 'Page Count', 'adventurebook' ); return $columns; } // Füllen der benutzerdefinierten Spalte mit Daten function adventurebook_custom_column_content( $column, $post_id ) { if ( $column === 'page_count' ) { // Abrufen der Anzahl der Seiten, die zu diesem Adventurebook gehören $page_count = count( get_posts( array( 'post_type' => 'adventurebook_page', 'post_parent' => $post_id, 'numberposts' => - 1, 'post_status' => 'publish' ) ) ); echo esc_html( $page_count ); } } // Füge eine benutzerdefinierte Spalte zur Übersicht hinzu function adventurebook_add_start_page_column( $columns ) { $columns['start_page'] = __( 'Start Page', 'adventurebook' ); return $columns; } // Fülle die benutzerdefinierte Spalte mit Daten function adventurebook_start_page_column_content( $column, $post_id ) { if ( $column === 'start_page' ) { // Abrufen der ID der Startseite für das Adventurebook $start_page_id = get_post_meta( $post_id, '_adventurebook_start_page', true ); if ( $start_page_id ) { // Abrufen des Titels der Startseite $start_page = get_post( $start_page_id ); if ( $start_page ) { echo esc_html( $start_page->post_title ) . ' (' . esc_html( $start_page_id ) . ')'; } else { esc_html_e( 'No Start Page', 'adventurebook' ); } } else { esc_html_e( 'No Start Page', 'adventurebook' ); } } } function adventurebook_add_new_page_link_meta_box() { add_meta_box( 'adventurebook_new_page_link', // Unique ID __( 'Add New Book Page', 'adventurebook' ), // Box title 'adventurebook_new_page_link_html', // Content callback 'adventurebook', // Post type 'side', // Context 'high' // Priority ); } function adventurebook_new_page_link_html( $post ) { $link = add_query_arg( array( 'post_type' => 'adventurebook_page', 'parent_adventurebook' => $post->ID, '_adventurebook_nonce' => wp_create_nonce( 'adventurebook_set_parent_action' ) ), admin_url( 'post-new.php' ) ); echo ''; esc_html_e( 'Add New Book Page', 'adventurebook' ); echo ''; } // Füge eine Metabox hinzu, um die Startseite auszuwählen function adventurebook_add_start_page_meta_box() { add_meta_box( 'adventurebook_start_page', // ID der Metabox esc_html( __( 'Select Start Page', 'adventurebook' ) ), // Titel der Metabox 'adventurebook_start_page_meta_box_html', // Callback zur Ausgabe des Inhalts 'adventurebook', // Post-Typ 'side', // Kontext 'default' // Priorität ); } // HTML für das Dropdown-Menü in der Metabox function adventurebook_start_page_meta_box_html( $post ) { wp_nonce_field( 'adventurebook_save_startpage', 'adventurebook_nonce_startpage' ); // retrieve the currently selected start page $selected_page_id = get_post_meta( $post->ID, '_adventurebook_start_page', true ); // get all pages, sorted by title, and with parent as the current book $pages = get_posts( array( 'post_type' => 'adventurebook_page', 'post_parent' => $post->ID, 'numberposts' => - 1, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC', ) ); if ( empty( $pages ) ) { echo esc_html( __( 'No pages found. Please create pages first.', 'adventurebook' ) ); return; } echo ''; } function adventurebook_save_start_page_meta_box( $post_id ) { // Verhindere Autosave-Effekte if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // Sicherstellen, dass es sich um den richtigen Post-Typ handelt if ( get_post_type( $post_id ) != 'adventurebook' ) { return; } // Check Nonce if ( ! isset( $_POST['adventurebook_nonce_startpage'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['adventurebook_nonce_startpage'] ) ), 'adventurebook_save_startpage' ) ) { return; } // check for editing rights if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } remove_action( 'save_post', 'adventurebook_save_start_page_meta_box' ); // Überprüfen, ob eine Startseite ausgewählt wurde if ( isset( $_POST['adventurebook_start_page'] ) ) { $start_page_id = intval( $_POST['adventurebook_start_page'] ); update_post_meta( $post_id, '_adventurebook_start_page', $start_page_id ); } add_action( 'save_post', 'adventurebook_save_start_page_meta_box' ); } // ----------------- PAGES ----------------- function adventurebook_set_parent_on_new_page( $post_id ) { // prevent autosave effects if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // check if the post type is 'adventurebook_page' if ( get_post_type( $post_id ) != 'adventurebook_page' ) { return; } if ( ! isset( $_GET['_adventurebook_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_adventurebook_nonce'] ) ), 'adventurebook_set_parent_action' ) ) { return; } // check for editing rights if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // check if the parent_adventurebook parameter is set in the URL if ( isset( $_GET['parent_adventurebook'] ) && isset( $_GET['post_type'] ) && $_GET['post_type'] === 'adventurebook_page' ) { $parent_id = intval( $_GET['parent_adventurebook'] ); // temporarily remove the hook to prevent infinite loops remove_action( 'save_post', 'adventurebook_set_parent_on_new_page', 10, 3 ); // set the parent value wp_update_post( array( 'ID' => $post_id, 'post_parent' => $parent_id, ) ); // add the hook back add_action( 'save_post', 'adventurebook_set_parent_on_new_page', 10, 3 ); } } function adventurebook_add_page_script_meta_box() { add_meta_box( 'adventurebook_page_script', // Unique ID __( 'Select Script', 'adventurebook' ), // Box title 'adventurebook_page_script_html', // Content callback 'adventurebook_page', // Post type 'side', // Context 'default' // Priority ); } function adventurebook_page_script_html( $post ) { // retrieve the currently selected script $script_code = get_post_meta( $post->ID, '_adventurebook_page_script', true ); // Nonce for security wp_nonce_field( 'adventurebook_save_page_script', 'adventurebook_nonce_page_script' ); echo ''; echo ''; } function adventurebook_save_page_script_meta_box_data( $post_id ) { // Check if the user has permission to edit the post if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // Verify the nonce if ( ! isset( $_POST['adventurebook_nonce_page_script'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['adventurebook_nonce_page_script'] ) ), 'adventurebook_save_page_script' ) ) { return; } // check for editing rights if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // Check if the script dropdown is set if ( isset( $_POST['adventurebook_page_script'] ) ) { $script_code = sanitize_text_field( wp_unslash( $_POST['adventurebook_page_script'] ) ); update_post_meta( $post_id, '_adventurebook_page_script', $script_code ); } } add_action( 'save_post', 'adventurebook_save_page_script_meta_box_data' ); add_action( 'add_meta_boxes', 'adventurebook_add_page_script_meta_box' ); /** add a meta box to select the type of page * available types: * default (normal page) * shop (page with shop functionality) - will display an additional meta box with shop settings */ function adventurebook_add_page_type_meta_box() { // only add meta box if the parent adventurebook is of type "game" $post = get_post(); $adventurebook_type = adventurebook_get_type( "adventurebook_page", $post ); if ( $adventurebook_type !== 'game' ) { return; } add_meta_box( 'adventurebook_page_type', // Unique ID __( 'Select Page Type', 'adventurebook' ), // Box title 'adventurebook_page_type_html', // Content callback 'adventurebook_page', // Post type 'side', // Context 'default' // Priority ); } function adventurebook_page_type_html( $post ) { // retrieve the currently selected page type $page_type = get_post_meta( $post->ID, '_adventurebook_page_type', true ); // Nonce for security wp_nonce_field( 'adventurebook_save_page_type', 'adventurebook_nonce_page_type' ); echo ''; echo ''; } function adventurebook_save_page_type_meta_box_data( $post_id ) { // Check if the user has permission to edit the post if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // Verify the nonce if ( ! isset( $_POST['adventurebook_nonce_page_type'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['adventurebook_nonce_page_type'] ) ), 'adventurebook_save_page_type' ) ) { return; } // check for editing rights if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // Check if the page type dropdown is set if ( isset( $_POST['adventurebook_page_type'] ) ) { $page_type = sanitize_text_field( wp_unslash( $_POST['adventurebook_page_type'] ) ); update_post_meta( $post_id, '_adventurebook_page_type', $page_type ); } } add_action( 'save_post', 'adventurebook_save_page_type_meta_box_data' ); add_action( 'add_meta_boxes', 'adventurebook_add_shop_meta_box' ); function adventurebook_add_shop_meta_box( $post_type, $post = null ) { // adventurebook must be game $adventurebook_type = adventurebook_get_type( "adventurebook_page", $post ); if ( $adventurebook_type === 'game' ) { add_meta_box( 'adventurebook_shop_options', __( 'Shop Options', 'adventurebook' ), 'adventurebook_shop_options_meta_box', 'adventurebook_page', 'normal', 'default' ); } } function adventurebook_shop_options_meta_box( $post ) { // Sicherheits-Nonce wp_nonce_field( 'adventurebook_shop_options_save', 'adventurebook_shop_options_nonce' ); // Gespeicherter Typ $page_type = get_post_meta( $post->ID, '_adventurebook_page_type', true ); // Nur anzeigen, wenn Typ bereits "Shop" ist if ( $page_type !== 'shop' ) { echo '

' . esc_html( __( 'Shop Options will be displayed once the Page Type is set to "Shop" and the page is saved.', 'adventurebook' ) ) . '

'; return; } // Gespeicherter Wert für List of items $items_list = get_post_meta( $post->ID, '_adventurebook_shop_items', true ); ?>





post_parent; // Abrufen aller Adventurebooks $books = get_posts( array( 'post_type' => 'adventurebook', 'numberposts' => - 1, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC', ) ); echo ''; } /** * Filtert die Post-Daten vor dem Speichern und setzt den Parent, * falls das Dropdown-Feld übergeben wurde. */ add_filter( 'wp_insert_post_data', 'adventurebook_handle_parent_dropdown', 10, 2 ); function adventurebook_handle_parent_dropdown( $data, $postarr ) { // Nur für unseren CPT if ( $data['post_type'] !== 'adventurebook_page' ) { return $data; } // Wenn kein POST-Kontext existiert (z. B. WP-CLI), abbrechen if ( empty( $_POST ) ) { return $data; } // Nonce prüfen if ( ! isset( $_POST['adventurebook_nonce_parent'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['adventurebook_nonce_parent'] ) ), 'adventurebook_save_parent' ) ) { return $data; } // Rechte prüfen if ( ! current_user_can( 'edit_post', $postarr['ID'] ) ) { return $data; } // Wenn das Dropdown-Feld vorhanden ist, Parent übernehmen if ( isset( $_POST['adventurebook_parent_dropdown'] ) ) { $data['post_parent'] = intval( $_POST['adventurebook_parent_dropdown'] ); } return $data; } // Callback-Funktion, um das Metabox-HTML anzuzeigen function adventurebook_type_meta_box_callback( $post ) { // Hole den gespeicherten Wert aus der Post-Meta $adventurebook_type = get_post_meta( $post->ID, '_adventurebook_type', true ); // Sicherheit: Nonce-Feld für die Verifizierung beim Speichern wp_nonce_field( 'adventurebook_save_type', 'adventurebook_nonce_type' ); ?> ID, '_additional_image', true ); $image_url = wp_get_attachment_url( $image_id ); ?>

admin_url( 'admin-ajax.php' ), // WordPress AJAX-URL 'nonce' => wp_create_nonce( 'adventurebook_ajax_nonce' ) // Nonce zur Sicherheit ) ); $version = filemtime( $plugin_path . '../../js/adventurebook_admin_decision_options.js' ); wp_enqueue_script( 'adventurebook-decision-options', $plugin_url . '../../js/adventurebook_admin_decision_options.js', array( 'jquery' ), $version, true ); // PHP-Daten vorbereiten global $post; // get the parent page id of this post $book_id = wp_get_post_parent_id( $post ); // get the game type of the parent book $book_type = get_post_meta( $book_id, '_adventurebook_type', true ); // get all pages of the same book $pages = get_posts( array( 'post_type' => 'adventurebook_page', 'post_parent' => $book_id, 'post_status' => 'publish', 'numberposts' => - 1, 'orderby' => 'title', 'order' => 'ASC', ) ); $pages_data = array_map( function ( $page ) { return [ 'ID' => $page->ID, 'title' => $page->post_title, ]; }, $pages ); // Übersetzungen und Seiten als JS-Variablen verfügbar machen wp_localize_script( 'adventurebook-decision-options', 'adventurebook_pages', $pages_data ); // JS also needs the available restriction types global $adventurebook_restriction_types; wp_localize_script( 'adventurebook-decision-options', 'adventurebook_restriction_types', $adventurebook_restriction_types ); wp_localize_script( 'adventurebook-decision-options', 'adventurebook_book_type', $book_type ); wp_localize_script( 'adventurebook-decision-options', 'adventurebook_i18n', [ 'label' => __( 'Label', 'adventurebook' ), 'target' => __( 'Target Page', 'adventurebook' ), 'select_page' => __( 'Select a page', 'adventurebook' ), 'generate_new_page' => __( 'Generate new page', 'adventurebook' ), 'remove' => __( 'Remove', 'adventurebook' ), 'new_page_title' => __( 'New Page Title', 'adventurebook' ), 'new_page_backlink' => __( 'Backlink Label', 'adventurebook' ), 'restriction_type' => __( 'Restriction Type', 'adventurebook' ), 'restriction_value' => __( 'Restriction Value', 'adventurebook' ), ] ); } // --- Dropdown for Game-type Adventurebook Combat Systems --- add_action( 'add_meta_boxes', function () { add_meta_box( 'adventurebook_combat_system', __( 'Combat System', 'adventurebook' ), 'render_adventurebook_combat_system_meta_box', 'adventurebook', 'side', 'default' ); } ); function render_adventurebook_combat_system_meta_box( $post ) { wp_nonce_field( 'adventurebook_combat_system_save', 'adventurebook_combat_system_nonce' ); // Aktueller Typ $adventurebook_type = get_post_meta( $post->ID, '_adventurebook_type', true ); // Nur anzeigen, wenn Typ == "Game" if ( $adventurebook_type !== 'game' ) { echo '

' . esc_html( __( 'No Combat System, Adventurebook-Type is not "Game".', 'adventurebook' ) ) . '

'; return; } // Gespeicherter Wert $selected_system = get_post_meta( $post->ID, '_adventurebook_combat_system', true ); $systems = get_all_combat_systems(); // Dropdown ?> getName(); } } } // if this is the premium version, add combat systems from premium/combat_systems/ if ( defined( 'ADVENTUREBOOK_PREMIUM' ) && ADVENTUREBOOK_PREMIUM ) { $premium_path = plugin_dir_path( __FILE__ ) . '../premium/combat_systems/'; foreach ( glob( $premium_path . '*.php' ) as $file ) { require_once $file; // the class name is the file name without extension $classname = basename( $file, '.php' ); $classname = "Adventurebook\\" . $classname; if ( class_exists( $classname ) ) { $obj = new $classname(); if ( method_exists( $obj, 'getName' ) ) { $systems[ $classname ] = $obj->getName(); } } } } return $systems; } /** * returns an instance of the combat system for the given adventurebook * or null if no combat system is set or the class does not exist * * @param $adventurebook_id * * @return Adventurebook\Combat_System|null */ function get_combat_system_for_adventurebook( $adventurebook_id ): ?Adventurebook\Combat_System { $adventurebook_combat_system_class = "Adventurebook\\" . get_post_meta( $adventurebook_id, '_adventurebook_combat_system', true ); if ( $adventurebook_combat_system_class && class_exists( $adventurebook_combat_system_class ) ) { return new $adventurebook_combat_system_class(); } return null; } function get_combat_system_for_character_id( $character_id ): ?Adventurebook\Combat_System { $character = Character::get_character_by_id( $character_id ); if ( $character ) { return get_combat_system_for_adventurebook( $character->adventurebook_id ); } return null; } #region --- CUSTOM POST TYPE "adventurebook_page" --- function adventurebook_register_page_post_type(): void { $labels = array( 'name' => __( 'Book Pages', 'adventurebook' ), 'singular_name' => __( 'Book Page', 'adventurebook' ), 'menu_name' => __( 'Book Pages', 'adventurebook' ), 'name_admin_bar' => __( 'Book Page', 'adventurebook' ), 'add_new' => __( 'Add New Page', 'adventurebook' ), 'add_new_item' => __( 'Add New Book Page', 'adventurebook' ), 'new_item' => __( 'New Book Page', 'adventurebook' ), 'edit_item' => __( 'Edit Book Page', 'adventurebook' ), 'view_item' => __( 'View Book Page', 'adventurebook' ), 'all_items' => __( 'Book Pages', 'adventurebook' ), 'search_items' => __( 'Search Book Pages', 'adventurebook' ), 'not_found' => __( 'No book pages found.', 'adventurebook' ), 'not_found_in_trash' => __( 'No book pages found in Trash.', 'adventurebook' ) ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => 'adventurebook_main', 'query_var' => true, 'rewrite' => array( 'slug' => __( 'book-page', 'adventurebook' ) ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => true, // Allows pages to be a "child" of a book 'menu_position' => 6, 'menu_icon' => 'dashicons-admin-page', 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ), 'show_in_rest' => true, ); register_post_type( 'adventurebook_page', $args ); } /** * Add parent filter dropdown in the admin list with nonce field */ add_action( 'restrict_manage_posts', 'adventurebook_page_parent_filter' ); function adventurebook_page_parent_filter() { global $typenow; if ( $typenow !== 'adventurebook_page' ) { return; } $parents = get_posts( array( 'post_type' => 'adventurebook', 'posts_per_page' => - 1, 'orderby' => 'title', 'order' => 'ASC' ) ); // Security-Check: check for nonce if ( isset( $_GET['adventurebook_filter_nonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['adventurebook_filter_nonce'] ) ), 'adventurebook_filter_action' ) ) { return; } $user_id = get_current_user_id(); $persisted_parent = get_user_meta( $user_id, 'adventurebook_last_parent', true ); $selected = isset( $_GET['adventurebook_parent'] ) ? sanitize_text_field( wp_unslash( $_GET['adventurebook_parent'] ) ) : $persisted_parent; echo ''; // Output nonce field for the filter wp_nonce_field( 'adventurebook_filter_action', 'adventurebook_filter_nonce' ); } /** * Apply filter to the list and save user meta only if nonce is valid */ add_action( 'pre_get_posts', 'adventurebook_page_filter_by_parent' ); function adventurebook_page_filter_by_parent( $query ) { global $pagenow; if ( $pagenow !== 'edit.php' ) { return; } if ( $query->get( 'post_type' ) !== 'adventurebook_page' ) { return; } $user_id = get_current_user_id(); if ( isset( $_GET['adventurebook_parent'] ) ) { // Verify nonce before persisting if ( ! isset( $_GET['adventurebook_filter_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['adventurebook_filter_nonce'] ) ), 'adventurebook_filter_action' ) ) { return; } $parent_id = intval( $_GET['adventurebook_parent'] ); if ( $parent_id > 0 ) { update_user_meta( $user_id, 'adventurebook_last_parent', $parent_id ); $query->set( 'post_parent', $parent_id ); } else { delete_user_meta( $user_id, 'adventurebook_last_parent' ); } } else { $last_parent = get_user_meta( $user_id, 'adventurebook_last_parent', true ); if ( $last_parent ) { $query->set( 'post_parent', intval( $last_parent ) ); } } } /** * Keep filter persistent on redirects */ add_filter( 'redirect_post_location', 'adventurebook_persist_parent_filter_redirect', 10, 2 ); function adventurebook_persist_parent_filter_redirect( $location, $post_id ) { if ( get_post_type( $post_id ) !== 'adventurebook_page' ) { return $location; } // Security-Check: check for nonce if ( isset( $_GET['adventurebook_filter_nonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['adventurebook_filter_nonce'] ) ), 'adventurebook_filter_action' ) ) { return $location; } $user_id = get_current_user_id(); $last_parent = get_user_meta( $user_id, 'adventurebook_last_parent', true ); if ( $last_parent ) { $location = add_query_arg( 'adventurebook_parent', intval( $last_parent ), $location ); } return $location; } /** * Hide the default Add New button */ add_action( 'admin_head', 'adventurebook_hide_default_add_new_button' ); function adventurebook_hide_default_add_new_button() { global $typenow; if ( $typenow !== 'adventurebook_page' ) { return; } echo ''; } /** * Add custom Add New Book Page button next to the post type title */ add_action( 'admin_notices', 'adventurebook_add_new_button_standard_position' ); function adventurebook_add_new_button_standard_position() { global $pagenow, $typenow; if ( $pagenow !== 'edit.php' || $typenow !== 'adventurebook_page' ) { return; } $user_id = get_current_user_id(); $last_parent = get_user_meta( $user_id, 'adventurebook_last_parent', true ); if ( empty( $last_parent ) ) { $first_parent = get_posts( array( 'post_type' => 'adventurebook', 'posts_per_page' => 1, 'orderby' => 'title', 'order' => 'ASC' ) ); if ( $first_parent ) { $last_parent = $first_parent[0]->ID; } } $link = add_query_arg( array( 'post_type' => 'adventurebook_page', 'parent_adventurebook' => intval( $last_parent ), '_adventurebook_nonce' => wp_create_nonce( 'adventurebook_set_parent_action' ) ), admin_url( 'post-new.php' ) ); echo ''; } /** * Automatically set parent when creating a new adventurebook_page * (already uses nonce verification via _adventurebook_nonce) */ add_action( 'load-post-new.php', 'adventurebook_set_new_page_parent' ); function adventurebook_set_new_page_parent() { if ( ! isset( $_GET['parent_adventurebook'] ) ) { return; } if ( ! isset( $_GET['_adventurebook_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_adventurebook_nonce'] ) ), 'adventurebook_set_parent_action' ) ) { return; } $parent_id = intval( $_GET['parent_adventurebook'] ); add_action( 'save_post_adventurebook_page', function ( $post_id, $post, $update ) use ( $parent_id ) { if ( ! $update ) { wp_update_post( array( 'ID' => $post_id, 'post_parent' => $parent_id ) ); } }, 10, 3 ); } /** * Get the adventurebook type of the parent adventurebook of the given post type * returns "storybook", "game" or null if not applicable * * @param $post_type * @param null $post * * @return string|null */ function adventurebook_get_type( $post_type, $post = null ): ?string { if ( $post_type !== 'adventurebook_page' ) { return null; } // context check if ( ! $post instanceof WP_Post ) { $post = get_post(); } if ( ! $post ) { return null; } // nonce check if ( isset( $_REQUEST['_wpnonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'adventurebook_edit_page' ) ) { return null; } // check if the adventurebook (parent of this page) is "game"-type // get the parent $parent_id = wp_get_post_parent_id( $post->ID ); if ( ! $parent_id ) { return null; } $parent_post = get_post( $parent_id ); if ( ! $parent_post || $parent_post->post_type !== 'adventurebook' ) { return null; } return get_post_meta( $parent_id, '_adventurebook_type', true ); } #endregion #region --- SETTINGS PAGE --- function adventurebook_settings_page(): void { add_submenu_page( 'adventurebook_main', __( 'Adventurebook Settings', 'adventurebook' ), __( 'Settings', 'adventurebook' ), 'manage_options', 'adventurebook-settings', 'adventurebook_settings_page_callback', ); } add_action( 'admin_menu', 'adventurebook_settings_page' ); // callback function to display the settings page function adventurebook_settings_page_callback(): void { // check for permissions if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html( __( 'You don\'t have the permission to view this page.', 'adventurebook' ) ) ); } // process form submission if ( isset( $_POST['submit'] ) ) { // security check check_admin_referer( 'adventurebook_settings' ); // FORM PROCESSING // save the character page ID option if ( isset( $_POST['adventurebook_character_page_id'] ) ) { $page_id = intval( $_POST['adventurebook_character_page_id'] ); } update_option( 'adventurebook_character_page_id', $page_id ); // save the fixed content height option if ( isset( $_POST['adventurebook_fixed_content_height'] ) ) { $height = intval( $_POST['adventurebook_fixed_content_height'] ); if ( $height < 0 ) { $height = 0; } } else { $height = 330; } update_option( 'adventurebook_fixed_content_height', $height ); echo '

' . esc_html( __( 'Options saved.', 'adventurebook' ) ) . '

'; } // process export form submission if ( isset( $_POST['export'] ) ) { // security check if ( ! isset( $_POST['adventurebook_export_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['adventurebook_export_nonce'] ) ), 'adventurebook_export' ) ) { echo '

' . esc_html( __( 'Security check failed. Please try again.', 'adventurebook' ) ) . '

'; } elseif ( isset( $_POST['adventurebook_id'] ) ) { $adventurebook_id = intval( $_POST['adventurebook_id'] ); // redirect to export handler $export_url = add_query_arg( array( 'action' => 'adventurebook_export', 'adventurebook' => $adventurebook_id, 'adventurebook_nonce' => wp_create_nonce( 'adventurebook_export_action' ) ), admin_url( 'admin-post.php' ) ); wp_redirect( $export_url ); exit; } else { echo '

' . esc_html( __( 'Please select an Adventurebook to export.', 'adventurebook' ) ) . '

'; } } // get current option value $current_page_id = get_option( 'adventurebook_character_page_id', '' ); ?>

$post->post_title, 'content' => $post->post_content, 'meta' => get_post_meta( $adventurebook_id ), 'date_export' => current_time( 'mysql' ), ); // get all pages of this adventurebook (CTP adventurebook_page) // get all items of this adventurebook - this means we get all adventurebook_item_spawns entries from the database // that have an item (code matches an entry in adventurebook_items) that belongs to this adventurebook // the item belongs to this adventurebook if the found item_spawns entry has a page_id that belongs to this adventurebook global $wpdb; $page_ids = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'adventurebook_page' AND post_parent = %d", $adventurebook_id ) ); if ( ! empty( $page_ids ) ) { // first get all item spawns and enemy spawns for these pages $placeholders = implode( ',', array_fill( 0, count( $page_ids ), '%d' ) ); $query = "SELECT * FROM {$wpdb->prefix}adventurebook_item_spawns WHERE page_id IN ($placeholders)"; $data['item_spawns'] = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->prepare( $query, ...$page_ids ), ARRAY_A ); $query = "SELECT * FROM {$wpdb->prefix}adventurebook_enemy_spawns WHERE page_id IN ($placeholders)"; $data['enemy_spawns'] = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->prepare( $query, ...$page_ids ), ARRAY_A ); // prepare an array with all item codes $item_codes = array(); foreach ( $data['item_spawns'] as $spawn ) { if ( isset( $spawn['code'] ) && ! in_array( $spawn['code'], $item_codes, true ) ) { $item_codes[] = $spawn['code']; } } // iterate through all adventurebook_pages with post_meta option _adventurebook_page_type == 'shop' // and get all items from the meta field _adventurebook_shop_items $shop_pages = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'adventurebook_page' AND post_parent = %d", $adventurebook_id ), ARRAY_A ); foreach ( $shop_pages as $shop_page ) { $page_meta = get_post_meta( $shop_page['ID'], '_adventurebook_page_type', true ); if ( $page_meta === 'shop' ) { $shop_items_codes = get_post_meta( $shop_page['ID'], '_adventurebook_shop_items', true ); $shop_items_codes = is_array( $shop_items_codes ) ? $shop_items_codes : explode( ',', $shop_items_codes ); foreach ( $shop_items_codes as $item_code ) { if ( ! in_array( $item_code, $item_codes, true ) ) { $item_codes[] = $item_code; } } } } // prepare an array with all enemy codes $enemy_codes = array(); foreach ( $data['enemy_spawns'] as $spawn ) { if ( isset( $spawn['code'] ) && ! in_array( $spawn['code'], $enemy_codes, true ) ) { $enemy_codes[] = $spawn['code']; } } // now get the actual item data for each unique item code if ( ! empty( $item_codes ) ) { $placeholders = implode( ',', array_fill( 0, count( $item_codes ), '%s' ) ); $query = "SELECT * FROM {$wpdb->prefix}adventurebook_item_definitions WHERE code IN ($placeholders)"; $data['items'] = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->prepare( $query, ...$item_codes ), ARRAY_A ); } else { $data['items'] = array(); } // now get the actual enemy data for each unique enemy code if ( ! empty( $enemy_codes ) ) { $placeholders = implode( ',', array_fill( 0, count( $enemy_codes ), '%s' ) ); $query = "SELECT * FROM {$wpdb->prefix}adventurebook_enemy_definitions WHERE code IN ($placeholders)"; $data['enemies'] = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->prepare( $query, ...$enemy_codes ), ARRAY_A ); } else { $data['enemies'] = array(); } } // get all pages for this adventurebook $pages = get_posts( array( 'post_type' => 'adventurebook_page', 'posts_per_page' => - 1, 'post_status' => 'any', 'post_parent' => $adventurebook_id, 'orderby' => 'ID', 'order' => 'ASC' ) ); $data['pages'] = array(); foreach ( $pages as $page ) { $data['pages'][] = array( 'title' => $page->post_title, 'content' => $page->post_content, 'meta' => get_post_meta( $page->ID ), ); } // create JSON $json = wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ); // Download-Header header( 'Content-Type: application/json; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="adventurebook_export_' . $adventurebook_id . '.json"' ); header( 'Content-Length: ' . strlen( $json ) ); echo $json; exit; } #endregion add_action( 'deleted_post', 'adventurebook_deleted_post' ); function adventurebook_deleted_post( $post_id ) { $post_type = get_post_type( $post_id ); if ( $post_type === 'adventurebook_page' ) { // remove any links that point to this page $all_pages = get_posts( array( 'post_type' => 'adventurebook_page', 'posts_per_page' => - 1, 'post_status' => 'any', ) ); global $wpdb; // fetch all adventurebook_page posts with the meta key _adventurebook_decision_options from the database $meta_key = '_adventurebook_decision_options'; $results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->prepare( "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s", $meta_key ), ARRAY_A ); if ( empty( $results ) ) { return; } foreach ( $results as $row ) { $meta_value = maybe_unserialize( $row['meta_value'] ); $post_id_with_meta = (int) $row['post_id']; $modified = false; if ( is_array( $meta_value ) ) { foreach ( $meta_value as $index => $option ) { if ( isset( $option['target'] ) && (int) $option['target'] === $post_id ) { unset( $meta_value[ $index ] ); $modified = true; } } } if ( $modified ) { // Neu speichern oder löschen, wenn leer if ( empty( $meta_value ) ) { delete_post_meta( $post_id_with_meta, $meta_key ); } else { update_post_meta( $post_id_with_meta, $meta_key, array_values( $meta_value ) ); } } } } }