Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BBL_Custom_Posts_Limiter {
- /**
- * Role to limit.
- *
- * @var mixed
- */
- private string $role;
- /**
- * Post type to limit.
- * @var string
- */
- private string $post_type;
- /**
- * Status to limit.
- * @var array
- */
- private $status = array();
- /**
- * Error message to show.
- *
- * @var string
- */
- private $error_message;
- /**
- * Limit.
- *
- * @var int
- */
- private $limit;
- /**
- * Constructor.
- *
- * @param array $args args.
- */
- public function __construct( $args = array() ) {
- $args = wp_parse_args(
- $args,
- array(
- 'role' => '',// Specify the role to limit.
- 'post_type' => 'post',
- 'status' => array( 'publish', 'pending', 'draft', 'future', 'private' ),
- 'limit' => 5,// limit to 5 posts.
- 'error' => __( 'You have reached the limit of creating new posts.', 'bbl' ),
- )
- );
- $this->role = $args['role'];
- $this->post_type = $args['post_type'];
- $this->status = $args['status'];
- $this->error_message = $args['error'];
- $this->limit = absint( $args['limit'] );
- }
- /**
- * Sets up hooks.
- */
- public function setup() {
- if ( ! $this->role || ! $this->post_type || ! $this->limit ) {
- return;
- }
- // filter permission check.
- add_filter( 'bblpro_user_can_create_post', array( $this, 'can_create_post' ), 15, 3 );
- // append message.
- add_action( 'bblpro_actions', array( $this, 'add_error_message' ), 30 );
- //add_action( 'bp_actions', array( $this, 'add_error_message' ), 20 );
- }
- public function add_error_message( $action ) {
- if ( $action !== 'create' ) {
- return;
- }
- $post_type = bblpro_get_current_post_type();
- if ( ! $post_type || $post_type !== $this->post_type ) {
- return;
- }
- // show message.
- if ( ! bblpro_user_can_create_post( get_current_user_id(), $this->post_type ) ) {
- buddyblog_pro()->notices->add( 'error', $this->error_message );
- return;
- }
- }
- /**
- * Filters user permission for post creation to apply our limit.
- *
- * @param bool $can Can the user create post.
- * @param int $user_id User id.
- * @param string $post_type Post type.
- *
- * @return bool
- */
- public function can_create_post( $can, $user_id, $post_type ) {
- // it is already restricted. No need to do anything.
- if ( ! $can ) {
- return $can;
- }
- // should never happen.
- if ( ! is_user_logged_in() ) {
- return $can;
- }
- // not our post type.
- if ( $post_type !== $this->post_type ) {
- return $can;
- }
- // not on other user's profile.
- if ( bp_is_user() && ! bp_is_my_profile() ) {
- return $can;
- }
- $user = get_user_by( 'id', $user_id );
- if ( ! $user ) {
- return $can;
- }
- // this instance does not deal with this role.
- if ( ! in_array( $this->role, $user->roles ) ) {
- return $can;
- }
- $posts_count = $this->get_user_posts_count( $user->ID, $this->post_type, $this->status );
- if ( $posts_count >= $this->limit ) {
- $can = false;
- }
- return $can;
- }
- /**
- * Retrieves posts count for user.
- *
- * @param int $user_id User id.
- * @param string $post_type Post type.
- * @param array $status Post statuses.
- *
- * @return int
- */
- private function get_user_posts_count( $user_id, $post_type = null, $status = array() ) {
- if ( ! $user_id ) {
- return 0;
- }
- global $wpdb;
- $where = array();
- $where[] = $wpdb->prepare( "post_author = %d", $user_id );
- if ( $post_type ) {
- $where[] = $wpdb->prepare( "post_type = %s", $post_type );
- }
- if ( $status ) {
- $prepared_status = array();
- $status = (array) $status;
- foreach ( $status as $post_status ) {
- $prepared_status[] = $wpdb->prepare( "%s", $post_status );
- }
- $where[] = sprintf( "post_status IN (%s)", join( ',', $prepared_status ) );
- }
- $sql = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE " . join( ' AND ', $where );
- return (int) $wpdb->get_var( $sql );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement