Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render_block_core_latest_posts() Add filter for The block attributes #11195

Closed
wants to merge 2 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Oct 29, 2018

Description

Add a filter so that plugins and themes can filter the block attributes.

Why is this needed.

Plugins may want to filter attributes in some situations. Eg. WPML will need to display posts in the current language only.

How has this been tested?

This has been tested with WPML using the following code example.

add_filter( 'render_block_core_latest_posts_attributes', 'render_block_core_latest_posts_attributes_filter' );
function render_block_core_latest_posts_attributes_filter( $attributes ) {
   $attributes['suppress_filters'] = false;
   return $attributes;
}

Types of changes

Add a new filter

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@andreasciamanna
Copy link

Will this change be included in WP 5.0?

@swissspidy
Copy link
Member

Shouldn't this be covered by #11730?

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's already a filter for the render_callback that is generic (across all blocks), I feel adding a specific filter like this is just a duplication.

@youknowriad
Copy link
Contributor

see #11523

@ghost
Copy link
Author

ghost commented Nov 14, 2018

@youknowriad @swissspidy it's little different because suppress_filters it's not a Gutenberg block attribute by default

image

@youknowriad
Copy link
Contributor

It is different, but you can achieve the same use-cases, maybe it's a bit more difficult in some cases and a bit easier in others.

@ghost
Copy link
Author

ghost commented Nov 14, 2018

@youknowriad I can filter $attributes array with 'render_block' filter but there no way to pass suppress_filters to wp_get_recent_posts there

@youknowriad
Copy link
Contributor

Can't you just regenerate the HTML output of the block the way you want?

@swissspidy
Copy link
Member

That sounds a bit hacky though, especially since the block HTML output could change.

Why does the block even use wp_get_recent_posts? A regular WP_Query would have been just fine. This way one doesn't have to work around suppress_filters limitations.

@andreasciamanna
Copy link

andreasciamanna commented Nov 14, 2018

I was wondering the same @swissspidy

I never saw a report from users complaining that the results of wp_get_recent_posts weren't filtered (WPML filters all queries when they deal with translatable post types), which makes me wonder how many themes use this function.

Anyway, if it should use wp_get_recent_posts, wouldn't make more sense to have a filter inside this function, instead of render_block_core_latest_posts?

@ghost
Copy link
Author

ghost commented Nov 14, 2018

added https://core.trac.wordpress.org/ticket/45349 for adding filter inside wp_get_recent_posts instead

@gziolo
Copy link
Member

gziolo commented Nov 14, 2018

I think it duplicates efforts started in #11730 which tries to be more generic. There is also a similar proposal which tries to modify a single attribute for Reusable block block: #8912.

@ghost
Copy link
Author

ghost commented Nov 16, 2018

@youknowriad @gziolo is it ok if I modify pull request and replace wp_get_recent_posts on regular WP_Query like @swissspidy proposed?

@swissspidy
Copy link
Member

Probably easier to do that in a separate PR as it's a totally different approach.

@gziolo
Copy link
Member

gziolo commented Nov 16, 2018

I'm closing this one in favor of #11730, which tries to do the same but for all blocks. This is example from the mentioned PR:

add_filter( 'block_prepared_attributes', 'block_attributes_filter', 1, 10 );
function block_attributes_filter( $attributes, $block_type ) {
    if ( $block_type->name === 'my-plugin/my-block' &&  isset( $attributes['data'] ) ) {
            $original_post_id = apply_filters( 'wpml_object_id', get_the_ID(), get_post_type(), true, apply_filters( 'wpml_default_language', null) );
            foreach ( $attributes['data'] as $name => $value ) {
                $string_name = WPML_Gutenberg_Strings_In_Block::get_string_id($attributes['name'], $value);
                $translated = apply_filters( 'wpml_translate_string',
                    $value,
                    $string_name,
                    array('kind' => 'Gutenberg', 'name' => $original_post_id) );
                $attributes['data'][$name] = $translated;
            }
        }

        return $attributes;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants