Skip to content

Commit

Permalink
Changes for limit posts per package
Browse files Browse the repository at this point in the history
  • Loading branch information
kprajapatii committed Jul 13, 2021
1 parent ffc3fd4 commit 6965ecd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 32 deletions.
2 changes: 1 addition & 1 deletion includes/admin/class-geodir-admin-post-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public static function owner_meta_box() {
);
?>
<label class="screen-reader-text" for="post_author_override"><?php _e('User', 'geodirectory'); ?></label>
<select class="geodir-user-search" name="post_author_override" id="post_author_override" data-placeholder="<?php esc_attr_e( 'Search for a user&hellip;', 'geodirectory' ); ?>" data-allow_clear="true"><option value="<?php echo esc_attr( $curent_user_id ); ?>" selected="selected"><?php echo $curent_user_name; ?><option></select>
<select class="geodir-user-search" name="post_author_override" id="post_author_override" data-placeholder="<?php esc_attr_e( 'Search for a user&hellip;', 'geodirectory' ); ?>" data-allow_clear="false"><option value="<?php echo esc_attr( $curent_user_id ); ?>" selected="selected"><?php echo $curent_user_name; ?><option></select>
<?php
}

Expand Down
8 changes: 5 additions & 3 deletions includes/admin/settings/class-geodir-settings-cpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public function get_settings( $current_section = '' ) {
// author
'author_posts_private' => '0',
'author_favorites_private' => '0',
'limit_posts' => '',
// Page template
'page_add' => '0',
'page_details' => '0',
Expand Down Expand Up @@ -370,16 +371,17 @@ public function get_settings( $current_section = '' ) {
array(
'type' => 'number',
'id' => 'limit_posts',
'name' => __( 'Limit Posts', 'geodirectory' ),
'name' => __( 'Limit Posts Per User', 'geodirectory' ),
'desc' => __( 'Limit total posts allowed per user. Leave blank or enter 0 to allow unlimited posts.', 'geodirectory' ),
'std' => '',
'placeholder' => __( 'Unlimited', 'geodirectory' ),
'value' => ( (int) $post_type_values['limit_posts'] === 0 ? '' : ( (int) $post_type_values['limit_posts'] < 0 ? -1 : (int) $post_type_values['limit_posts'] ) ),
'custom_attributes' => array(
'min' => '-1',
'step' => '1'
),
'desc_tip' => true,
'advanced' => true,
'value' => ( isset( $post_type_values['limit_posts'] ) && $post_type_values['limit_posts'] ? (int) $post_type_values['limit_posts'] : '' )
'advanced' => true
),
array( 'type' => 'sectionend', 'id' => 'cpt_settings_author' ),

Expand Down
10 changes: 6 additions & 4 deletions includes/class-geodir-post-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -1539,12 +1539,14 @@ public static function check_logged_out_author( $post_data ) {

// Check posts limit.
$args = array( 'post_type' => $post_data['post_type'], 'post_author' => $post_author );
$can_add_post = GeoDir_Post_Limit::user_can_add_post( $args );
if ( ! empty( $post_data['package_id'] ) ) {
$args['package_id'] = (int) $post_data['package_id'];
}

if ( ! $can_add_post ) {
$message = GeoDir_Post_Limit::posts_limit_message( $post_data['post_type'], $post_author );
$can_add_post = GeoDir_Post_Limit::user_can_add_post( $args, true );

$error = new WP_Error( 'add_listing_error', $message, array( 'status' => 400 ) );
if ( is_wp_error( $can_add_post ) ) {
$error = new WP_Error( 'add_listing_error', $can_add_post->get_error_message(), array( 'status' => 400 ) );
}
}
}
Expand Down
112 changes: 88 additions & 24 deletions includes/class-geodir-post-limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,37 @@ public static function rest_api_init() {
}
}

public static function cpt_posts_limit( $post_type, $post_author = 0 ) {
$post_type_info = geodir_get_posttype_info( $post_type );
public static function cpt_posts_limit( $args ) {
$defaults = array(
'post_type' => '',
'post_author' => 0
);

$args = wp_parse_args( $args, $defaults );

$post_type_info = geodir_get_posttype_info( $args['post_type'] );
$limit = 0;

if ( ! empty( $post_type_info ) && isset( $post_type_info['limit_posts'] ) ) {
$limit = (int) $post_type_info['limit_posts'];
}

return (int) apply_filters( 'geodir_cpt_posts_limit', $limit, $post_type, $post_author );
return (int) apply_filters( 'geodir_cpt_posts_limit', $limit, $args );
}

public static function cpt_posts_limits( $args = array() ) {
$defaults = array(
'post_type' => '',
'post_author' => 0
);

$params = wp_parse_args( $args, $defaults );

$limits = array(
'total' => (int) self::cpt_posts_limit( $params )
);

return apply_filters( 'geodir_cpt_posts_limits', $limits, $params );
}

public static function count_user_cpt_posts( $args = array() ) {
Expand Down Expand Up @@ -105,7 +126,7 @@ public static function count_user_cpt_posts( $args = array() ) {
return apply_filters( 'geodir_count_user_cpt_posts', $count, $query_args, $args );
}

public static function user_can_add_post( $args = array() ) {
public static function user_can_add_post( $args = array(), $wp_error = false ) {
global $wpdb;

$defaults = array(
Expand All @@ -129,47 +150,90 @@ public static function user_can_add_post( $args = array() ) {

if ( ! current_user_can( 'manage_options' ) ) {
if ( ! empty( $params['post_author'] ) && ! user_can( (int) $params['post_author'], 'manage_options' ) ) {
$posts_limit = (int) self::cpt_posts_limit( $params['post_type'], $params['post_author'] );
$message = '';
$posts_count = 0;
$posts_limits = self::cpt_posts_limits( $params );
$posts_limit = ! empty( $posts_limits['total'] ) ? (int) $posts_limits['total'] : 0;

if ( $posts_limit > 0 ) {
$posts_count = (int) self::count_user_cpt_posts( $params );

// Limit exceed.
if ( $posts_limit <= $posts_count ) {
$can_add = false;
$message = wp_sprintf( __( 'You have reached the limit of %s you can add at this time.', 'geodirectory' ), geodir_strtolower( geodir_post_type_name( $params['post_type'], true ) ) );
}
} else if ( $posts_limit < 0 ) {
$can_add = false; // Disabled from CPT
// Disabled from CPT
$can_add = false;
$message = wp_sprintf( __( 'You are not allowed to add the listing under %s.', 'geodirectory' ), geodir_strtolower( geodir_post_type_name( $params['post_type'], true ) ) );
}

if ( $can_add === false && $wp_error && $message ) {
$message = apply_filters( 'geodir_user_posts_limit_message', $message, $posts_limit, $posts_count, $posts_limits, $params );

if ( $message ) {
$can_add = new WP_Error( 'geodir_user_posts_limit', $message );
}
}

$can_add = apply_filters( 'geodir_check_user_posts_limits', $can_add, $posts_limits, $params, $args, $wp_error );
}
}

return apply_filters( 'geodir_user_can_add_post', $can_add, $params, $args );
return apply_filters( 'geodir_user_can_add_post', $can_add, $params, $args, $wp_error );
}

public static function posts_limit_message( $post_type, $post_author = 0 ) {
$posts_limit = (int) self::cpt_posts_limit( $post_type, $post_author );
$post_type_name = geodir_strtolower( geodir_post_type_name( $post_type ) );
public static function posts_limit_message( $args = array() ) {
$defaults = array(
'post_type' => '',
'post_author' => 0
);

$args = wp_parse_args( $args, $defaults );

$posts_limits = self::cpt_posts_limits( $args );
$post_type_name = geodir_strtolower( geodir_post_type_name( $args['post_type'] ) );

if ( $posts_limit < 0 ) {
if ( isset( $posts_limits['total'] ) && (int) $posts_limits['total'] < 0 ) {
$message = wp_sprintf( __( 'You are not allowed to add the listing under %s.', 'geodirectory' ), $post_type_name );
} else {
$message = wp_sprintf( __( 'You have reached the limit of %s you can add at this time.', 'geodirectory' ), $post_type_name );
}

return apply_filters( 'geodir_user_posts_limit_message', $message, $post_type, $posts_limit );
return apply_filters( 'geodir_user_posts_limit_message', $message, $args, $posts_limits );
}

public static function check_add_listing_output( $output, $args, $widget_args, $content ) {
if ( ! is_admin() && geodir_is_page( 'add-listing' ) && ! geodir_is_page( 'edit-listing' ) ) {
$post_type = geodir_get_current_posttype();

$can_add_post = self::user_can_add_post( array( 'post_type' => $post_type ) );
$_args = array(
'post_type' => $post_type,
'post_author' => null,
'group' => 'add'
);

if ( ! $can_add_post ) {
$message = geodir_notification( array( 'add_listing_error' => self::posts_limit_message( $post_type, (int) get_current_user_id() ) ) );
$package = geodir_get_post_package( array(), $post_type );

$output = apply_filters( 'geodir_posts_limit_add_listing_message', $message, $post_type );
if ( ! empty( $package ) && ! empty( $package->id ) ) {
$_args['package_id'] = $package->id;
}

$can_add_post = self::user_can_add_post( $_args, true );

if ( is_wp_error( $can_add_post ) ) {
if ( geodir_design_style() ) {
$message = aui()->alert( array(
'type' => 'info',
'content' => $can_add_post->get_error_message(),
'class' => 'mb-0'
) );
} else {
$message = geodir_notification( array( 'add_listing_error' => $can_add_post->get_error_message() ) );
}

$output = apply_filters( 'geodir_posts_limit_add_listing_message', $message, $post_type, $_args );
}
}

Expand All @@ -178,16 +242,16 @@ public static function check_add_listing_output( $output, $args, $widget_args, $

public static function check_rest_api_post( $prepared_post, $request ) {
if ( empty( $prepared_post->ID ) ) {
$args = array( 'post_type' => $prepared_post->post_type );
if ( ! empty( $prepared_post->post_author ) ) {
$args['post_author'] = absint( $prepared_post->post_author );
}
$args = array(
'post_type' => $prepared_post->post_type,
'post_author' => ! empty( $prepared_post->post_author ) ? absint( $prepared_post->post_author ) : null,
'package_id' => ! empty( $prepared_post->package_id ) ? absint( $prepared_post->package_id ) : 0
);

$can_add_post = self::user_can_add_post( $args );
if ( ! $can_add_post ) {
$message = self::posts_limit_message( $prepared_post->post_type, ( isset( $args['post_author'] ) ? $args['post_author'] : 0 ) );
$can_add_post = self::user_can_add_post( $args, true );

return new WP_Error( 'rest_posts_limit', $message, array( 'status' => 400 ) );
if ( is_wp_error( $can_add_post ) ) {
return new WP_Error( 'rest_posts_limit', $can_add_post->get_error_message(), array( 'status' => 400 ) );
}
}

Expand Down

0 comments on commit 6965ecd

Please sign in to comment.