| 1 |
<?php |
| 2 |
/** |
| 3 |
* Callbacks for display and dismissal of plugin conflicts warning. |
| 4 |
* |
| 5 |
* @package FaustWP |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPE\FaustWP\Detect_Conflicts; |
| 9 |
|
| 10 |
/** Unique ID for the conflict warning. */ |
| 11 |
const NOTICE_ID = 'faustwp_plugin_conflicts'; |
| 12 |
|
| 13 |
/** |
| 14 |
* Renders the plugin conflicts admin notice. |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
function show_warning() { |
| 19 |
if ( ! needs_warning() ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
$plugins = get_plugin_conflicts(); |
| 24 |
$warnings = array(); |
| 25 |
|
| 26 |
foreach ( $plugins as $plugin ) { |
| 27 |
$plugin_headers = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); |
| 28 |
$warnings[] = $plugin_headers['Name']; |
| 29 |
} |
| 30 |
|
| 31 |
printf( |
| 32 |
'<div id="%s" class="notice notice-warning notice-alt is-dismissible"> |
| 33 |
<h2 class="notice-title">%s</h2> |
| 34 |
<p>%s</p> |
| 35 |
<p>%s</p> |
| 36 |
</div>', |
| 37 |
esc_attr( NOTICE_ID ), |
| 38 |
esc_html__( 'Faust detected incompatible plugins', 'faustwp' ), |
| 39 |
esc_html__( 'The following active plugins are known to conflict with Faust and may cause unexpected behavior:', 'faustwp' ), |
| 40 |
esc_html( implode( ', ', $warnings ) ) |
| 41 |
); |
| 42 |
} |
| 43 |
add_action( 'admin_notices', __NAMESPACE__ . '\\show_warning' ); |
| 44 |
|
| 45 |
/** |
| 46 |
* Includes a script for dismissing the plugin conflicts warning. |
| 47 |
* |
| 48 |
* Modified from https://github.com/WPTT/admin-notices. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
function print_scripts() { |
| 53 |
if ( ! needs_warning() ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
// Create a nonce. |
| 58 |
$nonce = wp_create_nonce( NOTICE_ID ); |
| 59 |
?> |
| 60 |
<script> |
| 61 |
|
| 62 |
window.addEventListener( 'load', function() { |
| 63 |
let wait_for_btn = null; |
| 64 |
let notice_selector = '#<?php echo esc_attr( NOTICE_ID ); ?> .notice-dismiss'; |
| 65 |
|
| 66 |
// Wait for core WP to add the dismiss button to the notice. |
| 67 |
wait_for_btn = setInterval(function() { |
| 68 |
let dismissBtn = document.querySelector( notice_selector ); |
| 69 |
|
| 70 |
if (!dismissBtn && !dismissBtn.length) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
clearInterval(wait_for_btn); |
| 75 |
|
| 76 |
// Add an event listener to the dismiss button. |
| 77 |
dismissBtn.addEventListener( 'click', function( event ) { |
| 78 |
let postData = new FormData(); |
| 79 |
postData.append('action', '<?php echo esc_attr( NOTICE_ID ); ?>'); |
| 80 |
postData.append('nonce', '<?php echo esc_html( $nonce ); ?>'); |
| 81 |
|
| 82 |
window.fetch('<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', { |
| 83 |
method: 'POST', |
| 84 |
body: postData, |
| 85 |
}) |
| 86 |
}); |
| 87 |
}, 100); |
| 88 |
}); |
| 89 |
</script> |
| 90 |
<?php |
| 91 |
} |
| 92 |
add_action( 'admin_footer', __NAMESPACE__ . '\\print_scripts' ); |
| 93 |
|
| 94 |
/** |
| 95 |
* Ajax handler for dismissing plugin conflicts. |
| 96 |
* |
| 97 |
* Modified from https://github.com/WPTT/admin-notices. |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
function ajax_maybe_dismiss_conflicts() { |
| 102 |
// Sanity check: Early exit if we're not on a faustwp_plugin_conflicts action. |
| 103 |
if ( ! isset( $_POST['action'] ) || esc_attr( NOTICE_ID ) !== $_POST['action'] ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
// Security check: Make sure nonce is OK. |
| 108 |
check_ajax_referer( NOTICE_ID, 'nonce', true ); |
| 109 |
|
| 110 |
// If we got this far, we need to dismiss the current plugin conflicts. |
| 111 |
dismiss_active_conflicts(); |
| 112 |
} |
| 113 |
add_action( 'wp_ajax_' . NOTICE_ID, __NAMESPACE__ . '\\ajax_maybe_dismiss_conflicts' ); |
| 114 |
|