Guest User

Untitled

a guest
Oct 5th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.86 KB | None | 0 0
  1. <?php
  2. // Autoload the blocks
  3.  
  4. add_action('plugins_loaded', function() {
  5.  
  6.     // Return if ACF PRO is not installed/active
  7.     if(!function_exists('acf_register_block_type')) return;
  8.  
  9.     // Return if there is no directory 'blocks' in the current theme
  10.     $block_path = apply_filters('first_blocks_path', get_template_directory() . '/blocks');
  11.     if(!is_dir($block_path)) return;
  12.  
  13.     // Return if there are no block files inside the blocks directory
  14.     $block_files = glob($block_path . '/*');
  15.     if(!$block_files) return;
  16.  
  17.     // Initialize the array of allowed blocks ('core/block' is for reusable blocks)
  18.     $allowed_blocks = ['core/block'];
  19.  
  20.     // Register blocks
  21.     foreach($block_files as $block_file) {
  22.         $block_data = get_file_data($block_file, [
  23.             'title' => 'Title',
  24.             'category' => 'Category',
  25.             'icon' => 'Icon',
  26.             'multiple' => 'Multiple'
  27.         ]);
  28.  
  29.         if(!$block_data['title']) continue;
  30.  
  31.         $block_name = pathinfo($block_file)['filename'];
  32.         $allowed_blocks[] = 'acf/' . $block_name;
  33.  
  34.         $block_settings = [
  35.             'name' => $block_name,
  36.             'title' => $block_data['title'],
  37.             'category' => $block_data['category'] ?: 'custom',
  38.             'icon' => $block_data['icon'] ? str_replace('dashicons-', '', $block_data['icon']) : 'screenoptions',
  39.             'mode' => 'auto',
  40.             'supports' => [
  41.                 'align' => false,
  42.                 'mode' => false,
  43.                 'multiple' => $block_data['multiple'] !== '' ? filter_var($block_data['multiple'], FILTER_VALIDATE_BOOLEAN) : true,
  44.             ],
  45.             'render_callback' => function($block, $content = '', $is_preview = false, $post_id = 0) use($block_file) {
  46.                 if($is_preview === true): ?>
  47.                     <div class="first-block">
  48.                         <span class="first-block-icon dashicons dashicons-<?= $block['icon'] ?>"></span>
  49.                         <span class="first-block-title"><?= $block['title'] ?></span>
  50.                         <span class="first-block-edit dashicons dashicons-edit"></span>
  51.                     </div>
  52.                 <?php else:
  53.                     require $block_file;
  54.                 endif;
  55.             }
  56.         ];
  57.  
  58.         acf_register_block_type($block_settings);
  59.     }
  60.  
  61.     // Set allowed block types
  62.     add_filter('allowed_block_types', function() use($allowed_blocks) {
  63.         return $allowed_blocks;
  64.     });
  65.  
  66. });
  67.  
  68.  
  69. // Filter the allowed block categories; only allow 'reusable' && 'custom'
  70.  
  71. add_filter('block_categories', function($categories) {
  72.  
  73.     $allowed_categories = array_merge([
  74.         [
  75.             'slug' => 'custom',
  76.             'title' => 'First-blokken',
  77.             'icon' => ''
  78.         ]
  79.     ], $categories);
  80.  
  81.     return apply_filters('first_blocks_categories', $allowed_categories);
  82.  
  83. }, 99);
  84.  
  85.  
  86. // Save custom excerpt on save_post
  87.  
  88. add_action('save_post', function($post_id, $post) {
  89.  
  90.     if(has_blocks($post)) {
  91.         $parsed_blocks = parse_blocks($post->post_content);
  92.  
  93.         $rendered_blocks = array_reduce($parsed_blocks, function($prev_blocks, $current_block) {
  94.             if(strpos($current_block['blockName'], 'acf/') !== false) { // ACF blocks
  95.                 return $prev_blocks . acf_rendered_block($current_block['attrs']);
  96.             } elseif(strpos($current_block['blockName'], 'core/') !== false) { // Core blocks
  97.                 return $prev_blocks . render_block($current_block);
  98.             } else { // Other block types are not supported
  99.                 return $prev_blocks . '';
  100.             }
  101.         });
  102.  
  103.         $dom = new DOMDocument();
  104.         @$dom->loadHTML('<?xml encoding="utf-8" ?>' . $rendered_blocks);
  105.  
  106.         $p_tags = $dom->getElementsByTagName('p');
  107.         if($p_tags) {
  108.             $text = '';
  109.             foreach($p_tags as $p_tag) $text .= ' ' . $p_tag->nodeValue;
  110.  
  111.             update_post_meta($post->ID, 'first_blocks_excerpt', $text);
  112.         }
  113.     }
  114.  
  115. }, 10, 2);
  116.  
  117.  
  118. // Filters excerpt to add custom blocks
  119.  
  120. add_filter('get_the_excerpt', function($excerpt, $post) {
  121.  
  122.     if($excerpt) return $excerpt;
  123.  
  124.     if($first_blocks_excerpt = get_post_meta($post->ID, 'first_blocks_excerpt', true)) {
  125.         $length = apply_filters('first_blocks_excerpt_length', apply_filters('excerpt_length', 55), $post);
  126.         $more = apply_filters('first_blocks_excerpt_more', apply_filters('excerpt_more', '...'), $post);
  127.  
  128.         return wp_trim_words($first_blocks_excerpt, $length, $more);
  129.     }
  130.  
  131.     return '';
  132.  
  133. }, 20, 2);
  134.  
  135.  
  136. // Enqueue block editor assets
  137.  
  138. add_action('enqueue_block_editor_assets', function() {
  139.  
  140.     $src = FIRST_CORE_URL . '/assets/css/editor.css';
  141.     $deps = [];
  142.     $version = filemtime(FIRST_CORE_PATH . '/assets/css/editor.css');
  143.     wp_enqueue_style('first-core-editor-style', $src, $deps, $version);
  144.  
  145. });
Advertisement
Add Comment
Please, Sign In to add comment