Skip to content

Commit

Permalink
Merge pull request #150 from moderntribe/feature/MOOSE-132/block-bind…
Browse files Browse the repository at this point in the history
…ing-api-main

[MOOSE-132] Block Binding API Support
  • Loading branch information
GeoffDusome authored Aug 12, 2024
2 parents f0c50d0 + 3af0288 commit f0e8ea1
Show file tree
Hide file tree
Showing 27 changed files with 241 additions and 478 deletions.
40 changes: 40 additions & 0 deletions wp-content/plugins/core/src/Blocks/Bindings/Binding_Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Tribe\Plugin\Blocks\Bindings;

abstract class Binding_Base implements Binding_Interface {

protected string $slug = '';
protected string $label = '';

/**
* @var mixed[]
*/
protected array $get_value_callback = [];

/**
* @var string[]
*/
protected array $uses_context = [];

abstract protected function get_args(): array;

public function __construct() {
foreach ( $this->get_args() as $key => $value ) {
if ( ! property_exists( $this, $key ) || $key === self::SLUG ) {
continue;
}

$this->{$key} = $value;
}
}

public function get_properties(): array {
return array_filter( [
self::LABEL => $this->label,
self::GET_VALUE_CALLBACK => $this->get_value_callback,
self::USES_CONTEXT => $this->uses_context,
] );
}

}
16 changes: 16 additions & 0 deletions wp-content/plugins/core/src/Blocks/Bindings/Binding_Interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Tribe\Plugin\Blocks\Bindings;

interface Binding_Interface {

public const SLUG = 'slug';
public const LABEL = 'label';
public const GET_VALUE_CALLBACK = 'get_value_callback';
public const USES_CONTEXT = 'uses_context';

public function get_slug(): string;

public function get_properties(): array;

}
15 changes: 15 additions & 0 deletions wp-content/plugins/core/src/Blocks/Bindings/Binding_Registrar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace Tribe\Plugin\Blocks\Bindings;

class Binding_Registrar {

public function register( Binding_Interface $binding ): void {
if ( ! function_exists( 'register_block_bindings_source' ) ) {
return;
}

register_block_bindings_source( $binding->get_slug(), $binding->get_properties() );
}

}
13 changes: 10 additions & 3 deletions wp-content/plugins/core/src/Blocks/Blocks_Definer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use DI;
use Tribe\Libs\Container\Definer_Interface;
use Tribe\Plugin\Blocks\Filters\Contracts\Filter_Factory;
use Tribe\Theme\bindings\Post_Permalink;
use Tribe\Theme\bindings\Post_Type_Name;
use Tribe\Theme\bindings\Query_Results_Count;
use Tribe\Theme\blocks\core\button\Button;
use Tribe\Theme\blocks\core\column\Column;
use Tribe\Theme\blocks\core\columns\Columns;
Expand Down Expand Up @@ -34,13 +37,11 @@ class Blocks_Definer implements Definer_Interface {
public const CORE = 'blocks.core';
public const PATTERNS = 'blocks.patterns';
public const FILTERS = 'blocks.filters';
public const BINDINGS = 'blocks.bindings';

public function define(): array {
return [
self::TYPES => DI\add( [
'tribe/post-type-name',
'tribe/post-permalink',
'tribe/query-results-count',
'tribe/terms',
] ),

Expand Down Expand Up @@ -74,6 +75,12 @@ public function define(): array {
self::FILTERS => DI\add( [
] ),

self::BINDINGS => DI\add( [
DI\get( Post_Permalink::class ),
DI\get( Post_Type_Name::class ),
DI\get( Query_Results_Count::class ),
] ),

Filter_Factory::class => DI\autowire()->constructorParameter( 'filters', DI\get( self::FILTERS ) ),
];
}
Expand Down
6 changes: 6 additions & 0 deletions wp-content/plugins/core/src/Blocks/Blocks_Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tribe\Plugin\Blocks;

use Tribe\Libs\Container\Abstract_Subscriber;
use Tribe\Plugin\Blocks\Bindings\Binding_Registrar;
use Tribe\Plugin\Blocks\Filters\Contracts\Filter_Factory;
use Tribe\Plugin\Blocks\Patterns\Pattern_Category;
use Tribe\Plugin\Blocks\Patterns\Pattern_Registrar;
Expand Down Expand Up @@ -30,6 +31,11 @@ public function register(): void {
foreach ( $this->container->get( Blocks_Definer::PATTERNS ) as $pattern ) {
$this->container->get( Pattern_Registrar::class )->register( $pattern );
}

// Register block bindings.
foreach ( $this->container->get( Blocks_Definer::BINDINGS ) as $binding ) {
$this->container->get( Binding_Registrar::class )->register( $binding );
}
}, 10, 0 );

/**
Expand Down
10 changes: 10 additions & 0 deletions wp-content/themes/core/assets/pcss/templates/search.pcss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* -------------------------------------------------------------------------
*
* Templates: Search
*
* ------------------------------------------------------------------------- */

/* ported from query results count block (now using block bindings) */
.search-no-results .tribe-query-results-count {
display: none;
}
3 changes: 3 additions & 0 deletions wp-content/themes/core/assets/pcss/theme.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
/* Patterns */
@import "cards/post.pcss";
@import "cards/post-search-result.pcss";

/* Templates */
@import "templates/search.pcss";
33 changes: 33 additions & 0 deletions wp-content/themes/core/bindings/Post_Permalink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace Tribe\Theme\bindings;

use Tribe\Plugin\Blocks\Bindings\Binding_Base;

class Post_Permalink extends Binding_Base {

/**
* example markup:
<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"tribe/post-permalink"}}}} -->
<p>Post Permalink Placeholder</p>
<!-- /wp:paragraph -->
*/

public function get_slug(): string {
return 'tribe/post-permalink';
}

public function get_args(): array {
return [
Binding_Base::LABEL => __( 'Post Permalink', 'tribe' ),
Binding_Base::GET_VALUE_CALLBACK => [ $this, 'tribe_get_post_permalink' ],
];
}

public function tribe_get_post_permalink(): string {
$post_permalink = get_the_permalink();

return esc_html( $post_permalink );
}

}
44 changes: 44 additions & 0 deletions wp-content/themes/core/bindings/Post_Type_Name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);

