Frontend.php 17.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
<?php

namespace SearchWP\Results;

use SearchWP\Forms\Frontend as SearchFormsFrontend;
use SearchWP\Forms\Storage;
use SearchWP\Support\Arr;

/**
 * Display search results page on the frontend.
 *
 * @since 4.3.6
 */
class Frontend {

    /**
     * Init.
     *
     * @since 4.3.6
     */
    public function init() {

        $this->hooks();
    }

    /**
     * Hooks.
     *
     * @since 4.3.6
     */
    public function hooks() {

        add_action( 'wp_enqueue_scripts', [ __CLASS__, 'assets' ] );
        add_filter( 'template_include', [ __CLASS__, 'render' ], PHP_INT_MAX );
    }

    /**
     * Load frontend assets.
     *
     * @since 4.3.6
     */
    public static function assets() {

        wp_enqueue_style(
            'searchwp-results-page',
            SEARCHWP_PLUGIN_URL . 'assets/css/frontend/results-page.css',
            [],
            SEARCHWP_VERSION
        );

        wp_add_inline_style( 'searchwp-results-page', self::get_inline_styles() );
    }

    /**
     * Get dynamic inline styles based on current settings.
     *
     * @since 4.3.6
     */
    private static function get_inline_styles() {

        $settings = Settings::get();

        $css_array = [];

        $title_selector = '.swp-result-item .entry-title';
        if ( ! empty( $settings['swp-title-color'] ) ) {
            Arr::set( $css_array, "{$title_selector}/color", esc_html( $settings['swp-title-color'] ), '/' );
        }
        if ( ! empty( $settings['swp-title-font-size'] ) ) {
            Arr::set( $css_array, "{$title_selector}/font-size", absint( $settings['swp-title-font-size'] ) . 'px', '/' );
        }

        $price_selector = '.swp-result-item .swp-result-item--price, .swp-result-item .swp-result-item--price *';
        if ( ! empty( $settings['swp-price-color'] ) ) {
            Arr::set( $css_array, "{$price_selector}/color", esc_html( $settings['swp-price-color'] ), '/' );
        }
        if ( ! empty( $settings['swp-price-font-size'] ) ) {
            Arr::set( $css_array, "{$price_selector}/font-size", absint( $settings['swp-price-font-size'] ) . 'px', '/' );
        }

        $button_selector = '.swp-result-item a.swp-result-item--button';
        if ( ! empty( $settings['swp-button-font-color'] ) ) {
            Arr::set( $css_array, "{$button_selector}/color", esc_html( $settings['swp-button-font-color'] ), '/' );
        }
        if ( ! empty( $settings['swp-button-bg-color'] ) ) {
            Arr::set( $css_array, "{$button_selector}/background-color", esc_html( $settings['swp-button-bg-color'] ), '/' );
        }
        if ( ! empty( $settings['swp-button-font-size'] ) ) {
            Arr::set( $css_array, "{$button_selector}/font-size", absint( $settings['swp-button-font-size'] ) . 'px', '/' );
        }

        return self::css_array_to_css( $css_array );
    }

