177 lines
4.4 KiB
PHP
177 lines
4.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* The public-facing functionality of the plugin.
|
|
*
|
|
* @link https://github.com/Duskell
|
|
* @since 1.0.0
|
|
* @package Partnerexpo_Core
|
|
* @subpackage Partnerexpo_Core/public
|
|
* @author Juhász Levente <juhasz.levente@rendszerepito.hu>
|
|
*/
|
|
class Partnerexpo_Core_Public {
|
|
|
|
/**
|
|
* The ID of this plugin.
|
|
*
|
|
* @since 1.0.0
|
|
* @access private
|
|
* @var string $plugin_name The ID of this plugin.
|
|
*/
|
|
private $plugin_name;
|
|
|
|
/**
|
|
* The version of this plugin.
|
|
*
|
|
* @since 1.0.0
|
|
* @access private
|
|
* @var string $version The current version of this plugin.
|
|
*/
|
|
private $version;
|
|
|
|
/**
|
|
* Initialize the class and set its properties.
|
|
*
|
|
* @since 1.0.0
|
|
* @param string $plugin_name The name of the plugin.
|
|
* @param string $version The version of this plugin.
|
|
*/
|
|
public function __construct( $plugin_name, $version ) {
|
|
|
|
$this->plugin_name = $plugin_name;
|
|
$this->version = $version;
|
|
|
|
add_shortcode( 'partnerexpo_searchbox', [ $this, 'searchbox_shortcode' ] );
|
|
}
|
|
|
|
public function searchbox_shortcode() {
|
|
wp_enqueue_style( $this->plugin_name . '-searchbox-css' );
|
|
wp_enqueue_script( $this->plugin_name . '-searchbox-js' );
|
|
wp_enqueue_style( $this->plugin_name . '-multiselect-css' );
|
|
wp_enqueue_script( $this->plugin_name . '-multiselect-js' );
|
|
|
|
ob_start();
|
|
include plugin_dir_path( __FILE__ ) . 'partials/partnerexpo-core-public-searchbox.php';
|
|
return ob_get_clean();
|
|
}
|
|
|
|
public function register_endpoint() {
|
|
register_rest_route('pexpo/v1', '/query', [
|
|
'methods' => 'GET',
|
|
'callback' => [ $this, 'query_partners' ],
|
|
'permission_callback' => '__return_true',
|
|
]);
|
|
}
|
|
|
|
function query_partners(WP_REST_Request $request) {
|
|
$params = $request->get_query_params();
|
|
|
|
if (isset($params['force_tags']) && isset($params['tags']) && $params['force_tags'] === 'true') {
|
|
$params['tags'] = str_replace(',', '+', $params['tags']);
|
|
}
|
|
|
|
$args = [
|
|
'post_type' => 'pexpo_partners',
|
|
'posts_per_page' => $params['resultsPerPage'] ?? 10,
|
|
'paged' => $params['page'] ?? 1,
|
|
'post_status' => 'publish',
|
|
's' => $params['q'] ?? '',
|
|
'pexpo_tags' => $params['tags'] ?? '',
|
|
];
|
|
|
|
switch ($params['sort'] ?? 'relevance') {
|
|
case 'date_asc':
|
|
$args['orderby'] = 'date';
|
|
$args['order'] = 'ASC';
|
|
break;
|
|
case 'date_desc':
|
|
$args['orderby'] = 'date';
|
|
$args['order'] = 'DESC';
|
|
break;
|
|
case 'title_asc':
|
|
$args['orderby'] = 'title';
|
|
$args['order'] = 'ASC';
|
|
break;
|
|
case 'title_desc':
|
|
$args['orderby'] = 'title';
|
|
$args['order'] = 'DESC';
|
|
break;
|
|
case 'relevance':
|
|
default:
|
|
// Default WordPress search sorting
|
|
break;
|
|
}
|
|
|
|
$query = new WP_Query($args);
|
|
|
|
$posts = [];
|
|
|
|
foreach ($query->posts as $post) {
|
|
$tags = wp_get_post_terms($post->ID, 'pexpo_tags', ['fields' => 'names']);
|
|
|
|
$posts[] = [
|
|
'id' => $post->ID,
|
|
'title' => $post->post_title,
|
|
'image' => get_the_post_thumbnail_url($post->ID),
|
|
'excerpt' => $post->post_excerpt,
|
|
'date' => date('Y-m-d', strtotime($post->post_date)),
|
|
'tag' => $tags ?? null,
|
|
'url' => get_permalink($post),
|
|
'order' => $params['sort'] ?? 'relevance',
|
|
];
|
|
}
|
|
|
|
wp_reset_postdata();
|
|
|
|
return rest_ensure_response($posts);
|
|
}
|
|
|
|
/**
|
|
* Register the stylesheets for the public-facing side of the site.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function register_styles() {
|
|
wp_register_style(
|
|
$this->plugin_name . '-searchbox-css',
|
|
plugin_dir_url( __FILE__ ) . 'css/searchbox.css',
|
|
[],
|
|
$this->version,
|
|
'all'
|
|
);
|
|
|
|
wp_register_style(
|
|
$this->plugin_name . '-multiselect-css',
|
|
plugin_dir_url( __FILE__ ) . 'css/multiselect.css',
|
|
[],
|
|
$this->version,
|
|
'all'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Register the JavaScript for the public-facing side of the site.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function register_scripts() {
|
|
wp_register_script(
|
|
$this->plugin_name . '-searchbox-js',
|
|
plugin_dir_url( __FILE__ ) . 'js/searchbox.js',
|
|
[],
|
|
$this->version,
|
|
true
|
|
);
|
|
|
|
wp_register_script(
|
|
$this->plugin_name . '-multiselect-js',
|
|
plugin_dir_url( __FILE__ ) . 'js/multiselect.js',
|
|
[],
|
|
$this->version,
|
|
true
|
|
);
|
|
}
|
|
|
|
|
|
}
|