1

I'm new around here but I will do my best to get the question out as clear as possible.

I'm developing a website where users will be able to publish portfolio items from the front-end of the website. The portfolio item contains a title, content, featured image and image gallery.

To prevent a certain user from spamming the site and uploading lets say 100 images in 1 portfolio item, i have added some limits like the max size for an image upload (1MB) and also the max number of uploads (10). I've accomplished the second part with the answers from this post:

add_filter( 'wp_handle_upload_prefilter', 'limit_uploads_for_user_roles' );

function limit_uploads_for_user_roles( $file ) {
  $user = wp_get_current_user();
  // add the role you want to limit in the array
  $limit_roles = array('author');
  $filtered = apply_filters( 'limit_uploads_for_roles', $limit_roles, $user );
  if ( array_intersect( $limit_roles, $user->roles ) ) {
    $upload_count = get_user_meta( $user->ID, 'upload_count', true ) ? : 0;
    $limit = apply_filters( 'limit_uploads_for_user_roles_limit', 10, $user, $upload_count, $file );
    if ( ( $upload_count + 1 ) > $limit ) {
      $file['error'] = __('Upload limit has been reached for this account!', 'yourtxtdomain');
    } else {
      update_user_meta( $user->ID, 'upload_count', $upload_count + 1 );
    }
  }
  return $file;
}

add_action('delete_attachment', 'decrease_limit_uploads_for_user');

function decrease_limit_uploads_for_user( $id ) {
   $user = wp_get_current_user();
   // add the role you want to limit in the array
   $limit_roles = array('author');
   $filtered = apply_filters( 'limit_uploads_for_roles', $limit_roles, $user );
   if ( array_intersect( $limit_roles, $user->roles ) ) {
     $post = get_post( $id);
     if ( $post->post_author != $user->ID ) return;
     $count = get_user_meta( $user->ID, 'upload_count', true ) ? : 0;
     if ( $count ) update_user_meta( $user->ID, 'upload_count', $count - 1 );
   }
}

This works fine, when you get to 10 uploads you see an error message telling you've reached the upload limit for that account and if you delete an upload it deducts 1 from this $upload_count. Great!

Now, i'm struggling with a function to update the meta_value for 'upload_count' (reset back to 0) every 24hours (or other time limit). So that if a user reaches his limit today he can simply come back tomorrow and be able to continue posting.

From the research i've done, this is what i've been able to come up with so far but it's not working as expected. (for the sake of testing i set the time limit to 3 minutes):

  $schedules['every_180_seconds'] = array(
      'interval' => 180, // Every 180 seconds
      'display'  => __( 'Every 3 minutes' ),
  );
  return $schedules;
}
add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );

//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'myprefix_cron_hook' ) ) {
  wp_schedule_event( time(), 'every_180_seconds', 'myprefix_cron_hook' );
}

///Hook into that action that'll fire every six seconds
add_action( 'myprefix_cron_hook', 'myprefix_cron_function' );

//My update function, that runs on cron
function myprefix_cron_function() {
  $users = get_users( ['fields' => ['ID'] ] );
  foreach ( $users as $user ) {
    $upload_count = get_user_meta( $user->ID, 'upload_count', true );
    if($upload_count!=0)
      $user_update = update_user_meta($user->ID, 'upload_count', 0);
  }
} 

The outcome is kinda strange, sometimes it takes the expected time to update sometimes it updates right after i refresh the page...

Can someone help point me in the right direction to figure this out? Any critical reviews would be much appreciated. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.