Last active
September 24, 2019 23:21
-
-
Save timelsass/8ad0e7b3b7444b149097ac1ace250a30 to your computer and use it in GitHub Desktop.
This is a patch plugin for an issue with "E-Signature Plugin" not having mce buttons appear in TinyMCE editors when using BGTFW v1 based themes.
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: BGTFW v1 E-Signature Plugin Patch | |
* Description: This is a patch plugin for an issue with "E-Signature Plugin" not having mce buttons appear in TinyMCE editors when using BGTFW v1 based themes. | |
* Plugin URI: https://gist.github.com/timelsass/8ad0e7b3b7444b149097ac1ace250a30 | |
* Author: Tim Elsass | |
* Author URI: tim.ph | |
*/ | |
add_action( 'admin_init', 'bgtfw_v1_e_signature_patch_remove_bgtfw_hook' ); | |
add_action( 'mce_external_plugins', 'bgtfw_v1_e_signature_patch_post_bgtfw' ); | |
/** | |
* Apply BGTFW's filter correctly. | |
*/ | |
function bgtfw_v1_e_signature_patch_post_bgtfw( $plugin_array ) { | |
global $pagenow, $boldgrid_theme_framework; | |
if ( class_exists( 'Boldgrid_Framework_Editor' ) ) { | |
// Access configs for correct paths to files. | |
$configs = $boldgrid_theme_framework->get_configs(); | |
$bgtfw = new Boldgrid_Framework_Editor( $configs ); | |
$valid_pages = array( | |
'customize.php', | |
'post.php', | |
'post-new.php', | |
); | |
$valid_post_types = array( | |
'page', | |
'post', | |
); | |
if ( ! empty( $pagenow ) && ! in_array( $pagenow, $valid_pages ) ) { | |
return $plugin_array; | |
} | |
// Currently only pages and posts are supported. @since 1.3.1 | |
if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) { | |
if ( ! in_array( $bgtfw->get_post_type(), $valid_post_types ) ) { | |
return $plugin_array; | |
} | |
} | |
$mce_inline_styles = ''; | |
$mce_inline_styles = apply_filters( 'boldgrid_mce_inline_styles', $mce_inline_styles ); | |
$mce_inline_styles = apply_filters( 'kirki/global/dynamic_css', $mce_inline_styles ); | |
wp_localize_script( 'mce-view', 'BOLDGRID_THEME_FRAMEWORK', | |
array( | |
'Editor' => array( | |
'mce_inline_styles' => $mce_inline_styles, | |
), | |
'post_id' => ! empty( $_REQUEST['post'] ) ? $_REQUEST['post'] : null, | |
) | |
); | |
$editor_js_file = $configs['framework']['admin_asset_dir'] . 'js/editor.js'; | |
$plugin_array['boldgrid_theme_framework'] = $editor_js_file; | |
} | |
return $plugin_array; | |
} | |
/** | |
* Remove BGTFW v1's faulty code for e-signature plugin compatibility. | |
*/ | |
function bgtfw_v1_e_signature_patch_remove_bgtfw_hook() { | |
global $wp_filter; | |
$tag = 'mce_external_plugins'; | |
$class = 'Boldgrid_Framework_Editor'; | |
$name = 'add_tinymce_plugin'; | |
$priority = 10; | |
if ( ! is_admin() || wp_doing_ajax() ) { | |
return false; | |
} | |
// Check that filter exists. | |
if ( isset( $wp_filter[ $tag ] ) ) { | |
/** | |
* If filter config is an object, means we're using WordPress 4.7+ and the config is no longer | |
* a simple array, and it is an object that implements the ArrayAccess interface. | |
* | |
* To be backwards compatible, we set $callbacks equal to the correct array as a reference (so $wp_filter is updated). | |
* | |
* @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/ | |
*/ | |
if ( is_object( $wp_filter[ $tag ] ) && isset( $wp_filter[ $tag ]->callbacks ) ) { | |
// Create $fob object from filter tag, to use below. | |
$fob = $wp_filter[ $tag ]; | |
$callbacks = &$wp_filter[ $tag ]->callbacks; | |
} else { | |
$callbacks = &$wp_filter[ $tag ]; | |
} | |
// Exit if there aren't any callbacks for specified priority. | |
if ( ! isset( $callbacks[ $priority ] ) || empty( $callbacks[ $priority ] ) ) { | |
return false; | |
} | |
// Loop through each filter for the specified priority, looking for our class & method. | |
foreach( ( array ) $callbacks[ $priority ] as $filter_id => $filter ) { | |
// Filter should always be an array - array( $this, 'method' ), if not goto next. | |
if ( ! isset( $filter['function'] ) || ! is_array( $filter['function'] ) ) { | |
continue; | |
} | |
// If first value in array is not an object, it can't be a class. | |
if ( ! is_object( $filter['function'][0] ) ) { | |
continue; | |
} | |
// Method doesn't match the one we're looking for, goto next. | |
if ( $filter['function'][1] !== $name ) { | |
continue; | |
} | |
// Callback method matched, so check class. | |
if ( get_class( $filter['function'][0] ) === $class ) { | |
// WordPress 4.7+ use core remove_filter() since we found the class object. | |
if ( isset( $fob ) ) { | |
// Handles removing filter, reseting callback priority keys mid-iteration, etc. | |
$fob->remove_filter( $tag, $filter['function'], $priority ); | |
} else { | |
// Use legacy removal process (pre 4.7). | |
unset( $callbacks[ $priority ][ $filter_id ] ); | |
// If it was the only filter in that priority, unset that priority. | |
if ( empty( $callbacks[ $priority ] ) ) { | |
unset( $callbacks[ $priority ] ); | |
} | |
// If the only filter for that tag, set the tag to an empty array. | |
if ( empty( $callbacks ) ) { | |
$callbacks = array(); | |
} | |
// Remove this filter from merged_filters, which specifies if filters have been sorted. | |
unset( $GLOBALS['merged_filters'][ $tag ] ); | |
} | |
return true; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment