Skip to content

Instantly share code, notes, and snippets.

@msaari
Created May 10, 2018 02:57
Show Gist options
  • Select an option

  • Save msaari/e067346fc1af91dfac90ce9e6c8d46b1 to your computer and use it in GitHub Desktop.

Select an option

Save msaari/e067346fc1af91dfac90ce9e6c8d46b1 to your computer and use it in GitHub Desktop.
Correct Simple Membership support
<?php
// Replace the relevanssi_default_post_ok() function in the file lib/common.php with this:
/**
* Checks whether the user is allowed to see the post.
*
* The default behaviour on 'relevanssi_post_ok' filter hook. Do note that while
* this function takes $post_ok as a parameter, it actually doesn't care much
* about the previous value, and will instead overwrite it. If you want to make
* sure your value is preserved, either disable this default function, or run
* your function on a later priority (this defaults to 10).
*
* Includes support for various membership plugins. Currently supports Members,
* Groups, Simple Membership and s2member.
*
* @param boolean $post_ok Can the post be shown to the user.
* @param int $post_id The post ID.
*
* @return boolean $post_ok True if the user is allowed to see the post,
* otherwise false.
*/
function relevanssi_default_post_ok( $post_ok, $post_id ) {
$status = relevanssi_get_post_status( $post_id );
// If it's not public, don't show.
if ( 'publish' !== $status ) {
$post_ok = false;
}
// Let's look a bit closer at private posts.
if ( 'private' === $status ) {
$post_ok = false;
$type = relevanssi_get_post_type( $post_id );
if ( isset( $GLOBALS['wp_post_types'][ $type ]->cap->read_private_posts ) ) {
$cap = $GLOBALS['wp_post_types'][ $type ]->cap->read_private_posts;
} else {
// Just guessing here.
$cap = 'read_private_' . $type . 's';
}
if ( current_user_can( $cap ) ) {
// Current user has the required capabilities and can see the page.
$post_ok = true;
}
if ( function_exists( 'members_can_current_user_view_post' ) ) {
// Members.
$post_ok = members_can_current_user_view_post( $post_id );
}
}
if ( defined( 'GROUPS_CORE_VERSION' ) ) {
// Groups.
$current_user = wp_get_current_user();
$post_ok = Groups_Post_Access::user_can_read_post( $post_id, $current_user->ID );
}
if ( class_exists( 'MeprUpdateCtrl' ) && MeprUpdateCtrl::is_activated() ) {
// Memberpress.
$post = get_post( $post_id );
if ( MeprRule::is_locked( $post ) ) {
$post_ok = false;
}
}
if ( defined( 'SIMPLE_WP_MEMBERSHIP_VER' ) ) {
// Simple Membership.
$access_ctrl = SwpmAccessControl::get_instance();
$post = get_post( $post_id );
$post_ok = $access_ctrl->can_i_read_post( $post );
}
/**
* Filters statuses allowed in admin searches.
*
* By default, admin searches may show posts that have 'draft', 'pending' and
* 'future' status (in addition to 'publish' and 'private'). If you use custom
* statuses and want them included in the admin search, you can add the statuses
* using this filter.
*
* @param array $statuses Array of statuses to accept.
*/
if ( in_array( $status, apply_filters( 'relevanssi_valid_admin_status', array( 'draft', 'pending', 'future' ) ), true ) && is_admin() ) {
// Only show drafts, pending and future posts in admin search.
$post_ok = true;
}
return $post_ok;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment