Skip to content

Instantly share code, notes, and snippets.

@fergbrain
Last active September 4, 2020 11:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fergbrain/d3210d4c2fbe3c4c5c13d8b182ae8bd1 to your computer and use it in GitHub Desktop.
Save fergbrain/d3210d4c2fbe3c4c5c13d8b182ae8bd1 to your computer and use it in GitHub Desktop.
Tweaks to the Cozmo Profile Builder plugin
<?php
/**
* Plugin Name: Cozmo Tweaks
* Plugin URI: http://fergcorp.com
* Description: Tweaks to the Cozmo Profile Builder plugin.
* Version: 2020.03.07
* Author: Fergcorp
* Author URI: http://fergcorp.com
* License: GPL3
*/
/* Copyright 2019-2020 Andrew Ferguson (andrew@fergcorp.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 3, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// add_action( 'pre_get_posts', 'wppbc_exclude_post_from_query' );
/**
* Exclude posts that user doesn't have access to from all queries
*
* @author Cristi Antohe
* @param object $query data
*
*/
function wppbc_exclude_post_from_query( $query ) {
if( !is_admin() && $query->is_main_query() && function_exists('wppb_check_content_restriction_on_post_id') ) {
$args = $query->query_vars;
$args['suppress_filters'] = true;
$args['posts_per_page'] = get_option( 'posts_per_page' );
$posts = get_posts($args);
$ids = wp_list_pluck( $posts, 'ID' );
$restricted_ids = array_filter($ids,'wppb_check_content_restriction_on_post_id');
$query->set( 'post__not_in', $restricted_ids );
}
}
/**
* Restrict posts that haven't been created through the WordPress website.
*
* @author Andrew Ferguson (andrew@fergcorp.com)
*
*/
add_action( 'transition_post_status', 'fergcorp_wppbc_content_restrict_on_publish', 10, 3 );
function fergcorp_wppbc_content_restrict_on_publish($new_status, $old_status, $post)
{
// Run when a post is published
if ( $old_status != 'publish' && $new_status == 'publish' ) {
$wppb_content_restrict_type = get_post_meta( $post->ID, 'wppb-content-restrict-type', true );
// Only set 'wppb-content-restrict-user-status' to 'loggedin' if 'wppb-content-restrict-type' hasn't been set.
// The assumption is that 'wppb-content-restrict-type' is set if a user has either:
// 1) Drafted the post from the site and thus has had the option to either restrict the post or not
// (see changes made to content-restriction-meta-box.php at https://gist.github.com/fergbrain/9b2c79aeedece87be75437048bb2b410)
// 2) The user is using the WordPress App (or other API) that does not use meta boxes and thus hasn't (nor can be) been given the option.
// In which case we default to restricting the post.
if (empty ($wppb_content_restrict_type)) {
update_post_meta($post->ID, "wppb-content-restrict-user-status", "loggedin");
}
}
}
/**
* Exclude posts tags from users who don't have access
*
* @author Andrew Ferguson
*
*/
add_filter( 'get_the_terms', 'fergcorp_wppbc_tag_restrict', 10, 3 );
function fergcorp_wppbc_tag_restrict($terms, $id, $taxonomy)
{
if( function_exists('wppb_check_content_restriction_on_post_id') ){
if(wppb_check_content_restriction_on_post_id($id) && 'post_tag' == $taxonomy){
return false;
}
else{
return $terms;
}
}
else{
return $terms;
}
}
/**
* Exclude posts comments from users who don't have access
*
* @author Andrew Ferguson
*
*/
add_filter( 'post_password_required', 'fergcorp_wppbc_post_password_override', 10, 2 );
function fergcorp_wppbc_post_password_override($required, $post)
{
if( function_exists('wppb_check_content_restriction_on_post_id') ){
if(wppb_check_content_restriction_on_post_id($post->ID)){
return True;
}
else{
return $required;
}
}
else{
return $required;
}
}
/**
* Redirect back to original post or page after logging in.
*
* @author Andrew Ferguson (andrew@fergcorp.com)
*
*/
function fergcorp_wppbc_user_login_redirect( $url, $request ){
if( isset( $_POST['wppb_referer_url'] ) ) {
$url = esc_url_raw($_POST['wppb_referer_url']);
}
return $url;
}
add_filter( 'login_redirect', 'fergcorp_wppbc_user_login_redirect', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment