Guest User

Untitled

a guest
Jun 10th, 2024
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.80 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Link Scraper Debugger
  4. Description: Scrapes a link for the title and description and creates a new WooCommerce product.
  5. Version: 1.0
  6. Author: Plamen Ivanov
  7. */
  8.  
  9. add_action('admin_menu', 'link_scraper_debugger_menu');
  10.  
  11. function link_scraper_debugger_menu() {
  12.     add_menu_page('Link Scraper Debugger', 'Link Scraper Debugger', 'manage_options', 'link-scraper-debugger', 'link_scraper_debugger_page');
  13. }
  14.  
  15. function link_scraper_debugger_page() {
  16.     ?>
  17.     <div class="wrap">
  18.         <h1>Link Scraper Debugger</h1>
  19.         <form method="post" action="">
  20.             <label for="scrape_url">Enter URL to Scrape:</label>
  21.             <input type="text" id="scrape_url" name="scrape_url" style="width: 100%; max-width: 600px;">
  22.             <input type="submit" name="scrape_submit" value="Scrape" class="button button-primary">
  23.         </form>
  24.     </div>
  25.     <?php
  26.  
  27.     if (isset($_POST['scrape_submit'])) {
  28.         $url = sanitize_text_field($_POST['scrape_url']);
  29.         if (!empty($url)) {
  30.             link_scraper_debugger_process($url);
  31.         }
  32.     }
  33. }
  34.  
  35. function link_scraper_debugger_process($url) {
  36.     $decoded_url = urldecode($url);
  37.     $fullUrl = $decoded_url;
  38.    
  39.     $args = array(
  40.         'redirection' => 5,
  41.         'headers' => array(
  42.             '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'
  43.         )
  44.     );
  45.  
  46.     $response = wp_remote_get($decoded_url, $args);
  47.     if (is_wp_error($response)) {
  48.         echo '<div class="notice notice-error is-dismissible"><p>Failed to fetch URL: ' . $response->get_error_message() . '</p></div>';
  49.         return;
  50.     }
  51.  
  52.     $status_code = wp_remote_retrieve_response_code($response);
  53.     if ($status_code != 200) {
  54.         echo '<div class="notice notice-error is-dismissible"><p>Failed to fetch URL: HTTP status code ' . $status_code . '</p></div>';
  55.         echo '<div class="notice notice-error is-dismissible"><p>Failed to fetch URL: ' . $decoded_url . '</p></div>';
  56.         return;
  57.     }
  58.  
  59.     $html = wp_remote_retrieve_body($response);
  60.  
  61.     $encoding = mb_detect_encoding($html, "UTF-8, Windows-1251, ISO-8859-1", true);
  62.     if ($encoding != "UTF-8") {
  63.         $html = mb_convert_encoding($html, "UTF-8", $encoding);
  64.     }
  65.    
  66.     echo '<div class="notice notice-info is-dismissible"><p>Debug: HTML Content - ' . htmlspecialchars(substr($html, 0, 1000)) . '...</p></div>';
  67.  
  68.     if (!empty($html)) {
  69.         libxml_use_internal_errors(true);
  70.         $dom = new DOMDocument();
  71.         $dom->loadHTML('<?xml encoding="utf-8" ?>' . $html);
  72.         libxml_clear_errors();
  73.        
  74.         $xpath = new DOMXPath($dom);
  75.  
  76.         $titleNode = $xpath->query('//h1[@itemprop="name" and contains(@class, "product-single__title")]')->item(0);
  77.         $title = $titleNode ? $titleNode->textContent : '';
  78.  
  79.         if ($titleNode) {
  80.             echo '<div class="notice notice-info is-dismissible"><p>Debug: Title Node - ' . htmlspecialchars($dom->saveHTML($titleNode)) . '</p></div>';
  81.         } else {
  82.             echo '<div class="notice notice-warning is-dismissible"><p>Debug: Title Node not found</p></div>';
  83.         }
  84.  
  85.         $descNode = $xpath->query('//div[contains(@class, "rte description")]')->item(0);
  86.         $description = $descNode ? $dom->saveHTML($descNode) : '';
  87.  
  88.         if ($descNode) {
  89.             echo '<div class="notice notice-info is-dismissible"><p>Debug: Description Node - ' . htmlspecialchars($dom->saveHTML($descNode)) . '</p></div>';
  90.         } else {
  91.             echo '<div class="notice notice-warning is-dismissible"><p>Debug: Description Node not found</p></div>';
  92.         }
  93.     } else {
  94.         echo '<div class="notice notice-error is-dismissible"><p>Failed to retrieve HTML content.</p></div>';
  95.     }
  96. }
  97. ?>
  98.  
Advertisement
Add Comment
Please, Sign In to add comment