EnlighterJS / Plugin.WordPress

:package: Official WordPress Plugin of EnlighterJS
http://wordpress.org/plugins/enlighter/
GNU General Public License v2.0
118 stars 17 forks source link

Can't see list of themes in backend now that I used a code snippet #251

Closed ajtruckle closed 4 years ago

ajtruckle commented 4 years ago

Recently I added this code snippet:

// add a custom filter to modify the theme list
add_filter('enlighter_themes', function ($themes){
    // DEBUG: just display the dataset - uncomment the following line to debug issues
    // echo '<pre>', print_r($themes, true), '</pre>';

    // just show the default theme 
    return array(
     );
});

That was based on our discussion here. The problem I have now discovered is in my backend:

Theme

I thought it would atleast show "Atomic" but it shows none.

The theme customizer still lists all of them.

Ideally the front-end will be stripped so the user can only use the chosen theme. But i thought the backend would show all of them incase I want to change my mind as to what the default theme is.

AndiDittrich commented 4 years ago

if you want to use Enlighter outside of bbPress you have to set at least one theme.

the empty array setting works for the TinyMCE plugin alone but not in conjunction with the Enlighter plugin (sorry for this - i'm testing the plugins standalone most of the time).

ajtruckle commented 4 years ago

if you want to use Enlighter outside of bbPress you have to set at least one theme.

That is why I was surprised that in the backend I could not choose a theme anymore. Maybe something else going on?

ajtruckle commented 4 years ago

I had to disable the code snippet so I could select the theme customizer.

Maybe:

Snippet

I should set the option to only run it in the front end or something? Or is there a bug? But by deactivating the above code I was able to select a theme in the backend. Activating the code I could no longer select a theme.

AndiDittrich commented 4 years ago

the issue is that the "customizer" theme is also an item within the array

    // list of build-in themes
    const THEMES = array(
        'enlighter' => 'Enlighter',
        'godzilla' => 'Godzilla',
        'beyond' => 'Beyond',
        'classic' => 'Classic',
        'mowtoo' => 'MooTwo',
        'eclipse' => 'Eclipse',
        'droide' => 'Droide',
        'minimal' => 'Minimal',
        'atomic' => 'Atomic',
        'rowhammer' => 'Rowhammer',
        'bootstrap4' => 'Bootstrap4',
        'dracula'=> 'Dracula',
        'monokai' => 'Monokai',
        'wpcustom' => 'Theme Customizer'
    );
ajtruckle commented 4 years ago

Ok, So what is the plan?

  1. We want to suppress the list of themes the user can select in the front end when creating a code block via bbPress.
  2. We do not want to suppress the list of themes in the backend, since only I have access to it.
AndiDittrich commented 4 years ago

give me a few minutes....

but generally: without additional KSES filters it is not possible to suppress this (users can still edit the raw content)

AndiDittrich commented 4 years ago

see: https://github.com/EnlighterJS/documentation/blob/master/wordpress/FilterHooks.md#filterenlighter_editor_themes

added in v4.2.0

AndiDittrich commented 4 years ago

for your use case: you have to add some ACL related code to determine if the current user is admin/manager and if the request is related to the frontend/backend

AndiDittrich commented 4 years ago

ACL related stuff is projected for the pro/enterprise editions only - sorry... (it requires a full rework of the TinyMCE plugin...)

you can easily do it on your own by building a custom version of the TinyMCE plugin and hook into the enlighter_resource filter

ajtruckle commented 4 years ago

So I need to use this:

// add a custom filter to modify the theme list
add_filter('enlighter_editor_themes', function($themes){
    // DEBUG: just display the dataset - uncomment the following line to debug issues
    // echo '<pre>', print_r($themes, true), '</pre>';

    // unset the theme
    unset($themes['godzilla']);

    return $themes;
});

And add some wrapper logic to see if :

Right?

AndiDittrich commented 4 years ago

theoretically...^^

snippets

// frontend or dashboard area ?
if (is_admin()){}

// check frontend user privileges
$canEdit = is_user_logged_in() && (current_user_can('edit_posts') || current_user_can('edit_pages'));
ajtruckle commented 4 years ago

I will look into it. Got work tonight so it will be tomorrow on my staging site.

The only other alternative was setting my snippet to only run in the front-end. If you look at the screen grab it has an option to run the snippet in backend or frontend or both. It is currently set to both.

ajtruckle commented 4 years ago

I don't think I am understanding this quite right:

// add a custom filter to modify the theme list
add_filter('enlighter_editor_themes', function($themes){
    // DEBUG: just display the dataset - uncomment the following line to debug issues
    // echo '<pre>', print_r($themes, true), '</pre>';

  if (!is_admin()) // Front end
  {
    // check frontend user privileges
    if ( is_user_logged_in() && (current_user_can('edit_topics') || current_user_can('edit_replies') ) )
      $canEdit = true ;

    if(!$canEdit) $themes = array();
  }

    // unset the theme
    unset($themes['godzilla']);

    return $themes;
});

Doh! I not downloaded your latest version.

ajtruckle commented 4 years ago

Looks like I am doing something wrong. It doesn't seem to work. But this seems complicated to me.

  1. When we are in the backend and I go to the EnlighterJS settings page it should list all the themes. At the moment it does show them all. So that is good.

  2. When we are in the front edit and creating a bbPress forum post/reply we do not want to list all of the themes in the tiny MCE editor.

I had disabled the first snippet (add_filter('enlighter_themes', function ($themes)) as that was originally preventing the themes to show.

So I am getting confused. Now I have the themes show everywhere.

Note that in bbPress the actions are edit_topics and edit_replies.

AndiDittrich commented 4 years ago

your snippet above should work with the latest snapshot. you should not use the enlighter_theme filter anymore for your use-case.

maybe your acl check is not correct. just try a simple clause with is_admin only

AndiDittrich commented 4 years ago

https://github.com/EnlighterJS/documentation/blob/master/wordpress/bbPress.md#limit-the-set-of-themes-visible-to-users-in-the-frontend

ajtruckle commented 4 years ago

That works great - thanks. :)

Noticed a typo in the comment wthin instead of within.