Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Link Scraper Debugger
- Description: Scrapes a link for the title and description and creates a new WooCommerce product.
- Version: 1.0
- Author: Plamen Ivanov
- */
- add_action('admin_menu', 'link_scraper_debugger_menu');
- function link_scraper_debugger_menu() {
- add_menu_page('Link Scraper Debugger', 'Link Scraper Debugger', 'manage_options', 'link-scraper-debugger', 'link_scraper_debugger_page');
- }
- function link_scraper_debugger_page() {
- ?>
- <div class="wrap">
- <h1>Link Scraper Debugger</h1>
- <form method="post" action="">
- <label for="scrape_url">Enter URL to Scrape:</label>
- <input type="text" id="scrape_url" name="scrape_url" style="width: 100%; max-width: 600px;">
- <input type="submit" name="scrape_submit" value="Scrape" class="button button-primary">
- </form>
- </div>
- <?php
- if (isset($_POST['scrape_submit'])) {
- $url = sanitize_text_field($_POST['scrape_url']);
- if (!empty($url)) {
- link_scraper_debugger_process($url);
- }
- }
- }
- function link_scraper_debugger_process($url) {
- $decoded_url = urldecode($url);
- $fullUrl = $decoded_url;
- $args = array(
- 'redirection' => 5,
- 'headers' => array(
- 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
- )
- );
- $response = wp_remote_get($decoded_url, $args);
- if (is_wp_error($response)) {
- echo '<div class="notice notice-error is-dismissible"><p>Failed to fetch URL: ' . $response->get_error_message() . '</p></div>';
- return;
- }
- $status_code = wp_remote_retrieve_response_code($response);
- if ($status_code != 200) {
- echo '<div class="notice notice-error is-dismissible"><p>Failed to fetch URL: HTTP status code ' . $status_code . '</p></div>';
- echo '<div class="notice notice-error is-dismissible"><p>Failed to fetch URL: ' . $decoded_url . '</p></div>';
- return;
- }
- $html = wp_remote_retrieve_body($response);
- $encoding = mb_detect_encoding($html, "UTF-8, Windows-1251, ISO-8859-1", true);
- if ($encoding != "UTF-8") {
- $html = mb_convert_encoding($html, "UTF-8", $encoding);
- }
- echo '<div class="notice notice-info is-dismissible"><p>Debug: HTML Content - ' . htmlspecialchars(substr($html, 0, 1000)) . '...</p></div>';
- if (!empty($html)) {
- libxml_use_internal_errors(true);
- $dom = new DOMDocument();
- $dom->loadHTML('<?xml encoding="utf-8" ?>' . $html);
- libxml_clear_errors();
- $xpath = new DOMXPath($dom);
- $titleNode = $xpath->query('//h1[@itemprop="name" and contains(@class, "product-single__title")]')->item(0);
- $title = $titleNode ? $titleNode->textContent : '';
- if ($titleNode) {
- echo '<div class="notice notice-info is-dismissible"><p>Debug: Title Node - ' . htmlspecialchars($dom->saveHTML($titleNode)) . '</p></div>';
- } else {
- echo '<div class="notice notice-warning is-dismissible"><p>Debug: Title Node not found</p></div>';
- }
- $descNode = $xpath->query('//div[contains(@class, "rte description")]')->item(0);
- $description = $descNode ? $dom->saveHTML($descNode) : '';
- if ($descNode) {
- echo '<div class="notice notice-info is-dismissible"><p>Debug: Description Node - ' . htmlspecialchars($dom->saveHTML($descNode)) . '</p></div>';
- } else {
- echo '<div class="notice notice-warning is-dismissible"><p>Debug: Description Node not found</p></div>';
- }
- } else {
- echo '<div class="notice notice-error is-dismissible"><p>Failed to retrieve HTML content.</p></div>';
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment