Advertisement
buddydev

Untitled

Mar 13th, 2024
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.94 KB | None | 0 0
  1.  
  2. class BBL_Custom_Posts_Limiter {
  3.  
  4.     /**
  5.      * Role to limit.
  6.      *
  7.      * @var mixed
  8.      */
  9.     private string $role;
  10.  
  11.     /**
  12.      * Post type to limit.
  13.      * @var string
  14.      */
  15.     private string $post_type;
  16.  
  17.     /**
  18.      * Status to limit.
  19.      * @var array
  20.      */
  21.     private $status = array();
  22.  
  23.     /**
  24.      * Error message to show.
  25.      *
  26.      * @var string
  27.      */
  28.     private $error_message;
  29.  
  30.     /**
  31.      * Limit.
  32.      *
  33.      * @var int
  34.      */
  35.     private $limit;
  36.  
  37.     /**
  38.      * Constructor.
  39.      *
  40.      * @param array $args args.
  41.      */
  42.     public function __construct( $args = array() ) {
  43.         $args = wp_parse_args(
  44.             $args,
  45.             array(
  46.                 'role'      => '',// Specify the role to limit.
  47.                 'post_type' => 'post',
  48.                 'status'    => array( 'publish', 'pending', 'draft', 'future', 'private' ),
  49.                 'limit'     => 5,// limit to 5 posts.
  50.                 'error'     => __( 'You have reached the limit of creating new posts.', 'bbl' ),
  51.             )
  52.         );
  53.  
  54.         $this->role          = $args['role'];
  55.         $this->post_type     = $args['post_type'];
  56.         $this->status        = $args['status'];
  57.         $this->error_message = $args['error'];
  58.         $this->limit         = absint( $args['limit'] );
  59.     }
  60.  
  61.     /**
  62.      * Sets up hooks.
  63.      */
  64.     public function setup() {
  65.  
  66.         if ( ! $this->role || ! $this->post_type || ! $this->limit ) {
  67.             return;
  68.         }
  69.         // filter permission check.
  70.         add_filter( 'bblpro_user_can_create_post', array( $this, 'can_create_post' ), 15, 3 );
  71.         // append message.
  72.         add_action( 'bblpro_actions', array( $this, 'add_error_message' ), 30 );
  73.         //add_action( 'bp_actions', array( $this, 'add_error_message' ), 20 );
  74.     }
  75.  
  76.     public function add_error_message( $action ) {
  77.         if ( $action !== 'create' ) {
  78.             return;
  79.         }
  80.  
  81.         $post_type = bblpro_get_current_post_type();
  82.  
  83.         if ( ! $post_type || $post_type !== $this->post_type ) {
  84.             return;
  85.         }
  86.  
  87.         // show message.
  88.         if ( ! bblpro_user_can_create_post( get_current_user_id(), $this->post_type ) ) {
  89.             buddyblog_pro()->notices->add( 'error', $this->error_message );
  90.  
  91.             return;
  92.         }
  93.     }
  94.  
  95.     /**
  96.      * Filters user permission for post creation to apply our limit.
  97.      *
  98.      * @param bool $can Can the user create post.
  99.      * @param int $user_id User id.
  100.      * @param string $post_type Post type.
  101.      *
  102.      * @return bool
  103.      */
  104.     public function can_create_post( $can, $user_id, $post_type ) {
  105.         // it is already restricted. No need to do anything.
  106.         if ( ! $can ) {
  107.             return $can;
  108.         }
  109.         // should never happen.
  110.         if ( ! is_user_logged_in() ) {
  111.             return $can;
  112.         }
  113.  
  114.         // not our post type.
  115.         if ( $post_type !== $this->post_type ) {
  116.             return $can;
  117.         }
  118.  
  119.         // not on other user's profile.
  120.         if ( bp_is_user() && ! bp_is_my_profile() ) {
  121.             return $can;
  122.         }
  123.  
  124.         $user = get_user_by( 'id', $user_id );
  125.  
  126.         if ( ! $user ) {
  127.             return $can;
  128.         }
  129.  
  130.         // this instance does not deal with this role.
  131.         if ( ! in_array( $this->role, $user->roles ) ) {
  132.             return $can;
  133.         }
  134.  
  135.         $posts_count = $this->get_user_posts_count( $user->ID, $this->post_type, $this->status );
  136.  
  137.         if ( $posts_count >= $this->limit ) {
  138.             $can = false;
  139.         }
  140.  
  141.  
  142.         return $can;
  143.     }
  144.  
  145.     /**
  146.      * Retrieves posts count for user.
  147.      *
  148.      * @param int $user_id User id.
  149.      * @param string $post_type Post type.
  150.      * @param array $status Post statuses.
  151.      *
  152.      * @return int
  153.      */
  154.     private function get_user_posts_count( $user_id, $post_type = null, $status = array() ) {
  155.  
  156.         if ( ! $user_id ) {
  157.             return 0;
  158.         }
  159.  
  160.         global $wpdb;
  161.         $where   = array();
  162.         $where[] = $wpdb->prepare( "post_author = %d", $user_id );
  163.  
  164.         if ( $post_type ) {
  165.             $where[] = $wpdb->prepare( "post_type = %s", $post_type );
  166.         }
  167.  
  168.         if ( $status ) {
  169.             $prepared_status = array();
  170.  
  171.             $status = (array) $status;
  172.             foreach ( $status as $post_status ) {
  173.                 $prepared_status[] = $wpdb->prepare( "%s", $post_status );
  174.             }
  175.  
  176.             $where[] = sprintf( "post_status IN (%s)", join( ',', $prepared_status ) );
  177.         }
  178.  
  179.  
  180.         $sql = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE " . join( ' AND ', $where );
  181.  
  182.         return (int) $wpdb->get_var( $sql );
  183.     }
  184. }
  185.  
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement