jaredcobb / ccb-core

Church Community Builder Core API - A WordPress plugin to sync your church data
http://www.wpccb.com
GNU General Public License v2.0
8 stars 5 forks source link

Request for Campus #18

Closed mikesyph closed 6 years ago

mikesyph commented 6 years ago

Hey Jared,

First of all, thank you so much for all your hard work with this plugin! It has been incredibly helpful for our church. Thanks for being so intentional on making it so flexible to customize. I'm very new to wordpress development and have been learning a lot with your documentation.

I'm I want to make just a quick request that may be useful for some other users as well. What do you think about the idea of adding a restriction on the API settings page to select all campuses or a single campus to sync? I'm thinking this selection would appear after a successful credential test.

I'm aware this may be done with a filter in functions.php, but I bring it up because I'm using your plugin in a multisite install and would love to sync just a specific campus's groups rather than all of them for every site on the network (232 groups total). Thanks for your consideration. I may also try and see if I can figure it out myself but I just wanted to bring it up as an idea.

jaredcobb commented 6 years ago

I'm glad the plugin has been useful! Unfortunately the CCB API itself (specifically the group_profiles service) doesn't have an option to limit / filter based on campus. That service always responds with all groups (even inactive & non-public groups).

But you can certainly filter them down further (with a hook) before they get inserted which has effectively the same result.

If all your sites share the same theme you can customize this snippet and add it to your theme's functions.php. You would just match your Campus Names (from CCB) to your WordPress blog ids:

/**
 * Callback function for `ccb_core_synchronizer_entity_insert_allowed` and
 * `ccb_core_synchronizer_entity_update_allowed` so that we can filter the
 * groups based on the campus this multisite instance matches.
 *
 * @param    bool      $allowed Whether an insert/update is allowed.
 * @param    SimpleXML $entity The specific entity object.
 * @param    mixed     $entity_id A unique entity id.
 * @param    string    $post_type The current post type.
 * @return   bool
 */
function my_site_entity_insert_update_allowed( $allowed, $entity, $entity_id, $post_type ) {
    // If another filter already blacklisted this group, respect that setting.
    // (For example, it might be inactive or private).
    if ( ! $allowed ) {
        return false;
    }

    if ( 'ccb_core_group' === $post_type ) {
        // This map hepls to organize your campuses.
        // The key is the campus name in CCB and the value is the WordPress blog ID.
        $campus_site_map = [
            'Some Campus Name'     => 2,
            'Another Great Campus' => 5,
            'Yet another Campus'   => 4,
        ];

        // The $entity represents a CCB group and should have a `campus` value.
        $blog_id = false;
        if ( ! empty( $entity->campus ) ) {
            $campus = (string) $entity->campus;
            if ( ! empty( $campus_site_map[ $campus ] ) ) {
                $blog_id = $campus_site_map[ $campus ];
            }
        }
        return get_current_blog_id() === $blog_id;
    }

    return $allowed;
}
add_filter( 'ccb_core_synchronizer_entity_insert_allowed', 'my_site_entity_insert_update_allowed', 20, 4 );
add_filter( 'ccb_core_synchronizer_entity_update_allowed', 'my_site_entity_insert_update_allowed', 20, 4 );

Some notes:

jaredcobb commented 6 years ago

BTW, making this a setting in the dashboard is a good idea! I'll definitely consider that for the next version.

mikesyph commented 6 years ago

Wow I did not expect a response so quickly! Thank you so much for putting this together so quickly. Above that thanks for making it work with the same theme! I was thinking I'd have to fiddle with it and then use multiple child themes for the different campuses.

I'll do some testing and let you know how it all goes. Thank you thank you thank you

mikesyph commented 6 years ago

I've been playing around with it and am getting an 'Illegal Offset Type' error from $blog_id = $campus_site_map[ $entity->campus ] ?: false;

Trying to make myself useful while being new to php arrays I found that passing the $entity->campus array as the key might be what it was. Tried some fixes but I didn't have any luck. Thanks for your help

jaredcobb commented 6 years ago

I was probably trying to be too clever and combine several things. It sounds like the campus may need to be cast to a string first. See if this works:

// The $entity represents a CCB group and should have a `campus` value.
$blog_id = false;
if ( ! empty( $entity->campus ) ) {
    $campus = (string) $entity->campus;
    if ( ! empty( $campus_site_map[ $campus ] ) ) {
        $blog_id = $campus_site_map[ $campus ];
    }
}
return get_current_blog_id() === $blog_id;
mikesyph commented 6 years ago

Alright no longer getting an error but currently not having any groups getting pulled in.

function my_site_entity_insert_update_allowed( $allowed, $entity, $entity_id, $post_type ) {
    // If another filter already blacklisted this group, respect that setting.
    // (For example, it might be inactive or private).
    if ( ! $allowed ) {
        return false;
    }

    if ( 'ccb_core_group' === $post_type ) {
        // This map hepls to organize your campuses.
        // The key is the campus name in CCB and the value is the WordPress blog ID.
        $campus_site_map = [
            'Grace Ohio - Bath' => 3,
        ];

        // The $entity represents a CCB group and should have a `campus` value.
        $blog_id = false;
        if ( ! empty( $entity->campus ) ) {
            $campus = (string) $entity->campus;
            if ( ! empty( $campus_site_map[ $campus ] ) ) {
                $blog_id = $campus_site_map[ $campus ];
            }
        }
        return get_current_blog_id() === $blog_id;
    }
    return $allowed;
}
add_filter( 'ccb_core_synchronizer_entity_insert_allowed', 'my_site_entity_insert_update_allowed', 20, 4 );
add_filter( 'ccb_core_synchronizer_entity_update_allowed', 'my_site_entity_insert_update_allowed', 20, 4 );

I think I have everything all set correctly but throught it might be worth posting here in case I didn't understand something correctly. I'm just testing 1 campus at the moment on the template2018 site.

From phpMyAdmin image

CCB XML image

Thanks for all your help on this.

mikesyph commented 6 years ago

Nevermind, I figured out the issue was I was trying to pull the group images. My environment runs into timeout errors when pulling all the images at once. If I pull the groups without images first, switch images on, delete 40 groups or so, syncronize will then pull those 40, delete a different 40, etc. it pulls them all just fine. Thanks for all your help Jared!

jaredcobb commented 6 years ago

Ah, great to hear. Yes, I've heard some reports of group images causing issues with other folks as well. That's definitely on my todo list of next enhancements... Figuring out why those timeouts are so bad and determine how to best handle pulling those images down at that scale.