1
\$\begingroup\$

I've been working on a refactoring my old WordPress pricing page template and would appreciate feedback on the code structure, organization, and any potential improvements. The template is used to display a pricing list with categories and items.

I've found some time so here we are, I've splitted my long code into reusable parts, prepared my data, but still... My code is not performing as it should, it's kinda slow. Any idea how to refactor it in terms of performance?

<?php

    /**
     * Fetch posts and their children, then display them sorted by a custom order or category.
     * 
     * @param string $category_slug The slug of the category to display posts from.
     * @param int $category_term_id The term ID of the category.
     */
    function fetch_and_display_posts_by_category(string $category_slug, int $category_term_id): void {
        $term_posts_order_ids = get_field('cennik_order_' . $category_slug, 'option');

        $args = [
            'post_type' => 'cennik',
            'posts_per_page' => -1,
            'post_parent' => 0,
            'tax_query' => [[
                'taxonomy' => 'cennik_kat',
                'field' => 'term_id',
                'terms' => $category_term_id,
                'include_children' => true,
                'operator' => 'IN',
            ]],
            'orderby' => 'title',
            'order' => 'ASC',
            'no_found_rows' => true, 
        ];
        
        $query = new WP_Query($args);

        if (!empty($term_posts_order_ids)) {
            $query->posts = sort_objects_by_id_order($query->posts, $term_posts_order_ids);
        }

        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                display_post_and_children(get_the_ID(), $category_slug);
            }
            wp_reset_postdata();
        }
    }

    /**
     * Display a post and its children, sorted by a custom order.
     * 
     * @param int $post_id The ID of the post to display.
     * @param string $category_slug The slug of the category, for fetching custom order.
     */
    function display_post_and_children(int $post_id, string $category_slug) : void {
        $children = fetch_and_sort_children_posts($post_id, $category_slug);
        display_post_based_on_type($post_id, $category_slug, $children);
    }

    /**
     * Fetch and sort children posts of a given post.
     * 
     * @param int $post_id The ID of the parent post.
     * @param string $category_slug The slug of the category, for fetching custom order.
     * @return array The sorted children posts.
     */
    function fetch_and_sort_children_posts(int $post_id, string $category_slug): array {
        $children = get_children([
            'post_parent' => $post_id,
            'post_type' => 'cennik',
            'numberposts' => -1,
            'post_status' => 'publish',
            'orderby' => 'menu_order',
            'order' => 'ASC',
        ]);

        $children_order_ids = get_field('children_order', $post_id);
        if (!empty($children_order_ids)) {
            $children = sort_objects_by_id_order($children, $children_order_ids);
        } elseif ($term_posts_order_ids = get_field('cennik_order_' . $category_slug, 'option')) {
            $children = sort_objects_by_id_order($children, $term_posts_order_ids);
        }

        return $children;
    }


    function display_post_based_on_type($post_id, $category_slug = '') {
        $post = get_post($post_id);
        $is_pakiet = has_term(get_pakiet_term_id(), 'cennik_kat', $post_id);
        $children = fetch_and_sort_children_posts($post_id, $category_slug);
        $has_children = !empty($children);

        $is_pakiet = $is_pakiet && $has_children;
        $is_group = $has_children && !$is_pakiet;
        $is_single = !$has_children;

        ob_start();

        if ($is_group || $is_pakiet) { ?>
            <div class="details prl__item prl__gap prl__item--gr d-flex f-c w-100">
                <summary class="hm__title p d-flex f-c ais jcb cp w-100">
                    <span class="c-black-800 h5 f-400 m-0"><?= esc_html($post->post_title) ?></span>
                    <?php if (DEBUG) : ?>
                        <a style="font-size: 1em;" tabindex="-1" href="<?= esc_url(get_edit_post_link($post_id)) ?>">(Edytuj)</a>
                    <?php endif; ?>
                    <div class="prl__cat-btn d-flex aic jce">
                        <span class="toggle-text">Rozwiń</span>
                        <div class="hm__icon-wr d-flex aic jcc br">
                            <svg class="hm__icon" width="16" height="16" viewBox="0 0 16 16">
                                <use xlink:href="#arrow-down"></use>
                            </svg>
                        </div>
                    </div>
                </summary>
                <div class="content-wrapper prl__hm--wrapper w-100">
                    <div class="content-inner prl__hm--inner">
                        <div class="real-content prl__gap d-flex f-c">
                            <?php
                            foreach ($children as $child_id => $child) {
                                display_post_based_on_type($child->ID);
                            }
                            ?>
                        </div>
                    </div>
                </div>
            </div>
            <?php
        } elseif ($is_single) {
            ?>
            <div class="prl__item prl__gap d-flex f-c w-100">
                <?php print_pricelist_item($post_id);?>
            </div>
            <?php
        }

        echo ob_get_clean();
    }

    $cennik_kat_order         = get_field( 'cennik_kat', 'option' ) ?: [];
    $sorted_cennik_categories = get_sorted_pricelist_categories( $cennik_kat_order );

?>

<section class="prl c d-grid l-d">
    <div class="prl__h d-flex jcc aic f-c" style="--gap: 54px">
        <div class="prl__h--top">
            Informujemy Państwa, że w klinice Wilmed istnieje możliwość płacenia za usługi medyczne w systemie ratalnym MediRaty.
            <?php (new MiLossy($mediraty))->Draw(); ?>
        </div>
        <h3 class="prl__heading h3 f-400 c-heading m-reset">Cennik zabiegów</h3>
    </div>
    <div class="prl__cats hm d-flex f-c">
        <?php foreach ($sorted_cennik_categories as $key => $category) : ?>
            <div class="details prl__cat hm__item--wr br" id="<?= $category->slug; ?>">
                <summary class="hm__title--main p d-flex f-c ais jcb cp w-100 zero-level-hierarchy">
                    <span class="c-black-800 h5 f-400 m-0"><?= $category->name ?></span>
                    <?php if (DEBUG) : ?>
                        <a style="font-size: 1em;" tabindex="-1" href="<?= get_edit_term_link($category->term_id, 'cennik_kat'); ?>">(Edytuj)</a>
                    <?php endif; ?>
                    <div class="prl__cat-btn d-flex aic jce">
                        <span class="toggle-text level-zero">Rozwiń</span>
                        <div class="hm__icon-wr d-flex aic jcc br">
                            <svg class="hm__icon" width="16" height="16" viewBox="0 0 16 16">
                                <use xlink:href="#arrow-down"></use>
                            </svg>
                        </div>
                    </div>
                </summary>
                <div class="content-wrapper prl__hm--wrapper w-100">
                    <div class="content-inner prl__hm--inner">
                        <div class="real-content prl__gap d-flex f-c">
                            <?php fetch_and_display_posts_by_category($category->slug, $category->term_id); ?>
                        </div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</section>
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.