/** * Find Post from URL * * Supports: * * https://example.com/post.html * https://example.com/post/ * https://example.com/post * * Supports Thai / Unicode slugs. */ private function extract_from_url($url) { global $wpdb; /* * Validate URL */ if (!filter_var($url, FILTER_VALIDATE_URL)) { return [ 'success' => false, 'message' => 'URL ไม่ถูกต้อง' ]; } /* * Parse URL */ $parsed = wp_parse_url($url); if (!$parsed) { return [ 'success' => false, 'message' => 'ไม่สามารถอ่าน URL ได้' ]; } $post = null; /* * --------------------------------------------------------- * METHOD 1 * * Try WordPress's own URL resolver first. * * This is the most reliable method when the URL * belongs to the current WordPress installation. * --------------------------------------------------------- */ $post_id = url_to_postid($url); if ($post_id) { $post = get_post($post_id); } /* * --------------------------------------------------------- * METHOD 2 * * ?p=123 * --------------------------------------------------------- */ if (!$post && !empty($parsed['query'])) { parse_str( $parsed['query'], $query ); if ( !empty($query['p']) && is_numeric($query['p']) ) { $post = get_post( absint($query['p']) ); } } /* * --------------------------------------------------------- * METHOD 3 * * Find by slug. * * Important: * * DO NOT sanitize Thai slug manually before passing * it to get_page_by_path(). * * WordPress itself handles URL encoding. * --------------------------------------------------------- */ if (!$post) { $path = isset($parsed['path']) ? trim($parsed['path'], '/') : ''; if ($path !== '') { /* * Get final segment */ $segments = explode( '/', $path ); $slug = end( $segments ); /* * Remove .html * * Example: * * nakee-นาคี.html * * becomes: * * nakee-นาคี */ $slug = preg_replace( '/\.html$/iu', '', $slug ); /* * Try raw slug first * * Do NOT sanitize_title() */ $post = get_page_by_path( $slug, OBJECT, ['post'] ); /* * Try decoded slug */ if (!$post) { $decoded_slug = rawurldecode( $slug ); $post = get_page_by_path( $decoded_slug, OBJECT, ['post'] ); } /* * Try URL encoded slug */ if (!$post) { $encoded_slug = rawurlencode( $slug ); $post = get_page_by_path( $encoded_slug, OBJECT, ['post'] ); } } } /* * --------------------------------------------------------- * METHOD 4 * * Direct database lookup. * * This is the fallback for unusual permalink structures. * --------------------------------------------------------- */ if (!$post && !empty($slug)) { /* * Generate possible versions */ $candidates = []; /* * Original */ $candidates[] = $slug; /* * URL decoded */ $candidates[] = rawurldecode( $slug ); /* * URL encoded */ $candidates[] = rawurlencode( rawurldecode($slug) ); /* * WordPress sanitized/query version */ $candidates[] = sanitize_title_for_query( rawurldecode($slug) ); /* * Remove duplicates */ $candidates = array_values( array_unique( array_filter( $candidates ) ) ); /* * Search Database */ foreach ($candidates as $candidate) { $post_id = $wpdb->get_var( $wpdb->prepare( " SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'post' LIMIT 1 ", $candidate ) ); if ($post_id) { $post = get_post( $post_id ); break; } } } /* * --------------------------------------------------------- * Post not found * --------------------------------------------------------- */ if (!$post) { return [ 'success' => false, 'message' => 'ไม่พบ WordPress Post จาก URL นี้' ]; } /* * --------------------------------------------------------- * Get Post Content * --------------------------------------------------------- */ $content = (string) $post->post_content; /* * --------------------------------------------------------- * Find [watch file="..."] * * Support: * * [watch file="..."] * * [watch file='...'] * * Attribute order does not matter. * --------------------------------------------------------- */ $patterns = [ /* * Double quotes */ '/\[watch\b[^\]]*?\bfile\s*=\s*"([^"]+)"[^\]]*\]/iu', /* * Single quotes */ "/\\[watch\\b[^\\]]*?\\bfile\\s*=\\s*'([^']+)'[^\\]]*\\]/iu" ]; $files = []; /* * Extract */ foreach ($patterns as $pattern) { if ( preg_match_all( $pattern, $content, $matches ) ) { if ( !empty( $matches[1] ) ) { $files = array_merge( $files, $matches[1] ); } } } /* * --------------------------------------------------------- * Clean Files * --------------------------------------------------------- */ $clean_files = []; foreach ($files as $file) { $file = html_entity_decode( trim($file), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); if ($file === '') { continue; } $clean_files[] = $file; } /* * --------------------------------------------------------- * Remove duplicate Watch Files * --------------------------------------------------------- */ $clean_files = array_values( array_unique( $clean_files ) ); /* * --------------------------------------------------------- * Return * --------------------------------------------------------- */ return [ 'success' => true, 'post_id' => $post->ID, 'slug' => $post->post_name, 'files' => $clean_files ]; }