    /**
     * Render page.
     *
     * @since 4.3.6
     *
     * @param string $template The path of the template to include.
     *
     * @return string
     */
    public static function render( $template ) {

        if ( empty( $_GET['swps'] ) ) {
            return $template;
        }

        $form_id = isset( $_GET['swp_form']['form_id'] ) ? absint( $_GET['swp_form']['form_id'] ) : 0;
        $engine = 'default';

        if ( ! empty( $form_id ) ) {
            $form   = Storage::get( $form_id );
            $engine = ! empty( $form['engine'] ) ? $form['engine'] : 'default';
        }

        // TODO: Include template instead of echo output.

        $settings = Settings::get();

        // Retrieve applicable query parameters.
        $search_query = isset( $_GET['swps'] ) ? sanitize_text_field( $_GET['swps'] ) : null;
        $search_page  = isset( $_GET['swppg'] ) ? absint( $_GET['swppg'] ) : 1;

        // Perform the search.
        $search_results    = [];
        $search_pagination = '';
        $per_page = absint( $settings['swp-results-per-page'] );
        if ( ! empty( $search_query ) && class_exists( '\\SearchWP\\Query' ) ) {
            $search_args = [
                'engine' => $engine, // The Engine name.
                'fields' => 'all',          // Load proper native objects of each result.
                'page'   => $search_page,
            ];

            if ( ! empty( $per_page ) ) {
                $search_args['per_page'] = $per_page;
            }
            $searchwp_query = new \SearchWP\Query( $search_query, $search_args );

            $search_results = $searchwp_query->get_results();

            $search_pagination = paginate_links( array(
                'format'  => '?swppg=%#%',
                'current' => $search_page,
                'total'   => $searchwp_query->max_num_pages,
                'prev_text' => "&larr;",
                'next_text' => "&rarr;",
            ) );
        }

        if ( wp_is_block_theme() ) {
            ?><!DOCTYPE html>
            <html <?php language_attributes(); ?>>
            <head>
                <meta charset="<?php bloginfo( 'charset' ); ?>" />
                <?php wp_head(); ?>
            </head>
            <body <?php body_class(); ?>>
                <?php wp_body_open(); ?>
                <div class="wp-site-blocks">
                <?php block_template_part( 'header' ); ?>
                <main class="wp-block-group swp-rp-main">
                    <header class="swp-rp-page-header">
                        <?php echo ! empty( $form['id'] ) ? SearchFormsFrontend::render( [ 'id' => $form['id'] ] ) : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
                        <h1 class="page-title">
                        <?php
                            echo esc_html(
                                sprintf(
                                    /* translators: %s is the search term. */
                                    __( 'Search results for: "%s"', 'searchwp' ),
                                    $search_query
                                )
                            );
                        ?>
                    </h1>
                    </header>
            <?php

        } else {
            get_header('searchwp');
            ?>
            <div id="content" class="site-content">
                <div id="primary" class="content-area">
                    <main id="main" class="site-main swp-rp-main">
                        <header class="swp-rp-page-header">
                            <?php echo ! empty( $form['id'] ) ? SearchFormsFrontend::render( [ 'id' => $form['id'] ] ) : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>

                            <h1 class="page-title">
                                <?php
                                    echo esc_html(
                                        sprintf(
                                            /* translators: %s is the search term. */
                                            __( 'Search results for: "%s"', 'searchwp' ),
                                            $search_query
                                        )
                                    );
                                ?>
                            </h1>
                        </header>
                <?php
        }
        ?>
        <div class="<?php echo esc_attr( self::get_container_classes( $settings ) ); ?>">

        <?php if ( ! empty( $search_query ) && ! empty( $search_results ) ) : ?>
            <?php foreach ( $search_results as $search_result ) : ?>
                <?php $display_data = self::get_display_data( $search_result ); ?>
                <article id="post-0" class="swp-result-item post-0 post type-post status-publish format-standard hentry category-uncategorized entry">
                    <?php if ( ! empty( $display_data['image_html'] ) && ! empty( $settings['swp-image-size'] ) && $settings['swp-image-size'] !== 'none' ) : ?>
                        <div class="swp-result-item--img-container">
                            <div class="swp-result-item--img">
                                <?php echo wp_kses_post( $display_data['image_html'] ); ?>
                            </div>
                        </div>
                    <?php endif; ?>
                    <div class="swp-result-item--info-container">
                        <h2 class="entry-title">
                            <a href="<?php echo esc_url( $display_data['permalink'] ); ?>">
                                <?php echo wp_kses_post( $display_data['title'] ); ?>
                            </a>
                        </h2>
                        <?php if ( ! empty( $settings['swp-description-enabled'] ) ) : ?>
                            <p class="swp-result-item--desc">
                                <?php echo wp_kses_post( $display_data['content'] ); ?>
                            </p>
                        <?php endif; ?>

                        <?php if ( in_array( $display_data['type'], [ 'product', 'download' ], true ) ) : ?>
                            <p class="swp-result-item--price">
                                <?php echo wp_kses_post( $display_data['type'] === 'product' ? self::get_product_price_html( $display_data['id'] ) : self::get_download_price_html( $display_data['id'] ) ); ?>
                            </p>
                        <?php endif; ?>

                        <?php if ( ! empty( $settings['swp-button-enabled'] ) ) : ?>
                            <a href="<?php echo esc_url( $display_data['permalink'] ); ?>" class="swp-result-item--button">
                                <?php echo ! empty( $settings['swp-button-label'] ) ? esc_html( $settings['swp-button-label'] ) : esc_html__( 'Read More', 'searchwp' ); ?>
                            </a>
                                <?php endif; ?>
                    </div>
                </article>
            <?php endforeach; ?>

            </div><!-- End of .swp-search-results -->

            <?php if ( $searchwp_query->max_num_pages > 1 ) : ?>
                <div class="navigation pagination" role="navigation">
                    <h2 class="screen-reader-text"><?php esc_html_e( 'Results navigation', 'searchwp' ); ?></h2>
                    <div class="<?php echo esc_attr( self::get_pagination_classes( $settings ) ); ?>"><?php echo wp_kses_post( $search_pagination ); ?></div>
                </div>
            <?php endif; ?>
        <?php elseif ( ! empty( $search_query ) ) : ?>
            <p><?php esc_html_e( 'No results found, please search again.', 'searchwp' ); ?></p>
        <?php endif; ?>
        <?php

        if ( wp_is_block_theme() ) {
                    ?>
                        </main>
                        <footer class="wp-block-template-part">
                            <?php block_template_part( 'footer' ); ?>
                        </footer>
                    <div><!-- End of .wp-site-blocks -->

                    <?php wp_footer(); ?>
                </body>
            </html>
            <?php
        } else {
            ?>
                    </main>
                </div><!-- End of #primary -->
            </div><!-- End of #content -->
            <?php
            get_footer( 'searchwp' );
        }

        return '';
    }

