*/ private static array $rendered = array(); /** * Posts already checked for the shortcode or block. * * @var array */ private static array $checked = array(); /** Hooks the theme adjustments; each one checks the active theme itself. */ public function register(): void { add_filter( 'get_post_metadata', array( $this, 'cocoon_toc' ), 10, 3 ); } /** Called by the form renderer while the post content is being displayed. */ public static function form_rendered(): void { $id = get_the_ID(); if ( $id ) { self::$rendered[ (int) $id ] = true; } } /** * Answers Cocoon's per-page table-of-contents setting on booking pages. * * @param mixed $value Short-circuit value (null to read the database). * @param int $object_id Post ID. * @param string $meta_key Meta key. * @return mixed */ public function cocoon_toc( $value, $object_id, $meta_key ) { if ( self::COCOON_TOC_META !== $meta_key || is_admin() || 'cocoon-master' !== get_template() ) { return $value; } $object_id = (int) $object_id; if ( $object_id <= 0 || ! self::has_form( $object_id ) || ! apply_filters( 'abientot_hide_cocoon_toc', true, $object_id ) ) { return $value; } return array( '1' ); } /** * Whether the post shows a booking form (shortcode, block, or a form rendered from its content). * * @param int $id Post ID. */ private static function has_form( int $id ): bool { if ( ! empty( self::$rendered[ $id ] ) ) { return true; } if ( ! isset( self::$checked[ $id ] ) ) { $post = get_post( $id ); self::$checked[ $id ] = $post && ( has_shortcode( (string) $post->post_content, 'abientot_booking_form' ) || has_block( 'abientot/booking-form', $post ) ); } return self::$checked[ $id ]; } }