ID, '_adventurebook_decision_options', true );
$decision_options = is_array( $decision_options ) ? $decision_options : array();
// 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',
) );
?>
$option ) {
if ( $option['target'] == - 1 ) {
// create a new page
// set the title to decision option ['new_page_title'] if it exists, otherwise to "New Page"
$new_page_title = isset( $option['new_page_title'] ) && $option['new_page_title'] !== '' ? $option['new_page_title'] : __( 'New Page', 'adventurebook' );
$new_page = array(
'post_title' => $new_page_title,
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'adventurebook_page',
'post_parent' => wp_get_post_parent_id( $post_id ),
);
$new_page_id = wp_insert_post( $new_page );
if ( ! is_wp_error( $new_page_id ) ) {
$decision_options[ $index ]['target'] = $new_page_id;
// add a backlink from the new page to this page
$backlink_option = array(
'label' => $option['new_page_backlink'],
'target' => $post_id,
'decision_dir' => adventurebook_get_reverse_dir($option['decision_dir']),
'restriction_type' => $option['restriction_type'],
'restriction_value' => $option['restriction_value'],
);
update_post_meta( $new_page_id, '_adventurebook_decision_options', array( $backlink_option ) );
} else {
// if the page could not be created, remove the decision option
unset( $decision_options[ $index ] );
}
} else {
// make sure that the target page exists and is of the correct post type
$target_page = get_post( $option['target'] );
if ( ! $target_page || $target_page->post_type != 'adventurebook_page' ) {
// if the target page does not exist or is of the wrong post type, remove the decision option
unset( $decision_options[ $index ] );
}
// if a backlink was set, add a new decision option to the target page that links back to this page
if ( isset( $option['new_page_backlink'] ) && $option['new_page_backlink'] !== '' ) {
$backlink_option = array(
'label' => $option['new_page_backlink'],
'target' => $post_id,
'decision_dir' => adventurebook_get_reverse_dir($option['decision_dir']),
'restriction_type' => 'none',
'restriction_value' => '',
);
$target_decision_options = get_post_meta( $option['target'], '_adventurebook_decision_options', true );
$target_decision_options = is_array( $target_decision_options ) ? $target_decision_options : array();
// only add the backlink if it does not already exist
$backlink_exists = false;
foreach ( $target_decision_options as $existing_option ) {
if ( $existing_option['target'] == $post_id ) {
$backlink_exists = true;
break;
}
}
if ( ! $backlink_exists ) {
$target_decision_options[] = $backlink_option;
update_post_meta( $option['target'], '_adventurebook_decision_options', $target_decision_options );
}
}
}
}
// reactivate the save_post action
add_action( 'save_post', 'adventurebook_save_decision_options', 10 );
update_post_meta( $post_id, '_adventurebook_decision_options', $decision_options );
}
add_action( 'save_post', 'adventurebook_save_decision_options' );
/**
* return the title of the decision from the page $source to the page $target
* if no decision exists, an empty string is returned
*
* @param int $source
* @param int $target
*
* @return string
*/
function adventurebook_get_decision_title_from_source_to_target(int $source, int $target ): string {
// check if a decision exists from the page $source to the page $target
// this decision is saved in the meta field '_adventurebook_decision_options' of the page $source
$decision_options = get_post_meta( $source, '_adventurebook_decision_options', true );
if ( is_array( $decision_options ) ) {
foreach ( $decision_options as $option ) {
if ( $option['target'] == $target ) {
return $option['label'];
}
}
}
return '';
}