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() { 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', function() { 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); ?>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 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 ); wp_localize_script( 'adventurebook-decision-options', 'adventurebook_i18n', [ 'label' => __( 'Label', 'adventurebook' ), 'target' => __( 'Target Page', 'adventurebook' ), 'select_page' => __( 'Select a page', 'adventurebook' ), 'remove' => __( 'Remove', '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; } #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'), ); register_post_type('adventurebook_page', $args); } /** * Adventurebook Admin: Persistent Parent Filter and Custom Add New Book Page Button */ /** * Add parent filter dropdown in the admin list */ 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' ) ); $user_id = get_current_user_id(); $persisted_parent = get_user_meta( $user_id, 'adventurebook_last_parent', true ); $selected = isset( $_GET['adventurebook_parent'] ) ? $_GET['adventurebook_parent'] : $persisted_parent; echo ''; } /** * Apply filter to the list and save user meta */ 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'] ) ) { $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; } $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 */ 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( $_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 ); } #endregion