<?php

/*--------------------------------------------------------------
Add hub pages button
--------------------------------------------------------------*/

if ( ! function_exists( 'gp_add_hub_pages' ) ) {
	function gp_add_hub_pages() {
		$screen = get_current_screen();
		if ( $screen->id == 'edit-page' || $screen->id == 'page' ) { 

		?>        
			<script>
				jQuery( '.edit-php .wrap h2 .add-new-h2' ).parent().after( '<div id="message" class="updated below-h2"><p><strong><?php _e( "Automatic Hub Creation", "gp_lang" ); ?></strong></p><p><?php _e( "Quickly create a hub page and its child pages (review, news, images, videos, write a review and user reviews pages)", "gp_lang" ); ?>.</p><form method="post" action="" enctype="multipart/form-data" role="form"><p><?php _e( "The title for your parent hub page:", "gp_lang" ); ?> <input type="text" name="hub_page_title" id="hub-page-title" value="<?php if ( isset( $_POST["hub_page_title"] ) ) { echo esc_attr( $_POST["hub_page_title"] ); } ?>" placeholder="<?php _e( "Hub Page Title", "gp_lang" ); ?>" /></p><p><?php _e( "Prefix child pages with hub page title", "gp_lang" ); ?> <input type="checkbox" name="prefix_child_pages" id="prefix-child-page" value="1" /></p><p><?php _e( "Review", "gp_lang" ); ?> <input type="checkbox" name="review_page" id="review-page" value="1" checked="checked" /> <?php _e( "News", "gp_lang" ); ?> <input type="checkbox" name="news_page" id="news-page" value="1"  checked="checked" /> <?php _e( "Images", "gp_lang" ); ?> <input type="checkbox" name="images_page" id="images-page" value="1" checked="checked" /> <?php _e( "Videos", "gp_lang" ); ?> <input type="checkbox" name="videos_page" id="videos-page" value="1" checked="checked" /> <?php _e( "Write A Review", "gp_lang" ); ?> <input type="checkbox" name="write_a_review_page" id="write-a-review-page" value="1" checked="checked" /> <?php _e( "User Reviews", "gp_lang" ); ?> <input type="checkbox" name="user_reviews_page" id="user-reviews-page" value="1" checked="checked" /></p><p><?php _e( "Create a custom hub page layout and use it for all hubs by entering the page ID:", "gp_lang" ); ?> <input type="text" name="custom_page_content" id="custom-page-content" value="<?php if ( isset( $_POST["custom_page_content"] ) ) { echo absint( $_POST["custom_page_content"] ); } ?>" placeholder="<?php _e( "Page ID", "gp_lang" ); ?>" /></p><p><input type="submit" value="<?php _e( "Create Hub", "gp_lang" ); ?>" tabindex="40" id="hub-page-submit" name="submit" class="button add-new-h2" /><input type="hidden" name="action" value="gp_hub_pages" /></p></form></div>' );
			</script>
		<?php }
	}
}
add_action( 'admin_print_footer_scripts', 'gp_add_hub_pages' );


/*--------------------------------------------------------------
Create hub pages
--------------------------------------------------------------*/

