is_main_query() && $query->is_search() ) {
// Deine Custom Post Types ausschließen
$excluded = [ 'adventurebook', 'adventurebook_page' ];
// Falls es bereits post_type gesetzt ist, nur ergänzen
if ( ! empty( $query->get( 'post_type' ) ) ) {
$post_types = (array) $query->get( 'post_type' );
$post_types = array_diff( $post_types, $excluded );
$query->set( 'post_type', $post_types );
} else {
// Standardmäßig auf 'post' und 'page' beschränken
$query->set( 'post_type', [ 'post', 'page' ] );
}
}
} );
/**
* Safely send HTML content in a wp_send_json_error or wp_send_json_success call.
*
* @param string $message The message string, can contain HTML tags.
* @param array $allowed_html Optional. Allowed HTML tags. Defaults to common post tags.
* @param bool $success Optional. True = wp_send_json_success, False = wp_send_json_error.
*/
function adventurebook_send_json_message( $message, $allowed_html = null, $success = false ) {
// Default allowed tags if none provided
if ( $allowed_html === null ) {
$allowed_html = array(
'p' => array(
'style' => array(),
'class' => array(),
),
'a' => array(
'href' => array(),
'title' => array(),
'target' => array(),
),
'strong' => array(),
'em' => array(),
'br' => array(),
);
}
// Escape URLs in tags inside the message
$message = wp_kses( $message, $allowed_html );
// Send the JSON response
if ( $success ) {
wp_send_json_success( $message );
} else {
wp_send_json_error( $message );
}
}
/**
* checks if a character can travel from one zone to another
* on the from_zone page, a decision has to exist which leads to the to_zone page
* if the decision has a restriction, the character must fulfill the restriction to travel
*
* @param $character_id
* @param $from_zone_id
* @param $to_zone_id
*
* @return bool true if the character can travel, false otherwise
*/
function can_travel_from_zone_to_zone( $character_id, $from_zone_id, $to_zone_id ): bool {
global $adventurebook_id;
// if the character_id is 0, the character is not logged in or this is a story without game elements and restrictions
if ( $character_id == 0 ) {
$decisions = get_post_meta( $from_zone_id, '_adventurebook_decision_options', true );
$decisions = is_array( $decisions ) ? $decisions : array();
foreach ( $decisions as $decision ) {
if ( $decision['target'] == $to_zone_id ) {
return true;
}
}
// if the destination page is the starting page, we return true because jumping to the start is always possible
$start_page_id = get_post_meta( $adventurebook_id, '_adventurebook_start_page', true );
if ( $to_zone_id == $start_page_id ) {
return true;
}
return false;
}
// get the current character record
$adventurebook_current_character = Character::get_character_by_id( $character_id );
$character_zone = $adventurebook_current_character->zone;
$decisions = get_post_meta( $from_zone_id, '_adventurebook_decision_options', true );
$decisions = is_array( $decisions ) ? $decisions : array();
foreach ( $decisions as $decision ) {
if ( $decision['target'] == $to_zone_id ) {
// check if there is a restriction for this decision
if ( isset( $decision['restriction_type'] ) && isset( $decision['restriction_value'] ) && $decision['restriction_type'] != '' && $decision['restriction_value'] != '' ) {
$restriction_type = $decision['restriction_type'];
$restriction_value = $decision['restriction_value'];
// check if the character has the restriction
if ( $restriction_type == 'item' ) {
$items = $adventurebook_current_character->get_inventory();
foreach ( $items as $item_info ) {
if ( $item_info["item_code"] == $restriction_value ) {
return true;
}
}
return false;
}
}
}
}
return true;
}
/**
* checks if the given adventurebook_id is a game
*
* @param $adventurebook_id
*
* @return bool
*/
function is_game( $adventurebook_id ): bool {
// the adventure book id is the parent of the page
$adventurebook_type = get_post_meta( $adventurebook_id, '_adventurebook_type', true );
if ( $adventurebook_type === 'game' ) {
return true;
} else {
return false;
}
}
function adventurebook_check_ownership( $character_id ) {
// check if the character is owned by the actual user
if ( $character_id == 0 ) {
$response = array(
'status' => 'no character id provided',
);
wp_send_json( $response );
}
$user_id = get_current_user_id();
$character = Character::get_character_by_id( $character_id );
if ( $character->user_id != $user_id ) {
$response = array(
'status' => 'character not owned by user',
);
wp_send_json( $response );
}
}
function adventurebook_drop_item_ajax() {
check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren
if ( isset( $_POST['adventurebook_id'] ) ) {
$adventurebook_id = intval( $_POST['adventurebook_id'] );
}
// get the current character id
if ( isset( $_POST['character_id'] ) ) {
$character_id = intval( $_POST['character_id'] );
} else {
$character_id = 0;
}
adventurebook_check_ownership( $character_id );
// get the character
$character = Character::get_character_by_id( $character_id );
// check if the required parameters are set
if ( isset( $_POST['item_code'] ) ) {
$item_code = sanitize_text_field( wp_unslash( $_POST['item_code'] ) );
// remove the item from the character's inventory
$character->remove_item_from_inventory( $item_code, 1 );
// create a new item in the game at the character's current location
Item::create_item( $character_id, $item_code, $character->zone );
$response = array(
'status' => 'success',
);
} else {
$response = array(
'status' => 'missing item_id',
);
}
// now we need to call the adventurebook_get_page_content_ajax function to get the updated page content
adventurebook_get_page_content_ajax();
}
add_action( 'wp_ajax_adventurebook_drop_item', 'adventurebook_drop_item_ajax' );
function adventurebook_equip_item_ajax() {
check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren
if ( isset( $_POST['adventurebook_id'] ) ) {
$adventurebook_id = intval( $_POST['adventurebook_id'] );
}
// get the current character id
if ( isset( $_POST['character_id'] ) ) {
$character_id = intval( $_POST['character_id'] );
} else {
$character_id = 0;
}
adventurebook_check_ownership( $character_id );
// get the character
$character = Character::get_character_by_id( $character_id );
// check if the required parameters are set
if ( isset( $_POST['item_code'] ) ) {
$item_code = sanitize_text_field( wp_unslash( $_POST['item_code'] ) );
// equip the item to the character's inventory
$result = $character->equipItem( $item_code );
if ( $result ) {
$response = array(
'status' => 'success',
);
} else {
$response = array(
'status' => 'failed',
);
}
} else {
$response = array(
'status' => 'missing item_code',
);
}
// now we need to call the adventurebook_get_page_content_ajax function to get the updated page content
adventurebook_get_page_content_ajax();
//wp_send_json( $response );
}
add_action( 'wp_ajax_adventurebook_equip_item', 'adventurebook_equip_item_ajax' );
function adventurebook_unequip_item_ajax() {
check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren
// get the current character id
if ( isset( $_POST['character_id'] ) ) {
$character_id = intval( $_POST['character_id'] );
} else {
$character_id = 0;
}
adventurebook_check_ownership( $character_id );
// get the character
$character = Character::get_character_by_id( $character_id );
// check if the required parameters are set
if ( isset( $_POST['slotname'] ) ) {
$slotname = sanitize_text_field( wp_unslash( $_POST['slotname'] ) );
// just unequip this slot
$character->unEquipSlot( $slotname );
$response = array(
'status' => 'success',
);
} else {
$response = array(
'status' => 'missing item_code',
);
}
// now we need to call the adventurebook_get_page_content_ajax function to get the updated page content
adventurebook_get_page_content_ajax();
}
add_action( 'wp_ajax_adventurebook_unequip_item', 'adventurebook_unequip_item_ajax' );
function adventurebook_use_item_ajax() {
check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren
// get the current character id
if ( isset( $_POST['character_id'] ) ) {
$character_id = intval( $_POST['character_id'] );
} else {
$character_id = 0;
}
// get the character
$character = Character::get_character_by_id( $character_id );
adventurebook_check_ownership( $character_id );
// check if the required parameters are set
if ( isset( $_POST['item_code'] ) ) {
$item_code = sanitize_text_field( wp_unslash( $_POST['item_code'] ) );
// use the item from the character's inventory
$result = $character->use_item( $item_code );
if ( $result ) {
$response = array(
'status' => 'success',
);
} else {
$response = array(
'status' => 'failed',
);
}
} else {
$response = array(
'status' => 'missing item_code',
);
}
// now we need to call the adventurebook_get_page_content_ajax function to get the updated page content
adventurebook_get_page_content_ajax();
}
add_action( 'wp_ajax_adventurebook_use_item', 'adventurebook_use_item_ajax' );
/**
* AJAX handler to take an item from the page
* this will remove the item from the game, but add it's code to the character's inventory with a new unique inventory id
* and an amount ("count") of 1.
*/
function adventurebook_take_item_ajax() {
check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren
if ( isset( $_POST['adventurebook_id'] ) ) {
$adventurebook_id = intval( $_POST['adventurebook_id'] );
}
if ( isset( $_POST['character_id'] ) ) {
$character_id = intval( $_POST['character_id'] );
} else {
$character_id = 0;
}
adventurebook_check_ownership( $character_id );
// check if the required parameters are set
if ( isset( $_POST['item_id'] ) ) {
$item_id = intval( $_POST['item_id'] );
$item = Item::get_item_by_id( $item_id );
$item_code = $item->code;
// remove the item from the game
Item::delete_item( $item_id );
// add the item to the character's inventory
$character = Character::get_character_by_id( $character_id );
if ( $character ) {
$character->add_item_to_inventory( $item_code, 1 );
$response = array(
'status' => 'success',
);
} else {
$response = array(
'status' => 'character not found',
);
}
} else {
$response = array(
'status' => 'missing item_id',
);
}
// now we need to call the adventurebook_get_page_content_ajax function to get the updated page content
adventurebook_get_page_content_ajax();
}
add_action( 'wp_ajax_adventurebook_take_item', 'adventurebook_take_item_ajax' );
function adventurebook_buy_item_ajax() {
check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren
if ( isset( $_POST['adventurebook_id'] ) ) {
$adventurebook_id = intval( $_POST['adventurebook_id'] );
}
// check if the required parameters are set
if ( isset( $_POST['item_id'] ) && isset( $_POST['page_id'] ) && isset( $_POST['character_id'] ) ) {
$item_id = intval( $_POST['item_id'] );
$page_id = intval( $_POST['page_id'] );
$character_id = intval( $_POST['character_id'] );
adventurebook_check_ownership( $character_id );
// check if the item is for sale on this page
$shop_items_codes = get_post_meta( $page_id, '_adventurebook_shop_items', true );
$shop_items_codes = is_array( $shop_items_codes ) ? $shop_items_codes : explode( ',', $shop_items_codes );
$shop_items_codes = array_map( 'trim', $shop_items_codes );
$shop_items_codes = array_filter( $shop_items_codes );
$item_definition = Item_Definition::get_item_definition_by_id( $item_id );
if ( ! $item_definition || ! in_array( $item_definition->code, $shop_items_codes ) ) {
wp_send_json_error( esc_html( __( 'This item is not for sale on this page.', 'adventurebook' ) ) );
}
// get the wealth stat for the character
$character = Character::get_character_by_id( $character_id );
if ( ! $character ) {
wp_send_json_error( esc_html( __( 'Character not found.', 'adventurebook' ) ) );
}
$adventurebook_combat_system = get_combat_system_for_adventurebook( $adventurebook_id );
if ( ! $adventurebook_combat_system ) {
wp_send_json_error( esc_html( __( 'Combat system not found.', 'adventurebook' ) ) );
}
$character_stats = $character->getStats();
$character_wealth = isset( $character_stats['Wealth'] ) ? intval( $character_stats['Wealth'] ) : 0;
// get the item price
$stats = $item_definition->get_stats();
$item_price_stat_name = $adventurebook_combat_system->getItemStatNameForPrice();
$item_price = isset( $stats[ $item_price_stat_name ] ) ? intval( $stats[ $item_price_stat_name ] ) : 0;
$shop_price_factor = get_post_meta( $page_id, '_adventurebook_shop_price_factor', true );
$shop_price_factor = floatval( $shop_price_factor );
if ( $shop_price_factor <= 0 ) {
$shop_price_factor = 1.0;
}
$item_price = intval( $item_price * $shop_price_factor );
// check if the character has enough wealth to buy the item
if ( $character_wealth < $item_price ) {
status_header( 403 );
$unit = $adventurebook_combat_system->getWealthUnit();
/* translators: %s is the unit of wealth, e.g. "gold" */
wp_send_json_error( sprintf( esc_html( __( 'You do not have enough %s to buy this item.', 'adventurebook' ) ), esc_html( $unit ) ) );
}
// deduct the item price from the character's wealth
$character->addWealth( - $item_price );
// add the item to the character's inventory
$character->add_item_to_inventory( $item_definition->code, 1 );
$response = array(
'status' => 'success',
);
} else {
$response = array(
'status' => 'missing parameters',
);
}
// now we need to call the adventurebook_get_page_content_ajax function to get the updated page content
adventurebook_get_page_content_ajax();
}
add_action( 'wp_ajax_adventurebook_buy_item', 'adventurebook_buy_item_ajax' );
function adventurebook_sell_item_ajax() {
check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren
if ( isset( $_POST['adventurebook_id'] ) ) {
$adventurebook_id = intval( $_POST['adventurebook_id'] );
}
$adventurebook_combat_system = get_combat_system_for_adventurebook( $adventurebook_id );
if ( ! $adventurebook_combat_system ) {
wp_send_json_error( esc_html( __( 'Combat system not found.', 'adventurebook' ) ) );
}
// check if the required parameters are set
if ( isset( $_POST['item_code'] ) && isset( $_POST['page_id'] ) && isset( $_POST['character_id'] ) ) {
$item_code = sanitize_text_field( wp_unslash( $_POST['item_code'] ) );
$page_id = intval( $_POST['page_id'] );
$character_id = intval( $_POST['character_id'] );
adventurebook_check_ownership( $character_id );
// check if this page is of type "Shop"
// Gespeicherter Typ
$page_type = get_post_meta( $page_id, '_adventurebook_page_type', true );
// Nur anzeigen, wenn Typ bereits "Shop" ist
if ( $page_type !== 'shop' ) {
wp_send_json_error( esc_html( __( 'Here is no shop.', 'adventurebook' ) ) );
}
$character = Character::get_character_by_id( $character_id );
if ( ! $character ) {
wp_send_json_error( esc_html( __( 'Character not found.', 'adventurebook' ) ) );
}
// check if the item is in the character's inventory
$item_count = $character->get_item_count_by_code( $item_code );
if ( ! $item_count ) {
wp_send_json_error( esc_html( __( 'This item is not in your inventory.', 'adventurebook' ) ) );
}
$item_definition = Item_Definition::get_item_definition_by_code( $item_code );
if ( ! $item_definition ) {
wp_send_json_error( esc_html( __( 'Item definition not found.', 'adventurebook' ) ) );
}
// get the item price
$stats = $item_definition->get_stats();
$item_price_stat_name = $adventurebook_combat_system->getItemStatNameForPrice();
$item_price = isset( $stats[ $item_price_stat_name ] ) ? floatval( $stats[ $item_price_stat_name ] ) : 0;
// add the item price to the character's wealth
$character->addWealth( $item_price );
// remove the item from the character's inventory
$character->remove_item_from_inventory( $item_code, 1 );
$response = array(
'status' => 'success',
);
} else {
$response = array(
'status' => 'missing parameters',
);
}
// now we need to call the adventurebook_get_page_content_ajax function to get the updated page content
adventurebook_get_page_content_ajax();
}
add_action( 'wp_ajax_adventurebook_sell_item', 'adventurebook_sell_item_ajax' );
/**
* This function is called via AJAX when the player clicks on a link generated by the "showkeylink" script command.
* It sets the current key of the script to the given key, so that the script can continue execution from that point.
* The function expects the following parameters to be passed via POST:
* - script_code: the code of the script to be executed
* - key: the key to be set
* - character_id: the id of the character
*
* @return void
*/
function adventurebook_set_key_for_script() {
check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren
if ( isset( $_POST['adventurebook_id'] ) ) {
$adventurebook_id = intval( $_POST['adventurebook_id'] );
}
// check if the required parameters are set
if ( isset( $_POST['script_code'] ) && isset( $_POST['key'] ) && isset( $_POST['character_id'] ) ) {
$script_code = sanitize_text_field( wp_unslash( $_POST['script_code'] ) );
$key = sanitize_text_field( wp_unslash( $_POST['key'] ) );
$character_id = intval( $_POST['character_id'] );
adventurebook_check_ownership( $character_id );
$character = Character::get_character_by_id( $character_id );
// set the current key of the script to the given key
// this is done by setting a game var
$character->set_game_var( "script_" . $script_code . "_key", $key );
// save the character
$character->save_to_db();
$response = array(
'status' => 'success',
);
} else {
$response = array(
'status' => 'missing parameters',
);
}
// now we need to call the adventurebook_get_page_content_ajax function to get the updated page content
adventurebook_get_page_content_ajax();
}
add_action( 'wp_ajax_adventurebook_set_key_for_script', 'adventurebook_set_key_for_script' );
function adventurebook_attack_enemy_ajax() {
check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren
if ( isset( $_POST['adventurebook_id'] ) ) {
$adventurebook_id = intval( $_POST['adventurebook_id'] );
}
// check if the required parameters are set
if ( isset( $_POST['enemy_id'] ) && isset( $_POST['character_id'] ) ) {
$enemy_id = intval( $_POST['enemy_id'] );
$character_id = intval( $_POST['character_id'] );
adventurebook_check_ownership( $character_id );
$character = Character::get_character_by_id( $character_id );
// this will set the next action of the character to "attack_".$enemy_id
$character->setNextAction( "attack_" . $enemy_id );
// save the character
$character->save_to_db();
// afterwards, we return success - the next round will be started by the frontend via calling the refresh
$response = array(
'status' => 'success',
);
} else {
$response = array(
'status' => 'missing parameters',
);
}
// now we need to call the adventurebook_get_page_content_ajax function to get the updated page content
adventurebook_get_page_content_ajax();
}
add_action( 'wp_ajax_adventurebook_attack_enemy', 'adventurebook_attack_enemy_ajax' );
function adventurebook_respawn_ajax() {
check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren
if ( isset( $_POST['adventurebook_id'] ) ) {
$adventurebook_id = intval( $_POST['adventurebook_id'] );
} else {
// json error
$response = array(
'status' => 'missing adventurebook_id',
);
wp_send_json( $response );
return;
}
// get the current character id
if ( isset( $_POST['character_id'] ) ) {
$character_id = intval( $_POST['character_id'] );
} else {
// json error
$response = array(
'status' => 'missing character_id',
);
wp_send_json( $response );
return;
}
adventurebook_check_ownership( $character_id );
// get the character
$character = Character::get_character_by_id( $character_id );
if ( $character ) {
// respawn the character at the start page of this adventurebook
// get the start page
$start_page_id = get_post_meta( $adventurebook_id, '_adventurebook_start_page', true );
$character->zone = $character->getStat( 'RespawnZone' ) ? $character->getStat( 'RespawnZone' ) : $start_page_id;
// clear the next action
$character->setNextAction( "" );
// respawn the character
$character->respawn();
$response = array(
'status' => 'success',
);
} else {
$response = array(
'status' => 'character not found',
);
}
// now we need to call the adventurebook_get_page_content_ajax function to get the updated page content
adventurebook_get_page_content_ajax();
}
add_action( 'wp_ajax_adventurebook_respawn', 'adventurebook_respawn_ajax' );
/**
* AJAX handler to get the content of an adventurebook page
* This function checks if the user is logged in (if the adventurebook is a game)
* and if the user has a current character selected.
* It then fetches the current page of the adventurebook and returns the HTML content.
*
* IMPORTANT
*
* Each call of this function will calculate a new round for the character if the adventurebook is a game.
*/
function adventurebook_get_page_content_ajax() {
global $adventurebook_is_game, $page_id, $adventurebook_combat_system, $adventurebook_current_character, $adventurebook_id, $adventurebook_current_character_id, $decisions, $adventurebook_character_management_url;
// check for nonce
check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren
$error_msg = '';
// the adventurebook_id was passed to the ajax function
if ( isset( $_POST['adventurebook_id'] ) ) {
$adventurebook_id = intval( $_POST['adventurebook_id'] );
}
$adventurebook_is_game = is_game( $adventurebook_id );
// for adventurebook type "game" only: check if the character management page is configured
if ( $adventurebook_is_game ) {
$character_page_id = get_option( 'adventurebook_character_page_id', 0 );
if ( ! $character_page_id ) {
wp_send_json_error( ' ' . esc_html( __( 'Character management page not configured.', 'adventurebook' ) ) . ' ' . sprintf( __( 'You need to log in to access this page.', 'adventurebook' ), esc_url( $login_url ) ) . '
' . sprintf( __( 'The current character no longer exists. Go to character management.', 'adventurebook' ), $adventurebook_character_management_url ) . '
' ); } adventurebook_check_ownership( $adventurebook_current_character_id ); } // if there is no adventurebook_id, return an error message if ( ! $adventurebook_id ) { wp_send_json_error( '' . esc_html( __( 'No Adventurebook ID provided.', 'adventurebook' ) ) . '
' ); } // fetch the Adventurebook $adventurebook = get_post( $adventurebook_id ); // fetch the combat system $adventurebook_combat_system = get_combat_system_for_adventurebook( $adventurebook_id ); // Check if the Adventurebook exists and is of the type `adventurebook` if ( ! $adventurebook || $adventurebook->post_type !== 'adventurebook' ) { wp_send_json_error( '' . esc_html( __( 'Adventurebook not found.', 'adventurebook' ) ) . '
' ); } $page_id = get_actual_page_id( $adventurebook_id ); $adventurebook_page = get_post( $page_id ); // Check if the Adventurebook page exists and is of the type `adventurebook_page` if ( ! $adventurebook_page || $adventurebook_page->post_type !== 'adventurebook_page' ) { wp_send_json_error( '' . esc_html( __( 'Adventurebook page not found.', 'adventurebook' ) ) . '
' ); } // Get the decision options for the current page $decisions = get_post_meta( $page_id, '_adventurebook_decision_options', true ); $decisions = is_array( $decisions ) ? $decisions : array(); // set the current page as the current zone for the character, if this is a game // else set the page id in the session if ( $adventurebook_is_game ) { $wpdb->update( $wpdb->prefix . "adventurebook_characters", array( 'zone' => $page_id ), array( 'id' => $adventurebook_current_character_id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } else { // set the adventurebook_id AND the page_id in the session adventurebook_save_to_session( 'adventurebook_page_id_' . $adventurebook_id, $page_id ); } if ( $adventurebook_is_game ) { // add 1 to the total rounds played $rounds_played = intval( $adventurebook_current_character->get_game_var( 'rounds_played' ) ); $rounds_played ++; $adventurebook_current_character->set_game_var( 'rounds_played', $rounds_played ); // finally, check for respawned enemies on this page Enemy_Spawn::check_for_respawns( $adventurebook_current_character_id ); // check for an attached script on this page and run it $script_code = get_post_meta( $page_id, '_adventurebook_page_script', true ); $script = Script::get_script_by_code( $script_code ); if ( $script ) { $script_output = $script->run_script( $adventurebook_current_character ); } else { $script_output = []; } // each page refresh means a new round - so we call the central doCombat method of the combat system // for this we need all enemies on this page for this character $enemies = Enemy::get_enemies_for_page( $page_id, $adventurebook_current_character_id ); $output = $adventurebook_combat_system->doCombat( $adventurebook_current_character, $enemies ); // TODO: check if the combat modifications (e.g. health) are saved to the character and enemies and are shown correctly below } // --------------------------- // // Start output // // --------------------------- $content = ''; $content .= '' . esc_html( __( 'Your inventory is empty.', 'adventurebook' ) ) . '
'; $content .= '