buddydev

Untitled

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