![5d8b0e13bdf2f962156a1b7c1665683f](https://user-images.githubusercontent.com/48084051/229556850-49b37d01-97da-485b-b39f-d13d8077bd2a.jpg)

## Dynamic Connector Block

Dynamic connector block allows you to add dynamic content based on different conditions on each post.

## Usage

To get started with the Automatic Block Inserter plugin, follow these steps after installation:

### 1️⃣ Creating a dynamic block

![CleanShot 2024-05-13 at 21 15 30@2x](https://github.com/smallplugins/dynamic-connector-block/assets/48084051/1eaa586e-e518-4a31-82e6-0c6b3b4537f0)

1. In your WordPress dashboard, locate and click on the "Dynamic Connector Block" menu item.
2. Click on "Add New" to create a new block.

### 2️⃣ Creating the content

![CleanShot 2024-05-13 at 21 19 16@2x](https://github.com/smallplugins/dynamic-connector-block/assets/48084051/9202b483-7f4e-4bdc-8bad-d2f3668cfd09)

Let's make a dynamic a "Call to action" block. You can simply create the block content by adding necessary blocks in the WordPress editor as you normally do for posts/pages.

### 3️⃣ Configuring block rulesets

Now that we have our dynamic block ready. We can now configure the block rulesets. This block will only be eligible to be displayed on a post if that post satisfies the rulesets defined on this dynamic block.

https://github.com/smallplugins/dynamic-connector-block/assets/48084051/a4ebafea-5479-4b36-940c-b079d3709001

**💡 Pro tip:** On the premium version of the plugin. You can optionally select if you want all the rulesets to be satisfied at once (see below).

<img src="https://github.com/smallplugins/dynamic-connector-block/assets/48084051/ce9cd3d1-f584-441c-8b3a-521c2f17af46" width="300" />

### 4️⃣ Setting the priority

Dynamic blocks are processed based on the priority. Consider a scenario where a 2 different blocks are eligible to displayed on a certain post. Dynamic block with the higher priority will take precedence to be displayed over the other block with low priority.

Let's configure our block priority.

https://github.com/smallplugins/dynamic-connector-block/assets/48084051/a1b24cbf-b9c4-4ce3-a3f8-42c576cb03a4

### 5️⃣ Adding dynamic block

You can now insert the block into a page/post in your WordPress editor.

![CleanShot 2024-05-13 at 21 43 40@2x](https://github.com/smallplugins/dynamic-connector-block/assets/48084051/4bb9e04a-9b40-42f6-a370-a0bba3e36549)

## Premium version

| Feature           | Plan | Description                                             |
| :---------------- | :--- | :------------------------------------------------------ |
| Groups            | pro  | Group multiple dynamic blocks                           |
| Advanced Rulesets | pro  | Advanced ruleset features (like "Match all conditions") |

## Hooks and Filters

Dynamic connector block comes with hooks and filters that allows developers to do necessary changes smoothly.

### `dynamic_connector_block_query_args`

This filter allows you to add custom query argument before the actual block query is processed.

#### Arguments

| Parameter    | Type    | Description                                                                                   |
| :----------- | :------ | :-------------------------------------------------------------------------------------------- |
| `query`      | `array` | The current [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/) arguments |
| `attributes` | `array` | Current block attributes                                                                      |
| `post_id`    | `int`   | Post id at which the block is processing the result                                           |

#### Example

Here is a quick example using the filter to modify the queried posts scoped to the searched term "Recipe".

```php
add_filter( 'dynamic_connector_block_query_args', function( $query, $attributes, $post_id ) {

    $query[ 's' ] = 'Recipe';
    return $query;

}, 10, 3 );
```

### `dynamic_connector_block_conditional_posts`

This filter allows you to modify the queried posts result obtained from the `WP_Query` query. Can be useful to add complex condition on posts required to your needs.

#### Arguments

| Parameter    | Type        | Description                                         |
| :----------- | :---------- | :-------------------------------------------------- |
| `$posts`     | `WP_Post[]` | Currently queried posts objects                     |
| `attributes` | `array`     | Current block attributes                            |
| `post_id`    | `int`       | Post id at which the block is processing the result |

#### Example

The following basic example shortlists the queried posts to posts which title starts with the term "Recipe".

```php
add_filter( 'dynamic_connector_block_conditional_posts', function( $posts, $attributes, $post_id ) {

  $shortlisted_posts = array();

  foreach ( $posts as $post ) {

      if ( str_starts_with( $post->post_title, 'Recipe' ) ) {
          $shortlisted_posts[] = $post;
      }

  }

  return $shortlisted_posts;
}, 10, 3 )
```

## Changelog

= Version 1.0.9

-   New: Add support for alternative rendering method.

= Version 1.0.8

-   Dev: Update freemius SDK and maintenance update.

= Version 1.0.7

-   Dev: Bump freemius SDK.

= Version 1.0.6

-   Dev: Bump freemius SDK.

= Version 1.0.5

-   Refactor: Add longer prefix.
-   Refactor: Add GPL license header.
-   Fix: Prevent direct access to PHP files.

= Version 1.0.4

-   Refactor: Add licensing support.

= Version 1.0.3

-   Fix: Missing group selector on the block with Automatic block inserter plugin.

= Version 1.0.2

-   Tweak: Add support for ABI post type.

= Version 1.0.1

-   Dev: Update freemius SDK.

= Version 1.0.0

-   New: Initial Release.
