digitoimistodude/air-light

Improve load_acf_block() if conditionals especially with showing blocks in context

Closed this issue · 0 comments

In file inc/template-tags/acf-blocks.php the long if is kinda horrific:

  /**
   * Check if it's allowed to show this block in this context.
   * Always allow in preview mode.
   *
   * This might happen when we build a reusable block in a page and
   * then add that reusable block to post
   */
  if ( ! $is_preview ) {
    $post_type = get_post_type();
    if ( $post_type && 'wp_block' !== $post_type && is_array( $block['post_types'] ) && ! in_array( $post_type, $block['post_types'] ) ) {
      return '';
    }
  }

We could improve this.

Note to self: In older Air-light versions the check against $post_type causes block previews to disappear when it returns revision instead of wp_block. Not 100% sure if the newest if ( ! $is_preview ) { check fixes this, in one project it's fixed with this if:

  if ( $post_type && ( 'revision' !== $post_type && 'wp_block' !== $post_type ) && is_array( $block['post_types'] ) && ! in_array( $post_type, $block['post_types'] ) ) {
    return '';
  }

Anyhow, that if needs restructuring to something more manageable.