Instantly share code, notes, and snippets.
Created
May 25, 2020 09:31
-
Save Glinkfr/64cdb31ce4314575fbe7b84290678ab8 to your computer and use it in GitHub Desktop.
Plugin pour Intégrer NavXT Breadcrumb Plugin à Business Directory Plugin
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 | |
/* | |
Plugin Name: wpbdp navxt integration | |
Plugin URI: https://www.glink.fr | |
Version: 1.0.0 | |
Author: L. Perderiset and Business Directory Team | |
Description: Better integration of NavXT Breadcrumb Plugin with Business Directory Plugin | |
Author URI: https://www.glink.fr | |
*/ | |
// supprime toutes les actions de BD sur le plugin NavXT | |
add_action( 'wpbdp_loaded', function(){ | |
remove_all_actions('bcn_before_fill'); | |
remove_all_actions('bcn_after_fill'); | |
}); | |
// la nouvelle class | |
class WPBDP_NavXT_Integration_glink{ | |
private $state = array(); | |
private $doing = ''; | |
// les Hooks d'action de la class sur NavXT Plugin | |
function __construct() { | |
add_action( 'bcn_before_fill', array( &$this, 'prepare_state' ) ); | |
add_action( 'bcn_after_fill', array( &$this, 'restore_state' ) ); | |
} | |
// Détermine sur quelle vue ou page de l'annuaire on se trouve | |
// En fonction de la vue on met en route différentes fonctions | |
function prepare_state( $trail ) { | |
if ( $this->doing ) | |
return; | |
global $wpbdp; | |
$action = wpbdp_current_view(); | |
switch ( $action ) { | |
case 'show_listing': | |
$this->doing = 'listing'; | |
break; | |
case 'show_category': | |
$this->doing = 'category'; | |
break; | |
case 'show_tag': | |
$this->doing = 'tag'; | |
break; | |
case 'edit_listing': | |
$this->doing = 'edit'; | |
break; | |
case 'submit_listing': | |
$this->doing = 'submit'; | |
break; | |
case 'search': | |
$this->doing = 'search'; | |
break; | |
default: | |
$this->doing = ''; | |
} | |
if ( ! $this->doing ) | |
return; | |
if ( method_exists( $this, 'before_' . $this->doing ) ) | |
call_user_func( array( $this, 'before_' . $this->doing ), $trail ); | |
} | |
// sert à afficher en fonction de la page ou de la vue de l'annuaire la suite du fil | |
function restore_state( $trail ) { | |
if ( ! $this->doing ) | |
return; | |
if ( method_exists( $this, 'after_' . $this->doing ) ) | |
call_user_func( array( $this, 'after_' . $this->doing ), $trail ); | |
$this->doing = ''; | |
} | |
// affiche dans le fil la page index de l'annuaire | |
function main_page_breadcrumb( $trail ) { | |
$last = $trail->trail[ count( $trail->trail ) - 1 ]; | |
if ( method_exists( $last, 'get_types' ) ) { | |
$types = $last->get_types(); | |
} else if ( $last ) { | |
$vars = get_object_vars( $last ); | |
$types = (array) ( isset( $vars['type'] ) ? $vars['type'] : array() ); | |
} else { | |
$types = array(); | |
} | |
if ( in_array( 'home', $types, true ) ) { | |
array_pop( $trail->trail ); | |
} | |
// une premiere modification pour que le lien soit présent sur l'index j'ajoute true | |
$trail->add( new bcn_breadcrumb( get_the_title( wpbdp_get_page_id() ), | |
'', | |
array('post post-page'), | |
wpbdp_get_page_link(), | |
wpbdp_get_page_id(), | |
true ) ); | |
// si l'option afficher la page d'accueil dans la hierarchie est cochée on l'affiche | |
// on utilise le template home grâce à $trail->opt['Hhome_template'] | |
if ( $trail->opt['bhome_display'] ){ | |
$site_name = get_option('blogname'); | |
$trail->add(new bcn_breadcrumb($site_name, $trail->opt['Hhome_template'], array('home'), get_home_url(), null, true)); | |
} | |
} | |
// affiche l'annonce dans la hierrachie | |
function before_listing( $trail ) { | |
$listing_id = $this->get_current_listing_id(); | |
if ( ! $listing_id ) | |
return; | |
$this->state['post'] = $GLOBALS['post']; | |
$GLOBALS['post'] = get_post( $listing_id ); | |
} | |
// determine l'id ou le slug de l'annonce | |
private function get_current_listing_id() { | |
$id_or_slug = get_query_var( 'listing' ); | |
if ( ! $id_or_slug && isset( $_GET['listing'] ) ) { | |
$id_or_slug = $_GET['listing']; | |
} | |
if ( ! $id_or_slug ) { | |
$id_or_slug = get_query_var( 'id' ); | |
} | |
if ( ! $id_or_slug && isset( $_GET['id'] ) ) { | |
$id_or_slug = $_GET['id']; | |
} | |
if ( ! $id_or_slug ) { | |
$id_or_slug = get_query_var( '_' . wpbdp_get_option( 'permalinks-directory-slug' ) ); | |
} | |
if ( $id_or_slug ) { | |
$listing_id = wpbdp_get_post_by_id_or_slug( $id_or_slug, 'id', 'id' ); | |
} else { | |
$listing_id = get_queried_object_id(); | |
} | |
return $listing_id; | |
} | |
// affiche l'index de annuaire dans la hierarchie pour les pages annonce | |
function after_listing( $trail ) { | |
$GLOBALS['post'] = $this->state['post']; | |
unset( $this->state['post'] ); | |
$this->main_page_breadcrumb( $trail ); | |
} | |
// affiche la categorie ou sous categorie | |
function before_category( $trail ) { | |
$term = _wpbpd_current_category(); | |
if ( ! $term ) { | |
return; | |
} | |
global $wp_query; | |
$this->state['queried'] = $wp_query->get_queried_object(); | |
$wp_query->is_singular = false; | |
$wp_query->queried_object = $term; | |
} | |
// affiche l'index de annuaire dans la hierarchie pour les pages categorie | |
function after_category( $trail ) { | |
if ( ! $this->state['queried'] ) { | |
return; | |
} | |
global $wp_query; | |
$wp_query->queried_object = $this->state['queried']; | |
$wp_query->is_singular = true; | |
unset( $this->state['queried'] ); | |
$this->main_page_breadcrumb( $trail ); | |
} | |
// affiche les tags | |
function before_tag( $trail ) { | |
$tag = get_term_by( 'slug', get_query_var( 'tag' ), WPBDP_TAGS_TAX ); | |
if ( ! $tag ) | |
return; | |
global $wp_query; | |
$term = get_term( $category_id, WPBDP_CATEGORY_TAX ); | |
$this->state['queried'] = $wp_query->get_queried_object(); | |
$wp_query->is_singular = false; | |
$wp_query->queried_object = $tag; | |
} | |
// affiche l'index de annuaire dans la hierarchie pour les pages tag | |
function after_tag( $trail ) { | |
$this->after_category( $trail ); | |
} | |
// affiche la page de soumission d'annonce dans la hierarchie comme page en cours | |
// grâce à $trail->opt['bcurrent_item_linked'] on sait si on doit afficher le lien | |
function before_submit( $trail ) { | |
$trail->add( new bcn_breadcrumb( _x( 'Submit Listing', 'navxt', 'WPBDM' ), NULL, array('post post-page current-item'), '', NULL, $trail->opt['bcurrent_item_linked'] ) ); | |
} | |
// affiche la page d'édition d'annonce dans la hierarchie comme page en cours | |
// grâce à $trail->opt['bcurrent_item_linked'] on sait si on doit afficher le lien | |
function before_edit( $trail ) { | |
$trail->add( new bcn_breadcrumb( _x( 'Edit Listing', 'navxt', 'WPBDM' ), NULL, array('post post-page current-item'), '', NULL, $trail->opt['bcurrent_item_linked'] ) ); | |
} | |
// affiche la page de recherche d'annonce dans la hierarchie comme page en cours | |
// grâce à $trail->opt['bcurrent_item_linked'] on sait si on doit afficher le lien | |
function before_search( $trail) { | |
$trail->add( new bcn_breadcrumb( _x( 'Search', 'navxt', 'WPBDM' ), NULL, array('post post-page current-item'), '', NULL, $trail->opt['bcurrent_item_linked'] ) ); | |
} | |
} | |
// ajoute la nouvelle class au plugin BD APRES la suppression grâce à la priorité 999 | |
add_action('wpbdp_loaded', function(){new WPBDP_NavXT_Integration_glink();}, 999); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment