Ajout d’un panel Back Office avec enregistrement d’options

<?php

class my_panel{

	const option_id = 'my_panel_option_id';
	
	public static function hooks(){
		
		if ( is_admin() ){
			add_action('admin_menu', array(__CLASS__,'add_settings_panels'));
		}
		
	}
	
	public static function add_settings_panels(){
		
		add_menu_page(
        		__('Mes options'), 
        		__('Mes options'), 
        		'manage_options', 
        		'my_settings_panel', 
       			array(__CLASS__,'settings_panel')
        	);
	
	}
	
	public static function settings_panel(){
		$result = self::handle_posted_settings();
		$options = self::get_options();
		?>
		<div class="wrap">
		
			<h1>Mes options</h1>
			
			<?php if ( !empty( $result['message'] ) ): ?>
				<div class="<?php echo $result['type'] ?>" ><p><?php echo $result['message'] ?></p></div>
			<?php endif ?>

			<form method="post" action="<?php echo add_query_arg(array()) ?>">
				<table class="form-table">
				    <tr valign="top">
				        <th scope="row">Setting 1</th>
				        <td><input type="text" name="setting_1" value="<?php echo $options['setting_1'] ?>" /></td>
				    </tr>
				    <tr valign="top">
				        <th scope="row">Setting 2</th>
				        <td><input type="text" name="setting_2" value="<?php echo $options['setting_2'] ?>" /></td>
				    </tr>
				</table>
				
				<?php wp_nonce_field('my_panel_save_settings') ?>
				
				<input type="submit" class="button button-primary" value="Enregistrer" />
				
			</form>
			
		</div>
		<?php 
	}
	
	
	protected static function handle_posted_settings(){

		$result = array(
			'message' => '',
			'type' => 'updated'
		);

		if( isset($_POST['setting_1']) && check_admin_referer('my_panel_save_settings') ){
			$options = self::get_options();

			if( !empty($_POST['setting_1']) ){
				$options['setting_1'] = $_POST['setting_1']; //format/sanitize posted data!
			}
			
			if( !empty($_POST['setting_2']) ){
				$options['setting_2'] = $_POST['setting_2']; //format/sanitize posted data!
			}
			
			self::save_options($options);

			$result['message'] = "Options enregistrées";
		}

		return $result;
	}
	
	protected static function save_options($options){
		if ( get_option( self::option_id ) !== false ) {
			update_option( self::option_id, $options );
		} else {
			add_option( self::option_id, $options, '', 'no' );
		}
	}
	
	public static function get_options(){
		$options = get_option( self::option_id );
		$options = wp_parse_args(
			$options,
			array(
				'setting_1'=>'Default for setting 1',
				'setting_2'=>'Default for setting 2'
			)
		);
		return $options;
	}
	
}

my_panel::hooks();