<?php
$included_pages = array( 328, 320, 326, 322, 324, 314, 330, 316, 318, 370, 299, 301, 304, 306, 308, 294, 310, 312 );
$pages = get_pages( array( 'post_status' => 'publish,draft', 'sort_column' => 'menu_order', 'include' => $included_pages ) );
if ( !empty($_POST['calcs-submit'] ) ) {
	if ( $_SERVER["REQUEST_METHOD"] == "POST" ) {
		foreach( $pages as $page ) {
			if( array_key_exists( $page->ID, $_POST ) && ( isset( $_POST[$page->ID] ) || $_POST[$page->ID] == 'checked' ) ) {
				wp_update_post( array('ID' => $page->ID, 'post_status' => 'publish' ) );
			} else {
				wp_update_post( array('ID' => $page->ID, 'post_status' => 'draft' ) );
			}
		}
		unset($_POST);
		echo '<div class="notice notice-success is-dismissible custom-notice"><p><strong>Successfully updated.</strong></p></div>';
	}
}
?>
<form id="form-pages" method="post" action="<?php echo get_permalink(); ?>#calculators">
	<?php
	$pages = get_pages( array( 'post_status' => 'publish,draft', 'sort_column' => 'menu_order', 'include' => $included_pages ) );
	foreach( $pages as $page ) {
		if( $page->post_status == 'publish' ) {
			$checked = 'checked';
		} else {
			$checked = NULL;
		}
		echo '<p class="services-item"><input type="checkbox" id="check-'.$page->ID.'" name="'.$page->ID.'" value="'.$checked.'" '.$checked.'/><label for="check-'.$page->ID.'">'.$page->post_title.'</label></p>';
	}
	?>
	<div class="update-wrap">
		<p class="alert"><strong>Changing the options above will take time to update, please be patient.</strong></p>
		<p><input type="checkbox" id="confirm-update-calcs" required/><label for="confirm-update-calcs">Are you sure you want to update the pages selected?</label></p>
		<p><input type="submit" name="calcs-submit" value="Update Calculators" class="button button-primary"/></p>
	</div>
	<i class="clearfix"></i>
</form>