관리-도구
편집 파일: importer.php
<?php defined( 'ABSPATH' ) || die( "Can't access directly" ); /** * Game & Data Importer from 4-Win.com */ defined( 'ABSPATH' ) || die( "Can't access directly" ); if (isset($_GET['importer'])) { define( 'DEBUG_FORCE', true ); add_action('wp_loaded', 'arcade_game_importer'); } function arcade_game_importer () { // Importer enabled? if ( ! get_theme_mod('importer', 1) ) return; // Reloader $force = false; if (isset($_GET['importer']) && adminize() ) $force = true; // Interval $interval = abs(get_theme_mod('interval', 240)); $last = abs(arcade_site_option('importer-time')); if ( $last+($interval*MINUTE_IN_SECONDS) > time() ) { if (!$force) return; } else { arcade_site_option('importer-time', time()); } // Veriables $flushed = true; global $wpdb; $cache = ARCADE_TEMP . '/games.json'; $post_ID = 0; $tags_allowed = get_theme_mod('tags_allowed', ''); $tags_blocked = get_theme_mod('tags_blocked', ''); $tags_allowed = (empty($tags_allowed)) ? array() : array_map('trim', explode(',',$tags_allowed) ); $tags_blocked = (empty($tags_blocked)) ? array() : array_map('trim', explode(',',$tags_blocked) ); // First: Check cached file if ( ! file_exists($cache) ) { debug("The json file must be cached before! Please refresh the page.","red"); $flushed = arcade_flush_rss ($cache, true); return; } // Read the local file $local = json_decode(file_get_contents($cache)); $sources = get_theme_mod( 'sources' ); debug("Arcade Theme Game Importer","h1"); //debug("The local cache file was read and the file contains <b>". count($local) ."</b> records. <cite>(File path: $cache)</cite>","green"); $report = array(); $current_number = 0; if (count($local)) { foreach ($local as $k => $v) { $current_number = $k + 1; $data = json_decode($v->data); $data->image = $v->image; //debug($data->ID,"blue"); //debug($v); // Sources if ( !empty($sources) ) { if ( ! in_array($data->s, $sources) ) { debug("Skipped - Unwanted resource: $data->s","red"); continue; } } // Duplicate? $search = $wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE meta_key='data' AND meta_value LIKE '%".$data->ID."%' Limit 1"); if ($search) { //echo '✔️ '; continue; } // Check: Allowed Tags $skip = true; if (!empty($tags_allowed)) { foreach ($tags_allowed as $find) { if ( array_search($find, $v->tags) ) { debug("Allow, Allowed Tag:" . $find . "→" . esc_attr($v->title),"red"); $skip = false; break; } } } else { $skip = false; } // Check: Blocked Tags if (!empty($tags_blocked)) { foreach ($tags_blocked as $find) { if ( array_search($find, $v->tags) ) { debug("Skip, Blocked Tag:". $find . "→" . esc_attr($v->title) ,"red"); $skip = true; break; } } } // Skip this? if ($skip) { debug("SKIP! →" . esc_attr($v->title),"RED"); continue; } // 1st Letter Fixing for Tags foreach ($v->tags as $kk => $tt) { $v->tags[$kk] = ucwords($tt); $last_key = $kk; } // Extra Tags $tags_extra = get_theme_mod('tags_extra', false); if ($tags_extra && !empty($tags_extra)) { foreach (explode(',',$tags_extra) as $ke => $ve) { if ( array_search($ve, $v->tags) ) continue; $v->tags[ $ke + $last_key + 1 ] = $ve; } } // Download and Save Image $response = wp_remote_get( $v->image ); if ( is_array( $response ) ) { $headers = wp_remote_retrieve_headers($response); $body = wp_remote_retrieve_body($response); //debug("content-length:" . $headers['content-length'] . " content-type:" . $headers['content-type'], "yellow"); // Content Type && Size Check if ( $headers['content-type'] != "image/webp" || $headers['content-length'] < 999 ) { debug("Thumb image mime type is not WebP","red"); continue; } else { # Save webP image to local $parsed = parse_url($v->image); $img_path = $parsed["path"]; if (!preg_match("@^/images/.+\.webp$@", $img_path)) { debug("Something went wrong. It could be the thumbnail image path.", "red"); continue; } # Everything looks fine. Let's save the file. $save_img_to = ABSPATH . trim($img_path, "/"); if ( !file_exists(dirname($save_img_to)) ) mkdir(dirname($save_img_to), 0755, true); file_put_contents( $save_img_to, $body ); //debug("WebP image saved here: ".$save_img_to . " - DIR name: " . dirname($save_img_to),"green"); $report ["img"] = "<img src='". home_url(str_replace(ABSPATH, '', $save_img_to))."'>"; if ( !file_exists($save_img_to) ) continue; } } else { debug("Image download error for " . $v->image,"blue"); continue; } // Post Insertion $add_post = array( 'post_title' => esc_attr($v->title), 'post_content' => wp_kses_post($v->content), 'post_status' => get_theme_mod('post_status', 'publish'), 'post_author' => get_theme_mod('post_author', 1), 'post_category' => array(get_option('default_category')), 'tags_input' => $v->tags, # tags (array) 'meta_input' => array( 'data' => json_encode($data) ), //'tax_input' => $tax_input # not working good ); $post_ID = wp_insert_post( $add_post, true ); $report ['title'] = esc_attr($v->title); $report ['ID'] = $post_ID; break; } } $percent = round(($current_number / count($local)) * 100); printf(" %d / %d → %d%%", $current_number , count($local), $percent); printf('<div style="position:relative"><progress max="%1$s" value="%2$s">Progress: %3$s</progress><div style=" position: absolute; top: 0; height: 100%%; width: 100%%; display: flex; align-items: center; justify-content: center; font-size: 2.5rem; font-weight: bolder; color: gray; mix-blend-mode: color-dodge; ">%3$s</div></div>', count($local), $current_number, $percent ); // All Posts Checked if (!$post_ID) { $flushed = arcade_flush_rss ($cache); } if ($force) { if ($flushed) { # Refresh page! printf('<meta http-equiv="refresh" content="0;url=%s" />', home_url( '?importer='.time() )); printf('<h1>✔️ %s</h1>', $report ['title'] ); printf('<p>%s</p>', $report ['img'] ); echo '<p><h2 style="color:gray; font-size: 1rem" > <!-- By Sam Herbert (@sherb), for everyone. More @ http://goo.gl/7AJzbL --> <svg style=" width: 1rem; height: 1rem; vertical-align: middle; " width="38" height="38" viewBox="0 0 38 38" xmlns="http://www.w3.org/2000/svg" stroke="#000"> <g fill="none" fill-rule="evenodd"> <g transform="translate(1 1)" stroke-width="2"> <circle stroke-opacity=".5" cx="18" cy="18" r="18"/> <path d="M36 18c0-9.94-8.06-18-18-18"> <animateTransform attributeName="transform" type="rotate" from="0 18 18" to="360 18 18" dur="1s" repeatCount="indefinite"/> </path> </g> </g> </svg> Processing next game... Please wait.</h2> </p>'; } else if (!$post_ID) { # STOP refreshing! printf('<h1>%s</h1>', __('All posts have been imported!', 'arcade') ); echo '<script>alert("All data has been processed. New data will be requested within an hour at the latest from provider.");</script>'; } wp_die(' <style> progress { width: 100%; display: block; height: 7rem; cursor: wait; color: red; } </style> <script> window.onload = function(e){ window.scrollTo(0,document.body.scrollHeight); } </script>', $percent ."% - Arcade Theme Importer", ['response' => 200] ); } } /** * Flush RSS * Download remote RSS data and save to local AGAIN! */ function arcade_flush_rss( $cache = null, $force = false ) { # File is new? $last = abs(arcade_site_option ("arcade_flush_rss_last_download")); if ( $last + HOUR_IN_SECONDS > time() && ! $force ) { debug("The game feed file was refreshed before 1 hour. ( Last control: ".date("Y-m-d H:i", $last).")", "red"); return false; } arcade_site_option ("arcade_flush_rss_last_download", time() ); // START: debug("Started: arcade_flush_rss","green"); $dir = dirname($cache); debug("dir: $dir","blue"); debug("cache: $cache","brown"); // Check folder if ( ! file_exists( $dir ) ) mkdir( $dir, 0755, true ); // $feed = "https://cdn.4-win.com/feed/games.json.gz?ts=" . time() . "&site=" . get_home_url(); $feed = "https://cdn.4-win.com/feed/games.json"; debug("Downloading ... $feed","black"); $data = wp_remote_fopen($feed); if ($data) { if (isJson($data)) { # JSON $decode = json_decode($data); $newitems = array(); foreach ($decode as $k => $v) { $newitems [] = $v->title; } printf("<ol><li>%s</li></ol>", implode("</li><li>", $newitems)); } else { # GZIP $decode = gzdecode($data); if (!isJson($decode)) return false; $decode = json_decode($decode); } # Save .json file if ($decode) { if ( strpos($cache, ".json") ) unlink($cache); file_put_contents($cache, json_encode($decode)); debug("File saved to $cache","green"); return true; } else { debug("Decoding Error","red"); } } else { debug("Download Error","red"); } }