Commit edd91808 authored by Andrei Mondoc's avatar Andrei Mondoc
Browse files

Update prevent-civicrm-content-in-widgets.php

parents
<?php
/*
--------------------------------------------------------------------------------
Plugin Name: Prevent CiviCRM Content in widgets
Description: Prevents CiviCRM rendering (duplicate) content on widgets that use apply_filters( 'the_content' ), more widget names can be added using the 'cv/prevent_content_in_widgets/widget_names' filter.
Author: Andrei Mondoc
Version: 0.1
--------------------------------------------------------------------------------
*/
/**
* Filters CiviCRM's context and removes
* Civi basepage content (the_content) filter
* for posts that use apply_filters('the_content')
* preventing CiviCRM from injecting markup in those.
*
* @param string $ctx The CiviCRM context
* @return string $ctx The CiviCRM context
*/
add_filter( 'civicrm_context', function( $ctx ) {
// bail if not in basepage context
if ( $ctx != 'basepage' ) return $ctx;
/**
* Filters sidebar widget params, and removes
* Civi content filter if applicable.
*
* @param array $params The Widget display arguments
* @return array $params The Widget display arguments
*/
add_filter( 'dynamic_sidebar_params', function( $params ) {
list( $args ) = $params;
/**
* Filter the widgets to check
* for removing CiviCRM content.
*
* @param array $widgets_to_prevent The widget names to prevent displaying Civi content
* @param array $widget_args The widget display arguments
*/
$widgets_to_prevent = apply_filters(
'cv/prevent_content_in_widgets/widget_names',
[ 'Reusable block' ],
$args
);
if ( ! in_array( $args['widget_name'], $widgets_to_prevent ) ) return $params;
// remove civi content filter
remove_filter( 'the_content', [ civi_wp()->basepage, 'basepage_render' ] );
// add civi content filter back
add_action( 'dynamic_sidebar_after', function() {
if ( has_filter( 'the_content', [ civi_wp()->basepage, 'basepage_render' ] ) ) return;
add_filter( 'the_content', [ civi_wp()->basepage, 'basepage_render' ] );
} );
return $params;
} );
return $ctx;
} );
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment