james1075/ultimate-manga-scraper
0
1<?php2 3if (!defined('ABSPATH')) {4 exit; // Exit if accessed directly.5}6 7/*8 * Class UMS_Madara_Handler9 * Handles the admin interface and AJAX actions for the Ultimate Manga Scraper: Madara Enhancements plugin.10 */11class UMS_Madara_Handler {12 // Initialize hooks and actions13 public static function init() {14 add_action('wp_ajax_load_more_manga', [__CLASS__, 'load_more_manga']); // Handle AJAX request for loading more manga15 add_action('wp_ajax_add_manga', [__CLASS__, 'add_manga']); // Handle AJAX request for adding manga16 add_action('wp_ajax_search_manga', [__CLASS__, 'search_manga']); // Handle AJAX request for searching manga17 add_action('wp_ajax_save_manga_fetch_url', [__CLASS__, 'save_manga_fetch_url']); // Handle AJAX request for saving manga fetch URL18 }19 20 21 // Display the admin page22 public static function ums_enhancements() {23 // Clear the madara_manga_list option on first load24 update_option('madara_manga_list', []); // This empties the madara_manga_list25 $manga_list = get_option('madara_manga_list', []); // Get the list of manga26 $existing_manga_urls = array_map('trim', array_column(get_option('ums_manga_generic_list', []), 0)); // Get the list of existing manga URLs27 28 // Filter out existing manga from the list29 $filtered_manga_list = array_filter($manga_list, function($manga) use ($existing_manga_urls) {30 return !in_array(trim($manga['url']), $existing_manga_urls);31 });32 33 $manga_fetch_url = get_option('manga_fetch_url', 'https://manhuaus.com/wp-admin/admin-ajax.php'); // Get the current manga fetch URL34 35 ?>36 <div class="wrap">37 <h1>Ultimate Manga Scraper: Madara Enhancements</h1>38 <p>Credits to ThuGie</p>39 <!-- URL Settings Form -->40 <form id="url-settings-form">41 <h2>Set Manga Fetch URL</h2>42 <table class="form-table">43 <tr>44 <th scope="row"><label for="manga-fetch-url">Manga Fetch URL</label></th>45 <td><input name="manga-fetch-url" type="text" id="manga-fetch-url" value="<?php echo esc_attr($manga_fetch_url); ?>" class="regular-text" /></td>46 </tr>47 </table>48 <button type="button" class="button button-primary" id="save-url-settings">Save URL</button>49 </form>50 51 <!-- Search Form -->52 <hr />53 <form id="search-form">54 <h2>Search Manga</h2>55 <label for="search-query">Search Query</label>56 <input type="text" id="search-query" name="search-query" class="regular-text" />57 <label for="search-type">Search Type</label>58 <select id="search-type" name="search-type">59 <option value="new">New</option>60 <option value="latest">Latest</option>61 <option value="trending">Trending</option>62 <option value="most_viewed">Most Viewed</option>63 <option value="rating">Rating</option>64 <option value="a_z">A-Z</option>65 <option value="relevance">Relevance</option>66 </select>67 <button type="button" class="button button-secondary" id="search-button">Search</button>68 </form>69 70 <!-- Manga List -->71 <hr />72 <div>73 <label for="auto-load-more">74 <input type="checkbox" id="auto-load-more" /> Enable Auto Load More75 </label>76 <p id="manga-count">Manga Loaded: 0</p>77 </div>78 <table class="widefat" id="manga-table">79 <thead>80 <tr>81 <th>#</th>82 <th>Title</th>83 <th>Cover Image</th>84 <th>Description</th>85 <th>Genres</th>86 <th>Status</th>87 <th>Last Updated</th>88 <th>Latest Chapter</th>89 <th>Actions</th>90 </tr>91 </thead>92 <tbody>93 <?php foreach ($filtered_manga_list as $index => $manga): ?>94 <tr>95 <td><?php echo esc_html($index + 1); ?></td>96 <td><?php echo esc_html($manga['title']); ?></td>97 <td><img src="<?php echo esc_url($manga['cover_image']); ?>" alt="<?php echo esc_attr($manga['title']); ?>" width="100"></td>98 <td><?php echo esc_html($manga['description']); ?></td>99 <td><?php echo esc_html($manga['genres']); ?></td>100 <td><?php echo esc_html($manga['status']); ?></td>101 <td><?php echo esc_html($manga['last_updated']); ?></td>102 <td><?php echo esc_html($manga['latest_chapter']); ?></td>103 <td>104 <form method="post" class="add-manga-form">105 <input type="hidden" name="add_manga_url" value="<?php echo esc_attr($manga['url']); ?>">106 <button type="submit" class="button button-primary">Add Manga</button>107 </form>108 </td>109 </tr>110 <?php endforeach; ?>111 </tbody>112 </table>113 <button id="load-more" class="button button-secondary">Load More</button> <!-- Load More Button -->114 </div>115 <?php116 }117 118 // Handle AJAX request to load more manga119 public static function load_more_manga() {120 check_ajax_referer('madara_enhancements_nonce', '_ajax_nonce'); // Verify nonce for security121 122 $page = isset($_POST['page']) ? intval($_POST['page']) : 0; // Get the current page123 $query = isset($_POST['query']) ? sanitize_text_field($_POST['query']) : ''; // Get the search query124 $type = isset($_POST['type']) ? sanitize_text_field($_POST['type']) : ''; // Get the search type125 126 $manga_list = UMS_Madara_Fetcher::get_manga_data($page, $query, $type); // Fetch manga data127 128 $existing_manga_urls = array_map('trim', array_column(get_option('ums_manga_generic_list', []), 0)); // Get the list of existing manga URLs129 130 // Filter out existing manga from the list131 $filtered_manga_list = array_filter($manga_list, function($manga) use ($existing_manga_urls) {132 return !in_array(trim($manga['url']), $existing_manga_urls);133 });134 135 if (isset($manga_list['error'])) {136 wp_send_json_error($manga_list['error']); // Send error response if there's an error137 } else {138 // Append new manga to the existing list139 $current_manga_list = get_option('madara_manga_list', []);140 $updated_manga_list = array_merge($current_manga_list, $filtered_manga_list);141 update_option('madara_manga_list', $updated_manga_list); // Update the option with the latest manga list142 wp_send_json_success($filtered_manga_list); // Send success response with the filtered manga list143 }144 }145 146 // Handle AJAX request to add manga147 public static function add_manga() {148 check_ajax_referer('madara_enhancements_nonce', '_ajax_nonce'); // Verify nonce for security149 150 $url = isset($_POST['add_manga_url']) ? sanitize_text_field($_POST['add_manga_url']) : null; // Get the manga URL151 152 $manga_list = get_option('madara_manga_list', []); // Get the current manga list153 154 if ($url !== null) {155 $manga_to_add = array_filter($manga_list, function ($manga) use ($url) {156 return $manga['url'] === $url;157 });158 159 if (!empty($manga_to_add)) {160 $manga_to_add = array_values($manga_to_add)[0];161 162 // Check if the manga already exists in the saved rules163 $rules = get_option('ums_manga_generic_list', []);164 foreach ($rules as $rule) {165 if (trim($rule[0]) === trim($manga_to_add['url'])) {166 wp_send_json_error('Manga already exists.');167 return;168 }169 }170 171 self::save_manga_rules($manga_to_add); // Save manga rules172 173 // Remove the manga from the list after adding174 $manga_list = array_filter($manga_list, function ($manga) use ($url) {175 return $manga['url'] !== $url;176 });177 update_option('madara_manga_list', $manga_list); // Update the manga list178 179 wp_send_json_success(); // Send success response180 } else {181 wp_send_json_error('Manga not found.'); // Send error response if manga not found182 }183 } else {184 wp_send_json_error('Invalid request.'); // Send error response if the request is invalid185 }186 }187 188 // Save manga rules189 public static function save_manga_rules($manga) {190 // Clear the cache for the option191 $GLOBALS['wp_object_cache']->delete('ums_manga_generic_list', 'options');192 193 // Get current rules194 $rules = get_option('ums_manga_generic_list', []);195 196 // Validate the schedule format197 $schedule = '24';198 if (!is_numeric($schedule) || $schedule <= 0) {199 $schedule = '24';200 }201 202 $new_rule = [203 trim($manga['url']),204 $schedule,205 '1',206 (new DateTime())->modify('-24 hours -5 minutes')->format('Y-m-d H:i:s'),207 '1000',208 'publish',209 'admin',210 '',211 '',212 'genre',213 'genre',214 '',215 '1',216 '0',217 '1',218 '0',219 '0',220 '0',221 '0',222 ];223 224 $rules[] = $new_rule;225 update_option('ums_manga_generic_list', $rules); // Update the rules option226 }227 228 // Enqueue scripts for the admin page229 public static function enqueue_scripts() {230 wp_enqueue_script('madara-enhancements', plugin_dir_url(__FILE__) . '../assets/js/madara-enhancements.js', ['jquery'], null, true);231 wp_localize_script('madara-enhancements', 'madaraEnhancements', [232 'ajaxUrl' => admin_url('admin-ajax.php'),233 'nonce' => wp_create_nonce('madara_enhancements_nonce'),234 ]);235 }236 237 // Handle AJAX request to search manga238// Handle AJAX request to search manga239public static function search_manga() {240 check_ajax_referer('madara_enhancements_nonce', '_ajax_nonce'); // Verify nonce for security241 242 $query = isset($_POST['query']) ? sanitize_text_field($_POST['query']) : ''; // Get the search query243 $type = isset($_POST['type']) ? sanitize_text_field($_POST['type']) : ''; // Get the search type244 $page = isset($_POST['page']) ? intval($_POST['page']) : 0; // Get the page number245 246 // Fetch manga data from the URL247 $manga_list = UMS_Madara_Fetcher::get_manga_data($page, $query, $type);248 249 // Check if there was an error fetching the manga data250 if (isset($manga_list['error'])) {251 wp_send_json_error($manga_list['error']); // Send error response if there's an error252 return;253 }254 255 $existing_manga_urls = array_map('trim', array_column(get_option('ums_manga_generic_list', []), 0)); // Get the list of existing manga URLs256 257 // Filter out existing manga from the list258 $filtered_manga_list = array_filter($manga_list, function($manga) use ($existing_manga_urls) {259 return !in_array(trim($manga['url']), $existing_manga_urls);260 });261 262 // Get the current madara_manga_list263 $current_manga_list = get_option('madara_manga_list', []);264 // Merge the current list with the new filtered list265 $updated_manga_list = array_merge($current_manga_list, $filtered_manga_list);266 // Update the madara_manga_list with the combined list267 update_option('madara_manga_list', $updated_manga_list);268 269 wp_send_json_success(array_values($filtered_manga_list)); // Send success response with the filtered manga list270}271 272 // Handle AJAX request to save the manga fetch URL273 public static function save_manga_fetch_url() {274 check_ajax_referer('madara_enhancements_nonce', '_ajax_nonce'); // Verify nonce for security275 276 $manga_fetch_url = isset($_POST['manga_fetch_url']) ? esc_url_raw($_POST['manga_fetch_url']) : ''; // Get the manga fetch URL277 278 if ($manga_fetch_url) {279 update_option('manga_fetch_url', $manga_fetch_url); // Update the manga fetch URL option280 wp_send_json_success(); // Send success response281 } else {282 wp_send_json_error('Invalid URL.'); // Send error response if the URL is invalid283 }284 }285}