/** * Displayes json for search results. * @since 1.0 */ public function json() { $data = []; try { // Search args $args = apply_filters('addon_typeahead_query', ['posts_per_page' => Request::input('posts_per_page', get_option('addon_typeahead_limit', 5)), 'post_type' => Request::input('post_type', 'post'), 'post_status' => Request::input('post_status', 'publish'), 's' => Request::input('s', '')]); // Results $data = Cache::remember('addon_typeahead_res_' . $args['posts_per_page'] . '_' . urlencode($args['s']), 15, function () use(&$args) { $query = new WP_Query($args); $data = []; while ($query->have_posts()) { $query->the_post(); $data[] = apply_filters('addon_typeahead_post', get_post(null, OBJECT)); } return $data; }); // Filter results $data = Cache::remember('addon_typeahead_filteredres_' . $args['posts_per_page'] . '_' . urlencode($args['s']), 15, function () use(&$data, &$args) { return apply_filters('addon_typeahead_data', $data, $args); }); } catch (\Exception $e) { Log::error($e); $data = []; } // Render JSON header('Content-Type: application/json'); if (get_option('addon_typeahead_crossdomain', false)) { header('Access-Control-Allow-Origin: *'); } echo json_encode($data); die; }
/** * Returns thumbnail size image url. * @since 1.0 * * @return string */ protected function get_search_image_url() { $url = $this->image_url; return Cache::remember('addon_' . $this->ID . '_searchimage', 43200, function () use($url) { if ($url) { return resize_image($url, 300, 135); } return; }); }
/** * Returns a resized image url. * Resized on height constraint. * * @param string $url Base url. * @param int $height Height to resize to. * * @return string */ public static function height($url, $height = 0) { if (empty($height) || !is_numeric($height)) { $height = get_option('thumbnail_size_h'); } $info = pathinfo($url); $cacheKey = preg_replace(['/:filename/', '/:width/', '/:height/'], [$info['filename'], '', $height], get_option('thumbnail_cache_format', ':filename_:widthx:height')); return Cache::remember($cacheKey, get_option('thumbnail_cache_minutes', 43200), function () use($url, $height, $info) { $upload_dir = wp_upload_dir(); $size = getimagesize($url); $assetPath = sprintf('/%s_x%s.%s', $info['filename'], $height, $info['extension']); if (!file_exists($upload_dir['path'] . $assetPath)) { $image = new ImageResize($url); $image->interlace = 1; $image->scale(ceil(100 + ($height - $size[1]) / $size[1] * 100)); $image->save($upload_dir['path'] . $assetPath); } return $upload_dir['url'] . $assetPath; }); }