· 6 years ago · Mar 24, 2019, 10:36 AM
1<?php
2/*
3Plugin Name: GameStarRocks Games Import plugin
4Description:
5Plugin URI: http://localhost
6Author URI: http://localhost
7Author: Dmitriy Proskurin
8License: Public Domain
9Version: 1.0
10*/
11/*
12
13
14
15
16Includes
17
18
19
20
21*/
22require_once("functions.php");
23/*
24
25
26
27Admin page
28
29
30
31*/
32/**
33 * [callback] Spawns link to the plugin settings on admin menu
34 *
35 * @return VOID | html
36 *
37 */
38function gamestarrocks_import_plugin_menu_item()
39{
40
41 add_menu_page("GameStarRocks Game Import Plugin", "Import Games", 'activate_plugins',
42 'GameStarRocks-Game-Import', 'game_star_rocks_games_import_plugin_settings_page_handler');
43}
44/* hook to attach callback to the admin menu */
45add_action('admin_menu', 'gamestarrocks_import_plugin_menu_item');
46
47/**
48 * [callback] Renders plugin settings page on plugin page
49 *
50 * @return VOID | html
51 *
52 */
53function game_star_rocks_games_import_plugin_settings_page_handler()
54{
55 global $wpdb;
56 global $sites_table_name;
57 global $options_table_name;
58 global $post_types_table_name;
59 $notice = "";
60 $notice_type = "notice-error";
61 $display_notice = "none";
62 // if nonce is not set then we just open the page
63 // otherwise if nonce is set then we have a POST request to change settings
64 if (isset($_REQUEST['nonce']) && wp_verify_nonce($_REQUEST['nonce'], basename(__FILE__)) < 3)
65 {
66 if(isset($_REQUEST["show_untranslated_games"]))
67 {
68 global $wpdb;
69 $main_query = new WP_Query(array(
70 'post_type' => 'game',
71 'post_status' => 'any',
72 'posts_per_page' => -1,
73 'tax_query' => array(
74 array(
75 'taxonomy' => 'game-error',
76 'terms' => 'no-image',
77 'field' => 'slug',
78 'include_children' => true,
79 'operator' => 'NOT IN'
80 )
81 )
82 ));
83 $posts_on_main = $main_query->posts;
84 $sites = get_sites();
85 switch_to_blog($sites[1]->blog_id);
86 $second_query = new WP_Query(array(
87 'post_type' => 'game',
88 'post_status' => 'any',
89 'posts_per_page' => -1
90 ));
91 restore_current_blog();
92 $posts_on_second = $second_query->posts;
93 $search_by_property = function($array,$property_val)
94 {
95 foreach($array as $array_element)
96 {
97 if($array_element->post_title == $property_val)
98 {
99 return $array_element;
100 }
101 }
102 return null;
103 };
104 foreach($posts_on_second as $post)
105 {
106 $object = $search_by_property($posts_on_main,$post->post_title);
107 if($object != null)
108 {
109 if (($key = array_search($object, $posts_on_main)) !== false) {
110 unset($posts_on_main[$key]);
111 }
112 }
113 }
114 echo '<table border="1" style="width:100%">';
115 echo '<tr>
116 <th>ID</th>
117 <th>Title</th>
118 <th>Count (by title)</th>
119 <th>LINK</th>
120 </tr>';
121 foreach($posts_on_main as $post)
122 {
123 echo "<tr>";
124 echo "<td>".$post->ID."</td>";
125 echo "<td>".$post->post_title."</td>";
126 $count = $wpdb->get_var("SELECT COUNT(*) FROM ".$wpdb->prefix."posts WHERE post_type='game' AND post_title='".$post->post_title."'");
127 if($count > 0)
128 {
129 echo "<td>".$count."</td>";
130 }
131 else
132 {
133 echo "<td>0</td>";
134 }
135 echo "<td><a href='".get_post_permalink($post->ID)."'>CLICK </a></td>";
136 echo "</tr>";
137 }
138 echo '</table>';
139 }
140 // then we have $_POST request
141 else if(isset($_FILES["json_file"]) && strlen($_FILES["json_file"]["tmp_name"]) > 0)
142 {
143 //check if it's json
144 $allowed = array('json');
145 $filename = $_FILES['json_file']['name'];
146 $extension = pathinfo($filename, PATHINFO_EXTENSION);
147 if(in_array($extension,$allowed) )
148 {
149 // verify that it's correct json string
150 $json_string = file_get_contents($_FILES["json_file"]["tmp_name"]);
151 $json_array = json_decode($json_string);
152 if(!is_null($json_array))
153 {
154 if(!is_null($json_array->games))
155 {
156 $games = $json_array->games;
157 $counter = 0;
158 $error_flag = false;
159 foreach($games as $gamekey => $game)
160 {
161 if(is_object($game))
162 {
163 $game_desc_error = false;
164
165 $game_name = $game->name;
166 $game_desc = $game->description;
167 if(strpos(get_locale(), "en_") === false)
168 {
169 if(function_exists("get_blog_locale") && function_exists("google_translate"))
170 {
171 $game_desc = google_translate($game_desc,get_blog_locale(get_current_blog_id()));
172 }
173 }
174 if(strlen($game_desc) < 10)
175 {
176 $game_desc = "Description.";
177 $game_desc_error = true;
178 // add taxonomy
179 }
180 // custom meta / taxonomies
181 $game_src = $game->source;
182 $game_iframe = $game->location;
183 $game_dimensions = $game->dimensions;
184 $game_cats = $game->category;
185 $game_tags = $game->tags;
186 $game_company = $game->companyName;
187 $game_id = $gamekey;
188
189 // check if such post exist, anti - duplicate
190 $posts_with_meta = new WP_Query( array(
191 'posts_per_page' => 1,
192 'meta_key' => 'game_id_custom_field',
193 'meta_value' => $game_id,
194 'fields' => 'ids',
195 'post_type' => 'game'
196 ) );
197 if(!$posts_with_meta->have_posts())
198 {
199 //insert post
200 $new_game_data = array(
201 'post_title' => wp_strip_all_tags($game_name),
202 'post_content' => $game_desc,
203 'post_status' => 'publish',
204 'post_type' => 'game'
205 );
206 $new_game_id = wp_insert_post($new_game_data);
207 // save all custom data related to the game
208 if($new_game_id != 0)
209 {
210 if($game_desc_error)
211 {
212 custom_set_post_taxonomy($new_game_id,"No Description","game-error");
213 }
214 // insert tags/cats/custom meta
215 import_plugin_insert_game_meta_and_taxonomy($new_game_id,$game_iframe,$game_dimensions,$game_id,$game_cats,$game_src,$game_tags,$game_company);
216 $counter++;
217 }
218 else
219 {
220 $display_notice = "block";
221 $notice = "Unable to insert game. -> " . $game_id . "<br>Name:".$game_name . "<br>Description:".$game_desc;
222 $error_flag = true;
223 break;
224 }
225 }
226 }
227 }
228 if(!$error_flag)
229 {
230 $display_notice = "block";
231 $notice = $counter . " new games have been added.";
232 $notice_type = "notice-success";
233 }
234 }
235 }
236 else
237 {
238 $display_notice = "block";
239 $notice = "Invalid JSON input.<br>".esc_html($json_string);
240 }
241 }
242 else
243 {
244 $display_notice = "block";
245 $notice = "Incorrect file type. Allowed: JSON.";
246 }
247 }
248 else if(isset($_FILES["zip_file"]) && strlen($_FILES["zip_file"]["tmp_name"]) > 0 )
249 {
250 $myzip = new ZipArchive;
251 $game_counter = 0;
252 if(strlen($_FILES["zip_file"]["tmp_name"]) > 0)
253 {
254 $myzip->open($_FILES["zip_file"]["tmp_name"]);
255 }
256 else
257 {
258 wp_die("Error, file name could not be empty!");
259 }
260 // Get the path to the upload directory.
261 $wp_upload_dir = wp_upload_dir();
262
263 $unzipped_path = $wp_upload_dir["path"] ."/".'unzipped'; //ABSPATH . 'unzipped';
264 $unzipped_images_container = $unzipped_path . '/images';
265 // create directory with 777 file permissions
266 if(!file_exists($unzipped_path))
267 {
268 mkdir($unzipped_path);
269 }
270 chmod($unzipped_path, 0777);
271 $myzip->extractTo($unzipped_path);
272 $myzip->close();
273 // change file permissions back to 750
274 chmod($unzipped_path, 0750);
275 // check if folder was created
276 if(file_exists($unzipped_path))
277 {
278 if(is_dir($unzipped_path))
279 {
280 if(file_exists($unzipped_images_container))
281 {
282 if(is_dir($unzipped_images_container))
283 {
284 // here we get IDs of games (folder names)
285 $dirs = new DirectoryIterator($unzipped_images_container);
286 foreach ($dirs as $file_info)
287 {
288 if ($file_info->isDir() && !$file_info->isDot())
289 {
290 // get all folder contents
291 $current_game_id = $file_info->getFilename();
292 $current_path = $file_info->getPathname();
293 // get array of images in folder
294 $file_names_in_folder = scandir($current_path);
295 // get current game by its ID (custom meta key value)
296 // $current_game = WP_POST object
297 $current_game = new WP_Query(array(
298 'post_type' => 'game',
299 'post_status' => 'any',
300 'meta_query' => array(
301 'relation' => 'OR',
302 array(
303 'relation' => 'AND',
304 array(
305 'key' => 'logo_custom_image',
306 'compare' => 'NOT EXISTS'
307 ),
308 array(
309 'key' => 'game_id_custom_field',
310 'value' => $current_game_id,
311 'compare' => '='
312 )
313 ),
314 array(
315 'relation' => 'AND',
316 array(
317 'key' => 'logo_custom_image',
318 'value' => '',
319 'compare' => '='
320 ),
321 array(
322 'key' => 'game_id_custom_field',
323 'value' => $current_game_id,
324 'compare' => '='
325 )
326 )
327 )
328 )
329 );
330 $current_game = $current_game->posts[0];
331 // for some fails
332 //wp_delete_post( $current_game->ID, true );
333 //$current_game = null;
334
335 if(!is_null($current_game) && strlen($current_game->post_title) > 0)
336 {
337 $logo_set_flag = false;
338 $closest_to_needed_size = 0;
339 $closest_to_needed_size_bigger = 0;
340 $any_available = 0;
341 foreach($file_names_in_folder as $file)
342 {
343 $current_file_path = $current_path . "/" . $file;
344 $imagesize = getimagesize($current_file_path);
345 if(is_array($imagesize))
346 {
347 // upload image to wordpress media gallery
348 $attachment_id = upload_image_to_wp_gallery($current_file_path,false,$current_game->ID);
349 if($attachment_id != 0)
350 {
351 // set custom taxonomies - game name and game id
352 custom_set_post_taxonomy($attachment_id,$current_game_id,"attachment-gameid");
353
354 custom_set_post_taxonomy($attachment_id,$current_game->post_title,"attachment-gamename");
355
356 // LOCK TO 512x384
357 // check if input file is correct image and find the biggest image
358 if($imagesize[0] == 512 && $imagesize[1] == 384)
359 {
360 if(!$logo_set_flag)
361 {
362 // remove network site / site link from url
363 set_logo_image($attachment_id,$current_game);
364 $logo_set_flag = true;
365 }
366 }
367 else
368 {
369 if(!$logo_set_flag && $imagesize[0] != 0 && $imagesize[1] != 0)
370 {
371 if($imagesize[0] > 400 && $imagesize[0] < 650 &&
372 $imagesize[1] > 300 && $imagesize[1] < 550)
373 {
374 $closest_to_needed_size = $attachment_id;
375 }
376 else if($imagesize[0] > 150 && $imagesize[0] < 750 &&
377 $imagesize[1] > 150 && $imagesize[1] < 750)
378 {
379 $closest_to_needed_size_bigger = $attachment_id;
380 }
381 else
382 {
383 // set any available
384 $any_available = $attachment_id;
385 }
386 }
387 }
388 add_post_meta($current_game->ID,'imported_media_successfuly',"true",true);
389 }
390 }
391 }
392 // check if logo has been set
393 if(!$logo_set_flag)
394 {
395 //we should try closest image
396 if($closest_to_needed_size != 0)
397 {
398 set_logo_image($closest_to_needed_size,$current_game);
399 $logo_set_flag = true;
400 }
401 else if($closest_to_needed_size_bigger != 0)
402 {
403 set_logo_image($closest_to_needed_size_bigger,$current_game);
404 $logo_set_flag = true;
405 }
406 else if($any_available != 0)
407 {
408 set_logo_image($any_available,$current_game);
409 $logo_set_flag = true;
410 }
411 }
412 // check if logo is set or still not
413 if(!$logo_set_flag)
414 {
415 custom_set_post_taxonomy($current_game->ID,"No Image","game-error");
416 }
417 // check if translator plugin is installed
418 if(function_exists("translate_post_from_any_to_any") && $logo_set_flag)
419 {
420 if(isset($_REQUEST["translate_games"]))
421 {
422 translate_post_from_any_to_any($current_game);
423 }
424 }
425 $game_counter++;
426 }
427 }
428 }
429 }
430 else
431 {
432 $display_notice = "block";
433 $notice = "Images is not a folder.";
434 }
435 }
436 else
437 {
438 $display_notice = "block";
439 $notice = "Images folder not found.";
440 }
441 }
442 else
443 {
444 $display_notice = "block";
445 $notice = "Unzipped is not a folder.";
446 }
447 }
448 else
449 {
450 $display_notice = "block";
451 $notice = "Unzipped folder not found.";
452 }
453 // remove all temporary uploaded contents
454 rrmdir($unzipped_path);
455
456 $display_notice = "block";
457 $notice = $game_counter. " games have been updated.";
458 $notice_type = "notice-success";
459 }
460 }
461 ?>
462<style>
463 .wrap
464 {
465 border:1px dashed silver;
466 padding: 2rem;
467 }
468</style>
469<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
470<div class="wrap">
471 <h2 align="center">Import Games</h2>
472 <div class="notice <?php echo $notice_type; ?>" style="display: <?php echo $display_notice;?>">
473 <?php echo $notice; ?>
474 </div>
475 <form class="my-form" method="POST" id="my-form" enctype="multipart/form-data">
476 <input type="hidden" name="nonce" value="<?php echo wp_create_nonce(basename(__FILE__))?>"/>
477 <div class="form-group">
478 <label for="json_file">Select JSON file with games data to import:</label>
479 <input type="file" class="form-control-file" id="json_file" name="json_file" />
480 </div>
481 <div class="form-group">
482 <label>OR</label><br/>
483 <label for="zip_file">Select ZIP file with games images to import:</label>
484 <input type="file" class="form-control-file" id="zip_file" name="zip_file" />
485 <input type="checkbox" class="form-control" id="translate_games" checked name="translate_games">
486 <label for="translate_games">Translate games</label>
487 </div>
488 <button type="submit" name="save_btn" id="save_btn" class="btn btn-primary">Submit</button>
489 </form>
490</div>
491<div class="wrap">
492 <h2 align="center">OTHER</h2>
493 <form class="my-form" method="POST" id="my-form" enctype="multipart/form-data">
494 <input type="hidden" name="nonce" value="<?php echo wp_create_nonce(basename(__FILE__))?>"/>
495 <input type="checkbox" class="form-control" id="show_untranslated_games" name="show_untranslated_games">
496 <label for="show_untranslated_games">Show list of untranslated games(that exist on main site and don't exist on other sites)</label><br>
497 <button type="submit" name="save_btn" id="save_btn" class="btn btn-primary">Submit</button>
498 </form>
499</div>
500<?php
501}
502?>