*/ 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) { $terms = wp_get_post_terms($post->ID, 'taxidermy_tags', ['fields' => 'all']); $my_term = null; foreach ($terms as $term) { if ($term->slug === 'tags') { $my_term = $term->name; // or ->term_id break; } } $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' => $my_term ?? null, '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 ); } }