| Current Path : /home/pressec/www/presselib/wp-content/plugins/presselib-entreprises/ |
| Current File : /home/pressec/www/presselib/wp-content/plugins/presselib-entreprises/presselib-entreprises.php |
<?php
/*
Plugin Name: Entreprises d'Ici
Plugin URI: http://simtic.com
Description: Articles de type Entreprises d'Ici, réalisé pour Presselib
Version: 0.3
Author: Fred Malaterre
Author URI: http://frederic-malaterre.com
License: GPL2
*/
/**
* Example for use outside the loop:
* <?php the_PresselibEntreprise($post->ID); ?>
* @param $id Integer - Post-ID
* @param $type String - nom-entreprise, additional-info, listdata (default is ''-empty)
*
* Example: <?php the_PresselibEntreprise($post->ID, 'nom-entreprise'); ?>
*/
//avoid direct calls to this file, because now WP core and framework has been used
if ( !function_exists('add_action') ) {
header('Status: 403 Forbidden');
header('HTTP/1.1 403 Forbidden');
exit();
}
if ( function_exists('add_action') ) {
//WordPress definitions
if ( !defined('WP_CONTENT_URL') )
define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
if ( !defined('WP_CONTENT_DIR') )
define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
if ( !defined('WP_PLUGIN_URL') )
define('WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins');
if ( !defined('WP_PLUGIN_DIR') )
define('WP_PLUGIN_DIR', WP_CONTENT_DIR.'/plugins');
if ( !defined('PLUGINDIR') )
define( 'PLUGINDIR', 'wp-content/plugins' ); // Relative to ABSPATH. For back compat.
if ( !defined('WP_LANG_DIR') )
define('WP_LANG_DIR', WP_CONTENT_DIR . '/languages');
// plugin definitions
define( 'FB_DT_BASENAME', plugin_basename(__FILE__) ); // remplacer le nom de la constante par FM_PE_XXXX
define( 'FB_DT_BASEDIR', dirname( plugin_basename(__FILE__) ) );
define( 'FB_DT_TEXTDOMAIN', 'presselib-entreprise' );
}
if ( !class_exists( 'Presselib_Entreprises' ) ) {
class Presselib_Entreprises
{
// constructor
public function __construct()
{
add_action("init", array($this, "entreprises_post_type"),0);
add_filter("single_template", array($this, "get_custom_entreprise_template"));
add_filter("category_template", array($this, "get_custom_cat_etp_template"));
add_filter("archive_template", array($this, "get_custom_archive_etp_template"));
//add_action( 'init', array($this, "entreprise_taxonomy")); // TAXONOMIES Pour faire lien entre article et Post_entreprise
//add_action("init", array($this, "category_entreprise_init")); // TAXONOMIES
//add_action("init", array($this, "type_entreprise_init")); TAXO
add_action('add_meta_boxes', array($this, "init_metaboxe_entreprise")); // METABOXES
add_action('save_post',array($this, 'save_metaboxes_entreprise')); // METABOXES
}
// active for multilanguage
function PresseLib_Entreprise() {
if ( function_exists('load_plugin_textdomain') )
load_plugin_textdomain( FB_DT_TEXTDOMAIN, false, dirname( FB_DT_BASENAME ) . '/languages' );
}
// Register Custom Post Type
function entreprises_post_type() {
$labels = array(
'name' => _x( 'Entreprises', 'Post Type General Name', 'Presselib' ),
'singular_name' => _x( 'Entreprise', 'Post Type Singular Name', 'Presselib' ),
'menu_name' => __( 'Entreprises d\'Ici', 'Presselib' ),
'parent_item_colon' => __( 'Groupe :', 'Presselib' ),
'all_items' => __( 'Toutes les entreprises', 'Presselib' ),
'view_item' => __( 'Voir l\'entreprise', 'Presselib' ),
'add_new_item' => __( 'Ajouter une nouvelle entreprise', 'Presselib' ),
'add_new' => __( 'Nouvelle entreprise', 'Presselib' ),
'edit_item' => __( 'Modifier l\'entreprise', 'Presselib' ),
'update_item' => __( 'Mettre à jour l\'entreprise', 'Presselib' ),
'search_items' => __( 'Recherche d\'entreprises', 'Presselib' ),
'not_found' => __( 'Aucune entreprise n\'a été trouvée', 'Presselib' ),
'not_found_in_trash' => __( 'Aucune entreprise dans la corbeille', 'Presselib' ),
);
$rewrite = array(
'slug' => 'entreprise',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'entreprise', 'Presselib' ),
'description' => __( 'Article de présentation d\'une entreprise', 'Presselib' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'revisions', 'custom-fields', 'post-formats', ),
'taxonomies' => array( 'post_tag', 'category', 'modele' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 6,
'menu_icon' => 'dashicons-store',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'post',
);
register_post_type( 'entreprise', $args );
}
function get_custom_entreprise_template($single_template) {
global $post;
if ($post->post_type == 'entreprise') {
$single_template = dirname( __FILE__ ) . '/template/single-entreprise.php';
}
return $single_template;
}
function get_custom_cat_etp_template($category_template) {
global $post;
if ($post->post_type == 'entreprise') {
$category_template = dirname( __FILE__ ) . '/template/category-cat_etp.php';
}
return $category_template;
}
function get_custom_archive_etp_template($archive_template) {
global $post;
if ($post->post_type == 'entreprise') {
$archive_template = dirname( __FILE__ ) . '/template/archive-cat_etp.php';
}
return $archive_template;
}
// Register Custom MetaBoxes
function init_metaboxe_entreprise($post_type){
remove_meta_box('postcustom', 'entreprise', 'normal');
//on utilise la fonction add_metabox() pour initialiser une metabox
$post_types = array('entreprise'); //limite la meta box to certain post types
if ( in_array( $post_type, $post_types )) {
add_meta_box(
'meta_entreprise'
,'Détails de l\'Entreprise'
,array( $this, 'meta_entreprise_render' )
,$post_type ,'normal','high'
);
}
}
// load post_meta_data
function load_post_meta($id) {
return get_post_meta($id,'meta_entreprise', true);
}
function meta_entreprise_render( $post, $metabox ){
// on récupère la valeur actuelle pour la mettre dans le champ
$value = $this->load_post_meta($post->ID);
?>
<table id="pe-page-definition" width="100%" cellspacing="5px">
<tr valign="top">
<td style="width:20%;"><label for="pe-nom">Nom de l'entreprise :</label></td>
<td><input type="text" id="pe-nom" name="pe-nom" class="nom form-input-tip" size="16" autocomplete="off" value="<?php if(isset($value['nom'])) echo $value['nom']; ?>" tabindex="6" style="width:99.5%"/></td>
</tr>
<tr valign="top">
<td style="width:20%;"><label for="pe-adresse">Adresse :</label></td>
<td><input type="text" id="pe-adresse" name="pe-adresse" class="adresse form-input-tip" size="16" autocomplete="off" value="<?php if (isset($value['adresse'])) echo $value['adresse']; ?>" tabindex="6" style="width:99.5%"/></td>
</tr>
<tr valign="top">
<td style="width:20%;"><label for="pe-web">Site web :</label></td>
<td><input type="text" id="pe-web" name="pe-web" class="web form-input-tip" size="16" autocomplete="off" value="<?php if(isset($value['web'])) echo $value['web']; ?>" tabindex="6" style="width:99.5%"/></td>
</tr>
<tr valign="top">
<td style="width:20%;"><label for="pe-phone">Téléphone :</label></td>
<td><input type="text" id="pe-phone" name="pe-phone" class="phone form-input-tip" size="16" autocomplete="off" value="<?php if(isset($value['phone'])) echo $value['phone']; ?>" tabindex="6" style="width:99.5%"/></td>
</tr>
</table>
<?php
}
function save_metaboxes_entreprise( $post_ID ){
if ( isset($_POST['pe-nom']) && $_POST['pe-nom'] != '' )
$this->data['nom'] = esc_attr( $_POST['pe-nom'] );
if ( isset($_POST['pe-adresse']) && $_POST['pe-adresse'] != '' )
$this->data['adresse'] = esc_attr( $_POST['pe-adresse'] );
if ( isset($_POST['pe-web']) && $_POST['pe-web'] != '' )
$this->data['web'] = esc_attr( $_POST['pe-web'] );
if ( isset($_POST['pe-phone']) && $_POST['pe-phone'] != '' )
$this->data['phone'] = esc_attr( $_POST['pe-phone'] );
if ( isset($this->data) && $this->data != '' )
update_post_meta($post_ID, 'meta_entreprise', $this->data);
}
function get_MetaboxesEntreprise($id, $type, $value) { // renvoie les valeurs
if (!$value)
return false;
if ( $type == '' )
return false;
if ( 'nom' == $type && '' != $value['nom'] )
return $value['nom'];
if ( 'adresse' == $type && '' != $value['adresse'] )
return $value['adresse'];
if ( 'web' == $type && '' != $value['web'] )
return $value['web'];
if ( 'phone' == $type && '' != $value['phone'] )
return $value['phone'];
// j'ai besoin des articles dont la taxonomie entreprise_taxo est le meme que nom d'entreprise.
if ( 'articles' == $type ) {
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'entreprise_taxo',
'field' => 'slug',
'terms' => $value['nom'],
),
),
'post_status' => 'publish',
//PAR MOTEUR DE RECHERCHE 's' => $value['nom'],
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
// $the_query->the_post();
// $articles[]=$the_query;
while ( $the_query->have_posts() ) {
$the_query->the_post();
$articles[get_the_ID()]["link"]=get_the_ID();
$articles[get_the_ID()]["title"]=get_the_title();
$articles[get_the_ID()]["date"]=get_the_date();
if(get_the_post_thumbnail()) $articles[get_the_ID()]["thumb"]=get_the_post_thumbnail(get_the_ID(),array(60,60));
$terms = get_the_terms(get_the_ID(), "surtitre");
$surtitre="";
if($terms && !is_wp_error($terms)){
foreach($terms as $term){
$articles[get_the_ID()]["surtitre"] = $term->name;
break;
}
}
//$articles = print_r(get_object_vars ($the_query));
//get_template_part( 'content', get_post_format() );
// End the loop.
}
} else {
return false;
}
wp_reset_postdata();
return $articles;
}
}
function MetaboxesEntreprise($id, $type, $string) {
if ( $id ) {
$value = $this->load_post_meta($id);
$res = $this->get_MetaboxesEntreprise($id, $type, $value);
if(is_array($res)) {
//print_r($res);
?>
<h2><?php echo $value['nom']; ?> dans Presselib...</h2>
<div class="etp_articles_lies">
<?php foreach($res as $k=>$v) { ?>
<li class="collection_item">
<div class="vignette">
<?php if(isset($v['thumb'])) { ?><a href="<?php echo get_permalink($v['link']); ?>"><?php echo $v['thumb']; ?></a><?php } ?>
<span><?php echo $v['date']; ?></span>
</div>
<div class="titres">
<?php if(isset($v['surtitre'])) { ?>
<div class="surtitre"><a href="<?php echo get_permalink($v['link']); ?>"><?php echo $v['surtitre']; ?></a></div>
<?php } ?>
<div class="titre"><a href="<?php echo get_permalink($v['link']); ?>"><?php echo $v['title']; ?></a></div>
</div>
</li>
<?php } ?>
</div>
<?php
} else {
if($res) echo $res;
}
}
}
// Register Custom Taxonomy
function entreprise_taxonomy() { //Permet de faire le lien entre des articles et une entreprise (du moment que le nom d'entreprise = valeur de la taxonomie
$labels = array(
'name' => _x( 'Entreprises liées', 'Taxonomy General Name', 'Presselib' ),
'singular_name' => _x( 'Entreprise', 'Taxonomy Singular Name', 'Presselib' ),
'menu_name' => __( 'Lien d\'entreprise', 'Presselib' ),
'all_items' => __( 'Tous les liens d\'entreprise', 'Presselib' ),
'parent_item' => __( '', 'Presselib' ),
'parent_item_colon' => __( '', 'Presselib' ),
'new_item_name' => __( 'Nouveau lien d\'entreprise', 'Presselib' ),
'add_new_item' => __( 'Ajouter un nouveau lien d\'entreprise', 'Presselib' ),
'edit_item' => __( 'Modifier un lien d\'entreprise', 'Presselib' ),
'update_item' => __( 'Mettre à jour un lien d\'entreprise', 'Presselib' ),
'separate_items_with_commas' => __( 'Vous pouvez ajouter plusieurs noms d\'entreprises, séparez leurs noms par des virgules', 'Presselib' ),
'search_items' => __( 'Recherche de lien d\'entreprise', 'Presselib' ),
'add_or_remove_items' => __( 'Ajouter ou supprimer un lien d\'entreprise', 'Presselib' ),
'choose_from_most_used' => __( 'Rechercher parmi les plus utilisées', 'Presselib' ),
'not_found' => __( 'Introuvable', 'Presselib' ),
);
$rewrite = array(
'slug' => 'etp',
'with_front' => true,
'hierarchical' => false,
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => true,
'rewrite' => $rewrite,
);
register_taxonomy( 'entreprise_taxo', array( 'post', ' page', ' attachment', 'createur' ), $args );
}
function category_entreprise_init() {
$labels = array(
'name' => _x( 'Catégories d\'entreprise', 'Taxonomy General Name', 'Presselib' ),
'singular_name' => _x( 'Catégorie d\'entreprise', 'Taxonomy Singular Name', 'Presselib' ),
'menu_name' => __( 'Catégorie d\'entreprise', 'Presselib' ),
'all_items' => __( 'Toutes les catégories d\'entreprise', 'Presselib' ),
'parent_item' => __( 'Catégorie d\'entreprise mère', 'Presselib' ),
'parent_item_colon' => __( 'Catégorie d\'entreprise:', 'Presselib' ),
'new_item_name' => __( 'Nouvelle catégorie d\'entreprise', 'Presselib' ),
'add_new_item' => __( 'Ajouter une nouvelle catégorie d\'entreprise', 'Presselib' ),
'edit_item' => __( 'Modifier la catégorie d\'entreprise', 'Presselib' ),
'update_item' => __( 'Mettre à jour la catégorie d\'entreprise', 'Presselib' ),
'separate_items_with_commas' => __( '', 'Presselib' ),
'search_items' => __( '', 'Presselib' ),
'add_or_remove_items' => __( '', 'Presselib' ),
'choose_from_most_used' => __( '', 'Presselib' ),
'not_found' => __( 'Not Found', 'Presselib' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => false,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
);
register_taxonomy( 'cat_etp', array( 'entreprise' ), $args );
}
}
$Presselib_Entreprises = new Presselib_Entreprises();
// appels dans les template
function the_PresselibEntreprise($id, $type = '', $string = '') {
global $Presselib_Entreprises;
$Presselib_Entreprises->MetaboxesEntreprise($id, $type, $string);
}
}
?>