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

Adding/removing user roles #50

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

Conversation

matapatos
Copy link

Make it easy to add and remove user roles.

@Log1x
Copy link
Owner

Log1x commented Mar 17, 2023

Thanks for the PR!

I thought about adding this in the past but when you add a role to WordPress, it saves it to the database. This means we need some kind of conditional to wrap add_role() otherwise it is saved to the database every time a page loads. A way to do this would be to leverage update_option() or the like with a key referencing which roles are already added, but I feel like this could get a little messy when it comes to having everything saved in a config file like Poet does.

Ref: https://developer.wordpress.org/reference/functions/add_role/#comment-3194

@matapatos
Copy link
Author

matapatos commented Mar 18, 2023

You are right, doesn't makes sense to do a DB operation on every page load.

And if we use the update_option to keep track of the currently deployed database version?

add_action('update_option_deploy','poet_user_roles', 10, 2);
add_action('update_site_option_deploy','poet_user_roles', 10, 2); //For multisite installations

function poet_user_roles($oldValue, $newValue){
    if ($oldValue == $newValue) {
        return;
    }

    // Add or remove user roles here
    add_role('librarian', 'Librarian');
}

And add the following to Trellis:

- name: Update deploy option
  tags:
    - wordpress:deploy
  command: "wp option update deploy {{ deploy_helper.new_release_path | basename }} --url={{ item }}"
  become: true
  become_user: "{{ web_user }}"
  args:
    chdir: "{{ deploy_helper.new_release_path }}"
  changed_when:
    - "'Success: Updated ' in wp_options_update.stdout"
  register: wp_options_update
  loop: "{{ wordpress_sites.values() | map(attribute='site_hosts') | flatten | map(attribute='canonical') | flatten }}"

PS: I know that it's getting out of scope here. But it also brings some advantages besides knowing which current version is deployed using the DB, we could also use this option when restoring a database to a previous state using a backup, where we could know exactly which code version was running at the time.

@matapatos
Copy link
Author

matapatos commented Mar 23, 2023

@Log1x I added a condition using the wp_roles()->is_role($slug) function - which avoids an unnecessary database query. Tested using Query Monitor with a logged in and non logged in user.

WP_Roles is initialised by WordPress here.

Let me know what you think.

PS: Forget my last comment

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.

None yet

2 participants