surveywp / kk-star-ratings

kk Star Ratings wordpress plugin
https://wordpress.org/plugins/kk-star-ratings
Other
119 stars 44 forks source link

Overriding views, missing $payload parameter in filter #95

Closed stevygee closed 1 year ago

stevygee commented 4 years ago

Detailed description

In order to override the views (templates) the plugin is using to display stars in the frontend, it is currently possible to use the kksr::filter.view filters, for example kksr::filter.view:legend, to append or replace the markup using a function.

However, the $payload parameter is not included in the filter. This makes it quite painful to construct an useful template, because variables such as $score are not available and need to be queried from the database again (using get_post_meta() for example).

TL;DR This is an enhancement request to make overriding views easier.

Context

I want to override some of the views inside a theme and use my own templates. For example: I want to be able to change markup, the wording and use variables like $percentage, which was available in previous versions of the plugin.

Possible implementation

I propose to change line 42 in src/view.php: return apply_plugin_filters('view:'.$name, $content); to this: return apply_plugin_filters('view:'.$name, $content, $payload);

This way $payload is passed to filter functions and the variables inside can be used to construct a new template.

Alternatively, the plugin could look for templates inside the active theme directory (much like The Event Calendar does), so overriding views is as easy as copying and modifying some files.

You might also consider documenting the available filters somewhere. I was not able to find any documentation on them and instead had to dig through the code.

Your environment

To reproduce, add the following to a theme or plugin: <?php function example_legend_template( $markup ) { return '<div class="kksr-legend">Score: ' . $payload['score'] . '</div>'; } add_filter( 'kksr::filter.view:legend', 'example_legend_template' ); The score doesn't show up, because $payload is not available.

kamalkhan commented 4 years ago

Thank for your comment.

I think I just forgot to send the $payload through. I did not have the time to document all the hooks as my focus was to move to a stable release.

Moreover, the idea of looking for the template in the theme directory is a good choice to make.

I will see to it as soon as I get some spare time.

In the mean time you can send the $payload through by editing the plugin source as it will be available in the next release.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 14 days since being marked as stale.