namespace Tribe\Theme\bindings;

use Tribe\Plugin\Blocks\Bindings\Binding_Base;

class Post_Type_Name extends Binding_Base {

/**
* example markup:
<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"tribe/post-type-name"}}}} -->
<p>Post Type Name Placeholder</p>
<!-- /wp:paragraph -->
*/

public function get_slug(): string {
return 'tribe/post-type-name';
}

public function get_args(): array {
return [
Binding_Base::LABEL => __( 'Post Type Name', 'tribe' ),
Binding_Base::GET_VALUE_CALLBACK => [ $this, 'tribe_get_post_type_name' ],
];
}

public function tribe_get_post_type_name(): string {
// this gets us the post type, but we really want the name
$block_post_type = get_post_type();

if ( ! $block_post_type ) {
return '';
}

$post_object = get_post_type_object( $block_post_type );

if ( ! $post_object ) {
return '';
}

return esc_html__( $post_object->labels->singular_name, 'tribe' );
}

}
49 changes: 49 additions & 0 deletions wp-content/themes/core/bindings/Query_Results_Count.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace Tribe\Theme\bindings;

use Tribe\Plugin\Blocks\Bindings\Binding_Base;

class Query_Results_Count extends Binding_Base {

/**
* example markup:
<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"tribe/query-results-count"}}}} -->
<p>Query Results Count Placeholder</p>
<!-- /wp:paragraph -->
*/

public function get_slug(): string {
return 'tribe/query-results-count';
}

public function get_args(): array {
return [
Binding_Base::LABEL => __( 'Query Results Count', 'tribe' ),
Binding_Base::GET_VALUE_CALLBACK => [ $this, 'tribe_get_query_results_count' ],
];
}

public function tribe_get_query_results_count(): string {
global $wp_query;
$is_search = is_search();
$count = (int) $wp_query->found_posts;
$output = sprintf( _n( '%d result', '%d results', $count, 'tribe' ), number_format_i18n( $count ) );

if ( $is_search ) {
$output = sprintf(
_x(
'%s %s for <span class="search-term" style="font-weight:var(--wp--custom--font-weight--bold)">&ldquo;%s&rdquo;</span>',
'First value is the number of results, second is word "result" (pluralized if necessary), third is the search term',
'tribe'
),
number_format_i18n( $count ),
_n( 'result', 'results', $count, 'tribe' ),
get_search_query()
);
}

return wp_kses_post( $output );
}

}
21 changes: 0 additions & 21 deletions wp-content/themes/core/blocks/tribe/post-permalink/block.json

This file was deleted.

32 changes: 0 additions & 32 deletions wp-content/themes/core/blocks/tribe/post-permalink/edit.js

This file was deleted.

60 changes: 0 additions & 60 deletions wp-content/themes/core/blocks/tribe/post-permalink/index.js

This file was deleted.

Loading

0 comments on commit f0e8ea1

Please sign in to comment.