Skip to content

fix(dynamic-content): send JSON responses for all source fields in BlocksAjax::get_post() #545

Open

Description

@shamiulbishal

Describe the Bug

The Dynamic Content toolbar popover (admin/dynamic-content/toolbar/dynamicContentPopover.js) and its backend AJAX handler (includes/Admin/Ajax/BlocksAjax.php) have several bugs that prevent dynamic fields from working:

  1. Missing JSON responses in BlocksAjax::get_post() (includes/Admin/Ajax/BlocksAjax.php, lines 176–233):
    • While post_title, post_excerpt, featured_image, post_date, and post_time call Helper::json_success($data), all remaining cases (post_custom_field, post_terms, post_id, comments_number, author_name, author_first_name, author_last_name, author_nickname, author_bio, author_email, author_website, author_avatar_image_url, and author_custom_field) use return ...; instead of Helper::json_success(...).
    • In WordPress admin-ajax.php, returning a value from an action callback without calling wp_send_json_* outputs 0, so the frontend receives an empty/failed response.
  2. Unsafe property access on Helper::get_single_field() (BlocksAjax.php, lines 202, 222, 225, 229):
    • Helper::get_single_field($post_id, 'comment_count') returns a scalar value ($post->$field_name), not an object, yet lines 202, 222, 225, and 229 access ->comment_count and ->post_author on the scalar return value, causing PHP warnings (Attempt to read property on string) and returning null.
  3. Typo in hasChanges check (admin/dynamic-content/toolbar/dynamicContentPopover.js, line 59):
    • initialValues stores { dataSource, sourceField, ... }, but hasChanges checks dataSource !== initialValues.item (initialValues.item is always undefined), causing hasChanges to always evaluate to true.

Steps to Reproduce

  1. Open the Block Editor and select a block with the Frontis Dynamic Content toolbar button.
  2. Choose Data Source: Post Type, select a post, and select Author -> Name (or Post -> Comments Number / Post ID).
  3. Click Apply.
  4. Observe that admin-ajax.php returns 0 (or triggers a PHP warning trying to read ->post_author on a string) and the block content is not updated.

Expected Behavior

  • Every source_field branch in BlocksAjax::get_post() should return a structured JSON response via Helper::json_success([$source_field => $value]).
  • Helper::get_single_field($_POST['post_id'], 'post_author') and 'comment_count' should be used directly as scalar values without dereferencing ->post_author or ->comment_count.
  • dynamicContentPopover.js should compare dataSource !== initialValues.dataSource and handle all returned field keys when updating the block content.

Affected Block or Component

Core Plugin / General

WordPress Version

6.7

PHP Version

8.2

Activity

  1. added
    focus: blocks
    type: bug
    on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

focus: blockstype: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions