*/ 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' ] ); add_shortcode( 'partnerexpo_comment_section', [ $this, 'comments_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 comments_shortcode() { wp_enqueue_style( $this->plugin_name . '-comments-css' ); wp_enqueue_script( $this->plugin_name . '-comments-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-comments.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']); } $companies = explode(',', $params['companies'] ?? ''); $authors = get_users([ 'meta_key' => 'company', 'meta_value' => $companies, 'meta_compare' => 'IN', 'fields' => 'ID', ]); $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'] ?? '', ]; if (!empty($authors)) { $args['author__in'] = array_values($authors); } if ( ! empty( $params['tags'] ) ) { $tag_string = $params['tags']; $operator = 'IN'; $delimiter = ','; if ( strpos( $tag_string, '+' ) !== false ) { $operator = 'AND'; $delimiter = '+'; } $tag_slugs = explode( $delimiter, $tag_string ); $args['tax_query'] = [ [ 'taxonomy' => 'pexpo_tags', 'field' => 'slug', 'terms' => $tag_slugs, 'operator' => $operator, ], ]; } 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': $args['orderby'] = 'relevance'; break; default: break; } $query = new WP_Query_WithRelevance($args); $response = [ 'pages' => $query->max_num_pages, 'found' => $query->found_posts, 'results' => [], ]; $posts = []; foreach ($query->posts as $post) { $tags = wp_get_post_terms($post->ID, 'pexpo_tags', ['fields' => 'names']); $response['results'][] = [ '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), ]; } wp_reset_postdata(); return rest_ensure_response($response); } /** * 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 . '-comments-css', plugin_dir_url( __FILE__ ) . 'css/comments.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 . '-comments-js', plugin_dir_url( __FILE__ ) . 'js/comments.js', [], $this->version, true ); wp_register_script( $this->plugin_name . '-multiselect-js', plugin_dir_url( __FILE__ ) . 'js/multiselect.js', [], $this->version, true ); } }