Commit b3e60090 by Romain Duminil

CommunityVoice: Ajout d'une page de paramètre

parent b89536ab
......@@ -35,6 +35,8 @@ class CommunityVoice
add_action( 'wp_ajax_vote', array( new CommunityVoice_vote(), 'vote' ) );
add_action( 'admin_menu', array( self::$Settings, 'setting_page' ) );
new Vote_UI();
}
......@@ -119,4 +121,3 @@ class CommunityVoice
}
new CommunityVoice();
......@@ -9,9 +9,16 @@ class Settings
{
const SETTING_NAME = "CommunityVoice_settings";
private $settings;
private $settingsList;
function __construct()
{
$this->settingsList = [
['id' => 'CPT_only', 'label' => 'Custom post type only : ', 'type' => 'text'],
['id' => 'activate_negative_vote', 'label' => 'Activate negative vote : ', 'type' => 'checkbox'],
['id' => 'automatically_display_vote', 'label' => 'Automatically display vote : ', 'type' => 'checkbox'],
];
if( get_option( self::SETTING_NAME, "" ) !== "" )
{
$this->settings = get_option( self::SETTING_NAME, "" );
......@@ -22,7 +29,49 @@ class Settings
$this->settings = [];
add_option( self::SETTING_NAME, "" );
}
add_action( 'admin_post_save_settings', array( $this, 'post_save_settings' ) );
}
function post_save_settings()
{
if( isset( $_POST ) )
{
foreach( $this->settingsList as $oldSetting )
{
if( ! isset( $_POST[$oldSetting['id']] ) )
{
$this->saveSetting( $oldSetting['id'], '' );
continue;
}
foreach( $_POST as $id => $setting )
{
if( $id === $oldSetting['id'] )
{
$this->saveSetting( $id, $setting );
}
}
}
}
wp_redirect( admin_url( 'admin.php?page=cv_settings' ) );
}
function setting_page()
{
if ( current_user_can( 'administrator' ) )
{
add_menu_page('CommunityVoice', 'CommunityVoice', 'administrator', 'cv_settings', array( $this, 'display_settings' ) );
}
}
function display_settings()
{
ob_start();
include 'templates/settings.php';
echo ob_get_clean();
}
/**
* Return value of setting
* @param String $name : setting key value
......
<div class="postbox" style="margin-right:1rem;padding:1rem">
<h1>Paramètres</h1>
<form class="" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
<?php foreach( $this->settingsList as $setting ) :?>
<div class="" style="padding:1rem 0">
<label for="<?= $setting['id']?>" style="display:inline-block;min-width:200px"><?= $setting['label']?></label>
<input type="<?= $setting['type']?>" name="<?= $setting['id'] ?>" id="<?= $setting['id']?>" class="popular-category" value="<?= $this->getSetting( $setting['id'] ) ?>" <?= $this->getSetting( $setting['id'] ) === 'checked' ? 'checked' : '' ?>/>
</div>
<?php endforeach;?>
<input type="submit" class="button button-primary button-large" value="Sauvegarder">
<input type="hidden" name="action" value="save_settings">
</form>
<script type="text/javascript">
jQuery("input[type=checkbox]").change(function(){
jQuery(this).prop('checked') ? jQuery(this).val("checked") : jQuery(this).val("no");
});
</script>
</div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment