integration.php (3738B)
1 <?php 2 3 if ( ! defined('ABSPATH')) { 4 die('Silence is golden'); 5 } 6 7 if ( ! defined('MATERIALIS_GUTENBERG_INTEGRATION_PATH')) { 8 define('MATERIALIS_GUTENBERG_INTEGRATION_PATH', dirname(__FILE__) . '/'); 9 } 10 11 if ( ! defined('MATERIALIS_GUTENBERG_INTEGRATION_URL')) { 12 define('MATERIALIS_GUTENBERG_INTEGRATION_URL', plugin_dir_url(__FILE__)); 13 } 14 15 function materialis_gutenberg() { 16 17 materialis_do_enqueue_google_fonts(); 18 19 wp_register_script( 20 'materialis-gutenberg', 21 MATERIALIS_GUTENBERG_INTEGRATION_URL . 'assets/materialis.js', 22 array( 'wp-blocks', 'wp-element' ) 23 ); 24 25 wp_enqueue_style( 26 'materialis-gutenberg', 27 get_template_directory_uri() . '/gutenberg-style.min.css', 28 array( 'wp-edit-blocks' ), 29 materialis_get_version() 30 ); 31 32 wp_enqueue_style( 33 'materialis-companion', 34 MATERIALIS_GUTENBERG_INTEGRATION_URL . '../../assets/css/companion.bundle.min.css', 35 array( 'wp-edit-blocks' ), 36 materialis_get_version() 37 ); 38 39 wp_enqueue_style( 40 'materialis-fontawesome', 41 get_template_directory_uri() . '/assets/css/material-icons.min.css', 42 array( 'wp-edit-blocks' ), 43 materialis_get_version() 44 ); 45 46 if ( function_exists( 'register_block_type' ) ) { 47 register_block_type( 'extend/materialis-gutenberg', array( 48 'editor_script' => 'materialis-gutenberg', 49 ) ); 50 } 51 } 52 53 add_action( 'admin_init', 'materialis_gutenberg' ); 54 55 /* 56 Fix to keep gutenberg block html comments 57 */ 58 function materialis_gutenberg_keep_comment_before($text) 59 { 60 //from <!-- wp:namespace/block {"option1":1,"option2":"2"} --> to [wp:namespace/block {"option1":1,"option2":"2"}] 61 $gutenbergCommentRegex = '#<!--\s+(\/?)wp:([\w\/]+) (.*?)-->#'; 62 63 //use callback to escape html entities so that wordpress does not strip block options 64 $text = preg_replace_callback( 65 $gutenbergCommentRegex, 66 function($matches) { 67 68 return '@@' . $matches[1] . 'wp:' . $matches[2] . ' ' . htmlentities($matches[3]) . '@@'; 69 }, 70 $text); 71 72 return $text; 73 } 74 75 function materialis_gutenberg_keep_comment_after($text) 76 { 77 //from [wp:namespace/block {"option1":1,"option2":"2"}] to <!-- wp:namespace/block {"option1":1,"option2":"2"} --> 78 $gutenbergCommentRegex = '#@@(\/?)wp:([\w\/]+)\s+(.*?)( \/ )?@@#'; 79 80 $text = preg_replace_callback( 81 $gutenbergCommentRegex, 82 function($matches) { 83 84 $comment = '<!-- ' . $matches[1] . 'wp:' . $matches[2] . ($matches[3]?' ':'') . trim(html_entity_decode($matches[3])) . ' -->'; 85 if ( is_customize_preview() ) { 86 return $comment; 87 } 88 89 $return = ''; 90 //if not theme block and ordinary gutenberg section then wrap in gridContainer 91 if (strpos($matches[2], 'extendstudio') === false) 92 { 93 94 //single comment close tag 95 if (trim($matches[3]) == '/') { 96 $return .= '<!-- ' . str_replace('@@', '', $matches[0]) . '-->'; 97 //$return = '<div class="gridContainer"> ' . $comment . '</div>'; 98 } else //comment close tag 99 if ($matches[1]) { 100 $return .= '</div>' . $comment; 101 } else {//comment start tag 102 $return .= $comment . 103 '<div class="gridContainer">'; 104 } 105 } else $return = ''; 106 107 return $return; 108 }, 109 $text); 110 111 return $text; 112 } 113 114 add_filter( 'the_content', 'materialis_gutenberg_keep_comment_before', 5); 115 add_filter( 'the_content', 'materialis_gutenberg_keep_comment_after', is_customize_preview() ? 20 : 6);