admin-warnings.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Custom CSS and JS
  4. *
  5. */
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit; // Exit if accessed directly
  8. }
  9. /**
  10. * CustomCSSandJS_Warnings
  11. */
  12. class CustomCSSandJS_Warnings {
  13. private $allowed_actions = array(
  14. 'ccj_dismiss_qtranslate',
  15. );
  16. /**
  17. * Constructor
  18. */
  19. public function __construct() {
  20. if ( ! function_exists( 'is_plugin_active' ) ) {
  21. require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  22. }
  23. $this->check_qtranslatex();
  24. add_action( 'wp_ajax_ccj_dismiss', array( $this, 'notice_dismiss' ) );
  25. }
  26. /**
  27. * Check if qTranslate plugin is active and doesn't have the custom-css-js removed from the settings
  28. */
  29. function check_qtranslatex() {
  30. if ( ! is_plugin_active( 'qtranslate-x/qtranslate.php' ) ) return false;
  31. if ( get_option('ccj_dismiss_qtranslate') !== false ) {
  32. return;
  33. }
  34. $qtranslate_post_type_excluded = get_option('qtranslate_post_type_excluded');
  35. if ( ! is_array( $qtranslate_post_type_excluded ) || array_search( 'custom-css-js', $qtranslate_post_type_excluded ) === false ) {
  36. add_action( 'admin_notices', array( $this, 'check_qtranslate_notice' ) );
  37. return;
  38. }
  39. }
  40. /**
  41. * Show a warning about qTranslate
  42. */
  43. function check_qtranslate_notice() {
  44. $id = 'ccj_dismiss_qtranslate';
  45. $class = 'notice notice-warning is-dismissible';
  46. $message = sprintf(__( 'Please remove the <b>custom-css-js</b> post type from the <b>qTranslate settings</b> in order to avoid some malfunctions in the Simple Custom CSS & JS plugin. Check out <a href="%s" target="_blank">this screenshot</a> for more details on how to do that.', 'custom-css-js'), 'https://www.silkypress.com/wp-content/uploads/2016/08/ccj_qtranslate_compatibility.png' );
  47. $nonce = wp_create_nonce( $id );
  48. printf( '<div class="%1$s" id="%2$s" data-nonce="%3$s"><p>%4$s</p></div>', $class, $id, $nonce, $message );
  49. $this->dismiss_js( $id );
  50. }
  51. /**
  52. * Allow the dismiss button to remove the notice
  53. */
  54. function dismiss_js( $slug ) {
  55. ?>
  56. <script type='text/javascript'>
  57. jQuery(function($){
  58. $(document).on( 'click', '#<?php echo $slug; ?> .notice-dismiss', function() {
  59. var data = {
  60. action: 'ccj_dismiss',
  61. option: '<?php echo $slug; ?>',
  62. nonce: $(this).parent().data('nonce'),
  63. };
  64. $.post(ajaxurl, data, function(response ) {
  65. $('#<?php echo $slug; ?>').fadeOut('slow');
  66. });
  67. });
  68. });
  69. </script>
  70. <?php
  71. }
  72. /**
  73. * Ajax response for `notice_dismiss` action
  74. */
  75. function notice_dismiss() {
  76. $option = $_POST['option'];
  77. if ( ! in_array($option, $this->allowed_actions ) ) {
  78. return;
  79. }
  80. check_ajax_referer( $option, 'nonce' );
  81. update_option( $option, 1 );
  82. wp_die();
  83. }
  84. }
  85. return new CustomCSSandJS_Warnings();