Commit 2c5d6ed6 authored by Cyrille37's avatar Cyrille37

namespace + dashboard

parent d1d68a00
......@@ -7,11 +7,15 @@
* Author: Cyrille GIQUELLO, Romain DUMINIL
*/
require_once 'CommunityProject_install.php';
require_once 'UserRoleController.php';
require_once 'ProjectCustomTypeCreator.php';
require_once 'ShortcodeController.php';
require_once ABSPATH . '/wp-content/plugins/CommunityVoice/Settings.php';
namespace CommunityProject ;
require_once __DIR__. '/Settings.php';
require_once __DIR__. '/CommunityProject_install.php';
require_once __DIR__. '/UserRoleController.php';
require_once __DIR__. '/ProjectCustomTypeCreator.php';
require_once __DIR__. '/ShortcodeController.php';
require_once __DIR__. '/DashboardWidget.php';
//require_once ABSPATH . '/wp-content/plugins/CommunityVoice/Settings.php';
class CommunityProject
{
......@@ -20,30 +24,40 @@ class CommunityProject
const CP_DEBUG_MODE = false;
protected $settings ;
function __construct()
{
register_activation_hook( __FILE__, array( new CommunityProject_install(), 'activate' ) );
register_deactivation_hook( __FILE__, array( new CommunityProject_install(), 'deactivate' ) );
$installer = new CommunityProject_install() ;
register_activation_hook( __FILE__, array( $installer, 'activate' ) );
register_deactivation_hook( __FILE__, array( $installer, 'deactivate' ) );
register_uninstall_hook( __FILE__, array( 'CommunityProject_install', 'uninstall' ) );
add_action('plugins_loaded', array( $this, 'add_CommunityVoice_setting' ));
$this->settings = new Settings();
add_action( 'init', array( new ProjectCustomTypeCreator(), 'create_project_post_type' ) );
add_action( 'init', array( new UserRoleController(), 'create_student_role' ) );
add_action( 'init', array( new ProjectCustomTypeCreator(), 'create_status_taxonomy' ) );
add_action( 'init', array( new ProjectCustomTypeCreator(), 'create_default_status_terms' ) );
add_action( 'init', array( new UserRoleController(), 'set_user_role_permission' ) );
$userRoleController = new UserRoleController();
add_action( 'init', array( $userRoleController, 'create_student_role' ) );
add_action( 'init', array( $userRoleController, 'set_user_role_permission' ) );
$projectCustomTypeCreator = new ProjectCustomTypeCreator() ;
add_action( 'init', array( $projectCustomTypeCreator, 'create_project_post_type' ) );
add_action( 'init', array( $projectCustomTypeCreator, 'create_status_taxonomy' ) );
add_action( 'init', array( $projectCustomTypeCreator, 'create_default_status_terms' ) );
add_action( 'add_meta_boxes', array( $this, 'add_custom_fields' ) );
add_action( 'admin_menu', array( $this, 'remove_meta_boxes' ) );
add_action( 'admin_menu', array( $this, 'redirect_if_not_author' ) );
add_action( 'add_meta_boxes', array( $this, 'add_custom_fields' ) );
add_action( 'do_meta_boxes', array( $this, 'remove_edit_flow_metabox' ) );
add_action( 'save_post', array( $this, 'set_default_status' ) );
add_action( 'after_setup_theme', array( $this, 'remove_admin_bar') );
add_action( 'save_post', array( $this, 'set_default_status' ) );
add_action( 'save_post', array( $this, 'save_custom_meta_box' ), 10, 3 );
add_filter('screen_options_show_screen', array( $this, 'remove_screen_options_tab' ) );
add_action( 'wp_dashboard_setup', array( new DashboardWidget(), 'wp_dashboard_setup' ) );
new ShortcodeController();
}
......@@ -71,13 +85,6 @@ class CommunityProject
return current_user_can( 'publish_posts' ); // editor
}
function add_CommunityVoice_setting()
{
$Settings = new Settings();
$Settings->saveSetting( 'CPT_only', 'project' );
}
/**
* Set default status to project post type
* @param $post_id
......
<?php
namespace CommunityProject ;
class CommunityProject_install
{
function install()
......
<?php
/**
* DashboardWidget.php
*/
namespace CommunityProject ;
class DashboardWidget
{
const SLUG = 'communityproject-widget' ;
public function __construct()
{
}
public function wp_dashboard_setup()
{
wp_add_dashboard_widget(
self::SLUG, // Widget slug.
'Mes projets', // Title.
array( $this, 'display_widget' ) // Display function.
);
}
public function display_widget()
{
// Display whatever it is you want to show.
echo "Hello World, I'm a great Dashboard Widget";
}
}
<?php
namespace CommunityProject ;
class ProjectCustomTypeCreator
{
function create_project_post_type()
......
<?php
namespace CommunityProject ;
/**
* Class define CommunitVoice settings.
* Settings are save in wp_options table, with key : "CommunityVoice_settings"
* Settings format : setting1:value1;setting2:value2;
*/
class Settings
{
const SETTING_NAME = 'CommunityProject_settings';
private $settings;
private $settingsList;
function __construct()
{
$this->settingsList = [
//['id' => self::CPT, 'label' => 'Custom post type only : ', 'type' => 'text', 'description'=>'Vote actif seulement pour les articles de ce type.'],
];
if( get_option( self::SETTING_NAME, '' ) !== '' )
{
$this->settings = get_option( self::SETTING_NAME, '' );
$this->settings = explode( ";", $this->settings );
}
else
{
$this->settings = [];
add_option( self::SETTING_NAME, '' );
}
add_action( 'admin_menu', array( $this, 'wp_admin_menu_setting_page' ) );
add_action( 'admin_post_save_settings', array( $this, 'wp_admin_post_save_settings' ) );
}
function wp_admin_menu_setting_page()
{
if ( current_user_can( 'administrator' ) )
{
add_menu_page('CommunityProject', 'CommunityProject', 'administrator', 'communityproject_settings', array( $this, 'menu_page_settings' ) );
}
}
function menu_page_settings()
{
ob_start();
include 'templates/settings.php';
echo ob_get_clean();
}
function wp_admin_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' ) );
}
/**
* Return value of setting
* @param String $name : setting key value
* @return mixed
*/
function getSetting( $name )
{
return isset( $this->getArraySettings()[$name] ) ? $this->getArraySettings()[$name] : false;
}
/**
* Return array of all Community Voice setting
* @return array
*/
function getArraySettings()
{
$arraySettings = [];
foreach( $this->settings as $setting )
{
//list( $key, $value ) = explode( ":", $setting );
$parts = explode( ':', $setting );
$key = $parts[0] ;
$value = isset( $parts[1] ) ? $parts[1] : null;
if( $value === 'true' )
{
$value = true;
}
else if( $value === 'false' )
{
$value = false;
}
else if( is_numeric( $value ) )
{
settype( $value, 'float' );
}
$arraySettings[$key] = $value;
}
return $arraySettings;
}
/**
* Save a setting in database
* @param String $key : Setting key
* @param mixed $value : Setting value
*/
function saveSetting( $key, $value )
{
$settingsToSave = '';
$arraySettings = $this->getArraySettings();
$arraySettings[$key] = $value;
foreach( $arraySettings as $key => $value )
{
$settingsToSave .= $key . ":" . $value . ";";
}
$this->settings = $this->settings = explode( ";", $settingsToSave );
update_option( self::SETTING_NAME, $settingsToSave );
}
}
<?php
namespace CommunityProject ;
class ShortcodeController
{
function __construct()
......
<?php
namespace CommunityProject ;
class UserRoleController
{
const STUDENT_ROLE = 'student_role' ;
function set_user_role_permission()
{
// TODO: https://framagit.org/Artefacts/CommunityVoice/issues/61
$this->grant_all_project_permission( get_role( 'author' ) );
$this->grant_all_project_permission( get_role( 'editor' ) );
$this->grant_all_project_permission( get_role( 'administrator' ) );
......@@ -26,7 +31,7 @@ class UserRoleController
function create_student_role()
{
add_role(
'student_role',
self::STUDENT_ROLE,
'Etudiant',
[
'delete_posts' => false,
......
<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="<?php echo $setting['id']?>" style="display:inline-block;min-width:200px"><?php echo $setting['label']?></label>
<input type="<?php echo $setting['type'] ?>" name="<?php echo $setting['id'] ?>" id="<?php echo $setting['id']?>" aria-describedby="<?php $setting['id'].'-description' ?>" class="popular-category" value="<?php echo $this->getSetting( $setting['id'] ) ?>" <?php echo $this->getSetting( $setting['id'] ) === 'checked' ? 'checked' : '' ?>/>
<p id="<?php echo $setting['id'].'-description' ?>" class="description">
<?php echo $setting['description'] ?>
</p>
</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(function(){
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