Categories
WordPress Plugins

User Posts Limit

How to make some user unrestricted in rules that applied on certain post type?
Update the user id and the post type:

add_filter( 'upl_query', 'upl_unrestrict_user' );
function upl_unrestrict_user( $args ) {
	$post_type = 'post';
	$user_id = 12;
	if ( $post_type === $args['post_type'] && $user_id === $args['author'] ) {
		$args['post_type'] = 'none';
	}
	return $args;
}

How to make rules that applied on certain post type to limit the total posts in the website?
Update the post type:

add_filter( 'upl_query', 'upl_limit_total_posts' );
function upl_limit_total_posts( $args ) {
	$post_type = 'post';
	if ( $post_type === $args['post_type'] ) {
		$args['author'] = '';
	}
	return $args;
}

How to make rules that applied on certain post type to limit by posts in specific category only?
Update the post type and the category id (make sure users can’t migrate posts into unrestricted posts category):

add_filter( 'upl_query', 'upl_specific_category' );
function upl_specific_category( $args ) {
	$post_type = 'post';
	$category_id = 4;
	if ( $post_type === $args['post_type'] ) {
		$args['cat'] = $category_id;
	}
	return $args;
}

How to make rules that applied on certain post type to limit specific post status?
Update the post type and the post status (make sure users can’t migrate posts into unrestricted post status):

add_filter( 'upl_query', 'upl_specific_post_status' );
function upl_specific_post_status( $args ) {
	$post_type = 'post';
	$post_status = [ 'any' ];
	if ( $post_type === $args['post_type'] ) {
		$args['post_status'] = $post_status;
	}
	return $args;
}

How to modify the cycle for rules that applied on certain post type?
Update the post type and the cycle (keep in mind that the change will not take affect in places that the date is displayed):

add_filter( 'upl_query', 'upl_modify_cycle' );
function upl_modify_cycle( $args ) {
	$post_type = 'post';
	$cycle = '3 days ago';
	if ( $post_type === $args['post_type'] ) {
		$args['date_query']['after'] = $cycle;
	}
	return $args;
}

How to send users an Email to encourage them to upgrade their membership after they try to post after posts limit exceeded?
Attach your code for sending Email to the upl_limit_applied action hook.

How to define different message types for each user?
Use the upl_message and upl_notice filters.

How to add limit with code?
Update the role, type, limit and cycle:

$role = 'author';
$type = 'post';
$limit = '2';
$cycle = 'day';
$formatted_cycle = '1 ' . $cycle . ' ago';
$role_option = get_option( 'upl_user_role' );
$type_option = get_option( 'upl_posts_type' );
$limit_option = get_option( 'upl_num_limit' );
$period_option = get_option( 'upl_period' );
$role_option[] = $role;
$type_option[] = $type;
$limit_option[] = $limit;
$period_option[] = $formatted_cycle;
update_option( 'upl_rules_count', get_option( 'upl_rules_count' ) + 1 );
update_option( 'upl_user_role', $role_option );
update_option( 'upl_posts_type', $type_option );
update_option( 'upl_num_limit', $limit_option );
update_option( 'upl_period', $period_option );

How to modify the limits in all the rules that applied on specified user role with code?
Update the role and limit:

for ( $i = 0; $i < get_option( 'upl_rules_count' ); $i++ ) {
	$role = 'subscriber';
	$limit = '3';
	if ( $role === get_option( 'upl_user_role' )[ $i ] ) {
		$limit_option = get_option( 'upl_num_limit' );
		$limit_option[ $i ] = $limit;
		update_option( 'upl_num_limit', $limit_option );
	}
}

How to reset user’s posts counts when purchase some product in WooCommerce (by the end of the day)?
Update the product id:

add_action( 'woocommerce_order_status_completed', 'upl_update_user_cycle' );
add_filter( 'upl_query', 'upl_users_cycle', 10, 2 );
add_action( 'show_user_profile', 'upl_add_cycle_user_data' );
add_action( 'edit_user_profile', 'upl_add_cycle_user_data' );
add_action( 'personal_options_update', 'upl_save_cycle_user_data' );
add_action( 'edit_user_profile_update', 'upl_save_cycle_user_data' );
add_filter( 'manage_users_columns', 'upl_cycle_user_table' );
add_filter( 'manage_users_custom_column', 'upl_cycle_user_table_row', 10, 3 );

function upl_update_user_cycle( $order_id ) {
	$product_id = '683';
	$order = wc_get_order( $order_id );
	$user_id = $order->get_user_id();
	if ( ! user_can( $user_id, 'manage_options' ) ) {
		foreach ( $order->get_items() as $item ) {
			if ( $product_id == $item->get_product_id() ) {
				if ( empty( get_user_meta( $user_id, 'cycle', true ) ) || get_user_meta( $user_id, 'cycle', true ) <= current_time( 'Y-m-d' ) ) {
					update_user_meta( $user_id, 'cycle', date( 'Y-m-d', strtotime( current_time( 'Y-m-d' ) . ' + 1 day' ) ) );
				}
				break;
			}
		}
	}
}

function upl_users_cycle( $args, $i ) {
	if ( ! user_can( $args['author'], 'manage_options' ) ) {
		$rule_role = '';
		$cycle = get_user_meta( $args['author'], 'cycle', true );
		if ( ( empty( $rule_role ) || $rule_role === get_option( 'upl_user_role' )[ $i ] ) && ! empty( $cycle ) && $cycle <= current_time( 'Y-m-d' ) ) {
			$args['date_query']['after'] = date( 'Y-m-d', strtotime( $cycle . ' - 1 day' ) );
		}
	}
	return $args;
}

function upl_add_cycle_user_data( $user ) {
	if ( current_user_can( 'manage_options' ) ) {
		?>
			<h3><?php esc_html_e( 'User Posts Limit', 'user-posts-limit' ); ?></h3>
			<table class="form-table">
				<tr>
					<th><label for="cycle"><?php esc_html_e( 'Cycle', 'user-posts-limit' ); ?></label></th>
					<td><input type="date" min="1970-01-01" name="cycle" value="<?php echo esc_attr( get_user_meta( $user->ID, 'cycle', true ) ); ?>" class="regular-text" /></td>
				</tr>
			</table>
			<br />
		<?php
	}
}

function upl_save_cycle_user_data( $user_id ) {
	if ( current_user_can( 'manage_options' ) ) {
		update_user_meta( $user_id, 'cycle', sanitize_text_field( $_POST['cycle'] ) );
	}
}

function upl_cycle_user_table( $columns ) {
	$columns['cycle'] = __( 'Cycle', 'user-posts-limit' );
	return $columns;
}

function upl_cycle_user_table_row( $row_output, $column_id_attr, $user_id ) {
	if ( 'cycle' === $column_id_attr ) {
		return get_user_meta( $user_id, 'cycle', true );
	}
	return $row_output;
}

How to make the listing rules apply seperately on every listing type in MyListing theme?

add_filter( 'upl_query', 'upl_add_meta_query' );
function upl_add_meta_query( $args ) {
	if ( 'job_listing' === $args['post_type'] && isset( $_POST['listing_type'] ) ) {
		$args['meta_query'] = [
			[
				'key'	=> '_case27_listing_type',
				'value'	=> sanitize_text_field( $_POST['listing_type'] )
			]
		];
	}
	return $args;
}

Leave a Reply

Your email address will not be published. Required fields are marked *