Plugin Directory

Changeset 2490058


Ignore:
Timestamp:
03/08/2021 11:53:52 PM (5 years ago)
Author:
rebelwageslave
Message:

upgrade to 2.0

File:
1 edited

Legend:

Unmodified
Added
Removed
  • block-pattern-maker/trunk/block-pattern-maker.php

    r2420055 r2490058  
    44Plugin URI: https://github.com/rebelwageslave/block-pattern-maker/
    55Description: Create your own block patterns and use them.
    6 Version: 1.0
     6Version: 2.0
    77Author: rebelwageslave
    88Author URI: http://github.com/rebelwageslave/
     
    1313function dk_block_pattern_maker_init() {
    1414
    15     register_post_type('dk_block_patterns', array(
    16         'label' => __('Block patterns', 'block-pattern-maker'),
    17         'public' => true,
    18         'show_in_rest' => true,
    19         'exclude_from_search' => true,
    20         'menu_icon' => 'dashicons-grid-view'
    21     ));
     15    register_post_type( 'dk_block_patterns', array(
     16        'label'               => __( 'Block patterns', 'block-pattern-maker' ),
     17        'public'              => true,
     18        'show_in_rest'        => true,
     19        'exclude_from_search' => true,
     20        'menu_icon'           => 'dashicons-grid-view',
     21        'taxonomies'          => array( 'category' )
     22    ) );
    2223
    23     if ( ! function_exists('register_block_type') ) {
    24         return;
    25     }
     24    if ( ! function_exists( 'register_block_type' ) ) {
     25        return;
     26    }
    2627
    27     $posts = get_posts(
    28         array(
    29             'post_type' => 'dk_block_patterns',
    30             'numberposts' => -1,
    31             'post_status' => 'publish'
    32         )
    33     );
     28    $posts = get_posts(
     29        array(
     30            'post_type'  => 'dk_block_patterns',
     31            'numberposts' => - 1,
     32            'post_status' => 'publish'
     33        )
     34    );
    3435
    35     foreach ( $posts as $post ) {
    36         if (! $post->post_content ) {
    37             continue;
    38         }
    39         register_block_pattern('dk-'.md5($post->post_title),
    40         array(
    41             'title' => $post->post_title,
    42             'description' => $post->post_title,
    43             'content' => $post->post_content,
    44         ));
    45     }
     36    foreach ( $posts as $post ) {
     37        if ( ! $post->post_content ) {
     38            continue;
     39        }
     40        $categories = get_the_category( $post->ID );
     41
     42
     43        $category_names = array();
     44        foreach ( $categories as $category ) {
     45            $category_names[] = 'dk_' . $category->slug;
     46            /**
     47             * @var $category WP_Term
     48             */
     49            register_block_pattern_category( 'dk_' . $category->slug, array( 'label' => __( $category->name, 'dk_block_patterns' ) ) );
     50        }
     51
     52        register_block_pattern( 'dk-' . md5( $post->post_title ),
     53            array(
     54                'title'       => $post->post_title,
     55                'description' => $post->post_title,
     56                'content'     => $post->post_content,
     57                'categories'  => $category_names
     58            ) );
     59    }
    4660
    4761}
    4862
    4963
    50 
    51 
    52 
    53 add_action('init', 'dk_block_pattern_maker_init');
     64add_action( 'init', 'dk_block_pattern_maker_init' );
Note: See TracChangeset for help on using the changeset viewer.