    /**
     * Get the result data to display in the template.
     *
     * @since 4.3.6
     *
     * @param \WP_Post|\WP_User|\WP_Term|mixed $result Result object.
     *
     * @return array
     */
    private static function get_display_data( $result ) {

        if ( $result instanceof \WP_Post ) {
            $data = [
                'id'         => absint( $result->ID ),
                'type'       => get_post_type( $result ),
                'title'      => get_the_title( $result ),
                'permalink'  => get_the_permalink( $result ),
                'image_html' => get_the_post_thumbnail( $result ),
                'content'    => get_the_excerpt( $result ),
            ];
        }

        if ( $result instanceof \WP_User ) {
            $data = [
                'id'         => absint( $result->ID ),
                'type'       => 'user',
                'title'      => $result->data->display_name,
                'permalink'  => get_author_posts_url( $result->data->ID ),
                'image_html' => get_avatar( $result->data->ID ),
                'content'    => get_the_author_meta( 'description', $result->data->ID ),
            ];
        }

        if ( $result instanceof \WP_Term ) {
            $data = [
                'id'         => absint( $result->term_id ),
                'type'       => 'taxonomy-term',
                'title'      => $result->name,
                'permalink'  => get_term_link( $result->term_id, $result->taxonomy ),
                'image_html' => '',
                'content'    => $result->description,
            ];
        }

        $defaults = [
            'id'         => 0,
            'type'       => 'unknown',
            'title'      => '',
            'permalink'  => '',
            'image_html' => '',
            'content'    => '',
        ];

        $data = apply_filters( 'searchwp\results\entry\data', $data, $result );

        // Make sure that default array structure is preserved.
        return is_array( $data ) ? array_merge( $defaults, $data ) : $defaults;
    }

