<?php
$included_pages = array( 659, 646, 669 );
$pages = get_pages( array( 'post_status' => 'publish,draft', 'sort_column' => 'menu_order', 'include' => $included_pages ) );
if ( !empty($_POST['forms-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' ) );
			}
		}
		global $wpdb; //global calls the wordpress database
		$username = CLIENT_USERNAME;
		if ($username) {
			$fixed_quote = filter_var(htmlspecialchars($_POST['659']), FILTER_SANITIZE_STRING);
			$free_consultation = filter_var(htmlspecialchars($_POST['646']), FILTER_SANITIZE_STRING);
			$request_callback = filter_var(htmlspecialchars($_POST['669']), FILTER_SANITIZE_STRING);	

			$result = $wpdb->query( "REPLACE INTO ow_global_configuration (gc_option,gc_value) VALUES
				('form_fixed_quote', '$fixed_quote'),
				('form_free_consultation', '$free_consultation'),
				('form_request_callback', '$request_callback')
			");
			if ($result === false) { // If this returns false, there is incorrect code. Check all the database column names are correct. 
				echo '<div class="notice notice-error custom-notice"><p><strong>There was an error updating the database. Please contact the administrator.</strong></p></div>';
			} else if ($result == 0) { // This isn't an error
				echo '<div class="notice notice-warning is-dismissible custom-notice"><p><strong>The database hasn\'t been updated as the values haven\'t changed. Please make sure you enter the correct details.</strong></p></div>';
			} else { // Success message
				echo '<div class="notice notice-success is-dismissible custom-notice"><p><strong>The database has been successfully updated.</strong></p></div>';
			}
		} else {
			echo '<div class="notice notice-error custom-notice"><p><strong>Unable to update the database - You haven\'t specified a username.</strong></p></div>';
		}
		unset($_POST);
	}
}
?>
<form id="form-forms" method="post" action="<?php echo get_permalink(); ?>#contact-forms">
	<?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-forms" required/><label for="confirm-update-forms">Are you sure you want to update the forms selected?</label></p>
		<p><input type="submit" name="forms-submit" value="Update Forms" class="button button-primary"/></p>
	</div>
	<i class="clearfix"></i>
</form>