• Resolved Element LMS

    (@skinnycat)


    Assigning Read restriction to a post creates some issues with WP Cron and Woocommerce Action Scheduler. Crons run as no user (user_id = 0), therefore, any code run by Crons that tries to access posts restricted by group will not be able to do so.

    Ideally, Crons should run as a user and we would assign groups to that user, but WordPress doesn’t support that.

    Would it be possible disable the restriction for a cron as most lilkly the group read restriction does not apply (this could be a setting).

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Element LMS

    (@skinnycat)

    Note that I was able to solve my problem by using the various filters and disabling restrictions when wp_doing_cron() is true. However, it would be nice to have this out of the box

    Plugin Author Kento

    (@proaktion)

    Hi,

    Would you mind sharing how you approached this? It could be useful for others.

    Also thanks for suggesting that this could be useful to have incorporated into Groups itself, I’ll take note to review this possibility.

    Cheers

    Thread Starter Element LMS

    (@skinnycat)

    In my case, I just had to override the result of 2 filters:

    add_filter( 'groups_post_access_posts_where_apply', array( $this, 'handle_groups_post_access_posts_where_apply' ), 10 , 3 );
    add_filter( 'groups_comment_access_comments_clauses_apply', array( $this, 'handle_groups_comment_access_comments_clauses_apply' ), 10 , 3 );
    
    public function handle_groups_post_access_posts_where_apply( bool $filter_posts, string $where, WP_Query $query ): bool {
    
      if ( wp_doing_cron() ) {
        return false;
      }
    
      return $filter_posts;
    }
    
    public function handle_groups_comment_access_comments_clauses_apply( bool $filter_comments, array $pieces, WP_Comment_Query $comment_query ): bool {
    
      if ( wp_doing_cron()  ) {
        return false;
      }
    
      return $filter_comments;
    }

    Maybe it is fine to leave the current Group behavior as it is. However at the very least, having a setting disabling restriction when code is ran from within a Cron would be awesome. The change would be similar to what I have been doing but before applying the filter.

    • This reply was modified 2 years, 1 month ago by Element LMS.
    • This reply was modified 2 years, 1 month ago by Element LMS.
    Plugin Author Kento

    (@proaktion)

    @skinnycat Thank you very much for sharing your approach, this is very useful!

    Taking note to look into it further.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Post Read restriction and Cron job’ is closed to new replies.