• Resolved eliant

    (@eliant)


    I’m writing code to send emails when a Post is published, but only to those Users who have access to this Post based on the CC setting for the Post. Step 1: capture the CC setting for the Post. Step 2: identify all the WP capabilities allowed under this CC Post restriction. Step 3: cycle through Users with these capabilities and send emails. I’m missing Step 2: I need to learn the WP capabilities associated with this particular Post’s CC restriction.

    Is there a CC function I can call that will return the list of capabilities (Editor, Contributor, etc.) of a specific Restriction? Perhaps something from shortcodes.php.

    • This topic was modified 2 years, 7 months ago by eliant.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support mark l chaves

    (@mlchaves)

    Hey @eliant,

    Thanks so much for posting.

    You should be able to get the post ID through the global post object.

    For the Content Control restriction and roles, please have a look at the get_rules method in JP\CC\Site\Restrictions class.

    You’ll need to parse the array it returns to get the info you want. Then, you can cross-reference the restriction roles with WordPress capabilities if needed.

    We hope that helps.

    Let us know if you need anything else.

    Have a great day ??

    Thread Starter eliant

    (@eliant)

    I’m trying to see the CC information at Post Update time (see WP hook transition_post_status). When I add

    $restriction = Restrictions::get_rules( $post->ID );
    I get
    Uncaught Error: Class 'Restrictions' not found

    When I add

    $restriction = get_rules( $post->ID );
    I get
    Uncaught Error: Call to undefined function get_rules()

    I successfully used $membership = get_the_terms($post->ID,”membership”); to identify the CC restriction for the Post. If necessary, I can loop through the WP User Roles allowed under that particular CC restriction. Since I set up the restrictions, I know what they are. I just wanted the flexibility of adding or modifying CC rules without having to go back and edit my filter function.

    Thread Starter eliant

    (@eliant)

    Any thoughts on this?
    Did I explain clearly?

    Plugin Support mark l chaves

    (@mlchaves)

    Hey @eliant,

    Sorry for the delay.

    There are a couple of things that might be happening:

    1) First, Content Control loads only when the_content filter hook runs (it checks content restrictions before rendering content). So, we don’t think you’re going to be able to successfully run Restrictions::get_rules when transition_post_status runs.

    2) Make sure you’re running include_once( ABSPATH . '/wp-content/plugins/content-control/classes/Site/Restrictions.php' ); before you try to invoke Restrictions::get_rules. Again, you might be able to call it, but it may always return false if Content Control isn’t running.

    Again, we’re not sure if this is going to work out for you. But, please let us know if you have any other questions.

    Thanks!

    Thread Starter eliant

    (@eliant)

    I still don’t think i’m explaining properly.

    – I have the Post.
    – I know which CC restrictions apply to this Post (in this example, the restricted group is called “Resident”).

    Question:
    Is there a function in your plugin that returns an array of WP capabilities (like, “Subscriber” or “Editor”) based on the name of the restricted group (in this case, “Resident”)?

    Plugin Support mark l chaves

    (@mlchaves)

    Hey @eliant,

    Sorry if we misunderstood. What do you mean by “restricted group”?

    Do you mean the restriction title? If so, the answer is no. There isn’t.

    The get_rules public static class method is the closest thing we can think of. But now, we see that won’t work for your case.

    The other option is to write that API yourself to read in jp_cc_settings option value from the wp_options DB table and parse that array into a data structure that fits your needs. You should see the restriction title in that array.

    We hope that helps.

    Let us know if you need anything else.

    Have a great day ??

    Thread Starter eliant

    (@eliant)

    write that API yourself to read in jp_cc_settings option value from the wp_options DB table and parse that array into a data structure that fits your needs.
    That’s pretty much exactly what I was asking for, except I thought you might already have a function that reads in that jp_cc_settings option. That data value in the wp_options table is pretty complex. If you already have the code to read it in and parse it into its various pieces, can you just tell me the name of that function so I can re-purpose it?

    I would certainly share my results.

    Plugin Support mark l chaves

    (@mlchaves)

    Hey @eliant,

    If we’re following correctly, you might want to look at get_option().

    https://developer.www.ads-software.com/reference/functions/get_option/

    Example usage

    https://github.com/code-atlantic/content-control/blob/76a959d53bdee2699e8306f442e314103919f91d/classes/Options.php#L100

    Once you have that array of all Content Control settings, it’s a coding exercise to write your API that does a look-up via restriction title.

    Example look-up

    
    // Loop through the restrictions and grab the index for
    // the one called 'Sample page for logged in peeps'.
    foreach ( $options['restrictions'] as $key => $val ) {
        // Maybe pass in the title if you're in a function.
        if ($val['title'] === 'Sample page for logged in peeps') {
            echo '<p>The key for "Sample page for logged in peeps" is ' . $key;
            // Maybe return $key here if you're in a function.
        } // if
    } // foreach
    

    Have a great weekend ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘produce list of capabilities given the CC restriction’ is closed to new replies.