Skip to content

Instantly share code, notes, and snippets.

@rocketgeek
Last active October 7, 2021 14:17
Show Gist options
  • Save rocketgeek/229cfa95c5a71f06b6a8799cf5b38518 to your computer and use it in GitHub Desktop.
Save rocketgeek/229cfa95c5a71f06b6a8799cf5b38518 to your computer and use it in GitHub Desktop.
new is_blocked for individual post checking
<?php // do not include this line. Replace the function is_blocked() in class-wp-members.php with the below:
/**
* Determines if content should be blocked.
*
* This function was originally stand alone in the core file and
* was moved to the WP_Members class in 3.0.
*
* @since 3.0.0
* @since 3.3.0 Added $post_id
* @since 3.4.0 Added $is_post_check to allow for individual post checking.
*
* @global object $post The WordPress Post object.
*
* @param int $post_id
* @return bool $block true|false
*/
function is_blocked( $post_id = false ) {
global $post;
$is_post_check = ( false === $post_id ) ? false : true;
if ( $post || $post_id ) {
$the_post = ( false === $post_id ) ? $post : get_post( $post_id );
$meta = wpmem_get_block_setting( $the_post->ID );
// Backward compatibility for old block/unblock meta.
if ( ! $meta ) {
// Check for old meta.
$old_block = get_post_meta( $the_post->ID, 'block', true );
$old_unblock = get_post_meta( $the_post->ID, 'unblock', true );
$meta = ( $old_block ) ? 1 : ( ( $old_unblock ) ? 0 : $meta );
}
// Setup defaults.
$defaults = array(
'post_id' => $the_post->ID,
'post_type' => $the_post->post_type,
'block' => ( isset( $this->block[ $the_post->post_type ] ) && $this->block[ $the_post->post_type ] == 1 ) ? true : false,
'block_meta' => $meta,
'block_type' => ( isset( $this->block[ $the_post->post_type ] ) ) ? $this->block[ $the_post->post_type ] : 0,
);
/**
* Filter the block arguments.
*
* @since 2.9.8
* @since 3.0.0 Moved to is_blocked() in WP_Members object.
* @since 3.3.0 Passes $defaults, second argument deprecated.
*
* @param array $args $defaults.
* @param array $defaults Deprecated 3.3.0.
*/
$args = apply_filters( 'wpmem_block_args', $defaults, $defaults );
// Merge $args with defaults.
$args = ( wp_parse_args( $args, $defaults ) );
if ( $is_post_check || is_single() || is_page() || wpmem_is_rest() ) {
switch( $args['block_type'] ) {
case 1: // If content is blocked by default.
$args['block'] = ( $args['block_meta'] == '0' ) ? false : $args['block'];
break;
case 0 : // If content is unblocked by default.
$args['block'] = ( $args['block_meta'] == '1' ) ? true : $args['block'];
break;
}
} else {
$args['block'] = false;
}
} else {
$args = array( 'block' => false );
}
// Don't block user pages.
$args['block'] = ( in_array( get_permalink(), $this->user_pages ) ) ? false : $args['block'];
/**
* Filter the block boolean.
*
* @since 2.7.5
*
* @param bool $args['block']
* @param array $args {
* An array of arguments used in the function.
*
* @type string $post_id
* @type string $post_type
* @type string $block
* @type string $block_meta
* @tyep string $block_type
* }
*/
return apply_filters( 'wpmem_block', $args['block'], $args );
}
<?php // do not include this line.
// This is an example of how to use wpmem_is_blocked() to
// check if a post ID to see if it is blocked.
add_filter( 'the_title', function( $title ) {
if ( in_the_loop() ) {
global $post;
if ( wpmem_is_blocked( $post->ID ) ) {
$title .= " [members only]";
}
}
return $title;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment