'PostalAddress', 'addressCountry' => 'ID', ]; if (!empty($contact_info['streetAddress'])) { $address['streetAddress'] = ledak88_clean_text($contact_info['streetAddress']); } if (!empty($contact_info['addressLocality'])) { $address['addressLocality'] = ledak88_clean_text($contact_info['addressLocality']); } if (!empty($contact_info['postalCode'])) { $address['postalCode'] = preg_replace( '/[^0-9]/', '', (string) $contact_info['postalCode'] ); } // Do not publish an invented or incomplete address. if (count($address) <= 1) { return []; } return $address; } function ledak88_clean_schema_value($value) { if (!is_array($value)) { return $value; } foreach ($value as $key => $item) { $value[$key] = ledak88_clean_schema_value($item); if ( $value[$key] === '' || $value[$key] === null || $value[$key] === [] ) { unset($value[$key]); } } return $value; } function ledak88_get_context(): array { $brand_name = sanitize_text_field( get_option('seo_brand_name', '') ); if ($brand_name === '') { $brand_name = get_bloginfo('name'); } $meta_keywords = sanitize_text_field( get_option('seo_meta_keywords', '') ); $meta_description = wp_strip_all_tags( get_option('seo_meta_description', ''), true ); if ($meta_description === '') { $meta_description = get_bloginfo('description'); } $register_url_opt = (string) get_option( 'register_url', '/register' ); $brand_url = home_url('/'); $brand_base_url = untrailingslashit($brand_url); // Custom Logo (dynamic) $logo_id = (int) get_theme_mod('custom_logo'); $logo_url = ''; $logo_w = 0; $logo_h = 0; if ($logo_id) { $img = wp_get_attachment_image_src($logo_id, 'full'); if ($img) { $logo_url = esc_url_raw($img[0]); $logo_w = (int) $img[1]; $logo_h = (int) $img[2]; } } if (!$logo_url) { $fallback = get_site_icon_url(512); if ($fallback) { $logo_url = esc_url_raw($fallback); $logo_w = 512; $logo_h = 512; } else { $logo_url = $brand_url . 'wp-content/uploads/2025/09/Slot-8-16-2025-1-300x300.png'; $logo_w = 1023; $logo_h = 270; } } // Primary image dari Rank Math OpenGraph. $primary_img = ''; $primary_img_w = 0; $primary_img_h = 0; $front_id = (int) get_option('page_on_front'); if (!$front_id && is_front_page()) { $front_id = (int) get_queried_object_id(); } if ($front_id) { $rm_og_id = (int) get_post_meta( $front_id, 'rank_math_facebook_image_id', true ); $rm_og_url = (string) get_post_meta( $front_id, 'rank_math_facebook_image', true ); if ($rm_og_id) { $img = wp_get_attachment_image_src($rm_og_id, 'full'); if ($img) { $primary_img = esc_url_raw($img[0]); $primary_img_w = (int) $img[1]; $primary_img_h = (int) $img[2]; } } elseif ( $rm_og_url && filter_var($rm_og_url, FILTER_VALIDATE_URL) ) { $primary_img = esc_url_raw($rm_og_url); } } if (!$primary_img) { $rm_titles = get_option('rank-math-options-titles'); if (is_array($rm_titles)) { foreach ( [ 'homepage_facebook_image_id', 'home_facebook_image_id', 'facebook_image_id', 'open_graph_image_id', ] as $key ) { if (!empty($rm_titles[$key])) { $img = wp_get_attachment_image_src( (int) $rm_titles[$key], 'full' ); if ($img) { $primary_img = esc_url_raw($img[0]); $primary_img_w = (int) $img[1]; $primary_img_h = (int) $img[2]; break; } } } if (!$primary_img) { foreach ( [ 'homepage_facebook_image', 'home_facebook_image', 'facebook_image', 'open_graph_image', ] as $key ) { if ( !empty($rm_titles[$key]) && filter_var( $rm_titles[$key], FILTER_VALIDATE_URL ) ) { $primary_img = esc_url_raw($rm_titles[$key]); break; } } } } } if (!$primary_img && $front_id) { $thumb_id = get_post_thumbnail_id($front_id); if ($thumb_id) { $img = wp_get_attachment_image_src($thumb_id, 'full'); if ($img) { $primary_img = esc_url_raw($img[0]); $primary_img_w = (int) $img[1]; $primary_img_h = (int) $img[2]; } } } if (!$primary_img) { $site_icon = get_site_icon_url(512); if ($site_icon) { $primary_img = esc_url_raw($site_icon); $primary_img_w = 512; $primary_img_h = 512; } else { $primary_img = $brand_url . 'wp-content/uploads/2025/02/situs_utama.png'; } } $register_url = ( strpos($register_url_opt, 'http') === 0 ) ? esc_url_raw($register_url_opt) : esc_url_raw( $brand_url . ltrim($register_url_opt, '/') ); $front_title = $front_id ? get_the_title($front_id) : get_bloginfo('name'); $document_title = function_exists('wp_get_document_title') ? wp_get_document_title() : $front_title; $seo_headline = ledak88_clean_text( $document_title ?: $front_title ); $date_published = ''; $date_modified = ''; if ($front_id) { $date_published = get_post_time( 'c', true, $front_id ); $date_modified = get_post_modified_time( 'c', true, $front_id ); } return [ 'brand_name' => $brand_name, 'brand_url' => $brand_base_url, 'meta_keywords' => $meta_keywords, 'meta_description' => ledak88_clean_text($meta_description), 'register_url' => $register_url, 'logo_url' => esc_url_raw($logo_url), 'logo_width' => $logo_w, 'logo_height' => $logo_h, 'primary_img' => esc_url_raw($primary_img), 'primary_img_w' => $primary_img_w, 'primary_img_h' => $primary_img_h, 'author_img' => esc_url_raw( 'https://secure.gravatar.com/avatar/' . '9bdcf8bde35b2c78188e374ead5d384753db22a1e35d93da48484759ad7f0852' . '?s=96&d=wavatar&r=g' ), 'front_title' => ledak88_clean_text($front_title), 'seo_headline' => $seo_headline, 'date_published' => $date_published, 'date_modified' => $date_modified, ]; } /* ========================================================== * Preserve original field-normalization helper. * ========================================================== */ function ledak88_schema_force_types($value) { $to_bool = static function ($value) { if (is_bool($value)) { return $value; } if (is_int($value)) { return $value === 1; } if (is_float($value)) { return ((int) $value) === 1; } if (is_string($value)) { $value = strtolower(trim($value)); if (in_array($value, ['true', '1', 'yes'], true)) { return true; } if (in_array($value, ['false', '0', 'no'], true)) { return false; } } return (bool) $value; }; $walk = static function ($node) use (&$walk, $to_bool) { if (is_array($node)) { foreach ($node as $key => $value) { if ($key === 'isAccessibleForFree') { $node[$key] = $to_bool($value); } else { $node[$key] = $walk($value); } } } return $node; }; return $walk($value); } function ledak88_image_node( string $id, string $url, int $width = 0, int $height = 0, string $caption = '' ): array { $node = [ '@type' => 'ImageObject', '@id' => $id, 'url' => esc_url_raw($url), 'contentUrl' => esc_url_raw($url), ]; if ($width > 0) { $node['width'] = $width; } if ($height > 0) { $node['height'] = $height; } if ($caption !== '') { $node['caption'] = ledak88_clean_text($caption); } return $node; } function ledak88_build_organization_node( array $context, array $contact_info = [] ): array { $brand = $context['brand_name']; $home = $context['brand_url']; $same_as = ledak88_get_verified_same_as(); $contact_points = []; // Organization.image is separate from Organization.logo. // Use the site's verified primary image. $organization_image = [ '@type' => 'ImageObject', 'url' => esc_url_raw($context['primary_img']), 'contentUrl' => esc_url_raw($context['primary_img']), 'caption' => $brand, ]; if ((int) $context['primary_img_w'] > 0) { $organization_image['width'] = (int) $context['primary_img_w']; } if ((int) $context['primary_img_h'] > 0) { $organization_image['height'] = (int) $context['primary_img_h']; } if (!empty($contact_info['emails'][0])) { $contact_points[] = [ '@type' => 'ContactPoint', 'contactType' => 'customer support', 'email' => sanitize_email( $contact_info['emails'][0] ), 'areaServed' => 'ID', 'availableLanguage' => ['id'], ]; } if (!empty($contact_info['phones'][0])) { $contact_points[] = [ '@type' => 'ContactPoint', 'contactType' => 'customer support', 'telephone' => preg_replace( '/[^0-9+()\-\s]/', '', (string) $contact_info['phones'][0] ), 'areaServed' => 'ID', 'availableLanguage' => ['id'], ]; } if (!$contact_points) { $contact_points[] = [ '@type' => 'ContactPoint', 'contactType' => 'customer support', 'areaServed' => 'ID', 'availableLanguage' => ['id'], ]; } $organization = [ '@type' => 'Organization', '@id' => $home . '/#organization', 'name' => $brand, 'url' => $home . '/', 'logo' => ledak88_image_node( $home . '/#logo', $context['logo_url'], (int) $context['logo_width'], (int) $context['logo_height'], $brand . ' Logo' ), 'image' => $organization_image, 'description' => $context['meta_description'], 'contactPoint' => $contact_points, 'brand' => [ '@type' => 'Brand', 'name' => $brand, ], ]; if ($same_as) { $organization['sameAs'] = $same_as; } $address = ledak88_build_address($contact_info); if ($address) { $organization['address'] = $address; } if (!empty($contact_info['emails'][0])) { $organization['email'] = sanitize_email( $contact_info['emails'][0] ); } if (!empty($contact_info['phones'][0])) { $organization['telephone'] = preg_replace( '/[^0-9+()\-\s]/', '', (string) $contact_info['phones'][0] ); } return ledak88_clean_schema_value($organization); } /* ========================================= * Build one consistent homepage @graph. * ========================================= */ function ledak88_custom_graph_nodes(): array { $context = ledak88_get_context(); $brand = $context['brand_name']; $home = $context['brand_url']; $contact_info = ledak88_extract_contact_from_page(); $primary_image = ledak88_image_node( $home . '/#primaryimage', $context['primary_img'], (int) $context['primary_img_w'], (int) $context['primary_img_h'], $brand ); $breadcrumb = [ '@type' => 'BreadcrumbList', '@id' => $home . '/#breadcrumb', 'itemListElement' => [[ '@type' => 'ListItem', 'position' => 1, 'name' => 'Home', 'item' => $home . '/', ]], ]; $website = [ '@type' => 'WebSite', '@id' => $home . '/#website', 'url' => $home . '/', 'name' => $brand, 'description' => $context['meta_description'], 'publisher' => [ '@id' => $home . '/#organization', ], ]; $webpage = [ '@type' => 'WebPage', '@id' => $home . '/#webpage', 'url' => $home . '/', 'name' => $context['seo_headline'] ?: $brand, 'description' => $context['meta_description'], 'isPartOf' => [ '@id' => $home . '/#website', ], 'about' => [ '@id' => $home . '/#organization', ], 'primaryImageOfPage' => [ '@id' => $home . '/#primaryimage', ], 'image' => [ '@id' => $home . '/#primaryimage', ], 'breadcrumb' => [ '@id' => $home . '/#breadcrumb', ], 'inLanguage' => get_bloginfo('language') ?: 'id-ID', ]; if (!empty($context['date_published'])) { $webpage['datePublished'] = $context['date_published']; } if (!empty($context['date_modified'])) { $webpage['dateModified'] = $context['date_modified']; } // FAQPage intentionally is not generated here. $graph = [ ledak88_build_organization_node( $context, $contact_info ), $website, $primary_image, $breadcrumb, $webpage, ]; return array_map( 'ledak88_schema_force_types', $graph ); } /* ======================================= * Build @graph for single posts. * ======================================= */ function ledak88_post_graph_nodes(int $post_id): array { $context = ledak88_get_context(); $home = $context['brand_url']; $brand = $context['brand_name']; $post = get_post($post_id); if (!$post || $post->post_status !== 'publish') { return []; } $url = get_permalink($post_id); if (!$url) { return []; } $title = ledak88_clean_text( get_the_title($post_id) ); $desc = has_excerpt($post_id) ? ledak88_clean_text(get_the_excerpt($post_id)) : ledak88_clean_text( wp_trim_words( wp_strip_all_tags($post->post_content), 30, '' ) ); if ($desc === '') { $desc = $context['meta_description']; } $date_published = get_post_time( 'c', true, $post ); $date_modified = get_post_modified_time( 'c', true, $post ); $img_url = ''; $img_w = 0; $img_h = 0; $thumb_id = get_post_thumbnail_id($post_id); if ($thumb_id) { $img = wp_get_attachment_image_src( $thumb_id, 'full' ); if ($img) { $img_url = esc_url_raw($img[0]); $img_w = (int) $img[1]; $img_h = (int) $img[2]; } } if (!$img_url) { $img_url = $context['primary_img']; $img_w = (int) $context['primary_img_w']; $img_h = (int) $context['primary_img_h']; } $author_id = (int) $post->post_author; $author_name = $author_id ? ledak88_clean_text( get_the_author_meta( 'display_name', $author_id ) ) : ''; if ($author_name === '') { $author_name = $brand; } $author_is_organization = strcasecmp($author_name, $brand) === 0; $author_node_id = $url . '#author'; $author_ref = $author_is_organization ? ['@id' => $home . '/#organization'] : ['@id' => $author_node_id]; $author_node = null; if (!$author_is_organization) { $author_url = $author_id ? get_author_posts_url($author_id) : ''; $author_img = $author_id ? get_avatar_url( $author_id, ['size' => 96] ) : $context['author_img']; $author_node = [ '@type' => 'Person', '@id' => $author_node_id, 'name' => $author_name, ]; if ($author_url) { $author_node['url'] = esc_url_raw($author_url); } if ($author_img) { $author_node['image'] = ledak88_image_node( $author_node_id . '-image', $author_img, 96, 96, $author_name ); } } $item_list = [[ '@type' => 'ListItem', 'position' => 1, 'name' => 'Home', 'item' => $home . '/', ]]; $position = 2; $cats = get_the_category($post_id); if (!empty($cats) && !is_wp_error($cats)) { $cat = $cats[0]; $cat_link = get_category_link($cat); if ($cat_link && !is_wp_error($cat_link)) { $item_list[] = [ '@type' => 'ListItem', 'position' => $position++, 'name' => ledak88_clean_text( $cat->name ), 'item' => esc_url_raw($cat_link), ]; } } $item_list[] = [ '@type' => 'ListItem', 'position' => $position, 'name' => $title, 'item' => esc_url_raw($url), ]; $breadcrumb = [ '@type' => 'BreadcrumbList', '@id' => $url . '#breadcrumb', 'itemListElement' => $item_list, ]; $primary_image = ledak88_image_node( $url . '#primaryimage', $img_url, $img_w, $img_h, $title ); $contact_info = ledak88_extract_contact_from_page(); $webpage = [ '@type' => 'WebPage', '@id' => $url . '#webpage', 'url' => esc_url_raw($url), 'name' => $title, 'description' => $desc, 'isPartOf' => [ '@id' => $home . '/#website', ], 'about' => [ '@id' => $home . '/#organization', ], 'primaryImageOfPage' => [ '@id' => $url . '#primaryimage', ], 'image' => [ '@id' => $url . '#primaryimage', ], 'datePublished' => $date_published, 'dateModified' => $date_modified, 'breadcrumb' => [ '@id' => $url . '#breadcrumb', ], 'inLanguage' => get_bloginfo('language') ?: 'id-ID', ]; $article = [ '@type' => 'BlogPosting', '@id' => $url . '#article', 'headline' => $title, 'description' => $desc, 'url' => esc_url_raw($url), 'mainEntityOfPage' => [ '@id' => $url . '#webpage', ], 'author' => $author_ref, 'publisher' => [ '@id' => $home . '/#organization', ], 'datePublished' => $date_published, 'dateModified' => $date_modified, 'inLanguage' => get_bloginfo('language') ?: 'id-ID', 'image' => [ '@id' => $url . '#primaryimage', ], 'articleSection' => !empty($cats) ? ledak88_clean_text($cats[0]->name) : 'General', 'wordCount' => str_word_count( wp_strip_all_tags( $post->post_content ) ), ]; if ($context['meta_keywords'] !== '') { $article['keywords'] = $context['meta_keywords']; } $graph = [ ledak88_build_organization_node( $context, $contact_info ), $primary_image, $breadcrumb, $webpage, $article, ]; if ($author_node) { $graph[] = $author_node; } return array_map( function ($node) { return ledak88_schema_force_types( ledak88_clean_schema_value($node) ); }, $graph ); } /* ======================================= * Build @graph for regular Pages. * ======================================= */ function ledak88_page_graph_nodes(int $page_id): array { $context = ledak88_get_context(); $home = $context['brand_url']; $page = get_post($page_id); if (!$page || $page->post_status !== 'publish') { return []; } $url = get_permalink($page_id); if (!$url) { return []; } $title = ledak88_clean_text( get_the_title($page_id) ); $desc = has_excerpt($page_id) ? ledak88_clean_text(get_the_excerpt($page_id)) : ledak88_clean_text( wp_trim_words( wp_strip_all_tags($page->post_content), 30, '' ) ); if ($desc === '') { $desc = $context['meta_description']; } $img_url = $context['primary_img']; $img_w = (int) $context['primary_img_w']; $img_h = (int) $context['primary_img_h']; $thumb_id = get_post_thumbnail_id($page_id); if ($thumb_id) { $img = wp_get_attachment_image_src( $thumb_id, 'full' ); if ($img) { $img_url = esc_url_raw($img[0]); $img_w = (int) $img[1]; $img_h = (int) $img[2]; } } $breadcrumb = [ '@type' => 'BreadcrumbList', '@id' => $url . '#breadcrumb', 'itemListElement' => [ [ '@type' => 'ListItem', 'position' => 1, 'name' => 'Home', 'item' => $home . '/', ], [ '@type' => 'ListItem', 'position' => 2, 'name' => $title, 'item' => esc_url_raw($url), ], ], ]; $webpage = [ '@type' => 'WebPage', '@id' => $url . '#webpage', 'url' => esc_url_raw($url), 'name' => $title, 'description' => $desc, 'isPartOf' => [ '@id' => $home . '/#website', ], 'about' => [ '@id' => $home . '/#organization', ], 'primaryImageOfPage' => [ '@id' => $url . '#primaryimage', ], 'image' => [ '@id' => $url . '#primaryimage', ], 'breadcrumb' => [ '@id' => $url . '#breadcrumb', ], 'datePublished' => get_post_time( 'c', true, $page ), 'dateModified' => get_post_modified_time( 'c', true, $page ), 'inLanguage' => get_bloginfo('language') ?: 'id-ID', 'publisher' => [ '@id' => $home . '/#organization', ], ]; $graph = [ ledak88_build_organization_node( $context, ledak88_extract_contact_from_page() ), ledak88_image_node( $url . '#primaryimage', $img_url, $img_w, $img_h, $title ), $breadcrumb, $webpage, ]; return array_map( function ($node) { return ledak88_schema_force_types( ledak88_clean_schema_value($node) ); }, $graph ); } /* ===================================================== * Contact extraction. * ===================================================== */ function ledak88_parse_rankmath_contact(): array { $raw = get_option( 'rank_math_contact_info', '' ); $out = [ 'emails' => [], 'phones' => [], 'address' => '', 'hours' => [], ]; $arr = []; if (is_array($raw)) { $arr = $raw; } else { $lines = preg_split( '/\r\n|\r|\n/', (string) $raw ); foreach ($lines as $line) { if (strpos($line, ':') !== false) { [$key, $value] = array_map( 'trim', explode(':', $line, 2) ); if ($key !== '') { $arr[strtolower($key)] = $value; } } } } foreach ($arr as $key => $value) { $key = strtolower((string) $key); $value = trim((string) $value); if ($value === '') { continue; } if (strpos($key, 'email') !== false) { $mail = sanitize_email($value); if ($mail) { $out['emails'][] = $mail; } } elseif ( strpos($key, 'wa') !== false || strpos($key, 'tel') === 0 || strpos($key, 'phone') !== false || strpos($key, 'telepon') !== false ) { $out['phones'][] = preg_replace( '/[^0-9+()\-\s]/', '', $value ); } elseif ( strpos($key, 'alamat') !== false || strpos($key, 'address') !== false ) { $out['address'] = $value; } elseif ( strpos($key, 'jam') !== false || strpos($key, 'hour') !== false ) { $out['hours'][] = $value; } } $out['emails'] = array_values( array_filter( array_unique($out['emails']) ) ); $out['phones'] = array_values( array_filter( array_unique($out['phones']) ) ); $out['hours'] = array_values( array_filter( array_unique($out['hours']) ) ); return $out; } function ledak88_extract_contact_from_page(): array { $page = get_page_by_path('kontak-kami'); $contact_info = [ 'emails' => [], 'phones' => [], 'streetAddress' => '', 'addressLocality' => '', 'postalCode' => '', 'address' => '', 'hours' => [], ]; if ($page && !empty($page->post_content)) { $html = (string) $page->post_content; $plain = preg_replace( '//i', "\n", $html ); $plain = preg_replace( '/<\/(p|div|li|h[1-6])\s*>/i', "\n", $plain ); $stripped = wp_strip_all_tags($plain); preg_match_all( '/[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}/', $stripped, $email_matches ); foreach (($email_matches[0] ?? []) as $email) { $email = sanitize_email($email); if ($email) { $contact_info['emails'][] = $email; } } preg_match_all( '/(?:\+?62|0)[\d\-()\s]{8,16}/', $stripped, $phone_matches ); foreach (($phone_matches[0] ?? []) as $phone) { $phone = preg_replace( '/[^0-9+()\-\s]/', '', $phone ); if ($phone) { $contact_info['phones'][] = $phone; } } $lines = array_values( array_filter( array_map( 'trim', preg_split( '/\r\n|\r|\n/', $stripped ) ) ) ); foreach ($lines as $line) { if ( $contact_info['streetAddress'] === '' && preg_match( '/^(Jl\.?|Jalan|Street|St\.?)[^,]*/i', $line, $match ) ) { $contact_info['streetAddress'] = trim($match[0]); } if ( $contact_info['addressLocality'] === '' && preg_match( '/\b(Jakarta(?:\s+(?:Pusat|Selatan|Barat|Timur|Utara))?|Bandung|Surabaya|Medan|Semarang|Yogyakarta|Denpasar|Makassar|Palembang|Balikpapan)\b/i', $line, $match ) ) { $contact_info['addressLocality'] = trim($match[1]); } if ( $contact_info['postalCode'] === '' && preg_match( '/\b(\d{5})\b/', $line, $match ) ) { $contact_info['postalCode'] = $match[1]; } if ( preg_match( '/^(Jam|Hours|Buka|Open|Senin|Selasa|Rabu|Kamis|Jumat|Sabtu|Minggu)/i', $line ) ) { $contact_info['hours'][] = $line; } } if ( $contact_info['streetAddress'] !== '' || $contact_info['addressLocality'] !== '' ) { $parts = array_filter([ $contact_info['streetAddress'], $contact_info['addressLocality'], $contact_info['postalCode'], ]); $contact_info['address'] = implode(', ', $parts); } } $rankmath_contact = ledak88_parse_rankmath_contact(); if ( !$contact_info['emails'] && $rankmath_contact['emails'] ) { $contact_info['emails'] = $rankmath_contact['emails']; } if ( !$contact_info['phones'] && $rankmath_contact['phones'] ) { $contact_info['phones'] = $rankmath_contact['phones']; } if ( !$contact_info['address'] && $rankmath_contact['address'] ) { $contact_info['address'] = $rankmath_contact['address']; } if ( !$contact_info['hours'] && $rankmath_contact['hours'] ) { $contact_info['hours'] = $rankmath_contact['hours']; } if ( !$contact_info['postalCode'] && !empty($rankmath_contact['address']) && preg_match( '/\b(\d{5})\b/', $rankmath_contact['address'], $match ) ) { $contact_info['postalCode'] = $match[1]; } $contact_info['emails'] = array_values( array_unique( array_filter($contact_info['emails']) ) ); $contact_info['phones'] = array_values( array_unique( array_filter($contact_info['phones']) ) ); $contact_info['hours'] = array_values( array_unique( array_filter($contact_info['hours']) ) ); return $contact_info; } function ledak88_about_graph_nodes(): array { $context = ledak88_get_context(); $home = $context['brand_url']; $url = home_url('/tentang-kami/'); $page = get_page_by_path('tentang-kami'); $description = $context['meta_description']; $date_modified = ''; if ($page) { $description = has_excerpt($page->ID) ? ledak88_clean_text( get_the_excerpt($page->ID) ) : ledak88_clean_text( wp_trim_words( wp_strip_all_tags( $page->post_content ), 30, '' ) ); $date_modified = get_post_modified_time( 'c', true, $page ); } $graph = [ ledak88_build_organization_node( $context, ledak88_extract_contact_from_page() ), ledak88_image_node( $url . '#primaryimage', $context['primary_img'], (int) $context['primary_img_w'], (int) $context['primary_img_h'], $context['brand_name'] ), [ '@type' => 'BreadcrumbList', '@id' => $url . '#breadcrumb', 'itemListElement' => [ [ '@type' => 'ListItem', 'position' => 1, 'name' => 'Home', 'item' => $home . '/', ], [ '@type' => 'ListItem', 'position' => 2, 'name' => 'Tentang Kami', 'item' => esc_url_raw($url), ], ], ], [ '@type' => [ 'AboutPage', 'WebPage', ], '@id' => $url . '#webpage', 'url' => esc_url_raw($url), 'name' => 'Tentang ' . $context['brand_name'], 'description' => $description, 'isPartOf' => [ '@id' => $home . '/#website', ], 'about' => [ '@id' => $home . '/#organization', ], 'primaryImageOfPage' => [ '@id' => $url . '#primaryimage', ], 'image' => [ '@id' => $url . '#primaryimage', ], 'breadcrumb' => [ '@id' => $url . '#breadcrumb', ], 'publisher' => [ '@id' => $home . '/#organization', ], 'inLanguage' => get_bloginfo('language') ?: 'id-ID', ], ]; if ($date_modified) { $graph[3]['dateModified'] = $date_modified; } return array_map( function ($node) { return ledak88_schema_force_types( ledak88_clean_schema_value($node) ); }, $graph ); } function ledak88_contact_graph_nodes(): array { $context = ledak88_get_context(); $home = $context['brand_url']; $url = home_url('/kontak-kami/'); $page = get_page_by_path('kontak-kami'); $contact_info = ledak88_extract_contact_from_page(); $description = 'Hubungi ' . $context['brand_name'] . ' untuk dukungan dan pertanyaan.'; $date_modified = ''; if ($page) { $description = has_excerpt($page->ID) ? ledak88_clean_text( get_the_excerpt($page->ID) ) : ledak88_clean_text( wp_trim_words( wp_strip_all_tags( $page->post_content ), 30, '' ) ); $date_modified = get_post_modified_time( 'c', true, $page ); } $graph = [ ledak88_build_organization_node( $context, $contact_info ), ledak88_image_node( $url . '#primaryimage', $context['primary_img'], (int) $context['primary_img_w'], (int) $context['primary_img_h'], $context['brand_name'] ), [ '@type' => 'BreadcrumbList', '@id' => $url . '#breadcrumb', 'itemListElement' => [ [ '@type' => 'ListItem', 'position' => 1, 'name' => 'Home', 'item' => $home . '/', ], [ '@type' => 'ListItem', 'position' => 2, 'name' => 'Kontak Kami', 'item' => esc_url_raw($url), ], ], ], [ '@type' => [ 'ContactPage', 'WebPage', ], '@id' => $url . '#webpage', 'url' => esc_url_raw($url), 'name' => 'Kontak ' . $context['brand_name'], 'description' => $description, 'isPartOf' => [ '@id' => $home . '/#website', ], 'about' => [ '@id' => $home . '/#organization', ], 'mainEntity' => [ '@id' => $home . '/#organization', ], 'primaryImageOfPage' => [ '@id' => $url . '#primaryimage', ], 'image' => [ '@id' => $url . '#primaryimage', ], 'breadcrumb' => [ '@id' => $url . '#breadcrumb', ], 'publisher' => [ '@id' => $home . '/#organization', ], 'inLanguage' => get_bloginfo('language') ?: 'id-ID', ], ]; if ($date_modified) { $graph[3]['dateModified'] = $date_modified; } return array_map( function ($node) { return ledak88_schema_force_types( ledak88_clean_schema_value($node) ); }, $graph ); } /* ===================================================== * Rank Math integration. * ===================================================== */ function ledak88_schema_node_types($node): array { if (!is_array($node) || empty($node['@type'])) { return []; } $types = is_array($node['@type']) ? $node['@type'] : [$node['@type']]; return array_values( array_filter( array_map('strval', $types) ) ); } function ledak88_schema_remove_conflicts( array $data, array $conflicts ): array { $wrapped = isset($data['@graph']) && is_array($data['@graph']); $nodes = $wrapped ? $data['@graph'] : $data; foreach ($nodes as $key => $node) { if (!is_array($node)) { continue; } if ( array_intersect( ledak88_schema_node_types($node), $conflicts ) ) { unset($nodes[$key]); } } if ($wrapped) { $data['@graph'] = $nodes; return $data; } return $nodes; } function ledak88_schema_add_custom_graph( array $data, array $custom_graph ): array { $wrapped = isset($data['@graph']) && is_array($data['@graph']); $nodes = $wrapped ? $data['@graph'] : $data; foreach ($custom_graph as $index => $node) { if (!is_array($node)) { continue; } $types = ledak88_schema_node_types($node); $key = !empty($types) ? $types[0] : 'CustomNode'; if (isset($nodes[$key])) { $key .= '_' . $index; } $nodes[$key] = $node; } if ($wrapped) { $data['@graph'] = $nodes; return $data; } return $nodes; } function ledak88_rank_math_custom_schema_filter( $data, $jsonld = null ) { if (!is_array($data)) { return $data; } if (is_front_page()) { $conflicts = [ 'WebPage', 'WebSite', 'Organization', 'BreadcrumbList', 'ImageObject', 'Article', 'BlogPosting', 'NewsArticle', 'News', 'LiveBlogPosting', 'FAQPage', 'Person', 'Question', 'Answer', 'ItemList', 'Product', 'Blog', 'CollectionPage', 'SearchAction', ]; $data = ledak88_schema_remove_conflicts( $data, $conflicts ); return ledak88_schema_add_custom_graph( $data, ledak88_custom_graph_nodes() ); } if (is_singular('post')) { $conflicts = [ 'WebPage', 'BreadcrumbList', 'ImageObject', 'Article', 'BlogPosting', 'NewsArticle', 'News', 'LiveBlogPosting', 'Person', 'Organization', 'ProfilePage', 'SpeakableSpecification', ]; $data = ledak88_schema_remove_conflicts( $data, $conflicts ); $graph = ledak88_post_graph_nodes( (int) get_queried_object_id() ); return $graph ? ledak88_schema_add_custom_graph( $data, $graph ) : $data; } if (is_page('tentang-kami')) { $conflicts = [ 'WebPage', 'AboutPage', 'BreadcrumbList', 'ImageObject', 'Organization', 'FAQPage', 'ContactPoint', 'Person', 'Article', 'BlogPosting', 'NewsArticle', 'News', 'LiveBlogPosting', ]; $data = ledak88_schema_remove_conflicts( $data, $conflicts ); return ledak88_schema_add_custom_graph( $data, ledak88_about_graph_nodes() ); } if (is_page('kontak-kami')) { $conflicts = [ 'WebPage', 'ContactPage', 'BreadcrumbList', 'ImageObject', 'Organization', 'ContactPoint', 'Person', 'Article', 'BlogPosting', 'NewsArticle', 'News', 'LiveBlogPosting', ]; $data = ledak88_schema_remove_conflicts( $data, $conflicts ); return ledak88_schema_add_custom_graph( $data, ledak88_contact_graph_nodes() ); } if (is_page()) { $conflicts = [ 'WebPage', 'BreadcrumbList', 'ImageObject', 'Organization', 'Person', 'Article', 'BlogPosting', 'NewsArticle', 'News', 'LiveBlogPosting', 'FAQPage', 'Question', 'Answer', ]; $data = ledak88_schema_remove_conflicts( $data, $conflicts ); $graph = ledak88_page_graph_nodes( (int) get_queried_object_id() ); return $graph ? ledak88_schema_add_custom_graph( $data, $graph ) : $data; } return $data; } // Rank Math filter for modifying JSON-LD data. add_filter( 'rank_math/json_ld', 'ledak88_rank_math_custom_schema_filter', 99, 2 ); // Disable unnecessary SearchAction output. add_filter( 'rank_math/json_ld/disable_search', '__return_true' ); /* ===================================================== * Fallback when Rank Math is not active. * ===================================================== */ add_action('wp_head', function () { if ( defined('RANK_MATH_VERSION') || class_exists('RankMath') ) { return; } $graph = []; if (is_front_page()) { $graph = ledak88_custom_graph_nodes(); } elseif (is_singular('post')) { $graph = ledak88_post_graph_nodes( (int) get_queried_object_id() ); } elseif (is_page('tentang-kami')) { $graph = ledak88_about_graph_nodes(); } elseif (is_page('kontak-kami')) { $graph = ledak88_contact_graph_nodes(); } elseif (is_page()) { $graph = ledak88_page_graph_nodes( (int) get_queried_object_id() ); } if (!$graph) { return; } $payload = [ '@context' => 'https://schema.org', '@graph' => array_map( function ($node) { return ledak88_schema_force_types( ledak88_clean_schema_value($node) ); }, $graph ), ]; echo "\n\n"; }, 99);