intoxstudio / content-aware-sidebars

WordPress Plugin to create and display custom sidebars according to the content being viewed. Fast and powerful.
https://dev.institute/wordpress-sidebars/
GNU General Public License v3.0
24 stars 3 forks source link

Allow the negation of conditions/condition groups #3

Closed Blasz closed 8 years ago

Blasz commented 9 years ago

It would be nice to be able to apply negation conditions to the sidebar displaying as per https://wordpress.org/support/topic/how-do-i-negate-a-condition

It would be desirable to show a sidebar on all pages except certain ones.

My current use case for this is wanting to display a sidebar on all pages that do not use a certain page template (effectively all pages with the default page template). I can only do this by manually adding each page to a condition group resulting in maintenance when new pages are added or by creating another custom template that is the same as the default one that would then be applied to each of the 'default' pages.

I don't think either workaround is satisfactory.

I can potentially make a pull request for this if need be.

intoxstudio commented 9 years ago

I have thought about this too and definitely think it is a good idea. In the GUI, it would not be a problem to implement (i.e. a condition group could be negated).

However, on the top off my head, I do not see how it is possible because the sidebar filtering is done in SQL, and the plugin will only "consider" contents/contexts that are relevant.

E.g. on a page, taxonomies will never be taken in to consideration, and on a post, page templates will never be taken in to consideration.

If you have a idea on how it could be done efficiently, please do make a pull request.

For your use case, have you tried the following: Make a Sidebar B with condition group All Pages and Replace:Sidebar A Make a Sidebar C with condition group and Forced Replace:Sidebar A

Of course, this would display Sidebar C instead of Sidebar A on the selected pages.

intoxstudio commented 9 years ago

Having slept on it, I think it is easier to implement than I expected.

First, I filter the condition groups (as it is today) into Set A Then, I fetch all condition groups with a negation key into Set B Finally, I get all the groups that are either in Set A or Set B And the relevant sidebars can now be displayed

The reasoning is that if a group appears in both Set A and B, it means that its conditions are satisfied in the current context, but that it is negated and therefore should not be displayed. If it is only in Set A, then it should be displayed in this context. If it is only in Set B, then its conditions are satisfied in another context, but because it is negated, it should be displayed.

There might be an edge case I have not thought about, so it will need testing.

intoxstudio commented 9 years ago

This has been implemented as of 96704db