if ( ! function_exists( 'gp_create_hub_pages' ) ) {
	function gp_create_hub_pages() {

		if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty( $_POST['action'] ) && $_POST['action'] == 'gp_hub_pages' ) {

			if ( trim( $_POST['hub_page_title'] ) === '' ) {
				wp_die( __( 'You need to give your hub page a title before you can create it. Click the BACK button in your browser and add your title.', 'gp_lang' ) );
				$has_error = true;
			} else {
				$has_error = false;
			}
		
			if ( isset( $_POST['hub_page_title'] ) && $has_error == false ) {

				// Hub Page	
				$new_page_title = $_POST['hub_page_title'];
				if ( $_POST['custom_page_content'] ) {	
					$new_page_content = get_post_field( 'post_content', $_POST['custom_page_content'] );
				} else {	
					$new_page_content = '
				[vc_row full_width="" parallax="" parallax_image="" css=".vc_custom_1407855744612{margin-bottom: 0px !important;}"][vc_column width="7/12" css=".vc_custom_1407855616904{margin-bottom: 0px !important;}"][news widget_title="Latest News" format="blog-standard" size="blog-standard-size" orderby="newest" date_posted="all" date_modified="all" filter="disabled" filter_cats="" filter_date="" filter_title="" filter_comment_count="" filter_views="" filter_date_posted="" filter_date_modified="" per_page="10" offset="0" featured_image="enabled" image_width="140" image_height="140" hard_crop="enabled" image_alignment="image-align-left" title_position="title-next-to-thumbnail" content_display="excerpt" excerpt_length="100" meta_author="" meta_date="" meta_views="" meta_comment_count="" meta_cats="" meta_tags="" read_more_link="enabled" page_numbers="disabled" see_all="enabled" see_all_text="See All News"][/vc_column][vc_column width="5/12"][vc_row_inner][vc_column_inner el_class="" width="1/1"][images widget_title="Latest Images" number="8" image_width="157" image_height="132" hard_crop="enabled" see_all="enabled" see_all_text="See All Images"][/vc_column_inner][/vc_row_inner][vc_row_inner css=".vc_custom_1407855701457{margin-bottom: 0px !important;}"][vc_column_inner el_class="" width="1/1"][videos widget_title="Latest Videos" format="blog-standard" size="blog-small-size" orderby="newest" date_posted="all" date_modified="all" filter="disabled" filter_cats="" filter_date="" filter_title="" filter_comment_count="" filter_views="" filter_date_posted="" filter_date_modified="" per_page="5" offset="0" featured_image="enabled" image_width="75" image_height="75" hard_crop="enabled" image_alignment="image-align-left" title_position="title-next-to-thumbnail" content_display="excerpt" excerpt_length="0" meta_author="" meta_date="1" meta_views="" meta_comment_count="" meta_cats="" meta_tags="" read_more_link="enabled" page_numbers="disabled" see_all="enabled" see_all_text="See All Videos"][/vc_column_inner][/vc_row_inner][/vc_column][/vc_row]';
				}
				$new_page = array(
					'post_type'      => 'page',
					'post_title'     => $new_page_title,
					'post_content'   => $new_page_content,
					'post_status'    => 'publish',
					'comment_status' => 'closed'
				);
				$hub_page_id = wp_insert_post( $new_page );
				update_post_meta( $hub_page_id, '_wp_page_template', 'hub-template.php' );	
				update_post_meta( $hub_page_id, '_gp_hub_page', 'enabled' );			

				// Prefix child pages with hub page title	
				if ( isset( $_POST['prefix_child_pages'] ) && $_POST['prefix_child_pages'] == '1' ) {		
					$hub_prefix = $_POST['hub_page_title'] . ' ';
				} else {
					$hub_prefix = '';	
				}

				// Review Page
				if ( $_POST['review_page'] == '1' ) {
					$new_page_title = $hub_prefix . __( 'Review', 'gp_lang' );
					$new_page_content = '';
					$new_page = array(
						'post_type'      => 'page',
						'post_title'     => $new_page_title,
						'post_content'   => $new_page_content,
						'post_status'    => 'publish',
						'post_parent'    => $hub_page_id
					);
					$new_page_id = wp_insert_post( $new_page );
					update_post_meta( $new_page_id, '_wp_page_template', 'review-template.php' );		
					update_post_meta( $new_page_id, '_gp_hub_page', 'enabled' );
				}
				
				// News Page
				if ( $_POST['news_page'] == '1' ) {	
					$new_page_title = $hub_prefix . __( 'News', 'gp_lang' );
					$new_page_content = '';
					$new_page = array(
						'post_type'      => 'page',
						'post_title'     => $new_page_title,
						'post_content'   => $new_page_content,
						'post_status'    => 'publish',
						'comment_status' => 'closed',
						'post_parent'    => $hub_page_id
					);
					$new_page_id = wp_insert_post( $new_page );
					update_post_meta( $new_page_id, '_wp_page_template', 'news-template.php' );			
					update_post_meta( $new_page_id, '_gp_hub_page', 'enabled' );	
				}
				
				// Images Page
				if ( $_POST['images_page'] == '1' ) {
					$new_page_title = $hub_prefix . __( 'Images', 'gp_lang' );
					$new_page_content = '';
					$new_page = array(
						'post_type'      => 'page',
						'post_title'     => $new_page_title,
						'post_content'   => $new_page_content,
						'post_status'    => 'publish',
						'comment_status' => 'closed',
						'post_parent'    => $hub_page_id
					);
					$new_page_id = wp_insert_post( $new_page );
					update_post_meta( $new_page_id, '_wp_page_template', 'images-template.php' );		
					update_post_meta( $new_page_id, '_gp_hub_page', 'enabled' );		
				}
				// Videos Page
				if ( $_POST['videos_page'] == '1' ) {
				$new_page_title = $hub_prefix . __( 'Videos', 'gp_lang' );
					$new_page_content = '';
					$new_page = array(
						'post_type'      => 'page',
						'post_title'     => $new_page_title,
						'post_content'   => $new_page_content,
						'post_status'    => 'publish',
						'comment_status' => 'closed',
						'post_parent'    => $hub_page_id
					);
					$new_page_id = wp_insert_post( $new_page );
					update_post_meta( $new_page_id, '_wp_page_template', 'videos-template.php' );		
					update_post_meta( $new_page_id, '_gp_hub_page', 'enabled' );		
				}
					
				// Write A Review Page
				if ( $_POST['write_a_review_page'] == '1' ) {
					$new_page_title = $hub_prefix . __( 'Write A Review', 'gp_lang' );
					$new_page_content = '';
					$new_page = array(
						'post_type'      => 'page',
						'post_title'     => $new_page_title,
						'post_content'   => $new_page_content,
						'post_status'    => 'publish',
						'comment_status' => 'closed',
						'post_parent'    => $hub_page_id
					);
					$new_page_id = wp_insert_post( $new_page );
					update_post_meta( $new_page_id, '_wp_page_template', 'write-a-review-template.php' );		
				}
				
				// User Reviews Page
				if ( $_POST['user_reviews_page'] == '1' ) {
					$new_page_title = $hub_prefix . __( 'User Reviews', 'gp_lang' );
					$new_page_content = '';
					$new_page = array(
						'post_type'      => 'page',
						'post_title'     => $new_page_title,
						'post_content'   => $new_page_content,
						'post_status'    => 'publish',
						'comment_status' => 'closed',
						'post_parent'    => $hub_page_id
					);
					$new_page_id = wp_insert_post( $new_page );
					update_post_meta( $new_page_id, '_wp_page_template', 'user-reviews-template.php' );			
					update_post_meta( $new_page_id, '_gp_hub_page', 'enabled' );
				}
									
				// Redirect to hub page
				wp_redirect( get_permalink( $hub_page_id ) );
				exit;

			}
			
		}

	}
}

return gp_create_hub_pages();	

?>