Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Autoload the blocks
- add_action('plugins_loaded', function() {
- // Return if ACF PRO is not installed/active
- if(!function_exists('acf_register_block_type')) return;
- // Return if there is no directory 'blocks' in the current theme
- $block_path = apply_filters('first_blocks_path', get_template_directory() . '/blocks');
- if(!is_dir($block_path)) return;
- // Return if there are no block files inside the blocks directory
- $block_files = glob($block_path . '/*');
- if(!$block_files) return;
- // Initialize the array of allowed blocks ('core/block' is for reusable blocks)
- $allowed_blocks = ['core/block'];
- // Register blocks
- foreach($block_files as $block_file) {
- $block_data = get_file_data($block_file, [
- 'title' => 'Title',
- 'category' => 'Category',
- 'icon' => 'Icon',
- 'multiple' => 'Multiple'
- ]);
- if(!$block_data['title']) continue;
- $block_name = pathinfo($block_file)['filename'];
- $allowed_blocks[] = 'acf/' . $block_name;
- $block_settings = [
- 'name' => $block_name,
- 'title' => $block_data['title'],
- 'category' => $block_data['category'] ?: 'custom',
- 'icon' => $block_data['icon'] ? str_replace('dashicons-', '', $block_data['icon']) : 'screenoptions',
- 'mode' => 'auto',
- 'supports' => [
- 'align' => false,
- 'mode' => false,
- 'multiple' => $block_data['multiple'] !== '' ? filter_var($block_data['multiple'], FILTER_VALIDATE_BOOLEAN) : true,
- ],
- 'render_callback' => function($block, $content = '', $is_preview = false, $post_id = 0) use($block_file) {
- if($is_preview === true): ?>
- <div class="first-block">
- <span class="first-block-icon dashicons dashicons-<?= $block['icon'] ?>"></span>
- <span class="first-block-title"><?= $block['title'] ?></span>
- <span class="first-block-edit dashicons dashicons-edit"></span>
- </div>
- <?php else:
- require $block_file;
- endif;
- }
- ];
- acf_register_block_type($block_settings);
- }
- // Set allowed block types
- add_filter('allowed_block_types', function() use($allowed_blocks) {
- return $allowed_blocks;
- });
- });
- // Filter the allowed block categories; only allow 'reusable' && 'custom'
- add_filter('block_categories', function($categories) {
- $allowed_categories = array_merge([
- [
- 'slug' => 'custom',
- 'title' => 'First-blokken',
- 'icon' => ''
- ]
- ], $categories);
- return apply_filters('first_blocks_categories', $allowed_categories);
- }, 99);
- // Save custom excerpt on save_post
- add_action('save_post', function($post_id, $post) {
- if(has_blocks($post)) {
- $parsed_blocks = parse_blocks($post->post_content);
- $rendered_blocks = array_reduce($parsed_blocks, function($prev_blocks, $current_block) {
- if(strpos($current_block['blockName'], 'acf/') !== false) { // ACF blocks
- return $prev_blocks . acf_rendered_block($current_block['attrs']);
- } elseif(strpos($current_block['blockName'], 'core/') !== false) { // Core blocks
- return $prev_blocks . render_block($current_block);
- } else { // Other block types are not supported
- return $prev_blocks . '';
- }
- });
- $dom = new DOMDocument();
- @$dom->loadHTML('<?xml encoding="utf-8" ?>' . $rendered_blocks);
- $p_tags = $dom->getElementsByTagName('p');
- if($p_tags) {
- $text = '';
- foreach($p_tags as $p_tag) $text .= ' ' . $p_tag->nodeValue;
- update_post_meta($post->ID, 'first_blocks_excerpt', $text);
- }
- }
- }, 10, 2);
- // Filters excerpt to add custom blocks
- add_filter('get_the_excerpt', function($excerpt, $post) {
- if($excerpt) return $excerpt;
- if($first_blocks_excerpt = get_post_meta($post->ID, 'first_blocks_excerpt', true)) {
- $length = apply_filters('first_blocks_excerpt_length', apply_filters('excerpt_length', 55), $post);
- $more = apply_filters('first_blocks_excerpt_more', apply_filters('excerpt_more', '...'), $post);
- return wp_trim_words($first_blocks_excerpt, $length, $more);
- }
- return '';
- }, 20, 2);
- // Enqueue block editor assets
- add_action('enqueue_block_editor_assets', function() {
- $src = FIRST_CORE_URL . '/assets/css/editor.css';
- $deps = [];
- $version = filemtime(FIRST_CORE_PATH . '/assets/css/editor.css');
- wp_enqueue_style('first-core-editor-style', $src, $deps, $version);
- });
Advertisement
Add Comment
Please, Sign In to add comment