Advertisement
ssaidz

nori.php

Dec 8th, 2023 (edited)
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Importer
  4. Description: Plugin untuk mengambil data dari URL dan menyimpannya sebagai post baru di WordPress.
  5. Version: 1.21
  6. */
  7.  
  8. // Action ketika plugin diaktifkan
  9. register_activation_hook( __FILE__, 'custom_url_data_importer_activate' );
  10.  
  11. function custom_url_data_importer_activate() {
  12.     // Buat tabel kustom untuk menyimpan data URL
  13.     global $wpdb;
  14.     $table_name = $wpdb->prefix . 'custom_urls';
  15.     $charset_collate = $wpdb->get_charset_collate();
  16.     $sql = "CREATE TABLE $table_name (
  17.        id mediumint(9) NOT NULL AUTO_INCREMENT,
  18.        url_list text NOT NULL,
  19.        PRIMARY KEY  (id)
  20.    ) $charset_collate;";
  21.     require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  22.     dbDelta( $sql );
  23. }
  24.  
  25. // Tambahkan menu di dashboard untuk plugin
  26. add_action('admin_menu', 'custom_url_data_importer_menu');
  27.  
  28. function custom_url_data_importer_menu() {
  29.     add_menu_page(
  30.         'NovelRingan',
  31.         'NovelRingan',
  32.         'manage_options',
  33.         'novelringan',
  34.         'custom_url_data_importer_page'
  35.     );
  36. }
  37.  
  38.  
  39.  
  40. function custom_url_data_importer_page() {
  41.     // Tampilkan form untuk mengubah isi $url_list
  42.     ?>
  43.     <div class="wrap">
  44.         <h2>NovelRingan Data Importer</h2>
  45.         <form method="post" action="">
  46.             <label for="url_list"><b>Masukkan URL NovelRingan:</b></label><br>
  47.             <textarea id="url_list" name="url_list"  style=" min-width:300px; max-width:100%;min-height:250px;height:100%;width:100%;"></textarea><br>
  48.             <input type="submit" name="submit_url_list" value="Import Data" style="width:70%;margin: 15px auto; display:block;padding: 8px;background:blue ;color:white;border:2px solid lime;border-radius:5px;">
  49.         </form>
  50.     </div>
  51.     <?php
  52.  
  53.     if ( isset( $_POST['submit_url_list'] ) ) {
  54.         // Jika form disubmit, proses dan simpan data sebagai post baru
  55.         $url_list = $_POST['url_list'];
  56. //echo $url_list;
  57.         $urls = explode("\n", $url_list);
  58.  
  59.         foreach ($urls as $url) {
  60.             $data = getDataFromUrl(trim($url));
  61.             $post_data = array(
  62.                 'post_title'    => $data['title'],
  63.                 'post_content'  => $data['content'],
  64.                 'post_status'   => 'publish',
  65.                 'post_author'   => 1
  66.             );
  67.             wp_insert_post( $post_data );
  68.         }
  69.         echo "<script type='text/javascript'> window.addEventListener('load', (event) => {
  70.  alert('SUKSES MENGIMPORT KAH?');
  71. });</script>";
  72.     }
  73. }
  74.  
  75. // Fungsi untuk mengambil judul dan konten dari URL
  76. function getDataFromUrl($url) {
  77.     $ch = curl_init();
  78.     curl_setopt($ch, CURLOPT_URL, $url);
  79. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  80. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  81.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  82.     $output = curl_exec($ch);
  83.     curl_close($ch);
  84.  
  85.     // Mengambil judul dari tag <title>
  86.     preg_match("/<title>(.*)<\/title>/i", $output, $title);
  87. $newTitle = str_replace('Bahasa Indonesia - Novel Ringan', '', $title);
  88. echo '<br/> #'.$newTitle[1];
  89.     // Mengambil konten dari <div id="nr-nv-content">
  90.     preg_match("/<div id=\"nr-nv-content\">(.*?)<div class=\"nextprev\" id=\"add-h/is", $output, $content);
  91. $konten2 = preg_replace('/<div class="klm_media".*?<\/div>/', '', $content);
  92. //echo $content[1];
  93. $konten3 = preg_replace('/<(?!h1|\/h1|p|\/p|hr)[^>]*>.*?<\/[^>]*>/s', '', $konten2);
  94.     // Mengembalikan judul dan konten
  95.     return array("title" => $newTitle[1], "content" => $konten3[1]);
  96. }
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement