Files
PartnerExpo-Core/public/class-partnerexpo-core-public.php
2026-02-01 13:27:34 +01:00

122 lines
2.8 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' );
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) {
$args = [
'post_type' => 'pexpo_partners',
'posts_per_page' => 15,
'post_status' => 'publish',
];
$query = new WP_Query($args);
$posts = [];
foreach ($query->posts as $post) {
$posts[] = [
'id' => $post->ID,
'title' => $post->post_title,
'image' => get_the_post_thumbnail_url($post->ID),
'excerpt' => $post->post_excerpt,
'url' => get_permalink($post),
];
}
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 enqueue_styles() {
wp_register_style(
$this->plugin_name . '-searchbox-css',
plugin_dir_url( __FILE__ ) . 'css/searchbox.css',
[],
$this->version,
'all'
);
}
/**
* Register the JavaScript for the public-facing side of the site.
*
* @since 1.0.0
*/
public function enqueue_scripts() {
wp_register_script(
$this->plugin_name . '-searchbox-js',
plugin_dir_url( __FILE__ ) . 'js/searchbox.js',
[],
$this->version,
true
);
}
}