| Current Path : /home/pressec/www/presselib/wp-content/plugins/presselib-metaboxes/ |
| Current File : /home/pressec/www/presselib/wp-content/plugins/presselib-metaboxes/presselib-metaboxes.php |
<?php
/*
Plugin Name: presselib Metaboxes
Depends: Meta Box
Plugin URI: http://frederic-malaterre.com
Description: Gestions des metaboxes de Presselib
Version: 1.3
Author: Fred Malaterre
Author URI: http://frederic-malaterre.com
License: GPL2
*/
if ( !function_exists('add_action') ) {
header('Status: 403 Forbidden');
header('HTTP/1.1 403 Forbidden');
exit();
}
if ( !class_exists( 'Presselib_Metaboxes' ) ) {
class Presselib_Meta_Box {
public function __construct() {
if ( is_admin() ) {
//add_action( 'load-post.php', array( $this, 'init_metabox' ) );
//add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
add_filter( 'rwmb_meta_boxes', array( $this, 'presselib_register_meta_boxes' ) );
}
}
public function presselib_register_meta_boxes( $meta_boxes )
{
$prefix = 'presselib_';
$meta_boxes[] = array(
'id' => 'metainfos',
'title' => __( 'Infos complémentaires', 'presselib' ),
'post_types' => array( 'post', 'createur' ),
'context' => 'normal',
'priority' => 'high',
'autosave' => true,
'fields' => array(
// SURTITRE
array(
'name' => __( 'Surtitre', 'presselib' ),
'id' => "{$prefix}surtitle",
'type' => 'text',
),
// PUBLIREPORTAGE
array(
'name' => __( 'Publireportage', 'presselib' ),
'id' => "{$prefix}publireportage",
'type' => 'checkbox',
),
// Liaisons
array(
'type' => 'heading',
'name' => __( 'Liez cet article à d\'autres contenus', 'presselib' ),
'id' => 'fake_id', // Not used but needed for plugin
'desc' => __( 'Améliorez la navigation des lecteurs en liant cet articles à d\'autres contenus de Presselib', 'presselib' ),
),
// POSTS
array(
'name' => __( 'Associez d\'autres articles', 'presselib' ),
'id' => "{$prefix}pages",
'type' => 'post',
'post_type' => array( 'post', 'createur' ),
// Field type, either 'select' or 'select_advanced' (default)
'field_type' => 'select_advanced',
'placeholder' => __( 'Choisir un article parmi les 100 derniers', 'presselib' ),
// Query arguments (optional). No settings means get all published posts
'query_args' => array(
'post_status' => 'publish',
'post_type' => 'post',
'posts_per_page' => 100,
),
'clone' => true,
),
// CREATEURS
array(
'name' => __( 'Associez des créateurs', 'presselib' ),
'id' => "{$prefix}createurs",
'type' => 'post',
'post_type' => array( 'post', 'createur' ),
// Field type, either 'select' or 'select_advanced' (default)
'field_type' => 'select_advanced',
'placeholder' => __( 'Choisir un créateur', 'presselib' ),
// Query arguments (optional). No settings means get all published posts
'query_args' => array(
'post_status' => 'publish',
'post_type' => 'createur',
'posts_per_page' => -1,
),
'clone' => true,
),
)
);
return $meta_boxes;
}
/*
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'presselib_metabox',
__( 'Infos supplémentaires', 'presselib' ),
array( $this, 'render_metabox' ),
array('post','createur'),
'normal',
'high'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$presselib_surtitle = get_post_meta( $post->ID, 'presselib_surtitle', true );
// Set default values.
if( empty( $presselib_surtitle ) ) $presselib_surtitle = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="presselib_surtitle" class="presselib_surtitle_label">' . __( 'Surtitre', 'presselib' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="presselib_surtitle" name="presselib_surtitle" class="presselib_surtitle_field large-text" placeholder="' . esc_attr__( '', 'presselib' ) . '" value="' . esc_attr( $presselib_surtitle ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Check if it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Check if it's not a revision.
if ( wp_is_post_revision( $post_id ) )
return;
// Sanitize user input.
$presselib_new_surtitle = isset( $_POST[ 'presselib_surtitle' ] ) ? sanitize_text_field( $_POST[ 'presselib_surtitle' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'presselib_surtitle', $presselib_new_surtitle );
}
*/
function get_MetaboxesPresselib($id, $type) { // renvoie les valeurs
if ( $type == '' )
return "Type est pas admis";
if ( 'surtitle' == $type )
return get_post_meta($id,'presselib_surtitle', true);
if ( 'liaisons' == $type )
return get_post_meta($id,'presselib_pages', true);
if ( 'publireportage' == $type )
return get_post_meta($id,'presselib_publireportage', true);
}
function MetaboxesPresselib($id, $type) {
if ( $id ) {
$res = $this->get_MetaboxesPresselib($id, $type);
if($res && count($res)!=0) {
if(is_array($res) ) {
//echo "tableau";
if($type == "liaisons") { ?>
<h3 style="background: #2a2a2a;color: #ccc;font-size: 1.2rem;padding: 2px 15px; text-transform: none;">La rédaction vous conseille de lire aussi : </h3>
<?php
foreach($res as $k=>$v) {
//echo get_the_post_thumbnail($v,'thumbnail');
//echo get_the_title($v)."<br>";
//the_PresselibMetabox($v,'surtitle') ;
?>
<article <?php post_class('post moyen'); ?>>
<?php if (get_the_post_thumbnail($v,'thumbnail') ) { ?>
<div class="thumbnail">
<?php echo get_the_post_thumbnail($v,'thumbnail'); ?>
</div>
<?php } ?>
<header>
<h2><a href="<?php the_permalink($v);?>"><?php //echo $on_surtitre; ?><?php the_PresselibMetabox($v,'surtitle') ; ?></a></h2>
<?php echo get_the_title($v, sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink($v) ) ), '</a></h1>' ); ?>
<div style="display:none">
<span class="author"><?php echo get_the_author(); ?></span>
<span class="updated" rel="updated">Dernière mise à jour: <time datetime="<?php the_modified_date($v,'Y-m-d g:i'); ?>-0200"><?php the_modified_date($v); ?></time></span>
</div>
</header>
<div class="clearer"><!-- --></div>
<a href="<?php the_permalink($v);?>" class="more-link"><span>En savoir plus</span></a>
</article>
<?php
}
}
} else {
if($type == "publireportage") { return true; } else echo $res;
}
return true;
} else return false;
}
}
}
$Presselib_Metabox = new Presselib_Meta_Box;
function the_PresselibMetabox($id, $type = '', $string = '') {
global $Presselib_Metabox;
return $Presselib_Metabox->MetaboxesPresselib($id, $type);
}
add_filter('manage_post_posts_columns', 'presselib_add_post_columns' );
add_filter('manage_createur_posts_columns', 'presselib_add_post_columns' );
add_action('manage_posts_custom_column', 'presselib_render_post_columns', 10, 2);
// Add to our admin_init function
function presselib_add_post_columns($columns) {
$columns['presselib_surtitle'] = 'Surtitre';
return $columns;
}
// Add to our admin_init function
function presselib_render_post_columns($column_name, $post_id) {
switch ($column_name) {
case 'presselib_surtitle':
// show widget set
$terms = get_post_meta($post_id,'presselib_surtitle', true);
if ( $terms ) echo $terms;
break;
}
}
add_action( 'quick_edit_custom_box', 'display_custom_quickedit_surtitle', 10, 2 );
function display_custom_quickedit_surtitle( $column_name, $post_type ) {
static $printNonce = TRUE;
if ( $printNonce ) {
$printNonce = FALSE;
wp_nonce_field( plugin_basename( __FILE__ ), 'surtitle_edit_nonce' );
}
?>
<fieldset class="inline-edit-col-left inline-edit-surtitle">
<div class="inline-edit-col column-<?php echo $column_name; ?>">
<label class="inline-edit-group">
<?php
switch ( $column_name ) {
case 'presselib_surtitle':
?><span class="title">Surtitre</span>
<span class="input-text-wrap"><input name="presselib_surtitle" style="width: 97%;" /></span><?php
break;
}
?>
</label>
</div>
</fieldset>
<?php
}
add_action( 'save_post', 'save_surtitle_meta' );
function save_surtitle_meta( $post_id ) {
/* in production code, $slug should be set only once in the plugin,
preferably as a class property, rather than in each function that needs it.
*/
$slug = 'surtitle';
/*
if ( $slug !== $_POST['post_type'] ) {
return;
}*/
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}
$_POST += array("{$slug}_edit_nonce" => '');
if ( !wp_verify_nonce( $_POST["{$slug}_edit_nonce"],
plugin_basename( __FILE__ ) ) )
{
return;
}
if ( isset( $_REQUEST['presselib_surtitle'] ) ) {
update_post_meta( $post_id, 'presselib_surtitle', $_REQUEST['presselib_surtitle'] );
}
}
/* load script in the footer */
if ( ! function_exists('presselib_admin_enqueue_scripts') ):
function presselib_admin_enqueue_scripts( $hook ) {
//if ( 'edit.php' === $hook &&
//isset( $_GET['post_type'] ) &&
//'post' === $_GET['post_type'] ) {
wp_enqueue_script( 'my_custom_script', plugins_url('/js/admin_edit.js', __FILE__),
false, null, true );
//}
}
endif;
add_action( 'admin_enqueue_scripts', 'presselib_admin_enqueue_scripts' );
}