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_users_cycle( $args, $i ) {
if ( ! user_can( $args['author'], get_option( 'upl_manage_cap', '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( get_option( 'upl_manage_cap', '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( get_option( 'upl_manage_cap', '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 ) {
return 'cycle' === $column_id_attr ? get_user_meta( $user_id, 'cycle', true ) : $row_output;
}
add_action( 'woocommerce_order_status_completed', 'upl_wc_update_user_cycle' );
function upl_wc_update_user_cycle( $order_id ) {
$products_id = [ '683' ]; // Here update your product ID
$order = wc_get_order( $order_id );
$user_id = $order->get_user_id();
if ( ! user_can( $user_id, get_option( 'upl_manage_cap', 'manage_options' ) ) ) {
foreach ( $order->get_items() as $item ) {
if ( in_array( $item->get_product_id(), $products_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;
}
}
}
}