<?php
/*
Plugin Name: Ticket 28672 Samples
Author: Pete Nelson
Version: 1.0
*/

if ( !defined( 'ABSPATH' ) ) exit( 'restricted access' );

add_action( 'admin_init', 'ticket_28672_add_settings' );

function ticket_28672_add_settings() {

	global $ticket_28672_admin_notices;
	$ticket_28672_admin_notices = array();

	// add settings field to a valid built-in section
	$results = add_settings_field( '_28672_general_default', 'Valid General/Default Field', 'ticket_28672_settings_field', 'general', 'default' );

	if ( is_wp_error( $results ) )
		$ticket_28672_admin_notices['invalid'][] = $results->get_error_message();
	else
		$ticket_28672_admin_notices['valid'][] = 'Valid General/Default field added.';


	// add settings field to a non-existent built-in section
	$results = add_settings_field( '_28672_general_invalid', 'Invalid General Section Field', 'ticket_28672_settings_field', 'general', 'non-existent-section' );

	if ( is_wp_error( $results ) )
		$ticket_28672_admin_notices['invalid'][] = $results->get_error_message();
	else
		$ticket_28672_admin_notices['valid'][] = 'General Invalid Section field added.';


	// add a settings section to a built-in page
	add_settings_section( 'new_section', 'New General Section', 'ticket_28672_settings_section', 'general' );


	// add settings field to the new section
	$results = add_settings_field( '_28672_general_new_section_field', 'Valid General/New Section Field', 'ticket_28672_settings_field', 'general', 'new_section' );

	if ( is_wp_error( $results ) )
		$ticket_28672_admin_notices['invalid'][] = $results->get_error_message();
	else
		$ticket_28672_admin_notices['valid'][] = 'Valid General/New Section field added.';


	// add a settings field to a theoretical plugin page
	$results = add_settings_field( '__28672_plugin_field', 'New Plugin Field', 'ticket_28672_settings_field', 'ticket-28672-page', 'my-plugin-section' );

	if ( is_wp_error( $results ) )
		$ticket_28672_admin_notices['invalid'][] = $results->get_error_message();
	else
		$ticket_28672_admin_notices['valid'][] = 'My Plugin Section/New Plugin Field Added (before section was defined)';


	// whoops!  let's add the settings section for the theoretical plugin, then add the field
	add_settings_section( 'my-plugin-section', 'My Plugin Section', 'ticket_28672_settings_section', 'ticket-28672-page' );
	$results = add_settings_field( '__28672_plugin_field', 'New Plugin Field', 'ticket_28672_settings_field', 'ticket-28672-page', 'my-plugin-section' );

	if ( is_wp_error( $results ) )
		$ticket_28672_admin_notices['invalid'][] = $results->get_error_message();
	else
		$ticket_28672_admin_notices['valid'][] = 'My Plugin Section/New Plugin Field Added (after section was defined)';



	// display the results
	add_action( 'admin_notices', 'ticket_28672_display_admin_notices' );

}


function ticket_28672_settings_section( $args ) {
}


function ticket_28672_settings_field() {
	echo 'Ticket 28672 Input Field Placeholder';
}


function ticket_28672_display_admin_notices( ) {

	global $ticket_28672_admin_notices;

	foreach ( $ticket_28672_admin_notices['valid'] as $message ) {
?>
    <div class="updated">
        <p>Ticket 28672: <?php echo esc_attr( $message ); ?></p>
    </div>
<?php
	}

	foreach ( $ticket_28672_admin_notices['invalid'] as $message ) {
?>
    <div class="error">
        <p>Ticket 28672: <?php echo esc_attr( $message ); ?></p>
    </div>
<?php
	}

}
