<?php
$pages = get_pages( array( 'parent' => 29, 'post_status' => 'publish,draft' ) );
//if the form is posted, has all the mandatory fields and captcha matches
if ( ! empty( $_POST['resources-submit'] ) ) {
if ( $_SERVER["REQUEST_METHOD"] == "POST" ) {
?>

				<?php
				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>';	

} }
//if the form has not been posted	
?>
<form id="form-resources" method="post" action="<?php echo get_permalink(); ?>#resources-pages">
	<?php
	$pages = get_pages( array( 'parent' => 29, 'post_status' => 'publish,draft' ) );
	
	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-resources" required/><label for="confirm-update-resources">Are you sure you want to update the resources pages selected?</label></p>
		<p><input type="submit" name="resources-submit" value="Update Resources Pages" class="button button-primary"/></p>
	</div>
	<i class="clearfix"></i>
</form>