    /**
     * Get search results page container classes.
     *
     * @since 4.3.6
     *
     * @param array $settings Search Results Page settings.
     *
     * @return string
     */
    private static function get_container_classes( $settings ) {

        $classes = [
            'swp-search-results',
        ];

        if ( $settings['swp-layout-style'] === 'grid' ) {
            $classes[] = 'swp-grid';
            $per_row   = absint( $settings['swp-results-per-row'] );
            if ( ! empty( $per_row ) ) {
                $classes[] = 'swp-grid--cols-' . $per_row;
            }
        }

        if ( $settings['swp-layout-style'] === 'list' ) {
            $classes[] = 'swp-flex';
        }

        $image_size = $settings['swp-image-size'];
        if ( empty( $image_size ) || $image_size === 'none' ) {
            $classes[] = 'swp-rp--img-none';
        }
        if ( $image_size === 'small' ) {
            $classes[] = 'swp-rp--img-sm';
        }
        if ( $image_size === 'medium' ) {
            $classes[] = 'swp-rp--img-m';
        }
        if ( $image_size === 'large' ) {
            $classes[] = 'swp-rp--img-l';
        }

        return implode( ' ', $classes );
    }

    /**
     * Get search results page pagination classes.
     *
     * @since 4.3.6
     *
     * @param array $settings Search Results Page settings.
     *
     * @return string
     */
    private static function get_pagination_classes( $settings ) {
        $classes = [
            'nav-links',
        ];

        if ( $settings['swp-pagination-style'] === 'circular' ) {
            $classes[] = 'swp-results-pagination';
            $classes[] = 'swp-results-pagination--circular';
        }

        if ( $settings['swp-pagination-style'] === 'boxed' ) {
            $classes[] = 'swp-results-pagination';
            $classes[] = 'swp-results-pagination--boxed';
        }

        return implode( ' ', $classes );
    }

    /**
     * Get WooCommerce product price HTML.
     *
     * @since 4.3.6
     *
     * @param int $product_id WooCommerce product id.
     *
     * @return string
     */
    private static function get_product_price_html( $product_id ) {

        if ( ! function_exists( 'wc_get_product' ) ) {
            return '';
        }

        $product = wc_get_product( $product_id );

        if ( empty( $product ) ) {
            return '';
        }

        return $product->get_price_html();
    }

    /**
     * Get EDD product price HTML.
     *
     * @since 4.3.6
     *
     * @param int $download_id EDD download id.
     *
     * @return string
     */
    private static function get_download_price_html( $download_id ) {

        if ( ! function_exists( 'edd_price' ) ) {
            return '';
        }

        return edd_price( $download_id, false );
    }

    /**
     * Recursive function that generates from a multidimensional array of CSS rules, a valid CSS string.
     *
     * @since 4.3.6
     *
     * @param array $rules  CSS rules array.
     *   An array of CSS rules in the form of:
     *   array('selector'=>array('property' => 'value')). Also supports selector
     *   nesting, e.g.,
     *   array('selector' => array('selector'=>array('property' => 'value'))).
     * @param int   $indent Indentation level.
     *
     * @return string A CSS string of rules. This is not wrapped in <style> tags.
     * @source http://matthewgrasmick.com/article/convert-nested-php-array-css-string
     */
    private static function css_array_to_css( $rules, $indent = 0 ) {
        $css    = '';
        $prefix = str_repeat( '  ', $indent );

        foreach ( $rules as $key => $value ) {
            if ( is_array( $value ) ) {
                $selector   = $key;
                $properties = $value;

                $css .= $prefix . "$selector {\n";
                $css .= $prefix . self::css_array_to_css( $properties, $indent + 1 );
                $css .= $prefix . "}\n";
            } else {
                $property = $key;
                $css     .= $prefix . "$property: $value;\n";
            }
        }

        return $css;
    }
}