Last active
March 13, 2019 16:16
-
-
Save christianwach/1c53334c7529dc642556955fbeb74748 to your computer and use it in GitHub Desktop.
Removes the CiviCRM 5.12.x menu from the front-end in WordPress
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: CiviCRM Remove KAM | |
Description: Removes CiviCRM's menu on the front-end. | |
Author: Christian Wach | |
Version: 0.1 | |
Author URI: http://haystack.co.uk | |
Depends: CiviCRM | |
-------------------------------------------------------------------------------- | |
*/ | |
/** | |
* CiviCRM Remove KAM class. | |
* | |
* A class for encapsulating plugin functionality. | |
* | |
* @since 0.1 | |
*/ | |
class CiviCRM_Remove_KAM { | |
/** | |
* Initialise this object. | |
* | |
* @since 0.1 | |
*/ | |
public function __construct() { | |
// Bail if CiviCRM plugin is not present. | |
if ( ! function_exists( 'civi_wp' ) ) return; | |
// Finally, register hooks. | |
$this->register_hooks(); | |
} | |
/** | |
* Check if CiviCRM is initialised. | |
* | |
* @since 0.1 | |
* | |
* @return bool True if CiviCRM initialised, false otherwise. | |
*/ | |
public function is_civicrm_initialised() { | |
// Init only when CiviCRM is fully installed. | |
if ( ! defined( 'CIVICRM_INSTALLED' ) ) return false; | |
if ( ! CIVICRM_INSTALLED ) return false; | |
// Bail if no CiviCRM init function. | |
if ( ! function_exists( 'civi_wp' ) ) return false; | |
// Try and initialise CiviCRM. | |
return civi_wp()->initialize(); | |
} | |
/** | |
* Register hooks. | |
* | |
* @since 0.1 | |
*/ | |
public function register_hooks() { | |
// Hook in just before CiviCRM. | |
add_action( 'wp_head', array( $this, 'resources_disable' ), 9 ); | |
} | |
/** | |
* Disable CiviCRM resources from front-end. | |
* | |
* @since 0.1 | |
*/ | |
public function resources_disable() { | |
// Kick out if no CiviCRM. | |
if ( ! $this->is_civicrm_initialised() ) return; | |
// Only on front-end. | |
if ( ! is_admin() ) { | |
// Define resources to disable. | |
$remove = array( | |
'bower_components/smartmenus/dist/jquery.smartmenus.min.js', | |
'bower_components/smartmenus/dist/addons/keyboard/jquery.smartmenus.keyboard.min.js', | |
'js/crm.menubar.js', | |
'bower_components/smartmenus/dist/css/sm-core-css.css', | |
'css/crm-menubar.css', | |
'css/menubar-wordpress.css', | |
); | |
// Disable KAM resources. | |
foreach( $remove AS $item ) { | |
$this->resource_disable( 'civicrm', $item ); | |
} | |
} | |
} | |
/** | |
* Disable a resource enqueued by CiviCRM. | |
* | |
* @since 0.1 | |
* | |
* @param str $extension The name of the extension e.g. 'org.civicrm.shoreditch'. Default is CiviCRM core. | |
* @param str $file The relative path to the resource. Default is CiviCRM core stylesheet. | |
*/ | |
public function resource_disable( $extension = 'civicrm', $file = 'css/civicrm.css' ) { | |
// Kick out if no CiviCRM. | |
if ( ! $this->is_civicrm_initialised() ) return; | |
// Get the resource URL. | |
$url = $this->resource_get_url( $extension, $file ); | |
// Kick out if not enqueued. | |
if ( $url === false ) return; | |
// Set to disabled. | |
CRM_Core_Region::instance( 'html-header' )->update( $url, array( 'disabled' => TRUE ) ); | |
} | |
/** | |
* Get the URL of a resource if it is enqueued by CiviCRM. | |
* | |
* @since 0.1 | |
* | |
* @param str $extension The name of the extension e.g. 'org.civicrm.shoreditch'. Default is CiviCRM core. | |
* @param str $file The relative path to the resource. Default is CiviCRM core stylesheet. | |
* @return bool|str $url The URL if the resource is enqueued, false otherwise. | |
*/ | |
public function resource_get_url( $extension = 'civicrm', $file = 'css/civicrm.css' ) { | |
// Kick out if no CiviCRM. | |
if ( ! $this->is_civicrm_initialised() ) return false; | |
// Get registered URL. | |
$url = CRM_Core_Resources::singleton()->getUrl( $extension, $file, TRUE ); | |
// Get registration data from region. | |
$registration = CRM_Core_Region::instance( 'html-header' )->get( $url ); | |
// Bail if not registered. | |
if ( empty( $registration ) ) return false; | |
// Is enqueued. | |
return $url; | |
} | |
} // Class ends. | |
/** | |
* Get a reference to this plugin. | |
* | |
* @since 0.1 | |
* | |
* @return CiviCRM_Remove_KAM $civicrm_remove_kam The plugin reference. | |
*/ | |
function civicrm_remove_kam() { | |
// Hold the plugin instance in a static variable. | |
static $civicrm_remove_kam = false; | |
// Instantiate plugin if not yet instantiated. | |
if ( false === $civicrm_remove_kam ) { | |
$civicrm_remove_kam = new CiviCRM_Remove_KAM(); | |
} | |
// --< | |
return $civicrm_remove_kam; | |
} | |
// Init plugin. | |
add_action( 'plugins_loaded', 'civicrm_remove_kam' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment