card_data = $card_data; $this->query_builder = $query_builder; $this->card_markup = $card_markup; $this->pages_amount = 0; $this->post_ids = array(); } /** * @param array $custom_arguments * * @return array */ protected function get_twig_variables( bool $is_for_validation = false, array $custom_arguments = array() ): array { return array( '_card' => array( 'id' => $this->card_data->get_markup_id(), // short unique id is expected in the shortcode arguments. 'view_id' => str_replace( View_Data::UNIQUE_ID_PREFIX, '', $this->card_data->acf_view_id ), 'no_posts_found_message' => $this->card_data->get_no_posts_found_message_translation(), 'post_ids' => $this->post_ids, 'classes' => $this->get_classes(), ), ); } /** * @param array $twig_variables */ protected function render_twig_and_print_html( string $twig_template, array $twig_variables, bool $is_for_validation = false ): bool { ob_start(); $this->get_twig()->print( $this->card_data->get_unique_id(), $twig_template, $twig_variables, $is_for_validation ); // render the shortcodes. echo do_shortcode( (string) ob_get_clean() ); return true; } protected function get_pages_amount(): int { return $this->pages_amount; } protected function get_card_data(): Card_Data { return $this->card_data; } /** * @param array $custom_arguments */ public function query_insert_and_print_html( int $page_number, bool $is_minify_markup = true, bool $is_load_more = false, array $custom_arguments = array() ): void { if ( $is_minify_markup ) { // remove special symbols that used in the markup for a preview // exactly here, before the fields are inserted, to avoid affecting them. $twig_template = str_replace( array( "\t", "\n", "\r" ), '', $this->get_twig_template() ); } $posts_data = $this->query_builder->get_posts_data( $this->card_data, $page_number, $custom_arguments ); $this->pages_amount = key_exists( 'pagesAmount', $posts_data ) && is_int( $posts_data['pagesAmount'] ) ? $posts_data['pagesAmount'] : 0; $this->post_ids = key_exists( 'postIds', $posts_data ) && is_array( $posts_data['postIds'] ) ? $posts_data['postIds'] : array(); $twig_template = $this->card_markup->get_markup( $this->card_data, $is_load_more ); $twig_variables = $this->get_twig_variables( false, $custom_arguments ); $this->render_twig_and_print_html( $twig_template, $twig_variables ); } public function getCardData(): Card_Data { return $this->card_data; } public function get_markup_validation_error(): string { $this->set_twig_template( $this->card_markup->get_markup( $this->card_data ) ); return parent::get_markup_validation_error(); } }