WordPress.org

Ready to get started?Download WordPress

Forums

Peter's Collaboration E-mails
[resolved] Taxonomy rules not affecting email (14 posts)

  1. boxcarpress
    Member
    Posted 1 year ago #

    When I create rules based on a taxonomy, the emails only go out to the default (admin). I believe you are finding these rules in your code, however you are not assigning their values to the $post_type_moderators variable. You should add at line 415 of wordpress-collaboration-emails.php (in 1.6.2):
    $post_type_moderators[] = $post_type_rule->moderators;
    Hope this helps!

    http://wordpress.org/extend/plugins/peters-collaboration-e-mails/

  2. Peter
    Member
    Plugin Author

    Posted 1 year ago #

    Hi, that shouldn't be the case, because lines 163-169 are what add the moderators to the list. What is the exact case you are trying to capture? Is it a post being submitted for review? What taxonomy rule are you testing (category, tag, post type, other)?

  3. boxcarpress
    Member
    Posted 1 year ago #

    Right. Those moderators are gathered in line 161 via the function pce_get_post_type_moderators which, among other things, looks at the taxonomy rules in the database. The moderators found in those taxonomy rules are never assigned to the $post_type_moderators variable within the pce_get_post_type_moderators function, which is why I added line 415. I'm don't think it's relevant, but I'm using a custom non-hierarchical taxonomy called 'section' for a Page. The pce_get_post_type_moderators finds this rule just fine in line 379, but the function just doesn't return these moderators--it returns an empty array instead. This causes the plugin to send out emails to the default moderators instead of the ones defined in the taxonomy rules.

  4. Peter
    Member
    Plugin Author

    Posted 1 year ago #

    Sorry about that and thanks for clarifying. I think I was reading 1.6.2 as line 162 instead of line 415 as you said.

    Unfortunately I still can't reproduce the problem, but that doesn't mean the problem doesn't exist. What does your rule look like after you've added it? Is it something like this?

    Post type: section
    Taxonomy: all
    Term: *** n/a ***

    In pce_get_post_type_moderators(), $post_type_rule isn't intended to directly add to $post_type_moderators because it might not apply (if it's supposed to match on a specific tag, for example). At the end of the function, the "all" rule is added, but based on the assumption that the term for the "all" rule is an empty string. That might not actually be the case in all database setups and I suspect that to be the case if your rule is as outlined above. To help troubleshoot, could you please paste a var_dump or export of $post_type_moderator_rules just before the end of pce_get_post_type_moderators()?

  5. boxcarpress
    Member
    Posted 1 year ago #

    Actually, here is the situation (sorry if I didn't explain this clearly before):

    Post type: Page
    Taxonomy: Section
    Term: Meta

    Here are the variables at the end of pce_get_post_type_moderators() before and after my edit suggested above:

    $post_type_moderator_rules:

    Array
    (
        [section] => Array
            (
                [28] => a:2:{i:0;i:37;i:1;i:8;}
                [meta] => a:2:{i:0;i:37;i:1;i:8;}
            )
    
    )

    $post_type_moderators before my suggested edit:

    Array
    (
    )

    $post_type_moderators after my suggested edit:

    Array
    (
        [0] => a:2:{i:0;i:37;i:1;i:8;}
    )
  6. Peter
    Member
    Plugin Author

    Posted 1 year ago #

    If $post_type_moderators isn't being filled without your suggested edit, that means that it's not matching on the term "Meta" for some reason. Unless you've somehow forgotten to check the "Meta" term in the "Section" taxonomy or you're using a completely separate editorial interface, it should be matching based on the values in $_POST['tax_input'] variable. If that's not the case, unfortunately I'm stumped on this one :(

  7. boxcarpress
    Member
    Posted 1 year ago #

    Well, I've mentioned that pce_get_post_type_moderators() is finding the correct results based on taxonomy. They are correct in $post_type_moderator_rules. When you look at the code, where exactly do you put these values in the $post_type_moderators that you return? When you look at tax_input, post_category, etc. you have a line:
    $post_type_moderators[] = $whatever;
    ...that appears to be missing in the case of taxonomy rules. So, I added line 145:
    $post_type_moderators[] = $post_type_rule->moderators;
    Then, everything works as it should.

  8. Peter
    Member
    Plugin Author

    Posted 1 year ago #

    Hi,

    It is in the tax_input (which looks at the taxonomy rules) and post_category sections where moderators are added to the $post_type_moderators variable. The section of code above that, which builds $post_type_moderator_rules, is only supposed to "re-format" the rules for the tax_input and post_category sections to check against. The reason why I don't wholesale add to $post_type_moderators in the section that you are referring to is so that I can match against a category or other taxonomy rules; otherwise ALL rules for a particular post type will apply in all cases. That is why your code addition works as you intend it to, but that means that if other terms are selected, they would also match against the "Meta" rule (which they shouldn't). In other words, if you have two rules such as this:

    Post type: Page
    Taxonomy: Section
    Term: Meta

    Post type: Page
    Taxonomy: Section
    Term: Sidebar

    ... the pce_get_post_type_moderators() function looks up ALL rules for the "Page" post type, and then only applies those rules to those pages that match on the "Meta" Section and/or the "Sidebar" Section. If you are not getting the expected list of moderators, then either the rule didn't actually match the submitted page or the code that is trying to make the match (the tax_input and post_category sections) is flawed (which I'm still trying to reproduce).

  9. boxcarpress
    Member
    Posted 1 year ago #

    Aha. I had overlooked that block of code because of if the if( isset( $_POST['tax_input'] ) ). As it happens, the $_POST in my case does not contain this attribute, so these rules are skipped. Perhaps this is because of the user capabilities (cannot edit custom taxonomy) or because I'm using the Revisionary plugin. The user submitting the post is a "Revisor." Which makes me wonder, is there a way to determine the taxonomy of the Page without relying on the $_POST variable?

  10. Peter
    Member
    Plugin Author

    Posted 1 year ago #

    Good question(s).

    I haven't used Revisionary, but I don't think that would be causing the issue.

    If your user cannot edit a custom taxonomy, does that mean that they wouldn't be able to specify ANY value? Then for initial versions your taxonomy-based rule wouldn't be able to apply, unless you can set a default value. I do remember looking into alternatives to checking $_POST['tax_input'] (I don't like having to check that POST variable directly) for the first version of a post, and couldn't find any, because capturing the post transition happened too early in the workflow. Any tips are welcome!

    As for subsequent revisions on a post, I could envision having a fallback check to see what the existing taxonomy values are for edits to existing content (when there is no $_POST['tax_input'] value) -- something to add for a future version!

  11. boxcarpress
    Member
    Posted 1 year ago #

    This isn't too pretty because it's late, but I have this working by iterating through the rules (and checking against the page), rather than trying to fetch all the page's terms:

    if( count($post_type_moderator_rules>0) )
    	foreach($post_type_moderator_rules as $post_type_moderator_rule){
     	 if( count($post_type_moderator_rule>0) )
    	 foreach($post_type_moderator_rules as $taxonomy=>$rules){
     	 	if( count($rules>0) )
    	 	foreach($rules as $term=>$rule){
    	 		if(has_term($term,$taxonomy))
    			 	$post_type_moderators[] = $rule;
    	 	}
    	}
     }

    I just stuck this in above the isset( $_POST['tax_input'] ) and it appears to work as intended. You tell me, though!

  12. Peter
    Member
    Plugin Author

    Posted 1 year ago #

    Great tip on using has_term()!

    I'll look at rewriting pce_get_post_type_moderators() to use has_term() instead of checking POST variables directly. I'll have to carve out some time to implement it and test all of the cases, so that will take a while. However, this is the first version:

    function pce_get_post_type_moderators( $post_object )
    {
        global $wpdb, $pce_db_collabrules;
    
        $post_type_moderators = array();
    
        // Preserve this post object no matter what type it is so that we can check taxonomies
        $current_post_object = $post_object;
    
        // Get this post's taxonomies
        // First, find out its post type
        // If post type is a revision, fetch the post's parent
        if( 'revision' == $post_object->post_type )
        {
            $post_object = get_post( $post_object->post_parent );
        }
    
        // Get moderators for $post_object->post_type
        $post_type_rules = $wpdb->get_results( 'SELECT taxonomy, term, moderators FROM ' . $pce_db_collabrules . ' WHERE post_type = \'' . $post_object->post_type . '\'', OBJECT );
    
        if( $post_type_rules )
        {
            foreach( $post_type_rules as $post_type_rule )
            {
                // Note that terms are case sensitive!
                if( 'all' == $post_type_rule->taxonomy || has_term( $post_type_rule->term, $post_type_rule->taxonomy, $current_post_object ) )
                {
                    $post_type_moderators[] = $post_type_rule->moderators;
                }
            }
        }
    
        return $post_type_moderators;
    }
  13. boxcarpress
    Member
    Posted 1 year ago #

    I can confirm that this works for my situation! Thanks Peter, for working this out.

    Update: Forgot to mention earlier...I had to take the optional third parameter out of has_term() in order for this to work. So, the final if block would read:

    if( 'all' == $post_type_rule->taxonomy || has_term( $post_type_rule->term, $post_type_rule->taxonomy ) )
                {
                    $post_type_moderators[] = $post_type_rule->moderators;
                }

    Thanks
    Harold

  14. Peter
    Member
    Plugin Author

    Posted 1 year ago #

    Great! Thanks for... collaborating on this :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.