-
-
Save marcboivin/860392 to your computer and use it in GitHub Desktop.
Un example de controlleur
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Idee | |
{ | |
/** | |
* Initializes this class. | |
*/ | |
public static function static_init() | |
{ | |
add_action( | |
'add_meta_boxes', | |
Ns::qualify_method_name(__CLASS__, 'add_meta_boxes')); | |
add_action( | |
'save_post', | |
Ns::qualify_method_name(__CLASS__, 'prevent_non_idea_posts')); | |
add_action( | |
'save_post', | |
Ns::qualify_method_name(__CLASS__, 'save_metadata')); | |
} | |
// Public methods | |
/** | |
* Adds idea metadata options to the idea editing interface. | |
*/ | |
public static function add_meta_boxes() | |
{ | |
add_meta_box( | |
__CLASS__ . '_display_options', | |
'Options d\'affichage de l\'idée', | |
Ns::qualify_method_name( | |
__CLASS__, | |
'print_display_options_metabox'), | |
'idee'); | |
} | |
/** | |
* When attached to the WordPress 'save_post' event, prevents posts that | |
* aren't ideas from being written by users without the correct rights. | |
*/ | |
public static function prevent_non_idea_posts($post_id) | |
{ | |
$post_id = intval($post_id); | |
$post = get_post($post_id); | |
if ($post->post_type !== 'idee') | |
{ | |
// Détermine si l'usager courant est au moins un 'Author', puisque | |
// trouver le nom exact du rôle de l'usager semble être compliqué et | |
// pas vraiment supporté par WordPress. | |
$current_user_at_least_author | |
= current_user_can('publish_posts'); | |
if (!$current_user_at_least_author) | |
{ | |
// Idéalement on afficherait le message dans l'interface WordPress. | |
die(' | |
<?xml version="1.0" encoding="UTF-8" ?> | |
<html> | |
<head> | |
<meta | |
http-equiv="Content-Type" | |
content="text/html;charset=UTF-8"> | |
</head> | |
<body> | |
Vous êtes seulement autorisé(e) à écrire des idées. | |
Vous n\'êtes pas autorisé(e) à écrire des posts de type | |
"' . esc_html($post->post_type) . '". | |
</body> | |
<html> | |
'); | |
} | |
} | |
} | |
/** | |
* Saves metadata options when an idea is saved. | |
*/ | |
public static function save_metadata($post_id) | |
{ | |
// Verify that the post being saved is an idea. | |
if ('idee' !== $_POST['post_type']) | |
{ | |
return $post_id; | |
} | |
// Verify this came from the our screen and with proper authorization, | |
// because save_post can be triggered at other times. | |
if (!wp_verify_nonce($_POST[self::get_NONCE_FIELD_NAME()], __CLASS__)) | |
{ | |
return $post_id; | |
} | |
// Verify if this is an auto save routine. If it is our form has not | |
// been submitted, so we dont want to do anything. | |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) | |
{ | |
return $post_id; | |
} | |
// Check permissions. | |
if (!current_user_can('edit_post', $post_id)) | |
{ | |
return $post_id; | |
} | |
// OK, we're authenticated: we need to find and save the data. | |
$is_title_displayed | |
= Convert::boolean_to_string( | |
'on' === (string)$_POST[__CLASS__ . '_is_title_displayed']); | |
update_post_meta( | |
$post_id, | |
__CLASS__ . '_is_title_displayed', | |
$is_title_displayed); | |
return $is_title_displayed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment