\d+)', array( 'methods' => 'GET', 'callback' => 'deanna_get_ministore_data', 'permission_callback' => '__return_true', )); }); function dms_clean_text($text) { $text = wp_strip_all_tags($text); $text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8'); $text = preg_replace('/\s+/', ' ', $text); return trim($text); } function dms_extract_images_from_html($html) { $images = array(); if (empty($html)) return $images; libxml_use_internal_errors(true); $dom = new DOMDocument(); $dom->loadHTML('' . $html); libxml_clear_errors(); $img_tags = $dom->getElementsByTagName('img'); foreach ($img_tags as $img) { $src = $img->getAttribute('src'); if (!empty($src)) { $images[] = $src; } } return array_values(array_unique($images)); } function dms_extract_links_from_html($html) { $links = array(); if (empty($html)) return $links; libxml_use_internal_errors(true); $dom = new DOMDocument(); $dom->loadHTML('' . $html); libxml_clear_errors(); $a_tags = $dom->getElementsByTagName('a'); foreach ($a_tags as $a) { $href = trim($a->getAttribute('href')); $label = dms_clean_text($a->textContent); if (!$href || !$label) continue; if (stripos($href, 'http') !== 0) continue; $links[] = array( 'label' => $label, 'url' => $href ); } $links = array_slice($links, 0, 6); return $links; } function dms_extract_paragraphs_from_html($html) { $paragraphs = array(); if (empty($html)) return $paragraphs; libxml_use_internal_errors(true); $dom = new DOMDocument(); $dom->loadHTML('' . $html); libxml_clear_errors(); $p_tags = $dom->getElementsByTagName('p'); foreach ($p_tags as $p) { $text = dms_clean_text($p->textContent); if (!empty($text) && strlen($text) > 40) { $paragraphs[] = $text; } } return $paragraphs; } function dms_extract_sections_from_html($html, $hero_image) { $sections = array(); if (empty($html)) return $sections; libxml_use_internal_errors(true); $dom = new DOMDocument(); $dom->loadHTML('' . $html); libxml_clear_errors(); $body = $dom->getElementsByTagName('body')->item(0); if (!$body) return $sections; $current_title = ''; $current_texts = array(); foreach ($body->childNodes as $node) { if (!isset($node->nodeName)) continue; $tag = strtolower($node->nodeName); if ($tag === 'h2' || $tag === 'h3') { if ($current_title && !empty($current_texts)) { $sections[] = array( 'title' => dms_clean_text($current_title), 'text' => implode(' ', $current_texts), 'image' => $hero_image ); } $current_title = $node->textContent; $current_texts = array(); } elseif ($tag === 'p') { $text = dms_clean_text($node->textContent); if (!empty($text) && strlen($text) > 30) { $current_texts[] = $text; } } } if ($current_title && !empty($current_texts)) { $sections[] = array( 'title' => dms_clean_text($current_title), 'text' => implode(' ', $current_texts), 'image' => $hero_image ); } return array_slice($sections, 0, 8); } function dms_build_highlights($title, $sections, $paragraphs) { $highlights = array(); foreach ($sections as $section) { if (!empty($section['title'])) { $highlights[] = $section['title']; } } if (count($highlights) < 4 && !empty($paragraphs)) { $highlights[] = 'Original article content'; $highlights[] = 'Visual landing-page format'; } $highlights = array_values(array_unique($highlights)); return array_slice($highlights, 0, 6); } function deanna_get_ministore_data($request) { $post_id = intval($request['id']); $post = get_post($post_id); if (!$post) { return new WP_Error('not_found', 'Post not found', array('status' => 404)); } $title = get_the_title($post_id); $slug = $post->post_name; $content_html = apply_filters('the_content', $post->post_content); $hero_image = get_the_post_thumbnail_url($post_id, 'full'); if (!$hero_image) { $content_images = dms_extract_images_from_html($content_html); $hero_image = !empty($content_images) ? $content_images[0] : ''; } $paragraphs = dms_extract_paragraphs_from_html($content_html); $images = dms_extract_images_from_html($content_html); $links = dms_extract_links_from_html($content_html); $sections = dms_extract_sections_from_html($content_html, $hero_image); $highlights = dms_build_highlights($title, $sections, $paragraphs); $subtitle = !empty($paragraphs[0]) ? $paragraphs[0] : 'Discover this destination in a more visual, interactive format.'; $description = ''; if (!empty($paragraphs[0])) $description .= $paragraphs[0] . ' '; if (!empty($paragraphs[1])) $description .= $paragraphs[1]; $description = trim($description); if (empty($description)) { $description = 'This destination guide transforms a traditional WordPress entry into a more visual landing-page experience.'; } $gallery = array(); if ($hero_image) $gallery[] = $hero_image; foreach ($images as $img) { if ($img !== $hero_image) { $gallery[] = $img; } } $gallery = array_values(array_unique($gallery)); $gallery = array_slice($gallery, 0, 6); if (empty($links)) { $links = array( array( 'label' => 'Read original article', 'url' => get_permalink($post_id) ), array( 'label' => 'Explore Deanna Spain', 'url' => 'https://deannaspain.com' ) ); } else { array_unshift($links, array( 'label' => 'Read original article', 'url' => get_permalink($post_id) )); $links = array_slice($links, 0, 6); } return array( 'id' => $post_id, 'title' => $title, 'subtitle' => $subtitle, 'slug' => $slug, 'hero_image' => $hero_image, 'gallery' => $gallery, 'description' => $description, 'highlights' => $highlights, 'sections' => $sections, 'pricing' => array( 'label' => 'Article Type', 'value' => 'Travel Guide' ), 'video_url' => '', 'booking_url' => get_permalink($post_id), 'links' => $links, 'reviews_enabled' => false ); }