Skip to content

Commit

Permalink
Issue #80 Add support for block variations implemented as children of…
Browse files Browse the repository at this point in the history
… the parent block
  • Loading branch information
bobbingwide committed Oct 16, 2020
1 parent 1cc6913 commit 1426dc0
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 62 deletions.
62 changes: 23 additions & 39 deletions admin/oik-create-blocks.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php // (C) Copyright Bobbing Wide 2019
<?php // (C) Copyright Bobbing Wide 2019, 2020

if ( PHP_SAPI === "cli" ) {
oiksc_create_blocks_loaded();
Expand All @@ -19,9 +19,7 @@
*/

/**
* Create oik-shortcode entries programmatically
*
* After the plugins, themes and APIs have been created we need to update the definitions of all the shortblocks
* Creates Block and block variation entries programmatically.
*
*/
function oiksc_create_blocks_loaded() {
Expand All @@ -40,17 +38,12 @@ function oiksc_create_blocks_loaded() {
$post_title = "$title - $block_type_name";
oiksc_create_block( $block_type_name, $title, $component );


}





/**
* Programmatically create a block post
*
* This is run in batch
* This used to be run in batch.
*
* @param string $block_type_name
* @param string $title
Expand All @@ -59,7 +52,7 @@ function oiksc_create_blocks_loaded() {
* @param string|null $description
*/
function oiksc_create_block( $block_type_name, $title, $required_component, $icon=null, $description=null ) {
function oiksc_create_block( $block_type_name, $title, $required_component, $icon=null, $description=null, $parent=0, $variation=null ) {

oik_require( "admin/oik-apis.php", "oik-shortcodes" );
$component_id = oiksc_get_component_by_name( $required_component );
Expand All @@ -73,16 +66,18 @@ function oiksc_create_block( $block_type_name, $title, $required_component, $ico
echo "Creating $post_title: $required_component: $component_id" . PHP_EOL;
$post = array();

$post = oiksc_get_block( $block_type_name );
$post = oiksc_get_block( $block_type_name, $parent, $variation );
if ( !$post ) {
$post = [];
$post = array( 'post_type' => 'block'
, 'post_title' => $post_title
, 'post_status' => 'publish'
);
$post['title'] = $post_title;
$post['post_content' ] = oiksc_create_block_content( $block_type_name, $icon, $title, $description );
$post['post_content' ] = oiksc_create_block_content( $block_type_name, $icon, $title, $description, $variation );
$post['post_parent'] = $parent;
$_POST['_block_type_name'] = $block_type_name;
$_POST['_block_variation'] = $variation;
$_POST['_oik_sc_plugin'] = $component_id;
//oikb_get_response( "Continue?", true );
$post_id = wp_insert_post( $post );
Expand All @@ -105,8 +100,9 @@ function oiksc_create_block( $block_type_name, $title, $required_component, $ico
* @return array
*/

function oiksc_create_block_content( $block_type_name, $icon=null, $title=null, $description=null ) {
$atts = oiksc_block_atts_encode( [ 'className' => 'svg64', 'blockicon' => $block_type_name,
function oiksc_create_block_content( $block_type_name, $icon=null, $title=null, $description=null, $variation=null ) {
$blockicon = $variation ? $block_type_name . '|' . $variation : $block_type_name;
$atts = oiksc_block_atts_encode( [ 'className' => 'svg64', 'blockicon' => $blockicon,
'showBlockTypeName' => false, 'showTitle' => false, 'showCategory' => false, 'showKeywords' => false ] );
if ( $icon ) {
$iconinfo = oiksc_icon_blockinfo( $icon, $block_type_name, $title, $description );
Expand All @@ -129,6 +125,12 @@ function oiksc_create_block_content( $block_type_name, $icon=null, $title=null,
$content .= oiksc_generate_block( "separator", null, '<hr class="wp-block-separator"/>');
$content .= oiksc_generate_block( "heading", null, "<h2>Notes</h2>");
$content .= oiksc_generate_block( "list", null, '<ul><li>TBC</li></ul>');

// When it's a variation then we don't need to display Variations.
// When it's not then we don't know, programmatically, if there are variations.
// until we edit the block.
// $content .= oiksc_generate_block( "heading", null, "<h2>Variations</h2>");

//echo $content;
//oikb_get_response( "Continue?", true );
return $content;
Expand Down Expand Up @@ -162,9 +164,12 @@ function oiksc_default_blockinfo() {

function oiksc_icon_blockinfo( $icon, $block_type_name, $block_title=null, $block_description=null ) {
$iconinfo = '<div class="wp-block-oik-block-blockinfo svg64"><div>';
//$icon = str_replace( 'viewbox', 'viewBox', $icon );
//$icon = str_replace( 'preserveaspectratio', 'preserveAspectRatio', $icon );
$iconinfo .= $icon;
$iconinfo .= '</div>';
$iconinfo .= '<div>';
$iconinfo .= '</div><div>';
//$iconinfo .= $block_type_name;
//$iconinfo .= '</div><div>';
//$iconinfo .= $block_title;
Expand All @@ -184,34 +189,13 @@ function oiksc_get_component_by_name( $component_name ) {
if ( $plugin_post ) {
$component_id = $plugin_post->ID;
} else {

gob();
echo "Invalid component name: $component_name";
$component_id = 0;
}
return( $component_id );
}

if ( !function_exists( 'oiksc_get_block')) {

function oiksc_get_block( $block_type_name ) {
$args = array(
"post_type" => "block"
,
"meta_key" => "_block_type_name"
,
"number_posts" => 1
,
"meta_value" => $block_type_name
);
$posts = bw_get_posts( $args );
if ( $posts ) {
$post = $posts[0];
} else {
$post = null;
}

return $post;
}
}
/** oiksc_get_block now only in oik-update-blocks.php */

/**
* @return array
Expand Down
39 changes: 33 additions & 6 deletions admin/oik-create-or-update-block.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
<?php

/**
* @copyright (C) Copyright Bobbing Wide 2019
* @copyright (C) Copyright Bobbing Wide 2019, 2020
*/

/**
* Implements "oiksc_create_or_update_block" request
*
* To create or update a block definition for a WordPress block editor block
* To create or update a block definition for a WordPress block editor block.
* Now supports creating blocks which are variations.
*
* Fields:
* block
*
* https://blocks.wp.a2z/wp-admin/admin-ajax.php?action=oiksc_create_or_update_block
* &title=Search
* &name=default
* &description=Help%20visitors%20find%20your%20content.
* &component=gutenberg
* &keywords=
* &variation=core%2Fsearch
* &icon=someverylongstring
*/
function oiksc_lazy_create_or_update_block() {

Expand All @@ -21,6 +31,7 @@ function oiksc_lazy_create_or_update_block() {
$category = bw_array_get( $_REQUEST, 'category');
$icon = bw_array_get( $_REQUEST, 'icon');
$description = bw_array_get( $_REQUEST, 'description' );
$variation = bw_array_get( $_REQUEST, 'variation');

bw_trace2( $icon, 'icon' );
e( esc_html( $icon ));
Expand All @@ -32,12 +43,28 @@ function oiksc_lazy_create_or_update_block() {

kses_remove_filters();

$post = oiksc_get_block( $block_type_name );
$parent_ID = 0;
if ( $variation ) {
$parent = oiksc_get_block( $variation, 0, null );
if ( !$parent ) {
e( "No parent block found for variation: $variation $block_type_name ");
return;
} else {
$parent_ID = $parent->ID;
e( "Parent is: $parent_ID" );

}
$saved = $block_type_name;
$block_type_name = $variation;
$variation = $saved;
}

$post = oiksc_get_block( $block_type_name, $parent_ID, $variation );
if ( null === $post ) {
oiksc_create_block( $block_type_name, $title, $component, $icon, $description );
oiksc_create_block( $block_type_name, $title, $component, $icon, $description, $parent_ID, $variation );
}
oiksc_update_block( $block_type_name, $keywords, $category );
$post = oiksc_get_block( $block_type_name );
oiksc_update_block( $block_type_name, $keywords, $category, $parent_ID, $variation );
$post = oiksc_get_block( $block_type_name, $parent_ID, $variation );

//$block_icon = bw_array_get( $_REQUEST, 'icon');
e( $block_type_name );
Expand Down
44 changes: 29 additions & 15 deletions admin/oik-update-blocks.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php // (C) Copyright Bobbing Wide 2019
<?php // (C) Copyright Bobbing Wide 2019,2020

if ( PHP_SAPI === "cli" ) {
oiksc_update_blocks_loaded();
Expand Down Expand Up @@ -56,14 +56,14 @@ function oiksc_update_blocks_loaded() {
* @param string $required_component
*/
function oiksc_update_block( $block_type_name, $keywords, $category ) {
function oiksc_update_block( $block_type_name, $keywords, $category, $parent, $variation ) {

$block_keywords = "$block_type_name $keywords $category";
echo "Updating $block_keywords" . PHP_EOL;
$post = array();
oik_require( "includes/bw_posts.php" );

$post = oiksc_get_block( $block_type_name );
$post = oiksc_get_block( $block_type_name, $parent, $variation );
if ( $post ) {

//$post_id = wp_update_post( $post );
Expand All @@ -83,17 +83,31 @@ function oiksc_update_block( $block_type_name, $keywords, $category ) {
bw_flush();
}

if ( !function_exists( 'oiksc_get_block')) {
function oiksc_get_block( $block_type_name ) {
$args = array(
"post_type" => "block"
,
"meta_key" => "_block_type_name"
,
"number_posts" => 1
,
"meta_value" => $block_type_name
);

function oiksc_get_block( $block_type_name, $parent=0, $variation=null ) {

if ( $parent ) {

$args = array( "post_type" => "block"
, "meta_key" => "_block_variation"
, "number_posts" => 1
, "meta_value" => $variation
, 'post_parent' => $parent
);

} else {
$args=array(
"post_type" =>"block"
,
"meta_key" =>"_block_type_name"
,
"number_posts"=>1
,
"meta_value" =>$block_type_name
,
'post_parent' =>$parent
);
}
$posts = bw_get_posts( $args );
if ( $posts ) {
$post = $posts[0];
Expand All @@ -103,7 +117,7 @@ function oiksc_get_block( $block_type_name ) {

return $post;
}
}


function oiksc_update_block_yoastseo( $post, $keywords ){
$id = $post->ID;
Expand Down
33 changes: 32 additions & 1 deletion oik-shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ function oik_register_block_editor_stuff() {

oik_register_block_CPT();
oik_register_block_example_CPT();
//oik_register_block_variation_CPT();
//oik_register_metabox_CPT();
//oik_register_panel_CPT();

Expand All @@ -379,19 +380,23 @@ function oik_register_block_CPT() {
$post_type_args = array();
$post_type_args['label'] = 'Blocks';
$post_type_args['description'] = 'WordPress blocks';
$post_type_args['supports'] = array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'clone', 'author', 'home' );
$post_type_args['supports'] = array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'clone', 'author', 'home', 'page-attributes' );
$post_type_args['has_archive'] = true;
$post_type_args['show_in_rest'] = true;
$post_type_args['hierarchical'] = true;
$post_type_args['taxonomies'] = [ 'block_category', 'block_keyword', 'block_classification' ];
$post_type_args['menu_icon'] = 'dashicons-lightbulb'; //'dashicons-block-default';
$post_type_args['template'] = oik_block_CPT_template();
bw_register_post_type( $post_type, $post_type_args );
add_post_type_support( $post_type, 'publicize' );
add_post_type_support( $post_type, 'page-attributes');

bw_register_field( "_block_type_name", "text", "Block type name" );
bw_register_field( '_block_variation', 'text', 'Variation name' );
bw_register_field( "_block_doc_link", "url", "Documentation");

bw_register_field_for_object_type( "_block_type_name", $post_type );
bw_register_field_for_object_type( '_block_variation', $post_type );
bw_register_field_for_object_type( "_block_doc_link", $post_type );
bw_register_field_for_object_type( "_oik_sc_plugin", $post_type );
bw_register_field_for_object_type( "_oikp_dependency", $post_type );
Expand Down Expand Up @@ -445,6 +450,32 @@ function oik_register_block_example_CPT() {
bw_register_field_for_object_type( "_oikp_dependency", $post_type );
}

/**
* Register custom post type "block_variation"
*
* A block example refers to a particular block.
* It refers to the block for which it's an example.
* It may also reference the shortcodes it implements
* Post type support of "publicize" is added to enable publicizing using JetPack.
*
*/
function oik_register_block_variation_CPT() {
$post_type = 'block_variation';
$post_type_args = array();
$post_type_args['label'] = 'Block variations';
$post_type_args['description'] = 'Variations for blocks';
$post_type_args['supports'] = array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'clone', 'author', 'home' );
$post_type_args['has_archive'] = true;
$post_type_args['show_in_rest'] = true;
$post_type_args['taxonomies'] = [ 'block_category' ];
$post_type_args['menu_icon'] = 'dashicons-lightbulb'; //'dashicons-block-default';
bw_register_post_type( $post_type, $post_type_args );
add_post_type_support( $post_type, 'publicize' );
bw_register_field( "_block_ref", "noderef", "Block", array( '#type' => 'block') );
bw_register_field_for_object_type( "_block_ref", $post_type );
bw_register_field_for_object_type( "_oikp_dependency", $post_type );
}

/**
* Registers the Blocks catalogued virtual field
*
Expand Down
4 changes: 3 additions & 1 deletion shortcodes/oik-blockslink.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ function oikai_listblocks( $atts ) {
}

/**
* Load the blocks listed in the array
* Loads the blocks listed in the array.
*
* This now only loads parent blocks not variations.
* Since the plugin/theme name is not specified we could get duplicates in the results.
*
* @param array $blocks - array of blocks
Expand All @@ -78,6 +79,7 @@ function oiksc_get_blocks_byblock( $blocks ) {
, "compare" => "IN"
);
$atts['meta_query'] = $meta_query;
$atts['post_parent'] = 0;
$posts = bw_get_posts( $atts );
return( $posts );
}
Expand Down

0 comments on commit 1426dc0

Please sign in to comment.