_pluginname = get_class($this);
$this->_pluginfile = plugin_basename(__FILE__);
$this->_plugindir = '/' . PLUGINDIR . '/' . str_replace('\\', '/', dirname(plugin_basename(__FILE__)));
$this->_langdir = $this->_plugindir . '/lang';
$this->_jsdir = $GLOBALS['wpbase'] . $this->_plugindir . '/js';
$this->_cssdir = get_bloginfo('wpurl') . $GLOBALS['wpbase'] . $this->_plugindir . '/css';
$this->_shortcode = strtolower($this->_pluginname) . '-view';
$this->register();
add_action('init', array($this, 'run'));
}
protected function register() {
load_plugin_textdomain($this->_pluginname, null, $this->_langdir);
if (is_admin()) {
register_activation_hook($this->_pluginfile, array($this, 'activate'));
register_deactivation_hook($this->_pluginfile, array($this, 'deactivate'));
if (function_exists('register_uninstall_hook')) {
register_uninstall_hook($this->_pluginfile, array($this, 'uninstall'));
}
} else {
add_shortcode($this->_shortcode, array($this, 'page'));
}
}
public function activate() {
$post = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => get_user_option('id'),
'post_content' => '['. $this->_shortcode .']',
'post_name' => 'viadeo',
'post_status' => 'publish',
'post_title' => 'Viadéo',
'post_type' => 'page'
);
$page_ID = wp_insert_post($post);
$shortcodes = get_option('pages_shortcodes');
$shortcodes[$this->_shortcode] = $page_ID;
update_option('pages_shortcodes', $shortcodes);
add_option($this->_pluginname . self::KEY_PROFILES, '');
}
public function deactivate() {
$shortcodes = get_option('pages_shortcodes');
wp_delete_post($shortcodes[$this->_shortcode]);
unset($shortcodes[$this->_shortcode]);
update_option('pages_shortcodes', $shortcodes);
}
public function uninstall() {
delete_option($this->_pluginname . self::KEY_PROFILES);
}
public function run() {
if (is_admin()) {
add_action('wp_ajax_viadeo_add', array($this, 'ajax_add'));
add_action('wp_ajax_viadeo_delete', array($this, 'ajax_delete'));
add_action('wp_ajax_viadeo_update', array($this, 'ajax_update'));
add_action('wp_ajax_viadeo_list', array($this, 'ajax_list'));
add_action('admin_menu', array($this, 'menu'));
if (isset($_GET)
&& isset($_GET['page'])
&& $_GET['page'] == $this->_pluginname) {
add_thickbox();
wp_enqueue_script($this->_pluginname . '-js', $this->_jsdir . '/' . $this->_pluginname . 'Admin.js', array(), false);
wp_enqueue_style($this->_pluginname . '-css', $this->_cssdir . '/' . $this->_pluginname . 'Admin.css', array(), false, 'screen');
add_action('admin_head', array($this, 'head'));
add_filter('contextual_help', array($this, 'help'));
}
} else if ($post_ID == $page_ID) {
wp_enqueue_script('jquery');
wp_enqueue_script($this->_pluginname.'-js', $this->_jsdir . '/' . $this->_pluginname . 'Page.js', array(), false);
wp_enqueue_style($this->_pluginname . '-css', $this->_cssdir . '/' . $this->_pluginname . 'Page.css', array(), false, 'screen');
add_action('wp_head', array($this, 'head'));
}
}
public function head() {
$shortcodes = get_option('pages_shortcodes');
$page_ID = $shortcodes[$this->_shortcode];
$html = '';
echo $html;
}
public function menu() {
add_submenu_page('plugins.php',
__('Viadéo', $this->__pluginname),
__('Viadéo', $this->__pluginname),
'edit_users',
$this->_pluginname,
array($this, 'control'));
}
public function help($context = '') {
global $plugin_page;
$help = '';
if (strlen($plugin_page) > 1) {
$folder = substr($plugin_page,0,strrpos($plugin_page,'/')+1);
$racine = '../' . $folder . 'doc/' . $this->_pluginname . '-';
$fileDoc = $racine . WPLANG .'.html';
$fileDocFr = $racine . 'fr_FR.html';
if (file_exists($fileDoc)) {
$help .= file_get_contents($fileDoc);
} else if (file_exists($fileDocFr)) {
$help .= file_get_contents($fileDocFr);
}
}
$help .= $context;
return $help;
}
public function control() {
$html = '
';
$html .= '
';
$html .= '
' . __('Configuration Viadéo', $this->pluginname) . '
';
$html .= '
';
$html .= '
' . __('Consultez l´aide contextuelle concernant la documentation de ce plugin.', $this->_pluginname) . '';
$html .= '
';
$html .= '
';
$html .= '
';
$html .= '
';
$html .= '
';
$html .= '
';
echo $html;
}
public function page() {
$html = '';
$html .= '';
$html .= '' . __('La fiche du profil est affichée dans une fenêtre différente pour éviter tout conflit d´autorité!', $this->_pluginname) . '
';
$html .= '
';
$html .= '';
$profiles = get_option($this->_pluginname . self::KEY_PROFILES);
foreach ($profiles as $profile) {
$html .= '
';
$html .= '' . $profile['name'] . '';
$html .= '';
$html .= '' . $profile['comment'] . '
';
$html .= '';
}
$html .= '
';
return $html;
}
public function ajax_add() {
if (isset($_POST)
&& !empty($_POST)) {
if (!isset($_POST['name'])
|| empty($_POST['name'])
|| !$this->validateName($_POST['name'])) {
header("Status: 400 Bad Request", true, 400);
die(sprintf(__('Paramètre: %s invalide!', $this->_pluginname), 'name'));
} else if (!isset($_POST['url'])
|| empty($_POST['url'])
|| !$this->validateURL($_POST['url'])) {
header("Status: 400 Bad Request", true, 400);
die(sprintf(__('Paramètre: %s invalide!', $this->_pluginname), 'url'));
} else {
if (isset($_POST['comment'])
&& !empty($_POST['comment'])
&& !$this->validateComment($_POST['comment'])) {
header("Status: 400 Bad Request", true, 400);
die(sprintf(__('Paramètre: %s invalide!', $this->_pluginname), 'comment'));
} else {
$profile = array(
'id' => time(),
'name' => $_POST['name'],
'url' => $_POST['url'],
'comment' => $_POST['comment']);
$profiles = get_option($this->_pluginname . self::KEY_PROFILES);
$exists = false;
if (!empty($profiles)) {
foreach ($profiles as $p) {
if ($profile['id'] == $p['id']
|| $profile['name'] == $p['name']) {
header("Status: 400 Bad Request", true, 400);
die(__('Ce profil semble déjà exister, veuillez vérifier!', $this->_pluginname));
$exists = true;
break;
}
}
} else {
$profiles = array();
}
if ($exists == false) {
array_push($profiles, $profile);
update_option($this->_pluginname . self::KEY_PROFILES, $profiles);
echo sprintf(__('%s ajouté avec succès', $this->_pluginname), $_POST['name']);
}
}
}
} else {
header("Status: 400 Bad Request", true, 400);
die(__('Paramètres invalides!', $this->_pluginname));
}
}
public function ajax_delete() {
if (isset($_POST)
&& !empty($_POST)) {
if (!isset($_POST['ids'])
|| empty($_POST['ids'])) {
header("Status: 400 Bad Request", true, 400);
die(sprintf(__('Paramètre: %s invalide!', $this->_pluginname), 'ids'));
} else {
$ids = explode(',', $_POST['ids']);
$profiles = get_option($this->_pluginname . self::KEY_PROFILES);
$updates = array();
foreach ($profiles as $profile) {
if (!in_array($profile['id'], $ids)) {
array_push($updates, $profile);
}
}
update_option($this->_pluginname . self::KEY_PROFILES, $updates);
echo __('Liste des profils mise à jour avec succès', $this->_pluginname);
}
} else {
header("Status: 400 Bad Request", true, 400);
die(__('Paramètres invalides!', $this->_pluginname));
}
}
public function ajax_update() {
if (isset($_POST)
|| !empty($_POST)) {
if (!isset($_POST['id'])
|| empty($_POST['id'])) {
header("Status: 400 Bad Request", true, 400);
die(sprintf(__('Paramètre: %s invalide!', $this->_pluginname), 'id'));
} else if (!isset($_POST['name'])
|| empty($_POST['name'])
|| !$this->validateName($_POST['name'])) {
header("Status: 400 Bad Request", true, 400);
die(sprintf(__('Paramètre: %s invalide!', $this->_pluginname), 'name'));
} else if (!isset($_POST['url'])
|| empty($_POST['url'])
|| !$this->validateURL($_POST['url'])) {
header("Status: 400 Bad Request", true, 400);
die(sprintf(__('Paramètre: %s invalide!', $this->_pluginname), 'url'));
} else {
if (isset($_POST['comment'])
&& !empty($_POST['comment'])
&& !$this->validateComment($_POST['comment'])) {
header("Status: 400 Bad Request", true, 400);
die(sprintf(__('Paramètre: %s invalide!', $this->_pluginname), 'comment'));
} else {
$profile = array(
'id' => $_POST['id'],
'name' => $_POST['name'],
'url' => $_POST['url'],
'comment' => $_POST['comment']);
$profiles = get_option($this->_pluginname . self::KEY_PROFILES);
$updates = array();
if (empty($profiles)) {
header("Status: 400 Bad Request", true, 400);
die(__('Aucun profil, veuillez le créer!', $this->_pluginname));
} else {
foreach ($profiles as $p) {
if ($p['id'] == $profile['id']) {
array_push($updates, $profile);
} else {
array_push($updates, $p);
}
}
update_option($this->_pluginname . self::KEY_PROFILES, $updates);
echo sprintf(__('%s mise à jour avec succès!', $this->_pluginname), $_POST['name']);
}
}
}
} else {
header("Status: 400 Bad Request", true, 400);
die(__('Paramètres invalides!', $this->_pluginname));
}
}
public function ajax_list() {
$profiles = get_option($this->_pluginname . self::KEY_PROFILES);
sort($profiles, SORT_STRING);
header('Content-type: application/json');
echo json_encode($profiles);
}
private function validateName($name) {
$pattern = '/^([a-zA-Z \-\']{1,45})$/';
return $this->validate($pattern, trim($name));
}
private function validateURL($url) {
$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
return $this->validate($pattern, $url);
}
private function validateComment($comment) {
$pattern = '/^([a-zA-Z0-9 \-\',:\.éèêëàçùûüôöîï@]{1,500})$/mesi';
return $this->validate($pattern, trim($comment));
}
private function validate($pattern, $value) {
return preg_match($pattern, $value);
}
}
new PluginViadeo();
?>