Skip to content

Instantly share code, notes, and snippets.

@birgire
Last active September 2, 2022 20:37
Show Gist options
  • Save birgire/a24f25d9df15af5a5b79736c4fe17a72 to your computer and use it in GitHub Desktop.
Save birgire/a24f25d9df15af5a5b79736c4fe17a72 to your computer and use it in GitHub Desktop.
WordPress plugin: BBPMods Anonymous
<?php
/**
* Plugin Name: BBPMods Anonymous
* Description: bbPress anonymous modifications.
* Version: 0.0.2
* Requires PHP: 5.4
*/
/**
* Changelog.
*
* 0.0.2 Fixed formatting.
* Added changelog.
* 0.0.1 Init.
*/
namespace BBPMods\Anonymous;
/**
* Init.
*/
add_action( 'init', function(){
add_filter( 'bbp_current_user_can_access_anonymous_user_form', '__return_false' );
$anonymous_users = array(
array ( 'John', '[email protected]' ),
array ( 'Mike', '[email protected]' ),
array ( 'Will', '[email protected]' ),
array ( 'Pete', '[email protected]' ),
array ( 'Lucy', '[email protected]' ),
array ( 'Barbara', '[email protected]' ),
);
/**
* Filter anonymous users.
*
* @param array $anonymous_users Array of arrays with name and email.
*/
$anonymous_users = apply_filters( 'bbpmods_anonymous_users', $anonymous_users );
$obj = new Empty_Anonymous_Replies();
$obj ->set_anonymous_users( $anonymous_users )
->init();
} );
/**
* Class Empty_Anonymous_Replies.
*
* @since 0.0.1
*/
class Empty_Anonymous_Replies {
/**
* @var int
*
* @since 0.0.1
*/
protected $user_index = 0;
/**
* @var int
*
* @since 0.0.1
*/
protected $name_index = 0;
/**
* @var int
*
* @since 0.0.1
*/
protected $email_index = 1;
/**
* @var array
*
* @since 0.0.1
*/
protected $anonymous_users = array();
/**
* Set anonymous users.
*
* @since 0.0.1
*
* @param array $anonymous_users Array of arrays with name and email.
*/
public function set_anonymous_users( $anonymous_users ) {
$this->anonymous_users = $anonymous_users;
return $this;
}
/**
* Init.
*
* @since 0.0.1
*/
public function init() {
// Filters in order of activation.
add_filter( 'bbp_before_filter_anonymous_post_data_parse_args',
array( $this, 'bbp_before_filter_anonymous_post_data_parse_args' ),
10 );
add_filter( 'bbp_pre_anonymous_post_author_name',
array( $this, 'bbp_pre_anonymous_post_author_name' ) );
add_filter( 'bbp_pre_anonymous_post_author_email',
array( $this, 'bbp_pre_anonymous_post_author_email' ) );
add_filter( 'bbp_filter_anonymous_post_data',
array( $this, 'bbp_filter_anonymous_post_data' ),
11, 2 );
}
/**
* Filter's callback
*
* @since 0.0.1
*
* @param array $r Anonymous post data.
*/
public function bbp_before_filter_anonymous_post_data_parse_args( $r ) {
if ( ! empty( $this->anonymous_users ) ) {
$this->user_index = array_rand( $this->anonymous_users );
}
return $r;
}
/**
* Filter's callback
*
* @since 0.0.1
*
* @param string $name Anonymous user's name.
*/
public function bbp_pre_anonymous_post_author_name( $name ) {
remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
if( empty( $name ) && ! empty( $this->anonymous_users ) ) {
$name = $this->anonymous_users[$this->user_index][$this->name_index];
}
return $name;
}
/**
* Filter's callback
*
* @since 0.0.1
*
* @param string $email Anonymous user's email.
*/
public function bbp_pre_anonymous_post_author_email( $email ) {
remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
if( empty( $email ) && ! empty( $this->anonymous_users ) ) {
$email = $this->anonymous_users[$this->user_index][$this->email_index];
}
return $email;
}
/**
* Filter's callback
*
* @since 0.0.1
*
* @param array $retval Anonymous post data.
* @param array $r Arguments.
*/
public function bbp_filter_anonymous_post_data( $retval, $r ) {
if ( empty( $this->anonymous_users ) ) {
return $retval;
}
if( $this->anonymous_users[$this->user_index][$this->name_index] === $r['bbp_anonymous_name']
&& $this->anonymous_users[$this->user_index][$this->email_index] === $r['bbp_anonymous_email']
) {
// Reset the input to skip writing cookies.
$retval = array();
// Activate the IP flood check.
$retval['bbp_anonymous_flood_check'] = '1';
}
return $retval;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment